PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf(gpu): a chisel bevel's two fields find their sharp edges once
xsvm5a93e93a4481
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex ad6b76313..d4f65dcb4 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -2319,6 +2319,14 @@ pub fn distance_field(kind: FieldKind, alpha: Vec<f32>, w: usize, h: usize, reac}}+/// Both chiselled bevel fields of `alpha`, `[BevelInside, BevelOutside]`: each is its+/// [`distance_field`], with the sharp edges they both start from found once.+pub fn bevel_distance_fields(alpha: Vec<f32>, w: usize, h: usize, reach: f32) -> [Vec<f32>; 2] {+ let s = Map { w, h, v: alpha };+ let sharp = sharp_edges(&s);+ [bevel_dist_inside(&s, &sharp, reach), bevel_dist_outside(&s, &sharp, reach)]+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rsindex 01e147d51..4b4ae5d57 100644--- a/crates/gpu/src/fx.rs+++ b/crates/gpu/src/fx.rs@@ -558,6 +558,21 @@ fn field_banded(kind: FieldKind, reach: i32, shape: &[f32], region: Rect, out: Rparts.concat()}+/// [`field`] of both bevel fields over the same `out`, `[BevelInside, BevelOutside]`: one window,+/// whose sharp edges are found once instead of once per field.+pub(crate) fn bevel_fields(reach: i32, shape: &[f32], region: Rect, out: Rect) -> [Vec<f32>; 2] {+ let [inside, outside] = [FieldKind::BevelInside, FieldKind::BevelOutside];+ if !threaded_inside(inside) {+ return [inside, outside].map(|k| field(k, reach, shape, region, out));+ }+ let win = out.inflate(field_radius(inside, reach) + 1).intersect(®ion);+ if win.is_empty() {+ return [Vec::new(), Vec::new()];+ }+ let f = photocraft_compose::effects::bevel_distance_fields(crop(shape, region, win), win.width() as usize, win.height() as usize, reach as f32);+ if win == out { f } else { f.map(|f| crop(&f, win, out)) }+}+/// The layer's shape (`compose::layer_shape`) over `r`, in parallel bands.pub(crate) fn shape(doc: &photocraft_doc::Document, layer: &Layer, r: Rect) -> Vec<f32> {if r.is_empty() {@@ -842,6 +857,30 @@ mod tests {assert!(too_small_noticed, "a 2 row halo went unnoticed, so this test guards nothing");}+ /// Both bevel fields from one window (sharp edges found once) are bit for bit the fields+ /// computed one at a time, over the whole region and over a part of it.+ #[test]+ fn paired_bevel_fields_equal_the_fields_one_at_a_time() {+ let region = Rect::new(-20, -10, 300, 290);+ let (w, h) = (region.width() as usize, region.height() as usize);+ let mut shape = vec![0.0f32; w * h];+ for y in 0..h {+ for x in 0..w {+ let (fx, fy) = (x as f32 - 150.0, y as f32 - 140.0);+ let r = (fx * fx + fy * fy).sqrt() + 8.0 * (fy * 0.07).sin() * (fx * 0.05).cos();+ shape[y * w + x] = (90.0 - r).clamp(0.0, 1.0) * if (x / 13 + y / 17) % 5 == 0 { 0.6 } else { 1.0 };+ }+ }+ let bits = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::<Vec<_>>();+ for reach in [3, 9, 40] {+ for out in [region, Rect::new(40, 30, 170, 120), Rect::new(-20, 200, 300, 290)] {+ let [inside, outside] = bevel_fields(reach, &shape, region, out);+ assert_eq!(bits(&inside), bits(&field(FieldKind::BevelInside, reach, &shape, region, out)), "inside, reach {reach}, {out:?}");+ assert_eq!(bits(&outside), bits(&field(FieldKind::BevelOutside, reach, &shape, region, out)), "outside, reach {reach}, {out:?}");+ }+ }+ }+/// #3646: a stroke's outer edge is the fitted 5 × 5 chamfer while it is narrow and the exact/// Euclidean transform once it is wide — the chamfer's octagon facets a wide stroke. The GPU/// builds it from the field its width picks (`stroke_outside_exact`), while the spread of dropdiff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex 8f8839b9b..a90258b4b 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -1814,19 +1814,28 @@ impl Compositor {layer.effects.items.iter().filter(|e| e.enabled()).map(|e| fx::program_with(e, &doc.global_light, vector_shape, &doc.patterns, anchor)).collect();let want = field_reaches(&progs);e.fields.retain(|k, _| want.contains_key(k));- for (&kind, &reach) in &want {+ // Each field to recompute: over which rectangle, exact up to which reach.+ let jobs: Vec<(FieldKind, Rect, i32)> = want+ .iter()+ .filter_map(|(&kind, &reach)| match e.fields.get(&kind).map(|(r, _)| *r) {+ Some(r) if r >= reach => (!damage.is_empty()).then(|| (kind, damage.inflate(fx::field_radius(kind, r)).intersect(®ion), r)),+ _ => Some((kind, region, reach)),+ })+ .collect();+ // A chiselled bevel's two fields over the same rectangle share their window's sharp edges.+ let job = |k: FieldKind| jobs.iter().find(|j| j.0 == k).map(|j| (j.1, j.2));+ let mut bevel = match (job(FieldKind::BevelInside), job(FieldKind::BevelOutside)) {+ (Some((out, reach)), b) if b == Some((out, reach)) => fx::bevel_fields(reach, &e.shape_cpu, region, out).map(Some),+ _ => [None, None],+ };+ for (kind, out, reach) in jobs {let have = e.fields.get(&kind).map(|(r, _)| *r);- let out = match have {- Some(r) if r >= reach => {- if damage.is_empty() {- continue;- }- damage.inflate(fx::field_radius(kind, r)).intersect(®ion)- }- _ => region,+ let pair = match kind {+ FieldKind::BevelInside => bevel[0].take(),+ FieldKind::BevelOutside => bevel[1].take(),+ _ => None,};- let reach = have.filter(|r| *r >= reach).unwrap_or(reach);- let v = fx::field(kind, reach, &e.shape_cpu, region, out);+ let v = pair.unwrap_or_else(|| fx::field(kind, reach, &e.shape_cpu, region, out));if have.is_none_or(|r| r < reach) {e.fields.insert(kind, (reach, Tex::map(device, "pc_fx_field", region, MAP32)));}