shader: keep fract(NaN) as NaN on NVIDIA and add synthetic GPU VALU tests #37

Manually merged
github merged 1 commit from gh-pr/37/head into gh-pr/37/base 2026-11-26 03:47:32 +00:00
Owner

@⁠alessiodeia on GitHub · 2026-11-23 23:30 UTC · original
alessiodeia:fix/valu-med3-fract-trig → main · +686 −6 · 3 files · 1 commits · merged 2026-11-24 00:38 UTC

What

Addresses the remaining RDNA2 VALU semantic mismatches from #2 (leaving signed BFE untouched to avoid conflicting with #13):

  • v_med3_f32 / v_med3_f16: return min3(a, b, c) whenever any operand is NaN (pred::UNO check), instead of IEEE minNum/maxNum returning the larger non-NaN operand.
  • v_fract_f32 / v_fract_f16: clamp finite results past the largest representable value below 1.0 (>= 1.0 → 0x3F7F_FFFF for f32; >= 1 - 2^-12 (0x3F7F_F000) → 0x3F7F_E000 (0x3BFF in f16) for f16). Uses pred::OGE against a threshold distinct from the clamped value so drivers (observed on NVIDIA Turing) do not fold r > C ? C : r into minNum (FMNMX) and swallow NaN.
  • v_sin_f32 / v_cos_f32 / v_sin_f16 / v_cos_f16: reduce revolutions via a - Round_ne(a) into [-0.5, 0.5] before scaling by 2π, fixing large-turn phase drift on NVIDIA while preserving ±inf → NaN and NaN → NaN.
  • Synthetic RDNA2 → GPU tests (crates/victory-shader/src/emit/valu_tests.rs): builds minimal GFX10 compute programs, runs them through ir::build → resource::track → io::raise → structure::structure → lower::lower, validates the emitted DXIL container with dxil.dll when present, and executes them via Host::dispatch on both hardware GPU and WARP without requiring console dxc.

Tested

Windows 11 x64 (26300), Rust 1.98.1 (--locked), NVIDIA GeForce RTX 2070 SUPER (driver 617.42) + WARP + dxil.dll 1.9.2609:

  • Reproduced all 5 regressions against the original lowering on RTX 2070 SUPER:
    • v_med3_f32(1.0, 2.0, NaN) = 2.0 → now 1.0
    • v_med3_f16(1.0, 2.0, NaN) = 0x4000 → now 0x3c00
    • v_fract_f32(-1e-10) = 0x3f800000 (1.0) → now 0x3f7fffff (and NaN / ±inf stay NaN)
    • v_sin_f32(255.0) = -9.57e-5 → now < 1e-5
    • v_sin_f16(65504.0) = -0.0245 → now 0.0
  • cargo test -p victory-shader --locked --offline: 63 passed, 0 failed (hardware GPU + WARP + dxil.dll validation).
  • cargo test --workspace --locked --offline (skipping sandboxed socket test only_loopback_is_reachable): all passed.
  • cargo clippy -p victory-shader --all-targets --locked --offline -- -D warnings: 0 warnings.
  • cargo --offline xtask policy: asset policy: 148 files ok.

AI-assisted; no game assets were used.

**@⁠alessiodeia** on GitHub · 2026-11-23 23:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/37) `alessiodeia:fix/valu-med3-fract-trig` → `main` · +686 −6 · 3 files · 1 commits · merged 2026-11-24 00:38 UTC ## What Addresses the remaining RDNA2 VALU semantic mismatches from #2 (leaving signed BFE untouched to avoid conflicting with #13): - **`v_med3_f32` / `v_med3_f16`:** return `min3(a, b, c)` whenever any operand is NaN (`pred::UNO` check), instead of IEEE `minNum`/`maxNum` returning the larger non-NaN operand. - **`v_fract_f32` / `v_fract_f16`:** clamp finite results past the largest representable value below `1.0` (`>= 1.0` → `0x3F7F_FFFF` for f32; `>= 1 - 2^-12` (`0x3F7F_F000`) → `0x3F7F_E000` (`0x3BFF` in f16) for f16). Uses `pred::OGE` against a threshold distinct from the clamped value so drivers (observed on NVIDIA Turing) do not fold `r > C ? C : r` into `minNum` (`FMNMX`) and swallow NaN. - **`v_sin_f32` / `v_cos_f32` / `v_sin_f16` / `v_cos_f16`:** reduce revolutions via `a - Round_ne(a)` into `[-0.5, 0.5]` before scaling by `2π`, fixing large-turn phase drift on NVIDIA while preserving `±inf → NaN` and `NaN → NaN`. - **Synthetic RDNA2 → GPU tests (`crates/victory-shader/src/emit/valu_tests.rs`):** builds minimal GFX10 compute programs, runs them through `ir::build` → `resource::track` → `io::raise` → `structure::structure` → `lower::lower`, validates the emitted DXIL container with `dxil.dll` when present, and executes them via `Host::dispatch` on both hardware GPU and WARP without requiring console `dxc`. ## Tested Windows 11 x64 (26300), Rust `1.98.1` (`--locked`), NVIDIA GeForce RTX 2070 SUPER (driver `617.42`) + WARP + `dxil.dll` 1.9.2609: - Reproduced all 5 regressions against the original lowering on RTX 2070 SUPER: - `v_med3_f32(1.0, 2.0, NaN)` = `2.0` → now `1.0` - `v_med3_f16(1.0, 2.0, NaN)` = `0x4000` → now `0x3c00` - `v_fract_f32(-1e-10)` = `0x3f800000` (1.0) → now `0x3f7fffff` (and `NaN` / `±inf` stay `NaN`) - `v_sin_f32(255.0)` = `-9.57e-5` → now `< 1e-5` - `v_sin_f16(65504.0)` = `-0.0245` → now `0.0` - `cargo test -p victory-shader --locked --offline`: 63 passed, 0 failed (hardware GPU + WARP + `dxil.dll` validation). - `cargo test --workspace --locked --offline` (skipping sandboxed socket test `only_loopback_is_reachable`): all passed. - `cargo clippy -p victory-shader --all-targets --locked --offline -- -D warnings`: 0 warnings. - `cargo --offline xtask policy`: `asset policy: 148 files ok`. AI-assisted; no game assets were used.
Fixes a real-hardware NVIDIA bug in fract() from 36eb0b4 and adds synthetic GFX10 -> DXIL -> GPU/WARP regression tests for the RDNA2 VALU lowerings from #2:

- fract(): select(OGT(r, k), k, r) is folded by NVIDIA's DXIL compiler into FMNMX (minNum), which swallows NaN and turns v_fract_f32(NaN) into 0x3f7fffff (and v_fract_f16(NaN) into 0x3bff). Comparing with pred::OGE against a distinct threshold (1.0 for f32, 1 - 2^-12 for f16) prevents the minNum fold and preserves NaN and +/-inf -> NaN on both NVIDIA hardware and WARP.
- valu_tests.rs: builds minimal synthetic GFX10 compute shaders, lowers them through the full pipeline (ir::build -> resource::track -> io::raise -> structure -> lower), validates with dxil.dll when present, and executes them via Host::dispatch on both hardware GPU and WARP without requiring console dxc. Covers v_fract_f32/f16, v_med3_f32/f16, v_sin/v_cos_f32/f16, and v_bfe_i32/u32 + s_bfe_i32.

Co-authored-by: BlueKaiman <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:30 +00:00
github manually merged commit 2e5001cf9d into gh-pr/37/base 2026-11-26 03:47:32 +00:00
Author
Owner

@⁠Sunkiller-qc on GitHub · 2026-11-23 23:46 UTC · original

Thanks for the thorough work and the GPU-run tests! Heads-up: main now has 36eb0b4 and 0b499d7, which cover med3-with-NaN, the fract clamp and turn reduction, so this conflicts in lower.rs. Main's trig_turns (lower.rs:1430 on main) also keeps sin(-0) = -0, which turns_to_rad here loses because -0 - round_ne(-0) is +0, and it gives exact zeros at half and quarter turns. Two parts of this PR look like they'd still add value on top of main: (1) the NaN-preserving fract compare. Main's fract (lower.rs:1413-1418) uses r > k ? k : r with the same constant as threshold and clamp value, which is the exact min shape you describe NVIDIA folding into FMNMX. Your OGE against a distinct threshold avoids that. (2) The valu_tests.rs harness running synthetic GFX10 programs on the GPU and WARP. Would you consider rebasing down to just those two, against main's helpers? Static review, not built or run.

**@⁠Sunkiller-qc** on GitHub · 2026-11-23 23:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/37#issuecomment-7117352703) Thanks for the thorough work and the GPU-run tests! Heads-up: main now has 36eb0b4 and 0b499d7, which cover med3-with-NaN, the fract clamp and turn reduction, so this conflicts in `lower.rs`. Main's `trig_turns` (`lower.rs:1430` on main) also keeps `sin(-0) = -0`, which `turns_to_rad` here loses because `-0 - round_ne(-0)` is `+0`, and it gives exact zeros at half and quarter turns. Two parts of this PR look like they'd still add value on top of main: (1) the NaN-preserving fract compare. Main's `fract` (`lower.rs:1413-1418`) uses `r > k ? k : r` with the same constant as threshold and clamp value, which is the exact `min` shape you describe NVIDIA folding into `FMNMX`. Your `OGE` against a distinct threshold avoids that. (2) The `valu_tests.rs` harness running synthetic GFX10 programs on the GPU and WARP. Would you consider rebasing down to just those two, against main's helpers? Static review, not built or run.
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-23 23:53 UTC · original

@⁠Sunkiller-qc Done! Rebased onto main (d222b87) down to just those two changes:

  1. fract() in lower.rs: switched to pred::OGE against a distinct threshold (1.0 for f32, 1 - 2^-12 for f16) so NVIDIA's DXIL compiler does not fold r > k ? k : r into FMNMX (minNum). Verified live against main (36eb0b4) on RTX 2070 SUPER where v_fract_f32(NaN) returned 0x3f7fffff and v_fract_f16(NaN) returned 0x3bff; both now preserve NaN (and ±inf -> NaN) on both RTX 2070 SUPER and WARP.
  2. valu_tests.rs: synthetic GFX10 → DXIL → dxil.dll → Host::dispatch GPU + WARP tests running against main's helpers (med3, fract, trig_turns including sin(-0.0) == -0.0 and exact +0.0 at half/quarter turns, plus v_bfe_i32/v_bfe_u32/s_bfe_i32).

cargo test -p victory-shader --locked --offline passes 64/64 (RTX 2070 SUPER + WARP + dxil.dll), cargo clippy is clean, and cargo xtask policy passes (149 files ok).

**@⁠alessiodeia** on GitHub · 2026-11-23 23:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/37#issuecomment-7117473018) @⁠Sunkiller-qc Done! Rebased onto `main` (`d222b87`) down to just those two changes: 1. **`fract()` in `lower.rs`:** switched to `pred::OGE` against a distinct threshold (`1.0` for f32, `1 - 2^-12` for f16) so NVIDIA's DXIL compiler does not fold `r > k ? k : r` into `FMNMX` (`minNum`). Verified live against `main` (`36eb0b4`) on RTX 2070 SUPER where `v_fract_f32(NaN)` returned `0x3f7fffff` and `v_fract_f16(NaN)` returned `0x3bff`; both now preserve `NaN` (and `±inf -> NaN`) on both RTX 2070 SUPER and WARP. 2. **`valu_tests.rs`:** synthetic GFX10 → DXIL → `dxil.dll` → `Host::dispatch` GPU + WARP tests running against `main`'s helpers (`med3`, `fract`, `trig_turns` including `sin(-0.0) == -0.0` and exact `+0.0` at half/quarter turns, plus `v_bfe_i32`/`v_bfe_u32`/`s_bfe_i32`). `cargo test -p victory-shader --locked --offline` passes 64/64 (RTX 2070 SUPER + WARP + `dxil.dll`), `cargo clippy` is clean, and `cargo xtask policy` passes (`149 files ok`).
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-23 23:54 UTC · original

Tested 2e5001c stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • main alone does not build on Linux (E0425 in victory-gpu), so the check uses #39 as the base; the merge is clean.
  • cargo test --workspace: 155 passed, 0 failed, 3 ignored, including the new crates/victory-shader/src/emit/valu_tests.rs cases (the 675-line synthetic VALU set) — no NaN/fract/trig regressions in the rest of the suite.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-23 23:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/37#issuecomment-7117491704) Tested `2e5001c` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `main` alone does not build on Linux (E0425 in `victory-gpu`), so the check uses #39 as the base; the merge is clean. - `cargo test --workspace`: **155 passed, 0 failed, 3 ignored**, including the new `crates/victory-shader/src/emit/valu_tests.rs` cases (the 675-line synthetic VALU set) — no NaN/fract/trig regressions in the rest of the suite. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠lordon1a on GitHub · 2026-11-24 00:25 UTC · original

Verified 2e5001cf9 on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@35b054ec0) on two real D3D12 GPUs:

1. NVIDIA GeForce RTX 3070 Ti Laptop GPU (10de:24e0, driver 616.64) — PASS (7/7)

  • cargo xtask policy: 151 files ok
  • cargo test -p victory-shader valu_tests: 7 passed, 0 failed (v_med3_f32, v_med3_f16, v_fract_f32, v_fract_f16, v_sin_and_v_cos_f32, v_sin_and_v_cos_f16, bfe_signed_and_unsigned).
  • Negative control (reverting crates/victory-shader/src/emit/lower.rs to main on the same RTX 3070 Ti): 5 passed, 2 failed — both v_fract_f32_never_returns_one and v_fract_f16_never_returns_one fail on main and pass with this PR.

2. Intel(R) Iris(R) Xe Graphics (8086:46a6, driver 32.0.101.7088) — 6/7 passed, 1 pre-existing main med3_f32 failure

  • v_med3_f16, v_fract_f32, v_fract_f16, v_sin_and_v_cos_f32, v_sin_and_v_cos_f16, bfe_signed_and_unsigned: all pass.
  • v_med3_f32_matches_rdna2_with_nan: fails on main's med3() with:
    hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1
    
    (The PR's new test caught a real Intel Xe D3D12 driver / fcmp uno issue in main's med3() for f32; v_fract_* itself is completely green on Intel Xe too.)

(Note: on dual-GPU laptops DXGI adapter 0 is Intel Iris Xe, so the RTX 3070 Ti run selected the discrete adapter locally via IDXGIFactory6::EnumAdapterByGpuPreference.)

AI-assisted.

**@⁠lordon1a** on GitHub · 2026-11-24 00:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/37#issuecomment-7118052946) Verified `2e5001cf9` on Windows 11 (`x86_64-pc-windows-gnullvm`, `rustc 1.100.0`, applied cleanly to `main@35b054ec0`) on **two real D3D12 GPUs**: ### 1. NVIDIA GeForce RTX 3070 Ti Laptop GPU (`10de:24e0`, driver `616.64`) — **PASS (7/7)** - `cargo xtask policy`: 151 files ok - `cargo test -p victory-shader valu_tests`: **7 passed, 0 failed** (`v_med3_f32`, `v_med3_f16`, `v_fract_f32`, `v_fract_f16`, `v_sin_and_v_cos_f32`, `v_sin_and_v_cos_f16`, `bfe_signed_and_unsigned`). - **Negative control** (reverting `crates/victory-shader/src/emit/lower.rs` to `main` on the same RTX 3070 Ti): **5 passed, 2 failed** — both `v_fract_f32_never_returns_one` and `v_fract_f16_never_returns_one` fail on `main` and pass with this PR. ### 2. Intel(R) Iris(R) Xe Graphics (`8086:46a6`, driver `32.0.101.7088`) — **6/7 passed, 1 pre-existing `main` med3_f32 failure** - `v_med3_f16`, `v_fract_f32`, `v_fract_f16`, `v_sin_and_v_cos_f32`, `v_sin_and_v_cos_f16`, `bfe_signed_and_unsigned`: **all pass**. - `v_med3_f32_matches_rdna2_with_nan`: **fails on `main`'s `med3()`** with: ``` hardware: v_med3_f32(1.0, 2.0, NaN) = 2 (0x40000000), want 1 ``` (The PR's new test caught a real Intel Xe D3D12 driver / `fcmp uno` issue in `main`'s `med3()` for `f32`; `v_fract_*` itself is completely green on Intel Xe too.) *(Note: on dual-GPU laptops DXGI adapter 0 is Intel Iris Xe, so the RTX 3070 Ti run selected the discrete adapter locally via `IDXGIFactory6::EnumAdapterByGpuPreference`.)* AI-assisted.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!37
No description provided.