feat(rt): AMD hardware BVH ray-node intersection via IntersectBvhNode (VICTORY_HW_BVH=1) #317

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

@⁠MeMako on GitHub · 2026-11-26 16:14 UTC · original
MeMako:pr/amd-hw-bvh-110 → main · +868 −32 · 11 files · 1 commits · open

Implements opt-in AMD hardware BVH4 ray-node intersection (VICTORY_HW_BVH=1) for image_bvh_intersect_ray and image_bvh64_intersect_ray, closing #110.

Summary of Changes

  • Guarded AMD D3D12 extension loader (victory-shader/src/emit/host.rs, victory-gpu/src/host.rs):
    • Verifies adapter_desc.VendorId == 0x1002, non-software adapter, and D3D12_RAYTRACING_TIER >= D3D12_RAYTRACING_TIER_1_0 (D3D12_FEATURE_D3D12_OPTIONS5) before touching amdxc64.dll—preventing foreign-device calls on mixed AMD iGPU + NVIDIA dGPU systems and preventing pre-RDNA 2 (GCN / RDNA 1) GPUs from taking the hardware path.
    • Resolves AmdExtD3DCreateInterface from the already-loaded amdxc64.dll via GetModuleHandleA/GetProcAddress, acquires IAmdExtD3DFactory → IAmdExtD3DShaderIntrinsics, checks support, and calls Enable(). Held on Host via AmdExtGuard for the device lifetime and released cleanly via COM Release() on drop. Falls back silently to software BVH on unsupported adapters/drivers.
    • RDNA 4 (gfx1201) vs RDNA 2/3 note: Verified live on Radeon RX 9070 XT (RDNA 4, driver 32.0.32029.2010), where CheckSupport(0x19) (IntersectBvhNode) returns E_NOINTERFACE while CheckSupport(0x0D) returns S_OK and opcode 0x22 compiles natively to hardware BVH4 intersection. CheckSupport(0x19) is wired for RDNA 2/3 alongside the DXR-gated 0x0D fallback, though RDNA 2/3 silicon has not been tested locally yet—an RX 7900 XTX verification pass would be great!
  • Page GPU VA table (victory-gpu/src/memory.rs, victory-shader/src/emit/shader.rs): Doubles the directory buffer (16 MiB → 32 MiB, +16 MiB) to store a parallel 64-bit host GPU VA per mapped 64 KiB page at MEMORY_VA_OFFSET. Memory::copies is append-only for process lifetime (never drops a backing ID3D12Resource), and gone() clears both va_entry and entry to 0 before returning slots to self.free, ensuring no in-flight command list can traverse a freed ID3D12Resource VA.
  • Hardware BVH lowering (victory-shader/src/emit/intersect.rs): When VICTORY_HW_BVH=1 is active, looks up the host 64-bit page VA, notes unmapped pages in SLOT_FAULTS, validates 64-byte alignment of both va_lo and within (((va_lo | within) & 63) == 0) plus in-page bounds (within <= 0xFF80 for 128-byte box32, within <= 0xFFC0 for 64-byte nodes), computes nodePointer = ((page_va + within) >> 3) | kind, and emits the IntersectBvhNode (0x22) magic UAV atomic sequence (u0, space2147420894, BoxSortEnable | ReturnBarycentrics). Unaligned or page-straddling nodes fall back cleanly to the software path.
  • Pipeline & shader cache (victory-gpu/src/pipeline/host.rs, draw.rs, shader_cache.rs): Adds the SLOT_AMD_EXT root UAV binding (u0, space2147420894) and keys the shader cache dynamically on VICTORY_HW_BVH=1 + opcode so default software caches are untouched (the first VICTORY_HW_BVH=1 launch warms a fresh cache).

Verification (based on origin/main c9c932c)

  • Static checks:
    • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.
    • cargo clippy -p victory-gpu --all-targets --locked: 0 warnings in all modified files (1 pre-existing warning in untouched watch.rs:3626 on origin/main).
  • Tests (VICTORY_HW_BVH unset AND VICTORY_HW_BVH=1):
    • cargo test -p victory-shader --lib intersect_tests --locked: 10/10 passed on RX 9070 XT (including amd_hw_bvh_directory_form_lowers_and_validates with dxil.dll and amd_hw_bvh_matches_the_reference_on_the_card across all 352 box32/box16/triangle/sorted/unsorted/32-bit/64-bit/masked cases).
    • cargo test -p victory-gpu --lib memory:: --locked: 2/2 passed.
    • cargo test -p victory-gpu --lib pipeline:: --locked: 25/25 passed.
    • cargo test -p victory-gpu --lib shader_cache:: --locked: 3/3 passed.
  • Live 1080p full-RT A/B test (Windows 11 10.0.26200, Ryzen 9 7950X, Radeon RX 9070 XT, driver 32.0.32029.2010, -PortCbv, two ~5-minute sessions):
    • GPU frame time dropped ~1.8× (~54 ms GPU busy → ~29.7 ms GPU busy), moving steady-state FPS from 15.4–17.4 FPS → 20.4–21.6 FPS (and heavier scenes from 13.7–15.4 FPS → 16.9–19.7 FPS, now CPU-bound with GPU at 60–82% busy).
    • Visual A/B comparison confirmed parity with the software BVH path; 0 crashes, 0 device removals, 0 refused pipelines, 0 dropped draws.

Human-owned contribution: all design decisions, hardware testing, and code review were performed by me; AI coding assistance was used during implementation and drafting.

**@⁠MeMako** on GitHub · 2026-11-26 16:14 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/317) `MeMako:pr/amd-hw-bvh-110` → `main` · +868 −32 · 11 files · 1 commits · open Implements opt-in AMD hardware BVH4 ray-node intersection (`VICTORY_HW_BVH=1`) for `image_bvh_intersect_ray` and `image_bvh64_intersect_ray`, closing #110. ### Summary of Changes - **Guarded AMD D3D12 extension loader (`victory-shader/src/emit/host.rs`, `victory-gpu/src/host.rs`):** - Verifies `adapter_desc.VendorId == 0x1002`, non-software adapter, and `D3D12_RAYTRACING_TIER >= D3D12_RAYTRACING_TIER_1_0` (`D3D12_FEATURE_D3D12_OPTIONS5`) before touching `amdxc64.dll`—preventing foreign-device calls on mixed AMD iGPU + NVIDIA dGPU systems and preventing pre-RDNA 2 (GCN / RDNA 1) GPUs from taking the hardware path. - Resolves `AmdExtD3DCreateInterface` from the already-loaded `amdxc64.dll` via `GetModuleHandleA`/`GetProcAddress`, acquires `IAmdExtD3DFactory` → `IAmdExtD3DShaderIntrinsics`, checks support, and calls `Enable()`. Held on `Host` via `AmdExtGuard` for the device lifetime and released cleanly via COM `Release()` on drop. Falls back silently to software BVH on unsupported adapters/drivers. - **RDNA 4 (`gfx1201`) vs RDNA 2/3 note:** Verified live on Radeon RX 9070 XT (RDNA 4, driver `32.0.32029.2010`), where `CheckSupport(0x19)` (`IntersectBvhNode`) returns `E_NOINTERFACE` while `CheckSupport(0x0D)` returns `S_OK` and opcode `0x22` compiles natively to hardware BVH4 intersection. `CheckSupport(0x19)` is wired for RDNA 2/3 alongside the DXR-gated `0x0D` fallback, though RDNA 2/3 silicon has not been tested locally yet—an RX 7900 XTX verification pass would be great! - **Page GPU VA table (`victory-gpu/src/memory.rs`, `victory-shader/src/emit/shader.rs`):** Doubles the directory buffer (`16 MiB` → `32 MiB`, **`+16 MiB`**) to store a parallel 64-bit host GPU VA per mapped 64 KiB page at `MEMORY_VA_OFFSET`. `Memory::copies` is append-only for process lifetime (never drops a backing `ID3D12Resource`), and `gone()` clears both `va_entry` and `entry` to `0` before returning slots to `self.free`, ensuring no in-flight command list can traverse a freed `ID3D12Resource` VA. - **Hardware BVH lowering (`victory-shader/src/emit/intersect.rs`):** When `VICTORY_HW_BVH=1` is active, looks up the host 64-bit page VA, notes unmapped pages in `SLOT_FAULTS`, validates 64-byte alignment of both `va_lo` and `within` (`((va_lo | within) & 63) == 0`) plus in-page bounds (`within <= 0xFF80` for 128-byte box32, `within <= 0xFFC0` for 64-byte nodes), computes `nodePointer = ((page_va + within) >> 3) | kind`, and emits the `IntersectBvhNode` (`0x22`) magic UAV atomic sequence (`u0, space2147420894`, `BoxSortEnable | ReturnBarycentrics`). Unaligned or page-straddling nodes fall back cleanly to the software path. - **Pipeline & shader cache (`victory-gpu/src/pipeline/host.rs`, `draw.rs`, `shader_cache.rs`):** Adds the `SLOT_AMD_EXT` root UAV binding (`u0, space2147420894`) and keys the shader cache dynamically on `VICTORY_HW_BVH=1` + opcode so default software caches are untouched (the first `VICTORY_HW_BVH=1` launch warms a fresh cache). ### Verification (based on `origin/main` `c9c932c`) - **Static checks:** - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean. - `cargo clippy -p victory-gpu --all-targets --locked`: 0 warnings in all modified files (1 pre-existing warning in untouched `watch.rs:3626` on `origin/main`). - **Tests (`VICTORY_HW_BVH` unset AND `VICTORY_HW_BVH=1`):** - `cargo test -p victory-shader --lib intersect_tests --locked`: **10/10 passed** on RX 9070 XT (including `amd_hw_bvh_directory_form_lowers_and_validates` with `dxil.dll` and `amd_hw_bvh_matches_the_reference_on_the_card` across all 352 box32/box16/triangle/sorted/unsorted/32-bit/64-bit/masked cases). - `cargo test -p victory-gpu --lib memory:: --locked`: **2/2 passed**. - `cargo test -p victory-gpu --lib pipeline:: --locked`: **25/25 passed**. - `cargo test -p victory-gpu --lib shader_cache:: --locked`: **3/3 passed**. - **Live 1080p full-RT A/B test (Windows 11 `10.0.26200`, Ryzen 9 7950X, Radeon RX 9070 XT, driver `32.0.32029.2010`, `-PortCbv`, two ~5-minute sessions):** - GPU frame time dropped **~1.8×** (`~54 ms` GPU busy → `~29.7 ms` GPU busy), moving steady-state FPS from `15.4–17.4 FPS` → **`20.4–21.6 FPS`** (and heavier scenes from `13.7–15.4 FPS` → **`16.9–19.7 FPS`**, now CPU-bound with GPU at 60–82% busy). - Visual A/B comparison confirmed parity with the software BVH path; **0 crashes, 0 device removals, 0 refused pipelines, 0 dropped draws**. --- *Human-owned contribution: all design decisions, hardware testing, and code review were performed by me; AI coding assistance was used during implementation and drafting.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:16:50 +00:00
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-26 18:50 UTC · original

Useful direction for the picture, not just for ray-traced effects: in the prologue's heavy view our log reports ~340 draws a frame dropped with "a stage of the ray-tracing structure builder, which is off" - that is the missing geometry, not a shader gap. Enabling the builder today trades that geometry for frame time.

On an RX 7800 XT (RDNA3, Windows 10 22H2) the heavy frame is 167 ms at ~6,800 draws with GPU ~95 ms. So the number that decides this PR for us is the cost of the builder when it runs: if VICTORY_HW_BVH=1 brings the build within a few ms, the ~340 draws come back for almost nothing. A before/after measurement of the builder with the switch on would be very welcome.

**@⁠Calvix2k** on GitHub · 2026-11-26 18:50 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/317#issuecomment-7200903580) Useful direction for the picture, not just for ray-traced effects: in the prologue's heavy view our log reports ~340 draws a frame dropped with "a stage of the ray-tracing structure builder, which is off" - that is the missing geometry, not a shader gap. Enabling the builder today trades that geometry for frame time. On an RX 7800 XT (RDNA3, Windows 10 22H2) the heavy frame is 167 ms at ~6,800 draws with GPU ~95 ms. So the number that decides this PR for us is the cost of the builder when it runs: if `VICTORY_HW_BVH=1` brings the build within a few ms, the ~340 draws come back for almost nothing. A before/after measurement of the builder with the switch on would be very welcome.
Author
Owner

@⁠MeMako on GitHub · 2026-11-26 22:22 UTC · original

Great question—happy to break down what VICTORY_HW_BVH=1 does and doesn't speed up, plus share a 3-way measurement from my RX 9070 XT:

  1. BVH building vs. ray traversal: When VICTORY_BVH_BUILDER is unset (off), VIctory's log rolls the title's skipped RT compute dispatches into the same "draws dropped" counter ("a stage of the ray-tracing structure builder, which is off"—those are the title's RT compute passes for building/refitting acceleration structures and tracing bounce light/shadows, rather than rasterized scene meshes). VICTORY_HW_BVH=1 specifically accelerates the traversal / ray-node intersection shaders (image_bvh_intersect_ray / image_bvh64_intersect_ray) via AMD's hardware IntersectBvhNode intrinsic. The BVH builder compute passes themselves are standard compute shaders without ray-intersect ops, so they keep their own GPU cost (along with the existing ~3.4 ms/frame CPU memory.rs page tracking that runs whenever VICTORY_BVH_BUILDER=1 is enabled, independent of VICTORY_HW_BVH).

  2. 3-way GPU busy time on RX 9070 XT (1080p, -Console series-x -PortCbv, fresh back-to-back A/B/C runs across the same lighter and heavier indoor scenes at matching draw counts; the PR description's ~54 ms -> ~29.7 ms figure was from an earlier single-scene pair):

    • RT off (VICTORY_BVH_BUILDER unset): ~20–22 ms GPU busy (lighter scene, ~4.5k–5.4k draws) / ~22–28 ms (heavier scene, ~6.2k–7.0k draws)
    • RT on + Software BVH (VICTORY_HW_BVH unset): ~35–37 ms GPU busy (lighter scene, ~4.4k–5.9k draws; 18.0–19.5 fps) / ~57–65 ms (heavier scene, ~8.7k–10.7k draws; 13.6–15.4 fps)
    • RT on + Hardware BVH (VICTORY_HW_BVH=1): ~29–31 ms GPU busy (lighter scene, ~4.6k–5.3k draws; 19.5–20.7 fps) / ~45–48 ms (heavier scene, ~7.4k–10.2k draws; 16.3–18.7 fps)

    So VICTORY_HW_BVH=1 shaves ~6 ms in the lighter scene and ~12–17 ms in the heavier scene—cutting the net RT-on GPU overhead by ~35–45%—though it won't reduce the builder passes down to a few ms on its own.

  3. RDNA 3 (RX 7800 XT) note: On the clean-room upstream PR (Marvisak/VIctory#3294), an independent RX 7800 XT tester (@oni-swr) already confirmed CheckSupport(0x19) returns S_OK and opcode=0x22 passes all 16/16 synthetic BVH GPU tests, and a Radeon 780M (RDNA 3 iGPU, @kilolimaVI) measured 372.97 ms -> 120.92 ms on the --release synthetic BVH64 microbenchmark (536.8M ray-node intersections/dispatch, median of 5; isolated intersection throughput, not whole-frame FPS). Since nobody has tested #317 on a live scene on RDNA 3 yet, if you have a chance to throw this branch at your RX 7800 XT heavy frame with VICTORY_BVH_BUILDER=1 + VICTORY_HW_BVH=1, a live before/after datapoint there would be awesome!

Comment drafted with Antigravity.

**@⁠MeMako** on GitHub · 2026-11-26 22:22 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/317#issuecomment-7204615747) Great question—happy to break down what `VICTORY_HW_BVH=1` does and doesn't speed up, plus share a 3-way measurement from my RX 9070 XT: 1. **BVH building vs. ray traversal:** When `VICTORY_BVH_BUILDER` is unset (off), VIctory's log rolls the title's skipped RT compute dispatches into the same `"draws dropped"` counter (`"a stage of the ray-tracing structure builder, which is off"`—those are the title's RT compute passes for building/refitting acceleration structures and tracing bounce light/shadows, rather than rasterized scene meshes). `VICTORY_HW_BVH=1` specifically accelerates the **traversal / ray-node intersection** shaders (`image_bvh_intersect_ray` / `image_bvh64_intersect_ray`) via AMD's hardware `IntersectBvhNode` intrinsic. The **BVH builder** compute passes themselves are standard compute shaders without ray-intersect ops, so they keep their own GPU cost (along with the existing ~3.4 ms/frame CPU `memory.rs` page tracking that runs whenever `VICTORY_BVH_BUILDER=1` is enabled, independent of `VICTORY_HW_BVH`). 2. **3-way GPU busy time on RX 9070 XT (1080p, `-Console series-x -PortCbv`, fresh back-to-back A/B/C runs across the same lighter and heavier indoor scenes at matching draw counts; the PR description's `~54 ms -> ~29.7 ms` figure was from an earlier single-scene pair):** - **RT off (`VICTORY_BVH_BUILDER` unset):** `~20–22 ms` GPU busy (lighter scene, ~4.5k–5.4k draws) / `~22–28 ms` (heavier scene, ~6.2k–7.0k draws) - **RT on + Software BVH (`VICTORY_HW_BVH` unset):** `~35–37 ms` GPU busy (lighter scene, ~4.4k–5.9k draws; `18.0–19.5 fps`) / `~57–65 ms` (heavier scene, ~8.7k–10.7k draws; `13.6–15.4 fps`) - **RT on + Hardware BVH (`VICTORY_HW_BVH=1`):** `~29–31 ms` GPU busy (lighter scene, ~4.6k–5.3k draws; `19.5–20.7 fps`) / `~45–48 ms` (heavier scene, ~7.4k–10.2k draws; `16.3–18.7 fps`) So `VICTORY_HW_BVH=1` shaves **`~6 ms`** in the lighter scene and **`~12–17 ms`** in the heavier scene—cutting the net RT-on GPU overhead by **~35–45%**—though it won't reduce the builder passes down to a few ms on its own. 3. **RDNA 3 (RX 7800 XT) note:** On the clean-room upstream PR (`Marvisak/VIctory#3294`), an independent RX 7800 XT tester (`@oni-swr`) already confirmed `CheckSupport(0x19)` returns `S_OK` and `opcode=0x22` passes all 16/16 synthetic BVH GPU tests, and a Radeon 780M (RDNA 3 iGPU, `@kilolimaVI`) measured `372.97 ms -> 120.92 ms` on the `--release` synthetic BVH64 microbenchmark (536.8M ray-node intersections/dispatch, median of 5; isolated intersection throughput, not whole-frame FPS). Since nobody has tested `#317` on a live scene on RDNA 3 yet, if you have a chance to throw this branch at your RX 7800 XT heavy frame with `VICTORY_BVH_BUILDER=1` + `VICTORY_HW_BVH=1`, a live before/after datapoint there would be awesome! *Comment drafted with Antigravity.*
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/317/head:github-bot/gh-317'
git switch 'github-bot/gh-317'

Merge

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