subgroupElect
subgroupElect
Elects a single leader invocation within the hardware subgroup; returns true only for the active invocation with the lowest subgroup_invocation_id (zero parameters).
Core Advantages
Enables reliable ‘once-per-subgroup’ work with a single call, cutting atomic contention and divergent control flow.
Common Uses
Gate expensive or side-effecting work to once per subgroup (e.g., atomic counters, writing reduction results).
Seed subgroup-level algorithms (reductions, scans, hierarchical synchronization).
Debug/visualize which invocation is elected in each subgroup.
How to adjust
No parameters. Behavior is defined by the GPU’s subgroup implementation. Control it by placement: put it right before logic that must run once per subgroup, and combine with atomics/barriers when needed.
Code Examples
1// Visualize the elected invocation in red
2const leader = subgroupElect();
3const base = color( 0x202020 );
4material.colorNode = cond( leader, vec3( 1, 0, 0 ), base );