mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-25 16:24:24 +00:00
Implement index formats
This commit is contained in:
parent
20841859d1
commit
7f7bdd1ba1
@ -15,6 +15,7 @@ use crate::{
|
||||
all_buffer_stages, all_image_stages, FramebufferKey, RenderPassContext, RenderPassKey,
|
||||
},
|
||||
hub::{Storage, HUB},
|
||||
pipeline::IndexFormat,
|
||||
resource::TexturePlacement,
|
||||
swap_chain::{SwapChainLink, SwapImageEpoch},
|
||||
track::{DummyUsage, Stitch, TrackerSet},
|
||||
@ -355,6 +356,11 @@ pub fn command_encoder_begin_render_pass(
|
||||
depth_stencil: depth_stencil_attachment.map(|at| view_guard[at.attachment].format),
|
||||
};
|
||||
|
||||
let index_state = IndexState {
|
||||
bound_buffer_view: None,
|
||||
format: IndexFormat::Uint16,
|
||||
};
|
||||
|
||||
RenderPass::new(
|
||||
current_comb,
|
||||
Stored {
|
||||
@ -362,6 +368,7 @@ pub fn command_encoder_begin_render_pass(
|
||||
ref_count: cmb.life_guard.ref_count.clone(),
|
||||
},
|
||||
context,
|
||||
index_state,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ use crate::{
|
||||
conv,
|
||||
device::RenderPassContext,
|
||||
hub::HUB,
|
||||
pipeline::PipelineFlags,
|
||||
pipeline::{IndexFormat, PipelineFlags},
|
||||
resource::BufferUsageFlags,
|
||||
track::{Stitch, TrackerSet},
|
||||
BindGroupId, BufferId, Color, CommandBuffer, CommandBufferId, RenderPassId, RenderPipelineId, Stored,
|
||||
@ -31,6 +31,12 @@ enum DrawError {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IndexState {
|
||||
pub(crate) bound_buffer_view: Option<(BufferId, u32)>,
|
||||
pub(crate) format: IndexFormat,
|
||||
}
|
||||
|
||||
pub struct RenderPass<B: hal::Backend> {
|
||||
raw: B::CommandBuffer,
|
||||
cmb_id: Stored<CommandBufferId>,
|
||||
@ -38,6 +44,7 @@ pub struct RenderPass<B: hal::Backend> {
|
||||
binder: Binder,
|
||||
trackers: TrackerSet,
|
||||
blend_color_status: BlendColorStatus,
|
||||
index_state: IndexState,
|
||||
}
|
||||
|
||||
impl<B: hal::Backend> RenderPass<B> {
|
||||
@ -45,6 +52,7 @@ impl<B: hal::Backend> RenderPass<B> {
|
||||
raw: B::CommandBuffer,
|
||||
cmb_id: Stored<CommandBufferId>,
|
||||
context: RenderPassContext,
|
||||
index_state: IndexState,
|
||||
) -> Self {
|
||||
RenderPass {
|
||||
raw,
|
||||
@ -53,6 +61,7 @@ impl<B: hal::Backend> RenderPass<B> {
|
||||
binder: Binder::default(),
|
||||
trackers: TrackerSet::new(),
|
||||
blend_color_status: BlendColorStatus::Unused,
|
||||
index_state,
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,12 +131,14 @@ pub extern "C" fn wgpu_render_pass_set_index_buffer(
|
||||
let view = hal::buffer::IndexBufferView {
|
||||
buffer: &buffer.raw,
|
||||
offset: offset as u64,
|
||||
index_type: hal::IndexType::U16, //TODO?
|
||||
index_type: conv::map_index_format(pass.index_state.format),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
pass.raw.bind_index_buffer(view);
|
||||
}
|
||||
|
||||
pass.index_state.bound_buffer_view = Some((buffer_id, offset));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@ -286,6 +297,30 @@ pub extern "C" fn wgpu_render_pass_set_pipeline(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rebind index buffer if the index format has changed with the pipeline switch
|
||||
if pass.index_state.format != pipeline.index_format {
|
||||
pass.index_state.format = pipeline.index_format;
|
||||
|
||||
if let Some((buffer_id, offset)) = pass.index_state.bound_buffer_view {
|
||||
let buffer_guard = HUB.buffers.read();
|
||||
let buffer = pass
|
||||
.trackers
|
||||
.buffers
|
||||
.get_with_extended_usage(&*buffer_guard, buffer_id, BufferUsageFlags::INDEX)
|
||||
.unwrap();
|
||||
|
||||
let view = hal::buffer::IndexBufferView {
|
||||
buffer: &buffer.raw,
|
||||
offset: offset as u64,
|
||||
index_type: conv::map_index_format(pass.index_state.format),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
pass.raw.bind_index_buffer(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
@ -623,3 +623,10 @@ pub fn map_rasterization_state_descriptor(
|
||||
conservative: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_index_format(index_format: pipeline::IndexFormat) -> hal::IndexType {
|
||||
match index_format {
|
||||
pipeline::IndexFormat::Uint16 => hal::IndexType::U16,
|
||||
pipeline::IndexFormat::Uint32 => hal::IndexType::U32,
|
||||
}
|
||||
}
|
||||
|
@ -1450,6 +1450,7 @@ pub fn device_create_render_pipeline(
|
||||
layout_id: desc.layout,
|
||||
pass_context,
|
||||
flags,
|
||||
index_format: desc.vertex_buffer_state.index_format,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -301,4 +301,5 @@ pub struct RenderPipeline<B: hal::Backend> {
|
||||
pub(crate) layout_id: PipelineLayoutId,
|
||||
pub(crate) pass_context: RenderPassContext,
|
||||
pub(crate) flags: PipelineFlags,
|
||||
pub(crate) index_format: IndexFormat,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user