playcanvas / playcanvas/engine
Pool the WebGPU readback staging buffers
@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.
Pooling these is safe and needs no API change, because the buffer never escapes: readBuffer does getMappedRange → data.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.jsalready manages staging buffers for uploads and is worth mirroring, or extending.core/object-pool.jsis not suitable: it is a bulk-reset pool (allocate()plusfreeAll(), 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.