playcanvas / playcanvas/engine

Pool the WebGPU readback staging buffers

Open
#9,297 1 comment 1 reaction 1 assignee View on GitHub

@mvaligursky is already working on this.

Since Sep 4, 2026.

Dominant language
JavaScript
Stars
16.8k
Forks
2k
Avg merge
4h 32m
Merged PRs (30d)
222

Description

WebgpuGraphicsDevice.readStorageBuffer creates a staging buffer for every read and readBuffer destroys it once the read settles, so a readback issued every frame — which SceneDepthReader does, and Picker can — creates and destroys a GPU buffer every frame. There is no pooling anywhere on this path.

https://github.com/playcanvas/engine/blob/main/src/platform/graphics/webgpu/webgpu-graphics-device.js#L1497

Pooling these is safe and needs no API change, because the buffer never escapes: readBuffer does getMappedRangedata.set(...)unmap(), copying the bytes into the caller's array, so the buffer's lifetime is entirely internal. That is unlike the arrays handed back to callers, which deliberately are not pooled — the device cannot know when a caller has finished reading one.

Notes for whoever picks this up:

  • A mapped buffer cannot be reused until it is unmapped, so the pool has to hand back only unmapped buffers, and key them by size.
  • webgpu-upload-stream.js already manages staging buffers for uploads and is worth mirroring, or extending.
  • core/object-pool.js is not suitable: it is a bulk-reset pool (allocate() plus freeAll(), with no individual release) and constructs with no arguments, so it cannot size a buffer.

Came out of #9293, which fixed a WebGL-only readback stall by moving the blocking copy to the start of the next frame. WebGPU was never affected by that, as mapAsync does not block — this is a separate, backend-specific waste.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.