Skip to content

Commit

Permalink
use EncoderGuard for the other command encoder operations
Browse files Browse the repository at this point in the history
The spec requires us to invalidate the encoder if there was any error during the body of these operations.
  • Loading branch information
teoxoy authored and jimblandy committed Dec 2, 2024
1 parent 5e1fbd7 commit 02b28e2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
15 changes: 12 additions & 3 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand Down Expand Up @@ -138,6 +139,8 @@ impl Global {

if offset == end_offset {
log::trace!("Ignoring fill_buffer of size 0");

cmd_buf_data_guard.mark_successful();
return Ok(());
}

Expand All @@ -157,6 +160,8 @@ impl Global {
cmd_buf_raw.transition_buffers(dst_barrier.as_slice());
cmd_buf_raw.clear_buffer(dst_raw, offset..end_offset);
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -175,7 +180,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand Down Expand Up @@ -243,7 +249,10 @@ impl Global {
&device.alignments,
device.zero_buffer.as_ref(),
&snatch_guard,
)
)?;

cmd_buf_data_guard.mark_successful();
Ok(())
}
}

Expand Down
19 changes: 14 additions & 5 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ pub(crate) enum CommandEncoderStatus {

impl CommandEncoderStatus {
/// Checks that the encoder is in the [`Self::Recording`] state.
pub(crate) fn record(&mut self) -> Result<&mut CommandBufferMutable, CommandEncoderError> {
pub(crate) fn record(&mut self) -> Result<RecordingGuard<'_>, CommandEncoderError> {
match self {
Self::Recording(inner) => Ok(inner),
Self::Recording(_) => Ok(RecordingGuard { inner: self }),
Self::Locked(_) => {
*self = Self::Error;
Err(CommandEncoderError::Locked)
Expand Down Expand Up @@ -726,7 +726,8 @@ impl Global {

let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand All @@ -743,6 +744,8 @@ impl Global {
cmd_buf_raw.begin_debug_marker(label);
}
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -758,7 +761,8 @@ impl Global {

let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand All @@ -775,6 +779,8 @@ impl Global {
cmd_buf_raw.insert_debug_marker(label);
}
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -789,7 +795,8 @@ impl Global {

let cmd_buf = hub.command_buffers.get(encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand All @@ -806,6 +813,8 @@ impl Global {
cmd_buf_raw.end_debug_marker();
}
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand Down
8 changes: 6 additions & 2 deletions wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

cmd_buf
.device
Expand All @@ -344,6 +345,7 @@ impl Global {

cmd_buf_data.trackers.query_sets.insert_single(query_set);

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -362,7 +364,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

#[cfg(feature = "trace")]
if let Some(ref mut list) = cmd_buf_data.commands {
Expand Down Expand Up @@ -458,6 +461,7 @@ impl Global {

cmd_buf_data.trackers.query_sets.insert_single(query_set);

cmd_buf_data_guard.mark_successful();
Ok(())
}
}
18 changes: 14 additions & 4 deletions wgpu-core/src/command/ray_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ impl Global {
let mut scratch_buffer_blas_size = 0;
let mut blas_storage = Vec::new();
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

iter_blas(
blas_iter,
Expand Down Expand Up @@ -276,7 +277,10 @@ impl Global {

let scratch_size =
match wgt::BufferSize::new(max(scratch_buffer_blas_size, scratch_buffer_tlas_size)) {
None => return Ok(()),
None => {
cmd_buf_data_guard.mark_successful();
return Ok(());
}
Some(size) => size,
};

Expand Down Expand Up @@ -353,6 +357,7 @@ impl Global {
.consume_temp(TempResource::ScratchBuffer(scratch_buffer));
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand Down Expand Up @@ -486,7 +491,8 @@ impl Global {
let mut scratch_buffer_blas_size = 0;
let mut blas_storage = Vec::new();
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

iter_blas(
blas_iter,
Expand Down Expand Up @@ -611,7 +617,10 @@ impl Global {
let scratch_size =
match wgt::BufferSize::new(max(scratch_buffer_blas_size, scratch_buffer_tlas_size)) {
// if the size is zero there is nothing to build
None => return Ok(()),
None => {
cmd_buf_data_guard.mark_successful();
return Ok(());
}
Some(size) => size,
};

Expand Down Expand Up @@ -763,6 +772,7 @@ impl Global {
.consume_temp(TempResource::ScratchBuffer(scratch_buffer));
}

cmd_buf_data_guard.mark_successful();
Ok(())
}
}
Expand Down
23 changes: 19 additions & 4 deletions wgpu-core/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

let device = &cmd_buf.device;
device.check_is_valid()?;
Expand Down Expand Up @@ -652,6 +653,7 @@ impl Global {

if size == 0 {
log::trace!("Ignoring copy_buffer_to_buffer of size 0");
cmd_buf_data_guard.mark_successful();
return Ok(());
}

Expand Down Expand Up @@ -685,6 +687,8 @@ impl Global {
cmd_buf_raw.transition_buffers(&barriers);
cmd_buf_raw.copy_buffer_to_buffer(src_raw, dst_raw, &[region]);
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -708,7 +712,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

let device = &cmd_buf.device;
device.check_is_valid()?;
Expand All @@ -724,6 +729,7 @@ impl Global {

if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 {
log::trace!("Ignoring copy_buffer_to_texture of size 0");
cmd_buf_data_guard.mark_successful();
return Ok(());
}

Expand Down Expand Up @@ -838,6 +844,8 @@ impl Global {
cmd_buf_raw.transition_buffers(src_barrier.as_slice());
cmd_buf_raw.copy_buffer_to_texture(src_raw, dst_raw, &regions);
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -861,7 +869,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

let device = &cmd_buf.device;
device.check_is_valid()?;
Expand All @@ -877,6 +886,7 @@ impl Global {

if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 {
log::trace!("Ignoring copy_texture_to_buffer of size 0");
cmd_buf_data_guard.mark_successful();
return Ok(());
}

Expand Down Expand Up @@ -1005,6 +1015,8 @@ impl Global {
&regions,
);
}

cmd_buf_data_guard.mark_successful();
Ok(())
}

Expand All @@ -1028,7 +1040,8 @@ impl Global {
.command_buffers
.get(command_encoder_id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record()?;
let mut cmd_buf_data_guard = cmd_buf_data.record()?;
let cmd_buf_data = &mut *cmd_buf_data_guard;

let device = &cmd_buf.device;
device.check_is_valid()?;
Expand All @@ -1046,6 +1059,7 @@ impl Global {

if copy_size.width == 0 || copy_size.height == 0 || copy_size.depth_or_array_layers == 0 {
log::trace!("Ignoring copy_texture_to_texture of size 0");
cmd_buf_data_guard.mark_successful();
return Ok(());
}

Expand Down Expand Up @@ -1165,6 +1179,7 @@ impl Global {
);
}

cmd_buf_data_guard.mark_successful();
Ok(())
}
}
10 changes: 6 additions & 4 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,15 +1369,17 @@ impl Global {

let cmd_buf = hub.command_buffers.get(id.into_command_buffer_id());
let mut cmd_buf_data = cmd_buf.data.lock();
let cmd_buf_data = cmd_buf_data.record();
let cmd_buf_data_guard = cmd_buf_data.record();

if let Ok(cmd_buf_data) = cmd_buf_data {
let cmd_buf_raw = cmd_buf_data
if let Ok(mut cmd_buf_data_guard) = cmd_buf_data_guard {
let cmd_buf_raw = cmd_buf_data_guard
.encoder
.open(&cmd_buf.device)
.ok()
.and_then(|encoder| encoder.as_any_mut().downcast_mut());
hal_command_encoder_callback(cmd_buf_raw)
let ret = hal_command_encoder_callback(cmd_buf_raw);
cmd_buf_data_guard.mark_successful();
ret
} else {
hal_command_encoder_callback(None)
}
Expand Down

0 comments on commit 02b28e2

Please sign in to comment.