From 935629e453c81360a22e588e07a4403bfc90e1c0 Mon Sep 17 00:00:00 2001 From: i509VCB Date: Thu, 13 Oct 2022 01:14:06 -0500 Subject: [PATCH] remove open field from CommandBuffer --- wgpu-core/src/device/mod.rs | 38 ++++++++++++++++++++++++++----------- wgpu/src/backend/direct.rs | 17 ++++++++++------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 57c6b943fcf..0cb51bc4b5f 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3598,10 +3598,7 @@ impl Global { A::hub(self).buffers.label_for_resource(id) } - pub fn buffer_destroy( - &self, - buffer_id: id::BufferId, - ) -> Result<(), resource::DestroyError> { + pub fn buffer_destroy(&self, buffer_id: id::BufferId) -> Result<(), resource::DestroyError> { profiling::scope!("Buffer::destroy"); let map_closure; @@ -3637,10 +3634,7 @@ impl Global { trace.lock().add(trace::Action::FreeBuffer(buffer_id)); } - let raw = buffer - .raw - .take() - .ok_or(resource::DestroyError::AlreadyDestroyed)?; + let raw = buffer.raw.take().ok_or(resource::DestroyError::AlreadyDestroyed)?; let temp = queue::TempResource::Buffer(raw); if device.pending_writes.dst_buffers.contains(&buffer_id) { @@ -4583,13 +4577,30 @@ impl Global { A::hub(self).command_buffers.label_for_resource(id) } - pub fn command_encoder_drop(&self, command_encoder_id: id::CommandEncoderId) { + pub fn command_encoder_drop( + &self, + command_encoder_id: id::CommandEncoderId, + ) -> Result<(), resource::DestroyError> { profiling::scope!("CommandEncoder::drop"); - log::debug!("command encoder {:?} is dropped", command_encoder_id); let hub = A::hub(self); let mut token = Token::root(); + { + let (cmd_buf_guard, _) = hub.command_buffers.read(&mut token); + + let cmd_buf = cmd_buf_guard + .get(command_encoder_id) + .map_err(|_| resource::DestroyError::Invalid)?; + + // Defer destruction to command completion if the command buffer is finished. + if cmd_buf.is_finished() { + return Ok(()); + } + } + + log::debug!("command encoder {:?} is dropped", command_encoder_id); + let (mut device_guard, mut token) = hub.devices.write(&mut token); let (cmdbuf, _) = hub .command_buffers @@ -4599,9 +4610,14 @@ impl Global { device.untrack::(hub, &cmdbuf.trackers, &mut token); device.destroy_command_buffer(cmdbuf); } + + Ok(()) } - pub fn command_buffer_drop(&self, command_buffer_id: id::CommandBufferId) { + pub fn command_buffer_drop( + &self, + command_buffer_id: id::CommandBufferId, + ) -> Result<(), resource::DestroyError> { profiling::scope!("CommandBuffer::drop"); log::debug!("command buffer {:?} is dropped", command_buffer_id); self.command_encoder_drop::(command_buffer_id) diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 8572a452c08..1f2d92bafb7 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -796,7 +796,6 @@ pub struct Texture { pub(crate) struct CommandEncoder { id: wgc::id::CommandEncoderId, error_sink: ErrorSink, - open: bool, } impl crate::Context for Context { @@ -1620,7 +1619,6 @@ impl crate::Context for Context { CommandEncoder { id, error_sink: Arc::clone(&device.error_sink), - open: true, } } @@ -1840,14 +1838,20 @@ impl crate::Context for Context { wgc::gfx_select!(*shader_module => global.shader_module_drop(*shader_module)) } fn command_encoder_drop(&self, command_encoder: &Self::CommandEncoderId) { - if command_encoder.open { - let global = &self.0; + let global = &self.0; + if let Err(_cause) = wgc::gfx_select!(command_encoder.id => global.command_encoder_drop(command_encoder.id)) + { + todo!("Error?") } } fn command_buffer_drop(&self, command_buffer: &Self::CommandBufferId) { let global = &self.0; - wgc::gfx_select!(*command_buffer => global.command_buffer_drop(*command_buffer)) + if let Err(_cause) = + wgc::gfx_select!(*command_buffer => global.command_buffer_drop(*command_buffer)) + { + todo!("Error?") + } } fn render_bundle_drop(&self, render_bundle: &Self::RenderBundleId) { let global = &self.0; @@ -2114,9 +2118,8 @@ impl crate::Context for Context { } } - fn command_encoder_finish(&self, mut encoder: Self::CommandEncoderId) -> Self::CommandBufferId { + fn command_encoder_finish(&self, encoder: Self::CommandEncoderId) -> Self::CommandBufferId { let descriptor = wgt::CommandBufferDescriptor::default(); - encoder.open = false; // prevent the drop let global = &self.0; let (id, error) = wgc::gfx_select!(encoder.id => global.command_encoder_finish(encoder.id, &descriptor));