mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-19 08:51:49 +01:00
* WIP: colour NR v2 prototype (env LIGHTCRAFT_NR_V2, CPU only) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 on by default (LIGHTCRAFT_NR_V2=0 turns it off), GPU renders read back for it Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: cache renders with the new colour NR apart from older ones Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 on the GPU (fused down / apply kernels), coefficient grid sigma / 2 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 on the CPU without full-size intermediates (row-streamed passes) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: report peak device-buffer use per GPU stage with LIGHTCRAFT_PROFILE * WIP: colour NR v2 linear mode (NRV2_LINEAR=1: c - Y, additive, noise model), Jensen term, mean-drift probe Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 noise model with equal-count luminance bands and a non-negative fit; probe asserts the mean over 3 noise realisations Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 linear mode by default (per channel, K 64, coarse pass smooths only the coarse part); CPU on GPU renders; real-colour probe Known: tests_window::noise_reduction_does_not_depend_on_the_window fails (the noise is measured in the rendered window; the square-root mode failed it too). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 joint guide (NRV2_JOINT=1, opt-in), luminance guide knobs, leaf-next-to-black probe, camera-look guard from 858 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: colour NR v2 reads its env knobs once (per-pixel env reads made the linear mode 11x slower); joint stage timings with LIGHTCRAFT_PROFILE Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint colour NR without redundant passes (fused paths + next weights, none after the last scale, delta-1 weights from the path), two-component buffers, parallel reconstruction Bit-identical to8b89526d(test joint_matches_the_reference_bit_for_bit against a test-only copy). Cat 32.7 MP, CPU: 6.2 -> 5.0 s at Color 25, 7.0 -> 5.6 s at 100; working buffers 2.36 -> 1.44 GB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint colour NR with separate H/V slice loops and zero-difference shortcuts (no division, weight 1 without exp) Still bit-identical to8b89526d. Interleaved 24 MP bench: 2.72 s vs 4.52 s for the reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint colour NR on the GPU (nrcj.wgsl), default on; whole-image noise model from samples, halo bands above 2 GB Kernels mirror nrc_joint step for step (same scale plan, noise model fitted on the CPU from GPU samples). Cat 32.7 MP on a Quadro M2200: wb/nr 1.2-1.3 s vs 4-5 s CPU; against the CPU max 1 LSB; bands = single. Waits after dropping large buffers (freed memory otherwise piled up to the whole 4 GB). Cache key includes joint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: GPU joint NR plans its bands from a budget that includes the output buffer and the device buffer limit; halo 2*sum(delta) + max delta + 8; a band that cannot fit runs only this stage on the CPU Bands vs one piece: at most 1 LSB in a few dozen pixels (running-sum boxes round differently per band start). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: GPU joint NR budgets its bands from the free device memory where it can be asked without unsafe (nvidia-smi, amdgpu sysfs), cached 3 s; fixed 2 GB otherwise Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: docs/nr-color-v2-history.md: every approach tried, measured and dropped, speed numbers, known limits (no outlier brake yet) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: history doc: speck behaviour stated qualitatively until measured on full-size renders Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint colour NR speck brake: compact colour specks (chroma far past the noise, small luminance step, shift not reaching the 5x5 border) lose their colour before the guide is built; weak at Color 25, full at 100; CPU and GPU Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint NR CPU 5.2 -> 3.5 s (33 MP), bit for bit: guide as planes with branch-free distance rows, exponentiated paths, pilot straight into planes, speck brake in place with a min/max pre-test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: joint NR GPU: path and weight pairs instead of quads (48 instead of 60 bytes per pixel, 33 MP in one piece), speck and pilot kernels without loop-indexed private arrays, noise fit cached for identical samples; Quadro M2200 2.15 -> 1.47 s, HD 630 5.66 -> 4.26 s Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * WIP: speck brake: compactness from the second largest border share (a line or edge crosses the 5x5 border twice; one noisy border pixel no longer protects a speck) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #815: colour noise reduction as process V3 The joint method (linear R−Y / B−Y, edge-aware guide, speck brake) is process V3 and the latest; edits on V1 and V2 keep main's blurred chromaticity. The prototype switches (LIGHTCRAFT_NR_V2, NRV2_*) and the square-root and per-channel variants they selected are gone, with their GPU kernels; nrc_params uses the former defaults as constants, so V3 renders as the prototype did. The GPU falls back to the CPU joint filter when no band fits. vram is native only (wasm build). Docs: process-versions, nr-color-joint, parity checklist, what's new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #815: process routing test initialises its settings in place (clippy field_reassign_with_default) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #815: one GPU band plan at a time, with a reserve for every render on the device An export batch renders two photos side by side (`export_parallelism`). Each joint colour NR planned its bands from the device memory free at that moment and counted the shared buffer pool as its own, so two plans made at once could together ask for more than there is. Measured on a Quadro M2200 (4 GB): two 33 MP exports side by side peaked at 3761 of 4096 MiB. The plan and its bands now run under one lock (`BANDS`), and the budget leaves `VRAM_RESERVE` for each render on the device (`RENDERS`), not just for this one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #815: the joint colour NR's noise fit and pilot no longer switch at a hard bound With main's preview-scaled luminance NR (#960) the CPU and GPU renders of a 640-px preview differed by up to 10 levels (gpu equivalence `output_spaces_match`). Their images after luminance NR differ by 9e-7 rms, but one luminance band of the noise fit sat at exactly 4 times the first fit: the CPU's samples dropped it, the GPU's kept it, and the fitted noise came out 2.6 % apart. The pilot's axis choice then switched at a few pixels, and the edge-aware weights carried that on. - robust_line: a band's weight fades to 0 between 4 and 5 times the fit instead of dropping at 4 (identical below 4). - pilot_pick: the smoother-axis mean fades in between 0.8 and 1.25 times its bound instead of switching at it (identical outside), on the CPU, in nrcj.wgsl and in the test reference. All 19 CPU/GPU comparisons of the equivalence suite are within 1 level again. The new test noise_line_does_not_jump_when_a_band_crosses_the_outlier_bound uses the bands of that preview and fails on the old fit (5.6 % jump). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * #815: the joint colour NR measures the whole frame's noise and is keyed by process A window measured the noise model on the pixels it held, so a loupe window could smooth differently from the same pixels of the full render or the export. The model now comes from fixed tiles of the whole frame (`lightcraft_pipeline::colour_noise`, `nrc_joint::tiles`), the same for every window, for the CPU and the GPU, and is kept in the stage cache. The linear stage's cache key now includes the colour filter in use, so a process change (Update to Current Process, undo) no longer reuses the other filter's denoised image. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * M-NR: deep windows match the whole render; the VRAM query no longer stalls renders - A U16 / F32 window with the joint colour NR is grown by the filter's reach and cut back, like an 8-bit one (DeepImage::crop). - The free device memory is refreshed in the background once the cached answer is stale; only the first question for an adapter waits. The joint NR asks before it takes the BANDS lock. - nvidia-smi starts without a console window on Windows (CREATE_NO_WINDOW). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * M-NR: the VRAM query finds the right card and keeps one answer per adapter - A card is found by its PCI address (wgpu device_pci_bus_id, nvidia-smi pci.bus_id, the amdgpu sysfs directory) where both sides report one; else by a device id that only one card has. Two cards of one model without an address give no answer, and the fixed band budget applies. - Each adapter keeps its own cached answer, so a late background refresh cannot replace another adapter's answer. A refresh not done after 10 s is retried. - The cache tests wait on signals instead of fixed sleeps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * M-NR: the VRAM cache tests no longer depend on timing The two cache tests used a 20 ms TTL: on a slow machine a refreshed answer could go stale again between assertions and start an extra query. They now use a 30 s TTL and age an answer explicitly (Cache::expire, test only). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GPU: allow joint_band's eight arguments, as color_nr_joint does Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
This commit is contained in:
23 files changed
+3621
-29
No files matched your search
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+28
-1
@@ -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.
|
||||
|
||||
@@ -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};
|
||||
|
||||
+274
-8
@@ -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<Buf>,
|
||||
lin: Option<(u64, Arc<Buf>)>,
|
||||
/// 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<crate::Fault>,
|
||||
display: bool,
|
||||
) -> Option<Output> {
|
||||
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<std::time::Instant>, 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<Buf> {
|
||||
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<f32> = 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<nrc_joint::NoiseSample> = 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::<usize>() + 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::<usize>().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<Buf>,
|
||||
@@ -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};
|
||||
|
||||
@@ -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<u64> {
|
||||
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<Pci>,
|
||||
}
|
||||
|
||||
/// 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<Pci> {
|
||||
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<Pci>,
|
||||
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<u64> {
|
||||
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<u64>,
|
||||
asking: Option<Instant>,
|
||||
}
|
||||
|
||||
type Ask = fn(&Adapter) -> Option<u64>;
|
||||
|
||||
/// 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<Vec<Answer>>,
|
||||
}
|
||||
|
||||
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<u64> {
|
||||
{
|
||||
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<u64> {
|
||||
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<u64>) {
|
||||
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<u64> {
|
||||
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, <MiB>`.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn nvidia(adapter: &Adapter) -> Option<u64> {
|
||||
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<Card> {
|
||||
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::<u64>().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<u64> {
|
||||
let read_hex = |p: &std::path::Path| -> Option<u32> {
|
||||
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<u64> { std::fs::read_to_string(p).ok()?.trim().parse().ok() };
|
||||
let mut cards: Vec<Card> = 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<String> {
|
||||
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<u64> {
|
||||
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<u64> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<f32> {
|
||||
return vec3<f32>(a[3u * i], a[3u * i + 1u], a[3u * i + 2u]);
|
||||
}
|
||||
|
||||
fn jb3(i: u32) -> vec3<f32> {
|
||||
return vec3<f32>(b[3u * i], b[3u * i + 1u], b[3u * i + 2u]);
|
||||
}
|
||||
|
||||
fn ja2(i: u32) -> vec2<f32> {
|
||||
return vec2<f32>(a[2u * i], a[2u * i + 1u]);
|
||||
}
|
||||
|
||||
fn jvc(v: vec3<f32>, 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<f32>, q: vec3<f32>) -> 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<f32> {
|
||||
let c = ja(i);
|
||||
let y = lum2020(c);
|
||||
return vec3<f32>(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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<f32>(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<f32>, s: f32) -> vec2<f32> {
|
||||
return select(vec2<f32>(t.x, max(t.y, s)), vec2<f32>(s, t.x), s > t.x);
|
||||
}
|
||||
|
||||
// One border pixel's share of the colour shift (`speck_share`).
|
||||
fn sshare(q: vec3<f32>, m: vec3<f32>, 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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<f32>(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<f32>(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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<f32>(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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<f32>(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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<u32>, @builtin(num_workgroups) nw: vec3<u32>) {
|
||||
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<f32>(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;
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
@@ -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<Rgb32f> {
|
||||
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| {
|
||||
|
||||
@@ -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<Rgb32f>,
|
||||
lin: Option<(u64, Arc<Rgb32f>)>,
|
||||
/// 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<redeye::EyeK>,
|
||||
/// 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<PixelWindow>,
|
||||
/// The source's segmentation mattes ([`SourceInfo::mattes`]).
|
||||
pub mattes: Option<Arc<masks::Mattes>>,
|
||||
/// 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<nrc_joint::JointNoise> {
|
||||
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<Rgb32f> = 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);
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<NrLum>, Option<NrCo
|
||||
|
||||
/// Noise reduction at output resolution: luminance via an edge-aware self-guided filter on
|
||||
/// log-luminance, colour by blurring chromaticity (rgb / Y) and re-applying the original luminance.
|
||||
/// `scale`: output pixels per pixel of the original ([`nr_params`]).
|
||||
pub fn denoise(img: &mut Rgb32f, s: &DevelopSettings, scale: f32) {
|
||||
/// `scale`: output pixels per pixel of the original ([`nr_params`]). `colour_noise`: the whole
|
||||
/// frame's noise model for the joint colour NR ([`crate::colour_noise`]; `None`: measured on `img`).
|
||||
pub fn denoise(img: &mut Rgb32f, s: &DevelopSettings, scale: f32, colour_noise: Option<&crate::nrc_joint::JointNoise>) {
|
||||
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<NrLum>, col: Option<NrColor>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<NrcPass>,
|
||||
}
|
||||
|
||||
/// 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<NrcParams> {
|
||||
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::<f64>().max(1e-300);
|
||||
let (ml, mv) = (pts.iter().map(|p| p.2 * p.0).sum::<f64>() / sw, pts.iter().map(|p| p.2 * p.1).sum::<f64>() / 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::<f64>();
|
||||
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::<f64>() / 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>) -> 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::<f32>()
|
||||
* 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::<f64>() / 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::<f64>() / 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::<f32>()
|
||||
* 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);
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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::<f64>() / 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<f32> = 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::<f32>()
|
||||
}
|
||||
|
||||
/// 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::<f32>();
|
||||
}
|
||||
*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<std::time::Instant>| {
|
||||
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::<f32>() * 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);
|
||||
}
|
||||
}
|
||||
@@ -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<T: Copy>(v: &[T], width: usize, x0: usize, y0: usize, w: usize, h: usize) -> Vec<T> {
|
||||
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 {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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 |
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user