From e5fdc8c7ef10f24e5799ca9750e4760e7fb89a85 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Wed, 11 May 2022 21:41:41 +0200 Subject: [PATCH] First try, need to swap to Vec for world --- Cargo.lock | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/main.rs | 21 ++++++---- 3 files changed, 131 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b85a7d4..ba36ca2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,51 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1145cf131a2c6ba0615079ab6a638f7e1973ac9c2634fcbeaaad6114246efe8c" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38" +dependencies = [ + "cfg-if", + "lazy_static", +] + [[package]] name = "deflate" version = "1.0.0" @@ -68,6 +113,12 @@ dependencies = [ "adler32", ] +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "getrandom" version = "0.2.6" @@ -79,6 +130,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "image" version = "0.24.2" @@ -101,12 +161,27 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be7ef4b99870f0c9f2fc2f20dbef72707e2bcca675bb9196734cf433e999b0c5" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.125" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + [[package]] name = "miniz_oxide" version = "0.5.1" @@ -157,6 +232,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "png" version = "0.17.5" @@ -205,14 +290,45 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd249e82c21598a9a426a4e00dd7adc1d640b22445ec8545feef801d1a74c221" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f51245e1e62e1f1629cbfec37b5793bbabcaeb90f30e94d2ba03564687353e4" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "renderer" version = "0.1.0" dependencies = [ "image", "rand", + "rayon", ] +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index e01e985..f4397d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] image = { version = "0.24.2", default-features = false, features = ["jpeg", "png", "pnm"] } -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" +rayon = "1.5.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 572353c..19f9406 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use camera::Camera; use hittable::{HitRecord, Hittable, Sphere}; use hittable_list::HittableList; use image::{Rgb, RgbImage}; +use rayon::prelude::*; use ray::Ray; use vec3::{Color, Point3, Vec3}; @@ -85,15 +86,23 @@ fn main() { //let mut file = BufWriter::new(File::create(default_file).unwrap()); //writeln!(&mut file, "P3\n{image_width} {image_height}\n255\n").unwrap(); + TODO: // world in vec umwandeln + for j in (0..image_height).rev() { eprint!("\rScanlines remaining: {j:5}"); - for i in 0..image_width { - let mut pixel_color = Color::new(0.0, 0.0, 0.0); + (0..image_width).into_par_iter().for_each(|i| pixel_color_calc(&mut image, &cam, &world, j, i, image_width, image_height, samples_per_pixel)); + } + image.save(default_file).unwrap(); + eprintln!("\nDone!"); +} + +fn pixel_color_calc(image: &mut RgbImage, cam: &Camera, world: &HittableList, j: u32, i: u32, image_width: u32, image_height: u32, samples_per_pixel: u32) { + let mut pixel_color = Color::new(0.0, 0.0, 0.0); for _ in 0..samples_per_pixel { let u = (i as f64 + utility::random_f64()) / (image_width - 1) as f64; let v = (j as f64 + utility::random_f64()) / (image_height - 1) as f64; let r = cam.get_ray(u, v); - pixel_color += ray_color(&r, &mut world); + pixel_color += ray_color(&r, &world); } //color::write_color(&mut file, &pixel_color); @@ -104,8 +113,4 @@ fn main() { image_height - j - 1, samples_per_pixel, ); - } - } - image.save(default_file).unwrap(); - eprintln!("\nDone!"); -} +} \ No newline at end of file