Skip to content

Commit d2b0abd

Browse files
author
Peter Aarestad
committed
fps/sample meter
1 parent 2b5061a commit d2b0abd

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/util/interactive.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
22
use std::sync::mpsc::channel;
33
use std::sync::{Arc, Mutex};
4-
use std::time::Duration;
4+
use std::time::{Duration, Instant};
55

66
use minifb::{Key, MouseButton, MouseMode, Window, WindowOptions};
77
use nalgebra::{Rotation3, Unit};
@@ -110,7 +110,7 @@ fn render_pass(
110110
let du = world.image_width.saturating_sub(1).max(1) as f64;
111111
let dv = world.image_height.saturating_sub(1).max(1) as f64;
112112
let u = (x as f64 + rng.random::<f64>()) / du;
113-
let v = (flipped_y as f64 + rng.random::<f64>()) / dv;
113+
let v = (y as f64 + rng.random::<f64>()) / dv;
114114
let ray = camera.get_ray(u, v);
115115
let c = ray.color_in_world(
116116
world.hittable.as_ref(),
@@ -238,8 +238,21 @@ pub fn run_interactive(world: Arc<World>) -> Result<(), String> {
238238

239239
let mut last_left: Option<(f32, f32)> = None;
240240
let mut last_right: Option<(f32, f32)> = None;
241+
let mut last_frame = Instant::now();
242+
let mut fps_ema: f64 = 0.0;
241243

242244
while window.is_open() && !window.is_key_down(Key::Escape) {
245+
let now = Instant::now();
246+
let dt = now.duration_since(last_frame).as_secs_f64();
247+
last_frame = now;
248+
if dt > 0.0 {
249+
let instant_fps = 1.0 / dt;
250+
fps_ema = if fps_ema == 0.0 {
251+
instant_fps
252+
} else {
253+
0.1 * instant_fps + 0.9 * fps_ema
254+
};
255+
}
243256
let pos = window.get_mouse_pos(MouseMode::Clamp);
244257

245258
if let Some((mx, my)) = pos {
@@ -289,6 +302,12 @@ pub fn run_interactive(world: Arc<World>) -> Result<(), String> {
289302
}
290303
}
291304

305+
let samples = shared.samples.load(Ordering::Acquire);
306+
window.set_title(&format!(
307+
"ray-tracer {:.1} FPS {} spp (LMB orbit, RMB roll, wheel zoom)",
308+
fps_ema, samples
309+
));
310+
292311
if let Ok(guard) = shared.display.try_lock() {
293312
let _ = window.update_with_buffer(&guard, w, h);
294313
} else {

0 commit comments

Comments
 (0)