diff --git a/Cargo.lock b/Cargo.lock index 11b70341..2e1e8565 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2943,9 +2943,9 @@ dependencies = [ [[package]] name = "wgpu-profiler" -version = "0.18.0" +version = "0.18.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "111850b8b96db7f12f310e20d9f16ad2d18a0c8ac8493e434441d1dfb288c6bf" +checksum = "06b2cee91fdc885ff0d3d714c59810cc72c6d84b81b0eaa48bab8ff2ad54fb5b" dependencies = [ "parking_lot", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 3626a800..c5e5ad26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,5 +60,5 @@ clap = "4.5.4" anyhow = "1.0.86" 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,