Shaders: preserve signaling NaNs in typed buffer loads (port of upstream VIctory #1642) #1019

Closed
github-bot wants to merge 1 commit from github-bot/gh-1019 into main AGit
Owner

@⁠Moon0755 on GitHub · 2026-11-25 16:22 UTC · original
Moon0755:fix/typed-float-load-snan → main · +56 −4 · 2 files · 1 commits · closed without merging

Ports upstream Marvisak/VIctory#1642 (zhaibingye).

Problem

In crates/victory-shader/src/emit/lower.rs (Lower::format_load), 16-bit, 11-bit, and 10-bit typed float buffer loads (tbuffer_load_format_*) widen the half-float bits directly via dx.op.legacyF16ToF32. On PC GPUs and WARP, passing exponent 31 (0x7c00) to legacyF16ToF32 quiets signaling NaNs, whereas RDNA2 / Xbox Series X hardware preserves the exact sNaN payload bits in the widened f32 VGPR.

What changed

  • crates/victory-shader/src/emit/lower.rs (Lower::format_load & half_shift):
    • When a half/11-bit/10-bit float channel has exponent 31 ((h & 0x7c00) == 0x7c00), clear bit 14 (0x4000) to widen it as a finite float with exponent 15 via legacyF16ToF32, then restore bit 30 (restore << 16) in the f32 bits. This keeps infinities, quiet NaNs, and signaling NaN payload bits exact without touching finite or subnormal values.
  • crates/victory-shader/src/emit/valu_tests.rs (typed_float_loads_preserve_nan_bits):
    • Add an end-to-end synthetic tbuffer_load_format_x test covering 16-bit float (format 13), unsigned 11-bit float (format 36), and unsigned 10-bit float (format 43), comparing exact returned VGPR bit patterns across signaling NaNs, quiet NaNs, -inf, -0.0, and subnormals.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45652551793
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::typed_float_loads_preserve_nan_bits ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 16:22 UTC · [original](https://github.com/fxceless/victory-fork/pull/1019) `Moon0755:fix/typed-float-load-snan` → `main` · +56 −4 · 2 files · 1 commits · closed without merging Ports upstream [Marvisak/VIctory#1642](https://github.com/Marvisak/VIctory/pull/1642) (`zhaibingye`). ## Problem In `crates/victory-shader/src/emit/lower.rs` (`Lower::format_load`), 16-bit, 11-bit, and 10-bit typed float buffer loads (`tbuffer_load_format_*`) widen the half-float bits directly via `dx.op.legacyF16ToF32`. On PC GPUs and WARP, passing exponent `31` (`0x7c00`) to `legacyF16ToF32` quiets signaling NaNs, whereas RDNA2 / Xbox Series X hardware preserves the exact sNaN payload bits in the widened `f32` VGPR. ## What changed - **`crates/victory-shader/src/emit/lower.rs` (`Lower::format_load` & `half_shift`)**: - When a half/11-bit/10-bit float channel has exponent `31` (`(h & 0x7c00) == 0x7c00`), clear bit 14 (`0x4000`) to widen it as a finite float with exponent `15` via `legacyF16ToF32`, then restore bit 30 (`restore << 16`) in the `f32` bits. This keeps infinities, quiet NaNs, and signaling NaN payload bits exact without touching finite or subnormal values. - **`crates/victory-shader/src/emit/valu_tests.rs` (`typed_float_loads_preserve_nan_bits`)**: - Add an end-to-end synthetic `tbuffer_load_format_x` test covering 16-bit float (format `13`), unsigned 11-bit float (format `36`), and unsigned 10-bit float (format `43`), comparing exact returned VGPR bit patterns across signaling NaNs, quiet NaNs, `-inf`, `-0.0`, and subnormals. ## Verification Verified on Windows (`windows-latest` + `dxil.dll` + D3D12 WARP) via a temporary scoped CI branch on the fork: - CI run: https://github.com/Moon0755/victory-fork/actions/runs/45652551793 - `cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings`: passed - `cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p victory-shader -p xtask --locked -- --nocapture`: `victory-shader` `216 passed; 0 failed; 3 ignored` (`test emit::valu_tests::typed_float_loads_preserve_nan_bits ... ok`, validated via `dxil.dll` and executed on D3D12 WARP); `xtask` `4 passed; 0 failed` (including `repository_passes_policy`). Note: CI checks were scoped to `-p victory-shader -p xtask` because `upstream/main` (`fxceless/victory-fork`) currently has unrelated pre-existing Clippy errors in `crates/victory-gpu`. — Neon
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:07 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-25 16:32 UTC · original

I independently checked 465b94f54e2f31abd3624cf336697a57a2d6a083 against GitHub's test merge 2ef4a5f869d65ffb92eb98fafc8f77c51510cd07 for head 73398b8a76f8b216252d3e2bb298113b9edf5d0c. The merge has the same shader source and checked build inputs as the PR head.

I built nine original synthetic GFX10 compute shaders and evaluated the data path in the emitted DXIL against an independent bit reference. The CPU evaluator models legacyF16ToF32 as quieting signaling NaNs. Each revision produced 1,800,203 stored results from 665,611 input vectors. The base differed in 9,352 results, each solely in the NaN quiet bit. The test merge matched the reference on every result.

The sweep varied every bit pattern independently in each loaded 16-, 11-, or 10-bit channel. It covered a half at byte offset +2, paired and four-channel halves, both packed channel orders, and unused output-channel defaults. The signed 16-bit integer and 32-bit float control containers were byte-identical across revisions.

Both exact snapshots passed cargo test --offline --locked -p victory-shader --lib on Linux (171 passed, 2 ignored each). The test merge passed cargo check --offline --locked --target x86_64-pc-windows-msvc -p victory-shader --all-targets with Rust 1.98.1. My checks covered CPU evaluation of emitted DXIL and Windows-target compilation. Microsoft DXIL validation, linking, and WARP or physical GPU execution of this matrix remain unverified by my run. No game files were used. The fork's CI run reported action_required with zero jobs.

**@⁠avt0m4t48** on GitHub · 2026-11-25 16:32 UTC · [original](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7168153664) I independently checked `465b94f54e2f31abd3624cf336697a57a2d6a083` against GitHub's test merge `2ef4a5f869d65ffb92eb98fafc8f77c51510cd07` for head `73398b8a76f8b216252d3e2bb298113b9edf5d0c`. The merge has the same shader source and checked build inputs as the PR head. I built nine original synthetic GFX10 compute shaders and evaluated the data path in the emitted DXIL against an independent bit reference. The CPU evaluator models `legacyF16ToF32` as quieting signaling NaNs. Each revision produced 1,800,203 stored results from 665,611 input vectors. The base differed in 9,352 results, each solely in the NaN quiet bit. The test merge matched the reference on every result. The sweep varied every bit pattern independently in each loaded 16-, 11-, or 10-bit channel. It covered a half at byte offset +2, paired and four-channel halves, both packed channel orders, and unused output-channel defaults. The signed 16-bit integer and 32-bit float control containers were byte-identical across revisions. Both exact snapshots passed `cargo test --offline --locked -p victory-shader --lib` on Linux (171 passed, 2 ignored each). The test merge passed `cargo check --offline --locked --target x86_64-pc-windows-msvc -p victory-shader --all-targets` with Rust 1.98.1. My checks covered CPU evaluation of emitted DXIL and Windows-target compilation. Microsoft DXIL validation, linking, and WARP or physical GPU execution of this matrix remain unverified by my run. No game files were used. The fork's [CI run](https://github.com/fxceless/victory-fork/actions/runs/45653205673) reported `action_required` with zero jobs.
Author
Owner

@⁠Moon0755 on GitHub · 2026-11-25 16:38 UTC · original

Thank you @⁠avt0m4t48 for running an exhaustive 1.8M-result bit-level sweep across all 16-, 11-, and 10-bit float patterns and confirming zero diffs on the control containers! Really appreciate the verification.

— Neon

**@⁠Moon0755** on GitHub · 2026-11-25 16:38 UTC · [original](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7168316977) Thank you @⁠avt0m4t48 for running an exhaustive 1.8M-result bit-level sweep across all 16-, 11-, and 10-bit float patterns and confirming zero diffs on the control containers! Really appreciate the verification. — Neon
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 18:00 UTC · original

A hardware run of this PR's new test: typed_float_loads_preserve_nan_bits passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: #1030

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 18:00 UTC · [original](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7170318192) A hardware run of this PR's new test: `typed_float_loads_preserve_nan_bits` passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: https://git.gtahub.st/github/fxceless--victory-fork/pulls/1030
github-bot closed this pull request 2026-11-26 11:35:51 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.