diff --git a/rtiow/src/renderer.rs b/rtiow/src/renderer.rs index 72ac4e9..a1c7af0 100644 --- a/rtiow/src/renderer.rs +++ b/rtiow/src/renderer.rs @@ -119,6 +119,9 @@ 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, /// Sub-samples per pixel #[structopt(short = "s", long = "subsample", default_value = "8")] pub subsamples: usize, @@ -139,6 +142,7 @@ pub struct Opt { } pub fn opt_hash(opt: &Opt) -> String { + // TODO(wathiede): add threads. format!( "w:{}-h:{}-s:{}-pprof:{}-model:{}-use_accel:{}-{}", opt.width, @@ -155,6 +159,7 @@ pub struct Scene { pub world: Box, pub camera: Camera, pub subsamples: usize, + pub num_threads: Option, pub width: usize, pub height: usize, pub global_illumination: bool, @@ -269,13 +274,13 @@ fn render_worker( } pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::io::Error> { - 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 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 scene = sync::Arc::new(scene); - println!("Creating {} render threads", cpus); - for i in 0..cpus { + println!("Creating {} render threads", num_threads); + for i in 0..num_threads { let s = sync::Arc::clone(&scene); let pixel_req_rx = pixel_req_rx.clone(); let pixel_resp_tx = pixel_resp_tx.clone(); diff --git a/rtiow/src/scenes/bench.rs b/rtiow/src/scenes/bench.rs index 50475f2..4f2cd80 100644 --- a/rtiow/src/scenes/bench.rs +++ b/rtiow/src/scenes/bench.rs @@ -69,6 +69,7 @@ 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, diff --git a/rtiow/src/scenes/book.rs b/rtiow/src/scenes/book.rs index 6c5f912..db5ce51 100644 --- a/rtiow/src/scenes/book.rs +++ b/rtiow/src/scenes/book.rs @@ -51,6 +51,7 @@ 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, diff --git a/rtiow/src/scenes/bvh.rs b/rtiow/src/scenes/bvh.rs index 8ffc228..b23cec5 100644 --- a/rtiow/src/scenes/bvh.rs +++ b/rtiow/src/scenes/bvh.rs @@ -63,6 +63,7 @@ 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, diff --git a/rtiow/src/scenes/cornell_box.rs b/rtiow/src/scenes/cornell_box.rs index 2b574ac..42fd2f2 100644 --- a/rtiow/src/scenes/cornell_box.rs +++ b/rtiow/src/scenes/cornell_box.rs @@ -130,6 +130,7 @@ 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, diff --git a/rtiow/src/scenes/cornell_smoke.rs b/rtiow/src/scenes/cornell_smoke.rs index a54e9f2..f7f0765 100644 --- a/rtiow/src/scenes/cornell_smoke.rs +++ b/rtiow/src/scenes/cornell_smoke.rs @@ -115,6 +115,7 @@ 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, diff --git a/rtiow/src/scenes/final_scene.rs b/rtiow/src/scenes/final_scene.rs index e7d8c02..e3e5d33 100644 --- a/rtiow/src/scenes/final_scene.rs +++ b/rtiow/src/scenes/final_scene.rs @@ -166,6 +166,7 @@ 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, diff --git a/rtiow/src/scenes/mandelbrot.rs b/rtiow/src/scenes/mandelbrot.rs index 7237222..1783b6d 100644 --- a/rtiow/src/scenes/mandelbrot.rs +++ b/rtiow/src/scenes/mandelbrot.rs @@ -144,6 +144,7 @@ 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, diff --git a/rtiow/src/scenes/perlin_debug.rs b/rtiow/src/scenes/perlin_debug.rs index 32091e7..b5b789e 100644 --- a/rtiow/src/scenes/perlin_debug.rs +++ b/rtiow/src/scenes/perlin_debug.rs @@ -65,6 +65,7 @@ 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, diff --git a/rtiow/src/scenes/test.rs b/rtiow/src/scenes/test.rs index 143dcd6..461e7b1 100644 --- a/rtiow/src/scenes/test.rs +++ b/rtiow/src/scenes/test.rs @@ -142,6 +142,7 @@ 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, diff --git a/rtiow/src/scenes/tutorial.rs b/rtiow/src/scenes/tutorial.rs index caefd56..bf34ea9 100644 --- a/rtiow/src/scenes/tutorial.rs +++ b/rtiow/src/scenes/tutorial.rs @@ -70,6 +70,7 @@ 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,