diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index b43d66e..d43f399 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -1,4 +1,3 @@ -use std; use std::fmt; use std::io; use std::io::Write; @@ -6,12 +5,15 @@ use std::path::Path; use std::path::PathBuf; use std::str; use std::sync; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; use std::sync::mpsc::sync_channel; use std::sync::mpsc::Receiver; use std::sync::mpsc::SyncSender; use std::sync::Arc; use std::sync::Mutex; use std::thread; +use std::time; use image; use image::RgbImage; @@ -245,6 +247,9 @@ enum Response { pixels: Vec, }, } + +static PIXEL_COUNT: AtomicUsize = AtomicUsize::new(0); + fn render_pixel(scene: &Scene, x: usize, y: usize) -> Vec3 { let mut pixel: Vec3 = Default::default(); for _ in 0..scene.subsamples { @@ -253,6 +258,7 @@ fn render_pixel(scene: &Scene, x: usize, y: usize) -> Vec3 { pixel = pixel / scene.subsamples as f32; // Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2 or // sqrt. + PIXEL_COUNT.fetch_add(1, Ordering::SeqCst); Vec3::new(pixel[0].sqrt(), pixel[1].sqrt(), pixel[2].sqrt()) } @@ -319,7 +325,29 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i drop(pixel_req_tx); }); + thread::spawn(move || { + let mut last_time = time::Instant::now(); + let mut last_pixel_count = PIXEL_COUNT.load(Ordering::SeqCst); + loop { + let sleep_time = time::Duration::from_secs(1); + thread::sleep(sleep_time); + let now = time::Instant::now(); + let mut pixel_count = PIXEL_COUNT.load(Ordering::SeqCst); + let time_diff = now - last_time; + let pixel_diff = pixel_count - last_pixel_count; + info!( + "Woke up {}s passed, {} pixels rendered {} p/s", + time_diff.as_secs_f64(), + pixel_count, + pixel_diff as u64 / time_diff.as_secs() + ); + last_time = now; + last_pixel_count = pixel_count; + } + }); + println!("Rendering with {} subsamples", scene.subsamples); + info!("PIXEL_COUNT {} ", PIXEL_COUNT.load(Ordering::SeqCst)); let mut img = RgbImage::new(scene.width as u32, scene.height as u32); let total = scene.width * scene.height; let mut cur_pixel = 0;