From 1b03fbe9be5b4a2f5fd2a6333f77aeb18a074673 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Tue, 14 Jul 2020 09:00:32 +0300 Subject: [PATCH] Safe error handling for commands API --- player/src/main.rs | 12 +- wgpu-core/src/binding_model.rs | 2 +- wgpu-core/src/command/bundle.rs | 171 ++++++++------ wgpu-core/src/command/compute.rs | 76 +++--- wgpu-core/src/command/render.rs | 388 +++++++++++++++---------------- wgpu-core/src/device/mod.rs | 4 +- 6 files changed, 351 insertions(+), 302 deletions(-) diff --git a/player/src/main.rs b/player/src/main.rs index f172587b2..c5be023ee 100644 --- a/player/src/main.rs +++ b/player/src/main.rs @@ -123,7 +123,8 @@ impl GlobalExt for wgc::hub::Global { .command_encoder_copy_texture_to_texture::(encoder, &src, &dst, &size) .unwrap(), trace::Command::RunComputePass { base } => { - self.command_encoder_run_compute_pass_impl::(encoder, base.as_ref()); + self.command_encoder_run_compute_pass_impl::(encoder, base.as_ref()) + .unwrap(); } trace::Command::RunRenderPass { base, @@ -135,7 +136,8 @@ impl GlobalExt for wgc::hub::Global { base.as_ref(), &target_colors, target_depth_stencil.as_ref(), - ); + ) + .unwrap(); } } } @@ -364,14 +366,16 @@ impl GlobalExt for wgc::hub::Global { }, device, Some(base), - ); + ) + .unwrap(); self.render_bundle_encoder_finish::( bundle, &wgt::RenderBundleDescriptor { label: label.as_ptr(), }, id, - ); + ) + .unwrap(); } A::DestroyRenderBundle(id) => { self.render_bundle_destroy::(id); diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index aa7f0fb89..40483b7d7 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -378,7 +378,7 @@ pub type BindGroupEntry<'a> = wgt::BindGroupEntry>; pub type BindGroupDescriptor<'a> = wgt::BindGroupDescriptor<'a, BindGroupLayoutId, BindGroupEntry<'a>>; -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum BindError { /// Number of dynamic offsets doesn't match the number of dynamic bindings /// in the bind group layout. diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 266681485..82aef71da 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -37,6 +37,7 @@ !*/ use crate::{ + binding_model::PushConstantUploadError, command::{BasePass, RenderCommand}, conv, device::{AttachmentData, Label, RenderPassContext, MAX_VERTEX_BUFFERS, SHADER_STAGE_COUNT}, @@ -62,9 +63,9 @@ impl RenderBundleEncoder { desc: &wgt::RenderBundleEncoderDescriptor, parent_id: id::DeviceId, base: Option>, - ) -> Self { + ) -> Result { span!(_guard, INFO, "RenderBundleEncoder::new"); - RenderBundleEncoder { + Ok(RenderBundleEncoder { base: base.unwrap_or_else(BasePass::new), parent_id, context: RenderPassContext { @@ -75,11 +76,13 @@ impl RenderBundleEncoder { }, sample_count: { let sc = desc.sample_count; - assert!(sc != 0 && sc <= 32 && conv::is_power_of_two(sc)); + if sc == 0 || sc > 32 || !conv::is_power_of_two(sc) { + return Err(InvalidSampleCountError); + } sc as u8 }, }, - } + }) } pub fn parent(&self) -> id::DeviceId { @@ -87,6 +90,10 @@ impl RenderBundleEncoder { } } +/// Error type returned from `RenderBundleEncoder::new` if the sample count is invalid. +#[derive(Copy, Clone, Debug)] +pub struct InvalidSampleCountError; + //Note: here, `RenderBundle` is just wrapping a raw stream of render commands. // The plan is to back it by an actual Vulkan secondary buffer, D3D12 Bundle, // or Metal indirect command buffer. @@ -120,7 +127,7 @@ impl RenderBundle { bind_group_guard: &Storage, id::BindGroupId>, pipeline_guard: &Storage, id::RenderPipelineId>, buffer_guard: &Storage, id::BufferId>, - ) { + ) -> Result<(), RenderCommandError> { use hal::command::CommandBuffer as _; let mut offsets = self.base.dynamic_offsets.as_slice(); @@ -185,8 +192,9 @@ impl RenderBundle { size_bytes, values_offset, } => { - let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id - .expect("Must have a pipeline bound to use push constants")]; + let pipeline_layout_id = + pipeline_layout_id.ok_or(RenderCommandError::UnboundPipeline)?; + let pipeline_layout = &pipeline_layout_guard[pipeline_layout_id]; if let Some(values_offset) = values_offset { let values_end_offset = (values_offset + size_bytes / 4) as usize; @@ -268,6 +276,7 @@ impl RenderBundle { | RenderCommand::SetScissor(_) => unreachable!(), } } + Ok(()) } } @@ -579,13 +588,50 @@ impl State { } } +/// Error encountered when encoding a render command. +#[derive(Clone, Debug)] +pub enum RenderCommandError { + /// A bind group index is greater than the device's requested `max_bind_group` limit. + BindGroupIndexOutOfRange, + /// A dynamic buffer offset is not a multiple of the required buffer alignment. + UnalignedBufferOffset, + /// The number of buffer offsets does not match the number of dynamic bindings. + WrongBufferOffsetCount, + /// The pipeline is incompatible with the render pass. + /// + /// Either: + /// - The render pipeline output formats and sample counts do not match render pass attachment formats. + /// - The pipeline is not compatible with the depth-stencil read-only render pass. + IncompatiblePipeline, + /// The provided buffer does not have the required usage. + InvalidBufferUsage, + /// The provided texture does not have the required usage. + InvalidTextureUsage, + /// A pipeline must be bound. + UnboundPipeline, + /// There was an error with the push constants. + PushConstantUploadError(PushConstantUploadError), + /// Vertex extends beyond limit. + VertexBeyondLimit, + /// Instance extends beyond limit. + InstanceBeyondLimit, + /// Index extends beyond limit. + IndexBeyondLimit, +} + +impl From for RenderCommandError { + fn from(error: PushConstantUploadError) -> Self { + Self::PushConstantUploadError(error) + } +} + impl Global { pub fn render_bundle_encoder_finish( &self, bundle_encoder: RenderBundleEncoder, desc: &wgt::RenderBundleDescriptor