Repository navigation
perf: upload a 2D canvas as a texture on the GPU - #239
Merged
Merged
Conversation
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.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 everyTextchange.Builds on #234 and #238 (their commits show here until they merge).
Change
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.CopyTexSubImage2D. The contexts stay separate; WebGL 1 takes only unsized RGBA there.premultipliedAlphaon a WebGPU copy destination is honoured; it was ignored.UNPACK_PREMULTIPLY_ALPHA_WEBGLis its own unpack state and is applied to canvas uploads. Before, it overwrote the context'spremultipliedAlphaattribute and never reached the texels.Other combinations keep the readback.
Verified
iOS 26.4 simulator, one
copyExternalImageToTexturefrom a 2D canvas:Linux (Mesa, Iris Xe), one premultiplied
texSubImage2Dfrom a 2D canvas: 0.48 → 0.27 ms at 128x32, 2.73 → 0.50 ms at 1024x256. PixiJS WebGL scene with 40 changingText: 27.5 → 20.3 ms/frame.Specs, new in
canvassource: WebGPUpremultipliedAlpha,flipY, and copying the canvas as it was when called; WebGLUNPACK_PREMULTIPLY_ALPHA_WEBGLand premultiplied uploads with and withoutUNPACK_FLIP_Y_WEBGL.canvassource15/15,webgpu26/26,webgl92/92,webgl250/50,2d181/181,offscreen64/64.canvassource15/15,webgl92/92,2d181/181.