Intrinsics1 min read

atomic_max

Atomically stores the maximum of two values.

Reading Time
1 min
Word Count
149
Sections
12
Try It Live

Test atomic_max in a live shader

Open the playground, start from a visual preset, and wire atomic_max 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_max function atomically computes the maximum of the current value and the input, storing the result. Returns the original value.

Signature

bwsl
atomic_max(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 MaxLuminance {
pass "Main" {
compute "Main" [64, 1, 1] {
// Sample and compute luminance
float4 color = sample(hdrTex, threadUV);
float luminance = dot(color.rgb, float3(0.2126, 0.7152, 0.0722));
// Convert to integer for atomic ops
int lumInt = int(luminance * 65536.0);
// Atomically update maximum
atomic_max(maxLumBuffer[0], lumInt);
}
}
}

Common Use Cases

Peak Detection

bwsl
// Find maximum value
int valueInt = int(value * scale);
atomic_max(peakValue, valueInt);

Bounding Box Max

bwsl
// Compute bounding box maximum
atomic_max(boundsMaxX, int(position.x * scale));
atomic_max(boundsMaxY, int(position.y * scale));
atomic_max(boundsMaxZ, int(position.z * scale));

Hi-Z Buffer

bwsl
// Maximum depth for hierarchical-Z
int depthBits = floatBitsToInt(depth);
atomic_max(tileMaxDepth[tileId], depthBits);

Exposure Calculation

bwsl
// Track maximum brightness
int brightnessInt = int(brightness * precision);
int previous = atomic_max(maxBrightness, brightnessInt);

Error Tracking

bwsl
// Track maximum error
int errorInt = int(abs(error) * scale);
atomic_max(maxError, errorInt);

Initialize Properly

Initialize max tracking variables to the minimum possible value (e.g., INT_MIN or 0) before computing.

Reduction Pattern

For finding global max, combine wave-level wave_max first, then use atomic_max between waves for better performance.

Compiled Output

When compiled to GLSL:

glsl
atomicMax(dest, value)

When compiled to HLSL:

hlsl
InterlockedMax(dest, value, originalValue)

When compiled to SPIR-V:

Uses OpAtomicSMax (signed) or OpAtomicUMax (unsigned) instruction.

See Also