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);