Skip to content

Commit

Permalink
Print FPS every second, even if it's 0
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 26, 2024
1 parent 8ef6b65 commit 3531ed3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 33 deletions.
69 changes: 69 additions & 0 deletions src/frame_counter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{
sync::{Arc, Mutex},
thread::{self, JoinHandle},
time::{Duration, Instant},
};

#[derive(Debug)]
pub struct FrameCounter {
state: Arc<Mutex<FrameCounterState>>,
// Helper thread to ensure that we print the FPS every second, even if it's 0.
printer_thread: Option<JoinHandle<()>>,
}

impl Drop for FrameCounter {
fn drop(&mut self) {
self.printer_thread
.take()
.expect("printer thread set")
.join()
.expect("printer thread");
}
}

#[derive(Debug)]
struct FrameCounterState {
last_printed_instant: Instant,
frame_count: u32,
}

impl FrameCounter {
pub fn new() -> Self {
let state = FrameCounterState {
last_printed_instant: Instant::now(),
frame_count: 0,
};
let state: Arc<Mutex<FrameCounterState>> = Arc::new(Mutex::new(state));
let state_clone = Arc::clone(&state);
Self {
state,
printer_thread: Some(thread::spawn(move || loop {
state_clone.lock().unwrap().print();
thread::sleep(Duration::from_millis(100));
})),
}
}

pub fn update(&self) {
self.state.lock().unwrap().update();
}
}

impl FrameCounterState {
fn update(&mut self) {
self.frame_count += 1;
self.print();
}

fn print(&mut self) {
let now = Instant::now();
let elapsed = now - self.last_printed_instant;
if elapsed > Duration::from_secs(1) {
let fps = self.frame_count as f32 / elapsed.as_secs_f32();
tracing::info!("FPS: {:.1}", fps);

self.last_printed_instant = now;
self.frame_count = 0;
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use tracing_subscriber::filter::EnvFilter;

#[cfg(target_os = "macos")]
mod appkit_main;
mod frame_counter;
mod run_loop;
#[cfg(not(target_os = "macos"))]
mod uikit_main;
Expand Down
36 changes: 3 additions & 33 deletions src/wgpu_triangle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Adapted from `wgpu/examples/src/hello_triangle`.
use std::{
cell::{Cell, RefCell},
time::{Duration, Instant},
};
use std::cell::RefCell;

use crate::frame_counter::FrameCounter;

#[allow(unused)] // Unsure which of these need to be kept around!
#[derive(Debug)]
Expand Down Expand Up @@ -156,32 +155,3 @@ impl<'window> Triangle<'window> {
self.frame_counter.update();
}
}

#[derive(Debug)]
struct FrameCounter {
last_printed_instant: Cell<Instant>,
frame_count: Cell<u32>,
}

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

fn update(&self) {
self.frame_count.set(self.frame_count.get() + 1);

let now = Instant::now();
let elapsed = now - self.last_printed_instant.get();
if elapsed > Duration::from_secs(1) {
let fps = self.frame_count.get() as f32 / elapsed.as_secs_f32();
tracing::info!("FPS: {:.1}", fps);

self.last_printed_instant.set(now);
self.frame_count.set(0);
}
}
}

0 comments on commit 3531ed3

Please sign in to comment.