From cd39cdc75cb3018f84d4a0b3843fac0047b892c1 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 25 Oct 2026 12:24:50 -0600 Subject: [PATCH 1/2] fix(compose, gpu): adjustment layers apply Fill inside the special eight blend modes (#9501) Below 100% Fill, Photoshop blends Color Burn, Linear Burn, Color Dodge, Linear Dodge, Vivid Light, Linear Light, Hard Mix and Difference with the source pulled toward the mode's neutral colour, at Opacity. Pixel layers already did (psblend::composite_fill); adjustment layers used Fill as coverage on the CPU (blend_back) and the GPU (AdjMix), so a 40% Fill Color Dodge gave (90,179,217) instead of (80,160,240). Co-Authored-By: Claude Opus 5.5 --- crates/compose/src/lib.rs | 16 +++++++++++----- crates/compose/src/tests.rs | 21 +++++++++++++++++++++ crates/gpu/src/compose.wgsl | 14 +++++++++++++- crates/gpu/src/plan.rs | 11 +++++++++-- crates/gpu/tests/it/parity.rs | 29 +++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 4786bc1912..2c8d01a7ad 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -1065,8 +1065,10 @@ fn branchless_floor(x: f32) -> f32 { } /// Blend an adjustment layer's result (`adjusted`) back into the backdrop it was made from, each -/// pixel by its coverage `ks` (opacity × mask) in blend mode `mode`. -fn blend_back(backdrop: &mut [[f32; 4]], adjusted: &[[f32; 4]], ks: impl Iterator, mode: BlendMode) { +/// pixel by its coverage `ks` (opacity × mask) in blend mode `mode` at layer Fill `fill`: below +/// 100% the special eight ([`psblend::fill_is_special`]) blend a result pulled toward the mode's +/// neutral colour, as a pixel layer does (#9501); for every other mode Fill is in `ks`. +fn blend_back(backdrop: &mut [[f32; 4]], adjusted: &[[f32; 4]], ks: impl Iterator, mode: BlendMode, fill: f32) { for ((b, a), k) in backdrop.iter_mut().zip(adjusted).zip(ks) { if k <= 0.0 { continue; @@ -1074,7 +1076,7 @@ fn blend_back(backdrop: &mut [[f32; 4]], adjusted: &[[f32; 4]], ks: impl Iterato let s = [a[0], a[1], a[2]]; // Normal is the source as it is (`blend_rgb` gives exactly that), and most adjustment // layers are Normal: no per-pixel mode dispatch for them. - let blended = if mode == BlendMode::Normal { s } else { blend::blend_rgb(mode, [b[0], b[1], b[2]], s) }; + let blended = if mode == BlendMode::Normal { s } else { blend::blend_rgb_fill(mode, [b[0], b[1], b[2]], s, fill) }; *b = [b[0] + (blended[0] - b[0]) * k, b[1] + (blended[1] - b[1]) * k, b[2] + (blended[2] - b[2]) * k, b[3]]; } } @@ -2014,9 +2016,13 @@ fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer } } } else { + // Fill below 100% in one of the special eight changes the blend itself and leaves + // coverage to Opacity, as for a pixel layer (psblend::composite_fill, #9501). + let (k, fill) = + if layer.fill_opacity < 1.0 && psblend::fill_is_special(layer.blend) { (layer.opacity, layer.fill_opacity) } else { (opacity, 1.0) }; match &mv { - Some(m) => blend_back(&mut backdrop.px, &adjusted.px, m.iter().map(|v| opacity * v), layer.blend), - None => blend_back(&mut backdrop.px, &adjusted.px, std::iter::repeat(opacity), layer.blend), + Some(m) => blend_back(&mut backdrop.px, &adjusted.px, m.iter().map(|v| k * v), layer.blend, fill), + None => blend_back(&mut backdrop.px, &adjusted.px, std::iter::repeat(k), layer.blend, fill), } } recycle_buffer(adjusted); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 76ba8dc72b..b061a95e6b 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -417,6 +417,27 @@ fn every_blend_mode_matches_reference_on_opaque_pixels() { } } +/// #9501: an adjustment layer at 40% Fill in Color Dodge or Linear Burn blends a result pulled +/// toward the mode's neutral colour, as a pixel layer does, instead of fading the blend by Fill. +/// Photoshop: a constant 0.5 look over (64, 128, 192) gives (80, 160, 240) and (13, 77, 141). +#[test] +fn adjustment_fill_changes_the_special_blend_modes() { + for (mode, want) in [(BlendMode::ColorDodge, [80, 160, 240]), (BlendMode::LinearBurn, [13, 77, 141])] { + let mut d = Document::with_background("fill", Size::new(1, 1), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut base = Layer::raster("b", PixelFormat::RGBA8); + base.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 1, 1), &[64.0 / 255.0, 128.0 / 255.0, 192.0 / 255.0, 1.0]); + let look = Adjustment::ColorLookup { name: "half".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size: 2, tetrahedral: false, dither: false }; + let mut adj = Layer::new("look", LayerContent::Adjustment(look)); + adj.blend = mode; + adj.fill_opacity = 0.4; + d.layers.push(base); + d.layers.push(adj); + let got = px(&d, 0, 0); + let got: Vec = got[..3].iter().map(|v| (v * 255.0).round() as i32).collect(); + assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1), "{mode:?}: {got:?} vs Photoshop {want:?}"); + } +} + /// One opaque pixel: `top` in `mode` over `bottom`, in a document of `depth`. fn blend_px(depth: SampleType, mode: BlendMode, bottom: [f32; 3], top: [f32; 3]) -> [f32; 4] { let mut d = Document::new("b", Size::new(1, 1), ColorMode::Rgb, depth); diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 1a475827e8..587ee20dee 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -1000,7 +1000,19 @@ fn fs_adjmix(in: VOut) -> @location(0) vec4 { if (op.mode == M_DISSOLVE) { return select(b, vec4(a.rgb, b.a), dissolve_noise(doc_px(p)) < k); } - let bl = blend_rgb(op.mode, b.rgb, a.rgb); + // Fill below 100% in one of the special eight (compose::blend_back, psblend::blend_rgb_fill): + // the result pulled toward the mode's neutral colour; Hard Mix (cb - f(1 - cs)) / (1 - f). + let f = clamp(op.fill, 0.0, 1.0); + let special = f < 1.0 && fill_is_special(op.mode); + var cs = a.rgb; + if (special) { + var neutral = 0.0; + if (op.mode == 5 || op.mode == 6) { neutral = 1.0; } + if (op.mode == 16 || op.mode == 17) { neutral = 0.5; } + cs = vec3(neutral) + (a.rgb - vec3(neutral)) * f; + } + var bl = blend_rgb(op.mode, b.rgb, cs); + if (special && op.mode == 19) { bl = clamp((b.rgb - f * (vec3(1.0) - a.rgb)) / (1.0 - f), vec3(0.0), vec3(1.0)); } return vec4(b.rgb + (bl - b.rgb) * k, b.a); } diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index 38cc879b09..622d428563 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -742,7 +742,7 @@ impl<'a> Planner<'a> { p.a = Some(before); p.b = Some(adjusted); p.mode = layer.blend; - p.opacity = opacity; + (p.opacity, p.fill) = adjustment_fill(layer, opacity); p.mask = self.mask_use(layer); return Ok(self.emit(p)); } @@ -999,7 +999,7 @@ impl<'a> Planner<'a> { p.a = Some(before); p.b = Some(adjusted); p.mode = layer.blend; - p.opacity = opacity; + (p.opacity, p.fill) = adjustment_fill(layer, opacity); p.mask = self.mask_use(layer); return Ok(self.emit(p)); } @@ -1883,6 +1883,13 @@ fn single_content_pass(layer: &Layer) -> bool { } } +/// An adjustment layer's mix coverage and Fill (compose's adjustment branch): below 100% Fill the +/// special eight blend at Opacity with the result pulled toward the mode's neutral colour, every +/// other mode takes Fill as coverage (#9501). +fn adjustment_fill(layer: &Layer, opacity: f32) -> (f32, f32) { + if layer.fill_opacity < 1.0 && photocraft_compose::psblend::fill_is_special(layer.blend) { (layer.opacity, layer.fill_opacity) } else { (opacity, 1.0) } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs index 14a3e62243..3c9b2740ab 100644 --- a/crates/gpu/tests/it/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -626,6 +626,35 @@ fn special_eight_fill_matches_cpu() { } } +/// The same for adjustment layers (#9501): the GPU's adjustment mix takes Fill inside the special +/// eight's blend, as compose's adjustment branch does, with a mask and Opacity on top. +#[test] +fn special_eight_fill_on_adjustment_layers_matches_cpu() { + let Some(mut g) = gpu() else { return }; + let special = [ + BlendMode::ColorBurn, + BlendMode::LinearBurn, + BlendMode::ColorDodge, + BlendMode::LinearDodge, + BlendMode::VividLight, + BlendMode::LinearLight, + BlendMode::HardMix, + BlendMode::Difference, + ]; + for mode in special { + for fill in [0.3, 0.6] { + let mut d = base_doc(64, 48); + d.layers[0] = noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 5, 1.0); + let mut l = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + l.blend = mode; + l.opacity = 0.7; + l.fill_opacity = fill; + d.layers.push(l); + check(&mut g, &d, &format!("adjustment {mode:?} fill {fill}")); + } + } +} + #[test] fn xor_rounding_on_half_float_targets() { let Some((_adapter, device, queue, _lock)) = any_device() else { return }; From 6c55561128caf5ac917eb3d174e8e7bb4bd48e23 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 25 Oct 2026 12:48:03 -0600 Subject: [PATCH 2/2] GPU: a clipped adjustment takes Fill as coverage, as on the CPU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit on #9512 (inline 4371923258): the planner's clipped path (atop_any) took the special eight's separate Fill too, while compose's clipped branch (composite_atop) uses opacity × Fill as coverage, so a clipped adjustment at partial Fill rendered differently on the GPU (53 levels off at 30% Fill). The clipped path now uses combined coverage. Test: special_eight_fill_on_adjustment_layers_matches_cpu (adds a clipped adjustment for each mode and Fill) Co-Authored-By: Claude Opus 5.5 --- crates/gpu/src/plan.rs | 4 +++- crates/gpu/tests/it/parity.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index 622d428563..1576679715 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -999,7 +999,9 @@ impl<'a> Planner<'a> { p.a = Some(before); p.b = Some(adjusted); p.mode = layer.blend; - (p.opacity, p.fill) = adjustment_fill(layer, opacity); + // Clipped, an adjustment takes Fill as coverage in every mode, as compose's clipped + // branch (`composite_atop`) does. + (p.opacity, p.fill) = (opacity, 1.0); p.mask = self.mask_use(layer); return Ok(self.emit(p)); } diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs index 3c9b2740ab..2409b169b4 100644 --- a/crates/gpu/tests/it/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -651,6 +651,18 @@ fn special_eight_fill_on_adjustment_layers_matches_cpu() { l.fill_opacity = fill; d.layers.push(l); check(&mut g, &d, &format!("adjustment {mode:?} fill {fill}")); + // CodeRabbit on #9512: clipped to a pixel layer, the adjustment takes Fill as + // coverage on both paths (compose's clipped branch, the planner's `atop`). + let mut d = base_doc(64, 48); + d.layers[0] = noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 5, 1.0); + d.layers.push(noise_layer("base", PixelFormat::RGBA8, Rect::from_xywh(8, 6, 40, 30), 9, 1.0)); + let mut l = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + l.blend = mode; + l.opacity = 0.7; + l.fill_opacity = fill; + l.clipped = true; + d.layers.push(l); + check(&mut g, &d, &format!("clipped adjustment {mode:?} fill {fill}")); } } }