From 9a45ba6d72a468ffa390496425f177effafb5e24 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Thu, 10 Oct 2019 21:20:48 -0700 Subject: [PATCH] Async send the pixels to the workers, and bound queue to 2xthreads. This should allow the program to start recieving rendered pixels before all of the x,y's have been sent to the workers. Reducing the time to first pixel. --- rtiow/src/renderer.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index ba26d43..db21b8f 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -256,11 +256,13 @@ fn render_worker( } pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::io::Error> { - let (pixel_req_tx, pixel_req_rx) = channel::unbounded(); - let (pixel_resp_tx, pixel_resp_rx) = channel::unbounded(); + let cpus = num_cpus::get(); + let (pixel_req_tx, pixel_req_rx) = channel::bounded(2 * cpus); + let (pixel_resp_tx, pixel_resp_rx) = channel::bounded(2 * cpus); let scene = sync::Arc::new(scene); - for i in 0..num_cpus::get() { + println!("Creating {} render threads", cpus); + for i in 0..cpus { let s = sync::Arc::clone(&scene); let pixel_req_rx = pixel_req_rx.clone(); let pixel_resp_tx = pixel_resp_tx.clone(); @@ -271,12 +273,15 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i drop(pixel_req_rx); drop(pixel_resp_tx); - for y in 0..scene.height { - for x in 0..scene.width { - pixel_req_tx.send(PixelRequest { x, y }); + let (w, h) = (scene.width, scene.height); + thread::spawn(move || { + for y in 0..w { + for x in 0..h { + pixel_req_tx.send(PixelRequest { x, y }); + } } - } - drop(pixel_req_tx); + drop(pixel_req_tx); + }); println!("Rendering with {} subsamples", scene.subsamples); let mut img = RgbImage::new(scene.width as u32, scene.height as u32);