2026-06-10 21:22:43 +01:00
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace CultMath.Tests;
|
|
|
|
|
|
|
|
|
|
public sealed class HlslMirrorTests
|
|
|
|
|
{
|
2026-07-12 19:33:59 +01:00
|
|
|
[Fact]
|
2026-07-22 03:47:38 +01:00
|
|
|
public void HlslMirrorDoesNotPublishGeometryKernels()
|
2026-07-12 19:33:59 +01:00
|
|
|
{
|
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
|
|
|
var source = HlslSourceCompatibilityTests.ReadShaderSource(GetCultMathRoot());
|
2026-07-22 03:47:38 +01:00
|
|
|
Assert.DoesNotContain("AdvancedErosionFilter.hlsl", source);
|
|
|
|
|
Assert.DoesNotContain("Planetary.hlsl", source);
|
|
|
|
|
Assert.DoesNotContain("spherical_erosion", source);
|
|
|
|
|
Assert.DoesNotContain("planetary_radial_refinement", source);
|
2026-07-12 19:49:33 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 21:22:43 +01:00
|
|
|
[Fact]
|
|
|
|
|
public void HlslMirrorPublishesCultMathPrimitives()
|
|
|
|
|
{
|
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
|
|
|
var include = HlslSourceCompatibilityTests.ReadShaderSource(GetCultMathRoot());
|
2026-06-10 21:22:43 +01:00
|
|
|
var requiredSymbols = new[]
|
|
|
|
|
{
|
|
|
|
|
"cultmath_radians",
|
|
|
|
|
"cultmath_degrees",
|
|
|
|
|
"cultmath_frac",
|
|
|
|
|
"cultmath_clamp",
|
|
|
|
|
"cultmath_saturate",
|
|
|
|
|
"cultmath_lerp",
|
|
|
|
|
"cultmath_step",
|
|
|
|
|
"cultmath_smoothstep",
|
|
|
|
|
"cultmath_smootherstep",
|
|
|
|
|
"cultmath_lengthsq",
|
|
|
|
|
"cultmath_distance",
|
|
|
|
|
"cultmath_reflect",
|
2026-07-12 19:33:59 +01:00
|
|
|
"cultmath_rotate",
|
2026-06-10 21:22:43 +01:00
|
|
|
"cultmath_csum",
|
|
|
|
|
"cultmath_decay",
|
|
|
|
|
"cultmath_damp",
|
|
|
|
|
"cultmath_catmullrom",
|
|
|
|
|
"cultmath_quadratic_bezier",
|
|
|
|
|
"cultmath_cubic_bezier",
|
|
|
|
|
"cultmath_hash",
|
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
|
|
|
"cultmath_pcg(uint",
|
|
|
|
|
"cultmath_pcg3d(int3",
|
|
|
|
|
"cultmath_pcg4d(int4",
|
2026-06-10 21:22:43 +01:00
|
|
|
"cultmath_value_noise",
|
Make CultMath HLSL-shaped: writable components, bool-vector comparisons
HLSL is now CultMath's semantic authority where it defines one.
- float2/3/4, int2, double2/3, bool2 and quaternion are plain structs with
public mutable fields instead of record structs. Indexers and swizzles
(float2.yx; float3 xy xz yx yz zy xyz xzy yzx zyx; float4 xy xyz xyw yzx)
have setters.
- ==, !=, <, >, <=, >= are component-wise and return bool2/3/4. Equals and
GetHashCode stay value-based so vectors remain dictionary keys.
quaternion is not an HLSL type and keeps a bool ==.
- Scalar/vector arithmetic and comparisons get explicit overloads on both
sides instead of leaning on implicit splat.
- New types: bool3, bool4, int3, int4, float2x2, float3x3 (row-major like
HLSL, _mRC fields, Rotate / RotateX/Y/Z / Euler with math.RotationOrder).
- New intrinsics: mul (matrix-vector both sides, matrix-matrix), any, all,
select (HLSL argument order), log, vector pow/sqrt/sin/cos/acos/atan2,
double min/max, int3/int4/bool3/bool4/float2x2/float3x3 constructor
functions.
- simplex_noise is renamed snoise and gains the Ashima 3D variant; the HLSL
mirror follows as cultmath_snoise(float2/float3).
- Random gains NextFloat3() and NextFloat3(float3 max) on the same draw order.
Tests pin snoise(float3), float3x3.Euler and float2x2.Rotate to
Unity.Mathematics reference values, and an HLSL-shaped source block compiles
under using static CultMath.math.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 13:29:59 +02:00
|
|
|
"cultmath_snoise(float2",
|
|
|
|
|
"cultmath_snoise(float3",
|
2026-06-10 21:22:43 +01:00
|
|
|
"cultmath_value_noise_bicubic",
|
|
|
|
|
"cultmath_value_noise_texture",
|
|
|
|
|
"cultmath_value_noise_texture_bicubic",
|
Extend the HLSL mirror comparison to one struct return shape
HlslSourceCompatibilityTests previously flattened only bare vector returns to
scalar leaves. Values() now recurses through struct fields of any type, so a
struct return (invariant 8's one shape, e.g. CultCellular) is compared field
by field down to bit-for-bit scalars, exactly like a vector return already
was. Components() stays scoped to scalar-typed fields, since it only ever
builds function arguments and must not recurse.
TransformHlslToCSharp gains transformation 5: struct fields gain `public`,
since HLSL has no field access-modifier concept and a C# struct's fields
default to private, which would hide them from Values()' reflection. The old
wrap-in-a-class transformation is renumbered 6. design.md's transformation
list documents both, matching design.md:92's numbered list.
StructReturnComparisonRejectsAMismatchedField is the extension's own test: two
hand-built structs with the same shape (mirroring a real mirror-vs-C#
comparison) prove the field-by-field, bit-for-bit walk actually rejects a
struct whose leaf field differs, or whose nested vector's own component
differs, not just a length check.
EveryMirrorFunctionMatchesCSharpMath and HlslMirrorTests' required-symbol list
now cover cultmath_smin_grad and cultmath_cellular; the mirrored-function
count moves from 28 to 30.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-25 19:18:21 +02:00
|
|
|
"cultmath_smin_grad",
|
|
|
|
|
"cultmath_cellular",
|
2026-06-10 21:22:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var symbol in requiredSymbols)
|
|
|
|
|
{
|
|
|
|
|
Assert.Contains(symbol, include);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-14 18:04:04 +02:00
|
|
|
// HLSL's normalize intrinsic is the contract; a mirror copy would only restate it.
|
|
|
|
|
Assert.DoesNotContain("cultmath_normalize", include);
|
2026-06-10 21:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
[Theory]
|
|
|
|
|
[InlineData("CultMath.hlsl")]
|
|
|
|
|
[InlineData("CultMath.Phacelle.hlsl")]
|
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>
2026-10-02 19:52:33 +02:00
|
|
|
[InlineData("CultMath.Interval.hlsl")]
|
2026-10-04 09:47:49 +02:00
|
|
|
[InlineData("CultMath.Affine.hlsl")]
|
CultMath: Cycles node semantics (Math, Map Range, Clamp, Mix, RGB to BW, ramp index) as math.Cycles.cs and CultMath.Cycles.hlsl under Apache-2.0
Ports of Blender v5.2.2's intern/cycles kernel functions, one function per node
operation, with the HLSL mirror, the Unity copy, the lowering table entry for a
separate Apache-2.0 GLSL file, the golden-fixture families, the package licence
expressions and the notices section.
Builds. Does not pass yet: CultMath.Cycles.glsl and the regenerated golden
fixture arrive in a later commit, so CommittedGlslEqualsLowering and
GoldenFixtureMatchesCSharp fail at this commit.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 03:59:59 +02:00
|
|
|
[InlineData("CultMath.Cycles.hlsl")]
|
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
|
|
|
public void UnityPackageShaderIsIdenticalToTheCanonicalShader(string name)
|
|
|
|
|
{
|
|
|
|
|
var root = GetCultMathRoot();
|
|
|
|
|
Assert.Equal(
|
|
|
|
|
File.ReadAllText(Path.Combine(root, "shaders", name)).ReplaceLineEndings("\n"),
|
|
|
|
|
File.ReadAllText(Path.Combine(root, "unity", "org.gamecult.cultmath", "Shaders", name)).ReplaceLineEndings("\n"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:35:51 +02:00
|
|
|
private static string GetCultMathRoot()
|
2026-06-10 21:22:43 +01:00
|
|
|
{
|
|
|
|
|
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
|
|
|
|
while (directory is not null)
|
|
|
|
|
{
|
2026-08-26 17:35:51 +02:00
|
|
|
var monorepoPackage = Path.Combine(directory.FullName, "packages", "cultmath");
|
|
|
|
|
if (File.Exists(Path.Combine(monorepoPackage, "shaders", "CultMath.hlsl")))
|
2026-06-10 21:22:43 +01:00
|
|
|
{
|
2026-08-26 17:35:51 +02:00
|
|
|
return monorepoPackage;
|
2026-06-10 21:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
directory = directory.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-26 17:35:51 +02:00
|
|
|
throw new InvalidOperationException("Could not locate the CultMath package root.");
|
2026-06-10 21:22:43 +01:00
|
|
|
}
|
|
|
|
|
}
|