diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal b/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal new file mode 100644 index 000000000..612fb9ef5 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/coherent_texture_handoff.metal @@ -0,0 +1,63 @@ +// metal-std: metal4.1 +// metal-macos-min: 27.0 +// Owned fixture: a texture of MSL's `memory_coherence_device` handed between threadgroups. In a +// dispatch of two threadgroups of 64 threads over a 64 x 1 texture, each thread of threadgroup 0 +// writes its texel, fences texture memory at device scope with release order and raises a +// device-scope flag (a relaxed atomic add); each thread of threadgroup 1 loads the flag a bounded +// number of times, fences texture memory at device scope with acquire order and reads the texel of +// its own index. It stores 2 where it saw every writer's flag and still read the texel's earlier +// value, and 1 otherwise: under device coherence every word is 1, however the threadgroups are +// scheduled, and no thread waits without bound. `coherent_texture_handoff_seen` is the same +// handoff storing each reader's flag count and texel, for the runtime's device test to count the +// reads that saw every writer's flag and, of those, the ones that read the written texel. The +// fences are MSL 4.1's release and acquire: the runtime refuses a `seq_cst` fence beyond the +// thread (a known gap its README records). +#include +using namespace metal; + +kernel void coherent_texture_handoff( + texture2d tex [[texture(0)]], + device atomic_uint *flag [[buffer(0)]], + device uint *out [[buffer(1)]], + uint group [[threadgroup_position_in_grid]], + uint i [[thread_position_in_threadgroup]]) { + const uint WRITERS = 64; + const uint TRIES = 4096; + uint2 at = uint2(i, 0); + if (group == 0) { + tex.write(uint4(1000 + i), at); + atomic_thread_fence(mem_flags::mem_texture, memory_order_release, thread_scope_device); + atomic_fetch_add_explicit(flag, 1, memory_order_relaxed); + return; + } + uint seen = 0; + for (uint k = 0; k < TRIES && seen < WRITERS; k++) { + seen = atomic_load_explicit(flag, memory_order_relaxed); + } + atomic_thread_fence(mem_flags::mem_texture, memory_order_acquire, thread_scope_device); + uint v = tex.read(at).x; + out[i] = (seen == WRITERS && v != 1000 + i) ? 2 : 1; +} + +kernel void coherent_texture_handoff_seen( + texture2d tex [[texture(0)]], + device atomic_uint *flag [[buffer(0)]], + device uint2 *out [[buffer(1)]], + uint group [[threadgroup_position_in_grid]], + uint i [[thread_position_in_threadgroup]]) { + const uint WRITERS = 64; + const uint TRIES = 4096; + uint2 at = uint2(i, 0); + if (group == 0) { + tex.write(uint4(1000 + i), at); + atomic_thread_fence(mem_flags::mem_texture, memory_order_release, thread_scope_device); + atomic_fetch_add_explicit(flag, 1, memory_order_relaxed); + return; + } + uint seen = 0; + for (uint k = 0; k < TRIES && seen < WRITERS; k++) { + seen = atomic_load_explicit(flag, memory_order_relaxed); + } + atomic_thread_fence(mem_flags::mem_texture, memory_order_acquire, thread_scope_device); + out[i] = uint2(seen, tex.read(at).x); +}