feat(agc): select viewports from vertex and mesh viewport index exports #19373

Merged
c1b3r-p3d1a merged 8 commits from upstream-pr/19373 into main 2026-11-16 21:22:38 +00:00
c1b3r-p3d1a commented 2026-11-13 06:53:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by c1b3r-p3d1a on 2026-11-13T06:53:32Z.


What

PA_CL_VS_OUT_CNTL.USE_VTX_VIEWPORT_INDX (bit 19) was ignored with a warning, so every draw landed in viewport 0. With this change, vertex and mesh draws select one of 16 viewports per vertex:

  • Decode (State.cpp): when bit 19 is set, viewports 1 to 15 are read from PA_CL_VPORT_XSCALE_i..ZOFFSET_i (0x10f + 6i) with the same collapsed/usable rules as viewport 0. Their scissors are the screen, window and generic scissors intersected with PA_SC_VPORT_SCISSOR_i (0x94 + 2i) when PA_SC_MODE_CNTL_0.VPORT_SCISSOR_ENABLE is set. A missing, non-finite or reserved-bit viewport becomes an empty one that draws nothing; it does not throw, because titles leave unused slots unwritten. The draw's single scissor becomes the union, and depth-clear pinning, unused-depth normalisation and depth-bounds emulation apply to every viewport.
  • Index range: the index is the 4-bit field 19:16, so it is 0 to 15, and a draw that keeps it always declares all 16 viewports (ApplyViewportIndex requires it). No index is out of range, so nothing is clamped.
  • Vulkan: multiViewport is enabled when maxViewports >= 16. Pipelines take viewportCount/scissorCount from the state (it is part of the pipeline key and the draw-register key), and vkCmdSetViewport/vkCmdSetScissor set all 16.
  • Draw key (State.hpp): DrawKeyRegisters covers PA_SC_VPORT_SCISSOR_0..15 (0x094, 0x20 words) and PA_CL_VPORT_*_0..15 (0x10f, 0x60 words), so with the register draw key a draw that changes only one of viewports 1 to 15 misses the cache instead of reusing the old viewports.
  • Pipeline journal (PipelineJournal.cpp): CanonicalPipelineState keeps the viewport count, the recipe codec writes it (a count over 15 does not decode), the journal format is 19 (main is at 18) and the State size guard is 1008. Viewports and scissors are dynamic state, so the count is the only part of them a pipeline is built from.
  • Recompiler: the vertex shader writes ViewportIndex through ShaderViewportIndexLayerEXT when the device has VK_EXT_shader_viewport_index_layer, otherwise through the SPIR-V 1.5 capability. The mesh shader writes a per-primitive ViewportIndex block (PerPrimitiveEXT, MultiViewport) from the provoking vertex, the same way Layer is handled. The index is bits 19:16 of the misc vector's z, as Mesa's radeonsi and RADV pack it on GFX9+; that extraction was already present.
  • Kept as before, with the existing one-time warning: tessellation, rect-list and barycentric-patch routings, devices without multiViewport, and vertex draws on devices without VK_EXT_shader_viewport_index_layer (which would otherwise fail shader validation), ignore the index and draw into viewport 0. A depth range outside [0, 1] on a multi-viewport draw throws without VK_EXT_depth_range_unrestricted, because the remap is single-viewport. Only PA_SC_VPORT_ZMIN/ZMAX_0 is read. All of this is in the new debt entry.

No open PR implements multi-viewport. #15548 and #17611 touch the same files but only use ShaderViewportIndexLayerEXT for layer output in their compute emulation.

Overlap: #16752 rewrites the vkCmdSetViewport/vkCmdSetScissor calls in Pipeline::Continue/ContinueBackFaces that setViewports replaces here, so whichever merges second has to move setViewports onto its CommandState. I moved the DrawDecode.cpp and ShaderValidation.cpp hunks off the lines #16025, #17022 and #17911 change; a merge simulation against them is clean.

Tested

Head b35560753e on main 92b550a7c4, no title. It merges cleanly with main e22c5fb941.

This head (draw key ranges and the viewportIndexLayer gate), Windows 11 (26300), AMD Radeon RX 7700 XT, AMD driver 32.0.32015.7029:

  • agc_driver_viewport_index now records every register the multi-viewport decode reads through the register facade, requires that it read viewports 1 and 15 (0x96, 0xb3, 0x115, 0x16e), that DrawKeyCovers holds every one of them, and that 0x16f is not covered. It also checks that a vertex draw on a multiViewport device without VK_EXT_shader_viewport_index_layer falls back to viewport 0. The pin at tests/Graphics.cpp:638 still holds.
  • Mutation checks, each makes the test fail: the scissor range back to 2 words, the transform range back to 6, the transform range one word short (0x5f), one word too wide (0x61), and dropping the viewportIndexLayer condition.
  • With the Khronos validation layer 1.4.368 (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_REQUIRE_VULKAN=1), agc_driver_viewport_index passes 3 of 3 runs with the vertex and mesh draws; each log shows the layer inserted and no VUID- messages.
  • Full agc_driver|shader suite (1268 tests), every test executable rebuilt: 1258 pass. Eight f16 checks (agc_driver_f16_conversion_float_mode, float16_misc, float16_modifiers, float16_rounding, fp16_overflow, ldexp_f16_inline_constants, sdwa_ldexp_f16, vop2_modifier_forms) and agc_driver_sample_order_grid_reference fail. They test shader arithmetic and sample positions this PR does not touch, and they failed the same way on main in my earlier baseline run; I did not rerun main for this head. agc_driver_dumped_constant_capture failed in the suite and in 1 of 9 runs alone (8 passed), so it is intermittent.

Previous head 3f211973f3:

  • agc_driver_viewport_index:
    • Decoder checks: viewports 1, 5 and 15 follow their own PA_CL_VPORT registers and PA_SC_VPORT_SCISSOR, the other slots are empty, 16 scissors and their union, stripping without multiViewport or on tessellation, a draw keeping the index with fewer than 16 viewports is rejected, a reserved-bit scissor gives an empty viewport, and the depth remap rejection.
    • Journal checks: the canonical state keeps 16 viewports (1 for a single-viewport draw), the recipe round-trips byte for byte, and a recipe with 17 viewports does not decode.
    • GPU draws through the vertex and the mesh path into a 32x16 target split into 16 strips, each viewport with its own scissor height: every vertex selects viewport 1, 0, 2, 7, then 15, and the test checks every texel.
  • Mutation checks, each makes the test fail: dropping the count from the canonical state, writing 0 for it in the recipe, accepting 16 extra viewports on decode, removing the 16-viewport requirement, passing only scissor 0 to Vulkan, reading the wrong PA_SC_VPORT_SCISSOR register, and reading the index from bit 17.
  • Windows 11 (26300), AMD Radeon 680M, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R <pattern> -V, Khronos validation layer 1.4.363.1:
    • On this branch, agc_driver_viewport_index (vertex and mesh draws), agc_driver_mesh_provoking_layer and agc_driver_layered_array_color_draw pass. Each log shows Insert instance layer "VK_LAYER_KHRONOS_validation", there is no was not found but was requested, and there are no VUID- messages.
    • On main, the two existing tests run the same way, also with no VUID- messages.
    • agc_driver_viewport_index passes 3 of 3 runs, 0.4 s each.
    • Full agc_driver|shader suite (1267 tests): 1266 pass. agc_driver_sample_order_grid_reference fails, and fails the same way on main on this GPU. agc_driver_graphics failed once under -j 8 on a partially resident depth plane check and passes 3 of 3 runs alone.
  • Linux, Ubuntu 24.04 (WSL2), GCC 13.3.0, llvmpipe (Mesa 25.2.8): the full build compiles, including the State size guard in PipelineJournal.cpp. agc_driver_viewport_index passes with the vertex draws; its mesh draws skip because llvmpipe has no mesh compiler for these programs. agc_driver_pipeline_journal, agc_pipeline_journal, agc_driver_graphics and agc_driver_layered_array_color_draw pass; agc_driver_mesh_provoking_layer skips for the same reason. No validation layer on this Linux setup.
  • Not run: an NVIDIA GPU (I have none), and a Linux run on a GPU with mesh shaders.
  • python3 tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py could not confirm overlaps, so I compared the PRs that share files by hand (see What).

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 7700 XT, AMD driver 32.0.32015.7029, Windows 11 (this head); AMD Radeon 680M, AMD driver 32.0.21030.12001, Windows 11 (previous head); llvmpipe (Mesa 25.2.8), Ubuntu 24.04 on WSL2
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5291444530 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19373). Originally posted by [c1b3r-p3d1a](https://github.com/c1b3r-p3d1a) on 2026-11-13T06:53:32Z. --- ### What `PA_CL_VS_OUT_CNTL.USE_VTX_VIEWPORT_INDX` (bit 19) was ignored with a warning, so every draw landed in viewport 0. With this change, vertex and mesh draws select one of 16 viewports per vertex: - **Decode** (`State.cpp`): when bit 19 is set, viewports 1 to 15 are read from `PA_CL_VPORT_XSCALE_i`..`ZOFFSET_i` (0x10f + 6i) with the same collapsed/usable rules as viewport 0. Their scissors are the screen, window and generic scissors intersected with `PA_SC_VPORT_SCISSOR_i` (0x94 + 2i) when `PA_SC_MODE_CNTL_0.VPORT_SCISSOR_ENABLE` is set. A missing, non-finite or reserved-bit viewport becomes an empty one that draws nothing; it does not throw, because titles leave unused slots unwritten. The draw's single scissor becomes the union, and depth-clear pinning, unused-depth normalisation and depth-bounds emulation apply to every viewport. - **Index range**: the index is the 4-bit field 19:16, so it is 0 to 15, and a draw that keeps it always declares all 16 viewports (`ApplyViewportIndex` requires it). No index is out of range, so nothing is clamped. - **Vulkan**: `multiViewport` is enabled when `maxViewports >= 16`. Pipelines take `viewportCount`/`scissorCount` from the state (it is part of the pipeline key and the draw-register key), and `vkCmdSetViewport`/`vkCmdSetScissor` set all 16. - **Draw key** (`State.hpp`): `DrawKeyRegisters` covers `PA_SC_VPORT_SCISSOR_0..15` (`0x094`, 0x20 words) and `PA_CL_VPORT_*_0..15` (`0x10f`, 0x60 words), so with the register draw key a draw that changes only one of viewports 1 to 15 misses the cache instead of reusing the old viewports. - **Pipeline journal** (`PipelineJournal.cpp`): `CanonicalPipelineState` keeps the viewport count, the recipe codec writes it (a count over 15 does not decode), the journal format is 19 (main is at 18) and the `State` size guard is 1008. Viewports and scissors are dynamic state, so the count is the only part of them a pipeline is built from. - **Recompiler**: the vertex shader writes `ViewportIndex` through `ShaderViewportIndexLayerEXT` when the device has `VK_EXT_shader_viewport_index_layer`, otherwise through the SPIR-V 1.5 capability. The mesh shader writes a per-primitive `ViewportIndex` block (`PerPrimitiveEXT`, `MultiViewport`) from the provoking vertex, the same way `Layer` is handled. The index is bits 19:16 of the misc vector's z, as Mesa's radeonsi and RADV pack it on GFX9+; that extraction was already present. - **Kept as before, with the existing one-time warning**: tessellation, rect-list and barycentric-patch routings, devices without `multiViewport`, and vertex draws on devices without `VK_EXT_shader_viewport_index_layer` (which would otherwise fail shader validation), ignore the index and draw into viewport 0. A depth range outside [0, 1] on a multi-viewport draw throws without `VK_EXT_depth_range_unrestricted`, because the remap is single-viewport. Only `PA_SC_VPORT_ZMIN/ZMAX_0` is read. All of this is in the new debt entry. No open PR implements multi-viewport. #15548 and #17611 touch the same files but only use `ShaderViewportIndexLayerEXT` for layer output in their compute emulation. Overlap: #16752 rewrites the `vkCmdSetViewport`/`vkCmdSetScissor` calls in `Pipeline::Continue`/`ContinueBackFaces` that `setViewports` replaces here, so whichever merges second has to move `setViewports` onto its `CommandState`. I moved the `DrawDecode.cpp` and `ShaderValidation.cpp` hunks off the lines #16025, #17022 and #17911 change; a merge simulation against them is clean. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Head `b35560753e` on `main` `92b550a7c4`, no title. It merges cleanly with `main` `e22c5fb941`. **This head** (draw key ranges and the `viewportIndexLayer` gate), Windows 11 (26300), AMD Radeon RX 7700 XT, AMD driver 32.0.32015.7029: - `agc_driver_viewport_index` now records every register the multi-viewport decode reads through the register facade, requires that it read viewports 1 and 15 (`0x96`, `0xb3`, `0x115`, `0x16e`), that `DrawKeyCovers` holds every one of them, and that `0x16f` is not covered. It also checks that a vertex draw on a `multiViewport` device without `VK_EXT_shader_viewport_index_layer` falls back to viewport 0. The pin at `tests/Graphics.cpp:638` still holds. - Mutation checks, each makes the test fail: the scissor range back to 2 words, the transform range back to 6, the transform range one word short (0x5f), one word too wide (0x61), and dropping the `viewportIndexLayer` condition. - With the Khronos validation layer 1.4.368 (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_REQUIRE_VULKAN=1`), `agc_driver_viewport_index` passes 3 of 3 runs with the vertex and mesh draws; each log shows the layer inserted and no `VUID-` messages. - Full `agc_driver|shader` suite (1268 tests), every test executable rebuilt: 1258 pass. Eight f16 checks (`agc_driver_f16_conversion_float_mode`, `float16_misc`, `float16_modifiers`, `float16_rounding`, `fp16_overflow`, `ldexp_f16_inline_constants`, `sdwa_ldexp_f16`, `vop2_modifier_forms`) and `agc_driver_sample_order_grid_reference` fail. They test shader arithmetic and sample positions this PR does not touch, and they failed the same way on `main` in my earlier baseline run; I did not rerun `main` for this head. `agc_driver_dumped_constant_capture` failed in the suite and in 1 of 9 runs alone (8 passed), so it is intermittent. **Previous head** `3f211973f3`: - `agc_driver_viewport_index`: - **Decoder checks**: viewports 1, 5 and 15 follow their own `PA_CL_VPORT` registers and `PA_SC_VPORT_SCISSOR`, the other slots are empty, 16 scissors and their union, stripping without `multiViewport` or on tessellation, a draw keeping the index with fewer than 16 viewports is rejected, a reserved-bit scissor gives an empty viewport, and the depth remap rejection. - **Journal checks**: the canonical state keeps 16 viewports (1 for a single-viewport draw), the recipe round-trips byte for byte, and a recipe with 17 viewports does not decode. - **GPU draws** through the vertex and the mesh path into a 32x16 target split into 16 strips, each viewport with its own scissor height: every vertex selects viewport 1, 0, 2, 7, then 15, and the test checks every texel. - Mutation checks, each makes the test fail: dropping the count from the canonical state, writing 0 for it in the recipe, accepting 16 extra viewports on decode, removing the 16-viewport requirement, passing only scissor 0 to Vulkan, reading the wrong `PA_SC_VPORT_SCISSOR` register, and reading the index from bit 17. - **Windows 11 (26300), AMD Radeon 680M**, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest --test-dir build -R <pattern> -V`, Khronos validation layer 1.4.363.1: - On this branch, `agc_driver_viewport_index` (vertex and mesh draws), `agc_driver_mesh_provoking_layer` and `agc_driver_layered_array_color_draw` pass. Each log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"`, there is no `was not found but was requested`, and there are no `VUID-` messages. - On `main`, the two existing tests run the same way, also with no `VUID-` messages. - `agc_driver_viewport_index` passes 3 of 3 runs, 0.4 s each. - Full `agc_driver|shader` suite (1267 tests): 1266 pass. `agc_driver_sample_order_grid_reference` fails, and fails the same way on `main` on this GPU. `agc_driver_graphics` failed once under `-j 8` on a partially resident depth plane check and passes 3 of 3 runs alone. - **Linux, Ubuntu 24.04 (WSL2), GCC 13.3.0, llvmpipe (Mesa 25.2.8)**: the full build compiles, including the `State` size guard in `PipelineJournal.cpp`. `agc_driver_viewport_index` passes with the vertex draws; its mesh draws skip because llvmpipe has no mesh compiler for these programs. `agc_driver_pipeline_journal`, `agc_pipeline_journal`, `agc_driver_graphics` and `agc_driver_layered_array_color_draw` pass; `agc_driver_mesh_provoking_layer` skips for the same reason. No validation layer on this Linux setup. - Not run: an NVIDIA GPU (I have none), and a Linux run on a GPU with mesh shaders. - `python3 tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` could not confirm overlaps, so I compared the PRs that share files by hand (see What). ### 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 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> AMD Radeon RX 7700 XT, AMD driver 32.0.32015.7029, Windows 11 (this head); AMD Radeon 680M, AMD driver 32.0.21030.12001, Windows 11 (previous head); llvmpipe (Mesa 25.2.8), Ubuntu 24.04 on WSL2 - [x] AI-assisted: <!-- yes / no --> 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!19373
No description provided.