Newer WebGPU features on element textures

A small form and a block of text are uploaded to WebGPU textures with drawElementImageToTexture() and drawn in five tiles. Each tile uses one WebGPU feature that is newer than the first WebGPU release, when the device has it. When the device does not, the tile draws the same picture another way. The table under the canvas says which is in use here.

Element texture
Subpixel text Each colour channel has its own coverage. Quick brown fox, 0123456789.

The form in the first tile is the real one: type in it or press its button. Move a finger or the pointer over the trails tile to draw.

How it works

FeatureOn this deviceWhat it is used for
Adapter
requestAdapter({ featureLevel })
…A core adapter is asked for first. Where there is none, a compatibility mode adapter (OpenGL ES 3.1 class hardware) is asked for before the page gives up on WebGPU and uses WebGL.
Alpha as a mask
texture-component-swizzle
…The glow shader reads a one-channel mask from .r. A second view of the form's texture, createView({ swizzle: 'aaa1' }), hands it the alpha channel there.
Values for each draw
var<immediate>, setImmediates()
…Every draw has its own rectangle, tint and turn (48 bytes). They're set on the render pass before the draw, with no buffer and no bind group.
Trails
float32-blendable
…The trail is a texture that is faded and added to every frame. In a float texture the sums go above 1.0 and the fade reaches zero.
Shards
primitive-index
…The fragment shader tints and fades each of the 80 triangles by its number, read from @builtin(primitive_index).
Subpixel text
dual-source-blending
…The fragment shader writes the ink colour and a separate coverage for red, green and blue. The blend uses the second output as the factor for each channel.
Show the code one feature at a time, with what is drawn without it
  1. One drawable canvas with two elements. The form is drawn in four tiles, the text in the fifth.
  2. The device. webgpu({ features: true }) makes a device with each optional feature the adapter has, and compat: true accepts a compatibility mode device where there is no core adapter.
  3. Which features this device has.
  4. Alpha as a mask: a swizzled view, and the shader that reads it.
  5. Values for each draw: immediates, or one uniform buffer with a dynamic offset.
  6. Trails: a float target that is blended into.
  7. Shards: primitive_index in the fragment shader.
  8. Subpixel text: two fragment outputs and a blend that uses the second.
  9. The page's frame: upload the elements when they repaint, draw, and keep the form's hit box on its picture.
  10. The whole renderer, with the WebGL 2 version of each shader.

Live checks

Each check draws a copy of the canvas off screen twice, once with the feature and once without, and compares the two pictures.

ResultExperimentDetail

Findings