Proposal: build the title's acceleration structures with host DXR and trace them with inline RayQuery #17

Open
opened 2026-11-26 03:34:40 +00:00 by github-bot · 0 comments
Owner

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

Summary

The title's ray-traced lighting (probes, bounce, reflections, lamp and sun shadows) is currently either off or planned as an emulation: the title's BVH builder programs run as translated compute and image_bvh_intersect_ray is emulated in shader code.
That path keeps the console node format, but it does not use the GPU's ray-tracing hardware, some builder pipelines still fail on wave32 hosts, and rounding and edge behaviour of the emulated test are unverified.

This issue proposes a different path aimed at hardware-accelerated traversal where available:

  1. Intercept the title's calls into XboxBvhBuilder.dll, translate the Xbox batch records into desktop DXR build inputs, and build host DXR BLAS/TLAS.
  2. Replace the title's inline traversal loops (64 compute programs that call image_bvh_intersect_ray) with DXR 1.1 inline RayQuery, keeping the original loop as a per-dispatch fallback.

Nothing here is measured yet; it is a design proposal with pointers to existing code in other projects.
Its correctness and cost need to be measured against emulation on the same scene.

Why it is feasible: the title describes its geometry through a DXR-like API

docs/fork-ray-tracing.md says mapping onto host DXR "is not practical: the layer would have to know the geometry behind structures the title never describes to it".
The title does describe it.
five-star-recomp reversed this in detail (docs/raytracing.md, docs/d3d12x-shim.md):

  • The title sets DisableDXR = 1, builds every structure through XboxBvhBuilder.dll, and traverses inline.
  • BLAS and TLAS go through one export, BuildRaytracingAccelerationStructureBatched2X(..., D3D12XBOX_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_BATCHED_DESC2*). Its 0x50-byte batch records carry D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS (Type, Flags, NumDescs, geometry or instance descs) plus guest addresses, scratch slices and postbuild records.
  • BLAS geometry is 0x38-byte triangle geometry descs built from the title's mesh records (vertex and index buffers).
  • Instance descs are a title buffer of 64-byte records, the size of D3D12_RAYTRACING_INSTANCE_DESC.
  • Static BLASes ship serialized in core_bvh4.rpf and are restored with RehydrateRaytracingAccelerationStructureBatched2X, which rebuilds the structure in place from the game's own vertex data (docs/raytracing.md L74-L108).
  • The export signatures are already shimmed in BvhBuilderShim.cs. five-star builds console-format structures on the CPU from them (src/FiveStar.Shaders/Bvh/XdxrBlas.cs, XdxrTlas.cs).

So the host can translate these records into desktop DXR inputs: query host prebuild sizes with ID3D12Device5, allocate host scratch and result buffers, remap TLAS instance records to host BLAS addresses, and record builds on ID3D12GraphicsCommandList4.
For mirrored structures this bypasses the console builder programs, including the ones that need 64-lane waves (fork-ray-tracing.md "Known gaps").
The emulated fallback still reads console-format nodes, so console-format structures have to be kept where the fallback is used, for example with a compatible CPU builder like five-star's.

The hard part: traversal is the title's own code

The trace programs traverse console-format nodes using a per-thread stack in shared memory and raw-address loads (fork-ray-tracing.md L14-L28), and read the TLAS root and instance nodes in the layout documented in five-star docs/raytracing.md "Traversal header".
To use opaque host DXR structures, this proposal replaces those walks with inline RayQuery.

Prior art for exactly this, on PS5 (same RDNA node test): KytyPS5 RayQueryTraversal.

  • ir/passes/RayQueryTraversal.cpp matches specific PS5 traversal IR shapes (flat, nested TLAS→BLAS, and shared-stack walks) and rewrites the matches to ray-query operations.
  • Rewritten operations are gated on per-dispatch mirror validity; the IR selects hardware or guest values where needed (L948-L1030, validity load in spirvEmitterRayQuery.cpp).
  • A separate GPU mirror builds host structures from guest triangle and instance records, recognises the PS5 PSR_BVHL header and validates the mirrored data with GPU-side hashes (gpu_bvh_mirror.comp, L445-L450; host side starts in rayTracing.cpp Prepare).
  • Caveat: the matcher is strict and tied to PS5 SDK code shapes; the design transfers, the matcher does not. For one title, replacing the 64 programs by hash with hand-checked RayQuery versions may be simpler than a general matcher.

Prior art for console DXR on host DXR on Xbox: OpenScarlett (californiaAlmonds).

  • CommandListX::BuildRaytracingAccelerationStructure is forwarded to the host list4 when the host list, tier and descriptors allow it (command_list_x.cpp).
  • Its XboxBvhBuilder shim exposes separate BLAS and TLAS helpers backed by DxrHardwareEngine (BLAS, TLAS, dxr_hardware.cpp). Its export matching this title's Batched2X signature is a stub that records no build (L1281, L1306-L1317).
  • It selects DXR 1.1 RayQuery or a compute BVH4 fallback from host capabilities and shader requirements (device_x.cpp).
  • It reports standalone DXR tests passing on RTX and Radeon GPUs alongside software fallback tests (README); the sources do not document an end-to-end test with this title.

Proposed plan

  1. Shim XboxBvhBuilder (exports listed above). Keep the console-sized prebuild answers so title memory layout does not change.
  2. Build host DXR structures: translate the batch records (guest addresses, scratch, updates, postbuild) into desktop inputs; BLAS from the triangle geometry descs, TLAS from the 64-byte instance descs with references remapped to host BLAS addresses. For Rehydrate, either rebuild from the title's vertex data (as the DLL does) or decode triangle nodes from the serialized BLAS.
  3. Keep a map from the title's structure address to the host structure, so the traversal shaders find the right TLAS (the trace programs read the TLAS root at byte 0x30 of the structure through a T#-like record).
  4. Rewrite the 64 trace programs to inline RayQuery:
    • Reproduce the values each guest shader consumes: remap DXR triangle weights to the guest vertex order (see bvh.rs and the serialized barycentric swizzle in five-star docs/raytracing.md), map geometry-local primitive indices and instance identifiers, and verify winding and hit-distance conventions (the console test returns a signed denominator).
    • For alpha-tested triangles, expose non-opaque candidates, evaluate the guest test, and commit only accepted hits (KytyPS5 makes every primitive a non-opaque candidate: gpu_bvh_mirror.comp L462, spirvEmitterRayQuery.cpp L141-L144).
    • Keep the emulated loop behind a valid flag for the first frames and for anything not mirrored (with console-format data kept for it, see above).
  5. Measure against the emulated path on the same scene: GPU time per trace dispatch and the visual difference.

Expected result

  • Lighting in shadowed areas (today they are black because no RT runs).
  • Traversal and intersection on the GPU's ray-tracing hardware where the driver provides it; builds and refits by the driver.
  • No dependence on the console builder programs (and their 64-lane waves) for mirrored structures.
  • Whether this is faster than emulation, and by how much, is the main thing to measure.

Open questions

  • Hit precision may differ from the console's watertight test (driver vs title); edge cases need a tolerance check.
  • Whether all 64 trace programs share a few loop shapes (makes a matcher worthwhile) or need per-program replacement.
  • Dynamic refit frequency and TLAS rebuild cost per frame.

Notes for this fork

In this fork image_bvh*_intersect_ray is emulated and the builder stays off on 32-lane GPUs. Intercepting XboxBvhBuilder would let mirrored structures be built without the builder programs.
Separately, in emit/mod.rs the cross-wave retry now requires VICTORY_WAVE_WIDE=1, but a separate default-on LANE_MASK retry (L432-L446) calls the same helper. It is intended for the barrier-free trace shaders, but its guard does not explicitly exclude wave64 (the helper can admit a split wave64 with a 64-thread group, L483-L493). It may be worth checking which shaders reach the cross-wave lowering there before ruling it out for the device-loss reports.

**@⁠AlexMJHGF421** on GitHub · 2026-11-24 19:57 UTC · [original](https://github.com/fxceless/victory-fork/issues/17) ## Summary The title's ray-traced lighting (probes, bounce, reflections, lamp and sun shadows) is currently either off or planned as an emulation: the title's BVH builder programs run as translated compute and `image_bvh_intersect_ray` is emulated in shader code. That path keeps the console node format, but it does not use the GPU's ray-tracing hardware, some builder pipelines still fail on wave32 hosts, and rounding and edge behaviour of the emulated test are unverified. This issue proposes a different path aimed at hardware-accelerated traversal where available: 1. Intercept the title's calls into `XboxBvhBuilder.dll`, translate the Xbox batch records into desktop DXR build inputs, and build **host DXR** BLAS/TLAS. 2. Replace the title's inline traversal loops (64 compute programs that call `image_bvh_intersect_ray`) with **DXR 1.1 inline `RayQuery`**, keeping the original loop as a per-dispatch fallback. Nothing here is measured yet; it is a design proposal with pointers to existing code in other projects. Its correctness and cost need to be measured against emulation on the same scene. ## Why it is feasible: the title describes its geometry through a DXR-like API [`docs/fork-ray-tracing.md`](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/docs/fork-ray-tracing.md#L30-L34) says mapping onto host DXR "is not practical: the layer would have to know the geometry behind structures the title never describes to it". The title does describe it. five-star-recomp reversed this in detail ([`docs/raytracing.md`](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/docs/raytracing.md#L12-L48), [`docs/d3d12x-shim.md`](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/docs/d3d12x-shim.md#L43-L50)): - The title sets `DisableDXR = 1`, builds every structure through `XboxBvhBuilder.dll`, and traverses inline. - BLAS **and** TLAS go through one export, `BuildRaytracingAccelerationStructureBatched2X(..., D3D12XBOX_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_BATCHED_DESC2*)`. Its 0x50-byte batch records carry `D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS` (Type, Flags, NumDescs, geometry or instance descs) plus guest addresses, scratch slices and postbuild records. - BLAS geometry is `0x38`-byte triangle geometry descs built from the title's mesh records (vertex and index buffers). - Instance descs are a title buffer of 64-byte records, the size of `D3D12_RAYTRACING_INSTANCE_DESC`. - Static BLASes ship serialized in `core_bvh4.rpf` and are restored with `RehydrateRaytracingAccelerationStructureBatched2X`, which rebuilds the structure in place from the game's own vertex data ([`docs/raytracing.md` L74-L108](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/docs/raytracing.md#L74-L108)). - The export signatures are already shimmed in [`BvhBuilderShim.cs`](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/src/FiveStar.Loader/Shims/Bvh/BvhBuilderShim.cs#L34-L59). five-star builds console-format structures on the CPU from them (`src/FiveStar.Shaders/Bvh/XdxrBlas.cs`, `XdxrTlas.cs`). So the host can translate these records into desktop DXR inputs: query host prebuild sizes with `ID3D12Device5`, allocate host scratch and result buffers, remap TLAS instance records to host BLAS addresses, and record builds on `ID3D12GraphicsCommandList4`. For mirrored structures this bypasses the console builder programs, including the ones that need 64-lane waves ([fork-ray-tracing.md "Known gaps"](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/docs/fork-ray-tracing.md#L64-L75)). The emulated fallback still reads console-format nodes, so console-format structures have to be kept where the fallback is used, for example with a compatible CPU builder like five-star's. ## The hard part: traversal is the title's own code The trace programs traverse console-format nodes using a per-thread stack in shared memory and raw-address loads ([fork-ray-tracing.md L14-L28](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/docs/fork-ray-tracing.md#L14-L28)), and read the TLAS root and instance nodes in the layout documented in [five-star `docs/raytracing.md` "Traversal header"](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/docs/raytracing.md#L52-L72). To use opaque host DXR structures, this proposal replaces those walks with inline `RayQuery`. Prior art for exactly this, on PS5 (same RDNA node test): **KytyPS5 `RayQueryTraversal`**. - [`ir/passes/RayQueryTraversal.cpp`](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/shader/recompiler/ir/passes/RayQueryTraversal.cpp#L1150-L1178) matches specific PS5 traversal IR shapes (flat, nested TLAS→BLAS, and [shared-stack walks](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/shader/recompiler/ir/passes/RayQueryTraversal.cpp#L788)) and rewrites the matches to ray-query operations. - Rewritten operations are gated on per-dispatch mirror validity; the IR selects hardware or guest values where needed ([L948-L1030](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/shader/recompiler/ir/passes/RayQueryTraversal.cpp#L948-L1030), validity load in [`spirvEmitterRayQuery.cpp`](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/shader/recompiler/backend/spirv/spirvEmitterRayQuery.cpp#L38-L46)). - A separate GPU mirror builds host structures from guest triangle and instance records, recognises the PS5 `PSR_BVHL` header and validates the mirrored data with GPU-side hashes ([`gpu_bvh_mirror.comp`](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/host_gpu/shaders/gpu_bvh_mirror.comp#L150-L163), [L445-L450](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/host_gpu/shaders/gpu_bvh_mirror.comp#L445-L450); host side starts in [`rayTracing.cpp` `Prepare`](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/host_gpu/renderer/rayTracing.cpp#L394-L431)). - Caveat: the matcher is strict and tied to PS5 SDK code shapes; the design transfers, the matcher does not. For one title, replacing the 64 programs by hash with hand-checked `RayQuery` versions may be simpler than a general matcher. Prior art for console DXR on host DXR on Xbox: **OpenScarlett** (californiaAlmonds). - `CommandListX::BuildRaytracingAccelerationStructure` is forwarded to the host list4 when the host list, tier and descriptors allow it ([`command_list_x.cpp`](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/gpu/d3d12_xs/command_list_x.cpp#L4018-L4030)). - Its `XboxBvhBuilder` shim exposes separate BLAS and TLAS helpers backed by `DxrHardwareEngine` ([BLAS](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/shims/xbox_bvh_builder_shim.cpp#L238-L257), [TLAS](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/shims/xbox_bvh_builder_shim.cpp#L349-L379), [`dxr_hardware.cpp`](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/gpu/dxr_hardware.cpp#L748)). Its export matching this title's `Batched2X` signature is a stub that records no build ([L1281](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/shims/xbox_bvh_builder_shim.cpp#L1281), [L1306-L1317](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/shims/xbox_bvh_builder_shim.cpp#L1306-L1317)). - It selects DXR 1.1 `RayQuery` or a compute BVH4 fallback from host capabilities and shader requirements ([`device_x.cpp`](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/src/gpu/d3d12_xs/device_x.cpp#L3155-L3210)). - It reports standalone DXR tests passing on RTX and Radeon GPUs alongside software fallback tests ([README](https://github.com/californiaAlmonds/OpenScarlett/blob/46d8e2c2f47ce48f84e54d11eeaf7effdbd8ad57/README.md#L168)); the sources do not document an end-to-end test with this title. ## Proposed plan 1. **Shim `XboxBvhBuilder`** (exports listed above). Keep the console-sized prebuild answers so title memory layout does not change. 2. **Build host DXR structures**: translate the batch records (guest addresses, scratch, updates, postbuild) into desktop inputs; BLAS from the triangle geometry descs, TLAS from the 64-byte instance descs with references remapped to host BLAS addresses. For `Rehydrate`, either rebuild from the title's vertex data (as the DLL does) or decode triangle nodes from the serialized BLAS. 3. **Keep a map** from the title's structure address to the host structure, so the traversal shaders find the right TLAS (the trace programs read the TLAS root at byte 0x30 of the structure through a T#-like record). 4. **Rewrite the 64 trace programs** to inline `RayQuery`: - Reproduce the values each guest shader consumes: remap DXR triangle weights to the guest vertex order (see [`bvh.rs`](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/crates/victory-shader/src/bvh.rs#L44-L55) and the serialized barycentric swizzle in [five-star `docs/raytracing.md`](https://github.com/ThatRecompDev/five-star-recomp/blob/2646905de4b4c80d8e297c77c27fa20aee161187/docs/raytracing.md#L97-L99)), map geometry-local primitive indices and instance identifiers, and verify winding and hit-distance conventions (the console test returns a signed denominator). - For alpha-tested triangles, expose non-opaque candidates, evaluate the guest test, and commit only accepted hits (KytyPS5 makes every primitive a non-opaque candidate: [`gpu_bvh_mirror.comp` L462](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/host_gpu/shaders/gpu_bvh_mirror.comp#L462), [`spirvEmitterRayQuery.cpp` L141-L144](https://github.com/KytyPS5/KytyPS5/blob/30781dcda029cbd669aa81337ac2902c9c3015d8/src/graphics/shader/recompiler/backend/spirv/spirvEmitterRayQuery.cpp#L141-L144)). - Keep the emulated loop behind a `valid` flag for the first frames and for anything not mirrored (with console-format data kept for it, see above). 5. **Measure** against the emulated path on the same scene: GPU time per trace dispatch and the visual difference. ## Expected result - Lighting in shadowed areas (today they are black because no RT runs). - Traversal and intersection on the GPU's ray-tracing hardware where the driver provides it; builds and refits by the driver. - No dependence on the console builder programs (and their 64-lane waves) for mirrored structures. - Whether this is faster than emulation, and by how much, is the main thing to measure. ## Open questions - Hit precision may differ from the console's watertight test (driver vs title); edge cases need a tolerance check. - Whether all 64 trace programs share a few loop shapes (makes a matcher worthwhile) or need per-program replacement. - Dynamic refit frequency and TLAS rebuild cost per frame. ## Notes for this fork In this fork `image_bvh*_intersect_ray` is emulated and the builder stays off on 32-lane GPUs. Intercepting `XboxBvhBuilder` would let mirrored structures be built without the builder programs. Separately, in [`emit/mod.rs`](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/crates/victory-shader/src/emit/mod.rs#L344-L349) the cross-wave retry now requires `VICTORY_WAVE_WIDE=1`, but a separate default-on `LANE_MASK` retry ([L432-L446](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/crates/victory-shader/src/emit/mod.rs#L432-L446)) calls the same helper. It is intended for the barrier-free trace shaders, but its guard does not explicitly exclude wave64 (the helper can admit a split wave64 with a 64-thread group, [L483-L493](https://github.com/fxceless/victory-gta6/blob/e9765a73a68a75536b1c10ad5ef746d7f6ec0bcc/crates/victory-shader/src/emit/mod.rs#L483-L493)). It may be worth checking which shaders reach the cross-wave lowering there before ruling it out for the device-loss reports.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:34:40 +00:00
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/fxceless--victory-fork#17
No description provided.