1use super::*;
2
3impl StressMatrixReport {
5 #[must_use]
15 pub fn partial_damage_ab(&self) -> Option<&PartialDamageAbSummary> {
16 let Self {
17 partial_damage_ab, ..
18 } = self;
19 partial_damage_ab.as_ref()
20 }
21
22 #[must_use]
32 pub fn cold_start_ttff(&self) -> Option<&ColdStartTtffReport> {
33 let Self {
34 cold_start_ttff, ..
35 } = self;
36 cold_start_ttff.as_ref()
37 }
38
39 #[must_use]
50 pub fn with_cold_start_ttff(mut self, report: ColdStartTtffReport) -> Self {
51 let Self {
52 cold_start_ttff, ..
53 } = &mut self;
54 *cold_start_ttff = Some(report);
55 self
56 }
57}
58
59impl FrameMeasureAccum {
61 #[must_use]
71 pub(crate) fn with_capacity(capacity: usize) -> Self {
72 Self {
73 samples_us: Vec::with_capacity(capacity),
74 dirty_ratios: Vec::with_capacity(capacity),
75 full_redraw_count: 0,
76 alloc_count_total: 0,
77 free_count_total: 0,
78 bytes_allocated_total: 0,
79 max_allocs_per_frame: 0,
80 max_bytes_per_frame: 0,
81 peak_live_bytes: 0,
82 layout_invalidations: 0,
83 layout_passes: 0,
84 layout_changed_passes: 0,
85 layout_changed_nodes_total: 0,
86 draw_commands_total: 0,
87 pixels_shaded_total: 0,
88 }
89 }
90
91 pub(crate) fn record_frame(&mut self, elapsed_us: f64, alloc_delta: AllocationSnapshot) {
99 let Self {
100 samples_us,
101 alloc_count_total,
102 free_count_total,
103 bytes_allocated_total,
104 max_allocs_per_frame,
105 max_bytes_per_frame,
106 peak_live_bytes,
107 ..
108 } = self;
109 samples_us.push(elapsed_us);
110 let frame_allocs: u64 = alloc_delta.get_alloc_count();
111 let frame_bytes: u64 = alloc_delta.get_bytes_allocated();
112 *alloc_count_total = alloc_count_total.saturating_add(frame_allocs);
113 *free_count_total = free_count_total.saturating_add(alloc_delta.get_free_count());
114 *bytes_allocated_total = bytes_allocated_total.saturating_add(frame_bytes);
115 *max_allocs_per_frame = (*max_allocs_per_frame).max(frame_allocs);
116 *max_bytes_per_frame = (*max_bytes_per_frame).max(frame_bytes);
117 *peak_live_bytes = (*peak_live_bytes).max(alloc_delta.get_peak_live_bytes());
118 }
119
120 pub(crate) fn record_frame_telemetry(&mut self, telemetry: &FrameTelemetry) {
127 let Self {
128 dirty_ratios,
129 full_redraw_count,
130 draw_commands_total,
131 pixels_shaded_total,
132 ..
133 } = self;
134 dirty_ratios.push(telemetry.get_dirty_area_ratio());
135 if telemetry.get_full_redraw_reason() != FullRedrawReason::None {
136 *full_redraw_count = full_redraw_count.saturating_add(1);
137 }
138 *draw_commands_total =
139 draw_commands_total.saturating_add(telemetry.get_draw_commands() as u64);
140 *pixels_shaded_total =
141 pixels_shaded_total.saturating_add(telemetry.get_pixels_shaded() as u64);
142 }
143
144 pub(crate) fn record_paint_counts(&mut self, draw_commands: usize, pixels_shaded: usize) {
152 let Self {
153 draw_commands_total,
154 pixels_shaded_total,
155 ..
156 } = self;
157 *draw_commands_total = draw_commands_total.saturating_add(draw_commands as u64);
158 *pixels_shaded_total = pixels_shaded_total.saturating_add(pixels_shaded as u64);
159 }
160
161 pub(crate) fn set_layout_delta(&mut self, before: LayoutTelemetry, after: LayoutTelemetry) {
169 let Self {
170 layout_invalidations,
171 layout_passes,
172 layout_changed_passes,
173 layout_changed_nodes_total,
174 ..
175 } = self;
176 let passes: u64 = after.get_passes().saturating_sub(before.get_passes());
177 let changed_passes: u64 = after
178 .get_changed_passes()
179 .saturating_sub(before.get_changed_passes())
180 .min(passes);
181 *layout_invalidations = after
182 .get_invalidations()
183 .saturating_sub(before.get_invalidations());
184 *layout_passes = passes;
185 *layout_changed_passes = changed_passes;
186 *layout_changed_nodes_total = after
187 .get_changed_nodes_total()
188 .saturating_sub(before.get_changed_nodes_total());
189 }
190
191 #[must_use]
201 pub(crate) fn finish(&self) -> LatencyDistribution {
202 summarize_accumulated_latency(self)
203 }
204}
205
206impl BenchGame2dScene {
208 #[must_use]
214 pub(crate) fn new_arena() -> Self {
215 let mut tilemap: TileMap = TileMap::empty(24, 16, 32.0);
216 let floor_a: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
217 Color::hex(0x0B1120),
218 Color::rgba(30, 41, 59, 90),
219 false,
220 ));
221 let floor_b: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
222 Color::hex(0x0F172A),
223 Color::rgba(30, 41, 59, 90),
224 false,
225 ));
226 let wall_tile: u16 = tilemap.add_palette_entry(TilePaletteEntry::new(
227 Color::hex(0x1E293B),
228 Color::hex(0x38BDF8),
229 true,
230 ));
231 for ty in 0..tilemap.get_rows() {
232 for tx in 0..tilemap.get_columns() {
233 let border: bool = tx == 0
234 || ty == 0
235 || tx + 1 == tilemap.get_columns()
236 || ty + 1 == tilemap.get_rows();
237 let tid: u16 = if border {
238 wall_tile
239 } else if (tx + ty) % 2 == 0 {
240 floor_a
241 } else {
242 floor_b
243 };
244 tilemap.set_tile(tx, ty, tid);
245 }
246 }
247 let world_rect: Rect = tilemap.world_bounds();
248 let inner_arena: Rect = Rect::new(
249 36.0,
250 36.0,
251 (world_rect.get_width() - 72.0).max(200.0),
252 (world_rect.get_height() - 72.0).max(200.0),
253 );
254 let mut world: GameWorld = GameWorld::with_capacity(128);
255 let player_id: EntityId = world.spawn(EntityTag::Player, Point::new(280.0, 220.0));
256 if let Some(player) = world.get_entity_mut(player_id) {
257 player.set_body(Some(RigidBody2D::dynamic(1.0, 0.4, 1.5)));
258 player.set_collider(Some(Collider2D::circle(
259 11.0,
260 COLLISION_LAYER_PLAYER,
261 COLLISION_LAYER_WORLD,
262 false,
263 )));
264 player.set_sprite(Some(Sprite2D::new(
265 EntityVisualShape::Triangle,
266 Size::new(22.0, 22.0),
267 Color::hex(0x38BDF8),
268 Color::hex(0xE0F2FE),
269 10,
270 0.0,
271 true,
272 )));
273 }
274 for idx in 0..39usize {
275 let col: f32 = (idx % 8) as f32;
276 let row: f32 = (idx / 8) as f32;
277 let x: f32 = 80.0 + col * 76.0;
278 let y: f32 = 70.0 + row * 72.0;
279 let eid: EntityId = world.spawn(EntityTag::Enemy, Point::new(x, y));
280 if let Some(ent) = world.get_entity_mut(eid) {
281 ent.set_velocity(Velocity2D::new(
282 if idx % 2 == 0 { 95.0 } else { -85.0 },
283 if idx % 3 == 0 { 70.0 } else { -65.0 },
284 0.0,
285 ));
286 ent.set_body(Some(RigidBody2D::dynamic(1.0, 0.85, 0.2)));
287 ent.set_collider(Some(Collider2D::circle(
288 9.0,
289 COLLISION_LAYER_ENEMY,
290 COLLISION_LAYER_WORLD | COLLISION_LAYER_ENEMY,
291 false,
292 )));
293 let shape: EntityVisualShape = match idx % 3 {
294 0 => EntityVisualShape::Circle,
295 1 => EntityVisualShape::Diamond,
296 _ => EntityVisualShape::Rect,
297 };
298 ent.set_sprite(Some(Sprite2D::new(
299 shape,
300 Size::new(18.0, 18.0),
301 Color::hex(0xF59E0B),
302 Color::hex(0xFEF08A),
303 6,
304 0.0,
305 true,
306 )));
307 }
308 }
309 let mut phys_cfg: PhysicsConfig = PhysicsConfig::default();
310 phys_cfg.set_arena_bounds(Some(inner_arena));
311 let mut camera: Camera2D = Camera2D::at(Point::new(384.0, 256.0), 1.0);
312 camera.set_world_bounds(Some(world_rect));
313 let init_snap: World2DSnapshot = World2DSnapshot::capture(&world, camera, true, Vec::new());
314 Self {
315 world,
316 physics: PhysicsWorld2D::with_config(phys_cfg),
317 particles: ParticleSystem::with_capacity(256),
318 tilemap,
319 camera,
320 snapshots: DoubleBufferedSnapshot::new(init_snap),
321 player_id,
322 fixed_steps: 0,
323 rendered_frames: 0,
324 hud_text_buf: String::with_capacity(96),
325 }
326 }
327}
328
329impl GameScene for BenchGame2dScene {
331 fn fixed_update(&mut self, dt: f32, _input: &InputState) {
339 let next_tick: u64 = self.get_fixed_steps().saturating_add(1);
340 self.set_fixed_steps(next_tick);
341 let phase: f32 = (next_tick as f32) * 0.08;
342 let player_id: EntityId = self.get_player_id();
343 if let Some(player) = self.get_mut_world().get_entity_mut(player_id) {
344 player.set_velocity(Velocity2D::new(
345 phase.cos() * 140.0,
346 phase.sin() * 110.0,
347 0.0,
348 ));
349 }
350 let Self {
351 world,
352 physics,
353 particles,
354 camera,
355 snapshots,
356 ..
357 } = self;
358 let _: &[CollisionEvent] = physics.step_in_place(world, dt);
359 if let Some(player) = world.get_entity(player_id) {
360 let pos: Point = player.get_transform().get_position();
361 camera.follow(pos, 8.0, dt, Size::new(800.0, 520.0));
362 if next_tick.is_multiple_of(2) {
363 let emitter: ParticleEmitterConfig = ParticleEmitterConfig::default();
364 particles.emit_burst(pos, 3, emitter);
365 }
366 }
367 let _: usize = particles.update(dt);
368 let snap_cam: Camera2D = *camera;
369 snapshots.capture_into(|dst: &mut World2DSnapshot| {
370 dst.capture_into(world, snap_cam, false, &[]);
371 });
372 }
373
374 fn render(&mut self, alpha: f32, ctx: &mut GameRenderContext<'_>) {
382 let next_frame: u64 = self.get_rendered_frames().saturating_add(1);
383 self.set_rendered_frames(next_frame);
384 ctx.clear(Color::hex(0x020617));
385 let Self {
386 world,
387 particles,
388 tilemap,
389 snapshots,
390 hud_text_buf,
391 ..
392 } = self;
393 let interp: &World2DSnapshot = snapshots.sample_cached(alpha);
394 let cam: Camera2D = interp.get_camera();
395 ctx.render_tilemap(tilemap, &cam);
396 let drawn_entities: usize = ctx.render_snapshot_world(world, interp);
397 let drawn_particles: usize = ctx.render_particles(particles, &cam);
398 ctx.fill_rounded_rect(
399 Rect::new(10.0, 10.0, 320.0, 26.0),
400 Color::rgba(15, 23, 42, 210),
401 6.0,
402 );
403 hud_text_buf.clear();
404 let _: core::fmt::Result = write!(
405 hud_text_buf,
406 "{STRESS_GAME2D_HUD_PREFIX} | Ent {drawn_entities} | Part {drawn_particles} | F{next_frame}"
407 );
408 ctx.draw_text_str(
409 Point::new(18.0, 16.0),
410 hud_text_buf.as_str(),
411 12.0,
412 Color::hex(0xE2E8F0),
413 );
414 }
415}
416
417impl BenchGame3dGpuScene {
419 #[must_use]
425 pub(crate) fn new_gpu_city() -> Self {
426 let mut world: World3D = World3D::create_open_world_city();
427 world.ensure_bvh();
428 let init_snap: World3DSnapshot =
429 World3DSnapshot::capture(&world, 0, true, Vec::new(), Vec::new());
430 let mut gpu_target: GpuCompositeTarget = GpuCompositeTarget::default();
431 let tex: GpuTextureHandle =
432 GpuTextureHandle::new(77, 7, 1, (800, 450), GpuTextureFormat::Rgba8Unorm, 1, true);
433 gpu_target.attach_handle(tex, Rect::new(0.0, 0.0, 800.0, 450.0));
434 Self {
435 world,
436 actions: ActionMap::default(),
437 snapshots: DoubleBufferedSnapshot::new(init_snap),
438 gpu_target,
439 frame_fence: 0,
440 fixed_steps: 0,
441 rendered_frames: 0,
442 hud_text_buf: String::with_capacity(96),
443 }
444 }
445}
446
447impl GameScene for BenchGame3dGpuScene {
449 fn fixed_update(&mut self, dt: f32, input: &InputState) {
457 let next_tick: u64 = self.get_fixed_steps().saturating_add(1);
458 self.set_fixed_steps(next_tick);
459 let Self {
460 world,
461 actions,
462 snapshots,
463 gpu_target,
464 ..
465 } = self;
466 world.step_simulation(dt, input, actions);
467 world.ensure_bvh();
468 snapshots.capture_into(|dst: &mut World3DSnapshot| {
469 dst.capture_into(world, next_tick, false, &[], &[]);
470 });
471 gpu_target.mark_world_dirty();
472 }
473
474 fn render(&mut self, alpha: f32, ctx: &mut GameRenderContext<'_>) {
482 let next_frame: u64 = self.get_rendered_frames().saturating_add(1);
483 self.set_rendered_frames(next_frame);
484 let next_fence: u64 = self.get_frame_fence().saturating_add(1);
485 self.set_frame_fence(next_fence);
486 let size: Size = ctx.viewport_size();
487 let dst: Rect = Rect::new(0.0, 0.0, size.get_width(), size.get_height());
488 let handle: GpuTextureHandle = self.get_gpu_target().get_handle();
489 let _: bool = ctx.composite_gpu_texture(dst, handle);
490 if !self.get_gpu_target().needs_world_render() {
491 self.get_mut_gpu_target().mark_hud_dirty();
492 }
493 let _: Option<GpuCompositeDescriptor> = self
494 .get_mut_gpu_target()
495 .record_composite_present(next_fence);
496 ctx.fill_rounded_rect(
497 Rect::new(10.0, 10.0, 340.0, 26.0),
498 Color::rgba(15, 23, 42, 210),
499 6.0,
500 );
501 let Self { hud_text_buf, .. } = self;
502 hud_text_buf.clear();
503 let _: core::fmt::Result = write!(
504 hud_text_buf,
505 "{STRESS_GAME3D_HUD_PREFIX} | GPU Direct | a={alpha:.2} | F{next_frame}"
506 );
507 ctx.draw_text_str(
508 Point::new(18.0, 16.0),
509 hud_text_buf.as_str(),
510 12.0,
511 Color::hex(0xE2E8F0),
512 );
513 }
514}