From 6066e069d141d7c0c19e088011fdf3c06782d12c Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Fri, 10 May 2024 04:10:22 -0700 Subject: fix(ext/webgpu): invalidate GPUAdapter when a device is created (#23752) This removes the need for using `Deno.resources` to close the gpuadapter resource, while being more spec compliant. --- ext/webgpu/01_webgpu.js | 17 +++++++++++++++++ ext/webgpu/lib.rs | 8 +++++--- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'ext') diff --git a/ext/webgpu/01_webgpu.js b/ext/webgpu/01_webgpu.js index be0b87cdf..502de2124 100644 --- a/ext/webgpu/01_webgpu.js +++ b/ext/webgpu/01_webgpu.js @@ -417,9 +417,12 @@ function createGPUAdapter(inner) { return adapter; } +const _invalid = Symbol("[[invalid]]"); class GPUAdapter { /** @type {InnerGPUAdapter} */ [_adapter]; + /** @type {bool} */ + [_invalid]; /** @returns {GPUSupportedFeatures} */ get features() { @@ -466,6 +469,12 @@ class GPUAdapter { } } + if (this[_invalid]) { + throw new TypeError( + "The adapter cannot be reused, as it has been invalidated by a device creation", + ); + } + const { rid, queueRid, features, limits } = op_webgpu_request_device( this[_adapter].rid, descriptor.label, @@ -473,6 +482,8 @@ class GPUAdapter { descriptor.requiredLimits, ); + this[_invalid] = true; + const inner = new InnerGPUDevice({ rid, adapter: this, @@ -496,6 +507,12 @@ class GPUAdapter { requestAdapterInfo() { webidl.assertBranded(this, GPUAdapterPrototype); + if (this[_invalid]) { + throw new TypeError( + "The adapter cannot be reused, as it has been invalidated by a device creation", + ); + } + const { vendor, architecture, diff --git a/ext/webgpu/lib.rs b/ext/webgpu/lib.rs index eeaae2dd6..8a423fd00 100644 --- a/ext/webgpu/lib.rs +++ b/ext/webgpu/lib.rs @@ -673,7 +673,7 @@ pub fn op_webgpu_request_device( ) -> Result { let mut state = state.borrow_mut(); let adapter_resource = - state.resource_table.get::(adapter_rid)?; + state.resource_table.take::(adapter_rid)?; let adapter = adapter_resource.1; let instance = state.borrow::(); @@ -690,6 +690,7 @@ pub fn op_webgpu_request_device( None, None )); + adapter_resource.close(); if let Some(err) = maybe_err { return Err(DomExceptionOperationError::new(&err.to_string()).into()); } @@ -731,13 +732,14 @@ pub fn op_webgpu_request_adapter_info( state: Rc>, #[smi] adapter_rid: ResourceId, ) -> Result { - let state = state.borrow_mut(); + let mut state = state.borrow_mut(); let adapter_resource = - state.resource_table.get::(adapter_rid)?; + state.resource_table.take::(adapter_rid)?; let adapter = adapter_resource.1; let instance = state.borrow::(); let info = gfx_select!(adapter => instance.adapter_get_info(adapter))?; + adapter_resource.close(); Ok(GPUAdapterInfo { vendor: info.vendor.to_string(), -- cgit v1.2.3