renderer: preserve byte offsets for sub-dword storage buffers #476
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!476
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/476/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?
@MehmetCambaz on GitHub · 2026-09-03 18:47 UTC · original
MehmetCambaz:fix/storage-buffer-byte-offset-port→main· +119 −13 · 4 files · 1 commits · closed without mergingSummary
This is the current-upstream port of #448.
The shader-recompiler resource-specialization rework reconstructed the affected code path
without the fix from #448, which had never merged, so the underlying byte-offset bug is still
present on current
main. This reapplies it in terms of the newResourceForDescriptor/descriptor-array model.Problem
A storage buffer is bound at the guest base rounded down to
minStorageBufferOffsetAlignment,and the bytes that were rounded off are handed to the shader as an 8-bit offset per buffer.
The shader takes them as a dword index:
so the low two bits are dropped, and
NativeStorageBufferrejects any binding that needs themwith
storage buffer offset adjustment is unsupported. The device alignment here is 16, sothree of every four guest base addresses land in that guard.
Teardown (PPSA15246) hits it about a second into boot. Its first compute shader reads a byte
buffer whose base is 13 bytes past the boundary:
An offset of 13 separates byte addressing from dword folding, which is why the regression test
uses it. Storing one byte at guest offset 0 through a binding with a 13-byte offset must land
at byte 13, which is byte 1 of dword 3:
Folding at dword granularity computes
13 >> 2 == 3for the element and loses the byte withinit, so the store lands at byte 12 instead — the same dword, the wrong byte.
Fix
Nothing here needs a new mechanism. A
Bufferaccess already derives both its element indexand its byte within that element from one byte address:
LoadSubwordPreparedcomputesaddress >> 2for the index andLoadSubwordInBoundsusesaddress & 3for the shift. Theoffset was being folded in after that split instead of before it.
So fold the full byte adjustment into the byte address, in
BufferByteAddress, before theaddress is split. The element index, the sub-word shift and the bounds check then all stay
exact for any offset, because every
Bufferconsumer derives from that one address.ScalarBufferkeeps its existing dword index.ReadConstBufferloads a whole dword ataddress >> 2and has no byte address to fold into, so a base that is not dword aligned wouldstraddle two elements.
The host-side guard is relaxed only for the case that is now provably safe. It still rejects
anything that reads or writes 32 bits at a time — a 32-bit component, a packed bitfield, a
scalar buffer read, an atomic.
BufferAccessIsSubDwordnames the safe case rather thanwidening the check, and the message now prints its operands so the next title to reach it does
not need a debug build.
Atomic64 double-application, found in review and fixed
Recording this because it is useful reviewer context.
EmitBufferAtomic64is the one buffer path that does not go throughPrepareMemoryResourceAccess. It added the binding's byte offset itself, which was correctwhile
BufferByteAddressdid not. OnceBufferByteAddressincorporates the offset, thatmanual addition applies it a second time.
An earlier revision of this branch had exactly that defect. A review pass caught it, and it was
reproduced on hardware before anything was changed: with an 8-byte binding offset, a 64-bit
atomic swap landed at byte 16 instead of byte 8.
The redundant addition was removed, so the path reads
const auto byte_address = ByteAddress(ctx, inst, mem);and picks the offset up exactly once,like every other buffer access.
BufferAtomic64AppliesHostOffsetOncewas added to keep it thatway. This is a resolved review finding, not a defect in the diff being proposed.
Test plan
Windows 11, clang-cl 20.1.8, RTX 2060, Release.
BufferStoreByteAppliesByteHostOffset— offset 13, one byte stored, expected at byte13. Fails without the emitter change with the
0x0000ab00/0x000000abmismatch above.The harness itself asserted
offset % 4 == 0; that assumption was part of the bug and isrelaxed.
BufferAtomic64AppliesHostOffsetOnce— offset 8, 64-bit atomic swap, expected at byte8. Fails against the pre-correction revision of this branch with the byte-16 result above
and passes here, so it genuinely discriminates one application from two. The bound range
covers both destinations, so the case distinguishes where the write went rather than
whether it happened.
BufferStoreDwordAppliesHostOffsetand thepacked-lane/storage-fallback case
BufferOffsetsUsePackedLaneAndStorageFallbackstillpass, so the already-working paths are unchanged.
ctest: 34/36, identical across four consecutive runs. Pristine upstreamb3c8f91measures the same 34/36 across three runs, with the same two failures, so thisintroduces none. The two are
shader_recompiler_computeandtexture_cache_image_overlap;both run the same binary, which aborts at the pre-existing
RenderExecutorStencilBindingDiscoverycheck — before the buffer suite this PR touchesever runs.
Teardown
Reaching this blocker at all needs #443, which is a separate open PR and an unrelated
prerequisite; it was applied locally for this comparison only and is not part of this branch.
descriptors.cpp:163storage buffer offset adjustment is unsupportedResourceTracking.cpp:167GetBufferResource dword 0 is not a valid runtime valueTo be precise about what that shows: the 19 to 25 transition demonstrates that the host-side
rejection is cleared and the bindings are accepted. It does not by itself establish that the
address arithmetic is right — the regression tests above are what establish that, and the
atomic64 case specifically establishes that the adjustment is applied exactly once. Both halves
are needed; neither is sufficient alone.
Teardown does not boot. This removes one blocker. It does not fix the resource-tracking
blocker it then stops on, and it does not address the later PM4 REWIND blocker.
Secondary validation
PAC-MAN WORLD 2 Re-PAC (PPSA18189), which exercises storage buffers throughout, was booted and
rendered correctly during validation of this port — full 3D overworld, textures, lighting, no
corruption, no storage-buffer errors in the trace.
In fairness, that run was made before the atomic64 correction, and it could not be repeated
afterwards. The host GPU state degraded late in the session after many repeated launches and a
force-killed process, and both titles then hung at 0 compiled shaders. That was confirmed
environmental rather than a regression by re-running an earlier build that had worked minutes
before, which hung identically — a hang occurring before any shader compiles cannot be caused by
emitted SPIR-V. So no post-correction PAC-MAN result is being claimed here. The Teardown run
above was made with the corrected build.
Relationship to #448
This is the current-upstream port of #448. #448 remains open and untouched; it is not superseded
on the merits, only stranded by the rework that landed after it.
Also worth flagging: #427 still edits
NativeStorageBuffer, for range clamping rather than thisguard, so the two will conflict textually if both land.
AI use
AI assistance (Claude) was used during investigation, implementation, and test development. The
resulting diff and validation were reviewed and verified before submission.
@Nmzik on GitHub · 2026-10-21 19:42 UTC · original
Superseded by
c3f496271.Pull request closed