Skip to content

[Web] Batch GPU-to-GPU copies, fix WebGPU synchronization, and add tests for command batching - #20059

Merged
akaashrp merged 3 commits into
apache:mainfrom
akaashrp:prereq/webgpu-copy-batching
Jul 31, 2026
Merged

[Web] Batch GPU-to-GPU copies, fix WebGPU synchronization, and add tests for command batching#20059
akaashrp merged 3 commits into
apache:mainfrom
akaashrp:prereq/webgpu-copy-batching

Conversation

@akaashrp

@akaashrp akaashrp commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Batch GPU-to-GPU buffer copies with pending compute dispatches in the same GPUCommandEncoder instead of finishing and submitting after every copy. Since copyBufferToBuffer records an encoder command, dependencies remain ordered while copies can be submitted at the next required synchronization boundary (see https://www.w3.org/TR/webgpu/#buffer-copies).

Preserve pending GPU-to-CPU readbacks separately from their queue-tail status so sync() waits for both host readback completion and any later queue work when necessary. Also flush pending commands before canvas drawing to prevent rendering stale buffer contents.

Add WebGPU device mocks and tests covering:

  • Mixed compute and GPU-copy batching
  • Host-write, readback, deallocation, and canvas-draw flush boundaries
  • deviceCopyFromGPU followed by a batched GPU copy and sync()
  • Readback error propagation

Regarding importing from ../../src/webgpu instead of ../../dist: WebGPUContext is not re-exported from index.ts, so it is unavailable through the built package like for the other tests. Exporting it solely for this test would unnecessarily expand the public API. Importing ../../src/webgpu is a relatively simple alternative.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@akaashrp
akaashrp requested a review from guan404ming July 28, 2026 03:05

@guan404ming guan404ming left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look nice, left some comments.

web/src/webgpu.ts:531 (outside the diff): could we flush in drawImageFromBuffer too? It submits its own encoder and may render stale data.

Comment thread web/src/webgpu.ts Outdated
Comment thread web/src/webgpu.ts
Comment thread web/tests/node/test_webgpu.js
Comment thread web/tests/node/test_webgpu.js
Comment thread web/jest.config.js
Keep pending readbacks separate from queue-tail tracking so sync waits for both host readback completion and later queue work when needed. Flush pending commands before canvas draws and extend the WebGPU command-batching tests.
@akaashrp akaashrp changed the title [Web] Batch GPU-to-GPU copy commands and add tests for command batching [Web] Batch GPU-to-GPU copies, fix WebGPU synchronization, and add tests for command batching Jul 30, 2026
@akaashrp

Copy link
Copy Markdown
Contributor Author

Thanks @guan404ming, should be addressed now

@guan404ming guan404ming left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice, thanks!

@akaashrp
akaashrp merged commit abe2526 into apache:main Jul 31, 2026
8 checks passed
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.

2 participants