Skip to content

fix: Register StorageBuffer for WebGPU device lose/restore#8692

Merged
mvaligursky merged 1 commit intomainfrom
mv-storage-buffer-device-restore
May 6, 2026
Merged

fix: Register StorageBuffer for WebGPU device lose/restore#8692
mvaligursky merged 1 commit intomainfrom
mv-storage-buffer-device-restore

Conversation

@mvaligursky
Copy link
Copy Markdown
Contributor

WebGPU device loss previously either crashed when iterating GraphicsDevice.buffers (if StorageBuffer was tracked without loseContext/unlock) or skipped storage buffers entirely, leaving stale GPUBuffer references after requestDevice(). This change registers storage buffers on the device and runs the same lose/restore path as other buffers.

Changes:

  • Add StorageBuffer to GraphicsDevice.buffers on construction; remove on destroy().
  • Implement StorageBuffer loseContext() / restoreContext() (GPU backing recreated at the same byte size; contents are undefined until rewritten or recomputed). Document that in the class JSDoc.
  • GraphicsDevice.restoreContext() calls buffer.restoreContext() instead of unlock().
  • VertexBuffer and IndexBuffer add @ignore restoreContext() delegating to unlock() so the public lock/unlock API is unchanged.
  • WebgpuBuffer: loseContext() clears the buffer reference only (aligned with WebglBuffer); allocate() applies the GPU debug label so staging and storage paths stay consistent.

Supersedes the approach of excluding StorageBuffer from device.buffers (e.g. PR #8690).

Track storage buffers on GraphicsDevice with loseContext/restoreContext. GraphicsDevice.restoreContext calls buffer.restoreContext(); vertex/index delegate to unlock. WebgpuBuffer loseContext clears handle only; allocate applies debug labels.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes WebGPU device lose/restore handling for StorageBuffer by ensuring storage buffers are tracked by GraphicsDevice.buffers and participate in the same context loss / restoration lifecycle as other tracked buffers, preventing crashes and stale GPUBuffer references after device re-creation.

Changes:

  • Track StorageBuffer instances in GraphicsDevice.buffers, remove them on destroy(), and implement loseContext() / restoreContext() with documented “contents undefined” semantics.
  • Update GraphicsDevice.restoreContext() to call buffer.restoreContext() instead of unlock(); add @ignore restoreContext() shims to VertexBuffer / IndexBuffer.
  • Improve WebGPU buffer handling by nulling WebgpuBuffer.buffer on context loss and consistently applying GPU debug labels during allocation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/platform/graphics/webgpu/webgpu-buffer.js Clears stale GPUBuffer references on device loss and applies labels in allocate() so restore paths are labeled consistently.
src/platform/graphics/vertex-buffer.js Adds internal restoreContext() delegating to unlock() to preserve existing public API while supporting new restore iteration.
src/platform/graphics/storage-buffer.js Registers storage buffers for device tracking and adds lose/restore context methods; documents post-restore undefined contents behavior.
src/platform/graphics/index-buffer.js Adds internal restoreContext() delegating to unlock() to work with GraphicsDevice.restoreContext() changes.
src/platform/graphics/graphics-device.js Switches buffer restoration loop from unlock() to restoreContext() and clarifies behavior for storage buffers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mvaligursky mvaligursky merged commit 2b743a6 into main May 6, 2026
12 checks passed
@mvaligursky mvaligursky deleted the mv-storage-buffer-device-restore branch May 6, 2026 18:11
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