2026-05-08 01:49:44 +01:00
|
|
|
|
# Design
|
|
|
|
|
|
|
2026-06-03 02:58:56 +01:00
|
|
|
|
CultMath exists to make renderer math readable in every place it needs to live:
|
|
|
|
|
|
Rust hot kernels, C# host code, and HLSL shader code.
|
2026-05-08 01:49:44 +01:00
|
|
|
|
|
2026-09-14 13:30:00 +02:00
|
|
|
|
## Parity Target: HLSL
|
|
|
|
|
|
|
|
|
|
|
|
HLSL is the semantic authority for every type and intrinsic HLSL defines. The
|
|
|
|
|
|
goal is source compatibility: HLSL-shaped math compiles as C# under
|
|
|
|
|
|
`using static CultMath.math;`. C# forces float literal suffixes (`0.5f`); the
|
|
|
|
|
|
rest of the spelling should survive the move.
|
|
|
|
|
|
|
|
|
|
|
|
That makes these HLSL rules, not options:
|
|
|
|
|
|
|
2026-09-14 13:57:03 +02:00
|
|
|
|
- Every swizzle exists. `float2/3/4`, `int2/3/4`, and `bool2/3/4` expose every
|
|
|
|
|
|
read swizzle of length 2 to 4, repeats included (`v.wwxy`), in both `xyzw` and
|
|
|
|
|
|
`rgba` naming (never mixed), plus single-component `r/g/b/a`. `double2/3` get
|
|
|
|
|
|
lengths 2 and 3 only, because there is no `double4`. Components and swizzles are
|
|
|
|
|
|
writable: `v.xz = f2;`, `c.rgb *= 0.5f;`, and `v[1] = s;` work. Setters exist
|
|
|
|
|
|
only for non-repeating masks, because HLSL rejects `v.xx = ...`.
|
|
|
|
|
|
- Every HLSL mixed constructor exists, as a struct constructor and as a `math`
|
|
|
|
|
|
function: `float4(float2, float2)`, `float4(float, float2, float)`,
|
|
|
|
|
|
`float3(float, float2)`, the copy form `float3(float3)`, and the rest, for the
|
|
|
|
|
|
float, int, and bool families (double gets constructors only; a `math.double3`
|
|
|
|
|
|
function would hide the `double3` type name).
|
|
|
|
|
|
- Swizzles and mixed constructors are generated. `tools/generate-swizzles.ps1`
|
|
|
|
|
|
writes the checked-in `src/CultMath/Swizzles.g.cs` (partial structs plus a
|
|
|
|
|
|
partial `math`). Edit the generator, rerun it, and commit both; never edit the
|
|
|
|
|
|
output by hand.
|
2026-09-14 13:30:00 +02:00
|
|
|
|
- Comparison operators (`<`, `>`, `<=`, `>=`, `==`, `!=`) are component-wise and
|
|
|
|
|
|
return `bool2`/`bool3`/`bool4`. Reduce them with `any` and `all`.
|
|
|
|
|
|
`Equals`/`GetHashCode` stay value-based so vectors still work as dictionary
|
|
|
|
|
|
keys.
|
|
|
|
|
|
- `select(condition, whenTrue, whenFalse)` uses HLSL's argument order.
|
2026-09-14 13:57:03 +02:00
|
|
|
|
- Matrices are row-major: constructors take rows, `m[i]` returns a reference to
|
|
|
|
|
|
row i (so `m[1][2] = s;` writes one element), `_mRC` is row R column C, and
|
|
|
|
|
|
products go through `mul` (`mul(m, v)` treats `v` as a column, `mul(v, m)` as a
|
|
|
|
|
|
row). `*` on matrices would be component-wise in HLSL, so CultMath does not
|
|
|
|
|
|
define it.
|
Document where matrix row writes are silently lost
The operator kept the [UnscopedRef] ref this[int] row indexer as is. design.md
and the float2x2/float3x3 indexers now say where m[r][c] = v lands: locals,
array elements, class fields, and ref locals keep the write; a readonly field,
in parameter, property (auto-properties too), or static readonly identity
takes a defensive copy, so the write compiles and is lost. Fix: copy to a
local, write, assign back. Verified by compiling and running each case with
Roslyn against CultMath (no diagnostics; writes land 5, lost 0).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 14:18:00 +02:00
|
|
|
|
- `m[r][c] = v` writes through locals, array elements, fields of classes, and
|
|
|
|
|
|
ref locals. Through a readonly field, an `in` parameter, a property
|
|
|
|
|
|
(auto-properties included), or a static readonly value such as
|
|
|
|
|
|
`float3x3.identity`, C# takes a defensive copy: the write compiles and is
|
|
|
|
|
|
silently lost. Copy the matrix to a local, write to it, and assign it back.
|
2026-09-14 13:30:00 +02:00
|
|
|
|
- Scalar/vector arithmetic and comparisons have explicit overloads on both
|
|
|
|
|
|
sides rather than leaning on implicit scalar splat.
|
2026-09-14 13:57:03 +02:00
|
|
|
|
- Intrinsics return HLSL's types and edge cases: `sign` returns `int`/`intN`
|
2026-09-14 14:09:49 +02:00
|
|
|
|
(NaN gives 0); `step(y, x)` is `x < y ? 0 : 1` (NaN gives 1), which is how dxc
|
2026-09-14 14:47:10 +02:00
|
|
|
|
lowers it, in C# and in the HLSL mirror; float and double `min` and `max`, and
|
|
|
|
|
|
float `clamp` and `saturate`, follow the DXIL `FMin`/`FMax`/`Saturate` operations dxc emits for
|
Match float min, max, clamp, and saturate to DXIL FMin/FMax/Saturate
dxc lowers float min/max/clamp to dx.op FMin/FMax (36/35) and saturate to
dx.op Saturate (7). DXIL.rst defines them: FMin(a, b) is a < b ? a : b,
FMax(a, b) is a >= b ? a : b, a NaN operand returns the other, and
Saturate is FMin(1, FMax(0, x)). C# used MathF.Min/Max, which pass NaN
through and order signed zeros, so saturate(NaN) was NaN instead of 0.
Every float overload now follows the spec, normalize uses the same max as
its mirror, and the mirror's saturate spells Saturate's operand order
(which maps -0 to +0, where clamp(x, 0, 1) keeps -0). Non-NaN results
change only in the sign of a zero. design.md records the rules and that
dxc marks float math fast, so GPU NaN results are not guaranteed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 14:32:19 +02:00
|
|
|
|
them: `min(a, b)` is `a < b ? a : b`, `max(a, b)` is `a >= b ? a : b`, a NaN
|
|
|
|
|
|
operand returns the other (so `saturate(NaN)` is 0), `clamp(x, a, b)` is
|
|
|
|
|
|
`min(max(x, a), b)`, and `saturate(x)` is `min(1, max(0, x))`, which maps -0
|
|
|
|
|
|
to +0; `any`/`all` accept numeric vectors (a component is true when it is
|
Add HLSL vector conversions, int clamp, and double frac to CultMath
Aetheria Cut 8a needs int2(float2), clamp(int, int, int), and
frac(double). Fill them in CultMath as dxc compiles them:
- generate-swizzles.ps1 emits same-size conversion constructors and
math functions between float, int, and bool vectors. dxc lowers
float to int as fptosi (toward zero; NaN and out-of-range undefined),
numeric to bool as != 0 (fcmp une, NaN is true), bool to numeric as
0 or 1. floatN(int) splat keeps floatN(0) from going ambiguous
against floatN(intN).
- Int and int-vector clamp are min(max(x, a), b), matching dxc's
IMin(IMax(x, a), b); inverted bounds return b.
- frac(double) keeps double precision. dxc demotes double frac to
float, as it does exp and lerp; CultMath's existing double overloads
already diverge that way, and design.md now says so.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 16:16:32 +02:00
|
|
|
|
not 0); `min`, `max`, and `abs` on int vectors return int vectors; int
|
|
|
|
|
|
`clamp` is `IMin(IMax(x, a), b)`, so inverted bounds return `b`.
|
|
|
|
|
|
- Same-size vector conversions (`intN(floatN)`, `floatN(intN)`, `boolN(floatN)`,
|
|
|
|
|
|
`boolN(intN)`, `floatN(boolN)`, `intN(boolN)`) exist as constructors and
|
|
|
|
|
|
`math` functions and follow dxc: float to int is `fptosi`, truncating toward
|
|
|
|
|
|
zero; numeric to bool is `!= 0` (`fcmp une`, so NaN is true); bool to numeric
|
|
|
|
|
|
is 0 or 1. DXIL leaves float-to-int undefined for NaN and out-of-range values,
|
|
|
|
|
|
and so does C#; CultMath does not pin them.
|
2026-09-14 18:04:04 +02:00
|
|
|
|
- `normalize(x)` is `x * Rsqrt(Dot(x, x))`, which is how dxc lowers it, and
|
|
|
|
|
|
DXIL.rst defines `Rsqrt` as `1 / sqrt(src)`. A zero vector therefore
|
|
|
|
|
|
normalizes to NaN (`0 * inf`), and a NaN component makes every component NaN.
|
|
|
|
|
|
(The special-value table under `Rsqrt` in DXIL.rst is a copy of the `Round`
|
|
|
|
|
|
table and contradicts that definition, so CultMath follows the definition.)
|
|
|
|
|
|
`length` and `distance` are `Sqrt` of the summed squares, so a zero vector
|
|
|
|
|
|
gives 0. Callers that can meet zero-length vectors guard them before calling
|
|
|
|
|
|
`normalize`.
|
Add HLSL vector conversions, int clamp, and double frac to CultMath
Aetheria Cut 8a needs int2(float2), clamp(int, int, int), and
frac(double). Fill them in CultMath as dxc compiles them:
- generate-swizzles.ps1 emits same-size conversion constructors and
math functions between float, int, and bool vectors. dxc lowers
float to int as fptosi (toward zero; NaN and out-of-range undefined),
numeric to bool as != 0 (fcmp une, NaN is true), bool to numeric as
0 or 1. floatN(int) splat keeps floatN(0) from going ambiguous
against floatN(intN).
- Int and int-vector clamp are min(max(x, a), b), matching dxc's
IMin(IMax(x, a), b); inverted bounds return b.
- frac(double) keeps double precision. dxc demotes double frac to
float, as it does exp and lerp; CultMath's existing double overloads
already diverge that way, and design.md now says so.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 16:16:32 +02:00
|
|
|
|
- dxc demotes double `frac`, `exp`, `lerp`, and `floor` to float. CultMath's
|
|
|
|
|
|
double overloads keep double precision instead: `frac(double)` is
|
|
|
|
|
|
`x - floor(x)` in double. That is a deliberate divergence for CPU simulation
|
|
|
|
|
|
time, which shaders do not carry.
|
2026-09-14 13:57:03 +02:00
|
|
|
|
|
Match float min, max, clamp, and saturate to DXIL FMin/FMax/Saturate
dxc lowers float min/max/clamp to dx.op FMin/FMax (36/35) and saturate to
dx.op Saturate (7). DXIL.rst defines them: FMin(a, b) is a < b ? a : b,
FMax(a, b) is a >= b ? a : b, a NaN operand returns the other, and
Saturate is FMin(1, FMax(0, x)). C# used MathF.Min/Max, which pass NaN
through and order signed zeros, so saturate(NaN) was NaN instead of 0.
Every float overload now follows the spec, normalize uses the same max as
its mirror, and the mirror's saturate spells Saturate's operand order
(which maps -0 to +0, where clamp(x, 0, 1) keeps -0). Non-NaN results
change only in the sign of a zero. design.md records the rules and that
dxc marks float math fast, so GPU NaN results are not guaranteed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 14:32:19 +02:00
|
|
|
|
dxc marks float arithmetic and compares `fast` (no NaNs) unless a value is
|
|
|
|
|
|
`precise`, so drivers may optimize NaN handling away and NaN results on real
|
|
|
|
|
|
GPUs are not guaranteed. CultMath defines the CPU result by dxc's lowering and
|
|
|
|
|
|
the DXIL operation spec (DirectXShaderCompiler `docs/DXIL.rst`).
|
|
|
|
|
|
|
2026-09-14 14:47:10 +02:00
|
|
|
|
NaN does not reliably propagate through CultMath, just as it does not on the
|
|
|
|
|
|
GPU. Because `min`, `max`, and `saturate` return the non-NaN operand, some
|
|
|
|
|
|
functions turn NaN or infinity into numbers: `smoothstep(a, a, a)` is 0;
|
|
|
|
|
|
infinite inputs to `smoothstep` and the intercept helpers give numbers; the
|
|
|
|
|
|
bezier, `smoothstep01`, and `smootherstep` functions return finite values for
|
2026-09-14 18:04:04 +02:00
|
|
|
|
NaN input. Callers that must detect bad data should check `isnan` before calling these
|
2026-09-14 14:47:10 +02:00
|
|
|
|
functions.
|
|
|
|
|
|
|
2026-09-14 13:57:03 +02:00
|
|
|
|
Deferred until a consumer needs them: `float4x4`, `transpose`, `determinant`,
|
|
|
|
|
|
and HLSL's one-based `_11`..`_33` element names.
|
|
|
|
|
|
|
|
|
|
|
|
### HLSL Target: Source Transformations
|
|
|
|
|
|
|
|
|
|
|
|
`HlslSourceCompatibilityTests` compiles the real `shaders/CultMath.hlsl` with
|
|
|
|
|
|
Roslyn against CultMath. These transformations are the complete list; if a body
|
|
|
|
|
|
needs anything else, the test fails:
|
|
|
|
|
|
|
|
|
|
|
|
1. Include guards (`#ifndef`/`#define`/`#endif`) are dropped.
|
|
|
|
|
|
2. Functions taking `Texture2D`/`SamplerState` are dropped; GPU resources have
|
|
|
|
|
|
no CultMath analog.
|
|
|
|
|
|
3. File-scope `static const` becomes `const`.
|
|
|
|
|
|
4. Floating literals gain `f` (`0.5` becomes `0.5f`).
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
5. Struct fields gain `public`. HLSL struct members have no access-modifier
|
|
|
|
|
|
concept; a C# struct's fields default to `private`, which would hide them
|
|
|
|
|
|
from the mirror test's reflection-based field comparison.
|
|
|
|
|
|
6. The file body is wrapped in a class, since C# has no free functions; HLSL
|
2026-09-14 13:57:03 +02:00
|
|
|
|
functions become private instance methods.
|
|
|
|
|
|
|
|
|
|
|
|
HLSL constructs that stay outside the target and must not appear in the mirror:
|
|
|
|
|
|
`const` locals of vector type (C# `const` is primitives only; write a plain
|
|
|
|
|
|
local) and writes through a swizzle component (`v.xy.x = s`, which C# rejects as
|
|
|
|
|
|
modifying a temporary).
|
|
|
|
|
|
|
|
|
|
|
|
The HLSL side is checked too: `tools/get-dxc.ps1` fetches Microsoft's
|
|
|
|
|
|
DirectXShaderCompiler, and
|
|
|
|
|
|
`dxc -T lib_6_3 -HV 2021 shaders/CultMath.hlsl` must compile cleanly.
|
2026-09-14 13:30:00 +02:00
|
|
|
|
|
2026-09-14 14:32:43 +02:00
|
|
|
|
The mirror test compares bit patterns (any NaN equals any NaN; -0 differs from
|
|
|
|
|
|
0). It proves the text of `CultMath.hlsl` computes the same float32 results as
|
|
|
|
|
|
C# `math` on the CPU, not that a GPU agrees: driver `sin` precision alone makes
|
2026-09-14 14:47:10 +02:00
|
|
|
|
`cultmath_hash` and value noise differ bit for bit on hardware. The mirror is
|
|
|
|
|
|
compiled under `using static CultMath.math;`, so its intrinsics are C# `math`
|
|
|
|
|
|
itself: the test proves the composition in the text, not the intrinsic rules,
|
|
|
|
|
|
which only `HlslSemanticsTests` pins.
|
2026-09-14 14:32:43 +02:00
|
|
|
|
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
The comparison also allows exactly one struct return shape: a mirror function
|
|
|
|
|
|
whose HLSL return type is a plain struct of `float`/`floatN`/`int`/`intN`
|
|
|
|
|
|
fields is compared to its C# counterpart field by field, recursing into each
|
2026-09-25 20:40:43 +02:00
|
|
|
|
field's own components, down to bit-for-bit scalars, matching each leaf's
|
|
|
|
|
|
declaration path (field names, not just position) and declared type as well
|
2026-09-29 23:51:04 +02:00
|
|
|
|
as its value. `CultCellular` (`math.cs`, `cellular`) and `CultPhasor`
|
CultMath: Phacelle port moves into MPL-2.0 files; prune cut pinned by boundary points
MPL-2.0 is file-level and the Phacelle port sat inside MIT files. phacelle and
CultPhasor now live in math.Phacelle.cs and CultPhasor.cs, and cultmath_phacelle
in CultMath.Phacelle.hlsl (included by CultMath.hlsl, both tracked copies), each
with the MPL header and Johansen / Luke Mitchell attribution. math stays one
partial class, so math.phacelle is called exactly as before. The GameCult.Math
package declares MIT AND MPL-2.0 and packs THIRD-PARTY-NOTICES.md and the new
include; the Unity LICENSE.md points at CultLib; build-unity-package.ps1
requires the new include.
The random sweeps never met a cell whose box bound sits just under the 2.25 cut
while its weight is still nonzero, so cuts of 2.245, 2.24 and 2.2 survived.
Six hard-coded points (found by an offline search) sit next to such cells; the
C# pruned-vs-unpruned test and the HLSL mirror test both use them, with a
witness check that re-derives the property from the hash.
Docs: the prune keeps about 45 of 64 cells (14 is the nonzero-weight count),
the support radius is 1.49998 (d^2 = 2.24995), normalization's range and
saturation, W = 0 only for adversarial hashes, and the support-edge gradient
seam size.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 00:34:27 +02:00
|
|
|
|
(`math.Phacelle.cs`, `phacelle`) are its consumers. A struct return with a mismatched, reordered-by-name, or
|
2026-09-25 20:40:43 +02:00
|
|
|
|
retyped field does not get a second comparison path or a shape of its own;
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
it fails the same walk that already handles a bare vector return.
|
|
|
|
|
|
|
2026-10-02 20:09:45 +02:00
|
|
|
|
### GLSL Target: Source Transformations
|
|
|
|
|
|
|
|
|
|
|
|
`shaders/CultMath.glsl` is a GLSL ES 3.00 (WebGL2) library generated from
|
2026-10-03 13:40:16 +02:00
|
|
|
|
`shaders/CultMath.hlsl` by `GlslLowering` (in the test project), and
|
|
|
|
|
|
`shaders/CultMath.Phacelle.glsl` is generated beside it from
|
|
|
|
|
|
`shaders/CultMath.Phacelle.hlsl`. Both are derived files: never edit them by
|
|
|
|
|
|
hand. Regenerate them, and the golden fixture
|
2026-10-02 20:09:45 +02:00
|
|
|
|
`tests/CultMath.Tests/fixtures/glsl-parity.json`, with:
|
|
|
|
|
|
|
|
|
|
|
|
```powershell
|
|
|
|
|
|
$env:CULTMATH_WRITE_GLSL = "1"; dotnet test packages/cultmath/tests/CultMath.Tests --filter GlslMirrorTests
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-10-03 13:40:16 +02:00
|
|
|
|
`GlslMirrorTests.CommittedGlslEqualsLowering` fails on any difference between
|
|
|
|
|
|
either committed file and the lowering of the committed HLSL. These
|
|
|
|
|
|
transformations, in order, are the complete list:
|
2026-10-02 20:09:45 +02:00
|
|
|
|
|
|
|
|
|
|
1. Each `#include "name"` is replaced in place by the included file, by the
|
2026-10-03 13:40:16 +02:00
|
|
|
|
same resolver the C# mirror uses; GLSL has no `#include`. The exception is
|
|
|
|
|
|
an include under a file-level licence of its own, listed in one table,
|
|
|
|
|
|
`GlslLowering.SeparateFiles`: its line is dropped and the file is lowered on
|
|
|
|
|
|
its own, by the same steps, into its own output. The table has one entry,
|
|
|
|
|
|
`CultMath.Phacelle.hlsl` to `CultMath.Phacelle.glsl`, because Phacelle is
|
|
|
|
|
|
MPL-2.0 and MPL-2.0 is file-level: kept separate, it leaves
|
|
|
|
|
|
`CultMath.glsl` MIT. Nothing else in the lowering is keyed by name.
|
2026-10-02 20:09:45 +02:00
|
|
|
|
2. Include guards are dropped, and so are the functions taking
|
|
|
|
|
|
`Texture2D`/`SamplerState`; the host shader samples its own textures.
|
|
|
|
|
|
3. Type names: `floatN`, `intN`, `uintN` and `boolN` become `vecN`, `ivecN`,
|
|
|
|
|
|
`uvecN` and `bvecN`.
|
|
|
|
|
|
4. File-scope `static const` becomes `const`.
|
|
|
|
|
|
5. C-style casts `(float)x`, `(int)x` and `(uint)x` become `float(x)`, `int(x)`
|
|
|
|
|
|
and `uint(x)`. The operand is found by a balanced-bracket scanner:
|
|
|
|
|
|
`(int)((state >> 28) + 4u)` nests parentheses.
|
|
|
|
|
|
6. Intrinsics are renamed: `lerp` to `mix`, `frac` to `fract`, `rsqrt` to
|
|
|
|
|
|
`inversesqrt`, `asuint` to `floatBitsToUint`, `asfloat` to
|
|
|
|
|
|
`uintBitsToFloat`, and `saturate(x)` becomes `clamp(x, 0.0, 1.0)`.
|
|
|
|
|
|
7. Float literals pass through unchanged.
|
2026-10-03 13:40:16 +02:00
|
|
|
|
8. Each output is wrapped in a guard named for its file: `CULTMATH_GLSL`,
|
|
|
|
|
|
`CULTMATH_PHACELLE_GLSL`.
|
|
|
|
|
|
9. The first line names the file as generated, its sources and its licence,
|
|
|
|
|
|
and says how to regenerate it; `CultMath.glsl`'s also says which
|
|
|
|
|
|
functions live in the separate file.
|
2026-10-02 20:09:45 +02:00
|
|
|
|
|
|
|
|
|
|
GLSL ES 3.00 is stricter than HLSL: no implicit conversions between int, uint
|
|
|
|
|
|
and float, no C-style casts, and no `step(genType, float)` overload. Those gaps
|
|
|
|
|
|
are closed in the HLSL, never in the lowering: the HLSL is written in the subset
|
|
|
|
|
|
both languages share, with edits that are legal HLSL and compute the same bits
|
|
|
|
|
|
(`step(h, float4(0.0, 0.0, 0.0, 0.0))` rather than `step(h, 0.0)`, `+ 4u` where
|
|
|
|
|
|
a uint is added, `(float)((uint)hash.x >> 8)` before a float multiply). The C#
|
|
|
|
|
|
mirror and the FXC and dxc compiles keep those edits honest. A body that needs a
|
|
|
|
|
|
transformation outside the list fails `GlslMirrorTests.NoHlslOnlyTokensSurvive`
|
|
|
|
|
|
rather than gaining a special case.
|
|
|
|
|
|
|
|
|
|
|
|
The library declares no precision. The host shader does, and it must be
|
|
|
|
|
|
`precision highp float; precision highp int;`: the PCG hashes need 32-bit
|
|
|
|
|
|
integers. `tools/compile-glsl.ps1` writes such a wrapper (its own `#version
|
|
|
|
|
|
300 es` line, the precision, the library by string concatenation, and one call
|
|
|
|
|
|
of every public function) and compiles it with glslang, which
|
2026-10-03 13:40:16 +02:00
|
|
|
|
`tools/get-glslang.ps1` fetches pinned. It runs twice: over `CultMath.glsl`
|
|
|
|
|
|
alone, and with `-Phacelle` over `CultMath.glsl` followed by
|
|
|
|
|
|
`CultMath.Phacelle.glsl`, the order a consumer that calls `cultmath_phacelle`
|
|
|
|
|
|
concatenates them in. Two negative controls must fail: `-Body` with an HLSL
|
|
|
|
|
|
token (`float x = lerp(0.0, 1.0, 0.5);`), and `-Body` calling
|
|
|
|
|
|
`cultmath_phacelle` without `-Phacelle`, which proves the MIT file does not
|
|
|
|
|
|
carry it. The Unity package ships neither GLSL file.
|
2026-10-02 20:09:45 +02:00
|
|
|
|
|
|
|
|
|
|
Parity runs in two legs, each with one owner: HLSL against C# by the mirror
|
|
|
|
|
|
test, bit for bit; GLSL against C# by the golden fixture, which records 256
|
|
|
|
|
|
seeded cases per function family as float32 bit patterns, evaluated by the
|
2026-10-03 14:15:33 +02:00
|
|
|
|
consumer on WebGL2 and read back. The families `pcg3d` and `pcg4d` are marked
|
|
|
|
|
|
exact; the float families are ulp-bounded, the bound measured on the device.
|
|
|
|
|
|
|
|
|
|
|
|
`iv_frustum_ball` is ulp-bounded, not exact: it calls `sqrt`, which GLSL ES
|
|
|
|
|
|
3.00 does not require to be correctly rounded, and WebGL2 compilers may
|
|
|
|
|
|
reassociate. On a GTX 1070 under ANGLE's D3D11 backend its radius differs from
|
|
|
|
|
|
C# by up to 2 ulp in 43 of 256 cases, sometimes smaller. What the march needs
|
2026-10-03 14:35:48 +02:00
|
|
|
|
is enclosure, and what culling needs is a ball no wider than its rounding
|
|
|
|
|
|
widening, so the family's fixture entry carries a `check` the consumer applies
|
|
|
|
|
|
to every case: from the case's arguments `(mx, my, z0, z1, fp, warp)`, compute
|
|
|
|
|
|
in double `zm = (z0 + z1) / 2`, the centre `c = (mx zm, my zm, zm)`, its
|
|
|
|
|
|
`|c|_1 = |mx zm| + |my zm| + |zm|`, the radius
|
|
|
|
|
|
`r = (z1 - z0) / 2 sqrt(mx^2 + my^2 + 1) + z1 fp + warp` and
|
|
|
|
|
|
`d = |GPU centre - c|`, and require
|
|
|
|
|
|
`r + d <= GPU radius <= r + d + 2^-19 (r + |c|_1)`. The lower side is
|
|
|
|
|
|
enclosure; the upper side is the bound `NoiseBoundTests` pins in C#, so a
|
|
|
|
|
|
lowering that inflates the ball fails too. Only those two bounds fail the
|
|
|
|
|
|
check; the largest ulp distance is reported as for any ulp-bounded family.
|
2026-10-03 14:15:33 +02:00
|
|
|
|
`GoldenFixtureMatchesCSharp` applies the same check to C#'s own results.
|
|
|
|
|
|
|
2026-10-04 09:47:49 +02:00
|
|
|
|
Every `af_*` function is an ulp-bounded family, and `af_snoise`, `af_fbm` and `af_frustum_ball` also carry a two-sided `check`
|
|
|
|
|
|
of their own, applied the same way: the device's own `snoise` or fBm at 75 sampled points of the case's set must lie in the device's
|
|
|
|
|
|
form with no tolerance (a device whose gradient error exceeds the allowance fails), its width must not pass the Lipschitz form's
|
|
|
|
|
|
allowance, and the device's `af_frustum_ball` radius must enclose the sampled ray points and stay within its rounding widening.
|
2026-10-04 11:09:35 +02:00
|
|
|
|
The reference is the device's own noise, which is right for culling soundness but would pass a device whose noise is wrong
|
|
|
|
|
|
everywhere, so two appended families (`af_snoise` and `af_fbm`, 256 cases each) add the centre leg: the fixture's own `x0`
|
|
|
|
|
|
(C#'s value at the centre) must lie in the device form's `[x0 - e, x0 + e]`, for every `af_snoise` and `af_fbm` family. They
|
|
|
|
|
|
also draw reach `|axis| + radius` from 1e-6 to 1e-3, the short slices near a camera, where the float32 allowance is most of `e`:
|
|
|
|
|
|
the main families' reach is at least about 1e-3, where the second-order remainder is far larger and a device that drops the
|
|
|
|
|
|
allowance still passes. `GoldenFixtureMatchesCSharp` requires each of the two to hold cases that fail on C#'s own results once
|
|
|
|
|
|
the allowance is removed.
|
2026-10-04 09:47:49 +02:00
|
|
|
|
|
2026-10-03 14:35:48 +02:00
|
|
|
|
`GoldenFixtureMatchesCSharp` evaluates C# on the committed fixture's own
|
|
|
|
|
|
arguments. Arguments are drawn only when the fixture is regenerated
|
|
|
|
|
|
(`CULTMATH_WRITE_GLSL=1`), because the draw itself calls `MathF.Pow`, whose
|
|
|
|
|
|
bits differ by OS. `iv_exp` and `phacelle` take their C# results from the
|
|
|
|
|
|
platform's `exp`, `sin` and `cos`, which also differ by OS, so their entries
|
|
|
|
|
|
name the platform that generated them and the test compares them case by case
|
|
|
|
|
|
against the running platform, printing the largest distance. `iv_exp`
|
|
|
|
|
|
("ulp-bounded, platform exp") must agree within one ulp. `phacelle`
|
|
|
|
|
|
("ulp-bounded, platform exp, sin, cos; across platforms |diff| <= 2^-20
|
|
|
|
|
|
max(|v|, 1)") must agree within `2^-20 max(|v|, 1)` per component, `v` being
|
|
|
|
|
|
the fixture's value: its sums of `exp`, `sin` and `cos` cancel near zero, where
|
|
|
|
|
|
ulps mean nothing, and Windows differs from the Linux fixture by up to
|
|
|
|
|
|
`3.6e-7 max(|v|, 1)`. Every other family is compared as text. `phacelle` is in
|
|
|
|
|
|
`CultMath.Phacelle.glsl`, so the evaluator concatenates
|
|
|
|
|
|
`CultMath.Phacelle.glsl` after `CultMath.glsl` for that family, with
|
|
|
|
|
|
normalization 0.5. The site does not vendor the MPL file and skips the family.
|
|
|
|
|
|
Its evaluator is the cultmath-tapes follow-up `glsl-browser-parity-run`, owned
|
|
|
|
|
|
by the next CultMath parity cut: a repeatable CultLib WebGL2 run of every
|
|
|
|
|
|
fixture family, `phacelle` included. Until it lands, the one GLSL evaluation of
|
|
|
|
|
|
`phacelle` is a single probe (GTX 1070, ANGLE D3D11: within `3.1e-6 max(|v|, 1)`
|
|
|
|
|
|
of C#). Asura's `gpu-snoise-parity` is a different check, an FXC and Unity
|
|
|
|
|
|
readback of `snoise`, and evaluates no GLSL.
|
2026-10-02 20:09:45 +02:00
|
|
|
|
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
## Invariant 8: Value-and-Gradient Primitives
|
|
|
|
|
|
|
|
|
|
|
|
A primitive tagged invariant 8 returns its value and analytic gradient
|
|
|
|
|
|
together, laid out as `float4(gradient.xyz, value.w)`, with no value-only twin
|
|
|
|
|
|
(a value-only form would be a second path with no consumer). `smin_grad` and
|
|
|
|
|
|
`cellular` are the first primitives in this family:
|
|
|
|
|
|
|
2026-09-25 20:40:43 +02:00
|
|
|
|
- `smin_grad(float4 a, float4 b, float k)` is Inigo Quilez's quadratic-polynomial
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
smooth minimum ("smooth minimum",
|
|
|
|
|
|
<https://iquilezles.org/articles/smin/>), carried to value-and-gradient
|
|
|
|
|
|
form. Its blend factor `h` is affine in `b.w - a.w`, so differentiating the
|
|
|
|
|
|
value with respect to position, the terms carrying `dh/dp` cancel exactly;
|
|
|
|
|
|
the surviving gradient is `lerp(∇b, ∇a, h)`, the same `h` the value uses.
|
|
|
|
|
|
This is the analytic gradient, not an approximation, and it is continuous
|
|
|
|
|
|
across the `|a.w - b.w| = k` seam where `h` saturates to 0 or 1.
|
|
|
|
|
|
- `cellular(float3 p)` is Worley's cellular texture basis function ("A
|
|
|
|
|
|
Cellular Texture Basis Function", SIGGRAPH 1996), searched over the
|
2026-09-25 20:40:43 +02:00
|
|
|
|
jittered 5×5×5 neighbourhood of feature points selected by `pcg3d` (never
|
|
|
|
|
|
the sin-based `hash`), with cells whose position-only lower bound already
|
|
|
|
|
|
exceeds the running F2 skipped before their hash is even computed. It
|
|
|
|
|
|
returns `CultCellular { nearest, edge, id }`: `nearest` is `(∇F1, F1)`,
|
|
|
|
|
|
`edge` is `(∇F2 - ∇F1, F2 - F1)`, and `id` is the nearest cell's `pcg4d`
|
|
|
|
|
|
hash (over the cell coordinate alone, never the `pcg3d` hash that produced
|
|
|
|
|
|
its jitter) mapped to `[0, 1)`. `∇F1` and `∇F2` are each undefined exactly
|
|
|
|
|
|
at their own feature point (`F1 = 0` or `F2 = 0`); `cellular` follows the
|
|
|
|
|
|
repo's existing degenerate-normal convention
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
(`GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle`, which
|
|
|
|
|
|
guards a zero-length normal and returns the zero vector instead of the NaN
|
2026-09-25 20:40:43 +02:00
|
|
|
|
a bare `normalize` gives there) and returns the zero vector rather than NaN
|
|
|
|
|
|
for either. `F1 = 0` is ordinary (`p` sits on a feature point); `F2 = 0`
|
|
|
|
|
|
needs two distinct cells' `pcg3d`-jittered feature points to land on the
|
|
|
|
|
|
same float32 value in every component, which is unreachable at ordinary
|
|
|
|
|
|
magnitudes but confirmed reachable (see "Precision domain" below). The
|
|
|
|
|
|
identity of the nearest and second-nearest feature point changes
|
|
|
|
|
|
discontinuously across the `F1 = F2` set (a genuine kink, not a numerical
|
|
|
|
|
|
artifact), so `∇F1` and `∇F2` are only continuous away from that set.
|
|
|
|
|
|
|
|
|
|
|
|
The 5×5×5 radius is exact, not merely safer than the classic 3×3×3 radius:
|
|
|
|
|
|
for a query at position `u` inside its own cell (`u` in `[0, 1)^3` per
|
|
|
|
|
|
component) and a candidate feature at integer cell offset `d` with jitter
|
|
|
|
|
|
`j` in `[0, 1)^3`, the per-axis displacement `(u_i - j_i) - d_i` is always
|
|
|
|
|
|
strictly greater in magnitude than `max(0, |d_i| - 1)`, because
|
|
|
|
|
|
`u_i - j_i` is always strictly inside `(-1, 1)`. So any cell with `|d_i| >=
|
|
|
|
|
|
3` on some axis is strictly farther than 2 from the query. Two real,
|
docs: correct F6 proof, integer hashing, and the prune's precision domain
design.md follow-ups matching the math.cs/HLSL fix in the previous commit:
- The F6 "5x5x5 is exact" argument is rewritten with the corrected near-side
neighbour bound (choose the axis closest to its own boundary, not a fixed
axis), the m <= 1/2 derivation, and the counterexample that showed the old
argument wrong. Same text as the math.cs comment, so the two do not drift.
- "Integer hashing" now states that cellular hashes the winning cell's
integer coordinate, not its float32 bit pattern, and records the measured
chi-square evidence (203.8 over 1,000,000 cells, 15 df, critical value
37.7) for why the old bit-pattern hash failed a uniformity check.
- "Precision domain" gets a new paragraph: the lower-bound prune's bit
identity to the unpruned 5x5x5 search holds for |p| < 2^25, not
unconditionally. Above that, cell + offset itself starts to round in
float32, so the pruned and unpruned loops can visit different actual
neighbours at the same nominal offset (Soul, cut 2a-i: 180 of 1,100,000
points differ at |p| ~= 3.4e7). This is a float32 domain limit on the
equivalence proof, not a bug in the prune; the committed test's [-50, 50]^3
sweep is comfortably inside it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 23:13:25 +02:00
|
|
|
|
always-present candidates bound `F1` and `F2`: the query's own cell, whose
|
|
|
|
|
|
distance is always strictly less than `sqrt(3)` (every axis strictly under
|
|
|
|
|
|
1); and the near-side neighbour on whichever single axis has `u_i` closest
|
|
|
|
|
|
to a cell boundary. Let `m = min(u_i, 1 - u_i)` on that chosen axis, so
|
|
|
|
|
|
`m` is in `[0, 1/2]` by construction. Stepping one cell towards that
|
|
|
|
|
|
nearer boundary puts the query within `1 + m` of the far face on the
|
|
|
|
|
|
offset axis and within `1 - m` of the near face on each of the other two
|
|
|
|
|
|
axes, so that neighbour's squared distance is at most
|
|
|
|
|
|
`(1 + m)^2 + 2*(1 - m)^2 = 3 - 2m + 3m^2 <= 3` for every `m` in `[0, 1/2]`
|
2026-09-26 02:23:55 +02:00
|
|
|
|
(3 at `m = 0`, dipping to a minimum of `8/3 ~= 2.667` at `m = 1/3`, then
|
|
|
|
|
|
rising back to 2.75 at `m = 1/2`), and strictly below 3 because
|
docs: correct F6 proof, integer hashing, and the prune's precision domain
design.md follow-ups matching the math.cs/HLSL fix in the previous commit:
- The F6 "5x5x5 is exact" argument is rewritten with the corrected near-side
neighbour bound (choose the axis closest to its own boundary, not a fixed
axis), the m <= 1/2 derivation, and the counterexample that showed the old
argument wrong. Same text as the math.cs comment, so the two do not drift.
- "Integer hashing" now states that cellular hashes the winning cell's
integer coordinate, not its float32 bit pattern, and records the measured
chi-square evidence (203.8 over 1,000,000 cells, 15 df, critical value
37.7) for why the old bit-pattern hash failed a uniformity check.
- "Precision domain" gets a new paragraph: the lower-bound prune's bit
identity to the unpruned 5x5x5 search holds for |p| < 2^25, not
unconditionally. Above that, cell + offset itself starts to round in
float32, so the pruned and unpruned loops can visit different actual
neighbours at the same nominal offset (Soul, cut 2a-i: 180 of 1,100,000
points differ at |p| ~= 3.4e7). This is a float32 domain limit on the
equivalence proof, not a bug in the prune; the committed test's [-50, 50]^3
sweep is comfortably inside it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 23:13:25 +02:00
|
|
|
|
the jitter never reaches exactly 0 or 1. `F2` is at most the larger of
|
|
|
|
|
|
these two real candidates (any two real candidates upper-bound the
|
|
|
|
|
|
2nd-smallest value over the full infinite candidate set), so `F1 <
|
|
|
|
|
|
sqrt(3)` and `F2 < sqrt(3)` always, comfortably under the radius-2
|
|
|
|
|
|
exclusion distance of 2, with margin `2 - sqrt(3) ~= 0.268` (Soul's
|
|
|
|
|
|
adversarial check, cut 2a-i: the worst realizable in-box `F2` is exactly
|
|
|
|
|
|
`sqrt(3)`, reached only in the unattainable limit at a cell corner). 3×3×3's
|
|
|
|
|
|
matching argument only reaches radius 1, whose excluded cells start at
|
|
|
|
|
|
distance 1, short of the `sqrt(3)` ceiling — the gap its measured failures
|
|
|
|
|
|
(9 of 400,000 points in `[-50, 50]^3`, worst error 0.088) live in.
|
|
|
|
|
|
|
|
|
|
|
|
An earlier version of this proof picked the near-side neighbour on a fixed
|
|
|
|
|
|
axis instead of whichever axis sits closest to its own boundary, and that
|
|
|
|
|
|
version is wrong (Soul, cut 2a-i): `u = (0.5, 0, 0)` with the x-neighbour's
|
|
|
|
|
|
jittered feature at `(-1, 1 - 2^-24, 1 - 2^-24)` gives a distance of about
|
|
|
|
|
|
2.06, outside the radius-2 exclusion the proof needs — `u.x = 0.5` is the
|
|
|
|
|
|
point on the cell farthest from any boundary, so `x` was the wrong axis to
|
|
|
|
|
|
fix there. Choosing the axis by proximity to its own boundary is what keeps
|
|
|
|
|
|
`m <= 1/2` and the bound at `sqrt(3)`.
|
|
|
|
|
|
|
|
|
|
|
|
`CellularAndSminGradTests` pins both the prune's bit-identical equivalence
|
|
|
|
|
|
to an unpruned 5×5×5 reference and the search's exact agreement with an
|
|
|
|
|
|
independent 7×7×7 brute-force oracle.
|
2026-09-25 20:40:43 +02:00
|
|
|
|
|
|
|
|
|
|
Precision domain: `cellular`'s exactness above is a statement about the
|
|
|
|
|
|
search radius, not about float32. Past `|p|` of roughly `2^23`, adjacent
|
|
|
|
|
|
integer cells stop being distinguishable from their neighbours at float32
|
|
|
|
|
|
precision in some directions, and distinct cells' jittered features can
|
|
|
|
|
|
round onto the same value, making `F1 = F2` ties (and, per-component,
|
|
|
|
|
|
`F2 = 0`) increasingly common from about `1e6` and confirmed at `2^24`
|
|
|
|
|
|
(measured: 4096 of 4096 sampled integer points at `2^24` hit this before
|
|
|
|
|
|
the guard above). `cellular` stays finite there; it does not stay accurate
|
|
|
|
|
|
in the sense of matching the idealized real-valued Worley field.
|
Add smin_grad and cellular value-and-gradient primitives to CultMath
smin_grad(float4,float4,float) carries Inigo Quilez's cubic-polynomial smooth
minimum to invariant 8's value-and-gradient form: the gradient blend lerp(b,a,h)
is the exact analytic derivative, not an approximation, since the h-derivative
terms cancel algebraically inside the smoothing band.
cellular(float3) is Worley's cellular texture basis function (SIGGRAPH 1996),
searched over the jittered 3x3x3 neighbourhood of pcg3d-selected feature
points. It returns the new CultCellular struct (nearest, edge, id), following
the repo's existing degenerate-normal convention
(GameCult.Geometry.CultGeometryIsoSurface.EmitOrientedTriangle) to return a
finite zero gradient instead of NaN at a feature point.
Both are fresh implementations from the cited literature, not ports, so they
get a design.md/doc-comment citation instead of a THIRD-PARTY-NOTICES entry.
design.md gains an "Invariant 8: Value-and-Gradient Primitives" section
documenting both primitives' semantics and conventions.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:17:39 +02:00
|
|
|
|
|
docs: correct F6 proof, integer hashing, and the prune's precision domain
design.md follow-ups matching the math.cs/HLSL fix in the previous commit:
- The F6 "5x5x5 is exact" argument is rewritten with the corrected near-side
neighbour bound (choose the axis closest to its own boundary, not a fixed
axis), the m <= 1/2 derivation, and the counterexample that showed the old
argument wrong. Same text as the math.cs comment, so the two do not drift.
- "Integer hashing" now states that cellular hashes the winning cell's
integer coordinate, not its float32 bit pattern, and records the measured
chi-square evidence (203.8 over 1,000,000 cells, 15 df, critical value
37.7) for why the old bit-pattern hash failed a uniformity check.
- "Precision domain" gets a new paragraph: the lower-bound prune's bit
identity to the unpruned 5x5x5 search holds for |p| < 2^25, not
unconditionally. Above that, cell + offset itself starts to round in
float32, so the pruned and unpruned loops can visit different actual
neighbours at the same nominal offset (Soul, cut 2a-i: 180 of 1,100,000
points differ at |p| ~= 3.4e7). This is a float32 domain limit on the
equivalence proof, not a bug in the prune; the committed test's [-50, 50]^3
sweep is comfortably inside it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 23:13:25 +02:00
|
|
|
|
The lower-bound prune's bit-identity to the unpruned 5×5×5 search also has
|
|
|
|
|
|
a domain: it holds for `|p| < 2^25`. Above that, `cell + d` (an integer
|
|
|
|
|
|
cell coordinate plus a small integer offset) starts to round in float32,
|
|
|
|
|
|
so a pruned cell's own recomputed offset can land on a different neighbour
|
|
|
|
|
|
than the unpruned loop visits at that same nominal `(dx, dy, dz)`, and the
|
|
|
|
|
|
two search paths diverge (Soul, cut 2a-i: measured 180 of 1,100,000 points
|
|
|
|
|
|
differ at `|p| ~= 3.4e7`). This is a float32-precision limit on the prune's
|
|
|
|
|
|
equivalence proof, not a bug in the prune itself: below `2^25` the two
|
|
|
|
|
|
paths are proven bit-identical (`PrunedSearchIsBitIdenticalToTheUnprunedFiveCubedSearch`,
|
|
|
|
|
|
which samples `[-50, 50]^3`, is comfortably inside that domain).
|
|
|
|
|
|
|
CultMath: snoise(float3) takes the upstream simplex-boundary fix
Kernel radius^2 0.6 -> 0.5, scale 42 -> 105, permute (34x+1)x -> (34x+10)x, per
stegu/webgl-noise 22434e04d7 (fix 21d9fe23d7, scale ff3b5d34ea, permute a3e6d57095).
snoise_grad, fbm_grad, ridged_grad follow. C#, both HLSL copies, design.md and
THIRD-PARTY-NOTICES.md change together. The 2D snoise shares the permute helper, so
its values change too. Golden values are regenerated in the next commit.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 11:13:34 +02:00
|
|
|
|
- `snoise(float3)` follows webgl-noise `src/noise3D.glsl` as of
|
|
|
|
|
|
stegu/webgl-noise `22434e04d7`: kernel radius squared `0.5`, output scale
|
|
|
|
|
|
`105`, permutation `mod289((34x+10)x)`. Upstream's `21d9fe23d7` (2020-10-14,
|
|
|
|
|
|
"an age-old bug that caused slight discontinuities along simplex
|
|
|
|
|
|
boundaries") moved the radius from `0.6` to `0.5`; at `0.6` a lattice vertex
|
|
|
|
|
|
outside the four summed corners can lie inside the kernel, leaving a value
|
|
|
|
|
|
jump at the cell boundary (3.1e-3 over the committed probe's 400 lines). `ff3b5d34ea` set the scale
|
|
|
|
|
|
to `105` and `a3e6d57095` (2021-06-30) the permutation; the 2D `snoise` shares
|
|
|
|
|
|
that permutation helper, so its values changed too, while its own kernel
|
|
|
|
|
|
(`0.5`) and scale (`130`) already matched upstream.
|
|
|
|
|
|
`NoiseGradTests.SnoiseAndItsGradientAreContinuousAcrossSimplexCellBoundaries`
|
2026-10-04 03:26:10 +02:00
|
|
|
|
pins the continuity. Ties in the offset `x0` resolve in Gustavson's
|
|
|
|
|
|
`simplexnoise1234` order (x over y, y over z, x over z), a total order, so the
|
|
|
|
|
|
four corners are always a simplex. Upstream's `step` compares are cyclic: where
|
2026-10-09 23:07:48 +02:00
|
|
|
|
the three components are equal, which is every input whose differences `y - x`
|
|
|
|
|
|
and `z - x` are whole numbers (every integer point, the origin and every
|
|
|
|
|
|
`(t, t, t)` among them), the corners repeat the origin vertex and the value
|
|
|
|
|
|
jumps by about 0.77 near the origin and up to 1.56 at magnitudes of 64 and
|
|
|
|
|
|
above. `SnoiseTieScopeTests` pins the set against the 0.3.0 values. `NoiseGradTests.SnoiseIsContinuousOnTheSimplexDiagonal` pins the order.
|
2026-09-26 02:37:05 +02:00
|
|
|
|
- `snoise_grad(float3 p)` carries `snoise(float3)` to value-and-gradient form,
|
|
|
|
|
|
following the differentiation in webgl-noise `src/noise3Dgrad.glsl` (Ashima
|
CultMath: snoise(float3) takes the upstream simplex-boundary fix
Kernel radius^2 0.6 -> 0.5, scale 42 -> 105, permute (34x+1)x -> (34x+10)x, per
stegu/webgl-noise 22434e04d7 (fix 21d9fe23d7, scale ff3b5d34ea, permute a3e6d57095).
snoise_grad, fbm_grad, ridged_grad follow. C#, both HLSL copies, design.md and
THIRD-PARTY-NOTICES.md change together. The 2D snoise shares the permute helper, so
its values change too. Golden values are regenerated in the next commit.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 11:13:34 +02:00
|
|
|
|
Arts / Ian McEwan, MIT), which uses the same radius, permutation and scale as
|
|
|
|
|
|
`snoise(float3)`: per corner, `m0 = max(0.5 - dot(x,x), 0)`, so
|
|
|
|
|
|
`d(m0^4 * dot(p,x))/dx = -8*m0^3*dot(p,x)*x + m0^4*p`, summed over the four
|
|
|
|
|
|
corners and scaled by the same constant as the value, so `.w` is exactly
|
|
|
|
|
|
`snoise(float3)`. `fbm_grad` and
|
2026-09-26 02:37:05 +02:00
|
|
|
|
`ridged_grad` are octave sums of `snoise_grad`: each octave samples at
|
|
|
|
|
|
`p * frequency`, so by the chain rule its gradient scales by that same
|
|
|
|
|
|
frequency, and both amplitude (`gain`) and frequency (`lacunarity`) compound
|
|
|
|
|
|
per octave rather than applying once to the whole sum. `ridged_grad` folds
|
|
|
|
|
|
each octave about zero (`Σ aᵢ(1 − |nᵢ|)`, gradient `−Σ aᵢfᵢ sign(nᵢ)∇nᵢ`,
|
|
|
|
|
|
Musgrave's ridged multifractal, *Texturing and Modeling* ch. 16); the crease
|
|
|
|
|
|
at `nᵢ = 0` is deliberate (the operator's dune term, 2026-09-25), not a bug.
|
|
|
|
|
|
Both clamp `octaves` to `[0, 16]`: `HlslSourceCompatibilityTests`'s bit-parity
|
|
|
|
|
|
oracle drives every mirrored `int` parameter across the full int32 range,
|
|
|
|
|
|
which is the right domain for `pcg3d`/`pcg4d`'s O(1) hash inputs but would
|
|
|
|
|
|
turn an unclamped octave count into a multi-billion-iteration loop for these
|
|
|
|
|
|
two mirrors.
|
2026-09-29 23:51:04 +02:00
|
|
|
|
- `phacelle(float3 p, float3 side, float offset, float normalization)` is Rune
|
|
|
|
|
|
Skovbo Johansen's Phacelle noise ("phase + cell": a stripe pattern built by
|
|
|
|
|
|
blending one cosine/sine wave per jittered cell), generalised to 3D cells. It
|
|
|
|
|
|
returns `CultPhasor { cos, sin }`, each `(∇, value)`. `side` is the stripe
|
|
|
|
|
|
wave vector, whose length is 2π times the stripe frequency; in 3D the
|
|
|
|
|
|
perpendicular to the flow is not unique, so the caller chooses it (on a unit
|
|
|
|
|
|
sphere, `cross(p̂, flow)`). `offset` is the phase in cycles. It visits 4×4×4
|
|
|
|
|
|
cells with `pcg3d`-hashed jitter in `[-0.5, 0.5)`, weights each wave by
|
|
|
|
|
|
`max(0, exp(-2d²) - 0.01111)`, and normalizes the blended phasor as upstream
|
|
|
|
|
|
does (length 1 where the raw length is at least `1 - normalization`, scaled
|
|
|
|
|
|
by `1 / (1 - normalization)` below). The gradient is exact, weight
|
|
|
|
|
|
derivatives and normalization included, rather than upstream's
|
|
|
|
|
|
`∇cos ≈ -sin·side`, which ignores the weight gradient. It is discontinuous
|
|
|
|
|
|
where the raw length crosses the normalization threshold and where a cell's
|
CultMath: Phacelle port moves into MPL-2.0 files; prune cut pinned by boundary points
MPL-2.0 is file-level and the Phacelle port sat inside MIT files. phacelle and
CultPhasor now live in math.Phacelle.cs and CultPhasor.cs, and cultmath_phacelle
in CultMath.Phacelle.hlsl (included by CultMath.hlsl, both tracked copies), each
with the MPL header and Johansen / Luke Mitchell attribution. math stays one
partial class, so math.phacelle is called exactly as before. The GameCult.Math
package declares MIT AND MPL-2.0 and packs THIRD-PARTY-NOTICES.md and the new
include; the Unity LICENSE.md points at CultLib; build-unity-package.ps1
requires the new include.
The random sweeps never met a cell whose box bound sits just under the 2.25 cut
while its weight is still nonzero, so cuts of 2.245, 2.24 and 2.2 survived.
Six hard-coded points (found by an offline search) sit next to such cells; the
C# pruned-vs-unpruned test and the HLSL mirror test both use them, with a
witness check that re-derives the property from the hash.
Docs: the prune keeps about 45 of 64 cells (14 is the nonzero-weight count),
the support radius is 1.49998 (d^2 = 2.24995), normalization's range and
saturation, W = 0 only for adversarial hashes, and the support-edge gradient
seam size.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 00:34:27 +02:00
|
|
|
|
weight leaves its support (d² = 2.24995, distance 1.49998); the value is
|
|
|
|
|
|
continuous at both. At the support edge `∇w` jumps from `-4·0.01111·v` to 0,
|
|
|
|
|
|
a per-cell jump of length `0.0444·|v|` (0.0667 at `|v| = 1.5`); measured over
|
|
|
|
|
|
random points, the output gradient jumps by a median 1.5% of its length and
|
|
|
|
|
|
at most about 27%. `normalization` is meant to lie in `[0, 1]`: 1 or more
|
|
|
|
|
|
saturates (every output is stretched to length 1), a negative value scales
|
|
|
|
|
|
every output down by `1 / (1 - normalization)`, and the normalized gradient
|
|
|
|
|
|
scales as `1 / |I|` for the blended phasor `I`, so it grows where the blend
|
|
|
|
|
|
nearly cancels. The total weight `W` is positive for every `pcg3d` input (the
|
|
|
|
|
|
smallest found by hill-climbing is 0.056); `W = 0`, and a NaN output, needs
|
|
|
|
|
|
adversarial jitter no hash yields.
|
2026-09-29 23:51:04 +02:00
|
|
|
|
Cells whose per-axis lower bound `Σ max(0, |local - g| - 0.5)²` is at least
|
CultMath: Phacelle port moves into MPL-2.0 files; prune cut pinned by boundary points
MPL-2.0 is file-level and the Phacelle port sat inside MIT files. phacelle and
CultPhasor now live in math.Phacelle.cs and CultPhasor.cs, and cultmath_phacelle
in CultMath.Phacelle.hlsl (included by CultMath.hlsl, both tracked copies), each
with the MPL header and Johansen / Luke Mitchell attribution. math stays one
partial class, so math.phacelle is called exactly as before. The GameCult.Math
package declares MIT AND MPL-2.0 and packs THIRD-PARTY-NOTICES.md and the new
include; the Unity LICENSE.md points at CultLib; build-unity-package.ps1
requires the new include.
The random sweeps never met a cell whose box bound sits just under the 2.25 cut
while its weight is still nonzero, so cuts of 2.245, 2.24 and 2.2 survived.
Six hard-coded points (found by an offline search) sit next to such cells; the
C# pruned-vs-unpruned test and the HLSL mirror test both use them, with a
witness check that re-derives the property from the hash.
Docs: the prune keeps about 45 of 64 cells (14 is the nonzero-weight count),
the support radius is 1.49998 (d^2 = 2.24995), normalization's range and
saturation, W = 0 only for adversarial hashes, and the support-edge gradient
seam size.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-09-30 00:34:27 +02:00
|
|
|
|
2.25 lie outside the support and have weight exactly 0, so they are skipped,
|
|
|
|
|
|
which changes no output bit. Cells whose bound sits just under 2.25 can still
|
|
|
|
|
|
carry a nonzero weight, so the cut cannot be lowered; `PhacelleTests` pins
|
|
|
|
|
|
the cut with points next to such cells, against an internal overload with the
|
|
|
|
|
|
prune disabled, and the HLSL mirror test pins it on the shader side. The prune
|
|
|
|
|
|
keeps about 45 of the 64 cells on average (a saving of about 30%); about 14
|
|
|
|
|
|
have a nonzero weight. The port sits in its own MPL-2.0 files
|
|
|
|
|
|
(`math.Phacelle.cs`, `CultPhasor.cs`, `CultMath.Phacelle.hlsl`); see
|
|
|
|
|
|
`THIRD-PARTY-NOTICES.md`. No 2D variant exists; nothing consumes one.
|
2026-09-26 02:37:05 +02:00
|
|
|
|
|
Add PCG integer hashes to CultMath and release 0.2.3
CultMath's hash is the shader-style frac(sin(x) * 43758.5453) and
returns float. Unity.Mathematics' hash returned a well-mixed uint, and
seeding from the float hash collapses: a 100x100 position grid gave 1625
distinct values in Aetheria's zone generator.
- math.pcg(uint), pcg3d(int3/float2/float3), and pcg4d(int4/float4)
follow Jarzynski and Olano, "Hash Functions for GPU Rendering"
(JCGT 9(3), 2020). pcg3d and pcg4d are the paper's code from page 32.
pcg is O'Neill's RXS-M-XS 32/32 output over one LCG step, with the
constants checked against pcg-c's pcg_variants.h.
- CultMath has no uint vectors, so pcg3d and pcg4d carry uint bit
patterns in int3/int4. The float overloads hash IEEE-754 bits via the
HLSL intrinsic asuint(float), which is new. pcg3d(float2) holds z at
0, the paper's route for 2 -> N inputs.
- The HLSL mirror gains the same functions, written over scalar uints
so the text compiles both in dxc 1.9.0.5402 (lib_6_3, HV 2021, clean)
and in C#. HlslSourceCompatibilityTests now compares int and uint
arguments and results by value; a wrong shift in the mirror's pcg4d
gives 208 mismatches.
- Known-answer vectors come from an independent Python transcription.
pcg3d separates a 100x100 half-unit grid (the full int3). One
component alone has one birthday collision there, as a 32-bit value
can. The same grid on the integer lattice has none.
- design.md states that the hashes are integer-exact in HLSL and on the
CPU: dxc wraps uint multiply and add modulo 2^32, as C# unchecked does.
- Bump to 0.2.3 and rebuild the tracked Unity DLL and pdb.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 18:19:53 +02:00
|
|
|
|
Integer hashing uses the PCG hashes from Jarzynski and Olano, "Hash Functions
|
|
|
|
|
|
for GPU Rendering" (JCGT 9(3), 2020): `pcg(uint)` is O'Neill's RXS-M-XS 32/32
|
|
|
|
|
|
permutation over one LCG step, and `pcg3d`/`pcg4d` are the paper's (3 → 3) and
|
|
|
|
|
|
(4 → 4) hashes. They use only uint multiply, add, xor, and shifts. dxc wraps
|
|
|
|
|
|
uint multiply and add modulo 2^32 exactly as C# unchecked arithmetic does, so
|
|
|
|
|
|
they are integer-exact in HLSL and on the CPU, GPU included, unlike the
|
|
|
|
|
|
`sin`-based `hash`. CultMath has no uint vectors, so `pcg3d(int3)` and
|
|
|
|
|
|
`pcg4d(int4)` carry uint bit patterns in int components. The `float2`/`float3`/`float4`
|
docs: correct F6 proof, integer hashing, and the prune's precision domain
design.md follow-ups matching the math.cs/HLSL fix in the previous commit:
- The F6 "5x5x5 is exact" argument is rewritten with the corrected near-side
neighbour bound (choose the axis closest to its own boundary, not a fixed
axis), the m <= 1/2 derivation, and the counterexample that showed the old
argument wrong. Same text as the math.cs comment, so the two do not drift.
- "Integer hashing" now states that cellular hashes the winning cell's
integer coordinate, not its float32 bit pattern, and records the measured
chi-square evidence (203.8 over 1,000,000 cells, 15 df, critical value
37.7) for why the old bit-pattern hash failed a uniformity check.
- "Precision domain" gets a new paragraph: the lower-bound prune's bit
identity to the unpruned 5x5x5 search holds for |p| < 2^25, not
unconditionally. Above that, cell + offset itself starts to round in
float32, so the pruned and unpruned loops can visit different actual
neighbours at the same nominal offset (Soul, cut 2a-i: 180 of 1,100,000
points differ at |p| ~= 3.4e7). This is a float32 domain limit on the
equivalence proof, not a bug in the prune; the committed test's [-50, 50]^3
sweep is comfortably inside it.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 23:13:25 +02:00
|
|
|
|
overloads hash the IEEE-754 bits (`asuint`), so -0 and 0 hash differently; they
|
|
|
|
|
|
exist for callers that already hold a hash key as float bits, and `pcg3d(float2)`
|
|
|
|
|
|
holds z at 0, which is the paper's route for (2 → N) inputs. Seeds and other
|
|
|
|
|
|
scalar uses take one output component.
|
|
|
|
|
|
|
|
|
|
|
|
`cellular`'s jitter and id always hash the integer cell coordinate directly
|
|
|
|
|
|
(`pcg3d(int3(neighbor))`, `pcg4d(int4(int3(cellId), 0))`), never a cell
|
|
|
|
|
|
coordinate's float32 bit pattern. Hashing the bit pattern of an
|
|
|
|
|
|
integer-valued float is not the same permutation as hashing the integer: two
|
|
|
|
|
|
adjacent cells' bit patterns share more structure (both have the same
|
|
|
|
|
|
exponent and a mantissa that differs by one increment) than two adjacent
|
|
|
|
|
|
integers do going into `pcg3d`'s own mixing, and that residual structure
|
2026-09-26 02:23:55 +02:00
|
|
|
|
survived into the jitter. Hashing the integer cell coordinate directly is
|
|
|
|
|
|
exact and runtime-independent, and it is the HLSL mirror's own hash, so C#
|
|
|
|
|
|
and shader agree bit-for-bit. What pins this is the oracle and mirror tests,
|
|
|
|
|
|
not a uniformity measurement: reverting to hashing the float bits (or the
|
|
|
|
|
|
sin-based hash) is caught by `ProductionSearchMatchesTheBruteForceOracleExactly`,
|
|
|
|
|
|
`EveryMirrorFunctionMatchesCSharpMath`, `F1NeverExceedsF2`, and the other
|
|
|
|
|
|
tests that key off the integer-hash fixtures (cut 2a-i). `CellularAndSminGradTests`
|
|
|
|
|
|
(`JitterAndIdPassAChiSquareUniformityTest`) checks the uniformity of the hash
|
|
|
|
|
|
as `cellular` uses it: the jitter marginals and the id pass a chi-square test
|
|
|
|
|
|
at a stated significance against `pcg3d(int3(...))`.
|
Add PCG integer hashes to CultMath and release 0.2.3
CultMath's hash is the shader-style frac(sin(x) * 43758.5453) and
returns float. Unity.Mathematics' hash returned a well-mixed uint, and
seeding from the float hash collapses: a 100x100 position grid gave 1625
distinct values in Aetheria's zone generator.
- math.pcg(uint), pcg3d(int3/float2/float3), and pcg4d(int4/float4)
follow Jarzynski and Olano, "Hash Functions for GPU Rendering"
(JCGT 9(3), 2020). pcg3d and pcg4d are the paper's code from page 32.
pcg is O'Neill's RXS-M-XS 32/32 output over one LCG step, with the
constants checked against pcg-c's pcg_variants.h.
- CultMath has no uint vectors, so pcg3d and pcg4d carry uint bit
patterns in int3/int4. The float overloads hash IEEE-754 bits via the
HLSL intrinsic asuint(float), which is new. pcg3d(float2) holds z at
0, the paper's route for 2 -> N inputs.
- The HLSL mirror gains the same functions, written over scalar uints
so the text compiles both in dxc 1.9.0.5402 (lib_6_3, HV 2021, clean)
and in C#. HlslSourceCompatibilityTests now compares int and uint
arguments and results by value; a wrong shift in the mirror's pcg4d
gives 208 mismatches.
- Known-answer vectors come from an independent Python transcription.
pcg3d separates a 100x100 half-unit grid (the full int3). One
component alone has one birthday collision there, as a 32-bit value
can. The same grid on the integer lattice has none.
- design.md states that the hashes are integer-exact in HLSL and on the
CPU: dxc wraps uint multiply and add modulo 2^32, as C# unchecked does.
- Bump to 0.2.3 and rebuild the tracked Unity DLL and pdb.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 18:19:53 +02:00
|
|
|
|
|
2026-09-14 13:30:00 +02:00
|
|
|
|
Where HLSL is silent, CultMath keeps its own decisions and does not defer to
|
2026-09-14 18:04:04 +02:00
|
|
|
|
Unity.Mathematics: `hash` returns float, and `Random` is CultMath's own xorshift32. Engine-shaped
|
2026-09-14 13:30:00 +02:00
|
|
|
|
helpers that HLSL lacks (`float2x2.Rotate`, `float3x3.Euler`, `quaternion`,
|
|
|
|
|
|
`snoise`) enter only when a consumer needs them, with their semantics stated in
|
|
|
|
|
|
code.
|
|
|
|
|
|
|
|
|
|
|
|
Known C# friction: under `using static CultMath.math;` the constructor
|
|
|
|
|
|
functions (`float3(...)`, `float3x3(...)`) hide the type names in member access,
|
|
|
|
|
|
so static members need a qualified type (`CultMath.float3x3.Euler(...)`).
|
|
|
|
|
|
|
2026-10-02 19:56:30 +02:00
|
|
|
|
## Intervals
|
|
|
|
|
|
|
|
|
|
|
|
`math.Interval.cs` and `shaders/CultMath.Interval.hlsl` (included from
|
|
|
|
|
|
`CultMath.hlsl` after the Phacelle include) carry interval arithmetic for
|
|
|
|
|
|
culling empty space with a proof. Their invariant, **intervals-enclose**: every
|
|
|
|
|
|
interval function returns a `[lo, hi]` that contains `f(x)` for every `x` in
|
|
|
|
|
|
its input region, where `f(x)` is the same function evaluated pointwise in
|
|
|
|
|
|
float32. A consumer that skips a region because `hi < cutoff` has skipped
|
|
|
|
|
|
nothing that was there.
|
|
|
|
|
|
|
|
|
|
|
|
Representation: an interval is `float2(lo, hi)` with `lo <= hi`, both finite.
|
|
|
|
|
|
There is no empty interval; a caller that needs "provably empty" tests
|
|
|
|
|
|
`hi < cutoff`. `iv_point(x)` is `float2(x, x)`.
|
|
|
|
|
|
|
|
|
|
|
|
Ops: `iv_point`, `iv_add`, `iv_sub`, `iv_neg`, `iv_mul` (the least and greatest
|
|
|
|
|
|
of the four corner products), `iv_scale(iv, float)`, `iv_abs` and `iv_sqr`
|
|
|
|
|
|
(an interval straddling zero starts at 0), `iv_min`, `iv_max`, `iv_sqrt`
|
|
|
|
|
|
(`lo` clamped at 0), `iv_exp`, `iv_clamp(iv, float, float)`, `iv_saturate`,
|
|
|
|
|
|
`iv_smoothstep(float, float, iv)` (its ordered endpoints) and
|
|
|
|
|
|
`iv_lerp(iv, iv, float)`. No others until a consumer names one. Each has an
|
|
|
|
|
|
HLSL mirror named `cultmath_iv_*`, compared bit for bit by
|
|
|
|
|
|
`HlslSourceCompatibilityTests`.
|
|
|
|
|
|
|
|
|
|
|
|
The ulp-widening rule. Each bound is computed with the pointwise function's
|
|
|
|
|
|
own float32 operations. IEEE round-to-nearest is monotone, so an op that is
|
|
|
|
|
|
monotone on its region encloses its float32 evaluation exactly, with no
|
|
|
|
|
|
widening; that covers every op above except two, which widen by exactly one
|
|
|
|
|
|
ulp, in the implementation and never in the test:
|
|
|
|
|
|
|
|
|
|
|
|
- `iv_exp`: `exp` is not required to be correctly rounded, so a libm need not
|
|
|
|
|
|
be monotone. .NET's `MathF.Exp` on Linux steps down 0 times over every float
|
|
|
|
|
|
in [-87, 88.7] (`IntervalTests.MeasureMonotonicity`), so on that CPU the
|
|
|
|
|
|
widening is a margin; it is there for other libms and GPUs.
|
|
|
|
|
|
- `iv_smoothstep`: `t * t * (3 - 2t)` in float32 steps down 337,095 times over
|
|
|
|
|
|
the floats in [0, 1], each time by at most 1 ulp (`MeasureMonotonicity`).
|
|
|
|
|
|
Since `t = saturate((x - minimum) / (maximum - minimum))` is itself monotone
|
|
|
|
|
|
in `x`, that covers every `smoothstep`.
|
|
|
|
|
|
|
|
|
|
|
|
`iv_lerp` is the natural interval extension of `x + (y - x) * amount`, which is
|
|
|
|
|
|
how `lerp` evaluates; every step is monotone, so it encloses with no widening
|
|
|
|
|
|
for any `amount`, and it is tight only when `a` is a point, because `x` appears
|
|
|
|
|
|
twice.
|
|
|
|
|
|
|
|
|
|
|
|
`IntervalTests` runs every op over 10,000 seeded intervals (centres in
|
|
|
|
|
|
[-20, 20], widths log-uniform in [1e-4, 10], every eighth a point) and 16
|
|
|
|
|
|
points inside each, endpoints and zero included, with no tolerance; and for
|
|
|
|
|
|
every op it raises `lo` and lowers `hi` by one ulp and requires the harness to
|
|
|
|
|
|
catch each. A new op is one entry in that table.
|
|
|
|
|
|
|
|
|
|
|
|
Noise bounds. `iv_snoise_ball(c, r)` is `[n - L r, n + L r]` intersected with
|
|
|
|
|
|
`[-1, 1]`, where `n = snoise(c)` and `L = SNOISE_LIPSCHITZ`: one `snoise`, value
|
|
|
|
|
|
only. No interval function reads `snoise_grad`, and none has a value-only or
|
|
|
|
|
|
gradient twin. `iv_fbm_ball(c, r, octaves, lacunarity, gain)` is the octave sum
|
|
|
|
|
|
of `iv_snoise_ball(c * f_i, r * |f_i|)` scaled by `a_i`, compounding frequency
|
|
|
|
|
|
and amplitude exactly as `fbm_grad` does, octaves clamped to [0, 16]. A domain
|
|
|
|
|
|
warp that moves a point by at most `D` is enclosed by the radius `r + D`
|
|
|
|
|
|
(`NoiseBoundTests.WarpedPointsStayEnclosed`); this is what the gamecult.org
|
|
|
|
|
|
ground's march rests on.
|
|
|
|
|
|
|
2026-10-03 00:08:35 +02:00
|
|
|
|
`iv_frustum_ball(m_c, z0, z1, footprintPerDepth, warp)` is the ball one probe
|
|
|
|
|
|
serves a whole screen tile with. In a pinhole camera frame looking down `+z`,
|
|
|
|
|
|
where the ray of slope `m` is the points `(m z, z)`, it encloses every point of
|
|
|
|
|
|
every ray whose slope lies within `footprintPerDepth` of `m_c` (`N / (sqrt(2) f)`
|
|
|
|
|
|
for an `N x N` tile at focal length `f` pixels: the full pixel footprint, so
|
|
|
|
|
|
sub-pixel jitter is covered), over depths `[z0, z1]`, each point moved by a warp
|
|
|
|
|
|
of length at most `warp`. The centre is `(m_c z_m, z_m)`, `z_m` the mid-depth;
|
|
|
|
|
|
the radius is `((z1 - z0) / 2) |(m_c, 1)| + z1 footprintPerDepth + warp`. A point
|
|
|
|
|
|
of slope `m_c + e` at depth `z` is the centre plus `(z - z_m)(m_c, 1)` plus
|
2026-10-03 11:37:30 +02:00
|
|
|
|
`z (e, 0)`. The returned ball is float32, and on a degenerate (`z0 == z1`),
|
|
|
|
|
|
thin or far segment the rounding of the centre alone exceeds the gap the
|
|
|
|
|
|
triangle inequality leaves, so the radius is widened by
|
|
|
|
|
|
`(|c|_1 + radius) 2^-20`. With `u = 2^-24`, each centre component carries at most
|
|
|
|
|
|
two roundings (`z_m`, then the product), so the float centre is within
|
|
|
|
|
|
`2u |c|_1` of the exact one; the radius carries at most seven (`z1 - z0`, the
|
|
|
|
|
|
axis length at `3u`, the product, `z1 footprintPerDepth`, the sum, `+ warp`), so
|
|
|
|
|
|
the exact radius is at most `(1 + 7u)` times the float one; the widening rounds
|
|
|
|
|
|
once more. `2^-20 = 16u` covers `8u (|c|_1 + radius)` twice over, and fma
|
2026-10-03 14:15:33 +02:00
|
|
|
|
contraction only removes roundings. The axis count assumes a correctly rounded
|
|
|
|
|
|
`sqrt`, which IEEE gives C#. GLSL ES 3.00 does not: it inherits `sqrt`'s
|
|
|
|
|
|
precision from `inversesqrt`, and D3D-backed compilers may reassociate. So the
|
|
|
|
|
|
bound is proven for C# and HLSL on IEEE hardware, and on GLSL it is measured:
|
|
|
|
|
|
on a GTX 1070 under ANGLE's D3D11 backend the fixture's 256 cases keep a
|
|
|
|
|
|
minimum relative slack of `1.98e-6` (about `33u`), the same as C#'s. The
|
2026-10-03 14:42:28 +02:00
|
|
|
|
fixture's enclosure check is what measures it on each device. The caller rotates and translates the centre into world space and scales
|
2026-10-03 11:37:30 +02:00
|
|
|
|
centre and radius by its noise frequency; the rounding of that transform is the
|
|
|
|
|
|
caller's. `NoiseBoundTests.TileBallEnclosesEveryRaySegment` checks 2,000 seeded
|
|
|
|
|
|
tiles x 64 points of the r2 domain and 2,000 tiles x 8 corner points of each of
|
|
|
|
|
|
seven extreme families (`z0 == z1`, `z1 - z0 = 1e-6 z1`, `|m_c|` to 1000, `z` to
|
|
|
|
|
|
`1e7`, `z0 = 0` with `z1` to `1e-3`, `f` down to 1, warp to `1e6`), corner flows
|
|
|
|
|
|
full-length and outward, distances in double, with no tolerance. It pins the ball
|
|
|
|
|
|
to the derivation both ways (`r <= ball.w <= r + 2^-19 (r + |c|_1)`), so a ball
|
|
|
|
|
|
that forgets the widening fails and one grown past it fails too. A moving camera needs nothing
|
2026-10-03 00:08:35 +02:00
|
|
|
|
more: each frame's probes use that frame's camera, and no ball, mask or probe
|
|
|
|
|
|
result is carried from one frame to the next.
|
|
|
|
|
|
|
|
|
|
|
|
The noise ball alone proves little: at `L` near 10 it bounds `snoise` to a width
|
|
|
|
|
|
of `2 L r`, which covers all of `[-1, 1]` once `L r` reaches 1. Empty space
|
|
|
|
|
|
becomes provable through an analytic envelope. The consumer's density is a
|
|
|
|
|
|
pre-distortion SDF (a height fog, a well map, a carved sphere) that noise
|
|
|
|
|
|
displaces only near its surface, and the consumer composes the bound from `iv_*`
|
|
|
|
|
|
ops. The envelope is taken over the slice's exact box (`iv_mul` of the depth
|
|
|
|
|
|
range by each slope range, then the SDF's own monotone pieces), together with its
|
|
|
|
|
|
fade. The noise is `iv_snoise_ball` over `iv_frustum_ball`, scaled by the noise
|
|
|
|
|
|
frequency. The warp moves only the noise argument, so it enlarges the ball and
|
|
|
|
|
|
the envelope's box takes none of it. Where the fade's upper end is 0 the noise
|
|
|
|
|
|
term is identically 0 and no `snoise` is evaluated. Where the envelope already
|
|
|
|
|
|
proves the slice empty with the noise at its full range, the ball is not
|
|
|
|
|
|
evaluated either. A probe is worth attempting only when the best result any
|
|
|
|
|
|
centre value could give would prove the slice, and a ball with `L r >= 2` is
|
2026-10-04 12:23:02 +02:00
|
|
|
|
`[-1, 1]` whatever `snoise(c)` is, so it costs nothing. CultMath owns none of the
|
2026-10-03 00:08:35 +02:00
|
|
|
|
envelope: the interval files have no envelope function, and each consumer composes
|
|
|
|
|
|
its own. `NoiseBoundTests.EnvelopeBoundEnclosesDensity` checks that composition,
|
|
|
|
|
|
the one the site and Aetheria use, over 2,000 seeded slices x 64 warped points
|
|
|
|
|
|
with no tolerance.
|
|
|
|
|
|
|
2026-10-02 19:56:30 +02:00
|
|
|
|
`L` has provenance, not a proof. `NoiseBoundTests.MeasureLipschitz` (slow,
|
|
|
|
|
|
explicit) takes the largest `|snoise_grad|` over 1e6 seeded points in
|
|
|
|
|
|
[-256, 256]^3 (7.1933103), refines the 1e4 largest by gradient ascent
|
2026-10-04 03:16:31 +02:00
|
|
|
|
(7.2400064), and multiplies by 1.10: `L = 7.9640074`. It also reaches
|
2026-10-02 19:56:30 +02:00
|
|
|
|
`|snoise| = 0.9718335` by the same ascent on the value, which is what lets the
|
|
|
|
|
|
bound intersect with `[-1, 1]`. `LipschitzConstantPinsSampledGradients`
|
|
|
|
|
|
checks 1e5 more seeded points against `L` and climbs again from the start
|
|
|
|
|
|
`MeasureLipschitz` prints as its witness, failing if `L` is not 1.10 times that
|
|
|
|
|
|
maximum to within half a percent, so a 1% move of `L` fails. The enclosure tests
|
|
|
|
|
|
over 2,000 balls x 64 points are the defence. A change to the `snoise` kernel
|
|
|
|
|
|
must re-run `MeasureLipschitz` and re-pin `L` and the witness.
|
|
|
|
|
|
|
|
|
|
|
|
What the ball bounds guarantee: enclosure of `snoise` and `fbm_grad(...).w` over
|
2026-10-03 22:21:50 +02:00
|
|
|
|
the ball, in float32. What they do not: anything about tapes, pruning or choice tracking. Correlation
|
|
|
|
|
|
between the operands of one expression is what "Affine forms" below adds.
|
|
|
|
|
|
How loose the ball bounds are is a number:
|
2026-10-02 19:56:30 +02:00
|
|
|
|
`NoiseBoundTests.TightnessReport` prints the mean interval width over the
|
|
|
|
|
|
sampled range: 5.08 for `iv_snoise_ball` at `r = 0.05`, 1.96 at 0.25 and 1.28
|
|
|
|
|
|
at 1.0; 6.64 and 2.82 for 4-octave `iv_fbm_ball` at 0.05 and 0.25.
|
|
|
|
|
|
|
2026-10-03 00:08:35 +02:00
|
|
|
|
The saving is measured where there is empty space to find.
|
|
|
|
|
|
`NoiseBoundTests.IntervalSkipHalvesEvaluations` draws screen tiles, each with
|
|
|
|
|
|
its own camera, and runs one pre-pass per tile over the dense grid's cell ranges:
|
|
|
|
|
|
it starts with the whole grid, skips and doubles a range it proves empty, halves
|
2026-10-03 13:58:10 +02:00
|
|
|
|
one it cannot, and runs to the grid's end. It then draws the tile's `N^2` ray
|
|
|
|
|
|
slopes, one per pixel, jittered inside it. Each ray is marched three times with
|
|
|
|
|
|
the same integrator and the same early-out at transmittance 0.02: over every
|
|
|
|
|
|
cell (dense), over the tile's unproven cells (the tile march), and over the
|
|
|
|
|
|
oracle mask, the cells whose mid-depth density is nonzero for at least one of
|
|
|
|
|
|
the tile's rays. The tile march and the oracle march integrate the dense march's
|
|
|
|
|
|
nonzero cells, so their transmittance agrees with it exactly in every
|
|
|
|
|
|
configuration. The oracle is a ceiling the pre-pass cannot move: dense cost over
|
|
|
|
|
|
oracle cost. Against it the test prints the efficiency (oracle cost over the
|
|
|
|
|
|
tile march's cost with its probes) and the cull fraction (the oracle-empty cells
|
|
|
|
|
|
the pre-pass proves empty). The probe overhead is the tile march's cost over that
|
|
|
|
|
|
cost plus the probes'. Cost is counted in `snoise` evaluations, with an envelope
|
2026-10-03 00:08:35 +02:00
|
|
|
|
evaluation or probe weighted 0.2. That weight is an estimate, and both counts are
|
2026-10-03 13:58:10 +02:00
|
|
|
|
printed. The flow is not counted. At `N = 8`, in Aetheria's units on its
|
|
|
|
|
|
256-cell quadratic grid out to the Main Camera's far plane, 2048:
|
2026-10-03 00:08:35 +02:00
|
|
|
|
|
|
|
|
|
|
- (a) Height fog, camera above the safety band, rays level and up: the whole
|
|
|
|
|
|
grid is one envelope probe per tile, against 256 envelope evaluations per ray,
|
2026-10-03 13:58:10 +02:00
|
|
|
|
16384x. Every cell is oracle-empty and the pre-pass proves them all.
|
2026-10-04 09:33:28 +02:00
|
|
|
|
- (b) Inside the fog: 1.16x, and 1.11x with the warp. Most cells are fog, and
|
2026-10-03 13:58:10 +02:00
|
|
|
|
the pre-pass probes each one.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
- (c) The deep well (ruling `wells-scale-order-of-magnitude`): the zone bowl
|
|
|
|
|
|
plus one well at its centre, `PowerPulse` exponent 2, scale 2220, depth 600.
|
|
|
|
|
|
The band `G = 60` is where only the noise decides density (`s` within
|
|
|
|
|
|
`F -+ 1.5 A` with the fade at 1); depth and half-depth radius are each
|
|
|
|
|
|
`10 G`. The camera's `xz` is uniform within 300 of the centre, at
|
|
|
|
|
|
`y = F - h(xz) / 2`, so it looks across the bowl from inside it. Both rows run
|
2026-10-04 07:37:25 +02:00
|
|
|
|
on the same seeds. With no warp, the centred interval takes 3.20x (oracle
|
|
|
|
|
|
ceiling 5.24x, efficiency 0.611, cull fraction 0.988, probe overhead 0.940)
|
2026-10-04 12:23:02 +02:00
|
|
|
|
and the affine bound 3.57x (efficiency 0.682, cull 0.993, overhead 0.905). With
|
2026-10-04 07:37:25 +02:00
|
|
|
|
the warp `D = 60`: 3.12x (ceiling 5.31x, efficiency 0.588, cull 0.987, overhead
|
2026-10-04 12:23:02 +02:00
|
|
|
|
0.929) against 3.46x (efficiency 0.651, cull 0.993, overhead 0.880), so affine
|
|
|
|
|
|
is 1.12x and 1.11x cheaper. The range mask (the oracle cells plus every cell
|
2026-10-03 22:21:50 +02:00
|
|
|
|
where any of 5 x 5 slopes x 9 depths has density above zero) gives a range
|
|
|
|
|
|
ceiling of 0.859 (0.848 with the warp), the cost a bound that proves exactly
|
|
|
|
|
|
the range-empty cells would reach against the oracle's; the efficiencies
|
2026-10-04 12:23:02 +02:00
|
|
|
|
against it are 0.794 and 0.712 (warp 0), 0.768 and 0.694 (warp `D`).
|
2026-10-03 22:21:50 +02:00
|
|
|
|
- (c-band) The shallow wells, four mass-derived wells in the zone bowl with
|
|
|
|
|
|
the camera gazing across, whose geometry is band-dominated, printed only.
|
2026-10-04 07:37:25 +02:00
|
|
|
|
Centred interval 3.01x (efficiency 0.414, cull 0.960, overhead 0.958), affine
|
2026-10-04 12:23:02 +02:00
|
|
|
|
3.95x (0.544, 0.980, 0.901); with the warp 2.75x (0.383, 0.954, 0.944) against
|
|
|
|
|
|
3.54x (0.494, 0.976, 0.865).
|
2026-10-04 09:33:28 +02:00
|
|
|
|
- (d) r1's uniform slab, which has no envelope: 1.38x with the warp 0.05, and
|
|
|
|
|
|
1.67x with no warp.
|
2026-10-03 13:58:10 +02:00
|
|
|
|
- (e) The void at the shipped point (ruling `operator-shipped-void`): `Rh = 198`,
|
|
|
|
|
|
`S = 50`, `Rc = Rh + S = 248`, `e = -ln 1.5 / ln(1 - (Rh / Rc)^2)`, the
|
|
|
|
|
|
shipped camera, 200 tiles. The fixed-step reference takes 36.34 steps per
|
2026-10-04 09:33:28 +02:00
|
|
|
|
pixel. The same cells masked by the pre-pass take 20.08, with identical
|
|
|
|
|
|
transmittance. The footprint-aware march takes 13.50: 2.69x fewer. `snoise`
|
|
|
|
|
|
per pixel goes from 32.98 to 26.70, and combined cost from 40.25 to 29.54 with
|
2026-10-03 13:58:10 +02:00
|
|
|
|
the probes, 1.36x. Every one of the 200 tiles proves a body. Before the body
|
|
|
|
|
|
both marches are measured against a converged one (midpoint quadrature at
|
|
|
|
|
|
`h = 0.5` over every cell, trusting no pre-pass): the relative optical-depth
|
|
|
|
|
|
error at the body start is at most 0.0608 for the fixed-step march and 0.08639
|
|
|
|
|
|
for the footprint march, 1.42x. Over the grid of hollow radius, camera offset
|
2026-10-04 09:33:28 +02:00
|
|
|
|
and ramp width the steps ratio runs from 1.59x (`Rh = 100`, low, `S = 150`) to
|
2026-10-03 13:58:10 +02:00
|
|
|
|
27.45x (`Rh = 1000`, centred, `S = 10`), the cost ratio from 1.26x to 3.89x,
|
|
|
|
|
|
and the depth-error ratio from 0.85 to 4.72 (`Rh = 200`, centred, `S = 150`,
|
|
|
|
|
|
where the errors are 0.01504 and 0.003188). LOD-far takes 14.19x fewer steps;
|
|
|
|
|
|
its depth errors, 0.2849 and 0.205, are not comparable, because its
|
|
|
|
|
|
fixed-step reference keeps the octaves the footprint march truncates.
|
|
|
|
|
|
|
|
|
|
|
|
The analytic body (ruling `operator-analytic-wall-body`). When a single dense
|
|
|
|
|
|
cell's own bound is a positive point, the pre-pass spends one more probe over
|
|
|
|
|
|
the rest of the grid. If that bound is a positive point too, every point of
|
|
|
|
|
|
every ray of the tile from there on has exactly that density, so the footprint
|
|
|
|
|
|
march takes one closed-form step, `exp(-BodyDepth)`, from the body start to the
|
|
|
|
|
|
grid's end and stops. `BodyDepth` is density x extinction x length x `|dir|`.
|
|
|
|
|
|
`NoiseBoundTests.AnalyticBodyMatchesFineMarch` pins it: the bound, the mask and
|
|
|
|
|
|
the pointwise densities agree exactly at the body, and `BodyDepth` equals the
|
|
|
|
|
|
summed fixed-step depth of the body cells within 1e-4 relative. The comparison
|
|
|
|
|
|
is in the depth domain, so LOD-far's deep bodies do not underflow.
|
|
|
|
|
|
|
|
|
|
|
|
The footprint-truncated void weights its octaves by `w_i(z)`, decreasing in `z`.
|
|
|
|
|
|
Its bound takes each weight over `[w_i(z1), 1]` rather than
|
|
|
|
|
|
`[w_i(z1), w_i(z0)]`, because one bound serves both the full field (`w = 1`) and
|
|
|
|
|
|
the truncated one. `[w_i(z1), 1]` contains both, and `iv_mul` is monotone in its
|
|
|
|
|
|
interval argument, so the composed interval encloses both fields.
|
|
|
|
|
|
`NoiseBoundTests.WeightBoundEnclosesOneSignedNoise` checks it on LOD-far over
|
|
|
|
|
|
2,000 segments: each weight lies in its bound, and `w(z) n` lies in
|
|
|
|
|
|
`iv_mul(bound, n)` for a one-signed `n`. A lower end of `w(z0)` fails it.
|
|
|
|
|
|
|
|
|
|
|
|
The contracts, at `N = 8`, the lower of warp 0 and `D`:
|
|
|
|
|
|
|
|
|
|
|
|
- (a) at least 2x combined cost;
|
2026-10-03 22:21:50 +02:00
|
|
|
|
- (c) on the deep well (rulings `affine-wells-contract-cheaper-than-interval`
|
|
|
|
|
|
and `wells-overhead-plus-floor`): the affine bound's combined cost ratio is
|
|
|
|
|
|
above the centred interval's at warp 0 and at warp `D`; and, hard, the
|
|
|
|
|
|
pre-pass proves at least half of the oracle-empty cells empty under both
|
|
|
|
|
|
bounds, so a pre-pass that skips nothing fails. The probe overhead, the
|
|
|
|
|
|
efficiencies against the oracle and the range ceiling, the ceilings and the
|
|
|
|
|
|
cull fraction are printed, not asserted; (c-band) is printed only;
|
2026-10-03 13:58:10 +02:00
|
|
|
|
- (e) at the shipped void: the masked fixed-step march agrees with the reference
|
|
|
|
|
|
exactly; the footprint march's optical depth at the body start is off the
|
|
|
|
|
|
converged march's by at most twice the fixed-step march's error; and it takes
|
|
|
|
|
|
at most half the reference's steps per pixel.
|
|
|
|
|
|
|
2026-10-04 12:23:02 +02:00
|
|
|
|
This run meets every contract: (a) 16384x, (c) affine 3.57x and 3.46x against
|
2026-10-04 07:37:25 +02:00
|
|
|
|
the centred interval's 3.20x and 3.12x, with a cull fraction of 0.987 at the
|
2026-10-04 09:33:28 +02:00
|
|
|
|
lowest, (e) 2.69x fewer steps and a depth error 1.42x the fixed-step march's.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
The fields, the grids, the 0.2 envelope weight and the 2.0 gradient weight are
|
|
|
|
|
|
not tuned toward the contracts. An affine probe is one `snoise_grad`, counted at
|
|
|
|
|
|
2.0 `snoise` evaluations (an estimate, printed beside the count).
|
2026-10-02 19:56:30 +02:00
|
|
|
|
|
|
|
|
|
|
Consumers: the gamecult.org ground shader (through the GLSL lowering) and
|
|
|
|
|
|
Aetheria's nebula raymarch at its CultMath pin bump.
|
|
|
|
|
|
|
2026-10-03 22:21:50 +02:00
|
|
|
|
## Affine forms
|
|
|
|
|
|
|
|
|
|
|
|
`math.Affine.cs` carries reduced affine forms with one shared symbol: a
|
|
|
|
|
|
`float3(x0, a, e)` is the set `x0 + a eps + e delta` with `eps` and `delta` in
|
|
|
|
|
|
`[-1, 1]` and `e >= 0` (Messine's AF1, as Gamito and Maddock use it for gradient
|
|
|
|
|
|
noise, in one register as an interval is a `float2`). `eps` is shared by every
|
|
|
|
|
|
form over one region (for a screen-tile slice, the depth parameter, `z = z_m +
|
|
|
|
|
|
h eps`), so `af_sub(x, x)` has `a = 0`; `delta` is private to the form, and
|
|
|
|
|
|
every term that cannot be tracked is condensed into `e`. A slice is a
|
|
|
|
|
|
`float4(centre, radius)` with a `float3` axis. No op takes a compound type, a
|
|
|
|
|
|
reference parameter or a swizzle write, so each mirrors into the HLSL common
|
|
|
|
|
|
subset.
|
|
|
|
|
|
|
|
|
|
|
|
The ops: `af_point`, `af_symbol`, `af_from_iv`, `af_range` (the bridge back to
|
|
|
|
|
|
`iv_*`), `af_add`, `af_sub`, `af_neg`, `af_scale`, `af_add_iv`, `af_mul` (the
|
|
|
|
|
|
product `|a_x a_y|` and the cross-error terms condensed into `e`, so there is
|
|
|
|
|
|
no `af_sqr`), `af_snoise` and `af_fbm`, and the slice pair `af_frustum_axis` and
|
|
|
|
|
|
`af_frustum_ball`.
|
|
|
|
|
|
|
|
|
|
|
|
Rounding. Every op returns a form whose set at each `eps` contains the float32
|
|
|
|
|
|
value of its function at every point of its operands' sets at that `eps`. Each
|
|
|
|
|
|
op folds its own float32 rounding into `e` as `S 2^-20`, `S` the sum of the
|
|
|
|
|
|
magnitudes its roundings are relative to (named in the op's comment): at most
|
|
|
|
|
|
`8u S` is needed with `u = 2^-24`, and the factor of two over that is the
|
|
|
|
|
|
allowance for a device whose `+` and `*` are faithful rather than correctly
|
|
|
|
|
|
rounded. `AffineTests.EveryOpEnclosesItsPoints` evaluates the form's set in
|
2026-10-04 03:19:06 +02:00
|
|
|
|
double and the function in float32 with no tolerance. Products and `af_from_iv` also
|
|
|
|
|
|
add `2^-126`, the most an underflow to a subnormal can lose (`TinyOperandsEncloseTheirPoints`).
|
|
|
|
|
|
`af_from_iv` halves its endpoints before it sums and differences them, and `af_range`
|
|
|
|
|
|
scales its terms before it sums them, so endpoints near the float maximum do not overflow
|
|
|
|
|
|
in the middle of the op; a form that overflows anyway gives `af_range` infinite endpoints,
|
2026-10-04 11:09:25 +02:00
|
|
|
|
never NaN (`FromIntervalAndRangeHoldAtTheExtremes`). `af_range` finds a NaN endpoint by its bit pattern, because
|
|
|
|
|
|
`lo == lo` is a compare dxc marks `fast` and deletes under its default flags. `AbsorptionsArePinned` and
|
2026-10-04 03:19:06 +02:00
|
|
|
|
`SnoiseFormCarriesItsDocumentedWidth` pin each absorption and `af_snoise`'s float32 allowance both
|
|
|
|
|
|
ways: the enclosure tests alone cannot see them on a CPU, where float32 is exact.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
|
|
|
|
|
|
`af_snoise(centre, axis, radius)` encloses `snoise` over `centre + axis eps + w`,
|
|
|
|
|
|
`|w| <= radius`, from one `snoise_grad`. It returns the narrower of the
|
|
|
|
|
|
Lipschitz form `(n, 0, L R)` and the centred form `(n, g . axis, |g| radius +
|
|
|
|
|
|
M R^2 / 2)` (`R = |axis| + radius`, `L = SNOISE_LIPSCHITZ`, `M =
|
|
|
|
|
|
SNOISE_HESSIAN`), so its total width is never more than the Lipschitz form's.
|
|
|
|
|
|
The centred form is Taylor's theorem, which is why it beats the ball when the
|
|
|
|
|
|
radius is small and the axis carries the variation. `af_fbm` compounds octaves
|
|
|
|
|
|
as `iv_fbm_ball` does. The float32 evaluation allowance is derived in
|
|
|
|
|
|
`af_snoise`'s comment.
|
|
|
|
|
|
|
2026-10-14 13:36:15 +02:00
|
|
|
|
`SNOISE_HESSIAN = 55.642143` is 1.10 times the refined maximum of the spectral
|
2026-10-03 22:21:50 +02:00
|
|
|
|
norm of the Hessian of `snoise(float3)`, measured by
|
2026-10-14 13:36:15 +02:00
|
|
|
|
`NoiseBoundTests.MeasureHessian` over 1e6 seeded points (the analytic
|
|
|
|
|
|
`snoise_hessian`, below) and refined by ascent from the 1e4 largest: refined
|
|
|
|
|
|
maximum 50.5838. The central differences of `snoise_grad` that an earlier pin
|
|
|
|
|
|
used read 50.955 at h = 1e-3, their roundoff.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
`HessianConstantPinsSampledCurvature` fails on a 1% move.
|
2026-10-14 13:36:15 +02:00
|
|
|
|
`snoise_hessian(float3)` (`math.Span.cs`) is the analytic Hessian of `snoise`: a
|
|
|
|
|
|
symmetric `float3x3` on the same lattice, hash and tie order as `snoise_grad`,
|
|
|
|
|
|
differentiating each corner's gradient `-8 m^3 s x + m^4 p` once more. Each
|
|
|
|
|
|
off-diagonal entry is computed once and placed twice, so the matrix equals its
|
|
|
|
|
|
transpose bit for bit, and a clamped corner (`m = 0`) contributes nothing. Built
|
|
|
|
|
|
from nine scalars, the matrix reads as columns in GLSL (`mat3`) and rows in HLSL,
|
|
|
|
|
|
which is the same matrix because it is symmetric. `SNOISE_THIRD = 536.9152`
|
|
|
|
|
|
(`math.Span.cs`) is the next order: 1.10 times the maximum, over 1e6 seeded points,
|
|
|
|
|
|
of the operator norm of the third-derivative tensor of `snoise` (central
|
|
|
|
|
|
differences of `snoise_hessian` at h = 1e-3, then an ascent over unit vectors),
|
|
|
|
|
|
refined from the 1e4 largest; measured 488.1047 on the tied kernel.
|
|
|
|
|
|
`MeasureThird` takes the measurement and `ThirdConstantPinsSampled` pins it.
|
|
|
|
|
|
`integral_clamp_linear(a, b, lo, hi, t0, t1)` is the closed-form integral over
|
|
|
|
|
|
`[t0, t1]` of `clamp(a + b t, lo, hi)`. All three have HLSL and GLSL lowerings and
|
|
|
|
|
|
`glsl-parity.json` fixture families (`snoise_hessian`, `integral_clamp_linear`; the
|
|
|
|
|
|
`af_snoise` and `af_fbm` families are regenerated for the re-pin).
|
2026-10-03 22:21:50 +02:00
|
|
|
|
`SNOISE2_LIPSCHITZ = 8.117002` is the same for the gradient of `snoise(float2)`
|
|
|
|
|
|
(`MeasureLipschitz2`: sampled 7.2575, refined 7.3791, witness (-67.12454,
|
|
|
|
|
|
-129.53296), `|snoise2|` at most 0.99964), and it backs `iv_snoise_ball(float2,
|
2026-10-04 02:55:32 +02:00
|
|
|
|
float)`. Both are empirical with a margin, not proofs. A random ball cannot see a
|
|
|
|
|
|
curvature constant 20% low, so `SNOISE_HESSIAN` is defended by
|
|
|
|
|
|
`HessianConstantPinsSampledCurvature` and `SnoiseFormEnclosesTheCurvatureWitness`
|
|
|
|
|
|
(`af_snoise` at the point where the curvature is realised, along 4,000 directions: a
|
|
|
|
|
|
constant of 40 misses there). The margin over the function itself: a scan of the whole
|
|
|
|
|
|
289^3 hash period found a realised maximum of 50.58, and bounding each corner's gradient
|
2026-10-14 13:36:15 +02:00
|
|
|
|
independently over every hash configuration gives 52.22, so 55.64 sits 7% above the
|
2026-10-04 02:55:32 +02:00
|
|
|
|
configuration bound. A change to the kernels must re-run the measurements.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
|
|
|
|
|
|
The slice. `af_frustum_ball(centreSlope, z0, z1, footprintPerDepth,
|
|
|
|
|
|
warpVariation)` returns the centre `(m_c z_m, z_m)` and a radius `z1
|
|
|
|
|
|
footprintPerDepth + warpVariation`, with `af_frustum_axis` the axis `(m_c, 1) h`:
|
|
|
|
|
|
every ray point at depth `z` is `centre + axis eps(z) + w`. Its derivation
|
|
|
|
|
|
and the clamp residual are in the comment. The caller rotates, translates and
|
|
|
|
|
|
scales the slice into the noise's frame, and owns the rounding of that.
|
|
|
|
|
|
|
|
|
|
|
|
The centred warp. A flow-warped sample reads `snoise` at `p + flow(p) shift`. The
|
|
|
|
|
|
caller does not enlarge the ball by the whole displacement `|flow| shift`; it
|
|
|
|
|
|
moves the centre by the flow at the centre, `c + flow(c) shift`, and widens the
|
|
|
|
|
|
radius by the flow's variation across the ball, `r + |shift| rho` with `rho =
|
2026-10-04 02:55:49 +02:00
|
|
|
|
FlowAmplitude (SNOISE2_LIPSCHITZ FlowFrequency r + a) sqrt 2` (the clamp to the unit
|
|
|
|
|
|
disc is nonexpansive), where `a = 2^-14 + L2 2^-20 (|q|_1 + r FlowFrequency)` is the
|
|
|
|
|
|
float32 allowance of the flow's two `snoise(float2)` reads (the point `q` plus its offset rounds at the
|
|
|
|
|
|
magnitude of `q`, and a float32 flow at two points of a ball differs by more than the exact
|
|
|
|
|
|
function can). CultMath owns neither the flow nor the envelope: the warp's
|
2026-10-03 22:21:50 +02:00
|
|
|
|
centre and variation are the caller's, and a consumer adds them to the slice's
|
|
|
|
|
|
`warpVariation` or to the form's radius. With two phases that cross-fade, each
|
|
|
|
|
|
phase takes its own centre; one ball serves when the phases' centres coincide
|
2026-10-04 02:55:49 +02:00
|
|
|
|
(warp 0). `NoiseBoundTests.CentredWarpStaysEnclosed` checks 2,000 balls, radius 1e-3 to
|
|
|
|
|
|
500, and fails when `rho` is dropped or halved; `AffineWarpStaysEnclosed` checks
|
2026-10-04 07:37:35 +02:00
|
|
|
|
3,000 tile slices of `FogField.Slice`, which returns the final `af_snoise` arguments
|
|
|
|
|
|
(each phase's moved centre and grown radius, the detail's unwarped set, the shared axis),
|
|
|
|
|
|
so the affine bound wires nothing itself: the reach over which the flow is varied is
|
|
|
|
|
|
`|axis| + radius`, each phase carries its own growth, and the gate reads each argument's
|
|
|
|
|
|
reach. It fails when the reach is the ball's radius or the larger of the two, a phase's
|
|
|
|
|
|
growth is dropped or taken from the other phase, the axis or the detail radius is scaled,
|
|
|
|
|
|
or `rho` is halved. `AffineCompositionTightensItsRanges` sees the shared-symbol
|
|
|
|
|
|
composition itself: the bound is exactly the intersection of the affine arithmetic's piece
|
|
|
|
|
|
and the interval composition over the forms' ranges, and the affine piece is the narrower
|
|
|
|
|
|
of the two in at least 35% of the open slices (the piece alone is wider in total).
|
2026-10-03 22:21:50 +02:00
|
|
|
|
|
|
|
|
|
|
The deep well is the representative Aetheria case: the camera inside a sun's
|
|
|
|
|
|
bowl, looking across. `DeepWellIsTenBandsDeepAndWide` computes the band `G` from
|
|
|
|
|
|
`FogField`'s own floor offset, amplitude and noise range (60) and requires the
|
|
|
|
|
|
depth (633 at the centre against the rim) and the half-depth radius to be at
|
|
|
|
|
|
least `10 G`, every drawn camera sitting inside the well below the rim; a well of
|
|
|
|
|
|
depth 150 fails. The shallow wells stay as the printed shallow-well, band-dominated row (c-band).
|
|
|
|
|
|
|
|
|
|
|
|
The test field's affine bound takes depth as `af_symbol(z_m, h)`, builds `s`
|
|
|
|
|
|
affine in it with the height interval, the footprint and the slope-depth
|
|
|
|
|
|
product in `e` through `af_add_iv`, takes the noise through `af_frustum_*` and
|
|
|
|
|
|
`af_snoise` per phase and for the detail octave, fades by `af_from_iv` and
|
|
|
|
|
|
`af_mul`, reads the range with `af_range`, and intersects with the centred
|
|
|
|
|
|
interval's bound (both sound). `FogField.Bound` makes no direct `snoise` call.
|
|
|
|
|
|
The cull-map render (`DeepWellCullMap`, explicit) writes a PNG when
|
|
|
|
|
|
`CULTMATH_WRITE_CULLMAP` names a directory: one deep-well camera at warp `D`,
|
|
|
|
|
|
960 x 540 in 8 x 8 tiles, with panels for dense cells per ray under the centred
|
|
|
|
|
|
interval, under the affine bound and under the oracle mask (one logarithmic
|
2026-10-04 11:14:18 +02:00
|
|
|
|
scale) and the transmittance. Its run: 4.23, 3.60 and 2.02 cells per ray on
|
|
|
|
|
|
average, 44 the most, mean transmittance 0.278.
|
2026-10-03 22:21:50 +02:00
|
|
|
|
|
2026-10-14 06:05:19 +02:00
|
|
|
|
## Expressions
|
|
|
|
|
|
|
|
|
|
|
|
`CultMath.Expr` holds a scalar SSA expression graph as a persisted value, a builder
|
|
|
|
|
|
for it, and a C# evaluator.
|
|
|
|
|
|
|
|
|
|
|
|
- **Graph (`ExprGraph`)**: a sealed immutable class. It has no name of its own; the
|
|
|
|
|
|
consumer record that holds it names it. Nodes are in topological order, an operand
|
|
|
|
|
|
names a lower node, and an edit builds a new graph. A persisted graph carries a
|
|
|
|
|
|
format version (1); one naming an unknown op, a newer format or a forward operand is
|
|
|
|
|
|
refused whole at decode. `ExprGraph.Create` is the one validator, run by the builder
|
|
|
|
|
|
and by decode.
|
|
|
|
|
|
- **Node**: its index in the topological order. The builder hash-conses identical
|
|
|
|
|
|
(op, operands, constant bits) nodes and drops nodes no output reaches, so one
|
|
|
|
|
|
expression always builds the same bytes; any rebuild may renumber nodes.
|
|
|
|
|
|
- **Port**: graph inputs vary per point, params are uniform per batch and carry a
|
|
|
|
|
|
default, outputs name a node. Names are `[A-Za-z_][A-Za-z0-9_]*`, unique per kind.
|
|
|
|
|
|
An op's operands are positional; a multi-result op (`cellular3`: nearest.xyzw,
|
|
|
|
|
|
edge.xyzw, id) is read only through `Pick`.
|
|
|
|
|
|
- **Op vocabulary**: one table (`ExprOps`), a stable string per op, append-only.
|
|
|
|
|
|
Vector arguments are decomposed operands: `snoise2`, `snoise3`, `cellular3`. An op
|
|
|
|
|
|
calls its CultMath function; a function's value change is a Breaking release note.
|
|
|
|
|
|
- **Evaluation**: `ExprEvaluator.Evaluate` runs the nodes in order through the table,
|
|
|
|
|
|
one IEEE single per op, so a result equals the direct composition of the same math
|
|
|
|
|
|
calls bit for bit. Params bind by name (`ExprBindings`): a missing name takes its
|
|
|
|
|
|
default and an unknown name is refused.
|
|
|
|
|
|
- **Stencils** are substitution at build time (`ExprBuilder.Substitute`, Fidget's
|
|
|
|
|
|
`remap_xyz`): N substitutions of the position inputs combined by ops give an ordinary
|
|
|
|
|
|
flat graph. There is no loop construct.
|
|
|
|
|
|
|
|
|
|
|
|
The MessagePack shape belongs to `GameCult.Caching.MessagePack` (`CultMathExprResolver`).
|
|
|
|
|
|
Interval, affine and gradient evaluation over the graph, bytecode, the HLSL
|
|
|
|
|
|
interpreter and pruning are not built.
|
|
|
|
|
|
|
2026-05-08 01:49:44 +01:00
|
|
|
|
## Rules
|
|
|
|
|
|
|
2026-09-14 13:30:00 +02:00
|
|
|
|
- Keep type names HLSL-shaped: `float2`, `float3`, `float4`, `float3x3`, and `math`.
|
2026-06-03 02:58:56 +01:00
|
|
|
|
- Keep Rust as the owner of native hot-path kernels and parity fixtures.
|
|
|
|
|
|
- Keep C#, shader, and scripting surfaces as runtime wrappers, not competing
|
|
|
|
|
|
math authorities.
|
2026-06-10 21:22:43 +01:00
|
|
|
|
- Keep `shaders/CultMath.hlsl` as the canonical shader mirror for shared
|
2026-09-14 13:30:00 +02:00
|
|
|
|
CultMath functions HLSL does not already provide (HLSL intrinsics such as
|
|
|
|
|
|
`mul`, `any`, and `log` need no mirror). Project-local shader helpers should
|
|
|
|
|
|
move here once more than one organ needs them or once CPU/GPU parity matters.
|
2026-05-08 01:49:44 +01:00
|
|
|
|
- Implement public shader semantics directly; do not copy Unity.Mathematics.
|
2026-09-14 13:30:00 +02:00
|
|
|
|
- Prefer plain mutable value types with public component fields, as HLSL does.
|
2026-05-08 01:49:44 +01:00
|
|
|
|
- Prefer component-wise overloads over clever generic machinery until the API
|
|
|
|
|
|
earns that complexity.
|
2026-09-14 13:30:00 +02:00
|
|
|
|
- Keep the surface boring and exact. Fill what a consumer uses, guided by HLSL
|
|
|
|
|
|
semantics; noise primitives, matrices, quaternions, and packing helpers need an
|
|
|
|
|
|
owner before they enter.
|
|
|
|
|
|
- Keep the core assembly engine-free. Unity conversions live in the Unity
|
|
|
|
|
|
package's `CultMath.UnityBridge` assembly.
|
2026-05-08 01:49:44 +01:00
|
|
|
|
|
|
|
|
|
|
## Non-Goals
|
|
|
|
|
|
|
|
|
|
|
|
- Compiling C# to shaders.
|
2026-06-10 21:22:43 +01:00
|
|
|
|
- Generating HLSL from Rust or Rust from HLSL.
|
2026-05-08 01:49:44 +01:00
|
|
|
|
- Replacing `System.Numerics` for general .NET work.
|
|
|
|
|
|
- Recreating every Unity.Mathematics type because a spreadsheet somewhere says
|
|
|
|
|
|
“coverage.” That way lies decorative obesity.
|