use crate::math::Vector3;
#[derive(Debug, Clone, Copy)]
pub struct DenoiseParams {
pub iterations: u32,
pub sigma_color: f32,
pub sigma_normal: f32,
pub sigma_depth: f32,
pub sigma_albedo: f32,
}
impl Default for DenoiseParams {
fn default() -> Self {
Self {
iterations: 5,
sigma_color: 0.917,
sigma_normal: 0.566,
sigma_depth: 0.003,
sigma_albedo: 0.252,
}
}
}
const CONVERGED_ERROR: f32 = 0.01;
const KERNEL: [f32; 5] = [1.0 / 16.0, 4.0 / 16.0, 6.0 / 16.0, 4.0 / 16.0, 1.0 / 16.0];
#[derive(Debug, Clone, Copy)]
pub struct DenoiseGuides<'a> {
pub albedo: &'a [[f32; 3]],
pub normal: &'a [[f32; 3]],
pub depth: &'a [f32],
pub variance: &'a [f32],
pub scene_scale: f32,
}
pub fn denoise(
width: u32,
height: u32,
color: &[f32],
guides: &DenoiseGuides<'_>,
params: &DenoiseParams,
) -> Vec<f32> {
let (albedo, normal, depth) = (guides.albedo, guides.normal, guides.depth);
let variance = guides.variance;
let scene_scale = guides.scene_scale;
let (w, h) = (width as usize, height as usize);
let n = w * h;
if n == 0 || color.len() < n * 4 {
return color.to_vec();
}
let mut src: Vec<[f32; 3]> = (0..n)
.map(|i| [color[i * 4], color[i * 4 + 1], color[i * 4 + 2]])
.collect();
let mut dst = src.clone();
let depth_scale = (params.sigma_depth * scene_scale.max(1e-3)).max(1e-4);
for pass in 0..params.iterations {
let step = 1usize << pass;
let sigma_color = params.sigma_color;
for y in 0..h {
for x in 0..w {
let i = y * w + x;
let c_i = src[i];
let n_i = normal[i];
let a_i = albedo[i];
let d_i = depth[i];
let v_i = variance.get(i).copied().unwrap_or(f32::INFINITY);
let mut sum = [0.0f32; 3];
let mut weight_sum = 0.0f32;
for (ky, kv) in KERNEL.iter().enumerate() {
let sy = y as isize + (ky as isize - 2) * step as isize;
if sy < 0 || sy >= h as isize {
continue;
}
for (kx, ku) in KERNEL.iter().enumerate() {
let sx = x as isize + (kx as isize - 2) * step as isize;
if sx < 0 || sx >= w as isize {
continue;
}
let j = sy as usize * w + sx as usize;
let c_j = src[j];
let v_j = variance.get(j).copied().unwrap_or(f32::INFINITY);
let noise = v_i + v_j;
let w_c = if noise.is_finite() {
let dl = luminance(c_i) - luminance(c_j);
(-(dl * dl) / (sigma_color * sigma_color * noise).max(1e-9)).exp()
} else {
1.0
};
let dot =
(n_i[0] * normal[j][0] + n_i[1] * normal[j][1] + n_i[2] * normal[j][2])
.clamp(-1.0, 1.0);
let dn = (1.0 - dot).max(0.0);
let w_n =
(-dn / (params.sigma_normal * params.sigma_normal).max(1e-8)).exp();
let w_d = if d_i.is_finite() && depth[j].is_finite() {
let dd = (d_i - depth[j]).abs() / depth_scale;
(-dd * dd).exp()
} else if d_i.is_finite() == depth[j].is_finite() {
1.0 } else {
0.0 };
let da = sq_dist(a_i, albedo[j]);
let w_a =
(-da / (params.sigma_albedo * params.sigma_albedo).max(1e-8)).exp();
let weight = kv * ku * w_c * w_n * w_d * w_a;
if weight <= 0.0 {
continue;
}
sum[0] += c_j[0] * weight;
sum[1] += c_j[1] * weight;
sum[2] += c_j[2] * weight;
weight_sum += weight;
}
}
let filtered = if weight_sum > 1e-8 {
[
sum[0] / weight_sum,
sum[1] / weight_sum,
sum[2] / weight_sum,
]
} else {
c_i
};
let trust = if v_i.is_finite() {
let mean = luminance(c_i).abs().max(1e-3);
(v_i.sqrt() / mean / CONVERGED_ERROR).min(1.0)
} else {
1.0
};
dst[i] = [
c_i[0] + (filtered[0] - c_i[0]) * trust,
c_i[1] + (filtered[1] - c_i[1]) * trust,
c_i[2] + (filtered[2] - c_i[2]) * trust,
];
}
}
std::mem::swap(&mut src, &mut dst);
}
let mut out = Vec::with_capacity(n * 4);
for (i, c) in src.iter().enumerate() {
out.push(c[0]);
out.push(c[1]);
out.push(c[2]);
out.push(color[i * 4 + 3]);
}
out
}
pub struct DenoiseExample<'a> {
pub width: u32,
pub height: u32,
pub noisy: &'a [f32],
pub guides: DenoiseGuides<'a>,
pub reference: &'a [f32],
}
impl DenoiseExample<'_> {
pub fn relative_rms(&self, image: &[f32]) -> f32 {
let mut sum = 0.0f64;
let mut n = 0usize;
for (a, b) in image.chunks_exact(4).zip(self.reference.chunks_exact(4)) {
for k in 0..3 {
let d = (a[k] - b[k]) as f64;
let r = b[k] as f64;
sum += d * d / (r * r + 0.01);
n += 1;
}
}
if n == 0 {
return 0.0;
}
(sum / n as f64).sqrt() as f32
}
fn error_with(&self, params: &DenoiseParams) -> f32 {
let out = denoise(self.width, self.height, self.noisy, &self.guides, params);
self.relative_rms(&out)
}
}
impl DenoiseParams {
pub fn error_over(&self, examples: &[DenoiseExample<'_>]) -> f32 {
if examples.is_empty() {
return 0.0;
}
examples.iter().map(|e| e.error_with(self)).sum::<f32>() / examples.len() as f32
}
pub fn fit(examples: &[DenoiseExample<'_>], start: DenoiseParams) -> (DenoiseParams, f32) {
Self::fit_impl(examples, start, true)
}
pub fn fit_widths(
examples: &[DenoiseExample<'_>],
start: DenoiseParams,
) -> (DenoiseParams, f32) {
Self::fit_impl(examples, start, false)
}
fn fit_impl(
examples: &[DenoiseExample<'_>],
start: DenoiseParams,
search_iterations: bool,
) -> (DenoiseParams, f32) {
let mut best = start;
let mut best_error = best.error_over(examples);
if examples.is_empty() {
return (best, best_error);
}
let mut step = 2.0f32;
for _ in 0..12 {
let mut improved = false;
for iterations in 1..=6u32 {
if !search_iterations || iterations == best.iterations {
continue;
}
let mut trial = best;
trial.iterations = iterations;
let error = trial.error_over(examples);
if error < best_error {
best = trial;
best_error = error;
improved = true;
}
}
for axis in 0..4 {
for factor in [step, 1.0 / step] {
let mut trial = best;
let field = match axis {
0 => &mut trial.sigma_color,
1 => &mut trial.sigma_normal,
2 => &mut trial.sigma_depth,
_ => &mut trial.sigma_albedo,
};
*field = (*field * factor).clamp(1e-4, 1e3);
let error = trial.error_over(examples);
if error < best_error {
best = trial;
best_error = error;
improved = true;
}
}
}
if !improved {
step = step.sqrt();
if step < 1.02 {
break;
}
}
}
(best, best_error)
}
}
fn luminance(c: [f32; 3]) -> f32 {
0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
}
fn sq_dist(a: [f32; 3], b: [f32; 3]) -> f32 {
let d = Vector3::new(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
d.length_sq()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::raytrace::sampler::Rng;
type TestImage = (Vec<f32>, Vec<[f32; 3]>, Vec<[f32; 3]>, Vec<f32>, Vec<f32>);
fn split_image(w: usize, h: usize, noise: f32) -> TestImage {
let mut rng = Rng::new(1, 2);
let mut color = Vec::with_capacity(w * h * 4);
let mut albedo = Vec::with_capacity(w * h);
let mut normal = Vec::with_capacity(w * h);
let mut depth = Vec::with_capacity(w * h);
for y in 0..h {
for x in 0..w {
let left = x < w / 2;
let base = if left { 0.2 } else { 0.8 };
let n = (rng.next_f32() - 0.5) * 2.0 * noise;
color.extend_from_slice(&[base + n, base + n, base + n, 1.0]);
albedo.push([base, base, base]);
normal.push(if left {
[0.0, 0.0, 1.0]
} else {
[1.0, 0.0, 0.0]
});
depth.push(5.0);
let _ = y;
}
}
let variance = vec![noise * noise / 3.0; w * h];
(color, albedo, normal, depth, variance)
}
fn spatial_variance(v: &[f32], w: usize, h: usize, x0: usize, x1: usize) -> f32 {
let mut vals = Vec::new();
for y in 1..h - 1 {
for x in x0..x1 {
vals.push(v[(y * w + x) * 4]);
}
}
let mean: f32 = vals.iter().sum::<f32>() / vals.len() as f32;
vals.iter().map(|a| (a - mean) * (a - mean)).sum::<f32>() / vals.len() as f32
}
#[test]
fn noise_is_reduced_in_a_flat_region() {
let (w, h) = (48, 48);
let (color, albedo, normal, depth, variance) = split_image(w, h, 0.25);
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
scene_scale: 10.0,
},
&DenoiseParams::default(),
);
let before = spatial_variance(&color, w, h, 2, w / 2 - 2);
let after = spatial_variance(&out, w, h, 2, w / 2 - 2);
assert!(
after < before * 0.35,
"variance went {before} -> {after}; the filter is not working"
);
}
#[test]
fn the_edge_between_two_surfaces_survives() {
let (w, h) = (48, 48);
let (color, albedo, normal, depth, variance) = split_image(w, h, 0.1);
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
scene_scale: 10.0,
},
&DenoiseParams::default(),
);
let row = h / 2;
let left = out[(row * w + w / 2 - 1) * 4];
let right = out[(row * w + w / 2) * 4];
assert!(
right - left > 0.4,
"the 0.2/0.8 step was smoothed to {left} / {right}"
);
}
#[test]
fn alpha_passes_through_untouched() {
let (w, h) = (8, 8);
let mut color = vec![0.0f32; w * h * 4];
for (i, px) in color.chunks_exact_mut(4).enumerate() {
px[3] = (i % 2) as f32;
}
let albedo = vec![[0.0f32; 3]; w * h];
let normal = vec![[0.0f32, 0.0, 1.0]; w * h];
let depth = vec![1.0f32; w * h];
let variance = vec![0.02f32; w * h];
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
scene_scale: 1.0,
},
&DenoiseParams::default(),
);
for i in 0..w * h {
assert_eq!(out[i * 4 + 3], (i % 2) as f32);
}
}
#[test]
fn background_and_foreground_do_not_bleed_into_each_other() {
let (w, h) = (16, 16);
let mut color = vec![0.0f32; w * h * 4];
let albedo = vec![[0.5f32; 3]; w * h];
let normal = vec![[0.0f32, 0.0, 1.0]; w * h];
let mut depth = vec![f32::INFINITY; w * h];
let variance = vec![0.02f32; w * h];
for y in 0..h {
for x in 0..w {
let i = y * w + x;
if x < w / 2 {
depth[i] = 3.0;
color[i * 4] = 1.0;
}
color[i * 4 + 3] = 1.0;
}
}
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
scene_scale: 10.0,
},
&DenoiseParams::default(),
);
let i = (h / 2 * w + w / 2) * 4;
assert!(
out[i] < 0.05,
"background picked up {} from the object",
out[i]
);
}
#[test]
fn zero_iterations_is_a_no_op() {
let (w, h) = (8, 8);
let (color, albedo, normal, depth, variance) = split_image(w, h, 0.2);
let params = DenoiseParams {
iterations: 0,
..Default::default()
};
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
scene_scale: 1.0,
},
¶ms,
);
assert_eq!(out, color);
}
#[test]
fn an_empty_image_is_handled() {
let out = denoise(
0,
0,
&[],
&DenoiseGuides {
albedo: &[],
normal: &[],
depth: &[],
variance: &[],
scene_scale: 1.0,
},
&DenoiseParams::default(),
);
assert!(out.is_empty());
}
}