Skip to content

Commit

Permalink
added parallel processing using rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
waridh committed Sep 8, 2024
1 parent 1c1d845 commit 58720ce
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"

[dependencies]
anyhow = "1.0.86"
indicatif = "0.17.8"
indicatif = { version = "*", features = ["rayon"] }
rand = "0.8.5"
rayon = "1.10.0"
48 changes: 27 additions & 21 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ray::{self},
vec3::{self, Point3, Vec3},
};
use indicatif::{ProgressBar, ProgressStyle};
use indicatif::{ParallelProgressIterator, ProgressBar, ProgressIterator, ProgressStyle};
use rand::{self, Rng};
use rayon::prelude::*;
use std::f32::INFINITY;
Expand Down Expand Up @@ -127,24 +127,35 @@ impl Camera {

println!("P3\n{} {}\n255", self.image_width, self.image_height);

for j in 0..self.image_height {
for i in 0..self.image_width {
let mut pixel = Color::black();
for _ in 0..self.samples_per_pixel {
let r = self.get_ray(i, j);
pixel += Camera::ray_color(r, self.reflection_depth, world);
}

println!("{}", pixel * self.pixel_sample_scale);
}
bar.inc(1);
}
bar.finish();
let pixels = (0..self.image_height)
.into_par_iter()
.progress_with(bar)
.flat_map(|j| {
(0..self.image_width)
.map(|i| {
(0..self.samples_per_pixel)
.map(|_| {
let r = self.get_ray(i, j);
Camera::ray_color(r, self.reflection_depth, world)
})
.sum::<Color>()
})
.collect::<Vec<Color>>()
})
.collect::<Vec<Color>>();

let spinner = ProgressBar::new_spinner();
spinner.enable_steady_tick(std::time::Duration::from_millis(100));

pixels
.iter()
.progress_with(spinner)
.for_each(|e| println!("{}", self.pixel_sample_scale * e));
}

/// Create a ray from the defocus lens in the camera center, and direct
/// it at the pixel square
fn get_ray(&mut self, i: usize, j: usize) -> ray::Ray {
fn get_ray(&self, i: usize, j: usize) -> ray::Ray {
let offset = self.sample_square();
let pixel_center = self.pixel00
+ (self.pixel_delta_u * ((i as f32) + offset[0]))
Expand All @@ -161,7 +172,7 @@ impl Camera {
ray::Ray::new(raydir, ray_orig)
}

fn sample_square(&mut self) -> vec3::Vec3 {
fn sample_square(&self) -> vec3::Vec3 {
let mut rng = rand::thread_rng();
vec3::Vec3(rng.gen_range(-0.5..0.5), rng.gen_range(-0.5..0.5), 0.)
}
Expand All @@ -187,8 +198,3 @@ impl Camera {
}
}
}

#[cfg(test)]
mod test {
use super::*;
}
23 changes: 22 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::vec3;
use std::{fmt, ops};
use std::{fmt, iter, ops};

pub fn linear_to_gamma(linear: f32) -> f32 {
match linear {
Expand Down Expand Up @@ -35,6 +35,12 @@ impl From<(f32, f32, f32)> for Color {
}
}

impl iter::Sum for Color {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Color::new(0., 0., 0.), |a, e| a + e)
}
}

/// Used to clamp the color output
fn clamp(legal_range: &ops::Range<f32>, val: f32) -> f32 {
match val {
Expand Down Expand Up @@ -82,13 +88,28 @@ impl ops::Mul<f32> for Color {
}
}

impl ops::Mul<f32> for &Color {
type Output = Color;
fn mul(self, rhs: f32) -> Self::Output {
let new_val = (self[0] * rhs, self[1] * rhs, self[2] * rhs);
Color::from(new_val)
}
}

impl ops::Mul<Color> for f32 {
type Output = Color;
fn mul(self, rhs: Color) -> Self::Output {
rhs * self
}
}

impl ops::Mul<&Color> for f32 {
type Output = Color;
fn mul(self, rhs: &Color) -> Self::Output {
rhs * self
}
}

impl ops::Mul<Color> for Color {
type Output = Color;
fn mul(self, rhs: Color) -> Self::Output {
Expand Down

0 comments on commit 58720ce

Please sign in to comment.