Skip to content

Shaders: use DXIL IsNaN in med3 so Intel Xe does not fold fcmp uno - #110

Closed
lordon1a wants to merge 3 commits into
Marvisak:mainfrom
lordon1a:fix/med3-dxil-isnan
Closed

lordon1a wants to merge 3 commits into
Marvisak:mainfrom
lordon1a:fix/med3-dxil-isnan

Conversation

@lordon1a

Copy link
Copy Markdown
Contributor

Summary

Replaces fcmp uno in Lower::med3() (crates/victory-shader/src/emit/lower.rs) with DXIL's explicit dx.op.isSpecialFloat.f32 (IsNaN, opcode 8).

On Intel Iris Xe D3D12 (8086:46a6, driver 32.0.101.7088), the shader compiler folds fcmp uno third, third to false under default shader fast-math, causing v_med3_f32(1.0, 2.0, NaN) to return median (2.0, 0x40000000) instead of least (1.0, 0x3f800000) as RDNA2 specifies. Discovered while running #37's synthetic GPU VALU tests on Intel Iris Xe.

Using dx.op.isSpecialFloat.f32(8, x) preserves NaN detection across Intel Xe and NVIDIA drivers without conflicting with #37's fract() hunks.

Verification

Tested on Windows 11 (rustc 1.100.0, x86_64-pc-windows-gnullvm):

  • Negative control (main@35b054ec0 lower.rs on Intel Iris Xe 8086:46a6, driver 32.0.101.7088):
    cargo test -p victory-shader med3_uses_dxil_isnan_for_nan_operands -> FAILED: v_med3_f32(1.0, 2.0, NaN) got 0x40000000, want 1.
  • With this PR on Intel Iris Xe:
    cargo test -p victory-shader -> 58 passed, 0 failed (including med3_uses_dxil_isnan_for_nan_operands).
  • Stacked with shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU tests #37's full valu_tests:
    • Intel Iris Xe (8086:46a6): 7/7 passed (previously 6/7)
    • NVIDIA GeForce RTX 3070 Ti Laptop GPU (10de:24e0, driver 616.64): 7/7 passed
  • cargo xtask policy: 150 files ok (synthetic GFX10 shader only; zero game data).
  • cargo clippy -p victory-shader --all-targets: 0 new warnings.

AI-assisted.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 00:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

# Conflicts:
#	crates/victory-shader/src/emit/mod.rs
@lordon1a

Copy link
Copy Markdown
Contributor Author

Updated against latest main (fc0c4dd, which merged #37's valu_tests.rs and #56) — dropped the now-redundant test in emit/mod.rs so the diff is just the 1-file fix in crates/victory-shader/src/emit/lower.rs (+13/-3 lines).

Re-verified on Windows 11 (Intel Iris Xe 8086:46a6, driver 32.0.101.7088): cargo xtask policy (151 files ok), cargo test -p victory-shader -> 68 passed, 0 failed (including main's emit::valu_tests::v_med3_f32_matches_rdna2_with_nan, which fails on main@fc0c4dd). AI-assisted.

@raffclar

Copy link
Copy Markdown
Contributor

Your tests only work on Intel hardware.... I don't have an Intel GPU

Do a small test checking that med3 emits IsNaN instead of fcmp uno to make this more portable

@DaviTostes

Copy link
Copy Markdown

Tested 0322732 on Arch Linux x86_64, rustc 1.98.1: pass.

  • cargo test -p victory-shader: 55 passed, 0 failed (2 test binaries).
  • The Intel Xe fast-math case this addresses cannot be reproduced here (RADV), and the diff adds no test; the DXIL IsNaN path is unexercised by the suite.
  • The full workspace still needs Build: Linux workspace, contributor setup and CI #112 for the victory-gpu E0425.

@lordon1a

Copy link
Copy Markdown
Contributor Author

AI-assisted. Thanks for the feedback @raffclar! Added a hardware-independent unit test (\emit::lower::tests::med3_f32_emits_dxil_isnan_instead_of_fcmp_uno) that lowers \�_med3_f32\ and inspects the emitted DXIL module directly to verify it emits \dx.op.isSpecialFloat.f32\ (opcode 8, \IsNaN) and does not emit \ cmp uno\ (\pred::UNO). Verified \cargo xtask policy\ (151 files ok) and \cargo test -p victory-shader\ (69/69 passed).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Cross-checked med3 NaN lowering alongside #971 / #1259 across NVIDIA GeForce RTX 5090, AMD Radeon(TM) Graphics, D3D12 WARP, and dxil.dll. Two notes if this PR is rebased onto current main:

  1. Bitwise i32 NaN test ((as_int(x) & 0x7FFF_FFFF) >u 0x7F80_0000) + +inf substitution: Testing NaN on integer bits avoids both fcmp uno folding and dx.op.isSpecialFloat fast-math edge cases without adding #[cfg(test)] fields to Lowered, and replacing NaN lanes with +inf before self.max_min(false, ...) also preserves RDNA2's -0.0 < +0.0 ordering (med3(-0.0, +0.0, NaN) == -0.0) and returns a quiet NaN (0x7FC0_0000) when all three inputs are signaling NaNs (sNaN).
  2. Reference commit with f32 + f16 qNaN/sNaN/±0.0/±inf GPU readback tests: fuzeofficlal/VIctory@cc27bcb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants