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 {
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<Vec3>,
},
}
fn render_worker(
tid: usize,
scene: &Scene,
input_chan: channel::Receiver<PixelRequest>,
output_chan: &channel::Sender<PixelResponse>,
input_chan: channel::Receiver<Request>,
output_chan: &channel::Sender<Response>,
) {
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!();