Make render req/response enums.
All checks were successful
continuous-integration/drone/push Build is passing

First step in some optimizations to limit overhead of locking between
threads.
This commit is contained in:
Bill Thiede 2019-10-11 08:03:32 -07:00
parent bcf6b6d5d0
commit 8b99a1f487

View File

@ -219,38 +219,46 @@ fn trace_pixel(x: usize, y: usize, scene: &Scene) -> Vec3 {
) )
} }
struct PixelRequest { enum Request {
x: usize, Pixel { x: usize, y: usize },
y: usize, Line { width: usize, y: usize },
} }
struct PixelResponse { enum Response {
Pixel {
x: usize, x: usize,
y: usize, y: usize,
pixel: Vec3, pixel: Vec3,
},
Line {
width: usize,
y: usize,
pixels: Vec<Vec3>,
},
} }
fn render_worker( fn render_worker(
tid: usize, tid: usize,
scene: &Scene, scene: &Scene,
input_chan: channel::Receiver<PixelRequest>, input_chan: channel::Receiver<Request>,
output_chan: &channel::Sender<PixelResponse>, output_chan: &channel::Sender<Response>,
) { ) {
for req in input_chan { for req in input_chan {
match req {
Request::Pixel { x, y } => {
let mut pixel: Vec3 = Default::default(); let mut pixel: Vec3 = Default::default();
for _ in 0..scene.subsamples { for _ in 0..scene.subsamples {
pixel = pixel + trace_pixel(req.x, req.y, scene); pixel = pixel + trace_pixel(x, y, scene);
} }
pixel = pixel / scene.subsamples as f32; pixel = pixel / scene.subsamples as f32;
// Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2 or // Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2 or
// sqrt. // sqrt.
let pixel = Vec3::new(pixel[0].sqrt(), pixel[1].sqrt(), pixel[2].sqrt()); let pixel = Vec3::new(pixel[0].sqrt(), pixel[1].sqrt(), pixel[2].sqrt());
output_chan.send(PixelResponse { output_chan.send(Response::Pixel { x, y, pixel });
x: req.x, }
y: req.y, _ => unimplemented!("Only Pixel requests are implemented"),
pixel, }
});
} }
trace!(target: "renderer", "Shutting down worker {}", tid); 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); let (w, h) = (scene.width, scene.height);
thread::spawn(move || { thread::spawn(move || {
// TODO(wathiede): handle sending Line requests for optimization.
for y in 0..w { for y in 0..w {
for x in 0..h { for x in 0..h {
pixel_req_tx.send(PixelRequest { x, y }); pixel_req_tx.send(Request::Pixel { x, y });
} }
} }
drop(pixel_req_tx); drop(pixel_req_tx);
@ -288,14 +297,16 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i
let total = scene.width * scene.height; let total = scene.width * scene.height;
let mut last_progress = 1000; let mut last_progress = 1000;
for (i, resp) in pixel_resp_rx.iter().enumerate() { for (i, resp) in pixel_resp_rx.iter().enumerate() {
let y_inv = scene.height - resp.y - 1; match resp {
Response::Pixel { x, y, pixel } => {
let y_inv = scene.height - y - 1;
img.put_pixel( img.put_pixel(
resp.x as u32, x as u32,
y_inv as u32, y_inv as u32,
image::Rgb([ image::Rgb([
(resp.pixel[0] * 255.).min(255.) as u8, (pixel[0] * 255.).min(255.) as u8,
(resp.pixel[1] * 255.).min(255.) as u8, (pixel[1] * 255.).min(255.) as u8,
(resp.pixel[2] * 255.).min(255.) as u8, (pixel[2] * 255.).min(255.) as u8,
]), ]),
); );
let progress = 100 * i / total; let progress = 100 * i / total;
@ -309,6 +320,9 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
} }
} }
_ => unimplemented!("Only Pixel responses are implemented"),
}
}
println!(); println!();
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
let path = output_dir.join("final.png"); let path = output_dir.join("final.png");