fix(agc): fetch zero-stride vertex buffers shorter than their element #1426

Closed
gjonn wants to merge 6 commits from upstream-pr/1426 into main
gjonn commented 2026-10-08 04:33:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-08T04:33:07Z.


What

A vertex attribute whose buffer descriptor has stride 0 and NUM_RECORDS smaller than one element made the draw throw ("vertex fetch exceeds descriptor byte range"). Titles bind such placeholders, for example a one-byte descriptor read as a 32_32_32_32 float attribute.

With stride 0 every vertex fetches the same element. What the fetch returns depends on the descriptor's OOB_SELECT field (word 3 bits 29:28):

  • OOB_SELECT 2 (disabled): the only range check is NUM_RECORDS == 0, so the whole element is read (zeros when the buffer is empty).
  • Other values: the range check is in bytes, and each dword that does not lie wholly below NUM_RECORDS reads as zero.

The field values and checks follow Mesa (src/amd/registers/gfx10-rsrc.json, the ac_set_buf_desc_word3 comment in src/amd/common/ac_descriptors.c). The per-dword zero rule for range-checked buffers has no exact source and was not measured on hardware. It is listed under "Unknown function info" in TechnicalDebt with the Mesa comments that come closest.

  • ShortRawVertexBufferBytes (VertexInput.hpp) tells whether an attribute is such a buffer, and returns how many of its bytes are read: the whole element for OOB_SELECT 2, otherwise the whole in-range dwords.
  • The vertex input step in Draw gives such an attribute its own buffer at offset 0 instead of adding it to the guest copy plan. The bytes it reads go through CopyDrawInput (pending GPU writes flushed, writes collected, snapshot reused) and the render-target alias check, as planned vertex copies do. A buffer that is read whole is bound directly. Otherwise the bytes are placed in a zero-filled one-element buffer. This also applies to indirect draws, which would otherwise copy the descriptor's whole (too short) range.
  • Other attributes, including zero-stride buffers that hold a whole element, are planned and copied as before. VertexBufferReadSize still refuses a short zero-stride buffer if it is ever asked.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, RTX 5090 Laptop, merged with current main. ctest -R "^agc_driver_(graphics|short_vertex_buffer|vertex_fetch|scalar_swappc_fetch|mesh)$" -j1 gives 5/5 passed.
  • New agc_driver_short_vertex_buffer (tests/execution/ShortVertexBuffer.cpp): a fetch-shader vertex program stores both of its attributes to a buffer. One is a stride-0 32_32_32_32 float attribute shorter than its element, bound first. The other is a strided position. Each draw checks both values per vertex, so a wrong buffer, offset or fetchOf index fails:
    • range checked: 8 records give the first two dwords and then zeros; 6 records give one dword; 1 record gives zeros
    • OOB_SELECT 2: 1 record gives the whole element; 0 records give zeros
    • the same range-checked and OOB_SELECT 2 cases as indirect draws
    • a short buffer over the render target is refused with "vertex buffer aliases the render target"
  • Mutations to check that the test catches breakage: dropping the OOB_SELECT 2 case, the zero fill, or the alias check each fail it. Indexing the plan by attribute instead of fetchOf loses the device.
  • agc_driver_graphics also checks ShortRawVertexBufferBytes for a 16-byte attribute with stride 0: 16 records is not short, 8 keep 8 bytes, 1 keeps none, and with OOB_SELECT 2, 1 record keeps 16 and 0 keep none.
  • Not run on Linux by me.
  • Supporting context, not reproducible here: PPSA01317 renders all six faces of a cube target in one layered draw. Its vertex shader takes gl_Layer from the w component of a 32_32_32_32 float attribute read from a stride-0 buffer with NUM_RECORDS 1 and OOB_SELECT 2. With a zero fill every primitive went to face 0, faces 1-5 stayed empty, and the reflection probe filtered from that cube was black. Before this PR, the same title skipped about 31 000 draws in 120 s on these descriptors.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4781819547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1426). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-08T04:33:07Z. --- ### What A vertex attribute whose buffer descriptor has stride 0 and `NUM_RECORDS` smaller than one element made the draw throw ("vertex fetch exceeds descriptor byte range"). Titles bind such placeholders, for example a one-byte descriptor read as a 32_32_32_32 float attribute. With stride 0 every vertex fetches the same element. What the fetch returns depends on the descriptor's `OOB_SELECT` field (word 3 bits 29:28): - `OOB_SELECT` 2 (disabled): the only range check is `NUM_RECORDS == 0`, so the whole element is read (zeros when the buffer is empty). - Other values: the range check is in bytes, and each dword that does not lie wholly below `NUM_RECORDS` reads as zero. The field values and checks follow Mesa (`src/amd/registers/gfx10-rsrc.json`, the `ac_set_buf_desc_word3` comment in `src/amd/common/ac_descriptors.c`). The per-dword zero rule for range-checked buffers has no exact source and was not measured on hardware. It is listed under "Unknown function info" in TechnicalDebt with the Mesa comments that come closest. - `ShortRawVertexBufferBytes` (`VertexInput.hpp`) tells whether an attribute is such a buffer, and returns how many of its bytes are read: the whole element for `OOB_SELECT` 2, otherwise the whole in-range dwords. - The vertex input step in `Draw` gives such an attribute its own buffer at offset 0 instead of adding it to the guest copy plan. The bytes it reads go through `CopyDrawInput` (pending GPU writes flushed, writes collected, snapshot reused) and the render-target alias check, as planned vertex copies do. A buffer that is read whole is bound directly. Otherwise the bytes are placed in a zero-filled one-element buffer. This also applies to indirect draws, which would otherwise copy the descriptor's whole (too short) range. - Other attributes, including zero-stride buffers that hold a whole element, are planned and copied as before. `VertexBufferReadSize` still refuses a short zero-stride buffer if it is ever asked. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0, RTX 5090 Laptop, merged with current `main`. `ctest -R "^agc_driver_(graphics|short_vertex_buffer|vertex_fetch|scalar_swappc_fetch|mesh)$" -j1` gives 5/5 passed. - New `agc_driver_short_vertex_buffer` (`tests/execution/ShortVertexBuffer.cpp`): a fetch-shader vertex program stores both of its attributes to a buffer. One is a stride-0 32_32_32_32 float attribute shorter than its element, bound first. The other is a strided position. Each draw checks both values per vertex, so a wrong buffer, offset or `fetchOf` index fails: - range checked: 8 records give the first two dwords and then zeros; 6 records give one dword; 1 record gives zeros - `OOB_SELECT` 2: 1 record gives the whole element; 0 records give zeros - the same range-checked and `OOB_SELECT` 2 cases as indirect draws - a short buffer over the render target is refused with "vertex buffer aliases the render target" - Mutations to check that the test catches breakage: dropping the `OOB_SELECT` 2 case, the zero fill, or the alias check each fail it. Indexing the plan by attribute instead of `fetchOf` loses the device. - `agc_driver_graphics` also checks `ShortRawVertexBufferBytes` for a 16-byte attribute with stride 0: 16 records is not short, 8 keep 8 bytes, 1 keeps none, and with `OOB_SELECT` 2, 1 record keeps 16 and 0 keep none. - Not run on Linux by me. - Supporting context, not reproducible here: PPSA01317 renders all six faces of a cube target in one layered draw. Its vertex shader takes `gl_Layer` from the w component of a 32_32_32_32 float attribute read from a stride-0 buffer with `NUM_RECORDS` 1 and `OOB_SELECT` 2. With a zero fill every primitive went to face 0, faces 1-5 stayed empty, and the reflection probe filtered from that cube was black. Before this PR, the same title skipped about 31 000 draws in 120 s on these descriptors. ### 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 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
adriadam10 commented 2026-10-08 13:28:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:28:34Z.

Review: CHANGES_REQUESTED


Thanks, the idea (a zero-stride descriptor is a raw buffer, out-of-range dwords read as zero) is reasonable. Needs a few changes:

  • Draw.cpp:885-891, 906-913: the new path in prepareDrawInputs has no test. agc_driver_graphics only pins ShortRawVertexBufferBytes; breaking the buffer/offset/handle wiring leaves it green. Please add a Linux test that draws with a short zero-stride attribute and checks the fetched values (in-range dwords, then zeros), including an indirect draw and a mix with a normal attribute (checks fetchOf).
  • Draw.cpp:888: the regular copy calls GuestMemory::FlushGpuWrites / CollectWrites before Read (Draw.cpp:735-741); this path reads guest memory without flushing pending GPU writes. Same for the render-target alias check at Draw.cpp:894, skipped here.
  • VertexInput.hpp:136-141: say where "dword-granular, partial dword reads as zero" comes from (measured on HW or a cited source) and add a TechnicalDebt line under "Unknown function info" for it.
  • The real-title numbers in "Tested" can't be reproduced here, so the Linux test above is what we need as evidence.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5457333854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1426#pullrequestreview-5457333854). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T13:28:34Z. **Review:** CHANGES_REQUESTED --- Thanks, the idea (a zero-stride descriptor is a raw buffer, out-of-range dwords read as zero) is reasonable. Needs a few changes: - `Draw.cpp:885-891`, `906-913`: the new path in `prepareDrawInputs` has no test. `agc_driver_graphics` only pins `ShortRawVertexBufferBytes`; breaking the buffer/offset/handle wiring leaves it green. Please add a Linux test that draws with a short zero-stride attribute and checks the fetched values (in-range dwords, then zeros), including an indirect draw and a mix with a normal attribute (checks `fetchOf`). - `Draw.cpp:888`: the regular copy calls `GuestMemory::FlushGpuWrites` / `CollectWrites` before `Read` (`Draw.cpp:735-741`); this path reads guest memory without flushing pending GPU writes. Same for the render-target alias check at `Draw.cpp:894`, skipped here. - `VertexInput.hpp:136-141`: say where "dword-granular, partial dword reads as zero" comes from (measured on HW or a cited source) and add a TechnicalDebt line under "Unknown function info" for it. - The real-title numbers in "Tested" can't be reproduced here, so the Linux test above is what we need as evidence.
fisherjoey commented 2026-10-08 17:49:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T17:49:38Z.


Linux results, since this was tested on Windows only. Everything passes here, including agc_driver_graphics with the new zero-stride checks.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main 29e533ac.

main 29e533ac main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 183 / 183 183 / 183
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 183 / 183 183 / 183
CPU tests (-E agc_driver) 279 / 280 279 / 280

The one CPU failure is guest_dynamic_loader (subprocess aborted), and it fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065786842 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1426#issuecomment-6065786842). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T17:49:38Z. --- Linux results, since this was tested on Windows only. Everything passes here, including `agc_driver_graphics` with the new zero-stride checks. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` 29e533ac. | | `main` 29e533ac | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 183 / 183 | 183 / 183 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 183 / 183 | 183 / 183 | | CPU tests (`-E agc_driver`) | 279 / 280 | 279 / 280 | The one CPU failure is `guest_dynamic_loader` (subprocess aborted), and it fails the same way on `main`.
adriadam10 commented 2026-10-09 23:29:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:29:01Z.

Review: CHANGES_REQUESTED


Thanks for the test and the CopyDrawInput wiring, those address the previous review. I measured the stride-0 fetch on real RDNA2 hardware (gfx1035) with buffer_load_format_xyzw and tbuffer_load_format_xyzw ... idxen, using the same word 3 as the test (0x0104dfac | OOB_SELECT << 28, 32_32_32_32_FLOAT), NUM_RECORDS 0..32 and indices 0..2. Both instructions give the same results:

OOB_SELECT stride 0 result
0 always zero, even with 32 records (the offset >= stride check always fails)
1 the whole element when the index is below NUM_RECORDS, otherwise zero (vertex 1 with 1 record reads zero)
2 the whole element unless NUM_RECORDS is 0 (matches this PR)
3 all or nothing: the whole element with NUM_RECORDS >= 16, zero for 1..15. No partial dwords

So:

  • VertexInput.hpp:144: records & ~3 doesn't match the hardware for any value. With 3, and with 0, a short buffer reads all zeros. With 1 the result depends on the vertex index, so a single one-element buffer can't model it. That case should throw unless it's modelled per vertex.
  • ShortVertexBuffer.cpp:161-162,166: the range-checked cases with 8 and 6 records expect {1, 2, 0, 0} and {1, 0, 0, 0}. The hardware returns {0, 0, 0, 0}. Please pin the measured values and add an OOB_SELECT 0 case and an OOB_SELECT 1 case (expecting a throw, or per-vertex values).
  • TechnicalDebt.md:113: please update it once the rule follows these measurements. What isn't measured is the PS5's gfx1013, as opposed to gfx1035, and formats other than 32_32_32_32.
  • #1769 changes the same code (it conflicts in Draw.cpp, tests/Graphics.cpp and TechnicalDebt.md), and its OOB_SELECT 2 rule is the same. Please agree with @TideOPL on which PR carries the fix so only one of them lands.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5476313474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1426#pullrequestreview-5476313474). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T23:29:01Z. **Review:** CHANGES_REQUESTED --- Thanks for the test and the `CopyDrawInput` wiring, those address the previous review. I measured the stride-0 fetch on real RDNA2 hardware (gfx1035) with `buffer_load_format_xyzw` and `tbuffer_load_format_xyzw ... idxen`, using the same word 3 as the test (`0x0104dfac | OOB_SELECT << 28`, 32_32_32_32_FLOAT), `NUM_RECORDS` 0..32 and indices 0..2. Both instructions give the same results: | `OOB_SELECT` | stride 0 result | |---|---| | 0 | always zero, even with 32 records (the `offset >= stride` check always fails) | | 1 | the whole element when the index is below `NUM_RECORDS`, otherwise zero (vertex 1 with 1 record reads zero) | | 2 | the whole element unless `NUM_RECORDS` is 0 (matches this PR) | | 3 | all or nothing: the whole element with `NUM_RECORDS` >= 16, zero for 1..15. No partial dwords | So: - `VertexInput.hpp:144`: `records & ~3` doesn't match the hardware for any value. With 3, and with 0, a short buffer reads all zeros. With 1 the result depends on the vertex index, so a single one-element buffer can't model it. That case should throw unless it's modelled per vertex. - `ShortVertexBuffer.cpp:161-162,166`: the range-checked cases with 8 and 6 records expect `{1, 2, 0, 0}` and `{1, 0, 0, 0}`. The hardware returns `{0, 0, 0, 0}`. Please pin the measured values and add an `OOB_SELECT` 0 case and an `OOB_SELECT` 1 case (expecting a throw, or per-vertex values). - `TechnicalDebt.md:113`: please update it once the rule follows these measurements. What isn't measured is the PS5's gfx1013, as opposed to gfx1035, and formats other than 32_32_32_32. - #1769 changes the same code (it conflicts in `Draw.cpp`, `tests/Graphics.cpp` and `TechnicalDebt.md`), and its `OOB_SELECT` 2 rule is the same. Please agree with @TideOPL on which PR carries the fix so only one of them lands.
oneandonlydean commented 2026-10-11 08:03:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T08:03:27Z.


Closing: #1769 (merged) reads the whole element for a stride-0 OOB_SELECT 2 V#, which is the PPSA01317 case here. The other modes still throw on main; a PR that follows the 10-09 hardware measurements for them is welcome.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106943336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1426#issuecomment-6106943336). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T08:03:27Z. --- Closing: #1769 (merged) reads the whole element for a stride-0 `OOB_SELECT` 2 V#, which is the PPSA01317 case here. The other modes still throw on main; a PR that follows the 10-09 hardware measurements for them is welcome.

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