renderer: preserve byte offsets for sub-dword storage buffers #476

Closed
github-bot wants to merge 1 commit from gh-pr/476/head into gh-pr/476/base
Owner

@⁠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 merging

Summary

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 new
ResourceForDescriptor/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:

access.index_offset = memory_byte_offsets[i] >> 2;

so the low two bits are dropped, and NativeStorageBuffer rejects any binding that needs them
with storage buffer offset adjustment is unsupported. The device alignment here is 16, so
three 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:

stage=Compute stride=0x1 format=k8UInt offset=0x5f4d1d alignment=0x10 adjustment=0xd
scalar=0 atomic=0 written=0 max_byte_extent=0x10

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:

expected 0x0000ab00
actual   0x000000ab

Folding at dword granularity computes 13 >> 2 == 3 for the element and loses the byte within
it, so the store lands at byte 12 instead — the same dword, the wrong byte.

Fix

Nothing here needs a new mechanism. A Buffer access already derives both its element index
and its byte within that element from one byte address: LoadSubwordPrepared computes
address >> 2 for the index and LoadSubwordInBounds uses address & 3 for the shift. The
offset was being folded in after that split instead of before it.

So fold the full byte adjustment into the byte address, in BufferByteAddress, before the
address is split. The element index, the sub-word shift and the bounds check then all stay
exact for any offset, because every Buffer consumer derives from that one address.

ScalarBuffer keeps its existing dword index. ReadConstBuffer loads a whole dword at
address >> 2 and has no byte address to fold into, so a base that is not dword aligned would
straddle 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. BufferAccessIsSubDword names the safe case rather than
widening 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.

EmitBufferAtomic64 is the one buffer path that does not go through
PrepareMemoryResourceAccess. It added the binding's byte offset itself, which was correct
while BufferByteAddress did not. Once BufferByteAddress incorporates the offset, that
manual 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.

expected [0,0,0x11223344,0xaabbccdd,0,0,0,0]     byte 8,  applied once
actual   [0,0,0,0,0x11223344,0xaabbccdd,0,0]     byte 16, applied twice

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. BufferAtomic64AppliesHostOffsetOnce was added to keep it that
way. 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.

  • New BufferStoreByteAppliesByteHostOffset — offset 13, one byte stored, expected at byte
    13. Fails without the emitter change with the 0x0000ab00 / 0x000000ab mismatch above.
    The harness itself asserted offset % 4 == 0; that assumption was part of the bug and is
    relaxed.
  • New BufferAtomic64AppliesHostOffsetOnce — offset 8, 64-bit atomic swap, expected at byte
    8. 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.
  • The existing dword-aligned sibling BufferStoreDwordAppliesHostOffset and the
    packed-lane/storage-fallback case BufferOffsetsUsePackedLaneAndStorageFallback still
    pass, so the already-working paths are unchanged.
  • Full ctest: 34/36, identical across four consecutive runs. Pristine upstream
    b3c8f91 measures the same 34/36 across three runs, with the same two failures, so this
    introduces none. The two are shader_recompiler_compute and texture_cache_image_overlap;
    both run the same binary, which aborts at the pre-existing
    RenderExecutorStencilBindingDiscovery check — before the buffer suite this PR touches
    ever runs.
  • Production build succeeds.

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.

build result
#443 alone 19 compiled shaders, then descriptors.cpp:163 storage buffer offset adjustment is unsupported
#443 + this PR 25 compiled shaders, then ResourceTracking.cpp:167 GetBufferResource dword 0 is not a valid runtime value

To 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 this
guard, 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.

**@⁠MehmetCambaz** on GitHub · 2026-09-03 18:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/476) `MehmetCambaz:fix/storage-buffer-byte-offset-port` → `main` · +119 −13 · 4 files · 1 commits · closed without merging ## Summary 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 new `ResourceForDescriptor`/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: ```cpp access.index_offset = memory_byte_offsets[i] >> 2; ``` so the low two bits are dropped, and `NativeStorageBuffer` rejects any binding that needs them with `storage buffer offset adjustment is unsupported`. The device alignment here is 16, so three 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: ``` stage=Compute stride=0x1 format=k8UInt offset=0x5f4d1d alignment=0x10 adjustment=0xd scalar=0 atomic=0 written=0 max_byte_extent=0x10 ``` 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: ``` expected 0x0000ab00 actual 0x000000ab ``` Folding at dword granularity computes `13 >> 2 == 3` for the element and loses the byte within it, so the store lands at byte 12 instead — the same dword, the wrong byte. ## Fix Nothing here needs a new mechanism. A `Buffer` access already derives both its element index and its byte within that element from one byte address: `LoadSubwordPrepared` computes `address >> 2` for the index and `LoadSubwordInBounds` uses `address & 3` for the shift. The offset was being folded in after that split instead of before it. So fold the full byte adjustment into the byte address, in `BufferByteAddress`, before the address is split. The element index, the sub-word shift and the bounds check then all stay exact for any offset, because every `Buffer` consumer derives from that one address. `ScalarBuffer` keeps its existing dword index. `ReadConstBuffer` loads a whole dword at `address >> 2` and has no byte address to fold into, so a base that is not dword aligned would straddle 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. `BufferAccessIsSubDword` names the safe case rather than widening 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. `EmitBufferAtomic64` is the one buffer path that does not go through `PrepareMemoryResourceAccess`. It added the binding's byte offset itself, which was correct while `BufferByteAddress` did not. Once `BufferByteAddress` incorporates the offset, that manual 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. ``` expected [0,0,0x11223344,0xaabbccdd,0,0,0,0] byte 8, applied once actual [0,0,0,0,0x11223344,0xaabbccdd,0,0] byte 16, applied twice ``` 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. `BufferAtomic64AppliesHostOffsetOnce` was added to keep it that way. **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. - [x] New `BufferStoreByteAppliesByteHostOffset` — offset 13, one byte stored, expected at byte 13. Fails without the emitter change with the `0x0000ab00` / `0x000000ab` mismatch above. The harness itself asserted `offset % 4 == 0`; that assumption was part of the bug and is relaxed. - [x] New `BufferAtomic64AppliesHostOffsetOnce` — offset 8, 64-bit atomic swap, expected at byte 8. 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. - [x] The existing dword-aligned sibling `BufferStoreDwordAppliesHostOffset` and the packed-lane/storage-fallback case `BufferOffsetsUsePackedLaneAndStorageFallback` still pass, so the already-working paths are unchanged. - [x] Full `ctest`: **34/36**, identical across four consecutive runs. Pristine upstream `b3c8f91` measures the same 34/36 across three runs, with the same two failures, so this introduces none. The two are `shader_recompiler_compute` and `texture_cache_image_overlap`; both run the same binary, which aborts at the pre-existing `RenderExecutorStencilBindingDiscovery` check — before the buffer suite this PR touches ever runs. - [x] Production build succeeds. ## 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. | build | result | |---|---| | #443 alone | **19** compiled shaders, then `descriptors.cpp:163` `storage buffer offset adjustment is unsupported` | | #443 + this PR | **25** compiled shaders, then `ResourceTracking.cpp:167` `GetBufferResource dword 0 is not a valid runtime value` | To 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 this guard, 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.
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 took them as a dword index:

  access.index_offset = memory_byte_offsets[i] >> 2

so the low two bits were dropped, and NativeStorageBuffer rejected any binding that needed
them:

  storage buffer offset adjustment is unsupported

Three of every four guest base addresses land there, because the device alignment is 16 and
only multiples of four survive the shift.

Teardown (PPSA15246) hits it about a second into boot. Its first compute shader reads a byte
buffer whose base is 13 bytes past the alignment boundary:

  stage=Compute stride=0x1 format=k8UInt offset=0x5f4d1d alignment=0x10 adjustment=0xd
  scalar=0 atomic=0 written=0 max_byte_extent=0x10

Nothing about that needs a new mechanism. A Buffer access already derives both its element
index and its byte within that element from one byte address: LoadSubwordPrepared computes
address >> 2 for the index and LoadSubwordInBounds uses address & 3 for the shift. The offset
was simply being folded in after the split instead of before it.

So fold it into the byte address, in BufferByteAddress, where every consumer picks it up: the
element index, the sub-word shift and the bounds check all stay exact for any offset.

EmitBufferAtomic64 is the one buffer path that does not go through
PrepareMemoryResourceAccess. It added the binding's byte offset itself, which was correct
while BufferByteAddress did not, and would double-apply it now that BufferByteAddress does.
Drop that addition so the 64-bit atomic picks the offset up exactly once, like every other
buffer access. The two descriptor arrays are defined together, so the byte address is always
folded by the time this path runs.

ScalarBuffer keeps the dword index. ReadConstBuffer loads a whole dword at address >> 2 and
has no byte address to fold into, so a base that is not dword aligned would straddle two
elements. The guard still rejects that, and it still rejects anything else that reads or
writes 32 bits at a time: a 32-bit component, a packed bitfield, an atomic.
BufferAccessIsSubDword names the case that is safe rather than widening the check, and the
message now prints the operands so the next title to reach it does not need a debug build.

Two regression tests cover the halves that can go wrong independently. One stores a byte at a
13-byte offset and checks it lands at byte 13 rather than byte 12, which a dword-granular fold
would produce. The other issues a 64-bit atomic swap through a binding with an 8-byte offset
and checks it lands at byte 8 rather than byte 16, which a double application would produce;
the bound range covers both so the case distinguishes where the write went, not whether it
happened. The existing dword-aligned and packed-lane/storage-fallback cases stay green.

On Teardown this removes the boot blocker at 19 compiled shaders; the run continues to 25 and
stops on unrelated resource tracking. It does not boot; this removes one blocker.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:22:59 +00:00
github-bot closed this pull request 2026-11-26 09:22:59 +00:00
Author
Owner

@⁠Nmzik on GitHub · 2026-10-21 19:42 UTC · original

Superseded by c3f496271.

**@⁠Nmzik** on GitHub · 2026-10-21 19:42 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/476#issuecomment-6324936266) Superseded by c3f496271.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.