diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index a2679ba566..9b7bc9726a 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -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 { @@ -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(()); } @@ -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(()) } @@ -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 { @@ -243,7 +249,10 @@ impl Global { &device.alignments, device.zero_buffer.as_ref(), &snatch_guard, - ) + )?; + + cmd_buf_data_guard.mark_successful(); + Ok(()) } } diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index cbdcae9bed..cbc6969e29 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -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, CommandEncoderError> { match self { - Self::Recording(inner) => Ok(inner), + Self::Recording(_) => Ok(RecordingGuard { inner: self }), Self::Locked(_) => { *self = Self::Error; Err(CommandEncoderError::Locked) @@ -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 { @@ -743,6 +744,8 @@ impl Global { cmd_buf_raw.begin_debug_marker(label); } } + + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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 { @@ -775,6 +779,8 @@ impl Global { cmd_buf_raw.insert_debug_marker(label); } } + + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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 { @@ -806,6 +813,8 @@ impl Global { cmd_buf_raw.end_debug_marker(); } } + + cmd_buf_data_guard.mark_successful(); Ok(()) } diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs index becf7ce3bb..56ca3339de 100644 --- a/wgpu-core/src/command/query.rs +++ b/wgpu-core/src/command/query.rs @@ -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 @@ -344,6 +345,7 @@ impl Global { cmd_buf_data.trackers.query_sets.insert_single(query_set); + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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 { @@ -458,6 +461,7 @@ impl Global { cmd_buf_data.trackers.query_sets.insert_single(query_set); + cmd_buf_data_guard.mark_successful(); Ok(()) } } diff --git a/wgpu-core/src/command/ray_tracing.rs b/wgpu-core/src/command/ray_tracing.rs index 421c727ae7..8732377560 100644 --- a/wgpu-core/src/command/ray_tracing.rs +++ b/wgpu-core/src/command/ray_tracing.rs @@ -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, @@ -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, }; @@ -353,6 +357,7 @@ impl Global { .consume_temp(TempResource::ScratchBuffer(scratch_buffer)); } + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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, @@ -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, }; @@ -763,6 +772,7 @@ impl Global { .consume_temp(TempResource::ScratchBuffer(scratch_buffer)); } + cmd_buf_data_guard.mark_successful(); Ok(()) } } diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index e1eeece5d3..3ecff5ca43 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -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()?; @@ -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(()); } @@ -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(()) } @@ -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()?; @@ -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(()); } @@ -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, ®ions); } + + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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()?; @@ -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(()); } @@ -1005,6 +1015,8 @@ impl Global { ®ions, ); } + + cmd_buf_data_guard.mark_successful(); Ok(()) } @@ -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()?; @@ -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(()); } @@ -1165,6 +1179,7 @@ impl Global { ); } + cmd_buf_data_guard.mark_successful(); Ok(()) } } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 82d82ac871..f0afab14c0 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -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) }