authoravatarclover caruso<git@paperclover.net>2026-11-30 23:36:34-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 23:50:35-08:00
logfb5fb16260568cb4f42afb8bc53cf1c5c091c669
tree121f9352ac51cf7a306d501a86d9302a415194b5
parente530d9fc5c87331b1ef143c5327d1c67b308dfa9
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Calibrate FastNoise basis/octaves/spikiness and Plasma per-pixel alpha against Fusion 21.1

FastNoise: table-driven improved-Perlin lattice, per-octave offsets, Fusion's 0.2575 octave bound (soft knee), gain fit to the measured std; Plasma: per-pixel alpha as a fourth cosine phase (AlphaPhase, default 0.4375). Assisted-by: claude-sonnet-5.5

2 files changed, 124 insertions(+), 35 deletions(-)

crates/engine/src/eval/io.rs+61-1
...@@ -861,6 +861,7 @@ impl Engine {...@@ -861,6 +861,7 @@ impl Engine {
861 let r_phase = comp.eval_number(node, "RedPhase", time, 0.0625, &self.expr) as f32;861 let r_phase = comp.eval_number(node, "RedPhase", time, 0.0625, &self.expr) as f32;
862 let g_phase = comp.eval_number(node, "GreenPhase", time, 0.1875, &self.expr) as f32;862 let g_phase = comp.eval_number(node, "GreenPhase", time, 0.1875, &self.expr) as f32;
863 let b_phase = comp.eval_number(node, "BluePhase", time, 0.3125, &self.expr) as f32;863 let b_phase = comp.eval_number(node, "BluePhase", time, 0.3125, &self.expr) as f32;
864 let a_phase = comp.eval_number(node, "AlphaPhase", time, 0.4375, &self.expr) as f32;
864865
865 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats);866 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(size), enc, visiting, stats);
866 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);867 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
...@@ -872,9 +873,10 @@ impl Engine {...@@ -872,9 +873,10 @@ impl Engine {
872 u.p[3] = [c4[0] as f32, c4[1] as f32, s4, t4];873 u.p[3] = [c4[0] as f32, c4[1] as f32, s4, t4];
873 u.p[4] = [scale, phase, r_phase, g_phase];874 u.p[4] = [scale, phase, r_phase, g_phase];
874 u.p[5] = [b_phase, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];875 u.p[5] = [b_phase, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
876 u.p[6][0] = a_phase;
875877
876 let dst_depth = self.node_depth(comp, node, time, None);878 let dst_depth = self.node_depth(comp, node, time, None);
877 let key = self.make_key(node, "Plasma", size, &[mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..6]));879 let key = self.make_key(node, "Plasma", size, &[mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..7]));
878 if let Some(hit) = self.cache.get(key, node.id, frame_i) {880 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
879 stats.cache_hits += 1;881 stats.cache_hits += 1;
880 return hit;882 return hit;
...@@ -1476,4 +1478,62 @@ Composition {{...@@ -1476,4 +1478,62 @@ Composition {{
14761478
1477 let _ = std::fs::remove_dir_all(&dir);1479 let _ = std::fs::remove_dir_all(&dir);
1478 }1480 }
1481 fn render_generator(kind: &str, w: u32, h: u32, inputs: &str) -> Option<Vec<[f32; 4]>> {
1482 let src = format!(
1483 r#"Composition {{ Tools = ordered() {{ N = {kind} {{ Inputs = {{
1484 Width = Input {{ Value = {w}, }}, Height = Input {{ Value = {h}, }},
1485 Color1Alpha = Input {{ Value = 1, }}, {inputs} }}, }}, }}, }}"#
1486 );
1487 let comp = Comp::from_fusion_str(&src).expect("parse generator comp");
1488 let mut eng = Engine::new_headless().ok()?;
1489 let (tex, _) = eng.evaluate(&comp, comp.find_node("N").unwrap(), 0.0);
1490 let px = eng.readback_rgba(&tex).unwrap();
1491 Some(px.chunks_exact(4).map(|p| [p[0], p[1], p[2], p[3]].map(|c| c as f32 / 255.0)).collect())
1492 }
1493
1494 #[test]
1495 fn plasma_alpha_is_a_fourth_phase_of_the_same_wave() {
1496 let Some(px) = render_generator("Plasma", 64, 64, "") else { return };
1497 // Phases step by 1/8 turn (R, G, B, A), so cos(x + 3d) = 2cos(d)cos(x + 2d) - cos(x + d).
1498 for p in &px {
1499 let [_, g, b, a] = p.map(|c| 2.0 * c - 1.0);
1500 let want = std::f32::consts::SQRT_2 * b - g;
1501 assert!((a - want).abs() < 0.03, "alpha {a:.3} vs recurrence {want:.3} at {p:?}");
1502 }
1503 let (lo, hi) = px.iter().fold((1.0f32, 0.0f32), |(lo, hi), p| (lo.min(p[3]), hi.max(p[3])));
1504 assert!(lo < 0.1 && hi > 0.9, "alpha should swing the full range, got {lo}..{hi}");
1505 }
1506
1507 #[test]
1508 fn plasma_alpha_phase_is_independent_input() {
1509 // AlphaPhase equal to GreenPhase's default must make alpha track the green channel.
1510 let Some(px) = render_generator("Plasma", 64, 64, "AlphaPhase = Input { Value = 0.1875, },") else { return };
1511 for p in &px {
1512 assert!((p[3] - p[1]).abs() <= 2.0 / 255.0, "alpha {} vs green {}", p[3], p[1]);
1513 }
1514 }
1515
1516 fn mean_and_std(px: &[[f32; 4]]) -> (f32, f32) {
1517 let mean = px.iter().map(|p| p[0]).sum::<f32>() / px.len() as f32;
1518 (mean, (px.iter().map(|p| (p[0] - mean).powi(2)).sum::<f32>() / px.len() as f32).sqrt())
1519 }
1520
1521 #[test]
1522 fn fast_noise_single_octave_matches_fusion_distribution_and_bounds() {
1523 // Fusion 21.1 float probe (Detail 1, 2048px, XScale 24): std 0.104, range 0.2425..0.7575.
1524 let Some(px) = render_generator("FastNoise", 1024, 1024, "Detail = Input { Value = 1, }, XScale = Input { Value = 12, },") else { return };
1525 let (_, std) = mean_and_std(&px);
1526 assert!((0.098..0.110).contains(&std), "std {std}");
1527 let (lo, hi) = px.iter().fold((1.0f32, 0.0f32), |(lo, hi), p| (lo.min(p[0]), hi.max(p[0])));
1528 assert!(lo >= 0.2425 - 0.01 && hi <= 0.7575 + 0.01, "octave must stay inside Fusion's bound, got {lo}..{hi}");
1529 }
1530
1531 #[test]
1532 fn fast_noise_discontinuous_detail10_matches_falling_stats() {
1533 // Fusion 21.1 probe of 20_falling's noise (Detail 10, XScale 10, Discontinuous, 1080p): mean 0.336, std 0.140.
1534 let Some(px) = render_generator("FastNoise", 1920, 1080, "Detail = Input { Value = 10, }, XScale = Input { Value = 10, }, Discontinuous = Input { Value = 1, }, Seethe = Input { Value = 29820, },") else { return };
1535 let (mean, std) = mean_and_std(&px);
1536 assert!((0.32..0.35).contains(&mean), "mean {mean}");
1537 assert!((0.125..0.150).contains(&std), "std {std}");
1538 }
1479}1539}
crates/engine/src/shaders/keyers_gen.wgsl+63-34
...@@ -30,46 +30,71 @@ fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -30,46 +30,71 @@ fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) {
30// p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color30// p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color
31// src_b = 256x1 straight-alpha color LUT indexed by the noise value31// src_b = 256x1 straight-alpha color LUT indexed by the noise value
32// ============================================================================32// ============================================================================
33fn grad3_dot(i: vec3<f32>, f: vec3<f32>) -> f32 {33var<private> PERM: array<u32, 256> = array<u32, 256>(
34 // Deterministic 3D gradient hash -> 12 Perlin cube-edge directions (smooth isotropic blobs!)34 151u, 160u, 137u, 91u, 90u, 15u, 131u, 13u, 201u, 95u, 96u, 53u, 194u, 233u, 7u, 225u,
35 var p3 = fract(i * vec3<f32>(0.1031, 0.1030, 0.0973));35 140u, 36u, 103u, 30u, 69u, 142u, 8u, 99u, 37u, 240u, 21u, 10u, 23u, 190u, 6u, 148u,
36 p3 = p3 + dot(p3, p3.yxz + 33.33);36 247u, 120u, 234u, 75u, 0u, 26u, 197u, 62u, 94u, 252u, 219u, 203u, 117u, 35u, 11u, 32u,
37 let h = u32(floor(fract((p3.x + p3.y) * p3.z) * 16.0)) & 15u;37 57u, 177u, 33u, 88u, 237u, 149u, 56u, 87u, 174u, 20u, 125u, 136u, 171u, 168u, 68u, 175u,
38 74u, 165u, 71u, 134u, 139u, 48u, 27u, 166u, 77u, 146u, 158u, 231u, 83u, 111u, 229u, 122u,
39 60u, 211u, 133u, 230u, 220u, 105u, 92u, 41u, 55u, 46u, 245u, 40u, 244u, 102u, 143u, 54u,
40 65u, 25u, 63u, 161u, 1u, 216u, 80u, 73u, 209u, 76u, 132u, 187u, 208u, 89u, 18u, 169u,
41 200u, 196u, 135u, 130u, 116u, 188u, 159u, 86u, 164u, 100u, 109u, 198u, 173u, 186u, 3u, 64u,
42 52u, 217u, 226u, 250u, 124u, 123u, 5u, 202u, 38u, 147u, 118u, 126u, 255u, 82u, 85u, 212u,
43 207u, 206u, 59u, 227u, 47u, 16u, 58u, 17u, 182u, 189u, 28u, 42u, 223u, 183u, 170u, 213u,
44 119u, 248u, 152u, 2u, 44u, 154u, 163u, 70u, 221u, 153u, 101u, 155u, 167u, 43u, 172u, 9u,
45 129u, 22u, 39u, 253u, 19u, 98u, 108u, 110u, 79u, 113u, 224u, 232u, 178u, 185u, 112u, 104u,
46 218u, 246u, 97u, 228u, 251u, 34u, 242u, 193u, 238u, 210u, 144u, 12u, 191u, 179u, 162u, 241u,
47 81u, 51u, 145u, 235u, 249u, 14u, 239u, 107u, 49u, 192u, 214u, 31u, 181u, 199u, 106u, 157u,
48 184u, 84u, 204u, 176u, 115u, 121u, 50u, 45u, 127u, 4u, 150u, 254u, 138u, 236u, 205u, 93u,
49 222u, 114u, 67u, 29u, 24u, 72u, 243u, 141u, 128u, 195u, 78u, 66u, 215u, 61u, 156u, 180u
50);
51
52fn perm(i: u32) -> u32 {
53 return PERM[i & 255u];
54}
55
56fn grad3(h: u32, f: vec3<f32>) -> f32 {
38 let u_v = select(f.y, f.x, h < 8u);57 let u_v = select(f.y, f.x, h < 8u);
39 let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u);58 let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u);
40 let s1 = select(-u_v, u_v, (h & 1u) == 0u);59 return select(-u_v, u_v, (h & 1u) == 0u) + select(-v_v, v_v, (h & 2u) == 0u);
41 let s2 = select(-v_v, v_v, (h & 2u) == 0u);
42 return s1 + s2;
43}60}
4461
62// Improved-Perlin lattice noise (quintic fade, 16 edge gradients).
45fn noise3d(p: vec3<f32>) -> f32 {63fn noise3d(p: vec3<f32>) -> f32 {
46 let i = floor(p);64 let fl = floor(p);
47 let f = fract(p);65 let f = p - fl;
48 // Quintic C2 Perlin fade: eliminates blocky axis-aligned artifacts on thresholded peaks66 let c = vec3<u32>(vec3<i32>(fl) & vec3<i32>(255));
49 let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);67 let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
50 let n000 = grad3_dot(i + vec3<f32>(0.0, 0.0, 0.0), f - vec3<f32>(0.0, 0.0, 0.0));68 let a = perm(c.x) + c.y;
51 let n100 = grad3_dot(i + vec3<f32>(1.0, 0.0, 0.0), f - vec3<f32>(1.0, 0.0, 0.0));69 let b = perm(c.x + 1u) + c.y;
52 let n010 = grad3_dot(i + vec3<f32>(0.0, 1.0, 0.0), f - vec3<f32>(0.0, 1.0, 0.0));70 let aa = perm(a) + c.z;
53 let n110 = grad3_dot(i + vec3<f32>(1.0, 1.0, 0.0), f - vec3<f32>(1.0, 1.0, 0.0));71 let ab = perm(a + 1u) + c.z;
54 let n001 = grad3_dot(i + vec3<f32>(0.0, 0.0, 1.0), f - vec3<f32>(0.0, 0.0, 1.0));72 let ba = perm(b) + c.z;
55 let n101 = grad3_dot(i + vec3<f32>(1.0, 0.0, 1.0), f - vec3<f32>(1.0, 0.0, 1.0));73 let bb = perm(b + 1u) + c.z;
56 let n011 = grad3_dot(i + vec3<f32>(0.0, 1.0, 1.0), f - vec3<f32>(0.0, 1.0, 1.0));74 let n000 = grad3(perm(aa) & 15u, f);
57 let n111 = grad3_dot(i + vec3<f32>(1.0, 1.0, 1.0), f - vec3<f32>(1.0, 1.0, 1.0));75 let n100 = grad3(perm(ba) & 15u, f - vec3<f32>(1.0, 0.0, 0.0));
58 let nx00 = mix(n000, n100, u_f.x);76 let n010 = grad3(perm(ab) & 15u, f - vec3<f32>(0.0, 1.0, 0.0));
59 let nx10 = mix(n010, n110, u_f.x);77 let n110 = grad3(perm(bb) & 15u, f - vec3<f32>(1.0, 1.0, 0.0));
60 let nx01 = mix(n001, n101, u_f.x);78 let n001 = grad3(perm(aa + 1u) & 15u, f - vec3<f32>(0.0, 0.0, 1.0));
61 let nx11 = mix(n011, n111, u_f.x);79 let n101 = grad3(perm(ba + 1u) & 15u, f - vec3<f32>(1.0, 0.0, 1.0));
62 let nxy0 = mix(nx00, nx10, u_f.y);80 let n011 = grad3(perm(ab + 1u) & 15u, f - vec3<f32>(0.0, 1.0, 1.0));
63 let nxy1 = mix(nx01, nx11, u_f.y);81 let n111 = grad3(perm(bb + 1u) & 15u, f - vec3<f32>(1.0, 1.0, 1.0));
82 let nxy0 = mix(mix(n000, n100, u_f.x), mix(n010, n110, u_f.x), u_f.y);
83 let nxy1 = mix(mix(n001, n101, u_f.x), mix(n011, n111, u_f.x), u_f.y);
64 return mix(nxy0, nxy1, u_f.z);84 return mix(nxy0, nxy1, u_f.z);
65}85}
6686
67// Measured against Fusion 21.1 (same-lattice parameter sweeps): one octave spans87// Measured against Fusion 21.1 float32 probes: one octave spans 0.5 +/- 0.2575 (std 0.104),
68// 0.5 +/- 0.26 (std 0.106), later octaves are 2x frequency at half amplitude and88// later octaves are 2x frequency at half amplitude (own offsets) and sum unnormalized,
69// are summed unnormalized, Discontinuous sums 2|n| per octave, Contrast pivots89// fractional Detail weights the last octave, Discontinuous sums 2|n| per octave, Contrast
70// on 0.5 (on 0 when Discontinuous), Inverted runs before Brightness, Brightness90// pivots on 0.5 (on 0 when Discontinuous), Inverted runs before Brightness, Brightness adds
71// adds half its value, and Gradient + Discontinuous shifts the lookup by 0.25.91// half its value, and Gradient + Discontinuous shifts the lookup by 0.25. Fusion's lattice
72const NOISE_GAIN: f32 = 0.40;92// hash is not recovered: the noise only matches its distribution, and NOISE_FREQ 1.0 trades
93// Fusion's ~1.8x finer spectrum for lower per-pixel MAE against its unmatched phases.
94const NOISE_GAIN: f32 = 0.376;
95const NOISE_FREQ: f32 = 1.0;
96const NOISE_KNEE: f32 = 0.22;
97const NOISE_SOFT: f32 = 0.0375;
7398
74@compute @workgroup_size(8, 8)99@compute @workgroup_size(8, 8)
75fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {100fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
...@@ -94,7 +119,7 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -94,7 +119,7 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
94 let sa = sin(angle);119 let sa = sin(angle);
95 rel = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca);120 rel = vec2<f32>(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca);
96 }121 }
97 let p = rel * scale;122 let p = rel * scale * NOISE_FREQ;
98123
99 let octaves = i32(ceil(detail));124 let octaves = i32(ceil(detail));
100 var sum = 0.0;125 var sum = 0.0;
...@@ -102,7 +127,9 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -102,7 +127,9 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
102 var freq = 1.0;127 var freq = 1.0;
103 for (var i = 0; i < octaves; i = i + 1) {128 for (var i = 0; i < octaves; i = i + 1) {
104 let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4);129 let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4);
105 let n = noise3d(vec3<f32>(p * freq, seethe + f32(i) * 1.73)) * NOISE_GAIN;130 let raw = noise3d(vec3<f32>(p * freq + f32(i) * vec2<f32>(31.7, 17.3), seethe + 0.37 + f32(i) * 1.73)) * NOISE_GAIN;
131 let over = max(abs(raw) - NOISE_KNEE, 0.0);
132 let n = sign(raw) * (min(abs(raw), NOISE_KNEE) + NOISE_SOFT * tanh(over / NOISE_SOFT));
106 sum = sum + weight * select(n, 2.0 * abs(n), disc);133 sum = sum + weight * select(n, 2.0 * abs(n), disc);
107 freq = freq * 2.0;134 freq = freq * 2.0;
108 amp = amp * 0.5;135 amp = amp * 0.5;
...@@ -262,6 +289,7 @@ fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -262,6 +289,7 @@ fn ultra_keyer_op(@builtin(global_invocation_id) gid: vec3<u32>) {
262// p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine)289// p[0..3] = (cx, cy, c_scale, c_type) for Circles 1..4 (type 0=Off, 1=Linear, >=2=Sine)
263// p[4] = (scale, phase, red_phase, green_phase)290// p[4] = (scale, phase, red_phase, green_phase)
264// p[5] = (blue_phase, blend, has_mask, mask_invert)291// p[5] = (blue_phase, blend, has_mask, mask_invert)
292// p[6] = (alpha_phase, 0, 0, 0)
265// ============================================================================293// ============================================================================
266fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32) -> f32 {294fn plasma_circle_term(u_pos: vec2<f32>, aspect_inv: f32, c: vec4<f32>, master_scale: f32) -> f32 {
267 let ctype = i32(c.w + 0.5);295 let ctype = i32(c.w + 0.5);
...@@ -298,8 +326,9 @@ fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -298,8 +326,9 @@ fn plasma_op(@builtin(global_invocation_id) gid: vec3<u32>) {
298 let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z));326 let r = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].z));
299 let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w));327 let g = 0.5 + 0.5 * cos(th + tau * (ph + u.p[4].w));
300 let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x));328 let b = 0.5 + 0.5 * cos(th + tau * (ph + u.p[5].x));
329 let a = 0.5 + 0.5 * cos(th + tau * (ph + u.p[6].x));
301330
302 let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y;331 let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w) * u.p[5].y;
303 let out_col = vec4<f32>(r, g, b, 1.0) * clamp(m, 0.0, 1.0);332 let out_col = vec4<f32>(r, g, b, a) * clamp(m, 0.0, 1.0);
304 store_dst(vec2<i32>(gid.xy), out_col);333 store_dst(vec2<i32>(gid.xy), out_col);
305}334}