shader: support compute stores through V#s read from a descriptor table #931
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!931
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/931/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 22:07 UTC · original
pr/vsharp-table-stores→main· +1304 −69 · 22 files · 1 commits · closed without mergingSummary
Fixes the emulator exiting with
buffer descriptor is not a valid runtime valuewhen Grand Theft Auto V (PPSA04263) enters Story mode in Fidelity mode (ray tracing). The failing shaders are skinning compute shaders that only run in that mode and feed the ray-tracing BVH, Performance mode never dispatches them.Problem
The skinning compute shaders pick their output buffer per wave: a waterfall loop reads a key with
V_READFIRSTLANE, loads the store V# withS_BUFFER_LOAD_DWORDX4from a table of V#s atkey * 16, and stores through it.Resource tracking only accepted GPU-selected V#s for raw DWORD loads, so the store V# was not a valid runtime value and the dispatch failed.
Change
Shader. Resource tracking recognizes a store V# whose DWORDs are consecutive reads of one bound table at
(key << 4) + offset, in compute stages only, other untrackable descriptors still fail with a clear message. The SPIR-V store resolves the V# per lane with the same address and bounds rules as the existing indirect loads and writes through BDA.Every target is cached before the dispatch. BDA only reaches memory that a cached buffer holds, so a store to any other memory would be dropped. In GTA V the table is the game's 2 MiB descriptor heap (131072 entries, about 40000 buffer V#s over 600 MiB), prepared about 150 times a second.
IndirectWriteTableskeeps a snapshot of each table and only rescans the pages the CPU wrote since the previous preparation (MemoryTracker::ConsumeCpuWrites, fed by a per-page bit set when a page becomes CPU dirty), so an unchanged table costs a few microseconds. Targets sharing a caching page become one buffer, targets released by the garbage collector or mapped later are cached again. T#s, S#s, V#s without a data format (the shader drops those stores) and unmapped targets are skipped, and a table the unchanged table costs a few microseconds. Targets sharing a caching page become one buffer, targets released by the garbage collector or mapped later are cached again. T#s, S#s, V#s without a data format (the shader drops those stores) and unmapped targets are skipped, and a table the GPU wrote is downloaded first.The GPU reports what it wrote. Each store sets the bit of its caching page in a write bitmap in the second half of the fault buffer. After the submission completes, the bitmap is read back like the faults: the written bytes of the targets become GPU modified and images over them are invalidated as for any other buffer write, while pages the CPU dirtied meanwhile keep the CPU's bytes. Nothing is marked written before the dispatch, so buffers the CPU keeps writing are not read-protected. The fault-buffer parser now also skips empty words and keeps pages it has no room for until its next pass instead of dropping them.
Measured in GTA V (Fidelity): uncovered target pages ~21000 → 0, BDA accesses to uncached memory 463 → 0 per run, preparation cost per dispatch ~520 µs (first capped version) → 3-7 µs. The first preparation caches about 2000 target groups (~330 MiB) once, in ~150 ms.
Relation to #690
#690 targets the same failure (see #507, #548, #810) with a more general approach: any V# derived from memory becomes a dynamic buffer resolved per lane in the shader. This PR differs in that the host caches every target before the dispatch (so no store is dropped) and learns from the GPU which bytes were written (so later CPU reads and uploads stay coherent), and it adds no GPU waits, whereas #690 adds
FlushAndWait()after every direct compute dispatch. #690 covers more access kinds (sub-DWORD, add_tid, swizzle, graphics stages).Testing
resource_tracking_tests: table-selected store V#s are tracked as indirect stores, graphics stages still reject them.shader_recompiler_compute_tests:BufferStoresGpuSelectedDescriptors(--indirect-buffer-only) runs stores through GPU-selected V#s on the GPU and checks the write bitmap,IndirectWriteTables(--indirect-write-tables-only) covers target coverage, incremental rescans, GC and late-mapped targets and the write marking. All 479 cases pass.memory_tracker_tests: CPU-write consumption and GPU marking that skips CPU-dirty pages.AI usage
AI (Claude Opus 5.5) was used to investigate the issue 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 22:15 UTC · original · edited 2026-10-08 21:14 UTC
📝 Walkthrough
Walkthrough
The changes add compute-stage support for GPU-selected indirect DWORD stores. Resource tracking identifies V# descriptor tables and links them to bound buffers. The SPIR-V emitter writes active indirect-buffer components and records written-page bits. The renderer tracks CPU and GPU page ownership, processes fault and write bitmaps, and prepares indirect-write table targets during compute dispatch. Tests cover resource tracking, buffer stores, write-bit reporting, and table tracking.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
4b326The change allows compute shaders to write through descriptor-table-selected buffers. Several open issues can still leave written data or textures stale. One is a table read failure that is never retried. Another is writes to image aliases. A third is preparation limits that skip valid targets. These issues can cause rendering corruption in affected games, so they should be resolved or explicitly accepted before merging.
Security Architecture Review
Security architecture risk: 🟡 Moderate · up to
c87faThe new write capability can leave modified bytes incompletely tracked or allow later reads to use stale data. The demonstrated impact concerns emulated workload data; broader host-memory exposure has not been established. Data ownership and recovery need attention.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❓ 1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 13.25% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 83 functions across 21 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-29 22:15 UTC · original · edited 2026-09-30 08:10 UTC
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 1858 (outdated):🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Track actual writes to GPU-modified image aliases.
If the shader selects a V# that aliases a GPU-modified image, this branch skips the only texture-cache notification for that target. The buffer store can then change the backing buffer while
RefreshImagestill treats the native image as current. A later texture read can return the old image contents. Preserve the image’s existing GPU writes before dispatch, but mark an image stale when the selected target is written.✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-09-29 22:15 UTC · original · edited 2026-09-30 08:11 UTC
On
src/graphics/host_gpu/renderer/renderCompute.cppline 58 (outdated):🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Prepare every target the shader can select.
The host scans at most 4096 table entries and rejects targets larger than 256 MiB. The emitted store has no corresponding limits. If a shader selects a later entry or writes within a larger valid target, the host does not prepare its BDA range, so the store can be dropped. Scan large tables in bounded chunks and prepare valid target ranges without excluding shader-reachable addresses.
✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-09-29 22:15 UTC · original · edited 2026-09-30 08:10 UTC
On
src/graphics/host_gpu/renderer/renderCompute.cppline 93 (outdated):🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42126
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42022
Do not fall back to unsynchronized backing for a GPU-dirty table.
When
TryReadGpuCleanBackingrejects a GPU-dirty table, the code immediately callsTryReadBacking, which does not perform cache readback. It can read stale descriptors.PrepareIndirectWriteTargetscan then omit a target selected by the shader, so the target is not passed toObtainBufferorInvalidateMemoryFromGPU. Synchronize the table before scanning it when GPU-dirty state causes the clean read to fail.✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-09-29 22:15 UTC · original · reviewed
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
921d79c7-beca-431c-addd-3680bd114a66📥 Commits
Reviewing files that changed from the base of the PR and between
05057c9441and 0a4e916ec65379fb4e72bb952b16d9aebadfc7b5.📒 Files selected for processing (10)
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/shader/recompiler/backend/spirv/SpirvEmitter.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterMemory.cppsrc/graphics/shader/recompiler/ir/Program.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
@TheCruZ on GitHub · 2026-09-30 07:59 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 1858 (outdated):Reworked. The host no longer touches the texture cache for every table entry before the dispatch, so possible_write is gone from this PR. Each V#-table store now sets the bit of its caching page in a write bitmap (second half of the fault buffer); when the submission completes, the written bytes of the targets become GPU modified and are invalidated in the texture cache with the regular, exact InvalidateMemoryFromGPU, like any other buffer store. Images under entries the shader did not select are left alone. In GTA V no written page overlapped a GPU-modified image.
@TheCruZ on GitHub · 2026-09-30 07:59 UTC · original
On
src/graphics/host_gpu/renderer/renderCompute.cppline 58 (outdated):Done. There is no entry or size cap anymore: IndirectWriteTables covers the whole table (131072 entries in GTA V) and caches every buffer V# with a data format whose range is mapped. Each table keeps a snapshot and only rescans the pages the CPU wrote since the previous preparation; targets sharing a caching page are cached as one buffer, and targets released by the garbage collector or mapped later are cached again. In GTA V every target is now covered (0 uncovered pages, ~21000 before; 0 BDA faults, 463 before) and a preparation costs a few microseconds. Unmapped targets are skipped and revisited when the mappings change.
@TheCruZ on GitHub · 2026-09-30 08:00 UTC · original
On
src/graphics/host_gpu/renderer/renderCompute.cppline 93 (outdated):Fixed: when the buffer cache holds GPU-written bytes of the table, it is downloaded with ReadMemory first and then rescanned in full. (In GTA V the table is never GPU-written.)
@coderabbitai[bot] on GitHub · 2026-09-30 08:10 UTC · original
On
src/graphics/host_gpu/renderer/cache/textureCache.cppline 1858 (outdated):🧩 Analysis chain
🏁 Script executed:
Length of output: 13300
@TheCruZ, thanks for the rework. I verified that active, mapped stores record their caching page in the write bitmap. The deferred readback callsIndirectWriteTables::NoteWrites, which marks buffer ranges GPU-modified and calls the regularInvalidateMemoryFromGPU. Thepossible_writebypass is gone.This addresses the missing texture-cache notification in this finding. The bitmap tracks pages, not individual DWORDs, so invalidation remains conservative within each written page.
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-09-30 08:10 UTC · original
On
src/graphics/host_gpu/renderer/renderCompute.cppline 93 (outdated):🧩 Analysis chain
🏁 Script executed:
Length of output: 8984
@TheCruZ, verified. InIndirectWriteTables::Prepare, GPU-dirty table bytes triggerReadMemorybefore scanning. The code then setsfull = trueand scans all table entries. This addresses the stale-descriptor concern.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-09-30 08:11 UTC · original
On
src/graphics/host_gpu/renderer/renderCompute.cppline 58 (outdated):@TheCruZ, thanks for the fix and the implementation details. The updated preparation path removes both limits and delegates target caching toIndirectWriteTables. This addresses the shader-reachable target coverage concern.Your reported GTA V results—zero uncovered pages and zero BDA faults—also support the fix.
✏️ 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.
@ -649,0 +673,4 @@start, finish - start, [&](uint64_t page, uint64_t bytes) {const auto first = std::max(page, start);const auto last = std::min(page + bytes, finish);m_gpu_modified_ranges.Add(first, last - first);@coderabbitai[bot] on GitHub · 2026-10-05 17:05 UTC · original · edited 2026-10-08 21:18 UTC
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6971
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 22454
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8520
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5053
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4097
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8094
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3832
Track the aligned bytes written by an indirect DWORD store.
StoreIndirectBufferaligns the store address down to four bytes. An unaligned target base can therefore cause the store to write bytes before the target range.NoteWritesexcludes those bytes fromBufferCache::NoteGpuWrites, som_gpu_modified_rangescan omit data that the GPU changed and leave stale CPU readback.The callback uses an end address, not a size. Extend only the buffer-cache range. Keep texture invalidation on the original target range:
🐛 Suggested fix
✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-10-05 17:05 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
9a6a8f1c-2bab-41ec-b975-1f7cfe62692a📥 Commits
Reviewing files that changed from the base of the PR and between fd2791a94f17d1a88844cd3afb30c1854d6ec1d8 and c87faf95bad1842627cf1cd5ad3c43b493faa5ab.
📒 Files selected for processing (16)
CMakeLists.txtsrc/common/bitArray.hsrc/graphics/host_gpu/memoryTracker.hsrc/graphics/host_gpu/regionManager.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/graphics/shader/recompiler/backend/spirv/SpirvEmitter.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterMemory.cppsrc/graphics/shader/recompiler/ir/Program.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/MemoryTrackerTests.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@overlordxrz-source on GitHub · 2026-10-05 22:49 UTC · original
Validation + rebase report for this PR (read the diff, rebased onto current main
cdb64bf, built, tested on x86_64 macOS no-Qt Release):Rebase (3 spots, semantic resolutions):
PrepareGraphicsBindings: main refactoredFindBuffersto batch-span mode. Kept the batch call, added the per-stageCacheDmaBasesloop.DispatchDirect: same batch-FindBuffersrefactor; kept it and addedPrepareIndirectWriteTargetsafter it.BdaPageIndexhelper usesTypeScalarU64/ConstantDeviceAddress, which don't exist on main (renamed toTypeU64/ConstantU64). Ported (11 lines). Heads-up: the fork base behind this PR seems to have diverged renames; worth checking nothing else in the stack depends on the new names.Rebased helper branch (for reference, not a competing PR): overlordxrz-source/KytyPS5@helper/pr931-rebase.
Blocking finding:
shader_cfgfails on the PR's own head, before any rebase. Reproduced on pristinec87faf95(stashed my port, rebuilt, same result), so this is not a merge artifact:ShaderCfgTests: failed: material selection accepted an inclusive induction boundThe
ExpectFatal(TranslateProgram(unbounded...))negative test inshaderCfgTests.cppno longer rejects thes_cmp_le_u32wraparound case. The PR's materialization changes appear to have loosened induction-bound rejection while the test still expects it. Worth a look before landing.What passes:
kyty_emulatorlinks clean;resource_tracking+memory_trackergreen. GPU-harness tests not runnable here (no Vulkan loader — pre-existing env limitation, same as clean main).@ -649,0 +673,4 @@start, finish - start, [&](uint64_t page, uint64_t bytes) {const auto first = std::max(page, start);const auto last = std::min(page + bytes, finish);m_gpu_modified_ranges.Add(first, last - first);@TheCruZ on GitHub · 2026-10-08 21:08 UTC · original
Fixed in
4b326c8:IndirectWriteTables::TargetRangenow covers the whole DWORDs a target's stores can write (base aligned down and end aligned up to 4 bytes), so the buffer-cache GPU-modified ranges, the texture invalidation and the cached buffers all include the bytes an unaligned base exposes. Added an unaligned case to theIndirectWriteTablestest.@ -0,0 +56,4 @@table.address, address);}return;}@coderabbitai[bot] on GitHub · 2026-10-08 21:14 UTC · original
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Retry the scan after a failed
TryReadBacking.If
TryReadBackingfails,Scanreturns and keeps the old entries. For a new table, those entries are all zero. Later calls toPreparefind the table withfull = false. They rescan only the pages thatConsumeCpuWritesreports. If the CPU does not write the table again, the table is never scanned.target_countstays 0, soPreparereturns false and leaves the targets uncached. Stores through those V#s then reach memory that no cached buffer covers. The same failure keeps a stale snapshot after the GPU-dirty download path at Line 125. Thestatic bool loggedflag also hides every later failure, for every table.Fix: record the failure on the
Table, for example with aneeds_full_scanflag. Then forcefull = trueon the nextPrepare.Proposed fix
@coderabbitai[bot] on GitHub · 2026-10-08 21:14 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
32d0e555-69ac-4ac5-afff-1d7809365296📥 Commits
Reviewing files that changed from the base of the PR and between c87faf95bad1842627cf1cd5ad3c43b493faa5ab and
4b326c8b70.📒 Files selected for processing (2)
src/graphics/host_gpu/renderer/indirectWriteTables.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
@ -649,0 +673,4 @@start, finish - start, [&](uint64_t page, uint64_t bytes) {const auto first = std::max(page, start);const auto last = std::min(page + bytes, finish);m_gpu_modified_ranges.Add(first, last - first);@coderabbitai[bot] on GitHub · 2026-10-08 21:18 UTC · original
@TheCruZ, thanks for fixing this in4b326c8. AligningIndirectWriteTables::TargetRangecovers the exposed bytes consistently across cached buffers, GPU-modified ranges, and texture invalidation. The unaligned test case covers the reported edge case.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.
Pull request closed