Intrinsics1 min read

atomic_min

Atomically stores the minimum of two values.

Reading Time
1 min
Word Count
143
Sections
11
Try It Live

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 Playground

Live Demo Pending

This intrinsic does not yet have a self-contained interactive preview. Compute-only and external-resource intrinsics need a different demo path than the current fragment and 3D showcases.

The atomic_min function atomically computes the minimum of the current value and the input, storing the result. Returns the original value.

Signature

bwsl
atomic_min(dest, value)

Parameters

ParameterDescription
destInteger lvalue to modify atomically
valueInteger value to compare

Return Value

Returns the original value at dest before the operation.

Example

bwsl
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

bwsl
// Track minimum depth per tile
int depthBits = floatBitsToInt(depth);
atomic_min(tileMinDepth[tileId], depthBits);

Bounding Box

bwsl
// Compute bounding box minimum
atomic_min(boundsMinX, int(position.x * scale));
atomic_min(boundsMinY, int(position.y * scale));

Closest Hit

bwsl
// Track closest intersection
int distInt = int(hitDistance * precision);
atomic_min(closestHit, distInt);

Error Tracking

bwsl
// 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:

glsl
atomicMin(dest, value)

When compiled to HLSL:

hlsl
InterlockedMin(dest, value, originalValue)

When compiled to SPIR-V:

Uses OpAtomicSMin (signed) or OpAtomicUMin (unsigned) instruction.

See Also