euv-performance 0.48.3

Reproducible CPU, layout, rasterization, and allocation benchmarks for the cross-platform euv runtime.
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
use super::*;

/// Accessors and builder methods for `StressMatrixReport`.
impl StressMatrixReport {
    /// Returns the optional interleaved A/B partial-layout damage summary if that scenario ran.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Stress-matrix report instance.
    ///
    /// # Returns
    ///
    /// - `Option<&PartialDamageAbSummary>` - Borrowed A/B comparison summary when present.
    #[must_use]
    pub fn partial_damage_ab(&self) -> Option<&PartialDamageAbSummary> {
        let Self {
            partial_damage_ab, ..
        } = self;
        partial_damage_ab.as_ref()
    }

    /// Returns the optional cold-start TTFF stage breakdown report when that scenario ran.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Stress-matrix report instance.
    ///
    /// # Returns
    ///
    /// - `Option<&ColdStartTtffReport>` - Borrowed TTFF breakdown when present.
    #[must_use]
    pub fn cold_start_ttff(&self) -> Option<&ColdStartTtffReport> {
        let Self {
            cold_start_ttff, ..
        } = self;
        cold_start_ttff.as_ref()
    }

    /// Attaches a completed `ColdStartTtffReport` to this stress-matrix report.
    ///
    /// # Arguments
    ///
    /// - `Self` - Stress-matrix report builder.
    /// - `ColdStartTtffReport` - Stage-attributed cold-start TTFF report.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated stress-matrix report carrying the TTFF breakdown.
    #[must_use]
    pub fn with_cold_start_ttff(mut self, report: ColdStartTtffReport) -> Self {
        let Self {
            cold_start_ttff, ..
        } = &mut self;
        *cold_start_ttff = Some(report);
        self
    }
}

/// Pre-allocated frame timing, allocation, and layout/paint accumulation helpers for `FrameMeasureAccum`.
impl FrameMeasureAccum {
    /// Allocates a `FrameMeasureAccum` with pre-sized buffers for `capacity` frames.
    ///
    /// # Arguments
    ///
    /// - `usize` - Expected number of measured frames.
    ///
    /// # Returns
    ///
    /// - `Self` - Pre-allocated telemetry accumulator.
    #[must_use]
    pub(crate) fn with_capacity(capacity: usize) -> Self {
        Self {
            samples_us: Vec::with_capacity(capacity),
            dirty_ratios: Vec::with_capacity(capacity),
            full_redraw_count: 0,
            alloc_count_total: 0,
            free_count_total: 0,
            bytes_allocated_total: 0,
            max_allocs_per_frame: 0,
            max_bytes_per_frame: 0,
            peak_live_bytes: 0,
            layout_invalidations: 0,
            layout_passes: 0,
            layout_changed_passes: 0,
            layout_changed_nodes_total: 0,
            draw_commands_total: 0,
            pixels_shaded_total: 0,
        }
    }

    /// Records one measured frame's wall duration and exact heap allocation delta.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable telemetry accumulator.
    /// - `f64` - Elapsed frame wall time in microseconds.
    /// - `AllocationSnapshot` - Exact heap allocation delta for this frame.
    pub(crate) fn record_frame(&mut self, elapsed_us: f64, alloc_delta: AllocationSnapshot) {
        let Self {
            samples_us,
            alloc_count_total,
            free_count_total,
            bytes_allocated_total,
            max_allocs_per_frame,
            max_bytes_per_frame,
            peak_live_bytes,
            ..
        } = self;
        samples_us.push(elapsed_us);
        let frame_allocs: u64 = alloc_delta.get_alloc_count();
        let frame_bytes: u64 = alloc_delta.get_bytes_allocated();
        *alloc_count_total = alloc_count_total.saturating_add(frame_allocs);
        *free_count_total = free_count_total.saturating_add(alloc_delta.get_free_count());
        *bytes_allocated_total = bytes_allocated_total.saturating_add(frame_bytes);
        *max_allocs_per_frame = (*max_allocs_per_frame).max(frame_allocs);
        *max_bytes_per_frame = (*max_bytes_per_frame).max(frame_bytes);
        *peak_live_bytes = (*peak_live_bytes).max(alloc_delta.get_peak_live_bytes());
    }

    /// Records per-frame damage telemetry from `FrameTelemetry`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable telemetry accumulator.
    /// - `&FrameTelemetry` - Frame telemetry snapshot captured after `step_frame`.
    pub(crate) fn record_frame_telemetry(&mut self, telemetry: &FrameTelemetry) {
        let Self {
            dirty_ratios,
            full_redraw_count,
            draw_commands_total,
            pixels_shaded_total,
            ..
        } = self;
        dirty_ratios.push(telemetry.get_dirty_area_ratio());
        if telemetry.get_full_redraw_reason() != FullRedrawReason::None {
            *full_redraw_count = full_redraw_count.saturating_add(1);
        }
        *draw_commands_total =
            draw_commands_total.saturating_add(telemetry.get_draw_commands() as u64);
        *pixels_shaded_total =
            pixels_shaded_total.saturating_add(telemetry.get_pixels_shaded() as u64);
    }

    /// Records explicit draw-command and shaded-pixel counts for scenarios that render outside `App`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable telemetry accumulator.
    /// - `usize` - Display-list draw commands recorded this frame.
    /// - `usize` - Physical pixels shaded this frame.
    pub(crate) fn record_paint_counts(&mut self, draw_commands: usize, pixels_shaded: usize) {
        let Self {
            draw_commands_total,
            pixels_shaded_total,
            ..
        } = self;
        *draw_commands_total = draw_commands_total.saturating_add(draw_commands as u64);
        *pixels_shaded_total = pixels_shaded_total.saturating_add(pixels_shaded as u64);
    }

    /// Stores the exact cumulative `LayoutTelemetry` delta between window start and window end.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable telemetry accumulator.
    /// - `LayoutTelemetry` - Snapshot taken immediately before the measured window.
    /// - `LayoutTelemetry` - Snapshot taken immediately after the measured window.
    pub(crate) fn set_layout_delta(&mut self, before: LayoutTelemetry, after: LayoutTelemetry) {
        let Self {
            layout_invalidations,
            layout_passes,
            layout_changed_passes,
            layout_changed_nodes_total,
            ..
        } = self;
        let passes: u64 = after.get_passes().saturating_sub(before.get_passes());
        let changed_passes: u64 = after
            .get_changed_passes()
            .saturating_sub(before.get_changed_passes())
            .min(passes);
        *layout_invalidations = after
            .get_invalidations()
            .saturating_sub(before.get_invalidations());
        *layout_passes = passes;
        *layout_changed_passes = changed_passes;
        *layout_changed_nodes_total = after
            .get_changed_nodes_total()
            .saturating_sub(before.get_changed_nodes_total());
    }

    /// Finalizes all recorded samples into a `LatencyDistribution`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Completed telemetry accumulator.
    ///
    /// # Returns
    ///
    /// - `LatencyDistribution` - Computed percentiles (`p50..p99.9`), 240/360/480Hz budgets, allocations, and layout counts.
    #[must_use]
    pub(crate) fn finish(&self) -> LatencyDistribution {
        summarize_accumulated_latency(self)
    }
}

/// Initialization helpers for `BenchGame2dScene`.
impl BenchGame2dScene {
    /// Builds a deterministic 40-entity 2D physics arena with tilemap, particles, and double-buffered snapshots.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready 2D game benchmark scene.
    #[must_use]
    pub(crate) fn new_arena() -> Self {
        let mut tilemap: TileMap = TileMap::empty(24, 16, 32.0);
        let floor_a: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
            Color::hex(0x0B1120),
            Color::rgba(30, 41, 59, 90),
            false,
        ));
        let floor_b: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
            Color::hex(0x0F172A),
            Color::rgba(30, 41, 59, 90),
            false,
        ));
        let wall_tile: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
            Color::hex(0x1E293B),
            Color::hex(0x38BDF8),
            true,
        ));
        for ty in 0..tilemap.get_rows() {
            for tx in 0..tilemap.get_columns() {
                let border: bool = tx == 0
                    || ty == 0
                    || tx + 1 == tilemap.get_columns()
                    || ty + 1 == tilemap.get_rows();
                let tid: u16 = if border {
                    wall_tile
                } else if (tx + ty) % 2 == 0 {
                    floor_a
                } else {
                    floor_b
                };
                tilemap.set_tile(tx, ty, tid);
            }
        }
        let world_rect: Rect = tilemap.world_bounds();
        let inner_arena: Rect = Rect::new(
            36.0,
            36.0,
            (world_rect.get_width() - 72.0).max(200.0),
            (world_rect.get_height() - 72.0).max(200.0),
        );
        let mut world: GameWorld = GameWorld::with_capacity(128);
        let player_id: EntityId = world.spawn(EntityTag::Player, Point::new(280.0, 220.0));
        if let Some(player) = world.get_entity_mut(player_id) {
            player.set_body(Some(RigidBody2D::dynamic(1.0, 0.4, 1.5)));
            player.set_collider(Some(Collider2D::circle(
                11.0,
                COLLISION_LAYER_PLAYER,
                COLLISION_LAYER_WORLD,
                false,
            )));
            player.set_sprite(Some(Sprite2D::new(
                EntityVisualShape::Triangle,
                Size::new(22.0, 22.0),
                Color::hex(0x38BDF8),
                Color::hex(0xE0F2FE),
                10,
                0.0,
                true,
            )));
        }
        for idx in 0..39usize {
            let col: f32 = (idx % 8) as f32;
            let row: f32 = (idx / 8) as f32;
            let x: f32 = 80.0 + col * 76.0;
            let y: f32 = 70.0 + row * 72.0;
            let eid: EntityId = world.spawn(EntityTag::Enemy, Point::new(x, y));
            if let Some(ent) = world.get_entity_mut(eid) {
                ent.set_velocity(Velocity2D::new(
                    if idx % 2 == 0 { 95.0 } else { -85.0 },
                    if idx % 3 == 0 { 70.0 } else { -65.0 },
                    0.0,
                ));
                ent.set_body(Some(RigidBody2D::dynamic(1.0, 0.85, 0.2)));
                ent.set_collider(Some(Collider2D::circle(
                    9.0,
                    COLLISION_LAYER_ENEMY,
                    COLLISION_LAYER_WORLD | COLLISION_LAYER_ENEMY,
                    false,
                )));
                let shape: EntityVisualShape = match idx % 3 {
                    0 => EntityVisualShape::Circle,
                    1 => EntityVisualShape::Diamond,
                    _ => EntityVisualShape::Rect,
                };
                ent.set_sprite(Some(Sprite2D::new(
                    shape,
                    Size::new(18.0, 18.0),
                    Color::hex(0xF59E0B),
                    Color::hex(0xFEF08A),
                    6,
                    0.0,
                    true,
                )));
            }
        }
        let mut phys_cfg: PhysicsConfig = PhysicsConfig::default();
        phys_cfg.set_arena_bounds(Some(inner_arena));
        let mut camera: Camera2D = Camera2D::at(Point::new(384.0, 256.0), 1.0);
        camera.set_world_bounds(Some(world_rect));
        let init_snap: World2DSnapshot = World2DSnapshot::capture(&world, camera, true, Vec::new());
        Self {
            world,
            physics: PhysicsWorld2D::with_config(phys_cfg),
            particles: ParticleSystem::with_capacity(256),
            tilemap,
            camera,
            snapshots: DoubleBufferedSnapshot::new(init_snap),
            player_id,
            fixed_steps: 0,
            rendered_frames: 0,
            hud_text_buf: String::with_capacity(96),
        }
    }
}

/// Fixed-step simulation and 240Hz/480Hz interpolated rendering for `BenchGame2dScene`.
impl GameScene for BenchGame2dScene {
    /// Advances deterministic 2D physics, particle emission, camera follow, and double-buffered snapshot commit.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable 2D benchmark scene.
    /// - `f32` - Fixed simulation timestep in seconds.
    /// - `&InputState` - Real-time polled input state.
    fn fixed_update(&mut self, dt: f32, _input: &InputState) {
        let next_tick: u64 = self.get_fixed_steps().saturating_add(1);
        self.set_fixed_steps(next_tick);
        let phase: f32 = (next_tick as f32) * 0.08;
        let player_id: EntityId = self.get_player_id();
        if let Some(player) = self.get_mut_world().get_entity_mut(player_id) {
            player.set_velocity(Velocity2D::new(
                phase.cos() * 140.0,
                phase.sin() * 110.0,
                0.0,
            ));
        }
        let Self {
            world,
            physics,
            particles,
            camera,
            snapshots,
            ..
        } = self;
        let _: &[CollisionEvent] = physics.step_in_place(world, dt);
        if let Some(player) = world.get_entity(player_id) {
            let pos: Point = player.get_transform().get_position();
            camera.follow(pos, 8.0, dt, Size::new(800.0, 520.0));
            if next_tick.is_multiple_of(2) {
                let emitter: ParticleEmitterConfig = ParticleEmitterConfig::default();
                particles.emit_burst(pos, 3, emitter);
            }
        }
        let _: usize = particles.update(dt);
        let snap_cam: Camera2D = *camera;
        snapshots.capture_into(|dst: &mut World2DSnapshot| {
            dst.capture_into(world, snap_cam, false, &[]);
        });
    }

    /// Renders the interpolated 2D arena, tilemap, particles, and HUD at 240Hz/480Hz cadence.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable 2D benchmark scene.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    /// - `&mut GameRenderContext<'_>` - Immediate-mode game draw context.
    fn render(&mut self, alpha: f32, ctx: &mut GameRenderContext<'_>) {
        let next_frame: u64 = self.get_rendered_frames().saturating_add(1);
        self.set_rendered_frames(next_frame);
        ctx.clear(Color::hex(0x020617));
        let Self {
            world,
            particles,
            tilemap,
            snapshots,
            hud_text_buf,
            ..
        } = self;
        let interp: &World2DSnapshot = snapshots.sample_cached(alpha);
        let cam: Camera2D = interp.get_camera();
        ctx.render_tilemap(tilemap, &cam);
        let drawn_entities: usize = ctx.render_snapshot_world(world, interp);
        let drawn_particles: usize = ctx.render_particles(particles, &cam);
        ctx.fill_rounded_rect(
            Rect::new(10.0, 10.0, 320.0, 26.0),
            Color::rgba(15, 23, 42, 210),
            6.0,
        );
        hud_text_buf.clear();
        let _: core::fmt::Result = write!(
            hud_text_buf,
            "{STRESS_GAME2D_HUD_PREFIX} | Ent {drawn_entities} | Part {drawn_particles} | F{next_frame}"
        );
        ctx.draw_text_str(
            Point::new(18.0, 16.0),
            hud_text_buf.as_str(),
            12.0,
            Color::hex(0xE2E8F0),
        );
    }
}

/// Initialization helpers for `BenchGame3dGpuScene`.
impl BenchGame3dGpuScene {
    /// Builds an open-world 3D city bound to a valid same-device `GpuCompositeTarget` for zero-readback compositing benchmarks.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready 3D GPU composite benchmark scene.
    #[must_use]
    pub(crate) fn new_gpu_city() -> Self {
        let mut world: World3D = World3D::create_open_world_city();
        world.ensure_bvh();
        let init_snap: World3DSnapshot =
            World3DSnapshot::capture(&world, 0, true, Vec::new(), Vec::new());
        let mut gpu_target: GpuCompositeTarget = GpuCompositeTarget::default();
        let tex: GpuTextureHandle =
            GpuTextureHandle::new(77, 7, 1, (800, 450), GpuTextureFormat::Rgba8Unorm, 1, true);
        gpu_target.attach_handle(tex, Rect::new(0.0, 0.0, 800.0, 450.0));
        Self {
            world,
            actions: ActionMap::default(),
            snapshots: DoubleBufferedSnapshot::new(init_snap),
            gpu_target,
            frame_fence: 0,
            fixed_steps: 0,
            rendered_frames: 0,
            hud_text_buf: String::with_capacity(96),
        }
    }
}

/// Fixed-step 3D world simulation and zero-readback GPU direct-composite rendering for `BenchGame3dGpuScene`.
impl GameScene for BenchGame3dGpuScene {
    /// Steps vehicle dynamics, incremental dynamic BVH refit, and snapshot commit at fixed 60Hz cadence.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable 3D GPU benchmark scene.
    /// - `f32` - Fixed simulation timestep in seconds.
    /// - `&InputState` - Real-time polled input state.
    fn fixed_update(&mut self, dt: f32, input: &InputState) {
        let next_tick: u64 = self.get_fixed_steps().saturating_add(1);
        self.set_fixed_steps(next_tick);
        let Self {
            world,
            actions,
            snapshots,
            gpu_target,
            ..
        } = self;
        world.step_simulation(dt, input, actions);
        world.ensure_bvh();
        snapshots.capture_into(|dst: &mut World3DSnapshot| {
            dst.capture_into(world, next_tick, false, &[], &[]);
        });
        gpu_target.mark_world_dirty();
    }

    /// Emits a zero-readback `GpuCompositeTarget` texture bind and lightweight HUD overlay at 240Hz/480Hz cadence.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable 3D GPU benchmark scene.
    /// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
    /// - `&mut GameRenderContext<'_>` - Immediate-mode game draw context.
    fn render(&mut self, alpha: f32, ctx: &mut GameRenderContext<'_>) {
        let next_frame: u64 = self.get_rendered_frames().saturating_add(1);
        self.set_rendered_frames(next_frame);
        let next_fence: u64 = self.get_frame_fence().saturating_add(1);
        self.set_frame_fence(next_fence);
        let size: Size = ctx.viewport_size();
        let dst: Rect = Rect::new(0.0, 0.0, size.get_width(), size.get_height());
        let handle: GpuTextureHandle = self.get_gpu_target().get_handle();
        let _: bool = ctx.composite_gpu_texture(dst, handle);
        if !self.get_gpu_target().needs_world_render() {
            self.get_mut_gpu_target().mark_hud_dirty();
        }
        let _: Option<GpuCompositeDescriptor> = self
            .get_mut_gpu_target()
            .record_composite_present(next_fence);
        ctx.fill_rounded_rect(
            Rect::new(10.0, 10.0, 340.0, 26.0),
            Color::rgba(15, 23, 42, 210),
            6.0,
        );
        let Self { hud_text_buf, .. } = self;
        hud_text_buf.clear();
        let _: core::fmt::Result = write!(
            hud_text_buf,
            "{STRESS_GAME3D_HUD_PREFIX} | GPU Direct | a={alpha:.2} | F{next_frame}"
        );
        ctx.draw_text_str(
            Point::new(18.0, 16.0),
            hud_text_buf.as_str(),
            12.0,
            Color::hex(0xE2E8F0),
        );
    }
}