From 150ee25c012073f05d044c9768bf59a774b02263 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 14 Oct 2020 10:55:36 -0400 Subject: [PATCH] Align stencil reference flags between pipeline creation and setting --- wgpu-core/src/command/draw.rs | 2 -- wgpu-core/src/command/render.rs | 36 ++++++++++++++++++++++----------- wgpu-core/src/device/mod.rs | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index c2dc454b0..9f2ca4d79 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -24,8 +24,6 @@ pub type BufferError = UseExtendError; pub enum DrawError { #[error("blend color needs to be set")] MissingBlendColor, - #[error("stencil reference needs to be set")] - MissingStencilReference, #[error("render pipeline must be set")] MissingPipeline, #[error("current render pipeline has a layout which is incompatible with a currently set bind group, first differing at entry index {index}")] diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 55ed39d39..f9a98b0a5 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -269,9 +269,10 @@ impl VertexState { #[derive(Debug)] struct State { + pipeline_flags: PipelineFlags, binder: Binder, blend_color: OptionalState, - stencil_reference: OptionalState, + stencil_reference: u32, pipeline: OptionalState, index: IndexState, vertex: VertexState, @@ -294,9 +295,6 @@ impl State { if self.blend_color == OptionalState::Required { return Err(DrawError::MissingBlendColor); } - if self.stencil_reference == OptionalState::Required { - return Err(DrawError::MissingStencilReference); - } Ok(()) } @@ -943,9 +941,10 @@ impl Global { }; let mut state = State { + pipeline_flags: PipelineFlags::empty(), binder: Binder::new(cmd_buf.limits.max_bind_groups), blend_color: OptionalState::Unused, - stencil_reference: OptionalState::Unused, + stencil_reference: 0, pipeline: OptionalState::Required, index: IndexState::default(), vertex: VertexState::default(), @@ -1009,7 +1008,6 @@ impl Global { }; } RenderCommand::SetPipeline(pipeline_id) => { - state.pipeline = OptionalState::Set; let pipeline = trackers .render_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ()) @@ -1019,6 +1017,9 @@ impl Global { .check_compatible(&pipeline.pass_context) .map_err(RenderCommandError::IncompatiblePipeline)?; + state.pipeline = OptionalState::Set; + state.pipeline_flags = pipeline.flags; + if pipeline.flags.contains(PipelineFlags::WRITES_DEPTH_STENCIL) && is_ds_read_only { @@ -1028,14 +1029,20 @@ impl Global { state .blend_color .require(pipeline.flags.contains(PipelineFlags::BLEND_COLOR)); - state - .stencil_reference - .require(pipeline.flags.contains(PipelineFlags::STENCIL_REFERENCE)); unsafe { raw.bind_graphics_pipeline(&pipeline.raw); } + if pipeline.flags.contains(PipelineFlags::STENCIL_REFERENCE) { + unsafe { + raw.set_stencil_reference( + hal::pso::Face::all(), + state.stencil_reference, + ); + } + } + // Rebind resource if state.binder.pipeline_layout_id != Some(pipeline.layout_id.value) { let pipeline_layout = &pipeline_layout_guard[pipeline.layout_id.value]; @@ -1208,9 +1215,14 @@ impl Global { } } RenderCommand::SetStencilReference(value) => { - state.stencil_reference = OptionalState::Set; - unsafe { - raw.set_stencil_reference(hal::pso::Face::all(), value); + state.stencil_reference = value; + if state + .pipeline_flags + .contains(PipelineFlags::STENCIL_REFERENCE) + { + unsafe { + raw.set_stencil_reference(hal::pso::Face::all(), value); + } } } RenderCommand::SetViewport { diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 7f340e922..a2a9c97ea 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3135,7 +3135,7 @@ impl Global { } } if let Some(ds) = depth_stencil_state.as_ref() { - if ds.stencil.needs_ref_value() { + if ds.stencil.is_enabled() && ds.stencil.needs_ref_value() { flags |= pipeline::PipelineFlags::STENCIL_REFERENCE; } if !ds.is_read_only() {