From 96de35aac13483be0f0025769128b7e8418da64e Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 12 Feb 2025 12:36:29 -0800 Subject: [PATCH] [hlsl-out] Clarify comment on HLSL floating-point divide by zero. Replace the link to the resolved WGSL spec issue about floating-point division by zero (gpuweb/gpuweb#2798) with links to the Direct3D 11 functional specification (which Direct3D 12 inherits) and the DXIL specification, explaining that HLSL does what WGSL wants here. --- naga/src/back/hlsl/writer.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index c4cf8cd16d..cb74b7c197 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -2801,6 +2801,17 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { write!(self.out, ")")?; } + // WGSL says that floating-point division by zero should return + // infinity. Microsoft's Direct3D 11 functional specification + // (https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm) + // says: + // + // Divide by 0 produces +/- INF, except 0/0 which results in NaN. + // + // which is what we want. The DXIL specification for the FDiv + // instruction corroborates this: + // + // https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#fdiv Expression::Binary { op: crate::BinaryOperator::Divide, left, @@ -2833,7 +2844,6 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { write!(self.out, ")")?; } - // TODO: if right == 0 return ? see https://github.com/gpuweb/gpuweb/issues/2798 // While HLSL supports float operands with the % operator it is only // defined in cases where both sides are either positive or negative. Expression::Binary {