From cd807732799904f6731f460cc0469143c410c2c9 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 8 Feb 2026 19:09:05 +0100 Subject: feat(gpu): Add WGSL noise and hash function library (Task #59) Implements comprehensive RNG and noise functions for procedural shader effects: Hash Functions: - hash_1f, hash_2f, hash_3f (float-based, fast) - hash_2f_2f, hash_3f_3f (vector output) - hash_1u, hash_1u_2f, hash_1u_3f (integer-based, high quality) Noise Functions: - noise_2d, noise_3d (value noise with smoothstep) - fbm_2d, fbm_3d (fractional Brownian motion) - gyroid (periodic minimal surface) Integration: - Added to ShaderComposer as "math/noise" snippet - Available via #include "math/noise" in WGSL shaders - Test suite validates all 11 functions compile Testing: - test_noise_functions.cc validates shader loading - All 33 tests pass (100%) Size Impact: ~200-400 bytes per function used (dead-code eliminated) Files: - assets/final/shaders/math/noise.wgsl (new, 4.2KB, 150 lines) - assets/final/demo_assets.txt (added SHADER_MATH_NOISE) - assets/final/test_assets_list.txt (added SHADER_MATH_NOISE) - src/gpu/effects/shaders.cc (registered snippet) - src/tests/test_noise_functions.cc (new test) - CMakeLists.txt (added test target) Co-Authored-By: Claude Sonnet 4.5 --- assets/final/shaders/math/noise.wgsl | 147 +++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 assets/final/shaders/math/noise.wgsl (limited to 'assets/final/shaders/math/noise.wgsl') diff --git a/assets/final/shaders/math/noise.wgsl b/assets/final/shaders/math/noise.wgsl new file mode 100644 index 0000000..9f99e4a --- /dev/null +++ b/assets/final/shaders/math/noise.wgsl @@ -0,0 +1,147 @@ +// Random number generation and noise functions for WGSL shaders. +// Collection of hash functions and noise generators. + +// ============================================ +// Hash Functions (Float Input) +// ============================================ + +// Hash: f32 -> f32 +// Fast fractional hash for floats +fn hash_1f(x: f32) -> f32 { + var v = fract(x * 0.3351); + v *= v + 33.33; + v *= v + v; + return fract(v); +} + +// Hash: vec2 -> f32 +// 2D coordinate to single hash value +fn hash_2f(p: vec2) -> f32 { + var h = dot(p, vec2(127.1, 311.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec2 -> vec2 +// 2D coordinate to 2D hash (from Shadertoy 4djSRW) +fn hash_2f_2f(p: vec2) -> vec2 { + var p3 = fract(vec3(p.x, p.y, p.x) * vec3(0.1021, 0.1013, 0.0977)); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.xx + p3.yz) * p3.zy); +} + +// Hash: vec3 -> f32 +// 3D coordinate to single hash value +fn hash_3f(p: vec3) -> f32 { + var h = dot(p, vec3(127.1, 311.7, 74.7)); + return fract(sin(h) * 43758.5453123); +} + +// Hash: vec3 -> vec3 +// 3D coordinate to 3D hash +fn hash_3f_3f(p: vec3) -> vec3 { + var v = fract(p); + v += dot(v, v.yxz + 32.41); + return fract((v.xxy + v.yzz) * v.zyx); +} + +// ============================================ +// Hash Functions (Integer Input) +// ============================================ + +// Hash: u32 -> f32 +// Integer hash with bit operations (high quality) +fn hash_1u(p: u32) -> f32 { + var P = (p << 13u) ^ p; + P = P * (P * P * 15731u + 789221u) + 1376312589u; + return bitcast((P >> 9u) | 0x3f800000u) - 1.0; +} + +// Hash: u32 -> vec2 +fn hash_1u_2f(p: u32) -> vec2 { + return vec2(hash_1u(p), hash_1u(p + 1423u)); +} + +// Hash: u32 -> vec3 +fn hash_1u_3f(p: u32) -> vec3 { + return vec3(hash_1u(p), hash_1u(p + 1423u), hash_1u(p + 124453u)); +} + +// ============================================ +// Noise Functions +// ============================================ + +// Value Noise: 2D +// Interpolated grid noise using smoothstep +fn noise_2d(p: vec2) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n0 = hash_2f(i + vec2(0.0, 0.0)); + let n1 = hash_2f(i + vec2(1.0, 0.0)); + let n2 = hash_2f(i + vec2(0.0, 1.0)); + let n3 = hash_2f(i + vec2(1.0, 1.0)); + let ix0 = mix(n0, n1, u.x); + let ix1 = mix(n2, n3, u.x); + return mix(ix0, ix1, u.y); +} + +// Value Noise: 3D +fn noise_3d(p: vec3) -> f32 { + let i = floor(p); + let f = fract(p); + let u = f * f * (3.0 - 2.0 * f); + let n000 = hash_3f(i + vec3(0.0, 0.0, 0.0)); + let n100 = hash_3f(i + vec3(1.0, 0.0, 0.0)); + let n010 = hash_3f(i + vec3(0.0, 1.0, 0.0)); + let n110 = hash_3f(i + vec3(1.0, 1.0, 0.0)); + let n001 = hash_3f(i + vec3(0.0, 0.0, 1.0)); + let n101 = hash_3f(i + vec3(1.0, 0.0, 1.0)); + let n011 = hash_3f(i + vec3(0.0, 1.0, 1.0)); + let n111 = hash_3f(i + vec3(1.0, 1.0, 1.0)); + let ix00 = mix(n000, n100, u.x); + let ix10 = mix(n010, n110, u.x); + let ix01 = mix(n001, n101, u.x); + let ix11 = mix(n011, n111, u.x); + let iy0 = mix(ix00, ix10, u.y); + let iy1 = mix(ix01, ix11, u.y); + return mix(iy0, iy1, u.z); +} + +// ============================================ +// Special Functions +// ============================================ + +// Gyroid function (periodic triply-orthogonal minimal surface) +// Useful for procedural patterns and cellular structures +fn gyroid(p: vec3) -> f32 { + return abs(0.04 + dot(sin(p), cos(p.zxy))); +} + +// Fractional Brownian Motion (FBM) 2D +// Multi-octave noise for natural-looking variation +fn fbm_2d(p: vec2, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_2d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} + +// Fractional Brownian Motion (FBM) 3D +fn fbm_3d(p: vec3, octaves: i32) -> f32 { + var value = 0.0; + var amplitude = 0.5; + var frequency = 1.0; + var pos = p; + for (var i = 0; i < octaves; i++) { + value += amplitude * noise_3d(pos * frequency); + frequency *= 2.0; + amplitude *= 0.5; + } + return value; +} -- cgit v1.2.3