xref: /aosp_15_r20/external/skia/resources/sksl/compute/AtomicOperations.compute (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1layout(local_size_x = 64) in;
2
3layout(metal, binding = 0) buffer ssbo {
4    atomicUint globalCounter;
5};
6
7workgroup atomic_uint localCounter;  // atomic_uint is a synonym for atomicUint
8
9void main() {
10    // Initialize the local counter.
11    if (sk_LocalInvocationID.x == 0) {
12        atomicStore(localCounter, 0);
13    }
14
15    // Synchronize the threads in the workgroup so they all see the initial value.
16    workgroupBarrier();
17
18    // All threads increment the counter.
19    atomicAdd(localCounter, 1);
20
21    // Synchronize the threads again to ensure they have all executed the increment
22    // and the following load reads the same value across all threads in the
23    // workgroup.
24    workgroupBarrier();
25
26    // Add the workgroup-only tally to the global counter.
27    if (sk_LocalInvocationID.x == 0) {
28        atomicAdd(globalCounter, atomicLoad(localCounter));
29    }
30}
31