diff --git a/tests/tests/wgpu-gpu/encoder.rs b/tests/tests/wgpu-gpu/encoder.rs index 7ab047977..7f4d50498 100644 --- a/tests/tests/wgpu-gpu/encoder.rs +++ b/tests/tests/wgpu-gpu/encoder.rs @@ -308,23 +308,15 @@ fn encoder_operations_fail_while_pass_alive(ctx: TestingContext) { ctx.device.push_error_scope(wgpu::ErrorFilter::Validation); log::info!("Testing operation {op_name:?} on a locked command encoder while a {pass_type:?} pass is active"); - fail( - &ctx.device, - || op(&mut encoder), - Some("Command encoder is locked"), - ); + fail(&ctx.device, || op(&mut encoder), Some("encoder is locked")); // Drop the pass - this also fails now since the encoder is invalid: - fail( - &ctx.device, - || drop(pass), - Some("Command encoder is invalid"), - ); + fail(&ctx.device, || drop(pass), Some("encoder is invalid")); // Also, it's not possible to create a new pass on the encoder: fail( &ctx.device, || encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default()), - Some("Command encoder is invalid"), + Some("encoder is invalid"), ); } @@ -334,16 +326,8 @@ fn encoder_operations_fail_while_pass_alive(ctx: TestingContext) { .device .create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); let pass = create_pass(&mut encoder, pass_type); - fail( - &ctx.device, - || encoder.finish(), - Some("Command encoder is locked"), - ); - fail( - &ctx.device, - || drop(pass), - Some("Command encoder is invalid"), - ); + fail(&ctx.device, || encoder.finish(), Some("encoder is locked")); + fail(&ctx.device, || drop(pass), Some("encoder is invalid")); } } } diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index 42096f825..ac1d88ebc 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -5,7 +5,7 @@ use core::ops::Range; use crate::device::trace::Command as TraceCommand; use crate::{ api_log, - command::CommandEncoderError, + command::EncoderStateError, device::DeviceError, get_lowest_common_denom, global::Global, @@ -74,7 +74,7 @@ whereas subesource range specified start {subresource_base_array_layer} and coun #[error(transparent)] Device(#[from] DeviceError), #[error(transparent)] - CommandEncoderError(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error(transparent)] InvalidResource(#[from] InvalidResourceError), } diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index def050fd7..f25fbe520 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -4,6 +4,7 @@ use wgt::{BufferAddress, DynamicOffset}; use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec}; use core::{fmt, str}; +use crate::command::EncoderStateError; use crate::ray_tracing::AsAction; use crate::{ binding_model::{ @@ -127,7 +128,7 @@ pub enum ComputePassErrorInner { #[error(transparent)] Device(#[from] DeviceError), #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error("Parent encoder is invalid")] InvalidParentEncoder, #[error("Bind group index {index} is greater than the device's requested `max_bind_group` limit {max}")] @@ -301,7 +302,7 @@ impl Global { match cmd_buf.data.lock().lock_encoder() { Ok(_) => {} - Err(e) => return make_err(e, arc_desc), + Err(e) => return make_err(e.into(), arc_desc), }; arc_desc.timestamp_writes = match desc diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index fbb1c488a..c7f5b3b9f 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -95,15 +95,15 @@ pub(crate) enum CommandEncoderStatus { impl CommandEncoderStatus { /// Checks that the encoder is in the [`Self::Recording`] state. - pub(crate) fn record(&mut self) -> Result, CommandEncoderError> { + pub(crate) fn record(&mut self) -> Result, EncoderStateError> { match self { Self::Recording(_) => Ok(RecordingGuard { inner: self }), Self::Locked(_) => { *self = Self::Error; - Err(CommandEncoderError::Locked) + Err(EncoderStateError::Locked) } - Self::Finished(_) => Err(CommandEncoderError::NotRecording), - Self::Error => Err(CommandEncoderError::Invalid), + Self::Finished(_) => Err(EncoderStateError::Ended), + Self::Error => Err(EncoderStateError::Invalid), } } @@ -122,7 +122,7 @@ impl CommandEncoderStatus { /// Locks the encoder by putting it in the [`Self::Locked`] state. /// /// Call [`Self::unlock_encoder`] to put the [`CommandBuffer`] back into the [`Self::Recording`] state. - fn lock_encoder(&mut self) -> Result<(), CommandEncoderError> { + fn lock_encoder(&mut self) -> Result<(), EncoderStateError> { match mem::replace(self, Self::Error) { Self::Recording(inner) => { *self = Self::Locked(inner); @@ -130,10 +130,10 @@ impl CommandEncoderStatus { } Self::Finished(inner) => { *self = Self::Finished(inner); - Err(CommandEncoderError::NotRecording) + Err(EncoderStateError::Ended) } - Self::Locked(_) => Err(CommandEncoderError::Locked), - Self::Error => Err(CommandEncoderError::Invalid), + Self::Locked(_) => Err(EncoderStateError::Locked), + Self::Error => Err(EncoderStateError::Invalid), } } @@ -142,7 +142,7 @@ impl CommandEncoderStatus { /// This function is the unlocking counterpart to [`Self::lock_encoder`]. /// /// It is only valid to call this function if the encoder is in the [`Self::Locked`] state. - fn unlock_encoder(&mut self) -> Result, CommandEncoderError> { + fn unlock_encoder(&mut self) -> Result, EncoderStateError> { match mem::replace(self, Self::Error) { Self::Locked(inner) => { *self = Self::Recording(inner); @@ -150,10 +150,10 @@ impl CommandEncoderStatus { } Self::Finished(inner) => { *self = Self::Finished(inner); - Err(CommandEncoderError::NotRecording) + Err(EncoderStateError::Ended) } - Self::Recording(_) => Err(CommandEncoderError::Invalid), - Self::Error => Err(CommandEncoderError::Invalid), + Self::Recording(_) => Err(EncoderStateError::Invalid), + Self::Error => Err(EncoderStateError::Invalid), } } @@ -171,10 +171,10 @@ impl CommandEncoderStatus { } Self::Finished(inner) => { *self = Self::Finished(inner); - Err(CommandEncoderError::NotRecording) + Err(EncoderStateError::Ended.into()) } - Self::Locked(_) => Err(CommandEncoderError::Locked), - Self::Error => Err(CommandEncoderError::Invalid), + Self::Locked(_) => Err(EncoderStateError::Locked.into()), + Self::Error => Err(EncoderStateError::Invalid.into()), } } } @@ -725,18 +725,27 @@ impl BasePass { } } +/// Errors related to the state of a command or pass encoder. +#[derive(Clone, Debug, Error)] +#[non_exhaustive] +pub enum EncoderStateError { + #[error("Encoder is invalid")] + Invalid, + #[error("Encoding must not have ended")] + Ended, + + /// Note: only command encoders can be locked (not pass encoders). + #[error("Encoder is locked by a previously created render/compute pass. Before recording any new commands, the pass must be ended.")] + Locked, +} + #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum CommandEncoderError { - #[error("Command encoder is invalid")] - Invalid, - #[error("Command encoder must be active")] - NotRecording, + #[error(transparent)] + State(#[from] EncoderStateError), #[error(transparent)] Device(#[from] DeviceError), - #[error("Command encoder is locked by a previously created render/compute pass. Before recording any new commands, the pass must be ended.")] - Locked, - #[error(transparent)] InvalidColorAttachment(#[from] ColorAttachmentError), #[error(transparent)] diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs index c83ade669..7c6ff5561 100644 --- a/wgpu-core/src/command/query.rs +++ b/wgpu-core/src/command/query.rs @@ -4,7 +4,7 @@ use core::{iter, mem}; #[cfg(feature = "trace")] use crate::device::trace::Command as TraceCommand; use crate::{ - command::{CommandBuffer, CommandEncoderError}, + command::{CommandBuffer, EncoderStateError}, device::{DeviceError, MissingFeatures}, global::Global, id, @@ -96,7 +96,7 @@ pub enum QueryError { #[error(transparent)] Device(#[from] DeviceError), #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error(transparent)] MissingFeature(#[from] MissingFeatures), #[error("Error encountered while trying to use queries")] diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 644c190c7..7df559fa9 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -11,6 +11,7 @@ use wgt::{ use crate::binding_model::BindGroup; use crate::command::{ validate_and_begin_occlusion_query, validate_and_begin_pipeline_statistics_query, + EncoderStateError, }; use crate::init_tracker::BufferInitTrackerAction; use crate::pipeline::{RenderPipeline, VertexStep}; @@ -662,7 +663,7 @@ pub enum RenderPassErrorInner { #[error(transparent)] ColorAttachment(#[from] ColorAttachmentError), #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error("Parent encoder is invalid")] InvalidParentEncoder, #[error("The format of the {location} ({format:?}) is not resolvable")] @@ -1610,7 +1611,7 @@ impl Global { match cmd_buf.data.lock().lock_encoder() { Ok(_) => {} - Err(e) => return make_err(e, arc_desc), + Err(e) => return make_err(e.into(), arc_desc), }; let err = fill_arc_desc(hub, desc, &mut arc_desc, &cmd_buf.device).err(); diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index bdb5ea0b3..debc7530b 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -8,7 +8,7 @@ use wgt::{BufferAddress, BufferUsages, Extent3d, TextureSelector, TextureUsages} use crate::device::trace::Command as TraceCommand; use crate::{ api_log, - command::{clear_texture, CommandEncoderError}, + command::{clear_texture, EncoderStateError}, conv, device::{Device, DeviceError, MissingDownlevelFlags}, global::Global, @@ -161,7 +161,9 @@ pub enum TransferError { #[non_exhaustive] pub enum CopyError { #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), + #[error(transparent)] + Device(#[from] DeviceError), #[error("Copy error")] Transfer(#[from] TransferError), #[error(transparent)] @@ -170,12 +172,6 @@ pub enum CopyError { InvalidResource(#[from] InvalidResourceError), } -impl From for CopyError { - fn from(err: DeviceError) -> Self { - CopyError::Encoder(CommandEncoderError::Device(err)) - } -} - pub(crate) fn extract_texture_selector( copy_texture: &wgt::TexelCopyTextureInfo, copy_size: &Extent3d, diff --git a/wgpu-core/src/command/transition_resources.rs b/wgpu-core/src/command/transition_resources.rs index 794343e27..80dbfa606 100644 --- a/wgpu-core/src/command/transition_resources.rs +++ b/wgpu-core/src/command/transition_resources.rs @@ -1,7 +1,7 @@ use thiserror::Error; use crate::{ - command::CommandBuffer, + command::{CommandBuffer, EncoderStateError}, device::DeviceError, global::Global, id::{BufferId, CommandEncoderId, TextureId}, @@ -9,8 +9,6 @@ use crate::{ track::ResourceUsageCompatibilityError, }; -use super::CommandEncoderError; - impl Global { pub fn command_encoder_transition_resources( &self, @@ -85,7 +83,7 @@ pub enum TransitionResourcesError { #[error(transparent)] Device(#[from] DeviceError), #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error(transparent)] InvalidResource(#[from] InvalidResourceError), #[error(transparent)] diff --git a/wgpu-core/src/ray_tracing.rs b/wgpu-core/src/ray_tracing.rs index 602b42fe4..f3b4cfeb6 100644 --- a/wgpu-core/src/ray_tracing.rs +++ b/wgpu-core/src/ray_tracing.rs @@ -13,7 +13,7 @@ use thiserror::Error; use wgt::{AccelerationStructureGeometryFlags, BufferAddress, IndexFormat, VertexFormat}; use crate::{ - command::CommandEncoderError, + command::EncoderStateError, device::{DeviceError, MissingFeatures}, id::{BlasId, BufferId, TlasId}, resource::{ @@ -50,7 +50,7 @@ pub enum CreateTlasError { #[derive(Clone, Debug, Error)] pub enum BuildAccelerationStructureError { #[error(transparent)] - Encoder(#[from] CommandEncoderError), + EncoderState(#[from] EncoderStateError), #[error(transparent)] Device(#[from] DeviceError),