From db8e9f2e18b100438979c989ef5c3d034553b645 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 30 Nov 2026 19:28:42 -0800 Subject: [PATCH] Fix Vortex, Dent Sine, and 11_everyone_knows Transform parity (#44) - sBoolean: Fusion only accepts Union/Intersection/Subtract/Xor (default Intersection); any other Operation renders empty (repro_seed_125/122 were "Intersect" -> black in Fusion, not a Vortex bug). A missing Input2 passes Input1 through in the boolean's own color; a missing Input1 renders empty. - Dent Type=1 (Sine): new_r = r - Strength * (1 - r / R), measured against Fusion on a gradient plate (repro_seed_120 1.6% -> 0.2%). - Transform: implement InvertTransform (samples through the forward map). 11_everyone_knows Transform3/Transform1 residual traces to WandMask RangeSoftEdge (masks.rs), not Transform: Transform with a plain mask matches Fusion. Assisted-by: claude-sonnet-5.5 --- crates/engine/src/eval/shapes_warp.rs | 154 +++++++++++++++------- crates/engine/src/eval/transform.rs | 36 ++++- crates/engine/src/shaders/archive_fx.wgsl | 4 +- crates/engine/src/shaders/transform.wgsl | 23 +++- 4 files changed, 158 insertions(+), 59 deletions(-) diff --git a/crates/engine/src/eval/shapes_warp.rs b/crates/engine/src/eval/shapes_warp.rs index c30bd4ce6cddacbfbed32db7d6cd186e8acb5980..1d049ef8a2ac0e46a64289590af1e62d859e8c01 100644 --- a/crates/engine/src/eval/shapes_warp.rs +++ b/crates/engine/src/eval/shapes_warp.rs @@ -175,25 +175,17 @@ fn eval_first_num( default } -fn parse_bool_op(comp: &Comp, node: &Node, time: f64, expr: &ExprEngine) -> ShapeBoolOp { - if let Some(s) = node.static_str("Operation") { - let low = s.to_ascii_lowercase(); - if low.contains("inter") { - return ShapeBoolOp::Intersect; - } - if low.contains("sub") || low.contains("diff") { - return ShapeBoolOp::Subtract; - } - if low.contains("xor") { - return ShapeBoolOp::Xor; - } - return ShapeBoolOp::Union; +/// Fusion accepts exactly `Union | Intersection | Subtract | Xor` (default `Intersection`); any other value renders empty. +fn parse_bool_op(node: &Node) -> Option { + if !node.inputs.contains_key("Operation") { + return Some(ShapeBoolOp::Intersect); } - match comp.eval_number(node, "Operation", time, 0.0, expr).round() as i32 { - 1 => ShapeBoolOp::Intersect, - 2 => ShapeBoolOp::Subtract, - 3 => ShapeBoolOp::Xor, - _ => ShapeBoolOp::Union, + match node.static_str("Operation")? { + "Union" => Some(ShapeBoolOp::Union), + "Intersection" => Some(ShapeBoolOp::Intersect), + "Subtract" => Some(ShapeBoolOp::Subtract), + "Xor" => Some(ShapeBoolOp::Xor), + _ => None, } } @@ -375,42 +367,31 @@ fn collect_shape_tree( collect_shape_tree(comp, src_id, time, expr, parent_xform, Some((thickness, join, cap)), depth + 1) } "sBoolean" => { - let op = parse_bool_op(comp, node, time, expr); let r = comp.eval_number(node, "Red", time, 1.0, expr) as f32; let g = comp.eval_number(node, "Green", time, 1.0, expr) as f32; let b_col = comp.eval_number(node, "Blue", time, 1.0, expr) as f32; let a_col = comp.eval_number(node, "Alpha", time, 1.0, expr) as f32; let color = [r, g, b_col, a_col]; - let a = node - .connected_node("Input1") - .or_else(|| node.connected_node("Input")) - .and_then(|s| comp.find_node(s)) - .and_then(|id| { - collect_shape_tree(comp, id, time, expr, parent_xform, outline_override, depth + 1) - }); - let b = node - .connected_node("Input2") - .and_then(|s| comp.find_node(s)) - .and_then(|id| { - collect_shape_tree(comp, id, time, expr, parent_xform, outline_override, depth + 1) - }); - match (a, b) { - (Some(ta), Some(tb)) => Some(ShapeTree::Boolean { - op, - color, - a: Box::new(ta), - b: Box::new(tb), - }), - (Some(ta), None) => match op { - ShapeBoolOp::Intersect => None, - _ => Some(ta), - }, - (None, Some(tb)) => match op { - ShapeBoolOp::Union | ShapeBoolOp::Xor => Some(tb), - _ => None, - }, - (None, None) => None, - } + let child = |port: &str| { + node.connected_node(port) + .and_then(|s| comp.find_node(s)) + .and_then(|id| { + collect_shape_tree(comp, id, time, expr, parent_xform, outline_override, depth + 1) + }) + }; + let a = child("Input1").or_else(|| child("Input"))?; + // Input2 absent passes Input1 through (recolored) whatever the operation; Input1 absent renders empty. + let (op, b) = match (parse_bool_op(node), child("Input2")) { + (_, None) => (ShapeBoolOp::Union, ShapeTree::Group(Vec::new())), + (Some(op), Some(b)) => (op, b), + (None, Some(_)) => return None, + }; + Some(ShapeTree::Boolean { + op, + color, + a: Box::new(a), + b: Box::new(b), + }) } "PipeRouter" | "Fuse.Wireless" => { let src_id = node @@ -1756,6 +1737,81 @@ mod tests { assert_eq!(&px_xor[ngon_only..ngon_only + 4], &[255, 255, 255, 255]); } + #[test] + fn test_dent_sine_falls_off_linearly_from_strength_at_center() { + let src = r#"Composition { + CurrentTime = 0, RenderRange = { 0, 0 }, GlobalRange = { 0, 0 }, + Tools = ordered() { + BG1 = Background { Inputs = { Width = Input { Value = 128, }, Height = Input { Value = 128, }, + Type = Input { Value = FuID { "Horizontal" }, }, + TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 0, }, TopLeftBlue = Input { Value = 0, }, TopLeftAlpha = Input { Value = 1, }, + TopRightRed = Input { Value = 0, }, TopRightGreen = Input { Value = 1, }, TopRightBlue = Input { Value = 0, }, TopRightAlpha = Input { Value = 1, }, }, }, + Dent1 = Dent { Inputs = { Input = Input { SourceOp = "BG1", Source = "Output", }, + Center = Input { Value = { 0.5, 0.5 }, }, Size = Input { Value = 0.5, }, + Strength = Input { Value = 0.5, }, Type = Input { Value = 1, }, }, }, + }, + }"#; + let comp = Comp::from_fusion_str(src).unwrap(); + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let (tex, _) = eng.evaluate(&comp, comp.find_node("Dent1").unwrap(), 0.0); + let px = eng.readback_rgba(&tex).unwrap(); + // x=72 (r=0.066 of the 0.25 radius): new_r = r - 0.5 * (1 - r / R) = -0.30 -> source at x=0.20, + // so the gradient reads ~80% red / ~20% green. + let i = (64 * 128 + 72) * 4; + assert!(px[i].abs_diff(204) < 8 && px[i + 1].abs_diff(51) < 8, "got {:?}", &px[i..i + 4]); + } + + #[test] + fn test_s_boolean_operation_names_and_missing_inputs() { + let boolean = |name: &str, op: &str, inputs: &str| { + format!( + r#"{name} = sBoolean {{ Inputs = {{ {op} {inputs} }}, }}, + R{name} = sRender {{ Inputs = {{ Width = Input {{ Value = 128, }}, Height = Input {{ Value = 128, }}, + Input = Input {{ SourceOp = "{name}", Source = "Output", }}, }}, }},"# + ) + }; + let both = r#"Input1 = Input { SourceOp = "Rect", Source = "Output", }, Input2 = Input { SourceOp = "Ell", Source = "Output", },"#; + let first = r#"Input1 = Input { SourceOp = "Rect", Source = "Output", },"#; + let second = r#"Input2 = Input { SourceOp = "Ell", Source = "Output", },"#; + let fu = |v: &str| format!(r#"Operation = Input {{ Value = FuID {{ "{v}" }}, }},"#); + let src = format!( + r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ + Rect = sRectangle {{ Inputs = {{ Width = Input {{ Value = 0.5, }}, Height = Input {{ Value = 0.2, }}, + Red = Input {{ Value = 1, }}, Green = Input {{ Value = 0, }}, Blue = Input {{ Value = 0, }}, }}, }}, + Ell = sEllipse {{ Inputs = {{ Width = Input {{ Value = 0.3, }}, Height = Input {{ Value = 0.3, }}, }}, }}, + {} {} {} {} {} {} + }} }}"#, + boolean("Inter", &fu("Intersection"), both), + boolean("Default", "", both), + boolean("Unknown", &fu("Intersect"), both), + boolean("SubFirst", &fu("Subtract"), first), + boolean("SubSecond", &fu("Subtract"), second), + boolean("UnionBoth", &fu("Union"), both), + ); + let comp = Comp::from_fusion_str(&src).unwrap(); + let Ok(mut eng) = Engine::new_headless() else { + return; + }; + let mut px = |name: &str, x: usize| { + let (tex, _) = eng.evaluate(&comp, comp.find_node(&format!("R{name}")).unwrap(), 0.0); + let rgba = eng.readback_rgba(&tex).unwrap(); + let i = (64 * 128 + x) * 4; + [rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]] + }; + let white = [255; 4]; + let empty = [0; 4]; + // x=64 is inside both shapes, x=90 is inside only the rectangle. + assert_eq!(px("Inter", 64), white); + assert_eq!(px("Inter", 90), empty); + assert_eq!(px("Default", 64), white, "Operation defaults to Intersection"); + assert_eq!(px("Unknown", 64), empty, "unrecognized operation names render nothing"); + assert_eq!(px("SubFirst", 90), white, "a missing Input2 passes Input1 through in the boolean's own color"); + assert_eq!(px("SubSecond", 64), empty, "a missing Input1 renders nothing"); + assert_eq!(px("UnionBoth", 90), white); + } + #[test] fn test_perspective_positioner_and_s_grid() { let src = r#"{ diff --git a/crates/engine/src/eval/transform.rs b/crates/engine/src/eval/transform.rs index e28b9650d3450b90d63930bf089db4aad91738a5..6b18ebd0034e9c0aebe94b3e9e6b4ca8a8a8dfad 100644 --- a/crates/engine/src/eval/transform.rs +++ b/crates/engine/src/eval/transform.rs @@ -595,9 +595,10 @@ impl Engine { let angle = (comp.eval_number(node, "Angle", time, 0.0, &self.expr) as f32).to_radians(); let flip_h = comp.eval_number(node, "FlipHoriz", time, 0.0, &self.expr) as f32; let flip_v = comp.eval_number(node, "FlipVert", time, 0.0, &self.expr) as f32; + let invert = comp.eval_number(node, "InvertTransform", time, 0.0, &self.expr) > 0.5; let mut edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32; let edges_i = edges.round() as i32; - if edges_i == 0 || edges_i == 2 { + if !invert && (edges_i == 0 || edges_i == 2) { let has_up_roi = src.edge_mode.is_some() || upstream_infinite_edge_mode(comp, src_id, time, &self.expr).is_some(); if has_up_roi { @@ -640,7 +641,7 @@ impl Engine { u.p[1] = [sx, sy, angle, edges]; u.p[2] = [flip_h, flip_v, blend, if mask.is_some() { 1.0 } else { 0.0 }]; let mbm = comp.eval_number(node, "MultiplyByMask", time, 0.0, &self.expr) > 0.5; - u.p[3] = [if mask_inv { 1.0 } else { 0.0 }, nearest, if mbm { 1.0 } else { 0.0 }, 0.0]; + u.p[3] = [if mask_inv { 1.0 } else { 0.0 }, nearest, if mbm { 1.0 } else { 0.0 }, if invert { 1.0 } else { 0.0 }]; if src.has_dod() { u.p[4] = [ src.origin[0] as f32, @@ -1597,4 +1598,35 @@ mod tests { assert_eq!(px(1, 2, 2), [0, 0], "outside the mask is zeroed"); assert_eq!(px(1, 32, 32), [255, 255], "inside the mask is kept"); } + + #[test] + fn transform_invert_transform_shifts_the_opposite_way() { + let src = |invert: u8| { + format!( + r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 0 }}, GlobalRange = {{ 0, 0 }}, Tools = ordered() {{ + BG = Background {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, + Type = Input {{ Value = FuID {{ "Horizontal" }}, }}, + TopLeftRed = Input {{ Value = 1, }}, TopLeftGreen = Input {{ Value = 0, }}, TopLeftBlue = Input {{ Value = 0, }}, TopLeftAlpha = Input {{ Value = 1, }}, + TopRightRed = Input {{ Value = 0, }}, TopRightGreen = Input {{ Value = 1, }}, TopRightBlue = Input {{ Value = 0, }}, TopRightAlpha = Input {{ Value = 1, }}, }}, }}, + XF = Transform {{ Inputs = {{ InvertTransform = Input {{ Value = {invert}, }}, + Center = Input {{ Value = {{ 0.75, 0.5 }}, }}, Input = Input {{ SourceOp = "BG", Source = "Output", }}, }}, }}, + }} }}"# + ) + }; + if Engine::new_headless().is_err() { + return; + } + let mid_row = |invert: u8| { + let mut eng = Engine::new_headless().unwrap(); + let comp = Comp::from_fusion_str(&src(invert)).unwrap(); + let (tex, _) = eng.evaluate(&comp, comp.find_node("XF").unwrap(), 0.0); + let rgba = eng.readback_rgba(&tex).unwrap(); + let i = (32 * 64 + 32) * 4; + [rgba[i], rgba[i + 1]] + }; + let [r, g] = mid_row(0); + assert!(r > g + 60, "Center +0.25 moves the left (red) side under the middle: {r} {g}"); + let [r, g] = mid_row(1); + assert!(g > r + 60, "InvertTransform pulls the right (green) side under the middle: {r} {g}"); + } } diff --git a/crates/engine/src/shaders/archive_fx.wgsl b/crates/engine/src/shaders/archive_fx.wgsl index 57b306dfc1ee3c79425d9d7685cd8c33ccba4785..a900e143e6c3e75a0311b916a17dbcf9f5cff65c 100644 --- a/crates/engine/src/shaders/archive_fx.wgsl +++ b/crates/engine/src/shaders/archive_fx.wgsl @@ -86,8 +86,8 @@ fn dent_op(@builtin(global_invocation_id) gid: vec3) { // Dent: linear radial displacement (signed across center) new_r = r - strength * (radius - r); } else if (dent_type == 1) { - // Type 1: pi-scaled linear radial displacement - new_r = r - 3.14159265359 * strength * (radius - r); + // Sine: linear falloff from `strength` frame-widths at the center to 0 at the radius + new_r = r - strength * (1.0 - t); } else if (dent_type == 2) { // Pinch: power-law radial pinch/expand let p = select(1.0 / max(1.0 - strength, 0.05), 1.0 + strength, strength >= 0.0); diff --git a/crates/engine/src/shaders/transform.wgsl b/crates/engine/src/shaders/transform.wgsl index 45c39b4b6f009a5d358019abe303f1d8c6b522e6..6a47b084a55fe226c8ea59596ed18c7cd6fb53b4 100644 --- a/crates/engine/src/shaders/transform.wgsl +++ b/crates/engine/src/shaders/transform.wgsl @@ -3,7 +3,7 @@ // p[0] = (center.x, center.y, pivot.x, pivot.y) // p[1] = (size_x, size_y, angle_rad, edges) // p[2] = (flip_h, flip_v, blend, has_mask) -// p[3] = (mask_invert, nearest, multiply_by_mask, 0) +// p[3] = (mask_invert, nearest, multiply_by_mask, invert_transform) // ============================================================================ @compute @workgroup_size(8, 8) fn transform_op(@builtin(global_invocation_id) gid: vec3) { @@ -21,13 +21,24 @@ fn transform_op(@builtin(global_invocation_id) gid: vec3) { // Forward in Fusion: p' = R(angle) * S(sx, sy) * (p - pivot) + pivot + (center - 0.5) let shift = center - vec2(0.5); - let rel_px = (fu - pivot - shift) * fdims; let c = cos(angle); let s = sin(angle); - let unrot = vec2(rel_px.x * c + rel_px.y * s, -rel_px.x * s + rel_px.y * c); - var src_fu = vec2(unrot.x / sx, unrot.y / sy) / fdims + pivot; - if (u.p[2].x > 0.5) { src_fu.x = 1.0 - src_fu.x; } - if (u.p[2].y > 0.5) { src_fu.y = 1.0 - src_fu.y; } + var src_fu: vec2; + if (u.p[3].w > 0.5) { + // InvertTransform samples through the forward map instead of its inverse (flips apply first). + var p = fu; + if (u.p[2].x > 0.5) { p.x = 1.0 - p.x; } + if (u.p[2].y > 0.5) { p.y = 1.0 - p.y; } + let scaled = (p - pivot) * fdims * vec2(sx, sy); + let rot = vec2(scaled.x * c - scaled.y * s, scaled.x * s + scaled.y * c); + src_fu = rot / fdims + pivot + shift; + } else { + let rel_px = (fu - pivot - shift) * fdims; + let unrot = vec2(rel_px.x * c + rel_px.y * s, -rel_px.x * s + rel_px.y * c); + src_fu = vec2(unrot.x / sx, unrot.y / sy) / fdims + pivot; + if (u.p[2].x > 0.5) { src_fu.x = 1.0 - src_fu.x; } + if (u.p[2].y > 0.5) { src_fu.y = 1.0 - src_fu.y; } + } let nearest = u.p[3].y > 0.5; let orig = sample_dod_edges_mode(src_a, fu, 0, u.p[4], nearest); -- 2.54.1