xref: /aosp_15_r20/external/skia/tests/sksl/compute/AtomicOperations.metal (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker#include <metal_stdlib>
2*c8dee2aaSAndroid Build Coastguard Worker#include <simd/simd.h>
3*c8dee2aaSAndroid Build Coastguard Worker#ifdef __clang__
4*c8dee2aaSAndroid Build Coastguard Worker#pragma clang diagnostic ignored "-Wall"
5*c8dee2aaSAndroid Build Coastguard Worker#endif
6*c8dee2aaSAndroid Build Coastguard Workerusing namespace metal;
7*c8dee2aaSAndroid Build Coastguard Workerstruct Inputs {
8*c8dee2aaSAndroid Build Coastguard Worker    uint3 sk_LocalInvocationID;
9*c8dee2aaSAndroid Build Coastguard Worker};
10*c8dee2aaSAndroid Build Coastguard Workerstruct ssbo {
11*c8dee2aaSAndroid Build Coastguard Worker    atomic_uint globalCounter;
12*c8dee2aaSAndroid Build Coastguard Worker};
13*c8dee2aaSAndroid Build Coastguard Workerstruct Globals {
14*c8dee2aaSAndroid Build Coastguard Worker    device ssbo* _anonInterface0;
15*c8dee2aaSAndroid Build Coastguard Worker};
16*c8dee2aaSAndroid Build Coastguard Workerstruct Threadgroups {
17*c8dee2aaSAndroid Build Coastguard Worker    atomic_uint localCounter;
18*c8dee2aaSAndroid Build Coastguard Worker};
19*c8dee2aaSAndroid Build Coastguard Workerkernel void computeMain(uint3 sk_LocalInvocationID [[thread_position_in_threadgroup]], device ssbo& _anonInterface0 [[buffer(0)]]) {
20*c8dee2aaSAndroid Build Coastguard Worker    Globals _globals{&_anonInterface0};
21*c8dee2aaSAndroid Build Coastguard Worker    (void)_globals;
22*c8dee2aaSAndroid Build Coastguard Worker    threadgroup Threadgroups _threadgroups{{}};
23*c8dee2aaSAndroid Build Coastguard Worker    (void)_threadgroups;
24*c8dee2aaSAndroid Build Coastguard Worker    Inputs _in = { sk_LocalInvocationID };
25*c8dee2aaSAndroid Build Coastguard Worker    if (_in.sk_LocalInvocationID.x == 0u) {
26*c8dee2aaSAndroid Build Coastguard Worker        atomic_store_explicit(&_threadgroups.localCounter, 0u, memory_order_relaxed);
27*c8dee2aaSAndroid Build Coastguard Worker    }
28*c8dee2aaSAndroid Build Coastguard Worker    threadgroup_barrier(mem_flags::mem_threadgroup);
29*c8dee2aaSAndroid Build Coastguard Worker    atomic_fetch_add_explicit(&_threadgroups.localCounter, 1u, memory_order_relaxed);
30*c8dee2aaSAndroid Build Coastguard Worker    threadgroup_barrier(mem_flags::mem_threadgroup);
31*c8dee2aaSAndroid Build Coastguard Worker    if (_in.sk_LocalInvocationID.x == 0u) {
32*c8dee2aaSAndroid Build Coastguard Worker        atomic_fetch_add_explicit(&_globals._anonInterface0->globalCounter, atomic_load_explicit(&_threadgroups.localCounter, memory_order_relaxed), memory_order_relaxed);
33*c8dee2aaSAndroid Build Coastguard Worker    }
34*c8dee2aaSAndroid Build Coastguard Worker    return;
35*c8dee2aaSAndroid Build Coastguard Worker}
36