From 111b918ec06c5b96563cc970124be3442821ffcd Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 14 Sep 2026 18:04:04 +0200 Subject: [PATCH] Make CultMath normalize follow dxc and release 0.2.2 dxc 1.9.0.5402 lowers float2/3/4 normalize(x) to x * Rsqrt(Dot(x, x)), and DXIL.rst defines Rsqrt as 1 / sqrt(src). A zero vector normalizes to NaN and a NaN component poisons every component. CultMath divided by max(length, 1e-20), which returned 0 for a zero vector and a huge finite component beside a NaN one. - math.normalize(float2/3/4) is now value * (1 / sqrt(dot(value, value))). - The HLSL mirror drops cultmath_normalize and its epsilon constant: it now equals the normalize intrinsic, and nothing consumed it. - HlslSemanticsTests pins zero and NaN normalize, the rsqrt bit pattern, and zero length/distance. design.md states the rule and notes that DXIL.rst's Rsqrt special-value table is a copy of Round's. - GameCult.Geometry's iso-surface gave degenerate triangles (an inside vertex on the isovalue) a zero normal through the old epsilon. It now guards them explicitly, with a test. - quaternion normalize keeps its identity fallback: quaternion is not an HLSL type. - Bump to 0.2.2 and rebuild the tracked Unity DLL and pdb. Co-Authored-By: Claude Opus 5 --- packages/cultmath/README.md | 5 ++-- packages/cultmath/docs/design.md | 15 ++++++++---- packages/cultmath/shaders/CultMath.hlsl | 5 ---- .../cultmath/src/CultMath/CultMath.csproj | 2 +- packages/cultmath/src/CultMath/math.cs | 7 +++--- .../tests/CultMath.Tests/HlslMirrorTests.cs | 5 ++-- .../CultMath.Tests/HlslSemanticsTests.cs | 22 ++++++++++++++++++ .../HlslSourceCompatibilityTests.cs | 2 +- .../Runtime/Plugins/CultMath.dll | Bin 299008 -> 299008 bytes .../Runtime/Plugins/CultMath.pdb | Bin 142220 -> 142220 bytes .../Shaders/CultMath.hlsl | 5 ---- .../unity/org.gamecult.cultmath/package.json | 2 +- .../CultGeometryIsoSurface.cs | 4 +++- .../IsoSurfaceTests.cs | 17 ++++++++++++++ 14 files changed, 63 insertions(+), 28 deletions(-) diff --git a/packages/cultmath/README.md b/packages/cultmath/README.md index 0be46738..0f9a99fe 100644 --- a/packages/cultmath/README.md +++ b/packages/cultmath/README.md @@ -31,8 +31,7 @@ shader semantics. deterministic value-noise primitives - `shaders/CultMath.hlsl`, a canonical HLSL mirror include for shader-side parity. HLSL already owns `float2`, `float3`, and `float4`; the include - exposes `cultmath_*` functions for shared semantics, including CultMath's - safe `normalize` contract. + exposes `cultmath_*` functions for shared semantics HLSL does not provide. - `Voronoi.SampleTones`, a C# batch surface that calls the Rust `cultmath-core` native kernel when `cultmath_core` is available and falls back to the managed parity path otherwise. @@ -59,7 +58,7 @@ float grain = math.value_noise_bicubic(math.float2(12.0f, 3.5f)); ```hlsl #include "CultMath.hlsl" -float3 normal = cultmath_normalize(float3(0.25, 1.0, -0.1)); +float3 normal = normalize(float3(0.25, 1.0, -0.1)); float rim = cultmath_saturate(1.0 - dot(normal, float3(0.0, 0.0, -1.0))); float glow = cultmath_smoothstep(0.2, 1.0, rim); float grain = cultmath_value_noise_bicubic(float2(12.0, 3.5)); diff --git a/packages/cultmath/docs/design.md b/packages/cultmath/docs/design.md index ad8409fd..f43f48e2 100644 --- a/packages/cultmath/docs/design.md +++ b/packages/cultmath/docs/design.md @@ -60,6 +60,14 @@ That makes these HLSL rules, not options: zero; numeric to bool is `!= 0` (`fcmp une`, so NaN is true); bool to numeric is 0 or 1. DXIL leaves float-to-int undefined for NaN and out-of-range values, and so does C#; CultMath does not pin them. +- `normalize(x)` is `x * Rsqrt(Dot(x, x))`, which is how dxc lowers it, and + DXIL.rst defines `Rsqrt` as `1 / sqrt(src)`. A zero vector therefore + normalizes to NaN (`0 * inf`), and a NaN component makes every component NaN. + (The special-value table under `Rsqrt` in DXIL.rst is a copy of the `Round` + table and contradicts that definition, so CultMath follows the definition.) + `length` and `distance` are `Sqrt` of the summed squares, so a zero vector + gives 0. Callers that can meet zero-length vectors guard them before calling + `normalize`. - dxc demotes double `frac`, `exp`, `lerp`, and `floor` to float. CultMath's double overloads keep double precision instead: `frac(double)` is `x - floor(x)` in double. That is a deliberate divergence for CPU simulation @@ -75,9 +83,7 @@ GPU. Because `min`, `max`, and `saturate` return the non-NaN operand, some functions turn NaN or infinity into numbers: `smoothstep(a, a, a)` is 0; infinite inputs to `smoothstep` and the intercept helpers give numbers; the bezier, `smoothstep01`, and `smootherstep` functions return finite values for -NaN input; and `normalize((NaN, 2.41))` is `(NaN, 2.41e20)`, because the NaN -length is clamped to the epsilon and the other component is divided by it. -Callers that must detect bad data should check `isnan` before calling these +NaN input. Callers that must detect bad data should check `isnan` before calling these functions. Deferred until a consumer needs them: `float4x4`, `transpose`, `determinant`, @@ -115,8 +121,7 @@ itself: the test proves the composition in the text, not the intrinsic rules, which only `HlslSemanticsTests` pins. Where HLSL is silent, CultMath keeps its own decisions and does not defer to -Unity.Mathematics: `normalize` is safe (divides by `max(length, 1e-20)`), -`hash` returns float, and `Random` is CultMath's own xorshift32. Engine-shaped +Unity.Mathematics: `hash` returns float, and `Random` is CultMath's own xorshift32. Engine-shaped helpers that HLSL lacks (`float2x2.Rotate`, `float3x3.Euler`, `quaternion`, `snoise`) enter only when a consumer needs them, with their semantics stated in code. diff --git a/packages/cultmath/shaders/CultMath.hlsl b/packages/cultmath/shaders/CultMath.hlsl index 365705f5..afffb2c2 100644 --- a/packages/cultmath/shaders/CultMath.hlsl +++ b/packages/cultmath/shaders/CultMath.hlsl @@ -11,7 +11,6 @@ static const float CULTMATH_PI = 3.14159265358979323846; static const float CULTMATH_TAU = 6.28318530717958647692; static const float CULTMATH_HALF_PI = 1.57079632679489661923; -static const float CULTMATH_NORMALIZE_EPSILON = 1.0e-20; float cultmath_radians(float degrees_value) { return degrees_value * (CULTMATH_PI / 180.0); } float cultmath_degrees(float radians_value) { return radians_value * (180.0 / CULTMATH_PI); } @@ -171,10 +170,6 @@ float cultmath_distance(float2 left, float2 right) { return length(left - right) float cultmath_distance(float3 left, float3 right) { return length(left - right); } float cultmath_distance(float4 left, float4 right) { return length(left - right); } -float2 cultmath_normalize(float2 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } -float3 cultmath_normalize(float3 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } -float4 cultmath_normalize(float4 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } - float2 cultmath_reflect(float2 incident, float2 normal_value) { return incident - 2.0 * dot(normal_value, incident) * normal_value; diff --git a/packages/cultmath/src/CultMath/CultMath.csproj b/packages/cultmath/src/CultMath/CultMath.csproj index 1c2bfe24..efa883dd 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.1 + 0.2.2 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 f5c81095..7e8757ba 100644 --- a/packages/cultmath/src/CultMath/math.cs +++ b/packages/cultmath/src/CultMath/math.cs @@ -213,9 +213,10 @@ public static partial class math public static float distance(float3 left, float3 right) => length(left - right); public static float distance(float4 left, float4 right) => length(left - right); - public static float2 normalize(float2 value) => value / max(length(value), 1.0e-20f); - public static float3 normalize(float3 value) => value / max(length(value), 1.0e-20f); - public static float4 normalize(float4 value) => value / max(length(value), 1.0e-20f); + // dxc lowers normalize(x) to x * Rsqrt(Dot(x, x)), and DXIL Rsqrt is 1 / sqrt(src): a zero vector is 0 * inf = NaN. + public static float2 normalize(float2 value) => value * (1.0f / MathF.Sqrt(dot(value, value))); + public static float3 normalize(float3 value) => value * (1.0f / MathF.Sqrt(dot(value, value))); + public static float4 normalize(float4 value) => value * (1.0f / MathF.Sqrt(dot(value, value))); public static quaternion normalize(quaternion value) { var length = MathF.Sqrt(value.x * value.x + value.y * value.y + value.z * value.z + value.w * value.w); diff --git a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs index 445ad255..818fa3ed 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslMirrorTests.cs @@ -31,7 +31,6 @@ public sealed class HlslMirrorTests "cultmath_smootherstep", "cultmath_lengthsq", "cultmath_distance", - "cultmath_normalize", "cultmath_reflect", "cultmath_rotate", "cultmath_csum", @@ -54,8 +53,8 @@ public sealed class HlslMirrorTests Assert.Contains(symbol, include); } - Assert.Contains("CULTMATH_NORMALIZE_EPSILON", include); - Assert.Contains("max(length(value), CULTMATH_NORMALIZE_EPSILON)", include); + // HLSL's normalize intrinsic is the contract; a mirror copy would only restate it. + Assert.DoesNotContain("cultmath_normalize", include); } private static string GetCultMathRoot() diff --git a/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs b/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs index ac97f92f..665f2c02 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslSemanticsTests.cs @@ -187,6 +187,28 @@ public sealed class HlslSemanticsTests Assert.Equal(DoubleBits(-0.0), DoubleBits(math.max(-0.0, 0.0))); Assert.Equal(DoubleBits(0.0), DoubleBits(math.min(-0.0, 0.0))); + // dxc: normalize(x) is x * Rsqrt(Dot(x, x)); length and distance are Sqrt of the summed squares. + static bool AllNaN(float4 value) => + float.IsNaN(value.x) && float.IsNaN(value.y) && float.IsNaN(value.z) && float.IsNaN(value.w); + var n2 = math.normalize(float2.zero); + Assert.True(float.IsNaN(n2.x) && float.IsNaN(n2.y)); + var n3 = math.normalize(float3.zero); + Assert.True(float.IsNaN(n3.x) && float.IsNaN(n3.y) && float.IsNaN(n3.z)); + Assert.True(AllNaN(math.normalize(float4.zero))); + var nanX = math.normalize(new float2(float.NaN, 2.41f)); + Assert.True(float.IsNaN(nanX.x) && float.IsNaN(nanX.y)); + Assert.True(AllNaN(math.normalize(new float4(1.0f, float.NaN, 2.41f, 1.0f)))); + var skew = new float3(0.25f, 1.0f, -0.1f); + var inverseLength = 1.0f / MathF.Sqrt(skew.x * skew.x + skew.y * skew.y + skew.z * skew.z); + var normalized = math.normalize(skew); + Assert.Equal(Bits(skew.x * inverseLength), Bits(normalized.x)); + Assert.Equal(Bits(skew.y * inverseLength), Bits(normalized.y)); + Assert.Equal(Bits(skew.z * inverseLength), Bits(normalized.z)); + Assert.Equal(Bits(0.0f), Bits(math.length(float2.zero))); + Assert.Equal(Bits(0.0f), Bits(math.length(float3.zero))); + 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)))); + 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 ca4584c7..c6ece795 100644 --- a/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/HlslSourceCompatibilityTests.cs @@ -91,7 +91,7 @@ public sealed class HlslSourceCompatibilityTests } Assert.True(mismatches.Count == 0, $"{mismatches.Count} mismatches:{Environment.NewLine}{string.Join(Environment.NewLine, mismatches.Take(12))}"); - Assert.Equal(26, compared.Count); + Assert.Equal(25, compared.Count); } private static int Components(Type t) => t.GetFields().Count(f => f.FieldType == typeof(float) && !f.IsStatic); 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 d3c4a62fa9d4b99e1c064d91904c32608a6df662..3cfc761833b884bc7c9906223f7124db65f46828 100644 GIT binary patch delta 3425 zcmY+{4^R}>9S87zmwi8WxqZ99fgW<`sb^snP6WjNku#bQ#0)YR&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S!-PYIjDebA>^aRsf(b9YbTkg{t6bOved<}5_QbF6xO0TY|CIhDo1@5^v#^_kvZye zs6Z8|&%tigV)fVX7V0_md1ywhRoB2#)GO*b2%rY6FF_~j6Whyh7Nx7dfgYXHMQ%X} zTt=0trEnEhrLKqTT&u`l^%dwx)vLdSK~z@Mt3bw^4`vlg4O_S z5!DJ`CXdh>Az;RYEG0oZ2%Pe z1Vv%##z3blU2BHrJUEpQxdlp5Q?(YTL`~P)pq1;JI3gc`%Ul}>80tsO){ekKlwW%n zoD(ApjokdBkQ$L5zTRm0IMkUzDbJX+9U8g*k{nhGSG@hc)DB%-D~xMuhktTqYvIh> zT*c0?CMhXh;!^^a@;G7m# zjqteEqFn%ImZ?E`qjm{)Wt%F?w{>e@fcP_$8Pj5oHbTd=LXH*Q#|MJValnpDtD5y{CZ3;T(oCOwa-&rmA19UN=EsOZff&thv=pP= zF=BV>O3TH^qnrFM6whf`He*{-jJK!dDMwvvxj1hE?|u6t-fK+0?yb3n`o*2=dcTja zs?6qJImTSV{P1~l&13Ooc3@V8*iC9ur8;$|xJ4#HcrixlK;2I9PlTie=2eQnFphNx Ys>F2T@M)De#j<*GAf{TpYYdC}KTluauK)l5 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 896e41b0980de8be6bdce8384f09554296d8418c..169a71177222918626f6f0725af26bfb959832cc 100644 GIT binary patch delta 1469 zcmZY7dsNe97zgloKNxHY1R}~s*@keUF@(f{II#Kx;Dr#Hd7h2rh-Ez?gzNjy)~#>7 z&RXZq*YB7)pfR7zUL3^E++r_DUD}fNqy5{PhdcWtc5V2iwz9P|jg?}jzV9*?Gc=Of zpRU-Kmy!qhtKOTLUu(k9>BgFuyOi&@hq% znyKd6*rJ$wZ7-Jh>>XL47M|q#uFnXfciG!3rxm3;Zb-FF0c>UC?&jQT{xHYGC4P8o zx1Ze0aPfLXFXfr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QZGt!iGDc(`^+X7fDv+`1 zMa;X!7PbWkAr5TQ2Om;>egrZkJO7vLX0xdtHcEWgbKt#%x-~mEw9ZVgHDoCDKd0Y} zJ!G$@s;-cO--H(&!E%6fqjEj~-n&6ARt!h6Yv5P_RhL2@HUaXn0yyqL0Q6D~1X%*t zsbI-==%*c|WT(I=o4jU^kUvbSYn+I?48A&DNNoLB;!n+S&kdmsr417s@^G<>gZ^KT S8>xOHBwQGuY6Z@O!sI{SJ;9m) diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl index 365705f5..afffb2c2 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl @@ -11,7 +11,6 @@ static const float CULTMATH_PI = 3.14159265358979323846; static const float CULTMATH_TAU = 6.28318530717958647692; static const float CULTMATH_HALF_PI = 1.57079632679489661923; -static const float CULTMATH_NORMALIZE_EPSILON = 1.0e-20; float cultmath_radians(float degrees_value) { return degrees_value * (CULTMATH_PI / 180.0); } float cultmath_degrees(float radians_value) { return radians_value * (180.0 / CULTMATH_PI); } @@ -171,10 +170,6 @@ float cultmath_distance(float2 left, float2 right) { return length(left - right) float cultmath_distance(float3 left, float3 right) { return length(left - right); } float cultmath_distance(float4 left, float4 right) { return length(left - right); } -float2 cultmath_normalize(float2 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } -float3 cultmath_normalize(float3 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } -float4 cultmath_normalize(float4 value) { return value / max(length(value), CULTMATH_NORMALIZE_EPSILON); } - float2 cultmath_reflect(float2 incident, float2 normal_value) { return incident - 2.0 * dot(normal_value, incident) * normal_value; diff --git a/packages/cultmath/unity/org.gamecult.cultmath/package.json b/packages/cultmath/unity/org.gamecult.cultmath/package.json index 29c93763..9b13645b 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.1", + "version": "0.2.2", "unity": "2021.3", "description": "Precompiled CultMath numeric primitives and matching HLSL semantics for GameCult Unity runtimes.", "license": "MPL-2.0", diff --git a/src/GameCult.Geometry/CultGeometryIsoSurface.cs b/src/GameCult.Geometry/CultGeometryIsoSurface.cs index 6fc9355c..583fdd87 100644 --- a/src/GameCult.Geometry/CultGeometryIsoSurface.cs +++ b/src/GameCult.Geometry/CultGeometryIsoSurface.cs @@ -172,7 +172,9 @@ namespace GameCult.Geometry normal = -normal; } - normal = math.normalize(normal); + // A degenerate triangle (an inside vertex exactly on the isovalue) has no area and gets a zero + // normal; normalize follows HLSL and would return NaN for it. + normal = math.dot(normal, normal) > 0f ? math.normalize(normal) : float3.zero; var firstIndex = (uint)(positions.Count / 3); Append(a, positions); Append(b, positions); diff --git a/tests/GameCult.Geometry.Tests/IsoSurfaceTests.cs b/tests/GameCult.Geometry.Tests/IsoSurfaceTests.cs index 90d5f7f6..63b794b3 100644 --- a/tests/GameCult.Geometry.Tests/IsoSurfaceTests.cs +++ b/tests/GameCult.Geometry.Tests/IsoSurfaceTests.cs @@ -47,6 +47,23 @@ namespace GameCult.Geometry.Tests } } + [Test] + public void Samples_on_the_isovalue_emit_finite_normals() + { + var samples = new float[2, 2, 2]; + for (var y = 0; y < 2; y++) + for (var z = 0; z < 2; z++) + { + samples[0, y, z] = 0f; + samples[1, y, z] = 1f; + } + + var mesh = CultGeometryIsoSurface.Extract(samples); + + mesh.TriangleCount.Should().BeGreaterThan(0); + mesh.Normals.Should().OnlyContain(value => float.IsFinite(value)); + } + [Test] public void Extraction_is_deterministic_and_applies_origin_and_cell_size() {