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.
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
| Feature | On this device | What it is used for |
|---|---|---|
AdapterrequestAdapter({ 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 masktexture-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 drawvar<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. |
Trailsfloat32-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. |
Shardsprimitive-index | … | The fragment shader tints and fades each of the 80 triangles by its number, read from @builtin(primitive_index). |
Subpixel textdual-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
- One drawable canvas with two elements. The form is drawn in four tiles, the text in the fifth.
- The device.
webgpu({ features: true })makes a device with each optional feature the adapter has, andcompat: trueaccepts a compatibility mode device where there is no core adapter. - Which features this device has.
- Alpha as a mask: a swizzled view, and the shader that reads it.
- Values for each draw: immediates, or one uniform buffer with a dynamic offset.
- Trails: a float target that is blended into.
- Shards:
primitive_indexin the fragment shader. - Subpixel text: two fragment outputs and a blend that uses the second.
- The page's frame: upload the elements when they repaint, draw, and keep the form's hit box on its picture.
- 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.
| Result | Experiment | Detail |
|---|
Findings
- An element texture works with all five features.
drawElementImageToTexture()fills an ordinaryrgba8unormtexture, so a swizzled view of it, a float target fed from it and a dual-source blend over it need nothing special. - A swizzled view needs its own
usage. The element texture must haveRENDER_ATTACHMENTfor the upload. The swizzled view is made withusage: TEXTURE_BINDINGso it isn't also a render target. - Immediates replace a buffer and a bind group.
var<immediate> p: P;in the shader,immediateSizeon the pipeline layout andpass.setImmediates(0, floats)before each draw. The limit ismaxImmediateSize, 64 bytes in Chromium 157. The other way here is one uniform buffer with a 256-byte slot for each draw and a dynamic offset. - An 8-bit trail never fades out. Multiplying an 8-bit value by 0.95 and rounding stops near 10 out of 255, so old trails stay as a mark: after 200 fades the
rgba8unormtrail is still 14 of 255 brighter than the background, and the float ones are at 0. Eight additions of the form read 251 of 255 from a float target and 201 from the 8-bit one, which stops at 1.0.rgba16floatblends on every core device;rgba32floatneedsfloat32-blendableand can't be sampled with a filtering sampler, so the shader reads it withtextureLoad(). primitive_indexis a fragment shader input only. Moving the shards apart happens in the vertex shader, which has to work the triangle out fromvertex_index / 3anyway. For a list of separate triangles a flat varying carries the same number to the fragment shader, so the feature saves one varying here. It's needed when triangles share vertices through an index buffer.- Subpixel text needs the canvas to match the screen's pixels. The canvas is sized from
devicePixelContentBoxSize, and the text element is laid out at three times its shown size so each canvas pixel has 3 × 3 texels. The order red, green, blue is assumed. Most phone screens don't have that layout, so there the per-channel coverage shows as coloured edges and plain alpha is the better choice. - A compatibility mode device runs every shader on this page. On the software adapter these pages are tested with,
requestAdapter({ featureLevel: 'compatibility' })gives a device with lower limits (4096 pixel textures, 4 colour attachments) that still has swizzle, immediates and float blending, and its picture matches the core one. Flat varyings are written@interpolate(flat, either), which compatibility mode requires. - On a phone with a real GPU all ten checks pass. Reported from Chrome 159 on Android on 2026-11-21: the device has all four optional features,
primitive_indexmatches the flat varying exactly, and the dual-source text has 7986 pixels whose red and blue differ by more than 24 while its channel average stays within 2 of 255 of plain alpha. The software adapter these pages are tested with has neither of those two features, so there the two checks report that they didn't run.