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

Closed
Moon0755 wants to merge 1 commit from upstream-pr/1019 into main
Moon0755 commented 2026-11-25 16:22:42 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-25T16:22:42Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6608804287 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1019). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-25T16:22:42Z. --- 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
avt0m4t48 commented 2026-11-25 17:08:02 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-25T16:32:51Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7168153664 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7168153664). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-25T16:32:51Z. --- 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.
Moon0755 commented 2026-11-25 17:08:03 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Moon0755 on 2026-11-25T16:38:58Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:comment:7168316977 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7168316977). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-25T16:38:58Z. --- 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
HAL9000RELOADED commented 2026-11-25 18:05:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HAL9000RELOADED on 2026-11-25T18:00:56Z.


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://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986

<!-- greyfinch-sync:fxceless/victory-fork:comment:7170318192 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1019#issuecomment-7170318192). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-25T18:00:56Z. --- 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://github.com/fxceless/victory-fork/pull/1030#issuecomment-7170315986
upstream-sync closed this pull request 2026-11-26 12:07:11 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!1017
No description provided.