Skip to content
Open
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
9 changes: 7 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,22 @@ pub enum Process {
/// 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,
/// Sharpening works at the original photo's pixels (Radius is in them, a view smaller than the
/// photo sharpens the detail it shows), and a raw gets a base look under every profile: a tone
/// curve, a colour matrix and a hue table matched to a reference rendering.
V3,
}

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

pub const fn version(self) -> ProcessVersion {
match self {
Process::V1 => ProcessVersion(1),
Process::V2 => ProcessVersion(2),
Process::V3 => ProcessVersion(3),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/gpu/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const FIELDS: &[(&str, usize)] = &[
("SRGB_OFF", 1),
("CURVE_OFF", 1),
("CURVES", 1),
("LOOK", 1),
("LOOK_OFF", 1),
("GAIN", 1),
("EV", 1),
("AIR", 1),
Expand All @@ -28,6 +30,9 @@ const FIELDS: &[(&str, usize)] = &[
("DEHAZE", 1),
("SHARPEN", 1),
("SHARPEN_MASK", 1),
("SHARPEN_GAIN", 1),
("SHARPEN_FINE", 1),
("SHARPEN_TAPS", 3),
("HAS_CLAR", 1),
("HAS_TEX", 1),
("HAS_DARK", 1),
Expand Down Expand Up @@ -152,6 +157,10 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
aux.extend_from_slice(&l.v);
}
}
let look_off = aux.len();
if fp.look {
aux.extend(lightcraft_pipeline::look_table::TABLE.iter().flatten());
}
let mask_off = aux.len();
for m in masks {
aux.extend_from_slice(m);
Expand All @@ -165,6 +174,8 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
p.u("SRGB_OFF", srgb_off as u32);
p.u("CURVE_OFF", curve_off as u32);
p.b("CURVES", fp.curves.is_some());
p.b("LOOK", fp.look);
p.u("LOOK_OFF", look_off as u32);
p.f("REFINE_SAT", fp.refine_sat);
p.f("GAIN", fp.gain);
p.f("EV", fp.ev);
Expand All @@ -177,6 +188,9 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
p.f("DEHAZE", fp.dehaze);
p.f("SHARPEN", fp.sharpen);
p.f("SHARPEN_MASK", fp.sharpen_mask);
p.f("SHARPEN_GAIN", lightcraft_pipeline::finish::SHARPEN_GAIN);
p.b("SHARPEN_FINE", fp.sharpen_fine);
p.fs("SHARPEN_TAPS", &fp.sharpen_taps);
p.b("HAS_CLAR", present.clarity);
p.b("HAS_TEX", present.texture);
p.b("HAS_DARK", present.dark);
Expand Down
96 changes: 87 additions & 9 deletions crates/gpu/src/wgsl/finish.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,68 @@ fn out_encode(v: f32) -> f32 {
return 1.099 * pow(l, 0.45) - 0.099;
}

// `lightcraft_pipeline::look_table::apply`: linear Rec.2020 through the base look's hue table
// (OkLCh; 24 hues × 5 chromas × 6 lightnesses, trilinear, hue wrapping), stored in `aux` at LOOK_OFF.
fn look_node(i: u32) -> vec3<f32> {
let o = pu(F_LOOK_OFF) + 3u * i;
return vec3<f32>(aux[o], aux[o + 1u], aux[o + 2u]);
}

fn look_apply(rgb: vec3<f32>) -> vec3<f32> {
let lab = oklab(max(rgb, vec3<f32>(0.0)));
let l = lab.x;
let c = length(lab.yz);
// (`look_table::NEUTRAL_CHROMA`)
if (!(l > 0.0) || !(c > 5e-4)) {
return rgb;
}
let h = atan2(lab.z, lab.y);
let hf = fract(h / 6.283185307) * 24.0;
let h0 = u32(floor(hf)) % 24u;
let th = hf - floor(hf);
let cf = clamp(c / 0.05, 0.0, 4.0 - 1e-4);
let c0 = u32(floor(cf));
let tc = cf - floor(cf);
let lf = clamp(l / 0.2, 0.0, 5.0 - 1e-4);
let l0 = u32(floor(lf));
let tl = lf - floor(lf);
var v = vec3<f32>(0.0);
for (var dh = 0u; dh < 2u; dh++) {
let wh = select(1.0 - th, th, dh == 1u);
for (var dc = 0u; dc < 2u; dc++) {
let wc = select(1.0 - tc, tc, dc == 1u);
for (var dl = 0u; dl < 2u; dl++) {
let wl = select(1.0 - tl, tl, dl == 1u);
let i = (((h0 + dh) % 24u) * 5u + (c0 + dc)) * 6u + (l0 + dl);
v += wh * wc * wl * look_node(i);
}
}
}
let h2 = h + v.x;
let c2 = c * exp(v.y);
return oklab_inv(vec3<f32>(l * exp(v.z), c2 * cos(h2), c2 * sin(h2)));
}

// `lightcraft_pipeline::finish::blur5`: log_l at (x, y) through the separable five-tap
// SHARPEN_TAPS (centre, ±1, ±2), edges clamped; rows summed first, as the CPU does.
fn sharpen_blur(x: u32, y: u32, w: u32, h: u32) -> f32 {
var sum = 0.0;
for (var dy = -2; dy <= 2; dy++) {
let wy = pf(F_SHARPEN_TAPS + u32(abs(dy)));
if (wy == 0.0) {
continue;
}
let yy = u32(clamp(i32(y) + dy, 0, i32(h) - 1));
var row = 0.0;
for (var dx = -2; dx <= 2; dx++) {
let xx = u32(clamp(i32(x) + dx, 0, i32(w) - 1));
row += pf(F_SHARPEN_TAPS + u32(abs(dx))) * log_l[yy * w + xx];
}
sum += wy * row;
}
return sum;
}

@compute @workgroup_size(16, 16)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let w = pu(F_W);
Expand Down Expand Up @@ -378,19 +440,30 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
delta += cl * 0.85 * det * (0.35 + 0.65 * mid);
}
let tx = pf(F_TEX) + lt[8];
let sp = lt[13] * 0.6 + pf(F_SHARPEN);
if ((tx != 0.0 || sp != 0.0) && pu(F_HAS_TEX) != 0u) {
if (tx != 0.0 && pu(F_HAS_TEX) != 0u) {
let det = l_pre - tex[i];
let tame = 1.0 - 0.6 * sstep(0.4, 1.6, abs(det));
delta += tx * 1.1 * clamp(det, -1.0, 1.0) * tame;
if (sp != 0.0) {
let sm = pf(F_SHARPEN_MASK);
var mk = 1.0;
if (sm > 0.0) {
mk = sstep(sm * 0.25, sm * 0.25 + 0.15, abs(det));
}
delta += sp * 1.3 * clamp(det, -0.8, 0.8) * mk;
}
// sharpening: from V3 an unsharp mask at the original's pixels (SHARPEN_TAPS), before it the
// texture band
let sp = lt[13] * 0.6 + pf(F_SHARPEN);
let fine = pu(F_SHARPEN_FINE) != 0u;
if (sp != 0.0 && (fine || pu(F_HAS_TEX) != 0u)) {
var det = 0.0;
var gain = 1.3;
if (fine) {
det = l_pre - sharpen_blur(x, y, w, h);
gain = pf(F_SHARPEN_GAIN);
} else {
det = l_pre - tex[i];
}
let sm = pf(F_SHARPEN_MASK);
var mk = 1.0;
if (sm > 0.0) {
mk = sstep(sm * 0.25, sm * 0.25 + 0.15, abs(det));
}
delta += sp * gain * clamp(det, -0.8, 0.8) * mk;
}
// local Noise: smooth (or, negative, boost) small-amplitude detail, keep edges
if (l_noise != 0.0 && pu(F_HAS_TEX) != 0u) {
Expand Down Expand Up @@ -467,6 +540,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
}
}

// --- the base look's hue table (display-referred, before the output conversion)
if (pu(F_LOOK) != 0u) {
d = look_apply(d);
}

// --- gamut map to the output space (desaturate towards luminance until in range)
let om = array<vec3<f32>, 3>(
vec3<f32>(pf(F_OUT_M), pf(F_OUT_M + 1u), pf(F_OUT_M + 2u)),
Expand Down
Loading