/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /*! Render Bundles ## Software implementation The path from nothing to using a render bundle consists of 3 phases. ### Initial command encoding User creates a `RenderBundleEncoder` and populates it by issuing commands from `bundle_ffi` module, just like with `RenderPass`, except that the set of available commands is reduced. Everything is written into a `RawPass`. ### Bundle baking Once the commands are encoded, user calls `render_bundle_encoder_finish`. This is perhaps the most complex part of the logic. It consumes the commands stored in `RawPass`, while validating everything, tracking the state, and re-recording the commands into a separate `Vec`. It doesn't actually execute any commands. What's more important, is that the produced vector of commands is "normalized", which means it can be executed verbatim without any state tracking. More formally, "normalized" command stream guarantees that any state required by a draw call is set explicitly by one of the commands between the draw call and the last changing of the pipeline. ### Execution When the bundle is used in an actual render pass, `RenderBundle::execute` is called. It goes through the commands and issues them into the native command buffer. Thanks to the "normalized" property, it doesn't track any bind group invalidations or index format changes. !*/ use crate::{ command::{BasePass, RenderCommand}, conv, device::{AttachmentData, Label, RenderPassContext, MAX_VERTEX_BUFFERS}, hub::{GfxBackend, Global, GlobalIdentityHandlerFactory, Input, Storage, Token}, id, resource::BufferUse, span, track::TrackerSet, LifeGuard, RefCount, Stored, MAX_BIND_GROUPS, }; use arrayvec::ArrayVec; use std::{borrow::Borrow, iter, marker::PhantomData, ops::Range}; #[cfg_attr(feature = "serial-pass", derive(serde::Deserialize, serde::Serialize))] pub struct RenderBundleEncoder { base: BasePass, parent_id: id::DeviceId, pub(crate) context: RenderPassContext, } impl RenderBundleEncoder { pub fn new( desc: &wgt::RenderBundleEncoderDescriptor, parent_id: id::DeviceId, base: Option>, ) -> Self { span!(_guard, INFO, "RenderBundleEncoder::new"); RenderBundleEncoder { base: base.unwrap_or_else(BasePass::new), parent_id, context: RenderPassContext { attachments: AttachmentData { colors: desc.color_formats.iter().cloned().collect(), resolves: ArrayVec::new(), depth_stencil: desc.depth_stencil_format, }, sample_count: { let sc = desc.sample_count; assert!(sc != 0 && sc <= 32 && conv::is_power_of_two(sc)); sc as u8 }, }, } } pub fn parent(&self) -> id::DeviceId { self.parent_id } } //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. #[derive(Debug)] pub struct RenderBundle { // Normalized command stream. It can be executed verbatim, // without re-binding anything on the pipeline change. base: BasePass, pub(crate) device_id: Stored, pub(crate) used: TrackerSet, pub(crate) context: RenderPassContext, pub(crate) life_guard: LifeGuard, } unsafe impl Send for RenderBundle {} unsafe impl Sync for RenderBundle {} impl RenderBundle { /// Actually encode the contents into a native command buffer. /// /// This is partially duplicating the logic of `command_encoder_run_render_pass`. /// However the point of this function is to be lighter, since we already had /// a chance to go through the commands in `render_bundle_encoder_finish`. pub(crate) unsafe fn execute( &self, comb: &mut B::CommandBuffer, pipeline_layout_guard: &Storage< crate::binding_model::PipelineLayout, id::PipelineLayoutId, >, bind_group_guard: &Storage, id::BindGroupId>, pipeline_guard: &Storage, id::RenderPipelineId>, buffer_guard: &Storage, id::BufferId>, ) { use hal::command::CommandBuffer as _; let mut offsets = self.base.dynamic_offsets.as_slice(); let mut index_type = hal::IndexType::U16; let mut pipeline_layout_id = None::; for command in self.base.commands.iter() { match *command { RenderCommand::SetBindGroup { index, num_dynamic_offsets, bind_group_id, } => { let bind_group = &bind_group_guard[bind_group_id]; comb.bind_graphics_descriptor_sets( &pipeline_layout_guard[pipeline_layout_id.unwrap()].raw, index as usize, iter::once(bind_group.raw.raw()), &offsets[..num_dynamic_offsets as usize], ); offsets = &offsets[num_dynamic_offsets as usize..]; } RenderCommand::SetPipeline(pipeline_id) => { let pipeline = &pipeline_guard[pipeline_id]; comb.bind_graphics_pipeline(&pipeline.raw); index_type = conv::map_index_format(pipeline.index_format); pipeline_layout_id = Some(pipeline.layout_id.value); } RenderCommand::SetIndexBuffer { buffer_id, offset, size, } => { let buffer = &buffer_guard[buffer_id]; let view = hal::buffer::IndexBufferView { buffer: &buffer.raw, range: hal::buffer::SubRange { offset, size: size.map(|s| s.get()), }, index_type, }; comb.bind_index_buffer(view); } RenderCommand::SetVertexBuffer { slot, buffer_id, offset, size, } => { let buffer = &buffer_guard[buffer_id]; let range = hal::buffer::SubRange { offset, size: size.map(|s| s.get()), }; comb.bind_vertex_buffers(slot, iter::once((&buffer.raw, range))); } RenderCommand::Draw { vertex_count, instance_count, first_vertex, first_instance, } => { comb.draw( first_vertex..first_vertex + vertex_count, first_instance..first_instance + instance_count, ); } RenderCommand::DrawIndexed { index_count, instance_count, first_index, base_vertex, first_instance, } => { comb.draw_indexed( first_index..first_index + index_count, base_vertex, first_instance..first_instance + instance_count, ); } RenderCommand::MultiDrawIndirect { buffer_id, offset, count: None, indexed: false, } => { let buffer = &buffer_guard[buffer_id]; comb.draw_indirect(&buffer.raw, offset, 1, 0); } RenderCommand::MultiDrawIndirect { buffer_id, offset, count: None, indexed: true, } => { let buffer = &buffer_guard[buffer_id]; comb.draw_indexed_indirect(&buffer.raw, offset, 1, 0); } RenderCommand::MultiDrawIndirect { .. } | RenderCommand::MultiDrawIndirectCount { .. } => unimplemented!(), RenderCommand::PushDebugGroup { color: _, len: _ } => unimplemented!(), RenderCommand::InsertDebugMarker { color: _, len: _ } => unimplemented!(), RenderCommand::PopDebugGroup => unimplemented!(), RenderCommand::ExecuteBundle(_) | RenderCommand::SetBlendColor(_) | RenderCommand::SetStencilReference(_) | RenderCommand::SetViewport { .. } | RenderCommand::SetScissor(_) => unreachable!(), } } } } impl Borrow for RenderBundle { fn borrow(&self) -> &RefCount { self.life_guard.ref_count.as_ref().unwrap() } } #[derive(Debug)] struct IndexState { buffer: Option, format: wgt::IndexFormat, range: Range, is_dirty: bool, } impl IndexState { fn new() -> Self { IndexState { buffer: None, format: wgt::IndexFormat::default(), range: 0..0, is_dirty: false, } } fn limit(&self) -> u32 { assert!(self.buffer.is_some()); let bytes_per_index = match self.format { wgt::IndexFormat::Uint16 => 2, wgt::IndexFormat::Uint32 => 4, }; ((self.range.end - self.range.start) / bytes_per_index) as u32 } fn flush(&mut self) -> Option { if self.is_dirty { self.is_dirty = false; Some(RenderCommand::SetIndexBuffer { buffer_id: self.buffer.unwrap(), offset: self.range.start, size: wgt::BufferSize::new(self.range.end - self.range.start), }) } else { None } } fn set_format(&mut self, format: wgt::IndexFormat) { if self.format != format { self.format = format; self.is_dirty = true; } } fn set_buffer(&mut self, id: id::BufferId, range: Range) { self.buffer = Some(id); self.range = range; self.is_dirty = true; } } #[derive(Debug)] struct VertexState { buffer: Option, range: Range, stride: wgt::BufferAddress, rate: wgt::InputStepMode, is_dirty: bool, } impl VertexState { fn new() -> Self { VertexState { buffer: None, range: 0..0, stride: 0, rate: wgt::InputStepMode::Vertex, is_dirty: false, } } fn set_buffer(&mut self, buffer_id: id::BufferId, range: Range) { self.buffer = Some(buffer_id); self.range = range; self.is_dirty = true; } fn flush(&mut self, slot: u32) -> Option { if self.is_dirty { self.is_dirty = false; Some(RenderCommand::SetVertexBuffer { slot, buffer_id: self.buffer.unwrap(), offset: self.range.start, size: wgt::BufferSize::new(self.range.end - self.range.start), }) } else { None } } } #[derive(Debug)] struct BindState { bind_group: Option<(id::BindGroupId, id::BindGroupLayoutId)>, dynamic_offsets: Range, is_dirty: bool, } impl BindState { fn new() -> Self { BindState { bind_group: None, dynamic_offsets: 0..0, is_dirty: false, } } fn set_group( &mut self, bind_group_id: id::BindGroupId, layout_id: id::BindGroupLayoutId, dyn_offset: usize, dyn_count: usize, ) -> bool { match self.bind_group { Some((bg_id, _)) if bg_id == bind_group_id && dyn_count == 0 => false, _ => { self.bind_group = Some((bind_group_id, layout_id)); self.dynamic_offsets = dyn_offset..dyn_offset + dyn_count; self.is_dirty = true; true } } } } #[derive(Debug)] struct State { trackers: TrackerSet, index: IndexState, vertex: ArrayVec<[VertexState; MAX_VERTEX_BUFFERS]>, bind: ArrayVec<[BindState; MAX_BIND_GROUPS]>, raw_dynamic_offsets: Vec, flat_dynamic_offsets: Vec, used_bind_groups: usize, } impl State { fn vertex_limits(&self) -> (u32, u32) { let mut vertex_limit = !0; let mut instance_limit = !0; for vbs in &self.vertex { if vbs.stride == 0 { continue; } let limit = ((vbs.range.end - vbs.range.start) / vbs.stride) as u32; match vbs.rate { wgt::InputStepMode::Vertex => vertex_limit = vertex_limit.min(limit), wgt::InputStepMode::Instance => instance_limit = instance_limit.min(limit), } } (vertex_limit, instance_limit) } fn invalidate_group_from(&mut self, slot: usize) { for bind in self.bind[slot..].iter_mut() { if bind.bind_group.is_some() { bind.is_dirty = true; } } } fn set_bind_group( &mut self, slot: u8, bind_group_id: id::BindGroupId, layout_id: id::BindGroupLayoutId, offsets: &[wgt::DynamicOffset], ) { if self.bind[slot as usize].set_group( bind_group_id, layout_id, self.raw_dynamic_offsets.len(), offsets.len(), ) { self.invalidate_group_from(slot as usize + 1); } self.raw_dynamic_offsets.extend(offsets); } fn set_pipeline( &mut self, index_format: wgt::IndexFormat, vertex_strides: &[(wgt::BufferAddress, wgt::InputStepMode)], layout_ids: &[Stored], ) { self.index.set_format(index_format); for (vs, &(stride, step_mode)) in self.vertex.iter_mut().zip(vertex_strides) { if vs.stride != stride || vs.rate != step_mode { vs.stride = stride; vs.rate = step_mode; vs.is_dirty = true; } } self.used_bind_groups = layout_ids.len(); let invalid_from = self .bind .iter() .zip(layout_ids) .position(|(bs, layout_id)| match bs.bind_group { Some((_, bgl_id)) => bgl_id != layout_id.value, None => false, }); if let Some(slot) = invalid_from { self.invalidate_group_from(slot); } } fn flush_vertices(&mut self) -> impl Iterator + '_ { self.vertex .iter_mut() .enumerate() .flat_map(|(i, vs)| vs.flush(i as u32)) } fn flush_binds(&mut self) -> impl Iterator + '_ { for bs in self.bind[..self.used_bind_groups].iter() { if bs.is_dirty { self.flat_dynamic_offsets .extend_from_slice(&self.raw_dynamic_offsets[bs.dynamic_offsets.clone()]); } } self.bind .iter_mut() .take(self.used_bind_groups) .enumerate() .flat_map(|(i, bs)| { if bs.is_dirty { bs.is_dirty = false; Some(RenderCommand::SetBindGroup { index: i as u8, bind_group_id: bs.bind_group.unwrap().0, num_dynamic_offsets: (bs.dynamic_offsets.end - bs.dynamic_offsets.start) as u8, }) } else { None } }) } } impl Global { pub fn render_bundle_encoder_finish( &self, bundle_encoder: RenderBundleEncoder, desc: &wgt::RenderBundleDescriptor