[deno] Fix dropping of command encoders/buffers, and an enum typo (#7808)

Fixes #7797
This commit is contained in:
Andy Leiserson authored and GitHub committed 2025-06-16 10:50:19 -07:00
1 parent 371c8fd1db
commit afa0f71736
6 files changed
+35 -24

No files matched your search

+25 -5
View File
@@ -2,6 +2,7 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};
use deno_core::cppgc::Ptr;
use deno_core::op2;
@@ -28,11 +29,19 @@ pub struct GPUCommandEncoder {
pub id: wgpu_core::id::CommandEncoderId,
pub label: String,
pub finished: AtomicBool,
}
impl Drop for GPUCommandEncoder {
fn drop(&mut self) {
self.instance.command_encoder_drop(self.id);
// Command encoders and command buffers are both the same wgpu object.
// At the time `finished` is set, ownership of the id (and
// responsibility for dropping it) transfers from the encoder to the
// buffer.
if !self.finished.load(Ordering::SeqCst) {
self.instance.command_encoder_drop(self.id);
}
}
}
@@ -407,23 +416,34 @@ impl GPUCommandEncoder {
fn finish(
&self,
#[webidl] descriptor: crate::command_buffer::GPUCommandBufferDescriptor,
) -> GPUCommandBuffer {
) -> Result<GPUCommandBuffer, JsErrorBox> {
let wgpu_descriptor = wgpu_types::CommandBufferDescriptor {
label: crate::transform_label(descriptor.label.clone()),
};
// TODO(https://github.com/gfx-rs/wgpu/issues/7812): This is not right,
// it should be a validation error, and it would be nice if we can just
// let wgpu generate it for us. The problem is that if the encoder was
// already finished, we transferred ownership of the id to a command
// buffer, so we have to bail out before we mint a duplicate command
// buffer with the same id below.
if self.finished.fetch_or(true, Ordering::SeqCst) {
return Err(JsErrorBox::type_error(
"The command encoder has already finished.",
));
}
let (id, err) = self
.instance
.command_encoder_finish(self.id, &wgpu_descriptor);
self.error_handler.push_error(err);
GPUCommandBuffer {
Ok(GPUCommandBuffer {
instance: self.instance.clone(),
id,
label: descriptor.label,
consumed: Default::default(),
}
})
}
fn push_debug_group(&self, #[webidl] group_label: String) {