shader: bind both buffers when an atomic uses a descriptor chosen by a Phi #2219
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!2219
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2219/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?
@Nawid3333 on GitHub · 2026-11-11 18:30 UTC · original
Nawid3333:shader/finite-buffer-atomic-r2→main· +256 −28 · 8 files · 2 commits · closed without mergingFixes #2204.
Cause
A register can hold one of two buffer descriptors, depending on a branch the GPU takes. In Ghost of Yōtei's compute shader
0xc37d0172087dc1a6the registers20is loaded from the resource table before a loop (pc 0x8c) and again on a conditional path inside it (pc 0x660); theBUFFER_ATOMIC_ADDat pc 0x980, after the loop, uses whichever was loaded last. In the IR the descriptor is a Phi of the two loads.Resource tracking already plans a descriptor of that kind (
TryMakeFiniteDescriptor: one of a few known descriptors, picked by a Phi or a select), but only for image and sampler descriptors. For a buffer descriptor it stops withbuffer descriptor is not a valid runtime value.Change
TryMakeFiniteDescriptoralso takes a buffer descriptor when every use of it is a 32-bit buffer atomic, andPlanIndirectDescriptorssends buffer atomics to it. Both descriptors become candidates of one resource and a key (0 or 1, the Phi of the branch) selects between them. Loads from a descriptor chosen this way keep main's path for GPU-selected descriptors. A 64-bit atomic on such a descriptor still stops with the message above.NormalizeIndirectBufferis made for rows of a table the GPU indexes and would reject ordinary descriptors (a swizzle, another stride).EmitAtomicAccessemits throughEmitBufferAccess, like loads and stores do, so the atomic runs on the candidate the key selects, with that candidate's stride and offset. For a buffer without candidates it emits the same instructions as before; the element index is now computed after the resource access is prepared, so their order changes.packed_stride == 0on a buffer with an indirect source. A raw buffer picked by a Phi has stride 0 and such a source too, so the newEmptyIndirectBufferCandidatealso requires that the source has no finite list.#1939 is an open PR for the same message. It sends
BufferAtomicIAdd32down the path of loads from a descriptor the GPU computes; its test shape (userdata + iin a loop) is not this one, where the choice is between two descriptors that both sit in the resource table.Tests
resource_tracking_tests,TestFiniteBufferAtomic: a Phi of two user-data descriptors under a 32-bit atomic gives one finite source with two entries and a key Phi of 0 and 1; after specialization both candidates exist and are atomic; a 64-bit atomic on the same shape still stops.shader_cfg_tests,TestBufferAtomicOnPhiOfTwoDescriptors: guest code with twoS_LOAD_DWORDX4intos20around a branch on a value loaded from a buffer, thenBUFFER_ATOMIC_ADD. Two layouts (both raw; stride 0 and stride 4); the SPIR-V validates and the second layout has one atomic per candidate.shader_recompiler_compute_tests,BufferAtomicOnPhiOfTwoDescriptors(selector--buffer-atomic-phi-only): runs that shape on the GPU with two descriptors of stride 4 and 8, and with two of stride 4 (the path where both candidates share one binding array), each once for each side of the branch, and reads back that the add landed in the selected buffer at the selected stride and that the old value was returned.With main's
src/(bc934ef) under these tests, the first two fail with the issue's message (pc=0x00000980andpc=0x00000020 buffer descriptor is not a valid runtime value) and the GPU test fails at pc 0x2c. With the change all three pass. Two deliberate bugs in the change (always the first candidate; the index taken from the root's stride) each make the GPU test's second case fail at readback; a third (the shared binding array always indexed at the root's slot) makes only the equal-stride second case fail.The GPU test was run on an RX 9070 XT (Windows 11, clang-cl) with the local harness changes described in #2193; with the change the full suite has 22 failed checks, the same 22 as main
782a85con this machine (failures of the harness on this driver that were known before). The first two tests also pass in the fork's CI on Windows, Linux and macOS (run 43014871201, on this commit before the GPU test was added, with a workflow step that runsshader_cfg_tests, which the upstream workflow does not run). The fork CI run on this head (run 43085179853) passed on Windows, Linux and macOS.Offline, the shader dumps I have from four games (1,041 compute, pixel and vertex programs) were recompiled with main and with the change: one outcome differs, the shader above, which no longer stops in resource tracking. The IR is identical wherever both translate; of the 86 SPIR-V modules both write, 82 are byte-identical and 4 contain the same instructions in another order.
Costs
None for shaders that translate today, apart from the instruction order in shaders with buffer atomics. A shader of this shape binds both buffers and gets one branch per candidate at the atomic.
In a game
Ghost of Yōtei (PPSA26344) on an RX 9070 XT (Windows 11). On this GPU main (
bc934ef) stops earlier, atdescriptorHeap.cpp:55(!Allocate(layout, fresh_batch)); #1758 gets past that, so both builds below also carry #1758.0xc37d0172087dc1a6, pc 0x980, after 147 translated compute shaders.Written with Claude Code (AI): Claude Code traced the shader, wrote the change and the tests and ran them; GLM reviewed the diff.
@coderabbitai[bot] on GitHub · 2026-11-11 18:41 UTC · original · edited 2026-11-11 19:15 UTC
No actionable comments were generated in the recent review.
Walkthrough
Resource tracking now plans finite buffer descriptors for 32-bit atomics that use selected descriptors. Materialization skips indirect-buffer normalization for descriptors with explicit sources. The SPIR-V emitter uses a shared predicate for empty indirect-buffer candidates and routes atomic access through
EmitBufferAccess. New tests cover descriptor planning, SPIR-V generation, and compute execution.Suggested reviewers:
nmzikChange: Bug fix · Severity of issue fixed: High
Merge Risk:
Minimal · up to
96832This change adds support for buffer atomics on descriptors selected between two candidates. No actionable merge-blocking risk remains in the supplied evidence. The author did not report that Ghost of Yōtei reaches the main menu, though its shader now translates.
Pre-merge checks:
3 ·
2
#2204requires the game to boot to the main menu. The change addresses the reportedbuffer descriptor is not a valid runtime valuefailure and adds focused resource-tracking, CFG, and compute …#2204.#2204. They extend finite descriptor tracking for 32-bit buffer atomics, preserve the existing failure for unsupported buffer uses, update atomic buffer emission, and add…Full details: Out of Scope Changes check
Explanation
The changes stay within issue
#2204. They extend finite descriptor tracking for 32-bit buffer atomics, preserve the existing failure for unsupported buffer uses, update atomic buffer emission, and add tests for the affected Phi-selected descriptors. The emitter and materialization changes support this behavior.Full details: Linked Issues check
Explanation
Issue
#2204requires the game to boot to the main menu. The change addresses the reportedbuffer descriptor is not a valid runtime valuefailure and adds focused resource-tracking, CFG, and compute tests. The PR report states that the game continues running after the target shader translates, but it does not establish that the game reaches the main menu.Resolution
Establish whether the reviewed build boots to the main menu. If it does not, address the remaining blocker before claiming full compliance with issue
#2204.Full details: Docstring Coverage
Explanation
Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 8 functions across 6 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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
19b08faf-9714-4839-8391-a8483d16b9c6Reviewing files that changed from the base of the PR and between
f563cfe1c9and9683292638.tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
@ -1668,1 +1667,3 @@value, source->indirect_descriptor->indexed, base);return !source->indirect_descriptor->sources.empty() ||NormalizeIndirectBuffer(value, source->indirect_descriptor->indexed, base);@coderabbitai[bot] on GitHub · 2026-11-11 18:41 UTC · original · edited 2026-11-11 19:28 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42203
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 32852
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 32852
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 19997
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 37246
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 26985
Enforce candidate bounds before the atomic address wraps.
For a mode-0 candidate with stride 16 and one record, index
0x10000000wraps the byte address to zero. The atomic can then modify record zero instead of discarding the out-of-bounds write. Check the original index against the selected descriptor’s bounds before emitting the atomic.@coderabbitai[bot] on GitHub · 2026-11-11 18:41 UTC · original · reviewed
Actionable comments posted: 1
627fdf11-36d6-4ac0-b844-f5cc39f8049eReviewing files that changed from the base of the PR and between
bc934ef034andf563cfe1c9.src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterMemory.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterMemoryHelpers.cppsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@ -1668,1 +1667,3 @@value, source->indirect_descriptor->indexed, base);return !source->indirect_descriptor->sources.empty() ||NormalizeIndirectBuffer(value, source->indirect_descriptor->indexed, base);@Nawid3333 on GitHub · 2026-11-11 19:28 UTC · original
@coderabbitai The wrap is real, but this PR does not introduce it, and it is not specific to atomics.
CalculateBufferAddressinspirvEmitterMemory.cppcomputesindex * stride + offsetin 32 bits for every indexed buffer access. After that, the wrapped address is only checked as an element index against the binding length (EmitMemoryElementInBounds/EmitStorageElementInBounds). An atomic on a single descriptor onmaintakes the same path (DwordIndex+EmitMemoryElementInBounds), and loads use the same address withEmitStorageElementInBounds, so index0x10000000with stride 16 reaches record zero there too. Only descriptors read from an indexed table compare the original index before the multiplication (BufferIndexInBounds). This PR routes the Phi-selected atomic through the same path as an atomic on one descriptor. A record-index check for all indexed loads, stores and atomics belongs in that shared address path, so it will be a separate change.The equal-stride GPU case from the nitpick is added in
968329263:BufferAtomicOnPhiOfTwoDescriptorsalso runs with both strides equal, which takes the shared-binding path, for both selections, and checks the changed buffer and the returned value.@ -1668,1 +1667,3 @@value, source->indirect_descriptor->indexed, base);return !source->indirect_descriptor->sources.empty() ||NormalizeIndirectBuffer(value, source->indirect_descriptor->indexed, base);@coderabbitai[bot] on GitHub · 2026-11-11 19:29 UTC · original
Analysis chain
Script executed:
Length of output: 87892
Script executed:
Length of output: 7822
@Nawid3333You’re right. The base implementation already uses 32-bit address multiplication before the atomic bounds check. I withdraw the finding as a PR-specific regression.I also checked the equal-stride test. It covers both selections and checks the changed buffer and returned value. I did not run it.
Would you like me to open a follow-up issue for the shared bounds check?
Review thread resolved.
You are interacting with an AI system.
@Nawid3333 on GitHub · 2026-11-12 03:39 UTC · original
Ben-Dash ran this PR's Windows build (fork build
9683292, the head of this PR) with Ghost of Yōtei and posted the log in #2204 (comment):7d4e0d9, ends with the message of the issue at shader0xc37d0172087dc1a6, pc 0x980, afterShaders: VS 19 | PS 46 | CS 157.Shaders: VS 138 | PS 151 | CS 467and then stops atimage heaps exceed 65536 resident images(imageHeap.cpp:389), a limit this PR does not change.This build does not include #1758; on his system main reaches this shader without it.
Pull request closed