Repository navigation
feat(recompiler): read I/J from barycentric built-ins on MoltenVK - #9193
adriadam10 merged 3 commits into
Conversation
|
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.
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.
|
jotitadev
left a comment
There was a problem hiding this comment.
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.
|
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. What the fallback costs there: the title's sprite/text pixel shader keeps I/J live after its
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: 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.mp4AI-assisted: yes (Claude Code); the measurements and observations are mine. |
e0d5422 to
a127ee3
Compare
a127ee3 to
6222e07
Compare
|
Rebased onto 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, AI-assisted: yes |
6222e07 to
298a9d5
Compare
298a9d5 to
6145b7b
Compare
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|).
a6a6f17 to
03f7c11
Compare
|
@adriadam10 Rebased onto Rechecked on macOS (M4 Pro, MoltenVK 1.4.2, current
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? |
|
Re-checked against the current head: the |
…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.
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.
…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>
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 feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332.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 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 feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #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 aremain27bcd6a merged with #8658 at 4a7efd2, 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