diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 58eaa122d..454ab221d 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -38,7 +38,10 @@ #![allow(clippy::reversed_empty_ranges)] use crate::{ - command::{BasePass, DrawError, RenderCommand, RenderCommandError, StateChange}, + command::{ + BasePass, DrawError, MapPassErr, PassErrorScope, RenderCommand, RenderCommandError, + StateChange, + }, conv, device::{ AttachmentData, DeviceError, RenderPassContext, MAX_VERTEX_BUFFERS, SHADER_STAGE_COUNT, @@ -656,17 +659,47 @@ impl State { /// Error encountered when finishing recording a render bundle. #[derive(Clone, Debug, Error)] -pub enum RenderBundleError { +pub(super) enum RenderBundleErrorInner { #[error(transparent)] Device(#[from] DeviceError), #[error(transparent)] - RenderCommand(#[from] RenderCommandError), + RenderCommand(RenderCommandError), #[error(transparent)] ResourceUsageConflict(#[from] UsageConflict), #[error(transparent)] Draw(#[from] DrawError), } +impl From for RenderBundleErrorInner +where + T: Into, +{ + fn from(t: T) -> Self { + Self::RenderCommand(t.into()) + } +} + +/// Error encountered when finishing recording a render bundle. +#[derive(Clone, Debug, Error)] +#[error("{scope}")] +pub struct RenderBundleError { + scope: PassErrorScope, + #[source] + inner: RenderBundleErrorInner, +} + +impl MapPassErr for Result +where + E: Into, +{ + fn map_pass_err(self, scope: PassErrorScope) -> Result { + self.map_err(|inner| RenderBundleError { + scope, + inner: inner.into(), + }) + } +} + impl Global { pub fn render_bundle_encoder_finish( &self, @@ -675,13 +708,16 @@ impl Global { id_in: Input, ) -> Result { span!(_guard, INFO, "RenderBundleEncoder::finish"); + let scope = PassErrorScope::Bundle; + let hub = B::hub(self); let mut token = Token::root(); let (device_guard, mut token) = hub.devices.read(&mut token); let device = device_guard .get(bundle_encoder.parent_id) - .map_err(|_| DeviceError::Invalid)?; + .map_err(|_| DeviceError::Invalid) + .map_pass_err(scope)?; let render_bundle = { let (pipeline_layout_guard, mut token) = hub.pipeline_layouts.read(&mut token); let (bind_group_guard, mut token) = hub.bind_groups.read(&mut token); @@ -712,12 +748,15 @@ impl Global { num_dynamic_offsets, bind_group_id, } => { + let scope = PassErrorScope::SetBindGroup; + let max_bind_groups = device.limits.max_bind_groups; if (index as u32) >= max_bind_groups { - Err(RenderCommandError::BindGroupIndexOutOfRange { + return Err(RenderCommandError::BindGroupIndexOutOfRange { index, max: max_bind_groups, - })? + }) + .map_pass_err(scope); } let offsets = &base.dynamic_offsets[..num_dynamic_offsets as usize]; @@ -729,25 +768,32 @@ impl Global { .map(|offset| *offset as wgt::BufferAddress) .find(|offset| offset % wgt::BIND_BUFFER_ALIGNMENT != 0) { - Err(RenderCommandError::UnalignedBufferOffset(offset))? + return Err(RenderCommandError::UnalignedBufferOffset(offset)) + .map_pass_err(scope); } let bind_group = state .trackers .bind_groups .use_extend(&*bind_group_guard, bind_group_id, (), ()) - .map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id))?; + .map_err(|_| RenderCommandError::InvalidBindGroup(bind_group_id)) + .map_pass_err(scope)?; if bind_group.dynamic_binding_info.len() != offsets.len() { - Err(RenderCommandError::InvalidDynamicOffsetCount { + return Err(RenderCommandError::InvalidDynamicOffsetCount { actual: offsets.len(), expected: bind_group.dynamic_binding_info.len(), - })? + }) + .map_pass_err(scope); } state.set_bind_group(index, bind_group_id, bind_group.layout_id, offsets); - state.trackers.merge_extend(&bind_group.used)?; + state + .trackers + .merge_extend(&bind_group.used) + .map_pass_err(scope)?; } RenderCommand::SetPipeline(pipeline_id) => { + let scope = PassErrorScope::SetPipeline; if state.pipeline.set_and_check_redundant(pipeline_id) { continue; } @@ -761,7 +807,8 @@ impl Global { bundle_encoder .context .check_compatible(&pipeline.pass_context) - .map_err(RenderCommandError::IncompatiblePipeline)?; + .map_err(RenderCommandError::IncompatiblePipeline) + .map_pass_err(scope)?; //TODO: check read-only depth @@ -784,13 +831,14 @@ impl Global { offset, size, } => { + let scope = PassErrorScope::SetIndexBuffer; let buffer = state .trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDEX) .unwrap(); check_buffer_usage(buffer.usage, wgt::BufferUsage::INDEX) - .map_err(RenderCommandError::from)?; + .map_pass_err(scope)?; let end = match size { Some(s) => offset + s.get(), @@ -804,13 +852,14 @@ impl Global { offset, size, } => { + let scope = PassErrorScope::SetVertexBuffer; let buffer = state .trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::VERTEX) .unwrap(); check_buffer_usage(buffer.usage, wgt::BufferUsage::VERTEX) - .map_err(RenderCommandError::from)?; + .map_pass_err(scope)?; let end = match size { Some(s) => offset + s.get(), @@ -824,15 +873,17 @@ impl Global { size_bytes, values_offset: _, } => { + let scope = PassErrorScope::SetPushConstant; let end_offset = offset + size_bytes; - let pipeline_layout_id = - pipeline_layout_id.ok_or(DrawError::MissingPipeline)?; + let pipeline_layout_id = pipeline_layout_id + .ok_or(DrawError::MissingPipeline) + .map_pass_err(scope)?; let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id]; pipeline_layout .validate_push_constant_ranges(stages, offset, end_offset) - .map_err(RenderCommandError::from)?; + .map_pass_err(scope)?; commands.push(command); } @@ -842,20 +893,23 @@ impl Global { first_vertex, first_instance, } => { + let scope = PassErrorScope::Draw; let (vertex_limit, instance_limit) = state.vertex_limits(); let last_vertex = first_vertex + vertex_count; if last_vertex > vertex_limit { - Err(DrawError::VertexBeyondLimit { + return Err(DrawError::VertexBeyondLimit { last_vertex, vertex_limit, - })? + }) + .map_pass_err(scope); } let last_instance = first_instance + instance_count; if last_instance > instance_limit { - Err(DrawError::InstanceBeyondLimit { + return Err(DrawError::InstanceBeyondLimit { last_instance, instance_limit, - })? + }) + .map_pass_err(scope); } commands.extend(state.flush_vertices()); commands.extend(state.flush_binds()); @@ -868,22 +922,25 @@ impl Global { base_vertex: _, first_instance, } => { + let scope = PassErrorScope::DrawIndexed; //TODO: validate that base_vertex + max_index() is within the provided range let (_, instance_limit) = state.vertex_limits(); let index_limit = state.index.limit(); let last_index = first_index + index_count; if last_index > index_limit { - Err(DrawError::IndexBeyondLimit { + return Err(DrawError::IndexBeyondLimit { last_index, index_limit, - })? + }) + .map_pass_err(scope); } let last_instance = first_instance + instance_count; if last_instance > instance_limit { - Err(DrawError::InstanceBeyondLimit { + return Err(DrawError::InstanceBeyondLimit { last_instance, instance_limit, - })? + }) + .map_pass_err(scope); } commands.extend(state.index.flush()); commands.extend(state.flush_vertices()); @@ -896,13 +953,14 @@ impl Global { count: None, indexed: false, } => { + let scope = PassErrorScope::DrawIndirect; let buffer = state .trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT) .unwrap(); check_buffer_usage(buffer.usage, wgt::BufferUsage::INDIRECT) - .map_err(RenderCommandError::from)?; + .map_pass_err(scope)?; commands.extend(state.flush_vertices()); commands.extend(state.flush_binds()); @@ -914,13 +972,15 @@ impl Global { count: None, indexed: true, } => { + let scope = PassErrorScope::DrawIndexedIndirect; let buffer = state .trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT) - .map_err(|err| RenderCommandError::Buffer(buffer_id, err))?; + .map_err(|err| RenderCommandError::Buffer(buffer_id, err)) + .map_pass_err(scope)?; check_buffer_usage(buffer.usage, wgt::BufferUsage::INDIRECT) - .map_err(RenderCommandError::from)?; + .map_pass_err(scope)?; commands.extend(state.index.flush()); commands.extend(state.flush_vertices()); diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 792497a18..56ea40b21 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -6,7 +6,8 @@ use crate::{ binding_model::{BindError, BindGroup, PushConstantUploadError}, command::{ bind::{Binder, LayoutChange}, - BasePass, BasePassRef, CommandBuffer, CommandEncoderError, StateChange, + BasePass, BasePassRef, CommandBuffer, CommandEncoderError, MapPassErr, PassErrorScope, + StateChange, }, hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Storage, Token}, id, @@ -116,8 +117,9 @@ pub enum DispatchError { }, } +/// Error encountered when performing a compute pass. #[derive(Clone, Debug, Error)] -pub enum ComputePassError { +pub(super) enum ComputePassErrorInner { #[error(transparent)] Encoder(#[from] CommandEncoderError), #[error("bind group {0:?} is invalid")] @@ -142,6 +144,27 @@ pub enum ComputePassError { PushConstants(#[from] PushConstantUploadError), } +/// Error encountered when performing a compute pass. +#[derive(Clone, Debug, Error)] +#[error("{scope}")] +pub struct ComputePassError { + scope: PassErrorScope, + #[source] + inner: ComputePassErrorInner, +} + +impl MapPassErr for Result +where + E: Into, +{ + fn map_pass_err(self, scope: PassErrorScope) -> Result { + self.map_err(|inner| ComputePassError { + scope, + inner: inner.into(), + }) + } +} + #[derive(Debug)] struct State { binder: Binder, @@ -211,11 +234,14 @@ impl Global { mut base: BasePassRef, ) -> Result<(), ComputePassError> { span!(_guard, INFO, "CommandEncoder::run_compute_pass"); + let scope = PassErrorScope::Pass; + let hub = B::hub(self); let mut token = Token::root(); let (mut cmd_buf_guard, mut token) = hub.command_buffers.write(&mut token); - let cmd_buf = CommandBuffer::get_encoder(&mut *cmd_buf_guard, encoder_id)?; + let cmd_buf = + CommandBuffer::get_encoder(&mut *cmd_buf_guard, encoder_id).map_pass_err(scope)?; let raw = cmd_buf.raw.last_mut().unwrap(); #[cfg(feature = "trace")] @@ -247,12 +273,15 @@ impl Global { num_dynamic_offsets, bind_group_id, } => { + let scope = PassErrorScope::SetBindGroup; + let max_bind_groups = cmd_buf.limits.max_bind_groups; if (index as u32) >= max_bind_groups { - return Err(ComputePassError::BindGroupIndexOutOfRange { + return Err(ComputePassErrorInner::BindGroupIndexOutOfRange { index, max: max_bind_groups, - }); + }) + .map_pass_err(scope); } temp_offsets.clear(); @@ -264,8 +293,11 @@ impl Global { .trackers .bind_groups .use_extend(&*bind_group_guard, bind_group_id, (), ()) - .map_err(|_| ComputePassError::InvalidBindGroup(bind_group_id))?; - bind_group.validate_dynamic_bindings(&temp_offsets)?; + .map_err(|_| ComputePassErrorInner::InvalidBindGroup(bind_group_id)) + .map_pass_err(scope)?; + bind_group + .validate_dynamic_bindings(&temp_offsets) + .map_pass_err(scope)?; if let Some((pipeline_layout_id, follow_ups)) = state.binder.provide_entry( index as usize, @@ -292,6 +324,8 @@ impl Global { } } ComputeCommand::SetPipeline(pipeline_id) => { + let scope = PassErrorScope::SetPipeline; + if state.pipeline.set_and_check_redundant(pipeline_id) { continue; } @@ -300,7 +334,8 @@ impl Global { .trackers .compute_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ()) - .map_err(|_| ComputePassError::InvalidPipeline(pipeline_id))?; + .map_err(|_| ComputePassErrorInner::InvalidPipeline(pipeline_id)) + .map_pass_err(scope)?; unsafe { raw.bind_compute_pipeline(&pipeline.raw); @@ -369,6 +404,8 @@ impl Global { size_bytes, values_offset, } => { + let scope = PassErrorScope::SetPushConstant; + let end_offset_bytes = offset + size_bytes; let values_end_offset = (values_offset + size_bytes / wgt::PUSH_CONSTANT_ALIGNMENT) as usize; @@ -379,7 +416,10 @@ impl Global { .binder .pipeline_layout_id //TODO: don't error here, lazily update the push constants - .ok_or(ComputePassError::Dispatch(DispatchError::MissingPipeline))?; + .ok_or(ComputePassErrorInner::Dispatch( + DispatchError::MissingPipeline, + )) + .map_pass_err(scope)?; let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id]; pipeline_layout @@ -388,44 +428,55 @@ impl Global { offset, end_offset_bytes, ) - .map_err(ComputePassError::from)?; + .map_pass_err(scope)?; unsafe { raw.push_compute_constants(&pipeline_layout.raw, offset, data_slice) } } ComputeCommand::Dispatch(groups) => { - state.is_ready()?; - state.flush_states( - raw, - &mut cmd_buf.trackers, - &*bind_group_guard, - &*buffer_guard, - &*texture_guard, - )?; + let scope = PassErrorScope::Dispatch; + + state.is_ready().map_pass_err(scope)?; + state + .flush_states( + raw, + &mut cmd_buf.trackers, + &*bind_group_guard, + &*buffer_guard, + &*texture_guard, + ) + .map_pass_err(scope)?; unsafe { raw.dispatch(groups); } } ComputeCommand::DispatchIndirect { buffer_id, offset } => { - state.is_ready()?; + let scope = PassErrorScope::DispatchIndirect; + + state.is_ready().map_pass_err(scope)?; let indirect_buffer = state .trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT) - .map_err(|_| ComputePassError::InvalidIndirectBuffer(buffer_id))?; - check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT)?; + .map_err(|_| ComputePassErrorInner::InvalidIndirectBuffer(buffer_id)) + .map_pass_err(scope)?; + check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT) + .map_pass_err(scope)?; let &(ref buf_raw, _) = indirect_buffer .raw .as_ref() - .ok_or(ComputePassError::InvalidIndirectBuffer(buffer_id))?; + .ok_or(ComputePassErrorInner::InvalidIndirectBuffer(buffer_id)) + .map_pass_err(scope)?; - state.flush_states( - raw, - &mut cmd_buf.trackers, - &*bind_group_guard, - &*buffer_guard, - &*texture_guard, - )?; + state + .flush_states( + raw, + &mut cmd_buf.trackers, + &*bind_group_guard, + &*buffer_guard, + &*texture_guard, + ) + .map_pass_err(scope)?; unsafe { raw.dispatch_indirect(buf_raw, offset); } @@ -440,8 +491,11 @@ impl Global { base.string_data = &base.string_data[len..]; } ComputeCommand::PopDebugGroup => { + let scope = PassErrorScope::PopDebugGroup; + if state.debug_scope_depth == 0 { - return Err(ComputePassError::InvalidPopDebugGroup); + return Err(ComputePassErrorInner::InvalidPopDebugGroup) + .map_pass_err(scope); } state.debug_scope_depth -= 1; unsafe { diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 4d8322779..f2292ddb3 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -292,3 +292,45 @@ impl StateChange { self.last_state = None; } } + +trait MapPassErr { + fn map_pass_err(self, scope: PassErrorScope) -> Result; +} + +#[derive(Clone, Copy, Debug, Error)] +enum PassErrorScope { + #[error("In a bundle parameter")] + Bundle, + #[error("In a pass parameter")] + Pass, + #[error("In a set_bind_group command")] + SetBindGroup, + #[error("In a set_pipeline command")] + SetPipeline, + #[error("In a set_push_constant command")] + SetPushConstant, + #[error("In a set_vertex_buffer command")] + SetVertexBuffer, + #[error("In a set_index_buffer command")] + SetIndexBuffer, + #[error("In a set_viewport command")] + SetViewport, + #[error("In a set_scissor_rect command")] + SetScissorRect, + #[error("In a draw command")] + Draw, + #[error("In a draw_indexed command")] + DrawIndexed, + #[error("In a draw_indirect command")] + DrawIndirect, + #[error("In a draw_indexed_indirect command")] + DrawIndexedIndirect, + #[error("In a execute_bundle command")] + ExecuteBundle, + #[error("In a dispatch command")] + Dispatch, + #[error("In a dispatch_indirect command")] + DispatchIndirect, + #[error("In a pop_debug_group command")] + PopDebugGroup, +} diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 5cf157b5e..6dbdf97ed 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -7,7 +7,7 @@ use crate::{ command::{ bind::{Binder, LayoutChange}, BasePass, BasePassRef, CommandBuffer, CommandEncoderError, DrawError, ExecutionError, - RenderCommand, RenderCommandError, StateChange, + MapPassErr, PassErrorScope, RenderCommand, RenderCommandError, StateChange, }, conv, device::{ @@ -116,18 +116,18 @@ pub struct DepthStencilAttachmentDescriptor { } impl DepthStencilAttachmentDescriptor { - fn is_read_only(&self, aspects: hal::format::Aspects) -> Result { + fn is_read_only(&self, aspects: hal::format::Aspects) -> Result { if aspects.contains(hal::format::Aspects::DEPTH) && !self.depth.read_only { return Ok(false); } if (self.depth.load_op, self.depth.store_op) != (LoadOp::Load, StoreOp::Store) { - return Err(RenderPassError::InvalidDepthOps); + return Err(RenderPassErrorInner::InvalidDepthOps); } if aspects.contains(hal::format::Aspects::STENCIL) && !self.stencil.read_only { return Ok(false); } if (self.stencil.load_op, self.stencil.store_op) != (LoadOp::Load, StoreOp::Store) { - return Err(RenderPassError::InvalidStencilOps); + return Err(RenderPassErrorInner::InvalidStencilOps); } Ok(true) } @@ -318,7 +318,7 @@ impl State { /// Error encountered when performing a render pass. #[derive(Clone, Debug, Error)] -pub enum RenderPassError { +pub(super) enum RenderPassErrorInner { #[error(transparent)] Encoder(#[from] CommandEncoderError), #[error("attachment texture view {0:?} is invalid")] @@ -380,24 +380,45 @@ pub enum RenderPassError { Bind(#[from] BindError), } -impl From for RenderPassError { +impl From for RenderPassErrorInner { fn from(error: MissingBufferUsageError) -> Self { Self::RenderCommand(error.into()) } } -impl From for RenderPassError { +impl From for RenderPassErrorInner { fn from(error: MissingTextureUsageError) -> Self { Self::RenderCommand(error.into()) } } +/// Error encountered when performing a render pass. +#[derive(Clone, Debug, Error)] +#[error("{scope}")] +pub struct RenderPassError { + scope: PassErrorScope, + #[source] + inner: RenderPassErrorInner, +} + +impl MapPassErr for Result +where + E: Into, +{ + fn map_pass_err(self, scope: PassErrorScope) -> Result { + self.map_err(|inner| RenderPassError { + scope, + inner: inner.into(), + }) + } +} + fn check_device_features( actual: wgt::Features, expected: wgt::Features, -) -> Result<(), RenderPassError> { +) -> Result<(), RenderPassErrorInner> { if !actual.contains(expected) { - Err(RenderPassError::MissingDeviceFeatures(expected)) + Err(RenderPassErrorInner::MissingDeviceFeatures(expected)) } else { Ok(()) } @@ -428,6 +449,7 @@ impl Global { depth_stencil_attachment: Option<&DepthStencilAttachmentDescriptor>, ) -> Result<(), RenderPassError> { span!(_guard, INFO, "CommandEncoder::run_render_pass"); + let scope = PassErrorScope::Pass; let hub = B::hub(self); let mut token = Token::root(); @@ -436,7 +458,8 @@ impl Global { let (mut cmb_guard, mut token) = hub.command_buffers.write(&mut token); let mut trackers = TrackerSet::new(B::VARIANT); - let cmd_buf = CommandBuffer::get_encoder(&mut *cmb_guard, encoder_id)?; + let cmd_buf = + CommandBuffer::get_encoder(&mut *cmb_guard, encoder_id).map_pass_err(scope)?; let device = &device_guard[cmd_buf.device_id.value]; let mut raw = device.cmd_allocator.extend(cmd_buf); @@ -492,7 +515,7 @@ impl Global { let mut add_view = |view: &TextureView| { if let Some(ex) = extent { if ex != view.extent { - return Err(RenderPassError::ExtentStateMismatch { + return Err(RenderPassErrorInner::ExtentStateMismatch { state_extent: ex, view_extent: view.extent, }); @@ -503,7 +526,7 @@ impl Global { if sample_count == 0 { sample_count = view.samples; } else if sample_count != view.samples { - return Err(RenderPassError::SampleCountMismatch { + return Err(RenderPassErrorInner::SampleCountMismatch { actual: view.samples, expected: sample_count, }); @@ -521,14 +544,16 @@ impl Global { let view = trackers .views .use_extend(&*view_guard, at.attachment, (), ()) - .map_err(|_| RenderPassError::InvalidAttachment(at.attachment))?; - add_view(view)?; + .map_err(|_| RenderPassErrorInner::InvalidAttachment(at.attachment)) + .map_pass_err(scope)?; + add_view(view).map_pass_err(scope)?; depth_stencil_aspects = view.aspects; let source_id = match view.inner { TextureViewInner::Native { ref source_id, .. } => source_id, TextureViewInner::SwapChain { .. } => { - return Err(RenderPassError::SwapChainImageAsDepthStencil) + return Err(RenderPassErrorInner::SwapChainImageAsDepthStencil) + .map_pass_err(scope) } }; @@ -536,7 +561,7 @@ impl Global { let previous_use = base_trackers .textures .query(source_id.value, view.selector.clone()); - let new_use = if at.is_read_only(view.aspects)? { + let new_use = if at.is_read_only(view.aspects).map_pass_err(scope)? { is_ds_read_only = true; TextureUse::ATTACHMENT_READ } else { @@ -577,8 +602,9 @@ impl Global { let view = trackers .views .use_extend(&*view_guard, at.attachment, (), ()) - .map_err(|_| RenderPassError::InvalidAttachment(at.attachment))?; - add_view(view)?; + .map_err(|_| RenderPassErrorInner::InvalidAttachment(at.attachment)) + .map_pass_err(scope)?; + add_view(view).map_pass_err(scope)?; valid_attachment &= *attachment_width.get_or_insert(view.extent.width) == view.extent.width @@ -611,7 +637,8 @@ impl Global { TextureViewInner::SwapChain { ref source_id, .. } => { if let Some((ref sc_id, _)) = cmd_buf.used_swap_chain { if source_id.value != sc_id.value { - return Err(RenderPassError::SwapChainMismatch); + return Err(RenderPassErrorInner::SwapChainMismatch) + .map_pass_err(scope); } } else { assert!(used_swap_chain.is_none()); @@ -641,25 +668,29 @@ impl Global { } if !valid_attachment { - Err(RenderPassError::MismatchAttachments)? + return Err(RenderPassErrorInner::MismatchAttachments).map_pass_err(scope); } for resolve_target in color_attachments.iter().flat_map(|at| at.resolve_target) { let view = trackers .views .use_extend(&*view_guard, resolve_target, (), ()) - .map_err(|_| RenderPassError::InvalidAttachment(resolve_target))?; + .map_err(|_| RenderPassErrorInner::InvalidAttachment(resolve_target)) + .map_pass_err(scope)?; if extent != Some(view.extent) { - return Err(RenderPassError::ExtentStateMismatch { + return Err(RenderPassErrorInner::ExtentStateMismatch { state_extent: extent.unwrap_or_default(), view_extent: view.extent, - }); + }) + .map_pass_err(scope); } if view.samples != 1 { - return Err(RenderPassError::InvalidResolveTargetSampleCount); + return Err(RenderPassErrorInner::InvalidResolveTargetSampleCount) + .map_pass_err(scope); } if sample_count == 1 { - return Err(RenderPassError::InvalidResolveSourceSampleCount); + return Err(RenderPassErrorInner::InvalidResolveSourceSampleCount) + .map_pass_err(scope); } let layouts = match view.inner { @@ -688,7 +719,8 @@ impl Global { TextureViewInner::SwapChain { ref source_id, .. } => { if let Some((ref sc_id, _)) = cmd_buf.used_swap_chain { if source_id.value != sc_id.value { - return Err(RenderPassError::SwapChainMismatch); + return Err(RenderPassErrorInner::SwapChainMismatch) + .map_pass_err(scope); } } else { assert!(used_swap_chain.is_none()); @@ -722,7 +754,8 @@ impl Global { }; if sample_count & sample_count_limit == 0 { - return Err(RenderPassError::InvalidSampleCount(sample_count)); + return Err(RenderPassErrorInner::InvalidSampleCount(sample_count)) + .map_pass_err(scope); } let mut render_pass_cache = device.render_passes.lock(); @@ -833,7 +866,8 @@ impl Global { device .raw .create_framebuffer(&render_pass, attachments, extent.unwrap()) - .or(Err(RenderPassError::OutOfMemory))? + .or(Err(RenderPassErrorInner::OutOfMemory)) + .map_pass_err(scope)? }; cmd_buf.used_swap_chain = Some((sc_id, framebuffer)); &mut cmd_buf.used_swap_chain.as_mut().unwrap().1 @@ -863,7 +897,8 @@ impl Global { attachments, extent.unwrap(), ) - .or(Err(RenderPassError::OutOfMemory))? + .or(Err(RenderPassErrorInner::OutOfMemory)) + .map_pass_err(scope)? } }; e.insert(fb) @@ -968,13 +1003,14 @@ impl Global { num_dynamic_offsets, bind_group_id, } => { + let scope = PassErrorScope::SetBindGroup; let max_bind_groups = device.limits.max_bind_groups; if (index as u32) >= max_bind_groups { return Err(RenderCommandError::BindGroupIndexOutOfRange { index, max: max_bind_groups, - } - .into()); + }) + .map_pass_err(scope); } temp_offsets.clear(); @@ -988,9 +1024,11 @@ impl Global { .unwrap(); bind_group .validate_dynamic_bindings(&temp_offsets) - .map_err(RenderPassError::from)?; + .map_pass_err(scope)?; - trackers.merge_extend(&bind_group.used)?; + trackers + .merge_extend(&bind_group.used) + .map_pass_err(scope)?; if let Some((pipeline_layout_id, follow_ups)) = state.binder.provide_entry( index as usize, @@ -1017,6 +1055,7 @@ impl Global { }; } RenderCommand::SetPipeline(pipeline_id) => { + let scope = PassErrorScope::SetPipeline; if state.pipeline.set_and_check_redundant(pipeline_id) { continue; } @@ -1024,18 +1063,21 @@ impl Global { let pipeline = trackers .render_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ()) - .map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id))?; + .map_err(|_| RenderCommandError::InvalidPipeline(pipeline_id)) + .map_pass_err(scope)?; context .check_compatible(&pipeline.pass_context) - .map_err(RenderCommandError::IncompatiblePipeline)?; + .map_err(RenderCommandError::IncompatiblePipeline) + .map_pass_err(scope)?; state.pipeline_flags = pipeline.flags; if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH_STENCIL) && is_ds_read_only { - return Err(RenderCommandError::IncompatibleReadOnlyDepthStencil.into()); + return Err(RenderCommandError::IncompatibleReadOnlyDepthStencil) + .map_pass_err(scope); } state @@ -1155,15 +1197,18 @@ impl Global { offset, size, } => { + let scope = PassErrorScope::SetIndexBuffer; let buffer = trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDEX) - .map_err(|e| RenderCommandError::Buffer(buffer_id, e))?; - check_buffer_usage(buffer.usage, BufferUsage::INDEX)?; + .map_err(|e| RenderCommandError::Buffer(buffer_id, e)) + .map_pass_err(scope)?; + check_buffer_usage(buffer.usage, BufferUsage::INDEX).map_pass_err(scope)?; let &(ref buf_raw, _) = buffer .raw .as_ref() - .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))?; + .ok_or(RenderCommandError::DestroyedBuffer(buffer_id)) + .map_pass_err(scope)?; let end = match size { Some(s) => offset + s.get(), @@ -1191,15 +1236,18 @@ impl Global { offset, size, } => { + let scope = PassErrorScope::SetVertexBuffer; let buffer = trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::VERTEX) - .map_err(|e| RenderCommandError::Buffer(buffer_id, e))?; - check_buffer_usage(buffer.usage, BufferUsage::VERTEX)?; + .map_err(|e| RenderCommandError::Buffer(buffer_id, e)) + .map_pass_err(scope)?; + check_buffer_usage(buffer.usage, BufferUsage::VERTEX).map_pass_err(scope)?; let &(ref buf_raw, _) = buffer .raw .as_ref() - .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))?; + .ok_or(RenderCommandError::DestroyedBuffer(buffer_id)) + .map_pass_err(scope)?; let empty_slots = (1 + slot as usize).saturating_sub(state.vertex.inputs.len()); state @@ -1242,6 +1290,7 @@ impl Global { depth_min, depth_max, } => { + let scope = PassErrorScope::SetViewport; use std::{convert::TryFrom, i16}; if rect.w <= 0.0 || rect.h <= 0.0 @@ -1250,7 +1299,7 @@ impl Global { || depth_max < 0.0 || depth_max > 1.0 { - Err(RenderCommandError::InvalidViewport)? + return Err(RenderCommandError::InvalidViewport).map_pass_err(scope); } let r = hal::pso::Rect { x: i16::try_from(rect.x.round() as i64).unwrap_or(0), @@ -1274,8 +1323,10 @@ impl Global { size_bytes, values_offset, } => { - let values_offset = - values_offset.ok_or(RenderPassError::InvalidValuesOffset)?; + let scope = PassErrorScope::SetPushConstant; + let values_offset = values_offset + .ok_or(RenderPassErrorInner::InvalidValuesOffset) + .map_pass_err(scope)?; let end_offset_bytes = offset + size_bytes; let values_end_offset = @@ -1286,12 +1337,14 @@ impl Global { let pipeline_layout_id = state .binder .pipeline_layout_id - .ok_or(DrawError::MissingPipeline)?; + .ok_or(DrawError::MissingPipeline) + .map_pass_err(scope)?; let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id]; pipeline_layout .validate_push_constant_ranges(stages, offset, end_offset_bytes) - .map_err(RenderCommandError::from)?; + .map_err(RenderCommandError::from) + .map_pass_err(scope)?; unsafe { raw.push_graphics_constants( @@ -1303,13 +1356,14 @@ impl Global { } } RenderCommand::SetScissor(ref rect) => { + let scope = PassErrorScope::SetScissorRect; use std::{convert::TryFrom, i16}; if rect.w == 0 || rect.h == 0 || rect.x + rect.w > attachment_width.unwrap() || rect.y + rect.h > attachment_height.unwrap() { - Err(RenderCommandError::InvalidScissorRect)? + return Err(RenderCommandError::InvalidScissorRect).map_pass_err(scope); } let r = hal::pso::Rect { x: i16::try_from(rect.x).unwrap_or(0), @@ -1327,22 +1381,25 @@ impl Global { first_vertex, first_instance, } => { - state.is_ready()?; + let scope = PassErrorScope::Draw; + state.is_ready().map_pass_err(scope)?; let last_vertex = first_vertex + vertex_count; let vertex_limit = state.vertex.vertex_limit; if last_vertex > vertex_limit { - Err(DrawError::VertexBeyondLimit { + return Err(DrawError::VertexBeyondLimit { last_vertex, vertex_limit, - })? + }) + .map_pass_err(scope); } let last_instance = first_instance + instance_count; let instance_limit = state.vertex.instance_limit; if last_instance > instance_limit { - Err(DrawError::InstanceBeyondLimit { + return Err(DrawError::InstanceBeyondLimit { last_instance, instance_limit, - })? + }) + .map_pass_err(scope); } unsafe { @@ -1359,24 +1416,27 @@ impl Global { base_vertex, first_instance, } => { - state.is_ready()?; + let scope = PassErrorScope::DrawIndexed; + state.is_ready().map_pass_err(scope)?; //TODO: validate that base_vertex + max_index() is within the provided range let last_index = first_index + index_count; let index_limit = state.index.limit; if last_index > index_limit { - Err(DrawError::IndexBeyondLimit { + return Err(DrawError::IndexBeyondLimit { last_index, index_limit, - })? + }) + .map_pass_err(scope); } let last_instance = first_instance + instance_count; let instance_limit = state.vertex.instance_limit; if last_instance > instance_limit { - Err(DrawError::InstanceBeyondLimit { + return Err(DrawError::InstanceBeyondLimit { last_instance, instance_limit, - })? + }) + .map_pass_err(scope); } unsafe { @@ -1393,7 +1453,12 @@ impl Global { count, indexed, } => { - state.is_ready()?; + let scope = if indexed { + PassErrorScope::DrawIndexedIndirect + } else { + PassErrorScope::DrawIndirect + }; + state.is_ready().map_pass_err(scope)?; let stride = match indexed { false => 16, @@ -1401,31 +1466,36 @@ impl Global { }; if count.is_some() { - check_device_features(device.features, wgt::Features::MULTI_DRAW_INDIRECT)?; + check_device_features(device.features, wgt::Features::MULTI_DRAW_INDIRECT) + .map_pass_err(scope)?; } let indirect_buffer = trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT) - .map_err(|e| RenderCommandError::Buffer(buffer_id, e))?; - check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT)?; + .map_err(|e| RenderCommandError::Buffer(buffer_id, e)) + .map_pass_err(scope)?; + check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT) + .map_pass_err(scope)?; let &(ref indirect_raw, _) = indirect_buffer .raw .as_ref() - .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))?; + .ok_or(RenderCommandError::DestroyedBuffer(buffer_id)) + .map_pass_err(scope)?; let actual_count = count.map_or(1, |c| c.get()); let begin_offset = offset; let end_offset = offset + stride * actual_count as u64; if end_offset > indirect_buffer.size { - return Err(RenderPassError::IndirectBufferOverrun { + return Err(RenderPassErrorInner::IndirectBufferOverrun { offset, count, begin_offset, end_offset, buffer_size: indirect_buffer.size, - }); + }) + .map_pass_err(scope); } match indexed { @@ -1450,7 +1520,12 @@ impl Global { max_count, indexed, } => { - state.is_ready()?; + let scope = if indexed { + PassErrorScope::DrawIndexedIndirect + } else { + PassErrorScope::DrawIndirect + }; + state.is_ready().map_pass_err(scope)?; let stride = match indexed { false => 16, @@ -1460,48 +1535,57 @@ impl Global { check_device_features( device.features, wgt::Features::MULTI_DRAW_INDIRECT_COUNT, - )?; + ) + .map_pass_err(scope)?; let indirect_buffer = trackers .buffers .use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT) - .map_err(|e| RenderCommandError::Buffer(buffer_id, e))?; - check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT)?; + .map_err(|e| RenderCommandError::Buffer(buffer_id, e)) + .map_pass_err(scope)?; + check_buffer_usage(indirect_buffer.usage, BufferUsage::INDIRECT) + .map_pass_err(scope)?; let &(ref indirect_raw, _) = indirect_buffer .raw .as_ref() - .ok_or(RenderCommandError::DestroyedBuffer(buffer_id))?; + .ok_or(RenderCommandError::DestroyedBuffer(buffer_id)) + .map_pass_err(scope)?; let count_buffer = trackers .buffers .use_extend(&*buffer_guard, count_buffer_id, (), BufferUse::INDIRECT) - .map_err(|e| RenderCommandError::Buffer(count_buffer_id, e))?; - check_buffer_usage(count_buffer.usage, BufferUsage::INDIRECT)?; + .map_err(|e| RenderCommandError::Buffer(count_buffer_id, e)) + .map_pass_err(scope)?; + check_buffer_usage(count_buffer.usage, BufferUsage::INDIRECT) + .map_pass_err(scope)?; let &(ref count_raw, _) = count_buffer .raw .as_ref() - .ok_or(RenderCommandError::DestroyedBuffer(count_buffer_id))?; + .ok_or(RenderCommandError::DestroyedBuffer(count_buffer_id)) + .map_pass_err(scope)?; let begin_offset = offset; let end_offset = offset + stride * max_count as u64; if end_offset > indirect_buffer.size { - return Err(RenderPassError::IndirectBufferOverrun { + return Err(RenderPassErrorInner::IndirectBufferOverrun { offset, count: None, begin_offset, end_offset, buffer_size: indirect_buffer.size, - }); + }) + .map_pass_err(scope); } let begin_count_offset = count_buffer_offset; let end_count_offset = count_buffer_offset + 4; if end_count_offset > count_buffer.size { - return Err(RenderPassError::IndirectCountBufferOverrun { + return Err(RenderPassErrorInner::IndirectCountBufferOverrun { begin_count_offset, end_count_offset, count_buffer_size: count_buffer.size, - }); + }) + .map_pass_err(scope); } match indexed { @@ -1536,8 +1620,9 @@ impl Global { base.string_data = &base.string_data[len..]; } RenderCommand::PopDebugGroup => { + let scope = PassErrorScope::PopDebugGroup; if state.debug_scope_depth == 0 { - return Err(RenderPassError::InvalidPopDebugGroup); + return Err(RenderPassErrorInner::InvalidPopDebugGroup).map_pass_err(scope); } state.debug_scope_depth -= 1; unsafe { @@ -1552,6 +1637,7 @@ impl Global { base.string_data = &base.string_data[len..]; } RenderCommand::ExecuteBundle(bundle_id) => { + let scope = PassErrorScope::ExecuteBundle; let bundle = trackers .bundles .use_extend(&*bundle_guard, bundle_id, (), ()) @@ -1559,7 +1645,8 @@ impl Global { context .check_compatible(&bundle.context) - .map_err(RenderPassError::IncompatibleRenderBundle)?; + .map_err(RenderPassErrorInner::IncompatibleRenderBundle) + .map_pass_err(scope)?; unsafe { bundle.execute( @@ -1574,9 +1661,10 @@ impl Global { ExecutionError::DestroyedBuffer(id) => { RenderCommandError::DestroyedBuffer(id) } - })?; + }) + .map_pass_err(scope)?; - trackers.merge_extend(&bundle.used)?; + trackers.merge_extend(&bundle.used).map_pass_err(scope)?; state.reset_bundle(); } } @@ -1589,7 +1677,8 @@ impl Global { for ra in render_attachments { let texture = &texture_guard[ra.texture_id.value]; - check_texture_usage(texture.usage, TextureUsage::RENDER_ATTACHMENT)?; + check_texture_usage(texture.usage, TextureUsage::RENDER_ATTACHMENT) + .map_pass_err(scope)?; // the tracker set of the pass is always in "extend" mode trackers