Skip to content

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

Open
davi713albano-coder wants to merge 1 commit into
DexNom:mainfrom
davi713albano-coder:dexnom/bvh-memory-fast-path
Open

davi713albano-coder wants to merge 1 commit into
DexNom:mainfrom
davi713albano-coder:dexnom/bvh-memory-fast-path

Conversation

@davi713albano-coder

Copy link
Copy Markdown

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.

@lcg007xd-c lcg007xd-c left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

notquiteog pushed a commit to notquiteog/VIctory that referenced this pull request Nov 26, 2026
…up again only when it crosses a 64 KiB page (by davi713albano-coder)

From the pull request's commit as the previous port took it (d1785aa8f on
integration-next), only the box test's part. The other part, the note of a
missing page stored only when a page is missing, is on main already:
Lower::memory notes it under its own guard.

The 32-bit box node reads its second 64 bytes. In title memory they lie in
the same 64 KiB page, and so in the same host copy at 64 bytes on, for
1,023 of 1,024 aligned nodes; only a node that starts at 0xFFC0 or later in
its page asks the directory again, on a branch of its own, as every wide
node did before. Whether the second half's page is there is the first
half's on the fast path. The structures are walked only with
VICTORY_BVH_BUILDER=1, so a run without it translates the same programs
but traces nothing.

The ray programs' translations change, so cached translations of them are
made again. Not measured; the gain is the author's reasoning.

(cherry picked in part from commit d1785aa8f6a143fbd1474a8f192d3bc7a68f36e8)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andrewDoing

Copy link
Copy Markdown

Independent native Windows release check at 54cdf0f: cargo test --release --offline --locked -p victory-shader --lib reflected_rays_walk_linked_nodes_through_the_guest_directory -- --nocapture passed. Scratch-only instrumentation made both adapter paths mandatory and asserted physical PCI ID 10de:2f04; device LUIDs resolved to RTX 5070 and Microsoft Basic Render Driver (WARP).

The existing assertions verified the entire untouched fault buffer for mapped same-page and split Box32 reads (offset 0xFFC0), the missing-page bucket for unmapped halves, and child hits after mapping the second page. Both adapters produced the same bounce sequence: leaf 0 at 2, leaf 1 at 3, leaf 0 at 3, then miss. This is one synthetic traversal test, not coverage of every BVH case or gameplay timing.

Verification was AI-assisted with GPT-6.2 Astra.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants