From d0c26d6891d48303f511ac81874e38425ca32783 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 23 Jan 2024 19:03:42 -0800 Subject: [PATCH] Improved physics timings --- crates/render/src/ui/fpsindicator.rs | 9 +-- crates/system/src/phys/physsim.rs | 6 ++ crates/util/src/timing.rs | 100 ++++++++++++++++++++++++--- 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/crates/render/src/ui/fpsindicator.rs b/crates/render/src/ui/fpsindicator.rs index 6e5b838..5ae246e 100644 --- a/crates/render/src/ui/fpsindicator.rs +++ b/crates/render/src/ui/fpsindicator.rs @@ -35,14 +35,7 @@ impl FpsIndicator { self.buffer.set_text( &mut state.text_font_system, - &format!( - "Frame: {:#?} ({:05.00})\nShips: {:05.02}%\nPhys: {:05.02}%\n", - input.timing.frame, - 1.0 / input.timing.frame.as_secs_f32(), - 100.0 * (input.timing.physics_sim.as_secs_f32() / input.timing.frame.as_secs_f32()), - 100.0 - * (input.timing.physics_ship.as_secs_f32() / input.timing.frame.as_secs_f32()), - ), + &input.timing.get_string(), Attrs::new().family(Family::Monospace), Shaping::Basic, ); diff --git a/crates/system/src/phys/physsim.rs b/crates/system/src/phys/physsim.rs index 9ed54b2..82b6e7a 100644 --- a/crates/system/src/phys/physsim.rs +++ b/crates/system/src/phys/physsim.rs @@ -257,6 +257,7 @@ impl PhysSim { res.timing.start_physics_sim(); // Update physics + res.timing.start_physics_step(); self.wrapper.step(res.t); // Handle collision events @@ -281,12 +282,15 @@ impl PhysSim { self.collide_projectile_ship(&mut res, a, b); } } + res.timing.mark_physics_step(); + res.timing.start_physics_proj(); // Step and garbage-collect projectiles self.projectiles.retain(|_, proj| { proj.step(&mut res, &mut self.new, &mut self.wrapper); !proj.should_remove() }); + res.timing.mark_physics_proj(); // Step and garbage-collect ships res.timing.start_physics_ships(); @@ -296,11 +300,13 @@ impl PhysSim { }); res.timing.mark_physics_ships(); + res.timing.start_physics_effects(); // Step and garbage-collect effects self.effects.retain_mut(|x| { x.step(&res, &mut self.wrapper); !x.is_destroyed() }); + res.timing.mark_physics_effects(); // Process new objects for p in self.new.projectiles.iter() { diff --git a/crates/util/src/timing.rs b/crates/util/src/timing.rs index 16dbdda..d939411 100644 --- a/crates/util/src/timing.rs +++ b/crates/util/src/timing.rs @@ -9,13 +9,25 @@ pub struct Timing { pub frame: Duration, frame_timer: Instant, - /// The time we spent simulating physics + /// The total time we spent simulating physics pub physics_sim: Duration, physics_sim_timer: Instant, + /// The time we spent updating physics state + pub physics_step: Duration, + physics_step_timer: Instant, + /// The time we spent updating physics ships pub physics_ship: Duration, physics_ship_timer: Instant, + + /// The time we spent updating physics projectiles + pub physics_proj: Duration, + physics_proj_timer: Instant, + + /// The time we spent updating effects + pub physics_effect: Duration, + physics_effect_timer: Instant, } // TODO: document each duration @@ -27,6 +39,12 @@ impl Timing { frame: Duration::ZERO, physics_sim: Duration::ZERO, physics_ship: Duration::ZERO, + physics_effect: Duration::ZERO, + physics_proj: Duration::ZERO, + physics_step: Duration::ZERO, + physics_effect_timer: Instant::now(), + physics_proj_timer: Instant::now(), + physics_step_timer: Instant::now(), physics_sim_timer: Instant::now(), physics_ship_timer: Instant::now(), frame_timer: Instant::now(), @@ -38,28 +56,88 @@ impl Timing { self.frame_timer = Instant::now(); } - /// Start physics sim timer - pub fn start_physics_sim(&mut self) { - self.physics_sim_timer = Instant::now(); - } - - /// Start physics ship timer - pub fn start_physics_ships(&mut self) { - self.physics_ship_timer = Instant::now(); - } - /// Record total frame compute time pub fn mark_frame(&mut self) { self.frame = self.frame_timer.elapsed(); } + /// Start physics sim timer + pub fn start_physics_sim(&mut self) { + self.physics_sim_timer = Instant::now(); + } + /// Record physics simulation time pub fn mark_physics_sim(&mut self) { self.physics_sim = self.physics_sim_timer.elapsed(); } + /// Start physics ship timer + pub fn start_physics_ships(&mut self) { + self.physics_ship_timer = Instant::now(); + } + /// Record physics ship update time pub fn mark_physics_ships(&mut self) { self.physics_ship = self.physics_ship_timer.elapsed(); } + + /// Start physics step timer + pub fn start_physics_step(&mut self) { + self.physics_step_timer = Instant::now(); + } + + /// Record physics ship update time + pub fn mark_physics_step(&mut self) { + self.physics_step = self.physics_step_timer.elapsed(); + } + + /// Start physics ship timer + pub fn start_physics_proj(&mut self) { + self.physics_proj_timer = Instant::now(); + } + + /// Record physics ship update time + pub fn mark_physics_proj(&mut self) { + self.physics_proj = self.physics_proj_timer.elapsed(); + } + + /// Start physics ship timer + pub fn start_physics_effects(&mut self) { + self.physics_effect_timer = Instant::now(); + } + + /// Record physics ship update time + pub fn mark_physics_effects(&mut self) { + self.physics_effect = self.physics_effect_timer.elapsed(); + } + + /// Get current timing values as a pretty string + pub fn get_string(&self) -> String { + let mut s = String::new(); + //s.push_str(&format!( + // "Overall {:6.00} fps\n\n", + // 1.0 / self.frame.as_secs_f32() + //)); + + let f = self.physics_sim.as_secs_f32(); + s.push_str(&format!("Phys {:6.00} fps\n", 1.0 / f)); + s.push_str(&format!( + "Step {:6.02}%\n", + 100.0 * self.physics_step.as_secs_f32() / f + )); + s.push_str(&format!( + "Proj {:05.02}%\n", + 100.0 * self.physics_proj.as_secs_f32() / f + )); + s.push_str(&format!( + "Ships {:05.02}%\n", + 100.0 * self.physics_ship.as_secs_f32() / f + )); + s.push_str(&format!( + "Efcts {:05.02}%", + 100.0 * self.physics_effect.as_secs_f32() / f + )); + + return s; + } }