home/ atoms/ webgpu-workgroup-size

dispatchWorkgroups takes the number of workgroups, not invocations — total invocations equals workgroup count times workgroup size

GPU hardware executes compute work in workgroups — batches of shader invocations that run together on one compute unit. @workgroup_size(x, y, z) in WGSL declares how many invocations form one workgroup. computePass.dispatchWorkgroups(nx, ny, nz) specifies the number of workgroups, NOT total invocations. Total invocations = nx×x × ny×y × nz×z. For a 32×32 grid with workgroup size (8,8): dispatchWorkgroups(4, 4) → 4×8 × 4×8 = 1024 invocations. Common default is 8×8=64 per workgroup. Too small wastes dispatch overhead; too large may exceed hardware limits.

Examples

const WORKGROUP_SIZE = 8;
// WGSL: @workgroup_size(${WORKGROUP_SIZE}, ${WORKGROUP_SIZE})
const workgroupCount = Math.ceil(GRID_SIZE / WORKGROUP_SIZE);
computePass.dispatchWorkgroups(workgroupCount, workgroupCount);
// GRID_SIZE=32 → dispatchWorkgroups(4,4) → 16 workgroups × 64 invocations = 1024 total

Assessment

For a 64×64 grid with workgroup_size(8,8): what do you pass to dispatchWorkgroups? What happens if you dispatch too few workgroups?

“number you pass into `dispatchWorkgroups()` is **not** the number of i”
corpus · your-first-webgpu-app-google-codelab · chunk 22