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 <noreply@anthropic.com>
This commit is contained in:
parent
d0ea37f8e4
commit
bbf5269751
13 changed files with 408 additions and 2 deletions
|
|
@ -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)) {
|
||||
|
|
|
|||
107
packages/cultmath/shaders/CultMath.Interval.hlsl
Normal file
107
packages/cultmath/shaders/CultMath.Interval.hlsl
Normal file
|
|
@ -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
|
||||
7
packages/cultmath/shaders/CultMath.Interval.hlsl.meta
Normal file
7
packages/cultmath/shaders/CultMath.Interval.hlsl.meta
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4d8a899561654ac990cab0bf48658cc1
|
||||
ShaderIncludeImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -504,6 +504,7 @@ CultCellular cultmath_cellular(float3 p)
|
|||
}
|
||||
|
||||
#include "CultMath.Phacelle.hlsl"
|
||||
#include "CultMath.Interval.hlsl"
|
||||
|
||||
float cultmath_value_noise(float2 position)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
|
||||
<None Include="..\..\shaders\CultMath.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\CultMath.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="..\..\shaders\CultMath.Phacelle.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\CultMath.Phacelle.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="..\..\shaders\CultMath.Interval.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\CultMath.Interval.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="..\..\THIRD-PARTY-NOTICES.md" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
165
packages/cultmath/src/CultMath/math.Interval.cs
Normal file
165
packages/cultmath/src/CultMath/math.Interval.cs
Normal file
|
|
@ -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;
|
||||
|
||||
/// <summary>The point interval [x, x].</summary>
|
||||
public static float2 iv_point(float value) => new(value, value);
|
||||
|
||||
/// <summary>Encloses x + y for x in a, y in b. Monotone: no widening.</summary>
|
||||
public static float2 iv_add(float2 a, float2 b) => new(a.x + b.x, a.y + b.y);
|
||||
|
||||
/// <summary>Encloses x - y for x in a, y in b. Monotone: no widening.</summary>
|
||||
public static float2 iv_sub(float2 a, float2 b) => new(a.x - b.y, a.y - b.x);
|
||||
|
||||
/// <summary>Encloses -x for x in a. Exact.</summary>
|
||||
public static float2 iv_neg(float2 a) => new(-a.y, -a.x);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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)));
|
||||
}
|
||||
|
||||
/// <summary>Encloses x * s for x in a. Monotone: no widening.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>Encloses abs(x) for x in a; an interval straddling zero gives [0, max(-lo, hi)]. Exact.</summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>Encloses min(x, y) for x in a, y in b. Monotone: no widening.</summary>
|
||||
public static float2 iv_min(float2 a, float2 b) => new(min(a.x, b.x), min(a.y, b.y));
|
||||
|
||||
/// <summary>Encloses max(x, y) for x in a, y in b. Monotone: no widening.</summary>
|
||||
public static float2 iv_max(float2 a, float2 b) => new(max(a.x, b.x), max(a.y, b.y));
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static float2 iv_sqrt(float2 a) => new(sqrt(max(a.x, 0.0f)), sqrt(max(a.y, 0.0f)));
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>Encloses clamp(x, minimum, maximum) for x in a. Monotone: no widening.</summary>
|
||||
public static float2 iv_clamp(float2 a, float minimum, float maximum) =>
|
||||
new(clamp(a.x, minimum, maximum), clamp(a.y, minimum, maximum));
|
||||
|
||||
/// <summary>Encloses saturate(x) for x in a. Monotone: no widening.</summary>
|
||||
public static float2 iv_saturate(float2 a) => new(saturate(a.x), saturate(a.y));
|
||||
|
||||
/// <summary>
|
||||
/// 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].
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static float2 iv_lerp(float2 a, float2 b, float amount) => iv_add(a, iv_scale(iv_sub(b, a), amount));
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
2
packages/cultmath/src/CultMath/math.Interval.cs.meta
Normal file
2
packages/cultmath/src/CultMath/math.Interval.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3eb757caae684b388921e28e3c87ba3a
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 18b031192f344246ac523cea014b8f24
|
||||
ShaderIncludeImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -504,6 +504,7 @@ CultCellular cultmath_cellular(float3 p)
|
|||
}
|
||||
|
||||
#include "CultMath.Phacelle.hlsl"
|
||||
#include "CultMath.Interval.hlsl"
|
||||
|
||||
float cultmath_value_noise(float2 position)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue