atomic_min
Atomically stores the minimum of two values.
Test atomic_min in a live shader
Open the playground, start from a visual preset, and wire atomic_min into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo Pending
The atomic_min function atomically computes the minimum of the current value and the input, storing the result. Returns the original value.
Signature
atomic_min(dest, value)
Parameters
| Parameter | Description |
|---|---|
dest | Integer lvalue to modify atomically |
value | Integer value to compare |
Return Value
Returns the original value at dest before the operation.
Example
pipeline ClosestDepth {
pass "Main" {
compute "Main" [64, 1, 1] {
// Convert depth to integer for atomic ops
float depth = computeDepth(position);
int depthInt = int(depth * 16777216.0); // 24-bit precision
// Atomically update minimum
int2 tile = getTile(position);
atomic_min(minDepthBuffer[tile.y * tileWidth + tile.x], depthInt);
}
}
}Common Use Cases
Depth Buffer Min
// Track minimum depth per tile
int depthBits = floatBitsToInt(depth);
atomic_min(tileMinDepth[tileId], depthBits);Bounding Box
// Compute bounding box minimum
atomic_min(boundsMinX, int(position.x * scale));
atomic_min(boundsMinY, int(position.y * scale));Closest Hit
// Track closest intersection
int distInt = int(hitDistance * precision);
atomic_min(closestHit, distInt);Error Tracking
// Track minimum error for debugging
int errorBits = floatBitsToInt(error);
atomic_min(minError, errorBits);Signed vs Unsigned
For unsigned values, use the unsigned variant. The signed version performs signed comparison.
Float to Int
To use atomic min/max with floats, convert to int preserving order: positive floats can use floatBitsToInt directly; for general floats, handle sign separately.
Compiled Output
When compiled to GLSL:
atomicMin(dest, value)
When compiled to HLSL:
InterlockedMin(dest, value, originalValue)
When compiled to SPIR-V:
Uses OpAtomicSMin (signed) or OpAtomicUMin (unsigned) instruction.
See Also
- atomic_max - Atomic maximum
- atomic_add - Atomic add