home/ atoms/ webgpu-compute-shader

A compute shader is an @compute WGSL function that runs without fixed vertex/fragment I/O, reading and writing only storage buffers or textures

A compute shader is a GPU program stage with no required vertex, fragment, or texture inputs/outputs. Declared with @compute @workgroup_size(x, y, z), it receives a @builtin(global_invocation_id) to identify its position in the dispatch grid. Results are written only to storage buffers or textures. Compute shaders run in workgroups — 3D groups of invocations that execute together on the hardware. The calling code dispatches N workgroups per axis via computePass.dispatchWorkgroups(nx, ny, nz). They can share binding declarations with render pipelines when both pipelines use the same bind group layout.

Examples

@compute @workgroup_size(8, 8)
fn computeMain(@builtin(global_invocation_id) cell: vec3u) {
  cellStateOut[cellIndex(cell.xy)] = /* new state */;
}

Assessment

A 32×32 grid uses workgroup size (8,8). How many workgroups does dispatchWorkgroups need on each axis? What is the difference between global_invocation_id and local_invocation_id?

“A compute shader is similar to vertex and fragment shaders in that they are desi”
corpus · your-first-webgpu-app-google-codelab · chunk 19