Merge approved PhotoCraft PR #9512: fix(compose, gpu): adjustment layers apply Fill inside the special eight blend modes

Port behavior and regression coverage into current modules; preserve current upstream behavior.
Combine Fill-aware adjustment blending with stable HDR coverage and add cross-feature CPU/GPU regression matrix.
This commit is contained in:
FredApps committed 2026-10-29 03:54:31 +01:00
commit 29804c5221
5 files changed
+139 -10

No files matched your search

+11 -5
View File
@@ -72,8 +72,10 @@ pub(crate) 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;
@@ -81,7 +83,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·(1−k) + s·k`, not `b + (s − b)·k`: with a huge 32-bit backdrop the difference loses a
// small result to f32 rounding, so full coverage gave 0 instead of a lookup's 0.5 (#9483).
let j = 1.0 - k;
@@ -439,9 +441,13 @@ pub(crate) fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop:
}
}
} 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);
+22 -1
View File
@@ -426,7 +426,7 @@ fn full_coverage_adjustment_keeps_its_result_over_huge_hdr_values() {
let mut d = Document::new("hdr", Size::new(2, 2), ColorMode::Rgb, SampleType::F32);
let mut base = Layer::raster("b", PixelFormat::RGBA32F);
base.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 2, 2), &[v, v, v, 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 look = Adjustment::ColorLookup { name: "half".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size: 2, tetrahedral: false, dither: false, source: None };
d.layers = vec![base, Layer::new("look", LayerContent::Adjustment(look))];
let got = px(&d, 0, 0);
for c in &got[..3] {
@@ -435,6 +435,27 @@ fn full_coverage_adjustment_keeps_its_result_over_huge_hdr_values() {
}
}
/// #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, source: None };
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);
+15 -2
View File
@@ -1000,8 +1000,21 @@ 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);
return vec4(b.rgb + (bl - b.rgb) * k, b.a);
// 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)); }
// Keep bounded adjustment results exact at full coverage over HDR backdrops.
return vec4(b.rgb * (1.0 - k) + bl * k, b.a);
}
// Pass-through group coverage mixes premultiplied colour, then returns straight alpha.
+12 -2
View File
@@ -469,7 +469,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));
}
@@ -734,7 +734,8 @@ impl<'a> Planner<'a> {
p.a = Some(before);
p.b = Some(adjusted);
p.mode = layer.blend;
p.opacity = opacity;
// A clipped adjustment takes Fill as coverage in every mode.
(p.opacity, p.fill) = (opacity, 1.0);
p.mask = self.mask_use(layer);
return Ok(self.emit(p));
}
@@ -1413,3 +1414,12 @@ impl<'a> Planner<'a> {
self.emit(p)
}
}
/// Adjustment Fill belongs inside the special blend modes; other modes use it as coverage.
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)
}
}
+79
View File
@@ -629,6 +629,47 @@ 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}"));
// 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}"));
}
}
}
#[test]
fn xor_rounding_on_half_float_targets() {
let Some((_adapter, device, queue, _lock)) = any_device() else { return };
@@ -3215,3 +3256,41 @@ fn dissolve_clipped_adjustment_matches_cpu() {
diff_rect(&mut g, &d, Rect::new(250, 1, 270, 20), "clipped adjustment offset region").unwrap_or_else(|e| panic!("{e}"));
}
}
/// Integration of #9509 and #9512: coverage endpoints, Fill, masks and clipping share the CPU oracle.
#[test]
fn integration_adjustment_fill_opacity_masks_and_hdr_match_cpu() {
let Some(mut g) = gpu() else { return };
for clipped in [false, true] {
for mode in [BlendMode::Normal, BlendMode::ColorDodge, BlendMode::LinearBurn] {
for fill in [0.0, 0.5, 1.0] {
for opacity in [0.5, 1.0] {
for coverage in [0.5, 1.0] {
let mut d = Document::new("combined adjustment", Size::new(1, 1), ColorMode::Rgb, SampleType::F32);
let mut base = Layer::raster("base", PixelFormat::RGBA32F);
base.surface_mut().unwrap().write_pixel(0, 0, &[0.25, 0.5, 0.75, 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, source: None };
let mut layer = Layer::new("adjust", LayerContent::Adjustment(look));
layer.blend = mode;
layer.clipped = clipped;
layer.fill_opacity = fill;
layer.opacity = opacity;
let mut mask = LayerMask::reveal_all();
mask.surface = photocraft_raster::Surface::with_default(PixelFormat::GRAY32F, &[coverage]);
layer.mask = Some(mask);
d.layers = vec![base, layer];
check(&mut g, &d, &format!("combined {mode:?} fill={fill} opacity={opacity} mask={coverage} clipped={clipped}"));
}
}
}
}
}
// The existing GPU harness requires float32 accumulation. The CPU regression separately
// covers 1e8 inputs, beyond the range of half-float source textures on some adapters.
let mut d = Document::new("HDR full coverage", Size::new(1, 1), ColorMode::Rgb, SampleType::F32);
let mut base = Layer::raster("HDR", PixelFormat::RGBA32F);
base.surface_mut().unwrap().write_pixel(0, 0, &[1.0e4, 1.0e4, 1.0e4, 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, source: None };
d.layers = vec![base, Layer::new("lookup", LayerContent::Adjustment(look))];
check(&mut g, &d, "HDR full coverage with bounded result");
}