/
scene.rs
242 lines (226 loc) · 8.43 KB
/
scene.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use camera::Camera;
use collision::{ray, Ray, RayHit, Sphere, SpheresSoA};
use material::Material;
use math::maxf;
use rand::{weak_rng, Rng, SeedableRng, XorShiftRng};
use rayon::prelude::*;
use std::f32;
use std::sync::atomic::{AtomicUsize, Ordering};
use vmath::{vec3, Vec3};
const MAX_T: f32 = f32::MAX;
const MIN_T: f32 = 0.001;
#[derive(Copy, Clone)]
pub struct Params {
pub width: u32,
pub height: u32,
pub samples: u32,
pub max_depth: u32,
pub random_seed: bool,
}
pub struct Scene {
spheres: SpheresSoA,
materials: Vec<Material>,
emissive: Vec<u32>,
ray_count: AtomicUsize,
}
impl Scene {
pub fn new(sphere_materials: &[(Sphere, Material)]) -> Scene {
let (spheres, materials): (Vec<Sphere>, Vec<Material>) =
sphere_materials.iter().cloned().unzip();
let mut emissive = vec![];
for (index, material) in materials.iter().enumerate() {
if material.emissive.length_squared() > 0.0 {
emissive.push(index as u32);
}
}
Scene {
spheres: SpheresSoA::new(&spheres),
materials,
emissive,
ray_count: AtomicUsize::new(0),
}
}
fn ray_hit(&self, ray: &Ray, t_min: f32, t_max: f32) -> Option<(RayHit, u32)> {
unsafe { self.spheres.hit_simd(ray, t_min, t_max) }
}
fn sample_lights(
&self,
ray_in: &Ray,
ray_in_hit: &RayHit,
in_hit_index: u32,
attenuation: Vec3,
rng: &mut XorShiftRng,
ray_count: &mut usize,
) -> Vec3 {
let mut emissive_out = Vec3::zero();
for index in &self.emissive {
if *index == in_hit_index {
// skip self
continue;
}
// create a random direction towards sphere
// coord system for sampling: sw, su, sv
let sphere_centre = self.spheres.centre(*index);
let sphere_radius_sq = self.spheres.radius_sq(*index);
let sw = (sphere_centre - ray_in_hit.point).normalize();
let su = (if sw.get_x().abs() > 0.01 {
vec3(0.0, 1.0, 0.0)
} else {
vec3(1.0, 0.0, 0.0)
}).cross(sw)
.normalize();
let sv = sw.cross(su);
// sample sphere by solid angle
let cos_a_max = (1.0
- sphere_radius_sq / (ray_in_hit.point - sphere_centre).length_squared())
.sqrt();
let eps1 = rng.next_f32();
let eps2 = rng.next_f32();
let cos_a = 1.0 - eps1 + eps1 * cos_a_max;
let sin_a = (1.0 - cos_a * cos_a).sqrt();
let phi = 2.0 * f32::consts::PI * eps2;
let l = su * (phi.cos() * sin_a) + sv * (phi.sin() * sin_a) + sw * cos_a;
//l = normalize(l); // NOTE(fg): This is already normalized, by construction.
*ray_count += 1;
let ray_out = ray(ray_in_hit.point, l);
if let Some((_, out_hit_index)) = self.ray_hit(&ray_out, MIN_T, MAX_T) {
if *index == out_hit_index {
let omega = 2.0 * f32::consts::PI * (1.0 - cos_a_max);
let rdir = ray_in.direction;
let nl = if ray_in_hit.normal.dot(rdir) < 0.0 {
ray_in_hit.normal
} else {
-ray_in_hit.normal
};
let light_emission = self.materials[*index as usize].emissive;
emissive_out += (attenuation * light_emission)
* (maxf(0.0, l.dot(nl)) * omega / f32::consts::PI);
}
}
}
emissive_out
}
fn ray_trace(
&self,
ray_in: &Ray,
depth: u32,
max_depth: u32,
do_material_emission: bool,
rng: &mut XorShiftRng,
ray_count: &mut usize,
) -> Vec3 {
*ray_count += 1;
if let Some((ray_hit, hit_index)) = self.ray_hit(ray_in, MIN_T, MAX_T) {
let material = &self.materials[hit_index as usize];
if depth < max_depth {
if let Some((attenuation, scattered, do_light_sampling)) =
material.scatter(ray_in, &ray_hit, rng)
{
let light_emission = if do_light_sampling {
self.sample_lights(ray_in, &ray_hit, hit_index, attenuation, rng, ray_count)
} else {
Vec3::zero()
};
// don't do material emission if a previous call has already done explicit
// light sampling and added the contribution
let material_emission = if do_material_emission {
material.emissive
} else {
Vec3::zero()
};
let do_material_emission = !do_light_sampling;
return material_emission + light_emission
+ attenuation
* self.ray_trace(
&scattered,
depth + 1,
max_depth,
do_material_emission,
rng,
ray_count,
);
}
}
return material.emissive;
} else {
// sky
let t = 0.5 * (ray_in.direction.get_y() + 1.0);
(1.0 - t) * vec3(1.0, 1.0, 1.0) + t * vec3(0.5, 0.7, 1.0) * 0.3
}
}
pub fn update(
&self,
params: &Params,
camera: &Camera,
frame_num: u32,
buffer: &mut [(f32, f32, f32)],
) -> usize {
self.ray_count.store(0, Ordering::Relaxed);
let inv_nx = 1.0 / params.width as f32;
let inv_ny = 1.0 / params.height as f32;
let inv_ns = 1.0 / params.samples as f32;
let mix_prev = frame_num as f32 / (frame_num + 1) as f32;
let mix_new = 1.0 - mix_prev;
// parallel iterate each row of pixels
buffer
.par_chunks_mut(params.width as usize)
.enumerate()
.for_each(|(j, row)| {
let mut ray_count = 0;
let mut rng = if params.random_seed {
weak_rng()
} else {
let state = (j as u32 * 9781 + frame_num * 6271) | 1;
XorShiftRng::from_seed([state, state, state, state])
};
row.iter_mut().enumerate().for_each(|(i, color_out)| {
let mut col = Vec3::zero();
for _ in 0..params.samples {
let u = (i as f32 + rng.next_f32()) * inv_nx;
let v = (j as f32 + rng.next_f32()) * inv_ny;
let ray = camera.get_ray(u, v, &mut rng);
col += self.ray_trace(
&ray,
0,
params.max_depth,
true,
&mut rng,
&mut ray_count,
);
}
col *= inv_ns;
color_out.0 = color_out.0 * mix_prev + col.get_x() * mix_new;
color_out.1 = color_out.1 * mix_prev + col.get_y() * mix_new;
color_out.2 = color_out.2 * mix_prev + col.get_z() * mix_new;
});
self.ray_count.fetch_add(ray_count, Ordering::Relaxed);
});
self.ray_count.load(Ordering::Relaxed)
}
}
#[cfg(test)]
mod bench {
use presets;
use rand::{SeedableRng, XorShiftRng};
use scene::Params;
use std::f32;
use test::{black_box, Bencher};
const FIXED_SEED: [u32; 4] = [0x193a_6754, 0xa8a7_d469, 0x9783_0e05, 0x113b_a7bb];
#[bench]
fn ray_hit(b: &mut Bencher) {
const MAX_T: f32 = f32::MAX;
const MIN_T: f32 = 0.001;
let seed = black_box(FIXED_SEED);
let mut rng = XorShiftRng::from_seed(seed);
let params = Params {
width: 200,
height: 100,
samples: 10,
max_depth: 10,
random_seed: false,
};
let (scene, camera) = presets::aras_p(¶ms);
let ray = camera.get_ray(0.5, 0.5, &mut rng);
b.iter(|| scene.ray_hit(&ray, MIN_T, MAX_T));
}
}