From aef0c7c2c4705b032b64eee7af8c95a8c9955bdd Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Sun, 5 Apr 2020 11:16:50 +0530 Subject: [PATCH] Add check for bound pipeline fix #456 Validate that a pipeline is bound before issuing draw/dispatch call. --- wgpu-core/src/command/compute.rs | 10 ++++++++++ wgpu-core/src/command/render.rs | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 26b370e64c..e503b6bea7 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -19,6 +19,11 @@ use peek_poke::{Peek, PeekCopy, Poke}; use std::iter; +#[derive(Debug, PartialEq)] +enum PipelineState { + Required, + Set, +} #[derive(Clone, Copy, Debug, PeekCopy, Poke)] enum ComputeCommand { @@ -76,6 +81,8 @@ impl Global { let (buffer_guard, mut token) = hub.buffers.read(&mut token); let (texture_guard, _) = hub.textures.read(&mut token); + let mut pipeline_state = PipelineState::Required; + let mut peeker = raw_data.as_ptr(); let raw_data_end = unsafe { raw_data.as_ptr().add(raw_data.len()) @@ -142,6 +149,7 @@ impl Global { } } ComputeCommand::SetPipeline(pipeline_id) => { + pipeline_state = PipelineState::Set; let pipeline = cmb.trackers .compute_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ()) @@ -186,11 +194,13 @@ impl Global { } } ComputeCommand::Dispatch(groups) => { + assert_eq!(pipeline_state, PipelineState::Set, "Dispatch error: Pipeline is missing"); unsafe { raw.dispatch(groups); } } ComputeCommand::DispatchIndirect { buffer_id, offset } => { + assert_eq!(pipeline_state, PipelineState::Set, "Dispatch error: Pipeline is missing"); let (src_buffer, src_pending) = cmb.trackers.buffers.use_replace( &*buffer_guard, buffer_id, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index e6dde232eb..4d527193b0 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -182,6 +182,7 @@ impl OptionalState { enum DrawError { MissingBlendColor, MissingStencilReference, + MissingPipeline, IncompatibleBindGroup { index: u32, //expected: BindGroupLayoutId, @@ -255,6 +256,7 @@ struct State { binder: Binder, blend_color: OptionalState, stencil_reference: OptionalState, + pipeline: OptionalState, index: IndexState, vertex: VertexState, } @@ -269,6 +271,9 @@ impl State { index: bind_mask.trailing_zeros(), }); } + if self.pipeline == OptionalState::Required { + return Err(DrawError::MissingPipeline); + } if self.blend_color == OptionalState::Required { return Err(DrawError::MissingBlendColor); } @@ -785,6 +790,7 @@ impl Global { binder: Binder::new(cmb.features.max_bind_groups), blend_color: OptionalState::Unused, stencil_reference: OptionalState::Unused, + pipeline: OptionalState::Required, index: IndexState { bound_buffer_view: None, format: IndexFormat::Uint16, @@ -852,6 +858,7 @@ impl Global { }; } RenderCommand::SetPipeline(pipeline_id) => { + state.pipeline = OptionalState::Set; let pipeline = trackers .render_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ())