fix(agc): drop the vertex gl_Layer output from barycentric patch pipelines #16891
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16891"
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 frankea on 2026-11-07T20:48:58Z.
What
On a device without geometry shaders (MoltenVK), a pixel shader that emulates barycentrics runs behind generated tessellation stages: a pass-through control shader that copies gl_Position, gl_ClipDistance and the parameters the pixel shader reads, and an evaluation shader that selects the corner. When the vertex shader also writes gl_Layer, MoltenVK reads the wrong control-point positions; in the new test none of these triangles is drawn. This PR creates the vertex module of a barycentric patch pipeline with gl_Layer demoted to a private variable. It also creates these pipelines without the VkPipelineCache on MoltenVK, as guest tessellation pipelines already are since
5daac82d32.Why
MoltenVK runs the vertex shader as a compute kernel that captures its outputs to a buffer, and the control shader reads that buffer through its own input struct. The two structs disagree when the vertex shader writes a built-in output that the control shader does not read. The control shader cannot read gl_Layer. MoltenVK passes gl_Layer to SPIRV-Cross as a previous-stage output at the location its reflection gave it, and SPIRV-Cross adds a padding member for it (spirv_msl.cpp, the "add all outputs from the previous stage" loop for tessellation inputs). That member is not marked as a built-in, so the LocationThenBuiltInType sort puts it among the user locations, before gl_Position. The vertex kernel's own struct keeps gl_Layer as a built-in after gl_ClipDistance.
MSL that the local MoltenVK generated (MVK_CONFIG_DEBUG=1) for a vertex shader with four parameters, three clip distances and a layer:
out_param_0..3,gl_Positionat offset 64,gl_ClipDistance[6],gl_Layer, 112-byte records;m_40,m_83,m_88,m_93,uint m_98,gl_Positionat offset 80,gl_ClipDistance[6], 128-byte records.The control shader therefore reads gl_Position from the clip distances, at the wrong stride. In the new test every layer draw leaves all of its pixels unwritten, with a fresh pipeline cache too, so it needs no cache reuse.
Dropping the layer changes nothing that is rendered. In Vulkan the layer comes from the last pre-rasterization stage. The generated evaluation shader writes no layer, so these draws already render to the first layer on every driver.
The pipeline cache change is needed as well. MoltenVK reuses a cached shader conversion when the new pipeline has every interface entry that the cached conversion marked as used (
SPIRVToMSLConversionConfiguration::matches,outIsUsedByShader), and the padding members are not marked as used. The generated control shader is the same for every vertex shader that feeds the same parameters. A control conversion padded for gl_Layer in a VkPipelineCache saved before this change therefore still matches after it. Measured: a persisted pipeline cache written by main, then loaded by a build with only the first commit, still leaves the one-parameter layer draws of the new test unwritten (6 failed checks; the four-parameter cases pass). With the second fix the same stale cache file passes all 8 cases. The new test itself does not exercise this, because it starts without a persisted cache.Found on Gran Turismo 7 (PPSA01317) on an M5 Max. Seven forward-blended glass draws in the cafe scene, from vertex shaders 0x82c4402700 and 0x82c440ba00, export four parameters, three clip distances and gl_Layer. On the barycentric tessellation route they draw white wedges that converge at the screen centre. When the same pixel shaders read PerVertexKHR inputs natively (no generated stages), the wedges go away. The guest vertex data was dumped and evaluated, and it is correct. Confirmed in game on the M5 Max (macOS 27.0.1, local MoltenVK 1.4.2): a local build with both fixes, on the default emulated barycentric route, reached the cafe presentation with no white wedges and the car glass visible. Over 30 frame dumps per dialog line, the median share of fully clamped white pixels where the wedges appear was 0.5%, against 47% with the same local build without the two fixes. So the layout mismatch caused the wedges.
Tested
macOS 27.0.1 on an Apple M5 Max (Rosetta x86-64 build), with MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0 and with a local MoltenVK 1.4.2 build. All runs used
ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1, on both ICDs.agc_driver_barycentric_patch_vertex_layer. It draws a triangle through the generated stages, reading per-vertex values with v_interp_mov, for vertex shaders with and without gl_Layer, next to clip distances and up to four parameters. Each draw runs once with a fresh pipeline cache and once after the other vertex shader's pipeline seeded the cache. Every pixel's coverage and values are checked.agc_driver_pixel_barycentric_emulation,agc_driver_rect_list_corner,agc_driver_pipeline_journal,agc_driver_layered_array_color_draw,agc_driver_layered_volume_draw,agc_driver_zero_position_vertex,agc_driver_vertex_clip_cull_draw,agc_tessellation_pipeline_cache,agc_fragment_barycentric;APS5_NO_GPL=1:agc_driver_draw_cache_reuse,agc_driver_draw_cache_relocation,agc_driver_draw_data_hit_user_words;agc_driver_mesh_barycentric_emulationandagc_driver_tessellation_geometry(no mesh or geometry shaders).agc_driver_tessellation: the hull's relative patch index across instances;agc_driver_rect_list_indirect: rect-list position refusal;agc_driver_provoking_vertex: provokingVertexLast is unsupported.VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(local ICD), main and this PR, the layer loaded in every run:pixel_barycentric_emulation,rect_list_corner,zero_position_vertex,layered_array_color_drawandvertex_clip_cull_draw;VUID-: the only ones are 2VUID-VkGraphicsPipelineCreateInfo-topology-08773inzero_position_vertex, present on both.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)Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T22:36:04Z.
Review: COMMENTED
Non-MoltenVK cells, to check the demoted gl_Layer and the generated stages on drivers that read the interface correctly. Head
673188baf4merged ontomain302864d48c(clean). Windows 11 x64, WinLibs GCC 15.2.0 r7, Release,ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1, 3 runs per GPU.agc_driver_barycentric_patch_vertex_layer(new)agc_driver_pixel_barycentric_emulationmainmainagc_fragment_barycentricmainmainagc_driver_mesh_barycentric_emulationmainmainThe new test builds the tessellation stages itself through
BuildBarycentricTessellationShaders, so it does exercise the patch pipeline here even though these devices have geometry shaders. So the change keeps every case right on two drivers that never had the mismatch; it can't show the MoltenVK fix itself. No validation layer on this machine.AI-assisted: yes.