fix(agc): leave the primitive restart index out of an indexed draw's vertex range #1968

Merged
lubaxe merged 2 commits from upstream-pr/1968 into main 2026-10-10 04:43:21 +00:00
lubaxe commented 2026-10-09 03:25:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by lubaxe on 2026-10-09T03:25:55Z.


What

An indexed draw sizes its vertex buffer copies from the highest index in its index buffer. That scan counted the primitive restart index (all ones) as a vertex. A 16-bit strip draw with restart in PPSA12544 then needed vertex 65535 of a 4957-record buffer, and the draw threw vertex fetch exceeds descriptor record count.

  • The scan is now HighestDrawIndex in VertexInput.hpp. When the draw's pipeline enables restart, it skips the restart index. The condition is the same as primitiveRestartEnable in Pipeline.cpp (primitiveRestart and, for list topologies, primitiveListRestart).
  • Triangle-fan geometry draws keep counting it, so the existing "primitive restart in a triangle fan geometry draw is unsupported" check still sees it.
  • The highest index is cached with the index buffer snapshot. Restart draws use new SnapshotUse::Index16Restart and Index32Restart keys, so a buffer drawn with and without restart never shares the cached value.
  • The scan covers only the draw's index bytes, as before, not the pooled copy's whole size.

Tested

  • Windows 11 Pro, RTX 5090, WinLibs GCC 15.2.0 r7, Release.
  • New highestDrawIndexTests in agc_driver_graphics: 16- and 32-bit indices, with and without restart, a draw of only restart indices, and a range shorter than the data. Without the skip it fails with a 16-bit restart index counted as a vertex.
  • An all-restart draw (only restart indices) is skipped as nothing to draw. Covered end to end in agc_driver_scalar_swappc_fetch: an indexed strip of six restart indices over a vertex buffer with 0 records draws nothing and runs no vertex. With the old behaviour it fails with vertex fetch exceeds descriptor record count. (Added in edad2a16 after review; rebased onto main d70b8998.)
  • ctest: 487 of 488 passed. The failure is agc_driver_pixel_lds, which also fails on unmodified main on this GPU (vkWaitForFences graphics: Vulkan result -4).
  • agc_driver_recorder_tests, run directly: passed.
  • PPSA12544 (Hollow Knight: Silksong) with #846 and #425: the draw that threw now runs, and the title reaches tut_01.
  • tools/check_conventions.py --base origin/main passes.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4795586656 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1968). Originally posted by [lubaxe](https://github.com/lubaxe) on 2026-10-09T03:25:55Z. --- ### What An indexed draw sizes its vertex buffer copies from the highest index in its index buffer. That scan counted the primitive restart index (all ones) as a vertex. A 16-bit strip draw with restart in PPSA12544 then needed vertex 65535 of a 4957-record buffer, and the draw threw `vertex fetch exceeds descriptor record count`. - The scan is now `HighestDrawIndex` in `VertexInput.hpp`. When the draw's pipeline enables restart, it skips the restart index. The condition is the same as `primitiveRestartEnable` in `Pipeline.cpp` (`primitiveRestart` and, for list topologies, `primitiveListRestart`). - Triangle-fan geometry draws keep counting it, so the existing "primitive restart in a triangle fan geometry draw is unsupported" check still sees it. - The highest index is cached with the index buffer snapshot. Restart draws use new `SnapshotUse::Index16Restart` and `Index32Restart` keys, so a buffer drawn with and without restart never shares the cached value. - The scan covers only the draw's index bytes, as before, not the pooled copy's whole size. ### Tested - Windows 11 Pro, RTX 5090, WinLibs GCC 15.2.0 r7, Release. - New `highestDrawIndexTests` in `agc_driver_graphics`: 16- and 32-bit indices, with and without restart, a draw of only restart indices, and a range shorter than the data. Without the skip it fails with `a 16-bit restart index counted as a vertex`. - An all-restart draw (only restart indices) is skipped as nothing to draw. Covered end to end in `agc_driver_scalar_swappc_fetch`: an indexed strip of six restart indices over a vertex buffer with 0 records draws nothing and runs no vertex. With the old behaviour it fails with `vertex fetch exceeds descriptor record count`. (Added in edad2a16 after review; rebased onto `main` d70b8998.) - `ctest`: 487 of 488 passed. The failure is `agc_driver_pixel_lds`, which also fails on unmodified `main` on this GPU (`vkWaitForFences graphics: Vulkan result -4`). - `agc_driver_recorder_tests`, run directly: passed. - PPSA12544 (Hollow Knight: Silksong) with #846 and #425: the draw that threw now runs, and the title reaches `tut_01`. - `tools/check_conventions.py --base origin/main` passes. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-09 03:43:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T03:43:49Z.

Review: COMMENTED


The all-restart helper case still flows into vertex-input preparation with inputs.maxIndex == 0. VertexBufferReadSize then validates and copies vertex record 0 for every per-vertex attribute, so an indexed draw containing only restart markers can still fail on an empty/out-of-range vertex descriptor or read data that the draw never references. Could you track whether the range contains any non-restart index and skip per-vertex reads for an all-restart draw, with an end-to-end draw test? The current restartOnly assertion exercises HighestDrawIndex alone and does not cover this downstream path.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5465488493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1968#pullrequestreview-5465488493). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T03:43:49Z. **Review:** COMMENTED --- The all-restart helper case still flows into vertex-input preparation with `inputs.maxIndex == 0`. `VertexBufferReadSize` then validates and copies vertex record 0 for every per-vertex attribute, so an indexed draw containing only restart markers can still fail on an empty/out-of-range vertex descriptor or read data that the draw never references. Could you track whether the range contains any non-restart index and skip per-vertex reads for an all-restart draw, with an end-to-end draw test? The current `restartOnly` assertion exercises `HighestDrawIndex` alone and does not cover this downstream path.
fisherjoey commented 2026-10-09 04:36:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T04:36:43Z.


Linux results, since this was tested on Windows only. Everything passes here.

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

main f1451396 main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 207 / 207 207 / 207
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 207 / 207 207 / 207
CPU tests (-E agc_driver) 286 / 287 286 / 287

The CPU failure is guest_dynamic_loader, which fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074354064 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1968#issuecomment-6074354064). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T04:36:43Z. --- Linux results, since this was tested on Windows only. Everything passes here. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` f1451396 cleanly. | | `main` f1451396 | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 207 / 207 | 207 / 207 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 207 / 207 | 207 / 207 | | CPU tests (`-E agc_driver`) | 286 / 287 | 286 / 287 | The CPU failure is `guest_dynamic_loader`, which fails the same way on `main`.
lubaxe commented 2026-10-09 12:59:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by lubaxe on 2026-10-09T12:59:00Z.


Thanks, you were right. Addressed in edad2a16 (on top of a rebase onto main d70b8998):

  • HighestDrawIndex now returns std::optional. It is empty when every index is the restart index and restart is in effect.
  • prepareDrawInputs then marks the draw as nothing to draw (inputs.nothing, the existing exit for zero-count auto draws) before any vertex input is validated or copied. An all-restart index buffer issues no primitives, so the vertex stage has nothing to fetch.
  • The cached derived value for the Index16Restart / Index32Restart snapshot uses stores the highest index plus one, so 0 means "no vertex" on a cache hit as well.
  • End-to-end test in agc_driver_scalar_swappc_fetch (ctest). It draws an indexed triangle strip with restart enabled, six restart indices and a vertex buffer V# with 0 records. The vertex shader stores every fetched vertex to a buffer. The test requires that the draw does not throw, writes no pixels and runs no vertex. With the old behaviour (an all-restart draw treated as vertex 0) it fails with vertex fetch exceeds descriptor record count, the failure you described.
  • highestDrawIndexTests also checks the all-restart case without restart.

Windows 11, RTX 5090: ctest 506 of 507. The failure is agc_driver_pixel_lds, which also fails on unmodified main on this GPU. tools/check_conventions.py passes.

(AI-assisted: Claude Code.)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081370043 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1968#issuecomment-6081370043). Originally posted by [lubaxe](https://github.com/lubaxe) on 2026-10-09T12:59:00Z. --- Thanks, you were right. Addressed in edad2a16 (on top of a rebase onto `main` d70b8998): - `HighestDrawIndex` now returns `std::optional`. It is empty when every index is the restart index and restart is in effect. - `prepareDrawInputs` then marks the draw as nothing to draw (`inputs.nothing`, the existing exit for zero-count auto draws) before any vertex input is validated or copied. An all-restart index buffer issues no primitives, so the vertex stage has nothing to fetch. - The cached derived value for the `Index16Restart` / `Index32Restart` snapshot uses stores the highest index plus one, so 0 means "no vertex" on a cache hit as well. - End-to-end test in `agc_driver_scalar_swappc_fetch` (ctest). It draws an indexed triangle strip with restart enabled, six restart indices and a vertex buffer V# with 0 records. The vertex shader stores every fetched vertex to a buffer. The test requires that the draw does not throw, writes no pixels and runs no vertex. With the old behaviour (an all-restart draw treated as vertex 0) it fails with `vertex fetch exceeds descriptor record count`, the failure you described. - `highestDrawIndexTests` also checks the all-restart case without restart. Windows 11, RTX 5090: `ctest` 506 of 507. The failure is `agc_driver_pixel_lds`, which also fails on unmodified `main` on this GPU. `tools/check_conventions.py` passes. (AI-assisted: Claude Code.)
github-actions[bot] commented 2026-10-10 01:23:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T01:23:10Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2253 core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp L849-851, L853-866, L868-871
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092135037 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1968#issuecomment-6092135037). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T01:23:10Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2253 | `core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp` [L849-851](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp#L849-L851), [L853-866](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp#L853-L866), [L868-871](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp#L868-L871) | | |
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!1968
No description provided.