Shaders: a BVH node offset from a root descriptor is computed in 64 bits #2501

Open
github-bot wants to merge 2 commits from github-bot/gh-2501 into main AGit
Owner

@⁠Calvix2k on GitHub · 2026-11-25 14:54 UTC · original
Calvix2k:codex/shader-bvh-node-offset-64 → main · +73 −20 · 2 files · 2 commits · open

Fixes #2368 (the shift/add half of it).

What was wrong

With the node's base taken from a root descriptor (the Source::Built path, where the title shifted a root SRV address into place), the node's byte offset was built in 32 bits and then widened:

at = (node & !7) << 3     ; i32
at = at + k (+ dynamic)   ; i32
Nodes::Raw(base + wide64(at, 0))

A BVH larger than 2 GiB has node offsets that do not fit in 32 bits, and << 3 wraps. The explicit-descriptor path a few lines below already does the same shift in 64 bits (wide64(lo, node_hi) then << 3), so the two halves of the same instruction disagreed about the width of the same quantity.

What the change does

For a raw (title-memory) base the offset is now built in 64 bits — the shift of (node & !7), the constant part, and the computed part — and added to the 64-bit base. The buffer-view path keeps the 32-bit form, because that is what D3D12 takes as a buffer's offset; it is a different address space and its limits are the descriptor's.

The computed and constant parts still zero-extend (built_offset gives k as u32, the computed part goes through Cast::ZExt), as before; a negative offset is not what this changes, and the earlier claim that it did was wrong.

Verification

a_node_pointer_above_two_gib_reads_where_it_points is the distinction the review asked for: run_cases gains a high mode that sets bit 29 of the node pointer (a node 4 GiB into the BVH) and has page_table_high map that guest page to a second copy of the nodes 64 KiB further into the host buffer, while the page below holds zeros. On the old 32-bit shift the offset wraps back to the zeros and every case misses the reference; with the 64-bit shift the pointer reads its copy.

Locally, cargo test -p victory-shader --lib --no-run builds and clippy -D warnings is clean. The GPU cases need an SM 6.6 device; this Windows 10 test harness has no Agility opt-in, so they skip here and run in CI (WARP), where the existing BVH tests already run.

I did not touch the second half of #2368's report (descriptor base quantization): the raw base here is the root address from the user data (root_address), which is the whole 40-bit title address rather than the descriptor's >> 8 form, so I could not see a quantization there. If the reporter means a different reconstruction, that should be its own change.

**@⁠Calvix2k** on GitHub · 2026-11-25 14:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2501) `Calvix2k:codex/shader-bvh-node-offset-64` → `main` · +73 −20 · 2 files · 2 commits · open Fixes #2368 (the shift/add half of it). ### What was wrong With the node's base taken from a **root descriptor** (the `Source::Built` path, where the title shifted a root SRV address into place), the node's byte offset was built in 32 bits and then widened: ``` at = (node & !7) << 3 ; i32 at = at + k (+ dynamic) ; i32 Nodes::Raw(base + wide64(at, 0)) ``` A BVH larger than 2 GiB has node offsets that do not fit in 32 bits, and `<< 3` wraps. The explicit-descriptor path a few lines below already does the same shift in 64 bits (`wide64(lo, node_hi)` then `<< 3`), so the two halves of the same instruction disagreed about the width of the same quantity. ### What the change does For a raw (title-memory) base the offset is now built in 64 bits — the shift of `(node & !7)`, the constant part, and the computed part — and added to the 64-bit base. The **buffer-view** path keeps the 32-bit form, because that is what D3D12 takes as a buffer's offset; it is a different address space and its limits are the descriptor's. The computed and constant parts still **zero-extend** (`built_offset` gives `k` as `u32`, the computed part goes through `Cast::ZExt`), as before; a negative offset is not what this changes, and the earlier claim that it did was wrong. ### Verification `a_node_pointer_above_two_gib_reads_where_it_points` is the distinction the review asked for: `run_cases` gains a `high` mode that sets bit 29 of the node pointer (a node 4 GiB into the BVH) and has `page_table_high` map that guest page to a second copy of the nodes `64 KiB` further into the host buffer, while the page below holds zeros. On the old 32-bit shift the offset wraps back to the zeros and every case misses the reference; with the 64-bit shift the pointer reads its copy. Locally, `cargo test -p victory-shader --lib --no-run` builds and clippy `-D warnings` is clean. The GPU cases need an SM 6.6 device; this Windows 10 test harness has no Agility opt-in, so they skip here and run in CI (WARP), where the existing BVH tests already run. I did not touch the second half of #2368's report (descriptor base quantization): the raw base here is the root address from the user data (`root_address`), which is the whole 40-bit title address rather than the descriptor's `>> 8` form, so I could not see a quantization there. If the reporter means a different reconstruction, that should be its own change.
With the node's base taken from a root descriptor ("Built"), the node's byte
offset was shifted and added in 32 bits and then zero-extended to 64: a BVH
larger than 2 GiB has offsets that do not fit, and any negative intermediate
(the adds are signed) became about 4 GiB too large instead of sign-extending.
The explicit-descriptor path next to it already does the shift in 64 bits,
so the two halves of the same instruction disagreed.

The offset is now built in 64 bits for a raw (title-memory) base — shift,
constant, and the computed part — and only the buffer-view path keeps the
32-bit form D3D12 takes for a buffer's offset.

`intersections_with_64_bit_pointers_on_the_gpu_equal_the_reference` covers
this path on the card; it and the rest of the lib suite are green (247
passed, 3 ignored). A case that *fails* before this needs a BVH of more than
2 GiB, which this machine does not hold, so the change is argued from the
arithmetic rather than from a red test — if a title-side repro shows up, this
is the first place to look.
Review feedback on #2501: the comment said the 64-bit shift also fixed a
negative offset becoming about 4 GiB too large, but `built_offset` gives `k`
as `u32` and the computed part goes through `Cast::ZExt`, so the new code
zero-extends too; the old 32-bit add wrapped instead. Drop the claim.

Add the test the review asked for: `run_cases` gains a `high` mode that sets
bit 29 of the node pointer and maps the page 4 GiB higher in the page table to
a second copy of the nodes, with zeros in the page below. With the old 32-bit
shift the offset wraps back to the zeros and no case matches the reference;
with the 64-bit shift the pointer reaches its copy. `page_table_high` adds the
level-1 entry for the 256 MiB region 4 GiB up and a block for it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:07:37 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:57 UTC · original · edited 2026-11-25 16:48 UTC

Automated triage for the maintainers.

Merge candidate. In Lower::bvh (crates/victory-shader/src/emit/lower/bvh.rs), a root-descriptor BVH now computes its node offset in 64 bits: the shift by 3, the constant and the dynamic part. This matches the explicit-descriptor path, so a node more than 2 GiB into the BVH no longer wraps. The buffer-view path keeps its 32-bit offset, which is what D3D12 takes.

On your reply: both points are addressed.

  • The negative-offset claim is gone, and the code zero-extends consistently.
  • a_node_pointer_above_two_gib_reads_where_it_points tells the two forms apart. It sets bit 29 of the node pointer, and page_table_high maps the page at TITLE + 4 GiB (level-1 index + 16) to a copy of the nodes HIGH bytes into the buffer, with zeros at the low address. The old 32-bit shift would read zeros and fail against the reference.

Optional nit: page_table_high fills its new level-2 block with zeros, not [null, 0] like page_table, so the other pages in that region map to view 0. That is harmless here, since only the one page is read.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2501#issuecomment-7167460126) · edited 2026-11-25 16:48 UTC <!-- victory-triage v1 head=d916d06661e0820c483abf57a4963325bc61cf42 verdict=merge-candidate at=2026-11-25T16:45:09Z --> Automated triage for the maintainers. **Merge candidate.** In `Lower::bvh` (`crates/victory-shader/src/emit/lower/bvh.rs`), a root-descriptor BVH now computes its node offset in 64 bits: the shift by 3, the constant and the dynamic part. This matches the explicit-descriptor path, so a node more than 2 GiB into the BVH no longer wraps. The buffer-view path keeps its 32-bit offset, which is what D3D12 takes. On your reply: both points are addressed. - The negative-offset claim is gone, and the code zero-extends consistently. - `a_node_pointer_above_two_gib_reads_where_it_points` tells the two forms apart. It sets bit 29 of the node pointer, and `page_table_high` maps the page at `TITLE` + 4 GiB (level-1 index + 16) to a copy of the nodes `HIGH` bytes into the buffer, with zeros at the low address. The old 32-bit shift would read zeros and fail against the reference. Optional nit: `page_table_high` fills its new level-2 block with zeros, not `[null, 0]` like `page_table`, so the other pages in that region map to view 0. That is harmless here, since only the one page is read. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 16:39 UTC · original

The two points are addressed in d916d06:

  • The "negative offset" claim is gone from the commit and this body — the code zero-extends, as it did; that sentence was wrong.
  • a_node_pointer_above_two_gib_reads_where_it_points is the distinguishing test: bit 29 of the node pointer, the page table maps that page 4 GiB higher to a second copy of the nodes, zeros below. The old 32-bit shift wraps to the zeros and every case misses; the 64-bit one reads the copy. page_table_high adds the level-1 entry for the 256 MiB region 4 GiB up and a block for it.

CI is green on the rebased head.

**@⁠Calvix2k** on GitHub · 2026-11-25 16:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2501#issuecomment-7168320668) The two points are addressed in `d916d06`: - The "negative offset" claim is gone from the commit and this body — the code zero-extends, as it did; that sentence was wrong. - `a_node_pointer_above_two_gib_reads_where_it_points` is the distinguishing test: bit 29 of the node pointer, the page table maps that page 4 GiB higher to a second copy of the nodes, zeros below. The old 32-bit shift wraps to the zeros and every case misses; the 64-bit one reads the copy. `page_table_high` adds the level-1 entry for the 256 MiB region 4 GiB up and a block for it. CI is green on the rebased head.
Author
Owner

@⁠rminzao on GitHub · 2026-11-26 01:52 UTC · original

Tested head d916d06 on Windows 11 with an RTX 4050 Laptop (595.97), WARP and an Intel UHD (31.0.101.4146), running cargo test -p victory-shader --lib bvh:

RTX 4050 WARP Intel UHD
this PR 15 passed 15 passed 11 passed, 4 failed
a_node_pointer_above_two_gib_reads_where_it_points on the base's lower/bvh.rs fails fails —

The new test is a real regression test: it fails without the change on both devices where the BVH suite is green. The 4 Intel failures, the new test among them, are the 1-ULP box distances (1118671093 vs …094) that every BVH GPU test shows on this driver. They come from fused multiply-adds (#1856) and have nothing to do with this change.

One gap, which the triage also flagged: the Source::Built path still widens the node pointer with a zero high word (wide64(self, lo, zero)). So the issue's third counterexample (lo=5, hi=1, base + 32 GiB) still reads at the base. The explicit-descriptor path below uses wide64(lo, node_hi). #2467 makes that same one-word change on this path (node_hi.unwrap_or(zero)). The test here passes on #2467's lowering too (WARP, 15 passed). Using node_hi here, and adding a hi case to run_cases if page_table_high can map that far, would cover all three counterexamples.

Adding k and the computed part to the 64-bit offset separately matches resource::Address::offset ("added modulo 2^32, with carry into the high half"). #2467 sums them in 32 bits first, which drops that carry when the two parts overflow together.

AI-assisted (Claude Code).

**@⁠rminzao** on GitHub · 2026-11-26 01:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2501#issuecomment-7180237777) Tested head d916d06 on Windows 11 with an RTX 4050 Laptop (595.97), WARP and an Intel UHD (31.0.101.4146), running `cargo test -p victory-shader --lib bvh`: | | RTX 4050 | WARP | Intel UHD | |---|---|---|---| | this PR | 15 passed | 15 passed | 11 passed, 4 failed | | `a_node_pointer_above_two_gib_reads_where_it_points` on the base's `lower/bvh.rs` | fails | fails | — | The new test is a real regression test: it fails without the change on both devices where the BVH suite is green. The 4 Intel failures, the new test among them, are the 1-ULP box distances (`1118671093` vs `…094`) that every BVH GPU test shows on this driver. They come from fused multiply-adds (#1856) and have nothing to do with this change. One gap, which the triage also flagged: the `Source::Built` path still widens the node pointer with a zero high word (`wide64(self, lo, zero)`). So the issue's third counterexample (`lo=5, hi=1`, base + 32 GiB) still reads at the base. The explicit-descriptor path below uses `wide64(lo, node_hi)`. #2467 makes that same one-word change on this path (`node_hi.unwrap_or(zero)`). The test here passes on #2467's lowering too (WARP, 15 passed). Using `node_hi` here, and adding a `hi` case to `run_cases` if `page_table_high` can map that far, would cover all three counterexamples. Adding `k` and the computed part to the 64-bit offset separately matches `resource::Address::offset` ("added modulo 2^32, with carry into the high half"). #2467 sums them in 32 bits first, which drops that carry when the two parts overflow together. _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/2501/head:github-bot/gh-2501'
git switch 'github-bot/gh-2501'

Merge

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