sim-engine 0.4.2

A validated high-performance visualization engine for simulations
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Transfer oracle: independent per-draw data, exact queue order and bounded
//! reusable scratch. This is not a full surface/cache-collector test.
use super::*;
use std::time::Duration;
use wgpu::util::DeviceExt;

const INITIAL_COUNT: usize = 1000;
const GROWN_COUNT: usize = 2000;
const BYTE_LIMIT: usize = 256 * 1024;

struct Readback {
    buffer: wgpu::Buffer,
    expected: Vec<u8>,
    tolerance: u8,
}

pub(in crate::renderer) fn assert_gpu_uniform_upload_contract(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
) {
    let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("uniform transfer oracle layout"),
        entries: &[wgpu::BindGroupLayoutEntry {
            binding: 0,
            visibility: wgpu::ShaderStages::FRAGMENT,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        }],
    });
    let pipeline = pixel_pipeline(device, &layout);
    let mut values = (0..GROWN_COUNT)
        .map(|index| value(index, 0))
        .collect::<Vec<_>>();
    let bindings = values
        .iter()
        .map(|bytes| binding(device, &layout, bytes))
        .collect::<Vec<_>>();
    let mut uploads = UniformUploadBatch::default();
    let mut pending = Vec::new();

    // Two submissions share the retained scratch buffer, with no poll/map/wait
    // between them. Every frame captures old/new bytes and actual binding pixels.
    for generation in 1..=2 {
        let old_capacity = uploads.allocation_bytes();
        let bytes = values[..INITIAL_COUNT].iter().map(Vec::len).sum::<usize>();
        uploads.begin();
        assert!(uploads.prepare(
            bytes,
            INITIAL_COUNT,
            BYTE_LIMIT,
            device.limits().max_buffer_size
        ));
        for (index, destination) in values[..INITIAL_COUNT].iter_mut().enumerate() {
            *destination = value(index, generation);
            assert!(uploads.stage(index, destination));
        }
        let mut encoder = device.create_command_encoder(&Default::default());
        let report = uploads.flush(device, queue, &mut encoder, &bindings);
        assert_eq!(report.uploaded_bytes, bytes);
        assert_eq!(report.copy_count, INITIAL_COUNT);
        assert_eq!(report.created_buffers, usize::from(generation == 1));
        assert_eq!(report.buffer_bytes, uploads.allocation_bytes());
        assert!(report.buffer_bytes >= bytes && report.buffer_bytes <= BYTE_LIMIT);
        assert!(report.peak_buffer_bytes >= old_capacity);
        pending.push(capture_uniforms(
            device,
            &mut encoder,
            &bindings[..INITIAL_COUNT],
            &values[..INITIAL_COUNT],
        ));
        pending.push(capture_pixels(
            device,
            &pipeline,
            &mut encoder,
            &bindings[..INITIAL_COUNT],
            &values[..INITIAL_COUNT],
        ));
        queue.submit([encoder.finish()]);
    }
    for readback in pending {
        assert_readback(device, readback);
    }

    // An accepted plan with no changed records must issue no queue transfer.
    uploads.begin();
    assert!(uploads.prepare(
        96 * INITIAL_COUNT,
        INITIAL_COUNT,
        BYTE_LIMIT,
        device.limits().max_buffer_size
    ));
    let mut encoder = device.create_command_encoder(&Default::default());
    let report = uploads.flush(device, queue, &mut encoder, &bindings);
    assert_eq!(
        (
            report.uploaded_bytes,
            report.copy_count,
            report.created_buffers
        ),
        (0, 0, 0)
    );
    queue.submit([encoder.finish()]);

    // Sparse binding indices are not record indices: 32 copies may target index
    // 999. First, middle and last destinations change, all holes stay untouched.
    let indices: Vec<_> = (0..32)
        .map(|index| index * (INITIAL_COUNT - 1) / 31)
        .collect();
    let bytes = indices.iter().map(|&index| values[index].len()).sum();
    uploads.begin();
    assert!(uploads.prepare(
        bytes,
        indices.len(),
        BYTE_LIMIT,
        device.limits().max_buffer_size
    ));
    for &index in &indices {
        values[index] = value(index, 3);
        assert!(uploads.stage(index, &values[index]));
    }
    let mut encoder = device.create_command_encoder(&Default::default());
    let report = uploads.flush(device, queue, &mut encoder, &bindings);
    assert_eq!(
        (
            report.uploaded_bytes,
            report.copy_count,
            report.created_buffers
        ),
        (bytes, 32, 0)
    );
    let readback = capture_uniforms(
        device,
        &mut encoder,
        &bindings[..INITIAL_COUNT],
        &values[..INITIAL_COUNT],
    );
    queue.submit([encoder.finish()]);
    assert_readback(device, readback);

    // Growth replaces scratch once, shrink retains it but uploads/copies only
    // the current prefix. Old trailing records must never be replayed.
    for (count, generation) in [(GROWN_COUNT, 4), (INITIAL_COUNT, 5)] {
        let old_capacity = uploads.allocation_bytes();
        let bytes = values[..count].iter().map(Vec::len).sum::<usize>();
        uploads.begin();
        assert!(uploads.prepare(bytes, count, BYTE_LIMIT, device.limits().max_buffer_size));
        for (index, destination) in values[..count].iter_mut().enumerate() {
            *destination = value(index, generation);
            assert!(uploads.stage(index, destination));
        }
        let mut encoder = device.create_command_encoder(&Default::default());
        let report = uploads.flush(device, queue, &mut encoder, &bindings);
        assert_eq!(report.uploaded_bytes, bytes);
        assert_eq!(report.copy_count, count);
        assert_eq!(report.created_buffers, usize::from(count == GROWN_COUNT));
        if report.created_buffers > 0 {
            assert!(report.peak_buffer_bytes >= old_capacity + report.buffer_bytes);
        } else {
            assert_eq!(report.buffer_bytes, old_capacity);
        }
        let readback = capture_uniforms(device, &mut encoder, &bindings, &values);
        queue.submit([encoder.finish()]);
        assert_readback(device, readback);
    }

    // A temporary alias has no dirty slot of its own. Stage one independent
    // record per 32 real destinations and read all 64 interleaved aliases.
    let aliases = bindings[..32]
        .iter()
        .flat_map(|binding| [binding.clone(), binding.clone()])
        .collect::<Vec<_>>();
    let mut alias_values = Vec::new();
    uploads.begin();
    let bytes = values[..32].iter().map(Vec::len).sum();
    assert!(uploads.prepare(bytes, 32, BYTE_LIMIT, device.limits().max_buffer_size));
    for (index, destination) in values[..32].iter_mut().enumerate() {
        *destination = value(index, 6);
        assert!(uploads.stage(index * 2, destination));
        alias_values.extend([destination.clone(), destination.clone()]);
    }
    let mut encoder = device.create_command_encoder(&Default::default());
    let report = uploads.flush(device, queue, &mut encoder, &aliases);
    assert_eq!((report.uploaded_bytes, report.copy_count), (bytes, 32));
    let readback = capture_uniforms(device, &mut encoder, &aliases, &alias_values);
    queue.submit([encoder.finish()]);
    assert_readback(device, readback);

    // Disabled, too-small and over-device-limit plans fall back to ordinary
    // writes. A failed prepare must not replay an earlier accepted plan.
    for (count, byte_limit, max_buffer_size) in [
        (31, BYTE_LIMIT, device.limits().max_buffer_size),
        (32, 0, device.limits().max_buffer_size),
        (32, 31, device.limits().max_buffer_size),
        (32, BYTE_LIMIT, 31),
    ] {
        uploads.begin();
        let bytes = values[..count].iter().map(Vec::len).sum();
        assert!(!uploads.prepare(bytes, count, byte_limit, max_buffer_size));
        for (index, destination) in values[..count].iter_mut().enumerate() {
            *destination = value(index, 7);
            assert!(!uploads.stage(index, destination));
            queue.write_buffer(&bindings[index]._buffer, 0, destination);
        }
        let mut encoder = device.create_command_encoder(&Default::default());
        let report = uploads.flush(device, queue, &mut encoder, &bindings);
        assert_eq!((report.uploaded_bytes, report.copy_count), (0, 0));
        let readback = capture_uniforms(device, &mut encoder, &bindings[..count], &values[..count]);
        queue.submit([encoder.finish()]);
        assert_readback(device, readback);
    }
    uploads.clear();
    assert_eq!(uploads.allocation_bytes(), 0);
    assert_eq!(uploads.cpu_bytes(), 0);
}

fn value(index: usize, generation: usize) -> Vec<u8> {
    let size = [64, 96, 32][index % 3];
    let mut bytes = vec![0; size];
    for (word, chunk) in bytes.chunks_exact_mut(4).enumerate() {
        let sentinel = ((index as u32) << 16) ^ ((generation as u32) << 8) ^ word as u32;
        chunk.copy_from_slice(&sentinel.to_le_bytes());
    }
    let color = [
        ((index * 37 + generation * 41) % 251) as f32 / 255.0,
        ((index * 71 + generation * 13) % 251) as f32 / 255.0,
        ((index * 19 + generation * 83) % 251) as f32 / 255.0,
        1.0_f32,
    ];
    bytes[..16].copy_from_slice(bytemuck::bytes_of(&color));
    bytes
}

fn binding(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, bytes: &[u8]) -> FrameBinding {
    let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
        label: Some("uniform transfer oracle destination"),
        contents: bytes,
        usage: wgpu::BufferUsages::UNIFORM
            | wgpu::BufferUsages::COPY_DST
            | wgpu::BufferUsages::COPY_SRC,
    });
    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("uniform transfer oracle binding"),
        layout,
        entries: &[wgpu::BindGroupEntry {
            binding: 0,
            resource: buffer.as_entire_binding(),
        }],
    });
    FrameBinding {
        _buffer: buffer,
        bind_group,
    }
}

fn capture_uniforms(
    device: &wgpu::Device,
    encoder: &mut wgpu::CommandEncoder,
    bindings: &[FrameBinding],
    values: &[Vec<u8>],
) -> Readback {
    let expected = values.iter().flatten().copied().collect::<Vec<_>>();
    let buffer = readback_buffer(device, expected.len());
    let mut offset = 0;
    for (binding, bytes) in bindings.iter().zip(values) {
        encoder.copy_buffer_to_buffer(&binding._buffer, 0, &buffer, offset, bytes.len() as u64);
        offset += bytes.len() as u64;
    }
    Readback {
        buffer,
        expected,
        tolerance: 0,
    }
}

fn pixel_pipeline(device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> wgpu::RenderPipeline {
    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("uniform transfer oracle shader"),
        source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
            r#"
@group(0) @binding(0) var<uniform> color: vec4<f32>;
@vertex fn vs(@builtin(vertex_index) index: u32) -> @builtin(position) vec4<f32> {
    let positions = array(vec2<f32>(-1.0, -1.0), vec2<f32>(3.0, -1.0), vec2<f32>(-1.0, 3.0));
    return vec4<f32>(positions[index], 0.0, 1.0);
}
@fragment fn fs() -> @location(0) vec4<f32> { return color; }
"#,
        )),
    });
    let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("uniform transfer oracle pipeline layout"),
        bind_group_layouts: &[Some(layout)],
        immediate_size: 0,
    });
    device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
        label: Some("uniform transfer oracle pipeline"),
        layout: Some(&pipeline_layout),
        vertex: wgpu::VertexState {
            module: &shader,
            entry_point: Some("vs"),
            compilation_options: Default::default(),
            buffers: &[],
        },
        fragment: Some(wgpu::FragmentState {
            module: &shader,
            entry_point: Some("fs"),
            compilation_options: Default::default(),
            targets: &[Some(wgpu::ColorTargetState {
                format: wgpu::TextureFormat::Rgba8Unorm,
                blend: None,
                write_mask: wgpu::ColorWrites::ALL,
            })],
        }),
        primitive: Default::default(),
        depth_stencil: None,
        multisample: Default::default(),
        multiview_mask: None,
        cache: None,
    })
}

fn capture_pixels(
    device: &wgpu::Device,
    pipeline: &wgpu::RenderPipeline,
    encoder: &mut wgpu::CommandEncoder,
    bindings: &[FrameBinding],
    values: &[Vec<u8>],
) -> Readback {
    let width = 64;
    let height = (bindings.len() as u32).div_ceil(width);
    let size = wgpu::Extent3d {
        width,
        height,
        depth_or_array_layers: 1,
    };
    let texture = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("uniform transfer oracle pixels"),
        size,
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: wgpu::TextureFormat::Rgba8Unorm,
        usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
        view_formats: &[],
    });
    let view = texture.create_view(&Default::default());
    {
        let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
            label: Some("uniform transfer oracle pass"),
            color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                view: &view,
                depth_slice: None,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
                    store: wgpu::StoreOp::Store,
                },
            })],
            depth_stencil_attachment: None,
            timestamp_writes: None,
            occlusion_query_set: None,
            multiview_mask: None,
        });
        pass.set_pipeline(pipeline);
        for (index, binding) in bindings.iter().enumerate() {
            pass.set_bind_group(0, &binding.bind_group, &[]);
            pass.set_scissor_rect(index as u32 % width, index as u32 / width, 1, 1);
            pass.draw(0..3, 0..1);
        }
    }
    let mut expected = vec![0; (width * height * 4) as usize];
    for (index, bytes) in values.iter().enumerate() {
        for channel in 0..4 {
            let offset = channel * 4;
            let value = f32::from_le_bytes(bytes[offset..offset + 4].try_into().unwrap());
            expected[index * 4 + channel] = (value * 255.0).round() as u8;
        }
    }
    let buffer = readback_buffer(device, expected.len());
    encoder.copy_texture_to_buffer(
        wgpu::TexelCopyTextureInfo {
            texture: &texture,
            mip_level: 0,
            origin: wgpu::Origin3d::ZERO,
            aspect: wgpu::TextureAspect::All,
        },
        wgpu::TexelCopyBufferInfo {
            buffer: &buffer,
            layout: wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(256),
                rows_per_image: Some(height),
            },
        },
        size,
    );
    Readback {
        buffer,
        expected,
        tolerance: 1,
    }
}

fn readback_buffer(device: &wgpu::Device, size: usize) -> wgpu::Buffer {
    device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("uniform transfer oracle readback"),
        size: size as u64,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    })
}

fn assert_readback(device: &wgpu::Device, readback: Readback) {
    let (sender, receiver) = std::sync::mpsc::channel();
    let slice = readback.buffer.slice(..);
    slice.map_async(wgpu::MapMode::Read, move |result| {
        sender.send(result).unwrap()
    });
    device
        .poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: Some(Duration::from_secs(10)),
        })
        .unwrap();
    receiver
        .recv_timeout(Duration::from_secs(10))
        .unwrap()
        .unwrap();
    let actual = slice.get_mapped_range().unwrap();
    assert_eq!(actual.len(), readback.expected.len());
    for (index, (&actual, &expected)) in actual.iter().zip(&readback.expected).enumerate() {
        assert!(
            actual.abs_diff(expected) <= readback.tolerance,
            "uniform transfer byte {index}: {actual} != {expected}"
        );
    }
}