A video as a WebGPU external texture under HTML cards
The two cards below are HTML. The video frame behind them is a separate texture, and one shader pass draws all three: the cards are glass that bends and blurs the frame. Type in the field, press the buttons, change the effect and tap the picture. "Measure on this device" times each way of getting the frame and the cards to the GPU.
Show the code one pass over a video texture and two element textures, on three routes
- The markup. Two
drawablecards and a<video>that is a child of the canvas but not of a drawn element. - The CSS. The cards have no background. The shader draws it.
- The frame on WebGPU.
importExternalTexture()first, thencopyExternalImageToTexture(). - The WebGPU route. Upload each card with
drawElementImageToTexture(), import the frame, draw. - The render pass. The bind group is made again for each frame, because an external texture can't be kept.
- The WGSL shader.
texture_externaland twotexture_2din one fragment shader. - Where the cards go. The page computes each card's position. CSS
leftandtopdon't move a canvas child. - Hit testing.
updateElementGeometry()with a translation to where the shader draws the card. - The WebGL 2 route.
- The WebGL uploads.
texImage2D(video)andtexElementSubImage2D(). - The GLSL shader.
- The 2D route.
drawImage(video),drawElementImage(), filters and compositing. - The measurement on WebGPU.
Measure on this device
Each route draws the same small picture 40 times, one draw per video frame, into a texture that isn't shown. The picture above pauses while this runs. It takes about 20 seconds.
Not measured yet.
| Route | What's uploaded each frame | Time per frame |
|---|
How it works
- The canvas has three children. Two are
drawablecards. The third is the<video>, which has nodrawableancestor, so it's never part of an element's picture. - On WebGPU,
device.importExternalTexture({ source: video })gives the current frame as aGPUExternalTexturewith no copy.queue.drawElementImageToTexture()puts each card in anrgba8unormtexture. One fragment shader samples the frame withtextureSampleBaseClampToEdge()and the cards withtextureSampleLevel(). - If the import fails, the frame is copied with
queue.copyExternalImageToTexture()and the same shader is compiled withtexture_2d<f32>for the video. - On WebGL 2 the frame comes from
texImage2D(video)and the cards fromtexElementSubImage2D(). On a 2D canvas the page usesdrawImage(video),drawElementImage(),ctx.filterand compositing modes. The 2D route has no ripple. - The page tries WebGPU, then WebGL 2, then 2D. The line under the picture names the route in use and gives the reason when it isn't WebGPU.
- A shader draw doesn't place an element for hit testing. CSS
leftandtopdon't move a canvas child either. The page keeps each card's position, gives it to the shader as a rectangle, and gives it toupdateElementGeometry()as a translation. It then toggles an attribute on the card. - The measurement compares this arrangement with the one /video/ and /overlay/ use, where the video is a child of the drawn element and one upload carries the frame and the HTML together.
Live checks
Each check builds its own small canvas in a corner of the window. The WebGPU checks draw into a texture and read it back, so they also run on a software adapter. The picture above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 headless with a software WebGPU adapter (SwiftShader) and software video decoding, 2026-11-21. The rows above say what your browser does.
- Works: the pass itself. One WebGPU render pass samples a video texture and two element textures. The frame number in the composed picture and the colours of the cards were read back from the target texture.
- Fails here:
importExternalTexture(). With the software adapter it throws "Failed to import texture from video" for a<video>and for aVideoFrame.copyExternalImageToTexture(video)also throws in Chromium 157 with this adapter.createImageBitmap(video)followed bycopyExternalImageToTexture(bitmap)works, and the checks fall back to it. So thetexture_externalshader variant compiled here but didn't draw. The first, third and eighth checks answer this on a device with a real GPU. - Works: the video inside the element. An element that contains a playing
<video>uploads withdrawElementImageToTexture()and its texture holds a frame. - Works: the same picture on WebGL 2. The GLSL and WGSL shaders were compared on a paused frame for all three effects. The differences are in the fifth check.
- Needed: a translation in
canvasTransform. A canvas child withleft: 200px; top: 100pxhas its box at the canvas origin, and percentages inleft,rightandbottomgave a box 0 px wide. With an identity matrix the card is hit tested at the origin.new DOMMatrix().translate(200, 100), in CSS pixels, puts the hit box where the shader draws the card. - Needed: multiply by alpha in the shader. In these textures a box with
rgb(200 100 50 / .5)is stored as 199, 100, 50 with alpha 0.5, on WebGPU and on WebGL 2. Adding the texture's colour to the frame without multiplying it turned every translucent control white. - Refused: an element as an external texture.
importExternalTexture({ source: element })throws aTypeError. An element reaches WebGPU only through the copy indrawElementImageToTexture(). - Not verified here: WebGPU on screen,
importExternalTexture()with a hardware decoder, whether the external texture expires, and every timing on a real GPU. The "Measure on this device" button and the live checks report these on the device that opens the page.