subgroupAdd
Sums the scalar e across all active invocations in the current subgroup and returns the same sum to every invocation. Arity: 1.
Core Advantages
Uses native subgroup primitives for parallel reduction, requiring fewer synchronizations and memory accesses than shared‑memory loops.
Common Uses
Parallel sum and normalization (e.g., subgroupAdd(1.0) to get the active invocation count)
Image/particle statistics: histograms, accumulated sums, energy accumulation
Tile or screen‑space aggregated shading and post‑processing
How to adjust
Choose e to control what is reduced. Use e=1.0 to obtain the active count; use conditional e to accumulate only selected elements. Active lanes depend on control flow. Available only when the backend supports subgroups (WebGPU or GLSL/HLSL subgroup extensions). Match integer/float types and watch precision and overflow.
Code Examples
1// Sum local x within the subgroup and normalize by the active count, then map to color
2const sum = subgroupAdd( positionLocal.x );
3const count = subgroupAdd( 1.0 ); // active invocation count (~subgroupSize if all active)
4const intensity = clamp( abs( sum ) / max( count, 1.0 ), 0.0, 1.0 );
5
6<Canvas>
7 <mesh>
8 <sphereGeometry args={[0.4, 128, 128]} />
9 {/* Use subgroup-reduced intensity as grayscale color */}
10 <meshStandardNodeMaterial colorNode={vec3( intensity )} roughness={0.6} metalness={0.0} />
11 </mesh>
12</Canvas>