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 d3c4a62f..3cfc7618 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 896e41b0..169a7117 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 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()
{