Skip to content

Commit

Permalink
Changes to framework to debug judder
Browse files Browse the repository at this point in the history
  • Loading branch information
valadaptive committed Sep 10, 2024
1 parent 9b36a3e commit 32c85cc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions examples/src/framework.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use wgpu::{Instance, Surface};
use wgpu::{Instance, Maintain, Surface};
use winit::{
dpi::PhysicalSize,
event::{Event, KeyEvent, StartCause, WindowEvent},
Expand Down Expand Up @@ -186,6 +186,8 @@ impl SurfaceWrapper {
let mut config = surface
.get_default_config(&context.adapter, width, height)
.expect("Surface isn't supported by the adapter.");
// Reduces judder by 1 frame:
// config.desired_maximum_frame_latency = 1;
if srgb {
// Not all platforms (WebGPU) support sRGB swapchains, so we need to use view formats
let view_format = config.format.add_srgb_suffix();
Expand Down Expand Up @@ -340,21 +342,24 @@ struct FrameCounter {
last_printed_instant: web_time::Instant,
// Number of frames since the last time we printed the frame time.
frame_count: u32,
frame_id: u32,
}

impl FrameCounter {
fn new() -> Self {
Self {
last_printed_instant: web_time::Instant::now(),
frame_count: 0,
frame_id: 0,
}
}

fn update(&mut self) {
self.frame_count += 1;
self.frame_id += 1;
let new_instant = web_time::Instant::now();
let elapsed_secs = (new_instant - self.last_printed_instant).as_secs_f32();
if elapsed_secs > 1.0 {
if true {
let elapsed_ms = elapsed_secs * 1000.0;
let frame_time = elapsed_ms / self.frame_count as f32;
let fps = self.frame_count as f32 / elapsed_secs;
Expand Down Expand Up @@ -458,6 +463,9 @@ async fn start<E: Example>(title: &str) {

frame_counter.update();

let frame_id = frame_counter.frame_id;
log::info!("submitted {frame_id}");

let frame = surface.acquire(&context);
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(surface.config().view_formats[0]),
Expand All @@ -469,8 +477,15 @@ async fn start<E: Example>(title: &str) {
.unwrap()
.render(&view, &context.device, &context.queue);

context.queue.on_submitted_work_done(move || {
log::info!("submitted work done {frame_id}");
});

frame.present();

// Reduces the judder to 1 frame's worth:
// context.device.poll(Maintain::wait());

window_loop.window.request_redraw();
}
_ => example.as_mut().unwrap().update(event),
Expand Down

0 comments on commit 32c85cc

Please sign in to comment.