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

Open
kais7654 wants to merge 1 commit from upstream-pr/1903 into main
kais7654 commented 2026-11-25 00:28:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kais7654 on 2026-11-25T00:28:54Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6594299457 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903). Originally posted by [kais7654](https://github.com/kais7654) on 2026-11-25T00:28:54Z. --- ## 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).
oni-swr commented 2026-11-25 01:08:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T01:06:11Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7149207604 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7149207604). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T01:06:11Z. --- 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.
catminou4 commented 2026-11-25 03:02:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:47:13Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. Ray-test triangle multiplies kept apart from adds (no FMA contraction) — precision-preserving for the intersection math; suite green.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051070703 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903#pullrequestreview-6051070703). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:47:13Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-shader --locked`. 144 passed / 0 failed / 2 ignored. Ray-test triangle multiplies kept apart from adds (no FMA contraction) — precision-preserving for the intersection math; suite green.
Headache3318 commented 2026-11-25 03:03:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Headache3318 on 2026-11-25T02:06:55Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150333995 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7150333995). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T02:06:55Z. --- 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.
Marvisak commented 2026-11-25 09:06:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T08:18:14Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156859482 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7156859482). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T08:18:14Z. --- <!-- 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)_
sinan-cmyk commented 2026-11-25 12:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T11:34:27Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7161222721 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1903#issuecomment-7161222721). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T11:34:27Z. --- ### 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1903:upstream-pr/1903'
git switch 'upstream-pr/1903'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1903'
git switch 'upstream-pr/1903'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1903'
git switch 'upstream-pr/1903'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1903'
git switch 'main'
git merge --squash 'upstream-pr/1903'
git switch 'main'
git merge --ff-only 'upstream-pr/1903'
git switch 'main'
git merge 'upstream-pr/1903'
git push origin 'main'
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
Marvisak/VIctory!1901
No description provided.