Merge PR #3193: fix(compose): resolve Dissolve coverage across adjustment, effects, a…

Conflicts:
crates/compose/src/tests.rs: appended tests, kept both
crates/gpu/tests/parity.rs: appended tests, kept both
This commit is contained in:
aiongg committed 2026-10-22 02:23:06 +00:00
commit 2b3af0f815
5 files changed
+279 -2

No files matched your search

+7 -1
View File
@@ -2221,7 +2221,13 @@ pub(crate) fn composite_with_effects_prepared(
let i = ((y - rect.y0) as usize) * rect.width() as usize + (x - rect.x0) as usize;
let a = before.px[i];
let b = if x >= big.x0 && x < big.x1 && y >= big.y0 && y < big.y1 { work.px[((y - big.y0) as usize) * w + (x - big.x0) as usize] } else { a };
backdrop.px[i] = mix_premul(a, b, op);
backdrop.px[i] = if layer.blend == BlendMode::Dissolve {
// Dissolve applies layer opacity to the completed interior/exterior effect
// stack as pixel coverage, not continuous gray interpolation (#3051).
if crate::dissolve_noise(x, y) < op { b } else { a }
} else {
mix_premul(a, b, op)
};
}
}
}
+26
View File
@@ -1900,6 +1900,14 @@ fn composite_layer_plain(layer: &Layer, clipped: &[Layer], backdrop: &mut Buffer
// Only `adjusted` has changed; this pixel's original backdrop is still in place.
let b = backdrop.px[i];
let a = adjusted.px[i];
if layer.blend == BlendMode::Dissolve {
// An adjustment has no standalone source alpha. Its effective coverage
// comes from its own opacity and mask, not a continuous RGB mix (#3050).
if dissolve_noise(x, y) < k {
backdrop.px[i] = [a[0], a[1], a[2], b[3]];
}
continue;
}
let blended = blend::blend_rgb(layer.blend, [b[0], b[1], b[2]], [a[0], a[1], a[2]]);
backdrop.px[i] = [b[0] + (blended[0] - b[0]) * k, b[1] + (blended[1] - b[1]) * k, b[2] + (blended[2] - b[2]) * k, b[3]];
}
@@ -2265,6 +2273,14 @@ fn composite_atop_any(layer: &Layer, base: &mut Buffer, cx: &Ctx) {
for (i, p) in base.px.iter_mut().enumerate() {
let k = layer.opacity * layer.fill_opacity * mask_k(&mv, i);
let a = adjusted.px[i];
if layer.blend == BlendMode::Dissolve {
let x = rect.x0 + (i % rect.width() as usize) as i32;
let y = rect.y0 + (i / rect.width() as usize) as i32;
if dissolve_noise(x, y) < k {
*p = [a[0], a[1], a[2], p[3]];
}
continue;
}
let bl = blend::blend_rgb(layer.blend, [p[0], p[1], p[2]], [a[0], a[1], a[2]]);
for c in 0..3 {
p[c] += (bl[c] - p[c]) * k;
@@ -2311,6 +2327,16 @@ fn composite_atop_any(layer: &Layer, base: &mut Buffer, cx: &Ctx) {
continue;
}
let s = content.px[i];
if layer.blend == BlendMode::Dissolve {
// Clipping preserves the opaque base's alpha. Dissolve still selects source
// pixels by spatial coverage instead of continuously mixing the colours (#3052).
let x = rect.x0 + (i as i32 % rect.width() as i32);
let y = rect.y0 + (i as i32 / rect.width() as i32);
if dissolve_noise(x, y) < s[3] * opacity {
*p = [s[0], s[1], s[2], alpha];
}
continue;
}
// Blend as if the base were opaque, then keep the base's alpha.
let r = blend::composite_gamma(layer.blend, [p[0], p[1], p[2], 1.0], s, opacity, gamma);
*p = [r[0], r[1], r[2], alpha];
+161
View File
@@ -505,6 +505,65 @@ fn clipping_mask_restricts_to_base_alpha() {
assert!(close4(px(&d, 6, 1), [1.0; 4]), "outside base: clipped layer hidden");
}
/// #3052 / #4921: clipped Dissolve must apply layer Opacity × Fill as binary coverage,
/// restricted to the clipping base's shape.
#[test]
fn clipped_dissolve_opacity_fill_is_binary_and_preserves_base_shape() {
let mut d = doc_white(40, 12);
d.layers.push(solid_layer("base", Rect::new(0, 0, 20, 12), [0.0, 0.0, 1.0, 1.0]));
let mut clip = solid_layer("clip", d.bounds(), [1.0, 0.0, 0.0, 1.0]);
clip.clipped = true;
clip.blend = BlendMode::Dissolve;
clip.opacity = 0.6;
clip.fill_opacity = 0.5;
d.layers.push(clip);
let mut red = 0;
let mut blue = 0;
for y in 0..12 {
for x in 0..20 {
let p = px(&d, x, y);
if close4(p, [1.0, 0.0, 0.0, 1.0]) {
red += 1;
} else {
assert!(close4(p, [0.0, 0.0, 1.0, 1.0]), "Dissolve must not interpolate: {p:?} at ({x},{y})");
blue += 1;
}
}
assert!(close4(px(&d, 30, y), [1.0; 4]), "outside the base the clip is invisible");
}
assert!(red > 0 && blue > 0, "Dissolve must expose both source and base: {red}/{blue}");
assert!((red as f32 / (red + blue) as f32 - 0.3).abs() < 0.1, "coverage should follow 60% opacity × 50% fill: {red}/{blue}");
}
/// Dissolve noise uses document coordinates, not the region origin; checked across
/// negative coordinates, tile boundaries, and all supported RGB sample depths.
#[test]
fn clipped_dissolve_matches_whole_render_at_offset_regions_and_depths() {
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
let mut d = Document::new("coverage", Size::new(32, 18), ColorMode::Rgb, fmt.sample);
let bounds = Rect::new(-3, -2, 32, 18);
let mut base = Layer::raster("base", fmt);
base.surface_mut().unwrap().fill_rect(bounds, &[0.0, 0.0, 1.0, 0.5]);
d.layers.push(base);
let mut clip = Layer::raster("clip", fmt);
clip.surface_mut().unwrap().fill_rect(bounds, &[1.0, 0.0, 0.0, 0.75]);
clip.clipped = true;
clip.blend = BlendMode::Dissolve;
clip.opacity = 0.6;
clip.fill_opacity = 0.5;
d.layers.push(clip);
let whole = render(&d, bounds);
for region in [Rect::new(-3, -2, 5, 4), Rect::new(5, 4, 17, 11), Rect::new(17, 11, 32, 18)] {
let part = render(&d, region);
for y in region.y0..region.y1 {
for x in region.x0..region.x1 {
assert!(close4(part.get(x, y), whole.get(x, y)), "{fmt:?}: region mismatch at ({x},{y})");
}
}
}
}
}
#[test]
fn hidden_base_hides_clipping_group() {
let mut d = doc_white(4, 4);
@@ -3740,3 +3799,105 @@ fn fully_masked_adjustment_tiles_preserve_output_and_skip_tone_work() {
}
}
}
#[test]
fn dissolve_adjustment_resolves_opacity_to_binary_coverage() {
let mut d = doc_white(16, 16);
let mut layer = Layer::new("Invert", LayerContent::Adjustment(Adjustment::Invert));
layer.blend = BlendMode::Dissolve;
layer.opacity = 0.5;
d.layers.push(layer);
let pixels = render(&d, d.bounds()).px;
let black = pixels.iter().filter(|p| p[0] < 0.01).count();
let white = pixels.iter().filter(|p| p[0] > 0.99).count();
assert!(black > 0 && white > 0, "Dissolve must select both original and adjusted pixels");
assert_eq!(black + white, pixels.len(), "no intermediate gray pixels");
d.layers.last_mut().unwrap().blend = BlendMode::Normal;
let continuous = render(&d, d.bounds()).px;
assert!(continuous.iter().all(|p| (p[0] - 0.5).abs() < E), "Normal remains continuously interpolated");
}
#[test]
fn clipped_dissolve_fill_matches_opaque_unclipped_coverage() {
let mut d = doc_white(16, 16);
let mut fill = Layer::new("Black", LayerContent::Fill(Fill::Solid(Color::BLACK)));
fill.blend = BlendMode::Dissolve;
fill.opacity = 0.5;
d.layers.push(fill);
let control = render(&d, d.bounds()).px;
d.layers.last_mut().unwrap().clipped = true;
let clipped = render(&d, d.bounds()).px;
assert_eq!(clipped, control, "clipping over a fully opaque base must preserve dissolve pixels");
let black = clipped.iter().filter(|p| p[0] < 0.01).count();
assert!(black > 0 && black < clipped.len());
d.layers.last_mut().unwrap().blend = BlendMode::Normal;
let continuous = render(&d, d.bounds()).px;
assert!(continuous.iter().all(|p| (p[0] - 0.5).abs() < E));
}
#[test]
fn dissolve_color_overlay_preserves_binary_layer_coverage() {
let mut d = doc_white(16, 16);
let mut layer = Layer::new("Black", LayerContent::Fill(Fill::Solid(Color::BLACK)));
layer.blend = BlendMode::Dissolve;
layer.opacity = 0.5;
d.layers.push(layer);
let control = render(&d, d.bounds()).px;
d.layers
.last_mut()
.unwrap()
.effects
.items
.push(photocraft_doc::Effect::ColorOverlay { common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), color: Color::BLACK });
let styled = render(&d, d.bounds()).px;
assert_eq!(styled, control, "an identity overlay should not turn dissolve pixels gray");
d.layers.last_mut().unwrap().blend = BlendMode::Normal;
let continuous = render(&d, d.bounds()).px;
assert!(continuous.iter().all(|p| (p[0] - 0.5).abs() < E));
}
#[test]
fn clipped_dissolve_adjustment_preserves_alpha_and_document_coverage() {
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
let mut d = Document::new("clipped adjustment", Size::new(272, 12), ColorMode::Rgb, fmt.sample);
let bounds = Rect::new(-3, -2, 272, 12);
let mut base = Layer::raster("base", fmt);
base.surface_mut().unwrap().fill_rect(bounds, &[1.0, 1.0, 1.0, 0.5]);
d.layers.push(base);
let original = render(&d, bounds);
let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert));
adjustment.clipped = true;
adjustment.blend = BlendMode::Dissolve;
adjustment.opacity = 0.6;
adjustment.fill_opacity = 0.5;
let mut mask = photocraft_raster::Surface::new(PixelFormat::GRAY8);
mask.fill_rect(Rect::new(-3, -2, 260, 12), &[1.0]);
adjustment.mask = Some(LayerMask { surface: mask, enabled: true, linked: true, density: 1.0, feather: 0.0 });
d.layers.push(adjustment);
let whole = render(&d, bounds);
let mut changed = 0;
for y in bounds.y0..bounds.y1 {
for x in bounds.x0..bounds.x1 {
let p = whole.get(x, y);
let value = if x < 260 && dissolve_noise(x, y) < 0.3 {
changed += 1;
0.0
} else {
1.0
};
assert!(close4(p, [value, value, value, original.get(x, y)[3]]), "{fmt:?} at ({x}, {y}): {p:?}");
}
}
assert!(changed > 0 && changed < whole.px.len());
let region = Rect::new(250, 1, 270, 10);
let part = render(&d, region);
for y in region.y0..region.y1 {
for x in region.x0..region.x1 {
assert!(close4(part.get(x, y), whole.get(x, y)), "{fmt:?} region at ({x}, {y})");
}
}
d.layers.last_mut().unwrap().blend = BlendMode::Normal;
let normal = render(&d, bounds);
assert!(normal.px.iter().any(|p| p[0] > 0.01 && p[0] < 0.99), "Normal keeps continuous mixing");
}
}
+15 -1
View File
@@ -935,6 +935,9 @@ fn fs_atop(in: VOut) -> @location(0) vec4<f32> {
let base = textureLoad(tex_a, p, 0);
if (base.a <= 0.0) { return base; }
let s = textureLoad(tex_b, p, 0);
if (op.mode == M_DISSOLVE) {
return select(base, vec4(s.rgb, base.a), dissolve_noise(doc_px(p)) < s.a * op.opacity);
}
let r = composite_g(op.mode, vec4(base.rgb, 1.0), s, op.opacity, (op.flags & F_TEXT_GAMMA) != 0u);
return vec4(r.rgb, base.a);
}
@@ -950,6 +953,7 @@ fn fs_adjust(in: VOut) -> @location(0) vec4<f32> {
}
// Mix an adjusted result B back over the original A with blend mode, opacity and mask.
// Both ordinary and clipped adjustments use this pass, retaining the original alpha.
@fragment
fn fs_adjmix(in: VOut) -> @location(0) vec4<f32> {
let p = local(in.pos);
@@ -957,6 +961,9 @@ fn fs_adjmix(in: VOut) -> @location(0) vec4<f32> {
let k = op.opacity * mask_value(doc_px(p));
if (k <= 0.0) { return b; }
let a = textureLoad(tex_b, p, 0);
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);
}
@@ -1280,7 +1287,14 @@ fn fs_fxmerge(in: VOut) -> @location(0) vec4<f32> {
let atop = (op.flags & F_ATOP) != 0u;
var before = c;
if (atop) { before = vec4(c.rgb, 1.0); }
let o = mix_premul(before, w, op.opacity);
var o: vec4<f32>;
if (op.mode == M_DISSOLVE) {
// Match the CPU effects compositor: opacity gates the completed stack, not
// its individual colours or alpha. Use document coordinates across chunks.
o = select(before, w, dissolve_noise(doc_px(p)) < op.opacity);
} else {
o = mix_premul(before, w, op.opacity);
}
if (atop) {
if (c.a > 0.0) { return vec4(o.rgb, c.a); }
return c;
+70
View File
@@ -794,6 +794,19 @@ fn groups_masks_and_clipping() {
d.layers.extend([base, c1, c2]);
check(&mut g, &d, &format!("clipping hidden={hidden}"));
}
// #3052/#4921: a clipped Dissolve layer retains opacity/fill binary coverage
// across the partially covered clipping base; the GPU must match the CPU.
for (opacity, fill) in [(0.4, 1.0), (0.8, 0.5)] {
let mut d = base_doc(56, 44);
let base = noise_layer("base", PixelFormat::RGBA8, Rect::new(5, 6, 48, 40), 45, 0.0);
let mut clip = noise_layer("dissolve clip", PixelFormat::RGBA8, d.bounds(), 46, 0.0);
clip.clipped = true;
clip.blend = BlendMode::Dissolve;
clip.opacity = opacity;
clip.fill_opacity = fill;
d.layers.extend([base, clip]);
check(&mut g, &d, "clipped Dissolve opacity/fill CPU/GPU parity");
}
// Clipped group base (isolated) with a clipped layer.
let mut d = base_doc(56, 44);
let mut gb = Layer::group("gb", vec![child(18, BlendMode::Normal)]);
@@ -2893,3 +2906,60 @@ fn soft_shapes_match_the_cpu() {
}
}
}
/// An adjustment has no standalone source alpha: its opacity and mask gate binary coverage.
#[test]
fn dissolve_adjustment_matches_cpu() {
let Some(mut g) = gpu() else { return };
let mut d = base_doc(64, 48);
let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert));
adjustment.blend = BlendMode::Dissolve;
adjustment.opacity = 0.5;
adjustment.mask = Some(mask(Rect::new(0, 0, 32, 48), 31, 0.0));
d.layers.push(adjustment);
check(&mut g, &d, "dissolve adjustment + mask");
}
/// Clipped pixels still use document-positioned noise, without changing the base alpha.
#[test]
fn dissolve_clipped_layer_matches_cpu() {
let Some(mut g) = gpu() else { return };
let mut d = base_doc(64, 48);
let mut clipped = Layer::new("clipped", LayerContent::Fill(Fill::Solid(Color::BLACK)));
clipped.clipped = true;
clipped.blend = BlendMode::Dissolve;
clipped.opacity = 0.5;
d.layers.push(clipped);
check(&mut g, &d, "dissolve clipped");
}
/// Layer opacity gates the completed effects stack, instead of fading its pixels.
#[test]
fn dissolve_effects_stack_matches_cpu() {
let Some(mut g) = gpu() else { return };
let mut d = base_doc(64, 48);
let mut styled = Layer::new("styled", LayerContent::Fill(Fill::Solid(Color::BLACK)));
styled.blend = BlendMode::Dissolve;
styled.opacity = 0.5;
styled.effects.items.push(Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), color: Color::BLACK });
d.layers.push(styled);
check(&mut g, &d, "dissolve effects");
}
#[test]
fn dissolve_clipped_adjustment_matches_cpu() {
let Some(mut g) = gpu() else { return };
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
let mut d = Document::new("clipped adjustment", Size::new(272, 24), ColorMode::Rgb, fmt.sample);
let base = noise_layer("base", fmt, Rect::new(-3, -2, 268, 22), 47, 0.0);
let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert));
adjustment.clipped = true;
adjustment.blend = BlendMode::Dissolve;
adjustment.opacity = 0.6;
adjustment.fill_opacity = 0.5;
adjustment.mask = Some(mask(Rect::new(0, 0, 260, 24), 48, 0.0));
d.layers.extend([base, adjustment]);
check(&mut g, &d, &format!("clipped Dissolve adjustment {fmt:?}"));
diff_rect(&mut g, &d, Rect::new(250, 1, 270, 20), "clipped adjustment offset region").unwrap_or_else(|e| panic!("{e}"));
}
}