cranpose-render-wgpu 0.9.11

GPU renderer for Cranpose scenes through wgpu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use super::{
    FrameTextureDescriptor, MIN_RETAINED_TRANSIENT_BYTES, MIN_UPLOAD_BUFFER_BYTES, UploadPlacement,
    WgpuFrameGraph, WgpuFrameGraphExecutor, build_pass_schedule, place_upload, ring_outlives_frame,
};
use crate::{idle_pool::IDLE_FRAMES, offscreen::OffscreenTarget};

#[test]
fn a_transient_texture_no_frame_reuses_is_dropped_after_the_idle_frames() {
    let (_lock, device, _queue) = super::upload_test_device();
    let format = wgpu::TextureFormat::Rgba8Unorm;
    let descriptor = FrameTextureDescriptor::render_attachment("idle test", 8, 8, format);
    let mut executor = WgpuFrameGraphExecutor::default();
    executor.return_cached_transient(descriptor, OffscreenTarget::new(&device, format, 8, 8));
    for _ in 0..IDLE_FRAMES {
        executor.end_transient_frame();
    }
    assert_eq!(executor.retained_texture_count(), 1);
    executor.end_transient_frame();
    assert_eq!(executor.retained_texture_count(), 0);
    assert_eq!(executor.retained_texture_bytes(), 0);
}

const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;

fn exact(width: u32, height: u32) -> FrameTextureDescriptor {
    FrameTextureDescriptor::render_attachment("exact test", width, height, FORMAT)
}

/// Acquires `descriptor` from the executor's pool, returning the texture and
/// whether the pool had to create it.
fn acquire(
    executor: &mut WgpuFrameGraphExecutor,
    device: &wgpu::Device,
    descriptor: FrameTextureDescriptor,
) -> (crate::offscreen::OffscreenTarget, bool) {
    let target = executor.transient_textures.acquire(device, descriptor);
    let (_, news) = executor.transient_textures.take_counts();
    (target, news > 0)
}

#[test]
fn exact_requests_reuse_only_matching_dimensions() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    executor.return_cached_transient(exact(96, 64), OffscreenTarget::new(&device, FORMAT, 96, 64));
    executor.return_cached_transient(exact(64, 40), OffscreenTarget::new(&device, FORMAT, 64, 40));
    let (target, created) = acquire(&mut executor, &device, exact(64, 40));
    assert!(!created, "the matching size is reused");
    assert_eq!((target.width, target.height), (64, 40));
    let (_, created) = acquire(&mut executor, &device, exact(60, 36));
    assert!(
        created,
        "a larger pooled texture cannot serve a smaller request"
    );
    let (target, created) = acquire(&mut executor, &device, exact(96, 64));
    assert!(!created, "the remaining matching size is reused");
    assert_eq!((target.width, target.height), (96, 64));
}

#[test]
fn an_exact_request_takes_only_its_own_size() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    executor.return_cached_transient(exact(64, 40), OffscreenTarget::new(&device, FORMAT, 64, 40));
    let (target, created) = acquire(&mut executor, &device, exact(60, 36));
    assert!(
        created,
        "a whole-texture reader must not get a larger texture"
    );
    assert_eq!((target.width, target.height), (60, 36));
    let (_, created) = acquire(&mut executor, &device, exact(64, 40));
    assert!(!created);
}

#[test]
fn equal_area_does_not_make_different_dimensions_reusable() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    executor.return_cached_transient(
        exact(128, 64),
        OffscreenTarget::new(&device, FORMAT, 128, 64),
    );
    let (_, created) = acquire(&mut executor, &device, exact(64, 128));
    assert!(
        created,
        "matching total texels do not make dimensions interchangeable"
    );
}

#[test]
fn a_texture_goes_back_to_the_pool_at_its_own_size() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    executor.return_cached_transient(exact(80, 50), OffscreenTarget::new(&device, FORMAT, 96, 64));
    let (target, created) = acquire(&mut executor, &device, exact(96, 64));
    assert!(
        !created,
        "the pool records the target's 96x64 dimensions, not the stale descriptor"
    );
    assert_eq!((target.width, target.height), (96, 64));
}

#[test]
fn the_pool_keeps_a_frame_s_textures_past_the_floor() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    let side = 2048;
    let texture_bytes = u64::from(side) * u64::from(side) * 4;
    let count = MIN_RETAINED_TRANSIENT_BYTES / texture_bytes + 2;
    let descriptors: Vec<_> = (0..count)
        .map(|index| exact(side, side - index as u32))
        .collect();
    let first: Vec<_> = descriptors
        .iter()
        .map(|descriptor| acquire(&mut executor, &device, *descriptor).0)
        .collect();
    for (descriptor, target) in descriptors.iter().zip(first) {
        executor.return_cached_transient(*descriptor, target);
    }
    executor.end_transient_frame();
    assert!(executor.retained_texture_bytes() > MIN_RETAINED_TRANSIENT_BYTES);
    for descriptor in &descriptors {
        let (_, created) = acquire(&mut executor, &device, *descriptor);
        assert!(
            !created,
            "the second frame reuses every texture the first frame used"
        );
    }
}

#[test]
fn a_texture_a_cache_returns_counts_toward_the_frame_s_working_set() {
    let (_lock, device, _queue) = super::upload_test_device();
    let mut executor = WgpuFrameGraphExecutor::default();
    let side = 2048;
    let texture_bytes = u64::from(side) * u64::from(side) * 4;
    let frame_count = MIN_RETAINED_TRANSIENT_BYTES / texture_bytes;
    let frame: Vec<_> = (0..frame_count)
        .map(|index| exact(side, side - index as u32))
        .collect();
    let cached = exact(side, side - frame_count as u32);
    let acquired: Vec<_> = frame
        .iter()
        .map(|descriptor| acquire(&mut executor, &device, *descriptor).0)
        .collect();
    for (descriptor, target) in frame.iter().zip(acquired) {
        executor.transient_textures.release(*descriptor, target);
    }
    executor.return_cached_transient(
        cached,
        OffscreenTarget::new(&device, FORMAT, cached.width, cached.height),
    );
    executor.end_transient_frame();
    for descriptor in frame.iter().chain(std::iter::once(&cached)) {
        let (_, created) = acquire(&mut executor, &device, *descriptor);
        assert!(
            !created,
            "the frame used its own textures and the one its cache held; the pool keeps all"
        );
    }
}

#[test]
fn a_ring_outlives_the_frames_that_fill_a_quarter_of_it() {
    let capacity = 16 * MIN_UPLOAD_BUFFER_BYTES;
    assert!(ring_outlives_frame(capacity, capacity / 4));
    assert!(ring_outlives_frame(capacity, capacity));
    assert!(!ring_outlives_frame(capacity, capacity / 4 - 1));
    assert!(!ring_outlives_frame(capacity, 1));
}

#[test]
fn a_ring_at_the_floor_and_a_ring_after_an_empty_frame_stay() {
    assert!(ring_outlives_frame(MIN_UPLOAD_BUFFER_BYTES, 1));
    assert!(ring_outlives_frame(16 * MIN_UPLOAD_BUFFER_BYTES, 0));
}

#[test]
fn frame_uploads_preserve_bytes_across_growth_and_reset() {
    let (_lock, device, queue) = super::upload_test_device();
    let mut ring = super::UploadRing::new(
        wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
        "Upload Growth Test",
        256,
        true,
    );
    let large = vec![2; MIN_UPLOAD_BUFFER_BYTES as usize * 4 + 4];
    let small = [1; 16];
    let last = [3; 16];
    let mut sources = Vec::new();
    for bytes in [&small[..], &large, &last[..]] {
        let (generation, offset) = ring.upload(&device, bytes.len() as u64, bytes);
        sources.push((ring.generations[generation].buffer.clone(), offset));
    }
    let mut encoder = device.create_command_encoder(&Default::default());
    let mut uploads = super::BufferUploads::default();
    ring.stage_pending(&mut |buffer, offset, bytes| super::FrameCommandStats {
        upload_bytes: uploads.write(&device, Some(&mut encoder), buffer, offset, bytes),
        ..Default::default()
    });
    ring.reset();
    let readback = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: 48,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    for (index, (source, offset)) in sources.into_iter().enumerate() {
        encoder.copy_buffer_to_buffer(&source, offset, &readback, index as u64 * 16, 16);
    }
    uploads.finish();
    let submission = queue.submit([encoder.finish()]);
    uploads.recall();
    assert_eq!(
        super::read_uploaded_bytes(&device, &readback, submission),
        [&small[..], &large[..16], &last[..]].concat()
    );
}

#[test]
fn vertex_uploads_written_as_they_come_land_whole_across_growth() {
    let (_lock, device, queue) = super::upload_test_device();
    // The frame's rings, readable back for the test.
    let readable = wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST;
    let mut allocators = super::FrameUploadAllocators {
        rings: [
            super::UploadRing::new(readable, "Uniforms", 256, true),
            super::UploadRing::new(readable, "Vertices", wgpu::COPY_BUFFER_ALIGNMENT, false),
            super::UploadRing::new(readable, "Indices", wgpu::COPY_BUFFER_ALIGNMENT, false),
        ],
        ..super::FrameUploadAllocators::default()
    };
    let odd: Vec<u8> = (1..=13).collect();
    let large = vec![2; MIN_UPLOAD_BUFFER_BYTES as usize * 2];
    let last = [3; 16];
    let spec = super::UploadAllocatorSpec::vertex("Direct Upload Test", 0);
    let uploads: Vec<_> = [&odd[..], &large, &last[..]]
        .into_iter()
        .map(|bytes| allocators.upload_buffer(spec, &device, bytes))
        .collect();
    let stats = allocators.stage_pending(|_, _, _| super::FrameCommandStats::default());
    assert_eq!(stats.upload_writes, 3, "each upload is written once");
    allocators.buffers.finish();
    let mut encoder = allocators
        .buffers
        .take_before_passes()
        .expect("the uploads were written before the passes");
    let readback = device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: 48,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    for (index, upload) in uploads.iter().enumerate() {
        encoder.copy_buffer_to_buffer(
            &upload.buffer,
            upload.offset,
            &readback,
            index as u64 * 16,
            16,
        );
    }
    let submission = queue.submit([encoder.finish()]);
    allocators.buffers.recall();
    let mut odd_word = odd;
    odd_word.extend([0, 0, 0]);
    assert_eq!(
        super::read_uploaded_bytes(&device, &readback, submission),
        [&odd_word[..], &large[..16], &last[..]].concat()
    );
}

#[test]
fn an_upload_lands_aligned_after_the_last_one() {
    assert_eq!(
        place_upload(100, 64, 64, 256, Some(4096)),
        UploadPlacement::At(256)
    );
    assert_eq!(
        place_upload(0, 12, 0, 4, Some(4096)),
        UploadPlacement::At(0)
    );
    assert_eq!(
        place_upload(12, 12, 0, 4, Some(4096)),
        UploadPlacement::At(12)
    );
}

#[test]
fn an_upload_past_the_buffer_opens_one_at_least_twice_as_large() {
    assert_eq!(
        place_upload(99_950, 64, 64, 256, Some(100_000)),
        UploadPlacement::Grow(200_000)
    );
    assert_eq!(
        place_upload(0, 64, 64, 256, None),
        UploadPlacement::Grow(MIN_UPLOAD_BUFFER_BYTES)
    );
    assert_eq!(
        place_upload(4000, 64, 64, 256, Some(4096)),
        UploadPlacement::Grow(MIN_UPLOAD_BUFFER_BYTES)
    );
    assert_eq!(
        place_upload(
            0,
            3 * MIN_UPLOAD_BUFFER_BYTES,
            64,
            256,
            Some(MIN_UPLOAD_BUFFER_BYTES)
        ),
        UploadPlacement::Grow(3 * MIN_UPLOAD_BUFFER_BYTES)
    );
}

#[test]
fn a_binding_wider_than_its_bytes_reserves_the_binding() {
    assert_eq!(
        place_upload(98_000, 16, 1024, 64, Some(100_000)),
        UploadPlacement::At(98_048)
    );
    assert_eq!(
        place_upload(99_000, 16, 1024, 64, Some(100_000)),
        UploadPlacement::Grow(200_000)
    );
}

#[test]
fn pass_schedule_orders_reads_after_last_writer() {
    let mut graph = WgpuFrameGraph::new(None);
    let target = graph.import_surface("surface");
    graph.add_fallible_command_pass(Some("writer"), &[], &[target], |_| Ok(()));
    graph.add_fallible_command_pass(Some("independent"), &[], &[], |_| Ok(()));
    graph.add_fallible_command_pass(Some("reader"), &[target], &[], |_| Ok(()));

    let order = build_pass_schedule(&graph.passes).expect("valid pass schedule");
    let writer_index = order
        .iter()
        .position(|index| *index == 0)
        .expect("writer pass should be scheduled");
    let reader_index = order
        .iter()
        .position(|index| *index == 2)
        .expect("reader pass should be scheduled");

    assert!(writer_index < reader_index);
}

#[test]
fn pass_schedule_keeps_later_writes_after_earlier_reads() {
    let mut graph = WgpuFrameGraph::new(None);
    let target = graph.import_surface("surface");
    let dependency = graph.import_surface("dependency");
    graph.add_fallible_command_pass(Some("dependency writer"), &[], &[dependency], |_| Ok(()));
    graph.add_fallible_command_pass(
        Some("target reader"),
        &[target, dependency],
        &[],
        |_| Ok(()),
    );
    graph.add_fallible_command_pass(Some("target writer"), &[], &[target], |_| Ok(()));

    let order = build_pass_schedule(&graph.passes).expect("valid pass schedule");
    let reader_index = order
        .iter()
        .position(|index| *index == 1)
        .expect("reader pass should be scheduled");
    let writer_index = order
        .iter()
        .position(|index| *index == 2)
        .expect("writer pass should be scheduled");

    assert!(reader_index < writer_index);
}

#[test]
fn transient_texture_descriptor_clamps_and_accounts_bytes() {
    let descriptor =
        FrameTextureDescriptor::render_attachment("scratch", 0, 2, wgpu::TextureFormat::Bgra8Unorm);

    assert_eq!(descriptor.width, 1);
    assert_eq!(descriptor.height, 2);
    assert_eq!(descriptor.estimated_bytes(), 8);
}

#[test]
fn frame_graph_records_imported_texture_resources() {
    let mut graph = WgpuFrameGraph::new(None);
    let handle = graph.import_surface("surface");

    assert_eq!(handle.0, 0);
    assert_eq!(graph.resources.textures[0].label, "surface");
}

#[test]
fn command_nodes_declare_wgpu_pass_count() {
    let mut graph = WgpuFrameGraph::new(None);
    let source = graph.import_surface("source");
    let dest = graph.import_surface("dest");

    graph.add_fallible_command_pass(Some("copy"), &[source], &[dest], |_| Ok(()));

    assert_eq!(graph.node_count(), 1);
    assert_eq!(graph.declared_pass_count(), 1);
}