fix(shader): align raw vector buffer dword accesses to the V# base #16415

Open
akkadbakkad89 wants to merge 2 commits from upstream-pr/16415 into main
akkadbakkad89 commented 2026-11-06 16:34:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-11-06T16:34:58Z.


What

A V# whose base is not DWORD-aligned previously produced exact-byte accesses for raw buffer_load/store_dword[xN] (the gfx1035 UNALIGNED-mode model): SpirvRuntimeBuffer fed the base's low two bits into baseMisalignment and shift-merged the read across two dwords, and BDA paths added the unmasked base.

On the console (SH_MEM_CONFIG.alignment_mode = DWORD) the two low bits are dropped: a PS5 (13.40, gfx1013, measured 2026-10-19) buffer_load_dword with the V# base 1 byte past a dword, or an aligned base and voffset 1, returned the aligned dword (0x44332211, not 0x55443322). This change adds AlignsRawDwordAddress (ResourceKind::Buffer && !formatted && dataBits == 32 && non-atomic) and applies it in byteAddress (masks the computed guest byte offset), address (masks the V# base on the BDA path), and readBuffer/writeBuffer (skip the misalignment shift-merge), so raw dword accesses use the aligned-down dword.

Out of scope, still as before: scalar buffers (already aligned), buffer atomics (byte-addressed, measured on gfx1031), formatted/typed accesses (element alignment enforced at decode), and byte/short accesses (exact bytes, console behavior unmeasured — kept in the debt file).

Tested

Windows 11, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (confirmed loaded, no VUID- errors), ctest on an NVIDIA GeForce RTX 3070:

  • agc_driver_buffer_unaligned_base — updated the raw dword/dwordx2 load expectations and the store model to the aligned-down view (stores below the view base stay clipped by the copy-back window; later byte/short stores overwrite overlaps). Passes at base misalignments 1, 2 and 3 for loads, stores, dwordx2 GLC, scalar, atomic, formatted and fault cases.
  • agc_driver_buffer_descriptor_runtime — the base +1/+2/+3 and records 64 base +2 cases now expect the aligned-down view; the scalar sections were already aligned and are unchanged. Passes for specialized and registration modules, plain and coherent.
  • Regression: agc_driver_buffer_load_store, _draw_pipeline, agc_driver_buffer_atomics, _float, _inc_dec64, _or_swap64, agc_driver_buffer_atomic_csub, agc_driver_buffer_atomic_unaligned, agc_driver_buffer_integer_atomics, agc_driver_buffer_format, agc_driver_masked_global_store, agc_driver_scalar_wide_loads, agc_driver_sparse_buffer_holes, agc_driver_many_buffers, agc_driver_scalar_buffer_runtime_word3, agc_driver_formatted_store_unaligned, agc_driver_null_buffer_substitutes, agc_driver_pattern_buffer_fill, agc_driver_pooled_buffer_range — all pass.

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
  • 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 3070 (driver 617.42), Windows 11 — AGC / shader recompiler execution tests above
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5191201386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16415). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-11-06T16:34:58Z. --- ### What A V# whose base is not DWORD-aligned previously produced exact-byte accesses for raw `buffer_load/store_dword[xN]` (the gfx1035 UNALIGNED-mode model): `SpirvRuntimeBuffer` fed the base's low two bits into `baseMisalignment` and shift-merged the read across two dwords, and BDA paths added the unmasked base. On the console (`SH_MEM_CONFIG.alignment_mode` = DWORD) the two low bits are dropped: a PS5 (13.40, gfx1013, measured 2026-10-19) `buffer_load_dword` with the V# base 1 byte past a dword, or an aligned base and `voffset` 1, returned the aligned dword (0x44332211, not 0x55443322). This change adds `AlignsRawDwordAddress` (`ResourceKind::Buffer` && !formatted && dataBits == 32 && non-atomic) and applies it in `byteAddress` (masks the computed guest byte offset), `address` (masks the V# base on the BDA path), and `readBuffer`/`writeBuffer` (skip the misalignment shift-merge), so raw dword accesses use the aligned-down dword. Out of scope, still as before: scalar buffers (already aligned), buffer atomics (byte-addressed, measured on gfx1031), formatted/typed accesses (element alignment enforced at decode), and byte/short accesses (exact bytes, console behavior unmeasured — kept in the debt file). ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (confirmed loaded, no `VUID-` errors), `ctest` on an NVIDIA GeForce RTX 3070: - `agc_driver_buffer_unaligned_base` — updated the raw dword/dwordx2 load expectations and the store model to the aligned-down view (stores below the view base stay clipped by the copy-back window; later byte/short stores overwrite overlaps). Passes at base misalignments 1, 2 and 3 for loads, stores, `dwordx2` GLC, scalar, atomic, formatted and fault cases. - `agc_driver_buffer_descriptor_runtime` — the `base +1/+2/+3` and `records 64 base +2` cases now expect the aligned-down view; the scalar sections were already aligned and are unchanged. Passes for specialized and registration modules, plain and coherent. - Regression: `agc_driver_buffer_load_store`, `_draw_pipeline`, `agc_driver_buffer_atomics`, `_float`, `_inc_dec64`, `_or_swap64`, `agc_driver_buffer_atomic_csub`, `agc_driver_buffer_atomic_unaligned`, `agc_driver_buffer_integer_atomics`, `agc_driver_buffer_format`, `agc_driver_masked_global_store`, `agc_driver_scalar_wide_loads`, `agc_driver_sparse_buffer_holes`, `agc_driver_many_buffers`, `agc_driver_scalar_buffer_runtime_word3`, `agc_driver_formatted_store_unaligned`, `agc_driver_null_buffer_substitutes`, `agc_driver_pattern_buffer_fill`, `agc_driver_pooled_buffer_range` — all pass. ### 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) - [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 3070 (driver 617.42), Windows 11 — AGC / shader recompiler execution tests above - [x] AI-assisted: yes
milescoviello commented 2026-11-06 16:44:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-06T16:44:30Z.

Review: COMMENTED


Read through the change against SpirvRuntimeBuffer.cpp on main. Two points, neither about the measured cases, which the new expectations match.

  1. Base and offset are aligned separately. byteAddress masks the offset (byte & ~3), and the base's low bits are dropped either by forcing misalignment to 0 or, on the BDA path, by resource.base & ~3. So the dword read is (base & ~3) + (offset & ~3). Section 8.1.7 of the RDNA2 ISA, which the unaligned-atomics entry next to this one cites for mode DWORD, aligns the final address: that entry's PS5 measurement has the atomic at byte 0x0D act on the dword that holds it, 0x0C. If loads and stores follow the same rule, the dword is (base + offset) & ~3. The two only differ when (base & 3) + (offset & 3) >= 4. For example, a base 3 bytes past a dword with voffset 1 gives the next dword by the final-address rule and the current one here. The two PS5 cases (base +1 / offset 0, and an aligned base / voffset 1) can't tell them apart, so a base +3 / voffset 1 run on the console would settle it. Until then the debt entry could say which of the two this models.
  2. Stores below the V# base. With a base off a dword, the aligned-down dword of the first store starts before the base. The updated BufferUnalignedBase expects those leading bytes to keep their old value (clipped by the copy-back window), while mode DWORD would write the whole dword. That's a reasonable limit, but the debt entry doesn't mention it yet; one sentence there would keep the entry and the test in step.

No build or run from me yet.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5803677708 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16415#pullrequestreview-5803677708). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-06T16:44:30Z. **Review:** COMMENTED --- Read through the change against `SpirvRuntimeBuffer.cpp` on `main`. Two points, neither about the measured cases, which the new expectations match. 1. **Base and offset are aligned separately.** `byteAddress` masks the offset (`byte & ~3`), and the base's low bits are dropped either by forcing `misalignment` to 0 or, on the BDA path, by `resource.base & ~3`. So the dword read is `(base & ~3) + (offset & ~3)`. Section 8.1.7 of the RDNA2 ISA, which the unaligned-atomics entry next to this one cites for mode DWORD, aligns the final address: that entry's PS5 measurement has the atomic at byte 0x0D act on the dword that holds it, 0x0C. If loads and stores follow the same rule, the dword is `(base + offset) & ~3`. The two only differ when `(base & 3) + (offset & 3) >= 4`. For example, a base 3 bytes past a dword with `voffset` 1 gives the next dword by the final-address rule and the current one here. The two PS5 cases (base +1 / offset 0, and an aligned base / `voffset` 1) can't tell them apart, so a base +3 / `voffset` 1 run on the console would settle it. Until then the debt entry could say which of the two this models. 2. **Stores below the V# base.** With a base off a dword, the aligned-down dword of the first store starts before the base. The updated `BufferUnalignedBase` expects those leading bytes to keep their old value (clipped by the copy-back window), while mode DWORD would write the whole dword. That's a reasonable limit, but the debt entry doesn't mention it yet; one sentence there would keep the entry and the test in step. No build or run from me yet. AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-16 21:26:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T21:26:53Z.

Review: COMMENTED


Reviewed — comments

The changed code implements separate alignment of the buffer base and byte offset, while the review materials say console measurements do not distinguish that behavior from aligning their sum.

What I ran

Reviewed head 991eef4ef25bacc1fe6ce37d01b98181ecdda7a3. I read the prepared description, commits, manifest, diff, and targeted ranges from each of the six changed files. I did not run commands or tests.

Results

Check Result Note
Diff and description passed Reviewed prepared materials
Targeted code review passed Relevant changed code inspected
Tests not run No local execution
Negative control not run Not performed

Findings

Blocking

None.

Non-blocking

  • SpirvRuntimeBuffer.cpp:225 aligns the computed byte offset, and SpirvRuntimeBuffer.cpp:242 separately aligns the BDA base. As the updated debt entry describes, these produce a different address from aligning the final sum when both the base and offset have nonzero low bits. The cited console measurements cover each being misaligned separately, but do not resolve the combined case. Please keep this behavior marked as an unverified interpretation until a console measurement or other authoritative evidence establishes which rule applies.

Not verified

The tests described in the PR description were not run here. Only targeted ranges were read; the remainder of the six changed files was not inspected. files.json could not be read from the review folder, so any exclusions it records are not verified. Console behavior for the combined misalignment case is not verified.

Files read: description.md, commits.md, manifest.json, diff.patch; targeted ranges in GuardedMemoryLoads.cpp, ShaderDiskCache.cpp, BufferDescriptorRuntime.cpp, BufferUnalignedBase.cpp, SpirvRuntimeBuffer.cpp, and shader.md. No changed files were excluded by the manifest.

To get this to a good state

Nothing is required. Optional: add a console measurement for a base and offset that are both misaligned, then update the implementation and debt note to match the measured behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5928016878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16415#pullrequestreview-5928016878). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T21:26:53Z. **Review:** COMMENTED --- # Reviewed — comments The changed code implements separate alignment of the buffer base and byte offset, while the review materials say console measurements do not distinguish that behavior from aligning their sum. ## What I ran Reviewed head `991eef4ef25bacc1fe6ce37d01b98181ecdda7a3`. I read the prepared description, commits, manifest, diff, and targeted ranges from each of the six changed files. I did not run commands or tests. ## Results | Check | Result | Note | |---|---|---| | Diff and description | passed | Reviewed prepared materials | | Targeted code review | passed | Relevant changed code inspected | | Tests | not run | No local execution | | Negative control | not run | Not performed | ## Findings ### Blocking None. ### Non-blocking - `SpirvRuntimeBuffer.cpp:225` aligns the computed byte offset, and `SpirvRuntimeBuffer.cpp:242` separately aligns the BDA base. As the updated debt entry describes, these produce a different address from aligning the final sum when both the base and offset have nonzero low bits. The cited console measurements cover each being misaligned separately, but do not resolve the combined case. Please keep this behavior marked as an unverified interpretation until a console measurement or other authoritative evidence establishes which rule applies. ## Not verified The tests described in the PR description were not run here. Only targeted ranges were read; the remainder of the six changed files was not inspected. `files.json` could not be read from the review folder, so any exclusions it records are not verified. Console behavior for the combined misalignment case is not verified. Files read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`; targeted ranges in `GuardedMemoryLoads.cpp`, `ShaderDiskCache.cpp`, `BufferDescriptorRuntime.cpp`, `BufferUnalignedBase.cpp`, `SpirvRuntimeBuffer.cpp`, and `shader.md`. No changed files were excluded by the manifest. ## To get this to a good state Nothing is required. Optional: add a console measurement for a base and offset that are both misaligned, then update the implementation and debt note to match the measured behavior.
ligmaSec commented 2026-12-03 16:47:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:47:58Z.

Review: COMMENTED


On Linux/lavapipe, agc_driver_buffer_unaligned_base passed on main at ba0ff896612b and failed with this head integrated. In the first view +2, stores +1 run, byte 64 is 0x08 and the fixture expects 0xbe. The two buffer-descriptor and scalar controls passed on both sides. All six Vulkan invocations loaded Khronos validation 1.4.369 with Vulkan required and shader caching disabled. The logs show no missing-layer warnings or VUIDs.

The expected-byte loop prioritizes the upper byte of lane 7's unaligned short store at byte 64. After dword alignment, lane 8's dword store also covers byte 64 and can write 0x08 there. These are different lanes. Please give the raw dword and subword stores non-overlapping destinations so the fixture can check each addressing rule independently.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6174271869 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16415#pullrequestreview-6174271869). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T16:47:58Z. **Review:** COMMENTED --- On Linux/lavapipe, `agc_driver_buffer_unaligned_base` passed on main at `ba0ff896612b` and failed with this head integrated. In the first `view +2, stores +1` run, byte 64 is `0x08` and the fixture expects `0xbe`. The two buffer-descriptor and scalar controls passed on both sides. All six Vulkan invocations loaded Khronos validation 1.4.369 with Vulkan required and shader caching disabled. The logs show no missing-layer warnings or VUIDs. The expected-byte loop prioritizes the upper byte of lane 7's unaligned short store at byte 64. After dword alignment, lane 8's dword store also covers byte 64 and can write `0x08` there. These are different lanes. Please give the raw dword and subword stores non-overlapping destinations so the fixture can check each addressing rule independently. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/16415:upstream-pr/16415'
git switch 'upstream-pr/16415'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/16415'
git switch 'upstream-pr/16415'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/16415'
git switch 'upstream-pr/16415'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/16415'
git switch 'main'
git merge --squash 'upstream-pr/16415'
git switch 'main'
git merge --ff-only 'upstream-pr/16415'
git switch 'main'
git merge 'upstream-pr/16415'
git push origin 'main'
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!16415
No description provided.