Compare commits
No commits in common. "235a9d12043be5da7618c7c0dfe9566eab34a869" and "b0dafe4739c1498b91d7dc493892e820d5508c2b" have entirely different histories.
235a9d1204
...
b0dafe4739
@ -119,9 +119,6 @@ pub struct Opt {
|
||||
/// Image height
|
||||
#[structopt(short = "h", long = "height", default_value = "1024")]
|
||||
pub height: usize,
|
||||
/// Number of threads
|
||||
#[structopt(short = "t", long = "num_threads")]
|
||||
pub num_threads: Option<usize>,
|
||||
/// Sub-samples per pixel
|
||||
#[structopt(short = "s", long = "subsample", default_value = "8")]
|
||||
pub subsamples: usize,
|
||||
@ -142,7 +139,6 @@ pub struct Opt {
|
||||
}
|
||||
|
||||
pub fn opt_hash(opt: &Opt) -> String {
|
||||
// TODO(wathiede): add threads.
|
||||
format!(
|
||||
"w:{}-h:{}-s:{}-pprof:{}-model:{}-use_accel:{}-{}",
|
||||
opt.width,
|
||||
@ -159,7 +155,6 @@ pub struct Scene {
|
||||
pub world: Box<Hit>,
|
||||
pub camera: Camera,
|
||||
pub subsamples: usize,
|
||||
pub num_threads: Option<usize>,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub global_illumination: bool,
|
||||
@ -241,16 +236,6 @@ enum Response {
|
||||
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(
|
||||
tid: usize,
|
||||
@ -260,27 +245,32 @@ 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 pixel = render_pixel(scene, 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(Response::Pixel { x, y, pixel });
|
||||
}
|
||||
_ => unimplemented!("Only Pixel requests are implemented"),
|
||||
}
|
||||
}
|
||||
trace!(target: "renderer", "Shutting down worker {}", tid);
|
||||
}
|
||||
|
||||
pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::io::Error> {
|
||||
let num_threads = scene.num_threads.unwrap_or_else(num_cpus::get);
|
||||
let (pixel_req_tx, pixel_req_rx) = channel::bounded(2 * num_threads);
|
||||
let (pixel_resp_tx, pixel_resp_rx) = channel::bounded(2 * num_threads);
|
||||
let cpus = num_cpus::get();
|
||||
let (pixel_req_tx, pixel_req_rx) = channel::bounded(2 * cpus);
|
||||
let (pixel_resp_tx, pixel_resp_rx) = channel::bounded(2 * cpus);
|
||||
|
||||
let scene = sync::Arc::new(scene);
|
||||
println!("Creating {} render threads", num_threads);
|
||||
for i in 0..num_threads {
|
||||
println!("Creating {} render threads", cpus);
|
||||
for i in 0..cpus {
|
||||
let s = sync::Arc::clone(&scene);
|
||||
let pixel_req_rx = pixel_req_rx.clone();
|
||||
let pixel_resp_tx = pixel_resp_tx.clone();
|
||||
@ -293,16 +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 || {
|
||||
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 });
|
||||
}
|
||||
// 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 });
|
||||
}
|
||||
}
|
||||
drop(pixel_req_tx);
|
||||
@ -311,9 +295,8 @@ 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 resp in pixel_resp_rx {
|
||||
for (i, resp) in pixel_resp_rx.iter().enumerate() {
|
||||
match resp {
|
||||
Response::Pixel { x, y, pixel } => {
|
||||
let y_inv = scene.height - y - 1;
|
||||
@ -326,7 +309,7 @@ pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::i
|
||||
(pixel[2] * 255.).min(255.) as u8,
|
||||
]),
|
||||
);
|
||||
let progress = 100 * cur_pixel / total;
|
||||
let progress = 100 * i / total;
|
||||
if progress != last_progress {
|
||||
last_progress = progress;
|
||||
if progress % 10 == 0 {
|
||||
@ -336,37 +319,8 @@ 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!();
|
||||
|
||||
@ -69,7 +69,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: true,
|
||||
|
||||
@ -51,7 +51,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: true,
|
||||
|
||||
@ -63,7 +63,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: true,
|
||||
|
||||
@ -130,7 +130,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: false,
|
||||
|
||||
@ -115,7 +115,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: false,
|
||||
|
||||
@ -166,7 +166,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world: Box::new(HitableList::new(list)),
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: false,
|
||||
|
||||
@ -144,7 +144,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: false,
|
||||
|
||||
@ -65,7 +65,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: true,
|
||||
|
||||
@ -142,7 +142,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: false,
|
||||
|
||||
@ -70,7 +70,6 @@ pub fn new(opt: &Opt) -> Scene {
|
||||
camera,
|
||||
world,
|
||||
subsamples: opt.subsamples,
|
||||
num_threads: opt.num_threads,
|
||||
width: opt.width,
|
||||
height: opt.height,
|
||||
global_illumination: true,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user