Shaders: guard guest-memory fault writes and skip redundant Box32 directory lookups #21

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

@⁠davi713albano-coder on GitHub · 2026-11-25 18:57 UTC · original
davi713albano-coder:dexnom/bvh-memory-fast-path → main · +231 −120 · 3 files · 1 commits · open

Summary

Reduces per-node GPU overhead on the translated guest-memory and BVH traversal hot path (Lower::memory and Lower::bvh_boxes) while keeping bit-for-bit identical results and fault reporting:

  1. Guard guest-memory fault hashing and SLOT_FAULTS stores (crates/victory-shader/src/emit/intersect.rs, lower.rs):

    • Previously Lower::memory unconditionally computed URem(page, MEMORY_FAULTS - 1) and issued a rawBufferStore.i32 to the host-visible SLOT_FAULTS UAV (writing to sink slot 0) on every single guest-memory access and BVH node visit, even when the page was already mapped.
    • Moving the remainder and fault store inside self.begin_guarded(Some(missing)) lets mapped waves skip both the software integer modulo and PCIe/host-memory write completely, while preserving identical fault recording on unmapped pages.
    • Refreshed let c = self.cur at callers of self.memory(...) in memory_flat and memory_smem, and moved Lower::memory into intersect.rs (keeping lower.rs comfortably under the 512 KiB xtask policy limit).
  2. Skip redundant page-directory lookups for same-page 128-byte Box32 BVH nodes (crates/victory-shader/src/emit/intersect.rs):

    • bvh_address produces 64-byte-aligned addresses ((pointer >> 3) << 6 added to a 256-byte-aligned base), so a 128-byte Box32 node's second 64-byte half lies in the same 64 KiB page for 1,023 of every 1,024 aligned addresses ((low & 0xFFFF) < 0xFFC0).
    • On the same-page fast path, bvh_boxes reuses the already-resolved handle and reads from byte + 64 directly (avoiding a second directory rawBufferLoad, createHandleFromHeap, and fault check, with no %dx.types.Handle PHIs).
    • Only nodes at page offset >= 0xFFC0 branch to the slow path calling address_plus(..., 64) and bvh_memory(...), preserving full cross-page residency tracking and fault reporting.

Verification

  • Extended intersect_tests::ray_nodes_through_the_guest_page_directory to assert that mapped guest-memory accesses (both same-page controls and cross-page 0xFFC0 retried) leave the entire SLOT_FAULTS buffer untouched (all == 0), while unmapped lookups (absent, split_box, beyond) continue reporting the missing page in its expected bucket.
  • Verified unmodified CI (windows-latest: clippy --workspace --all-targets --locked -- -D warnings, --all-features, cargo test --workspace --locked, xtask asset-policy check, and DXIL validation across all 311 BVH ray/node cases) and Screen.

No title data used. AI-assisted.

**@⁠davi713albano-coder** on GitHub · 2026-11-25 18:57 UTC · [original](https://github.com/DexNom/VIctory/pull/21) `davi713albano-coder:dexnom/bvh-memory-fast-path` → `main` · +231 −120 · 3 files · 1 commits · open ### Summary Reduces per-node GPU overhead on the translated guest-memory and BVH traversal hot path (`Lower::memory` and `Lower::bvh_boxes`) while keeping bit-for-bit identical results and fault reporting: 1. **Guard guest-memory fault hashing and `SLOT_FAULTS` stores (`crates/victory-shader/src/emit/intersect.rs`, `lower.rs`):** - Previously `Lower::memory` unconditionally computed `URem(page, MEMORY_FAULTS - 1)` and issued a `rawBufferStore.i32` to the host-visible `SLOT_FAULTS` UAV (writing to sink slot `0`) on every single guest-memory access and BVH node visit, even when the page was already mapped. - Moving the remainder and fault store inside `self.begin_guarded(Some(missing))` lets mapped waves skip both the software integer modulo and PCIe/host-memory write completely, while preserving identical fault recording on unmapped pages. - Refreshed `let c = self.cur` at callers of `self.memory(...)` in `memory_flat` and `memory_smem`, and moved `Lower::memory` into `intersect.rs` (keeping `lower.rs` comfortably under the 512 KiB `xtask` policy limit). 2. **Skip redundant page-directory lookups for same-page 128-byte `Box32` BVH nodes (`crates/victory-shader/src/emit/intersect.rs`):** - `bvh_address` produces 64-byte-aligned addresses (`(pointer >> 3) << 6` added to a 256-byte-aligned base), so a 128-byte `Box32` node's second 64-byte half lies in the same 64 KiB page for 1,023 of every 1,024 aligned addresses (`(low & 0xFFFF) < 0xFFC0`). - On the same-page fast path, `bvh_boxes` reuses the already-resolved handle and reads from `byte + 64` directly (avoiding a second directory `rawBufferLoad`, `createHandleFromHeap`, and fault check, with no `%dx.types.Handle` PHIs). - Only nodes at page offset `>= 0xFFC0` branch to the slow path calling `address_plus(..., 64)` and `bvh_memory(...)`, preserving full cross-page residency tracking and fault reporting. ### Verification - Extended `intersect_tests::ray_nodes_through_the_guest_page_directory` to assert that mapped guest-memory accesses (both same-page `controls` and cross-page `0xFFC0` `retried`) leave the entire `SLOT_FAULTS` buffer untouched (`all == 0`), while unmapped lookups (`absent`, `split_box`, `beyond`) continue reporting the missing page in its expected bucket. - Verified unmodified `CI` (`windows-latest`: `clippy --workspace --all-targets --locked -- -D warnings`, `--all-features`, `cargo test --workspace --locked`, `xtask` asset-policy check, and DXIL validation across all 311 BVH ray/node cases) and `Screen`. No title data used. AI-assisted.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:31:55 +00:00
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:14 UTC · original · reviewed

Reviewed against main at 2e01ab4. The change is in victory-shader's DXIL lowering. The test it extends (reflected_rays_walk_linked_nodes_through_the_guest_directory) runs a real D3D12 device and WARP, so I could only type-check it here, not execute it.

Verified on Linux

  • cargo test -p victory-shader --lib --locked: 182 passed, 2 ignored, the same as main.
  • cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings (with CC_x86_64_pc_windows_msvc=clang-cl AR_x86_64_pc_windows_msvc=llvm-lib): clean, same as main. This also type-checks the changed Windows-only test.
  • cargo xtask policy: asset policy: 195 files ok. lower.rs is 520,369 bytes on this branch against 524,060 on main, and the limit in xtask/src/policy.rs:13 is 524,288, so main had only 228 bytes of headroom.
  • git merge-tree --write-tree origin/HEAD pr21: merges cleanly into main.
  • Could not run: the revert-and-see-it-fail check on the new assertions, because they need a D3D12 device.

What I checked by reading

  • Every caller of Lower::memory now re-reads self.cur after the call, since it now opens a guarded block: lower.rs global load (2859), atomics (2909), memory_smem (3045), and intersect.rs:172 (which uses self.cur afterwards). The global store path already goes through begin_guarded and self.cur. I found no stale c left.
  • The root UAV/SRV handles are created in the entry block (shader.rs:386-432), and host_word reads user data in the entry block (lower.rs:2704), so using them inside the new fault-gate block does not break dominance.
  • Same-page fast path: the node address is built as base<<8 + (ptr>>3)<<6 (bvh_address), so the low 16 bits are 64-byte aligned and the second half crosses a 64 KiB page only when they are 0xFFC0 exactly. within >= 0xFFC0 is therefore exact, and conservative if the alignment ever slipped.
  • The residency phi now has three incomings (same-page, cross-page, half), matching the three predecessors of join. half_end is also taken after the 16-bit loads rather than assuming half_block.

Findings

One, and it is about merging rather than the code. Conflict with #22: git merge-tree --write-tree --name-only pr21 pr22 reports a content conflict in crates/victory-shader/src/emit/lower.rs (both edit memory_smem: #22 changes the self.memory(...) call line that this PR follows with let c = self.cur;). Whoever lands second will need to rebase, and the resolution must keep this PR's let c = self.cur; after the call (otherwise the loads after it use a stale block, a silent miscompile that unit tests off-GPU would not catch).

I found no defect in the lowering. I did not approve because I could not execute the test that proves the "no fault writes when mapped" claim.

Not verified

  • Running intersect_tests on a GPU or WARP, and DXIL validation by dxil.dll.
  • The performance claim (skipping the modulo, the fault store and the second directory lookup).

Reviewed with Claude Code.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:14 UTC · [original](https://github.com/DexNom/VIctory/pull/21#pullrequestreview-6065074673) · **reviewed** Reviewed against `main` at `2e01ab4`. The change is in `victory-shader`'s DXIL lowering. The test it extends (`reflected_rays_walk_linked_nodes_through_the_guest_directory`) runs a real D3D12 device and WARP, so I could only type-check it here, not execute it. ## Verified on Linux - `cargo test -p victory-shader --lib --locked`: 182 passed, 2 ignored, the same as `main`. - `cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings` (with `CC_x86_64_pc_windows_msvc=clang-cl AR_x86_64_pc_windows_msvc=llvm-lib`): clean, same as `main`. This also type-checks the changed Windows-only test. - `cargo xtask policy`: `asset policy: 195 files ok`. `lower.rs` is 520,369 bytes on this branch against 524,060 on `main`, and the limit in `xtask/src/policy.rs:13` is 524,288, so `main` had only 228 bytes of headroom. - `git merge-tree --write-tree origin/HEAD pr21`: merges cleanly into `main`. - Could not run: the revert-and-see-it-fail check on the new assertions, because they need a D3D12 device. ## What I checked by reading - Every caller of `Lower::memory` now re-reads `self.cur` after the call, since it now opens a guarded block: `lower.rs` global load (2859), atomics (2909), `memory_smem` (3045), and `intersect.rs:172` (which uses `self.cur` afterwards). The global store path already goes through `begin_guarded` and `self.cur`. I found no stale `c` left. - The root UAV/SRV handles are created in the entry block (`shader.rs:386-432`), and `host_word` reads user data in the entry block (`lower.rs:2704`), so using them inside the new fault-gate block does not break dominance. - Same-page fast path: the node address is built as `base<<8 + (ptr>>3)<<6` (`bvh_address`), so the low 16 bits are 64-byte aligned and the second half crosses a 64 KiB page only when they are `0xFFC0` exactly. `within >= 0xFFC0` is therefore exact, and conservative if the alignment ever slipped. - The residency `phi` now has three incomings (same-page, cross-page, half), matching the three predecessors of `join`. `half_end` is also taken after the 16-bit loads rather than assuming `half_block`. ## Findings One, and it is about merging rather than the code. Conflict with #22: `git merge-tree --write-tree --name-only pr21 pr22` reports a content conflict in `crates/victory-shader/src/emit/lower.rs` (both edit `memory_smem`: #22 changes the `self.memory(...)` call line that this PR follows with `let c = self.cur;`). Whoever lands second will need to rebase, and the resolution must keep this PR's `let c = self.cur;` after the call (otherwise the loads after it use a stale block, a silent miscompile that unit tests off-GPU would not catch). I found no defect in the lowering. I did not approve because I could not execute the test that proves the "no fault writes when mapped" claim. ## Not verified - Running `intersect_tests` on a GPU or WARP, and DXIL validation by `dxil.dll`. - The performance claim (skipping the modulo, the fault store and the second directory lookup). Reviewed with Claude Code.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-21'
git switch 'github-bot/gh-21'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-21'
git switch 'github-bot/gh-21'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-21'
git switch 'main'
git merge --squash 'github-bot/gh-21'
git switch 'main'
git merge --ff-only 'github-bot/gh-21'
git switch 'main'
git merge 'github-bot/gh-21'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.