authoravatarclover caruso<git@paperclover.net>2026-12-01 04:02:08-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 05:21:12-08:00
log060671ff6777497c1ad242a091b015483184035f
tree297cc62a5485cc86dbbd3e9d0e45cadffc1b0bb6
parent40a16ea368e780b8fcd768204a0123c75dff7e5e
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Wave 16A: shapes hang, Scale sizing, positioner/shake/plasma parity

- sNGon/sRectangle/sEllipse: clip giant diagonal polygons to the canvas before zeno (Width=256 stalled the rasterizer); Alpha scales alpha only, Opacity scales RGBA, matching Fusion's unpremultiplied shape colour - Scale: LockXY follows XSize, unlocked YSize defaults to 1, each side rounds up to the source's parity - CornerPositioner/PerspectivePositioner: Fusion default corners, per-corner source-UV offsets, bilinear mapping, Process* channels, PerspectivePositioner defaults to Bi-Linear - CameraShake: single-sine model fitted to Fusion (Speed rate table, per-axis rates, Randomness bias), InvertTransform, MultiplyByMask, Process* channels - Plasma: Operation folds the four circle terms (None counts as 0) - Effect mask MaskChannel/MaskLow/MaskHigh remap for the tools above - Drip: Process* channels; MultiplyByMask with a PolylineMask crops to the mask domain Assisted-by: claude-sonnet-5.5

7 files changed, 401 insertions(+), 112 deletions(-)

crates/engine/src/eval/io.rs+34-6
...@@ -630,11 +630,17 @@ impl Engine {...@@ -630,11 +630,17 @@ impl Engine {
630 self.scaled_dims(w, h)630 self.scaled_dims(w, h)
631 } else {631 } else {
632 let sx = comp.eval_number(node, "XSize", time, 1.0, &self.expr);632 let sx = comp.eval_number(node, "XSize", time, 1.0, &self.expr);
633 let sy = comp.eval_number(node, "YSize", time, sx, &self.expr);633 let sy = if comp.eval_number(node, "LockXY", time, 1.0, &self.expr) > 0.5 {
634 [634 sx
635 ((src.size[0] as f64 * sx).round() as u32).max(1),635 } else {
636 ((src.size[1] as f64 * sy).round() as u32).max(1),636 comp.eval_number(node, "YSize", time, 1.0, &self.expr)
637 ]637 };
638 // Fusion rounds each side up to the next size with the source's parity so the image center stays pixel-aligned.
639 let scaled = |src: u32, s: f64| {
640 let n = (src as f64 * s - 1e-4).ceil().max(1.0) as u32;
641 if (n + src) % 2 == 1 { n + 1 } else { n }
642 };
643 [scaled(src.size[0], sx), scaled(src.size[1], sy)]
638 };644 };
639 let nearest = if comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr) < 0.5 { 1.0 } else { 0.0 };645 let nearest = if comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr) < 0.5 { 1.0 } else { 0.0 };
640 let mut u = UniformParams::default();646 let mut u = UniformParams::default();
...@@ -865,7 +871,7 @@ impl Engine {...@@ -865,7 +871,7 @@ impl Engine {
865 let b_phase = comp.eval_number(node, "BluePhase", time, 0.3125, &self.expr) as f32;871 let b_phase = comp.eval_number(node, "BluePhase", time, 0.3125, &self.expr) as f32;
866 let a_phase = comp.eval_number(node, "AlphaPhase", time, 0.4375, &self.expr) as f32;872 let a_phase = comp.eval_number(node, "AlphaPhase", time, 0.4375, &self.expr) as f32;
867873
868 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats);874 let (mask, mask_inv) = self.eval_mask_levels(comp, node, time, size, enc, visiting, stats);
869 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);875 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
870876
871 let mut u = UniformParams::default();877 let mut u = UniformParams::default();
...@@ -876,6 +882,7 @@ impl Engine {...@@ -876,6 +882,7 @@ impl Engine {
876 u.p[4] = [scale, phase, r_phase, g_phase];882 u.p[4] = [scale, phase, r_phase, g_phase];
877 u.p[5] = [b_phase, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];883 u.p[5] = [b_phase, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
878 u.p[6][0] = a_phase;884 u.p[6][0] = a_phase;
885 u.p[6][1] = comp.eval_number(node, "Operation", time, 0.0, &self.expr).round() as f32;
879886
880 let dst_depth = self.node_depth(comp, node, time, None);887 let dst_depth = self.node_depth(comp, node, time, None);
881 let key = self.make_key(node, "Plasma", size, &[mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..7]));888 let key = self.make_key(node, "Plasma", size, &[mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..7]));
...@@ -1578,4 +1585,25 @@ Composition {{...@@ -1578,4 +1585,25 @@ Composition {{
1578 assert!(!comp.node(comp.find_node("L1").unwrap()).clips[0].loop_clip);1585 assert!(!comp.node(comp.find_node("L1").unwrap()).clips[0].loop_clip);
1579 assert!(!comp.node(comp.find_node("LOmit").unwrap()).clips[0].loop_clip);1586 assert!(!comp.node(comp.find_node("LOmit").unwrap()).clips[0].loop_clip);
1580 }1587 }
1588
1589 #[test]
1590 fn scale_rounds_each_side_up_to_the_source_parity_and_lock_xy_follows_x() {
1591 let src = r#"Composition { Tools = ordered() {
1592 Src = Background { Inputs = { Width = Input { Value = 255, }, Height = Input { Value = 253, }, }, },
1593 Locked = Scale { Inputs = { XSize = Input { Value = 1.5, }, YSize = Input { Value = 3, }, Input = Input { SourceOp = "Src", Source = "Output", }, }, },
1594 Free = Scale { Inputs = { LockXY = Input { Value = 0, }, XSize = Input { Value = 0.5, }, Input = Input { SourceOp = "Src", Source = "Output", }, }, },
1595 }, }"#;
1596 let comp = Comp::from_fusion_str(src).expect("parse scale comp");
1597 let Ok(mut eng) = Engine::new_headless() else { return };
1598 let (tex, _) = eng.evaluate(&comp, comp.find_node("Locked").unwrap(), 0.0);
1599 assert_eq!(tex.size, [383, 381], "382.5 and 379.5 round up to odd sizes");
1600 let (tex, _) = eng.evaluate(&comp, comp.find_node("Free").unwrap(), 0.0);
1601 assert_eq!(tex.size, [129, 253], "unlocked YSize keeps its own default of 1");
1602 }
1603
1604 #[test]
1605 fn plasma_operation_counts_none_circles_as_zero() {
1606 let Some(px) = render_generator("Plasma", 32, 32, "Operation = Input { Value = 2, }, Circle3Type = Input { Value = 0, },") else { return };
1607 assert!(px.iter().all(|p| *p == px[0]), "product with a zero term is a constant image");
1608 }
1581}1609}
crates/engine/src/eval/shapes.rs+96-8
...@@ -225,6 +225,7 @@ fn collect_shape_tree(...@@ -225,6 +225,7 @@ fn collect_shape_tree(
225 let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32;225 let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32;
226 let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32;226 let b = comp.eval_number(node, "Blue", time, 1.0, expr) as f32;
227 let a = comp.eval_number(node, "Alpha", time, 1.0, expr) as f32;227 let a = comp.eval_number(node, "Alpha", time, 1.0, expr) as f32;
228 let opacity = comp.eval_number(node, "Opacity", time, 1.0, expr) as f32;
228229
229 let mut solid = comp.eval_number(node, "Solid", time, 1.0, expr) > 0.5;230 let mut solid = comp.eval_number(node, "Solid", time, 1.0, expr) > 0.5;
230 let mut border_width = comp.eval_number(node, "BorderWidth", time, 0.01, expr).abs() as f32;231 let mut border_width = comp.eval_number(node, "BorderWidth", time, 0.01, expr).abs() as f32;
...@@ -261,7 +262,7 @@ fn collect_shape_tree(...@@ -261,7 +262,7 @@ fn collect_shape_tree(
261 Some(ShapeTree::Primitive {262 Some(ShapeTree::Primitive {
262 kind,263 kind,
263 xform,264 xform,
264 color: [r, g, b, a],265 color: [r * opacity, g * opacity, b * opacity, a * opacity],
265 solid,266 solid,
266 border_width,267 border_width,
267 join_style,268 join_style,
...@@ -517,6 +518,67 @@ fn build_primitive_commands(kind: ShapePrimKind, xform: Affine2D, dims: [u32; 2]...@@ -517,6 +518,67 @@ fn build_primitive_commands(kind: ShapePrimKind, xform: Affine2D, dims: [u32; 2]
517 cmds518 cmds
518}519}
519520
521/// Flattens `cmds` and clips the polygon to the canvas grown by `margin`; zeno stalls on diagonal edges tens of canvases long.
522fn clip_to_viewport(cmds: Vec<zeno::Command>, dims: [u32; 2], margin: f32) -> Vec<zeno::Command> {
523 use zeno::Command::{Close, CurveTo, LineTo, MoveTo};
524 let [w, h] = [dims[0] as f32, dims[1] as f32];
525 let limit = 4.0 * w.max(h);
526 let far = |v: &zeno::Vector| v.x.abs() > limit || v.y.abs() > limit;
527 let huge = cmds.iter().any(|c| match c {
528 MoveTo(p) | LineTo(p) => far(p),
529 CurveTo(a, b, p) => far(a) || far(b) || far(p),
530 _ => false,
531 });
532 if !huge {
533 return cmds;
534 }
535 let mut poly: Vec<[f32; 2]> = Vec::new();
536 for c in &cmds {
537 match c {
538 MoveTo(p) | LineTo(p) => poly.push([p.x, p.y]),
539 CurveTo(a, b, p) => {
540 let p0 = poly.last().copied().unwrap_or([a.x, a.y]);
541 let len = (a.x - p0[0]).hypot(a.y - p0[1]) + (b.x - a.x).hypot(b.y - a.y) + (p.x - b.x).hypot(p.y - b.y);
542 let n = ((len.sqrt() * 1.5).ceil() as usize).clamp(16, 2048);
543 for i in 1..=n {
544 let t = i as f32 / n as f32;
545 let u = 1.0 - t;
546 let (c0, c1, c2, c3) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t);
547 poly.push([
548 c0 * p0[0] + c1 * a.x + c2 * b.x + c3 * p.x,
549 c0 * p0[1] + c1 * a.y + c2 * b.y + c3 * p.y,
550 ]);
551 }
552 }
553 _ => {}
554 }
555 }
556 let edges: [(usize, f32, f32); 4] = [(0, 1.0, -margin), (0, -1.0, w + margin), (1, 1.0, -margin), (1, -1.0, h + margin)];
557 for (axis, sign, bound) in edges {
558 let inside = |p: [f32; 2]| sign * (p[axis] - bound) >= 0.0;
559 let input = std::mem::take(&mut poly);
560 for (i, &cur) in input.iter().enumerate() {
561 let prev = input[(i + input.len() - 1) % input.len()];
562 if inside(cur) != inside(prev) {
563 let t = (bound - prev[axis]) / (cur[axis] - prev[axis]);
564 poly.push([prev[0] + t * (cur[0] - prev[0]), prev[1] + t * (cur[1] - prev[1])]);
565 }
566 if inside(cur) {
567 poly.push(cur);
568 }
569 }
570 }
571 let mut out = Vec::with_capacity(poly.len() + 1);
572 for (i, p) in poly.iter().enumerate() {
573 let v = zeno::Vector::new(p[0], p[1]);
574 out.push(if i == 0 { MoveTo(v) } else { LineTo(v) });
575 }
576 if !out.is_empty() {
577 out.push(Close);
578 }
579 out
580}
581
520fn composite_over(dst: &mut [f32], src: &[f32]) {582fn composite_over(dst: &mut [f32], src: &[f32]) {
521 for (d, s) in dst.chunks_exact_mut(4).zip(src.chunks_exact(4)) {583 for (d, s) in dst.chunks_exact_mut(4).zip(src.chunks_exact(4)) {
522 let sa = s[3].clamp(0.0, 1.0);584 let sa = s[3].clamp(0.0, 1.0);
...@@ -554,10 +616,11 @@ fn rasterize_shape_tree_f32(...@@ -554,10 +616,11 @@ fn rasterize_shape_tree_f32(
554 return;616 return;
555 }617 }
556 cov_buf.fill(0);618 cov_buf.fill(0);
619 let stroke_px = (*border_width * xform.avg_scale() * (w as f32)).max(0.5);
620 let cmds = clip_to_viewport(cmds, dims, if *solid { 4.0 } else { 8.0 + 6.0 * stroke_px });
557 let style = if *solid {621 let style = if *solid {
558 zeno::Style::Fill(zeno::Fill::NonZero)622 zeno::Style::Fill(zeno::Fill::NonZero)
559 } else {623 } else {
560 let stroke_px = (*border_width * xform.avg_scale() * (w as f32)).max(0.5);
561 let join = match *join_style {624 let join = match *join_style {
562 0 => zeno::Join::Bevel,625 0 => zeno::Join::Bevel,
563 1 => zeno::Join::Round,626 1 => zeno::Join::Round,
...@@ -582,16 +645,17 @@ fn rasterize_shape_tree_f32(...@@ -582,16 +645,17 @@ fn rasterize_shape_tree_f32(
582 if ca <= 1e-6 {645 if ca <= 1e-6 {
583 return;646 return;
584 }647 }
648 // Fusion keeps the colour unpremultiplied: Alpha scales coverage into alpha only, not RGB.
585 for (px, &cov) in dst.chunks_exact_mut(4).zip(cov_buf.iter()) {649 for (px, &cov) in dst.chunks_exact_mut(4).zip(cov_buf.iter()) {
586 if cov == 0 {650 if cov == 0 {
587 continue;651 continue;
588 }652 }
589 let sa = (cov as f32 / 255.0) * ca;653 let c = cov as f32 / 255.0;
590 let inv = 1.0 - sa;654 let inv = 1.0 - c * ca;
591 px[0] = color[0] * sa + px[0] * inv;655 px[0] = color[0] * c + px[0] * inv;
592 px[1] = color[1] * sa + px[1] * inv;656 px[1] = color[1] * c + px[1] * inv;
593 px[2] = color[2] * sa + px[2] * inv;657 px[2] = color[2] * c + px[2] * inv;
594 px[3] = sa + px[3] * inv;658 px[3] = c * ca + px[3] * inv;
595 }659 }
596 }660 }
597 ShapeTree::Group(children) => {661 ShapeTree::Group(children) => {
...@@ -1006,4 +1070,28 @@ mod tests {...@@ -1006,4 +1070,28 @@ mod tests {
1006 assert_eq!(&px_sr[(32 * 64 + 48) * 4..(32 * 64 + 48) * 4 + 4], &[255, 255, 255, 255]);1070 assert_eq!(&px_sr[(32 * 64 + 48) * 4..(32 * 64 + 48) * 4 + 4], &[255, 255, 255, 255]);
1007 assert_eq!(&px_sr[0..4], &[0, 0, 0, 0]);1071 assert_eq!(&px_sr[0..4], &[0, 0, 0, 0]);
1008 }1072 }
1073 #[test]
1074 fn test_giant_rotated_shapes_clip_instead_of_hanging_and_alpha_stays_unpremultiplied() {
1075 let src = r#"Composition {
1076 CurrentTime = 0,
1077 RenderRange = { 0, 1 },
1078 GlobalRange = { 0, 1 },
1079 Tools = ordered() {
1080 NG = sNGon { Inputs = { Width = Input { Value = 256, }, Height = Input { Value = 256, }, Alpha = Input { Value = 0.25, }, Opacity = Input { Value = 0.5, }, Blue = Input { Value = 0.5, }, }, },
1081 Rect = sRectangle { Inputs = { Width = Input { Value = 256, }, Height = Input { Value = 256, }, Angle = Input { Value = 33, }, }, },
1082 SRN = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Input = Input { SourceOp = "NG", Source = "Output", }, }, },
1083 SRR = sRender { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, Input = Input { SourceOp = "Rect", Source = "Output", }, }, },
1084 },
1085 }"#;
1086 let comp = Comp::from_fusion_str(src).unwrap();
1087 let Ok(mut eng) = Engine::new_headless() else { return; };
1088 let (t, _) = eng.evaluate(&comp, comp.find_node("SRN").unwrap(), 0.0);
1089 let px = eng.readback_rgba(&t).unwrap();
1090 let mid = (16 * 32 + 16) * 4;
1091 assert_eq!(&px[mid..mid + 4], &[128, 128, 64, 32], "Opacity scales RGBA, Alpha scales alpha only");
1092 let (t, _) = eng.evaluate(&comp, comp.find_node("SRR").unwrap(), 0.0);
1093 let px = eng.readback_rgba(&t).unwrap();
1094 assert_eq!(&px[mid..mid + 4], &[255, 255, 255, 255]);
1095 assert_eq!(&px[0..4], &[255, 255, 255, 255], "giant rotated rectangle covers the corner");
1096 }
1009}1097}
crates/engine/src/eval/transform.rs+149-68
...@@ -849,6 +849,45 @@ impl Engine {...@@ -849,6 +849,45 @@ impl Engine {
849 && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5849 && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5
850 }850 }
851851
852 /// `1.0` per channel whose `Process{Red,Green,Blue,Alpha}` is off, so the shader keeps the source value there.
853 pub(super) fn process_skip(&self, comp: &Comp, node: &Node, time: f64) -> [f32; 4] {
854 ["ProcessRed", "ProcessGreen", "ProcessBlue", "ProcessAlpha"]
855 .map(|c| if comp.eval_number(node, c, time, 1.0, &self.expr) > 0.5 { 0.0 } else { 1.0 })
856 }
857
858 /// `eval_mask_input_sized`, then the effect mask's `MaskChannel` selection and `MaskLow`/`MaskHigh` remap.
859 #[allow(clippy::too_many_arguments)]
860 pub(super) fn eval_mask_levels(
861 &mut self,
862 comp: &Comp,
863 node: &Node,
864 time: f64,
865 target_size: [u32; 2],
866 enc: &mut wgpu::CommandEncoder,
867 visiting: &mut HashSet<(NodeId, i64)>,
868 stats: &mut EvalStats,
869 ) -> (Option<CachedTexture>, bool) {
870 let (mask, inverted) = self.eval_mask_input_sized(comp, node, time, Some(target_size), enc, visiting, stats);
871 let channel = comp.eval_number(node, "MaskChannel", time, 3.0, &self.expr).round() as usize;
872 let low = comp.eval_number(node, "MaskLow", time, 0.0, &self.expr) as f32;
873 let high = comp.eval_number(node, "MaskHigh", time, 1.0, &self.expr) as f32;
874 // Coverage carries nothing for these images, so it reads as an empty mask.
875 let (low, high) = if channel == 7 { (2.0, 2.0) } else { (low, high) };
876 let remap = channel != 3 || low != 0.0 || high != 1.0;
877 let Some(m) = mask.clone().filter(|_| remap) else {
878 return (mask, inverted);
879 };
880 // Red, Green, Blue, Alpha, Hue, Luminance, Saturation, Coverage as `BitmapMask` channel ids.
881 let bitmap_channel: f32 = [2.0, 3.0, 4.0, 0.0, 7.0, 1.0, 5.0, 0.0][channel.min(7)];
882 let mut h = DefaultHasher::new();
883 (m.content_hash, bitmap_channel.to_bits(), low.to_bits(), high.to_bits()).hash(&mut h);
884 let out = create_texture(&self.device, m.size, h.finish());
885 let mut u = UniformParams::default();
886 u.p[0] = [bitmap_channel, low, high, 1.0];
887 self.dispatch_op(enc, ShaderOp::BitmapMask, &m.view, &self.dummy.view, &self.dummy.view, &out, &u);
888 (Some(out), inverted)
889 }
890
852 pub(super) fn eval_displace(891 pub(super) fn eval_displace(
853 &mut self,892 &mut self,
854 comp: &Comp,893 comp: &Comp,
...@@ -971,9 +1010,8 @@ impl Engine {...@@ -971,9 +1010,8 @@ impl Engine {
971 dst1010 dst
972 }1011 }
9731012
974 /// Computes inverse 3x3 projective homography mapping destination quad `(bl, br, tr, tl)` back to unit square `[0,1]^2`.1013 /// Row-major projective matrix (`h22 = 1`) taking the unit square's `(bl, br, tr, tl)` corners to the given quad.
975 pub(super) fn quad_inverse_homography(bl: [f64; 2], br: [f64; 2], tr: [f64; 2], tl: [f64; 2]) -> [f32; 9] {1014 fn quad_homography(bl: [f64; 2], br: [f64; 2], tr: [f64; 2], tl: [f64; 2]) -> Option<[f64; 9]> {
976 // Forward H maps (0,0)->bl, (1,0)->br, (1,1)->tr, (0,1)->tl:
977 let dx1 = br[0] - tr[0];1015 let dx1 = br[0] - tr[0];
978 let dy1 = br[1] - tr[1];1016 let dy1 = br[1] - tr[1];
979 let dx2 = tl[0] - tr[0];1017 let dx2 = tl[0] - tr[0];
...@@ -982,28 +1020,37 @@ impl Engine {...@@ -982,28 +1020,37 @@ impl Engine {
982 let sy = bl[1] - br[1] + tr[1] - tl[1];1020 let sy = bl[1] - br[1] + tr[1] - tl[1];
983 let denom = dx1 * dy2 - dx2 * dy1;1021 let denom = dx1 * dy2 - dx2 * dy1;
984 if denom.abs() < 1e-10 {1022 if denom.abs() < 1e-10 {
985 return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];1023 return None;
986 }1024 }
987 let g = (sx * dy2 - sy * dx2) / denom;1025 let g = (sx * dy2 - sy * dx2) / denom;
988 let h = (dx1 * sy - dy1 * sx) / denom;1026 let h = (dx1 * sy - dy1 * sx) / denom;
989 let a = br[0] - bl[0] + g * br[0];1027 Some([
990 let b = tl[0] - bl[0] + h * tl[0];1028 br[0] - bl[0] + g * br[0],
991 let c = bl[0];1029 tl[0] - bl[0] + h * tl[0],
992 let d = br[1] - bl[1] + g * br[1];1030 bl[0],
993 let e = tl[1] - bl[1] + h * tl[1];1031 br[1] - bl[1] + g * br[1],
994 let f = bl[1];1032 tl[1] - bl[1] + h * tl[1],
995 // Adjugate (inverse up to scale) of [[a,b,c],[d,e,f],[g,h,1]]:1033 bl[1],
996 [1034 g,
997 (e - f * h) as f32,1035 h,
998 (c * h - b) as f32,1036 1.0,
999 (b * f - c * e) as f32,1037 ])
1000 (f * g - d) as f32,1038 }
1001 (a - c * g) as f32,1039
1002 (c * d - a * f) as f32,1040 /// Adjugate (inverse up to scale) of a row-major 3x3 matrix.
1003 (d * h - e * g) as f32,1041 fn adjugate(m: &[f64; 9]) -> [f64; 9] {
1004 (b * g - a * h) as f32,1042 let [a, b, c, d, e, f, g, h, i] = *m;
1005 (a * e - b * d) as f32,1043 [e * i - f * h, c * h - b * i, b * f - c * e, f * g - d * i, a * i - c * g, c * d - a * f, d * h - e * g, b * g - a * h, a * e - b * d]
1006 ]1044 }
1045
1046 fn mat_mul(a: &[f64; 9], b: &[f64; 9]) -> [f64; 9] {
1047 std::array::from_fn(|k| (0..3).map(|j| a[k / 3 * 3 + j] * b[j * 3 + k % 3]).sum())
1048 }
1049
1050 /// Inverse 3x3 projective homography mapping destination quad `(bl, br, tr, tl)` back to unit square `[0,1]^2`.
1051 pub(super) fn quad_inverse_homography(bl: [f64; 2], br: [f64; 2], tr: [f64; 2], tl: [f64; 2]) -> [f32; 9] {
1052 Self::quad_homography(bl, br, tr, tl)
1053 .map_or([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], |h| Self::adjugate(&h).map(|v| v as f32))
1007 }1054 }
10081055
1009 pub(super) fn eval_corner_positioner(1056 pub(super) fn eval_corner_positioner(
...@@ -1025,12 +1072,28 @@ impl Engine {...@@ -1025,12 +1072,28 @@ impl Engine {
1025 return src;1072 return src;
1026 }1073 }
1027 let src = self.crop_to_canvas(enc, src);1074 let src = self.crop_to_canvas(enc, src);
1028 let bl = comp.eval_point(node, "BottomLeft", time, [0.0, 0.0], &self.expr);1075 // Corner `Offset`s shift the matching source-UV corner against the offset, so positive values extend the sampled quad.
1029 let br = comp.eval_point(node, "BottomRight", time, [1.0, 0.0], &self.expr);1076 let corner = |name: &str, default: [f64; 2]| {
1030 let tr = comp.eval_point(node, "TopRight", time, [1.0, 1.0], &self.expr);1077 let p = comp.eval_point(node, name, time, default, &self.expr);
1031 let tl = comp.eval_point(node, "TopLeft", time, [0.0, 1.0], &self.expr);1078 let off = [
1032 let h_inv = Self::quad_inverse_homography(bl, br, tr, tl);1079 comp.eval_number(node, &format!("{name}OffsetX"), time, 0.0, &self.expr),
1033 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);1080 comp.eval_number(node, &format!("{name}OffsetY"), time, 0.0, &self.expr),
1081 ];
1082 (p, off)
1083 };
1084 let [(bl, bl_o), (br, br_o), (tr, tr_o), (tl, tl_o)] = [
1085 corner("BottomLeft", [0.0, 0.3]),
1086 corner("BottomRight", [1.0, 0.1]),
1087 corner("TopRight", [0.9, 1.0]),
1088 corner("TopLeft", [0.1, 0.9]),
1089 ];
1090 let src_uv = |u: [f64; 2], o: [f64; 2]| [u[0] - o[0], u[1] - o[1]];
1091 let (s_bl, s_br, s_tr, s_tl) = (src_uv([0.0, 0.0], bl_o), src_uv([1.0, 0.0], br_o), src_uv([1.0, 1.0], tr_o), src_uv([0.0, 1.0], tl_o));
1092 let h_inv = match (Self::quad_homography(bl, br, tr, tl), Self::quad_homography(s_bl, s_br, s_tr, s_tl)) {
1093 (Some(out), Some(src)) => Self::mat_mul(&src, &Self::adjugate(&out)).map(|v| v as f32),
1094 _ => [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
1095 };
1096 let (mask, mask_inv) = self.eval_mask_levels(comp, node, time, src.size, enc, visiting, stats);
1034 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1097 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
1035 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;1098 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;
10361099
...@@ -1039,7 +1102,15 @@ impl Engine {...@@ -1039,7 +1102,15 @@ impl Engine {
1039 u.p[1] = [h_inv[4], h_inv[5], h_inv[6], h_inv[7]];1102 u.p[1] = [h_inv[4], h_inv[5], h_inv[6], h_inv[7]];
1040 u.p[2] = [h_inv[8], blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];1103 u.p[2] = [h_inv[8], blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
1041 u.p[3][1] = if mbm { 1.0 } else { 0.0 };1104 u.p[3][1] = if mbm { 1.0 } else { 0.0 };
1042 let key = self.make_key(node, "CornerPos", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..4]));1105 if comp.eval_number(node, "MappingType", time, 1.0, &self.expr) < 0.5 {
1106 u.p[3][3] = 1.0;
1107 let quads = [[bl, br], [tr, tl], [s_bl, s_br], [s_tr, s_tl]];
1108 for (row, pts) in quads.iter().enumerate() {
1109 u.p[6 + row] = [pts[0][0] as f32, pts[0][1] as f32, pts[1][0] as f32, pts[1][1] as f32];
1110 }
1111 }
1112 u.p[5] = self.process_skip(comp, node, time);
1113 let key = self.make_key(node, "CornerPos", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..10]));
1043 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1114 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
1044 stats.cache_hits += 1;1115 stats.cache_hits += 1;
1045 return hit;1116 return hit;
...@@ -1213,47 +1284,34 @@ impl Engine {...@@ -1213,47 +1284,34 @@ impl Engine {
1213 if blend <= 1e-6 || strength.abs() <= 1e-6 {1284 if blend <= 1e-6 || strength.abs() <= 1e-6 {
1214 return src;1285 return src;
1215 }1286 }
1216 let x_dev = comp.eval_number(node, "XDeviation", time, 0.05, &self.expr);1287 let x_dev = comp.eval_number(node, "XDeviation", time, 0.2, &self.expr);
1217 let y_dev = comp.eval_number(node, "YDeviation", time, 0.05, &self.expr);1288 let y_dev = comp.eval_number(node, "YDeviation", time, 0.2, &self.expr);
1218 let r_dev = comp.eval_number(node, "RotationDeviation", time, 5.0, &self.expr);1289 let r_dev = comp.eval_number(node, "RotationDeviation", time, 0.2, &self.expr);
1219 let speed = comp.eval_number(node, "Speed", time, 1.0, &self.expr);1290 let speed = comp.eval_number(node, "Speed", time, 0.8, &self.expr);
1220 let randomness = comp.eval_number(node, "Randomness", time, 0.5, &self.expr).clamp(0.0, 1.0);1291 let randomness = comp.eval_number(node, "Randomness", time, 0.5, &self.expr).clamp(0.0, 1.0);
1221 let seed = comp.eval_number(node, "RandomSeed", time, 0.0, &self.expr).round() as i64;1292 let method = comp.eval_number(node, "FrequencyMethod", time, 0.0, &self.expr).round() as i32;
1222 let edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32;1293 let edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32;
12231294
1224 // Smooth seeded wave blending sinusoidal motion (Randomness=0) and cubic value noise (Randomness=1)1295 // Probed Fusion 21.1: each axis is one sine of angular rate `rate(Speed) * k` radians per frame.
1225 let wave = |ch: i64, freq: f64| -> f64 {1296 const SPEED_RATE: [(f64, f64); 12] = [
1226 let phase = time * speed * 0.25 * freq;1297 (0.0, 0.2), (0.1, 0.262), (0.2, 0.338), (0.3, 0.37), (0.4, 0.425), (0.5, 0.502),
1227 let periodic = (phase * std::f64::consts::TAU + ch as f64 * 1.7).sin();1298 (0.6, 0.598), (0.7, 0.75), (0.8, 1.0), (0.9, 1.506), (0.95, 1.998), (1.0, 3.005),
1228 let u = time * speed * 0.35 + ch as f64 * 13.7;1299 ];
1229 let k = u.floor() as i64;1300 let rate = SPEED_RATE
1230 let f = u - k as f64;1301 .windows(2)
1231 let h11 = |i: i64| -> f64 {1302 .find(|w| speed <= w[1].0)
1232 let mut x = (i.wrapping_mul(0x9E37_79B9_7F4A_7C15u64 as i64)1303 .map_or(SPEED_RATE[11].1, |w| w[0].1 + (w[1].1 - w[0].1) * ((speed - w[0].0) / (w[1].0 - w[0].0)).clamp(0.0, 1.0));
1233 ^ seed.wrapping_mul(0xBF58_476D_1CE4_E5B9u64 as i64)1304 let wave = |k: f64, phase: f64| -> f64 {
1234 ^ ch.wrapping_mul(0x94D0_49BB_1331_11EBu64 as i64)) as u64;1305 let s = (rate * k * time + phase).sin();
1235 x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);1306 match method {
1236 x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);1307 2 => s.signum(),
1237 x ^= x >> 31;1308 _ => s,
1238 ((x >> 11) as f64 / (1u64 << 53) as f64) * 2.0 - 1.01309 }
1239 };
1240 let p0 = h11(k - 1);
1241 let p1 = h11(k);
1242 let p2 = h11(k + 1);
1243 let p3 = h11(k + 2);
1244 let f2 = f * f;
1245 let f3 = f2 * f;
1246 let noisy = (0.5 * ((2.0 * p1)
1247 + (-p0 + p2) * f
1248 + (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * f2
1249 + (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * f3)).clamp(-1.0, 1.0);
1250 periodic * (1.0 - randomness) + noisy * randomness
1251 };1310 };
12521311 let dx = x_dev * strength * 0.2 * (wave(1.0, 0.0) + 0.33 * randomness);
1253 let dx = x_dev * strength * wave(1, 1.0);1312 let dy = -y_dev * strength * 0.2 * wave(0.75_f64.sqrt(), -2.14);
1254 let dy = y_dev * strength * wave(2, 1.27);1313 let angle = (r_dev * strength * 5.1 * wave(1.2_f64.sqrt(), 0.18)).to_radians() as f32;
1255 let angle = (r_dev * strength * wave(3, 0.83)).to_radians() as f32;1314 let (mask, mask_inv) = self.eval_mask_levels(comp, node, time, src.canvas_size, enc, visiting, stats);
1256 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.canvas_size), enc, visiting, stats);
1257 let mask = mask.map(|m| self.crop_to_canvas(enc, m));1315 let mask = mask.map(|m| self.crop_to_canvas(enc, m));
1258 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1316 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
12591317
...@@ -1261,7 +1319,9 @@ impl Engine {...@@ -1261,7 +1319,9 @@ impl Engine {
1261 u.p[0] = [0.5 + dx as f32, 0.5 + dy as f32, 0.5, 0.5];1319 u.p[0] = [0.5 + dx as f32, 0.5 + dy as f32, 0.5, 0.5];
1262 u.p[1] = [1.0, 1.0, angle, edges];1320 u.p[1] = [1.0, 1.0, angle, edges];
1263 u.p[2] = [0.0, 0.0, blend, if mask.is_some() { 1.0 } else { 0.0 }];1321 u.p[2] = [0.0, 0.0, blend, if mask.is_some() { 1.0 } else { 0.0 }];
1264 u.p[3] = [if mask_inv { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0];1322 let invert = comp.eval_number(node, "InvertTransform", time, 0.0, &self.expr) > 0.5;
1323 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;
1324 u.p[3] = [if mask_inv { 1.0 } else { 0.0 }, 0.0, if mbm { 1.0 } else { 0.0 }, if invert { 1.0 } else { 0.0 }];
1265 if src.has_dod() {1325 if src.has_dod() {
1266 u.p[4] = [1326 u.p[4] = [
1267 src.origin[0] as f32,1327 src.origin[0] as f32,
...@@ -1270,8 +1330,9 @@ impl Engine {...@@ -1270,8 +1330,9 @@ impl Engine {
1270 src.canvas_size[1] as f32,1330 src.canvas_size[1] as f32,
1271 ];1331 ];
1272 }1332 }
1333 u.p[6] = self.process_skip(comp, node, time);
1273 let out_size = src.canvas_size;1334 let out_size = src.canvas_size;
1274 let key = self.make_key(node, "CamShake", out_size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5]));1335 let key = self.make_key(node, "CamShake", out_size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..7]));
1275 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1336 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
1276 stats.cache_hits += 1;1337 stats.cache_hits += 1;
1277 return hit;1338 return hit;
...@@ -2094,4 +2155,24 @@ mod tests {...@@ -2094,4 +2155,24 @@ mod tests {
2094 assert_eq!(&cs_px[i..i + 4], &xf_px[i..i + 4], "CameraShake mask misaligned at x={x}");2155 assert_eq!(&cs_px[i..i + 4], &xf_px[i..i + 4], "CameraShake mask misaligned at x={x}");
2095 }2156 }
2096 }2157 }
2158 #[test]
2159 fn corner_positioner_defaults_to_fusion_quad_and_offsets_extend_the_sampled_quad() {
2160 let src = r#"Composition { Tools = ordered() {
2161 BG = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, TopLeftAlpha = Input { Value = 1, }, }, },
2162 CP = CornerPositioner { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", }, }, },
2163 CPO = CornerPositioner { Inputs = { Input = Input { SourceOp = "BG", Source = "Output", }, TopLeftOffsetX = Input { Value = 1, }, }, },
2164 }, }"#;
2165 let comp = Comp::from_fusion_str(src).expect("parse corner positioner comp");
2166 let Ok(mut eng) = Engine::new_headless() else { return };
2167 let alpha = |eng: &mut Engine, name: &str, x: usize, y: usize| {
2168 let (tex, _) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0);
2169 eng.readback_rgba(&tex).unwrap()[(y * 64 + x) * 4 + 3]
2170 };
2171 // Default quad TL(0.1, 0.9) .. BR(1, 0.1): the bottom-left canvas corner lies outside, the center inside.
2172 assert_eq!(alpha(&mut eng, "CP", 1, 62), 0);
2173 assert_eq!(alpha(&mut eng, "CP", 32, 32), 255);
2174 // An X offset of 1 samples the source from u = -1, so the top-left of the quad turns transparent.
2175 assert_eq!(alpha(&mut eng, "CPO", 8, 8), 0);
2176 assert_eq!(alpha(&mut eng, "CPO", 56, 40), 255);
2177 }
2097}2178}
crates/engine/src/eval/warps.rs+45-9
...@@ -31,7 +31,7 @@ impl Engine {...@@ -31,7 +31,7 @@ impl Engine {
31 let phase = comp.eval_number(node, "Phase", time, 0.0, &self.expr) as f32;31 let phase = comp.eval_number(node, "Phase", time, 0.0, &self.expr) as f32;
32 let dampening = comp.eval_number(node, "Dampening", time, 0.0, &self.expr).max(0.0) as f32;32 let dampening = comp.eval_number(node, "Dampening", time, 0.0, &self.expr).max(0.0) as f32;
33 let shape = comp.eval_number(node, "Shape", time, 0.0, &self.expr).round() as f32;33 let shape = comp.eval_number(node, "Shape", time, 0.0, &self.expr).round() as f32;
34 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);34 let (mask, mask_inv) = self.eval_mask_levels(comp, node, time, src.size, enc, visiting, stats);
35 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);35 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
36 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;36 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;
3737
...@@ -40,13 +40,14 @@ impl Engine {...@@ -40,13 +40,14 @@ impl Engine {
40 u.p[1] = [freq, aspect, phase, dampening];40 u.p[1] = [freq, aspect, phase, dampening];
41 u.p[2] = [shape, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];41 u.p[2] = [shape, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
42 u.p[3][0] = if mbm { 1.0 } else { 0.0 };42 u.p[3][0] = if mbm { 1.0 } else { 0.0 };
43 u.p[4] = self.process_skip(comp, node, time);
4344
44 let key = self.make_key(45 let key = self.make_key(
45 node,46 node,
46 "Drip",47 "Drip",
47 src.size,48 src.size,
48 &[src.content_hash, mask_hash],49 &[src.content_hash, mask_hash],
49 bytemuck::cast_slice(&u.p[..4]),50 bytemuck::cast_slice(&u.p[..5]),
50 );51 );
51 if let Some(hit) = self.cache.get(key, node.id, frame_i) {52 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
52 stats.cache_hits += 1;53 stats.cache_hits += 1;
...@@ -54,13 +55,47 @@ impl Engine {...@@ -54,13 +55,47 @@ impl Engine {
54 }55 }
55 stats.cache_misses += 1;56 stats.cache_misses += 1;
56 stats.nodes_evaluated += 1;57 stats.nodes_evaluated += 1;
57 let dst = self.alloc_tex_like(&src, src.depth, key);58 let mask_dod = if mbm { Self::polyline_mask_dod(comp, node, time, src.canvas_size, &self.expr) } else { None };
59 let dst = self.alloc_tex_like(&src, src.depth, if mask_dod.is_some() { 0 } else { key });
58 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);60 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
59 self.dispatch_op(enc, ShaderOp::Drip, &src.view, &self.dummy.view, mv, &dst, &u);61 self.dispatch_op(enc, ShaderOp::Drip, &src.view, &self.dummy.view, mv, &dst, &u);
62 let dst = match mask_dod {
63 Some((origin, size)) => {
64 let cropped = self.alloc_tex_dod(size, origin, src.canvas_size, src.depth, key);
65 let mut crop = UniformParams::default();
66 crop.p[0] = [origin[0] as f32, (src.canvas_size[1] as i32 - size[1] as i32 - origin[1]) as f32, 1.0, 1.0];
67 self.dispatch_op(enc, ShaderOp::Crop, &dst.view, &self.dummy.view, &self.dummy.view, &cropped, &crop);
68 cropped
69 }
70 None => dst,
71 };
60 self.cache.insert(key, dst.clone(), node.id, frame_i);72 self.cache.insert(key, dst.clone(), node.id, frame_i);
61 dst73 dst
62 }74 }
6375
76 /// Fusion crops a `MultiplyByMask` result to the mask's own domain: its covered bounds grown by three `SoftEdge` sigmas.
77 fn polyline_mask_dod(comp: &Comp, node: &Node, time: f64, canvas: [u32; 2], expr: &ExprEngine) -> Option<([i32; 2], [u32; 2])> {
78 let mask = comp.node(comp.find_node(node.connected_node("EffectMask")?)?);
79 if mask.kind != "PolylineMask" || comp.eval_number(mask, "Invert", time, 0.0, expr) > 0.5 {
80 return None;
81 }
82 let center = comp.eval_point(mask, "Center", time, [0.5, 0.5], expr);
83 let angle = comp.eval_number(mask, "Angle", time, 0.0, expr) as f32;
84 let points = Self::polyline_points(comp, mask, time);
85 let px = rasterize_polyline_mask(canvas, &points, [center[0] as f32, center[1] as f32], angle, false, 1.0);
86 let [w, h] = canvas.map(|v| v as usize);
87 let covered = |i: usize| px[i * 4 + 3] > 0;
88 let xs = (0..w).filter(|&x| (0..h).any(|y| covered(y * w + x)));
89 let ys = (0..h).filter(|&y| (0..w).any(|x| covered(y * w + x)));
90 let (x0, x1) = (xs.clone().next()?, xs.last()? + 1);
91 let (y0, y1) = (ys.clone().next()?, ys.last()? + 1);
92 let margin = 3.0 * comp.eval_number(mask, "SoftEdge", time, 0.0, expr).max(0.0) * canvas[0] as f64;
93 let lo = |v: usize, max: usize| ((v as f64 - margin).floor().max(0.0) as usize).min(max);
94 let hi = |v: usize, max: usize| (((v as f64 + margin).floor() + 1.0).min(max as f64)) as usize;
95 let (lx, ly, hx, hy) = (lo(x0, w), lo(y0, h), hi(x1, w), hi(y1, h));
96 Some(([lx as i32, ly as i32], [(hx - lx).max(1) as u32, (hy - ly).max(1) as u32]))
97 }
98
64 #[allow(clippy::too_many_arguments)]99 #[allow(clippy::too_many_arguments)]
65 pub(super) fn eval_dent(100 pub(super) fn eval_dent(
66 &mut self,101 &mut self,
...@@ -133,13 +168,13 @@ impl Engine {...@@ -133,13 +168,13 @@ impl Engine {
133 if blend <= 1e-6 {168 if blend <= 1e-6 {
134 return src;169 return src;
135 }170 }
136 let bl = comp.eval_point(node, "BottomLeft", time, [0.0, 0.0], &self.expr);171 let bl = comp.eval_point(node, "BottomLeft", time, [0.0, 0.3], &self.expr);
137 let br = comp.eval_point(node, "BottomRight", time, [1.0, 0.0], &self.expr);172 let br = comp.eval_point(node, "BottomRight", time, [1.0, 0.1], &self.expr);
138 let tr = comp.eval_point(node, "TopRight", time, [1.0, 1.0], &self.expr);173 let tr = comp.eval_point(node, "TopRight", time, [0.9, 1.0], &self.expr);
139 let tl = comp.eval_point(node, "TopLeft", time, [0.0, 1.0], &self.expr);174 let tl = comp.eval_point(node, "TopLeft", time, [0.1, 0.9], &self.expr);
140 let mapping_type = comp.eval_number(node, "MappingType", time, 0.0, &self.expr).round() as i32;175 let mapping_type = comp.eval_number(node, "MappingType", time, 0.0, &self.expr).round() as i32;
141 let h_fwd = quad_forward_homography(bl, br, tr, tl);176 let h_fwd = quad_forward_homography(bl, br, tr, tl);
142 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.canvas_size), enc, visiting, stats);177 let (mask, mask_inv) = self.eval_mask_levels(comp, node, time, src.canvas_size, enc, visiting, stats);
143 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);178 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
144 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;179 let mbm = mask.is_some() && comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5;
145180
...@@ -192,13 +227,14 @@ impl Engine {...@@ -192,13 +227,14 @@ impl Engine {
192 if src.has_dod() {227 if src.has_dod() {
193 u.p[8] = [src.origin[0] as f32, src.origin[1] as f32, src.canvas_size[0] as f32, src.canvas_size[1] as f32];228 u.p[8] = [src.origin[0] as f32, src.origin[1] as f32, src.canvas_size[0] as f32, src.canvas_size[1] as f32];
194 }229 }
230 u.p[9] = self.process_skip(comp, node, time);
195231
196 let key = self.make_key(232 let key = self.make_key(
197 node,233 node,
198 "PerspectivePositioner",234 "PerspectivePositioner",
199 dst_size,235 dst_size,
200 &[src.content_hash, mask_hash],236 &[src.content_hash, mask_hash],
201 bytemuck::cast_slice(&u.p[..9]),237 bytemuck::cast_slice(&u.p[..10]),
202 );238 );
203 if let Some(hit) = self.cache.get(key, node.id, frame_i) {239 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
204 stats.cache_hits += 1;240 stats.cache_hits += 1;
crates/engine/src/shaders/archive_fx.wgsl+6-2
...@@ -13,6 +13,7 @@ fn mask_mix(orig: vec4<f32>, warped: vec4<f32>, raw_mask: f32, blend: f32, mbm:...@@ -13,6 +13,7 @@ fn mask_mix(orig: vec4<f32>, warped: vec4<f32>, raw_mask: f32, blend: f32, mbm:
13// p[1] = (frequency, aspect, phase, dampening)13// p[1] = (frequency, aspect, phase, dampening)
14// p[2] = (shape: 0=Circle, 1=Square, 2=Random, 3=Horizontal, 4=Vertical, blend, has_mask, mask_invert)14// p[2] = (shape: 0=Circle, 1=Square, 2=Random, 3=Horizontal, 4=Vertical, blend, has_mask, mask_invert)
15// p[3].x = multiply_by_mask15// p[3].x = multiply_by_mask
16// p[4] = (skip_r, skip_g, skip_b, skip_a): 1 keeps that source channel untouched
16@compute @workgroup_size(8, 8)17@compute @workgroup_size(8, 8)
17fn drip_op(@builtin(global_invocation_id) gid: vec3<u32>) {18fn drip_op(@builtin(global_invocation_id) gid: vec3<u32>) {
18 let dims = textureDimensions(dst_tex);19 let dims = textureDimensions(dst_tex);
...@@ -61,7 +62,8 @@ fn drip_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -61,7 +62,8 @@ fn drip_op(@builtin(global_invocation_id) gid: vec3<u32>) {
61 }62 }
62 let warped = sample_edges(src_a, src_fu, 2);63 let warped = sample_edges(src_a, src_fu, 2);
6364
64 store_dst(vec2<i32>(gid.xy), mask_mix(orig, warped, read_mask(gid.xy, dims, u.p[2].z, u.p[2].w), blend, u.p[3].x > 0.5));65 let res = mask_mix(orig, warped, read_mask(gid.xy, dims, u.p[2].z, u.p[2].w), blend, u.p[3].x > 0.5);
66 store_dst(vec2<i32>(gid.xy), select(res, orig, u.p[4] > vec4<f32>(0.5)));
65}67}
6668
67// 2. Dent (radial dent / sine / pinch / punch warp)69// 2. Dent (radial dent / sine / pinch / punch warp)
...@@ -125,6 +127,7 @@ fn dent_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -125,6 +127,7 @@ fn dent_op(@builtin(global_invocation_id) gid: vec3<u32>) {
125// p[6] = (roi_x0, roi_y0, roi_x1, roi_y1) corner bounding box in Fusion UV127// p[6] = (roi_x0, roi_y0, roi_x1, roi_y1) corner bounding box in Fusion UV
126// p[7] = (dod_origin_x, dod_origin_y, canvas_w, canvas_h) when the output extends past the canvas128// p[7] = (dod_origin_x, dod_origin_y, canvas_w, canvas_h) when the output extends past the canvas
127// p[8] = same for src_a when it carries a DoD129// p[8] = same for src_a when it carries a DoD
130// p[9] = (skip_r, skip_g, skip_b, skip_a): 1 keeps that source channel untouched
128@compute @workgroup_size(8, 8)131@compute @workgroup_size(8, 8)
129fn perspective_pos_op(@builtin(global_invocation_id) gid: vec3<u32>) {132fn perspective_pos_op(@builtin(global_invocation_id) gid: vec3<u32>) {
130 let dims = textureDimensions(dst_tex);133 let dims = textureDimensions(dst_tex);
...@@ -165,7 +168,8 @@ fn perspective_pos_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -165,7 +168,8 @@ fn perspective_pos_op(@builtin(global_invocation_id) gid: vec3<u32>) {
165 warped = sample_dod_edges(src_a, s_fu, 0, u.p[8]);168 warped = sample_dod_edges(src_a, s_fu, 0, u.p[8]);
166 }169 }
167170
168 store_dst(vec2<i32>(gid.xy), mask_mix(orig, warped, read_mask(gid.xy, dims, u.p[4].w, u.p[5].x), u.p[4].z, u.p[5].y > 0.5));171 let res = mask_mix(orig, warped, read_mask(gid.xy, dims, u.p[4].w, u.p[5].x), u.p[4].z, u.p[5].y > 0.5);
172 store_dst(vec2<i32>(gid.xy), select(res, orig, u.p[9] > vec4<f32>(0.5)));
169}173}
170174
171// 4. DaySky (Preetham Perez equirectangular physical sky dome)175// 4. DaySky (Preetham Perez equirectangular physical sky dome)
crates/engine/src/shaders/keyers_gen.wgsl+21-7
...@@ -297,16 +297,17 @@ fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -297,16 +297,17 @@ fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) {
297// p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine)297// p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine)
298// p[4] = (scale, phase, red_phase, green_phase)298// p[4] = (scale, phase, red_phase, green_phase)
299// p[5] = (blue_phase, blend, has_mask, mask_invert)299// p[5] = (blue_phase, blend, has_mask, mask_invert)
300// p[6] = (alpha_phase, 0, 0, 0)300// p[6] = (alpha_phase, operation: 0=Add 1=Subtract 2=Multiply 3=Divide 4=Maximum 5=Minimum 6=Difference, 0, 0)
301// ============================================================================301// ============================================================================
302fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32) -> f32 {302fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32, wrap: bool) -> f32 {
303 let ctype = i32(c.w + 0.5);303 let ctype = i32(c.w + 0.5);
304 if (ctype <= 0) { return 0.0; }304 if (ctype <= 0) { return 0.0; }
305 let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv);305 let delta = vec2<f32>(u_pos.x - c.x, (u_pos.y - c.y) * aspect_inv);
306 let d = length(delta);306 let d = length(delta);
307 let s = max(master_scale * c.z, 1e-5);307 let s = max(master_scale * c.z, 1e-5);
308 if (ctype == 1) {308 if (ctype == 1) {
309 return 39.269908 * d / s;309 let lin = 39.269908 * d / s;
310 return select(lin, lin - 6.2831853 * floor(lin / 6.2831853), wrap);
310 }311 }
311 let r0 = 0.01246 * s;312 let r0 = 0.01246 * s;
312 let dr = sqrt(d * d + r0 * r0) - r0;313 let dr = sqrt(d * d + r0 * r0) - r0;
...@@ -323,11 +324,24 @@ fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -323,11 +324,24 @@ fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) {
323 let aspect_inv = fdims.y / max(fdims.x, 1.0);324 let aspect_inv = fdims.y / max(fdims.x, 1.0);
324 let master_scale = max(u.p[4].x, 1e-5);325 let master_scale = max(u.p[4].x, 1e-5);
325326
327 // Fusion folds the four circle terms with `Operation`, a `None` circle counting as 0. Maximum, Minimum and
328 // Divide see Type 1 as a sawtooth in [0, 2pi); the other operations see the raw linear ramp.
329 let op = i32(u.p[6].y + 0.5);
330 let wrap = op >= 3 && op <= 5;
331 var t = array<f32, 4>(0.0, 0.0, 0.0, 0.0);
332 for (var i = 0u; i < 4u; i = i + 1u) {
333 t[i] = plasma_circle_term(u_pos, aspect_inv, u.p[i], master_scale, wrap);
334 }
326 var th = 0.0;335 var th = 0.0;
327 th += plasma_circle_term(u_pos, aspect_inv, u.p[0], master_scale);336 switch op {
328 th += plasma_circle_term(u_pos, aspect_inv, u.p[1], master_scale);337 case 1: { th = -(t[0] + t[1] + t[2] + t[3]); }
329 th += plasma_circle_term(u_pos, aspect_inv, u.p[2], master_scale);338 case 2: { th = t[0] * t[1] * t[2] * t[3]; }
330 th += plasma_circle_term(u_pos, aspect_inv, u.p[3], master_scale);339 case 3: { th = t[0] / t[1] / t[2] / t[3]; }
340 case 4: { th = max(max(t[0], t[1]), max(t[2], t[3])); }
341 case 5: { th = min(min(t[0], t[1]), min(t[2], t[3])); }
342 case 6: { th = abs(abs(abs(t[0] - t[1]) - t[2]) - t[3]); }
343 default: { th = t[0] + t[1] + t[2] + t[3]; }
344 }
331345
332 let tau = 6.2831853;346 let tau = 6.2831853;
333 let ph = u.p[4].y;347 let ph = u.p[4].y;
crates/engine/src/shaders/transform.wgsl+50-12
...@@ -4,6 +4,7 @@...@@ -4,6 +4,7 @@
4// p[1] = (size_x, size_y, angle_rad, edges)4// p[1] = (size_x, size_y, angle_rad, edges)
5// p[2] = (flip_h, flip_v, blend, has_mask)5// p[2] = (flip_h, flip_v, blend, has_mask)
6// p[3] = (mask_invert, nearest, multiply_by_mask, invert_transform)6// p[3] = (mask_invert, nearest, multiply_by_mask, invert_transform)
7// p[6] = (skip_r, skip_g, skip_b, skip_a): 1 keeps that source channel untouched
7// ============================================================================8// ============================================================================
8@compute @workgroup_size(8, 8)9@compute @workgroup_size(8, 8)
9fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) {10fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) {
...@@ -62,7 +63,7 @@ fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -62,7 +63,7 @@ fn transform_op(@builtin(global_invocation_id) gid: vec3<u32>) {
62 if (mbm) {63 if (mbm) {
63 out_col = out_col * raw_mask;64 out_col = out_col * raw_mask;
64 }65 }
65 store_dst(vec2<i32>(gid.xy), out_col);66 store_dst(vec2<i32>(gid.xy), select(out_col, orig, u.p[6] > vec4<f32>(0.5)));
66}67}
67// MultiplyByMask scales the warped result by mask * blend instead of mixing against the source.68// MultiplyByMask scales the warped result by mask * blend instead of mixing against the source.
68fn mask_mix(orig: vec4<f32>, warped: vec4<f32>, raw_mask: f32, blend: f32, mbm: bool) -> vec4<f32> {69fn mask_mix(orig: vec4<f32>, warped: vec4<f32>, raw_mask: f32, blend: f32, mbm: bool) -> vec4<f32> {
...@@ -287,7 +288,36 @@ fn xf_chroma_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -287,7 +288,36 @@ fn xf_chroma_op(@builtin(global_invocation_id) gid: vec3<u32>) {
287// p[2] = (h22, blend, has_mask, mask_invert)288// p[2] = (h22, blend, has_mask, mask_invert)
288// p[3] = (ignore_alpha, multiply_by_mask, 0, 0)289// p[3] = (ignore_alpha, multiply_by_mask, 0, 0)
289// p[4] = (dst_dod_ox, dst_dod_oy, canvas_w, canvas_h)290// p[4] = (dst_dod_ox, dst_dod_oy, canvas_w, canvas_h)
291// p[3].w = 1 selects bilinear mapping: p[6], p[7] = output quad (bl br / tr tl), p[8], p[9] = source-UV quad in the same order
292// p[5] = (skip_r, skip_g, skip_b, skip_a): 1 keeps that source channel untouched
290// ============================================================================293// ============================================================================
294// Maps `p` inside the quad (a = bl, b = br, c = tr, d = tl) to bilinear (u, v) in [0, 1]; NaN when unsolvable.
295fn inv_bilinear(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>, c: vec2<f32>, d: vec2<f32>) -> vec2<f32> {
296 let e = b - a;
297 let f = d - a;
298 let g = a - b + c - d;
299 let h = p - a;
300 let k2 = g.x * f.y - g.y * f.x;
301 let k1 = (e.x * f.y - e.y * f.x) + (h.x * g.y - h.y * g.x);
302 let k0 = h.x * e.y - h.y * e.x;
303 if (abs(k2) < 1e-5) {
304 let v = -k0 / k1;
305 return vec2<f32>((h.x - f.x * v) / (e.x + g.x * v), v);
306 }
307 let w = k1 * k1 - 4.0 * k0 * k2;
308 if (w < 0.0) {
309 return vec2<f32>(bitcast<f32>(0x7fc00000u));
310 }
311 let r = sqrt(w);
312 var v = (-k1 - r) * 0.5 / k2;
313 var uu = (h.x - f.x * v) / (e.x + g.x * v);
314 if (uu < 0.0 || uu > 1.0 || v < 0.0 || v > 1.0) {
315 v = (-k1 + r) * 0.5 / k2;
316 uu = (h.x - f.x * v) / (e.x + g.x * v);
317 }
318 return vec2<f32>(uu, v);
319}
320
291@compute @workgroup_size(8, 8)321@compute @workgroup_size(8, 8)
292fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {322fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {
293 let dims = textureDimensions(dst_tex);323 let dims = textureDimensions(dst_tex);
...@@ -307,18 +337,25 @@ fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -307,18 +337,25 @@ fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {
307 has_dod,337 has_dod,
308 );338 );
309339
310 let sx = u.p[0].x * fu.x + u.p[0].y * fu.y + u.p[0].z;340 var s_fu = vec2<f32>(0.0);
311 let sy = u.p[0].w * fu.x + u.p[1].x * fu.y + u.p[1].y;341 var valid = false;
312 let sw = u.p[1].z * fu.x + u.p[1].w * fu.y + u.p[2].x;342 if (u.p[3].w > 0.5) {
343 let uv = inv_bilinear(fu, u.p[6].xy, u.p[6].zw, u.p[7].xy, u.p[7].zw);
344 s_fu = mix(mix(u.p[8].xy, u.p[8].zw, uv.x), mix(u.p[9].zw, u.p[9].xy, uv.x), uv.y);
345 valid = all(uv == uv);
346 } else {
347 let sx = u.p[0].x * fu.x + u.p[0].y * fu.y + u.p[0].z;
348 let sy = u.p[0].w * fu.x + u.p[1].x * fu.y + u.p[1].y;
349 let sw = u.p[1].z * fu.x + u.p[1].w * fu.y + u.p[2].x;
350 s_fu = vec2<f32>(sx / sw, sy / sw);
351 valid = abs(sw) > 1e-6;
352 }
313353
314 var warped = vec4<f32>(0.0);354 var warped = vec4<f32>(0.0);
315 if (abs(sw) > 1e-6) {355 if (valid && s_fu.x >= 0.0 && s_fu.x <= 1.0 && s_fu.y >= 0.0 && s_fu.y <= 1.0) {
316 let s_fu = vec2<f32>(sx / sw, sy / sw);356 warped = sample_edges(src_a, s_fu, 0);
317 if (s_fu.x >= 0.0 && s_fu.x <= 1.0 && s_fu.y >= 0.0 && s_fu.y <= 1.0) {357 if (u.p[3].x > 0.5) {
318 warped = sample_edges(src_a, s_fu, 0);358 warped.a = 1.0;
319 if (u.p[3].x > 0.5) {
320 warped.a = 1.0;
321 }
322 }359 }
323 }360 }
324 let raw_mask = select(361 let raw_mask = select(
...@@ -326,7 +363,8 @@ fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -326,7 +363,8 @@ fn homography_op(@builtin(global_invocation_id) gid: vec3<u32>) {
326 read_mask_uv(fu_to_uv(fu), u.p[2].z, u.p[2].w),363 read_mask_uv(fu_to_uv(fu), u.p[2].z, u.p[2].w),
327 has_dod,364 has_dod,
328 );365 );
329 store_dst(vec2<i32>(gid.xy), mask_mix(orig, warped, raw_mask, u.p[2].y, u.p[3].y > 0.5));366 let res = mask_mix(orig, warped, raw_mask, u.p[2].y, u.p[3].y > 0.5);
367 store_dst(vec2<i32>(gid.xy), select(res, orig, u.p[5] > vec4<f32>(0.5)));
330}368}
331// ============================================================================369// ============================================================================
332// 34. Vortex (radial twist warp: th = -angle * (1 - r / (0.5*Size))^Power)370// 34. Vortex (radial twist warp: th = -angle * (1 - r / (0.5*Size))^Power)