graphics: cache the memory at DMA base addresses before the shader runs #925
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!925
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/925/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?
@TheCruZ on GitHub · 2026-09-29 21:11 UTC · original
pr/dma-base-cache→main· +208 −1 · 7 files · 1 commits · closed without mergingSummary
Fixes missing characters in the boot legal notice of Grand Theft Auto V (PPSA04263). Only scattered letters of the text are drawn.
Problem
The text pixel shader (
ps e26a052be0fc3e49) samples a 2048x4096 R8 glyph atlas. A compute shader (cs e9e4f0b919d80844) fills that atlas one glyph per dispatch, copying the bitmap the CPU rasterized into a staging ring withFLAT_LOAD_UBYTEfroms[12:13]+ lane offset.DMA only reaches memory that already has a cached buffer. Any other access records a fault and reads zero, and the buffer is created only after the dispatch has run.
So the first glyphs copied from each 16 KiB staging page were stored blank, and since each glyph is uploaded once, they stayed blank.
Change
This change makes resource tracking record the user-data register pairs that DMA addresses are offset from (the low dword derives from register N and the high dword from N + 1; only value operands are followed, not lane masks or loaded data).
Before a DMA draw or dispatch, the host caches the mapped memory in the 16 KiB (
BufferCache::CACHING_PAGESIZE) starting at each such base, so the shader reads the guest's bytes on its first access. A false positive only costs aFindBufferon mapped memory.Testing
resource_tracking_testspasses, including the newTestDmaBaseRegisterscase (checks that the registers of the lane offset are not reported as a base).AI usage
AI (Claude Opus 5.5) was used to investigate the issue (shader dumps and resource-tracking analysis) and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my GTA V fork, i will be bringing PRs here after test them enough.
@coderabbitai[bot] on GitHub · 2026-09-29 21:15 UTC · original · edited 2026-10-05 23:02 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
2825a8be-53b1-4c1c-8b85-b0d09a49ece8📥 Commits
Reviewing files that changed from the base of the PR and between 0e5cc69b4700a037b777b2ce4177f6a124aec4dc and bcccd431ba6df20d27bb794be924b742139a1f7d.
📒 Files selected for processing (6)
src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough
Resource tracking traces address operands and records DMA base register pairs in
ShaderInfo. Graphics binding preparation and direct and indirect compute dispatch use this metadata to cache mapped address ranges. Tests check the recorded registers for FLAT and GLOBAL address operations, including composite addresses and a 256-instruction traversal boundary.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
bcccdThis change fixes missing text in the GTA V legal notice by preparing memory that compute shaders read through DMA. One earlier concern remains unresolved: an unusual base address near the top of the address space could still stop rendering. Confirm that case before merging.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
bcccdThe change prepares mapped guest memory earlier and retains existing address checks. No introduced security vulnerability was established, but memory-boundary alignment and concurrent invalidation remain incompletely verified.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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.@ -126,0 +141,4 @@// DMA reaches only memory with a cached buffer; any other access records a fault and// reads zero, and the buffer arrives after the shader has run. That loses data the guest// writes for a single dispatch, such as the glyph bitmaps GTA V copies into its font atlas.m_mapped_ranges.ForEachInRange(base, BufferCache::CACHING_PAGESIZE,@coderabbitai[bot] on GitHub · 2026-09-29 21:15 UTC · original · edited 2026-10-08 20:53 UTC
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4753
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41493
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42504
Reject overflowing DMA base ranges before querying mappings.
RangeSet::ForEachInRangecomputes its endpoint before it checks mapped ranges. A base nearUINT64_MAXcan therefore callRangeSet::Endand terminate the draw or dispatch.CacheDmaBasesprocesses every statically collected DMA base without an access-mask check. If an inactive access leaves stale user-data words, those words can form an overflowing base.Validate the endpoint before calling
ForEachInRange. Clamp the interval and skip a zero-length result.Suggested fix
✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-09-29 21:15 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
48ceff8b-d509-4aed-9ee9-9abebb4bb9a9📥 Commits
Reviewing files that changed from the base of the PR and between
05057c9441and 0873b6ce4c52f86535df354461060c6b886b2c09.📒 Files selected for processing (7)
src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -126,0 +141,4 @@// DMA reaches only memory with a cached buffer; any other access records a fault and// reads zero, and the buffer arrives after the shader has run. That loses data the guest// writes for a single dispatch, such as the glyph bitmaps GTA V copies into its font atlas.m_mapped_ranges.ForEachInRange(base, BufferCache::CACHING_PAGESIZE,@TheCruZ on GitHub · 2026-09-29 21:37 UTC · original
Well thats right, teorically possible.
@Nmzik on GitHub · 2026-09-30 09:31 UTC · original
@TheCruZ that’s the first good and clean fix. Thanks. All previous attempts/PRs from other guys were basically hacks 👍
Looks very legit, I’ll take a look
@coderabbitai[bot] on GitHub · 2026-10-01 23:46 UTC · original · edited 2026-10-02 07:27 UTC
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 85 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Apply the visit limit to processed instructions, not queued instructions.
When adding an operand makes
visited.size()equal 256, the next iteration stops before processing that operand or the remaining queue. A low-dword graph with exactly 256 instructions can therefore omit its terminalGetUserData, even though the graph fits the stated limit.CollectDmaBasethen misses the base pair.Limit processing with
next < MaxVisited. Bound operand insertion separately if the queue must also remain capped. Add a boundary case with the base source at the final permitted instruction.✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-10-01 23:46 UTC · original · edited 2026-10-02 07:26 UTC
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 100 (outdated):🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Preserve component provenance when tracing composite extracts.
For an address whose dwords extract components 0 and 1 from
CompositeConstructU32x4(GetUserData(s12), GetUserData(s13), 0, 0), this walk follows every component of the composite. Both source sets become{12, 13}.CollectDmaBasethen rejects register 12 because register 13 appears in the low sources. The renderer does not cache this valid base, so an uncached DMA read still returns zero.For a constant-index extract from a composite constructor, follow only the selected component. Add this address shape to
TestDmaBaseRegisters.✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-10-01 23:46 UTC · original · reviewed
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
e4fd6247-e682-434c-8d64-5b2346e04f1d📥 Commits
Reviewing files that changed from the base of the PR and between 353653947b92880b1071de54954e1e948e9bacef and 1606bf8f91f20f8b0a406275ea9102f4fde3a962.
📒 Files selected for processing (6)
src/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@TheCruZ on GitHub · 2026-10-02 07:19 UTC · original
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 85 (outdated):Fixed in 0e5cc69: the walk now bounds the instructions it queues (and therefore processes) instead of stopping once the queue reaches the limit, so a graph of exactly 256 instructions is fully processed.
TestDmaBaseRegisterscovers a base register that is the 256th instruction of its chain (it fails with the previous loop).@TheCruZ on GitHub · 2026-10-02 07:19 UTC · original
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 100 (outdated):Fixed in 0e5cc69: a constant-index
CompositeExtractU32x2/3/4of aCompositeConstructU32x2/3/4now follows only the selected operand, so the dwords of a pair built withCompositeConstructkeep their own sources. Added the suggested shape (CompositeConstructU32x4(s12, s13, 0, 0)with extracts 0 and 1) toTestDmaBaseRegisters; it records s12 and failed before the change.@coderabbitai[bot] on GitHub · 2026-10-02 07:26 UTC · original
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 100 (outdated):@TheCruZ, thanks for the fix. The supplied code preserves separate low- and high-dword sources for the reported composite address. The regression case you added covers the original failure.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-02 07:27 UTC · original
On
src/graphics/shader/recompiler/ir/passes/ResourceTracking.cppline 85 (outdated):@TheCruZ, thanks for fixing this. The updated loop processes all queued instructions, including the 256th instruction. The boundary case inTestDmaBaseRegisterscovers the reported failure.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@TheCruZ on GitHub · 2026-10-02 09:37 UTC · original
@Nmzik i have rebased the change to fix conflicts, about the pull requests i see that you have a lot of work to review and i have doubts of how to push all my changes from my fork I don't want to bury you in work, but with my custom branch, many of the changes are substantial yet yield major performance improvements in GTA V hitting 40 fps in densely populated areas where the original emulator barely manages 12 apart of crash fixes and basic ray tracing support.
Should I just send everything over and wait for you to review it at some point, or should I hold off until the first three commits are ready or would you like to take a look at it separately? they are around 30 PRs
Maybe alternatively you can check them on my fork and take ideas/implement the ones you want etc and just give me some credits. I squash every fix/change to a single commit to have them easy to bisect later.
@Nmzik on GitHub · 2026-10-02 13:34 UTC · original · edited 2026-10-02 13:35 UTC
@TheCruZ I'll take a look at your repo, thanks ofc credit you
@TheCruZ on GitHub · 2026-10-02 14:26 UTC · original
@Nmzik about fidelity its working and it has ray casting using regular shader cores, not rt cores but the speed is around 7fps, not caused by ray casting but by some other existing bottleneck, i will look fidelity mode performance soon, for now i just focused in the performance mode to get those 30-40 fps to make the game playable
@overlordxrz-source on GitHub · 2026-10-05 22:35 UTC · original
Validated this locally (read the diff, rebased, built, tested):
main(cdb64bf): one conflict inPrepareGraphicsBindings(pipeline/descriptors.cpp).mainrefactoredFindBuffersto batch-span mode, so the per-stageFindBuffers(*stage)from this PR no longer applies; resolution keeps the batch call and adds the per-stageCacheDmaBasesloop:Happy to push the rebased branch somewhere if useful.
kyty_emulator(x86_64 macOS no-Qt Release) links clean.resource_tracking,shader_cfg,memory_trackerall pass. GPU-harness tests not runnable here (no Vulkan loader — same pre-existing env failure as clean main).On the fix itself: caching DMA bases before the shader runs (both graphics in
PrepareGraphicsBindingsand compute inDispatchDirect/DispatchIndirect) instead of faulting mid-dispatch and reading zero is clearly the right ordering — the GTA V glyph-atlas symptom (first glyphs of each 16KB staging page blank) matches a too-late fault-created buffer exactly. Small, targeted, and the newResourceTrackingcoverage comes with it. Looks good to me once rebased.@ -126,0 +141,4 @@// DMA reaches only memory with a cached buffer; any other access records a fault and// reads zero, and the buffer arrives after the shader has run. That loses data the guest// writes for a single dispatch, such as the glyph bitmaps GTA V copies into its font atlas.m_mapped_ranges.ForEachInRange(base, BufferCache::CACHING_PAGESIZE,@TheCruZ on GitHub · 2026-10-08 20:53 UTC · original
Fixed in
19cc54d:CacheDmaBasesskips a base whose caching-page window would pass the end of the address space (base > UINT64_MAX - CACHING_PAGESIZE) before callingForEachInRange. Mapped memory never reaches the top of the address space, so nothing valid is lost.@ -126,0 +141,4 @@// DMA reaches only memory with a cached buffer; any other access records a fault and// reads zero, and the buffer arrives after the shader has run. That loses data the guest// writes for a single dispatch, such as the glyph bitmaps GTA V copies into its font atlas.m_mapped_ranges.ForEachInRange(base, BufferCache::CACHING_PAGESIZE,@coderabbitai[bot] on GitHub · 2026-10-08 20:53 UTC · original
@TheCruZ, thanks for the fix and clarification. The guard prevents an overflowing window from reachingForEachInRange. Given the mapped-memory limit, skipping that window does not lose valid memory.✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@Nawid3333 on GitHub · 2026-10-19 15:43 UTC · original
@TheCruZ I saw you closed this today. Do you plan to send it again in another form (maybe with more of your fork's changes), or would you be fine with someone rebasing it onto current main with you as the author?
I checked current main (
0cad4672): it still has no equivalent. Nothing reads the DMA base user-data registers before a draw or dispatch, andPrepareBda->SynchronizeBdaonly re-syncs buffers that already exist in the pending ranges. If you resubmit, I can help with tests.Written with Claude Code (AI): it compared your commit with main; I checked the code before posting.
@TheCruZ on GitHub · 2026-10-19 16:46 UTC · original
yes i know, i had too many PRs open and was hard to keep all them up without being merged, i ask @Nmzik to check my fork when he had time and he can merge the changes from there when he want with the credits
in my fork i keep them clean to be merged without conflicts
Pull request closed