Render whole lines at a time.

This commit is contained in:
Bill Thiede 2019-10-11 11:14:52 -07:00
parent b0dafe4739
commit 1687077f4a

View File

@ -236,6 +236,16 @@ enum Response {
pixels: Vec<Vec3>, pixels: Vec<Vec3>,
}, },
} }
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( fn render_worker(
tid: usize, tid: usize,
@ -245,19 +255,14 @@ fn render_worker(
) { ) {
for req in input_chan { for req in input_chan {
match req { 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 } => { Request::Pixel { x, y } => {
let mut pixel: Vec3 = Default::default(); let pixel = render_pixel(scene, x, y);
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(Response::Pixel { x, y, pixel }); output_chan.send(Response::Pixel { x, y, pixel });
} }
_ => unimplemented!("Only Pixel requests are implemented"),
} }
} }
trace!(target: "renderer", "Shutting down worker {}", tid); 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); let (w, h) = (scene.width, scene.height);
thread::spawn(move || { thread::spawn(move || {
// TODO(wathiede): handle sending Line requests for optimization. let batch_by_line = true;
for y in 0..w { if batch_by_line {
for x in 0..h { for y in 0..h {
pixel_req_tx.send(Request::Pixel { x, y }); 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); 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); println!("Rendering with {} subsamples", scene.subsamples);
let mut img = RgbImage::new(scene.width as u32, scene.height as u32); let mut img = RgbImage::new(scene.width as u32, scene.height as u32);
let total = scene.width * scene.height; let total = scene.width * scene.height;
let mut cur_pixel = 0;
let mut last_progress = 1000; let mut last_progress = 1000;
for (i, resp) in pixel_resp_rx.iter().enumerate() { for resp in pixel_resp_rx {
match resp { match resp {
Response::Pixel { x, y, pixel } => { Response::Pixel { x, y, pixel } => {
let y_inv = scene.height - y - 1; 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, (pixel[2] * 255.).min(255.) as u8,
]), ]),
); );
let progress = 100 * i / total; let progress = 100 * cur_pixel / total;
if progress != last_progress { if progress != last_progress {
last_progress = progress; last_progress = progress;
if progress % 10 == 0 { 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(); 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!(); println!();