frame_history.rs (1410B)
1 /* 2 use egui::util::History; 3 4 pub struct FrameHistory { 5 frame_times: History<f32>, 6 } 7 8 impl Default for FrameHistory { 9 fn default() -> Self { 10 let max_age: f32 = 1.0; 11 let max_len = (max_age * 300.0).round() as usize; 12 Self { 13 frame_times: History::new(0..max_len, max_age), 14 } 15 } 16 } 17 18 impl FrameHistory { 19 // Called first 20 pub fn on_new_frame(&mut self, now: f64, previous_frame_time: Option<f32>) { 21 let previous_frame_time = previous_frame_time.unwrap_or_default(); 22 if let Some(latest) = self.frame_times.latest_mut() { 23 *latest = previous_frame_time; // rewrite history now that we know 24 } 25 self.frame_times.add(now, previous_frame_time); // projected 26 } 27 28 #[allow(unused)] 29 pub fn mean_frame_time(&self) -> f32 { 30 self.frame_times.average().unwrap_or_default() 31 } 32 33 #[allow(unused)] 34 pub fn fps(&self) -> f32 { 35 1.0 / self.frame_times.mean_time_interval().unwrap_or_default() 36 } 37 38 pub fn _ui(&mut self, ui: &mut egui::Ui) { 39 ui.label(format!( 40 "Mean CPU usage: {:.2} ms / frame", 41 1e3 * self.mean_frame_time() 42 )) 43 .on_hover_text( 44 "Includes egui layout and tessellation time.\n\ 45 Does not include GPU usage, nor overhead for sending data to GPU.", 46 ); 47 egui::warn_if_debug_build(ui); 48 } 49 } 50 */