Skip to content

Commit

Permalink
remove open field from CommandBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Oct 18, 2022
1 parent 468f3df commit 935629e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
38 changes: 27 additions & 11 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3598,10 +3598,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
A::hub(self).buffers.label_for_resource(id)
}

pub fn buffer_destroy<A: HalApi>(
&self,
buffer_id: id::BufferId,
) -> Result<(), resource::DestroyError> {
pub fn buffer_destroy<A: HalApi>(&self, buffer_id: id::BufferId) -> Result<(), resource::DestroyError> {
profiling::scope!("Buffer::destroy");

let map_closure;
Expand Down Expand Up @@ -3637,10 +3634,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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) {
Expand Down Expand Up @@ -4583,13 +4577,30 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
A::hub(self).command_buffers.label_for_resource(id)
}

pub fn command_encoder_drop<A: HalApi>(&self, command_encoder_id: id::CommandEncoderId) {
pub fn command_encoder_drop<A: HalApi>(
&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
Expand All @@ -4599,9 +4610,14 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
device.untrack::<G>(hub, &cmdbuf.trackers, &mut token);
device.destroy_command_buffer(cmdbuf);
}

Ok(())
}

pub fn command_buffer_drop<A: HalApi>(&self, command_buffer_id: id::CommandBufferId) {
pub fn command_buffer_drop<A: HalApi>(
&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::<A>(command_buffer_id)
Expand Down
17 changes: 10 additions & 7 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1620,7 +1619,6 @@ impl crate::Context for Context {
CommandEncoder {
id,
error_sink: Arc::clone(&device.error_sink),
open: true,
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 935629e

Please sign in to comment.