subgroupAdd
On this page

subgroupAdd

Function

A subgroup fold adds the value of every lane of the subgroup and gives the sum to every lane, in one instruction with no barrier.

Compare examples

Compare the four-lane sum with the one-lane case: WebGL2 and the CPU oracle run a subgroup of one invocation (Rule 11.21), so the fold is the lane's own value.

Model assumptionssubgroup of 4 lanes; x = [3, 1, 4, 1]; p = [true, false, true, true]

Example 1subgroupAdd(x)
Result
9 in every lane
Example 2subgroupAdd(x)
Result
one lane (WebGL2, CPU oracle): x

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it to pre-reduce before an atomic or a workgroup tree: one atomic per subgroup instead of one per lane.

Inputs and limits

Every lane of the subgroup must reach the call together (uniform control flow). An integer sum has one value in any order; a float sum does not, and the determinism report lists it as order.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns the sum of value over the lanes of the subgroup, in every lane.

Syntax

declare function subgroupAdd<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(value: V): V
declare function subgroupAdd<P0 extends number>(value: P0): ScalarOf<P0>

A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

type f32 = number & { readonly [f32Tag]?: true }

A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

ScalarOf
type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>

Parameters

  • value VP0

Return value

VScalarOf<P0>

Description

A float sum's order is the device's. On the CPU oracle and on WebGL2 a subgroup is one invocation (Rule 11.21), so it gives the lane's own value.

Targets

WGSL
subgroupAdd(a)
GLSL ES 3.00
subgroupAdd is lowered to one lane before the GLSL writer runs (Rule 11.21)

Examples

See also

Edit this page Report a problem