diff --git a/player/tests/data/quad.ron b/player/tests/data/quad.ron index 9bba47dc7..10d6f1d17 100644 --- a/player/tests/data/quad.ron +++ b/player/tests/data/quad.ron @@ -86,7 +86,7 @@ ], depth_stencil_state: None, vertex_state: ( - index_format: Uint16, + index_format: None, vertex_buffers: [], ), sample_count: 1, diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 19c11c713..ba48b64eb 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -245,6 +245,7 @@ impl RenderBundleEncoder { } RenderCommand::SetIndexBuffer { buffer_id, + index_format, offset, size, } => { @@ -261,6 +262,7 @@ impl RenderBundleEncoder { Some(s) => offset + s.get(), None => buffer.size, }; + state.index.set_format(index_format); state.index.set_buffer(buffer_id, offset..end); } RenderCommand::SetVertexBuffer { @@ -520,14 +522,17 @@ impl RenderBundle { RenderCommand::SetPipeline(pipeline_id) => { let pipeline = pipeline_guard.get(pipeline_id).unwrap(); cmd_buf.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, + index_format, offset, size, } => { + index_type = conv::map_index_format(index_format); + let &(ref buffer, _) = buffer_guard .get(buffer_id) .unwrap() @@ -675,6 +680,7 @@ impl Resource for RenderBundle { struct IndexState { buffer: Option, format: wgt::IndexFormat, + pipeline_format: Option, range: Range, is_dirty: bool, } @@ -684,6 +690,7 @@ impl IndexState { Self { buffer: None, format: wgt::IndexFormat::default(), + pipeline_format: None, range: 0..0, is_dirty: false, } @@ -703,6 +710,7 @@ impl IndexState { self.is_dirty = false; Some(RenderCommand::SetIndexBuffer { buffer_id: self.buffer.unwrap(), + index_format: self.format, offset: self.range.start, size: wgt::BufferSize::new(self.range.end - self.range.start), }) @@ -883,12 +891,13 @@ impl State { fn set_pipeline( &mut self, - index_format: wgt::IndexFormat, + index_format: Option, vertex_strides: &[(wgt::BufferAddress, wgt::InputStepMode)], layout_ids: &[id::Valid], push_constant_layouts: &[wgt::PushConstantRange], ) { - self.index.set_format(index_format); + self.index.pipeline_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; @@ -1072,12 +1081,14 @@ pub mod bundle_ffi { pub extern "C" fn wgpu_render_bundle_set_index_buffer( bundle: &mut RenderBundleEncoder, buffer_id: id::BufferId, + index_format: wgt::IndexFormat, offset: BufferAddress, size: Option, ) { span!(_guard, DEBUG, "RenderBundle::set_index_buffer"); bundle.base.commands.push(RenderCommand::SetIndexBuffer { buffer_id, + index_format, offset, size, }); diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index 30c19fef7..c0e1154ae 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -41,6 +41,8 @@ pub enum DrawError { }, #[error("index {last_index} extends beyond limit {index_limit}")] IndexBeyondLimit { last_index: u32, index_limit: u32 }, + #[error("pipeline index format and buffer index format do not match")] + UnmatchedIndexFormats, } /// Error encountered when encoding a render command. @@ -112,6 +114,7 @@ pub enum RenderCommand { SetPipeline(id::RenderPipelineId), SetIndexBuffer { buffer_id: id::BufferId, + index_format: wgt::IndexFormat, offset: BufferAddress, size: Option, }, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index d8ab62f46..dda113a11 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -208,6 +208,7 @@ impl OptionalState { struct IndexState { bound_buffer_view: Option<(id::Valid, Range)>, format: IndexFormat, + pipeline_format: Option, limit: u32, } @@ -304,6 +305,11 @@ impl State { if self.blend_color == OptionalState::Required { return Err(DrawError::MissingBlendColor); } + if let Some(pipeline_index_format) = self.index.pipeline_format { + if pipeline_index_format != self.index.format { + return Err(DrawError::UnmatchedIndexFormats); + } + } Ok(()) } @@ -1156,24 +1162,8 @@ impl Global { } } - // Rebind index buffer if the index format has changed with the pipeline switch - if state.index.format != pipeline.index_format { - state.index.format = pipeline.index_format; - state.index.update_limit(); + state.index.pipeline_format = pipeline.index_format; - if let Some((buffer_id, ref range)) = state.index.bound_buffer_view { - let &(ref buffer, _) = buffer_guard[buffer_id].raw.as_ref().unwrap(); - - let range = hal::buffer::SubRange { - offset: range.start, - size: Some(range.end - range.start), - }; - let index_type = conv::map_index_format(state.index.format); - unsafe { - raw.bind_index_buffer(buffer, range, index_type); - } - } - } // Update vertex buffer limits for (vbs, &(stride, rate)) in state.vertex.inputs.iter_mut().zip(&pipeline.vertex_strides) @@ -1190,6 +1180,7 @@ impl Global { } RenderCommand::SetIndexBuffer { buffer_id, + index_format, offset, size, } => { @@ -1211,6 +1202,8 @@ impl Global { None => buffer.size, }; state.index.bound_buffer_view = Some((id::Valid(buffer_id), offset..end)); + + state.index.format = index_format; state.index.update_limit(); let range = hal::buffer::SubRange { @@ -1764,12 +1757,14 @@ pub mod render_ffi { pub extern "C" fn wgpu_render_pass_set_index_buffer( pass: &mut RenderPass, buffer_id: id::BufferId, + index_format: wgt::IndexFormat, offset: BufferAddress, size: Option, ) { span!(_guard, DEBUG, "RenderPass::set_index_buffer"); pass.base.commands.push(RenderCommand::SetIndexBuffer { buffer_id, + index_format, offset, size, }); diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index c4ca0e140..07e5119fd 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -151,7 +151,7 @@ pub struct VertexBufferDescriptor<'a> { #[cfg_attr(feature = "replay", derive(serde::Deserialize))] pub struct VertexStateDescriptor<'a> { /// The format of any index buffers used with this pipeline. - pub index_format: IndexFormat, + pub index_format: Option, /// The format of any vertex buffers used with this pipeline. pub vertex_buffers: Cow<'a, [VertexBufferDescriptor<'a>]>, } @@ -240,7 +240,7 @@ pub struct RenderPipeline { pub(crate) device_id: Stored, pub(crate) pass_context: RenderPassContext, pub(crate) flags: PipelineFlags, - pub(crate) index_format: IndexFormat, + pub(crate) index_format: Option, pub(crate) vertex_strides: Vec<(BufferAddress, InputStepMode)>, pub(crate) life_guard: LifeGuard, } diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index a3a29c8c8..97161107c 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -963,8 +963,7 @@ impl DepthStencilStateDescriptor { /// Format of indices used with pipeline. #[repr(C)] #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] -#[cfg_attr(feature = "trace", derive(Serialize))] -#[cfg_attr(feature = "replay", derive(Deserialize))] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum IndexFormat { /// Indices are 16 bit unsigned integers. Uint16 = 0,