feat(agc): emulate list primitive restart without VK_EXT_primitive_topology_list_restart #12180

Merged
Gonic0 merged 3 commits from upstream-pr/12180 into main 2026-10-28 22:50:11 +00:00
Gonic0 commented 2026-10-28 01:26:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-28T01:26:57Z.


What

On a device without VK_EXT_primitive_topology_list_restart (MoltenVK has none), precheckDraw rejects an indexed point, line or triangle list draw with primitive restart enabled. The pipeline already leaves primitiveRestartEnable off for lists on such a device, so this change does the restart on the CPU side instead:

  • CompactListRestart (VertexInput.hpp) copies the indices as the primitive assembler reads them: each restart index drops the incomplete primitive before it, and an incomplete last primitive is dropped too. The draw uses the compacted count.
  • A direct draw compacts the bound index range. An indirect draw takes the CPU-read records path (IndirectDrawPath::FeatureGap), and each record's range is compacted into one new buffer, with its first index and count rewritten.
  • A mesh-path list draw still needs the extension and fails with that message.
  • Devices with the extension are unchanged: they keep the host's restart.

Tests: compactListRestartTests in tests/Graphics.cpp checks the compaction on the CPU. The new ListRestart execution test draws a triangle list 0 R 3 4 5 0 1 2 3 4 (R the restart index), directly and through indexed indirect records, and checks which of the two triangles reach the render target. On a device with the extension it checks the host's restart the same way. Without the extension it also draws the indirect records over an empty index range, and checks that nothing is drawn.

Prior art: no open or closed PR emulates list restart. #115 added restart for strips, and for lists only with the extension; #2757 and #12125 handle restart in mesh-path strips and fans.

Overlap: #9875 (winstrex) edits the lines next to this change in Graphics/src/Draw.cpp and tests/Graphics.cpp for a different change (the index range of a draw). As agreed on #9875, it lands first and this PR rebases onto it: the compaction branch then takes DrawIndexRange and the DrawIndexBounds that ClampDrawIndices returns, and sets the draw's lowest index too. #6188 restructures precheckDraw and keeps the rejection.

Tested

Apple M5 Pro, macOS 27.2, Apple clang 21.0.0, MoltenVK from Vulkan SDK 1.4.363 (no primitiveTopologyListRestart), x86-64 Release under Rosetta 2.

main alone does not build libkernel or the driver on macOS. The tree is main cccdaa2734 plus only the open PRs it needs to build: #10552, #11447, #10669, #11554, #11437, #7631, #7804, and Exception.cpp from #4630. #7631, #7804 and #4630 conflict with main; I resolved them locally. This commit is on top.

agc_driver_list_restart_tests agc_driver_graphics_tests
this commit passes, 3 of 3 passes, apart from a host import check (below)
this commit's tests with Graphics/ and Execution/ restored from main aborts, 3 of 3: "vertex buffer aliases the render target" (the new CPU check does not build without CompactListRestart)
this commit with written = primitiveStart; removed from CompactListRestart aborts, 3 of 3: "direct triangle list with a restart and an incomplete tail: the left triangle (0 1 2) is missing" aborts: "a restart did not drop the incomplete triangle before it"
this commit with record.firstVertexOrIndex = cursor; removed from CompactIndirectListRestart aborts, 3 of 3: "indirect record starting at the restart: the right triangle (3 4 5) is missing"
this commit without the empty index range check aborts, 3 of 3: "zero-sized GPU buffer"

agc_driver_graphics_tests sometimes stops at "two ranges under a budget for two were not imported", after MoltenVK refuses a host import (vkAllocateMemory -2). It did so in 2 of 10 runs with this commit and in 2 of 10 without it.

The existing execution tests that draw with restart or indexed indirect records, agc_driver_scalar_swappc_fetch_tests, agc_driver_stencil_replay_tests and agc_driver_draw_offset_indices_tests, pass 3 of 3 with this commit. On the de004fed72 tree, agc_driver_tessellation_tests failed with and without it, at the same check.

With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, agc_driver_graphics_tests, agc_driver_stencil_replay_tests, agc_driver_scalar_swappc_fetch_tests, agc_driver_draw_offset_indices_tests and agc_driver_pixel_barycentric_emulation_tests report no VUID- error on main or with this change, and neither does agc_driver_list_restart_tests with it. No log reports the layer as not found.

ASTRO BOT (PPSA21564), on the same tree with main de004fed72 (the change was identical), relinked with that tree's relinker (--macos --to-rosetta --macos-fixed-base 0x400000), ROSETTA_ADVERTISE_AVX=1 APS5_ASYNC_PIPELINES=1. The two library sets differ only in libSceAgcDriver.prx. Runs in this order, each variant starting from an empty shader cache:

Run This commit Shader cache Result
1 no cold aborts after about 105 s: "primitive restart is only supported for strips, and for lists with VK_EXT_primitive_topology_list_restart"
2 no warm aborts in under 15 s with the same error
3 yes cold passes that point; aborts after about 45 s: "device does not support mesh shaders"
4 yes warm aborts in under 15 s with the same mesh shader error

The window stays black up to the mesh shader abort, so the title shows only that its draws are no longer rejected; the execution test is what checks the output.

Not tested on Linux or Windows. Where a driver has the extension, the emulation is not taken.

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: #9875
  • Fixes: none
  • Unblocks: PPSA21564 #357 on macOS, only past this abort; it stops next at mesh shaders
  • GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5043687690 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12180). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-28T01:26:57Z. --- ### What On a device without `VK_EXT_primitive_topology_list_restart` (MoltenVK has none), `precheckDraw` rejects an indexed point, line or triangle list draw with primitive restart enabled. The pipeline already leaves `primitiveRestartEnable` off for lists on such a device, so this change does the restart on the CPU side instead: - `CompactListRestart` (`VertexInput.hpp`) copies the indices as the primitive assembler reads them: each restart index drops the incomplete primitive before it, and an incomplete last primitive is dropped too. The draw uses the compacted count. - A direct draw compacts the bound index range. An indirect draw takes the CPU-read records path (`IndirectDrawPath::FeatureGap`), and each record's range is compacted into one new buffer, with its first index and count rewritten. - A mesh-path list draw still needs the extension and fails with that message. - Devices with the extension are unchanged: they keep the host's restart. Tests: `compactListRestartTests` in `tests/Graphics.cpp` checks the compaction on the CPU. The new `ListRestart` execution test draws a triangle list `0 R 3 4 5 0 1 2 3 4` (R the restart index), directly and through indexed indirect records, and checks which of the two triangles reach the render target. On a device with the extension it checks the host's restart the same way. Without the extension it also draws the indirect records over an empty index range, and checks that nothing is drawn. Prior art: no open or closed PR emulates list restart. #115 added restart for strips, and for lists only with the extension; #2757 and #12125 handle restart in mesh-path strips and fans. Overlap: #9875 (winstrex) edits the lines next to this change in `Graphics/src/Draw.cpp` and `tests/Graphics.cpp` for a different change (the index range of a draw). As agreed on #9875, it lands first and this PR rebases onto it: the compaction branch then takes `DrawIndexRange` and the `DrawIndexBounds` that `ClampDrawIndices` returns, and sets the draw's lowest index too. #6188 restructures `precheckDraw` and keeps the rejection. ### Tested Apple M5 Pro, macOS 27.2, Apple clang 21.0.0, MoltenVK from Vulkan SDK 1.4.363 (no `primitiveTopologyListRestart`), x86-64 Release under Rosetta 2. `main` alone does not build libkernel or the driver on macOS. The tree is `main` `cccdaa2734` plus only the open PRs it needs to build: #10552, #11447, #10669, #11554, #11437, #7631, #7804, and `Exception.cpp` from #4630. #7631, #7804 and #4630 conflict with `main`; I resolved them locally. This commit is on top. | | `agc_driver_list_restart_tests` | `agc_driver_graphics_tests` | |---|---|---| | this commit | passes, 3 of 3 | passes, apart from a host import check (below) | | this commit's tests with `Graphics/` and `Execution/` restored from `main` | aborts, 3 of 3: "vertex buffer aliases the render target" | (the new CPU check does not build without `CompactListRestart`) | | this commit with `written = primitiveStart;` removed from `CompactListRestart` | aborts, 3 of 3: "direct triangle list with a restart and an incomplete tail: the left triangle (0 1 2) is missing" | aborts: "a restart did not drop the incomplete triangle before it" | | this commit with `record.firstVertexOrIndex = cursor;` removed from `CompactIndirectListRestart` | aborts, 3 of 3: "indirect record starting at the restart: the right triangle (3 4 5) is missing" | | | this commit without the empty index range check | aborts, 3 of 3: "zero-sized GPU buffer" | | `agc_driver_graphics_tests` sometimes stops at "two ranges under a budget for two were not imported", after MoltenVK refuses a host import (`vkAllocateMemory` -2). It did so in 2 of 10 runs with this commit and in 2 of 10 without it. The existing execution tests that draw with restart or indexed indirect records, `agc_driver_scalar_swappc_fetch_tests`, `agc_driver_stencil_replay_tests` and `agc_driver_draw_offset_indices_tests`, pass 3 of 3 with this commit. On the `de004fed72` tree, `agc_driver_tessellation_tests` failed with and without it, at the same check. With `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`, `agc_driver_graphics_tests`, `agc_driver_stencil_replay_tests`, `agc_driver_scalar_swappc_fetch_tests`, `agc_driver_draw_offset_indices_tests` and `agc_driver_pixel_barycentric_emulation_tests` report no `VUID-` error on `main` or with this change, and neither does `agc_driver_list_restart_tests` with it. No log reports the layer as not found. ASTRO BOT (PPSA21564), on the same tree with `main` `de004fed72` (the change was identical), relinked with that tree's relinker (`--macos --to-rosetta --macos-fixed-base 0x400000`), `ROSETTA_ADVERTISE_AVX=1 APS5_ASYNC_PIPELINES=1`. The two library sets differ only in `libSceAgcDriver.prx`. Runs in this order, each variant starting from an empty shader cache: | Run | This commit | Shader cache | Result | |---|---|---|---| | 1 | no | cold | aborts after about 105 s: "primitive restart is only supported for strips, and for lists with VK_EXT_primitive_topology_list_restart" | | 2 | no | warm | aborts in under 15 s with the same error | | 3 | yes | cold | passes that point; aborts after about 45 s: "device does not support mesh shaders" | | 4 | yes | warm | aborts in under 15 s with the same mesh shader error | The window stays black up to the mesh shader abort, so the title shows only that its draws are no longer rejected; the execution test is what checks the output. Not tested on Linux or Windows. Where a driver has the extension, the emulation is not taken. ### 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 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: #9875 - [x] Fixes: none - [x] Unblocks: PPSA21564 #357 on macOS, only past this abort; it stops next at mesh shaders - [x] GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 03:25:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T03:25:31Z.

Review: CHANGES_REQUESTED


I reviewed exact head 67ff0f666be6ec5c30a323e4cd89b7edb8489121. I found two blocking follow-ups:

  1. In the new list-restart branch in prepareDrawInputs, an indirect indexed draw with a zero-sized bound index range reaches std::make_shared<Buffer>(context, indexBytes, ...) before the record ranges are compacted. The existing early return covers zero draw.indexCount only when args == nullptr; Buffer rejects size 0 (zero-sized GPU buffer). Such an indirect range has no valid indices and should become a no-op, but currently throws. Please handle the zero-byte case before constructing the buffer and cover it with a regression test.
  2. The automatic overlap comment identifies open #9875 as conflicting on Draw.cpp and Graphics.cpp lines. CONTRIBUTING.md requires agreement on merge order and a Depends on: #N entry for same-line overlaps. The PR body says the later one rebases, but does not record an agreed order/dependency. Please coordinate with #9875’s author and update the checklist; I am not asking either PR to be closed.

The CPU compaction helper’s current unit cases cover restart positions and incomplete tails. Author-reported MoltenVK execution tests pass 3/3 and mutation controls fail as expected; the report also notes a separate agc_driver_graphics_tests host-import flake at equal 2/10 rates with and without this change. Hosted policy checks pass; platform build jobs are skipped for these paths. I did not independently run the Apple GPU tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677423174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12180#pullrequestreview-5677423174). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T03:25:31Z. **Review:** CHANGES_REQUESTED --- I reviewed exact head `67ff0f666be6ec5c30a323e4cd89b7edb8489121`. I found two blocking follow-ups: 1. In the new list-restart branch in `prepareDrawInputs`, an indirect indexed draw with a zero-sized bound index range reaches `std::make_shared<Buffer>(context, indexBytes, ...)` before the record ranges are compacted. The existing early return covers zero `draw.indexCount` only when `args == nullptr`; `Buffer` rejects size 0 (`zero-sized GPU buffer`). Such an indirect range has no valid indices and should become a no-op, but currently throws. Please handle the zero-byte case before constructing the buffer and cover it with a regression test. 2. The automatic overlap comment identifies open #9875 as conflicting on `Draw.cpp` and `Graphics.cpp` lines. CONTRIBUTING.md requires agreement on merge order and a `Depends on: #N` entry for same-line overlaps. The PR body says the later one rebases, but does not record an agreed order/dependency. Please coordinate with #9875’s author and update the checklist; I am not asking either PR to be closed. The CPU compaction helper’s current unit cases cover restart positions and incomplete tails. Author-reported MoltenVK execution tests pass 3/3 and mutation controls fail as expected; the report also notes a separate `agc_driver_graphics_tests` host-import flake at equal 2/10 rates with and without this change. Hosted policy checks pass; platform build jobs are skipped for these paths. I did not independently run the Apple GPU tests.
MarcZX10 commented 2026-10-28 10:00:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T10:00:40Z.

Review: APPROVED


Reviewed current head beb5f7a309eb037970186a3d2c83db207e683681. The two blockers from my review of 67ff0f666b are addressed: the zero-byte index range now returns a no-op before allocating the compacted buffer and has an execution regression case; and the overlap with #9875 now has explicit author agreement on merge order, a Depends on: #9875 checklist entry, and the rebased implementation uses the updated index-range/bounds APIs. The author reports the focused macOS and validation-layer runs on this head, including the zero-sized-buffer negative control; hosted Windows and conventions checks pass. I did not independently reproduce the GPU runs in this environment. No further blocking issue found in the reviewed diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680829179 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12180#pullrequestreview-5680829179). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T10:00:40Z. **Review:** APPROVED --- Reviewed current head `beb5f7a309eb037970186a3d2c83db207e683681`. The two blockers from my review of `67ff0f666b` are addressed: the zero-byte index range now returns a no-op before allocating the compacted buffer and has an execution regression case; and the overlap with #9875 now has explicit author agreement on merge order, a `Depends on: #9875` checklist entry, and the rebased implementation uses the updated index-range/bounds APIs. The author reports the focused macOS and validation-layer runs on this head, including the zero-sized-buffer negative control; hosted Windows and conventions checks pass. I did not independently reproduce the GPU runs in this environment. No further blocking issue found in the reviewed diff.
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!12180
No description provided.