subgroupExclusiveAdd
Performs an exclusive prefix sum (exclusive scan) within the GPU subgroup: returns the sum of e for all active invocations whose subgroup_invocation_id is less than the current one. Also known as: Exclusive Scan, Exclusive Prefix Sum, WavePrefixSum.
Core Advantages
Parallel prefix-sum without explicit loops or shared memory; can directly serve as an in-subgroup index/offset allocator for compaction, bucketing and sorting.
Common Uses
Local lane index: when e = 1, the result equals the current active invocation’s 0-based index.
Predicate-based compaction/allocation: use e as 0/1 mask to get the compacted offset for qualifying lanes.
Build cumulative distributions / procedural stripes; compute non-overlapping write ranges for work partitioning.
How to adjust
No extra parameters; change e (1, boolean mask, or weights) and the active mask to control behavior. Requires subgroup/wave support on the target backend; may be unavailable on some platforms.
Code Examples
1/* param e:number → returns number */
2const laneIndex = subgroupExclusiveAdd( float(1) );
3const alive = step( 0.5, lifeFactor );
4const offset = subgroupExclusiveAdd( alive );
5const stripe = mod( laneIndex, 2.0 ).equal( 0.0 );
6output.color = cond( stripe, color(0xffffff), color(0x222222) );