diff --git a/Cargo.lock b/Cargo.lock index c7a5724..4f7eb32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "cfg-if" version = "1.0.0" @@ -21,6 +27,49 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + [[package]] name = "either" version = "1.9.0" @@ -53,6 +102,12 @@ dependencies = [ "rand", ] +[[package]] +name = "hermit-abi" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" + [[package]] name = "indicatif" version = "0.17.6" @@ -63,6 +118,7 @@ dependencies = [ "instant", "number_prefix", "portable-atomic", + "rayon", "unicode-width", ] @@ -96,6 +152,25 @@ version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "number_prefix" version = "0.4.0" @@ -144,6 +219,28 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "raytracing-in-one-weekend" version = "0.1.0" @@ -152,8 +249,15 @@ dependencies = [ "indicatif", "itertools", "rand", + "rayon", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "unicode-width" version = "0.1.10" diff --git a/Cargo.toml b/Cargo.toml index 640a47b..7aed2bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] glam = { version = "0.24.1", features = ["rand"] } -indicatif = "0.17.6" +indicatif = { version = "0.17.6", features = ["rayon"] } itertools = "0.11.0" rand = "0.8.5" +rayon = "1.7.0" diff --git a/src/main.rs b/src/main.rs index b67136a..4d8db18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ use glam::DVec3; +use indicatif::ParallelProgressIterator; use indicatif::ProgressIterator; use itertools::Itertools; use rand::prelude::*; -use std::{fs, io, ops::Range}; +use rayon::iter::IntoParallelRefIterator; +use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use rayon::prelude::*; +use std::{fs, io, ops::Range}; fn main() -> io::Result<()> { let mut rng = rand::thread_rng(); @@ -111,7 +115,7 @@ fn main() -> io::Result<()> { look_at: Some(DVec3::ZERO), vup: Some(DVec3::Y), focus_dist: Some(10.0), - defocus_angle: Some(0.6), + defocus_angle: Some(0.0), }); camera.render_to_disk(world)?; @@ -301,10 +305,12 @@ impl Camera { } fn render_to_disk(&self, world: T) -> io::Result<()> where - T: Hittable, + T: Hittable + std::marker::Sync, { let pixels = (0..self.image_height) .cartesian_product(0..self.image_width) + .collect::>() + .into_par_iter() .progress_count( self.image_height as u64 * self.image_width as u64, @@ -347,6 +353,7 @@ impl Camera { color.x, color.y, color.z ) }) + .collect::>() .join("\n"); fs::write( "output.ppm", @@ -379,7 +386,7 @@ impl Ray { } fn color(&self, depth: i32, world: &T) -> DVec3 where - T: Hittable, + T: Hittable + std::marker::Sync, { if depth <= 0 { return DVec3::new(0., 0., 0.); @@ -656,7 +663,7 @@ impl Hittable for Sphere { } struct HittableList { - objects: Vec>, + objects: Vec>, } impl HittableList { fn clear(&mut self) { @@ -665,7 +672,7 @@ impl HittableList { fn add(&mut self, object: T) where - T: Hittable + 'static, + T: Hittable + 'static + Sync, { // was push_back self.objects.push(Box::new(object));