authoravatarclover caruso<git@paperclover.net>2026-11-30 17:34:01-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 18:09:38-08:00
log5c885076bb1b170ed84529096c0fa94fce3c4cef
treec6dbbb2ef7e3d035ba42ff503d5c01813df9a18c
parentb1f52e3193b8a300ec518e5c74b547b4c52922d0
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Calibrate FastNoise parameter and color/alpha parity (#34)

Transfer curves measured from Fusion 21.1 same-lattice sweeps: - octaves sum unnormalized at 2x frequency, 1/2 amplitude - Discontinuous sums 2|n| per octave; Contrast pivots on 0 there, 0.5 otherwise - Inverted runs before Brightness; Brightness adds half its value - Gradient + Discontinuous shifts the lookup by 0.25, clamped once at the end - colors leave unpremultiplied (Merge treats them as premultiplied) Assisted-by: claude-sonnet-5.5

2 files changed, 169 insertions(+), 44 deletions(-)

crates/engine/src/shaders/keyers_gen.wgsl+30-44
......@@ -24,10 +24,11 @@ fn luma_key_op(@builtin(global_invocation_id) gid: vec3<u32>) {
2424 store_dst(vec2<i32>(gid.xy), out_col);
2525}
2626// ============================================================================
27// 8. FastNoise (3D Perlin Gradient FBM with Seethe, Angle, Discontinuous & LUT)
27// 8. FastNoise (3D Perlin FBM with Seethe, Angle, Discontinuous & gradient LUT)
2828// p[0] = (x_scale, y_scale, detail, contrast)
2929// p[1] = (brightness, seethe, discontinuous, center_x)
30// p[2] = (center_y, angle_rad, inverted, use_lut)
30// 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 value
3132// ============================================================================
3233fn grad3_dot(i: vec3<f32>, f: vec3<f32>) -> f32 {
3334 // Deterministic 3D gradient hash -> 12 Perlin cube-edge directions (smooth isotropic blobs!)
......@@ -63,6 +64,13 @@ fn noise3d(p: vec3<f32>) -> f32 {
6364 return mix(nxy0, nxy1, u_f.z);
6465}
6566
67// Measured against Fusion 21.1 (same-lattice parameter sweeps): one octave spans
68// 0.5 +/- 0.26 (std 0.106), later octaves are 2x frequency at half amplitude and
69// are summed unnormalized, Discontinuous sums 2|n| per octave, Contrast pivots
70// on 0.5 (on 0 when Discontinuous), Inverted runs before Brightness, Brightness
71// adds half its value, and Gradient + Discontinuous shifts the lookup by 0.25.
72const NOISE_GAIN: f32 = 0.40;
73
6674@compute @workgroup_size(8, 8)
6775fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
6876 let dims = textureDimensions(dst_tex);
......@@ -77,8 +85,9 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
7785 let seethe = u.p[1].y;
7886 let disc = u.p[1].z > 0.5;
7987 let angle = u.p[2].y;
88 let gradient = u.p[2].w > 1.5;
8089
81 // Fusion FastNoise uses frame-width isotropic units `(fu - center) * vec2(1, 1/aspect)`
90 // Frame-width isotropic units
8291 var rel = (fu - center) * vec2<f32>(1.0, 1.0 / aspect);
8392 if (abs(angle) > 1e-5) {
8493 let ca = cos(angle);
......@@ -87,49 +96,26 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3<u32>) {
8796 }
8897 let p = rel * scale;
8998
90 var val = 0.5 + brightness;
91 if (detail > 1e-4) {
92 let octaves = i32(ceil(detail));
93 var sum = 0.0;
94 var norm = 0.0;
95 var amp = 1.0;
96 var freq = 1.0;
97 for (var i = 0; i < 10; i = i + 1) {
98 if (i >= octaves) { break; }
99 let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4);
100 let n = noise3d(vec3<f32>(p * freq, seethe + f32(i) * 1.73));
101 sum = sum + select(n, abs(n), disc) * weight;
102 norm = norm + weight;
103 freq = freq * 2.0;
104 amp = amp * 0.5;
105 }
106 let n_avg = sum / max(norm, 1.0);
107 // Perlin3D normalized fBm: at Detail=1 (norm=1), *0.41 matches Fusion std=0.106 [0.24, 0.76];
108 // at Detail=10 (norm=2), std naturally narrows to ~0.060 matching Fusion's pinpoint starfield threshold!
109 // Discontinuous mode takes |n| turbulence (~1.60x -> mean 0.34, std 0.14 in Type=0 TwoColor),
110 // pivoting Contrast around 0.5 when Contrast != 1 (verified via probe_moon_3d f02 & displace_wave f05/f07/f08).
111 if (disc) {
112 let grad_bias = select(0.0, 0.25, u.p[2].w > 1.5);
113 let base = n_avg * 1.60 + grad_bias;
114 val = (base - 0.5) * contrast + 0.5 + brightness;
115 } else {
116 val = n_avg * 0.41 * contrast + 0.5 + brightness;
117 }
99 let octaves = i32(ceil(detail));
100 var sum = 0.0;
101 var amp = 1.0;
102 var freq = 1.0;
103 for (var i = 0; i < octaves; i = i + 1) {
104 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;
106 sum = sum + weight * select(n, 2.0 * abs(n), disc);
107 freq = freq * 2.0;
108 amp = amp * 0.5;
118109 }
119 val = clamp(val, 0.0, 1.0);
110
111 var val = select(0.5 + sum, sum, disc);
112 val = select((val - 0.5) * contrast + 0.5, val * contrast, disc);
113 if (gradient && disc) { val = val + 0.25; }
120114 if (u.p[2].z > 0.5) { val = 1.0 - val; }
121 var col: vec4<f32>;
122 if (u.p[2].w > 0.5) {
123 // High-precision 256x1 gradient LUT in src_b (supports razor-sharp thresholds like starfields!)
124 col = textureSampleLevel(src_b, samp_lin, vec2<f32>(val, 0.5), 0.0);
125 } else {
126 let lut_pos = val * 12.0;
127 let i0 = u32(clamp(floor(lut_pos), 0.0, 11.0));
128 let f = lut_pos - f32(i0);
129 col = mix(u.p[3u + i0], u.p[4u + i0], f);
130 }
131 let out_col = vec4<f32>(col.rgb * col.a, col.a);
132 store_dst(vec2<i32>(gid.xy), out_col);
115 val = clamp(val + 0.5 * brightness, 0.0, 1.0);
116
117 // Fusion leaves the ramp color unpremultiplied; downstream Merge treats it as premultiplied
118 store_dst(vec2<i32>(gid.xy), textureSampleLevel(src_b, samp_lin, vec2<f32>(val, 0.5), 0.0));
133119}
134120// ============================================================================
135121// 12. Fuse.Outline (Clover's exact 2020 Outline.fuse passes!)
crates/engine/tests/fast_noise.rs created+139
......@@ -0,0 +1,139 @@
1//! FastNoise parameter transfer curves, checked pixel-for-pixel against a same-lattice baseline.
2//! The curves were measured from Fusion 21.1 sweeps where only one input changes.
3
4use engine::{Engine, ExportOptions, export_saver_png_sequence};
5use model::Comp;
6
7const SIZE: u32 = 96;
8const LINEAR_GRADIENT: &str = "Gradient { Colors = { [0] = { 0, 0, 0, 1 }, [1] = { 1, 1, 1, 1 } } }";
9
10fn render(tag: &str, overrides: &[(&str, &str)]) -> Vec<[f32; 4]> {
11 let mut inputs: Vec<(&str, &str)> = vec![
12 ("Detail", "1"),
13 ("XScale", "4"),
14 ("Color1Alpha", "1"),
15 ];
16 for (key, value) in overrides {
17 inputs.retain(|(k, _)| k != key);
18 inputs.push((key, value));
19 }
20 let inputs: String = inputs.iter().map(|(k, v)| format!("\t\t\t\t{k} = Input {{ Value = {v}, }},\n")).collect();
21
22 let dir = std::env::temp_dir().join(format!("fission-fast-noise-{}-{tag}", std::process::id()));
23 let _ = std::fs::remove_dir_all(&dir);
24 let path = dir.join("out_0000.png");
25 let path = path.to_string_lossy().replace('\\', "\\\\");
26 let src = format!(
27 r#"Composition {{
28 RenderRange = {{ 0, 0 }},
29 Tools = ordered() {{
30 Noise = FastNoise {{
31 Inputs = {{
32 Width = Input {{ Value = {SIZE}, }},
33 Height = Input {{ Value = {SIZE}, }},
34{inputs} }},
35 }},
36 Black = Background {{
37 Inputs = {{
38 Width = Input {{ Value = {SIZE}, }},
39 Height = Input {{ Value = {SIZE}, }},
40 TopLeftAlpha = Input {{ Value = 1, }},
41 }},
42 }},
43 Over = Merge {{
44 Inputs = {{
45 Background = Input {{ SourceOp = "Black", Source = "Output", }},
46 Foreground = Input {{ SourceOp = "Noise", Source = "Output", }},
47 }},
48 }},
49 Saver1 = Saver {{
50 Inputs = {{
51 Clip = Input {{ Value = Clip {{ Filename = "{path}", FormatID = "PNGFormat", Saving = true, }}, }},
52 Input = Input {{ SourceOp = "Over", Source = "Output", }},
53 }},
54 }},
55 }},
56 Prefs = {{ Comp = {{ FrameFormat = {{ Width = {SIZE}, Height = {SIZE}, Rate = 24, }}, }}, }},
57}}"#
58 );
59 let comp = Comp::from_fusion_str(&src).expect("parse FastNoise comp");
60 let mut engine = Engine::new_headless().expect("gpu");
61 export_saver_png_sequence(&mut engine, &comp, &ExportOptions::default(), |_| {}).expect("export");
62 let img = image::open(dir.join("out_0000.png")).expect("decode").to_rgba8();
63 let _ = std::fs::remove_dir_all(&dir);
64 img.pixels().map(|p| p.0.map(|c| c as f32 / 255.0)).collect()
65}
66
67/// Asserts `out == expected(base)` per pixel on pixels where `valid(base)` holds, and that enough pixels qualify.
68fn assert_transfer(base: &[[f32; 4]], out: &[[f32; 4]], valid: impl Fn(f32) -> bool, expected: impl Fn(f32) -> f32) {
69 let mut checked = 0;
70 for (b, o) in base.iter().zip(out) {
71 if !valid(b[0]) {
72 continue;
73 }
74 checked += 1;
75 let want = expected(b[0]);
76 assert!((o[0] - want).abs() <= 4.0 / 255.0, "base {:.3}: got {:.3}, want {want:.3}", b[0], o[0]);
77 }
78 assert!(checked > base.len() / 4, "only {checked} of {} pixels exercised", base.len());
79}
80
81fn std_dev(px: &[[f32; 4]]) -> f32 {
82 let mean = px.iter().map(|p| p[0]).sum::<f32>() / px.len() as f32;
83 (px.iter().map(|p| (p[0] - mean).powi(2)).sum::<f32>() / px.len() as f32).sqrt()
84}
85
86#[test]
87fn brightness_adds_half_its_value() {
88 let base = render("b0", &[]);
89 let bright = render("b1", &[("Brightness", "0.2")]);
90 assert_transfer(&base, &bright, |a| a < 0.85, |a| a + 0.1);
91}
92
93#[test]
94fn contrast_pivots_on_half() {
95 let base = render("c0", &[]);
96 let contrast = render("c1", &[("Contrast", "2")]);
97 assert_transfer(&base, &contrast, |a| (0.3..0.7).contains(&a), |a| (a - 0.5) * 2.0 + 0.5);
98}
99
100#[test]
101fn discontinuous_folds_noise_and_scales_contrast_from_zero() {
102 let base = render("d0", &[]);
103 let disc = render("d1", &[("Discontinuous", "1")]);
104 assert_transfer(&base, &disc, |_| true, |a| 2.0 * (a - 0.5).abs());
105 let disc_contrast = render("d2", &[("Discontinuous", "1"), ("Contrast", "0.5")]);
106 assert_transfer(&base, &disc_contrast, |_| true, |a| (a - 0.5).abs());
107}
108
109#[test]
110fn inverted_runs_before_brightness() {
111 let base = render("i0", &[]);
112 let inverted = render("i1", &[("Inverted", "1"), ("Brightness", "0.2")]);
113 assert_transfer(&base, &inverted, |a| a > 0.1, |a| 1.0 - a + 0.1);
114}
115
116#[test]
117fn gradient_with_discontinuous_shifts_lookup_by_quarter() {
118 let base = render("g0", &[]);
119 let plain = render("g1", &[("Type", "1"), ("Gradient", LINEAR_GRADIENT)]);
120 assert_transfer(&base, &plain, |_| true, |a| a);
121 let disc = render("g2", &[("Type", "1"), ("Gradient", LINEAR_GRADIENT), ("Discontinuous", "1")]);
122 assert_transfer(&base, &disc, |_| true, |a| (2.0 * (a - 0.5).abs() + 0.25).min(1.0));
123}
124
125#[test]
126fn detail_octaves_sum_unnormalized() {
127 let one = std_dev(&render("o1", &[("XScale", "8")]));
128 let three = std_dev(&render("o3", &[("XScale", "8"), ("Detail", "3")]));
129 // sqrt(1 + 1/4 + 1/16) = 1.146 for independent octaves at amplitudes 1, 1/2, 1/4
130 assert!((1.05..1.25).contains(&(three / one)), "std ratio {}", three / one);
131}
132
133#[test]
134fn colors_are_not_premultiplied_by_alpha() {
135 let px = render("a0", &[("Color1Alpha", "0.5"), ("Color1Red", "1"), ("Color2Alpha", "0.5")]);
136 // red is 1 at both ends of the ramp, so a premultiplied output would land near 0.5
137 assert!(px.iter().all(|p| p[0] > 0.99), "min red {}", px.iter().map(|p| p[0]).fold(1.0, f32::min));
138 assert!(std_dev(&px.iter().map(|p| [p[1], 0.0, 0.0, 0.0]).collect::<Vec<_>>()) > 0.05);
139}