Skip to content

compute.dispatchFolded

Summary

compute.dispatchFolded dispatches a shader using physical workgroup counts that fit the device's per-axis limit. The shader reconstructs its logical indices from the physical workgroup grid.

Syntax

dispatchFolded(pipeline: GPUComputePipeline | ComputePipeline, bindGroups: ReadonlyArray<GPUBindGroup | null | undefined>, size: WorkgroupExtent, workgroupSize: WorkgroupExtent, label?: string, opts?: ComputeDispatchOptions): GPUCommandBuffer
const command = wgpu.compute.dispatchFolded(pipeline, [bindings], count, 256);

Parameters

Name Type Required Description
pipeline GPUComputePipeline | ComputePipeline Yes Pipeline with a shader that decodes the physical grid.
bindGroups ReadonlyArray<GPUBindGroup | null | undefined> Yes Groups in layout order. Nullish entries are skipped.
size, workgroupSize WorkgroupExtent Yes Logical invocation counts and corresponding WGSL group sizes, with the same rank from one to three.
label string No Compute pass label.
opts ComputeDispatchOptions No submit defaults to true. validateLimits defaults to false. Planning always applies the device's per-axis limit.

Returns

GPUCommandBuffer - Encoded command buffer. Submit it yourself if you set submit: false. Zero extents encode no dispatch.

Example

const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const count = 5000;
const output = wgpu.compute.createStorageBuffer({ byteLength: count * 4, copySrc: true });
const pipeline = wgpu.compute.createPipeline({ code: `
    @group(0) @binding(0) var<storage, read_write> out: array<u32>;
    @compute @workgroup_size(256)
    fn main(
        @builtin(workgroup_id) wid: vec3<u32>,
        @builtin(num_workgroups) nwg: vec3<u32>,
        @builtin(local_invocation_index) lid: u32
    ) {
        let i = (wid.x + nwg.x * (wid.y + nwg.y * wid.z)) * 256u + lid;
        if (i < arrayLength(&out)) { out[i] = i; }
    }
` });
wgpu.compute.dispatchFolded(pipeline, [pipeline.createBindGroup(0, { 0: output })], count, 256);
console.log(await output.readAs(Uint32Array, 0, 16)); // [0, 1, 2, 3]
output.destroy();

See Also