atomicMin
Performs a thread-safe atomic minimum comparison and write operation on a value in shared memory (usually a Storage Buffer), ensuring the final stored value is the minimum among all candidates from many competing parallel threads.
Core Advantages
Guarantees the atomicity of the 'read-compare-write' operation at the hardware level, enabling efficient and correct parallel min reduction, which is fundamental for advanced algorithms like custom depth buffers and GPU pathfinding.
Common Uses
Implementing a manual depth buffer (Z-Buffer) to find the nearest fragment per pixel.
In GPU-accelerated pathfinding algorithms, to record the minimum cost to reach each grid node.
In physics or event simulations, to find the earliest time of occurrence among multiple concurrent events.
How to adjust
Its effect is defined by the `valueNode` (candidate value) parameter. For example, when `valueNode` is a depth value, it creates a standard depth map. If changed to material roughness, it will instead find and record the smoothest visible surface at each pixel, producing a 'smoothness map'. It's a general-purpose 'find the minimum' tool, where `valueNode` defines the 'competition'.
Code Examples
1// Convert the [0,1] depth value to an integer
2const fragment_depth_as_int = int( gl_FragCoord.z * 65535.0 );
3// Compare with the current min in the buffer and atomically write the new min
4atomicMin( myDepthBuffer.element( screenIndex ), fragment_depth_as_int );