feat(recompiler): read I/J from barycentric built-ins on MoltenVK #9193
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9193"
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-10-21T14:50:13Z.
What
On MoltenVK a pixel shader whose I/J VGPRs outlive their
v_interp_p1_f32/v_interp_p2_f32pairs now reads them fromBaryCoordKHRorBaryCoordNoPerspKHRinstead of taking the generated barycentric stages.MoltenVK exposes
VK_KHR_fragment_shader_barycentric, but its MSL step rejectsPerVertexKHR, soFragmentShaderBarycentricUsableturns 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 witha 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 onmain+ #8658.SpirvTargetflag,fragmentBarycentricBuiltins, enables the extension for its built-ins only. It is set wherever the device offers the extension, so devices whereFragmentShaderBarycentricUsablekeeps it on behave as before.HostInterpolationCheckeralso 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.[[position]], sogl_FragCoordis declared with theBaryCoordKHRbuilt-in and each perspective read returnsgl_FragCoord.w > 0 ? b : 0.wis positive for every rasterized fragment, so the weight is unchanged, and unlikemin(b, b + |gl_FragCoord.w|)a compiler cannot fold the read away.gl_FragCoord.wis loaded throughEmitBuiltinU32, a float load, instead of auintpointer into the float input, which adriadam10 pointed out on #7332.This is yripper's
376079ada4from the closed #7332 (originally508a8947b), ported to currentmain, with him as co-author. Changes from the original: the I/J check usesBarycentricInputOfand falls back for the offset and sample kindsmainadded since, thegl_FragCoord.wload above, the code comment moved intodocs/dev/debt/shader.md, and the execution test followsmain'sDraw/RunEmulationstructure.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.
mainis at version 47 (#10515), so this PR uses 48 until then. #11551 also raises the request format and lands after this PR;PixelBarycentricEmulation.cppand theshader.mdentry share lines with #8083 and #8339, theshader.mdentry with #8281, andRecompiler.hppone line with #8489.Tested
Current head
03f7c1134f, rebased ontomainfe2b52c7e4.maintook 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, currentmainwith the open macOS boot PRs and a local patch that loadslibvulkan.1.dylib), with the same variables and the validation layer loaded,agc_driver_pixel_barycentric_emulationpasses 3 of 3 and reads I/J from the built-ins (932 perspective and 932 linear I/J pixels), andrequest_serializer,agc_shader_memory,agc_fragment_barycentricandagc_driver_interpolation_modespass 3 of 3, with noVUID-lines.Previous head, rebased onto
main8a85a5c848, withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(ctest -V, layer loaded, no missing-layer line in any log):main8a85a5c848in the same container:request_serializer,agc_driver_graphics,agc_driver_depth_bounds_state,agc_driver_pixel_barycentric_emulation,agc_shader_memoryand the threeagc_shader_control_flowtests pass on both. The only validation message on both sides isVUID-VkDeviceCreateInfo-pNext-pNextat device creation. lavapipe has no barycentric extension, so the built-in case does not run there.maindoes not build on macOS, so both arms are #8658'sfeat/macos-port564e005acb, 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_emulationpasses on both, and with the change also its built-in case (932 perspective and 932 linear I/J pixels fromBaryCoordKHR/BaryCoordNoPerspKHR);agc_driver_graphics,agc_driver_depth_bounds_state,agc_shader_memory,agc_driver_interpolation_modesandagc_fragment_barycentricpass on both;agc_driver_mesh_barycentric_emulationis skipped on both. Validation:VUID-VkDeviceCreateInfo-pProperties-04451, 6 lines on each side.pixel (16, 6) channel 0 is 0.009037, expected 0.004731 or 0.004730, as on @iamthehimansh's M2 Pro), becausegl_FragCoordwas declared only for the offset kinds and itswwas 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_FragCoordfix 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.
maindoes not build on macOS, so both arms aremain27bcd6a29merged with #8658 at4a7efd236, without and with this change, same build directory.Focused tests with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(ctest -V, layer loaded, no missing-layer line in either log):request_serializeragc_shader_memoryagc_driver_graphicsagc_driver_pixel_barycentric_emulationBaryCoordKHR/BaryCoordNoPerspKHRagc_driver_interpolation_modesagc_fragment_barycentricagc_driver_interpolation_f16,agc_driver_mesh_barycentric_emulationValidation: the same single identifier in both logs,
VUID-VkDeviceCreateInfo-pProperties-04451(8 lines each), so no newVUID-errors.Negative controls on the change:
gl_FragCoord.wuse, 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.HostInterpolationChecker,agc_driver_graphicsfails: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 uncaughtIl2CppExceptionWrapper, reported on #8658, so there is no gameplay run onmainyet. 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_graphicsandagc_driver_pixel_barycentric_emulationpass; 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
FragmentShaderBarycentricUsablekeeps it, the shader takes the full extension path as before and the flag only enters the cache key.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)cc @yripper
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-21T22:45:33Z.
Review: COMMENTED
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(currentmain0c6a5bd593plus the macOS port), notmain—maindoes not build on macOS. This PR's diff (git diff merge-base..e0d54227c -- . ':!docs') applied on top, clean. Device reportspipelineLibrary=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:Both
Require(!builtin.barycentricEmulation.active, ...)checks hold, so on MoltenVK the I/J VGPRs are read fromBaryCoordKHR/BaryCoordNoPerspKHRand no generated barycentric stage is produced.agc_driver_graphicsandagc_shader_memorystill pass 3/3, andrequest_serializerpasses with the serialization flag moved.Negative control (SDK and #2704): with only
HostInterpolationChecker.cppreverted to this PR's base (the built-in acceptance removed, signature kept),agc_driver_pixel_barycentric_emulationfails:So the lowering in
HostInterpolationChecker::Loweris 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 onmain+ #8658).Full
^agcsuite (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.
Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-22T06:30:45Z.
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.
maindoes not build on macOS, so all three arms are the same macOS series build (42c3fd09a2b6 plus local macOS changes, hencedirty). The #9193 arm adds this PR's runtime files at e0d54227c8; 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_interppairs, 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 theirvkCmdDrawtook the screen from about 11 to about 34 FPS, so the cost is per draw, not per pixel.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:
This PR:
Traces:
aps5-trace-42c3fd09a2b6-21429.json.gz(base; its event buffer filled after 17 of 20 s, the summary line covers the window) andaps5-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:
https://github.com/user-attachments/assets/58f8da7a-3971-492c-9f15-5de422a3bcf2
AI-assisted: yes (Claude Code); the measurements and observations are mine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T03:54:24Z.
Review: CHANGES_REQUESTED
The head is rebased onto current
main(serialization version 47, fixtures adjusted) and merges cleanly, and the I/J check, cache key and serialization read correctly. One open problem blocks the merge:agc_driver_pixel_barycentric_emulationfails 3/3 on an Apple M2 Pro (MoltenVK 1.4.2) with this PR and passes without it:PERSP_CENTER I/J from BaryCoordKHR: pixel (16, 6) channel 0 is 0.009037, expected 0.004731 or 0.004730. That is the same message as your negative control without thegl_FragCoord.wuse, so on that machine the normalization is not taking effect, while it does on the M4 Pro, M2 Max and M2 runs. Please reproduce or explain the difference (MoltenVK/SPIRV-Cross build, macOS version, fast-math setting).min(b, b + |gl_FragCoord.w|)equalsbin exact arithmetic, so a compiler that folds it (Metal with fast math) drops the[[position]]read, and the weights come back unnormalized. The fix needs a dependence onwthat cannot be folded, or a way to detect that it was; as written the result depends on the Metal compiler not optimizing it.Once the test passes on that M2 Pro as well, this is ready. Note also that #10359 takes serialization version 47; whichever merges second moves to 48.