diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index c697b33..72ac4e9 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -236,6 +236,16 @@ enum Response { pixels: Vec, }, } +fn render_pixel(scene: &Scene, x: usize, y: usize) -> Vec3 { + let mut pixel: Vec3 = Default::default(); + for _ in 0..scene.subsamples { + pixel = pixel + trace_pixel(x, y, scene); + } + 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. + Vec3::new(pixel[0].sqrt(), pixel[1].sqrt(), pixel[2].sqrt()) +} fn render_worker( tid: usize, @@ -245,19 +255,14 @@ fn render_worker( ) { for req in input_chan { match req { + Request::Line { width, y } => { + let pixels = (0..width).map(|x| render_pixel(scene, x, y)).collect(); + output_chan.send(Response::Line { width, y, pixels }); + } Request::Pixel { x, y } => { - let mut pixel: Vec3 = Default::default(); - for _ in 0..scene.subsamples { - pixel = pixel + trace_pixel(x, y, scene); - } - 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. - let pixel = Vec3::new(pixel[0].sqrt(), pixel[1].sqrt(), pixel[2].sqrt()); - + let pixel = render_pixel(scene, x, y); output_chan.send(Response::Pixel { x, y, pixel }); } - _ => unimplemented!("Only Pixel requests are implemented"), } } trace!(target: "renderer", "Shutting down worker {}", tid); @@ -283,10 +288,16 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i let (w, h) = (scene.width, scene.height); thread::spawn(move || { - // TODO(wathiede): handle sending Line requests for optimization. - for y in 0..w { - for x in 0..h { - pixel_req_tx.send(Request::Pixel { x, y }); + let batch_by_line = true; + if batch_by_line { + for y in 0..h { + pixel_req_tx.send(Request::Line { width: w, y }); + } + } else { + for y in 0..h { + for x in 0..w { + pixel_req_tx.send(Request::Pixel { x, y }); + } } } drop(pixel_req_tx); @@ -295,8 +306,9 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i println!("Rendering with {} subsamples", scene.subsamples); let mut img = RgbImage::new(scene.width as u32, scene.height as u32); let total = scene.width * scene.height; + let mut cur_pixel = 0; let mut last_progress = 1000; - for (i, resp) in pixel_resp_rx.iter().enumerate() { + for resp in pixel_resp_rx { match resp { Response::Pixel { x, y, pixel } => { let y_inv = scene.height - y - 1; @@ -309,7 +321,7 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i (pixel[2] * 255.).min(255.) as u8, ]), ); - let progress = 100 * i / total; + let progress = 100 * cur_pixel / total; if progress != last_progress { last_progress = progress; if progress % 10 == 0 { @@ -319,8 +331,37 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i } io::stdout().flush().unwrap(); } + cur_pixel += 1; + } + Response::Line { + width: _, + y, + pixels, + } => { + for (x, pixel) in pixels.iter().enumerate() { + let y_inv = scene.height - y - 1; + img.put_pixel( + x as u32, + y_inv as u32, + image::Rgb([ + (pixel[0] * 255.).min(255.) as u8, + (pixel[1] * 255.).min(255.) as u8, + (pixel[2] * 255.).min(255.) as u8, + ]), + ); + let progress = 100 * cur_pixel / total; + if progress != last_progress { + last_progress = progress; + if progress % 10 == 0 { + print!("{}%", progress); + } else { + print!("."); + } + io::stdout().flush().unwrap(); + } + cur_pixel += 1; + } } - _ => unimplemented!("Only Pixel responses are implemented"), } } println!();