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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Opt-in complete-update CPU measurements with a frozen full-chain byte oracle.
//!
//! Setup, polling, alias lifetime management and full CPU/GPU verification are
//! outside update timing and allocation counting. Durations describe CPU work
//! on the calling thread, not GPU completion. Use this identical fixture on baseline
//! and candidate and alternate executable order in the external trial runner.

use super::*;
use std::hint::black_box;

use full_mip_reference::Level;

const WARMUPS: usize = 2;
const SAMPLES: usize = 5;

#[derive(Clone, Copy)]
enum Patch {
    Center(u32),
    Full,
    BottomRight(u32, u32),
}

#[derive(Clone, Copy)]
struct Case {
    name: &'static str,
    width: u32,
    height: u32,
    alpha: bool,
    patch: Patch,
}

impl Case {
    fn region(self) -> ImageTexelRect {
        let (x, y, width, height) = match self.patch {
            Patch::Center(side) => (
                (self.width - side) / 2,
                (self.height - side) / 2,
                side,
                side,
            ),
            Patch::Full => (0, 0, self.width, self.height),
            Patch::BottomRight(width, height) => {
                (self.width - width, self.height - height, width, height)
            }
        };
        ImageTexelRect::new(x, y, width, height).unwrap()
    }
}

const CASES: [Case; 12] = [
    Case {
        name: "opaque_512_center32",
        width: 512,
        height: 512,
        alpha: false,
        patch: Patch::Center(32),
    },
    Case {
        name: "opaque_512_full",
        width: 512,
        height: 512,
        alpha: false,
        patch: Patch::Full,
    },
    Case {
        name: "opaque_1024_center32",
        width: 1024,
        height: 1024,
        alpha: false,
        patch: Patch::Center(32),
    },
    Case {
        name: "opaque_1024_full",
        width: 1024,
        height: 1024,
        alpha: false,
        patch: Patch::Full,
    },
    Case {
        name: "opaque_2048_center32",
        width: 2048,
        height: 2048,
        alpha: false,
        patch: Patch::Center(32),
    },
    Case {
        name: "opaque_2048_full",
        width: 2048,
        height: 2048,
        alpha: false,
        patch: Patch::Full,
    },
    Case {
        name: "opaque_1024_corner1",
        width: 1024,
        height: 1024,
        alpha: false,
        patch: Patch::BottomRight(1, 1),
    },
    Case {
        name: "alpha_1024_center32",
        width: 1024,
        height: 1024,
        alpha: true,
        patch: Patch::Center(32),
    },
    Case {
        name: "alpha_1024_full",
        width: 1024,
        height: 1024,
        alpha: true,
        patch: Patch::Full,
    },
    Case {
        name: "alpha_511x257_corner8",
        width: 511,
        height: 257,
        alpha: true,
        patch: Patch::BottomRight(8, 8),
    },
    Case {
        name: "opaque_1023x769_center32",
        width: 1023,
        height: 769,
        alpha: false,
        patch: Patch::Center(32),
    },
    Case {
        name: "alpha_1x1023_last1x8",
        width: 1,
        height: 1023,
        alpha: true,
        patch: Patch::BottomRight(1, 8),
    },
];

fn pixels(width: u32, height: u32, alpha: bool, seed: u32) -> Vec<u8> {
    let mut pixels = Vec::with_capacity(width as usize * height as usize * 4);
    for y in 0..height {
        for x in 0..width {
            let mixed = x.wrapping_mul(0x045d_9f3b)
                ^ y.wrapping_mul(0x119d_e1f3)
                ^ seed.wrapping_mul(0x9e37_79b9);
            let mixed = mixed ^ (mixed >> 16);
            let opacity = if alpha {
                [0, 1, 17, 127, 254, 255][((x + 3 * y + seed) % 6) as usize]
            } else {
                255
            };
            pixels.extend_from_slice(&[
                mixed as u8,
                (mixed >> 8) as u8,
                (mixed >> 16) as u8,
                opacity,
            ]);
        }
    }
    pixels
}

fn patched_reference(previous: &[Level], region: ImageTexelRect, patch: &[u8]) -> Vec<Level> {
    let mut base = previous[0].pixels.clone();
    let row_bytes = region.width() as usize * 4;
    for row in 0..region.height() as usize {
        let destination =
            ((region.y() as usize + row) * previous[0].width as usize + region.x() as usize) * 4;
        base[destination..destination + row_bytes]
            .copy_from_slice(&patch[row * row_bytes..(row + 1) * row_bytes]);
    }
    full_mip_reference::rebuild(previous[0].width, previous[0].height, base)
}

fn assert_bytes(label: &str, level: usize, row: usize, actual: &[u8], expected: &[u8]) {
    assert_eq!(
        actual.len(),
        expected.len(),
        "{label}: mip {level}, row {row} length"
    );
    if let Some(offset) = actual
        .iter()
        .zip(expected)
        .position(|(left, right)| left != right)
    {
        panic!(
            "{label}: mip {level}, row {row}, byte {offset}: {} != {}",
            actual[offset], expected[offset]
        );
    }
}

fn assert_chain(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    texture: &Texture3d,
    expected: &[Level],
    label: &str,
) {
    assert_eq!(
        texture.mip_level_count() as usize,
        expected.len(),
        "{label}"
    );
    assert_eq!(
        texture.size(),
        (expected[0].width, expected[0].height),
        "{label}"
    );
    assert_eq!(
        texture.gpu_allocation_bytes(),
        expected
            .iter()
            .map(|level| level.pixels.len())
            .sum::<usize>(),
        "{label}"
    );
    let mut layouts = Vec::with_capacity(expected.len());
    let mut size = 0_u64;
    for (index, level) in expected.iter().enumerate() {
        assert_eq!(
            texture.mip_level_size(index as u32),
            Some((level.width, level.height)),
            "{label}"
        );
        assert_bytes(
            label,
            index,
            0,
            texture.mip_level_pixels(index as u32).unwrap(),
            &level.pixels,
        );
        let stride = (level.width * 4).div_ceil(256) * 256;
        layouts.push((size, stride));
        size += u64::from(stride) * u64::from(level.height);
    }
    assert!(size <= device.limits().max_buffer_size);
    let buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("texture update benchmark complete-chain verification"),
        size,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    let mut encoder = device.create_command_encoder(&Default::default());
    for (index, (level, &(offset, stride))) in expected.iter().zip(&layouts).enumerate() {
        encoder.copy_texture_to_buffer(
            wgpu::TexelCopyTextureInfo {
                texture: &texture.storage.texture,
                mip_level: index as u32,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::TexelCopyBufferInfo {
                buffer: &buffer,
                layout: wgpu::TexelCopyBufferLayout {
                    offset,
                    bytes_per_row: Some(stride),
                    rows_per_image: Some(level.height),
                },
            },
            wgpu::Extent3d {
                width: level.width,
                height: level.height,
                depth_or_array_layers: 1,
            },
        );
    }
    queue.submit([encoder.finish()]);
    let slice = buffer.slice(..);
    let (sender, receiver) = std::sync::mpsc::channel();
    slice.map_async(wgpu::MapMode::Read, move |result| {
        sender.send(result).unwrap();
    });
    device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
    receiver
        .recv_timeout(Duration::from_secs(10))
        .unwrap()
        .unwrap();
    let mapped = slice.get_mapped_range().unwrap();
    for (index, (level, &(offset, stride))) in expected.iter().zip(&layouts).enumerate() {
        let row_bytes = level.width as usize * 4;
        for row in 0..level.height as usize {
            let actual_start = offset as usize + row * stride as usize;
            let expected_start = row * row_bytes;
            assert_bytes(
                label,
                index,
                row,
                &mapped[actual_start..actual_start + row_bytes],
                &level.pixels[expected_start..expected_start + row_bytes],
            );
        }
    }
    drop(mapped);
    buffer.unmap();
}

fn assert_accounting(
    case: Case,
    aliased: bool,
    texture: &Texture3d,
    report: Texture3dUpdateReport,
    old_recovery: usize,
    expected: &[Level],
) {
    let region = case.region();
    let base_bytes = region.width() as usize * region.height() as usize * 4;
    let gpu_bytes = expected
        .iter()
        .map(|level| level.pixels.len())
        .sum::<usize>();
    let lower_bytes = gpu_bytes - expected[0].pixels.len();
    let copied_bytes = if aliased { gpu_bytes } else { 0 };
    assert_eq!(report.base_upload_bytes(), base_bytes);
    assert_eq!(
        report.uploaded_bytes(),
        base_bytes + report.mip_upload_bytes()
    );
    assert_eq!(
        report.regenerated_texel_count() * 4,
        report.mip_upload_bytes()
    );
    assert_eq!(report.upload_calls(), expected.len());
    assert!(report.mip_upload_bytes() <= lower_bytes);
    assert!(report.mip_upload_bytes() >= (expected.len() - 1) * 4);
    if matches!(case.patch, Patch::Full) {
        assert_eq!(report.mip_upload_bytes(), lower_bytes);
    }
    assert_eq!(report.gpu_copy_bytes(), copied_bytes);
    assert_eq!(
        report.gpu_copy_calls(),
        if aliased { expected.len() } else { 0 }
    );
    assert_eq!(report.gpu_allocation_count(), usize::from(aliased));
    assert_eq!(report.submission_count(), 1 + usize::from(aliased));
    assert_eq!(report.detached_aliases(), aliased);
    assert_eq!(report.reused_allocation(), !aliased);
    assert_eq!(report.gpu_bytes(), gpu_bytes);
    assert_eq!(report.recovery_bytes(), texture.recovery_memory_bytes());
    assert!(report.recovery_bytes() >= gpu_bytes);
    assert_eq!(
        report.peak_recovery_bytes(),
        old_recovery + report.recovery_bytes()
    );
    assert_eq!(report.peak_gpu_bytes(), gpu_bytes + copied_bytes);
    assert!(report.staging_bytes() >= base_bytes + report.uploaded_bytes());
    assert!(report.staging_bytes() <= Texture3dUpdateBudget::default().max_staging_bytes());
}

fn print_sample(
    case: Case,
    aliased: bool,
    iteration: usize,
    wall: Duration,
    allocations: usize,
    report: Texture3dUpdateReport,
) {
    let (phase, sample) = if iteration < WARMUPS {
        ("warmup", iteration)
    } else {
        ("sample", iteration - WARMUPS)
    };
    eprintln!(
        "texture_update_bench case={} ownership={} phase={} sample={} patch_variant={} wall_ns={} preparation_ns={} encode_submit_ns={} allocation_calls={} base_upload_bytes={} mip_upload_bytes={} uploaded_bytes={} upload_calls={} regenerated_texels={} gpu_copy_bytes={} gpu_copy_calls={} gpu_allocations={} submissions={} recovery_bytes={} gpu_bytes={} staging_bytes={} peak_recovery_bytes={} peak_gpu_bytes={}",
        case.name,
        if aliased { "aliased" } else { "unique" },
        phase,
        sample,
        iteration % 2,
        wall.as_nanos(),
        report.preparation_cpu().as_nanos(),
        report.encode_submit_cpu().as_nanos(),
        allocations,
        report.base_upload_bytes(),
        report.mip_upload_bytes(),
        report.uploaded_bytes(),
        report.upload_calls(),
        report.regenerated_texel_count(),
        report.gpu_copy_bytes(),
        report.gpu_copy_calls(),
        report.gpu_allocation_count(),
        report.submission_count(),
        report.recovery_bytes(),
        report.gpu_bytes(),
        report.staging_bytes(),
        report.peak_recovery_bytes(),
        report.peak_gpu_bytes(),
    );
}

fn run_case(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    identity: &Arc<()>,
    layout: &wgpu::BindGroupLayout,
    case: Case,
    aliased: bool,
) {
    let base = pixels(case.width, case.height, case.alpha, 0);
    let mut expected = full_mip_reference::rebuild(case.width, case.height, base.clone());
    let mut texture = create_texture_with_options(
        device,
        queue,
        identity,
        layout,
        case.width,
        case.height,
        base,
        ImageBudget::default(),
        Texture3dOptions::new()
            .with_alpha_preservation(case.alpha)
            .with_mipmaps(TextureMipmaps3d::Generate),
    )
    .unwrap();
    assert_chain(device, queue, &texture, &expected, case.name);
    let region = case.region();
    let patches = [37, 113].map(|seed| pixels(region.width(), region.height(), case.alpha, seed));
    for iteration in 0..WARMUPS + SAMPLES {
        device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
        assert_eq!(Arc::strong_count(&texture.storage), 1);
        let old_identity = texture.identity_key();
        let old_recovery = texture.recovery_memory_bytes();
        let patch = &patches[iteration % 2];
        let snapshot = aliased.then(|| texture.clone());
        assert_eq!(
            Arc::strong_count(&texture.storage),
            1 + usize::from(aliased)
        );
        let started = Instant::now();
        let (result, allocations) = crate::test_allocations::count(|| {
            update_texture_region(
                device,
                queue,
                identity,
                layout,
                black_box(&mut texture),
                black_box(region),
                black_box(patch),
                region.width() as usize * 4,
                Texture3dUpdateBudget::default(),
            )
        });
        let wall = started.elapsed();
        let report = black_box(result.unwrap());
        device.poll(wgpu::PollType::wait_indefinitely()).unwrap();

        let next = patched_reference(&expected, region, patch);
        assert_chain(device, queue, &texture, &next, case.name);
        assert_accounting(case, aliased, &texture, report, old_recovery, &next);
        if let Some(snapshot) = snapshot.as_ref() {
            assert_eq!(snapshot.identity_key(), old_identity);
            assert_ne!(texture.identity_key(), old_identity);
            assert_eq!(snapshot.recovery_memory_bytes(), old_recovery);
            assert_chain(device, queue, snapshot, &expected, "old alias");
        } else {
            assert_eq!(texture.identity_key(), old_identity);
        }
        drop(snapshot);
        assert_eq!(Arc::strong_count(&texture.storage), 1);
        expected = next;
        print_sample(case, aliased, iteration, wall, allocations, report);
    }
    device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
}

pub(in crate::renderer::mesh3d) fn run(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    format: wgpu::TextureFormat,
) {
    if std::env::var("SIM_ENGINE_BENCH_TEXTURE_UPDATES").as_deref() != Ok("1") {
        return;
    }
    if cfg!(debug_assertions) {
        panic!("texture update measurements require --release");
    }
    assert!(
        device.limits().max_texture_dimension_2d >= 2048,
        "texture update fixture requires its complete 2048-texel case matrix"
    );
    eprintln!(
        "texture_update_bench metadata package_version={} cases={} ownership_modes=2 warmups={} samples={} durations=cpu_calling_thread allocations=calling_thread_only gpu_timing=none oracle=frozen_dev6_full_filter",
        env!("CARGO_PKG_VERSION"),
        CASES.len(),
        WARMUPS,
        SAMPLES
    );
    eprintln!(
        "texture_update_bench provenance runtime={} fixture_sha256={} reference_sha256={} executable_sha256={}",
        std::env::var("SIM_ENGINE_RELEASE_SHA")
            .unwrap_or_else(|_| "unverified-working-tree".into()),
        std::env::var("SIM_ENGINE_TEXTURE_FIXTURE_SHA").unwrap_or_else(|_| "unverified".into()),
        std::env::var("SIM_ENGINE_TEXTURE_REFERENCE_SHA").unwrap_or_else(|_| "unverified".into()),
        std::env::var("SIM_ENGINE_BENCHMARK_BINARY_SHA256").unwrap_or_else(|_| "unverified".into())
    );
    let identity = Arc::new(());
    let renderer = Mesh3dRenderer::new(device, format);
    for case in CASES {
        for aliased in [false, true] {
            run_case(
                device,
                queue,
                &identity,
                &renderer.textures.layout,
                case,
                aliased,
            );
        }
    }
}