Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(compose): layer effects in Dissolve mode speckle instead of painting as Normal (#6995)

Korg1che0d90b289652
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index 5acdd7931..a552cbeef 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -68,13 +68,18 @@ pub const MAX_REACH: f32 = 512.0;
/// shape's extent), Precise glows and chiselled bevels (exact Euclidean distances, whose f32
/// arithmetic depends on where the region starts) and noise (it speckles any coverage above
/// zero, so it would pick up the blur's float noise, which depends on where the region starts).
+/// Dissolve is not local for the same reason: it turns any coverage above zero into a chance of
+/// a full pixel.
pub(crate) fn maps_are_local(layer: &Layer) -> bool {
- layer.effects.items.iter().filter(|e| e.enabled()).all(|e| match e {
- Effect::Stroke(s) => !matches!(s.paint, FxPaint::Gradient(_)),
- Effect::DropShadow(s) | Effect::InnerShadow(s) => s.noise <= 0.0,
- Effect::OuterGlow(g) | Effect::InnerGlow(g) => g.technique != GlowTechnique::Precise && g.noise <= 0.0,
- Effect::BevelEmboss(b) => b.technique == BevelTechnique::Smooth,
- _ => true,
+ layer.effects.items.iter().filter(|e| e.enabled()).all(|e| {
+ !dissolves(e)
+ && match e {
+ Effect::Stroke(s) => !matches!(s.paint, FxPaint::Gradient(_)),
+ Effect::DropShadow(s) | Effect::InnerShadow(s) => s.noise <= 0.0,
+ Effect::OuterGlow(g) | Effect::InnerGlow(g) => g.technique != GlowTechnique::Precise && g.noise <= 0.0,
+ Effect::BevelEmboss(b) => b.technique == BevelTechnique::Smooth,
+ _ => true,
+ }
})
}
@@ -1108,16 +1113,43 @@ fn sample_stops(stops: &[(f32, [f32; 3])], t: f32) -> [f32; 3] {
/// Composite `color × coverage` into `dst` (both over the same rectangle).
fn paint(dst: &mut Buffer, m: &Map, color: impl Fn(usize) -> [f32; 4], blend: BlendMode, opacity: f32) {
+ let (x0, y0, w) = (i64::from(dst.rect.x0), i64::from(dst.rect.y0), (dst.rect.width().max(1)) as usize);
for (i, p) in dst.px.iter_mut().enumerate() {
let a = m.v[i] * opacity;
if a <= 0.0 {
continue;
}
let c = color(i);
+ if blend == BlendMode::Dissolve {
+ if dissolves_in(x0 + (i % w) as i64, y0 + (i / w) as i64, c[3] * a) {
+ *p = psblend::composite(BlendMode::Normal, *p, [c[0], c[1], c[2], 1.0], 1.0);
+ }
+ continue;
+ }
*p = psblend::composite(blend, *p, [c[0], c[1], c[2], c[3] * a], 1.0);
}
}
+/// Whether `e` paints anything in Dissolve mode.
+fn dissolves(e: &Effect) -> bool {
+ let d = |m: BlendMode| m == BlendMode::Dissolve;
+ match e {
+ Effect::DropShadow(s) | Effect::InnerShadow(s) => d(s.common.blend),
+ Effect::OuterGlow(g) | Effect::InnerGlow(g) => d(g.common.blend),
+ Effect::Stroke(s) => d(s.common.blend),
+ Effect::ColorOverlay { common, .. } | Effect::GradientOverlay { common, .. } | Effect::PatternOverlay { common, .. } => d(common.blend),
+ Effect::Satin(s) => d(s.common.blend),
+ Effect::BevelEmboss(b) => d(b.highlight.blend) || d(b.shadow.blend),
+ }
+}
+
+/// Dissolve on an effect (#6995): a pixel takes the effect fully or not at all, with probability
+/// `coverage`, picked by the same document-coordinate noise as a Dissolve layer. A soft glow or
+/// shadow turns into scattered speckle instead of a smooth falloff.
+fn dissolves_in(x: i64, y: i64, coverage: f32) -> bool {
+ hash_noise(x, y) < coverage
+}
+
fn paint_color(dst: &mut Buffer, m: &Map, c: [f32; 3], blend: BlendMode, opacity: f32) {
paint(dst, m, |_| [c[0], c[1], c[2], 1.0], blend, opacity);
}
@@ -2110,7 +2142,12 @@ pub(crate) fn composite_with_effects_prepared(
} else {
band
};
- *sh = k * st.common.opacity * (1.0 - cover[i]);
+ let mut k = k * st.common.opacity;
+ if st.common.blend == BlendMode::Dissolve {
+ let (x, y) = (i64::from(big.x0) + (i % w) as i64, i64::from(big.y0) + (i / w) as i64);
+ k = if k > 0.0 && dissolves_in(x, y, k) { 1.0 } else { 0.0 };
+ }
+ *sh = k * (1.0 - cover[i]);
cover[i] += *sh;
}
// The stroke blended over the backdrop at full coverage.
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index 6457b1192..cf804cc44 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -1016,6 +1016,16 @@ fn fx_color(d: vec2<i32>) -> vec4<f32> {
return op.color;
}
+// effects::paint: blend an effect's colour `s` (alpha = colour alpha × coverage × opacity) into
+// `dst` at document pixel `d`. Dissolve takes the effect fully or not at all (effects::dissolves_in).
+fn fx_composite(d: vec2<i32>, dst: vec4<f32>, s: vec4<f32>) -> vec4<f32> {
+ if (op.mode == M_DISSOLVE) {
+ if (dissolve_noise(d) < s.a) { return composite(M_NORMAL, dst, vec4(s.rgb, 1.0), 1.0); }
+ return dst;
+ }
+ return composite(op.mode, dst, s, 1.0);
+}
+
// Effect chain steps (`kind`): 0 A at `opacity` × alpha; 1 an opaque copy of A (a clipping base
// for clipped layers' effects); 2 A's colour with alpha `opacity` inside B's shape (the layer
// before its interior effects); 3 A with its alpha × B's alpha (the shape's own alpha); 4 the
@@ -1034,7 +1044,7 @@ fn fs_fxinit(in: VOut) -> @location(0) vec4<f32> {
let a = textureLoad(tex_b, p, 0).a;
let s = textureLoad(tex_c, p, 0);
if (s.a <= 0.0 || a <= INSIDE_EPS) { return c; }
- return composite(op.mode, c, vec4(s.rgb, op.opacity * min(s.a / a, 1.0)), 1.0);
+ return fx_composite(doc_px(p), c, vec4(s.rgb, op.opacity * min(s.a / a, 1.0)));
}
case 5: {
let a = textureLoad(tex_b, p, 0).a;
@@ -1083,12 +1093,12 @@ fn fs_fxpaint(in: VOut) -> @location(0) vec4<f32> {
let k = min(m * op.p0.x, 1.0) * op.opacity;
if (k <= 0.0) { return dst; }
let t = 1.0 - clamp(m, 0.0, 1.0);
- return composite(op.mode, dst, vec4(lut(0, t), lut(1, t), lut(2, t), lut(3, t) * k), 1.0);
+ return fx_composite(d, dst, vec4(lut(0, t), lut(1, t), lut(2, t), lut(3, t) * k));
}
let k = m * op.opacity;
if (k <= 0.0) { return dst; }
let c = fx_color(d);
- return composite(op.mode, dst, vec4(c.rgb, c.a * k), 1.0);
+ return fx_composite(d, dst, vec4(c.rgb, c.a * k));
}
// Outside strokes (A = the exterior result before them, B = the layer, C = coverage so far in
@@ -1125,7 +1135,12 @@ fn fs_fxstroke(in: VOut) -> @location(0) vec4<f32> {
k = clamp((1.0 - clamp(a, 0.0, 1.0)) / (1.0 - lay_a), 0.0, 1.0);
}
}
- let share = k * op.opacity * (1.0 - cover);
+ var cov = k * op.opacity;
+ if (op.mode == M_DISSOLVE) {
+ // effects.rs: a Dissolve stroke covers each pixel fully or not at all.
+ cov = select(0.0, 1.0, cov > 0.0 && dissolve_noise(d) < cov);
+ }
+ let share = cov * (1.0 - cover);
if (op.kind == 1) { return vec4(cover + share, lay_a, 0.0, 0.0); }
var acc = vec4(0.0);
if (!first) { acc = textureLoad(layer_tex, p, 0); }
diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs
index 58ce6e429..48cefb702 100644
--- a/crates/gpu/src/plan.rs
+++ b/crates/gpu/src/plan.rs
@@ -1278,7 +1278,9 @@ impl<'a> Planner<'a> {
p.c = cover.map(|c| self.retain(c));
p.d = acc;
let a = self.emit(p);
- let mut p = self.fx_pass_kernel(Kernel::FxStroke, w, content, &Paint::Color([0.0; 3]), BlendMode::Normal, st.common.opacity, flags, clip, sb);
+ // The stroke's mode rides along only for Dissolve, whose coverage is all or
+ // nothing in this pass as in the colour pass (#6995).
+ let mut p = self.fx_pass_kernel(Kernel::FxStroke, w, content, &Paint::Color([0.0; 3]), st.common.blend, st.common.opacity, flags, clip, sb);
p.adjust_kind = 1;
p.map = Some(map(i, 0));
p.c = cover;