Skip to content

perf: upload a 2D canvas as a texture on the GPU - #239

Merged
triniwiz merged 6 commits into
masterfrom
perf/gpu-canvas-source
Nov 17, 2026
Merged

triniwiz merged 6 commits into
masterfrom
perf/gpu-canvas-source

Conversation

@triniwiz

Copy link
Copy Markdown
Member

Using a 2D canvas as a texture source (copyExternalImageToTexture, texImage2D/texSubImage2D) read the whole canvas back to the CPU and uploaded it again. PixiJS does this for every Text change.

Builds on #234 and #238 (their commits show here until they merge).

Change

  • Metal → WebGPU: the canvas is blitted into a new texture on its own queue, which then signals an MTLEvent. wgpu's queue waits on the event and blits that texture into the destination. Neither thread waits for the GPU, and later drawing cannot reach the copy.
  • GL → WebGL (Linux): for RGBA/UNSIGNED_BYTE premultiplied uploads (how PixiJS uploads text), the canvas draws itself into a texture of its own context. The texture is shared through an EGLImage with an EGL fence, and the WebGL context copies it into the bound texture with CopyTexSubImage2D. The contexts stay separate; WebGL 1 takes only unsized RGBA there.
  • premultipliedAlpha on a WebGPU copy destination is honoured; it was ignored.
  • UNPACK_PREMULTIPLY_ALPHA_WEBGL is its own unpack state and is applied to canvas uploads. Before, it overwrote the context's premultipliedAlpha attribute and never reached the texels.

Other combinations keep the readback.

Verified

iOS 26.4 simulator, one copyExternalImageToTexture from a 2D canvas:

size readback GPU
128x32 0.93 ms 0.32 ms
1024x256 2.40 ms 0.32 ms

Linux (Mesa, Iris Xe), one premultiplied texSubImage2D from a 2D canvas: 0.48 → 0.27 ms at 128x32, 2.73 → 0.50 ms at 1024x256. PixiJS WebGL scene with 40 changing Text: 27.5 → 20.3 ms/frame.

Specs, new in canvassource: WebGPU premultipliedAlpha, flipY, and copying the canvas as it was when called; WebGL UNPACK_PREMULTIPLY_ALPHA_WEBGL and premultiplied uploads with and without UNPACK_FLIP_Y_WEBGL.

  • iOS simulator: canvassource 15/15, webgpu 26/26, webgl 92/92, webgl2 50/50, 2d 181/181, offscreen 64/64.
  • Linux: canvassource 15/15, webgl 92/92, 2d 181/181.

A Metal 2D canvas that is not in a window still presented every frame and took a
new CAMetalLayer drawable on its next draw. An off-screen layer hands drawables
back about once per vsync, so every readback of such a canvas (getImageData,
texImage2D/texSubImage2D, copyExternalImageToTexture, drawImage) waited for the
display. Libraries that draw text into hidden canvases pay this per update:
PixiJS Text uploads took 8.3 ms each on a 120 Hz simulator.

The canvas now tracks whether its view is in a window. Off screen it keeps
drawing into the drawable it holds; entering a window presents what it drew.

Adds a lifecycle spec for it and a text upload benchmark (canvas-pixi page,
--suite=txup:* and pxtext:*).
wgpu's Metal adapter probes indirect command buffer support when it opens a
device. The Simulator's MTLDevice has no indirect command buffers and aborts
the process instead of failing the probe, so any requestDevice crashed the
app there. The wgpu pin moves to a fork commit that skips the probe in the
Simulator (target_abi = "sim"); multi-draws take the per-draw path there.
copyExternalImageToTexture with a 2D canvas read the whole canvas back to the
CPU and uploaded it again. On Metal the canvas is now copied on its own queue
into a new texture, which then signals an MTLEvent; wgpu's queue waits on the
event and blits that texture into the destination. Neither thread waits for
the GPU, and later drawing on the canvas cannot reach the copy.

premultipliedAlpha on the destination is now honoured (the readback path
ignored it), and both paths produce the same texels.
…EMULTIPLY_ALPHA_WEBGL

texImage2D/texSubImage2D with a 2D canvas read the canvas back to the CPU.
On Linux, when the upload is RGBA/UNSIGNED_BYTE and premultiplied (as PixiJS
uploads text), the canvas now draws itself into a texture of its own context,
shared through an EGLImage and an EGL fence, and the WebGL context copies it
into the bound texture with CopyTexSubImage2D. No thread waits for the GPU and
the contexts stay separate; WebGL 1 accepts only unsized RGBA there.

pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL) used to overwrite the context's
premultipliedAlpha attribute and was never applied to uploads. It is now its
own unpack state, and the readback path premultiplies canvas uploads when it
is set.
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 24a74fda-19e5-42b0-92a6-ccbf03e59150

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@triniwiz
triniwiz merged commit d7bd110 into master Nov 17, 2026
24 of 27 checks passed
@triniwiz
triniwiz deleted the perf/gpu-canvas-source branch November 17, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant