atomicXor
Performs a thread-safe bitwise XOR operation on a shared integer in memory (usually a Storage Buffer), primarily used to safely and reversibly flip one or more status bits in parallel computations.
Core Advantages
Provides a hardware-guaranteed, thread-safe method for toggling state bits. Its reversibility (A ^ B ^ B = A) allows using the same operation for both 'on' and 'off' effects, greatly simplifying certain algorithms.
Common Uses
Implementing 'marching ants' selection boxes by repeatedly XORing to draw and erase.
Performing parity checks, such as determining if a ray crosses an object an odd or even number of times.
As an erasable temporary marker, using the same mask to set and remove flags in multi-pass algorithms.
How to adjust
Its effect is controlled by the `valueNode` (bitmask) parameter, which determines which bits to 'flip'. In the light panel example, the `valueNode` determines which light is affected by a click. Changing the mask, e.g., from `1u << 5` to `1u << 10`, makes the click toggle the 10th light instead of the 5th. Using a mask of `0b11` would toggle two lights simultaneously.
Code Examples
1// Create a mask to flip only the bit for the specific light
2const switch_mask = 1u << bit_index;
3// Atomically XOR the light panel's state to 'toggle' it
4atomicXor(light_panel_buffer.element(index), switch_mask);