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 <[email protected]>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-10-25 12:24:50 -06:00
1 parent 938542c78b
commit cd39cdc75c
5 files changed
+83 -8

No files matched your search

+11 -5
View File
@@ -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<Item = f32>, 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<Item = f32>, 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);
+21
View File
@@ -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<i32> = 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);
+13 -1
View File
@@ -1000,7 +1000,19 @@ fn fs_adjmix(in: VOut) -> @location(0) vec4<f32> {
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);
}
+9 -2
View File
@@ -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::*;
+29
View File
@@ -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 };