From bbf52697515274736b093220e0f90bf91c615433 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 2 Oct 2026 19:52:33 +0200 Subject: [PATCH] CultMath intervals: cultmath_iv_* over float2(lo, hi) in C# and HLSL, with snoise and fbm ball bounds math.Interval.cs and shaders/CultMath.Interval.hlsl (included from CultMath.hlsl after Phacelle; both tracked copies) add iv_point, iv_add, iv_sub, iv_neg, iv_mul, iv_scale, iv_abs, iv_min, iv_max, iv_sqr, iv_sqrt, iv_exp, iv_clamp, iv_saturate, iv_smoothstep, iv_lerp, iv_snoise_ball and iv_fbm_ball, and SNOISE_LIPSCHITZ = 10.099261 (1.10 x the refined max |snoise_grad| that NoiseBoundTests.MeasureLipschitz prints). iv_exp and iv_smoothstep widen by one ulp through asfloat(uint), added beside asuint as the HLSL intrinsic. The mirror test now compares 52 functions; the Unity package and the NuGet content files carry the new include. Co-Authored-By: Claude Opus 5.5 --- .../cultmath/scripts/build-unity-package.ps1 | 4 +- .../cultmath/shaders/CultMath.Interval.hlsl | 107 ++++++++++++ .../shaders/CultMath.Interval.hlsl.meta | 7 + packages/cultmath/shaders/CultMath.hlsl | 1 + .../cultmath/src/CultMath/CultMath.csproj | 1 + .../cultmath/src/CultMath/math.Interval.cs | 165 ++++++++++++++++++ .../src/CultMath/math.Interval.cs.meta | 2 + packages/cultmath/src/CultMath/math.cs | 3 + .../tests/CultMath.Tests/HlslMirrorTests.cs | 1 + .../HlslSourceCompatibilityTests.cs | 4 +- .../Shaders/CultMath.Interval.hlsl | 107 ++++++++++++ .../Shaders/CultMath.Interval.hlsl.meta | 7 + .../Shaders/CultMath.hlsl | 1 + 13 files changed, 408 insertions(+), 2 deletions(-) create mode 100644 packages/cultmath/shaders/CultMath.Interval.hlsl create mode 100644 packages/cultmath/shaders/CultMath.Interval.hlsl.meta create mode 100644 packages/cultmath/src/CultMath/math.Interval.cs create mode 100644 packages/cultmath/src/CultMath/math.Interval.cs.meta create mode 100644 packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl create mode 100644 packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl.meta diff --git a/packages/cultmath/scripts/build-unity-package.ps1 b/packages/cultmath/scripts/build-unity-package.ps1 index 228d735c..2209ec66 100644 --- a/packages/cultmath/scripts/build-unity-package.ps1 +++ b/packages/cultmath/scripts/build-unity-package.ps1 @@ -80,7 +80,9 @@ $requiredTemplateFiles = @( "Shaders\CultMath.hlsl", "Shaders\CultMath.hlsl.meta", "Shaders\CultMath.Phacelle.hlsl", - "Shaders\CultMath.Phacelle.hlsl.meta" + "Shaders\CultMath.Phacelle.hlsl.meta", + "Shaders\CultMath.Interval.hlsl", + "Shaders\CultMath.Interval.hlsl.meta" ) foreach ($relativePath in $requiredTemplateFiles) { if (-not (Test-Path -LiteralPath (Join-Path $templateRoot $relativePath) -PathType Leaf)) { diff --git a/packages/cultmath/shaders/CultMath.Interval.hlsl b/packages/cultmath/shaders/CultMath.Interval.hlsl new file mode 100644 index 00000000..ed6f83db --- /dev/null +++ b/packages/cultmath/shaders/CultMath.Interval.hlsl @@ -0,0 +1,107 @@ +#ifndef CULTMATH_INTERVAL_HLSL +#define CULTMATH_INTERVAL_HLSL + +// Interval arithmetic over float2(lo, hi), lo <= hi, both finite. Every cultmath_iv_* function returns +// an interval containing f(x) for every x in its input intervals, mirroring math.Interval.cs bit for +// bit; see that file and docs/design.md ("Intervals") for each op's enclosure and its ulp widening. +// Included by CultMath.hlsl after its own definitions of cultmath_smoothstep and cultmath_snoise. + +// Lipschitz constant of cultmath_snoise(float3); provenance in math.Interval.cs (SNOISE_LIPSCHITZ). +static const float CULTMATH_SNOISE_LIPSCHITZ = 10.099261; + +float2 cultmath_iv_point(float value) { return float2(value, value); } +float2 cultmath_iv_add(float2 a, float2 b) { return float2(a.x + b.x, a.y + b.y); } +float2 cultmath_iv_sub(float2 a, float2 b) { return float2(a.x - b.y, a.y - b.x); } +float2 cultmath_iv_neg(float2 a) { return float2(-a.y, -a.x); } + +float2 cultmath_iv_mul(float2 a, float2 b) +{ + float p0 = a.x * b.x; + float p1 = a.x * b.y; + float p2 = a.y * b.x; + float p3 = a.y * b.y; + return float2(min(min(p0, p1), min(p2, p3)), max(max(p0, p1), max(p2, p3))); +} + +float2 cultmath_iv_scale(float2 a, float s) { return s >= 0.0 ? float2(a.x * s, a.y * s) : float2(a.y * s, a.x * s); } + +float2 cultmath_iv_abs(float2 a) +{ + if (a.x >= 0.0) + return a; + if (a.y <= 0.0) + return float2(-a.y, -a.x); + return float2(0.0, max(-a.x, a.y)); +} + +float2 cultmath_iv_min(float2 a, float2 b) { return float2(min(a.x, b.x), min(a.y, b.y)); } +float2 cultmath_iv_max(float2 a, float2 b) { return float2(max(a.x, b.x), max(a.y, b.y)); } + +float2 cultmath_iv_sqr(float2 a) +{ + float l = a.x * a.x; + float h = a.y * a.y; + if (a.x >= 0.0) + return float2(l, h); + if (a.y <= 0.0) + return float2(h, l); + return float2(0.0, max(l, h)); +} + +float2 cultmath_iv_sqrt(float2 a) { return float2(sqrt(max(a.x, 0.0)), sqrt(max(a.y, 0.0))); } + +// Widened by one ulp on each side: exp is not required to be correctly rounded. +float2 cultmath_iv_exp(float2 a) +{ + float lo = exp(a.x); + float hi = exp(a.y); + return float2(lo > 0.0 ? asfloat(asuint(lo) - 1u) : 0.0, asfloat(asuint(hi) + 1u)); +} + +float2 cultmath_iv_clamp(float2 a, float minimum, float maximum) +{ + return float2(cultmath_clamp(a.x, minimum, maximum), cultmath_clamp(a.y, minimum, maximum)); +} + +float2 cultmath_iv_saturate(float2 a) { return float2(cultmath_saturate(a.x), cultmath_saturate(a.y)); } + +// The endpoints, ordered, widened by one ulp inside [0, 1]: the float32 evaluation of smoothstep is +// monotone only to within one ulp. +float2 cultmath_iv_smoothstep(float minimum, float maximum, float2 a) +{ + float s0 = cultmath_smoothstep(minimum, maximum, a.x); + float s1 = cultmath_smoothstep(minimum, maximum, a.y); + float lo = min(s0, s1); + float hi = max(s0, s1); + return float2(lo > 0.0 ? asfloat(asuint(lo) - 1u) : 0.0, hi < 1.0 ? asfloat(asuint(hi) + 1u) : 1.0); +} + +// The natural interval extension of x + (y - x) * amount, which is how cultmath_lerp evaluates. +float2 cultmath_iv_lerp(float2 a, float2 b, float amount) { return cultmath_iv_add(a, cultmath_iv_scale(cultmath_iv_sub(b, a), amount)); } + +// [n - L r, n + L r] intersected with [-1, 1], n = cultmath_snoise(centre). One snoise, value only. +float2 cultmath_iv_snoise_ball(float3 centre, float radius) +{ + float n = cultmath_snoise(centre); + float e = CULTMATH_SNOISE_LIPSCHITZ * radius; + return float2(max(n - e, -1.0), min(n + e, 1.0)); +} + +// Octave sum of cultmath_iv_snoise_ball, compounding frequency and amplitude as cultmath_fbm_grad does. +float2 cultmath_iv_fbm_ball(float3 centre, float radius, int octaves, float lacunarity, float gain) +{ + octaves = clamp(octaves, 0, 16); + float amplitude = 1.0; + float frequency = 1.0; + float2 sum = float2(0.0, 0.0); + for (int i = 0; i < octaves; i++) + { + sum = cultmath_iv_add(sum, cultmath_iv_scale(cultmath_iv_snoise_ball(centre * frequency, radius * abs(frequency)), amplitude)); + frequency *= lacunarity; + amplitude *= gain; + } + + return sum; +} + +#endif diff --git a/packages/cultmath/shaders/CultMath.Interval.hlsl.meta b/packages/cultmath/shaders/CultMath.Interval.hlsl.meta new file mode 100644 index 00000000..a22ec627 --- /dev/null +++ b/packages/cultmath/shaders/CultMath.Interval.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4d8a899561654ac990cab0bf48658cc1 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/packages/cultmath/shaders/CultMath.hlsl b/packages/cultmath/shaders/CultMath.hlsl index af2d3741..da3362a6 100644 --- a/packages/cultmath/shaders/CultMath.hlsl +++ b/packages/cultmath/shaders/CultMath.hlsl @@ -504,6 +504,7 @@ CultCellular cultmath_cellular(float3 p) } #include "CultMath.Phacelle.hlsl" +#include "CultMath.Interval.hlsl" float cultmath_value_noise(float2 position) { diff --git a/packages/cultmath/src/CultMath/CultMath.csproj b/packages/cultmath/src/CultMath/CultMath.csproj index 0b1ee09f..2a82f8db 100644 --- a/packages/cultmath/src/CultMath/CultMath.csproj +++ b/packages/cultmath/src/CultMath/CultMath.csproj @@ -24,6 +24,7 @@ + diff --git a/packages/cultmath/src/CultMath/math.Interval.cs b/packages/cultmath/src/CultMath/math.Interval.cs new file mode 100644 index 00000000..1430e385 --- /dev/null +++ b/packages/cultmath/src/CultMath/math.Interval.cs @@ -0,0 +1,165 @@ +namespace CultMath; + +public static partial class math +{ + // Interval arithmetic over float2(lo, hi), lo <= hi, both finite (design.md, "Intervals"). Every + // iv_* function returns an interval that contains f(x) for every x in its input intervals, where + // f(x) is the same function evaluated pointwise in float32, mirrored by cultmath_iv_* in + // shaders/CultMath.Interval.hlsl. Each bound is computed with the pointwise function's own float32 + // operations; because IEEE round-to-nearest is monotone, an op that is monotone on its region + // encloses its float32 evaluation with no widening. Where float32 evaluation is not monotone, or + // the libm is not required to be, the bound is widened by exactly one ulp, and the doc comment + // says so. There is no empty interval: a caller that needs "provably empty" tests hi < cutoff. + + // Lipschitz constant of snoise(float3) in its input: an upper bound on |snoise_grad(p).xyz|. Provenance: + // NoiseBoundTests.MeasureLipschitz (slow, explicit) takes the largest |snoise_grad| over 1e6 seeded + // points, refines the 1e4 largest by gradient ascent, and multiplies the refined maximum by 1.10; + // NoiseBoundTests.LipschitzConstantPinsSampledGradients pins this value against a fast re-measure. + // Empirical with a margin, not a proof; the enclosure tests are the defence. A change to the + // snoise kernel must re-run MeasureLipschitz and re-pin this constant. + public const float SNOISE_LIPSCHITZ = 10.099261f; + + /// The point interval [x, x]. + public static float2 iv_point(float value) => new(value, value); + + /// Encloses x + y for x in a, y in b. Monotone: no widening. + public static float2 iv_add(float2 a, float2 b) => new(a.x + b.x, a.y + b.y); + + /// Encloses x - y for x in a, y in b. Monotone: no widening. + public static float2 iv_sub(float2 a, float2 b) => new(a.x - b.y, a.y - b.x); + + /// Encloses -x for x in a. Exact. + public static float2 iv_neg(float2 a) => new(-a.y, -a.x); + + /// + /// Encloses x * y for x in a, y in b: the least and greatest of the four corner products. The + /// product is monotone in each factor for a fixed sign of the other, so no widening. + /// + public static float2 iv_mul(float2 a, float2 b) + { + var p0 = a.x * b.x; + var p1 = a.x * b.y; + var p2 = a.y * b.x; + var p3 = a.y * b.y; + return new(min(min(p0, p1), min(p2, p3)), max(max(p0, p1), max(p2, p3))); + } + + /// Encloses x * s for x in a. Monotone: no widening. + public static float2 iv_scale(float2 a, float s) => s >= 0.0f ? new(a.x * s, a.y * s) : new(a.y * s, a.x * s); + + /// Encloses abs(x) for x in a; an interval straddling zero gives [0, max(-lo, hi)]. Exact. + public static float2 iv_abs(float2 a) + { + if (a.x >= 0.0f) + return a; + if (a.y <= 0.0f) + return new(-a.y, -a.x); + return new(0.0f, max(-a.x, a.y)); + } + + /// Encloses min(x, y) for x in a, y in b. Monotone: no widening. + public static float2 iv_min(float2 a, float2 b) => new(min(a.x, b.x), min(a.y, b.y)); + + /// Encloses max(x, y) for x in a, y in b. Monotone: no widening. + public static float2 iv_max(float2 a, float2 b) => new(max(a.x, b.x), max(a.y, b.y)); + + /// + /// Encloses x * x for x in a; an interval straddling zero gives [0, max(lo * lo, hi * hi)]. Tighter + /// than iv_mul(a, a), which cannot know both factors are the same x. Monotone on each side of zero: + /// no widening. + /// + public static float2 iv_sqr(float2 a) + { + var l = a.x * a.x; + var h = a.y * a.y; + if (a.x >= 0.0f) + return new(l, h); + if (a.y <= 0.0f) + return new(h, l); + return new(0.0f, max(l, h)); + } + + /// + /// Encloses sqrt(x) for x in a with x >= 0 (sqrt's domain): lo is clamped at 0, so [-4, 9] gives [0, 3] + /// and an interval wholly below zero gives [0, 0]. sqrt is correctly rounded, hence monotone: no + /// widening. + /// + public static float2 iv_sqrt(float2 a) => new(sqrt(max(a.x, 0.0f)), sqrt(max(a.y, 0.0f))); + + /// + /// Encloses exp(x) for x in a. Widened by one ulp on each side, because exp is not required to be + /// correctly rounded, so a libm need not be monotone. Where exp(hi) overflows to infinity, hi is NaN, + /// which a cull test hi < cutoff reads as "not provably below", the safe side. + /// + public static float2 iv_exp(float2 a) + { + var lo = exp(a.x); + var hi = exp(a.y); + return new(lo > 0.0f ? asfloat(asuint(lo) - 1u) : 0.0f, asfloat(asuint(hi) + 1u)); + } + + /// Encloses clamp(x, minimum, maximum) for x in a. Monotone: no widening. + public static float2 iv_clamp(float2 a, float minimum, float maximum) => + new(clamp(a.x, minimum, maximum), clamp(a.y, minimum, maximum)); + + /// Encloses saturate(x) for x in a. Monotone: no widening. + public static float2 iv_saturate(float2 a) => new(saturate(a.x), saturate(a.y)); + + /// + /// Encloses smoothstep(minimum, maximum, x) for x in a. smoothstep is monotone in x (increasing when + /// minimum < maximum, decreasing when minimum > maximum), so the bound is its value at the two + /// endpoints, ordered. Its float32 evaluation t * t * (3 - 2t) is not monotone, by at most one ulp + /// (IntervalTests.MeasureMonotonicity), so the bound is widened by one ulp inside [0, 1]. + /// + public static float2 iv_smoothstep(float minimum, float maximum, float2 a) + { + var s0 = smoothstep(minimum, maximum, a.x); + var s1 = smoothstep(minimum, maximum, a.y); + var lo = min(s0, s1); + var hi = max(s0, s1); + return new(lo > 0.0f ? asfloat(asuint(lo) - 1u) : 0.0f, hi < 1.0f ? asfloat(asuint(hi) + 1u) : 1.0f); + } + + /// + /// Encloses lerp(x, y, amount) = x + (y - x) * amount for x in a, y in b, by evaluating that expression + /// with iv_sub, iv_scale and iv_add. Each step is monotone, so the result encloses the float32 lerp + /// with no widening, for any amount. It is the natural interval extension: x appears twice, so for + /// non-point a the width is (width(a) + width(b)) * |amount| + width(a), wider than the true range. + /// + public static float2 iv_lerp(float2 a, float2 b, float amount) => iv_add(a, iv_scale(iv_sub(b, a), amount)); + + /// + /// Encloses snoise(x) for every x within distance radius of centre: [n - L r, n + L r] intersected + /// with [-1, 1], where n = snoise(centre) and L = SNOISE_LIPSCHITZ. radius must be at least 0. One + /// snoise evaluation, value only. A domain warp that moves x by at most D is enclosed by passing + /// radius + D (NoiseBoundTests.WarpedPointsStayEnclosed). + /// + public static float2 iv_snoise_ball(float3 centre, float radius) + { + var n = snoise(centre); + var e = SNOISE_LIPSCHITZ * radius; + return new(max(n - e, -1.0f), min(n + e, 1.0f)); + } + + /// + /// Encloses fbm_grad(x, octaves, lacunarity, gain).w for every x within distance radius of centre: the + /// octave sum of iv_snoise_ball(centre * f_i, radius * |f_i|) scaled by a_i, with frequency f_i and + /// amplitude a_i compounded exactly as fbm_grad compounds them. octaves is clamped to [0, 16] like + /// fbm_grad. + /// + public static float2 iv_fbm_ball(float3 centre, float radius, int octaves, float lacunarity, float gain) + { + octaves = clamp(octaves, 0, 16); + var amplitude = 1.0f; + var frequency = 1.0f; + var sum = new float2(0.0f, 0.0f); + for (var i = 0; i < octaves; i++) + { + sum = iv_add(sum, iv_scale(iv_snoise_ball(centre * frequency, radius * abs(frequency)), amplitude)); + frequency *= lacunarity; + amplitude *= gain; + } + + return sum; + } +} diff --git a/packages/cultmath/src/CultMath/math.Interval.cs.meta b/packages/cultmath/src/CultMath/math.Interval.cs.meta new file mode 100644 index 00000000..eb3b76d1 --- /dev/null +++ b/packages/cultmath/src/CultMath/math.Interval.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 3eb757caae684b388921e28e3c87ba3a diff --git a/packages/cultmath/src/CultMath/math.cs b/packages/cultmath/src/CultMath/math.cs index 23916b6b..d6c85847 100644 --- a/packages/cultmath/src/CultMath/math.cs +++ b/packages/cultmath/src/CultMath/math.cs @@ -388,6 +388,7 @@ public static partial class math public static float hash(float3 value) => hash(dot(value, new float3(127.1f, 311.7f, 74.7f))); public static uint asuint(float value) => (uint)BitConverter.SingleToInt32Bits(value); + public static float asfloat(uint value) => BitConverter.Int32BitsToSingle((int)value); // PCG hashes from Jarzynski and Olano, "Hash Functions for GPU Rendering" (JCGT 9(3), 2020). Integer-only, so // dxc and C# agree bit for bit. pcg3d/pcg4d carry uint bit patterns in int3/int4; float overloads hash IEEE bits. @@ -599,6 +600,8 @@ public static partial class math // src/noise3D.glsl at 22434e04d7 (kernel radius^2 0.5, scale 105, permute (34x+10)x); see // THIRD-PARTY-NOTICES.md. Continuous across simplex-cell boundaries // (NoiseGradTests.SnoiseAndItsGradientAreContinuousAcrossSimplexCellBoundaries). + // SNOISE_LIPSCHITZ (math.Interval.cs) is measured from this kernel: a change here must re-run + // NoiseBoundTests.MeasureLipschitz and re-pin it. public static float snoise(float3 value) { const float cx = 1.0f / 6.0f; diff --git a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs index 6b69a908..9e2dc9af 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs @@ -65,6 +65,7 @@ public sealed class HlslMirrorTests [Theory] [InlineData("CultMath.hlsl")] [InlineData("CultMath.Phacelle.hlsl")] + [InlineData("CultMath.Interval.hlsl")] public void UnityPackageShaderIsIdenticalToTheCanonicalShader(string name) { var root = GetCultMathRoot(); diff --git a/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs index 2cb8ae49..07f026ff 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs @@ -31,6 +31,8 @@ public sealed class HlslSourceCompatibilityTests Assert.NotNull(ShaderFunction(shader, "cultmath_fbm_grad", typeof(float3), typeof(int), typeof(float), typeof(float))); Assert.NotNull(ShaderFunction(shader, "cultmath_ridged_grad", typeof(float3), typeof(int), typeof(float), typeof(float))); Assert.NotNull(ShaderFunction(shader, "cultmath_phacelle", typeof(float3), typeof(float3), typeof(float), typeof(float))); + Assert.NotNull(ShaderFunction(shader, "cultmath_iv_snoise_ball", typeof(float3), typeof(float))); + Assert.NotNull(ShaderFunction(shader, "cultmath_iv_fbm_ball", typeof(float3), typeof(float), typeof(int), typeof(float), typeof(float))); } // HLSL functions carry no access modifier, so they compile as private instance methods. @@ -131,7 +133,7 @@ public sealed class HlslSourceCompatibilityTests } Assert.True(mismatches.Count == 0, $"{mismatches.Count} mismatches:{Environment.NewLine}{string.Join(Environment.NewLine, mismatches.Take(12))}"); - Assert.Equal(34, compared.Count); + Assert.Equal(52, compared.Count); } /// diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl new file mode 100644 index 00000000..ed6f83db --- /dev/null +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl @@ -0,0 +1,107 @@ +#ifndef CULTMATH_INTERVAL_HLSL +#define CULTMATH_INTERVAL_HLSL + +// Interval arithmetic over float2(lo, hi), lo <= hi, both finite. Every cultmath_iv_* function returns +// an interval containing f(x) for every x in its input intervals, mirroring math.Interval.cs bit for +// bit; see that file and docs/design.md ("Intervals") for each op's enclosure and its ulp widening. +// Included by CultMath.hlsl after its own definitions of cultmath_smoothstep and cultmath_snoise. + +// Lipschitz constant of cultmath_snoise(float3); provenance in math.Interval.cs (SNOISE_LIPSCHITZ). +static const float CULTMATH_SNOISE_LIPSCHITZ = 10.099261; + +float2 cultmath_iv_point(float value) { return float2(value, value); } +float2 cultmath_iv_add(float2 a, float2 b) { return float2(a.x + b.x, a.y + b.y); } +float2 cultmath_iv_sub(float2 a, float2 b) { return float2(a.x - b.y, a.y - b.x); } +float2 cultmath_iv_neg(float2 a) { return float2(-a.y, -a.x); } + +float2 cultmath_iv_mul(float2 a, float2 b) +{ + float p0 = a.x * b.x; + float p1 = a.x * b.y; + float p2 = a.y * b.x; + float p3 = a.y * b.y; + return float2(min(min(p0, p1), min(p2, p3)), max(max(p0, p1), max(p2, p3))); +} + +float2 cultmath_iv_scale(float2 a, float s) { return s >= 0.0 ? float2(a.x * s, a.y * s) : float2(a.y * s, a.x * s); } + +float2 cultmath_iv_abs(float2 a) +{ + if (a.x >= 0.0) + return a; + if (a.y <= 0.0) + return float2(-a.y, -a.x); + return float2(0.0, max(-a.x, a.y)); +} + +float2 cultmath_iv_min(float2 a, float2 b) { return float2(min(a.x, b.x), min(a.y, b.y)); } +float2 cultmath_iv_max(float2 a, float2 b) { return float2(max(a.x, b.x), max(a.y, b.y)); } + +float2 cultmath_iv_sqr(float2 a) +{ + float l = a.x * a.x; + float h = a.y * a.y; + if (a.x >= 0.0) + return float2(l, h); + if (a.y <= 0.0) + return float2(h, l); + return float2(0.0, max(l, h)); +} + +float2 cultmath_iv_sqrt(float2 a) { return float2(sqrt(max(a.x, 0.0)), sqrt(max(a.y, 0.0))); } + +// Widened by one ulp on each side: exp is not required to be correctly rounded. +float2 cultmath_iv_exp(float2 a) +{ + float lo = exp(a.x); + float hi = exp(a.y); + return float2(lo > 0.0 ? asfloat(asuint(lo) - 1u) : 0.0, asfloat(asuint(hi) + 1u)); +} + +float2 cultmath_iv_clamp(float2 a, float minimum, float maximum) +{ + return float2(cultmath_clamp(a.x, minimum, maximum), cultmath_clamp(a.y, minimum, maximum)); +} + +float2 cultmath_iv_saturate(float2 a) { return float2(cultmath_saturate(a.x), cultmath_saturate(a.y)); } + +// The endpoints, ordered, widened by one ulp inside [0, 1]: the float32 evaluation of smoothstep is +// monotone only to within one ulp. +float2 cultmath_iv_smoothstep(float minimum, float maximum, float2 a) +{ + float s0 = cultmath_smoothstep(minimum, maximum, a.x); + float s1 = cultmath_smoothstep(minimum, maximum, a.y); + float lo = min(s0, s1); + float hi = max(s0, s1); + return float2(lo > 0.0 ? asfloat(asuint(lo) - 1u) : 0.0, hi < 1.0 ? asfloat(asuint(hi) + 1u) : 1.0); +} + +// The natural interval extension of x + (y - x) * amount, which is how cultmath_lerp evaluates. +float2 cultmath_iv_lerp(float2 a, float2 b, float amount) { return cultmath_iv_add(a, cultmath_iv_scale(cultmath_iv_sub(b, a), amount)); } + +// [n - L r, n + L r] intersected with [-1, 1], n = cultmath_snoise(centre). One snoise, value only. +float2 cultmath_iv_snoise_ball(float3 centre, float radius) +{ + float n = cultmath_snoise(centre); + float e = CULTMATH_SNOISE_LIPSCHITZ * radius; + return float2(max(n - e, -1.0), min(n + e, 1.0)); +} + +// Octave sum of cultmath_iv_snoise_ball, compounding frequency and amplitude as cultmath_fbm_grad does. +float2 cultmath_iv_fbm_ball(float3 centre, float radius, int octaves, float lacunarity, float gain) +{ + octaves = clamp(octaves, 0, 16); + float amplitude = 1.0; + float frequency = 1.0; + float2 sum = float2(0.0, 0.0); + for (int i = 0; i < octaves; i++) + { + sum = cultmath_iv_add(sum, cultmath_iv_scale(cultmath_iv_snoise_ball(centre * frequency, radius * abs(frequency)), amplitude)); + frequency *= lacunarity; + amplitude *= gain; + } + + return sum; +} + +#endif diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl.meta b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl.meta new file mode 100644 index 00000000..023d218c --- /dev/null +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Interval.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 18b031192f344246ac523cea014b8f24 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl index af2d3741..da3362a6 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl @@ -504,6 +504,7 @@ CultCellular cultmath_cellular(float3 p) } #include "CultMath.Phacelle.hlsl" +#include "CultMath.Interval.hlsl" float cultmath_value_noise(float2 position) {