diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 1429300bc..62969cbe2 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -581,7 +581,7 @@ impl RenderBundleEncoder { fn set_bind_group( state: &mut State, - bind_group_guard: &crate::storage::Storage>, + bind_group_guard: &crate::storage::Storage>, dynamic_offsets: &[u32], index: u32, num_dynamic_offsets: usize, @@ -594,9 +594,7 @@ fn set_bind_group( let bind_group_id = bind_group_id.unwrap(); - let bind_group = bind_group_guard - .get_owned(bind_group_id) - .map_err(|_| RenderCommandError::InvalidBindGroupId(bind_group_id))?; + let bind_group = bind_group_guard.strict_get(bind_group_id).get()?; bind_group.same_device(&state.device)?; diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 6f9124656..51eadc587 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -132,8 +132,6 @@ pub enum ComputePassErrorInner { Encoder(#[from] CommandEncoderError), #[error("Parent encoder is invalid")] InvalidParentEncoder, - #[error("BindGroupId {0:?} is invalid")] - InvalidBindGroupId(id::BindGroupId), #[error("Bind group index {index} is greater than the device's requested `max_bind_group` limit {max}")] BindGroupIndexOutOfRange { index: u32, max: u32 }, #[error("ComputePipelineId {0:?} is invalid")] @@ -985,8 +983,8 @@ impl Global { let hub = &self.hub; let bg = hub .bind_groups - .get(bind_group_id) - .map_err(|_| ComputePassErrorInner::InvalidBindGroupId(bind_group_id)) + .strict_get(bind_group_id) + .get() .map_pass_err(scope)?; bind_group = Some(bg); } diff --git a/wgpu-core/src/command/compute_command.rs b/wgpu-core/src/command/compute_command.rs index c2f7facbb..30d1bb74e 100644 --- a/wgpu-core/src/command/compute_command.rs +++ b/wgpu-core/src/command/compute_command.rs @@ -81,113 +81,115 @@ impl ComputeCommand { let query_set_guard = hub.query_sets.read(); let pipelines_guard = hub.compute_pipelines.read(); - let resolved_commands: Vec = - commands - .iter() - .map(|c| -> Result { - Ok(match *c { - ComputeCommand::SetBindGroup { - index, - num_dynamic_offsets, - bind_group_id, - } => { - if bind_group_id.is_none() { - return Ok(ArcComputeCommand::SetBindGroup { - index, - num_dynamic_offsets, - bind_group: None, - }); - } - - let bind_group_id = bind_group_id.unwrap(); - let bg = bind_group_guard.get_owned(bind_group_id).map_err(|_| { - ComputePassError { - scope: PassErrorScope::SetBindGroup, - inner: ComputePassErrorInner::InvalidBindGroupId(bind_group_id), - } - })?; - - ArcComputeCommand::SetBindGroup { + let resolved_commands: Vec = commands + .iter() + .map(|c| -> Result { + Ok(match *c { + ComputeCommand::SetBindGroup { + index, + num_dynamic_offsets, + bind_group_id, + } => { + if bind_group_id.is_none() { + return Ok(ArcComputeCommand::SetBindGroup { index, num_dynamic_offsets, - bind_group: Some(bg), - } + bind_group: None, + }); } - ComputeCommand::SetPipeline(pipeline_id) => ArcComputeCommand::SetPipeline( - pipelines_guard.get_owned(pipeline_id).map_err(|_| { + + let bind_group_id = bind_group_id.unwrap(); + let bg = bind_group_guard + .strict_get(bind_group_id) + .get() + .map_err(|e| ComputePassError { + scope: PassErrorScope::SetBindGroup, + inner: e.into(), + })?; + + ArcComputeCommand::SetBindGroup { + index, + num_dynamic_offsets, + bind_group: Some(bg), + } + } + ComputeCommand::SetPipeline(pipeline_id) => ArcComputeCommand::SetPipeline( + pipelines_guard + .get_owned(pipeline_id) + .map_err(|_| ComputePassError { + scope: PassErrorScope::SetPipelineCompute, + inner: ComputePassErrorInner::InvalidPipelineId(pipeline_id), + })?, + ), + + ComputeCommand::SetPushConstant { + offset, + size_bytes, + values_offset, + } => ArcComputeCommand::SetPushConstant { + offset, + size_bytes, + values_offset, + }, + + ComputeCommand::Dispatch(dim) => ArcComputeCommand::Dispatch(dim), + + ComputeCommand::DispatchIndirect { buffer_id, offset } => { + ArcComputeCommand::DispatchIndirect { + buffer: buffers_guard.strict_get(buffer_id).get().map_err(|e| { ComputePassError { - scope: PassErrorScope::SetPipelineCompute, - inner: ComputePassErrorInner::InvalidPipelineId(pipeline_id), + scope: PassErrorScope::Dispatch { indirect: true }, + inner: e.into(), } })?, - ), - - ComputeCommand::SetPushConstant { offset, - size_bytes, - values_offset, - } => ArcComputeCommand::SetPushConstant { - offset, - size_bytes, - values_offset, - }, - - ComputeCommand::Dispatch(dim) => ArcComputeCommand::Dispatch(dim), - - ComputeCommand::DispatchIndirect { buffer_id, offset } => { - ArcComputeCommand::DispatchIndirect { - buffer: buffers_guard.strict_get(buffer_id).get().map_err(|e| { - ComputePassError { - scope: PassErrorScope::Dispatch { indirect: true }, - inner: e.into(), - } - })?, - offset, - } } + } - ComputeCommand::PushDebugGroup { color, len } => { - ArcComputeCommand::PushDebugGroup { color, len } - } + ComputeCommand::PushDebugGroup { color, len } => { + ArcComputeCommand::PushDebugGroup { color, len } + } - ComputeCommand::PopDebugGroup => ArcComputeCommand::PopDebugGroup, + ComputeCommand::PopDebugGroup => ArcComputeCommand::PopDebugGroup, - ComputeCommand::InsertDebugMarker { color, len } => { - ArcComputeCommand::InsertDebugMarker { color, len } - } + ComputeCommand::InsertDebugMarker { color, len } => { + ArcComputeCommand::InsertDebugMarker { color, len } + } - ComputeCommand::WriteTimestamp { - query_set_id, - query_index, - } => ArcComputeCommand::WriteTimestamp { - query_set: query_set_guard.strict_get(query_set_id).get().map_err( - |e| ComputePassError { - scope: PassErrorScope::WriteTimestamp, - inner: e.into(), - }, - )?, - query_index, - }, + ComputeCommand::WriteTimestamp { + query_set_id, + query_index, + } => ArcComputeCommand::WriteTimestamp { + query_set: query_set_guard + .strict_get(query_set_id) + .get() + .map_err(|e| ComputePassError { + scope: PassErrorScope::WriteTimestamp, + inner: e.into(), + })?, + query_index, + }, - ComputeCommand::BeginPipelineStatisticsQuery { - query_set_id, - query_index, - } => ArcComputeCommand::BeginPipelineStatisticsQuery { - query_set: query_set_guard.strict_get(query_set_id).get().map_err( - |e| ComputePassError { - scope: PassErrorScope::BeginPipelineStatisticsQuery, - inner: e.into(), - }, - )?, - query_index, - }, + ComputeCommand::BeginPipelineStatisticsQuery { + query_set_id, + query_index, + } => ArcComputeCommand::BeginPipelineStatisticsQuery { + query_set: query_set_guard + .strict_get(query_set_id) + .get() + .map_err(|e| ComputePassError { + scope: PassErrorScope::BeginPipelineStatisticsQuery, + inner: e.into(), + })?, + query_index, + }, - ComputeCommand::EndPipelineStatisticsQuery => { - ArcComputeCommand::EndPipelineStatisticsQuery - } - }) + ComputeCommand::EndPipelineStatisticsQuery => { + ArcComputeCommand::EndPipelineStatisticsQuery + } }) - .collect::, ComputePassError>>()?; + }) + .collect::, ComputePassError>>()?; Ok(resolved_commands) } } diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index 7d773568f..6b70f5f8e 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -68,8 +68,6 @@ pub enum DrawError { #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum RenderCommandError { - #[error("BindGroupId {0:?} is invalid")] - InvalidBindGroupId(id::BindGroupId), #[error("Render bundle {0:?} is invalid")] InvalidRenderBundle(id::RenderBundleId), #[error("Bind group index {index} is greater than the device's requested `max_bind_group` limit {max}")] diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index f5e092697..6dea322f7 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -632,8 +632,6 @@ pub enum RenderPassErrorInner { SurfaceTextureDropped, #[error("Not enough memory left for render pass")] OutOfMemory, - #[error("BindGroupId {0:?} is invalid")] - InvalidBindGroupId(id::BindGroupId), #[error("Unable to clear non-present/read-only depth")] InvalidDepthOps, #[error("Unable to clear non-present/read-only stencil")] @@ -2816,8 +2814,8 @@ impl Global { let hub = &self.hub; let bg = hub .bind_groups - .get(bind_group_id) - .map_err(|_| RenderPassErrorInner::InvalidBindGroupId(bind_group_id)) + .strict_get(bind_group_id) + .get() .map_pass_err(scope)?; bind_group = Some(bg); } diff --git a/wgpu-core/src/command/render_command.rs b/wgpu-core/src/command/render_command.rs index 288aab9b6..447debd65 100644 --- a/wgpu-core/src/command/render_command.rs +++ b/wgpu-core/src/command/render_command.rs @@ -129,9 +129,7 @@ impl RenderCommand { hub: &crate::hub::Hub, commands: &[RenderCommand], ) -> Result, super::RenderPassError> { - use super::{ - DrawKind, PassErrorScope, RenderCommandError, RenderPassError, RenderPassErrorInner, - }; + use super::{DrawKind, PassErrorScope, RenderCommandError, RenderPassError}; let buffers_guard = hub.buffers.read(); let bind_group_guard = hub.bind_groups.read(); @@ -157,12 +155,13 @@ impl RenderCommand { } let bind_group_id = bind_group_id.unwrap(); - let bg = bind_group_guard.get_owned(bind_group_id).map_err(|_| { - RenderPassError { + let bg = bind_group_guard + .strict_get(bind_group_id) + .get() + .map_err(|e| RenderPassError { scope: PassErrorScope::SetBindGroup, - inner: RenderPassErrorInner::InvalidBindGroupId(bind_group_id), - } - })?; + inner: e.into(), + })?; ArcRenderCommand::SetBindGroup { index, diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index f952b5164..878e98908 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -825,14 +825,14 @@ impl Global { Err(e) => break 'error e, }; - let id = fid.assign(bind_group); + let id = fid.assign(Fallible::Valid(bind_group)); api_log!("Device::create_bind_group -> {id:?}"); return (id, None); }; - let id = fid.assign_error(); + let id = fid.assign(Fallible::Invalid(Arc::new(desc.label.to_string()))); (id, Some(error)) } @@ -842,8 +842,10 @@ impl Global { let hub = &self.hub; - if let Some(_bind_group) = hub.bind_groups.unregister(bind_group_id) { - #[cfg(feature = "trace")] + let _bind_group = hub.bind_groups.strict_unregister(bind_group_id); + + #[cfg(feature = "trace")] + if let Ok(_bind_group) = _bind_group.get() { if let Some(t) = _bind_group.device.trace.lock().as_mut() { t.add(trace::Action::DestroyBindGroup(bind_group_id)); } diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index b3afac80c..1dfe285d5 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -168,7 +168,7 @@ pub struct Hub { pub(crate) pipeline_layouts: Registry>, pub(crate) shader_modules: Registry>, pub(crate) bind_group_layouts: Registry>, - pub(crate) bind_groups: Registry>, + pub(crate) bind_groups: Registry>, pub(crate) command_buffers: Registry>, pub(crate) render_bundles: Registry>, pub(crate) render_pipelines: Registry>,