Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ 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,
/// Pixel-scale sharpening with working Radius and Detail controls.
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
2 changes: 1 addition & 1 deletion crates/engine/src/media/source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Arc, Deserialize, DevelopSettings, MediaKind, Photo, Rgb32f, Rgba8, Serialize, SourceInfo};

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

/// 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
5 changes: 5 additions & 0 deletions crates/gpu/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const MODULES: &[Module] = &[
bindings: &[("src", false, "f32"), ("dst", true, "f32")],
entries: &["box_h", "box_v", "median_h", "median_v"],
},
Module {
src: include_str!("wgsl/gauss.wgsl"),
bindings: &[("src", false, "f32"), ("taps", false, "f32"), ("dst", true, "f32")],
entries: &["gauss_h", "gauss_v"],
},
Module {
src: include_str!("wgsl/resize.wgsl"),
bindings: &[("src", false, "f32"), ("table", false, "u32"), ("dst", true, "f32")],
Expand Down
12 changes: 12 additions & 0 deletions crates/gpu/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const FIELDS: &[(&str, usize)] = &[
("DEHAZE", 1),
("SHARPEN", 1),
("SHARPEN_MASK", 1),
("SHARPEN_LIMIT", 1),
("SHARPEN_GAIN", 1),
("SHARPEN_V3", 1),
("SHP_OFF", 1),
("HAS_SHP", 1),
("HAS_CLAR", 1),
("HAS_TEX", 1),
("HAS_DARK", 1),
Expand Down Expand Up @@ -140,6 +145,8 @@ impl Block {
pub struct Present {
pub clarity: bool,
pub texture: bool,
/// The sharpening blur plane (after the texture plane in `tex` when both are present).
pub sharpen: bool,
pub dark: bool,
/// The blurred chromaticity follows the mask planes in the `masks` buffer.
pub chroma: bool,
Expand Down Expand Up @@ -184,6 +191,11 @@ 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_LIMIT", fp.sharpen_limit);
p.f("SHARPEN_GAIN", fp.sharpen_gain);
p.b("SHARPEN_V3", fp.pixel_sharpen);
p.u("SHP_OFF", if present.texture { (fp.w * fp.h) as u32 } else { 0 });
p.b("HAS_SHP", present.sharpen);
p.b("HAS_CLAR", present.clarity);
p.b("HAS_TEX", present.texture);
p.b("HAS_DARK", present.dark);
Expand Down
40 changes: 35 additions & 5 deletions crates/gpu/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct Planes {
base: Option<(local::BaseKey, Arc<Buf>)>,
clarity: Option<(u32, Arc<Buf>)>,
texture: Option<(u32, Arc<Buf>)>,
sharpen: Option<(local::SharpenKey, Arc<Buf>)>,
dark: Option<(u32, Arc<Buf>, f32)>,
chroma: Option<(u32, Arc<Buf>)>,
}
Expand Down Expand Up @@ -105,6 +106,7 @@ impl GpuStages {
p.base.iter().for_each(|(_, b)| add(b));
p.clarity.iter().for_each(|(_, b)| add(b));
p.texture.iter().for_each(|(_, b)| add(b));
p.sharpen.iter().for_each(|(_, b)| add(b));
p.dark.iter().for_each(|(_, b, _)| add(b));
}
if let Some((_, b)) = &*self.source.lock().unwrap_or_else(|e| e.into_inner()) {
Expand Down Expand Up @@ -263,6 +265,18 @@ pub(crate) fn gaussian(cx: &mut Cx<'_>, src: &Buf, w: usize, h: usize, nc: usize
if last == 0 { a } else { b }
}

/// Exact Gaussian (`lightcraft_raster::blur::gaussian_taps`) of a one-channel plane.
pub(crate) fn gaussian_fir(cx: &mut Cx<'_>, src: &Buf, w: usize, h: usize, sigma: f32) -> Buf {
let taps = lightcraft_raster::blur::gauss_taps(sigma);
let taps_buf = cx.gpu.upload(&taps);
let r = (taps.len() / 2) as u32;
let (tmp, out) = (cx.gpu.buffer(w * h), cx.gpu.buffer(w * h));
let p = [w as u32, h as u32, r];
cx.run("gauss_h", &p, &[Some(src), Some(&taps_buf), Some(&tmp)], groups2(w, h, [16, 16]));
cx.run("gauss_v", &p, &[Some(&tmp), Some(&taps_buf), Some(&out)], groups2(w, h, [16, 16]));
out
}

/// Resample taps of `lightcraft_raster::resample::resize`, packed for the `resize_*` kernels.
fn taps(src: usize, dst: usize, filter: Filter) -> Vec<u32> {
let w = lightcraft_raster::resample::weights(src, dst, filter);
Expand Down Expand Up @@ -692,9 +706,22 @@ pub fn render(
let present = Present {
clarity: prep.clarity.is_some(),
texture: prep.texture.is_some(),
sharpen: prep.sharpen.is_some(),
dark: prep.dark.is_some(),
chroma: masks.is_some() && prep.chroma.is_some(),
};
// the finish kernel's `tex` binding: the texture plane, then the sharpening plane (one binding
// for both keeps the kernel within the storage-buffer count every supported device has)
let tex_buf: Option<Arc<Buf>> = match (&prep.texture, &prep.sharpen) {
(Some(t), Some(sh)) => {
let both = cx.gpu.buffer(2 * n);
cx.copy_into(t, &both, 0);
cx.copy_into(sh, &both, n);
Some(Arc::new(both))
}
(Some(b), None) | (None, Some(b)) => Some(b.clone()),
(None, None) => None,
};
let (p, aux) = finish_block(&fp, &terms, &present);
let aux = gpu.upload(&aux);
// cleared, so that work which never ran cannot pass for an image (see the check below)
Expand All @@ -717,7 +744,7 @@ pub fn render(
Some(&prep.log_l),
Some(&prep.base),
prep.clarity.as_deref(),
prep.texture.as_deref(),
tex_buf.as_deref(),
prep.dark.as_deref(),
masks.as_ref(),
Some(&aux),
Expand Down Expand Up @@ -794,8 +821,9 @@ pub fn render(
let overlay_mask = overlay_mask.or_else(|| {
req.overlay.reads_detail().then(|| {
let l = host.log_l.get_or_insert_with(|| Arc::new(cx.read_plane(&prep.log_l, w, h))).clone();
let tex = prep.texture.as_deref().map(|b| cx.read_plane(b, w, h));
lightcraft_pipeline::visualize::sharpen_plane(req.overlay, &plan.settings, &l, tex.as_ref(), plan.px_per_long)
let band = if local::pixel_sharpen(&plan.settings) { &prep.sharpen } else { &prep.texture };
let blur = band.as_deref().map(|b| cx.read_plane(b, w, h));
lightcraft_pipeline::visualize::sharpen_plane(req.overlay, &plan.settings, &l, blur.as_ref(), plan.px_per_long, plan.native_long as f64)
})?
});
lightcraft_pipeline::visualize::apply(&mut image, req.overlay, &plan, overlay_mask.as_ref(), all_overlay_alphas.as_deref());
Expand Down Expand Up @@ -874,6 +902,7 @@ struct Prep {
base: Arc<Buf>,
clarity: Option<Arc<Buf>>,
texture: Option<Arc<Buf>>,
sharpen: Option<Arc<Buf>>,
dark: Option<Arc<Buf>>,
/// Blurred chromaticity (local Moiré / Noise).
chroma: Option<Arc<Buf>>,
Expand All @@ -884,7 +913,7 @@ struct Prep {
fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, planes: &mut Planes) -> Prep {
let (w, h) = (plan.w, plan.h);
let n = w * h;
let sig = local::plane_sigmas(&plan.settings, plan.px_per_long, req.quality);
let sig = local::plane_sigmas(&plan.settings, plan.px_per_long, plan.native_long as f64, req.quality);
let log_l = match &planes.log_l {
Some(b) => b.clone(),
None => {
Expand All @@ -904,6 +933,7 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla
};
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 sharpen = sig.sharpen.map(|sg| plane_at(&mut planes.sharpen, local::sharpen_key(&plan.settings, sg), || gaussian_fir(cx, &log_l, w, h, sg)));
let (dark, air) = match sig.dark {
None => (None, 1.0),
Some(sg) => match &planes.dark {
Expand Down Expand Up @@ -931,7 +961,7 @@ fn prepare(cx: &mut Cx<'_>, lin: &Buf, plan: &Plan<'_>, req: &RenderRequest, pla
gaussian(cx, &ch, w, h, 3, sg)
})
});
Prep { log_l, base, clarity, texture, dark, chroma, air }
Prep { log_l, base, clarity, texture, sharpen, dark, chroma, air }
}

/// Most brush dabs the mask kernel evaluates per pixel (more: the CPU rasterizes the brush).
Expand Down
27 changes: 18 additions & 9 deletions crates/gpu/src/wgsl/finish.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -423,19 +423,28 @@ 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;
}
let sp = lt[13] * 0.6 + pf(F_SHARPEN);
if (pu(F_SHARPEN_V3) != 0u && sp != 0.0 && pu(F_HAS_SHP) != 0u) {
let det = l_pre - tex[pu(F_SHP_OFF) + i];
let sm = pf(F_SHARPEN_MASK);
var mk = 1.0;
if (sm > 0.0) {
mk = sstep(sm * 0.1, sm * 0.1 + 0.06, abs(det));
}
delta += sp * pf(F_SHARPEN_GAIN) * 7.0 * mk * det / (1.0 + abs(det) / pf(F_SHARPEN_LIMIT));
} else if (pu(F_SHARPEN_V3) == 0u && sp != 0.0 && pu(F_HAS_TEX) != 0u) {
let 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 * 1.3 * 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
36 changes: 36 additions & 0 deletions crates/gpu/src/wgsl/gauss.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Exact separable Gaussian with explicit taps over a one-channel plane (clamped edges); mirrors
// `lightcraft_raster::blur::gaussian_taps` (same taps, same accumulation order).
// P: w, h, r (taps has 2r+1 entries). Bindings: src, taps, dst.

@compute @workgroup_size(16, 16)
fn gauss_h(@builtin(global_invocation_id) g: vec3<u32>) {
let w = pu(0u);
let h = pu(1u);
let r = i32(pu(2u));
if (g.x >= w || g.y >= h) {
return;
}
let last = i32(w) - 1;
let row = g.y * w;
var acc = 0.0;
for (var k = 0; k <= 2 * r; k++) {
acc += src[row + u32(clamp(i32(g.x) + k - r, 0, last))] * taps[u32(k)];
}
dst[row + g.x] = acc;
}

@compute @workgroup_size(16, 16)
fn gauss_v(@builtin(global_invocation_id) g: vec3<u32>) {
let w = pu(0u);
let h = pu(1u);
let r = i32(pu(2u));
if (g.x >= w || g.y >= h) {
return;
}
let last = i32(h) - 1;
var acc = 0.0;
for (var k = 0; k <= 2 * r; k++) {
acc += src[u32(clamp(i32(g.y) + k - r, 0, last)) * w + g.x] * taps[u32(k)];
}
dst[g.y * w + g.x] = acc;
}
51 changes: 51 additions & 0 deletions crates/gpu/tests/it/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ fn cases() -> Vec<(&'static str, Edit)> {
s.effects.clarity = -60.0;
s.effects.texture = -40.0;
}),
("sharpen radius 2.5 detail 100", |s| {
s.detail.sharpen_amount = 100.0;
s.detail.sharpen_radius = 2.5;
s.detail.sharpen_detail = 100.0;
}),
("sharpen radius 0.6 detail 0", |s| {
s.detail.sharpen_amount = 80.0;
s.detail.sharpen_radius = 0.6;
s.detail.sharpen_detail = 0.0;
}),
("sharpen + texture + masking", |s| {
s.detail.sharpen_amount = 70.0;
s.detail.sharpen_masking = 30.0;
s.effects.texture = 40.0;
}),
("local sharpness", |s| {
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.6) },
}],
adjust: lightcraft_develop::LocalAdjustments { sharpness: 80.0, ..Default::default() },
..Default::default()
}];
}),
("sharpen masking", |s| {
s.detail.sharpen_amount = 90.0;
s.detail.sharpen_masking = 60.0;
Expand Down Expand Up @@ -656,6 +683,30 @@ fn highlights_shadows_base_matches_on_every_process() {
}
}

#[test]
fn sharpening_matches_each_process_and_reduced_source() {
if !gpu() {
return;
}
let src = scene(0, 256, 192);
let info = SourceInfo { raw: true, native_long: Some(1024), ..Default::default() };
let req = RenderRequest::fit(128, 128);
let cache = StageCache::default();
for process in [Process::V1, Process::V2, Process::V3, Process::V2, Process::V3] {
let mut s = DevelopSettings { process: process.version(), ..Default::default() };
s.detail.sharpen_amount = 80.0;
s.detail.sharpen_radius = 2.0;
s.detail.sharpen_detail = 75.0;
for nr in [0.0, 50.0, 20.0] {
s.detail.nr_luminance = nr;
check(&format!("{process:?} sharpening NR {nr}"), &src, &info, &s, &req);
let warm = lightcraft_gpu::render(&src, &info, &s, &req, Some(&cache)).expect("gpu");
let fresh = lightcraft_gpu::render(&src, &info, &s, &req, None).expect("gpu");
assert_eq!(warm.image, fresh.image, "{process:?} NR {nr}");
}
}
}

#[test]
fn overlays_match() {
if !gpu() {
Expand Down
Loading
Loading