ttypt/chaff_surf.h(3)chaff

NAME

ttypt/chaff_surf.h - Canonical Continuous Multi-Scale 2D Surface Field.

SYNOPSIS

#include <stdint.h>
#include <stddef.h>
#include <math.h>

Data Structures

struct chaff_surf_t

Flat, pointer-free multi-scale surface descriptor (maps directly to GLSL UBO).

struct chaff_eval_t

Continuous scalar value plus exact analytical world-space gradient.

Macros

#define CHAFF_SURF_MAX_OCTAVES 24u

Maximum number of octaves evaluated in a surface cascade.

#define CHAFF_SURF_SEED_PRIME 0x85EBCA77u

Multiplicative prime constant used to derive per-octave seeds.

#define CHAFF_SURF_WARP_Y_SALT 0x9E3779B9u

XOR salt used to derive the Y-channel domain warp seed.

#define CHAFF_SURF_FLAG_NO_WARP (1u << 1)

Surface cascade flag: disable domain warp even if warp_amp > 0.

Functions

static uint32_t chaff_hash2i (int32_t ix, int32_t iy, uint32_t seed)

Bit-exact 8-byte XXH32-style hash on full 32-bit integer lattice coordinates (ix, iy).

static float chaff_lowpass_f (float wavelength, float step_wu)

Continuous C2-quintic Nyquist low-pass transfer weight L(wavelength, step_wu) in [0, 1].

static float chaff_bandpass_f (float wavelength, float fine_step_wu, float coarse_step_wu)

Exact additive complementary Nyquist band-pass weight.

static uint32_t chaff_surf_axis_cells (uint32_t period_n, uint8_t period_base_shift, uint8_t shift)

Compute exact integer torus cell count for an octave of shift shift.

static int32_t chaff_wrap_cell_i32 (int32_t c, uint32_t n)

Wrap signed integer cell coordinate c into [0, n) (or return c if n == 0).

static int32_t chaff_fast_floor_i32 (float x)

Branch-minimal inline floor-to-int32 for bounded floats.

static void chaff_octave_phase (uint8_t shift, float *out_px, float *out_py)

Irrational-spaced fractional phase offset per octave in [0, 1).

static chaff_eval_t chaff_grad_octave_2d (int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, uint8_t shift, uint32_t nx, uint32_t ny, uint32_t seed)

Single-octave 2D C2-quintic periodic gradient noise with exact analytical world-space gradient.

static void chaff_surf_eval_warp (const chaff_surf_t *c, int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, float step_wu, float *out_wx, float *out_wy, float *out_dwx_dx, float *out_dwx_dy, float *out_dwy_dx, float *out_dwy_dy)

Evaluate the torus-periodic 2D domain warp vector w(p) and its 2x2 Jacobian J = [[1 + dwx/dx, dwx/dy], [dwy/dx, 1 + dwy/dy]].

static chaff_eval_t chaff_surf_eval_split (const chaff_surf_t *c, int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, float step_wu)

Evaluate the continuous multi-scale surface at split (int32 + float) world coordinates with Nyquist low-pass step step_wu (0.0f = unfiltered).

static chaff_eval_t chaff_surf_eval (const chaff_surf_t *c, double wx, double wy, float step_wu)

Evaluate the continuous multi-scale surface at double-precision world coordinates (wx, wy).

static chaff_eval_t chaff_surf_eval_i32 (const chaff_surf_t *c, int32_t wx, int32_t wy, float step_wu)

Evaluate the continuous multi-scale surface at integer world coordinates (wx, wy).

static chaff_eval_t chaff_surf_eval_band_split (const chaff_surf_t *c, int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, float fine_step_wu, float coarse_step_wu, float seed_gx, float seed_gy)

Complementary sub-texel band-pass evaluator matching GLSL chaff_eval_surf_band.

static chaff_eval_t chaff_surf_eval_band (const chaff_surf_t *c, double wx, double wy, float fine_step_wu, float coarse_step_wu, float seed_gx, float seed_gy)

Complementary sub-texel band-pass evaluator at double-precision world coordinates (wx, wy).

static void chaff_surf_eval_i32_4x (const chaff_surf_t *c, const int32_t wx[4], const int32_t wy[4], float step_wu, chaff_eval_t out[4])

4-wide batch evaluator bit-exact with 4 calls to chaff_surf_eval_i32().

static void chaff_surf_eval_row_i32 (const chaff_surf_t *c, int32_t wy, int32_t ox, int32_t span_w, uint32_t count, float step_wu, chaff_eval_t *out_row)

Scanline row evaluator over a uniform X span [ox, ox + span_w] at constant wy.

static void chaff_surf_bounds (const chaff_surf_t *c, float step_wu, float *out_min, float *out_max)

O(1) strictly conservative value envelope [out_min, out_max] derived directly from the surface descriptor.

Detailed Description

Canonical Continuous Multi-Scale 2D Surface Field.

LICENSE: BSD-2-Clause Copyright (c) 2026, tty-pt

Single source of truth for continuous multi-scale procedural surfaces in libchaff:

1.
2D C2-quintic gradient noise with exact analytical (val, dx, dy) and 64-direction uniform isotropic unit gradients (zero value-noise pillowing).
2.
Exact integer torus periodicity: world_span = period_n * 2^period_base_shift, with zero seam across wrap boundaries and irrational per-octave phase offsets.
3.
Torus-periodic domain warp with exact 2x2 Jacobian chain-rule gradients.
4.
Smooth C2 ridged multifractal blending (1 - sqrt(n^2 + eps^2)) and scale-invariant slope-damped erosion (1 / (1 + k * |grad|^2)).
5.
Exact additive Nyquist partition of unity: L(lambda, coarse) + max(0, L(lambda, fine) - L(lambda, coarse)) == L(lambda, fine)
6.
One flat data descriptor (chaff_surf_t) drives:
•
scalar eval (chaff_surf_eval, chaff_surf_eval_i32, chaff_surf_eval_band)
•
4-wide batch (chaff_surf_eval_i32_4x)
•
scanline row eval (chaff_surf_eval_row_i32)
•
exact O(1) conservative bounds (chaff_surf_bounds)
•
GPU GLSL ES 3.0 UBO evaluator (CHAFF_GLSL_SURF_FN in chaff_glsl.h)

Pure C99, zero heap allocations, zero domain/game constants.

Function Documentation

float chaff_bandpass_f (float wavelength, float fine_step_wu, float coarse_step_wu) [inline], [static]

Exact additive complementary Nyquist band-pass weight. B(lambda; fine_step, coarse_step) = max(0, L(lambda, fine_step) - L(lambda, coarse_step)). Satisfies exact partition of unity for ALL fine_step <= coarse_step and ALL wavelengths: L(lambda, coarse_step) + B(lambda; fine_step, coarse_step) == L(lambda, fine_step)

int32_t chaff_fast_floor_i32 (float x) [inline], [static]

Branch-minimal inline floor-to-int32 for bounded floats. Avoids libm floorf@PLT register spills inside the octave loop on baseline x86_64 SSE2 targets.

chaff_eval_t chaff_grad_octave_2d (int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, uint8_t shift, uint32_t nx, uint32_t ny, uint32_t seed) [inline], [static]

Single-octave 2D C2-quintic periodic gradient noise with exact analytical world-space gradient. Input coordinate is split as exact integer world units (wx_i, wy_i) + float offset (wx_f, wy_f) so precision remains full 24-bit intra-cell even at Earth-scale coordinates (~10^9 wu). Output value is strictly in [-1.0, +1.0]; (dx, dy) is in value-per-world-unit.

uint32_t chaff_hash2i (int32_t ix, int32_t iy, uint32_t seed) [inline], [static]

Bit-exact 8-byte XXH32-style hash on full 32-bit integer lattice coordinates (ix, iy). Zero 16-bit wrap collision even on Earth-scale (b=23) planets. Matches GLSL chaff_hash2i.

float chaff_lowpass_f (float wavelength, float step_wu) [inline], [static]

Continuous C2-quintic Nyquist low-pass transfer weight L(wavelength, step_wu) in [0, 1].

void chaff_octave_phase (uint8_t shift, float * out_px, float * out_py) [inline], [static]

Irrational-spaced fractional phase offset per octave in [0, 1). Prevents octave lattice lines from aligning across scales while preserving exact torus wrap.

uint32_t chaff_surf_axis_cells (uint32_t period_n, uint8_t period_base_shift, uint8_t shift) [inline], [static]

Compute exact integer torus cell count for an octave of shift shift. Returns 0 for an axis if aperiodic or if cell count overflows int32.

void chaff_surf_bounds (const chaff_surf_t * c, float step_wu, float * out_min, float * out_max) [inline], [static]

O(1) strictly conservative value envelope [out_min, out_max] derived directly from the surface descriptor. Guaranteed to contain chaff_surf_eval() for all (wx, wy).

chaff_eval_t chaff_surf_eval_band_split (const chaff_surf_t * c, int32_t wx_i, float wx_f, int32_t wy_i, float wy_f, float fine_step_wu, float coarse_step_wu, float seed_gx, float seed_gy) [inline], [static]

Complementary sub-texel band-pass evaluator matching GLSL chaff_eval_surf_band. Evaluates strictly the octaves resolved between fine_step_wu and coarse_step_wu using additive partition-of-unity weight max(0, L(lambda, fine) - L(lambda, coarse)).

Author

Generated automatically by Doxygen for chaff from the source code.