Match float min, max, clamp, and saturate to DXIL FMin/FMax/Saturate
dxc lowers float min/max/clamp to dx.op FMin/FMax (36/35) and saturate to dx.op Saturate (7). DXIL.rst defines them: FMin(a, b) is a < b ? a : b, FMax(a, b) is a >= b ? a : b, a NaN operand returns the other, and Saturate is FMin(1, FMax(0, x)). C# used MathF.Min/Max, which pass NaN through and order signed zeros, so saturate(NaN) was NaN instead of 0. Every float overload now follows the spec, normalize uses the same max as its mirror, and the mirror's saturate spells Saturate's operand order (which maps -0 to +0, where clamp(x, 0, 1) keeps -0). Non-NaN results change only in the sign of a zero. design.md records the rules and that dxc marks float math fast, so GPU NaN results are not guaranteed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
d298d666d0
commit
9407da0461
7 changed files with 47 additions and 18 deletions
|
|
@ -165,6 +165,20 @@ public sealed class HlslSemanticsTests
|
|||
Assert.Equal(1.0f, math.step(float.NaN, 1.0f));
|
||||
Assert.Equal(1.0f, math.step(1.0f, 1.0f));
|
||||
|
||||
// DXIL FMin/FMax/Saturate: a NaN operand returns the other; max(a, b) is a >= b ? a : b.
|
||||
Assert.Equal(1.0f, math.min(float.NaN, 1.0f));
|
||||
Assert.Equal(1.0f, math.min(1.0f, float.NaN));
|
||||
Assert.Equal(1.0f, math.max(float.NaN, 1.0f));
|
||||
Assert.Equal(1.0f, math.max(1.0f, float.NaN));
|
||||
Assert.True(float.IsNaN(math.max(float.NaN, float.NaN)));
|
||||
Assert.Equal(2.0f, math.clamp(float.NaN, 2.0f, 3.0f));
|
||||
Assert.Equal(new float3(0.0f, 1.0f, 0.0f), math.saturate(new float3(float.NaN, 2.0f, -1.0f)));
|
||||
Assert.Equal(new float2(-1.0f, 4.0f), math.max(new float2(float.NaN, 4.0f), new float2(-1.0f, float.NaN)));
|
||||
static int Bits(float value) => BitConverter.SingleToInt32Bits(value);
|
||||
Assert.Equal(Bits(-0.0f), Bits(math.max(-0.0f, 0.0f)));
|
||||
Assert.Equal(Bits(0.0f), Bits(math.min(-0.0f, 0.0f)));
|
||||
Assert.Equal(Bits(0.0f), Bits(math.saturate(-0.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)));
|
||||
|
|
|
|||
Loading…
Reference in a new issue