diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index db21b8f..c697b33 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -219,38 +219,46 @@ fn trace_pixel(x: usize, y: usize, scene: &Scene) -> Vec3 { ) } -struct PixelRequest { - x: usize, - y: usize, +enum Request { + Pixel { x: usize, y: usize }, + Line { width: usize, y: usize }, } -struct PixelResponse { - x: usize, - y: usize, - pixel: Vec3, +enum Response { + Pixel { + x: usize, + y: usize, + pixel: Vec3, + }, + Line { + width: usize, + y: usize, + pixels: Vec, + }, } fn render_worker( tid: usize, scene: &Scene, - input_chan: channel::Receiver, - output_chan: &channel::Sender, + input_chan: channel::Receiver, + output_chan: &channel::Sender, ) { for req in input_chan { - let mut pixel: Vec3 = Default::default(); - for _ in 0..scene.subsamples { - pixel = pixel + trace_pixel(req.x, req.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()); + match req { + 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()); - output_chan.send(PixelResponse { - x: req.x, - y: req.y, - pixel, - }); + output_chan.send(Response::Pixel { x, y, pixel }); + } + _ => unimplemented!("Only Pixel requests are implemented"), + } } trace!(target: "renderer", "Shutting down worker {}", tid); } @@ -275,9 +283,10 @@ 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(PixelRequest { x, y }); + pixel_req_tx.send(Request::Pixel { x, y }); } } drop(pixel_req_tx); @@ -288,25 +297,30 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i let total = scene.width * scene.height; let mut last_progress = 1000; for (i, resp) in pixel_resp_rx.iter().enumerate() { - let y_inv = scene.height - resp.y - 1; - img.put_pixel( - resp.x as u32, - y_inv as u32, - image::Rgb([ - (resp.pixel[0] * 255.).min(255.) as u8, - (resp.pixel[1] * 255.).min(255.) as u8, - (resp.pixel[2] * 255.).min(255.) as u8, - ]), - ); - let progress = 100 * i / total; - if progress != last_progress { - last_progress = progress; - if progress % 10 == 0 { - print!("{}%", progress); - } else { - print!("."); + match resp { + Response::Pixel { x, y, pixel } => { + 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 * i / total; + if progress != last_progress { + last_progress = progress; + if progress % 10 == 0 { + print!("{}%", progress); + } else { + print!("."); + } + io::stdout().flush().unwrap(); + } } - io::stdout().flush().unwrap(); + _ => unimplemented!("Only Pixel responses are implemented"), } } println!();