use super::*;
#[must_use]
pub fn svgf_edge_stopping_weight(
normals: [Vec3; 2],
depths: [f32; 2],
luminances: [f32; 2],
depth_scale: f32,
luma_sigma: f32,
config: SvgfConfig,
) -> f32 {
let n_dot: f32 = normals[0].dot(normals[1]).clamp(0.0, 1.0);
let w_normal: f32 = n_dot.powf(config.get_phi_normal().max(0.0));
if w_normal <= 1e-6 {
return 0.0;
}
let depth_diff: f32 = (depths[0] - depths[1]).abs();
let depth_den: f32 = config.get_phi_depth().max(1e-4) * depth_scale.max(0.01) + SVGF_EPSILON;
let w_depth: f32 = (-depth_diff / depth_den).exp();
let luma_diff: f32 = (luminances[0] - luminances[1]).abs();
let luma_den: f32 = config.get_phi_luma().max(1e-4) * luma_sigma.max(1e-3) + SVGF_EPSILON;
let w_luma: f32 = (-luma_diff / luma_den).exp();
(w_normal * w_depth * w_luma).clamp(0.0, 1.0)
}
#[must_use]
pub fn compute_image_luminance_variance(pixels: &[Vec3]) -> f32 {
if pixels.is_empty() {
return 0.0;
}
let inv_n: f32 = 1.0 / (pixels.len() as f32);
let mut sum_l: f32 = 0.0;
let mut sum_l2: f32 = 0.0;
for &px in pixels {
let l: f32 = rgb_luminance(px);
sum_l += l;
sum_l2 += l * l;
}
let mean: f32 = sum_l * inv_n;
(sum_l2 * inv_n - mean * mean).max(0.0)
}
#[must_use]
pub(crate) fn filter_variance_3x3(
variance: &[f32],
hit_mask: &[u8],
dims: [usize; 2],
coord: [usize; 2],
) -> f32 {
let w: isize = dims[0] as isize;
let h: isize = dims[1] as isize;
let cx: isize = coord[0] as isize;
let cy: isize = coord[1] as isize;
let center_idx: usize = coord[1] * dims[0] + coord[0];
let center_hit: u8 = hit_mask[center_idx];
let mut sum: f32 = 0.0;
let mut w_sum: f32 = 0.0;
for dy in -1_isize..=1_isize {
let ny: isize = cy + dy;
if ny < 0 || ny >= h {
continue;
}
for dx in -1_isize..=1_isize {
let nx: isize = cx + dx;
if nx < 0 || nx >= w {
continue;
}
let n_idx: usize = (ny as usize) * dims[0] + (nx as usize);
if hit_mask[n_idx] != center_hit {
continue;
}
let k_idx: usize = (dx.unsigned_abs() + dy.unsigned_abs()).min(2);
let k_w: f32 = SVGF_VARIANCE_KERNEL_3X3[k_idx];
sum += variance[n_idx].max(0.0) * k_w;
w_sum += k_w;
}
}
if w_sum > SVGF_EPSILON {
(sum / w_sum).max(0.0)
} else {
variance[center_idx].max(0.0)
}
}
#[must_use]
pub(crate) fn estimate_depth_gradient(
depths: &[f32],
hit_mask: &[u8],
dims: [usize; 2],
coord: [usize; 2],
) -> f32 {
let w: usize = dims[0];
let h: usize = dims[1];
let px: usize = coord[0];
let py: usize = coord[1];
let idx: usize = py * w + px;
let z: f32 = depths[idx];
let mask: u8 = hit_mask[idx];
let mut gx: f32 = 0.0;
let mut has_gx: bool = false;
if px > 0 && hit_mask[idx - 1] == mask {
gx = (z - depths[idx - 1]).abs();
has_gx = true;
}
if px + 1 < w && hit_mask[idx + 1] == mask {
let right_diff: f32 = (depths[idx + 1] - z).abs();
gx = if has_gx {
gx.min(right_diff)
} else {
right_diff
};
}
let mut gy: f32 = 0.0;
let mut has_gy: bool = false;
if py > 0 && hit_mask[idx - w] == mask {
gy = (z - depths[idx - w]).abs();
has_gy = true;
}
if py + 1 < h && hit_mask[idx + w] == mask {
let down_diff: f32 = (depths[idx + w] - z).abs();
gy = if has_gy { gy.min(down_diff) } else { down_diff };
}
(gx + gy).max(0.015)
}