diff --git a/packages/cultmath/docs/design.md b/packages/cultmath/docs/design.md index f43f48e2..f5f5d5a5 100644 --- a/packages/cultmath/docs/design.md +++ b/packages/cultmath/docs/design.md @@ -120,6 +120,18 @@ compiled under `using static CultMath.math;`, so its intrinsics are C# `math` itself: the test proves the composition in the text, not the intrinsic rules, which only `HlslSemanticsTests` pins. +Integer hashing uses the PCG hashes from Jarzynski and Olano, "Hash Functions +for GPU Rendering" (JCGT 9(3), 2020): `pcg(uint)` is O'Neill's RXS-M-XS 32/32 +permutation over one LCG step, and `pcg3d`/`pcg4d` are the paper's (3 → 3) and +(4 → 4) hashes. They use only uint multiply, add, xor, and shifts. dxc wraps +uint multiply and add modulo 2^32 exactly as C# unchecked arithmetic does, so +they are integer-exact in HLSL and on the CPU, GPU included, unlike the +`sin`-based `hash`. CultMath has no uint vectors, so `pcg3d(int3)` and +`pcg4d(int4)` carry uint bit patterns in int components. The `float2`/`float3`/`float4` +overloads hash the IEEE-754 bits (`asuint`), so -0 and 0 hash differently. +`pcg3d(float2)` holds z at 0, which is the paper's route for (2 → N) inputs. +Seeds and other scalar uses take one output component. + Where HLSL is silent, CultMath keeps its own decisions and does not defer to Unity.Mathematics: `hash` returns float, and `Random` is CultMath's own xorshift32. Engine-shaped helpers that HLSL lacks (`float2x2.Rotate`, `float3x3.Euler`, `quaternion`, diff --git a/packages/cultmath/shaders/CultMath.hlsl b/packages/cultmath/shaders/CultMath.hlsl index afffb2c2..6179d45a 100644 --- a/packages/cultmath/shaders/CultMath.hlsl +++ b/packages/cultmath/shaders/CultMath.hlsl @@ -281,6 +281,38 @@ float cultmath_hash(float value) { return cultmath_frac(sin(value) * 43758.5453) float cultmath_hash(float2 value) { return cultmath_hash(dot(value, float2(127.1, 311.7))); } float cultmath_hash(float3 value) { return cultmath_hash(dot(value, float3(127.1, 311.7, 74.7))); } +// PCG hashes (Jarzynski and Olano, JCGT 2020), written over scalar uints so CultMath's int3/int4 carry the bits. +uint cultmath_pcg(uint value) +{ + uint state = value * 747796405u + 2891336453u; + uint word = ((state >> (int)((state >> 28) + 4)) ^ state) * 277803737u; + return (word >> 22) ^ word; +} + +int3 cultmath_pcg3d(int3 value) +{ + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u, z = (uint)value.z * 1664525u + 1013904223u; + x += y * z; y += z * x; z += x * y; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; + x += y * z; y += z * x; z += x * y; + return int3((int)x, (int)y, (int)z); +} + +int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } +int3 cultmath_pcg3d(float3 value) { return cultmath_pcg3d(int3((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z))); } + +int4 cultmath_pcg4d(int4 value) +{ + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u; + uint z = (uint)value.z * 1664525u + 1013904223u, w = (uint)value.w * 1664525u + 1013904223u; + x += y * w; y += z * x; z += x * y; w += y * z; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; w ^= w >> 16; + x += y * w; y += z * x; z += x * y; w += y * z; + return int4((int)x, (int)y, (int)z, (int)w); +} + +int4 cultmath_pcg4d(float4 value) { return cultmath_pcg4d(int4((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z), (int)asuint(value.w))); } + float cultmath_value_noise(float2 position) { float2 cell = floor(position); diff --git a/packages/cultmath/src/CultMath/CultMath.csproj b/packages/cultmath/src/CultMath/CultMath.csproj index efa883dd..8474fc05 100644 --- a/packages/cultmath/src/CultMath/CultMath.csproj +++ b/packages/cultmath/src/CultMath/CultMath.csproj @@ -8,7 +8,7 @@ CultMath CultMath GameCult.Math - 0.2.2 + 0.2.3 Clean-room C# math shaped for HLSL-style renderer code. GameCult README.md diff --git a/packages/cultmath/src/CultMath/math.cs b/packages/cultmath/src/CultMath/math.cs index 7e8757ba..99b3c234 100644 --- a/packages/cultmath/src/CultMath/math.cs +++ b/packages/cultmath/src/CultMath/math.cs @@ -382,6 +382,43 @@ public static partial class math public static float hash(float2 value) => hash(dot(value, new float2(127.1f, 311.7f))); 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); + + // 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. + public static uint pcg(uint value) + { + var state = value * 747796405u + 2891336453u; + var word = ((state >> (int)((state >> 28) + 4)) ^ state) * 277803737u; + return (word >> 22) ^ word; + } + + public static int3 pcg3d(int3 value) + { + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u, z = (uint)value.z * 1664525u + 1013904223u; + x += y * z; y += z * x; z += x * y; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; + x += y * z; y += z * x; z += x * y; + return new int3((int)x, (int)y, (int)z); + } + + // The paper hashes 2 -> N inputs through pcg3d with the unused component held constant. + public static int3 pcg3d(float2 value) => pcg3d(new float3(value, 0.0f)); + public static int3 pcg3d(float3 value) => pcg3d(new int3((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z))); + + public static int4 pcg4d(int4 value) + { + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u; + uint z = (uint)value.z * 1664525u + 1013904223u, w = (uint)value.w * 1664525u + 1013904223u; + x += y * w; y += z * x; z += x * y; w += y * z; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; w ^= w >> 16; + x += y * w; y += z * x; z += x * y; w += y * z; + return new int4((int)x, (int)y, (int)z, (int)w); + } + + public static int4 pcg4d(float4 value) => + pcg4d(new int4((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z), (int)asuint(value.w))); + public static float log(float value) => MathF.Log(value); public static float2 log(float2 value) => new(log(value.x), log(value.y)); public static float3 log(float3 value) => new(log(value.x), log(value.y), log(value.z)); diff --git a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs index 818fa3ed..d73c6d7b 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs @@ -40,6 +40,9 @@ public sealed class HlslMirrorTests "cultmath_quadratic_bezier", "cultmath_cubic_bezier", "cultmath_hash", + "cultmath_pcg(uint", + "cultmath_pcg3d(int3", + "cultmath_pcg4d(int4", "cultmath_value_noise", "cultmath_snoise(float2", "cultmath_snoise(float3", diff --git a/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs index 665f2c02..954c4375 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs @@ -209,6 +209,11 @@ public sealed class HlslSemanticsTests Assert.Equal(Bits(0.0f), Bits(math.length(float4.zero))); Assert.Equal(Bits(0.0f), Bits(math.distance(new float3(1.0f, 2.0f, 3.0f), new float3(1.0f, 2.0f, 3.0f)))); + // asuint(float) reinterprets the IEEE-754 bits. + Assert.Equal(0x80000000u, math.asuint(-0.0f)); + Assert.Equal(0x3FC00000u, math.asuint(1.5f)); + Assert.Equal(0x7FC00000u, math.asuint(BitConverter.Int32BitsToSingle(0x7FC00000))); + Assert.Equal(0, math.sign(float.NaN)); Assert.Equal(-1, math.sign(-0.5f)); Assert.Equal(new int3(-1, 0, 1), math.sign(new float3(-2.0f, 0.0f, 4.0f))); diff --git a/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs index c6ece795..9426e4c8 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs @@ -78,26 +78,33 @@ public sealed class HlslSourceCompatibilityTests foreach (var input in cases) { var slot = 0; - var args = types.Select(t => t == typeof(float) ? (object)input(slot++) - : Activator.CreateInstance(t, Enumerable.Range(0, Components(t)).Select(_ => (object)input(slot++)).ToArray())!).ToArray(); - var expected = Floats(counterpart.Invoke(null, args)!); - var actual = Floats(mirror.Invoke(shader, args)!); - if (expected.Length != actual.Length || expected.Zip(actual).Any(p => !(float.IsNaN(p.First) && float.IsNaN(p.Second)) - && BitConverter.SingleToInt32Bits(p.First) != BitConverter.SingleToInt32Bits(p.Second))) + var args = types.Select(t => Scalars.Contains(t) ? Scalar(t, input(slot++)) + : Activator.CreateInstance(t, Components(t).Select(f => Scalar(f.FieldType, input(slot++))).ToArray())!).ToArray(); + var expected = Values(counterpart.Invoke(null, args)!); + var actual = Values(mirror.Invoke(shader, args)!); + if (expected.Length != actual.Length || expected.Zip(actual).Any(p => p.First is float a && p.Second is float b + ? !(float.IsNaN(a) && float.IsNaN(b)) && BitConverter.SingleToInt32Bits(a) != BitConverter.SingleToInt32Bits(b) + : !p.First.Equals(p.Second))) { - mismatches.Add($"{mirror.Name}({string.Join(", ", args.Select(a => string.Join(" ", Floats(a))))}): C# {string.Join(" ", expected)}, mirror {string.Join(" ", actual)}"); + mismatches.Add($"{mirror.Name}({string.Join(", ", args.Select(a => string.Join(" ", Values(a))))}): C# {string.Join(" ", expected)}, mirror {string.Join(" ", actual)}"); } } } Assert.True(mismatches.Count == 0, $"{mismatches.Count} mismatches:{Environment.NewLine}{string.Join(Environment.NewLine, mismatches.Take(12))}"); - Assert.Equal(25, compared.Count); + Assert.Equal(28, compared.Count); } - private static int Components(Type t) => t.GetFields().Count(f => f.FieldType == typeof(float) && !f.IsStatic); + // Integer arguments take the bit pattern of the float input, so specials become 0, 0x80000000, NaN bits, ... + private static readonly Type[] Scalars = { typeof(float), typeof(int), typeof(uint) }; - private static float[] Floats(object value) => value is float f ? new[] { f } - : value.GetType().GetFields().Where(field => field.FieldType == typeof(float) && !field.IsStatic).Select(field => (float)field.GetValue(value)!).ToArray(); + private static object Scalar(Type t, float f) => t == typeof(float) ? f + : t == typeof(int) ? BitConverter.SingleToInt32Bits(f) : (object)(uint)BitConverter.SingleToInt32Bits(f); + + private static FieldInfo[] Components(Type t) => t.GetFields().Where(f => !f.IsStatic && Scalars.Contains(f.FieldType)).ToArray(); + + private static object[] Values(object value) => Scalars.Contains(value.GetType()) ? new[] { value } + : Components(value.GetType()).Select(field => field.GetValue(value)!).ToArray(); /// The documented HLSL-to-C# transformations, and nothing else. internal static string TransformHlslToCSharp(string hlsl) diff --git a/packages/cultmath/tests/CultMath.Tests/MathTests.cs b/packages/cultmath/tests/CultMath.Tests/MathTests.cs index de1bf996..534ec276 100644 --- a/packages/cultmath/tests/CultMath.Tests/MathTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/MathTests.cs @@ -12,6 +12,41 @@ public sealed class MathTests Assert.Equal(0.75f, math.frac(-1.25f)); } + // Expected values come from a Python transcription of the paper's pcg3d/pcg4d (JCGT 9(3) 2020, p. 32) and + // O'Neill's pcg_output_rxs_m_xs_32_32 with PCG_DEFAULT_MULTIPLIER_32/INCREMENT_32, independent of this code. + [Fact] + public void PcgHashesMatchReferenceTranscription() + { + static int3 U3(uint x, uint y, uint z) => new((int)x, (int)y, (int)z); + static int4 U4(uint x, uint y, uint z, uint w) => new((int)x, (int)y, (int)z, (int)w); + + Assert.Equal(0x07BB2FE2u, math.pcg(0u)); + Assert.Equal(0xA8BEEA3Cu, math.pcg(1u)); + Assert.Equal(0x995312E1u, math.pcg(0x12345678u)); + Assert.Equal(0xE62A4902u, math.pcg(0xFFFFFFFFu)); + Assert.Equal(U3(0x9BAFD7C6u, 0xA8E88A6Bu, 0x3F15482Cu), math.pcg3d(new int3(0, 0, 0))); + Assert.Equal(U3(0xFA9F79A6u, 0x48F2F44Cu, 0x596F5AB1u), math.pcg3d(new int3(1, 2, 3))); + Assert.Equal(U3(0x67DCF282u, 0x49E87E17u, 0x10018917u), math.pcg3d(U3(0xFFFFFFFFu, 0x80000000u, 0x7FFFFFFFu))); + Assert.Equal(U4(0x0F02F829u, 0x2D568769u, 0x32B0C43Bu, 0xD32548EAu), math.pcg4d(new int4(0, 0, 0, 0))); + Assert.Equal(U4(0x3622CD16u, 0xF11471D8u, 0xE1109B3Fu, 0x02B94C2Fu), math.pcg4d(new int4(1, 2, 3, 4))); + Assert.Equal(U4(0x72180037u, 0x81D493E6u, 0xD9BA7226u, 0x14C9ABA7u), math.pcg4d(U4(0xFFFFFFFFu, 0x80000000u, 0x7FFFFFFFu, 0xDEADBEEFu))); + Assert.Equal(U3(0xA7B40FEEu, 0x9B70DFC0u, 0x3106C2C5u), math.pcg3d(new float3(1.5f, -2.25f, 1e7f))); + Assert.Equal(U3(0x5A09E5C6u, 0x53EC0DABu, 0x4BE4A1ECu), math.pcg3d(new float2(3.0f, 4.0f))); + Assert.Equal(U4(0x1849E380u, 0xF199DEE0u, 0x6C6B4934u, 0x34AF16DFu), + math.pcg4d(new float4(-0.0f, BitConverter.Int32BitsToSingle(0x7FC00000), 0.5f, 1e-7f))); + } + + [Fact] + public void Pcg3dSeparatesAPositionGrid() + { + // A single 32-bit component is subject to birthday collisions (x has one here); the full output is distinct. + var hashes = new HashSet(); + for (var i = 0; i < 100; i++) + for (var j = 0; j < 100; j++) + hashes.Add(math.pcg3d(new float2(i * 0.5f - 25.0f, j * 0.5f - 25.0f))); + Assert.Equal(10000, hashes.Count); + } + [Fact] public void VectorOperationsAreComponentWise() { diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.dll b/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.dll index 3cfc7618..ef93b69b 100644 Binary files a/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.dll and b/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.dll differ diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.pdb b/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.pdb index 169a7117..0bc1d414 100644 Binary files a/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.pdb and b/packages/cultmath/unity/org.gamecult.cultmath/Runtime/Plugins/CultMath.pdb differ diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl index afffb2c2..6179d45a 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl @@ -281,6 +281,38 @@ float cultmath_hash(float value) { return cultmath_frac(sin(value) * 43758.5453) float cultmath_hash(float2 value) { return cultmath_hash(dot(value, float2(127.1, 311.7))); } float cultmath_hash(float3 value) { return cultmath_hash(dot(value, float3(127.1, 311.7, 74.7))); } +// PCG hashes (Jarzynski and Olano, JCGT 2020), written over scalar uints so CultMath's int3/int4 carry the bits. +uint cultmath_pcg(uint value) +{ + uint state = value * 747796405u + 2891336453u; + uint word = ((state >> (int)((state >> 28) + 4)) ^ state) * 277803737u; + return (word >> 22) ^ word; +} + +int3 cultmath_pcg3d(int3 value) +{ + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u, z = (uint)value.z * 1664525u + 1013904223u; + x += y * z; y += z * x; z += x * y; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; + x += y * z; y += z * x; z += x * y; + return int3((int)x, (int)y, (int)z); +} + +int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } +int3 cultmath_pcg3d(float3 value) { return cultmath_pcg3d(int3((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z))); } + +int4 cultmath_pcg4d(int4 value) +{ + uint x = (uint)value.x * 1664525u + 1013904223u, y = (uint)value.y * 1664525u + 1013904223u; + uint z = (uint)value.z * 1664525u + 1013904223u, w = (uint)value.w * 1664525u + 1013904223u; + x += y * w; y += z * x; z += x * y; w += y * z; + x ^= x >> 16; y ^= y >> 16; z ^= z >> 16; w ^= w >> 16; + x += y * w; y += z * x; z += x * y; w += y * z; + return int4((int)x, (int)y, (int)z, (int)w); +} + +int4 cultmath_pcg4d(float4 value) { return cultmath_pcg4d(int4((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z), (int)asuint(value.w))); } + float cultmath_value_noise(float2 position) { float2 cell = floor(position); diff --git a/packages/cultmath/unity/org.gamecult.cultmath/package.json b/packages/cultmath/unity/org.gamecult.cultmath/package.json index 9b13645b..6ab136b4 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/package.json +++ b/packages/cultmath/unity/org.gamecult.cultmath/package.json @@ -1,7 +1,7 @@ { "name": "org.gamecult.cultmath", "displayName": "CultMath", - "version": "0.2.2", + "version": "0.2.3", "unity": "2021.3", "description": "Precompiled CultMath numeric primitives and matching HLSL semantics for GameCult Unity runtimes.", "license": "MPL-2.0",