v148 · WebGPU · WGSL

WebGPU: linear_indexing feature

The WGSL language extension linear_indexing adds two new compute-shader built-in values — global_invocation_index and workgroup_index — that convert 3-D dispatch coordinates to a flat index automatically, eliminating a common class of repetitive, error-prone boilerplate.

concepts

  1. Shader Code Comparison

    Side-by-side WGSL compute shader examples showing manual 3D→1D index arithmetic versus the new global_invocation_index built-in — same logic, less code.

  2. Index Visualizer

    Interactive grid showing how each invocation maps to a global_invocation_index value across a configurable dispatch, with live feature-detection to confirm linear_indexing support.

  3. Compute Benchmark

    Run the same sum-of-squares compute shader two ways — manual 3D index math vs global_invocation_index — and compare GPU execution time, shader line count, and output correctness side by side.

  4. Linear Memory Visualizer

    An interactive 16×16 canvas grid colored by linear index gradient. Five reshape buttons reflow the same 256 elements and update the WGSL shader code panel showing the recalculated linearIndex() call. Feature-detects linear-indexing in adapter.features, with a JS performance comparison of standard 2D vs. pre-multiplied base pointer approaches.

  5. Workgroup Index Demo

    Interactive canvas grid visualizing how @builtin(workgroup_index) assigns a unique flat index to each workgroup in a configurable 3D dispatch. Set workgroup counts for X, Y, Z, pick any Z slice, click a workgroup cell to see its (x,y,z) coordinates, its computed workgroup_index, and the WGSL showing both the old manual formula and the new built-in.

why it shipped

Compute shaders address data in three dimensions (@builtin(global_invocation_id) is a vec3u), but most algorithms need a single flat array index. The conversion formula — id.x + id.y * width + id.z * width * height — is conceptually trivial but written thousands of times across the WebGPU ecosystem. Getting it slightly wrong (wrong dimension order, off-by-one workgroup size) causes silent data corruption that is hard to debug on the GPU. The linear_indexing WGSL extension moves this calculation into the language itself: @builtin(global_invocation_index) returns the correct flat index directly, and @builtin(workgroup_index) gives the analogous workgroup-level value.

references

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗