diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs index 5da2035ac..7f0f9cf51 100644 --- a/crates/effects/src/lib.rs +++ b/crates/effects/src/lib.rs @@ -112,7 +112,7 @@ pub use transition::{GridWipe, place_layer}; // effectcraft-gpu fx_noise. pub use keying2::{InnerOuterPlan, inner_outer_plan}; pub use noise::remove_grain_level; -pub use noise3::{FractalGpu, NOISE_CELL, fractal_gpu}; +pub use noise3::{FractalGpu, NOISE_CELL, OCTAVE_SCALE_MAX, fractal_gpu}; pub use stylize3::{BumpLight, HexTile, bump_layer_id}; pub use textfx::{TextLayer, text_layer}; pub use time_fx::{PixelMotion, TimewarpPlan, TwStep, pmb_plan, posterized_time, time_frames, timewarp_crop, timewarp_plan}; diff --git a/crates/effects/src/noise3.rs b/crates/effects/src/noise3.rs index 0533db03b..ae76d756a 100644 --- a/crates/effects/src/noise3.rs +++ b/crates/effects/src/noise3.rs @@ -65,6 +65,27 @@ pub const NOISE_SOFT_LINEAR: u32 = 2; /// correlation length), not 100 px. pub const NOISE_CELL: f64 = 2.0 / 3.0; +/// Bound on an octave's frequency and on its Perspective Offset depth (and their inverses). +/// Unbounded, at Sub Scaling 10000 and Complexity 20 they would reach 100^±19, and Perspective +/// Offset or Turbulence Factor multiplying the offset by the depth overflowed to infinity (NaN +/// noise, #3486). Sub Scaling 10..100 never reaches the bound (10^19 at most). Both stay each +/// other's inverse inside it, so an octave's offset in lattice cells is unchanged; an octave this +/// far out is a constant (or sub-pixel noise) across any layer either way. +pub const OCTAVE_SCALE_MAX: f32 = 1e20; + +/// Bound on an octave's scaled Offset Turbulence in pixels, so a huge offset times the depth stays +/// finite. Times a frequency of at least 1 / [`OCTAVE_SCALE_MAX`], over cells of at most about +/// 6700 px (Scale 10000 %), it is still over 10^12 lattice cells, past where the lattice index +/// saturates (2^31), so the output is the same. +const OCTAVE_OFFSET_MAX: f32 = 1e36; + +/// The next octave's (frequency, depth) from this one's, for a Sub Scaling ratio `s`. +#[inline] +fn next_octave(f: f32, depth: f32, s: f32) -> (f32, f32) { + let lo = 1.0 / OCTAVE_SCALE_MAX; + ((f / s).clamp(lo, OCTAVE_SCALE_MAX), (depth * s).clamp(lo, OCTAVE_SCALE_MAX)) +} + fn spec(id: &'static str, name: &'static str, params: Vec, render: crate::RenderFn) -> EffectSpec { EffectSpec { id, name, category: "Noise & Grain", params, render, gpu: false, float: true } } @@ -275,6 +296,23 @@ impl Fractal { } } + /// Octave `of`'s origin in pixels, at Perspective Offset `depth`: Perspective Offset moves finer + /// (farther) layers less; turbulence makes them drift faster. The scaled offset stays within + /// ±[`OCTAVE_OFFSET_MAX`] (#3486). + #[inline] + fn octave_origin(&self, of: f32, depth: f32) -> [f32; 2] { + let k = if self.perspective { depth } else { 1.0 } * (1.0 + self.turbulence * of); + let mut org = self.origin; + if k != 1.0 { + let d = |i: usize| ((self.origin[i] - self.center[i]) * k).clamp(-OCTAVE_OFFSET_MAX, OCTAVE_OFFSET_MAX); + org = [self.center[0] + d(0), self.center[1] + d(1)]; + } + if !self.center_subscale { + org = [org[0] + self.sub_offset[0] * of, org[1] + self.sub_offset[1] * of]; + } + org + } + /// Raw fractal value (before contrast / overflow) at pixel centre (x, y). pub(crate) fn raw(&self, x: f32, y: f32) -> f32 { let n_oct = self.octaves.ceil() as usize; @@ -285,16 +323,7 @@ impl Fractal { for o in 0..n_oct { let w = if o + 1 == n_oct && frac > 0.0 { frac } else { 1.0 }; let of = o as f32; - // This layer's origin: Perspective Offset moves finer (farther) layers less; - // turbulence makes them drift faster. - let k = if self.perspective { depth } else { 1.0 } * (1.0 + self.turbulence * of); - let mut org = self.origin; - if k != 1.0 { - org = [self.center[0] + (self.origin[0] - self.center[0]) * k, self.center[1] + (self.origin[1] - self.center[1]) * k]; - } - if !self.center_subscale { - org = [org[0] + self.sub_offset[0] * of, org[1] + self.sub_offset[1] * of]; - } + let org = self.octave_origin(of, depth); let (dx, dy) = (x - org[0], y - org[1]); let (mut u, mut v) = ((dx * cr + dy * sr) / self.size[0], (-dx * sr + dy * cr) / self.size[1]); if self.sub_rotation != 0.0 { @@ -324,8 +353,7 @@ impl Fractal { sum += n * amp * w; norm += amp * w; amp *= self.influence; - f /= self.sub_scaling; - depth *= self.sub_scaling; + (f, depth) = next_octave(f, depth, self.sub_scaling); } if self.kind == 7 { best } else { sum / norm.max(1e-6) } } @@ -377,22 +405,14 @@ pub fn fractal_gpu(ctx: &EffectCtx, offset: [f64; 2], scale: f64, turbulent: boo for o in 0..n_oct { let w = if o + 1 == n_oct && frac > 0.0 { frac } else { 1.0 }; let of = o as f32; - let k = if fr.perspective { depth } else { 1.0 } * (1.0 + fr.turbulence * of); - let mut org = fr.origin; - if k != 1.0 { - org = [fr.center[0] + (fr.origin[0] - fr.center[0]) * k, fr.center[1] + (fr.origin[1] - fr.center[1]) * k]; - } - if !fr.center_subscale { - org = [org[0] + fr.sub_offset[0] * of, org[1] + fr.sub_offset[1] * of]; - } + let org = fr.octave_origin(of, depth); let (s, c) = (fr.sub_rotation * of).sin_cos(); let evo = fr.evolution * (1.0 + fr.turbulence * of); let dk = if fr.kind == 6 { 0.5 + of * 0.35 } else { 1.0 }; octaves.push([org[0], org[1], s, c, f, amp, w, evo, of * 7.31, dk]); norm += amp * w; amp *= fr.influence; - f /= fr.sub_scaling; - depth *= fr.sub_scaling; + (f, depth) = next_octave(f, depth, fr.sub_scaling); } let cycle = fr.cycle.map(|c| { let t = fr.evolution.rem_euclid(c); @@ -496,6 +516,24 @@ mod tests { } } + /// #3486: octave frequency and depth stay each other's inverse and finite at 20 octaves of + /// either end of Sub Scaling, and are unchanged inside the old 10..100 range. + #[test] + fn octave_scales_stay_finite_and_inverse() { + for s in [0.1f32, 0.56, 1.0, 2.0, 100.0] { + let (mut f, mut depth, (mut f0, mut d0)) = (1.0f32, 1.0f32, (1.0f32, 1.0f32)); + for _ in 0..19 { + (f, depth) = next_octave(f, depth, s); + (f0, d0) = (f0 / s, d0 * s); + assert!(f.is_finite() && depth.is_finite() && f > 0.0 && depth > 0.0, "{s}: {f} {depth}"); + assert!((f * depth - 1.0).abs() < 1e-3, "{s}: {f} × {depth}"); + if s <= 1.0 { + assert_eq!((f, depth), (f0, d0), "{s}"); + } + } + } + } + /// Evolution moves the noise at a constant rate: no ease in or out within a revolution. #[test] fn evolution_is_linear() { diff --git a/crates/gpu/src/effects.rs b/crates/gpu/src/effects.rs index fd5a97c38..5f204d1c4 100644 --- a/crates/gpu/src/effects.rs +++ b/crates/gpu/src/effects.rs @@ -608,7 +608,7 @@ fn pointwise(e: &mut Enc, id: &str, ctx: &EffectCtx, b: GBuf) -> Option { (f("opacity") as f32 / 100.0).clamp(0.0, 1.0), (sh * effectcraft_effects::NOISE_CELL * b.scale).max(1.0) as f32, ]; - p.f[3] = [(f("subSettings/subInfluence") as f32 / 100.0).clamp(0.0, 1.0), 1.0 / sub as f32, 0.0, 0.0]; + p.f[3] = [(f("subSettings/subInfluence") as f32 / 100.0).clamp(0.0, 1.0), 1.0 / sub as f32, effectcraft_effects::OCTAVE_SCALE_MAX, 0.0]; } _ => return None, } diff --git a/crates/gpu/src/shaders/kernels.wgsl b/crates/gpu/src/shaders/kernels.wgsl index a123b70ca..e038a9ee8 100644 --- a/crates/gpu/src/shaders/kernels.wgsl +++ b/crates/gpu/src/shaders/kernels.wgsl @@ -618,7 +618,8 @@ fn straight(px: vec4) -> vec3 { // 9 Gradient Ramp f[0] = (sx, sy, ex, ey); f[1] = start colour; f[2] = end colour; // f[3] = (len2, scatter, blend); u[0] = (_, radial, seed) // 10 Fractal Noise f[0] = (contrast, brightness, scale x, frac); f[1] = (ox, oy, sr, cr); -// f[2] = (evo, blend, opacity, scale y); f[3] = (sub influence, 1 / sub scaling); +// f[2] = (evo, blend, opacity, scale y); f[3] = (sub influence, 1 / sub scaling, +// OCTAVE_SCALE_MAX); // u[0] = (_, kind, n_oct, seed); u[1] = (invert, overflow, normal blend) @compute @workgroup_size(16, 16) fn pointwise(@builtin(global_invocation_id) gid: vec3) { @@ -856,6 +857,7 @@ fn pointwise(@builtin(global_invocation_id) gid: vec3) { let scale_y = P.f[2].w; let infl = P.f[3].x; let fmul = P.f[3].y; + let fmax = P.f[3].z; let frac = P.f[0].w; let ox = P.f[1].x; let oy = P.f[1].y; @@ -888,7 +890,8 @@ fn pointwise(@builtin(global_invocation_id) gid: vec3) { sum += n * amp * w; norm += amp * w; amp *= infl; - f *= fmul; + // noise3::next_octave: the frequency stays within OCTAVE_SCALE_MAX (#3486). + f = clamp(f * fmul, 1.0 / fmax, fmax); } var val = sum / max(norm, 1e-6); val = (val - 0.5) * contrast + 0.5 + brightness;