@workgroup_size
Attribute
Optionally states the workgroup size of each loop that does not reduce in a kernel function, an exported function that takes an array with no size or a 2D storage texture, as one extent: @workgroup_size([128]) export function scale(xs: array<f32>) { … }.
@workgroup_size([n]) states the workgroup size the loops of the kernel function it marks run in on WebGPU.
Compare examples
Compare a loop under the default, under 128 and a loop that reduces. The reducing loop runs at 256 whatever is written.
Example program@workgroup_size([128])
export function scale(xs: array<f32>): void {
for (let i = 0; i < xs.length; i++) {
xs[i] = xs[i] * 2.;
}
}
Example 1no @workgroup_size
- Result
64
Example 2@workgroup_size([128])
- Result
128
Example 3a loop that reduces
- Result
256
The examples are calculation models and do not run GPU work. The reference below gives the syntax of the attribute. 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 when the loops of a kernel function should run in workgroups of another size than the default of 64.
Inputs and limits
It takes one extent, and only a kernel function takes it. A kernel function with a loop that reduces refuses it (TS8077).
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related attributes
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Syntax
declare function workgroup_size(size: readonly [x: number]): (target: Function, context?: unknown) => voidParameters
-
sizereadonly [x: number]
The declaration also names what the TypeScript decorator runtime hands a decorator, target and context. A call passes neither.
Description
Without it the compiler chooses the size, and a loop that reduces runs at 256 whatever is written. An entry takes its size as @compute([128]).