Skip to content

Commit

Permalink
Upgrade wgpu-profiler (#687)
Browse files Browse the repository at this point in the history
Fixes #678
  • Loading branch information
DJMcNab authored Sep 25, 2024
1 parent d8a07c6 commit 5ad79d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
49 changes: 30 additions & 19 deletions examples/with_winit/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/linebender/vello/pull/475> 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())
Expand Down Expand Up @@ -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(),
];

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5ad79d0

Please sign in to comment.