atomicAdd
Performs a thread-safe atomic addition on a value in shared memory (usually a Storage Buffer) on the GPU, ensuring data is not lost or corrupted due to race conditions in highly parallel computations.
Core Advantages
Guarantees the atomicity of the 'read-modify-write' operation at the hardware level, completely eliminating data race conditions. This enables high-performance, correct parallel data aggregation algorithms on the GPU.
Common Uses
As an atomic counter for fragment linked lists in Order-Independent Transparency (OIT).
Calculating spatial density of particles or data (building histograms).
Marking voxels occupied by geometry during mesh voxelization.
How to adjust
The effect of `atomicAdd` is indirect, as it modifies data in memory. To observe its effect, a subsequent rendering pass is typically needed to read and visualize this data. Adjusting its `valueNode` parameter (the value to be added) directly impacts the magnitude of the aggregated result. For instance, in particle density statistics, increasing the added value from 1 to 10 will make the final generated heatmap 10 times brighter (hotter) overall.
Code Examples
1// Calculate the grid index for the particle
2const gridIndex = calculateGridIndex( particlePos );
3// Atomically add to the counter for that grid cell
4atomicAdd( densityGridBuffer.element( gridIndex ), 1 );