aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/render.rs b/src/render.rs
index 740c3eb..47c6698 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,6 +1,7 @@
use cgmath::prelude::*;
use cgmath::{Vector3, vec3};
use rand::prelude::*;
+use rayon::prelude::*;
use std::f64::INFINITY;
use crate::camera::Camera;
use crate::scene::Scene;
@@ -23,20 +24,34 @@ fn ray_colour(rng: &mut ThreadRng, ray: &Ray, scene: &Scene, depth: i32) -> Vect
}
}
-pub fn render(world: &Scene, camera: Camera, image_height: i32, image_width: i32,
- samples: i32, max_depth: i32, rng: &mut ThreadRng) {
+pub fn thread_render(world: &Scene, camera: &Camera, rng: &mut ThreadRng, image_height: i32,
+ image_width: i32, samples: i32, max_depth: i32, i: i32, j: i32) -> Vector3<f64> {
+ let mut colour = vec3(0.0, 0.0, 0.0);
+ for _ in 0 .. samples {
+ let ru: f64 = rng.gen();
+ let rv: f64 = rng.gen();
+ let u = (i as f64 + ru) / image_width as f64;
+ let v = ((image_height - 1 - j) as f64 + rv) / image_height as f64;
+ let ray = camera.get_ray(rng, u, v);
+ colour += ray_colour(rng, &ray, &world, max_depth);
+ }
+ colour
+}
+
+pub fn render(world: &Scene, camera: &Camera, image_height: i32, image_width: i32,
+ samples: i32, max_depth: i32, threads: i32) {
for j in 0 .. image_height {
eprint!("\rScanlines: {:3} / {:3}", j+1, image_height);
for i in 0 .. image_width {
- let mut colour = vec3(0.0, 0.0, 0.0);
- for _ in 0 .. samples {
- let ru: f64 = rng.gen();
- let rv: f64 = rng.gen();
- let u = (i as f64 + ru) / image_width as f64;
- let v = ((image_height - 1 - j) as f64 + rv) / image_height as f64;
- let ray = camera.get_ray(rng, u, v);
- colour += ray_colour(rng, &ray, &world, max_depth);
- }
+ let colours = (0 .. threads)
+ .into_par_iter()
+ .map(|_| {
+ let mut trng = thread_rng();
+ thread_render(
+ world, camera, &mut trng, image_height, image_width,
+ samples / threads, max_depth, i, j)
+ });
+ let colour = colours.sum::<Vector3<f64>>();
print_colour(&colour, samples);
}
}