Skip to content

Commit

Permalink
[none] Rendering of image using Rayon
Browse files Browse the repository at this point in the history
- Parallel iterator used for outer i,j loops
to render image faster

%SOFTWARE
  • Loading branch information
soumyasen1809 committed Aug 15, 2024
1 parent cac498f commit 489cc21
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/.[Vv]scode
*.ppm
*.png
32 changes: 18 additions & 14 deletions lib/src/utilities/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,30 @@ impl Camera {
pub fn render(&mut self, world: Vec<Box<dyn Hittable>>) {
self.initialize();

let mut pixel_color_vec: Vec<Color> = Vec::new();

// Render and write to file
let file_path = "image_test.ppm";
let mut file = File::create(&file_path).unwrap();
writeln!(file, "P3\n{} {}\n255", self.image_width, self.image_height).unwrap();
for y_index in 0..self.image_height {
println!("Remaining scanlines: {}", self.image_height - y_index); // Adding a Progress Indicator
for x_index in 0..self.image_width {
let pixel_color: Color = (0..self.samples_per_pixel)

let pixel_color_vec: Vec<Color> = (0..self.image_height)
.into_par_iter()
.flat_map(|y_index| {
// If we use a .map(..) here, we will get output as Vec<Vec<Color>> instead
(0..self.image_width)
.into_par_iter()
.map(|_| {
let ray_sent: Ray = self.get_ray(x_index, y_index);
Self::ray_color(ray_sent, self.max_depth, &world[..])
.map(|x_index| {
let pixel_color: Color = (0..self.samples_per_pixel)
.into_par_iter()
.map(|_| {
let ray_sent: Ray = self.get_ray(x_index, y_index);
Self::ray_color(ray_sent, self.max_depth, &world[..])
})
.sum(); // need to implement sum trait for Color
return pixel_color; // Return the Color from the map closure
})
.sum(); // need to implement sum trait for Color

pixel_color_vec.push(pixel_color);
}
}
.collect::<Vec<Color>>() // Collect the inner Vec<Color>
})
.collect(); // Collect the outer Vec<Color>

for pixel in pixel_color_vec.iter() {
let write_res = (*pixel * self.pixel_samples_scale).write_color(&mut file);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utilities/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ impl Add for Color {

impl Sum for Color {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
// iter.fold() is a method in Rust that allows you to
// accumulate or reduce the elements of an iterator into a single value.
// init: The initial value of the accumulator, which is passed to
// the closure as the first argument in the first iteration.
// E.g.: Summing a list: numbers.iter().fold(0, |a, &b| a + b);
iter.fold(Color::default(), |a, b| a + b)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use lib::utilities::{
const NUMBER_BALLS: i32 = 5;

const ASPECT_RATIO: f64 = 16.0 / 9.0;
const IMAGE_WIDTH: i32 = 160;
const SAMPLES_PER_PIXEL: i32 = 100;
const IMAGE_WIDTH: i32 = 1600;
const SAMPLES_PER_PIXEL: i32 = 200;
const MAX_DEPTH: i32 = 50;
const VERTICAL_FOV: f64 = 40.0;

Expand Down

0 comments on commit 489cc21

Please sign in to comment.