Skip to content

Commit

Permalink
Enable concurrency.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbiffle committed Apr 9, 2019
1 parent e9bbbe3 commit fcebb90
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
112 changes: 112 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"

[dependencies]
rand = "0.6.5"
rayon = "1.0"
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rand::prelude::*;
use rayon::prelude::*;
use std::sync::Arc;

#[derive(Copy, Clone, Default, Debug)]
Expand Down Expand Up @@ -484,9 +485,10 @@ fn random_scene() -> Vec<Box<dyn Object>> {
struct Image(Vec<Vec<Vec3>>);

impl Image {
fn compute(nx: usize, ny: usize, mut f: impl FnMut(usize, usize) -> Vec3) -> Image {
fn compute(nx: usize, ny: usize, f: impl Fn(usize, usize) -> Vec3 + Sync) -> Image {
Image(
(0..ny)
.into_par_iter()
.rev()
.map(|y| (0..nx).map(|x| f(x, y)).collect())
.collect(),
Expand Down Expand Up @@ -530,11 +532,11 @@ fn main() {
aperture,
dist_to_focus,
);
let mut rng = rand::thread_rng();

let image = Image::compute(NX, NY, |x, y| {
let col: Vec3 = (0..NS)
.map(|_| {
let mut rng = rand::thread_rng();
let u = (x as f32 + rng.gen::<f32>()) / NX as f32;
let v = (y as f32 + rng.gen::<f32>()) / NY as f32;
let r = camera.get_ray(u, v);
Expand Down

0 comments on commit fcebb90

Please sign in to comment.