Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
AlfaRojo:fix/shader-buffer-short-dword-straddle
Nov 26, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
AlfaRojo:fix/shader-buffer-short-dword-straddle

Conversation

@AlfaRojo

@AlfaRojo AlfaRojo commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

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

AlfaRojo and others added 2 commits November 16, 2026 15:31
…from both dwords

buffer_load_ushort and _sshort at an offset of 3 modulo 4 returned only the low
byte, because the load looked at one dword. RDNA2 (gfx1036, AMD Windows driver 32.0.21043.5001,
through the hardware oracle; IEEE 0, DX10 clamp 1, denorm32 0, denorm16 3, round32/16 0, fp16
overflow 0) returns both bytes whenever offset + 2 <= NUM_RECORDS. The second byte now comes from
the following dword, for every V# range.

BufferHalfStraddle pins ushort and sshort loads against 512 rows measured on that GPU, with
soffset, an immediate offset and a short range, in wave32 and wave64.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…nned

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added shader core/shader docs agc libSceAgc* labels Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6412 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 7642 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 9508 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 10193 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 13928 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 13929 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 20248 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp
PR 22071 core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp

…t mode UNALIGNED

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@domx4q

domx4q commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Ran the focused tests with the Khronos validation layer on Linux and an NVIDIA GPU, since CI runs the GPU tests only on lavapipe and the RTX 4070 run in the Tested section is on Windows.

Setup: Arch Linux (kernel 7.3.4), GCC 16.2.1, Release, NVIDIA GeForce RTX 4090 (vendor 0x10de, device 0x2684, Ada), proprietary driver 620.70.06, Vulkan 1.4.357, Khronos validation layer 1.4.363.1. Head 6037d3f541 merged onto main fbaae8a79d with --no-ff (clean, as e60fe2e93a). Main baseline is a Release build of fbaae8a79d on the 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 '^agc_driver_buffer_|^agc_driver_runtime_buffer_spans' -V
main fbaae8a79d this PR
Result 23 of 23 passed 24 of 24 passed
Insert instance layer "VK_LAYER_KHRONOS_validation" 23 24
was not found but was requested by env var VK_INSTANCE_LAYERS 0 0
VUID- none none

agc_driver_buffer_half_straddle is new and does not exist on main; it passes on this PR. Every device selected the RTX 4090.

Negative control: with main's SpirvRuntimeBuffer.cpp and this PR's test, agc_driver_buffer_half_straddle fails with 184 mismatch lines (for example wave=64 opcode=11 records=8 soffset=0 immediate=0 lane=3 actual=00000096 expected=ffff9d96) and AGC graphics: 16-bit buffer loads differ from RDNA2, the same count as in the Tested section. With the change restored it passes.

No new VUID- errors; all focused tests pass on Ada.

AI-assisted: yes (Claude Code)

@Colewiz7

Copy link
Copy Markdown
Contributor

Head 6037d3f541 merged onto main 7c836568c8 as 57bc987f56, no conflict. Linux 7.3.4, GCC 16.2.1 Release, Mesa 26.2.6 RADV, RX 7900 XT (gfx1100) and 7800X3D iGPU (gfx1036).

No new failures and no new validation messages on either GPU.

RX 7900 XT iGPU
tests run (6207 on the 7900 XT, the 2008 GPU ones on the iGPU), new failures vs main none none
-R '^agc' (1941 tests) with the layer, new failures vs main none none
VUID- / SYNC-HAZARD lines 0 / 0 0 / 0

Layer runs used VK_LAYER_KHRONOS_validation 1.4.363.1 with VK_LOADER_DEBUG=layer and ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1; the loader inserted the layer in every run, and the set of distinct validation messages matches main.

AI-assisted: yes

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Helldiverr

Copy link
Copy Markdown
Contributor

GPU test run: no VUID- errors on AMD RDNA4. The new test fails on main only at straddling offsets, and passes on this PR, including with a V# base off a dword.

Base: main 8e1af3d9b0. This PR's head 6037d3f541 merged onto it cleanly (merge 9aa6b42cb7). main has moved code in SpirvRuntimeBuffer.cpp, so line numbers below are from head unless marked "on the merge".

Code: Line numbers are from head 6037d3f541.

  • readBufferHalf (core/shader/recompiler/SpirvBackend/src/SpirvRuntimeBuffer.cpp:338; :338 on the merge) reads 16 bits with readBuffer. When address & 3 == 3 (:342), it also reads the byte at address + 1 and joins low & 0xff | high << 8.
  • Before this PR, readBuffer took both bytes from one dword window, shifted right by (address & 3) * 8 (:305). At offset 3 that left only the low byte.
  • The check uses the buffer-relative address, which matches the window that readBuffer builds for both aligned and misaligned V# bases (:279, :284).
  • It is used only on the non-BDA path (:1077; :1137 on the merge). The BDA path keeps readBuffer, which goes to EmitBdaRead and reads 16-bit values byte by byte (core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp:637), so it never had this problem.
  • access.inBounds covers both bytes, so the second read needs no separate range check. The test's records=8 case confirms that an access whose second byte is past the range returns 0.
  • tools/check_conventions.py --base 8e1af3d9b0 --head gpu-pr-21925 reports All conventions checks passed.

Outside this PR (code reading only, not run):

  • 16-bit stores have the same straddle in non-compute stages. writeBuffer writes bytes one at a time only when byteBuffers exists, which is compute only (:350).
  • Otherwise it shifts the 0xffff mask and value left by (address & 3) * 8 within one word (:402 to :407). So a buffer_store_short at offset 3 from a vertex or pixel shader with an aligned base drops its high byte.
  • The test is compute only, so it would not catch this.

Negative control:

  • I added the PR's BufferHalfStraddle.cpp unchanged to main 8e1af3d9b0, then rebuilt it and ran it.
  • It failed with 184 mismatches, for example opcode=10 ... lane=3 actual=00000096 expected=00009d96 and opcode=11 ... lane=3 actual=00000096 expected=ffff9d96.
  • All 184 are at (soffset + immediate + lane) % 4 == 3: 180 with records=64 and 4 with records=8. No other offset differed.

Mutation:

  • I changed the second read from address + 1 to address + 2 (:343; :343 on the merge). The test failed on the same 184 lanes, for example lane=3 actual=0000a496 expected=00009d96.
  • I reverted the mutation.

Input the test misses:

  • The test's V# base is 256-byte aligned (core/libs/prx/libSceAgcDriver/tests/execution/BufferHalfStraddle.cpp:59).
  • I changed it locally to Source.data() + 1, and changed Expected (:47) to read Source[1 + ...]. On the merge, all 16 cases (8 cases × 2 wave sizes) passed with 0 mismatches. So the straddle join also holds when readBuffer builds its window from two dwords.
  • I reverted the edit.

GPU:

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 '^agc_driver_(buffer_half_straddle|buffer_load_store.*|runtime_buffer_spans.*)$' -V
Test main head run 1 head run 2 head run 3
agc_driver_buffer_half_straddle not on main (negative control above: Failed 1.15 s) Passed 0.35 s Passed 0.27 s Passed 0.29 s
agc_driver_buffer_load_store Passed 1.32 s Passed 0.51 s Passed 0.26 s Passed 0.30 s
agc_driver_buffer_load_store_draw_pipeline Passed 0.26 s Passed 0.22 s Passed 0.25 s Passed 0.30 s
agc_driver_buffer_load_store_plan_verify Passed 0.25 s Passed 0.22 s Passed 0.25 s Passed 0.31 s
agc_driver_runtime_buffer_spans Passed 0.44 s Passed 0.38 s Passed 0.32 s Passed 0.44 s
agc_driver_runtime_buffer_spans_fault_barrier Passed 0.28 s Passed 0.26 s Passed 0.26 s Passed 0.30 s
agc_driver_runtime_buffer_spans_fault_stop Passed 0.27 s Passed 0.27 s Passed 0.27 s Passed 0.33 s
agc_driver_runtime_buffer_spans_x2_reference Passed 0.39 s Passed 0.37 s Passed 0.36 s Passed 0.39 s
agc_driver_runtime_buffer_spans_x2_spans Passed 0.47 s Passed 0.41 s Passed 0.39 s Passed 0.44 s
agc_driver_runtime_buffer_spans_x3_reference Passed 0.40 s Passed 0.36 s Passed 0.38 s Passed 0.45 s
agc_driver_runtime_buffer_spans_x3_spans Passed 0.48 s Passed 0.42 s Passed 0.42 s Passed 0.46 s
agc_driver_runtime_buffer_spans_x4_reference Passed 0.41 s Passed 0.37 s Passed 0.36 s Passed 0.41 s
agc_driver_runtime_buffer_spans_x4_spans Passed 0.45 s Passed 0.39 s Passed 0.40 s Passed 0.42 s
  • All 13 are GPU tests. Each selected the R9700 (subgroup=64) in every run, and none skipped.
  • The new test runs wave32 and wave64 (:105).
  • The agc_driver_buffer_load_store time on main includes a cold start after a clean rebuild.

Validation:

  • Insert instance layer "VK_LAYER_KHRONOS_validation" appears 12 times in the main log and 13 times in each head log, once per test.
  • was not found but was requested by env var VK_INSTANCE_LAYERS appears in no log.
  • VUID- lines: 0 on main, and 0, 0 and 0 on the three head runs.

Host:

  • GPU: AMD Radeon AI PRO R9700 (gfx1201, 1002:7551), driver 32.0.31044.16, Vulkan 1.4.349
  • Validation layer: Khronos 1.4.363 (LunarG SDK)
  • CPU and OS: Ryzen 9 9950X3D, Windows 11 Pro 26300
  • Build: WinLibs GCC 15.2, Release
  • Commits: main 8e1af3d9b0, head 6037d3f541 (merge 9aa6b42cb7)

AI-assisted: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants