Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/platform/graphics/webgpu/webgpu-graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const _indirectEntryByteSize = 5 * 4;
const _indirectDispatchEntryByteSize = 3 * 4;

class WebgpuGraphicsDevice extends GraphicsDevice {
/**
* Array of GPU resources pending destruction. Resources are destroyed after the current
* command buffers are submitted to ensure they're not in use.
*
* @type {Array<GPUTexture|GPUBuffer|GPUQuerySet>}
* @private
*/
_deferredDestroys = [];

/**
* Object responsible for caching and creation of render pipelines.
*/
Expand Down Expand Up @@ -1109,6 +1118,28 @@ class WebgpuGraphicsDevice extends GraphicsDevice {
// notify dynamic buffers
this.dynamicBuffers.onCommandBuffersSubmitted();
}

// destroy deferred resources after submit to ensure they're no longer referenced
const deferredDestroys = this._deferredDestroys;
if (deferredDestroys.length > 0) {
for (let i = 0; i < deferredDestroys.length; i++) {
deferredDestroys[i].destroy();
}
deferredDestroys.length = 0;
}
}

/**
* Defer destruction of a GPU resource until after the current command buffers are submitted.
* This ensures the resource is not destroyed while still referenced by pending GPU commands.
*
* @param {GPUTexture|GPUBuffer|GPUQuerySet} gpuResource - The GPU resource to destroy.
* @private
*/
deferDestroy(gpuResource) {
if (gpuResource) {
this._deferredDestroys.push(gpuResource);
}
}

clear(options) {
Expand Down
3 changes: 2 additions & 1 deletion src/platform/graphics/webgpu/webgpu-texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class WebgpuTexture {
}

destroy(device) {
this.gpuTexture?.destroy();
// defer GPU texture destruction until after command buffer submission
device.deferDestroy(this.gpuTexture);
this.gpuTexture = null;
this.view = null;
this.viewCache.clear();
Expand Down