Skip to content

fix(webgpu): wait on each 2D canvas copy's own Metal event - #292

Merged
triniwiz merged 1 commit into
masterfrom
fix/webgpu-canvas-copy
Nov 27, 2026
Merged

triniwiz merged 1 commit into
masterfrom
fix/webgpu-canvas-copy

Conversation

@triniwiz

Copy link
Copy Markdown
Member

Problem

GPUQueue.copyExternalImageToTexture with a Metal-backed 2D canvas sometimes produced a texture that was never written (magenta on the iOS simulator, blank elsewhere). PixiJS's WebGPU text rendering hits this because it uploads every Text's canvas this way.

Every copy signalled one shared MTLEvent with a global counter, and each canvas signals it from its own command queue. Those queues can finish in any order. A later canvas could signal value N+1 first, which satisfied wgpu's wait for copy N, so the blit sampled copy N's intermediate texture before it was written.

Fix

  • Each copy gets its own MTLEvent, and wgpu's queue waits on exactly that event.
  • The event is kept alive until wgpu's submission completes, because Metal may not retain an event that a command buffer waits on.

Android (a SYNC_FD semaphore per copy) and Linux (an fd per frame) already sync each copy separately, so they don't have this problem.

Specs (canvassource.webgpu)

  • waits for each 2d canvas, not a later one: a slow 1024² canvas (blurred shapes) is copied, then a quick 16² canvas, for 6 rounds. Each round reads back the slow copy. On the iOS sim this fails every time without the fix (got [255, 0, 255, 255]) and passes with it.
  • matches getImageData for many redrawn 2d canvases: 12 text canvases are resized, redrawn and uploaded back to back over 5 frames, mixing rgba8unorm and bgra8unorm. Every pixel is compared with getImageData.

copyExternalImageToTexture from a Metal 2D canvas signalled one shared
MTLEvent with a global counter, from each canvas's own command queue.
Queues finish in any order, so a later canvas's signal could satisfy
the wait for an earlier copy and wgpu sampled the intermediate texture
before it was written (magenta / blank PixiJS text on iOS).

Each copy now gets its own event, kept alive until wgpu's submission
completes. Android (per-copy SYNC_FD) and Linux (per-frame fd) already
sync per copy.

Specs: a slow canvas copied before a quick one, and many resized text
canvases compared against getImageData.
@coderabbitai

coderabbitai Bot commented Nov 27, 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: b320f207-d843-43f8-bb69-80141fda6088

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 eec4e08 into master Nov 27, 2026
25 of 31 checks passed
@triniwiz
triniwiz deleted the fix/webgpu-canvas-copy branch November 27, 2026 14:25
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