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.

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.

2 participants