feat(shader): translate NGG geometry programs to mesh shaders #133

Merged
oneandonlydean merged 2 commits from upstream-pr/133 into main 2026-09-30 15:54:45 +00:00
oneandonlydean commented 2026-09-29 00:33:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T00:33:18Z.


What

The geometry path compiles merged ES/GS (NGG) programs as mesh-stage programs, but BuildShaderStageInputInfo never filled the mesh input metadata and the ShaderMeshInputInfo helpers threw "not implemented", so every such draw failed with "mesh shader translation configuration is not supported (... primitives per group 0 ...)".

Recompiler:

  • BuildShaderStageInputInfo takes the draw's MeshConfiguration; the primitive size/step/count helpers are implemented.
  • The prologue emulates one NGG subgroup per workgroup: s2 GS_TG_INFO, s3 merged wave info per wave, GS vertex offsets (ES thread × VGT_ESGS_RING_ITEMSIZE, odd strip triangles swapped), primitive id, ES vertex and instance ids. An indexed draw's vertex id comes from the index buffer through a raw V# the driver places in hidden user words 4..7.
  • s_sendmsg GS_ALLOC_REQ becomes MeshAllocate. The epilogue zeroes the allocation first, clamps it to the output limits, culls primitives no thread exported and decodes the 9-bit vertex indices.
  • Draw parameters live in the last six push dwords (MeshDrawPushOffsetBytes). A BDA fault in a mesh program no longer stops the invocation: as for other programs with barriers, the fault is recorded and the read returns zero, so every invocation reaches the subgroup barriers.
  • Cache keys cover the mesh configuration. MeshConfiguration gains esgsItemSize, so RequestSerializer moves to version 4 (main is at 3 since #153). Mesh requests before version 4 read back with an item size of 4, which is what main's prologue assumed (<< 2).

Driver:

  • DecodeShaderStages reads VGT_ESGS_RING_ITEMSIZE (0x2ab, added to DrawKeyRegisters) and sizes the subgroup for its ES vertices, GS primitives and outputs. It throws on an item size of 0 or one that overflows the 16-bit offsets.
  • On the mesh path, stages compile below the draw parameters, and every mesh draw pushes them. The push block still takes #153's guest buffer offset patches (PatchPushConstants) before the draw parameters are added. The index-buffer V# is set per draw, and per record on the CPU-resolved indirect path. The index-buffer alias check now applies only to written elements, in both addGuestBuffer and CheckBufferAliases.
  • Workgroup limit and LocalSize checks count two guest lanes per invocation for wave64 on 32-wide subgroups. The LocalSize check reads the module, so upstream's existing APS5_SINGLE_LANE switch still validates. Mesh draws may carry a first vertex/instance.
  • Validation expects the push block of every mesh-stage program. A mesh program reads its draw parameters from the end of the pipeline's push block (MeshDrawPushOffsetBytes), so its SPIR-V always declares the push-constant variable, even when its user data do not fit the push range and RecompileResult::pushConstants is empty. Validation refused such a draw with "invalid push constant interface". It still requires the block to match the recompiler's metadata for the other stages.

Still throws: wave32 NGG programs, subgroups over 15 waves, inconsistent vertex/primitive counts, and GPU-side indirect mesh draws (unchanged).

RequestSerializer: #136 also moves the format to version 4 with different fields. Whichever PR merges second must rebase to version 5 and gate its own field on >= 5.

Tested

  • Linux (CachyOS, RTX 4090): new agc_driver_mesh_tests (EXCLUDE_FROM_ALL, needs a GPU with VK_EXT_mesh_shader, so it is not in ctest) recompiles a hand-assembled merged ES/GS program in the PS5 compiler's layout and draws it through VulkanDevice::Draw: indexed and non-indexed triangle lists in one- and two-wave subgroups (full and partial), and a triangle strip across odd-starting subgroups, with pixel readback. All pass. Main never fills the mesh input metadata, so these draws fail its configuration check (primitives per group 0).
  • agc_shader_memory gains verifyMeshConfiguration: helper sizes for list/strip/line/point, esgsItemSize survives serialization, cache key and context hash change with it. It does not compile on main (no esgsItemSize) and the helpers throw there.
  • agc_driver_graphics_tests (ShaderStageTests: subgroup outputs, esgsItemSize decode, invalid item size throws): run on a throwaway merge with #113, where 65/65 pass.
  • Exercised in PPSA21564 on our integration branch before the rebase onto #153.
  • agc_driver_mesh_tests also draws the non-indexed triangle list with a geometry program given no push range for its own data (its user data then reach it through a buffer). Without the validation change it fails with "invalid push constant interface".
  • Exercised in PPSA21564 on our integration branch: a geometry draw that was refused 12 times a frame from the warp scene on now validates and renders.
  • ctest: 64/64 pass. agc_driver_mesh_tests and agc_driver_recorder_tests 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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: #113 (to build the test target)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4670963911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/133). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T00:33:18Z. --- ### What The geometry path compiles merged ES/GS (NGG) programs as mesh-stage programs, but `BuildShaderStageInputInfo` never filled the mesh input metadata and the `ShaderMeshInputInfo` helpers threw "not implemented", so every such draw failed with "mesh shader translation configuration is not supported (... primitives per group 0 ...)". Recompiler: - `BuildShaderStageInputInfo` takes the draw's `MeshConfiguration`; the primitive size/step/count helpers are implemented. - The prologue emulates one NGG subgroup per workgroup: s2 `GS_TG_INFO`, s3 merged wave info per wave, GS vertex offsets (ES thread × `VGT_ESGS_RING_ITEMSIZE`, odd strip triangles swapped), primitive id, ES vertex and instance ids. An indexed draw's vertex id comes from the index buffer through a raw V# the driver places in hidden user words 4..7. - `s_sendmsg GS_ALLOC_REQ` becomes `MeshAllocate`. The epilogue zeroes the allocation first, clamps it to the output limits, culls primitives no thread exported and decodes the 9-bit vertex indices. - Draw parameters live in the last six push dwords (`MeshDrawPushOffsetBytes`). A BDA fault in a mesh program no longer stops the invocation: as for other programs with barriers, the fault is recorded and the read returns zero, so every invocation reaches the subgroup barriers. - Cache keys cover the mesh configuration. `MeshConfiguration` gains `esgsItemSize`, so `RequestSerializer` moves to version 4 (main is at 3 since #153). Mesh requests before version 4 read back with an item size of 4, which is what main's prologue assumed (`<< 2`). Driver: - `DecodeShaderStages` reads `VGT_ESGS_RING_ITEMSIZE` (0x2ab, added to `DrawKeyRegisters`) and sizes the subgroup for its ES vertices, GS primitives and outputs. It throws on an item size of 0 or one that overflows the 16-bit offsets. - On the mesh path, stages compile below the draw parameters, and every mesh draw pushes them. The push block still takes #153's guest buffer offset patches (`PatchPushConstants`) before the draw parameters are added. The index-buffer V# is set per draw, and per record on the CPU-resolved indirect path. The index-buffer alias check now applies only to written elements, in both `addGuestBuffer` and `CheckBufferAliases`. - Workgroup limit and LocalSize checks count two guest lanes per invocation for wave64 on 32-wide subgroups. The LocalSize check reads the module, so upstream's existing `APS5_SINGLE_LANE` switch still validates. Mesh draws may carry a first vertex/instance. - Validation expects the push block of every mesh-stage program. A mesh program reads its draw parameters from the end of the pipeline's push block (`MeshDrawPushOffsetBytes`), so its SPIR-V always declares the push-constant variable, even when its user data do not fit the push range and `RecompileResult::pushConstants` is empty. Validation refused such a draw with "invalid push constant interface". It still requires the block to match the recompiler's metadata for the other stages. Still throws: wave32 NGG programs, subgroups over 15 waves, inconsistent vertex/primitive counts, and GPU-side indirect mesh draws (unchanged). `RequestSerializer`: #136 also moves the format to version 4 with different fields. Whichever PR merges second must rebase to version 5 and gate its own field on `>= 5`. ### Tested - Linux (CachyOS, RTX 4090): new `agc_driver_mesh_tests` (EXCLUDE_FROM_ALL, needs a GPU with `VK_EXT_mesh_shader`, so it is not in ctest) recompiles a hand-assembled merged ES/GS program in the PS5 compiler's layout and draws it through `VulkanDevice::Draw`: indexed and non-indexed triangle lists in one- and two-wave subgroups (full and partial), and a triangle strip across odd-starting subgroups, with pixel readback. All pass. Main never fills the mesh input metadata, so these draws fail its configuration check (primitives per group 0). - `agc_shader_memory` gains `verifyMeshConfiguration`: helper sizes for list/strip/line/point, `esgsItemSize` survives serialization, cache key and context hash change with it. It does not compile on main (no `esgsItemSize`) and the helpers throw there. - `agc_driver_graphics_tests` (`ShaderStageTests`: subgroup outputs, `esgsItemSize` decode, invalid item size throws): run on a throwaway merge with #113, where 65/65 pass. - Exercised in PPSA21564 on our integration branch before the rebase onto #153. - `agc_driver_mesh_tests` also draws the non-indexed triangle list with a geometry program given no push range for its own data (its user data then reach it through a buffer). Without the validation change it fails with "invalid push constant interface". - Exercised in PPSA21564 on our integration branch: a geometry draw that was refused 12 times a frame from the warp scene on now validates and renders. - `ctest`: 64/64 pass. `agc_driver_mesh_tests` and `agc_driver_recorder_tests` pass. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: #113 (to build the test target) - [x] AI-assisted: yes
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!133
No description provided.