diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index b9248cace..2a0c5354d 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -526,12 +526,19 @@ impl RenderBundleEncoder { size, } => { let scope = PassErrorScope::SetIndexBuffer(buffer_id); - let buffer = state + + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) + .map_pass_err(scope)?; + + state .trackers .buffers .write() - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDEX) + .merge_single(buffer, hal::BufferUses::INDEX) .map_pass_err(scope)?; + self.check_valid_to_use(buffer.device.info.id()) .map_pass_err(scope)?; buffer.check_usage(wgt::BufferUsages::INDEX).map_pass_err(scope)?; @@ -564,12 +571,17 @@ impl RenderBundleEncoder { .map_pass_err(scope); } - let buffer = state - .trackers - .buffers - .write() - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::VERTEX) + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) .map_pass_err(scope)?; + + state + .trackers + .buffers.write() + .merge_single(buffer, hal::BufferUses::VERTEX) + .map_pass_err(scope)?; + self.check_valid_to_use(buffer.device.info.id()) .map_pass_err(scope)?; buffer.check_usage(wgt::BufferUsages::VERTEX).map_pass_err(scope)?; @@ -690,12 +702,17 @@ impl RenderBundleEncoder { let pipeline = state.pipeline(scope)?; let used_bind_groups = pipeline.used_bind_groups; - let buffer = state - .trackers - .buffers - .write() - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT) + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) .map_pass_err(scope)?; + + state + .trackers + .buffers.write() + .merge_single(buffer, hal::BufferUses::INDIRECT) + .map_pass_err(scope)?; + self.check_valid_to_use(buffer.device.info.id()) .map_pass_err(scope)?; buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?; @@ -728,12 +745,17 @@ impl RenderBundleEncoder { let pipeline = state.pipeline(scope)?; let used_bind_groups = pipeline.used_bind_groups; - let buffer = state - .trackers - .buffers - .write() - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT) + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) .map_pass_err(scope)?; + + state + .trackers + .buffers.write() + .merge_single(buffer, hal::BufferUses::INDIRECT) + .map_pass_err(scope)?; + self.check_valid_to_use(buffer.device.info.id()) .map_pass_err(scope)?; buffer.check_usage(wgt::BufferUsages::INDIRECT).map_pass_err(scope)?; diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index d8057916b..c6c11fc35 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -70,6 +70,8 @@ pub enum DrawError { #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum RenderCommandError { + #[error("BufferId {0:?} is invalid")] + InvalidBufferId(id::BufferId), #[error("BindGroupId {0:?} is invalid")] InvalidBindGroupId(id::BindGroupId), #[error("Render bundle {0:?} is invalid")] @@ -119,12 +121,6 @@ impl crate::error::PrettyError for RenderCommandError { Self::InvalidPipeline(id) => { fmt.render_pipeline_label(&id); } - Self::UsageConflict(UsageConflict::TextureInvalid { id }) => { - fmt.texture_label(&id); - } - Self::UsageConflict(UsageConflict::BufferInvalid { id }) => { - fmt.buffer_label(&id); - } _ => {} }; } diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index fa2c1ffb7..24bcced40 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1662,10 +1662,15 @@ impl Global { api_log!("RenderPass::set_index_buffer {buffer_id:?}"); let scope = PassErrorScope::SetIndexBuffer(buffer_id); - let buffer = info - .usage_scope + + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) + .map_pass_err(scope)?; + + info.usage_scope .buffers - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDEX) + .merge_single(buffer, hal::BufferUses::INDEX) .map_pass_err(scope)?; buffer @@ -1712,10 +1717,15 @@ impl Global { api_log!("RenderPass::set_vertex_buffer {slot} {buffer_id:?}"); let scope = PassErrorScope::SetVertexBuffer(buffer_id); - let buffer = info - .usage_scope + + let buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) + .map_pass_err(scope)?; + + info.usage_scope .buffers - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::VERTEX) + .merge_single(buffer, hal::BufferUses::VERTEX) .map_pass_err(scope)?; buffer @@ -2020,11 +2030,16 @@ impl Global { .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION) .map_pass_err(scope)?; - let indirect_buffer = info - .usage_scope - .buffers - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT) + let indirect_buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) .map_pass_err(scope)?; + + info.usage_scope + .buffers + .merge_single(indirect_buffer, hal::BufferUses::INDIRECT) + .map_pass_err(scope)?; + indirect_buffer .check_usage(BufferUsages::INDIRECT) .map_pass_err(scope)?; @@ -2090,26 +2105,32 @@ impl Global { .require_downlevel_flags(wgt::DownlevelFlags::INDIRECT_EXECUTION) .map_pass_err(scope)?; - let indirect_buffer = info - .usage_scope - .buffers - .merge_single(&*buffer_guard, buffer_id, hal::BufferUses::INDIRECT) + let indirect_buffer = buffer_guard + .get(buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(buffer_id)) .map_pass_err(scope)?; + + info.usage_scope + .buffers + .merge_single(indirect_buffer, hal::BufferUses::INDIRECT) + .map_pass_err(scope)?; + indirect_buffer .check_usage(BufferUsages::INDIRECT) .map_pass_err(scope)?; let indirect_raw = indirect_buffer.try_raw(&snatch_guard).map_pass_err(scope)?; - let count_buffer = info - .usage_scope - .buffers - .merge_single( - &*buffer_guard, - count_buffer_id, - hal::BufferUses::INDIRECT, - ) + let count_buffer = buffer_guard + .get(count_buffer_id) + .map_err(|_| RenderCommandError::InvalidBufferId(count_buffer_id)) .map_pass_err(scope)?; + + info.usage_scope + .buffers + .merge_single(count_buffer, hal::BufferUses::INDIRECT) + .map_pass_err(scope)?; + count_buffer .check_usage(BufferUsages::INDIRECT) .map_pass_err(scope)?; diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index 47c89ed61..ae16e151d 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -835,8 +835,8 @@ impl LifetimeTracker { for buffer in self.mapped.drain(..) { let submit_index = buffer.info.submission_index(); log::trace!( - "Mapping of {:?} at submission {:?} gets assigned to active {:?}", - buffer.info.id(), + "Mapping of {} at submission {:?} gets assigned to active {:?}", + buffer.error_ident(), submit_index, self.active.iter().position(|a| a.index == submit_index) ); diff --git a/wgpu-core/src/track/buffer.rs b/wgpu-core/src/track/buffer.rs index d11cc2c3b..8813c23a0 100644 --- a/wgpu-core/src/track/buffer.rs +++ b/wgpu-core/src/track/buffer.rs @@ -10,12 +10,10 @@ use std::{borrow::Cow, marker::PhantomData, sync::Arc}; use super::{PendingTransition, ResourceTracker, TrackerIndex}; use crate::{ hal_api::HalApi, - id::BufferId, lock::{rank, Mutex}, resource::{Buffer, Resource}, resource_log, snatch::SnatchGuard, - storage::Storage, track::{ invalid_resource_state, skip_barrier, ResourceMetadata, ResourceMetadataProvider, ResourceUses, UsageConflict, @@ -227,18 +225,12 @@ impl BufferUsageScope { /// /// If the ID is higher than the length of internal vectors, /// the vectors will be extended. A call to set_size is not needed. - pub fn merge_single<'a>( + pub fn merge_single( &mut self, - storage: &'a Storage>, - id: BufferId, + buffer: &Arc>, new_state: BufferUses, - ) -> Result<&'a Arc>, UsageConflict> { - let buffer = storage - .get(id) - .map_err(|_| UsageConflict::BufferInvalid { id })?; - + ) -> Result<(), UsageConflict> { self.insert_merge_single(buffer.clone(), new_state) - .map(|_| buffer) } /// Merge a single state into the UsageScope, using an already resolved buffer. diff --git a/wgpu-core/src/track/mod.rs b/wgpu-core/src/track/mod.rs index 391b91367..6e501a285 100644 --- a/wgpu-core/src/track/mod.rs +++ b/wgpu-core/src/track/mod.rs @@ -340,10 +340,6 @@ fn skip_barrier(old_state: T, new_state: T) -> bool { #[derive(Clone, Debug, Error, Eq, PartialEq)] pub enum UsageConflict { - #[error("Attempted to use invalid buffer")] - BufferInvalid { id: id::BufferId }, - #[error("Attempted to use invalid texture")] - TextureInvalid { id: id::TextureId }, #[error("Attempted to use buffer with {invalid_use}.")] Buffer { id: id::BufferId, @@ -395,12 +391,6 @@ impl crate::error::PrettyError for UsageConflict { fn fmt_pretty(&self, fmt: &mut crate::error::ErrorFormatter) { fmt.error(self); match *self { - Self::BufferInvalid { id } => { - fmt.buffer_label(&id); - } - Self::TextureInvalid { id } => { - fmt.texture_label(&id); - } Self::Buffer { id, .. } => { fmt.buffer_label(&id); }