diff --git a/rtiow/Cargo.lock b/rtiow/Cargo.lock index b8d6e99..81f5689 100644 --- a/rtiow/Cargo.lock +++ b/rtiow/Cargo.lock @@ -39,6 +39,18 @@ name = "color_quant" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "crossbeam-channel" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-epoch 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-deque" version = "0.2.0" @@ -62,6 +74,19 @@ dependencies = [ "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-epoch" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-utils" version = "0.2.2" @@ -70,6 +95,11 @@ dependencies = [ "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-utils" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "deflate" version = "0.7.18" @@ -154,6 +184,15 @@ name = "libc" version = "0.2.43" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lock_api" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lzw" version = "0.10.0" @@ -219,6 +258,35 @@ dependencies = [ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "owning_ref" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "parking_lot" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "parking_lot_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "png" version = "0.12.0" @@ -288,8 +356,19 @@ dependencies = [ name = "rtiow" version = "0.1.0" dependencies = [ + "crossbeam-channel 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -302,6 +381,32 @@ name = "scopeguard" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "smallvec" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "stable_deref_trait" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "0.14.9" @@ -317,11 +422,24 @@ name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "version_check" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.5" @@ -349,9 +467,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum color_quant 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" +"checksum crossbeam-channel 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6c0a94250b0278d7fc5a894c3d276b11ea164edc8bf8feb10ca1ea517b44a649" "checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3" "checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150" +"checksum crossbeam-epoch 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "30fecfcac6abfef8771151f8be4abc9e4edc112c2bcb233314cafde2680536e9" "checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9" +"checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum deflate 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)" = "32c8120d981901a9970a3a1c97cf8b630e0fa8c3ca31e75b6fd6fd5f9f427b31" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" @@ -362,6 +483,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum jpeg-decoder 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "c8b7d43206b34b3f94ea9445174bda196e772049b9bddbc620c9d29b2d20110d" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" +"checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54" "checksum lzw 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" @@ -371,6 +493,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-rational 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e" "checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" +"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" +"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" +"checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" "checksum png 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f54b9600d584d3b8a739e1662a595fab051329eff43f20e7d8cc22872962145b" "checksum proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "ffe022fb8c8bd254524b0b3305906c1921fa37a84a644e29079a9e62200c3901" "checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5" @@ -378,11 +503,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" "checksum rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "df7a791f788cb4c516f0e091301a29c2b71ef680db5e644a7d68835c8ae6dbfa" "checksum rayon-core 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b055d1e92aba6877574d8fe604a63c8b5df60f60e5982bf7ccbb1338ea527356" +"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" +"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "153ffa32fd170e9944f7e0838edf824a754ec4c1fc64746fcc9fe1f8fa602e5d" +"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rtiow/Cargo.toml b/rtiow/Cargo.toml index 84f0c2a..0619904 100644 --- a/rtiow/Cargo.toml +++ b/rtiow/Cargo.toml @@ -6,3 +6,6 @@ authors = ["Bill Thiede "] [dependencies] rand = "0.5.5" image = "0.19.0" +crossbeam-channel = "0.2.4" +num_cpus = "1.8.0" +rayon = "1.0.2" diff --git a/rtiow/src/bin/tracer.rs b/rtiow/src/bin/tracer.rs index b834b7f..cece355 100644 --- a/rtiow/src/bin/tracer.rs +++ b/rtiow/src/bin/tracer.rs @@ -1,11 +1,13 @@ extern crate image; extern crate rand; +extern crate rayon; extern crate rtiow; use std::time::Instant; use image::RgbImage; use rand::Rng; +use rayon::prelude::*; use rtiow::camera::Camera; use rtiow::hitable::Hit; @@ -104,15 +106,16 @@ fn random_scene() -> Vec> { objects } -fn main() -> Result<(), std::io::Error> { - let start = Instant::now(); - let mut rng = rand::thread_rng(); - // Set to 1 to do full res. - let dev_factor = 4; - let nx = 1200 / dev_factor; - let ny = 800 / dev_factor; - let ns = 100 / dev_factor; - let (cam, world) = if BOOK_COVER { +struct Scene { + world: HitableList, + camera: Camera, + subsamples: usize, + width: usize, + height: usize, +} + +fn build_scene(width: usize, height: usize, subsamples: usize) -> Scene { + let (camera, world) = if BOOK_COVER { let lookfrom = Vec3::new(13., 2., 3.); let lookat = Vec3::new(0., 0., 0.); let dist_to_focus = 10.; @@ -122,7 +125,7 @@ fn main() -> Result<(), std::io::Error> { lookat, Vec3::new(0., 1., 0.), 20., - nx as f32 / ny as f32, + width as f32 / height as f32, aperture, dist_to_focus, ); @@ -138,7 +141,7 @@ fn main() -> Result<(), std::io::Error> { lookat, Vec3::new(0., 1., 0.), 20., - nx as f32 / ny as f32, + width as f32 / height as f32, aperture, dist_to_focus, ); @@ -171,28 +174,60 @@ fn main() -> Result<(), std::io::Error> { ]); (cam, world) }; - - let mut img = RgbImage::new(nx, ny); - for j in 0..ny { - for i in 0..nx { - let mut col: Vec3 = Default::default(); - for _ in 0..ns { - let u = (rng.gen_range::(0., 1.) + i as f32) / nx as f32; - let v = (rng.gen_range::(0., 1.) + j as f32) / ny as f32; - let r = cam.get_ray(u, v); - col = col + color(r, &world, 0); - } - col = col / ns as f32; - // Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2 - // or sqrt. - col = Vec3::new(col[0].sqrt(), col[1].sqrt(), col[2].sqrt()); - let ir = (255.99 * col[0]) as u8; - let ig = (255.99 * col[1]) as u8; - let ib = (255.99 * col[2]) as u8; - // ny-j is to flip y-axis. - img.put_pixel(i, ny - j - 1, image::Rgb([ir, ig, ib])); - } + Scene { + camera, + world, + subsamples, + width, + height, } +} + +fn trace_pixel(x: usize, y: usize, scene: &Scene) -> [u8; 3] { + let mut rng = rand::thread_rng(); + let mut col: Vec3 = Default::default(); + for _ in 0..scene.subsamples { + let u = (rng.gen_range::(0., 1.) + x as f32) / scene.width as f32; + let v = (rng.gen_range::(0., 1.) + y as f32) / scene.height as f32; + let ray = scene.camera.get_ray(u, v); + col = col + color(ray, &scene.world, 0); + } + col = col / scene.subsamples as f32; + // Gamma correct, use gamma 2 correction, which is 1/gamma where gamma=2 which is 1/2 + // or sqrt. + col = Vec3::new(col[0].sqrt(), col[1].sqrt(), col[2].sqrt()); + let ir = (255.99 * col[0]) as u8; + let ig = (255.99 * col[1]) as u8; + let ib = (255.99 * col[2]) as u8; + [ir, ig, ib] +} + +fn main() -> Result<(), std::io::Error> { + let start = Instant::now(); + // Set to 1 to do full res. + let dev_factor = 1; + let width = 1200 / dev_factor; + let height = 800 / dev_factor; + let subsamples = 1000 / dev_factor; + + let scene = build_scene(width, height, subsamples); + let mut img = RgbImage::new(width as u32, height as u32); + let coords: Vec<_> = (0..height) + .flat_map(|j| (0..width).map(move |i| (i, j))) + .collect(); + + let pixels = coords + .par_iter() + .map(|(i, j)| { + let p = trace_pixel(*i, *j, &scene); + // height-j is to flip y-axis. + (*i as u32, (height - *j - 1) as u32, image::Rgb(p)) + }) + .collect::>(); + + pixels + .iter() + .for_each(|(x, y, p)| img.put_pixel(*x, *y, *p)); let runtime = start.elapsed(); eprintln!( "Render time {}.{} seconds", diff --git a/rtiow/src/hitable.rs b/rtiow/src/hitable.rs index fc4b955..3dbad07 100644 --- a/rtiow/src/hitable.rs +++ b/rtiow/src/hitable.rs @@ -9,6 +9,6 @@ pub struct HitRecord<'m> { pub material: &'m Material, } -pub trait Hit: Sync { +pub trait Hit: Send + Sync { fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option; } diff --git a/rtiow/src/material.rs b/rtiow/src/material.rs index 7bcf0ac..b40b6af 100644 --- a/rtiow/src/material.rs +++ b/rtiow/src/material.rs @@ -28,7 +28,7 @@ pub struct ScatterResponse { pub reflected: bool, } -pub trait Material: Sync { +pub trait Material: Send + Sync { fn scatter(&self, r_in: &Ray, rec: &HitRecord) -> ScatterResponse; } diff --git a/rtiow/src/sphere.rs b/rtiow/src/sphere.rs index c118012..42325a1 100644 --- a/rtiow/src/sphere.rs +++ b/rtiow/src/sphere.rs @@ -31,21 +31,21 @@ impl Hit for Sphere { if discriminant > 0. { let temp = (-b - (b * b - a * c).sqrt()) / a; if temp < t_max && temp > t_min { - let p = r.point_at_parameter(temp); + let point = r.point_at_parameter(temp); return Some(HitRecord { t: temp, - p, - normal: (p - self.center) / self.radius, + p: point, + normal: (point - self.center) / self.radius, material: &*self.material, }); } let temp = (-b + (b * b - a * c).sqrt()) / a; if temp < t_max && temp > t_min { - let p = r.point_at_parameter(temp); + let point = r.point_at_parameter(temp); return Some(HitRecord { t: temp, - p, - normal: (p - self.center) / self.radius, + p: point, + normal: (point - self.center) / self.radius, material: &*self.material, }); }