shader: apply a storage buffer's host offset at byte granularity #448

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

@⁠MehmetCambaz on GitHub · 2026-09-01 22:14 UTC · original
MehmetCambaz:ai/work/teardown-storage-offset → main · +90 −11 · 4 files · 1 commits · closed without merging

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 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 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

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 it into the byte address, in BufferByteAddress, which every Buffer consumer
derives from. The element index, the sub-word shift and the bounds check then all stay exact
for any offset.

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 still rejects anything else that touches 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 its operands
so the next title to reach it does not need a debug build.

Test plan

Windows 11, clang-cl 20.1.8, RTX 2060, Release.

  • New BufferStoreByteAppliesByteHostOffset: offset 13, one byte stored, expected at byte
    13. Reverting just the emitter change makes it fail with
    expected [...0x0000ab00...] actual [...0x000000ab...] — the byte lands at 12 instead
    of 13, which is exactly the two bits the shift discarded. The harness itself asserted
    offset % 4 == 0; that assumption was part of the bug and is relaxed.
  • shader_recompiler_compute_tests: 344 cases, all pass, including all 272 compute cases.
  • ctest 36/36 with #446 applied. On plain main it is 34/36, unchanged from baseline —
    the two failures are the pre-existing RenderExecutorStencilBindingDiscovery ones.
  • Teardown PPSA15246: 19 compiled shaders to 25, and the storage message is gone. It
    then stops in resource tracking with
    GetBufferResource dword 0 is not a valid runtime value, which is unrelated to this.
    Teardown still does not boot — this removes one blocker and nothing more is claimed.
  • PAC-MAN WORLD 2 Re-PAC PPSA18189, which exercises storage buffers throughout: renders
    correctly, checked against a screenshot, with no shader or storage errors in the log.

Reaching the Teardown blocker at all needs #443, which is still open; the fix here is
independent of it and is based on plain main.

Worth flagging: #427 also edits NativeStorageBuffer, for range clamping rather than this
guard, so the two will conflict textually if both land.

AI use

Developed with AI assistance (Claude). It reproduced the blocker, added the diagnostic that
exposed the format and access flags, traced the address path to BufferByteAddress, wrote the
change and the regression test, and ran the builds, the test suite and the game runs above on
my machine. I reviewed the diff and confirmed the ScalarBuffer path still takes the dword
index.

**@⁠MehmetCambaz** on GitHub · 2026-09-01 22:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/448) `MehmetCambaz:ai/work/teardown-storage-offset` → `main` · +90 −11 · 4 files · 1 commits · closed without merging ## 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 took them as a dword index: ```cpp access.index_offset = memory_byte_offsets[i] >> 2; ``` so the low two bits were dropped, and `NativeStorageBuffer` rejected any binding that needed 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 ``` ## 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 it into the byte address, in `BufferByteAddress`, which every `Buffer` consumer derives from. The element index, the sub-word shift and the bounds check then all stay exact for any offset. `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 still rejects anything else that touches 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 its operands so the next title to reach it does not need a debug build. ## Test plan Windows 11, clang-cl 20.1.8, RTX 2060, Release. - [x] New `BufferStoreByteAppliesByteHostOffset`: offset 13, one byte stored, expected at byte 13. Reverting just the emitter change makes it fail with `expected [...0x0000ab00...] actual [...0x000000ab...]` — the byte lands at 12 instead of 13, which is exactly the two bits the shift discarded. The harness itself asserted `offset % 4 == 0`; that assumption was part of the bug and is relaxed. - [x] `shader_recompiler_compute_tests`: 344 cases, all pass, including all 272 compute cases. - [x] `ctest` 36/36 with #446 applied. On plain `main` it is 34/36, unchanged from baseline — the two failures are the pre-existing `RenderExecutorStencilBindingDiscovery` ones. - [x] Teardown PPSA15246: **19 compiled shaders to 25**, and the storage message is gone. It then stops in resource tracking with `GetBufferResource dword 0 is not a valid runtime value`, which is unrelated to this. **Teardown still does not boot** — this removes one blocker and nothing more is claimed. - [x] PAC-MAN WORLD 2 Re-PAC PPSA18189, which exercises storage buffers throughout: renders correctly, checked against a screenshot, with no shader or storage errors in the log. Reaching the Teardown blocker at all needs #443, which is still open; the fix here is independent of it and is based on plain `main`. Worth flagging: #427 also edits `NativeStorageBuffer`, for range clamping rather than this guard, so the two will conflict textually if both land. ## AI use Developed with AI assistance (Claude). It reproduced the blocker, added the diagnostic that exposed the format and access flags, traced the address path to `BufferByteAddress`, wrote the change and the regression test, and ran the builds, the test suite and the game runs above on my machine. I reviewed the diff and confirmed the `ScalarBuffer` path still takes the dword index.
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 survived 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.

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.

Teardown goes from 19 compiled shaders to 25 and stops somewhere unrelated, in 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:21:23 +00:00
github-bot closed this pull request 2026-11-26 09:21:23 +00:00
Author
Owner

@⁠Nmzik on GitHub · 2026-10-06 17:18 UTC · original

superseded by main changes

**@⁠Nmzik** on GitHub · 2026-10-06 17:18 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/448#issuecomment-6021595258) superseded by main changes

Pull request closed

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