Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,20 @@ impl ProcessVersion {
pub enum Process {
/// The rendering of LightCraft before process versions existed (2026-10).
V1,
/// Highlights and Shadows keep the local contrast of the tones they move: their edge-aware base
/// is taken of a fine median of log luminance (issue #632).
V2,
}

impl Process {
/// Every process, oldest first.
pub const ALL: [Process; 1] = [Process::V1];
pub const LATEST: Process = Process::V1;
pub const ALL: [Process; 2] = [Process::V1, Process::V2];
pub const LATEST: Process = Process::V2;

pub const fn version(self) -> ProcessVersion {
match self {
Process::V1 => ProcessVersion(1),
Process::V2 => ProcessVersion(2),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl SettingsHashes {
}

/// Bump when the pipeline's output changes, to invalidate cached thumbnails.
pub const RENDER_CACHE_VERSION: u64 = 42;
pub const RENDER_CACHE_VERSION: u64 = 54;

/// Thumbnails render at one of these long edges (so window/cell size changes reuse the cache).
pub const THUMB_SIZES: [usize; 4] = [128, 256, 384, 512];
Expand Down
6 changes: 5 additions & 1 deletion crates/gpu/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const MODULES: &[Module] = &[
],
entries: &["main"],
},
Module { src: include_str!("wgsl/blur.wgsl"), bindings: &[("src", false, "f32"), ("dst", true, "f32")], entries: &["box_h", "box_v"] },
Module {
src: include_str!("wgsl/blur.wgsl"),
bindings: &[("src", false, "f32"), ("dst", true, "f32")],
entries: &["box_h", "box_v", "median_h", "median_v"],
},
Module {
src: include_str!("wgsl/resize.wgsl"),
bindings: &[("src", false, "f32"), ("table", false, "u32"), ("dst", true, "f32")],
Expand Down
47 changes: 31 additions & 16 deletions crates/gpu/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use lightcraft_develop::DevelopSettings;
use lightcraft_geom::Orientation;
use lightcraft_pipeline::finish::{FinishParams, MASK_TERMS, mask_terms};
use lightcraft_pipeline::geometry::SampleMode;
use lightcraft_pipeline::local::plane_at;
use lightcraft_pipeline::{Plan, RenderRequest, Rendered, SourceInfo, local};
use lightcraft_raster::resample::Filter;
use lightcraft_raster::{Histogram, Plane, Rgb32f, Rgba8};
Expand Down Expand Up @@ -40,7 +41,7 @@ struct Entry {
struct Planes {
key: u64,
log_l: Option<Arc<Buf>>,
base: Option<(u32, Arc<Buf>)>,
base: Option<(local::BaseKey, Arc<Buf>)>,
clarity: Option<(u32, Arc<Buf>)>,
texture: Option<(u32, Arc<Buf>)>,
dark: Option<(u32, Arc<Buf>, f32)>,
Expand Down Expand Up @@ -312,6 +313,28 @@ fn guided_coeffs(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps:
gaussian(cx, &ab, w, h, 2, sigma)
}

/// Separable median of a plane (`local::median_hv`).
fn median_hv(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, r: usize) -> Buf {
if r == 0 || w * h == 0 {
return cx.copy(p);
}
let pr = [w as u32, h as u32, 1, r.min(local::BASE_DETAIL_MAX) as u32];
let tmp = cx.gpu.buffer(w * h);
cx.run("median_h", &pr, &[Some(p), Some(&tmp)], groups2(w, h, [16, 16]));
let out = cx.gpu.buffer(w * h);
cx.run("median_v", &pr, &[Some(&tmp), Some(&out)], groups2(w, h, [16, 16]));
out
}

/// The highlights/shadows base (`local::tone_base`; `detail` 0: V1, no median).
fn tone_base(cx: &mut Cx<'_>, l: &Buf, w: usize, h: usize, sigma: f32, detail: usize) -> Buf {
if detail == 0 {
return guided_fast(cx, l, w, h, sigma, local::BASE_EPS);
}
let m = median_hv(cx, l, w, h, detail);
guided_fast(cx, &m, w, h, sigma, local::BASE_EPS)
}

/// Fast guided filter (`local::guided_fast`): coefficients on a subsampled grid.
fn guided_fast(cx: &mut Cx<'_>, p: &Buf, w: usize, h: usize, sigma: f32, eps: f32) -> Buf {
let s = local::guided_fast_step(sigma);
Expand Down Expand Up @@ -805,17 +828,6 @@ struct Prep {
air: f32,
}

fn plane_at(slot: &mut Option<(u32, Arc<Buf>)>, sigma: f32, f: impl FnOnce() -> Buf) -> Arc<Buf> {
match slot {
Some((k, p)) if *k == sigma.to_bits() => p.clone(),
_ => {
let p = Arc::new(f());
*slot = Some((sigma.to_bits(), p.clone()));
p
}
}
}

/// The spatial planes (`local::prepare`), reusing those in `planes`.
fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, planes: &mut Planes) -> Prep {
let (w, h) = (plan.w, plan.h);
Expand All @@ -832,11 +844,14 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla
}
};
let base = match sig.base {
Some(sg) => plane_at(&mut planes.base, sg, || guided_fast(cx, &log_l, w, h, sg, local::BASE_EPS)),
Some(sg) => {
let key = local::base_key(&plan.settings, sg, sig.base_detail);
plane_at(&mut planes.base, key, || tone_base(cx, &log_l, w, h, sg, sig.base_detail))
}
None => log_l.clone(),
};
let clarity = sig.clarity.map(|sg| plane_at(&mut planes.clarity, sg, || guided_fast(cx, &log_l, w, h, sg, local::CLARITY_EPS)));
let texture = sig.texture.map(|sg| plane_at(&mut planes.texture, sg, || gaussian(cx, &log_l, w, h, 1, sg)));
let clarity = sig.clarity.map(|sg| plane_at(&mut planes.clarity, sg.to_bits(), || guided_fast(cx, &log_l, w, h, sg, local::CLARITY_EPS)));
let texture = sig.texture.map(|sg| plane_at(&mut planes.texture, sg.to_bits(), || gaussian(cx, &log_l, w, h, 1, sg)));
let (dark, air) = match sig.dark {
None => (None, 1.0),
Some(sg) => match &planes.dark {
Expand All @@ -858,7 +873,7 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla
},
};
let chroma = sig.chroma.map(|sg| {
plane_at(&mut planes.chroma, sg, || {
plane_at(&mut planes.chroma, sg.to_bits(), || {
let ch = cx.gpu.buffer(n * 3);
map(cx, "chroma_k", n, &[], [Some(lin), None, None], &ch);
gaussian(cx, &ch, w, h, 3, sg)
Expand Down
55 changes: 55 additions & 0 deletions crates/gpu/src/wgsl/blur.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,58 @@ fn box_v(@builtin(global_invocation_id) g: vec3<u32>) {
acc = acc + ld(u32(min(i32(y) + r + 1, last)) * w + x, nc) - ld(u32(max(i32(y) - r, 0)) * w + x, nc);
}
}

// Separable median of a single-channel plane (`lightcraft_pipeline::local::median_hv`): 2r+1
// taps (r ≤ 24, clamped edges) along a row (median_h) or a column (median_v), one thread per
// pixel. The median is a selection, so the result matches the CPU's bit for bit.
// P: w, h, nc (unused), r.
fn median_of(n: u32, r: u32, v: ptr<function, array<f32, 49>>) -> f32 {
// partial selection sort up to the middle
for (var i = 0u; i <= r; i++) {
var m = i;
for (var j = i + 1u; j < n; j++) {
if ((*v)[j] < (*v)[m]) {
m = j;
}
}
let t = (*v)[i];
(*v)[i] = (*v)[m];
(*v)[m] = t;
}
return (*v)[r];
}

@compute @workgroup_size(16, 16)
fn median_h(@builtin(global_invocation_id) g: vec3<u32>) {
let w = pu(0u);
let h = pu(1u);
let r = min(pu(3u), 24u);
if (g.x >= w || g.y >= h) {
return;
}
let n = 2u * r + 1u;
var v: array<f32, 49>;
let row = g.y * w;
for (var k = 0u; k < n; k++) {
let x = clamp(i32(g.x + k) - i32(r), 0, i32(w) - 1);
v[k] = src[row + u32(x)];
}
dst[row + g.x] = median_of(n, r, &v);
}

@compute @workgroup_size(16, 16)
fn median_v(@builtin(global_invocation_id) g: vec3<u32>) {
let w = pu(0u);
let h = pu(1u);
let r = min(pu(3u), 24u);
if (g.x >= w || g.y >= h) {
return;
}
let n = 2u * r + 1u;
var v: array<f32, 49>;
for (var k = 0u; k < n; k++) {
let y = clamp(i32(g.y + k) - i32(r), 0, i32(h) - 1);
v[k] = src[u32(y) * w + g.x];
}
dst[g.y * w + g.x] = median_of(n, r, &v);
}
46 changes: 45 additions & 1 deletion crates/gpu/tests/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use std::sync::Arc;

use lightcraft_develop::{BrushStroke, DevelopSettings, Mask, MaskComponent, MaskOp, MaskShape, Spot, Treatment, VignetteStyle, WbMode, Wheel};
use lightcraft_develop::{
BrushStroke, DevelopSettings, Mask, MaskComponent, MaskOp, MaskShape, Process, Spot, Treatment, VignetteStyle, WbMode, Wheel,
};
use lightcraft_geom::{Orientation, Point, Rect};
use lightcraft_pipeline::{Quality, RenderRequest, SourceInfo, StageCache, render};
use lightcraft_raster::{Rgb32f, Rgba8};
Expand Down Expand Up @@ -550,6 +552,48 @@ fn cached_renders_match_uncached() {
}
}

/// Issue #632: the Highlights / Shadows base of each process (V1: the edge-aware filter of log
/// luminance; V2: of its fine median, a radius that grows with the render) matches the CPU's, and
/// a warm cache never hands one process the other's base.
#[test]
fn highlights_shadows_base_matches_on_every_process() {
if !gpu() {
return;
}
let raw = SourceInfo { raw: true, ..Default::default() };
let src = scene(0, 2400, 1600);
let cache = StageCache::default();
for &(w, h) in &[(720, 480), (2400, 1600)] {
let req = RenderRequest::fit(w, h);
for p in Process::ALL {
let mut s = DevelopSettings { process: p.version(), ..DevelopSettings::default() };
s.light.highlights = -100.0;
s.light.shadows = 60.0;
check(&format!("{p:?} highlights/shadows {w}"), &src, &raw, &s, &req);
typical(&mut s);
check(&format!("{p:?} typical {w}"), &src, &raw, &s, &req);
s.masks = vec![Mask {
components: vec![MaskComponent {
name: None,
op: MaskOp::Add,
invert: false,
shape: MaskShape::Linear { start: Point::new(0.5, 0.0), end: Point::new(0.5, 0.9) },
}],
adjust: lightcraft_develop::LocalAdjustments { highlights: -70.0, shadows: 40.0, ..Default::default() },
..Default::default()
}];
check(&format!("{p:?} local highlights {w}"), &src, &raw, &s, &req);
}
for p in [Process::V1, Process::V2, Process::V1, Process::V2] {
let mut s = DevelopSettings { process: p.version(), ..DevelopSettings::default() };
s.light.highlights = -100.0;
let warm = lightcraft_gpu::render(&src, &raw, &s, &req, Some(&cache)).expect("gpu");
let fresh = lightcraft_gpu::render(&src, &raw, &s, &req, None).expect("gpu");
assert_eq!(warm.image, fresh.image, "{p:?} at {w} px, cached");
}
}
}

#[test]
fn overlays_match() {
if !gpu() {
Expand Down
3 changes: 2 additions & 1 deletion crates/pipeline/src/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ impl FinishParams {
pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap {
let l = &s.light;
match s.process.process() {
Process::V1 => match info.camera_tone.as_ref().filter(|_| info.raw && peak <= 1.0) {
// V2 changes the Highlights/Shadows base (`local::tone_base`), not the tone map
Process::V1 | Process::V2 => match info.camera_tone.as_ref().filter(|_| info.raw && peak <= 1.0) {
Some(curve) => ToneMap::camera(curve, l.contrast, l.whites, l.blacks),
None if info.raw => ToneMap::hdr(l.contrast, l.whites, l.blacks, peak),
None => ToneMap::display_hdr(l.contrast, l.whites, l.blacks, peak),
Expand Down
2 changes: 2 additions & 0 deletions crates/pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,6 @@ mod tests_hdr;
#[cfg(test)]
mod tests_local;
#[cfg(test)]
mod tests_tone;
#[cfg(test)]
mod tests_window;
Loading