From e0d3d940c622f03e72b7d9338777b8311747372a Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 11:36:47 +0700 Subject: [PATCH 01/10] fix(compose): resolve Dissolve coverage across adjustment, effects, and clipping paths (#3050-#3052) --- crates/compose/src/effects.rs | 8 +++++- crates/compose/src/lib.rs | 18 ++++++++++++ crates/compose/src/tests.rs | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs index 5acdd79314..c50449d817 100644 --- a/crates/compose/src/effects.rs +++ b/crates/compose/src/effects.rs @@ -2172,7 +2172,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) + }; } } } diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 3cf2f1dc19..38c7dcfccc 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -1440,6 +1440,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]]; } @@ -1839,6 +1847,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]; diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index ddd4871548..9ec37ff884 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3214,3 +3214,57 @@ fn concurrent_effect_identity_misses_compute_once() { }); assert_eq!(keys.built.load(std::sync::atomic::Ordering::Relaxed), 1, "concurrent cache misses must compute one memoized identity"); } + +#[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)); +} From 61724b49f0fa32385e3ce1aaf3d1ccb7d0692fbe Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:21:50 +0700 Subject: [PATCH 02/10] fix(gpu): match CPU Dissolve coverage for adjustment, clipping and effects --- crates/gpu/src/compose.wgsl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 6457b1192c..8aeea51d48 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -833,6 +833,9 @@ fn fs_atop(in: VOut) -> @location(0) vec4 { 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); } @@ -855,6 +858,9 @@ fn fs_adjmix(in: VOut) -> @location(0) vec4 { 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); } @@ -1174,7 +1180,9 @@ fn fs_fxmerge(in: VOut) -> @location(0) vec4 { 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); + let o = select(mix_premul(before, w, op.opacity), + select(before, w, dissolve_noise(doc_px(p)) < op.opacity), + op.mode == M_DISSOLVE); if (atop) { if (c.a > 0.0) { return vec4(o.rgb, c.a); } return c; From 28807ed7ab13775487e60b816ed51d19a6aa9d2a Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:21:57 +0700 Subject: [PATCH 03/10] test(gpu): cover Dissolve adjustment, clipping and effect parity --- crates/gpu/tests/parity.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 84d3d0e937..3f69affa1c 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -2618,3 +2618,35 @@ fn pass_through_clipping_base_with_an_adjustment() { d.layers.push(clip); check(&mut g, &d, "pass-through clipping base with an adjustment"); } + + +/// Dissolve must use document-coordinate binary coverage on both renderers. +#[test] +fn dissolve_specialized_paths_match_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; + d.layers.push(adjustment); + check(&mut g, &d, "dissolve adjustment"); + + 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"); + + 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"); +} From 21871dd40e75049953bef26d78a2cbb4902e5c9d Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:35:26 +0700 Subject: [PATCH 04/10] refactor(gpu): branch explicitly on Dissolve effect-stack coverage --- crates/gpu/src/compose.wgsl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 8aeea51d48..439cbafe38 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -1180,9 +1180,14 @@ fn fs_fxmerge(in: VOut) -> @location(0) vec4 { let atop = (op.flags & F_ATOP) != 0u; var before = c; if (atop) { before = vec4(c.rgb, 1.0); } - let o = select(mix_premul(before, w, op.opacity), - select(before, w, dissolve_noise(doc_px(p)) < op.opacity), - op.mode == M_DISSOLVE); + var o: vec4; + 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; From 7e1c21e9a86681d34174f224662f76a818a0057f Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:36:02 +0700 Subject: [PATCH 05/10] test(gpu): split Dissolve parity cases and exercise adjustment masking --- crates/gpu/tests/parity.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 3f69affa1c..ac2c04ee14 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -2620,17 +2620,23 @@ fn pass_through_clipping_base_with_an_adjustment() { } -/// Dissolve must use document-coordinate binary coverage on both renderers. +/// An adjustment has no standalone source alpha: its opacity and mask gate binary coverage. #[test] -fn dissolve_specialized_paths_match_cpu() { +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"); + 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; @@ -2638,7 +2644,12 @@ fn dissolve_specialized_paths_match_cpu() { 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; From 157711d1be64f2ff9f885199fd2c216afb9a53e8 Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:38:27 +0700 Subject: [PATCH 06/10] style(compose): apply rustfmt layout from CI for Dissolve regression --- crates/compose/src/tests.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 9ec37ff884..3d550201fa 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3258,10 +3258,12 @@ fn dissolve_color_overlay_preserves_binary_layer_coverage() { 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, - }); + 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; From d127cd6afb285da8816bdd439f5d4f337029fada Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:38:36 +0700 Subject: [PATCH 07/10] style(gpu): match rustfmt compact ColorOverlay initialization --- crates/gpu/tests/parity.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index ac2c04ee14..474a6e8e89 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -2654,10 +2654,7 @@ fn dissolve_effects_stack_matches_cpu() { 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, - }); + 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"); } From 9050e86dac506fa7561a583658d32ee1ac67afb2 Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Wed, 14 Oct 2026 04:54:00 +0700 Subject: [PATCH 08/10] test(compose): preserve clipped Dissolve coverage across fill, boundaries, and regions --- crates/compose/src/tests.rs | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 3d550201fa..d02ddb7000 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -316,6 +316,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); From 2ef53bb673b728eb745158a346738e37e1e26f4f Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Wed, 14 Oct 2026 04:54:04 +0700 Subject: [PATCH 09/10] test(gpu): cover clipped Dissolve opacity and fill parity --- crates/gpu/tests/parity.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 474a6e8e89..b40d3786a3 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -735,6 +735,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)]); From a221622ad4742e096f1f92d609acd2d7ee00bb7c Mon Sep 17 00:00:00 2001 From: Ni-zav Date: Mon, 19 Oct 2026 07:18:53 +0700 Subject: [PATCH 10/10] fix(compose): cover clipped Dissolve adjustments Port the clipped-adjustment coverage path from #4711 and verify depth, alpha, masks, and document-coordinate GPU parity. Co-authored-by: Vikram Bhaskaran --- crates/compose/src/lib.rs | 8 +++++++ crates/compose/src/tests.rs | 46 +++++++++++++++++++++++++++++++++++++ crates/gpu/src/compose.wgsl | 1 + crates/gpu/tests/parity.rs | 19 ++++++++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 38c7dcfccc..cebc6a64a1 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -1793,6 +1793,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; diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index d02ddb7000..1d967e6fc6 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -3329,3 +3329,49 @@ fn dissolve_color_overlay_preserves_binary_layer_coverage() { 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"); + } +} diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 439cbafe38..c495618539 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -851,6 +851,7 @@ fn fs_adjust(in: VOut) -> @location(0) vec4 { } // 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 { let p = local(in.pos); diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index b40d3786a3..3f59aa710f 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -2632,7 +2632,6 @@ fn pass_through_clipping_base_with_an_adjustment() { check(&mut g, &d, "pass-through clipping base with an adjustment"); } - /// An adjustment has no standalone source alpha: its opacity and mask gate binary coverage. #[test] fn dissolve_adjustment_matches_cpu() { @@ -2671,3 +2670,21 @@ fn dissolve_effects_stack_matches_cpu() { 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}")); + } +}