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,
pub min_samples: u32,
}
impl Default for DenoiseParams {
fn default() -> Self {
Self {
iterations: 5,
sigma_color: 1.297,
sigma_normal: 0.566,
sigma_depth: 0.0007,
sigma_albedo: 0.252,
min_samples: 4,
}
}
}
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 sample_counts: Option<&'a [u32]>,
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 sample_counts = guides.sample_counts;
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);
let min_samples = params.min_samples.max(2);
for pass in 0..params.iterations {
let step = 1usize << pass;
let sigma_color = params.sigma_color;
filter_pass(
w,
h,
step,
sigma_color,
params,
depth_scale,
min_samples,
&src,
&mut dst,
albedo,
normal,
depth,
variance,
sample_counts,
);
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
}
#[allow(clippy::too_many_arguments)]
fn filter_pass(
w: usize,
h: usize,
step: usize,
sigma_color: f32,
params: &DenoiseParams,
depth_scale: f32,
min_samples: u32,
src: &[[f32; 3]],
dst: &mut [[f32; 3]],
albedo: &[[f32; 3]],
normal: &[[f32; 3]],
depth: &[f32],
variance: &[f32],
sample_counts: Option<&[u32]>,
) {
#[cfg(all(feature = "parallel", not(target_arch = "wasm32")))]
{
use rayon::prelude::*;
dst.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
for (x, out) in row.iter_mut().enumerate() {
*out = filter_pixel(
x,
y,
w,
h,
step,
sigma_color,
params,
depth_scale,
min_samples,
src,
albedo,
normal,
depth,
variance,
sample_counts,
);
}
});
}
#[cfg(not(all(feature = "parallel", not(target_arch = "wasm32"))))]
{
for y in 0..h {
for x in 0..w {
let i = y * w + x;
dst[i] = filter_pixel(
x,
y,
w,
h,
step,
sigma_color,
params,
depth_scale,
min_samples,
src,
albedo,
normal,
depth,
variance,
sample_counts,
);
}
}
}
}
#[allow(clippy::too_many_arguments)]
fn filter_pixel(
x: usize,
y: usize,
w: usize,
h: usize,
step: usize,
sigma_color: f32,
params: &DenoiseParams,
depth_scale: f32,
min_samples: u32,
src: &[[f32; 3]],
albedo: &[[f32; 3]],
normal: &[[f32; 3]],
depth: &[f32],
variance: &[f32],
sample_counts: Option<&[u32]>,
) -> [f32; 3] {
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 mean_l = (luminance(c_i).abs() + luminance(c_j).abs()) * 0.5;
let w_c = if v_i.is_finite() && v_j.is_finite() {
let scale = (1.0 + mean_l).max(1e-3);
let li = (1.0 + luminance(c_i).max(0.0)).ln();
let lj = (1.0 + luminance(c_j).max(0.0)).ln();
let dl_log = li - lj;
let dl_lin = (luminance(c_i) - luminance(c_j)).abs() / scale;
let dl = dl_log.abs().min(dl_lin);
let noise_lin = (v_i + v_j).max((mean_l * 0.01).powi(2).max(1e-12));
let noise = noise_lin / (scale * scale);
(-(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 mut 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
};
if let Some(counts) = sample_counts {
let n = counts.get(i).copied().unwrap_or(0);
if n < min_samples {
let boost = 1.0 - (n as f32 / min_samples as f32);
trust = trust.max(boost);
}
}
blend_denoised(c_i, filtered, trust)
}
fn blend_denoised(c_i: [f32; 3], filtered: [f32; 3], trust: f32) -> [f32; 3] {
if trust <= 0.0 {
return c_i;
}
if trust >= 1.0 - 1e-6 {
return filtered;
}
let li = luminance(c_i).max(0.0);
let lf = luminance(filtered).max(0.0);
if li <= 1e-8 && lf <= 1e-8 {
return [
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,
];
}
let log_lo = (1.0 + li).ln() + trust * ((1.0 + lf).ln() - (1.0 + li).ln());
let lo = log_lo.exp() - 1.0;
let norm_i = chroma_dir(c_i, li);
let norm_f = chroma_dir(filtered, lf);
let norm = [
norm_i[0] + trust * (norm_f[0] - norm_i[0]),
norm_i[1] + trust * (norm_f[1] - norm_i[1]),
norm_i[2] + trust * (norm_f[2] - norm_i[2]),
];
let nl = luminance(norm).max(1e-8);
let scale = lo / nl;
[norm[0] * scale, norm[1] * scale, norm[2] * scale]
}
fn chroma_dir(c: [f32; 3], l: f32) -> [f32; 3] {
if l > 1e-8 {
[c[0] / l, c[1] / l, c[2] / l]
} else {
[1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0]
}
}
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
.as_chunks::<4>()
.0
.iter()
.zip(self.reference.as_chunks::<4>().0)
{
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,
sample_counts: None,
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,
sample_counts: None,
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.as_chunks_mut::<4>().0.iter_mut().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,
sample_counts: None,
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,
sample_counts: None,
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 blend_denoised_preserves_chroma_at_hdr() {
let c = [1000.0, 1000.0, 1000.0];
let f = [1000.0, 1100.0, 1000.0];
let out = blend_denoised(c, f, 0.5);
let linear_g = c[1] + 0.5 * (f[1] - c[1]);
assert!(
(out[0] - out[1]).abs() < (c[0] - linear_g).abs(),
"chroma drift {:?} vs linear green {linear_g}",
out
);
let neutral = blend_denoised(c, [1000.0, 1000.0, 1000.0], 1.0);
assert!((neutral[0] - neutral[1]).abs() < 1e-3);
}
#[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,
sample_counts: None,
scene_scale: 1.0,
},
¶ms,
);
assert_eq!(out, color);
}
#[test]
fn low_sample_count_filters_harder() {
let (w, h) = (16, 16);
let (color, albedo, normal, depth, mut variance) = split_image(w, h, 0.4);
variance[w / 2] = 1e-8;
let counts = vec![1u32; w * h];
let params = DenoiseParams {
min_samples: 8,
..Default::default()
};
let out = denoise(
w as u32,
h as u32,
&color,
&DenoiseGuides {
albedo: &albedo,
normal: &normal,
depth: &depth,
variance: &variance,
sample_counts: Some(&counts),
scene_scale: 10.0,
},
¶ms,
);
let idx = (h / 2 * w + w / 2) * 4;
let center = out[idx];
let noisy = color[idx];
assert!(
(center - noisy).abs() > 0.05,
"low-sample pixel should have been filtered: {noisy} -> {center}"
);
}
#[test]
fn an_empty_image_is_handled() {
let out = denoise(
0,
0,
&[],
&DenoiseGuides {
albedo: &[],
normal: &[],
depth: &[],
variance: &[],
sample_counts: None,
scene_scale: 1.0,
},
&DenoiseParams::default(),
);
assert!(out.is_empty());
}
}