shader: implement software BVH ray intersection for MIMG 0xe6 #640
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!640
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/640/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@carbonimax on GitHub · 2026-09-16 15:34 UTC · original
carbonimax:fix/mimg-bvh-ray→main· +1098 −72 · 29 files · 13 commits · closed without mergingSummary
Implement software BVH ray intersection for lowerable MIMG forms behind an explicit opt-in. Default behavior remains the current main behavior: skip these compute dispatches so a game can boot before a non-RT mode is selected.
Enable with
--software-bvh true, or the launcher checkbox (unchecked by default). Supported lowering covers the software RTIP 1.1 intersection path with 32-bit node pointers and triangle return mode 1. BVH64, A16 and other non-lowerable encodings retain the skipped-dispatch path. Runtime descriptor specialization checks remain in place.Port and current main
Adapted from Kowalski4-ko's contributed port, with attribution in the merge commit. Updated onto main
44798085without force-pushing the PR branch. The current main CFG structurizer and SRT/resource-tracking implementation are preserved. New dynamic scalar-buffer lowering is scoped to enabled BVH shaders; unrelated shaders keep the existing main path.Validation of this update
Independent macOS x86_64 Release build of the emulator, shader CFG, scalar provenance, resource tracking/materialization and compute-test harness succeeds. Host suites
shader_cfg_tests,scalar_provenance_tests,resource_tracking_tests,resource_materialization_testsand--scalar-memory-host-onlypass. The restored scalar host selector retains the existing alignment/48-bit address-mask regression check.No new GPU test or game run was performed during this update. These host/build results do not qualify the new head's GPU behavior or Astro's final image.
Historical GPU evidence and remaining limits
--bvh-onlyon5233c5a(historical PR head).2a4c47freported--bvh-onlypassing; that is evidence for that port, not this adapted head.Skipping BVH dispatches is not equivalent to implementing their result. Enabling the software path is experimental and does not establish exact game rendering. CI for the new head must be evaluated separately from previous successful CI.
@coderabbitai[bot] on GitHub · 2026-09-16 15:34 UTC · original · edited 2026-10-01 17:50 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
f5b72829-bc5a-4dfc-9888-77562e898e24📥 Commits
Reviewing files that changed from the base of the PR and between
f92132b489and5233c5a9b2.📒 Files selected for processing (2)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterMemory.cpptests/ShaderRecompilerComputeTests.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough
The recompiler adds RDNA2 BVH ray-intersection decoding and translation, BVH descriptor tracking, dynamic scalar-buffer lowering, updated loop-exit handling, address masking, and regression tests.
Changes
LoadAddressU32operations and runs dead-code elimination when lowering changes the IR.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Feature · Severity of issue fixed: Medium
Merge Risk: ⚪ Minimal · up to
5233cNo concrete current-head defect remains supported by the reviewed evidence. The pending native retest is validation follow-up rather than an established merge-blocking failure.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 6.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 90 functions across 19 files. (1 skipped: 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-09-16 15:45 UTC · original · edited 2026-10-01 17:50 UTC
On
tests/shaderCfgTests.cppline 4210 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18461
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 50371
Mask 32-bit shift counts in the evaluator.
TestBvhIntersectionsreachesShiftRightLogical32for box nodes such as0x204,0x205, and0x206.RayTracing.cppcomputes counts of 32 or more for the remap operation. The evaluator then shifts auint32_tby that count, which is undefined behavior in C++. The IR’s constant evaluator masks 32-bit shift counts with& 31, so use the same semantics here. Apply the mask to both 32-bit shift cases.🛡️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
@coderabbitai[bot] on GitHub · 2026-09-16 15:45 UTC · original · reviewed
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Advanced
Run ID:
30642619-5ea3-4ce2-835c-7770115af24e📥 Commits
Reviewing files that changed from the base of the PR and between
5b541375a3andc5e73f2427.📒 Files selected for processing (21)
src/graphics/shader/recompiler/ShaderRecompiler.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.hsrc/graphics/shader/recompiler/frontend/cfg/ShaderCFG.cppsrc/graphics/shader/recompiler/frontend/decode/ImageOps.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.hsrc/graphics/shader/recompiler/frontend/translate/Memory.cppsrc/graphics/shader/recompiler/frontend/translate/RayTracing.cppsrc/graphics/shader/recompiler/frontend/translate/Translator.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/passes/DynamicBuffer.cppsrc/graphics/shader/recompiler/ir/passes/DynamicBuffer.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.htests/ScalarProvenanceTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@francoisatt on GitHub · 2026-09-16 19:31 UTC · original
Ran this on Windows with a native Vulkan driver (RTX 5080, clang-cl,Ninja) — the part of the harness your MoltenVK setup can't reach.
Good news first: --bvh-only passes. MimgBvhIntersectRayTriangle runs tocompletion on the GPU, so the synthetic triangle test does execute andsucceed on real hardware.
The full suite is a different story. shader_recompiler_compute exits0xc0000409 on: ScalarMemoryLoadVariants failed at dispatch: BDA test has no explicit guest mapping
On clean origin/main (
63fb822) the same test passes — I rebuilt and re-ran to check. Caveat: your branch is based on18a1e0f, and main has since touched SrtWalker.cpp, ResourceTracking.cpp and added ~2100 lines to ShaderRecompilerComputeTests.cpp, so I can't say whether it passed on your base. What I can say is that it passes on main today and crashes on this branch.Given the change keeps loop-dependent scalar buffer reads on the GPU as BDA reads, the message reads like the test's guest mapping isn't set up for that path. Might be the test needing an explicit mapping rather than a bug in the lowering.
Everything else: 35/39 ctest, the other three failures are the known SDL link problem (kernel_file_system, irtual_memory_allocation, guest_red_zone_patcher — see #565, present on clean main too).
Happy to re-run once it's rebased on current main.
@carbonimax on GitHub · 2026-09-16 21:35 UTC · original
Thanks for testing this on native Vulkan and confirming that
--bvh-onlypasses on the RTX 5080.Updated in
a588023: merged currentmain(63fb822) into this branch and fixed the missing guest-memory mappings in three compute tests:ScalarMemoryLoadVariantsScalarLoadSignedImmediateOffsetAddsSoffsetScalarLoadAlignsComponentsAndMasksAddressThis PR keeps data-only scalar address loads on the GPU instead of collecting them as CPU SRT reads. These fixtures therefore need an explicit BDA mapping. Each now maps guest address 0 to the start of its initial buffer; the shader instructions and expected results are unchanged. A host-side audit of all
MakeCases()shaders found these three missing mappings; the other BDA cases already declared theirs.Also fixed the undefined 32-bit shifts in the BVH test evaluator reported by CodeRabbit, masking the shift count with
& 31to match the IR semantics.Local validation: x86_64 Release build passed;
shader_cfg,scalar_provenance,resource_tracking, andresource_materializationpassed. The new portable--scalar-memory-host-onlyselector passes for all three fixtures, including SPIR-V validation and mapping checks. GPU execution remains unverified here: the MoltenVK harness stops before dispatch because image gather / compute derivative quad support is unavailable.For a native Windows retest, the new selector isolates the three affected cases:
Could you rerun these on the updated branch? The host checks do not establish that the full GPU suite now passes.
@francoisatt on GitHub · 2026-09-17 19:26 UTC · original
Re-ran on
3cb78d5(Windows, native Vulkan, RTX 5080, full clean build).Two of the three fixtures are fixed: ScalarMemoryLoadVariants and ScalarLoadSignedImmediateOffsetAddsSoffset both pass on GPU now.
--bvh-only still passes.
The third one now fails differently — no longer a crash, a wrong result:
The first dword comes back as one byte of word 0 followed by three bytes of word 1, so the read looks byte-offset by 3 rather than snapped to the dword boundary. Given the fixture's name, the address mask doesn't seem to be applied on the BDA path.
Worth knowing: --scalar-memory-host-only reports all three ok, including this one. So the host-side selector doesn't catch it — it's a host-vs-GPU divergence, which is exactly the gap your MoltenVK harness can't reach.
Rest of the suite unchanged: 35/39, the other three failures are the known SDL link issue (#565), present on clean main too.
@coderabbitai[bot] on GitHub · 2026-09-17 20:02 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
4d8c16c0-2c24-45cf-86d8-58b9feacaf00📥 Commits
Reviewing files that changed from the base of the PR and between
3cb78d5398andf92132b489.📒 Files selected for processing (21)
src/graphics/shader/recompiler/ShaderRecompiler.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.hsrc/graphics/shader/recompiler/frontend/cfg/ShaderCFG.cppsrc/graphics/shader/recompiler/frontend/decode/ImageOps.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.cppsrc/graphics/shader/recompiler/frontend/decode/ShaderDecoder.hsrc/graphics/shader/recompiler/frontend/translate/Memory.cppsrc/graphics/shader/recompiler/frontend/translate/RayTracing.cppsrc/graphics/shader/recompiler/frontend/translate/Translator.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/passes/DynamicBuffer.cppsrc/graphics/shader/recompiler/ir/passes/DynamicBuffer.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.htests/ScalarProvenanceTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -26585,2 +26591,4 @@{0x11111111u, 0x22222222u},{O::S_MOV_B32, O::S_LOAD_DWORD, O::BUFFER_STORE_DWORD, O::S_ENDPGM}};test.bda_mappings = {{0, 0}};return test;@coderabbitai[bot] on GitHub · 2026-09-17 20:02 UTC · original · edited 2026-09-17 20:40 UTC
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 50372
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 50372
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 30474
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 50372
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 19904
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 15077
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41753
Apply the 48-bit mask in the scalar BDA address path.
S_LOAD_DWORDforms its raw base from low3and high0xffff0000. The host scalar path masks the combined address with0x0000ffffffffffffand aligns it to a dword, producing guest address0.GuestAddressonly applies the dword-alignment mask to the low word beforeDeviceAddressFromWords, so the BDA path retains the upper bits and misses thetest.bda_mappings = {{0, 0}}mapping.Because
ScalarLoadAlignsComponentsAndMasksAddressis registered inMakeCases(), the GPU workflow can read the wrong value and fail atRunCase'sCompareWordscheck. Mask the combined raw scalar address in the BDA lowering path. Keep the expected value{0x11111111u, 0x22222222u}unchanged.✅ Addressed in commit
5233c5a@carbonimax on GitHub · 2026-09-17 20:39 UTC · original
Thanks for the GPU retest. Fixed in
5233c5a9b2: the scalar BDA path aligned the offsets but left the guest base unaligned and its upper address bits unmasked. It now aligns the base to four bytes and masks it to 48 bits before adding the aligned offsets. Added a SPIR-V dataflow regression check for ScalarLoadAlignsComponentsAndMasksAddress; removing either the base alignment or the upper-bit mask makes it fail. Build, shader_cfg_tests, and --scalar-memory-host-only pass locally. I could not validate the native GPU result here. Could you rerun ScalarLoadAlignsComponentsAndMasksAddress (expected [0x11111111, 0x22222222]), the other two scalar-load fixtures, and --bvh-only on Windows/native Vulkan with your RTX 5080?@francoisatt on GitHub · 2026-09-18 17:10 UTC · original
Re-ran on
5233c5a(Windows, native Vulkan, RTX 5080, full clean build).All three scalar-load fixtures now pass on GPU, including ScalarLoadAlignsComponentsAndMasksAddress (the byte-offset readback is gone).
--scalar-memory-host-only: 4/4 ok
--bvh-only: MimgBvhIntersectRayTriangle ok
Full suite: 36/39. The three remaining failures are the known SDL link issue (#565): kernel_file_system, virtual_memory_allocation, guest_red_zone_patcher, all "Not Run", same as on clean main.
From my side the GPU path is clean now.
@theantipopau on GitHub · 2026-09-22 21:06 UTC · original
Tested #640 with the real game on Windows + AMD, since the description says GPU execution wasn't verified and the GPU runs reported so far were on NVIDIA.
Setup: tested PR head
5233c5amerged onto upstreammainf100f78(clean merge, builds). I checked the current head (b584ee5, at the time of writing) and it's the same5233c5aplus just a rebase onto newermain— the BVH-related source files (RayTracing.cpp, DynamicBuffer.cpp, ImageOps.cpp, ShaderCFG.cpp) are byte-identical, so this result should still hold. Windows 11, Ryzen 7 9800X3D, Radeon RX 9070 XT, AMD driver 26.8.1 (LLPC), Vulkan 1.4.349. Astro Bot PPSA21564, no patches.Before (plain main): exits after ~12 s with
unsupported family=MIMG opcode=0xe6(raw=[0xf1989f07 0x00040505 0x4442413d 0x4543403e 0x00004746]), same as #281.With #640: the decode error is gone and the BVH compute shaders now translate, but the game never gets past frame ~125-163 (title bar: fps 0) and one thread stays pinned at 100% CPU for 15+ minutes. Sampling that thread with the build's PDB shows it is inside the AMD driver, not the emulator:
With
--graphics-debug-dump true, the dump timestamps show the driver taking longer on each BVH kernel:spirv-val --target-env vulkan1.3passes on the dumped kernels, so this doesn't look like invalid SPIR-V. Structurally each has a singleOpLoopMerge+OpSwitch(the dispatcher fallback from the CFG change), ~1050 function-scopeOpVariables, ~710OpPhis and ~230OpFunctionCalls (~35k disassembled lines). I have not tested whether the structured (non-dispatcher) path avoids this, or whether NVIDIA behaves the same. I'm not attaching the dumps because they're derived from a commercial game's shaders; the hashes above are for cross-reference.Happy to try a variant (e.g. avoiding the dispatcher fallback for these kernels) if you want another AMD data point.
AI disclosure: I used Claude Code to help build, run the game, sample the stack and analyze the SPIR-V. I ran and checked everything on my own machine.
@Kowalski4-ko on GitHub · 2026-09-27 21:53 UTC · original
Hey @carbonimax, this doesn't merge with main anymore so I rebased it for my own testing, maybe it saves you some time: https://github.com/Kowalski4-ko/KytyPS5/tree/gta6-prep
I cherry-picked
c5e73f2plus the actual fixes froma588023and5233c5ainstead of the merge commits. Those reformatted a bunch of unrelated files, which is where most of the conflicts came from. Then adapted it to the void translators and the SrtWalker class, the BVH descriptor check now lives in MaterializeResources.One behavior thing: main now skips dispatches with 0xe6/0xe7 so games can boot before you pick a non-RT mode. I left that as the default and put the lowering behind
--software-bvh true(plus a launcher checkbox), mostly because of the multi-minute AMD compiles theantipopau hit. Whatever IsLowerableBvh() rejects (BVH64, A16) still gets skipped.Also had to loosen TestNewShaderRecompilerCfgLoopExitSharedWithSelection a bit, Structurize still fails there but with a different message on current main.
On a 4070 SUPER
--bvh-onlypasses and ctest is 43/44 (kernel_file_system is the SDL_main link issue, same on main). No RT game here, so I can't say anything about compile times on NVIDIA, sorry.Grab the commit or ignore it, whatever is easier for you.
@carbonimax on GitHub · 2026-10-01 17:46 UTC · original
Thanks for the port, and thanks for the detailed AMD report. I am adapting 2a4c47f onto current main
44798085, keeping software BVH explicitly opt-in (--software-bvh true, launcher checkbox off by default). The current main CFG structurizer and resource-tracking/SRT implementation are preserved; the older port needed another adaptation to those changes. Unsupported BVH64/A16 forms still take the existing skipped-dispatch path.The reported AMD compile times remain an unresolved limitation: I have not rerun the game on AMD or measured a reduction. The default therefore keeps main behavior. The RTX 5080 result on
5233c5aand the 4070 SUPER result on the contributed port are historical GPU evidence, not GPU validation of this new head. Local checks for this update will be build and host-only tests; no new GPU run is being claimed.@carbonimax on GitHub · 2026-10-01 17:50 UTC · original
On
tests/shaderCfgTests.cppline 4210 (outdated):The evaluator now masks both 32-bit shift counts with & 31u, matching the IR semantics and avoiding undefined C++ shifts. The current-main adaptation preserves the same masking in PossibleU32Bits. The complete shader_cfg_tests suite passes; no new GPU result is claimed.
@coderabbitai[bot] on GitHub · 2026-10-01 17:51 UTC · original
On
tests/shaderCfgTests.cppline 4210 (outdated):Oops, something went wrong! Please try again later. 🐰 💔
@Nmzik on GitHub · 2026-10-03 15:33 UTC · original
software BVH ray intersection is now in main. Feel free to open Pr if we are missing something
Pull request closed