diff --git a/Cargo.toml b/Cargo.toml index 398914da..ca9c2a8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,5 +60,5 @@ clap = "4.5.18" anyhow = "1.0.89" pollster = "0.3.0" web-time = "1.1.0" -wgpu-profiler = "0.18.0" +wgpu-profiler = "0.18.2" scenes = { path = "examples/scenes" } diff --git a/examples/with_winit/src/stats.rs b/examples/with_winit/src/stats.rs index 3b5339de..9660f351 100644 --- a/examples/with_winit/src/stats.rs +++ b/examples/with_winit/src/stats.rs @@ -250,6 +250,30 @@ fn round_up(n: usize, f: usize) -> usize { #[cfg(feature = "wgpu-profiler")] use wgpu_profiler::GpuTimerQueryResult; +/// Formats the given number of seconds as a Duration. +/// +/// This is necessary as sometimes, the duration from wgpu-profiler turns out to be negative. +/// We have not yet debugged this, but we choose to display the absolute +/// value in that case. +/// +/// See for context +#[cfg(feature = "wgpu-profiler")] +struct PaniclessSecondsFormatter(f64); + +#[cfg(feature = "wgpu-profiler")] +impl std::fmt::Display for PaniclessSecondsFormatter { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.0 < 0. { + if let Ok(val) = Duration::try_from_secs_f64(-self.0) { + return write!(f, "-{val:.2?}(!)"); + } + } else if let Ok(val) = Duration::try_from_secs_f64(self.0) { + return write!(f, "{val:.2?}"); + } + write!(f, "{} seconds(!)", self.0) + } +} + #[cfg(feature = "wgpu-profiler")] fn profiles_are_empty(profiles: &[GpuTimerQueryResult]) -> bool { profiles.iter().all(|p| p.time.is_none()) @@ -313,7 +337,7 @@ pub fn draw_gpu_profiling( let total_time = max - min; { let labels = [ - format!("GPU Time: {:.2?}", Duration::from_secs_f64(total_time)), + format!("GPU Time: {}", PaniclessSecondsFormatter(total_time)), "Press P to save a trace".to_string(), ]; @@ -397,24 +421,11 @@ pub fn draw_gpu_profiling( let text_size = (text_height * 0.9) as f32; // Text is specified by the baseline, but the y positions all refer to the top of the text cur_text_y = text_y + text_height; - let label = { - // Sometimes, the duration turns out to be negative - // We have not yet debugged this, but display the absolute value in that case - // see https://github.com/linebender/vello/pull/475 for more - if this_time < 0.0 { - format!( - "-{:.2?}(!!) - {:.30}", - Duration::from_secs_f64(this_time.abs()), - profile.label - ) - } else { - format!( - "{:.2?} - {:.30}", - Duration::from_secs_f64(this_time), - profile.label - ) - } - }; + let label = format!( + "{} - {:.30}", + PaniclessSecondsFormatter(this_time), + profile.label + ); scene.fill( Fill::NonZero, offset,