use crate::math::Vector3;
use std::f32::consts::PI;
#[derive(Debug, Clone, Copy)]
pub struct Rng {
state: u64,
inc: u64,
sample_index: u32,
pixel_seed: u32,
dim: u32,
stratified: bool,
}
impl Rng {
pub fn new(stream: u64, seed: u64) -> Self {
let mut rng = Self {
state: 0,
inc: (stream << 1) | 1,
sample_index: 0,
pixel_seed: 0,
dim: 0,
stratified: false,
};
rng.next_u32();
rng.state = rng.state.wrapping_add(hash64(seed));
rng.next_u32();
rng
}
pub fn for_sample(x: u32, y: u32, sample: u32, seed: u64) -> Self {
let pixel = (y as u64) << 32 | x as u64;
let mut rng = Self::new(hash64(pixel ^ seed.rotate_left(17)), sample as u64);
rng.sample_index = sample;
rng.pixel_seed = (hash64(pixel ^ seed) >> 32) as u32;
rng.stratified = true;
rng
}
pub fn set_bounce(&mut self, bounce: u32) {
self.dim = bounce.saturating_mul(DIMENSIONS_PER_BOUNCE);
}
pub fn next_u32(&mut self) -> u32 {
let old = self.state;
self.state = old.wrapping_mul(6364136223846793005).wrapping_add(self.inc);
let xorshifted = (((old >> 18) ^ old) >> 27) as u32;
let rot = (old >> 59) as u32;
xorshifted.rotate_right(rot)
}
pub fn next_f32(&mut self) -> f32 {
(self.next_u32() >> 8) as f32 * (1.0 / 16_777_216.0)
}
pub fn next_2d(&mut self) -> (f32, f32) {
if !self.stratified {
return (self.next_f32(), self.next_f32());
}
let d = self.dim;
self.dim = self.dim.wrapping_add(1);
sobol02_owen(
self.sample_index,
self.pixel_seed ^ hash32(d.wrapping_add(1)),
)
}
pub fn next_index(&mut self, n: usize) -> usize {
if n == 0 {
return 0;
}
((self.next_f32() as f64 * n as f64) as usize).min(n - 1)
}
}
fn hash64(mut x: u64) -> u64 {
x = x.wrapping_add(0x9E3779B97F4A7C15);
x = (x ^ (x >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
x = (x ^ (x >> 27)).wrapping_mul(0x94D049BB133111EB);
x ^ (x >> 31)
}
#[derive(Debug, Clone, Copy)]
pub struct Onb {
pub t: Vector3,
pub b: Vector3,
pub n: Vector3,
}
impl Onb {
pub fn new(n: Vector3) -> Self {
let sign = if n.z >= 0.0 { 1.0 } else { -1.0 };
let a = -1.0 / (sign + n.z);
let b = n.x * n.y * a;
Self {
t: Vector3::new(1.0 + sign * n.x * n.x * a, sign * b, -sign * n.x),
b: Vector3::new(b, sign + n.y * n.y * a, -n.y),
n,
}
}
pub fn to_world(&self, v: Vector3) -> Vector3 {
self.t * v.x + self.b * v.y + self.n * v.z
}
pub fn to_local(&self, v: Vector3) -> Vector3 {
Vector3::new(v.dot(self.t), v.dot(self.b), v.dot(self.n))
}
}
pub fn cosine_hemisphere(u1: f32, u2: f32) -> Vector3 {
let (dx, dy) = concentric_disk(u1, u2);
let z = (1.0 - dx * dx - dy * dy).max(0.0).sqrt();
Vector3::new(dx, dy, z)
}
pub fn cosine_hemisphere_pdf(cos_theta: f32) -> f32 {
(cos_theta / PI).max(0.0)
}
pub fn concentric_disk(u1: f32, u2: f32) -> (f32, f32) {
let ox = 2.0 * u1 - 1.0;
let oy = 2.0 * u2 - 1.0;
if ox == 0.0 && oy == 0.0 {
return (0.0, 0.0);
}
let (r, theta) = if ox.abs() > oy.abs() {
(ox, PI / 4.0 * (oy / ox))
} else {
(oy, PI / 2.0 - PI / 4.0 * (ox / oy))
};
(r * theta.cos(), r * theta.sin())
}
pub fn uniform_sphere(u1: f32, u2: f32) -> Vector3 {
let z = 1.0 - 2.0 * u1;
let r = (1.0 - z * z).max(0.0).sqrt();
let phi = 2.0 * PI * u2;
Vector3::new(r * phi.cos(), r * phi.sin(), z)
}
pub fn uniform_cone(u1: f32, u2: f32, cos_theta_max: f32) -> Vector3 {
let cos_theta = 1.0 - u1 * (1.0 - cos_theta_max);
let sin_theta = (1.0 - cos_theta * cos_theta).max(0.0).sqrt();
let phi = 2.0 * PI * u2;
Vector3::new(sin_theta * phi.cos(), sin_theta * phi.sin(), cos_theta)
}
pub fn uniform_cone_pdf(cos_theta_max: f32) -> f32 {
let solid_angle = 2.0 * PI * (1.0 - cos_theta_max);
if solid_angle <= 0.0 {
0.0
} else {
1.0 / solid_angle
}
}
pub fn sample_ggx_vndf(view: Vector3, ax: f32, ay: f32, u1: f32, u2: f32) -> Vector3 {
let vh = Vector3::new(ax * view.x, ay * view.y, view.z).normalize();
let len_sq = vh.x * vh.x + vh.y * vh.y;
let t1 = if len_sq > 0.0 {
Vector3::new(-vh.y, vh.x, 0.0) * (1.0 / len_sq.sqrt())
} else {
Vector3::new(1.0, 0.0, 0.0)
};
let t2 = vh.cross(t1);
let r = u1.sqrt();
let phi = 2.0 * PI * u2;
let p1 = r * phi.cos();
let mut p2 = r * phi.sin();
let s = 0.5 * (1.0 + vh.z);
p2 = (1.0 - s) * (1.0 - p1 * p1).max(0.0).sqrt() + s * p2;
let nh = t1 * p1 + t2 * p2 + vh * (1.0 - p1 * p1 - p2 * p2).max(0.0).sqrt();
Vector3::new(ax * nh.x, ay * nh.y, nh.z.max(1e-6)).normalize()
}
const DIMENSIONS_PER_BOUNCE: u32 = 8;
pub fn sobol02_owen(index: u32, seed: u32) -> (f32, f32) {
let shuffled = owen_scramble(index, hash32(seed ^ 0x9e37_79b9));
let x = owen_scramble(sobol_x(shuffled), hash32(seed ^ 0x6c50_b47c));
let y = owen_scramble(sobol_y(shuffled), hash32(seed ^ 0xb82f_1e52));
(to_unit(x), to_unit(y))
}
fn owen_scramble(x: u32, seed: u32) -> u32 {
let mut x = x.reverse_bits();
x = x.wrapping_add(seed);
x ^= x.wrapping_mul(0x6c50_b47c);
x ^= x.wrapping_mul(0xb82f_1e52);
x ^= x.wrapping_mul(0xc7af_e638);
x ^= x.wrapping_mul(0x8d22_f6e6);
x.reverse_bits()
}
fn sobol_x(index: u32) -> u32 {
index.reverse_bits()
}
fn sobol_y(mut index: u32) -> u32 {
let mut result = 0u32;
let mut v = 1u32 << 31;
while index != 0 {
if index & 1 != 0 {
result ^= v;
}
index >>= 1;
v ^= v >> 1;
}
result
}
fn hash32(mut x: u32) -> u32 {
x ^= x >> 16;
x = x.wrapping_mul(0x7feb_352d);
x ^= x >> 15;
x = x.wrapping_mul(0x846c_a68b);
x ^= x >> 16;
x
}
fn to_unit(x: u32) -> f32 {
(x >> 8) as f32 * (1.0 / 16_777_216.0)
}
pub fn power_heuristic(n_f: f32, pdf_f: f32, n_g: f32, pdf_g: f32) -> f32 {
let f = n_f * pdf_f;
let g = n_g * pdf_g;
let denom = f * f + g * g;
if denom <= 0.0 {
0.0
} else {
(f * f) / denom
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sobol_prefixes_are_stratified_nets() {
for seed in [1u32, 0xdead_beef, 12345] {
let n = 64usize;
let points: Vec<(f32, f32)> = (0..n).map(|i| sobol02_owen(i as u32, seed)).collect();
for a in 0..=6u32 {
let (nx, ny) = (1usize << a, 1usize << (6 - a));
let mut cells = vec![0u32; nx * ny];
for &(x, y) in &points {
let cx = ((x * nx as f32) as usize).min(nx - 1);
let cy = ((y * ny as f32) as usize).min(ny - 1);
cells[cy * nx + cx] += 1;
}
assert!(
cells.iter().all(|&c| c == 1),
"seed {seed}, {nx}x{ny} grid: cells hold {:?}",
cells
.iter()
.copied()
.collect::<std::collections::BTreeSet<_>>()
);
}
}
}
#[test]
fn sobol_stays_in_the_unit_square() {
for seed in [0u32, 7, 0xffff_ffff] {
for i in 0..10_000u32 {
let (x, y) = sobol02_owen(i, seed);
assert!(
(0.0..1.0).contains(&x) && (0.0..1.0).contains(&y),
"{x},{y}"
);
}
}
}
#[test]
fn different_seeds_decorrelate() {
let a: Vec<(f32, f32)> = (0..64).map(|i| sobol02_owen(i, 11)).collect();
let b: Vec<(f32, f32)> = (0..64).map(|i| sobol02_owen(i, 12)).collect();
let matching = a
.iter()
.zip(&b)
.filter(|(p, q)| (p.0 - q.0).abs() < 1e-6)
.count();
assert!(matching < 8, "{matching} of 64 samples coincided");
}
#[test]
fn sobol_is_unbiased() {
let n = 1 << 14;
let mut sum = (0.0f64, 0.0f64);
for i in 0..n {
let (x, y) = sobol02_owen(i, 0x51633e2d);
sum.0 += x as f64;
sum.1 += y as f64;
}
let mean = (sum.0 / n as f64, sum.1 / n as f64);
assert!((mean.0 - 0.5).abs() < 1e-3, "x mean {}", mean.0);
assert!((mean.1 - 0.5).abs() < 1e-3, "y mean {}", mean.1);
}
#[test]
fn sobol_converges_faster_than_independent_sampling() {
let f = |x: f32, y: f32| {
(std::f32::consts::FRAC_PI_2 * x).cos() * (std::f32::consts::FRAC_PI_2 * y).cos()
};
let exact = (2.0 / PI as f64).powi(2);
let n = 256u32;
let trials = 64;
let mut sobol_err = 0.0f64;
let mut random_err = 0.0f64;
for t in 0..trials {
let mut a = 0.0f64;
for i in 0..n {
let (x, y) = sobol02_owen(i, 1000 + t);
a += f(x, y) as f64;
}
sobol_err += (a / n as f64 - exact).abs();
let mut rng = Rng::new(t as u64, 99);
let mut b = 0.0f64;
for _ in 0..n {
let (x, y) = (rng.next_f32(), rng.next_f32());
b += f(x, y) as f64;
}
random_err += (b / n as f64 - exact).abs();
}
sobol_err /= trials as f64;
random_err /= trials as f64;
assert!(
sobol_err * 5.0 < random_err,
"sobol mean error {sobol_err:.3e} vs independent {random_err:.3e} — \
not the improvement this is for"
);
}
#[test]
fn only_pixel_samplers_are_stratified() {
let mut plain = Rng::new(1, 2);
let a = plain.next_2d();
let b = plain.next_2d();
assert_ne!(a, b);
let p0 = Rng::for_sample(4, 9, 0, 77).next_2d();
let p1 = Rng::for_sample(4, 9, 1, 77).next_2d();
let q0 = Rng::for_sample(5, 9, 0, 77).next_2d();
assert_ne!(p0, p1);
assert_ne!(p0, q0);
}
#[test]
fn bounce_blocks_are_deterministic() {
let mut a = Rng::for_sample(3, 3, 7, 5);
let mut b = Rng::for_sample(3, 3, 7, 5);
a.set_bounce(2);
b.set_bounce(0);
let _ = b.next_2d();
let _ = b.next_2d();
let _ = b.next_2d();
b.set_bounce(2);
assert_eq!(a.next_2d(), b.next_2d());
}
#[test]
fn rng_stays_in_unit_interval() {
let mut rng = Rng::new(7, 11);
for _ in 0..100_000 {
let v = rng.next_f32();
assert!((0.0..1.0).contains(&v), "{v} out of range");
}
}
#[test]
fn rng_mean_is_close_to_half() {
let mut rng = Rng::new(1, 2);
let n = 200_000;
let mean: f64 = (0..n).map(|_| rng.next_f32() as f64).sum::<f64>() / n as f64;
assert!((mean - 0.5).abs() < 5e-3, "mean {mean}");
}
#[test]
fn distinct_pixels_decorrelate() {
let a = Rng::for_sample(10, 20, 0, 99).next_f32();
let b = Rng::for_sample(11, 20, 0, 99).next_f32();
assert!((a - b).abs() > 1e-6, "adjacent pixels produced {a} and {b}");
}
#[test]
fn onb_is_orthonormal_at_the_poles() {
for n in [
Vector3::new(0.0, 0.0, 1.0),
Vector3::new(0.0, 0.0, -1.0),
Vector3::new(0.3, -0.5, 0.81).normalize(),
] {
let onb = Onb::new(n);
assert!(onb.t.dot(onb.b).abs() < 1e-5);
assert!(onb.t.dot(onb.n).abs() < 1e-5);
assert!(onb.b.dot(onb.n).abs() < 1e-5);
assert!((onb.t.length() - 1.0).abs() < 1e-5);
assert!((onb.b.length() - 1.0).abs() < 1e-5);
let v = Vector3::new(0.2, 0.7, -0.4);
let rt = onb.to_local(onb.to_world(v));
assert!((rt - v).length() < 1e-5);
}
}
#[test]
fn cosine_hemisphere_pdf_integrates_to_one() {
let mut rng = Rng::new(3, 4);
let n = 200_000;
let mut sum = 0.0f64;
for _ in 0..n {
let (u1, u2) = rng.next_2d();
let d = uniform_sphere(u1, u2);
if d.z <= 0.0 {
continue;
}
sum += (cosine_hemisphere_pdf(d.z) * 4.0 * PI) as f64;
}
let integral = sum / n as f64;
assert!((integral - 1.0).abs() < 0.02, "integral {integral}");
}
#[test]
fn cone_pdf_matches_its_solid_angle() {
let cos_max = 0.99f32;
let mut rng = Rng::new(5, 6);
let pdf = uniform_cone_pdf(cos_max);
for _ in 0..1000 {
let (u1, u2) = rng.next_2d();
let d = uniform_cone(u1, u2, cos_max);
assert!(d.z >= cos_max - 1e-4, "sample escaped the cone: {}", d.z);
assert!((d.length() - 1.0).abs() < 1e-4);
}
assert!((pdf * 2.0 * PI * (1.0 - cos_max) - 1.0).abs() < 1e-4);
}
#[test]
fn power_heuristic_weights_sum_to_one() {
let (a, b) = (0.3f32, 1.7f32);
let wa = power_heuristic(1.0, a, 1.0, b);
let wb = power_heuristic(1.0, b, 1.0, a);
assert!((wa + wb - 1.0).abs() < 1e-6);
}
#[test]
fn vndf_samples_face_the_normal() {
let mut rng = Rng::new(9, 10);
for _ in 0..2000 {
let (u1, u2) = rng.next_2d();
let v = Vector3::new(0.6, 0.1, 0.79).normalize();
let h = sample_ggx_vndf(v, 0.25, 0.25, u1, u2);
assert!(h.z > 0.0, "half vector below the surface: {h:?}");
assert!((h.length() - 1.0).abs() < 1e-4);
}
}
}