From 73781f93ff4fe52082ebe08fb85b3e041d36161d Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Wed, 2 Dec 2026 10:22:21 +0530 Subject: [PATCH] Vignette: separate the CPU distance into column and row terms (#4950) Each pixel did three powf. The column term is now a table of image width and the row term is computed once per row, which leaves one powf per pixel only inside the transition band, and none for the default power 2 (sqrt). Same output up to float rounding. --- crates/pipeline/src/finish.rs | 90 +++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rs index bad24796f0..d6980fa1c4 100644 --- a/crates/pipeline/src/finish.rs +++ b/crates/pipeline/src/finish.rs @@ -62,6 +62,51 @@ pub struct Vig { pub style: VignetteStyle, } +/// A vignette's falloff over the summed per-axis terms, with the band edges raised to the power once. +#[derive(Clone, Copy, Debug)] +struct VigFalloff { + power: f32, + kx: f32, + ky: f32, + start: f32, + end: f32, + s0: f32, + s1: f32, +} + +impl VigFalloff { + fn new(v: &Vig, aspect: f32) -> Self { + let sx = 1.0 + (aspect - 1.0) * v.aspect_mix; + let sy = 1.0 + (1.0 / aspect - 1.0) * v.aspect_mix; + let m = sx.max(sy); + let end = v.start + v.width; + Self { power: v.power, kx: sx.max(1.0) / m, ky: sy.max(1.0) / m, start: v.start, end, s0: term(v.start, v.power), s1: term(end, v.power) } + } + + /// One axis's share of the distance for a normalised coordinate `c` in -1..1 (`horizontal` picks the axis). + fn axis(&self, c: f32, horizontal: bool) -> f32 { + term((c * if horizontal { self.kx } else { self.ky }).abs(), self.power) + } + + /// The vignette strength 0..1 from the summed axis terms. + fn falloff(&self, s: f32) -> f32 { + if s <= self.s0 { + 0.0 + } else if s >= self.s1 { + 1.0 + } else { + let dist = if self.power == 2.0 { s.sqrt() } else { s.powf(1.0 / self.power) }; + smooth(self.start, self.end, dist) + } + } +} + +/// `a` raised to the vignette power, for `a >= 0`. +#[inline] +fn term(a: f32, p: f32) -> f32 { + if p == 2.0 { a * a } else { a.powf(p) } +} + fn vignette(s: &DevelopSettings) -> Option { let v = &s.vignette; (v.amount != 0.0).then(|| { @@ -526,7 +571,12 @@ pub(crate) fn finish_with( let peak = fp.peak; let hdr = peak > 1.0; let mut out = vec![T::default(); w * h]; + // the vignette splits into a column term and a row term, so each is computed once + let vig_fall = vig.as_ref().map(|v| VigFalloff::new(v, aspect)); + let vig_cols: Vec = + vig_fall.as_ref().map_or_else(Vec::new, |f| (0..w).map(|x| f.axis((vx + x as f32 + 0.5) / vw * 2.0 - 1.0, true)).collect()); for_rows(&mut out, w, |y, row| { + let vig_row = vig_fall.as_ref().map_or(0.0, |f| f.axis((vy + y as f32 + 0.5) / vh * 2.0 - 1.0, false)); // the crystal grain of the whole row at once when its pixels are axis-aligned let crystal_row = crystal_row(fp, y, row.len()); for (x, px) in row.iter_mut().enumerate() { @@ -578,14 +628,8 @@ pub(crate) fn finish_with( } // --- vignette (display linear, post-crop) - if let Some(v) = vig { - let u = (vx + x as f32 + 0.5) / vw * 2.0 - 1.0; - let vv = (vy + y as f32 + 0.5) / vh * 2.0 - 1.0; - let sx = 1.0 + (aspect - 1.0) * v.aspect_mix; - let sy = 1.0 + (1.0 / aspect - 1.0) * v.aspect_mix; - let (ax, ay) = ((u * sx.max(1.0) / sx.max(sy)).abs(), (vv * sy.max(1.0) / sx.max(sy)).abs()); - let dist = (ax.powf(v.power) + ay.powf(v.power)).powf(1.0 / v.power); - let t = smooth(v.start, v.start + v.width, dist); + if let (Some(v), Some(f)) = (vig, &vig_fall) { + let t = f.falloff(vig_cols.get(x).copied().unwrap_or(0.0) + vig_row); if t > 0.0 { let lum = luminance_2020(d).clamp(0.0, 1.0); if v.amount < 0.0 { @@ -935,6 +979,36 @@ mod tests { Process::ALL.into_iter().filter(|p| *p < Process::V13) } + #[test] + fn separable_vignette_matches_the_direct_formula() { + let (vx, vy, vw, vh) = (0.0f32, 0.0f32, 61.0f32, 41.0f32); + let (mut zero, mut mid, mut one) = (false, false, false); + let vigs = [(2.0, 1.0, 0.25, 0.6), (2.0, 0.5, 0.3, 0.8), (5.0, 1.0, 0.3, 0.5), (5.0, 0.0, 0.4, 0.7)]; + for aspect in [1.5f32, 0.6] { + for (power, aspect_mix, start, width) in vigs { + let v = Vig { amount: -0.5, start, width, aspect_mix, power, highlights: 0.0, style: VignetteStyle::default() }; + let f = VigFalloff::new(&v, aspect); + for y in 0..vh as usize { + for x in 0..vw as usize { + let u = (vx + x as f32 + 0.5) / vw * 2.0 - 1.0; + let vv = (vy + y as f32 + 0.5) / vh * 2.0 - 1.0; + let sx = 1.0 + (aspect - 1.0) * v.aspect_mix; + let sy = 1.0 + (1.0 / aspect - 1.0) * v.aspect_mix; + let (ax, ay) = ((u * sx.max(1.0) / sx.max(sy)).abs(), (vv * sy.max(1.0) / sx.max(sy)).abs()); + let dist = (ax.powf(v.power) + ay.powf(v.power)).powf(1.0 / v.power); + let t_ref = smooth(v.start, v.start + v.width, dist); + let t = f.falloff(f.axis(u, true) + f.axis(vv, false)); + assert!((t - t_ref).abs() < 1e-4, "{power} {aspect} at {x},{y}: {t} vs {t_ref}"); + zero |= t == 0.0; + one |= t == 1.0; + mid |= t > 0.0 && t < 1.0; + } + } + } + } + assert!(zero && mid && one, "the grid covers the whole falloff"); + } + #[test] fn tone_sliders_ease_off_from_v13_only() { for v in [-100.0, -52.0, 0.0, 13.0, 100.0, 250.0] {