Match upstream API changes for IndexFormat

This commit is contained in:
kejor 2020-11-08 22:45:55 -05:00
parent 2d87fd9067
commit 4513fb2b83
6 changed files with 33 additions and 25 deletions

View File

@ -86,7 +86,7 @@
],
depth_stencil_state: None,
vertex_state: (
index_format: Uint16,
index_format: None,
vertex_buffers: [],
),
sample_count: 1,

View File

@ -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<id::BufferId>,
format: wgt::IndexFormat,
pipeline_format: Option<wgt::IndexFormat>,
range: Range<wgt::BufferAddress>,
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<wgt::IndexFormat>,
vertex_strides: &[(wgt::BufferAddress, wgt::InputStepMode)],
layout_ids: &[id::Valid<id::BindGroupLayoutId>],
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<BufferSize>,
) {
span!(_guard, DEBUG, "RenderBundle::set_index_buffer");
bundle.base.commands.push(RenderCommand::SetIndexBuffer {
buffer_id,
index_format,
offset,
size,
});

View File

@ -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<BufferSize>,
},

View File

@ -208,6 +208,7 @@ impl OptionalState {
struct IndexState {
bound_buffer_view: Option<(id::Valid<id::BufferId>, Range<BufferAddress>)>,
format: IndexFormat,
pipeline_format: Option<IndexFormat>,
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<G: GlobalIdentityHandlerFactory> Global<G> {
}
}
// 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<G: GlobalIdentityHandlerFactory> Global<G> {
}
RenderCommand::SetIndexBuffer {
buffer_id,
index_format,
offset,
size,
} => {
@ -1211,6 +1202,8 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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<BufferSize>,
) {
span!(_guard, DEBUG, "RenderPass::set_index_buffer");
pass.base.commands.push(RenderCommand::SetIndexBuffer {
buffer_id,
index_format,
offset,
size,
});

View File

@ -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<IndexFormat>,
/// The format of any vertex buffers used with this pipeline.
pub vertex_buffers: Cow<'a, [VertexBufferDescriptor<'a>]>,
}
@ -240,7 +240,7 @@ pub struct RenderPipeline<B: hal::Backend> {
pub(crate) device_id: Stored<DeviceId>,
pub(crate) pass_context: RenderPassContext,
pub(crate) flags: PipelineFlags,
pub(crate) index_format: IndexFormat,
pub(crate) index_format: Option<IndexFormat>,
pub(crate) vertex_strides: Vec<(BufferAddress, InputStepMode)>,
pub(crate) life_guard: LifeGuard,
}

View File

@ -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,