Skip to content

Commit

Permalink
[hlsl-out] Clarify comment on HLSL floating-point divide by zero.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimblandy committed Feb 14, 2025
1 parent bafeee6 commit 96de35a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion naga/src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 96de35a

Please sign in to comment.