Skip to content

Commit

Permalink
rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBiscardi committed Aug 28, 2023
1 parent e284e5d commit ba8ca6d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -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"
19 changes: 13 additions & 6 deletions 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();

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -301,10 +305,12 @@ impl Camera {
}
fn render_to_disk<T>(&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::<Vec<(u32, u32)>>()
.into_par_iter()
.progress_count(
self.image_height as u64
* self.image_width as u64,
Expand Down Expand Up @@ -347,6 +353,7 @@ impl Camera {
color.x, color.y, color.z
)
})
.collect::<Vec<String>>()
.join("\n");
fs::write(
"output.ppm",
Expand Down Expand Up @@ -379,7 +386,7 @@ impl Ray {
}
fn color<T>(&self, depth: i32, world: &T) -> DVec3
where
T: Hittable,
T: Hittable + std::marker::Sync,
{
if depth <= 0 {
return DVec3::new(0., 0., 0.);
Expand Down Expand Up @@ -656,7 +663,7 @@ impl Hittable for Sphere {
}

struct HittableList {
objects: Vec<Box<dyn Hittable>>,
objects: Vec<Box<dyn Hittable + Sync>>,
}
impl HittableList {
fn clear(&mut self) {
Expand All @@ -665,7 +672,7 @@ impl HittableList {

fn add<T>(&mut self, object: T)
where
T: Hittable + 'static,
T: Hittable + 'static + Sync,
{
// was push_back
self.objects.push(Box::new(object));
Expand Down

0 comments on commit ba8ca6d

Please sign in to comment.