From 752c1aad33026f360bd5e5d0390fc802fe958b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20L=C3=B6bel?= Date: Tue, 20 Oct 2020 19:47:34 +0200 Subject: [PATCH] Elide redundant set_pipeline calls. Add a small abstraction over state changes Deduplicate redundant pipeline state checking --- wgpu-core/src/command/bundle.rs | 8 +++++++- wgpu-core/src/command/compute.rs | 19 ++++++++----------- wgpu-core/src/command/mod.rs | 22 ++++++++++++++++++++++ wgpu-core/src/command/render.rs | 15 +++++++++------ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index fe22efe3a..58eaa122d 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -38,7 +38,7 @@ #![allow(clippy::reversed_empty_ranges)] use crate::{ - command::{BasePass, DrawError, RenderCommand, RenderCommandError}, + command::{BasePass, DrawError, RenderCommand, RenderCommandError, StateChange}, conv, device::{ AttachmentData, DeviceError, RenderPassContext, MAX_VERTEX_BUFFERS, SHADER_STAGE_COUNT, @@ -515,6 +515,7 @@ struct State { raw_dynamic_offsets: Vec, flat_dynamic_offsets: Vec, used_bind_groups: usize, + pipeline: StateChange, } impl State { @@ -698,6 +699,7 @@ impl Global { raw_dynamic_offsets: Vec::new(), flat_dynamic_offsets: Vec::new(), used_bind_groups: 0, + pipeline: StateChange::new(), }; let mut commands = Vec::new(); let mut base = bundle_encoder.base.as_ref(); @@ -746,6 +748,10 @@ impl Global { state.trackers.merge_extend(&bind_group.used)?; } RenderCommand::SetPipeline(pipeline_id) => { + if state.pipeline.set_and_check_redundant(pipeline_id) { + continue; + } + let pipeline = state .trackers .render_pipes diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index a6897c342..6bd4c00d6 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -6,7 +6,7 @@ use crate::{ binding_model::{BindError, BindGroup, PushConstantUploadError}, command::{ bind::{Binder, LayoutChange}, - BasePass, BasePassRef, CommandBuffer, CommandEncoderError, + BasePass, BasePassRef, CommandBuffer, CommandEncoderError, StateChange, }, hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Storage, Token}, id, @@ -137,16 +137,10 @@ pub enum ComputePassError { PushConstants(#[from] PushConstantUploadError), } -#[derive(Debug, PartialEq)] -enum PipelineState { - Required, - Set, -} - #[derive(Debug)] struct State { binder: Binder, - pipeline: PipelineState, + pipeline: StateChange, trackers: TrackerSet, debug_scope_depth: u32, } @@ -161,7 +155,7 @@ impl State { index: bind_mask.trailing_zeros(), }); } - if self.pipeline == PipelineState::Required { + if self.pipeline.is_unset() { return Err(DispatchError::MissingPipeline); } Ok(()) @@ -235,7 +229,7 @@ impl Global { let mut state = State { binder: Binder::new(cmd_buf.limits.max_bind_groups), - pipeline: PipelineState::Required, + pipeline: StateChange::new(), trackers: TrackerSet::new(B::VARIANT), debug_scope_depth: 0, }; @@ -293,7 +287,10 @@ impl Global { } } ComputeCommand::SetPipeline(pipeline_id) => { - state.pipeline = PipelineState::Set; + if state.pipeline.set_and_check_redundant(pipeline_id) { + continue; + } + let pipeline = cmd_buf .trackers .compute_pipes diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index 66dad6d63..4d8322779 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -270,3 +270,25 @@ where count_words += size_to_write_words; } } + +#[derive(Debug)] +struct StateChange { + last_state: Option, +} + +impl StateChange { + fn new() -> Self { + Self { last_state: None } + } + fn set_and_check_redundant(&mut self, new_state: T) -> bool { + let already_set = self.last_state == Some(new_state); + self.last_state = Some(new_state); + already_set + } + fn is_unset(&self) -> bool { + self.last_state.is_none() + } + fn reset(&mut self) { + self.last_state = None; + } +} diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index f9a98b0a5..6794227d8 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, + RenderCommand, RenderCommandError, StateChange, }, conv, device::{ @@ -273,7 +273,7 @@ struct State { binder: Binder, blend_color: OptionalState, stencil_reference: u32, - pipeline: OptionalState, + pipeline: StateChange, index: IndexState, vertex: VertexState, debug_scope_depth: u32, @@ -289,7 +289,7 @@ impl State { index: bind_mask.trailing_zeros(), }); } - if self.pipeline == OptionalState::Required { + if self.pipeline.is_unset() { return Err(DrawError::MissingPipeline); } if self.blend_color == OptionalState::Required { @@ -301,7 +301,7 @@ impl State { /// Reset the `RenderBundle`-related states. fn reset_bundle(&mut self) { self.binder.reset(); - self.pipeline = OptionalState::Required; + self.pipeline.reset(); self.index.reset(); self.vertex.reset(); } @@ -945,7 +945,7 @@ impl Global { binder: Binder::new(cmd_buf.limits.max_bind_groups), blend_color: OptionalState::Unused, stencil_reference: 0, - pipeline: OptionalState::Required, + pipeline: StateChange::new(), index: IndexState::default(), vertex: VertexState::default(), debug_scope_depth: 0, @@ -1008,6 +1008,10 @@ impl Global { }; } RenderCommand::SetPipeline(pipeline_id) => { + if state.pipeline.set_and_check_redundant(pipeline_id) { + continue; + } + let pipeline = trackers .render_pipes .use_extend(&*pipeline_guard, pipeline_id, (), ()) @@ -1017,7 +1021,6 @@ 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)