Merge hands/cultmath-stage-shaders-device (part 1: allowance seam, band read families, one owner of the read's absolute terms)

Verdicts cut-stage-shaders-device.s1/s2 hold. main's interim changes are Rust packages only (cultnet-rs, cultcache-rs, cultmesh-rs), disjoint from the verified C#/shader tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-16 06:55:43 +02:00
commit de584f5a39
14 changed files with 1375 additions and 37 deletions

View file

@ -243,6 +243,13 @@ the main families' reach is at least about 1e-3, where the second-order remainde
allowance still passes. `GoldenFixtureMatchesCSharp` requires each of the two to hold cases that fail on C#'s own results once
the allowance is removed.
Four more appended families, the coordinate-band `snoise_grad` and `snoise_hessian` reads (256 cases each, the magnitude of the
point log-uniform from 1e-4 to 1e2 and from 1e2 to 4e6), carry an allowance `check`: with `S = |x|_1` and the allowances
`math.span_slack(math.span_rounding(1, x, zero stage, 0, 0, 0))` (the owners of the span stage's allowances in C#, HLSL and GLSL),
the result must lie within the allowance of the kernel's exact value, gradient or Hessian in double, no tolerance. The two small
families must also fail it in at least 16 cases once the absolute term is removed. `GoldenFixtureMatchesCSharp` applies the check
to C#'s own results, and a device capture is judged by the same `Verify`.
`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

View file

@ -12,6 +12,12 @@
// math.Affine.cs (SNOISE_HESSIAN).
static const float CULTMATH_SNOISE_HESSIAN = 55.642143;
// The absolute rounding of one float32 read of cultmath_snoise_grad, value and gradient; the owner is
// math.Affine.cs (SNOISE_READ_VALUE, SNOISE_READ_GRADIENT), read here by cultmath_af_snoise and in
// CultMath.hlsl by cultmath_span_slack.
static const float CULTMATH_SNOISE_READ_VALUE = 6.103515625e-5;
static const float CULTMATH_SNOISE_READ_GRADIENT = 2.44140625e-4;
// An upper bound on the operator norm of the third-derivative tensor of cultmath_snoise(float3), the
// next order after CULTMATH_SNOISE_HESSIAN; provenance in math.Span.cs (SNOISE_THIRD).
static const float CULTMATH_SNOISE_THIRD = 535.8009;
@ -87,7 +93,7 @@ float3 cultmath_af_snoise(float3 centre, float3 axis, float radius)
float lipschitz = CULTMATH_SNOISE_LIPSCHITZ * reach;
float a = dot(gradient, axis);
float remainder = length(gradient) * radius + 0.5 * CULTMATH_SNOISE_HESSIAN * reach * reach;
float allowance = 6.103515625e-5 + 2.44140625e-4 * reach
float allowance = CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_READ_GRADIENT * reach
+ CULTMATH_SNOISE_LIPSCHITZ * 9.5367431640625e-7 * (abs(centre.x) + abs(centre.y) + abs(centre.z) + reach);
bool centred = abs(a) + remainder < lipschitz;
float slope = centred ? a : 0.0;

View file

@ -748,6 +748,12 @@ vec4 cultmath_iv_frustum_ball(vec2 centreSlope, float z0, float z1, float footpr
// math.Affine.cs (SNOISE_HESSIAN).
const float CULTMATH_SNOISE_HESSIAN = 55.642143;
// The absolute rounding of one float32 read of cultmath_snoise_grad, value and gradient; the owner is
// math.Affine.cs (SNOISE_READ_VALUE, SNOISE_READ_GRADIENT), read here by cultmath_af_snoise and in
// CultMath.hlsl by cultmath_span_slack.
const float CULTMATH_SNOISE_READ_VALUE = 6.103515625e-5;
const float CULTMATH_SNOISE_READ_GRADIENT = 2.44140625e-4;
// An upper bound on the operator norm of the third-derivative tensor of cultmath_snoise(vec3), the
// next order after CULTMATH_SNOISE_HESSIAN; provenance in math.Span.cs (SNOISE_THIRD).
const float CULTMATH_SNOISE_THIRD = 535.8009;
@ -823,7 +829,7 @@ vec3 cultmath_af_snoise(vec3 centre, vec3 axis, float radius)
float lipschitz = CULTMATH_SNOISE_LIPSCHITZ * reach;
float a = dot(gradient, axis);
float remainder = length(gradient) * radius + 0.5 * CULTMATH_SNOISE_HESSIAN * reach * reach;
float allowance = 6.103515625e-5 + 2.44140625e-4 * reach
float allowance = CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_READ_GRADIENT * reach
+ CULTMATH_SNOISE_LIPSCHITZ * 9.5367431640625e-7 * (abs(centre.x) + abs(centre.y) + abs(centre.z) + reach);
bool centred = abs(a) + remainder < lipschitz;
float slope = centred ? a : 0.0;
@ -877,7 +883,8 @@ vec4 cultmath_af_frustum_ball(vec2 centreSlope, float z0, float z1, float footpr
// mirror. They follow the includes because the bounds read CULTMATH_SNOISE_LIPSCHITZ, _HESSIAN and _THIRD.
// A shader caller builds a SpanStage itself (SpanStage(1.0, 1.0, vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0))
// is the identity of math.SpanStage.Identity, which has no shader form); see math.span_stage's comments for
// the span, the transform, the bounds and the allowances.
// the span, the transform, the bounds and the allowances. cultmath_span_rounding and cultmath_span_slack
// are math.span_rounding and math.span_slack, the owners of S_o and of the three allowances.
struct SpanPath
{
vec3 midpoint;
@ -904,6 +911,18 @@ struct SpanNoise
float fold;
};
float cultmath_span_rounding(float scale, vec3 mq, SpanStage stage, vec3 octaveShift, float nu, float halfLength)
{
return 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
}
vec3 cultmath_span_slack(float rounding)
{
return vec3(CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_LIPSCHITZ * rounding,
CULTMATH_SNOISE_READ_GRADIENT + CULTMATH_SNOISE_HESSIAN * rounding,
9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding);
}
// The octave stage behind cultmath_span_fbm and cultmath_span_ridged, as math.span_stage is behind span_fbm
// and span_ridged. The Frobenius norm of the Hessian is written out in C#'s order (GLSL has no _mRC member
// access); the C# local named step is octaveShift here (it would shadow GLSL's step()).
@ -946,10 +965,11 @@ SpanNoise cultmath_span_stage(bool ridged, SpanPath path, SpanStage stage, int o
float weight = abs(amplitude);
float factor = abs(scale) * norm;
float nu = factor * path.speed;
float rounding = 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
float valueSlack = 6.103515625e-5 + CULTMATH_SNOISE_LIPSCHITZ * rounding;
float gradientSlack = 2.44140625e-4 + CULTMATH_SNOISE_HESSIAN * rounding;
float hessianSlack = 9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding;
float rounding = cultmath_span_rounding(scale, mq, stage, octaveShift, nu, halfLength);
vec3 slack = cultmath_span_slack(rounding);
float valueSlack = slack.x;
float gradientSlack = slack.y;
float hessianSlack = slack.z;
mat3 hm = cultmath_snoise_hessian(x);
float frobenius = sqrt(hm[0][0] * hm[0][0] + hm[0][1] * hm[0][1] + hm[0][2] * hm[0][2] + hm[1][0] * hm[1][0] + hm[1][1] * hm[1][1] + hm[1][2] * hm[1][2]
+ hm[2][0] * hm[2][0] + hm[2][1] * hm[2][1] + hm[2][2] * hm[2][2]);

View file

@ -610,7 +610,8 @@ CultCellular cultmath_cellular(float3 p)
// mirror. They follow the includes because the bounds read CULTMATH_SNOISE_LIPSCHITZ, _HESSIAN and _THIRD.
// A shader caller builds a SpanStage itself (SpanStage(1.0, 1.0, vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0))
// is the identity of math.SpanStage.Identity, which has no shader form); see math.span_stage's comments for
// the span, the transform, the bounds and the allowances.
// the span, the transform, the bounds and the allowances. cultmath_span_rounding and cultmath_span_slack
// are math.span_rounding and math.span_slack, the owners of S_o and of the three allowances.
struct SpanPath
{
float3 midpoint;
@ -637,6 +638,18 @@ struct SpanNoise
float fold;
};
float cultmath_span_rounding(float scale, float3 mq, SpanStage stage, float3 octaveShift, float nu, float halfLength)
{
return 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
}
float3 cultmath_span_slack(float rounding)
{
return float3(CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_LIPSCHITZ * rounding,
CULTMATH_SNOISE_READ_GRADIENT + CULTMATH_SNOISE_HESSIAN * rounding,
9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding);
}
// The octave stage behind cultmath_span_fbm and cultmath_span_ridged, as math.span_stage is behind span_fbm
// and span_ridged. The Frobenius norm of the Hessian is written out in C#'s order (GLSL has no _mRC member
// access); the C# local named step is octaveShift here (it would shadow GLSL's step()).
@ -679,10 +692,11 @@ SpanNoise cultmath_span_stage(bool ridged, SpanPath path, SpanStage stage, int o
float weight = abs(amplitude);
float factor = abs(scale) * norm;
float nu = factor * path.speed;
float rounding = 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
float valueSlack = 6.103515625e-5 + CULTMATH_SNOISE_LIPSCHITZ * rounding;
float gradientSlack = 2.44140625e-4 + CULTMATH_SNOISE_HESSIAN * rounding;
float hessianSlack = 9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding;
float rounding = cultmath_span_rounding(scale, mq, stage, octaveShift, nu, halfLength);
float3 slack = cultmath_span_slack(rounding);
float valueSlack = slack.x;
float gradientSlack = slack.y;
float hessianSlack = slack.z;
float3x3 hm = cultmath_snoise_hessian(x);
float frobenius = sqrt(hm[0][0] * hm[0][0] + hm[0][1] * hm[0][1] + hm[0][2] * hm[0][2] + hm[1][0] * hm[1][0] + hm[1][1] * hm[1][1] + hm[1][2] * hm[1][2]
+ hm[2][0] * hm[2][0] + hm[2][1] * hm[2][1] + hm[2][2] * hm[2][2]);

View file

@ -35,6 +35,13 @@ public static partial class math
// kernel must re-run MeasureHessian and re-pin it. SNOISE_THIRD (math.Span.cs) is the next-order pin.
public const float SNOISE_HESSIAN = 55.642143f;
// The absolute rounding of one float32 read of the snoise kernel, apart from the coordinate's own
// rounding (af_snoise's comment derives both): 2^-14 on the value and 2^-12 on the gradient. The single
// owner of the two, read by af_snoise and by span_slack (math.Span.cs); the shader twins are
// CULTMATH_SNOISE_READ_VALUE and CULTMATH_SNOISE_READ_GRADIENT.
internal const float SNOISE_READ_VALUE = 6.103515625e-5f;
internal const float SNOISE_READ_GRADIENT = 2.44140625e-4f;
/// <summary>The point form (x, 0, 0): the value x at every eps. Exact.</summary>
public static float3 af_point(float value) => new(value, 0.0f, 0.0f);
@ -165,7 +172,7 @@ public static partial class math
var lipschitz = SNOISE_LIPSCHITZ * reach;
var a = dot(gradient, axis);
var remainder = length(gradient) * radius + 0.5f * SNOISE_HESSIAN * reach * reach;
var allowance = 6.103515625e-5f + 2.44140625e-4f * reach
var allowance = SNOISE_READ_VALUE + SNOISE_READ_GRADIENT * reach
+ SNOISE_LIPSCHITZ * 9.5367431640625e-7f * (abs(centre.x) + abs(centre.y) + abs(centre.z) + reach);
var centred = abs(a) + remainder < lipschitz;
var slope = centred ? a : 0.0f;

View file

@ -151,6 +151,22 @@ public static partial class math
public static SpanNoise span_ridged(SpanPath path, SpanStage stage, int octaves, float lacunarity, float gain) =>
span_stage(true, path, stage, octaves, lacunarity, gain);
// 2^-20 S_o: the rounding one octave's read of the noise carries, S_o being the sum of the magnitudes the
// octave's coordinate is formed from and the span's reach (the allowance paragraph above). The single
// owner of S_o; span_slack turns it into the three allowances.
internal static float span_rounding(float scale, float3 mq, SpanStage stage, float3 step, float nu, float halfLength) =>
9.5367431640625e-7f * (abs(scale) * (csum(abs(mq)) + csum(abs(stage.shift))) + csum(abs(stage.offset)) + csum(abs(step)) + 1.7320508f * nu * halfLength);
// The allowances (e_n, e_g, e_h) of the value, the gradient's length and the Hessian's Frobenius norm at
// a rounding of 2^-20 S_o: the absolute term of each plus its derivative's pin times the rounding. The
// single owner of the three allowances of a span; af_snoise (math.Affine.cs) forms its own allowance
// from the same SNOISE_READ_VALUE and SNOISE_READ_GRADIENT, the owners of the first two absolute terms,
// so widening either moves every reader of the kernel at once.
internal static float3 span_slack(float rounding) => new(
SNOISE_READ_VALUE + SNOISE_LIPSCHITZ * rounding,
SNOISE_READ_GRADIENT + SNOISE_HESSIAN * rounding,
9.765625e-4f + SNOISE_THIRD * rounding);
// The span is SpanPath: q(t), |t| <= halfLength, through midpoint with velocity at t = 0, |q'| <= speed
// and |q''| <= acceleration along it (speed at least |velocity|; all three and halfLength non-negative).
// The transform is SpanStage: octave o samples snoise_grad at x_o(q) = f_o (M q + shift) + offset +
@ -182,16 +198,14 @@ public static partial class math
// coordinate by about 2u of the magnitudes it is formed from, which moves a result by its derivative
// times that, and the caller's scaling of the point and a GPU's evaluation take 2u to 16u = 2^-20.
// S_o is the sum of those magnitudes, |f_o| (|M q|_1 + |shift|_1) + |offset|_1 + |o octaveStep|_1, plus
// the span's reach sqrt(3) nu halfLength (the coordinate of a path point). The allowances are the
// value's e_n = 2^-14 + L 2^-20 S_o, the gradient length's e_g = 2^-12 + H 2^-20 S_o (H is the
// gradient's own derivative's pin) and the Hessian's Frobenius norm's e_h = 2^-10 + T 2^-20 S_o (T is
// the Hessian's derivative's pin): af_snoise's absolute terms for the first two, and 2^-10 for the
// Hessian's, which the kernel's rounding at small coordinates (2.3e-5 absolute, measured against a
// double kernel) sits well under. Two samples of that probe (6000 points per band, |x| from 2^-2 to
// 2^23) observed the error's maxima, as fractions of e_n, e_g and e_h: 0.057, 0.046 and 0.075 in the
// cut's own sample (seed not recorded; the 0.075 is against T 2^-20 |x|_1 alone, at the smallest
// band), and 0.064, 0.0485 and 0.057 in an audit's (seed 0x501). They are samples, not bounds: a new
// seed moves them. What they say is that the allowances enclose the error with a margin of about 15.
// the span's reach sqrt(3) nu halfLength (the coordinate of a path point); span_rounding is its owner. The
// allowances are the value's e_n = 2^-14 + L 2^-20 S_o, the gradient length's e_g = 2^-12 + H 2^-20 S_o
// (H is the gradient's own derivative's pin) and the Hessian's Frobenius norm's e_h = 2^-10 + T 2^-20 S_o
// (T is the Hessian's derivative's pin): SNOISE_READ_VALUE and SNOISE_READ_GRADIENT (math.Affine.cs) are the
// absolute terms of the first two, 2^-10 the Hessian's; span_slack is the owner of the three. They are observed where they are decided: the coordinate-band
// snoise_grad and snoise_hessian families of glsl-parity.json and SpanAllowanceCoversReadsAlongTheSpan
// check every read against them. Those observations are samples, not bounds: a new seed moves them. What
// they say is that the allowances enclose the error with a margin of about 15.
// No new pin enters.
//
// Domain: no coordinate range is checked, clamped or refused. The allowances grow with S_o, so
@ -237,10 +251,11 @@ public static partial class math
var weight = abs(amplitude);
var factor = abs(scale) * norm;
var nu = factor * path.speed;
var rounding = 9.5367431640625e-7f * (abs(scale) * (csum(abs(mq)) + csum(abs(stage.shift))) + csum(abs(stage.offset)) + csum(abs(step)) + 1.7320508f * nu * halfLength);
var valueSlack = 6.103515625e-5f + SNOISE_LIPSCHITZ * rounding;
var gradientSlack = 2.44140625e-4f + SNOISE_HESSIAN * rounding;
var hessianSlack = 9.765625e-4f + SNOISE_THIRD * rounding;
var rounding = span_rounding(scale, mq, stage, step, nu, halfLength);
var slack = span_slack(rounding);
var valueSlack = slack.x;
var gradientSlack = slack.y;
var hessianSlack = slack.z;
var local = min(SNOISE_HESSIAN, span_frobenius(snoise_hessian(x)) + hessianSlack + SNOISE_THIRD * nu * halfLength);
curvature += weight * factor * factor * local;
if (ridged)

View file

@ -295,9 +295,35 @@ public sealed class AffineTests
// af_snoise's float32 allowance, as its doc comment states it, for the width comparison below.
private static double Allowance(float3 centre, double reach) =>
Math.ScaleB(1.0, -14) + Math.ScaleB(reach, -12)
SNOISE_READ_VALUE + SNOISE_READ_GRADIENT * reach
+ SNOISE_LIPSCHITZ * Math.ScaleB(Math.Abs(centre.x) + Math.Abs(centre.y) + Math.Abs(centre.z) + reach, -20);
/// <summary>
/// af_snoise reads the same absolute rounding terms of the kernel as span_slack: at the origin with no
/// axis, the form's error is span_slack's value allowance at zero rounding plus the Lipschitz form's
/// L R, and each unit of reach adds span_slack's gradient term at zero rounding plus L 2^-20 (the
/// coordinate's own rounding). Widening the kernel's absolute read in one reader and not the other,
/// the device judge's possible outcome, fails here. The expected bits are formed from span_slack, not
/// from a literal.
/// </summary>
[Fact]
public void SnoiseFormAndSpanSlackShareTheKernelsAbsoluteTerms()
{
var zero = new float3(0.0f, 0.0f, 0.0f);
var slack = span_slack(0.0f);
var value = snoise_grad(zero).w;
foreach (var reach in new[] { 0.0f, 1.0f, 3.0f })
{
var lipschitz = SNOISE_LIPSCHITZ * reach;
var allowance = slack.x + slack.y * reach + SNOISE_LIPSCHITZ * 9.5367431640625e-7f * reach;
var e = lipschitz + allowance;
var expected = e + (abs(value) + 0.0f + e) * 9.5367431640625e-7f;
var form = af_snoise(zero, zero, reach);
Assert.True(BitConverter.SingleToInt32Bits(form.z) == BitConverter.SingleToInt32Bits(expected),
$"af_snoise(0, 0, {reach:R}).z is {form.z:R}; the allowances span_slack reads give {expected:R}");
}
}
private static float3 UnitVector(System.Random random)
{
float3 v;

View file

@ -336,7 +336,7 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
// The float32 allowance of one octave of af_snoise: 2^-14 + 2^-12 R + L 2^-20 (|c|_1 + R).
private static double SnoiseAllowance(double centre1, double reach) =>
Math.ScaleB(1.0, -14) + Math.ScaleB(reach, -12) + math.SNOISE_LIPSCHITZ * Math.ScaleB(centre1 + reach, -20);
math.SNOISE_READ_VALUE + math.SNOISE_READ_GRADIENT * reach + math.SNOISE_LIPSCHITZ * Math.ScaleB(centre1 + reach, -20);
// The Lipschitz form's width bound for one octave of the sampled-form check.
private static double SnoiseWidthBound(double centre1, double reach) => math.SNOISE_LIPSCHITZ * reach + SnoiseAllowance(centre1, reach);
@ -408,6 +408,74 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
return $"least enclosure slack {enclosure:E3} of the radius, least slack above R0 + d {lower:E3} of R0, below its widening {upper:F3} of 2^-19 (R0 + |c0|_1 + spread)";
}
private const string SpanAllowanceRead =
"Read at the case's point x, with S = |x|_1 and (e_n, e_g, e_h) = math.span_slack(math.span_rounding(1, x, zero stage, 0, 0, 0)), " +
"against the kernel's exact values in double: |n - n_exact| <= e_n and |g - g_exact|_2 <= e_g for snoise_grad, " +
"||H - H_exact||_F <= e_h for snoise_hessian, no tolerance. " +
"Report the largest fraction of each allowance per decade of S and the largest absolute error at S < 1e-2.";
// SpanAllowanceRead applied to a family's results (C#'s own, or a device's): every read is within the
// allowance math.span_slack gives at its point, the owner's own numbers and no restated literal. A small
// family (bands 1e-4 to 1e2) must also fail the check once the absolute term is removed in at least 16
// cases, and hold at least 48 cases with S <= 1e-2: the allowance's absolute term is what decides there.
private static string AssertSpanAllowance(JsonArray cases, bool hessian, bool small)
{
var absolute = math.span_slack(0.0f);
var fractions = new SortedDictionary<int, (double Value, double Gradient)>();
double smallestError = 0.0;
int absoluteDecides = 0, nearOrigin = 0;
foreach (var entry in cases)
{
var sides = ((string)entry!).Split(" -> ");
double[] x = Floats(sides[0]), result = Floats(sides[1]);
var s = Math.Abs(x[0]) + Math.Abs(x[1]) + Math.Abs(x[2]);
var slack = math.span_slack(math.span_rounding(1.0f, new float3((float)x[0], (float)x[1], (float)x[2]), default, new float3(0.0f, 0.0f, 0.0f), 0.0f, 0.0f));
double valueError = 0.0, gradientError, valueFraction = 0.0, gradientFraction;
bool needsAbsolute;
if (hessian)
{
var exact = NoiseBoundTests.HessianDouble(x[0], x[1], x[2]);
var sum = 0.0;
for (var i = 0; i < 9; i++)
sum += (result[i] - exact[i]) * (result[i] - exact[i]);
gradientError = Math.Sqrt(sum);
Assert.True(gradientError <= slack.z, $"snoise_hessian case {entry}: ||H - H_exact||_F is {gradientError:R}, past the allowance {slack.z:R}");
gradientFraction = gradientError / slack.z;
needsAbsolute = gradientError > slack.z - absolute.z;
}
else
{
var exact = NoiseBoundTests.GradDouble(x[0], x[1], x[2]);
valueError = Math.Abs(result[3] - NoiseBoundTests.ValueDouble(x[0], x[1], x[2]));
gradientError = Math.Sqrt((result[0] - exact[0]) * (result[0] - exact[0]) + (result[1] - exact[1]) * (result[1] - exact[1]) + (result[2] - exact[2]) * (result[2] - exact[2]));
Assert.True(valueError <= slack.x, $"snoise_grad case {entry}: |n - n_exact| is {valueError:R}, past the allowance {slack.x:R}");
Assert.True(gradientError <= slack.y, $"snoise_grad case {entry}: |g - g_exact|_2 is {gradientError:R}, past the allowance {slack.y:R}");
valueFraction = valueError / slack.x;
gradientFraction = gradientError / slack.y;
needsAbsolute = valueError > slack.x - absolute.x || gradientError > slack.y - absolute.y;
}
var decade = (int)Math.Floor(Math.Log10(s));
fractions.TryGetValue(decade, out var largest);
fractions[decade] = (Math.Max(largest.Value, valueFraction), Math.Max(largest.Gradient, gradientFraction));
if (needsAbsolute)
absoluteDecides++;
if (s < 1.0e-2)
smallestError = Math.Max(smallestError, Math.Max(valueError, gradientError));
if (s <= 1.0e-2)
nearOrigin++;
}
if (small)
{
Assert.True(absoluteDecides >= 16, $"only {absoluteDecides} of {cases.Count} cases fail once the absolute term is removed: the family cannot tell a device that needs it from one that does not");
Assert.True(nearOrigin >= 48, $"only {nearOrigin} of {cases.Count} cases have |x|_1 <= 1e-2");
}
var report = string.Join(", ", fractions.Select(f => hessian ? $"1e{f.Key}: {f.Value.Gradient:F3}" : $"1e{f.Key}: {f.Value.Value:F3}/{f.Value.Gradient:F3}"));
return $"largest fraction of {(hessian ? "e_h" : "e_n/e_g")} per decade of |x|_1: {report}; largest absolute error at |x|_1 < 1e-2 {smallestError:E3}; {absoluteDecides} of {cases.Count} cases need the absolute term";
}
// A committed case's arguments, decoded into the C# method's parameter types in Hex's field order.
private static object[] Arguments(MethodInfo method, string hex)
{
@ -575,7 +643,8 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
// Every family the site's WebGL2 readback evaluates, in a fixed order. A new family is appended, so the
// seeded stream and every earlier family's cases stay as they were. pcg3d and pcg4d are "exact"; the
// float families are ulp-bounded, with the bound measured on the device by the consumer, and
// iv_frustum_ball also carries an enclosure check. phacelle is in CultMath.Phacelle.glsl, so its
// iv_frustum_ball also carries an enclosure check; the four coordinate-band snoise_grad and snoise_hessian
// families carry an allowance check, applied to C#'s own results here and to a device's by the device judge. phacelle is in CultMath.Phacelle.glsl, so its
// evaluator concatenates that file after CultMath.glsl; the site, which does not vendor it, skips it.
private static readonly Family[] Families = new Family[]
{
@ -720,8 +789,24 @@ static int4 clamp(int4 x, int4 a, int4 b) { if (a.x > b.x || a.y > b.y || a.z >
// midpoint a Point3, one span in four a point.
new Family("SpanNoise cultmath_span_fbm(SpanPath, SpanStage, int, float, float)", "ulp-bounded", SpanArguments),
new Family("SpanNoise cultmath_span_ridged(SpanPath, SpanStage, int, float, float)", "ulp-bounded", SpanArguments),
// The kernel's reads over coordinate bands, the magnitude log-uniform in the band: the allowance of
// math.span_slack is checked against the exact kernel at every case (SpanAllowanceRead).
new Family("vec4 cultmath_snoise_grad(vec3)", "ulp-bounded, coordinate bands 1e-4 to 1e2", r => new object[] { BandPoint3(r, 1.0e-4, 1.0e2) }, SpanAllowanceRead, Verify: cases => AssertSpanAllowance(cases, hessian: false, small: true)),
new Family("vec4 cultmath_snoise_grad(vec3)", "ulp-bounded, coordinate bands 1e2 to 4e6", r => new object[] { BandPoint3(r, 1.0e2, 4.0e6) }, SpanAllowanceRead, Verify: cases => AssertSpanAllowance(cases, hessian: false, small: false)),
new Family("mat3 cultmath_snoise_hessian(vec3)", "ulp-bounded, coordinate bands 1e-4 to 1e2", r => new object[] { BandPoint3(r, 1.0e-4, 1.0e2) }, SpanAllowanceRead, Verify: cases => AssertSpanAllowance(cases, hessian: true, small: true)),
new Family("mat3 cultmath_snoise_hessian(vec3)", "ulp-bounded, coordinate bands 1e2 to 4e6", r => new object[] { BandPoint3(r, 1.0e2, 4.0e6) }, SpanAllowanceRead, Verify: cases => AssertSpanAllowance(cases, hessian: true, small: false)),
}).ToArray();
// A point of magnitude m, m log-uniform in [lo, hi]: one component at +-m, the other two uniform in [-m, m],
// the axis of the large one random.
private static float3 BandPoint3(System.Random r, double lo, double hi)
{
var m = lo * Math.Pow(hi / lo, r.NextDouble());
var v = new double[] { (r.NextDouble() * 2.0 - 1.0) * m, (r.NextDouble() * 2.0 - 1.0) * m, (r.NextDouble() * 2.0 - 1.0) * m };
v[r.Next(3)] = r.Next(2) == 0 ? m : -m;
return new float3((float)v[0], (float)v[1], (float)v[2]);
}
private static object[] SpanArguments(System.Random r)
{
var stage = SpanStageTests.Stage.Draw(r);

View file

@ -178,7 +178,7 @@ public sealed class HlslSourceCompatibilityTests
}
Assert.True(mismatches.Count == 0, $"{mismatches.Count} mismatches:{Environment.NewLine}{string.Join(Environment.NewLine, mismatches.Take(12))}");
Assert.Equal(125, compared.Count);
Assert.Equal(127, compared.Count);
}
/// <summary>

View file

@ -722,6 +722,44 @@ public sealed class NoiseBoundTests
return total;
}
// The gradient of snoise at v in double: 105 sum (m^4 p - 8 m^3 s x) over Corners, the derivative of
// ValueDouble (every corner's x moves one for one with v, dm = -2 x, ds = p).
internal static double[] GradDouble(double vx, double vy, double vz)
{
var (x, p, _) = Corners(vx, vy, vz);
var gradient = new double[3];
for (var c = 0; c < 4; c++)
{
var m = 0.5 - (x[c][0] * x[c][0] + x[c][1] * x[c][1] + x[c][2] * x[c][2]);
if (m <= 0.0)
continue;
var s = p[c][0] * x[c][0] + p[c][1] * x[c][1] + p[c][2] * x[c][2];
for (var i = 0; i < 3; i++)
gradient[i] += 105.0 * (m * m * m * m * p[c][i] - 8.0 * m * m * m * s * x[c][i]);
}
return gradient;
}
/// <summary>
/// GradDouble is the kernel's gradient: within float rounding of snoise_grad (L1 distance at most 2e-4)
/// at 300 seeded points of [-20, 20]^3, the way ExactReferenceIsTheKernel pins ValueDouble.
/// </summary>
[Fact]
public void GradDoubleIsTheKernelsGradient()
{
var random = new System.Random(0x6BAD);
double Coordinate() => random.NextDouble() * 40.0 - 20.0;
for (var k = 0; k < 300; k++)
{
var p = new float3((float)Coordinate(), (float)Coordinate(), (float)Coordinate());
var exact = GradDouble(p.x, p.y, p.z);
var kernel = snoise_grad(p);
var distance = Math.Abs(exact[0] - kernel.x) + Math.Abs(exact[1] - kernel.y) + Math.Abs(exact[2] - kernel.z);
Assert.True(distance <= 2.0e-4, $"GradDouble({p}) is {exact[0]:R}, {exact[1]:R}, {exact[2]:R}, snoise_grad {kernel.x:R}, {kernel.y:R}, {kernel.z:R}");
}
}
private static double Delta(int a, int b) => a == b ? 1.0 : 0.0;
// The symmetric third-derivative tensor of snoise at p, entry (i * 3 + j) * 3 + k, in closed form:

View file

@ -752,6 +752,54 @@ public sealed class SpanStageTests
Assert.True(crossing >= 100, $"only {crossing} of 600 midpoints had an exact sign opposite to the float one: the check has no teeth");
}
/// <summary>
/// The allowance a span computes covers the float kernel's read at every point of its path: the identity
/// stage, the midpoint within |q|_inf 1, a velocity of random direction and speed 1 to 1e3 (log-uniform),
/// a halfLength 1 to 1e3, so path points lie out to about 1e6. At 16 points of the straight path
/// q + v t the value, the gradient's length and the Hessian's Frobenius norm of the float kernel are
/// within math.span_slack(math.span_rounding(...)) of the exact kernel's, the reach term of S_o included.
/// Teeth: the largest value error exceeds the allowance computed with nu = 0 by more than 10 times.
/// </summary>
[Fact]
public void SpanAllowanceCoversReadsAlongTheSpan()
{
var random = new System.Random(0x5EA4);
double worstValue = 0.0, worstGradient = 0.0, worstHessian = 0.0, teeth = 0.0;
for (var k = 0; k < 200; k++)
{
var q = Vector(random, -1.0f, 1.0f);
var speed = LogUniform(random, 1.0f, 1.0e3f);
var velocity = UnitVector(random) * speed;
var halfLength = LogUniform(random, 1.0f, 1.0e3f);
var slack = span_slack(span_rounding(1.0f, q, SpanStage.Identity, None, speed, halfLength));
var midpointSlack = span_slack(span_rounding(1.0f, q, SpanStage.Identity, None, 0.0f, halfLength));
for (var j = 0; j < 16; j++)
{
var x = q + velocity * (halfLength * (j / 7.5f - 1.0f));
var value = NoiseBoundTests.ValueDouble(x.x, x.y, x.z);
var gradient = NoiseBoundTests.GradDouble(x.x, x.y, x.z);
var hessian = NoiseBoundTests.HessianDouble(x.x, x.y, x.z);
var read = snoise_grad(x);
var h = snoise_hessian(x);
var valueError = Math.Abs(read.w - value);
var gradientError = Math.Sqrt(Math.Pow(read.x - gradient[0], 2) + Math.Pow(read.y - gradient[1], 2) + Math.Pow(read.z - gradient[2], 2));
var hessianError = Math.Sqrt(Math.Pow(h._m00 - hessian[0], 2) + Math.Pow(h._m01 - hessian[1], 2) + Math.Pow(h._m02 - hessian[2], 2)
+ Math.Pow(h._m10 - hessian[3], 2) + Math.Pow(h._m11 - hessian[4], 2) + Math.Pow(h._m12 - hessian[5], 2)
+ Math.Pow(h._m20 - hessian[6], 2) + Math.Pow(h._m21 - hessian[7], 2) + Math.Pow(h._m22 - hessian[8], 2));
Assert.True(valueError <= slack.x, $"midpoint {q}, speed {speed:R}, halfLength {halfLength:R}, point {x}: the value error {valueError:R} is past the allowance {slack.x:R}");
Assert.True(gradientError <= slack.y, $"midpoint {q}, speed {speed:R}, halfLength {halfLength:R}, point {x}: the gradient error {gradientError:R} is past the allowance {slack.y:R}");
Assert.True(hessianError <= slack.z, $"midpoint {q}, speed {speed:R}, halfLength {halfLength:R}, point {x}: the Hessian error {hessianError:R} is past the allowance {slack.z:R}");
worstValue = Math.Max(worstValue, valueError / slack.x);
worstGradient = Math.Max(worstGradient, gradientError / slack.y);
worstHessian = Math.Max(worstHessian, hessianError / slack.z);
teeth = Math.Max(teeth, valueError / midpointSlack.x);
}
}
output.WriteLine($"IV-REPORT span allowance: largest fraction of e_n {worstValue:F3}, e_g {worstGradient:F3}, e_h {worstHessian:F3}; largest value error over the allowance without the reach {teeth:F1}");
Assert.True(teeth > 10.0, $"the largest value error is only {teeth:F1} times the allowance without the reach: the check has no teeth");
}
/// <summary>
/// The fold's acceleration term, alone, at the identity stage and under a transform (frequency 2.5, stretch
/// 0.5, shift and offset): the midpoint velocity is zero and the acceleration points along the gradient

View file

@ -12,6 +12,12 @@
// math.Affine.cs (SNOISE_HESSIAN).
static const float CULTMATH_SNOISE_HESSIAN = 55.642143;
// The absolute rounding of one float32 read of cultmath_snoise_grad, value and gradient; the owner is
// math.Affine.cs (SNOISE_READ_VALUE, SNOISE_READ_GRADIENT), read here by cultmath_af_snoise and in
// CultMath.hlsl by cultmath_span_slack.
static const float CULTMATH_SNOISE_READ_VALUE = 6.103515625e-5;
static const float CULTMATH_SNOISE_READ_GRADIENT = 2.44140625e-4;
// An upper bound on the operator norm of the third-derivative tensor of cultmath_snoise(float3), the
// next order after CULTMATH_SNOISE_HESSIAN; provenance in math.Span.cs (SNOISE_THIRD).
static const float CULTMATH_SNOISE_THIRD = 535.8009;
@ -87,7 +93,7 @@ float3 cultmath_af_snoise(float3 centre, float3 axis, float radius)
float lipschitz = CULTMATH_SNOISE_LIPSCHITZ * reach;
float a = dot(gradient, axis);
float remainder = length(gradient) * radius + 0.5 * CULTMATH_SNOISE_HESSIAN * reach * reach;
float allowance = 6.103515625e-5 + 2.44140625e-4 * reach
float allowance = CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_READ_GRADIENT * reach
+ CULTMATH_SNOISE_LIPSCHITZ * 9.5367431640625e-7 * (abs(centre.x) + abs(centre.y) + abs(centre.z) + reach);
bool centred = abs(a) + remainder < lipschitz;
float slope = centred ? a : 0.0;

View file

@ -610,7 +610,8 @@ CultCellular cultmath_cellular(float3 p)
// mirror. They follow the includes because the bounds read CULTMATH_SNOISE_LIPSCHITZ, _HESSIAN and _THIRD.
// A shader caller builds a SpanStage itself (SpanStage(1.0, 1.0, vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0))
// is the identity of math.SpanStage.Identity, which has no shader form); see math.span_stage's comments for
// the span, the transform, the bounds and the allowances.
// the span, the transform, the bounds and the allowances. cultmath_span_rounding and cultmath_span_slack
// are math.span_rounding and math.span_slack, the owners of S_o and of the three allowances.
struct SpanPath
{
float3 midpoint;
@ -637,6 +638,18 @@ struct SpanNoise
float fold;
};
float cultmath_span_rounding(float scale, float3 mq, SpanStage stage, float3 octaveShift, float nu, float halfLength)
{
return 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
}
float3 cultmath_span_slack(float rounding)
{
return float3(CULTMATH_SNOISE_READ_VALUE + CULTMATH_SNOISE_LIPSCHITZ * rounding,
CULTMATH_SNOISE_READ_GRADIENT + CULTMATH_SNOISE_HESSIAN * rounding,
9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding);
}
// The octave stage behind cultmath_span_fbm and cultmath_span_ridged, as math.span_stage is behind span_fbm
// and span_ridged. The Frobenius norm of the Hessian is written out in C#'s order (GLSL has no _mRC member
// access); the C# local named step is octaveShift here (it would shadow GLSL's step()).
@ -679,10 +692,11 @@ SpanNoise cultmath_span_stage(bool ridged, SpanPath path, SpanStage stage, int o
float weight = abs(amplitude);
float factor = abs(scale) * norm;
float nu = factor * path.speed;
float rounding = 9.5367431640625e-7 * (abs(scale) * (cultmath_csum(abs(mq)) + cultmath_csum(abs(stage.shift))) + cultmath_csum(abs(stage.offset)) + cultmath_csum(abs(octaveShift)) + 1.7320508 * nu * halfLength);
float valueSlack = 6.103515625e-5 + CULTMATH_SNOISE_LIPSCHITZ * rounding;
float gradientSlack = 2.44140625e-4 + CULTMATH_SNOISE_HESSIAN * rounding;
float hessianSlack = 9.765625e-4 + CULTMATH_SNOISE_THIRD * rounding;
float rounding = cultmath_span_rounding(scale, mq, stage, octaveShift, nu, halfLength);
float3 slack = cultmath_span_slack(rounding);
float valueSlack = slack.x;
float gradientSlack = slack.y;
float hessianSlack = slack.z;
float3x3 hm = cultmath_snoise_hessian(x);
float frobenius = sqrt(hm[0][0] * hm[0][0] + hm[0][1] * hm[0][1] + hm[0][2] * hm[0][2] + hm[1][0] * hm[1][0] + hm[1][1] * hm[1][1] + hm[1][2] * hm[1][2]
+ hm[2][0] * hm[2][0] + hm[2][1] * hm[2][1] + hm[2][2] * hm[2][2]);