Intrinsics1 min read
atomic_and
Atomically performs a bitwise AND operation.
Reading Time
1 min
Word Count
139
Sections
12
Try It Live
Test atomic_and in a live shader
Open the playground, start from a visual preset, and wire atomic_and into the fragment stage to see how it behaves with real values.
Open PlaygroundLive 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_and function atomically performs a bitwise AND between the current value and the input, storing the result. Returns the original value.
Signature
bwsl
atomic_and(dest, value)
Parameters
| Parameter | Description |
|---|---|
dest | Integer lvalue to modify atomically |
value | Integer value to AND with |
Return Value
Returns the original value at dest before the operation.
Example
bwsl
pipeline FlagClearing {
pass "Main" {
compute "Main" [64, 1, 1] {
// Clear specific bit flag atomically
int mask = ~(1 << bitPosition); // All bits except the one to clear
atomic_and(flags[index], mask);
}
}
}Common Use Cases
Clear Bits
bwsl
// Clear specific flags
int clearMask = ~(FLAG_A | FLAG_B);
atomic_and(stateFlags, clearMask);Mask Operations
bwsl
// Apply visibility mask
atomic_and(visibilityMask[tileId], objectMask);Lock Bits
bwsl
// Clear lock bit
int unlockMask = ~LOCK_BIT;
atomic_and(lockState, unlockMask);Accumulate Intersections
bwsl
// AND together intersection masks
atomic_and(intersectionMask, thisObjectMask);Common Flags
bwsl
// Find flags common to all objects
atomic_and(commonFlags, objectFlags);
// After all threads: commonFlags has only bits set in ALL objectsClear vs Set
Use atomic_and with inverted mask to clear bits. Use atomic_or to set bits.
Initialization
When accumulating with AND, initialize to all 1s (-1 or 0xFFFFFFFF) so the first AND doesn't clear everything.
Compiled Output
When compiled to GLSL:
glsl
atomicAnd(dest, value)
When compiled to HLSL:
hlsl
InterlockedAnd(dest, value, originalValue)
When compiled to SPIR-V:
Uses OpAtomicAnd instruction.
See Also
- atomic_or - Atomic OR
- atomic_xor - Atomic XOR