v142 · graphics

WebGPU: 'primitive_index' feature

This feature adds a new optional capability to WebGPU that exposes a new WGSL shader builtin, 'primitive_index'. This builtin provides a per-primitive index to fragment shaders on supported hardware, similar to the existing vertex_index and instance_index builtins. The primitive index is useful for advanced graphical techniques, such as virtualized geometry.

concepts

  1. primitive_index

    Render a triangle fan where each triangle gets a colour straight from @builtin(primitive_index) — no per-vertex attribute. Falls back gracefully on adapters without the feature.

  2. Triangle Picking

    Pure GPU picking: render the mesh twice, once to the screen and once to an ID buffer where each fragment writes its primitive_index as RGB. Click a triangle, read one pixel back, know which triangle was hit. No CPU ray-trace.

  3. Primitive Heatmap Debugger

    The canonical debugging workflow: flip a tessellated terrain mesh between solid, primitive-ID, and overdraw-heatmap modes. Built on @builtin(primitive_index) writing into a storage buffer with atomic adds — the hottest triangle gets outlined. Adapter feature probe + fallback rendering.

  4. Mesh Inspector

    WebGPU renders a subdivided cube mesh; a WGSL shader uses @builtin(primitive_index) to colour each triangle. CPU-side ray-triangle intersection picks the hovered primitive — the info panel shows its index, vertex positions, centroid, and face area. Three display modes: hover-highlight, per-primitive shading, and an index heatmap gradient.

why it shipped

This feature adds a new optional capability to WebGPU that exposes a new WGSL shader builtin, 'primitive_index'. This builtin provides a per-primitive index to fragment shaders on supported hardware, similar to the existing vertex_index and instance_index builtins. The primitive index is useful for advanced graphical techniques, such as virtualized geometry.

references