atomic_max
Atomically stores the maximum of two values.
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 PlaygroundLive Demo Pending
The atomic_max function atomically computes the maximum of the current value and the input, storing the result. Returns the original value.
Signature
atomic_max(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 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
// Find maximum value
int valueInt = int(value * scale);
atomic_max(peakValue, valueInt);Bounding Box Max
// 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
// Maximum depth for hierarchical-Z
int depthBits = floatBitsToInt(depth);
atomic_max(tileMaxDepth[tileId], depthBits);Exposure Calculation
// Track maximum brightness
int brightnessInt = int(brightness * precision);
int previous = atomic_max(maxBrightness, brightnessInt);Error Tracking
// 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:
atomicMax(dest, value)
When compiled to HLSL:
InterlockedMax(dest, value, originalValue)
When compiled to SPIR-V:
Uses OpAtomicSMax (signed) or OpAtomicUMax (unsigned) instruction.
See Also
- atomic_min - Atomic minimum
- atomic_add - Atomic add
- wave_max - Wave-level maximum