perf(shader): read a pass-through input that the pixel shader reads only at P0 as a flat input #19530

Merged
imellag merged 3 commits from upstream-pr/19530 into main 2026-11-14 08:23:50 +00:00
imellag commented 2026-11-13 13:38:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-11-13T13:38:53Z.


What

On a device without VK_KHR_fragment_shader_barycentric per-vertex inputs (MoltenVK), a pixel shader that reads a pass-through input (SPI_PS_INPUT_CNTL flat shading with bit 5 of the offset set, InputIsCustom) with v_interp_mov_f32 P0 is rejected by HostInterpolationChecker (GetInterpolationParameter is accepted only for default or flat inputs). The whole pixel shader then goes through the generated barycentric stages: on MoltenVK every triangle becomes a three-point patch through two generated tessellation stages.

When P0 is the only read of the input, those stages give it one value: the per-vertex array's element 0 (EmitInterpolationParameterValue picks vertex (2 + 1) % 3 = 0, raw for a pass-through input), the triangle's first vertex in Vulkan's order. A Flat input under the first provoking vertex gives the same vertex, for lists (3i) and for strips (i, also for odd triangles, whose Vulkan order is (i, i+2, i+1)), so the shader can stay on the host path.

  • HostInterpolationChecker accepts a P0 read of a pass-through input (InputIsPassthrough) when every read of that input in the shader is a 32-bit P0 read (no P10, P20 or 16-bit read), counts it as flat for the shared-slot check, and records the inputs in IrProgramMetadata::pixelFirstVertexInputs when the shader stays on the host path. Recompile enables this only when a rejected shader would go through the generated stages (!hostInterpolationFirst).
  • ShaderInfoCollector, DescribeFragmentParameters and the SPIR-V input declaration treat those inputs as flat: not per-vertex, Flat-decorated, FragmentParameter::flat set (with custom, a pair no other input has).
  • ValidateProvokingVertex refuses PROVOKING_VTX_LAST for a fragment with such a parameter, since the host's last vertex would be read. This keeps today's boundary: last mode already throws for these shaders through the generated stages (tessellation throws in last mode with VK_EXT_provoking_vertex, and without it a custom input already counts as reading the provoking vertex).
  • Shader disk cache format 68 → 69, as the SPIR-V of these shaders changes. 69 is also set by #6924, #9852, #10049, #11551, #14032, #14871, #15343, #17056 and #18185; whichever lands later renumbers.
  • New debt entry in shader.md; which vertex the console passes as P0 is not measured, as for the generated stages.

#12619 (flat aliases in barycentric emulation) changes the same files for another case, a flat input that shares an export slot with an interpolated one, and also moves the provoking-vertex checks before the pipeline cache lookup. Neither depends on the other; whichever lands second takes the other's lines.

Scope: the change applies only where a rejected pixel shader goes through the generated stages, i.e. devices without usable VK_KHR_fragment_shader_barycentric per-vertex inputs (MoltenVK, and devices without the extension). On RADV (HostInterpolationPreferred) the checker runs first and a rejected shader falls back to native PerVertexKHR inputs; there the first-vertex acceptance is off, so such a shader keeps its native per-vertex input and last provoking mode works as before. Devices that use native per-vertex inputs without trying host interpolation first never run the checker.

Newly accepted, with the console's P0 unmeasured (the same assumption as the generated stages): on the tessellation path, draws those stages refused now draw with the host's first provoking vertex: triangle fans, lines, points, an indexed triangle strip with primitive restart, and a triangle list with primitive restart on a device with VK_EXT_primitive_topology_list_restart. On a device with geometry shaders and without the barycentric extension, a draw in last provoking mode that the generated geometry stage drew now throws. Rect lists are unchanged: BuildRectListShaders refuses a custom input before and after (checked on this device: custom per-vertex interpolation is unsupported).

Why it matters: on Astro's Playroom (PPSA21564) on macOS, the barycentric patch emulation ran for ~180k draws in the first 200 s, almost all triangle lists in first provoking mode. Logging the first instruction the checker rejected in each pixel shader gave 96 of 120 rejected shaders on exactly this case ("P0 of a pass-through input, no other read"), including the three most-drawn ones (44.7k draws for the top one). The log's per-shader tally at 180k patch draws attributes 167k of them to listed shaders; the shaders of this case account for 119k of those (72%). The rest: 18.7k (11%) for the five shaders that read a pass-through input at P10, which needs real per-vertex values, and 28.7k (17%) for shaders the checker did not log a reason for. Each of those draws goes through MoltenVK's tessellation emulation, which allocates temporary vertex and control-point buffers per draw (this was most of MoltenVK's ~2-4 GB of temporary buffers in that title). That measurement was made on a local tree before this change; the game is not available to me now, so no run of a title with this PR is claimed.

Tested

macOS 27.2, Apple M4 Pro, x86-64 build under Rosetta 2, MoltenVK 1.4.2 and the validation layer of Vulkan SDK 1.4.363.0 (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer), main d4556df8a8.

agc_driver_pixel_barycentric_emulation gains CheckFirstVertex:

  • a pixel shader that reads a pass-through input only at P0 (three vertices with different values) must compile without generated stages, pass the input as flat, custom and not per-vertex, write the first vertex's value to every channel of every interior pixel, and refuse the last provoking vertex;

  • a triangle strip whose shader reads x and y at P0 (all four vertices differ in the pair) must give each pixel the first vertex in Vulkan's order: vertex 0 for the even triangle and vertex 1 for the odd one, whose order is (1, 3, 2);

  • an input read at P10, P20 and P0, and an input read only at P10, must still be per-vertex through the generated stages;

  • compiled with fragmentShaderBarycentricEnabled and hostInterpolationFirst (RADV's setup; compile only, since MoltenVK cannot run PerVertexKHR), the P0-only shader must keep a per-vertex, non-flat input without generated stages.

  • this PR: passes (932 interior pixels at the first vertex's value, 864 and 864 pixels of the strip's even and odd triangle; the existing tessellation, strip, restart and rect-list cases pass as before). Log shows Insert instance layer "VK_LAYER_KHRONOS_validation", no VUID-.

  • negative control (the test change on main): fails with a pass-through input read only at P0 needed generated stages.

  • negative control for the RADV scope (the first-vertex acceptance forced on with hostInterpolationFirst, as in the first version of this PR): fails with with native per-vertex inputs preferred after host interpolation, a pass-through input read only at P0 stopped being read per vertex.

The 116 tests matching (rerun on the current head, same result) ^(agc_[a-z0-9_]*(barycentric|interp|flat|provoking|mesh|rect|pixel|fragment)[a-z0-9_]*|shader_[a-z0-9_]*|request_serializer)$, on main and on this PR, each after a full build of its tree, with the environment above:

main this PR
passed 112 (14 skipped: no mesh shaders, interlock or f16 interpolation here) 112 (same 14 skipped)
failed agc_driver_pixel_sample_coverage (vkCreateGraphicsPipelines -3), agc_driver_pixel_sample_iteration (sample shading rate), agc_driver_provoking_vertex (provokingVertexLast is not supported), agc_driver_queued_indirect_read_order (timeout) the same four, same messages
validation layer inserted in 133 tests, VUID-VkDeviceCreateInfo-sType-unique 2 times the same

So the change adds no failure and no validation error on this device.

Not run on Linux or Windows.

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: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.2 (x86-64 under Rosetta 2)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5296464477 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19530). Originally posted by [imellag](https://github.com/imellag) on 2026-11-13T13:38:53Z. --- ### What On a device without `VK_KHR_fragment_shader_barycentric` per-vertex inputs (MoltenVK), a pixel shader that reads a pass-through input (`SPI_PS_INPUT_CNTL` flat shading with bit 5 of the offset set, `InputIsCustom`) with `v_interp_mov_f32` P0 is rejected by `HostInterpolationChecker` (`GetInterpolationParameter` is accepted only for default or flat inputs). The whole pixel shader then goes through the generated barycentric stages: on MoltenVK every triangle becomes a three-point patch through two generated tessellation stages. When P0 is the only read of the input, those stages give it one value: the per-vertex array's element 0 (`EmitInterpolationParameterValue` picks vertex `(2 + 1) % 3 = 0`, raw for a pass-through input), the triangle's first vertex in Vulkan's order. A `Flat` input under the first provoking vertex gives the same vertex, for lists (3i) and for strips (i, also for odd triangles, whose Vulkan order is (i, i+2, i+1)), so the shader can stay on the host path. - `HostInterpolationChecker` accepts a P0 read of a pass-through input (`InputIsPassthrough`) when every read of that input in the shader is a 32-bit P0 read (no P10, P20 or 16-bit read), counts it as flat for the shared-slot check, and records the inputs in `IrProgramMetadata::pixelFirstVertexInputs` when the shader stays on the host path. `Recompile` enables this only when a rejected shader would go through the generated stages (`!hostInterpolationFirst`). - `ShaderInfoCollector`, `DescribeFragmentParameters` and the SPIR-V input declaration treat those inputs as flat: not per-vertex, `Flat`-decorated, `FragmentParameter::flat` set (with `custom`, a pair no other input has). - `ValidateProvokingVertex` refuses `PROVOKING_VTX_LAST` for a fragment with such a parameter, since the host's last vertex would be read. This keeps today's boundary: last mode already throws for these shaders through the generated stages (tessellation throws in last mode with `VK_EXT_provoking_vertex`, and without it a custom input already counts as reading the provoking vertex). - Shader disk cache format 68 → 69, as the SPIR-V of these shaders changes. 69 is also set by #6924, #9852, #10049, #11551, #14032, #14871, #15343, #17056 and #18185; whichever lands later renumbers. - New debt entry in `shader.md`; which vertex the console passes as P0 is not measured, as for the generated stages. #12619 (flat aliases in barycentric emulation) changes the same files for another case, a flat input that shares an export slot with an interpolated one, and also moves the provoking-vertex checks before the pipeline cache lookup. Neither depends on the other; whichever lands second takes the other's lines. Scope: the change applies only where a rejected pixel shader goes through the generated stages, i.e. devices without usable `VK_KHR_fragment_shader_barycentric` per-vertex inputs (MoltenVK, and devices without the extension). On RADV (`HostInterpolationPreferred`) the checker runs first and a rejected shader falls back to native `PerVertexKHR` inputs; there the first-vertex acceptance is off, so such a shader keeps its native per-vertex input and last provoking mode works as before. Devices that use native per-vertex inputs without trying host interpolation first never run the checker. Newly accepted, with the console's P0 unmeasured (the same assumption as the generated stages): on the tessellation path, draws those stages refused now draw with the host's first provoking vertex: triangle fans, lines, points, an indexed triangle strip with primitive restart, and a triangle list with primitive restart on a device with `VK_EXT_primitive_topology_list_restart`. On a device with geometry shaders and without the barycentric extension, a draw in last provoking mode that the generated geometry stage drew now throws. Rect lists are unchanged: `BuildRectListShaders` refuses a custom input before and after (checked on this device: `custom per-vertex interpolation is unsupported`). Why it matters: on Astro's Playroom (PPSA21564) on macOS, the barycentric patch emulation ran for ~180k draws in the first 200 s, almost all triangle lists in first provoking mode. Logging the first instruction the checker rejected in each pixel shader gave 96 of 120 rejected shaders on exactly this case ("P0 of a pass-through input, no other read"), including the three most-drawn ones (44.7k draws for the top one). The log's per-shader tally at 180k patch draws attributes 167k of them to listed shaders; the shaders of this case account for 119k of those (72%). The rest: 18.7k (11%) for the five shaders that read a pass-through input at P10, which needs real per-vertex values, and 28.7k (17%) for shaders the checker did not log a reason for. Each of those draws goes through MoltenVK's tessellation emulation, which allocates temporary vertex and control-point buffers per draw (this was most of MoltenVK's ~2-4 GB of temporary buffers in that title). That measurement was made on a local tree before this change; the game is not available to me now, so no run of a title with this PR is claimed. ### Tested macOS 27.2, Apple M4 Pro, x86-64 build under Rosetta 2, MoltenVK 1.4.2 and the validation layer of Vulkan SDK 1.4.363.0 (`ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`), `main` d4556df8a8. `agc_driver_pixel_barycentric_emulation` gains `CheckFirstVertex`: - a pixel shader that reads a pass-through input only at P0 (three vertices with different values) must compile without generated stages, pass the input as `flat`, `custom` and not per-vertex, write the first vertex's value to every channel of every interior pixel, and refuse the last provoking vertex; - a triangle strip whose shader reads x and y at P0 (all four vertices differ in the pair) must give each pixel the first vertex in Vulkan's order: vertex 0 for the even triangle and vertex 1 for the odd one, whose order is (1, 3, 2); - an input read at P10, P20 and P0, and an input read only at P10, must still be per-vertex through the generated stages; - compiled with `fragmentShaderBarycentricEnabled` and `hostInterpolationFirst` (RADV's setup; compile only, since MoltenVK cannot run `PerVertexKHR`), the P0-only shader must keep a per-vertex, non-flat input without generated stages. - this PR: passes (932 interior pixels at the first vertex's value, 864 and 864 pixels of the strip's even and odd triangle; the existing tessellation, strip, restart and rect-list cases pass as before). Log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"`, no `VUID-`. - negative control (the test change on `main`): fails with `a pass-through input read only at P0 needed generated stages`. - negative control for the RADV scope (the first-vertex acceptance forced on with `hostInterpolationFirst`, as in the first version of this PR): fails with `with native per-vertex inputs preferred after host interpolation, a pass-through input read only at P0 stopped being read per vertex`. The 116 tests matching (rerun on the current head, same result) `^(agc_[a-z0-9_]*(barycentric|interp|flat|provoking|mesh|rect|pixel|fragment)[a-z0-9_]*|shader_[a-z0-9_]*|request_serializer)$`, on `main` and on this PR, each after a full build of its tree, with the environment above: | | `main` | this PR | |---|---|---| | passed | 112 (14 skipped: no mesh shaders, interlock or f16 interpolation here) | 112 (same 14 skipped) | | failed | `agc_driver_pixel_sample_coverage` (`vkCreateGraphicsPipelines` -3), `agc_driver_pixel_sample_iteration` (sample shading rate), `agc_driver_provoking_vertex` (`provokingVertexLast is not supported`), `agc_driver_queued_indirect_read_order` (timeout) | the same four, same messages | | validation | layer inserted in 133 tests, `VUID-VkDeviceCreateInfo-sType-unique` 2 times | the same | So the change adds no failure and no validation error on this device. Not run on Linux or Windows. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.2 (x86-64 under Rosetta 2) - [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!19530
No description provided.