diff --git a/crates/develop/src/settings.rs b/crates/develop/src/settings.rs index 9c2af1740c..c4197c2911 100644 --- a/crates/develop/src/settings.rs +++ b/crates/develop/src/settings.rs @@ -93,12 +93,15 @@ pub enum Process { /// V4, with a raw's tone curve rolling off into white in stops past its last fitted knot, so /// highlights the sensor recorded keep their detail when Exposure lifts them (issue #1632). V5, + /// V5, with colour noise reduction on the linear colour differences R−Y and B−Y, leaving Y alone, with + /// edge-aware weights from a denoised guide and a brake for isolated colour specks (issue #815). + V6, } impl Process { /// Every process, oldest first. - pub const ALL: [Process; 5] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5]; - pub const LATEST: Process = Process::V5; + pub const ALL: [Process; 6] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5, Process::V6]; + pub const LATEST: Process = Process::V6; pub const fn version(self) -> ProcessVersion { match self { @@ -107,6 +110,7 @@ impl Process { Process::V3 => ProcessVersion(3), Process::V4 => ProcessVersion(4), Process::V5 => ProcessVersion(5), + Process::V6 => ProcessVersion(6), } } } diff --git a/crates/gpu/src/ctx.rs b/crates/gpu/src/ctx.rs index 290b95ac99..dee6205a36 100644 --- a/crates/gpu/src/ctx.rs +++ b/crates/gpu/src/ctx.rs @@ -64,6 +64,23 @@ const MODULES: &[Module] = &[ "fringe_apply", ], }, + Module { + src: include_str!("wgsl/nrcj.wgsl"), + bindings: &[("a", false, "f32"), ("b", false, "f32"), ("dst", true, "f32")], + entries: &[ + "nrcj_yuv", + "nrcj_uv", + "nrcj_samples", + "nrcj_speck", + "nrcj_pilot", + "nrcj_links1", + "nrcj_paths", + "nrcj_links", + "nrcj_dh", + "nrcj_dv", + "nrcj_recon", + ], + }, Module { src: include_str!("wgsl/spot.wgsl"), bindings: &[("img", true, "f32"), ("pts", false, "f32"), ("al", true, "f32"), ("sp", true, "f32"), ("df", true, "f32"), ("wt", true, "f32")], @@ -294,7 +311,8 @@ pub(crate) struct Tracked(wgpu::Buffer); impl Tracked { fn new(b: wgpu::Buffer) -> Tracked { - ALLOCATED.fetch_add(b.size(), Ordering::Relaxed); + let now = ALLOCATED.fetch_add(b.size(), Ordering::Relaxed) + b.size(); + PEAK.fetch_max(now, Ordering::Relaxed); Tracked(b) } } @@ -317,6 +335,15 @@ thread_local! { /// Bytes of device buffers that exist (in use, retired or pooled). static ALLOCATED: AtomicU64 = AtomicU64::new(0); +/// Most bytes allocated since [`take_peak`] (profiling). +static PEAK: AtomicU64 = AtomicU64::new(0); + +/// The most bytes allocated since the last call; starts over from what is allocated now. +pub(crate) fn take_peak() -> (u64, u64) { + let now = ALLOCATED.load(Ordering::Relaxed); + (PEAK.swap(now, Ordering::Relaxed), now) +} + /// Bytes waiting in per-thread retired lists. static RETIRED_BYTES: AtomicU64 = AtomicU64::new(0); /// The render device is lost: wgpu's `poll` panics from then on, so [`Gpu::poll`] stops calling it. diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 342fe039d0..faac8634d7 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -45,6 +45,8 @@ pub mod nn; mod params; #[cfg(not(target_arch = "wasm32"))] mod render; +#[cfg(not(target_arch = "wasm32"))] +mod vram; #[cfg(not(target_arch = "wasm32"))] pub use render::{DisplayFrame, GpuStages, Output}; diff --git a/crates/gpu/src/render.rs b/crates/gpu/src/render.rs index 5a60e547ea..8e6afa3700 100644 --- a/crates/gpu/src/render.rs +++ b/crates/gpu/src/render.rs @@ -9,6 +9,7 @@ use lightcraft_geom::Orientation; use lightcraft_pipeline::finish::{FinishParams, FinishParamsInput, MASK_TERMS, mask_terms}; use lightcraft_pipeline::geometry::SampleMode; use lightcraft_pipeline::local::plane_at; +use lightcraft_pipeline::nrc_joint::JointNoise; use lightcraft_pipeline::{Plan, RenderRequest, Rendered, SourceInfo, local}; use lightcraft_raster::resample::Filter; use lightcraft_raster::{Histogram, Plane, Rgb32f, Rgba8}; @@ -33,6 +34,8 @@ struct Entry { geo: u64, sampled: Arc, lin: Option<(u64, Arc)>, + /// The joint colour NR's noise model of the whole frame, keyed by `Plan::noise_key`. + colour_noise: Option<(u64, JointNoise)>, planes: Planes, } @@ -645,13 +648,15 @@ pub fn render( fault: Option, display: bool, ) -> Option { + let _in_flight = InFlight::new(); let mut t = profiling().then(std::time::Instant::now); // Each stage is submitted on its own; under `LIGHTCRAFT_PROFILE` also waited for, so the // timings are real. let lap = |what: &str, t: &mut Option, cx: &mut Cx<'_>| match t { Some(t) => { cx.sync(); - eprintln!(" gpu {what}: {:.1} ms", t.elapsed().as_secs_f64() * 1e3); + let (peak, now) = crate::ctx::take_peak(); + eprintln!(" gpu {what}: {:.1} ms (device buffers: peak {} MB, now {} MB)", t.elapsed().as_secs_f64() * 1e3, peak >> 20, now >> 20); *t = std::time::Instant::now(); } None => cx.flush(), @@ -704,9 +709,15 @@ pub fn render( lap("sample", &mut t, &mut cx); // 2. white balance, defringe, spots, noise reduction + let mut colour_noise = cached.as_ref().and_then(|e| e.colour_noise).filter(|(k, _)| *k == plan.noise_key); let lin = match cached.as_ref().and_then(|e| e.lin.clone()).filter(|(k, _)| *k == plan.lin_key) { Some((_, b)) => b, - None => Arc::new(linear(&mut cx, &sampled, info, &plan, &mut host)), + None => { + if colour_noise.is_none() { + colour_noise = lightcraft_pipeline::colour_noise(src, info, &plan).map(|n| (plan.noise_key, n)); + } + Arc::new(linear(&mut cx, &sampled, info, &plan, &mut host, colour_noise.as_ref().map(|c| &c.1))) + } }; lap("wb/nr", &mut t, &mut cx); @@ -722,7 +733,7 @@ pub fn render( } lap("planes", &mut t, &mut cx); if let Some(c) = stages { - c.put(Entry { src: src.clone(), geo: plan.geo, sampled, lin: Some((plan.lin_key, lin.clone())), planes }); + c.put(Entry { src: src.clone(), geo: plan.geo, sampled, lin: Some((plan.lin_key, lin.clone())), colour_noise, planes }); } // 4. masks @@ -930,8 +941,8 @@ fn unwritten(data: &[[u8; 4]]) -> usize { data.chunks(4096).filter(|c| c.iter().fold(255u8, |a, p| a & p[3]) != 255).map(|c| c.iter().filter(|p| p[3] != 255).count()).sum() } -/// The white-balanced, retouched, denoised image. -fn linear(cx: &mut Cx<'_>, sampled: &Buf, info: &SourceInfo, plan: &Plan<'_>, host: &mut Host) -> Buf { +/// The white-balanced, retouched, denoised image (`colour_noise`: see `denoise`). +fn linear(cx: &mut Cx<'_>, sampled: &Buf, info: &SourceInfo, plan: &Plan<'_>, host: &mut Host, colour_noise: Option<&JointNoise>) -> Buf { let (w, h) = (plan.w, plan.h); let n = w * h; let s = &*plan.settings; @@ -963,7 +974,7 @@ fn linear(cx: &mut Cx<'_>, sampled: &Buf, info: &SourceInfo, plan: &Plan<'_>, ho eyed } }; - denoise(cx, img, plan) + denoise(cx, img, plan, colour_noise) } /// Defringe (`optics::defringe`) of the white-balanced image. @@ -1056,8 +1067,9 @@ fn histogram(cx: &mut Cx<'_>, out: &Buf, w: usize, region: (usize, usize, usize, Histogram { r: part(0), g: part(1), b: part(2), luma: part(3), total: v.get(4 * Histogram::BINS).copied().unwrap_or(0), hdr: None } } -/// Noise reduction (`local::denoise`). -fn denoise(cx: &mut Cx<'_>, img: Buf, plan: &Plan<'_>) -> Buf { +/// Noise reduction (`local::denoise`). `colour_noise`: the whole frame's noise model for the joint colour +/// NR (`lightcraft_pipeline::colour_noise`); `None`: measured on `img`. +fn denoise(cx: &mut Cx<'_>, img: Buf, plan: &Plan<'_>, colour_noise: Option<&JointNoise>) -> Buf { let (w, h) = (plan.w, plan.h); let n = w * h; let (lum, col) = local::nr_params(&plan.settings, plan.detail_scale); @@ -1070,6 +1082,20 @@ fn denoise(cx: &mut Cx<'_>, img: Buf, plan: &Plan<'_>) -> Buf { map(cx, "nr_lum", n, &[nr.k.to_bits()], [Some(&img), Some(&l), Some(&f)], &out); img = out; } + if col.is_some() && local::joint_colour_nr(&plan.settings) { + let Some(p) = local::nrc_params(&plan.settings, plan.detail_scale) else { return img }; + let s = &plan.settings.detail; + let (detail, amount) = ((s.nr_color_detail / 100.0) as f32, (s.nr_color / 100.0) as f32); + if let Some(out) = color_nr_joint(cx, &img, w, h, &p, detail, amount, colour_noise) { + return out; + } + // too small for the noise samples, or not even one band fits the device: this step on the CPU + let mut host = cx.read_rgb(&img, w, h); + let mut d = (*plan.settings).clone(); + d.detail.nr_luminance = 0.0; + local::denoise(&mut host, &d, plan.detail_scale, colour_noise); + return cx.gpu.upload(rgb_words(&host)); + } if let Some(nr) = col { let chroma = cx.gpu.buffer(n * 3); map(cx, "chroma_k", n, &[], [Some(&img), None, None], &chroma); @@ -1081,6 +1107,226 @@ fn denoise(cx: &mut Cx<'_>, img: Buf, plan: &Plan<'_>) -> Buf { img } +/// Run a `nrcj.wgsl` kernel on `n` items with inputs `a`, `b` and output `out`. +fn nrcj(cx: &mut Cx<'_>, k: &str, n: usize, p: &[u32], a: &Buf, b: Option<&Buf>, out: &Buf) { + cx.run(k, p, &[Some(a), b, Some(out)], groups1(n)); +} + +/// Device memory left aside when the joint NR plans its bands from the free memory (the rest of the +/// render, the display, other programs). +const VRAM_RESERVE: u64 = 384 << 20; + +/// Renders on the device right now ([`render`]). An export batch renders photos side by side: while one +/// plans its noise reduction bands, each of the others may still allocate the buffers of its other stages. +static RENDERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Held while a noise reduction plans its bands and runs them: two plans made at once from the same free +/// memory would together ask for more than there is. +static BANDS: Mutex<()> = Mutex::new(()); + +/// Counts a render in [`RENDERS`] while it lives. +struct InFlight; + +impl InFlight { + fn new() -> Self { + RENDERS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + InFlight + } +} + +impl Drop for InFlight { + fn drop(&mut self) { + RENDERS.fetch_sub(1, std::sync::atomic::Ordering::Relaxed); + } +} + +/// The band budget from the device memory free right now and our own pooled buffers (which we reuse), less +/// [`VRAM_RESERVE`] for each render on the device (this one's other stages and every other render's). +fn band_budget_from(free: u64, pooled: u64, renders: usize) -> usize { + let reserve = VRAM_RESERVE.saturating_mul(renders.max(1) as u64); + free.saturating_add(pooled).saturating_sub(reserve) as usize +} + +/// The joint colour noise reduction (`lightcraft_pipeline::nrc_joint`) on the GPU, the same steps as the +/// CPU: only the noise samples are read back (the noise model is fitted on the CPU, by the same code). +/// Large images are filtered in horizontal bands whose halo covers every step's reach, so each kept pixel +/// gets the whole-image result; the noise model is the whole image's, or `frame_noise` (the whole frame's, +/// for a window). `None` when the image is too small for the noise samples (the caller falls back to the CPU). +#[allow(clippy::too_many_arguments)] +fn color_nr_joint( + cx: &mut Cx<'_>, + img: &Buf, + w: usize, + h: usize, + p: &local::NrcParams, + detail: f32, + amount: f32, + frame_noise: Option<&JointNoise>, +) -> Option { + use lightcraft_pipeline::nrc_joint; + let strength = nrc_joint::strength(amount); + let brake = nrc_joint::speck_brake(amount); + if w < 3 || h < 3 { + return None; + } + if strength <= 0.0 { + return Some(cx.copy(img)); + } + let n = w * h; + let started = profiling().then(std::time::Instant::now); + let mut noise = match frame_noise { + Some(noise) => *noise, + None => { + // noise samples of the whole image: same pixels as the CPU, fitted by the same code + let stride = nrc_joint::noise_sample_stride(n); + let count = n.div_ceil(stride); + let smp = cx.gpu.buffer(11 * count); + nrcj(cx, "nrcj_samples", count, &[count as u32, w as u32, h as u32, stride as u32], img, None, &smp); + let raw: Vec = cx.read(&smp, 11 * count); + drop(smp); + if let Some(t) = started { + eprintln!(" gpu joint samples + read: {:.1} ms", t.elapsed().as_secs_f64() * 1e3); + } + let samples: Vec = raw + .as_chunks::<11>() + .0 + .iter() + .filter(|s| s[0] == 1.0 && s.iter().all(|v| v.is_finite())) + .map(|s| (s[1], [[s[2], s[3], s[4]], [s[5], s[6], s[7]], [s[8], s[9], s[10]]])) + .collect(); + nrc_joint::noise_from_samples(samples) + } + }; + noise.tolerance = nrc_joint::tolerance(detail); + if let Some(t) = started { + eprintln!(" gpu joint noise model: {:.1} ms", t.elapsed().as_secs_f64() * 1e3); + } + let sigma = p.passes.last().map_or(0.0, |q| q.sigma); + let plan = nrc_joint::scale_plan(sigma, w, h); + // Rows a kept pixel depends on: a wrong value at a band edge (treated as an image border) moves + // inwards by delta per vertical sweep, two sweeps per scale; at the farthest row it reaches, a + // scale's weight and path still read up to delta rows further (at most the largest delta); the + // guide's blur and pilot reach a few rows, the speck brake two more. Hence 2·Σdelta + max delta + 10. + let halo = plan.iter().map(|d| 2 * d.0).sum::() + plan.last().map_or(0, |d| d.0) + 10; + // Memory: the whole output (3 values per pixel) is held besides the input; a band then holds about + // 12 values per pixel (guide 3, two chroma buffers 2 + 2, paths 2, weights 2; up to 12 while the + // speck brake and the guide run). The budget for both together is `band_budget_from` the device memory + // free right now (where the driver can be asked, `crate::vram`); else a fixed 2 GB. One band plan at a + // time (`BANDS`), held until the bands are done. PROTOTYPE: `NRV2_GPU_BAND_MB` overrides it. + // (free memory asked before the lock: the first question for a device waits for the driver) + let free = crate::vram::free_bytes(&cx.gpu.info); + let _bands = BANDS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let fixed = std::env::var("NRV2_GPU_BAND_MB").ok().and_then(|v| v.parse::().ok()).map(|mb| mb << 20); + let renders = RENDERS.load(std::sync::atomic::Ordering::Relaxed); + let measured = free.map(|free| band_budget_from(free, crate::ctx::memory().pooled, renders)); + let budget = fixed.or(measured).unwrap_or(2048 << 20); + if profiling() { + eprintln!( + " gpu joint budget: {} MB ({}, {renders} render(s) on the device)", + budget >> 20, + if fixed.is_some() { + "NRV2_GPU_BAND_MB" + } else if measured.is_some() { + "free device memory" + } else { + "fixed default" + } + ); + } + let out_bytes = 12 * n; + let per_row = 48 * w; + // no band may exceed the device's largest buffer either (3 values per pixel are the largest) + let limit_rows = (cx.gpu.limit() as usize / (12 * w)).max(1); + let rows = (budget.saturating_sub(out_bytes) / per_row).min(limit_rows); + let core = if rows >= h { + h + } else if rows > 2 * halo { + rows - 2 * halo + } else { + // not even one band with its halo fits: this stage runs on the CPU (the rest of the render stays here) + return None; + }; + let out = cx.gpu.buffer(3 * n); + let mut y0 = 0; + while y0 < h { + let y1 = (y0 + core).min(h); + let (b0, b1) = if core >= h { (0, h) } else { (y0.saturating_sub(halo), (y1 + halo).min(h)) }; + let filtered = joint_band(cx, img, w, b0, b1 - b0, &noise, brake, &plan); + let kept = (y1 - y0) * w; + nrcj(cx, "nrcj_recon", kept, &[kept as u32, strength.to_bits(), (y0 * w) as u32, (b0 * w) as u32], img, Some(&filtered), &out); + drop(filtered); + // wait: the driver frees a dropped buffer only once the GPU is done with it, and a band's + // buffers are large (without waiting, freed memory piled up to the whole 4 GB of a Quadro M2200) + cx.sync(); + y0 = y1; + } + Some(out) +} + +/// The filtered chroma pairs of rows `b0 .. b0 + bh` of the image, treated as an image of their own. +#[allow(clippy::too_many_arguments)] +fn joint_band( + cx: &mut Cx<'_>, + img: &Buf, + w: usize, + b0: usize, + bh: usize, + noise: &lightcraft_pipeline::nrc_joint::JointNoise, + brake: lightcraft_pipeline::nrc_joint::SpeckBrake, + plan: &[(usize, f32)], +) -> Buf { + let n = w * bh; + let mut np = vec![n as u32, w as u32, bh as u32]; + np.extend(noise.model.alpha.iter().chain(&noise.model.beta).chain(&noise.ymax).map(|v| v.to_bits())); + np.push(noise.tolerance.to_bits()); + let mut yuv = cx.gpu.buffer(3 * n); + nrcj(cx, "nrcj_yuv", n, &[n as u32, (b0 * w) as u32], img, None, &yuv); + if brake.share > 0.0 { + let mut sp = np.clone(); + sp.extend([brake.threshold.to_bits(), brake.share.to_bits()]); + let despecked = cx.gpu.buffer(3 * n); + nrcj(cx, "nrcj_speck", n, &sp, &yuv, None, &despecked); + yuv = despecked; + } + let blurred = gaussian(cx, &yuv, w, bh, 3, 0.8); + let guide = cx.gpu.buffer(3 * n); + nrcj(cx, "nrcj_pilot", n, &np, &yuv, Some(&blurred), &guide); + drop(blurred); + let mut filtered = cx.gpu.buffer(2 * n); + nrcj(cx, "nrcj_uv", n, &[n as u32], &yuv, None, &filtered); + drop(yuv); + // dropped buffers only become reusable (and their memory free) once the GPU is done with them + cx.sync(); + // paths (exp(−path), as on the CPU) and pair weights; the next scale's paths go to the scratch buffer, + // free after the diffusion steps, as on the CPU + let mut paths = cx.gpu.buffer(2 * n); + nrcj(cx, "nrcj_links1", n, &np, &guide, None, &paths); + let links = cx.gpu.buffer(2 * n); + let mut lp = np.clone(); + lp.push(1); + nrcj(cx, "nrcj_links", n, &lp, &guide, Some(&paths), &links); + let mut scratch = cx.gpu.buffer(2 * n); + for (k, &(delta, rate)) in plan.iter().enumerate() { + let dp = [n as u32, w as u32, bh as u32, delta as u32, rate.to_bits()]; + for _ in 0..2 { + nrcj(cx, "nrcj_dh", n, &dp, &filtered, Some(&links), &scratch); + std::mem::swap(&mut filtered, &mut scratch); + nrcj(cx, "nrcj_dv", n, &dp, &filtered, Some(&links), &scratch); + std::mem::swap(&mut filtered, &mut scratch); + } + if let Some(&(next, _)) = plan.get(k + 1) { + nrcj(cx, "nrcj_paths", n, &[n as u32, w as u32, bh as u32, delta as u32], &paths, None, &scratch); + std::mem::swap(&mut paths, &mut scratch); + let mut lp = np.clone(); + lp.push(next as u32); + nrcj(cx, "nrcj_links", n, &lp, &guide, Some(&paths), &links); + } + } + drop((scratch, paths, links, guide)); + cx.sync(); + filtered +} + /// The planes the per-pixel stage reads. struct Prep { log_l: Arc, @@ -1374,6 +1620,26 @@ fn masks(cx: &mut Cx<'_>, lin: &Buf, prep: &Prep, plan: &Plan<'_>, host: &mut Ho mod tests { use super::*; + /// Two exports side by side: each band plan leaves room for the other render, so the two together + /// stay within the free memory they both measured. + #[test] + fn band_budget_leaves_room_for_every_render_on_the_device() { + let (free, pooled) = (3000u64 << 20, 200u64 << 20); + let alone = band_budget_from(free, pooled, 1); + assert_eq!(alone, (3200usize << 20) - VRAM_RESERVE as usize); + assert_eq!(band_budget_from(free, pooled, 0), alone, "the render itself is always counted"); + let two = band_budget_from(free, pooled, 2); + assert_eq!(two, alone - VRAM_RESERVE as usize); + assert_eq!(band_budget_from(100 << 20, 0, 3), 0, "nothing left: no band fits, the CPU runs the step"); + let before = RENDERS.load(std::sync::atomic::Ordering::Relaxed); + { + let _a = InFlight::new(); + let _b = InFlight::new(); + assert!(RENDERS.load(std::sync::atomic::Ordering::Relaxed) >= before + 2); + } + assert!(RENDERS.load(std::sync::atomic::Ordering::Relaxed) < before + 2, "a finished render no longer counts"); + } + #[test] fn coverage_mask_matches_per_pixel_decision() { use lightcraft_develop::{EmbeddedLens, EmbeddedWarp}; diff --git a/crates/gpu/src/vram.rs b/crates/gpu/src/vram.rs new file mode 100644 index 0000000000..ae79478d22 --- /dev/null +++ b/crates/gpu/src/vram.rs @@ -0,0 +1,373 @@ +//! How much device memory is free right now, where that can be asked without `unsafe`: the NVIDIA +//! driver's `nvidia-smi` (Linux and Windows) and the amdgpu driver's sysfs counters (Linux). Other +//! devices (AMD/Intel on Windows needs DXGI; integrated GPUs share system memory) report `None`, and the +//! caller keeps its fixed budget. The answer is cached for a few seconds: renders ask often, and the +//! query starts a process on NVIDIA. Only the first question for an adapter waits for the query; later +//! ones get the last answer while a background thread asks again, so no render (and no lock it holds) +//! waits for `nvidia-smi`. A card is found by its PCI address where the backend reports it, else by its +//! device id; when that matches several cards (two of the same model), the answer is `None`. + +use std::sync::{Mutex, PoisonError}; +use std::time::{Duration, Instant}; + +/// How long an answer is fresh; an older one is still returned while a newer one is asked for. +const TTL: Duration = Duration::from_secs(3); +/// How long `nvidia-smi` may take before it is given up on. +const QUERY_TIMEOUT: Duration = Duration::from_secs(2); + +const NVIDIA: u32 = 0x10de; +const AMD: u32 = 0x1002; + +static CACHE: Cache = Cache::new(TTL); + +/// Free device memory of the adapter `info` describes, in bytes, or `None` when it cannot be asked. +pub fn free_bytes(info: &wgpu::AdapterInfo) -> Option { + let adapter = Adapter { vendor: info.vendor, device: info.device, bus: pci_address(&info.device_pci_bus_id) }; + CACHE.get(&adapter, query) +} + +/// Which card: vendor and device id, and the PCI address where the backend reports it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Adapter { + vendor: u32, + device: u32, + bus: Option, +} + +/// A PCI address: domain, bus, device, function. +type Pci = [u32; 4]; + +/// `0000:01:00.0` (Vulkan, sysfs) or `00000000:01:00.0` (`nvidia-smi`). +fn pci_address(s: &str) -> Option { + let mut parts = s.trim().split(':'); + let (domain, bus, rest) = (parts.next()?, parts.next()?, parts.next()?); + let (device, function) = rest.split_once('.')?; + if parts.next().is_some() { + return None; + } + let hex = |x: &str| u32::from_str_radix(x.trim(), 16).ok(); + Some([hex(domain)?, hex(bus)?, hex(device)?, hex(function)?]) +} + +/// One card a driver lists, with its free memory in bytes. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Card { + bus: Option, + device: u32, + free: u64, +} + +/// The free memory of `want` among the listed `cards`: the card at its PCI address when both sides +/// report one, else the only card with its device id, else the only card listed. `None` when that is +/// ambiguous or the address is listed by no card. +fn pick(cards: &[Card], want: &Adapter) -> Option { + if let Some(bus) = want.bus + && cards.iter().any(|c| c.bus.is_some()) + { + return cards.iter().find(|c| c.bus == Some(bus)).map(|c| c.free); + } + let mut same = cards.iter().filter(|c| c.device == want.device); + match (same.next(), same.next()) { + (Some(c), None) => Some(c.free), + (None, _) if cards.len() == 1 => cards.first().map(|c| c.free), + _ => None, + } +} + +/// The last answer for one adapter: when it was asked, the answer, and since when a newer one is +/// being asked for. +struct Answer { + adapter: Adapter, + at: Instant, + free: Option, + asking: Option, +} + +type Ask = fn(&Adapter) -> Option; + +/// Adapters remembered (a renderer, a face finder and a model may each use another device). +const ADAPTERS: usize = 4; + +/// A background query not done after this is taken as lost, and the next question asks again. +const LOST: Duration = Duration::from_secs(10); + +struct Cache { + ttl: Duration, + answers: Mutex>, +} + +impl Cache { + const fn new(ttl: Duration) -> Self { + Cache { ttl, answers: Mutex::new(Vec::new()) } + } + + /// The answer of `ask` for `adapter`: cached, refreshed in the background once it is older than the + /// TTL (in the caller where no thread can be started), and asked for directly the first time. + fn get(&'static self, adapter: &Adapter, ask: Ask) -> Option { + { + let mut answers = self.answers.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(a) = answers.iter_mut().find(|a| a.adapter == *adapter) { + let busy = a.asking.is_some_and(|t| t.elapsed() < LOST); + if a.at.elapsed() >= self.ttl && !busy { + let adapter = *adapter; + let refresh = std::thread::Builder::new().name("vram-query".into()).spawn(move || self.store(adapter, ask(&adapter))); + if refresh.is_err() { + drop(answers); + return self.ask_now(&adapter, ask); + } + a.asking = Some(Instant::now()); + } + return a.free; + } + } + self.ask_now(adapter, ask) + } + + fn ask_now(&self, adapter: &Adapter, ask: Ask) -> Option { + let free = ask(adapter); + self.store(*adapter, free); + free + } + + /// Keep `adapter`'s answer (each adapter has its own; the oldest one goes when the list is full). + fn store(&self, adapter: Adapter, free: Option) { + let mut answers = self.answers.lock().unwrap_or_else(PoisonError::into_inner); + answers.retain(|a| a.adapter != adapter); + if answers.len() >= ADAPTERS { + answers.remove(0); + } + answers.push(Answer { adapter, at: Instant::now(), free, asking: None }); + } + + /// Make `adapter`'s answer as old as the TTL (tests: no waiting, so no answer goes stale by itself + /// on a slow machine). `false` when there is no answer, or the clock does not reach that far back. + #[cfg(test)] + fn expire(&self, adapter: &Adapter) -> bool { + let mut answers = self.answers.lock().unwrap_or_else(PoisonError::into_inner); + let Some(a) = answers.iter_mut().find(|a| a.adapter == *adapter) else { return false }; + let Some(at) = a.at.checked_sub(self.ttl) else { return false }; + a.at = at; + true + } + + #[cfg(test)] + fn asking(&self, adapter: &Adapter) -> bool { + self.answers.lock().unwrap_or_else(PoisonError::into_inner).iter().any(|a| a.adapter == *adapter && a.asking.is_some()) + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn query(adapter: &Adapter) -> Option { + match adapter.vendor { + NVIDIA => nvidia(adapter), + AMD => amdgpu(adapter), + _ => None, + } +} + +/// `nvidia-smi --query-gpu=pci.bus_id,pci.device_id,memory.free`: one line per GPU, +/// `DDDDDDDD:BB:DD.F, 0xDDDDVVVV, `. +#[cfg(not(target_arch = "wasm32"))] +fn nvidia(adapter: &Adapter) -> Option { + let out = run_with_timeout("nvidia-smi", &["--query-gpu=pci.bus_id,pci.device_id,memory.free", "--format=csv,noheader,nounits"])?; + pick(&parse_nvidia(&out), adapter) +} + +/// The cards `nvidia-smi` lists, free memory in bytes. +fn parse_nvidia(out: &str) -> Vec { + out.lines() + .filter_map(|l| { + let mut f = l.split(','); + let (bus, id, free) = (f.next()?, f.next()?, f.next()?); + let id = u32::from_str_radix(id.trim().trim_start_matches("0x").trim_start_matches("0X"), 16).ok()?; + let free = free.trim().parse::().ok()?.checked_mul(1 << 20)?; + Some(Card { bus: pci_address(bus), device: id >> 16, free }) + }) + .collect() +} + +/// `mem_info_vram_total - mem_info_vram_used` of the amdgpu card (its sysfs device directory is named +/// by its PCI address). +#[cfg(not(target_arch = "wasm32"))] +fn amdgpu(adapter: &Adapter) -> Option { + let read_hex = |p: &std::path::Path| -> Option { + let s = std::fs::read_to_string(p).ok()?; + u32::from_str_radix(s.trim().trim_start_matches("0x"), 16).ok() + }; + let read_u64 = |p: &std::path::Path| -> Option { std::fs::read_to_string(p).ok()?.trim().parse().ok() }; + let mut cards: Vec = Vec::new(); + let mut seen = Vec::new(); + for entry in std::fs::read_dir("/sys/class/drm").ok()?.flatten() { + // card0 and renderD128 are the same device + let Ok(dev) = std::fs::canonicalize(entry.path().join("device")) else { continue }; + if seen.contains(&dev) || read_hex(&dev.join("vendor")) != Some(adapter.vendor) { + continue; + } + seen.push(dev.clone()); + let (Some(device), Some(total), Some(used)) = + (read_hex(&dev.join("device")), read_u64(&dev.join("mem_info_vram_total")), read_u64(&dev.join("mem_info_vram_used"))) + else { + continue; + }; + let bus = dev.file_name().and_then(|n| pci_address(&n.to_string_lossy())); + cards.push(Card { bus, device, free: total.saturating_sub(used) }); + } + pick(&cards, adapter) +} + +/// Run a program and return its standard output, or `None` if it is missing, fails or takes too long. +#[cfg(not(target_arch = "wasm32"))] +fn run_with_timeout(program: &str, args: &[&str]) -> Option { + use std::io::Read; + use std::process::{Command, Stdio}; + let mut cmd = Command::new(program); + cmd.args(args).stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()); + // the release app has no console: without this, Windows opens one for each query + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + cmd.creation_flags(CREATE_NO_WINDOW); + } + let mut child = cmd.spawn().ok()?; + let start = Instant::now(); + loop { + match child.try_wait() { + Ok(Some(status)) if status.success() => break, + Ok(Some(_)) | Err(_) => return None, + Ok(None) if start.elapsed() > QUERY_TIMEOUT => { + let _ = child.kill(); + let _ = child.wait(); + return None; + } + Ok(None) => std::thread::sleep(Duration::from_millis(5)), + } + } + let mut out = String::new(); + child.stdout.take()?.read_to_string(&mut out).ok()?; + Some(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + const fn adapter(vendor: u32, device: u32) -> Adapter { + Adapter { vendor, device, bus: None } + } + + /// Wait until `flag` is set (a fake query that answers when the test says so), at most 10 s. + fn wait_for(flag: &std::sync::atomic::AtomicBool) { + let start = Instant::now(); + while !flag.load(std::sync::atomic::Ordering::SeqCst) && start.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + } + + /// Wait until `cache` has no background query for `a` running, at most 10 s. + fn settled(cache: &Cache, a: &Adapter) { + let start = Instant::now(); + while cache.asking(a) && start.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + assert!(!cache.asking(a), "the background query finished"); + } + + #[test] + fn a_stale_answer_is_returned_while_a_new_one_is_asked_for() { + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; + static CACHE: Cache = Cache::new(Duration::from_secs(30)); + static ASKED: AtomicU64 = AtomicU64::new(0); + static ANSWER: AtomicBool = AtomicBool::new(true); + fn blocked(_: &Adapter) -> Option { + wait_for(&ANSWER); + Some(ASKED.fetch_add(1, Ordering::SeqCst) + 1) + } + let a = adapter(NVIDIA, 1); + // the first question waits for the answer + assert_eq!(CACHE.get(&a, blocked), Some(1)); + assert!(CACHE.expire(&a)); + // stale, and the driver does not answer: the old answer at once, a new one in the background + ANSWER.store(false, Ordering::SeqCst); + let t = Instant::now(); + assert_eq!(CACHE.get(&a, blocked), Some(1)); + assert_eq!(CACHE.get(&a, blocked), Some(1)); + assert!(t.elapsed() < Duration::from_secs(5), "waited {:?} for the query", t.elapsed()); + assert!(CACHE.asking(&a)); + ANSWER.store(true, Ordering::SeqCst); + settled(&CACHE, &a); + assert_eq!(CACHE.get(&a, blocked), Some(2), "the background answer is kept"); + assert_eq!(ASKED.load(Ordering::SeqCst), 2, "one query in the background, not one per question"); + // another adapter is asked for directly + assert_eq!(CACHE.get(&adapter(AMD, 2), blocked), Some(3)); + } + + #[test] + fn each_adapter_keeps_its_own_answer() { + use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; + static CACHE: Cache = Cache::new(Duration::from_secs(30)); + static ASKED: AtomicU64 = AtomicU64::new(0); + static ANSWER: AtomicBool = AtomicBool::new(true); + // card 1 answers when the test says so, card 2 at once; 1000s and 2000s + fn ask(a: &Adapter) -> Option { + if a.device == 1 { + wait_for(&ANSWER); + } + Some(a.device as u64 * 1000 + ASKED.fetch_add(1, Ordering::SeqCst)) + } + let (one, two) = (adapter(NVIDIA, 1), adapter(NVIDIA, 2)); + assert_eq!(CACHE.get(&one, ask), Some(1000)); + assert_eq!(CACHE.get(&two, ask), Some(2001)); + assert!(CACHE.expire(&one) && CACHE.expire(&two)); + // card 1 refreshes in the background while card 2 is asked for: neither waits, nor replaces the other + ANSWER.store(false, Ordering::SeqCst); + assert_eq!(CACHE.get(&one, ask), Some(1000)); + assert_eq!(CACHE.get(&two, ask), Some(2001)); + settled(&CACHE, &two); + ANSWER.store(true, Ordering::SeqCst); + settled(&CACHE, &one); + let asked = ASKED.load(Ordering::SeqCst); + assert_eq!(asked, 4, "one background query per card"); + let (a1, a2) = (CACHE.get(&one, ask), CACHE.get(&two, ask)); + assert!(a1.is_some_and(|v| v / 1000 == 1) && a2.is_some_and(|v| v / 1000 == 2), "{a1:?} {a2:?}"); + assert_eq!(ASKED.load(Ordering::SeqCst), asked, "fresh answers are not asked for again"); + } + + #[test] + fn pci_addresses_parse_in_both_forms() { + assert_eq!(pci_address("0000:01:00.0"), Some([0, 1, 0, 0])); + assert_eq!(pci_address("00000000:0A:00.1"), Some([0, 10, 0, 1])); + assert_eq!(pci_address(""), None); + assert_eq!(pci_address("01:00.0"), None); + assert_eq!(pci_address("0000:01:00.0:9"), None); + } + + #[test] + fn nvidia_output_is_matched_by_address_then_device_id() { + let two = "00000000:01:00.0, 0x179B10DE, 3990\n00000000:02:00.0, 0x1F0810DE, 5120\n"; + let cards = parse_nvidia(two); + assert_eq!(pick(&cards, &adapter(NVIDIA, 0x1f08)), Some(5120 << 20)); + assert_eq!(pick(&cards, &adapter(NVIDIA, 0x179b)), Some(3990 << 20)); + // several GPUs and none matches: unknown + assert_eq!(pick(&cards, &adapter(NVIDIA, 0x2204)), None); + // a single GPU is taken even if its id is reported differently + assert_eq!(pick(&parse_nvidia("00000000:01:00.0, 0x179B10DE, 3990\n"), &adapter(NVIDIA, 0x1234)), Some(3990 << 20)); + // garbage is not a number + assert!(parse_nvidia("No devices were found\n").is_empty()); + assert_eq!(pick(&parse_nvidia(""), &adapter(NVIDIA, 0x179b)), None); + } + + #[test] + fn two_cards_of_one_model_are_told_apart_by_address_or_not_at_all() { + let twins = parse_nvidia("00000000:01:00.0, 0x1F0810DE, 1000\n00000000:02:00.0, 0x1F0810DE, 6000\n"); + let at = |bus| Adapter { bus: pci_address(bus), ..adapter(NVIDIA, 0x1f08) }; + assert_eq!(pick(&twins, &at("0000:02:00.0")), Some(6000 << 20)); + assert_eq!(pick(&twins, &at("0000:01:00.0")), Some(1000 << 20)); + // without an address the model alone is ambiguous: no answer, the caller keeps its fixed budget + assert_eq!(pick(&twins, &adapter(NVIDIA, 0x1f08)), None); + assert_eq!(pick(&twins, &at("")), None); + // an address no card has is no card: the model is not looked up instead + let mixed = parse_nvidia("00000000:01:00.0, 0x1F0810DE, 1000\n00000000:02:00.0, 0x220410DE, 6000\n"); + assert_eq!(pick(&mixed, &at("0000:03:00.0")), None); + } +} diff --git a/crates/gpu/src/wgsl/nrcj.wgsl b/crates/gpu/src/wgsl/nrcj.wgsl new file mode 100644 index 0000000000..4ea60f5fcf --- /dev/null +++ b/crates/gpu/src/wgsl/nrcj.wgsl @@ -0,0 +1,489 @@ +// Joint colour noise reduction (`lightcraft_pipeline::nrc_joint`). Each kernel mirrors its CPU +// twin there, in the same order of operations; keep them in step. +// +// Buffers: `a`, `b` inputs, `dst` output. Images are interleaved: RGB / Y,R−Y,B−Y (3 per pixel), +// chroma pairs R−Y,B−Y (2 per pixel), path pairs (h, v; exp(−path), see `nrc_joint`) or weight pairs (h, v). +// Kernels with the noise model: P[0] = n, P[1] = w, P[2] = h, P[3..6] = alpha, P[6..9] = beta, +// P[9..12] = ymax, P[12] = tolerance. + +fn ja(i: u32) -> vec3 { + return vec3(a[3u * i], a[3u * i + 1u], a[3u * i + 2u]); +} + +fn jb3(i: u32) -> vec3 { + return vec3(b[3u * i], b[3u * i + 1u], b[3u * i + 2u]); +} + +fn ja2(i: u32) -> vec2 { + return vec2(a[2u * i], a[2u * i + 1u]); +} + +fn jvc(v: vec3, i: u32) -> f32 { + return select(select(v.x, v.y, i == 1u), v.z, i == 2u); +} + +// `JointNoise::var`. +fn nvar(i: u32, y: f32) -> f32 { + let yy = min(y, pf(9u + i)); + return max(pf(3u + i) * max(yy, 0.0) + pf(6u + i), 1e-14); +} + +// `nrc_joint::distance`. +fn jdist(p: vec3, q: vec3) -> f32 { + let y = 0.5 * (p.x + q.x); + let t = 0.025 * max(y, 0.0); + let allowance = t * t; + var sum = 0.0; + for (var i = 0u; i < 3u; i++) { + let v = nvar(i, y); + let d = jvc(p, i) - jvc(q, i); + let excess = max(d * d - v, 0.0); + if (excess != 0.0) { + sum += excess / (3.0 * pf(12u) * v + allowance); + } + } + return sum; +} + +// `nrc_joint::weight`. +fn jweight(d: f32, p: f32) -> f32 { + let m = max(d, p); + return select(exp(-m), 1.0, m == 0.0); +} + +// `nrc_joint::yuv` of pixel `i` of image `a`. +fn jyuv(i: u32) -> vec3 { + let c = ja(i); + let y = lum2020(c); + return vec3(y, c.x - y, c.z - y); +} + +// `nrc_joint::yuv` of `n` pixels of image `a` from pixel P[1] on (a band's rows): P[0] = n. +@compute @workgroup_size(256) +fn nrcj_yuv(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let c = jyuv(pu(1u) + i); + dst[3u * i] = c.x; + dst[3u * i + 1u] = c.y; + dst[3u * i + 2u] = c.z; +} + +// The chroma pair (R−Y, B−Y) of the transformed image `a` (P[0] = n). +@compute @workgroup_size(256) +fn nrcj_uv(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + dst[2u * i] = a[3u * i + 1u]; + dst[2u * i + 1u] = a[3u * i + 2u]; +} + +// Noise samples of image `a`, transformed on the fly (`nrc_joint::noise_model`): P[0] = sample count, +// P[1] = w, P[2] = h, P[3] = stride. Per sample 11 values: valid, brightness, 3 × 3 residuals. +@compute @workgroup_size(256) +fn nrcj_samples(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let k = lin_index(g, nw); + if (k >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let index = k * pu(3u); + let x = index % w; + let y = index / w; + let o = 11u * k; + if (x == 0u || y == 0u || x + 1u == w || y + 1u == h) { + dst[o] = 0.0; + return; + } + let c = jyuv(index); + var brightness = 0.0; + for (var s = 0u; s < 3u; s++) { + let d = min(min(min(min(1u << s, x), y), w - x - 1u), h - y - 1u); + var m = vec3(0.0); + m = m + 0.25 * jyuv(index - d); + m = m + 0.25 * jyuv(index + d); + m = m + 0.25 * jyuv(index - d * w); + m = m + 0.25 * jyuv(index + d * w); + let r = c - m; + dst[o + 2u + 3u * s] = r.x * r.x; + dst[o + 3u + 3u * s] = r.y * r.y; + dst[o + 4u + 3u * s] = r.z * r.z; + brightness += m.x / 3.0; + } + dst[o] = 1.0; + dst[o + 1u] = max(brightness, 0.0); +} + +// `nrc_joint::median8`: the same 19-comparator network, on plain variables (a private array indexed in a +// loop ends up in slow local memory on some GPUs). +fn med8(v0: f32, v1: f32, v2: f32, v3: f32, v4: f32, v5: f32, v6: f32, v7: f32) -> f32 { + var a = v0; + var b = v1; + var c = v2; + var d = v3; + var e = v4; + var f = v5; + var g = v6; + var h = v7; + let t0 = min(a, c); + c = max(a, c); + a = t0; + let t1 = min(b, d); + d = max(b, d); + b = t1; + let t2 = min(e, g); + g = max(e, g); + e = t2; + let t3 = min(f, h); + h = max(f, h); + f = t3; + let t4 = min(a, e); + e = max(a, e); + a = t4; + let t5 = min(b, f); + f = max(b, f); + b = t5; + let t6 = min(c, g); + g = max(c, g); + c = t6; + let t7 = min(d, h); + h = max(d, h); + d = t7; + let t8 = min(a, b); + b = max(a, b); + a = t8; + let t9 = min(c, d); + d = max(c, d); + c = t9; + let t10 = min(e, f); + f = max(e, f); + e = t10; + let t11 = min(g, h); + h = max(g, h); + g = t11; + let t12 = min(c, e); + e = max(c, e); + c = t12; + let t13 = min(d, f); + f = max(d, f); + d = t13; + let t14 = min(b, e); + e = max(b, e); + b = t14; + let t15 = min(d, g); + g = max(d, g); + d = t15; + let t16 = min(b, c); + c = max(b, c); + b = t16; + let t17 = min(d, e); + e = max(d, e); + d = t17; + let t18 = min(f, g); + g = max(f, g); + f = t18; + return 0.5 * (d + e); +} + +// The farther of a value's distances to the least and largest of 8 (`speck_share`'s pre-test). +fn reach8(p: f32, v0: f32, v1: f32, v2: f32, v3: f32, v4: f32, v5: f32, v6: f32, v7: f32) -> f32 { + let lo = min(min(min(v0, v1), min(v2, v3)), min(min(v4, v5), min(v6, v7))); + let hi = max(max(max(v0, v1), max(v2, v3)), max(max(v4, v5), max(v6, v7))); + return max(abs(p - lo), abs(p - hi)); +} + +// The two largest values so far (`t.x` ≥ `t.y`) with `s` added, as the CPU's fold. +fn top2(t: vec2, s: f32) -> vec2 { + return select(vec2(t.x, max(t.y, s)), vec2(s, t.x), s > t.x); +} + +// One border pixel's share of the colour shift (`speck_share`). +fn sshare(q: vec3, m: vec3, du: f32, dv: f32, vu: f32, vv: f32, excess: f32) -> f32 { + return ((q.y - m.y) * du / vu + (q.z - m.z) * dv / vv) / excess; +} + +// `nrc_joint::despeck` + `speck_share`: `a` = transformed image, writes it with the chroma of compact +// colour specks replaced. Noise parameters as `nrcj_pilot`; P[13] = threshold, P[14] = share. +@compute @workgroup_size(256) +fn nrcj_speck(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let x = i % w; + let y = i / w; + let p = ja(i); + var o = p; + if (x >= 2u && y >= 2u && x + 2u < w && y + 2u < h) { + let threshold = pf(13u); + let share = pf(14u); + let up = i - 2u * w; + let dn = i + 2u * w; + // ring: corners and edge centres of the 5 × 5 square (in the CPU's order) + let r0 = ja(up - 2u); + let r1 = ja(up); + let r2 = ja(up + 2u); + let r3 = ja(i - 2u); + let r4 = ja(i + 2u); + let r5 = ja(dn - 2u); + let r6 = ja(dn); + let r7 = ja(dn + 2u); + let ym = (((((((r0.x + r1.x) + r2.x) + r3.x) + r4.x) + r5.x) + r6.x) + r7.x) / 8.0; + let vu = nvar(1u, ym); + let vv = nvar(2u, ym); + let bu = reach8(p.y, r0.y, r1.y, r2.y, r3.y, r4.y, r5.y, r6.y, r7.y); + let bv = reach8(p.z, r0.z, r1.z, r2.z, r3.z, r4.z, r5.z, r6.z, r7.z); + if (bu * bu / vu + bv * bv / vv > threshold) { + var m = vec3(0.0, med8(r0.y, r1.y, r2.y, r3.y, r4.y, r5.y, r6.y, r7.y), med8(r0.z, r1.z, r2.z, r3.z, r4.z, r5.z, r6.z, r7.z)); + let du = p.y - m.y; + let dv = p.z - m.z; + let excess = du * du / vu + dv * dv / vv; + if (excess > threshold) { + m.x = med8(r0.x, r1.x, r2.x, r3.x, r4.x, r5.x, r6.x, r7.x); + let dy = p.x - m.x; + let luminance = dy * dy / nvar(0u, ym); + let alone = clamp((1.5 * excess - luminance) / excess, 0.0, 1.0); + if (alone > 0.0) { + // the two largest border shares (`speck_share`) + var top = vec2(0.0, 0.0); + top = top2(top, sshare(r0, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r1, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r2, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r3, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r4, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r5, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r6, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(r7, m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(up - 1u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(up + 1u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(i - w - 2u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(i - w + 2u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(i + w - 2u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(i + w + 2u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(dn - 1u), m, du, dv, vu, vv, excess)); + top = top2(top, sshare(ja(dn + 1u), m, du, dv, vu, vv, excess)); + let compact = clamp((0.8 - top.y) / 0.3, 0.0, 1.0); + let t = clamp((excess - threshold) / threshold, 0.0, 1.0); + let k = share * t * t * (3.0 - 2.0 * t) * alone * compact; + if (k > 0.0) { + o.y += k * (m.y - p.y); + o.z += k * (m.z - p.z); + } + } + } + } + } + dst[3u * i] = o.x; + dst[3u * i + 1u] = o.y; + dst[3u * i + 2u] = o.z; +} + +// `nrc_joint::pilot`: `a` = transformed image, `b` = its gaussian(0.8); writes the guide. +@compute @workgroup_size(256) +fn nrcj_pilot(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let x = i % w; + let y = i / w; + var out = jb3(i); + if (!(x == 0u || y == 0u || x + 1u == w || y + 1u == h)) { + let p = ja(i); + // 9 · noise variance per component, at the pixel's luminance + let nv = 9.0 * vec3(nvar(0u, p.x), nvar(1u, p.x), nvar(2u, p.x)); + // axis 0 (left/right) and axis 1 (up/down), written out: private arrays indexed in a loop end up in + // slow local memory on some GPUs + let qa = ja(i - 1u); + let qb = ja(i + 1u); + let mh = (qa + p + qb) / 3.0; + let ha = qa - mh; + let hp = p - mh; + let hb = qb - mh; + let th = (ha * ha + hp * hp + hb * hb) / nv; + let vh = th.x + th.y + th.z; + let ua = ja(i - w); + let ub = ja(i + w); + let mv = (ua + p + ub) / 3.0; + let va = ua - mv; + let vp = p - mv; + let vb = ub - mv; + let tv = (va * va + vp * vp + vb * vb) / nv; + let vvar = tv.x + tv.y + tv.z; + // the axis choice fades in between 0.8 and 1.25 times its bound (nrc_joint::pilot_pick) + let sv = smoothstep(0.8, 1.25, vh / max(4.0, 4.0 * vvar)); + let sh = smoothstep(0.8, 1.25, vvar / max(4.0, 4.0 * vh)); + if (sv >= 1.0) { + out = mv; + } else if (sh >= 1.0) { + out = mh; + } else { + out = out + (mv - out) * sv + (mh - out) * sh; + } + } + dst[3u * i] = out.x; + dst[3u * i + 1u] = out.y; + dst[3u * i + 2u] = out.z; +} + +// Paths of the direct neighbours, exp(−distance) (1 where there is no pair): `a` = guide; writes path pairs. +@compute @workgroup_size(256) +fn nrcj_links1(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let x = i % w; + let y = i / w; + let p = ja(i); + var q = vec2(1.0); + if (x + 1u < w) { + let d = jdist(p, ja(i + 1u)); + q.x = jweight(d, d); + } + if (y + 1u < h) { + let d = jdist(p, ja(i + w)); + q.y = jweight(d, d); + } + dst[2u * i] = q.x; + dst[2u * i + 1u] = q.y; +} + +// Paths of length 2·delta from those of length delta (`a`, path pairs): their minimum. P[3] = delta. +@compute @workgroup_size(256) +fn nrcj_paths(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let delta = pu(3u); + let x = i % w; + let y = i / w; + var ph = a[2u * i]; + var pv = a[2u * i + 1u]; + if (x + delta < w) { + ph = min(ph, a[2u * (i + delta)]); + } + if (y + delta < h) { + pv = min(pv, a[2u * (i + delta * w) + 1u]); + } + dst[2u * i] = ph; + dst[2u * i + 1u] = pv; +} + +// `nrc_joint::path_weight`. +fn jpweight(d: f32, path: f32) -> f32 { + return select(min(exp(-d), path), path, d == 0.0); +} + +// The pair weights at distance `next` from the guide (`a`) and the paths (`b`); writes weight pairs. +// Noise parameters as `nrcj_pilot`; P[13] = next. +@compute @workgroup_size(256) +fn nrcj_links(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let next = pu(13u); + let x = i % w; + let y = i / w; + let p = ja(i); + var lh = 0.0; + var lv = 0.0; + let ph = b[2u * i]; + let pv = b[2u * i + 1u]; + if (x + next < w && ph > 0.0) { + lh = jpweight(jdist(p, ja(i + next)), ph); + } + if (y + next < h && pv > 0.0) { + lv = jpweight(jdist(p, ja(i + next * w)), pv); + } + dst[2u * i] = lh; + dst[2u * i + 1u] = lv; +} + +// One horizontal diffusion step (`nrc_joint::diffuse_h`): `a` = chroma pairs, `b` = weight pairs. +// P[0] = n, P[1] = w, P[2] = h, P[3] = delta, P[4] = rate. +@compute @workgroup_size(256) +fn nrcj_dh(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let delta = pu(3u); + let rate = pf(4u); + let x = i % w; + let p = ja2(i); + var o = p; + if (x >= delta) { + o += rate * b[2u * (i - delta)] * (ja2(i - delta) - p); + } + if (x + delta < w) { + o += rate * b[2u * i] * (ja2(i + delta) - p); + } + dst[2u * i] = o.x; + dst[2u * i + 1u] = o.y; +} + +// One vertical diffusion step (`nrc_joint::v_row`), parameters as `nrcj_dh`. +@compute @workgroup_size(256) +fn nrcj_dv(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let i = lin_index(g, nw); + if (i >= pu(0u)) { + return; + } + let w = pu(1u); + let h = pu(2u); + let delta = pu(3u); + let rate = pf(4u); + let y = i / w; + let p = ja2(i); + var o = p; + if (y >= delta) { + o += rate * b[2u * (i - delta * w) + 1u] * (ja2(i - delta * w) - p); + } + if (y + delta < h) { + o += rate * b[2u * i + 1u] * (ja2(i + delta * w) - p); + } + dst[2u * i] = o.x; + dst[2u * i + 1u] = o.y; +} + +// Back to RGB with the image's own luminance, blended by the strength: `a` = image, `b` = a band's +// filtered chroma pairs, `dst` = the whole output. P[0] = pixels written, P[1] = strength, +// P[2] = first pixel written (in the image), P[3] = first pixel of the band (in the image). +@compute @workgroup_size(256) +fn nrcj_recon(@builtin(global_invocation_id) g: vec3, @builtin(num_workgroups) nw: vec3) { + let k = lin_index(g, nw); + if (k >= pu(0u)) { + return; + } + let s = pf(1u); + let i = pu(2u) + k; + let bi = i - pu(3u); + let p = ja(i); + let y = lum2020(p); + let r = y + b[2u * bi]; + let bl = y + b[2u * bi + 1u]; + let res = vec3(r, (y - 0.2627 * r - 0.0593 * bl) / 0.6780, bl); + let o = p + s * (res - p); + dst[3u * i] = o.x; + dst[3u * i + 1u] = o.y; + dst[3u * i + 2u] = o.z; +} diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rs index 6feec0b333..4cb3388127 100644 --- a/crates/pipeline/src/finish.rs +++ b/crates/pipeline/src/finish.rs @@ -349,7 +349,7 @@ pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap { }, // V4, with a raw's tone curve rolling off into white in stops: a camera curve past its last // knot, the generic curve from half a stop below its white point instead of clipping (issue #1632) - Process::V5 => match info.camera_tone.as_ref().filter(|_| info.raw) { + Process::V5 | Process::V6 => match info.camera_tone.as_ref().filter(|_| info.raw) { Some(curve) => ToneMap::camera_rolloff_hdr(curve, 0.0, l.whites, 0.0, peak), None if info.raw => ToneMap::rolloff_hdr(l.contrast, l.whites, l.blacks, peak), None => ToneMap::display_hdr(l.contrast, l.whites, l.blacks, peak), diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index 6cfbd6d9a0..5d8a07489a 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -336,6 +336,43 @@ impl Frame { Some((nw, nh)) => std::borrow::Cow::Owned(resize(o, nw, nh, Filter::Mitchell)), None => std::borrow::Cow::Borrowed(o), }; + self.sample_base(base, plan, w, h) + } + + /// [`Frame::sample`] of each of `windows` of this frame at its `full_w × full_h` output size: the + /// same pixels, with the source oriented and prefiltered once for all of them rather than per window. + pub fn sample_windows(&self, src: &Rgb32f, full_w: usize, full_h: usize, windows: &[crate::PixelWindow]) -> Vec { + let oriented = if self.orient == Orientation::Normal { None } else { Some(src.oriented(self.orient)) }; + let o = oriented.as_ref().unwrap_or(src); + let plans: Vec<(Frame, SamplePlan)> = windows + .iter() + .map(|t| { + let frame = if (t.w, t.h) == (full_w, full_h) { self.clone() } else { self.window(full_w, full_h, *t) }; + let plan = frame.sample_plan(src.width, src.height, t.w, t.h); + (frame, plan) + }) + .collect(); + let mut bases: Vec<((usize, usize), Rgb32f)> = Vec::new(); + for dims in plans.iter().filter_map(|p| p.1.prefilter) { + if !bases.iter().any(|b| b.0 == dims) { + bases.push((dims, resize(o, dims.0, dims.1, Filter::Mitchell))); + } + } + plans + .into_iter() + .zip(windows) + .map(|((frame, plan), t)| { + let base = match plan.prefilter.and_then(|dims| bases.iter().find(|b| b.0 == dims)) { + Some(b) => std::borrow::Cow::Borrowed(&b.1), + None => std::borrow::Cow::Borrowed(o), + }; + frame.sample_base(base, plan, t.w, t.h) + }) + .collect() + } + + /// The sampling of [`Frame::sample`] from the oriented, prefiltered source `base`. + fn sample_base(&self, base: std::borrow::Cow<'_, Rgb32f>, plan: SamplePlan, w: usize, h: usize) -> Rgb32f { let affine = |xf: Affine| { let mut out = Rgb32f::new(w, h); par_rows(&mut out.data, w, |y, row| { diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index 0c1b795ab1..d8835f4ef1 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -40,6 +40,9 @@ pub mod local_tone; pub mod lut; pub mod mask_space; pub mod masks; +pub mod nrc_joint; +#[cfg(test)] +mod nrc_joint_reference; pub mod optics; pub mod output; pub mod profiles; @@ -393,6 +396,8 @@ struct CacheEntry { geo: u64, sampled: Arc, lin: Option<(u64, Arc)>, + /// The joint colour NR's noise model of the whole frame ([`colour_noise`]), keyed by [`Plan::noise_key`]. + colour_noise: Option<(u64, nrc_joint::JointNoise)>, planes: local::Planes, } @@ -425,6 +430,9 @@ pub struct Plan<'a> { /// [`Self::lin_key`] without the noise-reduction settings: draft renders reuse the smooth /// planes of an image that differs from this one only in its noise reduction. pub pre_nr_key: u64, + /// Key of the whole frame's noise model for the joint colour NR ([`colour_noise`]): what it is + /// measured after (everything before the colour NR), not the colour NR's own settings. + pub noise_key: u64, /// Red eye / pet eye corrections with their detected pupils, in output pixels. pub eyes: Vec, /// The whole frame's dehaze airlight, for a windowed render (which can't see the whole frame). @@ -435,6 +443,8 @@ pub struct Plan<'a> { pub keep: Option, /// The source's segmentation mattes ([`SourceInfo::mattes`]). pub mattes: Option>, + /// The whole frame and its output size (a windowed render's [`Self::frame`] is part of it). + pub full: (geometry::Frame, usize, usize), } /// Long edge of the small render a windowed render estimates the whole frame's airlight from. @@ -461,8 +471,13 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R let (full_w, full_h) = req.size(&frame); // sizes and scale are those of the whole output; a window only narrows what is drawn let px_per_long = frame.px_per_long(full_w); + let src_long = src.width.max(src.height); + // (an original reported smaller than the source is the source) + let native_long = info.native_long.unwrap_or(0).max(src_long).max(1); + let detail_scale = local::detail_scale(px_per_long, native_long); let mut fixed_air = None; let mut keep = None; + let full = (frame.clone(), full_w, full_h); let (frame, w, h) = match req.window { Some(win) => { let win = win.clamped(full_w, full_h); @@ -471,8 +486,20 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R let (pw, ph) = (((full_w as f64 * k).round() as usize).max(1), ((full_h as f64 * k).round() as usize).max(1)); fixed_air = Some(local::frame_airlight(src, info, s, &frame, pw, ph)); } - // (spots grow the rendered window; only 8-bit renders are cut back to the request) - let work = if req.depth == OutputDepth::U8 { spots::window_for_reads(s, &frame, full_w, full_h, px_per_long, win) } else { win }; + // (spots grow the rendered window of 8-bit renders; a grown window is cut back to the request) + let mut work = if req.depth == OutputDepth::U8 { spots::window_for_reads(s, &frame, full_w, full_h, px_per_long, win) } else { win }; + if local::joint_colour_nr(s) + && let Some(params) = local::nrc_params(s, detail_scale) + { + // The joint diffusion reads a chain of neighbours across its scales. Keep + // those reads outside the requested zoom window, just as spot reads are, at every depth. + let halo = params.passes.last().map_or(0, |p| (6.0 * p.sigma).ceil() as usize).saturating_add(4); + let x = work.x.saturating_sub(halo); + let y = work.y.saturating_sub(halo); + let right = work.x.saturating_add(work.w).saturating_add(halo).min(full_w); + let bottom = work.y.saturating_add(work.h).saturating_add(halo).min(full_h); + work = PixelWindow { x, y, w: right.saturating_sub(x), h: bottom.saturating_sub(y) }.clamped(full_w, full_h); + } if work != win { keep = Some(PixelWindow { x: win.x - work.x, y: win.y - work.y, ..win }); } @@ -480,10 +507,6 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R } None => (frame, full_w, full_h), }; - let src_long = src.width.max(src.height); - // (an original reported smaller than the source is the source) - let native_long = info.native_long.unwrap_or(0).max(src_long).max(1); - let detail_scale = local::detail_scale(px_per_long, native_long); let geo = hash_of((format!("{frame:?}"), w, h)); let (wb_t, wb_tint) = local::effective_wb(info, s); let eyes = redeye::resolve(src, &s.red_eye, s.orientation, &frame, w, h, px_per_long); @@ -503,7 +526,10 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R pre_nr_key, [d.nr_luminance, d.nr_detail, d.nr_color, d.nr_color_detail, d.nr_color_smoothness].map(f64::to_bits), detail_scale.to_bits(), + // the process picks the colour filter (Update to Current Process, undo) + local::joint_colour_nr(s), )); + let noise_key = hash_of((pre_nr_key, [d.nr_luminance, d.nr_detail].map(f64::to_bits), detail_scale.to_bits())); Plan { settings, frame, @@ -516,13 +542,64 @@ pub fn plan<'a>(src: &Rgb32f, info: &SourceInfo, s: &'a DevelopSettings, req: &R geo, lin_key, pre_nr_key, + noise_key, eyes, fixed_air, keep, mattes: info.mattes.clone(), + full, } } +/// The joint colour NR's noise model of the whole frame, measured on fixed tiles of it +/// ([`nrc_joint::tiles`]) so that every window of it is filtered alike. The tiles are resampled, +/// tone-mapped, white-balanced and luminance-denoised like the frame; the retouching and defringing +/// are left out. `None` when the joint colour NR is off, or when the render is the whole frame and +/// that is a single tile: the noise is then measured on the image itself, as the CPU and the GPU do. +pub fn colour_noise(src: &Rgb32f, info: &SourceInfo, p: &Plan<'_>) -> Option { + let s = &*p.settings; + if !local::joint_colour_nr(s) || local::nrc_params(s, p.detail_scale).is_none() { + return None; + } + let (full, fw, fh) = &p.full; + let windows = nrc_joint::tiles(*fw, *fh); + if (p.w, p.h) == (*fw, *fh) && windows.len() == 1 { + return None; + } + let t = profiling().then(std::time::Instant::now); + let (lum, _) = local::nr_params(s, p.detail_scale); + // the source oriented and prefiltered once for all tiles + let sampled = full.sample_windows(src, *fw, *fh, &windows); + let tiles: Vec = windows + .into_iter() + .zip(sampled) + .map(|(t, mut img)| { + let frame = if (t.w, t.h) == (*fw, *fh) { full.clone() } else { full.window(*fw, *fh, t) }; + let tile = Plan { + settings: Cow::Borrowed(s), + frame, + w: t.w, + h: t.h, + eyes: Vec::new(), + fixed_air: None, + keep: None, + mattes: None, + full: p.full.clone(), + ..*p + }; + local_tone::apply(&mut img, info, &tile); + local::white_balance(&mut img, info, s); + local::apply_nr(&mut img, lum, None); + img + }) + .collect(); + let noise = nrc_joint::noise_of_tiles(&tiles); + if let Some(t) = t { + eprintln!(" nr colour noise model ({} tiles): {:.0} ms", tiles.len(), t.elapsed().as_secs_f64() * 1e3); + } + Some(noise) +} + /// Whether the scene-linear stage needs work only the CPU does (defringe, spot removal). pub fn lin_needs_cpu(s: &DevelopSettings, info: &SourceInfo) -> bool { let o = &s.optics; @@ -633,13 +710,17 @@ fn render_impl(src: Src<'_>, info: &SourceInfo, s: &DevelopSettings, req: &Rende }; lap("sample", &mut t); + let mut colour_noise = cached.as_ref().and_then(|e| e.colour_noise).filter(|(k, _)| *k == plan.noise_key); let lin = match cached.as_ref().and_then(|e| e.lin.clone()).filter(|(k, _)| *k == lin_key) { Some((_, img)) => img, None => { // Without a cache the resampled buffer is ours: work on it in place. let mut img = if shared.is_some() { (*sampled).clone() } else { Arc::unwrap_or_clone(sampled.clone()) }; lin_cpu(&mut img, info, &plan); - local::denoise(&mut img, s, detail_scale); + if colour_noise.is_none() { + colour_noise = self::colour_noise(src_img, info, &plan).map(|n| (plan.noise_key, n)); + } + local::denoise(&mut img, s, detail_scale, colour_noise.as_ref().map(|c| &c.1)); Arc::new(img) } }; @@ -659,10 +740,14 @@ fn render_impl(src: Src<'_>, info: &SourceInfo, s: &DevelopSettings, req: &Rende } lap("prepare", &mut t); if let Some((a, c)) = shared { - c.put(CacheEntry { src: a.clone(), geo, sampled, lin: Some((lin_key, lin)), planes }); + c.put(CacheEntry { src: a.clone(), geo, sampled, lin: Some((lin_key, lin)), colour_noise, planes }); } if req.depth != OutputDepth::U8 { let deep = finish::finish_deep(&prep, s, frame, info, req.space, req.depth, req.proof); + let deep = match plan.keep { + Some(k) => deep.crop(k.x, k.y, k.w, k.h), + None => deep, + }; let image = deep.to_rgba8(); let histogram = Histogram::of_srgb8(&image); lap("finish (deep)", &mut t); diff --git a/crates/pipeline/src/light_v3.rs b/crates/pipeline/src/light_v3.rs index 03c651c79a..394c7c0829 100644 --- a/crates/pipeline/src/light_v3.rs +++ b/crates/pipeline/src/light_v3.rs @@ -284,7 +284,7 @@ pub fn applies(s: &DevelopSettings, info: &SourceInfo) -> bool { let camera_look = info.raw && info.camera_tone.is_some(); match s.process.process() { Process::V1 | Process::V2 | Process::V3 => false, - Process::V4 | Process::V5 => camera_look, + Process::V4 | Process::V5 | Process::V6 => camera_look, } } diff --git a/crates/pipeline/src/local.rs b/crates/pipeline/src/local.rs index 96177fe3fe..debb5e8db6 100644 --- a/crates/pipeline/src/local.rs +++ b/crates/pipeline/src/local.rs @@ -375,7 +375,7 @@ pub fn base_detail_radius(px_per_long: f64) -> usize { pub fn tone_base_detail(s: &DevelopSettings, px_per_long: f64) -> usize { match s.process.process() { Process::V1 => 0, - Process::V2 | Process::V3 | Process::V4 | Process::V5 => base_detail_radius(px_per_long), + Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 => base_detail_radius(px_per_long), } } @@ -458,9 +458,17 @@ pub fn nr_params(s: &DevelopSettings, scale: f32) -> (Option, Option) { let (lum, col) = nr_params(s, scale); + if col.is_some() && joint_colour_nr(s) { + apply_nr(img, lum, None); + if let Some(p) = nrc_params(s, scale) { + crate::nrc_joint::denoise(img, &p, (s.detail.nr_color_detail / 100.0) as f32, (s.detail.nr_color / 100.0) as f32, colour_noise); + } + return; + } apply_nr(img, lum, col); } @@ -505,6 +513,87 @@ pub fn apply_nr(img: &mut Rgb32f, lum: Option, col: Option) { } } +/// Most samples the noise level of the new colour noise reduction is measured from. The CPU and +/// the GPU sample the same pixels (see `nrc_joint::noise_sample_stride`). +pub const NRC_SAMPLES: usize = 131_072; + +/// One self-guided pass of the new colour noise reduction: Gaussian windows of `sigma` px, the +/// coefficients computed on a plane subsampled by `step`, `eps = k ×` the `pct`-quantile of the +/// local variance. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct NrcPass { + pub sigma: f32, + pub step: usize, + pub k: f32, + pub pct: f32, +} + +/// Parameters of the new colour noise reduction: the offset of the square roots and the passes +/// (fine, then coarse). +#[derive(Clone, Debug, PartialEq)] +pub struct NrcParams { + pub off: f32, + pub passes: Vec, +} + +/// The new colour noise reduction's parameters at `scale` output pixels per pixel of the original +/// ([`crate::Plan::detail_scale`]), `None` when Color is 0. +pub fn nrc_params(s: &DevelopSettings, scale: f32) -> Option { + let amount = (s.detail.nr_color / 100.0) as f32; + if amount <= 0.0 { + return None; + } + let detail = (s.detail.nr_color_detail / 100.0) as f32; + let smooth = 1.0 + (s.detail.nr_color_smoothness / 100.0) as f32; + let scale = if scale.is_finite() { scale.clamp(0.05, 1.0) } else { 1.0 }; + let sigma = (2.0 + 10.0 * amount) * smooth * scale; + let k = 64.0 * amount * (1.5 - detail); + // the coefficients vary over about sigma: a grid of sigma / 2 holds them + let pass = |sigma: f32| NrcPass { sigma, step: (sigma / 2.0).floor().clamp(1.0, 16.0) as usize, k, pct: 0.2 }; + // fine, then coarse (three times wider); the joint method takes the coarse one's reach + Some(NrcParams { off: 0.003, passes: vec![pass(sigma), pass(sigma * 3.0)] }) +} + +/// Chroma noise variance per channel as a function of the local luminance: `α · Y + β` (shot plus read noise). +#[derive(Clone, Copy, Debug)] +pub struct NrcNoise { + pub alpha: [f32; 3], + pub beta: [f32; 3], +} + +impl NrcNoise { + #[inline] + pub fn var(&self, i: usize, y: f32) -> f32 { + (self.alpha[i] * y.max(0.0) + self.beta[i]).max(1e-14) + } +} + +/// Weighted least squares `v = a · l + b` over `(l, v, weight)` with `a, b ≥ 0`: the free fit if both come out +/// non-negative, else the better of the line through the origin (`b = 0`) and the constant (`a = 0`). +pub fn fit_nonneg_line(pts: &[(f64, f64, f64)]) -> (f64, f64) { + let sw: f64 = pts.iter().map(|p| p.2).sum::().max(1e-300); + let (ml, mv) = (pts.iter().map(|p| p.2 * p.0).sum::() / sw, pts.iter().map(|p| p.2 * p.1).sum::() / sw); + let (cov, vl) = pts.iter().fold((0.0, 0.0), |(c, v), p| (c + p.2 * (p.0 - ml) * (p.1 - mv), v + p.2 * (p.0 - ml) * (p.0 - ml))); + if vl > 0.0 { + let a = cov / vl; + let b = mv - a * ml; + if a >= 0.0 && b >= 0.0 { + return (a, b); + } + } + let sse = |a: f64, b: f64| pts.iter().map(|p| p.2 * (p.1 - a * p.0 - b).powi(2)).sum::(); + let ll: f64 = pts.iter().map(|p| p.2 * p.0 * p.0).sum(); + let origin = if ll > 0.0 { (pts.iter().map(|p| p.2 * p.0 * p.1).sum::() / ll).max(0.0) } else { 0.0 }; + let constant = mv.max(0.0); + if sse(origin, 0.0) < sse(0.0, constant) { (origin, 0.0) } else { (0.0, constant) } +} + +/// Whether `s` gets the joint colour noise reduction (`nrc_joint`, process V6 on), else main's +/// blurred chromaticity. +pub fn joint_colour_nr(s: &DevelopSettings) -> bool { + s.process.process() >= lightcraft_develop::Process::V6 +} + pub fn log_lum(c: [f32; 3]) -> f32 { (luminance_2020(c).max(1e-7) / crate::tone::GREY).log2() } @@ -811,6 +900,250 @@ pub fn airlight_of(mut v: Vec) -> f32 { #[cfg(test)] mod tests { + /// Edits saved under V1 to V5 keep main's colour noise reduction (blurred chromaticity); V6 gets + /// the joint method of `nrc_joint`. + #[test] + fn colour_nr_follows_the_process() { + use lightcraft_develop::{DevelopSettings, Process}; + let (w, h) = (96usize, 64usize); + let img = Rgb32f::from_fn(w, h, |x, y| { + let n = |c: u32| { + let mut v = (x as u32).wrapping_mul(0x9e37_79b9) ^ (y as u32).wrapping_mul(0x85eb_ca6b) ^ c.wrapping_mul(0xc2b2_ae35) | 1; + v ^= v << 13; + v ^= v >> 17; + v ^= v << 5; + (v as f32 / u32::MAX as f32 - 0.5) * 0.02 + }; + let base = if x < w / 2 { [0.06, 0.03, 0.02] } else { [0.02, 0.04, 0.06] }; + [base[0] + n(1), base[1] + n(2), base[2] + n(3)] + }); + let run = |p: Process| { + let mut s = DevelopSettings { process: p.version(), ..Default::default() }; + s.detail.nr_color = 50.0; + let mut o = img.clone(); + denoise(&mut o, &s, 1.0, None); + o + }; + let (v1, v2, v5, v6) = (run(Process::V1), run(Process::V2), run(Process::V5), run(Process::V6)); + assert!(v1.data == v2.data && v2.data == v5.data, "V1 to V5 share main's colour NR"); + // main's colour NR clamps at 0 and keeps each pixel's luminance; the joint method keeps it too + assert!(v5.data != v6.data, "V6 takes the joint colour NR"); + let mut s = DevelopSettings { process: Process::V6.version(), ..Default::default() }; + s.detail.nr_color = 50.0; + let mut joint = img.clone(); + let p = nrc_params(&s, 1.0).expect("Color 50 has parameters"); + crate::nrc_joint::denoise(&mut joint, &p, (s.detail.nr_color_detail / 100.0) as f32, 0.5, None); + assert!(joint.data == v6.data, "V6 is the joint method"); + } + + /// Float mean of a flat, noisy dark patch over three noise realisations; linear modes assert drift bounds. + #[test] + #[ignore] + fn probe_nrc_mean_drift() { + use lightcraft_develop::DevelopSettings; + let (w, h) = (768usize, 768usize); + let mut worst = 0.0f64; + for seed0 in [0x2545_f491_4f6c_dd1du64, 0x9e37_79b9_7f4a_7c15, 0x1234_5678_9abc_def1] { + let mut seed = seed0; + let mut gauss = || { + (0..4) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as f32 / (1u64 << 53) as f32 - 0.5 + }) + .sum::() + * 1.732 + }; + for (name, base, sigma) in [ + ("dark warm, blue near 0", [0.006f32, 0.004, 0.0008], [0.002f32, 0.0015, 0.003]), + ("cat-like, red near 0", [0.0015, 0.004, 0.009], [0.004, 0.003, 0.005]), + ] { + let mut img = Rgb32f::new(w, h); + for p in img.data.iter_mut() { + *p = [0, 1, 2].map(|i| base[i] + sigma[i] * gauss()); + } + let mean = |im: &Rgb32f| { + let n = im.data.len() as f64; + [0, 1, 2].map(|i| im.data.iter().map(|p| p[i] as f64).sum::() / n) + }; + let m0 = mean(&img); + for nr in [25.0, 100.0] { + let mut s = DevelopSettings::default(); + s.detail.nr_color = nr; + let mut a = img.clone(); + denoise(&mut a, &s, 1.0, None); + let m = mean(&a); + let clipped0 = mean(&img.map(|p| p.map(|v| v.max(0.0)))); + let sd = |im: &Rgb32f, mm: [f64; 3]| { + [0, 1, 2].map(|i| (im.data.iter().map(|p| (p[i] as f64 - mm[i]).powi(2)).sum::() / im.data.len() as f64).sqrt()) + }; + worst = worst.max([0, 1, 2].iter().map(|&i| (m[i] / m0[i] - 1.0).abs()).fold(0.0, f64::max)); + println!( + "{name} NR {nr}: true mean {m0:.5?}, clipped noisy mean {clipped0:.5?}, after v2 {m:.5?} → {:+.3?} % vs true; noise sd {:.5?} → {:.5?}", + [0, 1, 2].map(|i| 100.0 * (m[i] / m0[i] - 1.0)), + sd(&img, m0), + sd(&a, m) + ); + } + } + } + println!("worst mean deviation {:.4} %", 100.0 * worst); + assert!(worst < 0.0005, "the joint colour NR moved a channel's mean by {:.3} %", 100.0 * worst); + } + + /// PROBE (prints, never fails): what colour NR v2 keeps of real colour. A clean synthetic scene (an + /// equal-luminance red/green edge, a 2 px red line on grey, a saturated blue patch near black, a flat grey + /// patch) plus noise; each measure compares the output with the clean scene. + #[test] + #[ignore] + fn probe_nrc_real_colour() { + use lightcraft_develop::DevelopSettings; + let (w, h) = (768usize, 512usize); + let y_of = |c: [f32; 3]| luminance_2020(c); + // two colours of the same luminance 0.03 + let red = [0.08f32, 0.0155, 0.02]; + let green = { + let g = [0.012f32, 0.04, 0.015]; + let k = y_of(red) / y_of(g); + g.map(|v| v * k) + }; + let grey = [0.03f32; 3]; + let blue_dark = [0.001f32, 0.0015, 0.02]; + // a bright saturated green next to black (a lit leaf against a dark background) + let leaf = [0.02f32, 0.12, 0.01]; + let clean = |x: usize, y: usize| -> [f32; 3] { + if y < 256 { + if x < 384 { red } else { green } + } else if x < 384 { + if (190..192).contains(&x) { red } else { grey } + } else if x < 576 { + blue_dark + } else if y < 384 { + grey + } else if x < 672 { + leaf + } else { + [0.0005f32; 3] + } + }; + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut gauss = || { + (0..4) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as f32 / (1u64 << 53) as f32 - 0.5 + }) + .sum::() + * 1.732 + }; + let mut img = Rgb32f::new(w, h); + for y in 0..h { + for x in 0..w { + let c = clean(x, y); + // shot + read noise per channel + img.data[y * w + x] = c.map(|v| v + (0.00002 + 0.004 * v.max(0.0)).sqrt() * 0.6 * gauss()); + } + } + let chroma = |c: [f32; 3]| { + let y = y_of(c); + c.map(|v| v - y) + }; + let dist = |a: [f32; 3], b: [f32; 3]| ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2) + (a[2] - b[2]).powi(2)).sqrt(); + for nr in [25.0, 100.0] { + let mut s = DevelopSettings::default(); + s.detail.nr_color = nr; + let mut o = img.clone(); + denoise(&mut o, &s, 1.0, None); + let px = |x: usize, y: usize| o.data[y * w + x]; + // edge: chroma error 1..8 px either side, relative to the step between the two colours + let step = dist(chroma(red), chroma(green)); + let mut e = 0.0; + let mut n = 0.0; + for y in 40..216 { + for d in 1..=8 { + for x in [384 - d, 383 + d] { + e += dist(chroma(px(x, y)), chroma(clean(x, y))); + n += 1.0; + } + } + } + // line: share of the line's chroma kept (mean over its rows) + let (mut kept, mut m) = (0.0, 0.0); + for y in 300..480 { + for x in 190..192 { + let c = chroma(px(x, y)); + let r = chroma(red); + let rg = chroma(grey); + let full = [0, 1, 2].map(|i| r[i] - rg[i]); + let got = [0, 1, 2].map(|i| c[i] - rg[i]); + kept += (got[0] * full[0] + got[1] * full[1] + got[2] * full[2]) / (full[0] * full[0] + full[1] * full[1] + full[2] * full[2]); + m += 1.0; + } + } + // saturated blue near black: chroma of the patch's mean kept + let mut acc = [0.0f32; 3]; + let mut cnt = 0.0; + for y in 300..480 { + for x in 420..540 { + acc = [0, 1, 2].map(|i| acc[i] + px(x, y)[i]); + cnt += 1.0; + } + } + let mean = acc.map(|v| v / cnt); + let sat_kept = dist(chroma(mean), [0.0; 3]) / dist(chroma(blue_dark), [0.0; 3]); + // grey: residual chroma noise + let mut sd = 0.0; + let mut k = 0.0; + for y in 270..370 { + for x in 600..760 { + sd += dist(chroma(px(x, y)), [0.0; 3]).powi(2); + k += 1.0; + } + } + let mut sd0 = 0.0; + for y in 270..370 { + for x in 600..760 { + sd0 += dist(chroma(img.data[y * w + x]), [0.0; 3]).powi(2); + } + } + // leaf next to black: chroma kept in the leaf's 12 px next to the black (mean over rows) + let (mut lk, mut ln) = (0.0f32, 0.0f32); + for y in 400..500 { + for x in 660..672 { + let c = chroma(px(x, y)); + let r = chroma(leaf); + lk += (c[0] * r[0] + c[1] * r[1] + c[2] * r[2]) / (r[0] * r[0] + r[1] * r[1] + r[2] * r[2]); + ln += 1.0; + } + } + println!("NR {nr}: leaf next to black keeps {:.0} % of its chroma (12 px next to the black)", 100.0 * lk / ln); + println!( + "NR {nr}: edge error {:.1} % of the step | red line kept {:.0} % | dark blue chroma kept {:.0} % | grey chroma noise {:.5} → {:.5}", + 100.0 * e / n / step, + 100.0 * kept / m, + 100.0 * sat_kept, + (sd0 / k).sqrt(), + (sd / k).sqrt() + ); + } + } + + #[test] + fn nrc_noise_fit_is_non_negative() { + // a falling cloud would give a negative slope: the constant fits instead + let (a, b) = fit_nonneg_line(&[(0.0, 4.0, 1.0), (1.0, 3.0, 1.0), (2.0, 2.0, 1.0)]); + assert!(a == 0.0 && (b - 3.0).abs() < 1e-9); + // a line through a negative intercept: refitted through the origin + let (a, b) = fit_nonneg_line(&[(1.0, 1.0, 1.0), (2.0, 3.0, 1.0), (3.0, 5.0, 1.0)]); + assert!(a > 0.0 && b == 0.0); + // an exact shot + read noise line is found as it is + let (a, b) = fit_nonneg_line(&[(0.0, 2.0, 1.0), (1.0, 5.0, 1.0), (2.0, 8.0, 1.0)]); + assert!((a - 3.0).abs() < 1e-9 && (b - 2.0).abs() < 1e-9); + } use super::*; use lightcraft_color::cct::xy_to_uv; @@ -1057,7 +1390,7 @@ mod nr_tests { let (v0, m0) = var(&img); let mut s = DevelopSettings::default(); s.detail.nr_luminance = 80.0; - denoise(&mut img, &s, 1.0); + denoise(&mut img, &s, 1.0, None); let (v1, m1) = var(&img); assert!(v1 < v0 * 0.5, "{v0} -> {v1}"); assert!((m1 - m0).abs() < 0.01); diff --git a/crates/pipeline/src/nrc_joint.rs b/crates/pipeline/src/nrc_joint.rs new file mode 100644 index 0000000000..37a55d07fb --- /dev/null +++ b/crates/pipeline/src/nrc_joint.rs @@ -0,0 +1,1175 @@ +//! Linear colour NR with a fixed, denoised joint guide. Strength controls the spatial scale; +//! noise-normalized guide distances control which neighbours can exchange colour. Both chroma +//! components use the same weights. Symmetric fluxes preserve the grid's channel sums, including +//! at its boundaries; no nonnegative clamp or square-root bias correction is involved. + +use lightcraft_color::luminance_2020; +use lightcraft_raster::{Rgb32f, blur::gaussian, resample::Filter}; + +use crate::{ + for_rows, + local::{NrcNoise, NrcParams, fit_nonneg_line}, +}; + +#[derive(Clone, Copy, Debug)] +pub struct JointNoise { + pub model: NrcNoise, + /// Do not extrapolate beyond bands that contain usable noise measurements. + pub ymax: [f32; 3], + pub tolerance: f32, +} + +impl JointNoise { + pub fn var(&self, i: usize, y: f32) -> f32 { + self.model.var(i, y.min(self.ymax[i])) + } +} + +fn yuv(c: [f32; 3]) -> [f32; 3] { + let y = luminance_2020(c); + [y, c[0] - y, c[2] - y] +} + +/// Noise of Y, R-Y and B-Y measured from small high-pass residuals, not the colour variance +/// of a wide smoothing window. The median-square calibration is for Gaussian residuals. +/// Luminance bands with predominantly texture may only lower their weight in the fit: they +/// cannot force a shadow scene's extrapolated highlight noise arbitrarily high. +fn noise_model(img: &Rgb32f) -> JointNoise { + if img.width < 3 || img.height < 3 { + return JointNoise { model: NrcNoise { alpha: [0.0; 3], beta: [1e-14; 3] }, ymax: [0.0; 3], tolerance: 1.0 }; + } + let mut samples = Vec::new(); + push_samples(img, noise_sample_stride(img.len()), &mut samples); + noise_from_samples(samples) +} + +/// The noise samples of `img` (Y, R-Y, B-Y) every `stride` pixels, in pixel order (see [`noise_model`]). +fn push_samples(img: &Rgb32f, stride: usize, samples: &mut Vec) { + if img.width < 3 || img.height < 3 { + return; + } + for index in (0..img.len()).step_by(stride) { + let (x, y) = (index % img.width, index / img.width); + if x == 0 || y == 0 || x + 1 == img.width || y + 1 == img.height { + continue; + } + let Some(c) = img.data.get(index) else { continue }; + let mut residual = [[0.0f32; 3]; 3]; + let mut brightness = 0.0; + let mut valid = true; + for (scale, delta) in [1usize, 2, 4].into_iter().enumerate() { + // Tiny images use the nearest supported scale rather than extending an edge + // into the noise samples. + let delta = delta.min(x).min(y).min(img.width - x - 1).min(img.height - y - 1); + let neighbours = [index - delta, index + delta, index - delta * img.width, index + delta * img.width]; + let mut m = [0.0f32; 3]; + for n in neighbours { + if let Some(p) = img.data.get(n) { + m = std::array::from_fn(|i| m[i] + 0.25 * p[i]); + } + } + residual[scale] = std::array::from_fn(|i| (c[i] - m[i]).powi(2)); + brightness += m[0] / 3.0; + valid &= c.iter().chain(m.iter()).all(|v| v.is_finite()); + } + if valid { + samples.push((brightness.max(0.0), residual)); + } + } +} + +/// Side of the tiles a frame's noise is measured on (px). +const TILE: usize = 128; +/// Tiles per row and column: many small tiles follow a mostly dark frame's few bright parts better +/// than a few large ones. +const GRID: usize = 12; + +/// The windows of a `full_w × full_h` frame its noise is measured on: the whole frame up to the +/// area of `GRID²` tiles, else `GRID × GRID` tiles spread evenly over it. A windowed render sees +/// only part of the frame; measured on these, every window of it gets the same noise model. +pub fn tiles(full_w: usize, full_h: usize) -> Vec { + if full_w * full_h <= GRID * GRID * TILE * TILE || full_w < 2 * TILE || full_h < 2 * TILE { + return vec![crate::PixelWindow { x: 0, y: 0, w: full_w, h: full_h }]; + } + let at = |n: usize, k: usize| ((n - TILE) as f64 * (k as f64 + 0.5) / GRID as f64).round() as usize; + (0..GRID * GRID).map(|k| crate::PixelWindow { x: at(full_w, k % GRID), y: at(full_h, k / GRID), w: TILE, h: TILE }).collect() +} + +/// The noise model of `tiles` of a frame (linear RGB at output scale, see [`tiles`]): the same +/// measurement as for a whole image, with the samples of all tiles together. +pub fn noise_of_tiles(tiles: &[Rgb32f]) -> JointNoise { + let transformed: Vec = tiles.iter().map(|t| t.map(yuv)).collect(); + let stride = noise_sample_stride(transformed.iter().map(Rgb32f::len).sum()); + let mut samples = Vec::new(); + for t in &transformed { + push_samples(t, stride, &mut samples); + } + if samples.is_empty() { + return JointNoise { model: NrcNoise { alpha: [0.0; 3], beta: [1e-14; 3] }, ymax: [0.0; 3], tolerance: 1.0 }; + } + noise_from_samples(samples) +} + +/// Distance between the noise samples of a `len`-pixel plane (CPU and GPU take the same pixels). +pub fn noise_sample_stride(len: usize) -> usize { + len.div_ceil(crate::local::NRC_SAMPLES).max(1) | 1 +} + +/// One noise sample: the mean brightness of its neighbourhoods and the squared high-pass residual of +/// Y, R−Y, B−Y at spacings 1, 2 and 4. +pub type NoiseSample = (f32, [[f32; 3]; 3]); + +/// The fitted noise model from the samples, in pixel order (see `noise_model`). +pub fn noise_from_samples(samples: Vec) -> JointNoise { + // Moving a slider re-renders the same image: the last fit is reused for identical samples (64-bit + // hash of their bits and count; the samples alone determine the fit). + static LAST: std::sync::Mutex> = std::sync::Mutex::new(None); + let mut hash = 0xcbf2_9ce4_8422_2325u64 ^ samples.len() as u64; + for s in &samples { + for v in std::iter::once(s.0).chain(s.1.iter().flatten().copied()) { + hash = (hash ^ u64::from(v.to_bits())).wrapping_mul(0x0000_0100_0000_01b3).rotate_left(5); + } + } + if let Some((h, noise)) = *LAST.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + && h == hash + { + return noise; + } + let noise = fit_noise(samples); + *LAST.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some((hash, noise)); + noise +} + +fn fit_noise(samples: Vec) -> JointNoise { + // brightness order, ties in pixel order (as a stable sort): sort compact keys (the bits `total_cmp` + // compares by, then the index), then gather + let mut keys: Vec<(i32, u32)> = samples + .iter() + .enumerate() + .map(|(k, s)| { + let bits = s.0.to_bits() as i32; + (bits ^ (((bits >> 31) as u32) >> 1) as i32, k as u32) + }) + .collect(); + keys.sort_unstable(); + let samples: Vec = keys.iter().filter_map(|&(_, k)| samples.get(k as usize).copied()).collect(); + const BANDS: usize = 16; + let per = samples.len().div_ceil(BANDS).max(32); + let mut pts: [Vec<(f64, f64, f64)>; 3] = std::array::from_fn(|_| Vec::new()); + let mut r: Vec = Vec::with_capacity(per); + for band in samples.chunks(per).filter(|b| b.len() >= 32) { + let y = band.iter().map(|p| p.0 as f64).sum::() / band.len() as f64; + for (i, pts) in pts.iter_mut().enumerate() { + let mut variance = 1e-14f64; + for scale in 0..3 { + r.clear(); + r.extend(band.iter().map(|p| p.1[scale][i])); + let middle = r.len() / 2; + let (_, median, _) = r.select_nth_unstable_by(middle, f32::total_cmp); + // Each kernel has norm squared 1.25. Larger spacings recover noise + // hidden by the positive spatial correlations of demosaiced RGB. + variance = variance.max(*median as f64 / (0.454_936_4 * 1.25)); + } + pts.push((y, variance, band.len() as f64)); + } + } + let mut model = NrcNoise { alpha: [0.0; 3], beta: [1e-14; 3] }; + let mut ymax = [0.0; 3]; + for (i, pts) in pts.iter().enumerate() { + if pts.is_empty() { + continue; + } + let (a, b, top) = robust_line(pts); + ymax[i] = top; + model.alpha[i] = a as f32; + model.beta[i] = (b as f32).max(1e-14); + } + JointNoise { model, ymax, tolerance: 1.0 } +} + +/// The noise line `v = a·y + b` through the bands `(y, variance, weight)` and the brightest band it +/// rests on. Seeded from the darker half, then all bands with a bounded influence for unexpectedly +/// high residuals (foliage etc.); refit rather than clamp the intercept. +pub(crate) fn robust_line(pts: &[(f64, f64, f64)]) -> (f64, f64, f32) { + let (mut a, mut b) = fit_nonneg_line(&pts[..pts.len().div_ceil(2)]); + let mut ymax = 0.0; + for _ in 0..4 { + // A band's weight fades to 0 between 4 and 5 times the fitted noise instead of dropping at + // 4: a band at the bound must not flip in or out with the last bits of the samples (the + // GPU's differ from the CPU's there), which moved the fit by a few percent. + let weighted: Vec<_> = pts + .iter() + .filter_map(|&(y, v, w)| { + let excess = v / (a * y + b).max(1e-14); + let fade = (5.0 - excess).clamp(0.0, 1.0); + (fade > 0.0).then_some((y, v, w * (2.0 / excess.max(2.0)).powi(4) * fade)) + }) + .collect(); + if !weighted.is_empty() { + (a, b) = fit_nonneg_line(&weighted); + ymax = weighted.iter().map(|p| p.0 as f32).fold(0.0, f32::max); + } + } + (a, b, ymax) +} + +/// Squared distance beyond the expected noise in a difference of two guide samples. A small +/// relative luminance allowance lets a smooth surface exchange colour without crossing a +/// significant luminance or isoluminant colour boundary. The guide is fixed across all scales. +/// (The filter runs `distance_run`, the same operations over planes; this per-pair form is its test reference.) +#[cfg(test)] +fn distance(a: [f32; 3], b: [f32; 3], noise: &JointNoise) -> f32 { + let y = 0.5 * (a[0] + b[0]); + let allowance = (0.025 * y.max(0.0)).powi(2); + let terms: [f32; 3] = std::array::from_fn(|i| { + let v = noise.var(i, y); + let excess = ((a[i] - b[i]).powi(2) - v).max(0.0); + // The 3x3 pilot blur reduces uncorrelated noise to 1/9. Keep a conservative + // allowance for correlations left by demosaicing and grid resampling. + // (A difference within the noise adds exactly 0: no division needed.) + if excess == 0.0 { 0.0 } else { excess / (3.0 * noise.tolerance * v + allowance) } + }); + terms.into_iter().sum::() +} + +/// The guide as three planes (Y, R−Y, B−Y): `distance_run` over them vectorises. +struct Planes { + c: [Vec; 3], + w: usize, +} + +impl Planes { + /// `len` pixels of row `y` from column `from`. + fn run(&self, y: usize, from: usize, len: usize) -> Option<[&[f32]; 3]> { + let start = y.checked_mul(self.w)?.checked_add(from)?; + let end = start.checked_add(len)?; + let [a, b, c] = &self.c; + Some([a.get(start..end)?, b.get(start..end)?, c.get(start..end)?]) + } +} + +/// `distance` of each pair of guide pixels `(a[k], b[k])` into `out[k]`, the same operations in the same +/// order (bit for bit), without branches so the compiler can work on several pixels at once. +fn distance_run(a: [&[f32]; 3], b: [&[f32]; 3], noise: &JointNoise, out: &mut [f32]) { + let ([ay, au, av], [by, bu, bv]) = (a, b); + let (alpha, beta, ymax, tolerance) = (noise.model.alpha, noise.model.beta, noise.ymax, noise.tolerance); + for ((((((o, &ay), &au), &av), &by), &bu), &bv) in out.iter_mut().zip(ay).zip(au).zip(av).zip(by).zip(bu).zip(bv) { + let y = 0.5 * (ay + by); + let allowance = (0.025 * y.max(0.0)).powi(2); + // `JointNoise::var`; an excess of 0 divides to exactly 0, as the branch in `distance` gives + let term = |i: usize, d: f32| { + let v = (alpha[i] * y.min(ymax[i]).max(0.0) + beta[i]).max(1e-14); + let excess = (d.powi(2) - v).max(0.0); + excess / (3.0 * tolerance * v + allowance) + }; + *o = term(0, ay - by) + term(1, au - bu) + term(2, av - bv); + } +} + +/// The exchange weight `exp(−max(distance, path))`; exactly 1 when both are 0 (no `exp` needed). +#[inline] +fn weight(distance: f32, path: f32) -> f32 { + let m = distance.max(path); + if m == 0.0 { 1.0 } else { (-m).exp() } +} + +/// `weight(distance, path)` with the path given as exp(−path): min(exp(−distance), that), and no `exp` +/// for a distance of 0. +#[inline] +fn path_weight(distance: f32, path: f32) -> f32 { + if distance == 0.0 { path } else { (-distance).exp().min(path) } +} + +/// Denoise the pilot along a clearly smoother axis at strong edges. An isotropic blur +/// dilutes a two-pixel isoluminant line before the edge decision even sees it. Weak or +/// ambiguous structure uses the isotropic pilot, so noise alone does not protect itself. +/// Written straight into planes (see `Planes`), one row at a time without branches per pixel. +fn pilot(original: &Rgb32f, noise: &JointNoise) -> Planes { + use rayon::prelude::*; + let blurred = gaussian(original, 0.8); + let (w, h) = (original.width, original.height); + let mut planes = Planes { c: std::array::from_fn(|_| vec![0.0; original.len()]), w }; + let [py, pu, pv] = &mut planes.c; + py.par_chunks_mut(w.max(1)).zip(pu.par_chunks_mut(w.max(1))).zip(pv.par_chunks_mut(w.max(1))).enumerate().for_each_init( + Vec::new, + |out: &mut Vec<[f32; 3]>, (y, ((ry, ru), rv))| { + let Some(blur) = blurred.data.get(y * w..(y + 1) * w) else { return }; + out.clear(); + out.extend_from_slice(blur); + if y > 0 + && y + 1 < h + && let (Some(up), Some(mid), Some(down)) = + (original.data.get((y - 1) * w..y * w), original.data.get(y * w..(y + 1) * w), original.data.get((y + 1) * w..(y + 2) * w)) + { + // columns 1 .. w − 1: the pixel, its left/right (axis 0) and upper/lower (axis 1) neighbours + for (((o, win), &u), &d) in out.iter_mut().skip(1).zip(mid.windows(3)).zip(up.iter().skip(1)).zip(down.iter().skip(1)) { + let &[l, p, r] = win else { continue }; + let var: [f32; 3] = std::array::from_fn(|i| 9.0 * noise.var(i, p[0])); + let axis = |a: [f32; 3], b: [f32; 3]| { + let m: [f32; 3] = std::array::from_fn(|i| (a[i] + p[i] + b[i]) / 3.0); + let t = |i: usize| ((a[i] - m[i]).powi(2) + (p[i] - m[i]).powi(2) + (b[i] - m[i]).powi(2)) / var[i]; + (m, t(0) + t(1) + t(2)) + }; + let (mh, vh) = axis(l, r); + let (mv, vvar) = axis(u, d); + *o = pilot_pick(*o, mh, vh, mv, vvar); + } + } + for (((c, y), u), v) in out.iter().zip(ry.iter_mut()).zip(ru.iter_mut()).zip(rv.iter_mut()) { + (*y, *u, *v) = (c[0], c[1], c[2]); + } + }, + ); + planes +} + +/// The pilot at one pixel from its isotropic blur `o`, the horizontal mean `mh` and noise-normalised +/// variance `vh`, and the vertical ones `mv`, `vvar`: the mean along the clearly smoother axis where +/// the other is more than 4 times as rough (and above 4), the blur where neither is. The choice fades +/// in between 0.8 and 1.25 times that bound instead of switching at it, so a pixel near the bound +/// takes nearly the same pilot when the noise model moves by a hair. +pub(crate) fn pilot_pick(o: [f32; 3], mh: [f32; 3], vh: f32, mv: [f32; 3], vvar: f32) -> [f32; 3] { + let sv = smoothstep(0.8, 1.25, vh / 4.0f32.max(4.0 * vvar)); + let sh = smoothstep(0.8, 1.25, vvar / 4.0f32.max(4.0 * vh)); + if sv >= 1.0 { + mv + } else if sh >= 1.0 { + mh + } else { + std::array::from_fn(|i| o[i] + (mv[i] - o[i]) * sv + (mh[i] - o[i]) * sh) + } +} + +/// The two chroma components `R − Y`, `B − Y`, or a pair of per-axis values (horizontal, vertical). +type Pair = [f32; 2]; +type PairImage = lightcraft_raster::Image; + +fn rgb2(y: f32, uv: Pair) -> [f32; 3] { + let r = y + uv[0]; + let b = y + uv[1]; + [r, (y - 0.2627 * r - 0.0593 * b) / 0.6780, b] +} + +/// One conservative horizontal/vertical diffusion step. Pair weights are symmetric; the +/// guide is never replaced by the filtered result. Long taps also account for the intervening +/// guide edges, so a wide pass cannot jump over a thin differently coloured object. +/// +/// Per pixel: `out = p`, then the lower neighbour's exchange, then the upper one's, in that order +/// (the order the result depends on, bit for bit). +fn diffuse(src: &PairImage, dst: &mut PairImage, links: &PairImage, delta: usize, vertical: bool, rate: f32) { + if vertical { diffuse_v(src, dst, links, delta, rate) } else { diffuse_h(src, dst, links, delta, rate) } +} + +#[inline(always)] +fn exchange(out: &mut Pair, p: Pair, q: Pair, weight: f32) { + out[0] += weight * (q[0] - p[0]); + out[1] += weight * (q[1] - p[1]); +} + +fn diffuse_h(src: &PairImage, dst: &mut PairImage, links: &PairImage, delta: usize, rate: f32) { + let w = src.width; + for_rows(&mut dst.data, w, |y, out| { + let (Some(s), Some(l)) = (src.data.get(y * w..(y + 1) * w), links.data.get(y * w..(y + 1) * w)) else { return }; + for (x, o) in out.iter_mut().enumerate() { + let p = s[x]; + *o = p; + if x >= delta { + exchange(o, p, s[x - delta], rate * l[x - delta][0]); + } + if x + delta < w { + exchange(o, p, s[x + delta], rate * l[x][0]); + } + } + }); +} + +fn diffuse_v(src: &PairImage, dst: &mut PairImage, links: &PairImage, delta: usize, rate: f32) { + let (w, h) = (src.width, src.height); + for_rows(&mut dst.data, w, |y, out| { + let Some(s) = src.data.get(y * w..(y + 1) * w) else { return }; + out.copy_from_slice(s); + if y >= delta + && let (Some(up), Some(lu)) = (src.data.get((y - delta) * w..(y - delta + 1) * w), links.data.get((y - delta) * w..(y - delta + 1) * w)) + { + for (((o, &p), &q), lk) in out.iter_mut().zip(s).zip(up).zip(lu) { + exchange(o, p, q, rate * lk[1]); + } + } + if y + delta < h + && let (Some(down), Some(ld)) = (src.data.get((y + delta) * w..(y + delta + 1) * w), links.data.get(y * w..(y + 1) * w)) + { + for (((o, &p), &q), lk) in out.iter_mut().zip(s).zip(down).zip(ld) { + exchange(o, p, q, rate * lk[1]); + } + } + }); +} + +/// The diffusion scales for a final spatial `sigma` (in grid pixels) on a `w × h` grid: each step's +/// neighbour distance and rate, in order. CPU and GPU run exactly these. +pub fn scale_plan(sigma: f32, w: usize, h: usize) -> Vec<(usize, f32)> { + let mut plan = Vec::new(); + let mut remaining = sigma * sigma; + let mut delta = 1usize; + while remaining > 0.01 && delta <= w.max(h) { + let rate = (remaining / (4.0 * (delta as f32).powi(2))).min(0.25); + plan.push((delta, rate)); + remaining -= 4.0 * rate * (delta as f32).powi(2); + delta = delta.saturating_mul(2); + } + plan +} + +/// Share of the filtered colour at Color `amount` (0..=1): a ramp up to the default 25. +pub fn strength(amount: f32) -> f32 { + (4.0 * amount).clamp(0.0, 1.0) +} + +/// Edge tolerance at Color Detail `detail` (0..=1). +pub fn tolerance(detail: f32) -> f32 { + (1.5 - detail).clamp(0.5, 1.5) +} + +/// The speck brake at Color `amount` (0..=1): the chroma excess (in noise variances of the two chroma +/// components together) from which a pixel starts to count as a colour speck (fully at twice that), and +/// the largest share of its colour that is replaced. Weak at the default 25 (only extreme specks, at most +/// half), stronger up to 100. Below 25 the final blend (`strength`) fades it out with the rest. +pub fn speck_brake(amount: f32) -> SpeckBrake { + let t = ((amount - 0.25) / 0.75).clamp(0.0, 1.0); + SpeckBrake { threshold: 64.0 - 39.0 * t, share: 0.5 + 0.5 * t } +} + +/// See [`speck_brake`]; `share` 0 turns the brake off. +#[derive(Clone, Copy, Debug)] +pub struct SpeckBrake { + pub threshold: f32, + pub share: f32, +} + +impl SpeckBrake { + pub const OFF: SpeckBrake = SpeckBrake { threshold: f32::INFINITY, share: 0.0 }; +} + +/// The median (mean of the middle two) of 8 values, by an optimal 19-comparator sorting network +/// (the GPU kernel uses the same one). +fn median8(v: [f32; 8]) -> f32 { + let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = v; + let cmp = |x: &mut f32, y: &mut f32| { + let (lo, hi) = (x.min(*y), x.max(*y)); + (*x, *y) = (lo, hi); + }; + cmp(&mut a, &mut c); + cmp(&mut b, &mut d); + cmp(&mut e, &mut g); + cmp(&mut f, &mut h); + cmp(&mut a, &mut e); + cmp(&mut b, &mut f); + cmp(&mut c, &mut g); + cmp(&mut d, &mut h); + cmp(&mut a, &mut b); + cmp(&mut c, &mut d); + cmp(&mut e, &mut f); + cmp(&mut g, &mut h); + cmp(&mut c, &mut e); + cmp(&mut d, &mut f); + cmp(&mut b, &mut e); + cmp(&mut d, &mut g); + cmp(&mut b, &mut c); + cmp(&mut d, &mut e); + cmp(&mut f, &mut g); + let _ = (a, b, c, f, g, h); + 0.5 * (d + e) +} + +fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { + let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0); + t * t * (3.0 - 2.0 * t) +} + +/// How much of a pixel's chroma the speck brake replaces by the median of the ring around it, and that +/// median (Y, R−Y, B−Y), from the pixel `p`, the ring `ring` and the rest of the 5 × 5 border `rest`. A +/// speck stands out in colour far beyond the noise, but not in luminance (a star, a lit point or any real +/// small detail almost always has a luminance step), and it is compact: the colour shift does not reach +/// the border (a line or an edge carries it on). Demosaicing spreads high-ISO specks over 2 × 2 to +/// 3 × 3 pixels, which all lie inside. The noise is taken at the ring's mean luminance (a median of it +/// is only needed for the few pixels past the threshold). +fn speck_share(p: [f32; 3], ring: &[[f32; 3]; 8], rest: impl Iterator, noise: &JointNoise, brake: SpeckBrake) -> (f32, [f32; 3]) { + let y = ring.iter().map(|q| q[0]).sum::() / 8.0; + let (vu, vv) = (noise.var(1, y), noise.var(2, y)); + // The medians lie between the ring's least and largest value, so the farther of the two bounds the + // excess from above (rounding keeps that order): most pixels are decided without the medians. + let reach = |c: usize| { + let (lo, hi) = ring.iter().fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), q| (lo.min(q[c]), hi.max(q[c]))); + (p[c] - lo).abs().max((p[c] - hi).abs()) + }; + let (bu, bv) = (reach(1), reach(2)); + if bu * bu / vu + bv * bv / vv <= brake.threshold { + return (0.0, [0.0; 3]); + } + let m = [0.0, median8(ring.map(|q| q[1])), median8(ring.map(|q| q[2]))]; + let (du, dv) = (p[1] - m[1], p[2] - m[2]); + let excess = du * du / vu + dv * dv / vv; + if excess <= brake.threshold { + return (0.0, m); + } + let m = [median8(ring.map(|q| q[0])), m[1], m[2]]; + // luminance: full brake up to half the chroma excess, none from 1.5 times it + let dy = p[0] - m[0]; + let luminance = dy * dy / noise.var(0, y); + let alone = ((1.5 * excess - luminance) / excess).clamp(0.0, 1.0); + if alone <= 0.0 { + return (0.0, m); + } + // compactness: the second largest share of the pixel's colour shift that a border pixel also has (a + // line or an edge through the pixel crosses the border at least twice; one noisy border pixel that + // happens to lean the same way does not protect a speck) + let (_, second) = ring + .iter() + .copied() + .chain(rest) + .map(|q| ((q[1] - m[1]) * du / vu + (q[2] - m[2]) * dv / vv) / excess) + .fold((0.0f32, 0.0f32), |(a, b), s| if s > a { (s, a) } else { (a, b.max(s)) }); + let compact = ((0.8 - second) / 0.3).clamp(0.0, 1.0); + (brake.share * smoothstep(brake.threshold, 2.0 * brake.threshold, excess) * alone * compact, m) +} + +/// Replace the chroma of compact colour specks by that of their surroundings (see `speck_share`), in the +/// transformed image (Y, R−Y, B−Y); luminance is never touched. Runs before the guide is built, so a speck +/// is neither kept by the edge-aware weights nor spread by them. +fn despeck(img: &mut Rgb32f, noise: &JointNoise, brake: SpeckBrake) { + use rayon::prelude::*; + if brake.share <= 0.0 || img.width < 5 || img.height < 5 { + return; + } + let (w, h) = (img.width, img.height); + let source = &*img; + // few pixels change: collect their new chroma per row from the unchanged image, then write them + let changes: Vec> = (2..h - 2) + .into_par_iter() + .map(|y| { + let mut changed = Vec::new(); + let rows: [Option<&[[f32; 3]]>; 5] = std::array::from_fn(|k| source.data.get((y + k - 2) * w..(y + k - 1) * w)); + let [Some(r0), Some(r1), Some(r2), Some(r3), Some(r4)] = rows else { return changed }; + for (x, ((((w0, w1), w2), w3), w4)) in + r0.windows(5).zip(r1.windows(5)).zip(r2.windows(5)).zip(r3.windows(5)).zip(r4.windows(5)).enumerate() + { + let (&[a0, a1, a2, a3, a4], &[b0, _, _, _, b4], &[c0, _, p, _, c4], &[d0, _, _, _, d4], &[e0, e1, e2, e3, e4]) = (w0, w1, w2, w3, w4) + else { + continue; + }; + // the ring: corners and edge centres of the 5 × 5 square, far enough that a speck spread over + // its direct neighbours by demosaicing does not reach them; the rest of the border: with the + // ring, every thin line or edge through the pixel crosses at least one of the 16 at any angle + let ring = [a0, a2, a4, c0, c4, e0, e2, e4]; + let rest = [a1, a3, b0, b4, d0, d4, e1, e3]; + let (k, m) = speck_share(p, &ring, rest.into_iter(), noise, brake); + if k > 0.0 { + changed.push((y * w + x + 2, [p[1] + k * (m[1] - p[1]), p[2] + k * (m[2] - p[2])])); + } + } + changed + }) + .collect(); + for (index, uv) in changes.into_iter().flatten() { + if let Some(o) = img.data.get_mut(index) { + (o[1], o[2]) = (uv[0], uv[1]); + } + } +} + +/// `noise`: the frame's noise model (see [`noise_of_tiles`]); `None` measures it on `img`. +pub(crate) fn denoise(img: &mut Rgb32f, params: &NrcParams, detail: f32, amount: f32, noise: Option<&JointNoise>) { + // Continuous ramp from zero; from the ordinary default Color=25 onward the spatial + // scale controls the extra smoothing. Noise calibration and edge decisions stay fixed. + denoise_with(img, params, 1, detail, strength(amount), speck_brake(amount), noise); +} + +#[cfg(test)] +fn denoise_grid(img: &mut Rgb32f, params: &NrcParams, step: usize, detail: f32, strength: f32, brake: SpeckBrake) { + denoise_with(img, params, step, detail, strength, brake, None); +} + +fn denoise_with(img: &mut Rgb32f, params: &NrcParams, step: usize, detail: f32, strength: f32, brake: SpeckBrake, noise: Option<&JointNoise>) { + if img.is_empty() || strength <= 0.0 { + return; + } + let start = crate::profiling().then(std::time::Instant::now); + // A maximum 2x grid keeps colour boundaries visible even at high strength. The old grid + // grew to 16x with the radius, making the colour loss inseparable from slider strength. + let step = if img.width.is_multiple_of(step) && img.height.is_multiple_of(step) { step } else { 1 }; + let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step)); + // Transform before resampling; negative linear differences are valid throughout. + let mut original = Rgb32f::new(w, h); + if step == 1 { + // the generic loop below with one sample per pixel, same operations (0 + c, / 1) + for_rows(&mut original.data, w, |y, row| { + for (x, p) in row.iter_mut().enumerate() { + if let Some(&c) = img.data.get(y * w + x) { + let c = yuv(c); + *p = c.map(|v| (0.0f32 + v) / 1.0f32); + } + } + }); + } else { + for_rows(&mut original.data, w, |y, row| { + for (x, p) in row.iter_mut().enumerate() { + let mut count = 0.0f32; + for sy in y * step..((y + 1) * step).min(img.height) { + for sx in x * step..((x + 1) * step).min(img.width) { + if let Some(&c) = img.data.get(sy * img.width + sx) { + let c = yuv(c); + *p = std::array::from_fn(|i| p[i] + c[i]); + count += 1.0; + } + } + } + *p = p.map(|v| v / count.max(1.0)); + } + }); + } + let lap = |what: &str, t: &mut Option| { + if let Some(t0) = t { + eprintln!(" joint {what}: {:.0} ms", t0.elapsed().as_secs_f64() * 1e3); + *t = Some(std::time::Instant::now()); + } + }; + let mut tt = start; + lap("transform", &mut tt); + let mut noise = match noise { + Some(n) => *n, + None => noise_model(&original), + }; + lap("noise model", &mut tt); + noise.tolerance = tolerance(detail); + despeck(&mut original, &noise, brake); + lap("speck brake", &mut tt); + let planes = pilot(&original, &noise); + lap("pilot", &mut tt); + if start.is_some() { + eprintln!(" nr joint: {w}x{h}, Y/R-Y/B-Y alpha {:?} beta {:?}, supported Y {:?}", noise.model.alpha, noise.model.beta, noise.ymax); + } + // Working buffers carry the two chroma components only (Y is restored from the image at the end). + let mut filtered: PairImage = original.map(|p| [p[1], p[2]]); + // the full-resolution reconstruction reads Y from `img`: the transformed copy is no longer needed + let original = (step != 1).then_some(original); + let mut scratch = PairImage::new(w, h); + let mut paths = PairImage::new(w, h); + let mut links = PairImage::new(w, h); + // Direct neighbours: the path over one step is the pair's own distance, and its weight is exp(−distance). + // `paths` holds exp(−path), the largest distance along a path, exponentiated: exp(−max(a, b)) is + // min(exp(−a), exp(−b)) bit for bit (exp is monotonic), so longer paths take minima and a weight with + // a new distance of 0 needs no `exp`; a path of 0 (no pair, at the border) is 1. + // the distances of row `y` to the pixels `offset` to the right and `offset` rows down + let row_distances = |y: usize, offset: usize, dh: &mut Vec, dv: &mut Vec| { + let n = w.saturating_sub(offset); + dh.resize(w, 0.0); + dv.resize(w, 0.0); + if let (Some(a), Some(b), Some(out)) = (planes.run(y, 0, n), planes.run(y, offset, n), dh.get_mut(..n)) { + distance_run(a, b, &noise, out); + } + if y + offset < h + && let (Some(a), Some(b)) = (planes.run(y, 0, w), planes.run(y + offset, 0, w)) + { + distance_run(a, b, &noise, dv); + } + }; + { + use rayon::prelude::*; + paths.data.par_chunks_mut(w).zip(links.data.par_chunks_mut(w)).enumerate().for_each_init( + || (Vec::new(), Vec::new()), + |(dh, dv), (y, (prow, lrow))| { + row_distances(y, 1, dh, dv); + for (x, (((out, link), &d0), &d1)) in prow.iter_mut().zip(lrow.iter_mut()).zip(dh.iter()).zip(dv.iter()).enumerate() { + *link = [0.0; 2]; + *out = [1.0; 2]; + if x + 1 < w { + link[0] = weight(d0, d0); + out[0] = link[0]; + } + if y + 1 < h { + link[1] = weight(d1, d1); + out[1] = link[1]; + } + } + }, + ); + } + lap("links 1", &mut tt); + // Scale is independent of the edge decision. K has no role in deciding whether a leaf + // is noise. Two dyadic sweeps provide broad smoothing with a fixed number of scales. + let sigma = params.passes.last().map_or(0.0, |p| p.sigma / step as f32); + let plan = scale_plan(sigma, w, h); + for (k, &(delta, rate)) in plan.iter().enumerate() { + for _ in 0..2 { + diffuse(&filtered, &mut scratch, &links, delta, false, rate); + std::mem::swap(&mut filtered, &mut scratch); + diffuse(&filtered, &mut scratch, &links, delta, true, rate); + std::mem::swap(&mut filtered, &mut scratch); + } + lap(&format!("scale {delta}"), &mut tt); + let Some(&(next, _)) = plan.get(k + 1) else { break }; + // In one pass: paths of length 2·delta (from the two adjacent delta-length paths, read from + // `paths`, written to `scratch`) and the next scale's pair weights from them. + { + use rayon::prelude::*; + let old = &paths; + scratch.data.par_chunks_mut(w).zip(links.data.par_chunks_mut(w)).enumerate().for_each_init( + || (Vec::new(), Vec::new()), + |(dh, dv), (y, (prow, lrow))| { + row_distances(y, next, dh, dv); + let (Some(here), below) = (old.data.get(y * w..(y + 1) * w), old.data.get((y + delta) * w..(y + delta + 1) * w)) else { return }; + for (x, (((out, link), &d0), &d1)) in prow.iter_mut().zip(lrow.iter_mut()).zip(dh.iter()).zip(dv.iter()).enumerate() { + let Some(&p) = here.get(x) else { continue }; + *out = p; + if x + delta < w + && let Some(q) = here.get(x + delta) + { + out[0] = out[0].min(q[0]); + } + if y + delta < h + && let Some(q) = below.and_then(|b| b.get(x)) + { + out[1] = out[1].min(q[1]); + } + *link = [0.0; 2]; + if x + next < w { + link[0] = path_weight(d0, out[0]); + } + if y + next < h { + link[1] = path_weight(d1, out[1]); + } + } + }, + ); + } + std::mem::swap(&mut paths, &mut scratch); + } + drop(scratch); + drop(paths); + drop(links); + match &original { + None => { + let fw = img.width; + for_rows(&mut img.data, fw, |y, row| { + for (x, p) in row.iter_mut().enumerate() { + let Some(&q) = filtered.data.get(y * w + x) else { continue }; + let result = rgb2(luminance_2020(*p), q); + *p = std::array::from_fn(|i| p[i] + strength * (result[i] - p[i])); + } + }); + } + Some(original) => { + // Reconstruct only rows. Keep the fine residual with a shared, noise-subtracted + // Wiener share; a flat noisy patch receives no automatic protection from noisy Y. + let mean = gaussian(original, 1.0); + let corr = gaussian(&original.map(|p| p.map(|v| v * v)), 1.0); + let share = corr.zip_map(&mean, |c, m| { + let snr = (1..3).map(|i| ((c[i] - m[i] * m[i]).max(0.0) / noise.var(i, m[0]) - 1.0).max(0.0)).sum::() * 0.5; + [snr / (snr + 8.0); 3] + }); + let share = gaussian(&share, 0.8); + // Resize one output row at a time using the raster resampler's exact pixel centres. + use lightcraft_raster::resample::weights; + let ux = weights(w, img.width, Filter::Bilinear); + let uy = weights(h, img.height, Filter::Bilinear); + let ow = img.width; + for_rows(&mut img.data, ow, |y, row| { + let Some((y0, wy)) = uy.get(y) else { return }; + for (x, p) in row.iter_mut().enumerate() { + let Some((x0, wx)) = ux.get(x) else { continue }; + let (mut f, mut o, mut a) = ([0.0; 2], [0.0; 3], 0.0); + for (j, &dy) in wy.iter().enumerate() { + for (i, &dx) in wx.iter().enumerate() { + let index = (y0 + j) * w + x0 + i; + if let (Some(q), Some(r), Some(s)) = (filtered.data.get(index), original.data.get(index), share.data.get(index)) { + let weight = dx * dy; + f = std::array::from_fn(|c| f[c] + weight * q[c]); + o = std::array::from_fn(|c| o[c] + weight * r[c]); + a += weight * s[0]; + } + } + } + let c = yuv(*p); + let q = [f[0] + a * (c[1] - o[1]), f[1] + a * (c[2] - o[2])]; + let result = rgb2(c[0], q); + *p = std::array::from_fn(|i| p[i] + strength * (result[i] - p[i])); + } + }); + } + } + if let Some(start) = start { + eprintln!(" nr joint: {:.1} ms", start.elapsed().as_secs_f64() * 1e3); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::local::NrcPass; + + fn params(step: usize) -> NrcParams { + NrcParams { off: 0.003, passes: vec![NrcPass { sigma: 4.0, step, k: 64.0, pct: 0.2 }, NrcPass { sigma: 12.0, step, k: 64.0, pct: 0.2 }] } + } + + fn noisy(clean: &Rgb32f, seed: u64, sigma: f32) -> Rgb32f { + let mut result = clean.clone(); + let mut seed = seed; + for p in &mut result.data { + for v in p { + let mut n = 0.0; + for _ in 0..4 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + n += (seed >> 11) as f32 / (1u64 << 53) as f32 - 0.5; + } + *v += sigma * 1.732 * n; + } + } + result + } + + /// The optimised filter reproduces the reference implementation (8b89526d) bit for bit: same scales, + /// weights, paths and reconstruction, only fewer passes and smaller buffers. + #[test] + fn joint_matches_the_reference_bit_for_bit() { + let mut scene = Rgb32f::new(97, 83); + for (i, p) in scene.data.iter_mut().enumerate() { + let (x, y) = (i % 97, i / 97); + // edges, a thin line, a dark patch and a gradient + *p = if x < 30 { + [0.08, 0.016, 0.02] + } else if x < 32 { + [0.01, 0.05, 0.01] + } else if y < 40 { + [0.0015, 0.004, 0.009] + } else { + [0.02 + 0.001 * x as f32, 0.03, 0.01 + 0.0005 * y as f32] + }; + } + for (w, h) in [(97usize, 83usize), (96, 82), (5, 3), (1, 7)] { + let mut clean = Rgb32f::new(w, h); + for y in 0..h { + for x in 0..w { + clean.data[y * w + x] = scene.data[y * 97 + x]; + } + } + for seed in [0x2545_f491_4f6c_dd1d, 0x9e37_79b9_7f4a_7c15] { + let input = noisy(&clean, seed, 0.004); + for step in [1, 2] { + for (detail, strength, sigma) in [(0.5, 1.0, 12.0), (0.25, 0.6, 4.0), (0.9, 1.0, 27.0)] { + let p = NrcParams { off: 0.003, passes: vec![NrcPass { sigma, step, k: 64.0, pct: 0.2 }] }; + let (mut a, mut b) = (input.clone(), input.clone()); + denoise_grid(&mut a, &p, step, detail, strength, SpeckBrake::OFF); + crate::nrc_joint_reference::denoise_grid(&mut b, &p, step, detail, strength); + for (i, (x, y)) in a.data.iter().zip(&b.data).enumerate() { + for c in 0..3 { + assert!( + x[c].to_bits() == y[c].to_bits(), + "{w}x{h} step {step} sigma {sigma}: pixel {i} channel {c}: {} vs {}", + x[c], + y[c] + ); + } + } + } + } + } + } + } + + /// BENCH (ignored, prints): the optimised filter against the reference on the same 24 MP image, + /// interleaved so both see the same machine load. Median of 3 each. + #[test] + #[ignore] + fn bench_joint_against_reference() { + let (w, h) = (6000usize, 4000usize); + let mut clean = Rgb32f::new(w, h); + for (i, p) in clean.data.iter_mut().enumerate() { + let (x, y) = (i % w, i / w); + *p = if (x / 300 + y / 300) % 2 == 0 { [0.03, 0.02, 0.01] } else { [0.004, 0.006, 0.012] }; + } + let input = noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.004); + let p = NrcParams { off: 0.003, passes: vec![NrcPass { sigma: 27.0, step: 1, k: 64.0, pct: 0.2 }] }; + let (mut new, mut old) = (Vec::new(), Vec::new()); + for _ in 0..3 { + let mut a = input.clone(); + let t = std::time::Instant::now(); + denoise_grid(&mut a, &p, 1, 0.5, 1.0, SpeckBrake::OFF); + new.push(t.elapsed().as_secs_f64()); + let mut b = input.clone(); + let t = std::time::Instant::now(); + crate::nrc_joint_reference::denoise_grid(&mut b, &p, 1, 0.5, 1.0); + old.push(t.elapsed().as_secs_f64()); + } + new.sort_by(f64::total_cmp); + old.sort_by(f64::total_cmp); + println!("joint 24 MP: optimised {:.2} s, reference {:.2} s (medians of {new:.2?} / {old:.2?})", new[1], old[1]); + } + + /// BENCH (ignored, prints): fitting the noise model to 130 000 samples (a 33 MP image's), median of 5. + #[test] + #[ignore] + fn bench_noise_fit() { + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut rnd = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as f32 / (1u64 << 53) as f32 + }; + let samples: Vec = (0..130_000) + .map(|_| { + let y = rnd() * 0.3; + (y, std::array::from_fn(|_| std::array::from_fn(|_| (1e-5 + 1e-4 * y) * rnd() * 2.0))) + }) + .collect(); + let mut times: Vec = (0..5) + .map(|_| { + let s = samples.clone(); + let t = std::time::Instant::now(); + let model = fit_noise(s); + assert!(model.model.beta[0] > 0.0); + t.elapsed().as_secs_f64() * 1e3 + }) + .collect(); + times.sort_by(f64::total_cmp); + println!("noise fit, 130 000 samples: {:.1} ms (of {times:.1?})", times[2]); + } + + /// BENCH (ignored, prints): the speck brake alone against the whole joint filter on one 24 MP image, + /// interleaved. Median of 3 each. + #[test] + #[ignore] + fn bench_speck_brake() { + let (w, h) = (6000usize, 4000usize); + let clean = Rgb32f::from_fn(w, h, |x, y| if (x / 300 + y / 300) % 2 == 0 { [0.03, 0.02, 0.01] } else { [0.004, 0.006, 0.012] }); + let input = noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.004); + let p = NrcParams { off: 0.003, passes: vec![NrcPass { sigma: 27.0, step: 1, k: 64.0, pct: 0.2 }] }; + let transformed = input.map(yuv); + let noise = noise_model(&transformed); + let (mut brake, mut joint) = (Vec::new(), Vec::new()); + for _ in 0..3 { + let mut out = transformed.clone(); + let t = std::time::Instant::now(); + despeck(&mut out, &noise, speck_brake(1.0)); + brake.push(t.elapsed().as_secs_f64()); + let mut a = input.clone(); + let t = std::time::Instant::now(); + denoise_grid(&mut a, &p, 1, 0.5, 1.0, SpeckBrake::OFF); + joint.push(t.elapsed().as_secs_f64()); + } + brake.sort_by(f64::total_cmp); + joint.sort_by(f64::total_cmp); + println!("24 MP: speck brake {:.3} s, joint without it {:.2} s (medians of {brake:.3?} / {joint:.2?})", brake[1], joint[1]); + } + + #[test] + fn joint_keeps_negative_means_luminance_and_reduces_chroma_noise() { + for (w, h) in [(128, 96), (127, 95), (1, 19), (19, 1)] { + let clean = Rgb32f::filled(w, h, [0.006, 0.004, 0.0008]); + for seed in [0x2545_f491_4f6c_dd1d, 0x1234_5678_9abc_def1] { + let input = noisy(&clean, seed, 0.003); + for step in [1, 2] { + let mut output = input.clone(); + denoise_grid(&mut output, ¶ms(step), step, 0.5, 1.0, speck_brake(1.0)); + assert!(output.data.iter().flatten().all(|v| v.is_finite())); + for (a, b) in input.data.iter().zip(&output.data) { + assert!((luminance_2020(*a) - luminance_2020(*b)).abs() < 1e-7); + } + // Thin inputs cannot support a spatial noise estimate; finite/luminance + // checks above still apply, but their sample mean is too noisy for this bound. + if w < 3 || h < 3 { + continue; + } + for c in 0..3 { + let difference = input.data.iter().zip(&output.data).map(|(a, b)| (b[c] - a[c]) as f64).sum::() / input.len() as f64; + assert!(difference.abs() < 2e-6, "step {step}, {w}x{h}, mean error {difference}"); + } + let energy = |im: &Rgb32f| { + im.data + .iter() + .map(|&p| { + let p = yuv(p); + let c = yuv(clean.data[0]); + (p[1] - c[1]).powi(2) + (p[2] - c[2]).powi(2) + }) + .sum::() + / im.len() as f32 + }; + assert!(energy(&output) < 0.08 * energy(&input)); + assert!(output.data.iter().any(|p| p.iter().any(|&v| v < 0.0)), "negative values were clipped"); + } + } + } + } + + #[test] + fn joint_protects_leaf_and_isoluminant_edges_without_chroma_halos() { + let leaf = [0.02, 0.12, 0.01]; + let black = [0.0005; 3]; + let red = [0.08, 0.0155, 0.02]; + let mut green = [0.012, 0.04, 0.015]; + let gain = luminance_2020(red) / luminance_2020(green); + green = green.map(|v| v * gain); + for (left, right) in [(leaf, black), (red, green)] { + let clean = Rgb32f::from_fn(192, 128, |x, _| if x < 96 { left } else { right }); + for seed in [0x2545_f491_4f6c_dd1d, 0x1234_5678_9abc_def1] { + let input = noisy(&clean, seed, 0.003); + for step in [1, 2] { + let mut output = input.clone(); + denoise_grid(&mut output, ¶ms(step), step, 0.5, 1.0, speck_brake(1.0)); + let step_uv = [yuv(left)[1] - yuv(right)[1], yuv(left)[2] - yuv(right)[2]]; + let length = step_uv.iter().map(|v| v * v).sum::().sqrt(); + for x in [92, 93, 98, 99] { + let mut mean = [0.0; 3]; + for y in 16..112 { + let p = yuv(output.get(x, y)); + mean = std::array::from_fn(|i| mean[i] + p[i] / 96.0); + } + let expected = yuv(if x < 96 { left } else { right }); + let error = ((mean[1] - expected[1]).powi(2) + (mean[2] - expected[2]).powi(2)).sqrt() / length; + assert!(error < 0.08, "step {step}, x {x}, colour error {}%", 100.0 * error); + } + } + } + } + } + + #[test] + fn joint_noise_fit_does_not_treat_bright_texture_as_shot_noise() { + let clean = Rgb32f::from_fn(256, 192, |x, y| { + let l = if x < 192 { 0.003 + x as f32 * 0.00002 } else { 0.15 + if (x + y) % 2 == 0 { 0.08 } else { -0.08 } }; + [l; 3] + }); + let input = noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.002).map(yuv); + let model = noise_model(&input); + for i in 0..3 { + assert!(model.var(i, 0.15) < 5.0 * model.var(i, 0.005), "texture dominated noise fit: {model:?}"); + } + } + + #[test] + fn joint_keeps_a_two_pixel_isoluminant_line() { + let grey = [0.03; 3]; + let mut red = [0.08, 0.0155, 0.02]; + let gain = luminance_2020(grey) / luminance_2020(red); + red = red.map(|v| v * gain); + let clean = Rgb32f::from_fn(192, 128, |x, _| if (94..96).contains(&x) { red } else { grey }); + let mut output = noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.006); + denoise_grid(&mut output, ¶ms(1), 1, 0.5, 1.0, speck_brake(1.0)); + let reference = yuv(red); + let mut mean = [0.0; 3]; + for y in 16..112 { + for x in 94..96 { + let p = yuv(output.get(x, y)); + mean = std::array::from_fn(|i| mean[i] + p[i] / 192.0); + } + } + let kept = (mean[1] * reference[1] + mean[2] * reference[2]) / (reference[1].powi(2) + reference[2].powi(2)); + assert!(kept > 0.85, "thin line keeps only {}%", 100.0 * kept); + } + + #[test] + fn joint_estimates_correlated_noise_at_a_matching_scale() { + let clean = Rgb32f::filled(256, 192, [0.03; 3]); + let correlated = gaussian(&noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.012), 0.8).map(yuv); + let model = noise_model(&correlated); + for i in 0..3 { + let mean = correlated.data.iter().map(|p| p[i] as f64).sum::() / correlated.len() as f64; + let variance = correlated.data.iter().map(|p| (p[i] as f64 - mean).powi(2)).sum::() / correlated.len() as f64; + let predicted = model.var(i, 0.03) as f64; + assert!(predicted > 0.6 * variance && predicted < 1.6 * variance, "channel {i}: estimated {predicted}, actual {variance}"); + } + } + + /// The speck brake takes the colour out of a compact, isoluminant 2 × 2 speck (as demosaicing leaves + /// them at high ISO), more at Color 100 than at 25, but leaves a coloured star (a luminance peak) alone. + #[test] + fn noise_line_does_not_jump_when_a_band_crosses_the_outlier_bound() { + // The luminance bands of a 640-px preview of demo scene 0 after luminance NR. Its band at + // 0.593 sat at 4 times the first fit: the CPU's samples put it just above, the GPU's just + // below, and the fits came out 2.6 % apart (CPU and GPU renders then differed by 10 levels). + let mut pts: Vec<(f64, f64, f64)> = [ + (0.009, 5.028e-7), + (0.022, 1.554e-4), + (0.063, 1.459e-4), + (0.147, 9.728e-6), + (0.213, 2.701e-5), + (0.272, 5.077e-6), + (0.315, 3.236e-5), + (0.356, 2.531e-5), + (0.390, 5.329e-6), + (0.420, 4.924e-6), + (0.467, 5.453e-5), + (0.530, 1.823e-5), + (0.593, 0.0), + (0.648, 1.501e-8), + (0.667, 1.898e-8), + (0.996, 3.490e-7), + ] + .map(|(y, v)| (y, v, 5649.0)) + .to_vec(); + let (a, b) = fit_nonneg_line(&pts[..pts.len().div_ceil(2)]); + let bound = 4.0 * (a * 0.593 + b); + let mut fit = |k: f64| { + pts[12].1 = bound * k; + robust_line(&pts) + }; + let (below, above) = (fit(1.0 - 1e-6), fit(1.0 + 1e-6)); + assert!((below.1 / above.1 - 1.0).abs() < 1e-4, "{below:?} vs {above:?}"); + assert!((below.0 - above.0).abs() <= 1e-4 * below.0.abs().max(1e-12), "{below:?} vs {above:?}"); + } + + #[test] + fn speck_brake_removes_compact_colour_specks_but_not_stars() { + let dark = [0.004f32; 3]; + let mut magenta = [0.024, 0.0, 0.024]; + magenta[1] = (luminance_2020(dark) - 0.2627 * magenta[0] - 0.0593 * magenta[2]) / 0.6780; + let star = [0.3, 0.2, 0.1]; + let clean = Rgb32f::from_fn(128, 96, |x, y| match (x, y) { + (40..42, 40..42) => magenta, + (90..92, 40..42) => star, + _ => dark, + }); + let input = noisy(&clean, 0x2545_f491_4f6c_dd1d, 0.001); + // share of the object's chroma (against the background) left in its 2 × 2 pixels + let kept = |out: &Rgb32f, x0: usize, colour: [f32; 3]| { + let (r, b) = (yuv(colour), yuv(dark)); + let reference = [r[1] - b[1], r[2] - b[2]]; + let mut mean = [0.0f32; 2]; + for (x, y) in [(x0, 40), (x0 + 1, 40), (x0, 41), (x0 + 1, 41)] { + let p = yuv(out.get(x, y)); + mean = [mean[0] + 0.25 * (p[1] - b[1]), mean[1] + 0.25 * (p[2] - b[2])]; + } + (mean[0] * reference[0] + mean[1] * reference[1]) / (reference[0].powi(2) + reference[1].powi(2)) + }; + let run = |brake| { + let mut out = input.clone(); + denoise_grid(&mut out, ¶ms(1), 1, 0.5, 1.0, brake); + (kept(&out, 40, magenta), kept(&out, 90, star)) + }; + let (off, star_off) = run(SpeckBrake::OFF); + let (weak, _) = run(speck_brake(0.25)); + let (strong, star_strong) = run(speck_brake(1.0)); + assert!(off > 0.5, "without the brake the speck keeps {off}"); + assert!(weak < 0.8 * off && strong < 0.2, "speck kept: off {off}, at 25 {weak}, at 100 {strong}"); + assert!(strong < weak, "the brake is stronger at 100 ({strong}) than at 25 ({weak})"); + assert!((star_strong - star_off).abs() < 0.02, "star colour {star_off} -> {star_strong}"); + } + + /// `distance_run` (planes, no branches) gives `distance` bit for bit. + #[test] + fn distance_run_matches_distance() { + let clean = Rgb32f::from_fn(64, 3, |x, _| [0.002 * x as f32, 0.01, 0.03 - 0.0004 * x as f32]); + let img = noisy(&clean, 0x9e37_79b9_7f4a_7c15, 0.01).map(yuv); + let mut noise = noise_model(&noisy(&Rgb32f::filled(64, 64, [0.02; 3]), 7, 0.004).map(yuv)); + noise.tolerance = 0.7; + let planes = Planes { c: std::array::from_fn(|i| img.data.iter().map(|p| p[i]).collect()), w: 64 }; + let mut out = vec![0.0; 64]; + distance_run(planes.run(0, 0, 64).unwrap(), planes.run(2, 0, 64).unwrap(), &noise, &mut out); + for (x, d) in out.iter().enumerate() { + assert_eq!(d.to_bits(), distance(img.get(x, 0), img.get(x, 2), &noise).to_bits(), "pixel {x}"); + } + } +} diff --git a/crates/pipeline/src/nrc_joint_reference.rs b/crates/pipeline/src/nrc_joint_reference.rs new file mode 100644 index 0000000000..13e2c46b95 --- /dev/null +++ b/crates/pipeline/src/nrc_joint_reference.rs @@ -0,0 +1,352 @@ +//! TEST ONLY: the joint colour NR as of 8b89526d, kept as the reference the optimised `nrc_joint` +//! must reproduce bit for bit. Changed since only where `nrc_joint` changed its result on purpose: +//! the noise line's outlier bands fade out (`nrc_joint::robust_line`) and the pilot's axis choice +//! fades in (`nrc_joint::pilot_pick`). +//! +//! Linear colour NR with a fixed, denoised joint guide. Strength controls the spatial scale; +//! noise-normalized guide distances control which neighbours can exchange colour. Both chroma +//! components use the same weights. Symmetric fluxes preserve the grid's channel sums, including +//! at its boundaries; no nonnegative clamp or square-root bias correction is involved. + +use lightcraft_color::luminance_2020; +use lightcraft_raster::{Rgb32f, blur::gaussian, resample::Filter}; + +use crate::{ + for_rows, + local::{NrcNoise, NrcParams}, +}; + +#[derive(Debug)] +struct JointNoise { + model: NrcNoise, + /// Do not extrapolate beyond bands that contain usable noise measurements. + ymax: [f32; 3], + tolerance: f32, +} + +impl JointNoise { + fn var(&self, i: usize, y: f32) -> f32 { + self.model.var(i, y.min(self.ymax[i])) + } +} + +fn yuv(c: [f32; 3]) -> [f32; 3] { + let y = luminance_2020(c); + [y, c[0] - y, c[2] - y] +} + +fn rgb(y: f32, uv: [f32; 3]) -> [f32; 3] { + let r = y + uv[1]; + let b = y + uv[2]; + [r, (y - 0.2627 * r - 0.0593 * b) / 0.6780, b] +} + +/// Noise of Y, R-Y and B-Y measured from small high-pass residuals, not the colour variance +/// of a wide smoothing window. The median-square calibration is for Gaussian residuals. +/// Luminance bands with predominantly texture may only lower their weight in the fit: they +/// cannot force a shadow scene's extrapolated highlight noise arbitrarily high. +fn noise_model(img: &Rgb32f) -> JointNoise { + const BANDS: usize = 16; + let mut samples = Vec::new(); + let stride = img.len().div_ceil(crate::local::NRC_SAMPLES).max(1) | 1; + if img.width < 3 || img.height < 3 { + return JointNoise { model: NrcNoise { alpha: [0.0; 3], beta: [1e-14; 3] }, ymax: [0.0; 3], tolerance: 1.0 }; + } + for index in (0..img.len()).step_by(stride) { + let (x, y) = (index % img.width, index / img.width); + if x == 0 || y == 0 || x + 1 == img.width || y + 1 == img.height { + continue; + } + let Some(c) = img.data.get(index) else { continue }; + let mut residual = [[0.0f32; 3]; 3]; + let mut brightness = 0.0; + let mut valid = true; + for (scale, delta) in [1usize, 2, 4].into_iter().enumerate() { + // Tiny images use the nearest supported scale rather than extending an edge + // into the noise samples. + let delta = delta.min(x).min(y).min(img.width - x - 1).min(img.height - y - 1); + let neighbours = [index - delta, index + delta, index - delta * img.width, index + delta * img.width]; + let mut m = [0.0f32; 3]; + for n in neighbours { + if let Some(p) = img.data.get(n) { + m = std::array::from_fn(|i| m[i] + 0.25 * p[i]); + } + } + residual[scale] = std::array::from_fn(|i| (c[i] - m[i]).powi(2)); + brightness += m[0] / 3.0; + valid &= c.iter().chain(m.iter()).all(|v| v.is_finite()); + } + if valid { + samples.push((brightness.max(0.0), residual)); + } + } + samples.sort_by(|a, b| a.0.total_cmp(&b.0)); + let per = samples.len().div_ceil(BANDS).max(32); + let mut pts: [Vec<(f64, f64, f64)>; 3] = std::array::from_fn(|_| Vec::new()); + for band in samples.chunks(per).filter(|b| b.len() >= 32) { + let y = band.iter().map(|p| p.0 as f64).sum::() / band.len() as f64; + for (i, pts) in pts.iter_mut().enumerate() { + let mut variance = 1e-14f64; + for scale in 0..3 { + let mut r: Vec = band.iter().map(|p| p.1[scale][i]).collect(); + let middle = r.len() / 2; + let (_, median, _) = r.select_nth_unstable_by(middle, f32::total_cmp); + // Each kernel has norm squared 1.25. Larger spacings recover noise + // hidden by the positive spatial correlations of demosaiced RGB. + variance = variance.max(*median as f64 / (0.454_936_4 * 1.25)); + } + pts.push((y, variance, band.len() as f64)); + } + } + let mut model = NrcNoise { alpha: [0.0; 3], beta: [1e-14; 3] }; + let mut ymax = [0.0; 3]; + for (i, pts) in pts.iter().enumerate() { + if pts.is_empty() { + continue; + } + // Seed from the darker half, then include all bands with a bounded influence for + // unexpectedly high residuals (foliage etc.). Refit rather than clamp the intercept. + let (a, b, top) = crate::nrc_joint::robust_line(pts); + ymax[i] = top; + model.alpha[i] = a as f32; + model.beta[i] = (b as f32).max(1e-14); + } + JointNoise { model, ymax, tolerance: 1.0 } +} + +/// Squared distance beyond the expected noise in a difference of two guide samples. A small +/// relative luminance allowance lets a smooth surface exchange colour without crossing a +/// significant luminance or isoluminant colour boundary. The guide is fixed across all scales. +fn distance(a: [f32; 3], b: [f32; 3], noise: &JointNoise) -> f32 { + let y = 0.5 * (a[0] + b[0]); + let allowance = (0.025 * y.max(0.0)).powi(2); + (0..3) + .map(|i| { + let v = noise.var(i, y); + // The 3x3 pilot blur reduces uncorrelated noise to 1/9. Keep a conservative + // allowance for correlations left by demosaicing and grid resampling. + ((a[i] - b[i]).powi(2) - v).max(0.0) / (3.0 * noise.tolerance * v + allowance) + }) + .sum::() +} + +/// Denoise the pilot along a clearly smoother axis at strong edges. An isotropic blur +/// dilutes a two-pixel isoluminant line before the edge decision even sees it. Weak or +/// ambiguous structure uses the isotropic pilot, so noise alone does not protect itself. +fn pilot(original: &Rgb32f, noise: &JointNoise) -> Rgb32f { + let mut guide = gaussian(original, 0.8); + let (w, h) = (original.width, original.height); + for_rows(&mut guide.data, w, |y, row| { + for (x, out) in row.iter_mut().enumerate() { + if x == 0 || y == 0 || x + 1 == w || y + 1 == h { + continue; + } + let index = y * w + x; + let Some(&p) = original.data.get(index) else { continue }; + let mut means = [[0.0f32; 3]; 2]; + let mut variances = [0.0; 2]; + for (axis, offset) in [1, w].into_iter().enumerate() { + let (Some(&a), Some(&b)) = (original.data.get(index - offset), original.data.get(index + offset)) else { continue }; + means[axis] = std::array::from_fn(|i| (a[i] + p[i] + b[i]) / 3.0); + variances[axis] = (0..3) + .map(|i| { + let m = means[axis][i]; + ((a[i] - m).powi(2) + (p[i] - m).powi(2) + (b[i] - m).powi(2)) / (9.0 * noise.var(i, p[0])) + }) + .sum::(); + } + *out = crate::nrc_joint::pilot_pick(*out, means[0], variances[0], means[1], variances[1]); + } + }); + guide +} + +/// One conservative horizontal/vertical diffusion step. Pair weights are symmetric; the +/// guide is never replaced by the filtered result. Long taps also account for the intervening +/// guide edges, so a wide pass cannot jump over a thin differently coloured object. +fn diffuse(src: &Rgb32f, dst: &mut Rgb32f, links: &Rgb32f, delta: usize, vertical: bool, rate: f32) { + let (w, h) = (src.width, src.height); + for_rows(&mut dst.data, w, |y, row| { + for (x, out) in row.iter_mut().enumerate() { + let index = y * w + x; + let Some(&p) = src.data.get(index) else { continue }; + *out = p; + let coordinate = if vertical { y } else { x }; + let length = if vertical { h } else { w }; + for neighbour in [coordinate.checked_sub(delta), coordinate.checked_add(delta).filter(|&v| v < length)].into_iter().flatten() { + let ni = if vertical { neighbour * w + x } else { y * w + neighbour }; + let start = coordinate.min(neighbour); + let pi = if vertical { start * w + x } else { y * w + start }; + let (Some(&q), Some(link)) = (src.data.get(ni), links.data.get(pi)) else { continue }; + let weight = rate * link[usize::from(vertical)]; + for i in 1..3 { + out[i] += weight * (q[i] - p[i]); + } + } + } + }); +} + +pub(crate) fn denoise_grid(img: &mut Rgb32f, params: &NrcParams, step: usize, detail: f32, strength: f32) { + if img.is_empty() || strength <= 0.0 { + return; + } + let start = crate::profiling().then(std::time::Instant::now); + // A maximum 2x grid keeps colour boundaries visible even at high strength. The old grid + // grew to 16x with the radius, making the colour loss inseparable from slider strength. + let step = if img.width.is_multiple_of(step) && img.height.is_multiple_of(step) { step } else { 1 }; + let (w, h) = (img.width.div_ceil(step), img.height.div_ceil(step)); + // Transform before resampling; negative linear differences are valid throughout. + let mut original = Rgb32f::new(w, h); + for_rows(&mut original.data, w, |y, row| { + for (x, p) in row.iter_mut().enumerate() { + let mut count = 0.0f32; + for sy in y * step..((y + 1) * step).min(img.height) { + for sx in x * step..((x + 1) * step).min(img.width) { + if let Some(&c) = img.data.get(sy * img.width + sx) { + let c = yuv(c); + *p = std::array::from_fn(|i| p[i] + c[i]); + count += 1.0; + } + } + } + *p = p.map(|v| v / count.max(1.0)); + } + }); + let lap = |what: &str, t: &mut Option| { + if let Some(t0) = t { + eprintln!(" joint {what}: {:.0} ms", t0.elapsed().as_secs_f64() * 1e3); + *t = Some(std::time::Instant::now()); + } + }; + let mut tt = start; + lap("transform", &mut tt); + let mut noise = noise_model(&original); + lap("noise model", &mut tt); + noise.tolerance = (1.5 - detail).clamp(0.5, 1.5); + let guide = pilot(&original, &noise); + lap("pilot", &mut tt); + if std::env::var_os("NRV2_DEBUG").is_some() { + eprintln!(" nr joint: {w}x{h}, Y/R-Y/B-Y alpha {:?} beta {:?}, supported Y {:?}", noise.model.alpha, noise.model.beta, noise.ymax); + } + let mut filtered = original.clone(); + let mut scratch = Rgb32f::new(w, h); + let mut paths = Rgb32f::new(w, h); + let mut links = Rgb32f::new(w, h); + for_rows(&mut paths.data, w, |y, row| { + for (x, out) in row.iter_mut().enumerate() { + let index = y * w + x; + let Some(&g) = guide.data.get(index) else { continue }; + if x + 1 < w + && let Some(&b) = guide.data.get(index + 1) + { + out[0] = distance(g, b, &noise); + } + if y + 1 < h + && let Some(&b) = guide.data.get(index + w) + { + out[1] = distance(g, b, &noise); + } + } + }); + // Scale is independent of the edge decision. K has no role in deciding whether a leaf + // is noise. Two dyadic sweeps provide broad smoothing with a fixed number of scales. + let sigma = params.passes.last().map_or(0.0, |p| p.sigma / step as f32); + let mut remaining = sigma * sigma; + let mut delta = 1usize; + while remaining > 0.01 && delta <= w.max(h) { + let rate = (remaining / (4.0 * (delta as f32).powi(2))).min(0.25); + // Each symmetric pair weight is evaluated once per scale and reused by both + // diffusion sweeps and both ends of the pair. + for_rows(&mut links.data, w, |y, row| { + for (x, out) in row.iter_mut().enumerate() { + let index = y * w + x; + let (Some(&g), Some(path)) = (guide.data.get(index), paths.data.get(index)) else { continue }; + *out = [0.0; 3]; + for (axis, offset, valid) in [(0, delta, x + delta < w), (1, delta * w, y + delta < h)] { + if valid && let Some(&other) = guide.data.get(index + offset) { + out[axis] = (-distance(g, other, &noise).max(path[axis])).exp(); + } + } + } + }); + for _ in 0..2 { + diffuse(&filtered, &mut scratch, &links, delta, false, rate); + std::mem::swap(&mut filtered, &mut scratch); + diffuse(&filtered, &mut scratch, &links, delta, true, rate); + std::mem::swap(&mut filtered, &mut scratch); + } + remaining -= 4.0 * rate * (delta as f32).powi(2); + // Maxima over paths of length 2*delta, using the two adjacent delta-length paths. + // Reuse the diffusion scratch so a long tap costs O(1), irrespective of radius. + for_rows(&mut scratch.data, w, |y, row| { + for (x, out) in row.iter_mut().enumerate() { + let index = y * w + x; + let Some(&p) = paths.data.get(index) else { continue }; + *out = p; + if x + delta < w + && let Some(q) = paths.data.get(index + delta) + { + out[0] = out[0].max(q[0]); + } + if y + delta < h + && let Some(q) = paths.data.get(index + delta * w) + { + out[1] = out[1].max(q[1]); + } + } + }); + std::mem::swap(&mut paths, &mut scratch); + lap(&format!("scale {delta}"), &mut tt); + delta = delta.saturating_mul(2); + } + drop(scratch); + drop(paths); + drop(links); + if step == 1 { + for (p, q) in img.data.iter_mut().zip(&filtered.data) { + let result = rgb(luminance_2020(*p), *q); + *p = std::array::from_fn(|i| p[i] + strength * (result[i] - p[i])); + } + } else { + // Reconstruct only rows. Keep the fine residual with a shared, noise-subtracted + // Wiener share; a flat noisy patch receives no automatic protection from noisy Y. + let mean = gaussian(&original, 1.0); + let corr = gaussian(&original.map(|p| p.map(|v| v * v)), 1.0); + let share = corr.zip_map(&mean, |c, m| { + let snr = (1..3).map(|i| ((c[i] - m[i] * m[i]).max(0.0) / noise.var(i, m[0]) - 1.0).max(0.0)).sum::() * 0.5; + [snr / (snr + 8.0); 3] + }); + let share = gaussian(&share, 0.8); + // Resize one output row at a time using the raster resampler's exact pixel centres. + use lightcraft_raster::resample::weights; + let ux = weights(w, img.width, Filter::Bilinear); + let uy = weights(h, img.height, Filter::Bilinear); + let ow = img.width; + for_rows(&mut img.data, ow, |y, row| { + let Some((y0, wy)) = uy.get(y) else { return }; + for (x, p) in row.iter_mut().enumerate() { + let Some((x0, wx)) = ux.get(x) else { continue }; + let (mut f, mut o, mut a) = ([0.0; 3], [0.0; 3], 0.0); + for (j, &dy) in wy.iter().enumerate() { + for (i, &dx) in wx.iter().enumerate() { + let index = (y0 + j) * w + x0 + i; + if let (Some(q), Some(r), Some(s)) = (filtered.data.get(index), original.data.get(index), share.data.get(index)) { + let weight = dx * dy; + f = std::array::from_fn(|c| f[c] + weight * q[c]); + o = std::array::from_fn(|c| o[c] + weight * r[c]); + a += weight * s[0]; + } + } + } + let c = yuv(*p); + let q = std::array::from_fn(|i| f[i] + a * (c[i] - o[i])); + let result = rgb(c[0], q); + *p = std::array::from_fn(|i| p[i] + strength * (result[i] - p[i])); + } + }); + } + if let Some(start) = start { + eprintln!(" nr joint: {:.1} ms", start.elapsed().as_secs_f64() * 1e3); + } +} diff --git a/crates/pipeline/src/output.rs b/crates/pipeline/src/output.rs index 09f2bbe6a4..b514c25503 100644 --- a/crates/pipeline/src/output.rs +++ b/crates/pipeline/src/output.rs @@ -364,6 +364,27 @@ impl DeepImage { } out } + + /// Crop to `[x0, x0+w) × [y0, y0+h)` (clamped to the image). + pub fn crop(&self, x0: usize, y0: usize, w: usize, h: usize) -> Self { + let x0 = x0.min(self.width); + let y0 = y0.min(self.height); + let w = w.min(self.width - x0); + let h = h.min(self.height - y0); + fn rows(v: &[T], width: usize, x0: usize, y0: usize, w: usize, h: usize) -> Vec { + let mut out = Vec::with_capacity(3 * w * h); + for y in y0..y0 + h { + let at = 3 * (y * width + x0); + out.extend_from_slice(v.get(at..at + 3 * w).unwrap_or_default()); + } + out + } + let samples = match &self.samples { + DeepSamples::U16(v) => DeepSamples::U16(rows(v, self.width, x0, y0, w, h)), + DeepSamples::F32(v) => DeepSamples::F32(rows(v, self.width, x0, y0, w, h)), + }; + Self { width: w, height: h, space: self.space, samples } + } } trait IntoRgba { diff --git a/crates/pipeline/src/tests.rs b/crates/pipeline/src/tests.rs index cee4ec1ffb..f1229bd569 100644 --- a/crates/pipeline/src/tests.rs +++ b/crates/pipeline/src/tests.rs @@ -845,6 +845,48 @@ fn sliver_crops_render_quickly_with_wide_stages_on() { } } +// Given a cached render with one process, a change to a process with another colour filter (Update +// to Current Process, undo) renders like a fresh render, not the cached denoised image +#[test] +fn a_process_change_does_not_reuse_the_other_colour_filter() { + use crate::{StageCache, render_cached}; + use lightcraft_develop::Process; + use std::sync::Arc; + let src = Arc::new(colour_noise_scene(128, 96, 128)); + let info = SourceInfo::default(); + let req = RenderRequest::fit(128, 96); + let cache = StageCache::default(); + let mut old = DevelopSettings::default(); + old.detail.nr_color = 25.0; + old.detail.nr_color_smoothness = 0.0; + old.process = Process::V2.version(); + let current = DevelopSettings { process: Process::LATEST.version(), ..old.clone() }; + let first = render_cached(&src, &info, &old, &req, &cache).image; + let fresh = render(&src, &info, ¤t, &req).image; + assert!(max_diff(&first, &fresh) > 0, "the two colour filters differ on this scene"); + assert_eq!(render_cached(&src, &info, ¤t, &req, &cache).image, fresh, "process update"); + assert_eq!(render_cached(&src, &info, &old, &req, &cache).image, first, "undo"); +} + +/// Flat grey with chroma noise: `quiet` columns on the left with little noise, the rest with much. +pub(crate) fn colour_noise_scene(w: usize, h: usize, quiet: usize) -> Rgb32f { + let mut seed = 0x2545_f491_4f6c_dd1d_u64; + let mut noise = move || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + ((seed >> 11) as f32 / (1u64 << 53) as f32 - 0.5) * 2.0 + }; + let mut img = Rgb32f::new(w, h); + for (i, p) in img.data.iter_mut().enumerate() { + let sigma = if i % w < quiet { 0.001 } else { 0.015 }; + let y = 0.04f32; + let (r, b) = (y + noise() * sigma, y + noise() * sigma); + *p = [r, (y - 0.2627 * r - 0.0593 * b) / 0.6780, b]; + } + img +} + /// A crop of no width used to give grain NaN coordinates (and NaN pixels); the render stays finite. #[test] fn zero_width_crop_with_grain_renders_finite_pixels() { diff --git a/crates/pipeline/src/tests_nr_scale.rs b/crates/pipeline/src/tests_nr_scale.rs index 3247422fa5..c7dd86db2b 100644 --- a/crates/pipeline/src/tests_nr_scale.rs +++ b/crates/pipeline/src/tests_nr_scale.rs @@ -70,14 +70,14 @@ fn rms_chroma(a: &Rgb32f, b: &Rgb32f) -> f32 { fn preview_error(s: &DevelopSettings, scales: &[f32], metric: fn(&Rgb32f, &Rgb32f) -> f32) -> Vec<(f32, f32, f32)> { let src = noisy_scene(0.15); let mut full = src.clone(); - denoise(&mut full, s, 1.0); + denoise(&mut full, s, 1.0, None); scales .iter() .map(|&k| { let reference = scaled(&full, k); let plain = scaled(&src, k); let mut preview = plain.clone(); - denoise(&mut preview, s, k); + denoise(&mut preview, s, k, None); (k, metric(&preview, &reference), metric(&plain, &reference)) }) .collect() diff --git a/crates/pipeline/src/tests_window.rs b/crates/pipeline/src/tests_window.rs index 0d7508bc20..e5fb6ea801 100644 --- a/crates/pipeline/src/tests_window.rs +++ b/crates/pipeline/src/tests_window.rs @@ -422,6 +422,46 @@ fn auto_mask_strokes_match_the_whole_render() { check_noisy("auto mask", &s, PixelWindow { x: 400, y: 300, w: 300, h: 250 }, 2, 0.1); } +// Given the joint colour NR, a window is filtered with the whole frame's noise model: a quiet part of +// the frame does not make a window of its noisy part (or the other way round) smooth differently +#[test] +fn joint_colour_nr_measures_the_noise_of_the_whole_frame() { + let mut s = DevelopSettings::default(); + s.detail.nr_color = 25.0; + s.detail.nr_color_smoothness = 0.0; + // a frame that is one noise tile, and one of many tiles (`nrc_joint::tiles`) + for (w, h, win) in [(768, 128, PixelWindow { x: 640, y: 32, w: 64, h: 64 }), (1664, 1536, PixelWindow { x: 1400, y: 700, w: 128, h: 128 })] { + let src = crate::tests::colour_noise_scene(w, h, w * 2 / 3); + let req = RenderRequest::fit(w, h); + let f = render(&src, &SourceInfo::default(), &s, &req).image; + let part = render(&src, &SourceInfo::default(), &s, &RenderRequest { window: Some(win), ..req }).image; + let (max, mean) = compare_inset(&f, &part, win, win.w / 4); + assert!(max <= 2 && mean <= 0.1, "{w}×{h}: max {max}, mean {mean:.3}"); + } +} + +// Given a 16-bit or float render of a window with the joint colour NR, then its edges are the whole +// render's too: the window is grown by the filter's reach and cut back +#[test] +fn a_deep_window_with_colour_noise_reduction_matches_the_whole_render() { + use crate::OutputDepth; + let (w, h, win) = (768, 128, PixelWindow { x: 600, y: 32, w: 96, h: 64 }); + let src = crate::tests::colour_noise_scene(w, h, w * 2 / 3); + let mut s = DevelopSettings::default(); + s.detail.nr_color = 25.0; + s.detail.nr_color_smoothness = 0.0; + for depth in [OutputDepth::U16, OutputDepth::F32Linear] { + let req = RenderRequest { depth, ..RenderRequest::fit(w, h) }; + let deep = |req: &RenderRequest| render(&src, &SourceInfo::default(), &s, req).deep.map(|d| d.to_rgba8()); + let (Some(f), Some(part)) = (deep(&req), deep(&RenderRequest { window: Some(win), ..req })) else { + panic!("{depth:?}: no deep image"); + }; + assert_eq!((part.width, part.height), (win.w, win.h), "{depth:?}: the window's own size"); + let (max, mean) = compare_inset(&f, &part, win, 0); + assert!(max <= 1 && mean <= 0.01, "{depth:?}: max {max}, mean {mean:.3}"); + } +} + /// The margin the loupe's windows carry around what they show at this size (`region::margin_for`: /// 4.5 % of the long edge). const OVERLAY_INSET: usize = W * 45 / 1000; diff --git a/docs/nr-color-joint.md b/docs/nr-color-joint.md new file mode 100644 index 0000000000..d31c598e17 --- /dev/null +++ b/docs/nr-color-joint.md @@ -0,0 +1,64 @@ +# Colour NR: joint guide + +From process V6 (`docs/process-versions.md`) colour noise reduction uses this method on the CPU +(`crates/pipeline/src/nrc_joint.rs`) and the GPU (`crates/gpu/src/wgsl/nrcj.wgsl`, in bands of rows +whose halo covers the filter's reach). Edits on V1 and V2 keep the blurred chromaticity. The history +of the method, including the square-root and per-channel variants it replaced, is in +`docs/nr-color-v2-history.md`. + +## Method + +- Filter signed, linear `R-Y` and `B-Y`, with original Rec.2020 luminance restored at each + pixel. No channel clipping, root transform or Jensen correction is used in this filter. +- Estimate Y/chroma noise from median squared high-pass residuals at spacings 1, 2 and 4. + Larger spacings account for positive correlations from demosaicing. Fit equal-count + luminance bands with nonnegative coefficients, reject texture-dominated outliers and + stop extrapolating beyond the bands supported by usable noise measurements. +- Speck brake (before the guide): a pixel whose chroma stands out from the median of the 8 + pixels at distance 2 by more than a threshold (in noise variances), whose luminance step is + small against that colour step, and whose colour shift does not reach the 16-pixel border of + its 5 × 5 square at two places (a line or an edge through it always does) gets its chroma moved + towards that median. + Threshold 64 and at most half at Color 25, threshold 25 and fully at 100. Luminance is never + changed, so a bright point (a star) keeps its brightness and, by the luminance test, its colour. +- Make a fixed, lightly denoised guide. At a clear directional edge, average along the + smoother axis; this keeps thin isoluminant lines visible to the guide. +- Apply conservative, symmetric colour exchanges at increasing spatial scales. The two + chroma components have identical neighbour weights. Subtract the expected noise from + guide distances; luminance texture cannot simply increase the raw-pixel retention factor. + Long taps also check the guide edges between their endpoints. +- Symmetric exchanges preserve channel sums on the full-resolution grid, including its + borders. This does not promise identical means inside arbitrary cropped regions. +- Colour amount controls the radius and ramps the filtered contribution continuously from + zero to the normal default of 25. Colour Detail changes edge tolerance; Smoothness changes + radius (`local::nrc_params`: the coarse reach is three times `(2 + 10 · Color) · (1 + Smoothness)` + px at full size, scaled with the output). +- Windowed 8-bit renders request extra surrounding pixels for the diffusion's spatial + dependencies. The existing window/full-frame comparison retains its original tolerance. +- The noise model belongs to the whole frame, not to the pixels a render holds + (`lightcraft_pipeline::colour_noise`). A frame larger than 12 × 12 tiles of 128 px is measured + on that grid of tiles spread over it (`nrc_joint::tiles`), resampled, tone-mapped, white-balanced and + luminance-denoised like the frame (retouching and defringe are left out); a smaller frame + rendered whole is measured on itself, a window of it on the whole frame. So a loupe window + is filtered like the same pixels of the full render or the export + (`joint_colour_nr_measures_the_noise_of_the_whole_frame`). The model is kept in the stage + cache with the image before the noise reduction: moving a colour slider does not measure again. +- The stage cache keys the denoised image by the colour filter in use, so a change of process + (Update to Current Process, undo) never reuses the other filter's result + (`a_process_change_does_not_reuse_the_other_colour_filter`). + +## Local checks + +```sh +cargo test --release -p lightcraft-pipeline --lib nrc_joint +cargo test --release -p lightcraft-pipeline --lib probe_nrc -- --ignored --nocapture +cargo test --release -p lightcraft-gpu --test equivalence +LIGHTCRAFT_GPU=0 cargo run --release -p lightcraft-cli -- render INPUT.ARW -o OUTPUT.png --set detail.nrColor=100 +``` + +`LIGHTCRAFT_PROFILE=1` prints the filter's steps and times and the fitted Y/R-Y/B-Y model. + +The regression tests cover signed dark means, luminance, chroma-noise reduction, leaf/black and +isoluminant edges, a two-pixel isoluminant line, structured highlights, spatially correlated noise, +the speck brake (specks removed, stars kept) and the routing by process (`colour_nr_follows_the_process`). +`nrc_joint_reference.rs` keeps an earlier version of the filter for bit-for-bit checks of later speedups. diff --git a/docs/nr-color-v2-history.md b/docs/nr-color-v2-history.md new file mode 100644 index 0000000000..5f92ab806e --- /dev/null +++ b/docs/nr-color-v2-history.md @@ -0,0 +1,183 @@ +# Colour noise reduction V2: what was tried, measured and dropped + +Prototype branch `nr-color-v2` for storytold/lightcraft#815 (dark-scene colour noise also in #820). This file +records every approach that was tried, including the ones that failed on quality or speed, so later work does +not repeat them. Method details of the current version: [`nr-color-joint.md`](nr-color-joint.md). All numbers +are our own measurements (October 2026); test files: the two ILCE-7M4 raws from #820 ("candle", ISO 3200; +"cat", ISO 25600) and own Sony α77 II / α99 II raws. + +## Summary + +| Version | Idea | Outcome | +|---|---|---| +| main (V1) | chroma as `rgb / Y`, plain Gaussian, mixed in by `t = amount·(1 − detail/2)` | weak at 25, colour fringes at 100, blows up near black | +| V2 "root" | `√(c+off) − √(Y+off)`, self-guided filter, ε from a global 20 % quantile | strong at 25, but **shifts dark-area colour** (clipping), loses edge chroma at ISO 25600, preview ≠ export | +| V2 "linear" | `c − Y`, additive, noise model `σ² = α·Y + β` | mean colour fixed, but **desaturated textured colour** (−48 % leaf chroma at 100) | +| **V2 "joint" (current)** | linear R−Y / B−Y, shared edge-aware weights, multi-scale diffusion with path maxima, measured noise | colour kept, no fringes; on GPU 0.2–0.4 s on a current card; known limits below | + +## 1. V2 "root" + +- Reviewer measurements (#815): blue −25…−45 % in dark patches already at 25, green/cyan cast at ISO 25600, + up to −57 % chroma at colour edges at ISO 25600, the blue shift flips sign between 1600 px and full size. +- **Jensen correction** (add the removed variance back when squaring): only ~3 points better + (−37.6 → −34.4 %). Not the main cause. +- **Float test without 8-bit clipping** (`probe_nrc_mean_drift`): a channel near 0 gains **+90 %** + (0.00080 → 0.00152), almost exactly what rectifying negative noise gives (0.00165). Cause: `v.max(0.0)` in + `nrc_chroma` and `.max(0.0)` in `nrc_recombine`. +- **Measurement pitfall:** comparisons against an 8-bit Color 0 render are biased, because that reference is + itself clipped (a channel near 0 is lifted by rectified noise). Re-rendering at +2.5 EV does not remove the + bias (noise and mean scale together). Check means in float. + +## 2. V2 "linear" (`c − Y`) + +Mean kept within 0.05 % in float (3 seeds × 2 patches × 2 strengths). Noise model fitted per channel on +equal-count luminance bands with a truly non-negative fit (`fit_nonneg_line`). Synthetic checks +(`probe_nrc_real_colour`): an isoluminant red/green edge, a 2 px red line on grey, saturated blue near black, +a flat grey patch, later a bright leaf next to black. + +| Tried | Result | +|---|---| +| per channel, K 16 | dark blue 99 % kept (root: 69 %), but the thin line only 27 % (root: 78 %); compare only at equal residual noise | +| **shared weights** (one `a` for all channels) | **worse** than per channel: edge error 12.7 % vs 9.6 %, line 52 % vs 59 % at equal noise | +| luminance guide in the shared mode | no measurable effect (the test line is isoluminant on purpose) | +| coarse pass with the same K | destroys thin lines (94 % without coarse pass, but too much residual noise) | +| coarse pass with K × 0.1 | synthetic fine, **real ISO 25600 file far too weak** (spread 13.3 vs 6.0): real noise is blotchy after demosaicing | +| coarse pass smoothing only the coarse part | line survives (75–86 %); the cat needs K 128 to match root V2, then the line drops to 37 % | +| **per channel, K 64, coarse-only (shipped in a test package)** | **failed:** lit leaves at Color 100 keep only **52 %** of their chroma (V1 96 %, root 98 %); the synthetic leaf test (95 %) did **not** catch it | +| luminance guide per channel | leaves 92 %, but flat dark areas less smoothed (cat 10.6 vs 6.0): luminance noise counted as structure | + +Pitfalls of this phase: flat-patch selection used interleaved columns instead of spatial quadrants (fixed); +environment knobs read per pixel made the linear mode 11× slower (fixed: read once); a CLI not rebuilt after +changing defaults measured the old mode; renders without the #858 fix showed white dots in every variant; +background load from other jobs made single timings useless (compare in one process, interleaved). + +## 3. V2 "joint" (current) + +- Results at Color 25 / 100 (cat: spread / blotch in flat dark patches; candle: chroma of the lit leaves): + + | | Cat 25 | Cat 100 | Leaves 25 / 100 | + |---|---|---|---| + | V1 | 14.8 / 5.5 | – | 100 % / 96 % | + | root V2 | 5.1 / 1.8 | 3.4 / 0.6 | 99 % / 97 % | + | **joint** | 6.5 / 2.8 | 6.1 / 2.4 | 97 % / 97 % | + +- Dark-area mean colour against the Color 0 render (8-bit, see the pitfall above): cat R −14 % at 25 and 100 + (V1 −13 % at 25, −51 % at 100; root V2 −43 %). +- `tests_window::noise_reduction_does_not_depend_on_the_window` passes (root and linear V2 failed it). + +### Known limits + +- Specks that the speck brake leaves (section 3a): ones with their own luminance step (they stay as + grey or bright dots, as in V1) and, at Color 25, all but the strongest (on purpose, see below). +- Flat dark areas are smoothed somewhat less than by root V2 (cat spread 6.5 vs 5.1 at 25). +- The Color slider changes little above 25 (the blend is complete at 25; only the reach grows). To us 25 already + removes the visible colour noise. +- Prototype switches (`NRV2_*`, `LIGHTCRAFT_NR_V2`) instead of a process version; debug output; a test-only copy + of an earlier version (`nrc_joint_reference.rs`) for bit-exactness checks. + +## 3a. Speck brake + +Isolated, strongly coloured pixels in dark areas (a red, a blue speck) were treated as real detail by the +edge-aware weights and stayed visible at Color 100, while V1's blind blur largely removed them. Measured on +dumps of the transformed image (`NRV2_DUMP`) of the two #820 files: + +- The specks are not single pixels at ISO 25600: demosaicing spreads them over 2 × 2 to 3 × 3 pixels. A first + test "no direct neighbour shares the colour shift" therefore protected them; the brake now asks whether the + shift reaches the 16-pixel border of the 5 × 5 square (any thin line or edge through the pixel does). +- In the centre of the remaining cat specks the chroma excess is 250–1500 noise variances, the luminance step + 0.02–0.17 of it, the border share ≤ 0.4: all three tests pass clearly. What stays visible afterwards is a grey + or bright dot (luminance noise, as in V1). +- Real noise has far heavier tails than a Gaussian: 570 000 pixels of the candle exceed 36 (a Gaussian: 0.5). + A threshold of 12 at Color 100 also moved the mean of a synthetic flat patch (2.2e-6, the test allows 2e-6); + now 64 at 25 and 25 at 100, at most half the colour replaced at 25. + +- First compactness rule "the largest border share stays below 0.6": one noisy border pixel leaning the same + way protected a speck (the three dots below a lit leaf tip in the candle file: border shares 0.44 and 0.71 + kept two of them). Now the **second** largest share decides (0.5 → full brake, 0.8 → none): a line or an + edge through the pixel always crosses the border at least twice. + +| Full-size, 8-bit, against the same build without the brake | Joint | with brake | +|---|---|---| +| Leaves (candle), C* at 25 / 100 | 97 % / 97 % | 97 % / 98 % | +| Cat dark mean R / G / B at 100 | −14.1 / +1.2 / +6.7 % | −11.9 / +2.0 / +2.9 % | +| Candle dark mean B at 25 / 100 | −26.1 / −26.0 % | −26.4 / −29.8 % | +| Cat flat dark spread / blotch at 25 | 6.46 / 2.78 | 5.81 / 2.57 | +| Cat flat dark spread / blotch at 100 | 6.11 / 2.37 | 4.17 / 1.47 | +| Candle flat dark spread / blotch at 100 | 4.67 / 3.13 | 4.10 / 2.97 | + +Crops of the 10 most visible specks per file: blue specks on black become grey at 100, a magenta speck in +orange almost disappears, the three dots below the candle's leaf tip go except for faint dark traces +(luminance); at 25 they are only weakened. The largest changes in bright areas are specks too +(pink dots in the cat's blue, yellow dots in cream); no edge or line changed visibly. Synthetic test +`speck_brake_removes_compact_colour_specks_but_not_stars`: an isoluminant 2 × 2 speck loses most of its colour +at 100, less at 25; a coloured 2 × 2 star keeps its colour within 2 %. + +Cost (machine under heavy load, so rough): CPU 24 MP synthetic 0.54 s against 4.1 s for the rest of the filter +(a sort per median: 1.8 s; now a sorting network and the luminance median only past the threshold). GPU +(Intel HD 630, 33 MP, white balance + NR stage): median 6.19 s with, 5.61 s without. GPU and CPU agree to 1/255 +on the cat; on the candle 26 pixels differ by up to 30/255 with and without the brake (not caused by it). + +## 4. Speed + +CPU, all bit-identical to the first joint version (`8b89526d`): + +| Change | Result | +|---|---| +| no duplicate distance pass, paths + next weights in one pass, none after the last scale, two-channel buffers, parallel reconstruction | 6.2 → 5.0 s (33 MP), buffers 2.36 → 1.44 GB | +| separate H/V slice loops, zero-difference shortcut (no division, weight 1 without `exp`) | 24 MP synthetic 4.5 → 2.7 s | +| **cache-blocked H→V→H→V per band** (bands 4–256 rows, deltas up to 2/4/8) | **no gain** (2.77–3.22 s), slower at delta 8; dropped | +| remaining cost | next-scale weights (`exp` + distance), hard to reduce without changing results | + +GPU (`crates/gpu/src/wgsl/nrcj.wgsl`, kernels mirror the CPU steps, the noise model is fitted on the CPU from +GPU samples; at most 1/255 from the CPU): + +| Tried | Result | +|---|---| +| a new path/weight buffer per scale, only `flush` | out of device memory, whole render fell back to the CPU | +| two ping-pong buffers + `flush` | still out of memory: **3950 MiB real use** (nvidia-smi) while lightcraft counted 1.9 GB – the driver frees dropped buffers only when the GPU is done with them | +| **`sync` after large drops** | fixes it (3.27 GB one piece, 2.4 GB in bands) | +| bands with halo 4·Σδ + 8 | correct but slow at small budgets; now 2·Σδ + δmax + 8 (bands vs one piece: ≤ 1/255 in a few dozen pixels, from the box blur's running sums) | +| band budget | free device memory where it can be asked without `unsafe` (nvidia-smi; amdgpu sysfs) + own pooled buffers − 384 MB, else 2 GB | + +Measured (white balance + noise reduction stage on the GPU, median of 3; CPU: colour NR only): + +| | 24 MP | 33 MP | 43 MP | preview 2560 px | +|---|---|---|---|---| +| Radeon RX 9070 XT (Windows) | 0.19 s | 0.28 s | 0.34 s | 55 ms | +| Quadro M2200 4 GB (Linux) | 1.07 s | 1.54 s | 2.01 s | 0.2 s | +| Intel HD 630 | 3.0 s | 4.3 s | 5.6 s | | +| CPU Ryzen 7 5800X3D | 1.5 s | 2.3 s | 2.9 s | | +| CPU i7-7820HQ | 2.6 s | 3.7 s | 4.8 s | | + +V1 on the same Radeon: 0.08–0.12 s. A Windows test build made with MinGW was ~3× slower than Linux in the +camera-colour step (not in the noise reduction); the official MSVC release still has to be compared. + +### Second round (after the speck brake; 33 MP candle at Color 100, median of 3–6, interleaved) + +| Change | Result | +|---|---| +| guide as three planes, distances one row at a time without branches (the compiler vectorises the three divisions) | CPU next-scale weights ~360 → ~190 ms per scale; bit for bit (test `distance_run_matches_distance`) | +| paths stored as exp(−path): longer paths take minima, a new distance of 0 needs no `exp` (exp is monotonic, so bit for bit) | alone no measurable gain on the CPU (distance, not `exp`, was the cost); kept, it halves the GPU path buffers | +| pilot straight into the planes; speck brake in place (it changes < 1 % of pixels) with a min/max pre-test before the medians | brake 492 → 252 ms; pilot + planes 713 → 568 ms | +| vertical diffusion in one loop, horizontal interior split from the borders | **no gain** (3.59 vs 3.54 s): the diffusion is memory-bound (~24 bytes per pixel and pass); dropped | +| skipping the `exp` also where the new distance is below the path's | counted: only 2–24 % of pairs beyond the 40–67 % with distance 0, ~15 ms per scale for one more buffer; not done | +| GPU: separate path and weight pairs (2 + 2 instead of two 4-value quads), next paths into the free scratch buffer | 60 → 48 bytes per pixel planned; 33 MP fits in one piece in 2 GB (before: two bands with 328-row halos) | +| GPU: medians and pilot without private arrays indexed in loops (those go to slow local memory) | speck brake on the Quadro 0.33 → 0.05 s; pilot ~2 % | +| GPU: dropping the `sync` calls | Quadro: out of device memory (4032 of 4096 MiB), CPU fallback; kept | +| noise model: fit cached for identical samples (moving a slider); compact key sort | fit 13–37 ms (noisy); helps repeated previews only | + +Totals, white balance + NR stage (GPU) / colour NR (CPU), before → after this round: + +| | full 33 MP | preview 2560 px | +|---|---|---| +| Quadro M2200 | 2.15 → 1.47 s | 323 → 248 ms | +| Intel HD 630 | 5.66 → 4.26 s | 818 → 776 ms | +| CPU i7-7820HQ | 5.17 → 3.54 s | | + +CPU output is bit for bit the same as before; GPU output agrees with the previous GPU build to 1/255. + +## 5. Not part of this work + +- White dots near black in #820 came from the camera look's hue/saturation table, fixed by #858. +- A raw-level hot pixel filter was prototyped on a separate branch; the #820 dots were not hot pixels (their + positions don't repeat between frames), so it is not part of this change. diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 99381ec5f5..49bfe4ec9c 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -207,7 +207,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | LR-EDIT-EFFECTS-GRAIN | Grain | P1 | ✅ | `ctl:grain.*` | | | LR-EDIT-DETAIL-SHARPEN | Sharpening | P0 | ✅ | `ctl:detail.sharpenAmount`, `ctl:detail.sharpenRadius`, `ctl:detail.sharpenDetail`, `ctl:detail.sharpenMasking`, `crates/pipeline/src/visualize.rs` | Process V3: pixel-scale sharpening, working Radius and Detail, original-photo scale in previews and exports; V1/V2 preserve the previous rendering (`crates/pipeline/src/tests_sharpen.rs`). Alt/Option-drag previews: Amount greyscale, Radius / Detail the detail band sharpening amplifies, Masking the mask (white = sharpened) | | LR-EDIT-DETAIL-NR | Luminance noise reduction | P0 | ✅ | `ctl:detail.nrLuminance`, `ctl:detail.nrDetail`, `ctl:detail.nrContrast` | radii in the original's pixels, so grid, loupe and smaller exports look like the full-size render scaled down (`local::nr_params`, `Plan::detail_scale`, #960). Alt/Option-drag on Luminance, Detail and Contrast shows the photo in greyscale | -| LR-EDIT-DETAIL-CNR | Colour noise reduction | P0 | ✅ | `ctl:detail.nrColor`, `ctl:detail.nrColorDetail`, `ctl:detail.nrColorSmoothness` | preview-size independent like luminance NR (#960) | +| LR-EDIT-DETAIL-CNR | Colour noise reduction | P0 | ✅ | `ctl:detail.nrColor`, `ctl:detail.nrColorDetail`, `ctl:detail.nrColorSmoothness` | preview-size independent like luminance NR (#960). From process V6 the joint method (#815, `docs/nr-color-joint.md`): linear R−Y / B−Y, edge-aware weights from a denoised guide, a speck brake; acts clearly at the default 25 and keeps colour edges at 100. CPU and GPU within 1/255 | | LR-EDIT-DETAIL-DENOISE | AI denoise | P2 | 🟡 | cmd:denoise.toggle, cmd:denoise.models.install, cmd:denoise.queue, ctl:enhance.denoise, `crates/denoise/`, `docs/denoise.md` | non-destructive Bayer RAW, works out of the box with a built-in classical denoiser (block-matching collaborative filtering, `crates/denoise-core/src/builtin.rs`; its filter runs on the graphics card when there is one that is faster, `crates/gpu/src/bm3d.rs`), optional user-installed weights, pure-Rust CPU/GPU; GPU convolution uses independently written shared scalars (Metal equivalence regression, issue #479); tiles can also run on another computer's graphics card (`lightcraft-cli denoise-serve`, `crates/denoise/src/remote.rs`); X-Trans/linear RGB, model policy and Lightroom fidelity remain; there is no model set-up step to abandon (#922) | | LR-EDIT-DETAIL-RAWDETAILS | Improved demosaic toggle | P2 | ⬜ | | | | LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | | @@ -804,6 +804,7 @@ Latin stays upright. Advanced Japanese composition remains open. Regression cove |---|---|---| | 2026-11-04 | minor | LR-IMP-FORMATS / LR-IMP-CAMERA-COVERAGE: Olympus compressed ORF bitstream decoder implemented (`crates/raw/src/vendor/orfc.rs`); OM-D E-M1 and E-M10 Mark III verified on CC0 files; 58 bodies newly unlocked (78 total across Olympus and OM) | | 2026-10-28 | minor | KEY-HIDEPINS ✅ → 🟡: in the Masking panel an unmodified H toggles the mask pins (`view.maskPins`, the View ▸ Show Mask Pins switch) instead of opening Remove; the toggle yields only to an explicit assignment of H itself, a Remove key reassigned to anything else still opens Remove there, and outside Masking — and the full-screen preview, where no panel shows — H keeps its own binding (Remove, by default). Remove's spot pins keep ⇧H as their only hide toggle (`tests/keymap` `h_toggles_the_mask_pins_while_masking`, `a_remapped_remove_key_still_opens_remove_while_masking`, `h_still_toggles_the_pins_when_remove_moves_to_another_key`) | +| 2026-11-06 | minor | LR-EDIT-DETAIL-CNR, LRC-DEV-PROCESS: process V6 takes colour noise reduction to the joint method on linear R−Y / B−Y (issue #815); status unchanged | | 2026-11-05 | minor | LR-BEHAV-RENDER-FIDELITY, LRC-DEV-PROCESS: process V5 rolls a raw's tone curve off into white in stops, past a camera curve's last knot and from half a stop below the generic curve's white point, so raising Exposure no longer turns recorded highlights white (#1632); statuses and counts unchanged | | 2026-11-05 | minor | LR-LIB-PEOPLE: face boxes another app drew on the photo as stored land on the upright photo (#2674) | | 2026-11-02 | minor | LRC-DEV-AUTOSYNC (N-A3, note 18): Auto Sync carries white balance as a mode or on each photo's own scale: As Shot and Auto per photo (were the active photo's numbers), a Temp move keeps each photo's tint, the same number on photos of the same scale, a photo of another scale (a JPEG beside a raw) moves by as much; Sync Settings unchanged; status unchanged | diff --git a/docs/process-versions.md b/docs/process-versions.md index 5a96152f79..7d1ae1927c 100644 --- a/docs/process-versions.md +++ b/docs/process-versions.md @@ -13,7 +13,8 @@ stronger Light sliders, issue #196) without changing a single photo someone has | **V2** | Highlights and Shadows keep fine detail (issue #632): their base layer (`local::tone_base`) is the edge-aware filter of a separable median of log luminance (radius 0.15 % of the long edge, at most 24 px, `local::BASE_DETAIL`) instead of log luminance itself, so twigs, lines and texture take their surroundings' shift and keep their contrast; region edges stay sharp. CPU (`local::prepare`) and GPU prepare branch on `local::tone_base_detail` (0 = no median under V1); the base plane is cached per process (`local::BaseKey`). Renders without Highlights or Shadows (global or local) are the same as V1's. Tone map unchanged. | | **V3** | Pixel-scale sharpening (issue #757): Radius uses the original photo’s pixels, Detail controls halo damping, and downsized sources retain a response-matched sharpening band. V1 and V2 keep texture-band sharpening exactly. The sharpen plane is keyed by radius and process and invalidated after noise-reduction changes. The Highlights/Shadows base and tone map are unchanged from V2. | | **V4** | V3 plus calibrated global Contrast (per channel) and Blacks (per-channel/luminance mix) on raws with a camera tone curve. Whites behaves exactly as V2, both signs; its calibration is a planned follow-up. Other sources and local adjustments retain V2 operators. See [calibrated Light operators](light-v3.md). | -| **V5** (the latest) | V4 plus: a raw's tone curve rolls off into white in stops instead of reaching it within about a stop of the sensor's white, so raising Exposure keeps the highlights the sensor recorded (issue #1632). **Camera curve** (the look fitted to the embedded JPEG, or a DNG's `ProfileToneCurve`; `CameraTone`): past its last knot `CameraTone::apply_rolloff` closes the gap to white by `gap / (1 + c·(2^t − 1))` at `t` stops past the knot, with `c` matching the curve's slope there and the gap halving every stop in the end (`tone::ROLLOFF_STOPS`); `ToneMap::camera_rolloff_hdr` builds the table from it. Under V1 to V4 the curve past its last knot (fitted only to camera JPEG tones below 0.85, so 1 to 3 stops below the sensor's white on most photos) closes on white exponentially in linear light: the sensor's white renders at 8-bit 246 to 254 at Exposure 0 and 254 to 255 from Exposure +1. On the 11 CC0 corpus files with a fitted look (8 Sony, 2 Nikon, 1 Fujifilm) it now renders at 239 to 250 at Exposure 0 and 247 to 253 at +1. A DNG curve that reaches white at the sensor's white renders as before. **Generic curve** (raws without a camera curve, `ToneMap::rolloff_hdr`): from half a stop below its white point (`tone::GENERIC_KNEE_STOPS`) the same shoulder takes over, with the extended Reinhard curve's value and slope at the join, where V1 to V4 clip at the white point (scene 1.18 at default settings, ¼ stop above the sensor's white). The sensor's white renders at about 235, 248 and 252 at Exposure 0, +1 and +2 (V1 to V4: 239, 255, 255; the CC0 Canon R8 file and 6 CC0 DNGs without a camera curve). Content past the old clip point, such as a clipped sky, now renders up to 15 codes below white at Exposure 0 and reaches 255 about 5 stops past the sensor's white. Below the last knot or the join, JPEGs and other display-referred sources, and the camera fit itself are unchanged; CPU and GPU share the tone LUT. | +| **V5** | V4 plus: a raw's tone curve rolls off into white in stops instead of reaching it within about a stop of the sensor's white, so raising Exposure keeps the highlights the sensor recorded (issue #1632). **Camera curve** (the look fitted to the embedded JPEG, or a DNG's `ProfileToneCurve`; `CameraTone`): past its last knot `CameraTone::apply_rolloff` closes the gap to white by `gap / (1 + c·(2^t − 1))` at `t` stops past the knot, with `c` matching the curve's slope there and the gap halving every stop in the end (`tone::ROLLOFF_STOPS`); `ToneMap::camera_rolloff_hdr` builds the table from it. Under V1 to V4 the curve past its last knot (fitted only to camera JPEG tones below 0.85, so 1 to 3 stops below the sensor's white on most photos) closes on white exponentially in linear light: the sensor's white renders at 8-bit 246 to 254 at Exposure 0 and 254 to 255 from Exposure +1. On the 11 CC0 corpus files with a fitted look (8 Sony, 2 Nikon, 1 Fujifilm) it now renders at 239 to 250 at Exposure 0 and 247 to 253 at +1. A DNG curve that reaches white at the sensor's white renders as before. **Generic curve** (raws without a camera curve, `ToneMap::rolloff_hdr`): from half a stop below its white point (`tone::GENERIC_KNEE_STOPS`) the same shoulder takes over, with the extended Reinhard curve's value and slope at the join, where V1 to V4 clip at the white point (scene 1.18 at default settings, ¼ stop above the sensor's white). The sensor's white renders at about 235, 248 and 252 at Exposure 0, +1 and +2 (V1 to V4: 239, 255, 255; the CC0 Canon R8 file and 6 CC0 DNGs without a camera curve). Content past the old clip point, such as a clipped sky, now renders up to 15 codes below white at Exposure 0 and reaches 255 about 5 stops past the sensor's white. Below the last knot or the join, JPEGs and other display-referred sources, and the camera fit itself are unchanged; CPU and GPU share the tone LUT. | +| **V6** (the latest) | V5 plus: colour noise reduction filters the linear colour differences R−Y and B−Y with the joint method (issue #815, `docs/nr-color-joint.md`): edge-aware weights from a denoised guide shared by both differences, anisotropic diffusion over scales, a brake for isolated colour specks; Y is kept exactly. V1 to V5 keep the blurred chromaticity (`local::denoise`). CPU (`local::denoise`) and GPU (`render::denoise`) branch on `local::joint_colour_nr`. Renders with Color 0 are the same as V2's. | ## The rule diff --git a/docs/whats-new.md b/docs/whats-new.md index c0b75f74c6..ab0f90c033 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -2,6 +2,13 @@ ## Unreleased +### Colour noise reduction (process V6) +- Colour noise reduction removes colour noise at the raw default 25, where it used to leave most of it, and keeps + colour edges at high values instead of bleeding them into fringes (issue #815). It filters the colour differences + R−Y and B−Y, leaving brightness exactly as it was, along edges found in a denoised guide, and takes isolated + coloured specks out. New photos get it; photos edited before keep their look until Update to Current Process. A zoomed + view is smoothed like the same part of the whole photo and the export. + ### The web build can be embedded - A page framing LightCraft in an iframe can open it with `?host=parent` and speak to it over `postMessage`, the protocol PdfCraft, GridCraft and DeckCraft share: it hands photos in, receives exports and backups instead of diff --git a/tools/nrc-metrics.py b/tools/nrc-metrics.py new file mode 100644 index 0000000000..2849faf6f4 --- /dev/null +++ b/tools/nrc-metrics.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Compare colour NR exports using spatial quadrants and one fixed reference mask. + +PNG comparisons assess the displayed result, not an unclipped float ground truth. +Usage: python3 tools/nrc-metrics.py REF.png VARIANT.png ... [--crop X Y W H] +""" +import argparse +import json +from pathlib import Path + +import numpy as np +from PIL import Image + + +def linear(p): + p = np.asarray(p, dtype=np.float64) / 255.0 + return np.where(p <= 0.04045, p / 12.92, ((p + 0.055) / 1.055) ** 2.4) + + +def lab(p): + p = linear(p) + xyz = p @ np.array([[0.4124, 0.2126, 0.0193], [0.3576, 0.7152, 0.1192], [0.1805, 0.0722, 0.9505]]) + xyz /= np.array([0.95047, 1.0, 1.08883]) + f = np.where(xyz > 0.008856, np.cbrt(xyz), 7.787 * xyz + 16 / 116) + return np.stack((116 * f[..., 1] - 16, 500 * (f[..., 0] - f[..., 1]), 200 * (f[..., 1] - f[..., 2])), axis=-1) + + +def tiles(reference): + """Four actual 48x48 quadrants; tiles selected only on the reference.""" + result = [] + for y in range(0, reference.height - 95, 192): + for x in range(0, reference.width - 95, 192): + p = linear(reference.crop((x, y, x + 96, y + 96)))[::4, ::4] + luminance = p @ np.array([0.2126, 0.7152, 0.0722]) + mean = luminance.mean() + lightness = 116 * (np.cbrt(mean) if mean > 0.008856 else 7.787 * mean + 16 / 116) - 16 + quadrants = luminance.reshape(2, 12, 2, 12).mean(axis=(1, 3)) + if 6 <= lightness <= 25 and np.ptp(quadrants) < 0.15 * mean: + result.append((x, y)) + return result[:60] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("files", nargs="+") + parser.add_argument("--crop", nargs=4, type=int, default=[1325, 2085, 360, 240]) + args = parser.parse_args() + reference = Image.open(args.files[0]).convert("RGB") + regions = tiles(reference) + x, y, w, h = args.crop + box = (x, y, x + w, y + h) + if x < 0 or y < 0 or w <= 0 or h <= 0 or x + w > reference.width or y + h > reference.height: + parser.error("crop must lie inside the image") + ref_lab = lab(reference.crop(box)) + mask = ref_lab[..., 0] > 20 + if not mask.any(): + parser.error("crop contains no reference pixels with L* > 20") + reference_ab = ref_lab[..., 1:][mask].mean(axis=0) + reference_c = np.linalg.norm(ref_lab[..., 1:][mask], axis=-1).mean() + records = [] + for file in args.files: + image = Image.open(file).convert("RGB") + if image.size != reference.size: + parser.error(f"size differs from reference: {file}") + spreads, blotches, means = [], [], [] + for tx, ty in regions: + patch = np.asarray(image.crop((tx, ty, tx + 96, ty + 96))) + ab = lab(patch)[..., 1:] + spreads.append(np.sqrt(ab.var(axis=(0, 1)).sum())) + blocks = ab.reshape(12, 8, 12, 8, 2).mean(axis=(1, 3)) + blotches.append(np.sqrt(blocks.var(axis=(0, 1)).sum())) + means.append(linear(patch).mean(axis=(0, 1))) + ab = lab(image.crop(box))[..., 1:][mask] + mean_ab = ab.mean(axis=0) + records.append({ + "file": str(Path(file)), "flat_tiles": len(regions), + "spread": float(np.mean(spreads)) if spreads else None, + "blotch": float(np.mean(blotches)) if blotches else None, + "dark_rgb": np.mean(means, axis=0).tolist() if means else None, + "crop_mean_ab": mean_ab.tolist(), + "crop_mean_colour_delta": float(np.linalg.norm(mean_ab - reference_ab)), + "crop_mean_chroma_percent": float(100 * np.linalg.norm(mean_ab) / max(np.linalg.norm(reference_ab), 1e-12)), + "crop_pixel_chroma_percent": float(100 * np.linalg.norm(ab, axis=-1).mean() / max(reference_c, 1e-12)), + "crop_hue_delta_degrees": float(np.degrees(np.arctan2(np.sin(np.arctan2(mean_ab[1], mean_ab[0]) - np.arctan2(reference_ab[1], reference_ab[0])), np.cos(np.arctan2(mean_ab[1], mean_ab[0]) - np.arctan2(reference_ab[1], reference_ab[0]))))), + }) + print(json.dumps({"reference": args.files[0], "crop": args.crop, "results": records}, indent=2)) + + +if __name__ == "__main__": + main()