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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Paired cache-disabled/enabled desktop composition benchmark.
//! `--frames N` sets measured presented frames per case; `--quick` uses one label.
//! CPU allocation counts include wgpu work on this event-loop thread, not GPU
//! execution or worker-thread allocations. Surface acquire is reported separately.
//! Cache off/on controls composition storage/bindings, not source-owned proofs.
//! Use --case/--labels/--cache for focused comparisons and --trials for repeated
//! measurements of identical content; every trial is printed independently.

use std::{
    alloc::{GlobalAlloc, Layout, System},
    cell::Cell,
    error::Error,
    sync::Arc,
    time::{Duration, Instant},
};

use sim_engine::{
    Camera2d, Color, FrameBudget, FrameCacheBudget, FramePassOptions, GlyphAtlas2d,
    GlyphAtlasBudget, GlyphAtlasEntry, GlyphId, GlyphRun2d, GlyphRunBudget, Image2d,
    ImageBatchPlacement, ImageBudget, ImageSampling, ImageTexelRect, LogicalScreenPosition,
    LogicalScreenVector, LogicalViewport, LogicalViewportRegion, PositionedGlyph2d, RenderStatus,
    RendererPresentMode, Scene, ScreenScene, ShapeStyle, Vec2, WgpuRenderer, WgpuRendererOptions,
};
use winit::{
    application::ApplicationHandler,
    dpi::LogicalSize,
    event::WindowEvent,
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
    window::{Window, WindowId},
};

type Result<T> = std::result::Result<T, Box<dyn Error>>;

#[path = "support/frame_cache_cases.rs"]
mod cases;
use cases::Selection;

thread_local! {
    static COUNTING: Cell<bool> = const { Cell::new(false) };
    static ALLOCATIONS: Cell<usize> = const { Cell::new(0) };
    static ALLOCATED_BYTES: Cell<usize> = const { Cell::new(0) };
}

struct CountingAllocator;

fn allocated(size: usize) {
    let _ = COUNTING.try_with(|active| {
        if active.get() {
            let _ = ALLOCATIONS.try_with(|count| count.set(count.get().saturating_add(1)));
            let _ = ALLOCATED_BYTES.try_with(|count| count.set(count.get().saturating_add(size)));
        }
    });
}

// SAFETY: All allocations and deallocations are forwarded to System with the
// original pointer/layout. The thread-local counters do not allocate.
unsafe impl GlobalAlloc for CountingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        allocated(layout.size());
        // SAFETY: Forward the allocator contract unchanged.
        unsafe { System.alloc(layout) }
    }
    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        allocated(layout.size());
        // SAFETY: Forward the allocator contract unchanged.
        unsafe { System.alloc_zeroed(layout) }
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        // SAFETY: Forward the allocator contract unchanged.
        unsafe { System.dealloc(ptr, layout) }
    }
    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 {
        allocated(size);
        // SAFETY: Forward the allocator contract unchanged.
        unsafe { System.realloc(ptr, layout, size) }
    }
}

#[global_allocator]
static ALLOCATOR: CountingAllocator = CountingAllocator;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Content {
    World,
    WorldScreen,
    Mixed64,
    Labels,
    Recolored,
    Moved,
    Mixed,
}

impl Content {
    fn name(self) -> &'static str {
        match self {
            Self::World => "world",
            Self::WorldScreen => "world_screen",
            Self::Mixed64 => "mixed64_images128rects",
            Self::Labels => "unchanged",
            Self::Recolored => "recolored",
            Self::Moved => "moved",
            Self::Mixed => "mixed",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Case {
    content: Content,
    labels: usize,
    cached: bool,
}

fn expected_counts(case: Case) -> (usize, usize, usize, usize, usize) {
    match case.content {
        Content::World => (1, 0, 0, 100, 1),
        Content::WorldScreen => (2, 0, 0, 356, 2),
        Content::Mixed64 => (65, 64, 0, 292, 129),
        Content::Labels | Content::Recolored | Content::Moved => {
            (0, 0, case.labels, case.labels, case.labels)
        }
        Content::Mixed => (
            1 + case.labels,
            case.labels,
            case.labels,
            100 + 3 * case.labels,
            1 + 3 * case.labels,
        ),
    }
}

fn p95(samples: &mut [Duration]) -> Duration {
    samples.sort_unstable();
    samples
        .get((samples.len() * 95).div_ceil(100).saturating_sub(1))
        .copied()
        .unwrap_or_default()
}

#[derive(Default)]
struct Samples {
    draws: usize,
    attempts: usize,
    allocations: usize,
    allocated_bytes: usize,
    buffers: usize,
    groups: usize,
    uploads: usize,
    elapsed: Duration,
    acquire: Duration,
    tessellation: Duration,
    upload: Duration,
    camera_upload: Duration,
    uniform_calls: usize,
    uniform_copies: usize,
    uniform_copy_bytes: usize,
    staging_buffers: usize,
    encode: Duration,
    outside_report: Duration,
    measured_started: Option<Instant>,
    work_times: Vec<Duration>,
    acquire_times: Vec<Duration>,
}

impl Samples {
    fn new(frames: usize) -> Self {
        Self {
            work_times: Vec::with_capacity(frames),
            acquire_times: Vec::with_capacity(frames),
            ..Self::default()
        }
    }
}

struct Benchmark {
    window: Arc<Window>,
    renderer: WgpuRenderer,
    atlas: GlyphAtlas2d,
    runs: Vec<GlyphRun2d>,
    image: Image2d,
    world: Scene,
    panel: ScreenScene,
    hud: ScreenScene,
    mixed_panels: Vec<ScreenScene>,
    cases: Vec<Case>,
    case_index: usize,
    samples: Samples,
    measured_frames: usize,
    trials: usize,
    upload_staging_bytes: Option<usize>,
}

impl Benchmark {
    fn new(events: &ActiveEventLoop, measured_frames: usize, selection: Selection) -> Result<Self> {
        let cases = selection.cases()?;
        let window = Arc::new(
            events.create_window(
                Window::default_attributes()
                    .with_title("Sim;Engine frame cache benchmark")
                    .with_inner_size(LogicalSize::new(1280.0, 720.0)),
            )?,
        );
        let size = window.inner_size();
        let options =
            WgpuRendererOptions::new(RendererPresentMode::NoVsync, window.scale_factor())?;
        let mut renderer = pollster::block_on(WgpuRenderer::new_with_options(
            window.clone(),
            size.width,
            size.height,
            options,
        ))?;
        let notify = window.clone();
        renderer.set_pre_present_notify(move || notify.pre_present_notify());
        println!(
            "frame_cache_benchmark backend={} adapter={:?} pci_bus_id={} vendor={} device={} driver={:?} driver_info={:?} format={:?} msaa={} present={:?} scale={} physical_width={} physical_height={} allocation_scope=event_loop_thread baseline=cache_disabled gpu_timestamps=false",
            renderer.adapter_backend(),
            renderer.adapter_name(),
            renderer.adapter_pci_bus_id(),
            renderer.adapter_vendor_id(),
            renderer.adapter_device_id(),
            renderer.adapter_driver(),
            renderer.adapter_driver_info(),
            renderer.surface_format(),
            renderer.surface_sample_count(),
            renderer.surface_present_mode(),
            renderer.scale_factor(),
            size.width,
            size.height,
        );
        let atlas = renderer.create_glyph_atlas(
            1,
            1,
            vec![255; 4],
            vec![GlyphAtlasEntry::new(
                GlyphId::new(1),
                ImageTexelRect::new(0, 0, 1, 1)?,
            )],
            GlyphAtlasBudget::default(),
        )?;
        let glyphs = (0..32)
            .map(|index| {
                PositionedGlyph2d::new(
                    GlyphId::new(1),
                    LogicalViewportRegion::new(
                        LogicalScreenPosition::new(index as f32 * 3.0, 0.0),
                        LogicalViewport::new(2.0, 6.0).unwrap(),
                    )
                    .unwrap(),
                    Color::WHITE,
                )
                .unwrap()
            })
            .collect::<Vec<_>>();
        let mut runs = Vec::new();
        for _ in 0..if selection.quick { 1 } else { 1000 } {
            runs.push(renderer.create_glyph_run(
                &atlas,
                glyphs.clone(),
                GlyphRunBudget::default(),
            )?);
        }
        let image = renderer.create_image_rgba8(1, 1, vec![255; 4], ImageBudget::default())?;
        let mut world = Scene::new(Color::BLACK)?;
        for index in 0..100 {
            world.try_circle(
                Vec2::new((index % 10) as f32 * 12.0, (index / 10) as f32 * 12.0),
                3.0,
                ShapeStyle::filled(Color::rgb(0.2, 0.3, 0.4)),
            )?;
        }
        let mut panel = ScreenScene::new(Color::BLACK)?;
        panel.try_square_rect(
            LogicalScreenPosition::new(0.0, 0.0),
            LogicalScreenVector::new(24.0, 12.0),
            ShapeStyle::filled(Color::rgb(0.0, 0.1, 0.2).with_alpha(0.5)),
        )?;
        let hud = ScreenScene::new(Color::BLACK)?;
        let mut mixed_panels = Vec::new();
        for index in 0..64 {
            let mut scene = ScreenScene::new(Color::BLACK)?;
            let x = (index % 8) as f32 * 140.0;
            let y = (index / 8) as f32 * 80.0;
            for offset in [0.0, 14.0] {
                scene.try_square_rect(
                    LogicalScreenPosition::new(x, y + offset),
                    LogicalScreenVector::new(104.0, 12.0),
                    ShapeStyle::filled(Color::rgb(0.0, 0.1, 0.2).with_alpha(0.5)),
                )?;
            }
            mixed_panels.push(scene);
        }
        renderer.set_frame_cache_budget(cases[0].cache_budget(selection.upload_staging_bytes));
        Ok(Self {
            window,
            renderer,
            atlas,
            runs,
            image,
            world,
            panel,
            hud,
            mixed_panels,
            cases,
            case_index: 0,
            samples: Samples::new(measured_frames),
            measured_frames,
            trials: selection.trials,
            upload_staging_bytes: selection.upload_staging_bytes,
        })
    }

    fn redraw(&mut self) -> Result<bool> {
        const WARMUP: usize = 20;
        let case = self.cases[self.case_index];
        ALLOCATIONS.set(0);
        ALLOCATED_BYTES.set(0);
        COUNTING.set(true);
        let started = Instant::now();
        let result = self.draw(case);
        let elapsed = started.elapsed();
        COUNTING.set(false);
        let allocations = ALLOCATIONS.get();
        let allocated_bytes = ALLOCATED_BYTES.get();
        let report = result?;
        self.samples.attempts += 1;
        if report.status() != RenderStatus::Drawn || report.surface_present_count() != 1 {
            if self.samples.attempts > (WARMUP + self.measured_frames) * 4 {
                return Err("benchmark cannot collect the requested presented frames".into());
            }
            return Ok(false);
        }
        self.samples.draws += 1;
        let sources = report.statistics().source_counts();
        let observed = (
            sources.streaming_scenes(),
            sources.images(),
            sources.glyph_runs(),
            report.statistics().command_count(),
            report.statistics().pass_count(),
        );
        if observed != expected_counts(case) {
            return Err(format!(
                "case {} source/command counts {observed:?} != {:?}",
                case.content.name(),
                expected_counts(case)
            )
            .into());
        }
        let cache = self.renderer.frame_cache_statistics();
        if self.samples.draws > WARMUP {
            self.samples.measured_started.get_or_insert(started);
            if case.cached
                && (cache.created_buffers() != 0
                    || cache.created_bind_groups() != 0
                    || cache.created_upload_staging_buffers() != 0)
            {
                return Err(
                    "warmed composition created a new uniform/staging buffer or bind group".into(),
                );
            }
            if case.cached
                && matches!(
                    case.content,
                    Content::World
                        | Content::WorldScreen
                        | Content::Mixed64
                        | Content::Labels
                        | Content::Mixed
                )
                && cache.uploaded_uniform_bytes() != 0
            {
                return Err("unchanged warmed composition uploaded an unchanged uniform".into());
            }
            self.samples.allocations += allocations;
            self.samples.allocated_bytes += allocated_bytes;
            self.samples.buffers += cache.created_buffers();
            self.samples.groups += cache.created_bind_groups();
            self.samples.uniform_calls += cache.uniform_upload_calls();
            self.samples.uniform_copies += cache.batched_uniform_copies();
            self.samples.uniform_copy_bytes += cache.batched_uniform_bytes();
            self.samples.staging_buffers += cache.created_upload_staging_buffers();
            self.samples.uploads += report.statistics().upload_bytes();
            self.samples.elapsed += elapsed;
            self.samples.acquire += report.metrics().surface_acquire();
            self.samples.tessellation += report.metrics().tessellation();
            self.samples.upload += report.metrics().upload();
            self.samples.camera_upload += report.metrics().camera_uniform_upload();
            self.samples.encode += report.metrics().encode_submit_present();
            self.samples.outside_report += elapsed.saturating_sub(report.metrics().total_cpu());
            self.samples
                .work_times
                .push(elapsed.saturating_sub(report.metrics().surface_acquire()));
            self.samples
                .acquire_times
                .push(report.metrics().surface_acquire());
        }
        if self.samples.draws < WARMUP + self.measured_frames {
            return Ok(false);
        }
        let count = self.measured_frames as f64;
        let measured_seconds = self
            .samples
            .measured_started
            .map(|started| started.elapsed().as_secs_f64())
            .unwrap_or(0.0);
        let work_p95 = p95(&mut self.samples.work_times).as_secs_f64() * 1000.0;
        let acquire_p95 = p95(&mut self.samples.acquire_times).as_secs_f64() * 1000.0;
        let trial = self.case_index / (self.cases.len() / self.trials) + 1;
        println!("measurement trial={trial} trials={}", self.trials);
        println!(
            "case={} labels={} glyphs_per_label=32 cache={} drawn={} presented_fps={:.2} main_thread_allocations_per_frame={:.2} allocated_bytes_per_frame={:.0} new_uniform_buffers_per_frame={:.2} new_bind_groups_per_frame={:.2} upload_bytes_per_frame={:.0} renderer_and_build_ms={:.3} acquire_ms={:.3} cache_cpu_bytes={} cache_uniform_bytes={} cache_texture_bytes={} cache_bindings={} cache_peak_cpu_bytes={} cache_peak_uniform_bytes={} source_scenes={} source_images={} source_glyph_runs={} commands={} passes={} renderer_and_build_p95_ms={:.3} acquire_p95_ms={:.3}",
            case.content.name(),
            case.labels,
            case.cached,
            self.measured_frames,
            count / measured_seconds,
            self.samples.allocations as f64 / count,
            self.samples.allocated_bytes as f64 / count,
            self.samples.buffers as f64 / count,
            self.samples.groups as f64 / count,
            self.samples.uploads as f64 / count,
            (self.samples.elapsed.saturating_sub(self.samples.acquire)).as_secs_f64() * 1000.0
                / count,
            self.samples.acquire.as_secs_f64() * 1000.0 / count,
            cache.cpu_bytes(),
            cache.uniform_bytes(),
            cache.texture_bytes(),
            cache.binding_count(),
            cache.peak_cpu_bytes(),
            cache.peak_uniform_bytes(),
            report.statistics().source_counts().streaming_scenes(),
            report.statistics().source_counts().images(),
            report.statistics().source_counts().glyph_runs(),
            report.statistics().command_count(),
            report.statistics().pass_count(),
            work_p95,
            acquire_p95,
        );
        println!(
            "stages case={} labels={} cache={} tessellation_and_preflight_ms={:.3} upload_and_image_bindings_ms={:.3} camera_bindings_ms={:.3} encode_submit_present_ms={:.3} outside_report_ms={:.3}",
            case.content.name(),
            case.labels,
            case.cached,
            self.samples.tessellation.as_secs_f64() * 1000.0 / count,
            self.samples.upload.as_secs_f64() * 1000.0 / count,
            self.samples.camera_upload.as_secs_f64() * 1000.0 / count,
            self.samples.encode.as_secs_f64() * 1000.0 / count,
            self.samples.outside_report.as_secs_f64() * 1000.0 / count,
        );
        println!(
            "uniform_uploads case={} labels={} cache={} queue_writes_per_frame={:.2} buffer_copies_per_frame={:.2} buffer_copy_bytes_per_frame={:.0} new_staging_buffers_per_frame={:.2} retained_staging_bytes={} peak_staging_bytes={} staging_limit={}",
            case.content.name(),
            case.labels,
            case.cached,
            self.samples.uniform_calls as f64 / count,
            self.samples.uniform_copies as f64 / count,
            self.samples.uniform_copy_bytes as f64 / count,
            self.samples.staging_buffers as f64 / count,
            cache.upload_staging_bytes(),
            cache.peak_upload_staging_bytes(),
            self.renderer
                .frame_cache_budget()
                .max_upload_staging_bytes(),
        );
        self.samples = Samples::new(self.measured_frames);
        self.case_index += 1;
        if self.case_index == self.cases.len() {
            return Ok(true);
        }
        self.renderer.set_frame_cache_budget(
            self.cases[self.case_index].cache_budget(self.upload_staging_bytes),
        );
        Ok(false)
    }

    fn draw(&mut self, case: Case) -> Result<sim_engine::FrameReport> {
        if matches!(case.content, Content::WorldScreen) {
            self.hud.clear();
            let phase = self.samples.draws as f32;
            for index in 0..256 {
                self.hud.try_square_rect(
                    LogicalScreenPosition::new(
                        (index % 16) as f32 * 64.0 + phase.sin() * 2.0,
                        (index / 16) as f32 * 32.0,
                    ),
                    LogicalScreenVector::new(48.0, 24.0),
                    ShapeStyle::filled(Color::rgb(0.1, 0.3 + phase.sin() * 0.1, 0.6)),
                )?;
            }
        }
        let mut frame = self.renderer.begin_frame(
            Color::BLACK,
            FrameBudget::new(
                4096,
                100_000,
                4_000_000,
                256 * 1024 * 1024,
                512 * 1024 * 1024,
                65536,
            ),
        )?;
        if matches!(
            case.content,
            Content::World | Content::WorldScreen | Content::Mixed64 | Content::Mixed
        ) {
            frame.draw_scene(&self.world, Camera2d::default(), FramePassOptions::new(0))?;
        }
        if matches!(case.content, Content::WorldScreen) {
            frame.draw_screen_scene(&self.hud, FramePassOptions::new(1))?;
        }
        if matches!(case.content, Content::Mixed64) {
            for (index, panel) in self.mixed_panels.iter().enumerate() {
                let order = 1 + index as i32 * 2;
                frame.draw_screen_scene(panel, FramePassOptions::new(order))?;
                frame.draw_image(
                    &self.image,
                    None,
                    Color::WHITE,
                    ImageSampling::Nearest,
                    FramePassOptions::new(order + 1).with_viewport(LogicalViewportRegion::new(
                        LogicalScreenPosition::new(
                            (index % 8) as f32 * 140.0,
                            (index / 8) as f32 * 80.0 + 32.0,
                        ),
                        LogicalViewport::new(24.0, 24.0)?,
                    )?),
                )?;
            }
        }
        let tick = self.samples.draws as f32;
        for (index, run) in self.runs[..case.labels].iter().enumerate() {
            let x = (index % 10) as f32 * 112.0
                + if matches!(case.content, Content::Moved) {
                    tick.sin() * 8.0
                } else {
                    0.0
                };
            let y = (index / 10) as f32 * 7.0;
            let tint = if matches!(case.content, Content::Recolored) {
                Color::rgb(0.5 + tick.sin() * 0.5, 0.5, 1.0)
            } else {
                Color::WHITE
            };
            let order = 1 + index as i32 * 3;
            if matches!(case.content, Content::Mixed) {
                frame.draw_screen_scene(&self.panel, FramePassOptions::new(order))?;
                frame.draw_image(
                    &self.image,
                    Some(self.image.full_rect()),
                    Color::WHITE,
                    ImageSampling::Nearest,
                    FramePassOptions::new(order + 1).with_viewport(LogicalViewportRegion::new(
                        LogicalScreenPosition::new(0.0, 0.0),
                        LogicalViewport::new(12.0, 12.0)?,
                    )?),
                )?;
            }
            frame.draw_glyph_run_placed(
                &self.atlas,
                run,
                ImageBatchPlacement::new(LogicalScreenVector::new(x, y), tint)?,
                ImageSampling::Nearest,
                FramePassOptions::new(order + 2),
            )?;
        }
        Ok(frame.present()?)
    }
}

struct App {
    benchmark: Option<Benchmark>,
    frames: usize,
    selection: Selection,
    error: Option<String>,
}

impl ApplicationHandler for App {
    fn resumed(&mut self, events: &ActiveEventLoop) {
        if self.benchmark.is_some() {
            return;
        }
        match Benchmark::new(events, self.frames, self.selection) {
            Ok(benchmark) => {
                benchmark.window.request_redraw();
                self.benchmark = Some(benchmark);
            }
            Err(error) => {
                self.error = Some(error.to_string());
                events.exit();
            }
        }
    }
    fn window_event(&mut self, events: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
        let Some(benchmark) = &mut self.benchmark else {
            return;
        };
        if benchmark.window.id() != id {
            return;
        }
        let result: Result<bool> = match event {
            WindowEvent::CloseRequested => Err("benchmark window closed before completion".into()),
            WindowEvent::Resized(size) => benchmark
                .renderer
                .resize_with_scale_factor(size.width, size.height, benchmark.window.scale_factor())
                .map(|_| false)
                .map_err(Into::into),
            WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
                let size = benchmark.window.inner_size();
                benchmark
                    .renderer
                    .resize_with_scale_factor(size.width, size.height, scale_factor)
                    .map(|_| false)
                    .map_err(Into::into)
            }
            WindowEvent::RedrawRequested => benchmark.redraw(),
            _ => Ok(false),
        };
        match result {
            Ok(true) => events.exit(),
            Err(error) => {
                self.error = Some(error.to_string());
                events.exit();
            }
            _ => {}
        }
    }
    fn about_to_wait(&mut self, _: &ActiveEventLoop) {
        if let Some(benchmark) = &self.benchmark {
            benchmark.window.request_redraw();
        }
    }
}

fn main() -> Result<()> {
    env_logger::init();
    let mut arguments = std::env::args().skip(1);
    let mut frames = 60;
    let mut selection = Selection::default();
    while let Some(argument) = arguments.next() {
        match argument.as_str() {
            "--frames" => {
                frames = arguments
                    .next()
                    .ok_or("--frames requires a count")?
                    .parse()?;
            }
            "--quick" => selection.quick = true,
            "--case" => {
                selection.set_content(&arguments.next().ok_or("--case requires a name")?)?
            }
            "--labels" => {
                selection.labels = Some(
                    arguments
                        .next()
                        .ok_or("--labels requires a count")?
                        .parse()?,
                )
            }
            "--cache" => {
                selection.set_cache(&arguments.next().ok_or("--cache requires on/off/both")?)?
            }
            "--trials" => {
                selection.trials = arguments
                    .next()
                    .ok_or("--trials requires a count")?
                    .parse()?
            }
            "--upload-staging" => {
                selection.upload_staging_bytes = Some(
                    arguments
                        .next()
                        .ok_or("--upload-staging requires bytes (0 disables batching)")?
                        .parse()?,
                );
            }
            "--help" => {
                println!(
                    "frame_cache_benchmark [--frames 1..10000] [--quick] [--case world|world_screen|mixed64_images128rects|unchanged|recolored|moved|mixed] [--labels 0|1|100|1000] [--cache on|off|both] [--trials 1..100] [--upload-staging BYTES]\nEach trial warms up 20 presents; trial order alternates. Defaults retain all 30 cases. --upload-staging 0 keeps the cache but disables packed uniform uploads. This is diagnostic, not a release gate."
                );
                return Ok(());
            }
            _ => return Err(format!("unknown argument {argument}").into()),
        }
    }
    if !(1..=10000).contains(&frames) {
        return Err("--frames must be in 1..=10000".into());
    }
    selection.cases()?;
    let mut app = App {
        benchmark: None,
        frames,
        selection,
        error: None,
    };
    let events = EventLoop::new()?;
    events.set_control_flow(ControlFlow::Poll);
    events.run_app(&mut app)?;
    if let Some(error) = app.error {
        return Err(error.into());
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fixed_controls_keep_requested_scene_and_image_command_counts() {
        assert_eq!(
            expected_counts(Case {
                content: Content::WorldScreen,
                labels: 0,
                cached: true
            }),
            (2, 0, 0, 356, 2)
        );
        assert_eq!(
            expected_counts(Case {
                content: Content::Mixed64,
                labels: 0,
                cached: true
            }),
            (65, 64, 0, 292, 129)
        );
        assert_eq!(
            expected_counts(Case {
                content: Content::Labels,
                labels: 1000,
                cached: true
            }),
            (0, 0, 1000, 1000, 1000)
        );
    }

    #[test]
    fn percentile_uses_nearest_rank_without_dropping_the_last_sample() {
        let mut times = (1..=20)
            .rev()
            .map(Duration::from_millis)
            .collect::<Vec<_>>();
        assert_eq!(p95(&mut times), Duration::from_millis(19));
        assert_eq!(
            p95(&mut [Duration::from_millis(7)]),
            Duration::from_millis(7)
        );
    }
}