[Ready to merge] Shaders: emulate image_bvh intersect instructions as miss to enable lighting compute shaders #1533

Closed
opened 2026-11-26 04:35:20 +00:00 by github-bot · 4 comments
Owner

@⁠Xvania7 on GitHub · 2026-11-24 18:52 UTC · original

Related to #876, #71, #274.

Problem

In world rendering, rasterized geometry in G-buffer targets is present, but the 2560x1440 RGBA16F accumulation target that TAA and Bloom read stays flat/clear-colored (a few KB as PNG), starving post-processing passes and upscalers of lit scene pixels (as identified by @⁠luisaugustz in #876 and @⁠HenriqueSBueno in #1317).

Root Cause:
The direct lighting, RTGI probe resolve (CS_RaytracingResolve), and directional shadow passes (rtdirectionalshadows) walk the console's BVH4 structures using RDNA2 MIMG instructions:

  • image_bvh_intersect_ray (opcode 0x0e6)
  • image_bvh64_intersect_ray (opcode 0x0e7)

In upstream crates/victory-shader/src/emit/lower.rs:image(), these instructions had no lowering match arm and reached _ => return Err(name), failing translation with ShaderError::Unsupported and dropping over 40-77 compute pipelines per frame.

Solution

This branch implements an emulation pass for image_bvh_intersect_ray and image_bvh64_intersect_ray as a clean miss termination:

  1. Fallback as Miss:

    • crates/victory-shader/src/gfx10/fields.rs: Ensure image_bvh* allocate 4 dwords for destination vector vdata.
    • crates/victory-shader/src/emit/lower.rs:image(): Intercept image_bvh* at the top of image(), writing 0 to all 4 destination VGPRs under the active lane guard.
    • On RDNA2, vdata[0] == 0 represents a zero child hit mask. The console's traversal loop interprets this as "no bounding box or triangle intersected", cleanly terminating traversal:
      • For shadow rays (rtdirectionalshadows): no occluder hit -> surfaces are evaluated as unoccluded/lit (shadow = 1.0).
      • For RTGI / reflection rays (CS_RaytracingResolve): no geometry hit -> falls back to probe/skybox radiance.
  2. Descriptor Mapping:

    • crates/victory-shader/src/emit/lower.rs:descriptor_data(): Treat image_bvh* descriptors as raw buffer views (kind 11) rather than typed textures, bypassing texture dimension queries on BVH buffers.

Verification

  • Added synthetic unit tests in crates/victory-shader/src/emit/lower.rs:
    • image_bvh_intersect_ray_lowers_as_miss
    • image_bvh64_intersect_ray_lowers_as_miss
  • cargo test -p victory-shader --lib emit::lower: 13 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings).
  • cargo xtask policy: 154 files ok.

Branch: feat/bvh-raytracing-fallback (commit da8761c).

**@⁠Xvania7** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/1533) Related to #876, #71, #274. ### Problem In world rendering, rasterized geometry in G-buffer targets is present, but the 2560x1440 RGBA16F accumulation target that TAA and Bloom read stays flat/clear-colored (a few KB as PNG), starving post-processing passes and upscalers of lit scene pixels (as identified by @⁠luisaugustz in #876 and @⁠HenriqueSBueno in #1317). **Root Cause:** The direct lighting, RTGI probe resolve (`CS_RaytracingResolve`), and directional shadow passes (`rtdirectionalshadows`) walk the console's BVH4 structures using RDNA2 MIMG instructions: - `image_bvh_intersect_ray` (opcode `0x0e6`) - `image_bvh64_intersect_ray` (opcode `0x0e7`) In upstream `crates/victory-shader/src/emit/lower.rs:image()`, these instructions had no lowering match arm and reached `_ => return Err(name)`, failing translation with `ShaderError::Unsupported` and dropping over 40-77 compute pipelines per frame. ### Solution This branch implements an emulation pass for `image_bvh_intersect_ray` and `image_bvh64_intersect_ray` as a clean miss termination: 1. **Fallback as Miss**: - `crates/victory-shader/src/gfx10/fields.rs`: Ensure `image_bvh*` allocate 4 dwords for destination vector `vdata`. - `crates/victory-shader/src/emit/lower.rs:image()`: Intercept `image_bvh*` at the top of `image()`, writing `0` to all 4 destination VGPRs under the active lane guard. - On RDNA2, `vdata[0] == 0` represents a zero child hit mask. The console's traversal loop interprets this as "no bounding box or triangle intersected", cleanly terminating traversal: - For shadow rays (`rtdirectionalshadows`): no occluder hit -> surfaces are evaluated as unoccluded/lit (`shadow = 1.0`). - For RTGI / reflection rays (`CS_RaytracingResolve`): no geometry hit -> falls back to probe/skybox radiance. 2. **Descriptor Mapping**: - `crates/victory-shader/src/emit/lower.rs:descriptor_data()`: Treat `image_bvh*` descriptors as raw buffer views (kind 11) rather than typed textures, bypassing texture dimension queries on BVH buffers. ### Verification - Added synthetic unit tests in `crates/victory-shader/src/emit/lower.rs`: - `image_bvh_intersect_ray_lowers_as_miss` - `image_bvh64_intersect_ray_lowers_as_miss` - `cargo test -p victory-shader --lib emit::lower`: 13 passed, 0 failed. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean (0 warnings). - `cargo xtask policy`: 154 files ok. Branch: [`feat/bvh-raytracing-fallback`](https://github.com/Xvania7/VIctory/tree/feat/bvh-raytracing-fallback) (commit `da8761c`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:20 +00:00
Author
Owner

@⁠luisaugustz on GitHub · 2026-11-24 19:04 UTC · original

A data point from a different stack, to scope this.

Stack: r4tedx integration/testers (92bc27b) + #638 + #302/#489/#710 + #876, on an RX 9070 XT. There, victory translate --validate --list over shaders.rpf shows no program failing on image_bvh_intersect_ray / image_bvh64_intersect_ray.

I ported this change onto that stack (the two lowering pieces plus the fields.rs dword count). Before and after:

  • Translation: identical, program for program: 14,108 vertex/pixel, 2,411 compute and 8,555 mesh programs validated in both.
  • Why: on that stack the rtdirectionalshadows trace programs (CS_RTShadowTrace32*, CS_RTDistantShadowTrace32_Tiled, ...) stop one step earlier, in resource tracking, at an address not from a root parameter.
  • What that address is: read from the disassembly, it is built from a descriptor's own base-address bits (shifted to 256-byte alignment, | 48) and then read with s_load_dword, which looks like the BVH header. So they never reach the BVH instructions.
  • CS_RaytracingResolve already translated on that stack.

So on main this may well be the first blocker, as described above. On stacks with more of the shader PRs, these programs also need their memory reads by computed address to work. fxceless/victory-gta6 (#1528) appears to address both: a directory from title addresses to host copies for raw-address loads, and a full ray/box and ray/triangle emulation instead of a miss.

I've kept this change in our stack; it costs nothing. I'll report whether anything lights once the trace programs get past resource tracking.

AI-assisted (Claude Code): measured offline on this machine.

**@⁠luisaugustz** on GitHub · 2026-11-24 19:04 UTC · [original](https://github.com/Marvisak/VIctory/issues/1533#issuecomment-7141552985) A data point from a different stack, to scope this. **Stack:** r4tedx `integration/testers` (`92bc27b`) + #638 + #302/#489/#710 + #876, on an RX 9070 XT. There, `victory translate --validate --list` over `shaders.rpf` shows **no program failing on `image_bvh_intersect_ray` / `image_bvh64_intersect_ray`**. I ported this change onto that stack (the two lowering pieces plus the `fields.rs` dword count). Before and after: - **Translation:** identical, program for program: 14,108 vertex/pixel, 2,411 compute and 8,555 mesh programs validated in both. - **Why:** on that stack the `rtdirectionalshadows` trace programs (`CS_RTShadowTrace32*`, `CS_RTDistantShadowTrace32_Tiled`, ...) stop one step earlier, in resource tracking, at `an address not from a root parameter`. - **What that address is:** read from the disassembly, it is built from a descriptor's own base-address bits (shifted to 256-byte alignment, `| 48`) and then read with `s_load_dword`, which looks like the BVH header. So they never reach the BVH instructions. - **`CS_RaytracingResolve`** already translated on that stack. So on `main` this may well be the first blocker, as described above. On stacks with more of the shader PRs, these programs also need their memory reads by computed address to work. `fxceless/victory-gta6` (#1528) appears to address both: a directory from title addresses to host copies for raw-address loads, and a full ray/box and ray/triangle emulation instead of a miss. I've kept this change in our stack; it costs nothing. I'll report whether anything lights once the trace programs get past resource tracking. _AI-assisted (Claude Code): measured offline on this machine._
Author
Owner

@⁠Xvania7 on GitHub · 2026-11-24 19:11 UTC · original

@⁠luisaugustz Thanks for measuring that against shaders.rpf — knowing CS_RaytracingResolve already translates on integration/testers and that CS_RTShadowTrace32* / CS_RTDistantShadowTrace32_Tiled stop earlier on the s_load_dword BVH header read ((desc << 8) | 48) in resource.rs scopes this cleanly.

Two updates on feat/bvh-raytracing-fallback (now cleanly rebased onto origin/main ffe310e at a10c287, dropping the unrelated FSPX commits that had been stacked underneath):

  1. Accurate RDNA2 miss encoding (bvh_miss): Returning all zeros was unsafe for real traversal loops because 0 is not INVALID_NODE (0xFFFFFFFF) for box nodes and yields 0 / 0 = NaN on leaves. Lowering now checks node_lo & 7 dynamically and returns [0x7F80_0000, 0x3F80_0000, 0, 0] (+inf, 1.0) for leaf nodes (0..=3) and [0xFFFF_FFFF; 4] (INVALID_NODE) for box/internal nodes (4..=7).
  2. BVH descriptor tolerance & data reads: resource::track now tolerates dynamically built/loop-carried image_bvh* descriptors without scheduling waterfall checks, and descriptor_data() resolves descriptors used only by image_bvh* cleanly to zero without allocating a raw buffer handle.

Validated with dxil.dll on the three synthetic BVH tests (101 passed in victory-shader non-GPU lib tests, clippy and xtask policy clean). On its own this keeps the lightweight miss path without the runtime cost of address_map, and pairs cleanly if a runtime computed-address s_load / global_load solution (like #1198 / victory-gta6) brings CS_RTShadowTrace* past resource::track.

**@⁠Xvania7** on GitHub · 2026-11-24 19:11 UTC · [original](https://github.com/Marvisak/VIctory/issues/1533#issuecomment-7141732469) @⁠luisaugustz Thanks for measuring that against `shaders.rpf` — knowing `CS_RaytracingResolve` already translates on `integration/testers` and that `CS_RTShadowTrace32*` / `CS_RTDistantShadowTrace32_Tiled` stop earlier on the `s_load_dword` BVH header read (`(desc << 8) | 48`) in `resource.rs` scopes this cleanly. Two updates on [`feat/bvh-raytracing-fallback`](https://github.com/Xvania7/VIctory/tree/feat/bvh-raytracing-fallback) (now cleanly rebased onto `origin/main` `ffe310e` at `a10c287`, dropping the unrelated FSPX commits that had been stacked underneath): 1. **Accurate RDNA2 miss encoding (`bvh_miss`):** Returning all zeros was unsafe for real traversal loops because `0` is not `INVALID_NODE` (`0xFFFFFFFF`) for box nodes and yields `0 / 0 = NaN` on leaves. Lowering now checks `node_lo & 7` dynamically and returns `[0x7F80_0000, 0x3F80_0000, 0, 0]` (`+inf, 1.0`) for leaf nodes (`0..=3`) and `[0xFFFF_FFFF; 4]` (`INVALID_NODE`) for box/internal nodes (`4..=7`). 2. **BVH descriptor tolerance & data reads:** `resource::track` now tolerates dynamically built/loop-carried `image_bvh*` descriptors without scheduling waterfall checks, and `descriptor_data()` resolves descriptors used only by `image_bvh*` cleanly to zero without allocating a raw buffer handle. Validated with `dxil.dll` on the three synthetic BVH tests (101 passed in `victory-shader` non-GPU lib tests, `clippy` and `xtask policy` clean). On its own this keeps the lightweight miss path without the runtime cost of `address_map`, and pairs cleanly if a runtime computed-address `s_load` / `global_load` solution (like #1198 / `victory-gta6`) brings `CS_RTShadowTrace*` past `resource::track`.
Author
Owner

@⁠HenriqueSBueno on GitHub · 2026-11-24 19:28 UTC · original

A data point that may help scope this. On our stack (RTX 3080, Series S profile, main + #189 #89 #211 #282 #493 #530 #587 + local translator changes for spilled user data, wave-uniform v_readfirstlane, raw root UAVs and descriptor-heap reads as data), no shader reports image_bvh* as its first refusal: 430 refusals, led by lane-mask classes ("select on an untracked mask" 77, "lane mask … read as data" 68, "cross-lane op in divergent flow" 60). The translator only logs the first refusal per program, so the lighting programs probably stop on those classes first and would reach the BVH instructions next. That fits @⁠luisaugustz's note that CS_RTShadowTrace32* stops earlier on the BVH header read.

So for a lit frame we expect to need both this change and the lane-mask work (H-6.1–H-6.3 in #1317). In a world-frame target dump on our side, the G-buffer targets hold geometry while every 1280x720 lighting/HDR target (DXGI formats 10, 26, 42, 61) is all zero, the same symptom as the flat RGBA16F buffer here.

AI-assisted (Claude Code)

**@⁠HenriqueSBueno** on GitHub · 2026-11-24 19:28 UTC · [original](https://github.com/Marvisak/VIctory/issues/1533#issuecomment-7142158396) A data point that may help scope this. On our stack (RTX 3080, Series S profile, `main` + #189 #89 #211 #282 #493 #530 #587 + local translator changes for spilled user data, wave-uniform `v_readfirstlane`, raw root UAVs and descriptor-heap reads as data), **no shader reports `image_bvh*` as its first refusal**: 430 refusals, led by lane-mask classes ("select on an untracked mask" 77, "lane mask … read as data" 68, "cross-lane op in divergent flow" 60). The translator only logs the first refusal per program, so the lighting programs probably stop on those classes first and would reach the BVH instructions next. That fits @⁠luisaugustz's note that `CS_RTShadowTrace32*` stops earlier on the BVH header read. So for a lit frame we expect to need both this change and the lane-mask work (H-6.1–H-6.3 in #1317). In a world-frame target dump on our side, the G-buffer targets hold geometry while every 1280x720 lighting/HDR target (DXGI formats 10, 26, 42, 61) is all zero, the same symptom as the flat RGBA16F buffer here. AI-assisted (Claude Code)
Author
Owner

@⁠Xvania7 on GitHub · 2026-11-25 07:44 UTC · original

Thanks @⁠r4tedx for checking against \940d32f! Confirmed \main\ now includes full BVH intersection lowering (\␀b35b7f\ / \lower/bvh.rs), so this miss fallback is superseded. Closing in favor of \main\ (and #1883 / #2027 for the remaining register-join validation on the 40 RT programs).

**@⁠Xvania7** on GitHub · 2026-11-25 07:44 UTC · [original](https://github.com/Marvisak/VIctory/issues/1533#issuecomment-7156207206) Thanks @⁠r4tedx for checking against \940d32f\! Confirmed \main\ now includes full BVH intersection lowering (\␀b35b7f\ / \lower/bvh.rs\), so this miss fallback is superseded. Closing in favor of \main\ (and #1883 / #2027 for the remaining register-join validation on the 40 RT programs).
This discussion has been locked. Commenting is limited to contributors.
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#1533
No description provided.