perf(shader): read a pass-through input that the pixel shader reads only at P0 as a flat input #19530
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19530"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-11-13T13:38:53Z.
What
On a device without
VK_KHR_fragment_shader_barycentricper-vertex inputs (MoltenVK), a pixel shader that reads a pass-through input (SPI_PS_INPUT_CNTLflat shading with bit 5 of the offset set,InputIsCustom) withv_interp_mov_f32P0 is rejected byHostInterpolationChecker(GetInterpolationParameteris 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 (
EmitInterpolationParameterValuepicks vertex(2 + 1) % 3 = 0, raw for a pass-through input), the triangle's first vertex in Vulkan's order. AFlatinput 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.HostInterpolationCheckeraccepts 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 inIrProgramMetadata::pixelFirstVertexInputswhen the shader stays on the host path.Recompileenables this only when a rejected shader would go through the generated stages (!hostInterpolationFirst).ShaderInfoCollector,DescribeFragmentParametersand the SPIR-V input declaration treat those inputs as flat: not per-vertex,Flat-decorated,FragmentParameter::flatset (withcustom, a pair no other input has).ValidateProvokingVertexrefusesPROVOKING_VTX_LASTfor 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 withVK_EXT_provoking_vertex, and without it a custom input already counts as reading the provoking vertex).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_barycentricper-vertex inputs (MoltenVK, and devices without the extension). On RADV (HostInterpolationPreferred) the checker runs first and a rejected shader falls back to nativePerVertexKHRinputs; 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:BuildRectListShadersrefuses 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),maind4556df8a8.agc_driver_pixel_barycentric_emulationgainsCheckFirstVertex: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,customand 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
fragmentShaderBarycentricEnabledandhostInterpolationFirst(RADV's setup; compile only, since MoltenVK cannot runPerVertexKHR), 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", noVUID-.negative control (the test change on
main): fails witha 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 withwith 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)$, onmainand on this PR, each after a full build of its tree, with the environment above:mainagc_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)VUID-VkDeviceCreateInfo-sType-unique2 timesSo the change adds no failure and no validation error on this device.
Not run on Linux or Windows.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)