Shaders: v_med3 with a NaN operand gives the median on NVIDIA (fcmp uno folded); fix on a branch #745

Open
opened 2026-11-26 04:13:54 +00:00 by github-bot · 8 comments
Owner

@⁠raphael-riesner on GitHub · 2026-11-24 09:26 UTC · original

v_med3_f32_matches_rdna2_with_nan fails on current main (8e79c80) on an NVIDIA GeForce RTX 3050 Ti Laptop GPU (driver 32.0.16.2114, Windows 11):

hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1

Cause: med3 in crates/victory-shader/src/emit/lower.rs chooses the NaN case (least operand) with fcmp uno. NVIDIA's driver assumes no NaNs in that compare and folds it to false, so a NaN operand gives the plain median. fract next to it already works around the same driver behaviour.

Fix: test the operands for NaN on their bits (|x| > +inf as integers), which the driver cannot fold away. 17 lines in lower.rs, one commit:

Tested on the same GPU:

  • cargo test -p victory-shader --lib v_med3: fails before (above), both med3 tests pass after
  • cargo test -p victory-shader: 89 passed, 1 ignored
  • cargo clippy -p victory-shader --all-targets: clean

Not tested on AMD.

I tried to open a pull request, but creating one was refused for my account (pull requests seem limited to collaborators right now), so the fix is offered here instead. Happy to open the PR if that opens up again.

🤖 Generated with Claude Code

**@⁠raphael-riesner** on GitHub · 2026-11-24 09:26 UTC · [original](https://github.com/Marvisak/VIctory/issues/745) `v_med3_f32_matches_rdna2_with_nan` fails on current `main` (8e79c80) on an NVIDIA GeForce RTX 3050 Ti Laptop GPU (driver 32.0.16.2114, Windows 11): ``` hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1 ``` **Cause:** `med3` in `crates/victory-shader/src/emit/lower.rs` chooses the NaN case (least operand) with `fcmp uno`. NVIDIA's driver assumes no NaNs in that compare and folds it to false, so a NaN operand gives the plain median. `fract` next to it already works around the same driver behaviour. **Fix:** test the operands for NaN on their bits (`|x| > +inf` as integers), which the driver cannot fold away. 17 lines in `lower.rs`, one commit: - Branch: https://github.com/raphael-riesner/VIctory/tree/shader/med3-nan-bits - Compare: https://github.com/Marvisak/VIctory/compare/main...raphael-riesner:VIctory:shader/med3-nan-bits **Tested** on the same GPU: - `cargo test -p victory-shader --lib v_med3`: fails before (above), both med3 tests pass after - `cargo test -p victory-shader`: 89 passed, 1 ignored - `cargo clippy -p victory-shader --all-targets`: clean Not tested on AMD. I tried to open a pull request, but creating one was refused for my account (pull requests seem limited to collaborators right now), so the fix is offered here instead. Happy to open the PR if that opens up again. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:55 +00:00
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 09:36 UTC · original

Read the branch (shader/med3-nan-bits, 1 commit ahead of main, 0 behind). I reviewed the code but did not run it.

  • is_nan_bits is right: (bits & 0x7FFFFFFF) > 0x7F800000 as an unsigned compare is true exactly for NaNs (both quiet and signalling, either sign), and false for ±inf.
  • It also covers v_med3_f16. That call site (lower.rs ~4384) passes operands from half_fsrc, which widens with legacyF16ToF32, so med3 always sees f32 values. A NaN half stays a NaN after the widening, so the f32 bit test holds there too, and no separate 16-bit constant is needed.
  • On AMD the old fcmp uno form already gives the RDNA2 result: on main, v_med3_f32_matches_rdna2_with_nan and v_med3_f16_matches_rdna2_with_nan pass on an RX 9070 (RDNA4) and a Ryzen 9600X iGPU (RDNA2) as the hardware device. So for AMD this change only replaces an equivalent compare.
**@⁠slint6614-dev** on GitHub · 2026-11-24 09:36 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7127637411) Read the branch (`shader/med3-nan-bits`, 1 commit ahead of main, 0 behind). I reviewed the code but did not run it. - `is_nan_bits` is right: `(bits & 0x7FFFFFFF) > 0x7F800000` as an unsigned compare is true exactly for NaNs (both quiet and signalling, either sign), and false for ±inf. - It also covers `v_med3_f16`. That call site (`lower.rs` ~4384) passes operands from `half_fsrc`, which widens with `legacyF16ToF32`, so `med3` always sees f32 values. A NaN half stays a NaN after the widening, so the f32 bit test holds there too, and no separate 16-bit constant is needed. - On AMD the old `fcmp uno` form already gives the RDNA2 result: on main, `v_med3_f32_matches_rdna2_with_nan` and `v_med3_f16_matches_rdna2_with_nan` pass on an RX 9070 (RDNA4) and a Ryzen 9600X iGPU (RDNA2) as the hardware device. So for AMD this change only replaces an equivalent compare.
Author
Owner

@⁠26dwh7yy8g-rgb on GitHub · 2026-11-24 11:55 UTC · original

Ran the branch on Ada: RTX 4060 (desktop), driver 616.92, Windows 11 Pro 26200, Ryzen 5 5600X (no iGPU, so the hardware adapter is the RTX), Rust 1.98.1 (x86_64-pc-windows-msvc), Windows SDK 10.0.26100.

Compared main at ffe310e with ffe310e + c796377 (cherry-picks cleanly):

  • cargo test --workspace --locked --no-fail-fast: 342 passed, 0 failed, 6 ignored on both. The 4 diff_tests skip here (no console compiler), so they are not covered.
  • cargo test --locked -p victory-shader --lib v_med3 -- --test-threads=1 --nocapture, 10 runs on each: both tests pass every time on the hardware adapter and WARP, no adapter skipped.
  • With the branch, clippy --workspace --all-targets --locked -- -D warnings is clean, also with --all-features.

The failure does not reproduce on this card, so this does not confirm the fix; it only shows no regression here.

No game data used. AI-assisted (Claude Code).

**@⁠26dwh7yy8g-rgb** on GitHub · 2026-11-24 11:55 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7130734779) Ran the branch on Ada: **RTX 4060 (desktop), driver 616.92**, Windows 11 Pro 26200, Ryzen 5 5600X (no iGPU, so the hardware adapter is the RTX), Rust 1.98.1 (`x86_64-pc-windows-msvc`), Windows SDK 10.0.26100. Compared `main` at `ffe310e` with `ffe310e` + `c796377` (cherry-picks cleanly): - `cargo test --workspace --locked --no-fail-fast`: 342 passed, 0 failed, 6 ignored on both. The 4 `diff_tests` skip here (no console compiler), so they are not covered. - `cargo test --locked -p victory-shader --lib v_med3 -- --test-threads=1 --nocapture`, 10 runs on each: both tests pass every time on the hardware adapter and WARP, no adapter skipped. - With the branch, clippy `--workspace --all-targets --locked -- -D warnings` is clean, also with `--all-features`. The failure does not reproduce on this card, so this does not confirm the fix; it only shows no regression here. No game data used. AI-assisted (Claude Code).
Author
Owner

@⁠ayanbarosan on GitHub · 2026-11-24 12:41 UTC · original

Tested this fix on Intel UHD Graphics (Tiger Lake Xe-LP, driver 31.0.101.5333), Windows 11 build 26300, where the failure does reproduce. The test host used the Intel iGPU here (DXGI adapter 0 on an Optimus laptop), so the fcmp uno fold is not NVIDIA-only.

  • main at ffe310e: v_med3_f32_matches_rdna2_with_nan fails with the same result: hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1
  • main at ffe310e with c796377 cherry-picked (applies cleanly): both med3 tests pass, on the Intel UHD and on WARP
  • cargo test -p victory-shader --lib with the fix: 120 passed, 0 failed, 1 ignored

No game data used. AI-assisted (Claude Code).

**@⁠ayanbarosan** on GitHub · 2026-11-24 12:41 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7131782471) Tested this fix on **Intel UHD Graphics (Tiger Lake Xe-LP, driver 31.0.101.5333)**, Windows 11 build 26300, where the failure does reproduce. The test host used the Intel iGPU here (DXGI adapter 0 on an Optimus laptop), so the `fcmp uno` fold is not NVIDIA-only. - `main` at ffe310e: `v_med3_f32_matches_rdna2_with_nan` fails with the same result: `hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1` - `main` at ffe310e with c796377 cherry-picked (applies cleanly): both med3 tests pass, on the Intel UHD and on WARP - `cargo test -p victory-shader --lib` with the fix: 120 passed, 0 failed, 1 ignored No game data used. AI-assisted (Claude Code).
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-24 12:54 UTC · original

This is the same failure as #726 (v_med3_f32(1.0, 2.0, NaN) returns 2 on NVIDIA). The data on #726 has it passing on desktop cards (2080, 4070 Ti, 5060 Ti, 5070 Ti, GTX 1060) and failing on the RTX 5060 Laptop it was reported on. Together with your 3050 Ti Laptop and the Intel UHD result above, it doesn't look like it hits every NVIDIA card. It would help to link this branch on #726 so the people there can try it, then close one of the two issues.

**@⁠Sunkiller-qc** on GitHub · 2026-11-24 12:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7132074882) This is the same failure as #726 (`v_med3_f32(1.0, 2.0, NaN)` returns 2 on NVIDIA). The data on #726 has it passing on desktop cards (2080, 4070 Ti, 5060 Ti, 5070 Ti, GTX 1060) and failing on the RTX 5060 Laptop it was reported on. Together with your 3050 Ti Laptop and the Intel UHD result above, it doesn't look like it hits every NVIDIA card. It would help to link this branch on #726 so the people there can try it, then close one of the two issues.
Author
Owner

@⁠0xKaji on GitHub · 2026-11-24 14:14 UTC · original

Tested main (ffe310e) on desktop NVIDIA GeForce RTX 3060 8GB (Ampere, Linux via Wine + DXVK + vkd3d-proton, proprietary driver 620.73.04, CachyOS kernel 7.3.5-2-cachyos, wine-11.22, DXVK v3.1.1-97-ga78a467cadb22be-pcos + NVAPI).

Both v_med3_f32_matches_rdna2_with_nan and v_med3_f16_matches_rdna2_with_nan (along with all 13/13 emit::valu_tests) pass under both vkd3d-proton versions tested:

  • vkd3d-proton 3.1.0 (0bb64c56c8f0dcc):
    cargo xwin build --tests -p victory-shader --target x86_64-pc-windows-msvc -> wine victory_shader.exe -- --test-threads=1 -> valu_tests 13 passed, 0 failed (full crate run: 119 passed, 1 failed, 1 ignored — the only failure is emit::tests::stub_pipeline_is_created, where vkd3d-proton accepts an all-zero DXIL MD5 digest).
  • vkd3d-proton 3.2.0 (GE-Proton11-14, cae8353ed7959c4+):
    valu_tests 13 passed, 0 failed (plus waterfall_tests 5 passed).

This matches the #726/#745 pattern (desktop NVIDIA passing while laptop NVIDIA / Intel UHD fail on Windows native DXIL drivers), and confirms vkd3d-proton's dxil-spirv -> NVIDIA Vulkan path preserves the NaN comparison semantics on Ampere.

(No game data used.)

**@⁠0xKaji** on GitHub · 2026-11-24 14:14 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7134069402) Tested `main` (`ffe310e`) on desktop **NVIDIA GeForce RTX 3060 8GB** (Ampere, Linux via Wine + DXVK + vkd3d-proton, proprietary driver `620.73.04`, CachyOS kernel `7.3.5-2-cachyos`, `wine-11.22`, `DXVK v3.1.1-97-ga78a467cadb22be-pcos` + NVAPI). Both `v_med3_f32_matches_rdna2_with_nan` and `v_med3_f16_matches_rdna2_with_nan` (along with all `13/13` `emit::valu_tests`) **pass** under both vkd3d-proton versions tested: - **`vkd3d-proton 3.1.0`** (`0bb64c56c8f0dcc`): `cargo xwin build --tests -p victory-shader --target x86_64-pc-windows-msvc` -> `wine victory_shader.exe -- --test-threads=1` -> `valu_tests` **13 passed, 0 failed** (full crate run: `119 passed, 1 failed, 1 ignored` — the only failure is `emit::tests::stub_pipeline_is_created`, where vkd3d-proton accepts an all-zero DXIL MD5 digest). - **`vkd3d-proton 3.2.0`** (`GE-Proton11-14`, `cae8353ed7959c4+`): `valu_tests` **13 passed, 0 failed** (plus `waterfall_tests` 5 passed). This matches the `#726`/`#745` pattern (desktop NVIDIA passing while laptop NVIDIA / Intel UHD fail on Windows native DXIL drivers), and confirms `vkd3d-proton`'s `dxil-spirv` -> NVIDIA Vulkan path preserves the NaN comparison semantics on Ampere. *(No game data used.)*
Author
Owner

@⁠DeerSilver on GitHub · 2026-11-24 18:52 UTC · original

Before/after for c796377 on two more adapters, in one laptop: Intel UHD Graphics (Raptor Lake Xe-LP, 8086:a788), driver 32.0.101.7088, and NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell, 10de:2d59), driver 621.24. Windows 11 26300, Rust 1.98.1. No game files: the test suite only.

c796377 cherry-picks cleanly onto main ffe310e. To pick the adapter, I added a local, uncommitted switch to emit::host::Host::new that creates the device on a given DXGI adapter and prints its name. Every run below printed the adapter it ran on.

cargo test --locked -p victory-shader --lib v_med3 -- --test-threads=1, 5 runs of each:

Intel UHD (adapter 1) RTX 5060 Laptop (adapter 0)
main ffe310e v_med3_f32 fails 5 of 5: hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1. v_med3_f16 passes both pass 5 of 5
main + c796377 both pass 5 of 5 both pass 5 of 5

The whole victory-shader lib suite on the Intel UHD: 119 passed, 1 failed on main, where the failure is v_med3_f32, and 120 passed, 0 failed with c796377, with no SM 6.6 skips. So on this Xe-LP no other shader test fails behind the med3 one, which #971 asked about.

Together with the Tiger Lake Xe-LP run above (31.0.101.5333) and #110's Iris Xe (32.0.101.7088, failing), the fcmp uno fold is on current Intel drivers too, and the bit test fixes it there. On NVIDIA Blackwell with a current driver, main already passes and the fix keeps it passing.

AI-assisted (Claude Code).

**@⁠DeerSilver** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7141270659) Before/after for `c796377` on two more adapters, in one laptop: **Intel UHD Graphics (Raptor Lake Xe-LP, `8086:a788`), driver 32.0.101.7088**, and **NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell, `10de:2d59`), driver 621.24**. Windows 11 26300, Rust 1.98.1. No game files: the test suite only. `c796377` cherry-picks cleanly onto `main` `ffe310e`. To pick the adapter, I added a local, uncommitted switch to `emit::host::Host::new` that creates the device on a given DXGI adapter and prints its name. Every run below printed the adapter it ran on. `cargo test --locked -p victory-shader --lib v_med3 -- --test-threads=1`, 5 runs of each: | | Intel UHD (adapter 1) | RTX 5060 Laptop (adapter 0) | |---|---|---| | `main` `ffe310e` | `v_med3_f32` **fails 5 of 5**: `hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1`. `v_med3_f16` passes | both pass 5 of 5 | | `main` + `c796377` | both pass 5 of 5 | both pass 5 of 5 | The whole `victory-shader` lib suite on the Intel UHD: **119 passed, 1 failed** on `main`, where the failure is `v_med3_f32`, and **120 passed, 0 failed** with `c796377`, with no SM 6.6 skips. So on this Xe-LP no other shader test fails behind the med3 one, which #971 asked about. Together with the Tiger Lake Xe-LP run above (31.0.101.5333) and #110's Iris Xe (32.0.101.7088, failing), the `fcmp uno` fold is on current Intel drivers too, and the bit test fixes it there. On NVIDIA Blackwell with a current driver, `main` already passes and the fix keeps it passing. AI-assisted (Claude Code).
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-24 19:45 UTC · original

@⁠raphael-riesner your fix for this issue is now in a pull request, #1610, with seven other fixes from contributors' forks: the branch is merged as it is, with your commit(s) and your name unchanged, onto main ffe310e. Clippy (both runs) and cargo test --workspace pass on the result. If you would rather open your own pull request for this one, say so and I will take it out of #1610. AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-24 19:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7142544452) @⁠raphael-riesner your fix for this issue is now in a pull request, #1610, with seven other fixes from contributors' forks: the branch is merged as it is, with your commit(s) and your name unchanged, onto `main` `ffe310e`. Clippy (both runs) and `cargo test --workspace` pass on the result. If you would rather open your own pull request for this one, say so and I will take it out of #1610. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:49 UTC · original

Automated triage for the maintainers.

This is the same failure as #726, which is open and confirmed and is where the cause is tracked. On main, Lower::med3 in crates/victory-shader/src/emit/lower.rs still picks the NaN case with pred::UNO compares. The comments here show the failing devices are Intel UHD/Xe iGPUs and some laptop NVIDIA GPUs. They also show that c796377 fixes it.

Your commit was carried in bundle #1610, which was closed without merging, so the fix is not on main. Open PRs carrying a fix: #2010 (NaN-safe v_med3_f32) and #1483 (bit test plus adapter choice, needs a rebase). Pull requests are open to everyone. If c796377 is preferred over those, please open it as its own PR.

Proposed close as a duplicate of #726. This issue will be closed in 48 hours unless the reporter replies or a maintainer removes the label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:49 UTC · [original](https://github.com/Marvisak/VIctory/issues/745#issuecomment-7152281386) <!-- victory-issue-triage v1 verdict=proposed-close seen=2026-11-24T19:45:23Z at=2026-11-25T03:49:54Z --> Automated triage for the maintainers. This is the same failure as **#726**, which is open and confirmed and is where the cause is tracked. On `main`, `Lower::med3` in `crates/victory-shader/src/emit/lower.rs` still picks the NaN case with `pred::UNO` compares. The comments here show the failing devices are Intel UHD/Xe iGPUs and some laptop NVIDIA GPUs. They also show that `c796377` fixes it. Your commit was carried in bundle #1610, which was closed without merging, so the fix is not on `main`. Open PRs carrying a fix: #2010 (NaN-safe `v_med3_f32`) and #1483 (bit test plus adapter choice, needs a rebase). Pull requests are open to everyone. If `c796377` is preferred over those, please open it as its own PR. Proposed close as a duplicate of #726. This issue will be closed in 48 hours unless the reporter replies or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#745
No description provided.