From d0c875aa773e5d28656a545f701099d66106098c Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 1 Dec 2026 00:33:44 -0800 Subject: [PATCH] FastNoise: exact 3D simplex lattice recovered from Float32 probes Replace the improved-Perlin approximation with Fusion's lattice: Gustavson-style 3D simplex noise, kernel 16.642 * (0.5196152 - r^2)^4, hash perm[k + perm[j + perm[i]]] & 15 into a 16-entry edge-gradient table. Octave i samples 2^i * (x, y, Seethe) + 50.3 * i at weight 0.5^i; Seethe is the z coordinate. Coordinates are frame-width units measured from the pixel corner (y up), Angle rotates clockwise before the per-axis scale. Falling noise nodes now agree with Fusion to one 8-bit LSB. MAE vs the previous shader: falling_2360 4.23 -> 3.07, falling_2400 0.95 -> 0.58, falling_2199 0.50 -> 0.45, violet_4000 0.66 -> 0.21; falling_2380 1.02 -> 1.25. Assisted-by: claude-sonnet-5.5 --- crates/engine/src/eval/io.rs | 40 ++++---- crates/engine/src/shaders/keyers_gen.wgsl | 109 +++++++++++----------- 2 files changed, 73 insertions(+), 76 deletions(-) diff --git a/crates/engine/src/eval/io.rs b/crates/engine/src/eval/io.rs index 7f6b415216d397c7ce779ebd486fb15d1075d80d..5c5196198675d2d3575d0353dad6b3d4ccfc40a9 100644 --- a/crates/engine/src/eval/io.rs +++ b/crates/engine/src/eval/io.rs @@ -1513,27 +1513,25 @@ Composition {{ } } - fn mean_and_std(px: &[[f32; 4]]) -> (f32, f32) { - let mean = px.iter().map(|p| p[0]).sum::() / px.len() as f32; - (mean, (px.iter().map(|p| (p[0] - mean).powi(2)).sum::() / px.len() as f32).sqrt()) - } - + /// Fusion 21.1 Float32 probes (R at x, y) of the simplex lattice, octave offsets, Seethe, Angle, Center and YScale. #[test] - fn fast_noise_single_octave_matches_fusion_distribution_and_bounds() { - // Fusion 21.1 float probe (Detail 1, 2048px, XScale 24): std 0.104, range 0.2425..0.7575. - let Some(px) = render_generator("FastNoise", 1024, 1024, "Detail = Input { Value = 1, }, XScale = Input { Value = 12, },") else { return }; - let (_, std) = mean_and_std(&px); - assert!((0.098..0.110).contains(&std), "std {std}"); - let (lo, hi) = px.iter().fold((1.0f32, 0.0f32), |(lo, hi), p| (lo.min(p[0]), hi.max(p[0]))); - assert!(lo >= 0.2425 - 0.01 && hi <= 0.7575 + 0.01, "octave must stay inside Fusion's bound, got {lo}..{hi}"); - } - - #[test] - fn fast_noise_discontinuous_detail10_matches_falling_stats() { - // Fusion 21.1 probe of 20_falling's noise (Detail 10, XScale 10, Discontinuous, 1080p): mean 0.336, std 0.140. - 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 }; - let (mean, std) = mean_and_std(&px); - assert!((0.32..0.35).contains(&mean), "mean {mean}"); - assert!((0.125..0.150).contains(&std), "std {std}"); + fn fast_noise_matches_fusion_float_probes() { + let cases: [(u32, u32, &str, [(usize, usize, f32); 6]); 4] = [ + (512, 512, "Detail = Input { Value = 2, }, XScale = Input { Value = 4, }, Seethe = Input { Value = 1, },", + [(37, 41, 0.4457), (200, 150, 0.5710), (311, 17, 0.4191), (480, 333, 0.5275), (90, 300, 0.4076), (255, 255, 0.2991)]), + (640, 360, "Detail = Input { Value = 3, }, XScale = Input { Value = 4, }, Discontinuous = Input { Value = 1, },", + [(37, 41, 0.2150), (200, 150, 0.4042), (311, 17, 0.1601), (480, 333, 0.1774), (90, 300, 0.1933), (255, 255, 0.1025)]), + (640, 360, "Detail = Input { Value = 1, }, XScale = Input { Value = 4, }, Angle = Input { Value = 30, }, Center = Input { Value = { 0.3, 0.6 }, },", + [(37, 41, 0.4068), (200, 150, 0.5251), (311, 17, 0.4916), (480, 333, 0.4710), (90, 300, 0.5406), (255, 255, 0.7552)]), + (640, 360, "Detail = Input { Value = 1, }, XScale = Input { Value = 4, }, LockXY = Input { Value = 0, }, YScale = Input { Value = 7, },", + [(37, 41, 0.3507), (200, 150, 0.2588), (311, 17, 0.5995), (480, 333, 0.5001), (90, 300, 0.5444), (255, 255, 0.5119)]), + ]; + for (w, h, inputs, probes) in cases { + let Some(px) = render_generator("FastNoise", w, h, inputs) else { return }; + for (x, y, want) in probes { + let got = px[y * w as usize + x][0]; + assert!((got - want).abs() <= 1.5 / 255.0, "{inputs} at ({x}, {y}): got {got:.4}, want {want:.4}"); + } + } } } diff --git a/crates/engine/src/shaders/keyers_gen.wgsl b/crates/engine/src/shaders/keyers_gen.wgsl index e16625ff8d5e558ec6a85e21ec9cf67f820fc596..c32c7dd258e9483e18fadcb9f65e6d708c521afb 100644 --- a/crates/engine/src/shaders/keyers_gen.wgsl +++ b/crates/engine/src/shaders/keyers_gen.wgsl @@ -24,7 +24,7 @@ fn luma_key_op(@builtin(global_invocation_id) gid: vec3) { store_dst(vec2(gid.xy), out_col); } // ============================================================================ -// 8. FastNoise (3D Perlin FBM with Seethe, Angle, Discontinuous & gradient LUT) +// 8. FastNoise (3D simplex FBM with Seethe, Angle, Discontinuous & gradient LUT) // p[0] = (x_scale, y_scale, detail, contrast) // p[1] = (brightness, seethe, discontinuous, center_x) // p[2] = (center_y, angle_rad, inverted, type) with type 2 = Gradient, else Two Color @@ -49,59 +49,60 @@ var PERM: array = array( 222u, 114u, 67u, 29u, 24u, 72u, 243u, 141u, 128u, 195u, 78u, 66u, 215u, 61u, 156u, 180u ); -fn perm(i: u32) -> u32 { - return PERM[i & 255u]; -} +const GRAD16: array, 16> = array, 16>( + vec3(0.0, 1.0, 1.0), vec3(0.0, 1.0, -1.0), vec3(0.0, -1.0, 1.0), vec3(0.0, -1.0, -1.0), + vec3(1.0, 0.0, 1.0), vec3(1.0, 0.0, -1.0), vec3(-1.0, 0.0, 1.0), vec3(-1.0, 0.0, -1.0), + vec3(1.0, 1.0, 0.0), vec3(1.0, -1.0, 0.0), vec3(-1.0, 1.0, 0.0), vec3(-1.0, -1.0, 0.0), + vec3(1.0, 0.0, -1.0), vec3(-1.0, 0.0, -1.0), vec3(0.0, -1.0, 1.0), vec3(0.0, 1.0, 1.0), +); -fn grad3(h: u32, f: vec3) -> f32 { - let u_v = select(f.y, f.x, h < 8u); - let v_v = select(select(f.z, f.x, h == 12u || h == 14u), f.y, h < 4u); - return select(-u_v, u_v, (h & 1u) == 0u) + select(-v_v, v_v, (h & 2u) == 0u); +fn simplex_corner(ijk: vec3, d: vec3) -> f32 { + let t = SIMPLEX_RADIUS2 - dot(d, d); + if (t <= 0.0) { return 0.0; } + let c = ijk & vec3(255); + let h = PERM[(u32(c.z) + PERM[(u32(c.y) + PERM[u32(c.x)]) & 255u]) & 255u] & 15u; + let t2 = t * t; + return t2 * t2 * dot(GRAD16[h], d); } -// Improved-Perlin lattice noise (quintic fade, 16 edge gradients). -fn noise3d(p: vec3) -> f32 { - let fl = floor(p); - let f = p - fl; - let c = vec3(vec3(fl) & vec3(255)); - let u_f = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); - let a = perm(c.x) + c.y; - let b = perm(c.x + 1u) + c.y; - let aa = perm(a) + c.z; - let ab = perm(a + 1u) + c.z; - let ba = perm(b) + c.z; - let bb = perm(b + 1u) + c.z; - let n000 = grad3(perm(aa) & 15u, f); - let n100 = grad3(perm(ba) & 15u, f - vec3(1.0, 0.0, 0.0)); - let n010 = grad3(perm(ab) & 15u, f - vec3(0.0, 1.0, 0.0)); - let n110 = grad3(perm(bb) & 15u, f - vec3(1.0, 1.0, 0.0)); - let n001 = grad3(perm(aa + 1u) & 15u, f - vec3(0.0, 0.0, 1.0)); - let n101 = grad3(perm(ba + 1u) & 15u, f - vec3(1.0, 0.0, 1.0)); - let n011 = grad3(perm(ab + 1u) & 15u, f - vec3(0.0, 1.0, 1.0)); - let n111 = grad3(perm(bb + 1u) & 15u, f - vec3(1.0, 1.0, 1.0)); - let nxy0 = mix(mix(n000, n100, u_f.x), mix(n010, n110, u_f.x), u_f.y); - let nxy1 = mix(mix(n001, n101, u_f.x), mix(n011, n111, u_f.x), u_f.y); - return mix(nxy0, nxy1, u_f.z); +// Fusion's lattice: Gustavson-style 3D simplex noise with kernel (0.5196 - r^2)^4 and a 16-entry +// gradient table, recovered from Float32 probes (residual 2e-7). +fn simplex3d(p: vec3) -> f32 { + let cell = floor(p + (p.x + p.y + p.z) / 3.0); + let ijk = vec3(cell); + let d0 = p - (cell - (cell.x + cell.y + cell.z) / 6.0); + var a = vec3(0); + var b = vec3(0); + if (d0.x >= d0.y) { + if (d0.y >= d0.z) { a = vec3(1, 0, 0); b = vec3(1, 1, 0); } + else if (d0.x >= d0.z) { a = vec3(1, 0, 0); b = vec3(1, 0, 1); } + else { a = vec3(0, 0, 1); b = vec3(1, 0, 1); } + } else { + if (d0.y < d0.z) { a = vec3(0, 0, 1); b = vec3(0, 1, 1); } + else if (d0.x < d0.z) { a = vec3(0, 1, 0); b = vec3(0, 1, 1); } + else { a = vec3(0, 1, 0); b = vec3(1, 1, 0); } + } + let g = 1.0 / 6.0; + return SIMPLEX_GAIN * ( + simplex_corner(ijk, d0) + + simplex_corner(ijk + a, d0 - vec3(a) + g) + + simplex_corner(ijk + b, d0 - vec3(b) + 2.0 * g) + + simplex_corner(ijk + vec3(1), d0 - 1.0 + 3.0 * g) + ); } -// Measured against Fusion 21.1 float32 probes: one octave spans 0.5 +/- 0.2575 (std 0.104), -// later octaves are 2x frequency at half amplitude (own offsets) and sum unnormalized, -// fractional Detail weights the last octave, Discontinuous sums 2|n| per octave, Contrast -// pivots on 0.5 (on 0 when Discontinuous), Inverted runs before Brightness, Brightness adds -// half its value, and Gradient + Discontinuous shifts the lookup by 0.25. Fusion's lattice -// hash is not recovered: the noise only matches its distribution, and NOISE_FREQ 1.0 trades -// Fusion's ~1.8x finer spectrum for lower per-pixel MAE against its unmatched phases. -const NOISE_GAIN: f32 = 0.376; -const NOISE_FREQ: f32 = 1.0; -const NOISE_KNEE: f32 = 0.22; -const NOISE_SOFT: f32 = 0.0375; +// Octave i samples the lattice at 2^i * (x, y, seethe) + 50.3 * i with weight 0.5^i; a fractional +// Detail scales the last octave. Discontinuous sums 2|n| per octave, Contrast pivots on 0.5 (on 0 +// when Discontinuous), Inverted runs before Brightness, Brightness adds half its value, and +// Gradient + Discontinuous shifts the lookup by 0.25. +const SIMPLEX_RADIUS2: f32 = 0.5196152; +const SIMPLEX_GAIN: f32 = 16.642; +const OCTAVE_OFFSET: f32 = 50.3; @compute @workgroup_size(8, 8) fn fast_noise_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); if (gid.x >= dims.x || gid.y >= dims.y) { return; } - let aspect = f32(dims.x) / max(f32(dims.y), 1.0); - let fu = pix_to_fu(gid.xy, dims); let center = vec2(u.p[1].w, u.p[2].x); let scale = u.p[0].xy; let detail = clamp(u.p[0].z, 0.0, 10.0); @@ -112,14 +113,13 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3) { let angle = u.p[2].y; let gradient = u.p[2].w > 1.5; - // Frame-width isotropic units - var rel = (fu - center) * vec2(1.0, 1.0 / aspect); - if (abs(angle) > 1e-5) { - let ca = cos(angle); - let sa = sin(angle); - rel = vec2(rel.x * ca - rel.y * sa, rel.x * sa + rel.y * ca); - } - let p = rel * scale * NOISE_FREQ; + // Frame-width units, y up, measured from the pixel corner. + let w = f32(dims.x); + let h = f32(dims.y); + let rel = vec2(f32(gid.x) / w, (h - 1.0 - f32(gid.y)) / w) - center * vec2(1.0, h / w); + let ca = cos(angle); + let sa = sin(angle); + let p = vec2(rel.x * ca + rel.y * sa, rel.y * ca - rel.x * sa) * scale; let octaves = i32(ceil(detail)); var sum = 0.0; @@ -127,9 +127,8 @@ fn fast_noise_op(@builtin(global_invocation_id) gid: vec3) { var freq = 1.0; for (var i = 0; i < octaves; i = i + 1) { let weight = select(amp, amp * fract(detail), i == octaves - 1 && fract(detail) > 1e-4); - let raw = noise3d(vec3(p * freq + f32(i) * vec2(31.7, 17.3), seethe + 0.37 + f32(i) * 1.73)) * NOISE_GAIN; - let over = max(abs(raw) - NOISE_KNEE, 0.0); - let n = sign(raw) * (min(abs(raw), NOISE_KNEE) + NOISE_SOFT * tanh(over / NOISE_SOFT)); + let off = OCTAVE_OFFSET * f32(i); + let n = simplex3d(vec3(p * freq + off, seethe * freq + off)); sum = sum + weight * select(n, 2.0 * abs(n), disc); freq = freq * 2.0; amp = amp * 0.5; -- 2.54.1