fix(shader): read a 16-bit buffer load that crosses a dword boundary from both dwords #21925

Merged
AlfaRojo merged 3 commits from upstream-pr/21925 into main 2026-11-26 02:51:19 +00:00
AlfaRojo commented 2026-11-16 21:50:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-16T21:50:02Z.


What

A 16-bit buffer load (buffer_load_ushort, buffer_load_sshort) at a byte offset of 3 mod 4 crosses a dword boundary. The recompiler read only the dword holding the first byte, so it returned the low byte with a zero or sign-extended high byte, on any GPU and for any range. On RDNA2 the load returns both bytes: the low byte from the first dword and the high byte from the next one. The access is still all-or-nothing: if offset + 2 is past the range, the whole load returns 0.

This PR adds readBufferHalf to the non-BDA path of SpirvRuntimeBuffer.cpp. A 16-bit load at offset 3 mod 4 reads the high byte from the next dword, under the same whole-access enable as the low byte. Other offsets take the existing path. The BDA path already reads 16-bit values byte by byte and is unchanged.

Measurement: AMD Radeon Graphics (Ryzen iGPU, PCI 1002:13c0), AMD Windows driver 32.0.21043.5001, tools/hw-oracle through HIP, assembly target gfx1036, wave32. Float controls on every run: IEEE 0, DX10 clamp 1, denorm32 0, denorm16 3, round32 and round16 0, FP16 overflow 0. Raw V# word 3 0x31016fac. Rows cover ushort and sshort at every offset mod 4, with soffset, an immediate offset, and a short range where the second byte falls outside (NUM_RECORDS 8, offset 7: returns 0).

The new execution test BufferHalfStraddle runs eight cases (ushort and sshort, each with a 64-byte range at no offset, at soffset 3, at immediate offset 3, and an 8-byte range) over 64 lanes, so lane offsets 0 to 63 cover every offset mod 4. Each lane is compared with the measured rule above (both bytes joined, 0 when offset + 2 is past the range): 512 lane checks per wave size, wave32 and wave64, with 0 mismatches.

Alignment mode. These rows come from the AMD Windows driver, which runs alignment mode UNALIGNED (an unaligned buffer_load_dword reads the exact bytes there). The console runs mode DWORD (docs/dev/debt/shader.md, V# bases off a DWORD boundary, measured on a PS5). Whether DWORD mode splits or aligns a 16-bit access that crosses a dword is unmeasured, so this PR matches the measured PC hardware and records the console case in the debt fragment.

Typed 16-bit loads (tbuffer_load_format_x with a 16-bit format) at odd offsets are a separate path. Measured on a structured V# they return 0 at offsets 1, 3, 7, 11 and 15, and the recompiler's result there is not measured, so they are recorded in the debt fragment, not changed. gfx1013 itself is not measured.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, head 6037d3f541 (code at d3335727b9) on main bca2d36814.

  • ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1: BufferHalfStraddle passes in 3/3 runs on each GPU; Selected GPU name= names the requested GPU. With the fix reverted it fails on both GPUs (184 mismatches each). With the test copied onto main it fails on both GPUs.
  • ctest -R 'buffer|mubuf|mtbuf' (AGC tests): 63/63 pass on both GPUs. agc_formatted_buffer_once and agc_formatted_buffer_byte_stores could not be built here: Windows Defender quarantines their executables (Trojan:Win64/ShellCode.MKA!MTB, a heuristic detection on the generated test binaries; see BUILD.md on antivirus).
  • Validation layer (Khronos 1.4.363.0, LunarG SDK), same machine, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R '<runtime buffer spans, buffer load/store and buffer execution tests>' -V; the branch side adds the new test:
GPU side result layer inserted was not found VUID-
RTX 4070 main bca2d36814 21/21 21 0 0
RTX 4070 branch 22/22 22 0 0
Radeon 1002:13c0 main bca2d36814 21/21 21 0 0
Radeon 1002:13c0 branch 22/22 22 0 0

No new VUID- errors.

  • check_conventions.py --base upstream/main, check_test_registration.py and debt_fold.py --check pass.
  • Overlap: pr_overlap.py could not compare nine PRs that conflict with main and touch SpirvRuntimeBuffer.cpp. I read their hunks: #6412, #7642, #7689, #10193, #13928, #13929, #14217, #18515 and #20248 do not touch 16-bit buffer reads. The new test avoids unaligned dword loads, so it does not depend on #16415.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4070 (driver 32.0.16.2124) and AMD Radeon Graphics Ryzen iGPU 1002:13c0 (driver 32.0.21043.5001), Windows 11 Pro 10.0.26200
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5345799845 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21925). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-16T21:50:02Z. --- ### What A 16-bit buffer load (`buffer_load_ushort`, `buffer_load_sshort`) at a byte offset of 3 mod 4 crosses a dword boundary. The recompiler read only the dword holding the first byte, so it returned the low byte with a zero or sign-extended high byte, on any GPU and for any range. On RDNA2 the load returns both bytes: the low byte from the first dword and the high byte from the next one. The access is still all-or-nothing: if `offset + 2` is past the range, the whole load returns 0. This PR adds `readBufferHalf` to the non-BDA path of `SpirvRuntimeBuffer.cpp`. A 16-bit load at offset 3 mod 4 reads the high byte from the next dword, under the same whole-access enable as the low byte. Other offsets take the existing path. The BDA path already reads 16-bit values byte by byte and is unchanged. Measurement: AMD Radeon Graphics (Ryzen iGPU, PCI 1002:13c0), AMD Windows driver 32.0.21043.5001, `tools/hw-oracle` through HIP, assembly target gfx1036, wave32. Float controls on every run: IEEE 0, DX10 clamp 1, denorm32 0, denorm16 3, round32 and round16 0, FP16 overflow 0. Raw V# word 3 `0x31016fac`. Rows cover ushort and sshort at every offset mod 4, with `soffset`, an immediate offset, and a short range where the second byte falls outside (`NUM_RECORDS` 8, offset 7: returns 0). The new execution test `BufferHalfStraddle` runs eight cases (ushort and sshort, each with a 64-byte range at no offset, at `soffset` 3, at immediate offset 3, and an 8-byte range) over 64 lanes, so lane offsets 0 to 63 cover every offset mod 4. Each lane is compared with the measured rule above (both bytes joined, 0 when `offset + 2` is past the range): 512 lane checks per wave size, wave32 and wave64, with 0 mismatches. **Alignment mode.** These rows come from the AMD Windows driver, which runs alignment mode UNALIGNED (an unaligned `buffer_load_dword` reads the exact bytes there). The console runs mode DWORD (`docs/dev/debt/shader.md`, V# bases off a DWORD boundary, measured on a PS5). Whether DWORD mode splits or aligns a 16-bit access that crosses a dword is unmeasured, so this PR matches the measured PC hardware and records the console case in the debt fragment. Typed 16-bit loads (`tbuffer_load_format_x` with a 16-bit format) at odd offsets are a separate path. Measured on a structured V# they return 0 at offsets 1, 3, 7, 11 and 15, and the recompiler's result there is not measured, so they are recorded in the debt fragment, not changed. gfx1013 itself is not measured. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, head `6037d3f541` (code at `d3335727b9`) on `main` `bca2d36814`. - `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`: `BufferHalfStraddle` passes in 3/3 runs on each GPU; `Selected GPU name=` names the requested GPU. With the fix reverted it fails on both GPUs (184 mismatches each). With the test copied onto `main` it fails on both GPUs. - `ctest -R 'buffer|mubuf|mtbuf'` (AGC tests): 63/63 pass on both GPUs. `agc_formatted_buffer_once` and `agc_formatted_buffer_byte_stores` could not be built here: Windows Defender quarantines their executables (`Trojan:Win64/ShellCode.MKA!MTB`, a heuristic detection on the generated test binaries; see BUILD.md on antivirus). - Validation layer (Khronos 1.4.363.0, LunarG SDK), same machine, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R '<runtime buffer spans, buffer load/store and buffer execution tests>' -V`; the branch side adds the new test: | GPU | side | result | layer inserted | `was not found` | `VUID-` | |---|---|---|---|---|---| | RTX 4070 | main `bca2d36814` | 21/21 | 21 | 0 | 0 | | RTX 4070 | branch | 22/22 | 22 | 0 | 0 | | Radeon 1002:13c0 | main `bca2d36814` | 21/21 | 21 | 0 | 0 | | Radeon 1002:13c0 | branch | 22/22 | 22 | 0 | 0 | No new `VUID-` errors. - `check_conventions.py --base upstream/main`, `check_test_registration.py` and `debt_fold.py --check` pass. - Overlap: `pr_overlap.py` could not compare nine PRs that conflict with `main` and touch `SpirvRuntimeBuffer.cpp`. I read their hunks: #6412, #7642, #7689, #10193, #13928, #13929, #14217, #18515 and #20248 do not touch 16-bit buffer reads. The new test avoids unaligned dword loads, so it does not depend on #16415. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4070 (driver 32.0.16.2124) and AMD Radeon Graphics Ryzen iGPU 1002:13c0 (driver 32.0.21043.5001), Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes
MarcZX10 commented 2026-11-19 14:25:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T14:25:59Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 6037d3f5417d278335f5f08196e0558d6f3db68b against the prepared diff. I used the repository reader to inspect all three changed files and the related readBuffer and runtime address logic. I ran no build or tests; results reported in the PR description are author-reported.

Results

Check Result Note
Changed code review passed All changed lines were read
Build and tests not run Author reports passing hardware runs

Findings

Blocking

None.

Non-blocking

  • BufferHalfStraddle.cpp covers every lane for offsets 0–63 (and a short-range case), not 512 distinct rows as stated in the description. The test checks the straddle offset and both signedness modes across wave32 and wave64, but consider correcting the claim or expanding the matrix if the larger row count is intended.

Not verified

No build or tests were run in this review. The reported GPU measurements and test results were not independently verified. The console's DWORD alignment behavior and gfx1013 behavior remain unmeasured per the description. Read: BufferHalfStraddle.cpp, SpirvRuntimeBuffer.cpp, and fix-shader-buffer-short-dword-straddle.md. Excluded: none of the changed files; there is no files.json in the prepared review folder.

To get this to a good state

Nothing is required. Optional: align the “512 measured rows” description with the execution test’s actual cases.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5976850373 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21925#pullrequestreview-5976850373). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T14:25:59Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `6037d3f5417d278335f5f08196e0558d6f3db68b` against the prepared diff. I used the repository reader to inspect all three changed files and the related `readBuffer` and runtime address logic. I ran no build or tests; results reported in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All changed lines were read | | Build and tests | not run | Author reports passing hardware runs | ## Findings ### Blocking None. ### Non-blocking - `BufferHalfStraddle.cpp` covers every lane for offsets 0–63 (and a short-range case), not 512 distinct rows as stated in the description. The test checks the straddle offset and both signedness modes across wave32 and wave64, but consider correcting the claim or expanding the matrix if the larger row count is intended. ## Not verified No build or tests were run in this review. The reported GPU measurements and test results were not independently verified. The console's DWORD alignment behavior and gfx1013 behavior remain unmeasured per the description. Read: `BufferHalfStraddle.cpp`, `SpirvRuntimeBuffer.cpp`, and `fix-shader-buffer-short-dword-straddle.md`. Excluded: none of the changed files; there is no `files.json` in the prepared review folder. ## To get this to a good state Nothing is required. Optional: align the “512 measured rows” description with the execution test’s actual cases.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!21925
No description provided.