Skip to content

feat(recompiler): read I/J from barycentric built-ins on MoltenVK - #9193

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
imellag:feat/shader-barycentric-builtins-moltenvk
Oct 27, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
imellag:feat/shader-barycentric-builtins-moltenvk

Conversation

@imellag

@imellag imellag commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

On MoltenVK a pixel shader whose I/J VGPRs outlive their v_interp_p1_f32/v_interp_p2_f32 pairs now reads them from BaryCoordKHR or BaryCoordNoPerspKHR instead of taking the generated barycentric stages.

MoltenVK exposes VK_KHR_fragment_shader_barycentric, but its MSL step rejects PerVertexKHR, so FragmentShaderBarycentricUsable turns the extension off there and every such shader takes the generated stages. Without geometry shaders those accept only a triangle list without primitive restart, so other draws stop with a pixel shader that reads barycentrics without VK_KHR_fragment_shader_barycentric or geometry shaders needs a triangle list without primitive restart. Planet of Lana (PPSA19450) stops at its first such draw on main + #8658.

  • A new SpirvTarget flag, fragmentBarycentricBuiltins, enables the extension for its built-ins only. It is set wherever the device offers the extension, so devices where FragmentShaderBarycentricUsable keeps it on behave as before.
  • With the flag, HostInterpolationChecker also accepts I/J reads left after the pairs are lowered, as long as they are all perspective or all linear (Metal takes one barycentric built-in per shader) and read at the fragment's default position. Raw vertex parameters and I/J at an offset or a sample position still take the generated stages. The interpolated attributes stay hardware varyings.
  • Apple GPUs return the perspective weights unnormalized unless the compiled fragment function reads [[position]], so gl_FragCoord is declared with the BaryCoordKHR built-in and each perspective read returns gl_FragCoord.w > 0 ? b : 0. w is positive for every rasterized fragment, so the weight is unchanged, and unlike min(b, b + |gl_FragCoord.w|) a compiler cannot fold the read away. gl_FragCoord.w is loaded through EmitBuiltinU32, a float load, instead of a uint pointer into the float input, which adriadam10 pointed out on feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332.
  • The flag is part of the shader cache key and of the request serialization (version 48).

This is yripper's 376079a from the closed #7332 (originally 508a894), ported to current main, with him as co-author. Changes from the original: the I/J check uses BarycentricInputOf and falls back for the offset and sample kinds main added since, the gl_FragCoord.w load above, the code comment moved into docs/dev/debt/shader.md, and the execution test follows main's Draw/RunEmulation structure.

Overlap: the serialization version and its checks share lines with #10359, which also needs a new version and goes first as agreed there; this PR then takes the next number. main is at version 47 (#10515), so this PR uses 48 until then. #11551 also raises the request format and lands after this PR; PixelBarycentricEmulation.cpp and the shader.md entry share lines with #8083 and #8339, the shader.md entry with #8281, and Recompiler.hpp one line with #8489.

Tested

Current head 03f7c1134f, rebased onto main fe2b52c7e4. main took request format 47 in #10515, so this PR now uses 48; the commit that only moved fixtures to 47 became empty and was dropped. On macOS (same machine, current main with the open macOS boot PRs and a local patch that loads libvulkan.1.dylib), with the same variables and the validation layer loaded, agc_driver_pixel_barycentric_emulation passes 3 of 3 and reads I/J from the built-ins (932 perspective and 932 linear I/J pixels), and request_serializer, agc_shader_memory, agc_fragment_barycentric and agc_driver_interpolation_modes pass 3 of 3, with no VUID- lines.

Previous head, rebased onto main 8a85a5c848, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (ctest -V, layer loaded, no missing-layer line in any log):

  • Linux x86-64 (Ubuntu 24.04 amd64 container on the same Mac, GCC 14.2, Release, lavapipe from Mesa 25.2.8, validation layer 1.3.275), paired with main 8a85a5c848 in the same container: request_serializer, agc_driver_graphics, agc_driver_depth_bounds_state, agc_driver_pixel_barycentric_emulation, agc_shader_memory and the three agc_shader_control_flow tests pass on both. The only validation message on both sides is VUID-VkDeviceCreateInfo-pNext-pNext at device creation. lavapipe has no barycentric extension, so the built-in case does not run there.
  • macOS, Apple M4 Pro, macOS 27.0.1, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2. main does not build on macOS, so both arms are feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #8658's feat/macos-port 564e005acb, without and with this change (its serializer hunks left out, since that branch is on an older format), 3 runs each: agc_driver_pixel_barycentric_emulation passes on both, and with the change also its built-in case (932 perspective and 932 linear I/J pixels from BaryCoordKHR/BaryCoordNoPerspKHR); agc_driver_graphics, agc_driver_depth_bounds_state, agc_shader_memory, agc_driver_interpolation_modes and agc_fragment_barycentric pass on both; agc_driver_mesh_barycentric_emulation is skipped on both. Validation: VUID-VkDeviceCreateInfo-pProperties-04451, 6 lines on each side.
  • Controls on that base: the previous head fails the built-in case 3 of 3 (pixel (16, 6) channel 0 is 0.009037, expected 0.004731 or 0.004730, as on @iamthehimansh's M2 Pro), because gl_FragCoord was declared only for the offset kinds and its w was read as the constant 0; declaring it alone makes it pass; the select without the declaration fails (... is 0.000000).

Earlier runs, before the gl_FragCoord fix and the last rebase:

Apple M4 Pro, macOS 27.0.1, x86-64 under Rosetta 2, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), Apple clang 21.0.0. main does not build on macOS, so both arms are main 27bcd6a merged with #8658 at 4a7efd2, without and with this change, same build directory.

Focused tests with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (ctest -V, layer loaded, no missing-layer line in either log):

test base change
request_serializer pass pass
agc_shader_memory pass pass
agc_driver_graphics pass pass
agc_driver_pixel_barycentric_emulation pass (tessellation stages: 932 perspective and 932 linear I/J pixels) pass, plus the new built-in case: 932 perspective and 932 linear I/J pixels from BaryCoordKHR/BaryCoordNoPerspKHR
agc_driver_interpolation_modes pass pass
agc_fragment_barycentric pass pass
agc_driver_interpolation_f16, agc_driver_mesh_barycentric_emulation skipped skipped

Validation: the same single identifier in both logs, VUID-VkDeviceCreateInfo-pProperties-04451 (8 lines each), so no new VUID- errors.

Negative controls on the change:

  • Without the gl_FragCoord.w use, the built-in case fails: PERSP_CENTER I/J from BaryCoordKHR: pixel (16, 6) channel 0 is 0.009037, expected 0.004731 or 0.004730.
  • With the built-ins refused in HostInterpolationChecker, agc_driver_graphics fails: reading the I/J VGPRs with barycentric built-ins did not use BaryCoordKHR.

Titles: with this change on main + #8658, Planet of Lana gets past the draw above; both it and God of War Sons of Sparta (PPSA28997) then stop at scene load with an uncaught Il2CppExceptionWrapper, reported on #8658, so there is no gameplay run on main yet. On the closed #7332 branch, which carries the original commit, both titles reach gameplay on this machine.

Linux x86-64 (Ubuntu 24.04 amd64 container on the same Mac, GCC 14.2, lavapipe/llvmpipe from LLVM 20.1.2, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1): request_serializer, agc_shader_memory, agc_driver_graphics and agc_driver_pixel_barycentric_emulation pass; lavapipe has no barycentric extension, so the last one runs the geometry and the tessellation emulation (932 perspective and 932 linear I/J pixels each) and not the new built-in case.

Not tested: Windows. On a device that lacks the extension the flag is off; on one where FragmentShaderBarycentricUsable keeps it, the shader takes the full extension path as before and the flag only enters the cache key.

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.0.1; lavapipe (llvmpipe, LLVM 20.1.2), Ubuntu 24.04 container
  • AI-assisted: yes

cc @yripper

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10348 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp L5743, L5861, L6247-6248
core/shader/recompiler/tests/RequestSerializerTests.cpp L301, L344, L373, L506, L532-534, L549, L576, L604, L614, L741
PR 10547 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp L385
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp L5743, L5861, L6020, L6182, L6247-6248
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp L856, L896, L905, L963
core/shader/recompiler/Recompiler.hpp L333
core/shader/recompiler/tests/RequestSerializerTests.cpp L301, L309, L344, L352, L373, L380, L506, L516, L532-534, L549, L576, L586, L604-605, L614, L729, L741, L767, L782, L821
PR 11351 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp L5743, L5861, L6020, L6182, L6247-6248
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp L856, L896, L905, L963
core/shader/recompiler/Recompiler.hpp L333
core/shader/recompiler/tests/RequestSerializerTests.cpp L301, L309, L344, L352, L373, L380, L506, L516, L532-534, L549, L576, L586, L604, L614, L729, L741, L767, L782, L821
PR 11684 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp L6015-6016

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1395 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 2434 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 4040 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 5021 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 5371 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
docs/dev/debt/shader.md
PR 5609 core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 5925 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/Recompiler.hpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/shader/recompiler/Recompiler.hpp
PR 6194 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6235 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 6280 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/shader.md
PR 6412 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
docs/dev/debt/shader.md
PR 6481 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6488 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 6548 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6804 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6808 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 6924 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 6960 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 6970 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/Recompiler.hpp
PR 6977 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 7256 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 7275 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 7369 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 7395 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 7410 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 7449 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PixelBarycentricEmulation.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 7684 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
docs/dev/debt/shader.md
PR 7893 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 7922 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
docs/dev/debt/shader.md
PR 8004 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 8395 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 8939 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9020 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 9335 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 9356 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 9431 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9432 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9457 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9489 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PixelBarycentricEmulation.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 9633 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9672 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9673 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9674 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9675 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 9676 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9700 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 9748 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 9749 core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9790 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 9842 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9852 core/shader/recompiler/Recompiler.hpp
PR 9889 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
docs/dev/debt/shader.md
PR 9891 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9892 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9893 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9895 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9897 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 9898 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 9969 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.cpp
docs/dev/debt/shader.md
PR 10000 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
docs/dev/debt/shader.md
PR 10043 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 10049 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/shader/recompiler/Recompiler.hpp
PR 10193 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/execution/PixelBarycentricEmulation.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
PR 10233 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
docs/dev/debt/shader.md
PR 10359 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
PR 10435 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 10442 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 10489 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
docs/dev/debt/shader.md
PR 10678 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 10757 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
PR 10803 docs/dev/debt/shader.md
PR 10846 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 10867 core/libs/prx/libSceAgcDriver/Execution/src/ShaderDeviceProfile.cpp
core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
docs/dev/debt/shader.md
PR 10940 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 11435 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 11436 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
PR 11451 core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
PR 11483 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/CacheKey.hpp
core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp
core/shader/recompiler/Recompiler.cpp
core/shader/recompiler/Recompiler.hpp
core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp
core/shader/recompiler/tests/RequestSerializerTests.cpp
docs/dev/debt/shader.md
PR 11557 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
docs/dev/debt/shader.md
PR 11920 core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apple GPU / MoltenVK run of this PR, which is exactly the host it targets.

Machine: Apple M2 Max (vendor=0x106b device=0x1b020208), macOS 27.2, x86_64 under Rosetta 2. Vulkan loader + MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0); also re-run against MoltenVK main + SPIRV-Cross #2704. Release, BUILD_TESTING=ON.

Base/method: PR #8658 feat/macos-port @ 4ebe71f2e5 (current main 0c6a5bd593 plus the macOS port), not main — main does not build on macOS. This PR's diff (git diff merge-base..e0d54227c -- . ':!docs') applied on top, clean. Device reports pipelineLibrary=0, tessellationAvailable=1, mesh=0.

Result (agc_driver_pixel_barycentric_emulation, --repeat until-fail:3, both drivers): passes 3/3 on each. The new built-in block runs:

barycentric built-in tests passed: 932 perspective and 932 linear I/J pixels

Both Require(!builtin.barycentricEmulation.active, ...) checks hold, so on MoltenVK the I/J VGPRs are read from BaryCoordKHR / BaryCoordNoPerspKHR and no generated barycentric stage is produced. agc_driver_graphics and agc_shader_memory still pass 3/3, and request_serializer passes with the serialization flag moved.

Negative control (SDK and #2704): with only HostInterpolationChecker.cpp reverted to this PR's base (the built-in acceptance removed, signature kept), agc_driver_pixel_barycentric_emulation fails:

AGC graphics: reading PERSP_CENTER I/J with barycentric built-ins needed generated stages

So the lowering in HostInterpolationChecker::Lower is what lets MoltenVK take the built-in path; without it the draw falls back to generated stages, which is the case the PR describes (Planet of Lana stops at its first such draw on main + #8658).

Full ^agc suite (SDK, with #9125 also stacked in the same build): 16 failures vs 18 at base; the only delta is the two tests those PRs address, no new failure. So the shader changes add nothing to the failing set on Apple.

GPUs tested: Apple M2 Max (MoltenVK 1.4.2; and MoltenVK main + SPIRV-Cross #2704).

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@krishalivaibhav

krishalivaibhav commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested this on a fanless Apple M2 with Balatro (PPSA21402), where the same fallback is the main cost of the shop and booster-pack screens. It removes it.

Setup: MacBook Air M2, macOS 27.2, MoltenVK 1.4.2, x86-64 under Rosetta 2, 1920x1080. main does not build on macOS, so all three arms are the same macOS series build (42c3fd0 plus local macOS changes, hence dirty). The #9193 arm adds this PR's runtime files at e0d5422; on that older base the conflicts were only fields it does not have (waveLdsMemoryBarriers, float32DenormPreserve), and the request serialization takes local version 39 instead of 43. Scene: Continue into a saved run's shop/pack screen, untouched. Warm caches, APS5_TRACE_SECONDS=60:20, no other trace or profile switch.

What the fallback costs there: the title's sprite/text pixel shader keeps I/J live after its v_interp pairs, only as the value lanes outside the export mask keep (a select on a WQM of the export mask). On MoltenVK every such draw goes through the generated tessellation stages, which restarts the Metal render encoder per draw; the pack screen issues about 730 of them per frame. Clipping those draws to a 1x1 scissor changed nothing, and skipping only their vkCmdDraw took the screen from about 11 to about 34 FPS, so the cost is per draw, not per pixel.

Same scene, 60–80 s base narrower local fix this PR
presents / FPS 908 / 45.4 1192 / 59.6 1199 / 60.0
frame p50 / p90 / p99 / max 21.7 / 23.8 / 25.4 / 79.4 ms 16.7 / 17.9 / 23.9 / 55.1 ms 16.7 / 17.8 / 18.4 / 18.6 ms
GPU busy per present 21.3 ms 13.7 ms 12.4 ms

The "narrower local fix" is something I had written before finding this PR: it proves the leftover I/J only reaches lanes that never export and replaces it with 0. It covers 4 of the title's pixel programs; this PR also covers the two it cannot prove (a CRT post-process and one more), and after it only the title's rect-list draws still use tessellation. I'm not opening mine, since this PR handles the case more generally.

Base:

{"commit":"42c3fd09a2b6","dirty":true,"env":{"APS5_SYNC_COMMAND_ALLOCATIONS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"60:20"},"gpu":"Apple M2","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","resolution":"1920x1080","window":{"spec":"seconds 60:20","first_present":2945,"ms":20005.583},"presents":908,"frame_ms":{"p50":21.730,"p90":23.802,"p99":25.384,"max":79.376},"gpu_busy_ms_per_present":21.344,"worker_busy_pct":{"queue worker 0x0":98.854},"top_phases_ms_per_frame":{"Driver.Packet":21.777,"VideoOut.Flip":20.737,"Driver.Present":20.017,"VideoOut.Flip.present":19.996,"Driver.Present.render_fence_wait":18.713,"Vulkan.Retire":18.711,"Vulkan.Retire.wait":18.696,"Driver.Packet.rendering_wait":13.372},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":17.007,"events_lost":45073,"totals_full":false}},"trace":"aps5-trace-42c3fd09a2b6-21429.json.gz"}

This PR:

{"commit":"42c3fd09a2b6","dirty":true,"env":{"APS5_SYNC_COMMAND_ALLOCATIONS":"1","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"60:20"},"gpu":"Apple M2","driver":"MoltenVK 1.4.2","os":"Darwin 27.2.0","resolution":"1920x1080","window":{"spec":"seconds 60:20","first_present":3573,"ms":20003.296},"presents":1199,"frame_ms":{"p50":16.687,"p90":17.790,"p99":18.403,"max":18.578},"gpu_busy_ms_per_present":12.400,"worker_busy_pct":{"queue worker 0x0":98.477},"top_phases_ms_per_frame":{"Driver.Packet":16.425,"VideoOut.Flip":16.250,"VideoOut.Flip.vblank_wait":12.344,"Driver.Packet.rendering_wait":12.056,"Driver.Packet.draw":3.349,"Driver.Draw":3.307,"VideoOut.Flip.present":3.263,"Driver.Present":3.261},"trace_dropped":false,"trace":"aps5-trace-42c3fd09a2b6-44665.json.gz"}

Traces: aps5-trace-42c3fd09a2b6-21429.json.gz (base; its event buffer filled after 17 of 20 s, the summary line covers the window) and aps5-trace-42c3fd09a2b6-44665.json.gz (this PR); I can attach both if useful.

Rendering looked correct in the scene (text, cards, CRT effect). Clip of this PR's build in normal play, FPS in the title bar:

balatro-m2-pr9193-60fps.mp4

AI-assisted: yes (Claude Code); the measurements and observations are mine.

@imellag

imellag commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 6a6a35fdf5, now 6222e0738d. On the way it met #8083 (barycentric triangle strips): the test helper Draw keeps main's new strip parameter and takes builtins after it, so the two built-in draws pass false, true; the debt entry keeps main's wording for strips and adds this PR's paragraph after it. No runtime file changed.

Paired run on that base, same container and device for both sides (Ubuntu 24.04 x86-64, GCC 14.2, Release, lavapipe from Mesa 25.2.8, validation layer 1.3.275, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -V): request_serializer, agc_shader_memory, agc_driver_graphics and agc_driver_pixel_barycentric_emulation pass on main and on the PR, and the only validation message on both sides is VUID-VkDeviceCreateInfo-pNext-pNext at device creation (Ubuntu's layer does not know one structure in the device chain). lavapipe has no barycentric built-ins, so the built-in path itself still rests on the MoltenVK runs above.

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@imellag
imellag force-pushed the feat/shader-barycentric-builtins-moltenvk branch from 6222e07 to 298a9d5 Compare October 23, 2026 10:43
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 23, 2026
@imellag
imellag force-pushed the feat/shader-barycentric-builtins-moltenvk branch from 298a9d5 to 6145b7b Compare October 23, 2026 13:29
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 23, 2026
@adriadam10 adriadam10 self-assigned this Oct 23, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 27, 2026
imellag and others added 3 commits October 27, 2026 07:25
MoltenVK exposes VK_KHR_fragment_shader_barycentric but its MSL step
rejects PerVertexKHR, so the extension is disabled there and a pixel
shader whose I/J VGPRs outlive their v_interp_p1/p2 pairs takes the
generated barycentric stages, which accept only triangle lists without
primitive restart on a device without geometry shaders.

A new target flag, fragmentBarycentricBuiltins, enables the extension
for its built-ins only. The host interpolation check then keeps the
leftover I/J reads (all perspective or all linear, at the fragment's
default position) and reads them from BaryCoordKHR or
BaryCoordNoPerspKHR, while the interpolated attributes stay hardware
varyings. Raw vertex parameters and I/J at an offset or a sample
position still take the generated stages. Apple GPUs return
unnormalized perspective weights unless the fragment function reads
[[position]], so each perspective read keeps a neutral use of
gl_FragCoord.w, loaded as a float. Devices with the full extension are
unaffected; the request serialization moves to version 45.

Co-authored-by: yripper <hello@joaquinsoza.dev>
…tures

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The I/J reads take the BaryCoordSmooth input, but gl_FragCoord was declared
only for the offset kinds, so its w was read as the constant 0 and the
fragment function never read [[position]]; Apple GPUs then return the
weights unnormalized. Declare it with the built-in, and select on
gl_FragCoord.w > 0, which a compiler cannot fold as it can
min(b, b + |w|).
@imellag

imellag commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@adriadam10 Rebased onto main fe2b52c7e4 (head 03f7c1134f). main took request format 47 in #10515, so this PR now uses 48. The order with #10359 stands: if it lands first, this PR moves to the next number. The last commit only moved fixtures to 47, so it became empty and is gone.

Rechecked on macOS (M4 Pro, MoltenVK 1.4.2, current main with the open macOS boot PRs, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, validation layer loaded):

  • agc_driver_pixel_barycentric_emulation passes 3 of 3 and reads I/J from the built-ins (932 perspective and 932 linear I/J pixels).
  • request_serializer, agc_shader_memory, agc_fragment_barycentric and agc_driver_interpolation_modes pass 3 of 3.
  • No VUID- lines.

Both points of your review were addressed at the previous head, and @iamthehimansh confirmed the fix on the M2 Pro. Could you take another look?

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026
@adriadam10
adriadam10 merged commit 6f7cb75 into boykopovar:main Oct 27, 2026
15 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked against the current head: the gl_FragCoord declaration for the perspective built-in is now in ShaderInfoCollector.cpp, the read is gl_FragCoord.w > 0 ? b : 0 through a float load, and @iamthehimansh confirmed agc_driver_pixel_barycentric_emulation passes 3/3 on the M2 Pro. Request serialization version 48, its fixtures, the cache key and the device-profile check are consistent, the I/J check still sends offset and sample kinds to the generated stages, and the tests fail without the change; merged.

oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 27, 2026
…on 49

Main took request serialization version 48 for the fragment barycentric
built-ins flag (boykopovar#9193). The depth export clamp bounds are now written at
version 49 and read only for versions from 49, so a version 48 request as main
writes it still decodes, with default bounds. Tests: version 49 is the
emitted prefix, 50 the first unsupported one, a depth exporting request
without its bounds decodes at 48, 47 and 46 (the last two without main's
barycentric byte), and a version 48 two-sample dithered request decodes as
written.
oneandonlydean added a commit that referenced this pull request Oct 28, 2026
The DB clamps a pixel shader's exported depth to PA_SC_VPORT_ZMIN_0 and
ZMAX_0 unless DB_RENDER_OVERRIDE.DISABLE_VIEWPORT_CLAMP is set. Vulkan only
clamps with depthClamp, which also drops clipping, so the exported value
reached the depth attachment unclamped.

The pixel stage info now carries the two bounds when the shader exports
depth (0 and 1 otherwise), and the exported value is NClamped to them
before it is stored to gl_FragDepth. With
DISABLE_VIEWPORT_CLAMP the value is stored as exported; non-finite or
inverted bounds are rejected. DB_RENDER_OVERRIDE joins the draw key
registers. The bounds
are part of the recompile cache key (only for depth-exporting shaders, so
other keys do not change), the draw key and its register check, and
recompile requests (serialization version 47).

Tests: the pixel stage decode reads the bounds, passes the disable flag through
and rejects bad bounds, and leaves a draw without depth export at 0 and 1; a
recompiled depth export stores the NClamped value, and its bounds survive
serialization and key the cache.

Request version 47: main is at 44 (#8612) and open PRs also claim 44 to 46 (#8564, #7888, #9193 and the #9020, #9335, #9748 stack). Whichever of these lands second renumbers.
oneandonlydean pushed a commit that referenced this pull request Oct 28, 2026
…ation test

#9193 added fragmentBarycentricBuiltins and cleared it in
PixelBarycentricEmulation, but MeshBarycentricEmulation still takes
the flag from the device. On a device with
VK_KHR_fragment_shader_barycentric the host interpolation check then
reads I/J from BaryCoordKHR, the pixel shader does not emulate
barycentrics, and the test fails before its first draw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants