Shader: the ray test's triangle multiplies stay apart from their adds #1903

Open
github-bot wants to merge 1 commit from github-bot/gh-1903 into main AGit
Owner

@⁠kais7654 on GitHub · 2026-11-25 00:28 UTC · original
kais7654:shader/bvh-fma → main · +28 −7 · 1 files · 1 commits · open

Problem

The three BVH intersection tests fail on NVIDIA hardware (bvh_tests.rs:787),
one to three ULPs off in t's numerator (and occasionally a barycentric), while
WARP matches the reference word for word.

Replaying the failing inputs shows the reference (strict, per-operation
rounding, the instruction's documented behavior) is bit-identical to the
driver only if every fmul feeding a fadd/fsub is contracted into an
FMA. And indeed the emitted DXIL carries no fast-math flags — the ops are
precise (DXC emits plain fmul/fadd for precise, fmul fast otherwise) —
yet the driver's DXIL compiler contracts them anyway.

Fix

In the triangle test (crates/victory-shader/src/emit/lower/bvh.rs), the
multiplies that feed a following add or sub (the sheared products, the edge
products and t's three summands) are emitted as dx.op.tertiary.f32
(FMad, opcode 46) over a zero: FMad(a, b, z) rounds exactly like the plain
a * b, but the call is opaque to the fusion pass, so each add keeps its own
rounding. The addend is a zero the driver cannot see through — a select
driven by the direction's own comparison, over +0.0 and -0.0 (both round
the product alike); a constant zero would fold straight back into the
multiplication.

The box test and the remaining products are untouched: nothing there feeds an
add.

Testing

  • All three intersection tests pass word for word on RTX 5070 (laptop)
    and on WARP, hardware and warp each (e.g. 2093 boxes and 324 triangles
    hit, equal).
  • Full suite cargo test -p victory-shader --lib -- --test-threads=1:
    225 passed; the 4 remaining failures are pre-existing on main
    (02fcf5b) and outside this change.
  • cargo clippy -p victory-shader --all-targets: clean.
  • Diff limited to bvh.rs (23 insertions, 7 deletions).
**@⁠kais7654** on GitHub · 2026-11-25 00:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/1903) `kais7654:shader/bvh-fma` → `main` · +28 −7 · 1 files · 1 commits · open ## Problem The three BVH intersection tests fail on NVIDIA hardware (`bvh_tests.rs:787`), one to three ULPs off in t's numerator (and occasionally a barycentric), while WARP matches the reference word for word. Replaying the failing inputs shows the reference (strict, per-operation rounding, the instruction's documented behavior) is bit-identical to the driver only if every `fmul` feeding a `fadd`/`fsub` is contracted into an FMA. And indeed the emitted DXIL carries no fast-math flags — the ops are precise (DXC emits plain `fmul`/`fadd` for `precise`, `fmul fast` otherwise) — yet the driver's DXIL compiler contracts them anyway. ## Fix In the triangle test (`crates/victory-shader/src/emit/lower/bvh.rs`), the multiplies that feed a following add or sub (the sheared products, the edge products and t's three summands) are emitted as `dx.op.tertiary.f32` (FMad, opcode 46) over a zero: `FMad(a, b, z)` rounds exactly like the plain `a * b`, but the call is opaque to the fusion pass, so each add keeps its own rounding. The addend is a zero the driver cannot see through — a `select` driven by the direction's own comparison, over `+0.0` and `-0.0` (both round the product alike); a constant zero would fold straight back into the multiplication. The box test and the remaining products are untouched: nothing there feeds an add. ## Testing - All three intersection tests pass word for word on RTX 5070 (laptop) and on WARP, hardware and warp each (e.g. 2093 boxes and 324 triangles hit, equal). - Full suite `cargo test -p victory-shader --lib -- --test-threads=1`: 225 passed; the 4 remaining failures are pre-existing on `main` (`02fcf5b`) and outside this change. - `cargo clippy -p victory-shader --all-targets`: clean. - Diff limited to `bvh.rs` (23 insertions, 7 deletions).
A fmul feeding a fadd is the FMA the drivers may make of it, even though
the stores carry no fast-math flag to bless one - and the NVIDIA driver
does fold these, rounding once where the console rounds its product and
its sum apart (123 inputs off on the three intersection tests here;
nothing an emitter can mark makes a driver keep them apart - DXC itself
drops `precise` before it writes DXIL).

So each multiply now goes out as an FMad over a zero that carries the
product's own sign (sign(a) ^ sign(b)): a sum the driver fuses rounds its
exact product plus zero once, which is the plain product's rounding,
signed zero included - the same bits on a driver that fuses (NVIDIA,
AMD's FMad) and on one that does not (WARP), and no way for a driver to
see two adds where the console wrote one.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:48:10 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 01:06 UTC · original

AMD RDNA3 (RX 7800 XT) test report for #1903 at head cbafed53 merged with main 42363ab6 (clean merge).

Result: regression on RX 7800 XT hardware — the 3 BVH intersection tests pass on main 42363ab6, and all 3 fail by 1 ULP on t with #1903.

Tested

  • AMD Radeon RX 7800 XT 16 GB (RDNA3, driver 32.0.31035.1003; hardware adapter fuses FMad: true), Ryzen 7 5700X3D, 64 GB RAM, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug build.
  • Baseline on origin/main 42363ab6:
    • cargo test -p victory-shader --lib bvh_tests -- --test-threads=1 --nocapture: 14 passed, 0 failed, 0 ignored (1.17s). Both hardware and WARP matched the reference on all 3 GPU intersection tests (551 boxes / 76 triangles, 519 boxes / 74 triangles, 2093 boxes / 324 triangles, 1070 boxes / 138 triangles).
  • PR #1903 (cbafed53 merged with 42363ab6):
    • cargo test -p victory-shader --lib bvh_tests -- --test-threads=1 --nocapture: 11 passed, 3 failed, 0 ignored (0.49s):
      • emit::bvh_tests::intersections_on_nodes_in_title_memory_equal_the_reference (bvh_tests.rs:787):
        hardware: type 1, Ray { extent: 5.0, origin: [6.125, 0.890625, 0.359375], dir: [-1.0, -0.796875, -0.796875], inv: [-1.0, -1.254902, -1.254902] }
        left: [1118671093, 1092638436, 1079331024, 1076591664] vs right: [1118671094, 1092638436, 1079331024, 1076591664]
      • emit::bvh_tests::intersections_on_the_gpu_equal_the_reference (bvh_tests.rs:787): identical mismatch (left[0] = 1118671093 vs right[0] = 1118671094).
      • emit::bvh_tests::intersections_with_64_bit_pointers_on_the_gpu_equal_the_reference (bvh_tests.rs:787):
        hardware: type 3, Ray { extent: 5.0, origin: [-6.421875, -5.90625, -7.171875], dir: [0.203125, 0.5, -0.0625], inv: [4.923077, 2.0, -16.0] }
        left: [1140801401, 1105748384, 1088566368, 1092953536] vs right: [1140801402, 1105748384, 1088566368, 1092953536]
    • cargo test -p victory-shader --lib -- --test-threads=1 --nocapture: 243 passed, 3 failed, 3 ignored (34.84s; the 3 failures are only the 3 BVH tests above; 6 no console [or PC] compiler; skipped + 1 warp: no pipeline for a wave of 32; skipped).
    • cargo clippy -p victory-shader --all-targets: clean.

Not tested

  • Release build, full workspace outside victory-shader, or a game run.

AI use

  • T3 Code (Antigravity), Gemini 4 Neon: checked out the PR in a temporary worktree, ran the baseline and PR tests on @⁠oni-swr's machine, and wrote this report.
**@⁠oni-swr** on GitHub · 2026-11-25 01:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7149207604) AMD RDNA3 (RX 7800 XT) test report for #1903 at head `cbafed53` merged with `main` `42363ab6` (clean merge). **Result: regression on RX 7800 XT hardware — the 3 BVH intersection tests pass on `main` `42363ab6`, and all 3 fail by 1 ULP on `t` with #1903.** **Tested** - AMD Radeon RX 7800 XT 16 GB (RDNA3, driver 32.0.31035.1003; `hardware adapter fuses FMad: true`), Ryzen 7 5700X3D, 64 GB RAM, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug build. - Baseline on `origin/main` `42363ab6`: - `cargo test -p victory-shader --lib bvh_tests -- --test-threads=1 --nocapture`: **14 passed, 0 failed, 0 ignored** (1.17s). Both hardware and WARP matched the reference on all 3 GPU intersection tests (`551 boxes / 76 triangles`, `519 boxes / 74 triangles`, `2093 boxes / 324 triangles`, `1070 boxes / 138 triangles`). - PR #1903 (`cbafed53` merged with `42363ab6`): - `cargo test -p victory-shader --lib bvh_tests -- --test-threads=1 --nocapture`: **11 passed, 3 failed, 0 ignored** (0.49s): - `emit::bvh_tests::intersections_on_nodes_in_title_memory_equal_the_reference` (`bvh_tests.rs:787`): `hardware: type 1, Ray { extent: 5.0, origin: [6.125, 0.890625, 0.359375], dir: [-1.0, -0.796875, -0.796875], inv: [-1.0, -1.254902, -1.254902] }` `left: [1118671093, 1092638436, 1079331024, 1076591664]` vs `right: [1118671094, 1092638436, 1079331024, 1076591664]` - `emit::bvh_tests::intersections_on_the_gpu_equal_the_reference` (`bvh_tests.rs:787`): identical mismatch (`left[0] = 1118671093` vs `right[0] = 1118671094`). - `emit::bvh_tests::intersections_with_64_bit_pointers_on_the_gpu_equal_the_reference` (`bvh_tests.rs:787`): `hardware: type 3, Ray { extent: 5.0, origin: [-6.421875, -5.90625, -7.171875], dir: [0.203125, 0.5, -0.0625], inv: [4.923077, 2.0, -16.0] }` `left: [1140801401, 1105748384, 1088566368, 1092953536]` vs `right: [1140801402, 1105748384, 1088566368, 1092953536]` - `cargo test -p victory-shader --lib -- --test-threads=1 --nocapture`: **243 passed, 3 failed, 3 ignored** (34.84s; the 3 failures are only the 3 BVH tests above; 6 `no console [or PC] compiler; skipped` + 1 `warp: no pipeline for a wave of 32; skipped`). - `cargo clippy -p victory-shader --all-targets`: clean. **Not tested** - Release build, full workspace outside `victory-shader`, or a game run. **AI use** - T3 Code (Antigravity), Gemini 4 Neon: checked out the PR in a temporary worktree, ran the baseline and PR tests on @⁠oni-swr's machine, and wrote this report.
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 02:06 UTC · original

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head cbafed5 applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 133 passed, 0 failed, 2 ignored. All tests pass.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 4 pre-existing errors in test code (fail identically on main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

**@⁠Headache3318** on GitHub · 2026-11-25 02:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7150333995) Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head `cbafed5` applied cleanly onto current `main` `ccfb029`. - `cargo test -p victory-shader --locked`: 133 passed, 0 failed, 2 ignored. All tests pass. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: 4 pre-existing errors in test code (fail identically on main `ccfb029`). The PR introduces no new clippy warnings. Not tested: Windows MSVC build, the title itself. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:18 UTC · original · edited 2026-11-25 16:47 UTC

Automated triage for the maintainers.

Needs work. The triangle test's multiplies in Lower::bvh (emit/lower/bvh.rs) are now emitted as FMad (opcode 46) over a run-time zero, so a driver cannot contract them with the following add or sub.

What changed: the zero now carries the product's sign, (bits(a) ^ bits(b)) & 0x8000_0000, instead of a +0.0 select. That answers the earlier signed-zero point. By my reading FMad(a, b, z) now gives the plain product's rounding whether or not the driver fuses it.

What is still open:

  • AMD. On the earlier head, the RX 7800 XT report showed all three BVH intersection tests failing by 1 ULP on t; they pass on main. The sign change only affects zero products, so it does not obviously explain a 1 ULP difference in t. Please rerun the three tests on AMD hardware on this head.
  • Evidence. This changes shader semantics. Please show that the console's ray-intersection instruction rounds each product and sum separately (public ISA documentation or a hardware capture), not only that the repo's reference in bvh_tests.rs does.
  • Minor: smul duplicates Lower::fmad in lower.rs. Calling that helper would be shorter.

An offline translate run on the earlier head reported no change in validated count. CI: passing (WARP).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7156859482) · edited 2026-11-25 16:47 UTC <!-- victory-triage v1 head=5f19a03328d61483a437445be68ba75cb3338ad8 verdict=needs-work at=2026-11-25T16:45:09Z --> Automated triage for the maintainers. **Needs work.** The triangle test's multiplies in `Lower::bvh` (`emit/lower/bvh.rs`) are now emitted as FMad (opcode 46) over a run-time zero, so a driver cannot contract them with the following add or sub. What changed: the zero now carries the product's sign, `(bits(a) ^ bits(b)) & 0x8000_0000`, instead of a `+0.0` select. That answers the earlier signed-zero point. By my reading `FMad(a, b, z)` now gives the plain product's rounding whether or not the driver fuses it. What is still open: - **AMD.** On the earlier head, the RX 7800 XT report showed all three BVH intersection tests failing by 1 ULP on `t`; they pass on `main`. The sign change only affects zero products, so it does not obviously explain a 1 ULP difference in `t`. Please rerun the three tests on AMD hardware on this head. - **Evidence.** This changes shader semantics. Please show that the console's ray-intersection instruction rounds each product and sum separately (public ISA documentation or a hardware capture), not only that the repo's reference in `bvh_tests.rs` does. - Minor: `smul` duplicates `Lower::fmad` in `lower.rs`. Calling that helper would be shorter. An offline translate run on the earlier head reported no change in validated count. CI: passing (WARP). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 11:34 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head cbafed53 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 11:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7161222721) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head cbafed53 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1903/head:github-bot/gh-1903'
git switch 'github-bot/gh-1903'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1903'
git switch 'github-bot/gh-1903'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1903'
git switch 'github-bot/gh-1903'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1903'
git switch 'main'
git merge --squash 'github-bot/gh-1903'
git switch 'main'
git merge --ff-only 'github-bot/gh-1903'
git switch 'main'
git merge 'github-bot/gh-1903'
git push origin 'main'
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!1903
No description provided.