feat(shader): emulate 64-bit image and buffer atomics on devices without them #18842
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/18842"
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 atinseau on 2026-11-12T12:10:20Z.
What
On a device without
VK_EXT_shader_image_atomic_int64orshaderBufferInt64Atomics, such as MoltenVK, a 64-bitimage_atomic_*orbuffer_atomic_*_x2fails SPIR-V emission, and the tests that cover them skip. Reanimal (PPSA23372) stopped there at its first present.Each such atomic now takes one word of a device-wide table of 2^20 lock words (4 MiB), hashed from the texel coordinates or from the guest address of its 8-byte word. It reads and writes its two dwords and releases the word in the same loop iteration; the loop ends once no lane of the host subgroup waits.
R32G32_UINTview declaredCoherent; the placeholder image of anR64_UINTT# uses the same format on such a device.agc_driver_image_atomics64,agc_driver_buffer_atomics_or_swap64andagc_driver_buffer_atomics_inc_dec64run on MoltenVK instead of skipping, with new contention cases of 2,048 threads each, run with 32- and 64-lane waves:image_atomic_addandimage_atomic_umaxon one texel,buffer_atomic_swap_x2on one qword (the returned values and the final value must be a permutation of the initial and the swapped values) andbuffer_atomic_ormixed withbuffer_atomic_or_x2on one qword.Tested
macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with
-DBUILD_TESTING=ON, x86_64 under Rosetta 2.maindoes not run its libraries on macOS, so both sides aremain3b7f31fa7with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests ofmainthat Apple clang refuses; the change side adds this PR's commits.main(MoltenVK has neither feature).VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=layer,ANYPS5_REQUIRE_VULKAN=1andANYPS5_NO_SHADER_CACHE=1(ctest --test-dir build-macos -R '<811 test names>' -V): on the change 707 pass, 68 skip, 35 fail and 1 times out, against 704, 71, 35 and 1 onmain: the three tests above go from skipped to passed and no other test changes status. The failures are the onesmainhas on macOS (MoltenVK limits and macOS-only gaps of the tests, including the nineagc_driver_gds_integer_atomics64*tests, which this PR does not touch). Both runs report the same 77VUID-errors test by test, andwas not found but was requested by env var VK_INSTANCE_LAYERSappears only in the 12 lines that print the failure regex of theagc_driver_image_table_packed*tests. The logs show the loader inserting the layer (Insert instance layer) in 490 tests.python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pycould not compare the open pull requests that conflict withmainand shareVulkanDevice.cpp,Texture.cpp,Pipeline.cpp,Recompiler.cppor the SPIR-V emitters. The open pull requests that mention 64-bit atomics either skip their tests withoutshaderBufferInt64Atomics(#16934, #18655) or implementimage_atomic_incandimage_atomic_decnatively (#16952); none emulates the atomics on a device that lacks them.Not tested: Linux and Windows, and any device that has the 64-bit atomics natively (the emulation is not emitted there).
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)Context
Found while bringing up Reanimal (PPSA23372) on MoltenVK.
Imported from boykopovar/AnyPS5. Originally posted by Ethan-Hainline on 2026-11-12T22:03:52Z.
Review: COMMENTED
I traced the two riskiest paths on
c441deb085; both check out, and I have one question about the loop exit.Lock table publication is correct.
VulkanDevice.cpp:2681creates the table when either 64-bit feature is missing, andVulkanDevice.cpp:4873publishesDeviceAddress()intoContext::atomicLockTablewhile assembling the context. The reset at:1212is in the teardown that also resetspipelineCache, so it cannot leave a cached pipeline holding a freed address.An unserved lane cannot return 0. In
LockedDeviceUpdate(SpirvEmitterHelpers.hpp:311), a lane that loses the CAS reachesattemptMergewithserved=false, sodoneNext=falseandwaiting=true; the ballot keepsagaintrue for that subgroup, and the lane only leaves afterserved=true, whereresultNextis the old value.HostSubgroupSingleLaneuses the lane's own predicate, which is the same rule.Question: the ballot is a subgroup op that can be inside divergent control flow. Atomics are emitted wherever the guest instruction is, so a conditional
if (valid) atomicAddputs the loop, andOpGroupNonUniformBallot, inside a selection. SPIR-V requires group ops in uniform control flow, and on MoltenVK this lowers tosimd_ballot, which expects the whole simdgroup. The loop does not need a collective exit: a lane can leave when its owndoneNextis true. Was the ballot chosen to keep the wave converged, or only for the "no lane waits" property? If it is not required, exiting ondoneNextwould remove the uniformity requirement; if it is, a comment saying why the atomic site is uniform would help.This is a question, not a blocker; the native path and the three hardware reports look right.
AI-assisted review.