diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index cbcb5979a0..6f9124656b 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -138,8 +138,6 @@ pub enum ComputePassErrorInner { BindGroupIndexOutOfRange { index: u32, max: u32 }, #[error("ComputePipelineId {0:?} is invalid")] InvalidPipelineId(id::ComputePipelineId), - #[error("QuerySet {0:?} is invalid")] - InvalidQuerySet(id::QuerySetId), #[error(transparent)] DestroyedResource(#[from] DestroyedResourceError), #[error("Indirect buffer uses bytes {offset}..{end_offset} which overruns indirect buffer of size {buffer_size}")] @@ -309,11 +307,9 @@ impl Global { }; arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes { - let Ok(query_set) = hub.query_sets.get(tw.query_set) else { - return make_err( - CommandEncoderError::InvalidTimestampWritesQuerySetId(tw.query_set), - arc_desc, - ); + let query_set = match hub.query_sets.strict_get(tw.query_set).get() { + Ok(query_set) => query_set, + Err(e) => return make_err(e.into(), arc_desc), }; Some(ArcPassTimestampWrites { @@ -389,8 +385,8 @@ impl Global { Some(ArcPassTimestampWrites { query_set: hub .query_sets - .get(tw.query_set) - .map_err(|_| ComputePassErrorInner::InvalidQuerySet(tw.query_set)) + .strict_get(tw.query_set) + .get() .map_pass_err(scope)?, beginning_of_pass_write_index: tw.beginning_of_pass_write_index, end_of_pass_write_index: tw.end_of_pass_write_index, @@ -1167,8 +1163,8 @@ impl Global { let hub = &self.hub; let query_set = hub .query_sets - .get(query_set_id) - .map_err(|_| ComputePassErrorInner::InvalidQuerySet(query_set_id)) + .strict_get(query_set_id) + .get() .map_pass_err(scope)?; base.commands.push(ArcComputeCommand::WriteTimestamp { @@ -1191,8 +1187,8 @@ impl Global { let hub = &self.hub; let query_set = hub .query_sets - .get(query_set_id) - .map_err(|_| ComputePassErrorInner::InvalidQuerySet(query_set_id)) + .strict_get(query_set_id) + .get() .map_pass_err(scope)?; base.commands diff --git a/wgpu-core/src/command/compute_command.rs b/wgpu-core/src/command/compute_command.rs index 7003989826..c2f7facbb6 100644 --- a/wgpu-core/src/command/compute_command.rs +++ b/wgpu-core/src/command/compute_command.rs @@ -81,113 +81,113 @@ 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: Some(bg), - } - } + bind_group_id, + } => { + if bind_group_id.is_none() { + return Ok(ArcComputeCommand::SetBindGroup { + index, + num_dynamic_offsets, + bind_group: None, + }); + } - 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| { + let bind_group_id = bind_group_id.unwrap(); + let bg = bind_group_guard.get_owned(bind_group_id).map_err(|_| { ComputePassError { - scope: PassErrorScope::Dispatch { indirect: true }, - inner: e.into(), + scope: PassErrorScope::SetBindGroup, + inner: ComputePassErrorInner::InvalidBindGroupId(bind_group_id), + } + })?; + + 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::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.get_owned(query_set_id).map_err(|_| { - ComputePassError { - scope: PassErrorScope::WriteTimestamp, - inner: ComputePassErrorInner::InvalidQuerySet(query_set_id), - } - })?, - 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.get_owned(query_set_id).map_err(|_| { - ComputePassError { - scope: PassErrorScope::BeginPipelineStatisticsQuery, - inner: ComputePassErrorInner::InvalidQuerySet(query_set_id), - } - })?, - 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 dc343f0af9..7d773568f5 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -80,8 +80,6 @@ pub enum RenderCommandError { UnalignedBufferOffset(u64, &'static str, u32), #[error("RenderPipelineId {0:?} is invalid")] InvalidPipelineId(id::RenderPipelineId), - #[error("QuerySet {0:?} is invalid")] - InvalidQuerySet(id::QuerySetId), #[error("Render pipeline targets are incompatible with render pass")] IncompatiblePipelineTargets(#[from] crate::device::RenderPassCompatibilityError), #[error("{0} writes to depth, while the pass has read-only depth access")] diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 296a8725a0..00c923103a 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -597,12 +597,8 @@ pub enum CommandEncoderError { #[error("Command encoder is locked by a previously created render/compute pass. Before recording any new commands, the pass must be ended.")] Locked, - #[error("QuerySet {0:?} for pass timestamp writes is invalid.")] - InvalidTimestampWritesQuerySetId(id::QuerySetId), #[error(transparent)] InvalidColorAttachment(#[from] ColorAttachmentError), - #[error("Occlusion QuerySetId {0:?} is invalid")] - InvalidOcclusionQuerySetId(id::QuerySetId), #[error(transparent)] InvalidResource(#[from] InvalidResourceError), } diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs index 886201e27f..fd8eb8c32b 100644 --- a/wgpu-core/src/command/query.rs +++ b/wgpu-core/src/command/query.rs @@ -103,8 +103,6 @@ pub enum QueryError { Resolve(#[from] ResolveError), #[error(transparent)] DestroyedResource(#[from] DestroyedResourceError), - #[error("QuerySetId {0:?} is invalid or destroyed")] - InvalidQuerySetId(id::QuerySetId), #[error(transparent)] InvalidResource(#[from] InvalidResourceError), } @@ -349,10 +347,7 @@ impl Global { let raw_encoder = encoder.open(&cmd_buf.device)?; - let query_set = hub - .query_sets - .get(query_set_id) - .map_err(|_| QueryError::InvalidQuerySetId(query_set_id))?; + let query_set = hub.query_sets.strict_get(query_set_id).get()?; let query_set = tracker.query_sets.insert_single(query_set); @@ -404,10 +399,7 @@ impl Global { return Err(QueryError::Resolve(ResolveError::BufferOffsetAlignment)); } - let query_set = hub - .query_sets - .get(query_set_id) - .map_err(|_| QueryError::InvalidQuerySetId(query_set_id))?; + let query_set = hub.query_sets.strict_get(query_set_id).get()?; let query_set = tracker.query_sets.insert_single(query_set); diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index d341cef12d..f5e0926978 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -585,8 +585,6 @@ pub enum RenderPassErrorInner { InvalidDepthStencilAttachmentFormat(wgt::TextureFormat), #[error("Render pipeline {0:?} is invalid")] InvalidPipeline(id::RenderPipelineId), - #[error("QuerySet {0:?} is invalid")] - InvalidQuerySet(id::QuerySetId), #[error("Render bundle {0:?} is invalid")] InvalidRenderBundle(id::RenderBundleId), #[error("The format of the {location} ({format:?}) is not resolvable")] @@ -1401,9 +1399,7 @@ impl Global { }; arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes { - let query_set = query_sets.get_owned(tw.query_set).map_err(|_| { - CommandEncoderError::InvalidTimestampWritesQuerySetId(tw.query_set) - })?; + let query_set = query_sets.strict_get(tw.query_set).get()?; Some(ArcPassTimestampWrites { query_set, @@ -1416,9 +1412,7 @@ impl Global { arc_desc.occlusion_query_set = if let Some(occlusion_query_set) = desc.occlusion_query_set { - let query_set = query_sets.get_owned(occlusion_query_set).map_err(|_| { - CommandEncoderError::InvalidOcclusionQuerySetId(occlusion_query_set) - })?; + let query_set = query_sets.strict_get(occlusion_query_set).get()?; Some(query_set) } else { @@ -2784,8 +2778,8 @@ impl Global { let hub = &self.hub; let query_set = hub .query_sets - .get(query_set_id) - .map_err(|_| RenderPassErrorInner::InvalidQuerySet(query_set_id)) + .strict_get(query_set_id) + .get() .map_pass_err(scope)?; Ok(query_set) diff --git a/wgpu-core/src/command/render_command.rs b/wgpu-core/src/command/render_command.rs index cbbae1a08d..288aab9b68 100644 --- a/wgpu-core/src/command/render_command.rs +++ b/wgpu-core/src/command/render_command.rs @@ -206,12 +206,13 @@ impl RenderCommand { query_set_id, query_index, } => ArcRenderCommand::WriteTimestamp { - query_set: query_set_guard.get_owned(query_set_id).map_err(|_| { - RenderPassError { + query_set: query_set_guard + .strict_get(query_set_id) + .get() + .map_err(|e| RenderPassError { scope: PassErrorScope::WriteTimestamp, - inner: RenderPassErrorInner::InvalidQuerySet(query_set_id), - } - })?, + inner: e.into(), + })?, query_index, }, @@ -219,12 +220,13 @@ impl RenderCommand { query_set_id, query_index, } => ArcRenderCommand::BeginPipelineStatisticsQuery { - query_set: query_set_guard.get_owned(query_set_id).map_err(|_| { - RenderPassError { + query_set: query_set_guard + .strict_get(query_set_id) + .get() + .map_err(|e| RenderPassError { scope: PassErrorScope::BeginPipelineStatisticsQuery, - inner: RenderPassErrorInner::InvalidQuerySet(query_set_id), - } - })?, + inner: e.into(), + })?, query_index, }, diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 5e49b24c3a..f952b51647 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1151,13 +1151,13 @@ impl Global { Err(err) => break 'error err, }; - let id = fid.assign(query_set); + let id = fid.assign(Fallible::Valid(query_set)); api_log!("Device::create_query_set -> {id:?}"); return (id, None); }; - let id = fid.assign_error(); + let id = fid.assign(Fallible::Invalid(Arc::new(desc.label.to_string()))); (id, Some(error)) } @@ -1167,9 +1167,11 @@ impl Global { let hub = &self.hub; - if let Some(_query_set) = hub.query_sets.unregister(query_set_id) { - #[cfg(feature = "trace")] - if let Some(trace) = _query_set.device.trace.lock().as_mut() { + let _query_set = hub.query_sets.strict_unregister(query_set_id); + + #[cfg(feature = "trace")] + if let Ok(query_set) = _query_set.get() { + if let Some(trace) = query_set.device.trace.lock().as_mut() { trace.add(trace::Action::DestroyQuerySet(query_set_id)); } } diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 6fc3bb0882..d0b333f024 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -174,7 +174,7 @@ pub struct Hub { pub(crate) render_pipelines: Registry>, pub(crate) compute_pipelines: Registry>, pub(crate) pipeline_caches: Registry>, - pub(crate) query_sets: Registry>, + pub(crate) query_sets: Registry>, pub(crate) buffers: Registry>, pub(crate) staging_buffers: Registry>, pub(crate) textures: Registry>,