feat(recompiler): format loads and dword atomics through GPU-selected buffer descriptors #1183

Closed
MichonGoddijn231849 wants to merge 4 commits from upstream-pr/1183 into main
MichonGoddijn231849 commented 2026-10-07 16:44:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-07T16:44:36Z.


What

A buffer access whose V# the shader reads at run time (a waterfall over a descriptor table) already worked for raw dword loads and stores. This adds:

  • Format loads: the element is read through BDA and converted for the FORMAT field of that V#, with a switch over the buffer formats GetFormatInfo knows, then swizzled by its DST_SEL. Any other FORMAT reads zero. The reserved DST_SEL values follow the hardware: 2 reads the output's own component modulo the format's component count, 3 reads zero on multi-component formats and X on single-component ones. Bounds follow the element, not the dword: raw (OOB_SELECT 3) needs offset + element bytes <= num_records, structured needs the whole element inside the stride, and an element out of bounds reads zero as a whole. A typed (tbuffer) load takes its format from the instruction. Format stores and D16 loads are still refused.
  • Dword atomics: a 32-bit buffer atomic through such a V# addresses its element like the loads and stores, runs on the BDA write pointer through the existing EmitBdaAtomic, and notes the write for GPU ownership.

tests/ControlFlow.cpp recompiles a waterfall-style format load and an atomic through a V# read from a table. tests/execution/GpuSelectedBufferFormat.cpp (agc_driver_gpu_selected_buffer_format) runs the measured rows from the review on the device for wave32 and wave64: element bounds, a stride smaller than the element, selectors 0 to 7, a typed load and the dword atomic.

Tested

Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB, MinGW-w64 GCC 15.2.0.

  • GTA V PPSA04264 (01.005) runs with this change on the combined gta-v/testing branch (Performance mode, the prologue).
  • On this branch: builds; agc_driver_gpu_selected_buffer_format passes (44 of its 98 checks fail on the previous version of this PR), and the recompiler and buffer tests pass. GTA V has not been re-run with the bounds and selector changes. Not run here: the relinker's Python tests (my local Python does not find their helper modules) and guest_gameupdate.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4775678103 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1183). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-07T16:44:36Z. --- ### What A buffer access whose V# the shader reads at run time (a waterfall over a descriptor table) already worked for raw dword loads and stores. This adds: - **Format loads:** the element is read through BDA and converted for the FORMAT field of that V#, with a switch over the buffer formats `GetFormatInfo` knows, then swizzled by its DST_SEL. Any other FORMAT reads zero. The reserved DST_SEL values follow the hardware: 2 reads the output's own component modulo the format's component count, 3 reads zero on multi-component formats and X on single-component ones. Bounds follow the element, not the dword: raw (OOB_SELECT 3) needs offset + element bytes <= num_records, structured needs the whole element inside the stride, and an element out of bounds reads zero as a whole. A typed (tbuffer) load takes its format from the instruction. Format stores and D16 loads are still refused. - **Dword atomics:** a 32-bit buffer atomic through such a V# addresses its element like the loads and stores, runs on the BDA write pointer through the existing `EmitBdaAtomic`, and notes the write for GPU ownership. `tests/ControlFlow.cpp` recompiles a waterfall-style format load and an atomic through a V# read from a table. `tests/execution/GpuSelectedBufferFormat.cpp` (`agc_driver_gpu_selected_buffer_format`) runs the measured rows from the review on the device for wave32 and wave64: element bounds, a stride smaller than the element, selectors 0 to 7, a typed load and the dword atomic. ### Tested Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB, MinGW-w64 GCC 15.2.0. - GTA V PPSA04264 (01.005) runs with this change on the combined [`gta-v/testing`](https://github.com/MichonGoddijn231849/AnyPS5-fork/tree/gta-v/testing) branch (Performance mode, the prologue). - On this branch: builds; `agc_driver_gpu_selected_buffer_format` passes (44 of its 98 checks fail on the previous version of this PR), and the recompiler and buffer tests pass. GTA V has not been re-run with the bounds and selector changes. Not run here: the relinker's Python tests (my local Python does not find their helper modules) and `guest_gameupdate`. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-08 08:43:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:43:55Z.

Review: CHANGES_REQUESTED


Thanks. I measured the new runtime path on the HW oracle (gfx1035, buffer_load_format_xyzw with the V# built in SGPRs, one row per launch). Unknown FORMAT values (0, 78..127) do read zero, and the dword atomic's bounds match. Three things don't match, and the tests don't catch them:

  • Raw bounds (OOB_SELECT=3) use the dword check, not the element size. LoadGpuDescriptorFormatted takes inBounds from ForEachGpuDescriptorDword (SpirvMemoryInstructions.cpp:711), whose hasDword test is num_records >= 4 && offset <= num_records - 4. On hardware an element is in bounds iff offset + element bytes <= num_records, and out-of-bounds means the whole element reads 0:

    format offset num_records HW PR
    8_UNORM 2 3 value 0
    8_UNORM 0 1 value 0
    16_UNORM 4 6 value 0
    32_32_UINT 0 4 / 7 0 reads both dwords
    32_32_32_32_UINT 0 8 / 12 / 15 0 reads all 4 dwords
    32_32_32_32_UINT 0 16 value value
  • Structured bounds (OOB_SELECT=0) need the whole element inside the stride. With idxen, 32_32_32_32 at stride 8 and 32_32 at stride 4 read 0 on HW. The PR only checks offset < stride. The typed path uses the same inBounds, so it has both bound issues.

  • Reserved DST_SEL values don't read zero (:738). Hardware gives: selector 2 = the slot's own component, modulo the component count (32_32 with all four selectors at 2 gives x, y, x, y; 32_32_32 gives x, y, z, x). Selector 3 = 0 on multi-component formats, but X on single-component ones (32_UINT, 8_UNORM). A runtime selector can't throw, so the measured values have to be modelled.

  • Tests: the two ControlFlow.cpp cases (:925, :937) only check that the shader recompiles. They pin no values, so none of the above shows up. An execution test (tests/execution/) with a V# read from a table would catch these. It should cover the bound rows above, a stride smaller than the element, selectors 0/1/2/3/4-7 on single- and multi-component formats, a typed load, and the atomic (returned value and memory), run in wave64 too.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5453984232 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1183#pullrequestreview-5453984232). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:43:55Z. **Review:** CHANGES_REQUESTED --- Thanks. I measured the new runtime path on the HW oracle (gfx1035, `buffer_load_format_xyzw` with the V# built in SGPRs, one row per launch). Unknown FORMAT values (0, 78..127) do read zero, and the dword atomic's bounds match. Three things don't match, and the tests don't catch them: - **Raw bounds (OOB_SELECT=3) use the dword check, not the element size.** `LoadGpuDescriptorFormatted` takes `inBounds` from `ForEachGpuDescriptorDword` (`SpirvMemoryInstructions.cpp:711`), whose `hasDword` test is `num_records >= 4 && offset <= num_records - 4`. On hardware an element is in bounds iff `offset + element bytes <= num_records`, and out-of-bounds means the whole element reads 0: | format | offset | num_records | HW | PR | |---|---|---|---|---| | 8_UNORM | 2 | 3 | value | 0 | | 8_UNORM | 0 | 1 | value | 0 | | 16_UNORM | 4 | 6 | value | 0 | | 32_32_UINT | 0 | 4 / 7 | 0 | reads both dwords | | 32_32_32_32_UINT | 0 | 8 / 12 / 15 | 0 | reads all 4 dwords | | 32_32_32_32_UINT | 0 | 16 | value | value | - **Structured bounds (OOB_SELECT=0) need the whole element inside the stride.** With `idxen`, 32_32_32_32 at stride 8 and 32_32 at stride 4 read 0 on HW. The PR only checks `offset < stride`. The typed path uses the same `inBounds`, so it has both bound issues. - **Reserved DST_SEL values don't read zero** (`:738`). Hardware gives: selector 2 = the slot's own component, modulo the component count (32_32 with all four selectors at 2 gives x, y, x, y; 32_32_32 gives x, y, z, x). Selector 3 = 0 on multi-component formats, but X on single-component ones (32_UINT, 8_UNORM). A runtime selector can't throw, so the measured values have to be modelled. - **Tests:** the two `ControlFlow.cpp` cases (`:925`, `:937`) only check that the shader recompiles. They pin no values, so none of the above shows up. An execution test (`tests/execution/`) with a V# read from a table would catch these. It should cover the bound rows above, a stride smaller than the element, selectors 0/1/2/3/4-7 on single- and multi-component formats, a typed load, and the atomic (returned value and memory), run in wave64 too.
Celegans12 commented 2026-10-08 09:11:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-08T09:11:28Z.


I ran Ghost of Yōtei (PPSA26344 v1.008) with this PR at 5a7cd643e, and it gets one compute program past its runtime V#, but that program then compiles to a SPIR-V module so big that the NVIDIA driver ran the machine out of memory building its pipeline.

The program is 0x801cdd2700, 6,596 code words. Without the PR it was skipped with GetBufferResource dword 0 is not a valid runtime value at pc 0x234. With it, the recompile succeeds in 248 ms but gives 13,415,426 SPIR-V words. vkCreateComputePipelines started on it about 386 s into the run and never returned: the game was at 58 GB private bytes in our last memory sample, Windows then logged a low-virtual-memory event naming eboot.exe at 73 GB of commit, and the game exited with code 3 at 910 s. Building the same pipeline on its own from the captured request reached 76 GB after about 12 minutes before I stopped it.

As far as I can tell the size comes from LoadGpuDescriptorFormatted. For an untyped format load it emits an OpSwitch with one arm per known buffer format, each with its own conversion, at every load site, so the module grows with loads times formats. Would you consider emitting that conversion once, as a SPIR-V function that each load calls with descriptor word 3 and the raw dwords? I'm happy to test a new head on this program, and I can send you the captured recompile request (73 KB) if you want to reproduce it.

The rest looks good so far: another compute program (0x8000333c00) now gets past its formatted load at pc 0x2e8 and stops at the next unresolved V#, at pc 0x650. To let our hour-long run finish I reverted this PR there, and 0x801cdd2700 went back to being skipped.

Details

Windows 11, RTX 3080 Laptop (16 GB), 32 GB RAM. Build: our integration tree on main c8a2f0632 with this PR at 5a7cd643e (plus our open PRs and env-gated probes).

[capture] compute variant 0b9ea3d6bff68940: program 0x801cdd2700, 13415426 SPIR-V words, recompile 248 ms (564 new this run)
[gpu] compute shader 0x801cdd2700 recompile took 248 ms (13415426 SPIR-V words, 14 captured regions, total 1.2 s)
[dispatch] creating a pipeline for 13415426 SPIR-V words (program 0x801cdd2700)
[watchdog] 396141.9 queue 0x20 serial 4878 offset 3477 DISPATCH_DIRECT shader 0x801cdd2700 color 0x0 stuck 10 s (packetsDone 1656608 inFlight 2)
[watchdog] 506176.0 queue 0x20 serial 4878 offset 3477 DISPATCH_DIRECT shader 0x801cdd2700 color 0x0 stuck 120 s (packetsDone 1656608 inFlight 2)

Windows System log, event 2004 at 08:07:57: "eboot.exe (10948) consumed 73118875648 bytes". The game exited with code 3 at 910 s.

Without the PR (previous run):

[fmt-probe] TOLERATED OTHER dispatch error #18 (...): compute shader 0x801cdd2700 (shader_801cdd2700.req): GetBufferResource dword 0 is not a valid runtime value; chain: ReadConstBuffer@pc=0x00000234(GetBufferResource(GetUserData(Void), GetUserData(Void), GetUserData(Void), GetUserData(Void)), ...

0x8000333c00: GetBufferResource dword 0 ... ReadConstBuffer@pc=0x000002e8 without the PR, ReadConstBuffer@pc=0x00000650(GetBufferResource(ReadConst(GetSrtResource, Immediate), ... with it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056606399 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1183#issuecomment-6056606399). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-08T09:11:28Z. --- I ran Ghost of Yōtei (PPSA26344 v1.008) with this PR at 5a7cd643e, and it gets one compute program past its runtime V#, but that program then compiles to a SPIR-V module so big that the NVIDIA driver ran the machine out of memory building its pipeline. The program is 0x801cdd2700, 6,596 code words. Without the PR it was skipped with `GetBufferResource dword 0 is not a valid runtime value` at pc 0x234. With it, the recompile succeeds in 248 ms but gives 13,415,426 SPIR-V words. `vkCreateComputePipelines` started on it about 386 s into the run and never returned: the game was at 58 GB private bytes in our last memory sample, Windows then logged a low-virtual-memory event naming eboot.exe at 73 GB of commit, and the game exited with code 3 at 910 s. Building the same pipeline on its own from the captured request reached 76 GB after about 12 minutes before I stopped it. As far as I can tell the size comes from `LoadGpuDescriptorFormatted`. For an untyped format load it emits an `OpSwitch` with one arm per known buffer format, each with its own conversion, at every load site, so the module grows with loads times formats. Would you consider emitting that conversion once, as a SPIR-V function that each load calls with descriptor word 3 and the raw dwords? I'm happy to test a new head on this program, and I can send you the captured recompile request (73 KB) if you want to reproduce it. The rest looks good so far: another compute program (0x8000333c00) now gets past its formatted load at pc 0x2e8 and stops at the next unresolved V#, at pc 0x650. To let our hour-long run finish I reverted this PR there, and 0x801cdd2700 went back to being skipped. <details><summary>Details</summary> Windows 11, RTX 3080 Laptop (16 GB), 32 GB RAM. Build: our integration tree on main c8a2f0632 with this PR at 5a7cd643e (plus our open PRs and env-gated probes). ``` [capture] compute variant 0b9ea3d6bff68940: program 0x801cdd2700, 13415426 SPIR-V words, recompile 248 ms (564 new this run) [gpu] compute shader 0x801cdd2700 recompile took 248 ms (13415426 SPIR-V words, 14 captured regions, total 1.2 s) [dispatch] creating a pipeline for 13415426 SPIR-V words (program 0x801cdd2700) [watchdog] 396141.9 queue 0x20 serial 4878 offset 3477 DISPATCH_DIRECT shader 0x801cdd2700 color 0x0 stuck 10 s (packetsDone 1656608 inFlight 2) [watchdog] 506176.0 queue 0x20 serial 4878 offset 3477 DISPATCH_DIRECT shader 0x801cdd2700 color 0x0 stuck 120 s (packetsDone 1656608 inFlight 2) ``` Windows System log, event 2004 at 08:07:57: "eboot.exe (10948) consumed 73118875648 bytes". The game exited with code 3 at 910 s. Without the PR (previous run): ``` [fmt-probe] TOLERATED OTHER dispatch error #18 (...): compute shader 0x801cdd2700 (shader_801cdd2700.req): GetBufferResource dword 0 is not a valid runtime value; chain: ReadConstBuffer@pc=0x00000234(GetBufferResource(GetUserData(Void), GetUserData(Void), GetUserData(Void), GetUserData(Void)), ... ``` 0x8000333c00: `GetBufferResource dword 0 ... ReadConstBuffer@pc=0x000002e8` without the PR, `ReadConstBuffer@pc=0x00000650(GetBufferResource(ReadConst(GetSrtResource, Immediate), ...` with it. </details>
MichonGoddijn231849 commented 2026-10-08 15:58:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-08T15:58:21Z.


Thanks, the measured tables made this straightforward. Rebased onto main, and in this push:

  • Raw bounds (OOB_SELECT 3): an element is in bounds only when offset + its bytes <= num_records, with the size from the V#'s format. Out of bounds the whole element reads zero, matching your 8_UNORM, 16_UNORM, 32_32 and 32_32_32_32 rows.
  • Structured (idxen): the whole element must fit in the stride. Typed loads use the same check with the instruction's format.
  • Reserved DST_SEL: 2 reads the output's own component modulo the component count; 3 reads zero on multi-component formats and X on single-component ones.
  • Tests: tests/execution/GpuSelectedBufferFormat.cpp (agc_driver_gpu_selected_buffer_format) dispatches each row on the device for wave32 and wave64: your table, a stride smaller than the element, selectors 0 to 7 on single- and multi-component formats, a typed load, and the dword atomic (returned value and memory). On the previous version 44 of its 98 checks fail; with this change all pass.
  • Not measured, listed in TechnicalDebt: selectors 5 to 7 past the component count, constant selectors on an out-of-bounds element, the whole-element structured rule applied to dword loads and dword atomics (separable if your oracle disagrees), and the typed FORMAT gate. If any row differs on your hardware, tell me which and I'll change it.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6063858320 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1183#issuecomment-6063858320). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-08T15:58:21Z. --- Thanks, the measured tables made this straightforward. Rebased onto main, and in this push: - Raw bounds (OOB_SELECT 3): an element is in bounds only when offset + its bytes <= num_records, with the size from the V#'s format. Out of bounds the whole element reads zero, matching your 8_UNORM, 16_UNORM, 32_32 and 32_32_32_32 rows. - Structured (idxen): the whole element must fit in the stride. Typed loads use the same check with the instruction's format. - Reserved DST_SEL: 2 reads the output's own component modulo the component count; 3 reads zero on multi-component formats and X on single-component ones. - Tests: `tests/execution/GpuSelectedBufferFormat.cpp` (`agc_driver_gpu_selected_buffer_format`) dispatches each row on the device for wave32 and wave64: your table, a stride smaller than the element, selectors 0 to 7 on single- and multi-component formats, a typed load, and the dword atomic (returned value and memory). On the previous version 44 of its 98 checks fail; with this change all pass. - Not measured, listed in TechnicalDebt: selectors 5 to 7 past the component count, constant selectors on an out-of-bounds element, the whole-element structured rule applied to dword loads and dword atomics (separable if your oracle disagrees), and the typed FORMAT gate. If any row differs on your hardware, tell me which and I'll change it.
MichonGoddijn231849 commented 2026-10-09 08:32:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-09T08:32:04Z.


Closing: superseded by #639. Buffer accesses now go through runtime descriptors (SpirvRuntimeBuffer.cpp decodes the format at runtime), which covers the format loads and dword atomics through GPU-selected V#s this PR added.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077377410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1183#issuecomment-6077377410). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-09T08:32:04Z. --- Closing: superseded by #639. Buffer accesses now go through runtime descriptors (`SpirvRuntimeBuffer.cpp` decodes the format at runtime), which covers the format loads and dword atomics through GPU-selected V#s this PR added.

Pull request closed

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!1183
No description provided.