Add LabeledContextError

Purpose for this is to add more context to an error.
create_render_pipeline error is improved, by showing the action
(creating render pipeline) and the user provided label, to aid with
the investigation.
This commit is contained in:
Mikko Lehtonen 2020-09-05 02:54:35 +03:00
parent 730313177e
commit 2fb8fc34d2
3 changed files with 84 additions and 36 deletions

View File

@ -2595,7 +2595,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
implicit_pipeline_ids: Option<ImplicitPipelineIds<G>>, implicit_pipeline_ids: Option<ImplicitPipelineIds<G>>,
) -> Result< ) -> Result<
(id::RenderPipelineId, pipeline::ImplicitBindGroupCount), (id::RenderPipelineId, pipeline::ImplicitBindGroupCount),
pipeline::CreateRenderPipelineError, crate::LabeledContextError<pipeline::CreateRenderPipelineError>,
> { > {
span!(_guard, INFO, "Device::create_render_pipeline"); span!(_guard, INFO, "Device::create_render_pipeline");
@ -2605,7 +2605,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let samples = { let samples = {
let sc = desc.sample_count; let sc = desc.sample_count;
if sc == 0 || sc > 32 || !conv::is_power_of_two(sc) { if sc == 0 || sc > 32 || !conv::is_power_of_two(sc) {
return Err(pipeline::CreateRenderPipelineError::InvalidSampleCount(sc)); return Err(pipeline::CreateRenderPipelineError::InvalidSampleCount(sc)
.with_label(&desc.label));
} }
sc as u8 sc as u8
}; };
@ -2638,7 +2639,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
return Err(pipeline::CreateRenderPipelineError::UnalignedVertexStride { return Err(pipeline::CreateRenderPipelineError::UnalignedVertexStride {
index: i as u32, index: i as u32,
stride: vb_state.stride, stride: vb_state.stride,
}); }
.with_label(&desc.label));
} }
vertex_buffers.alloc().init(hal::pso::VertexBufferDesc { vertex_buffers.alloc().init(hal::pso::VertexBufferDesc {
binding: i as u32, binding: i as u32,
@ -2655,7 +2657,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset { pipeline::CreateRenderPipelineError::InvalidVertexAttributeOffset {
location: attribute.shader_location, location: attribute.shader_location,
offset: attribute.offset, offset: attribute.offset,
}, }
.with_label(&desc.label),
); );
} }
attributes.alloc().init(hal::pso::AttributeDesc { attributes.alloc().init(hal::pso::AttributeDesc {
@ -2711,15 +2714,16 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}; };
let (device_guard, mut token) = hub.devices.read(&mut token); let (device_guard, mut token) = hub.devices.read(&mut token);
let device = device_guard let device = device_guard.get(device_id).map_err(|_| {
.get(device_id) pipeline::CreateRenderPipelineError::from(DeviceError::Invalid).with_label(&desc.label)
.map_err(|_| DeviceError::Invalid)?; })?;
if rasterization_state.clamp_depth if rasterization_state.clamp_depth
&& !device.features.contains(wgt::Features::DEPTH_CLAMPING) && !device.features.contains(wgt::Features::DEPTH_CLAMPING)
{ {
return Err(pipeline::CreateRenderPipelineError::MissingFeature( return Err(pipeline::CreateRenderPipelineError::MissingFeature(
wgt::Features::DEPTH_CLAMPING, wgt::Features::DEPTH_CLAMPING,
)); )
.with_label(&desc.label));
} }
let (raw_pipeline, layout_id, layout_ref_count, derived_bind_group_count) = { let (raw_pipeline, layout_id, layout_ref_count, derived_bind_group_count) = {
@ -2781,17 +2785,21 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let shader_module = let shader_module =
shader_module_guard shader_module_guard
.get(desc.vertex_stage.module) .get(desc.vertex_stage.module)
.map_err(|_| pipeline::CreateRenderPipelineError::Stage { .map_err(|_| {
flag, pipeline::CreateRenderPipelineError::Stage {
error: validation::StageError::InvalidModule, flag,
error: validation::StageError::InvalidModule,
}
.with_label(&desc.label)
})?; })?;
if let Some(ref module) = shader_module.module { if let Some(ref module) = shader_module.module {
let group_layouts = match desc.layout { let group_layouts = match desc.layout {
Some(pipeline_layout_id) => Device::get_introspection_bind_group_layouts( Some(pipeline_layout_id) => Device::get_introspection_bind_group_layouts(
pipeline_layout_guard pipeline_layout_guard.get(pipeline_layout_id).map_err(|_| {
.get(pipeline_layout_id) pipeline::CreateRenderPipelineError::InvalidLayout
.map_err(|_| pipeline::CreateRenderPipelineError::InvalidLayout)?, .with_label(&desc.label)
})?,
&*bgl_guard, &*bgl_guard,
), ),
None => validation::IntrospectionBindGroupLayouts::Derived( None => validation::IntrospectionBindGroupLayouts::Derived(
@ -2806,7 +2814,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
naga::ShaderStage::Vertex, naga::ShaderStage::Vertex,
interface, interface,
) )
.map_err(|error| pipeline::CreateRenderPipelineError::Stage { flag, error })?; .map_err(|error| {
pipeline::CreateRenderPipelineError::Stage { flag, error }
.with_label(&desc.label)
})?;
validated_stages |= flag; validated_stages |= flag;
} }
@ -2827,13 +2838,15 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
flag, flag,
error: validation::StageError::InvalidModule, error: validation::StageError::InvalidModule,
} }
.with_label(&desc.label)
})?; })?;
let group_layouts = match desc.layout { let group_layouts = match desc.layout {
Some(pipeline_layout_id) => Device::get_introspection_bind_group_layouts( Some(pipeline_layout_id) => Device::get_introspection_bind_group_layouts(
pipeline_layout_guard pipeline_layout_guard.get(pipeline_layout_id).map_err(|_| {
.get(pipeline_layout_id) pipeline::CreateRenderPipelineError::InvalidLayout
.map_err(|_| pipeline::CreateRenderPipelineError::InvalidLayout)?, .with_label(&desc.label)
})?,
&*bgl_guard, &*bgl_guard,
), ),
None => validation::IntrospectionBindGroupLayouts::Derived( None => validation::IntrospectionBindGroupLayouts::Derived(
@ -2852,6 +2865,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
) )
.map_err(|error| { .map_err(|error| {
pipeline::CreateRenderPipelineError::Stage { flag, error } pipeline::CreateRenderPipelineError::Stage { flag, error }
.with_label(&desc.label)
})?; })?;
validated_stages |= flag; validated_stages |= flag;
} }
@ -2879,7 +2893,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
return Err( return Err(
pipeline::CreateRenderPipelineError::IncompatibleOutputFormat { pipeline::CreateRenderPipelineError::IncompatibleOutputFormat {
index: i as u8, index: i as u8,
}, }
.with_label(&desc.label),
); );
} }
} }
@ -2889,7 +2904,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
None => wgt::ShaderStage::VERTEX, None => wgt::ShaderStage::VERTEX,
}; };
if desc.layout.is_none() && !validated_stages.contains(last_stage) { if desc.layout.is_none() && !validated_stages.contains(last_stage) {
Err(pipeline::ImplicitLayoutError::ReflectionError(last_stage))? Err(pipeline::CreateRenderPipelineError::from(
pipeline::ImplicitLayoutError::ReflectionError(last_stage),
)
.with_label(&desc.label))?
} }
let primitive_assembler = hal::pso::PrimitiveAssemblerDesc::Vertex { let primitive_assembler = hal::pso::PrimitiveAssemblerDesc::Vertex {
@ -2908,18 +2926,22 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let (pipeline_layout_id, derived_bind_group_count) = match desc.layout { let (pipeline_layout_id, derived_bind_group_count) = match desc.layout {
Some(id) => (id, 0), Some(id) => (id, 0),
None => self.derive_pipeline_layout( None => self
device, .derive_pipeline_layout(
device_id, device,
implicit_pipeline_ids, device_id,
derived_group_layouts, implicit_pipeline_ids,
&mut *bgl_guard, derived_group_layouts,
&mut *pipeline_layout_guard, &mut *bgl_guard,
)?, &mut *pipeline_layout_guard,
)
.map_err(|e| {
pipeline::CreateRenderPipelineError::from(e).with_label(&desc.label)
})?,
}; };
let layout = pipeline_layout_guard let layout = pipeline_layout_guard.get(pipeline_layout_id).map_err(|_| {
.get(pipeline_layout_id) pipeline::CreateRenderPipelineError::InvalidLayout.with_label(&desc.label)
.map_err(|_| pipeline::CreateRenderPipelineError::InvalidLayout)?; })?;
let mut render_pass_cache = device.render_passes.lock(); let mut render_pass_cache = device.render_passes.lock();
let pipeline_desc = hal::pso::GraphicsPipelineDesc { let pipeline_desc = hal::pso::GraphicsPipelineDesc {
@ -2936,9 +2958,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
main_pass: match render_pass_cache.entry(rp_key) { main_pass: match render_pass_cache.entry(rp_key) {
Entry::Occupied(e) => e.into_mut(), Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => { Entry::Vacant(e) => {
let pass = device let pass = device.create_compatible_render_pass(e.key()).or(Err(
.create_compatible_render_pass(e.key()) pipeline::CreateRenderPipelineError::from(DeviceError::OutOfMemory)
.or(Err(DeviceError::OutOfMemory))?; .with_label(&desc.label),
))?;
e.insert(pass) e.insert(pass)
} }
}, },
@ -2952,7 +2975,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.raw .raw
.create_graphics_pipeline(&pipeline_desc, None) .create_graphics_pipeline(&pipeline_desc, None)
.map_err(|err| match err { .map_err(|err| match err {
hal::pso::CreationError::OutOfMemory(_) => DeviceError::OutOfMemory, hal::pso::CreationError::OutOfMemory(_) => {
pipeline::CreateRenderPipelineError::from(DeviceError::OutOfMemory)
.with_label(&desc.label)
}
_ => panic!("failed to create graphics pipeline: {}", err), _ => panic!("failed to create graphics pipeline: {}", err),
})? })?
}; };

View File

@ -238,3 +238,11 @@ fn test_default_limits() {
let limits = wgt::Limits::default(); let limits = wgt::Limits::default();
assert!(limits.max_bind_groups <= MAX_BIND_GROUPS as u32); assert!(limits.max_bind_groups <= MAX_BIND_GROUPS as u32);
} }
#[derive(Clone, Debug, thiserror::Error)]
#[error("{description} (label: {label:?})")]
pub struct LabeledContextError<E: std::error::Error + 'static> {
source: E,
description: &'static str,
label: Option<String>,
}

View File

@ -183,13 +183,27 @@ pub enum CreateRenderPipelineError {
}, },
#[error("missing required device features {0:?}")] #[error("missing required device features {0:?}")]
MissingFeature(wgt::Features), MissingFeature(wgt::Features),
#[error("error in stage {flag:?}: {error}")] #[error("error in stage {flag:?}")]
Stage { Stage {
flag: wgt::ShaderStage, flag: wgt::ShaderStage,
#[source]
error: StageError, error: StageError,
}, },
} }
impl CreateRenderPipelineError {
pub(crate) fn with_label<'a>(
self,
label: &Option<Cow<'a, str>>,
) -> crate::LabeledContextError<CreateRenderPipelineError> {
crate::LabeledContextError {
source: self,
description: "Creating render pipeline",
label: label.as_ref().map(|inner| inner.to_string()),
}
}
}
bitflags::bitflags! { bitflags::bitflags! {
#[repr(transparent)] #[repr(transparent)]
pub struct PipelineFlags: u32 { pub struct PipelineFlags: u32 {