atomicStore
Performs a thread-safe, uninterruptible overwrite operation on a shared memory location (usually a Storage Buffer), ensuring the written value is complete and not corrupted or 'torn' by writes from other parallel threads.
Core Advantages
Guarantees write integrity (atomicity) at the hardware level, eliminating the risk of 'torn writes'. It also provides memory synchronization, ensuring a thread's write result is correctly observed by others, making it fundamental for publishing final computation results.
Common Uses
In producer-consumer patterns, for the producer thread to publish a final result.
Safely writing the final color of each pixel when procedurally generating textures in a compute shader.
Resetting or initializing large buffers in parallel (e.g., clearing all elements to zero).
How to adjust
`atomicStore` is a faithful executor; its effect is entirely determined by the provided `valueNode` (the value to write). For instance, when generating a procedural texture, if the color calculation logic is changed from a checkerboard to a circle, it will write circle data, and the final visualized image will change accordingly. Its own behavior doesn't change, but the content it writes dictates the final result.
Code Examples
1// Calculate checkerboard color and pack it
2uint c = (thread_id.x / 8u + thread_id.y / 8u) % 2u;
3uint finalColor = (c == 0u) ? 0xFF000000u : 0xFFFFFFFFu;
4// Atomically store the final color into the buffer
5atomicStore(myTextureBuffer.element(index), finalColor);