From 953b17bc69f6bd956d9bd161046482c3343ba683 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 30 Oct 2023 16:31:17 -0700 Subject: [PATCH] change model index_format based on number of vertices --- src/graphics.rs | 67 ++++++++++++++++++++++++++----------------- src/model_graphics.rs | 12 +++++--- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index a1d106e..2d799e1 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -8,17 +8,31 @@ pub struct ModelUpdate{ color:Option, } -struct Entity { - index_count: u32, - index_buf: wgpu::Buffer, +struct Entity{ + index_count:u32, + index_buf:wgpu::Buffer, +} +fn create_entities(device:&wgpu::Device,entities:&Vec>)->Vec{ + entities.iter().map(|indices|{ + let index_buf=device.create_buffer_init(&wgpu::util::BufferInitDescriptor{ + label:Some("Index"), + contents:bytemuck::cast_slice(indices), + usage:wgpu::BufferUsages::INDEX, + }); + Entity{ + index_buf, + index_count:indices.len() as u32, + } + }).collect() } -struct ModelGraphics { - instances: Vec, - vertex_buf: wgpu::Buffer, - entities: Vec, - bind_group: wgpu::BindGroup, - model_buf: wgpu::Buffer, +struct ModelGraphics{ + entities:Vec, + model_buf:wgpu::Buffer, + vertex_buf:wgpu::Buffer, + bind_group:wgpu::BindGroup, + index_format:wgpu::IndexFormat, + instances:Vec, } pub struct GraphicsSamplers{ @@ -413,7 +427,6 @@ impl GraphicsState{ let models:Vec=deduplicated_models.into_iter().map(|model|{ let mut vertices = Vec::new(); let mut index_from_vertex = std::collections::HashMap::new();//:: - let mut entities = Vec::new(); //this mut be combined in a more complex way if the models use different render patterns per group let mut indices = Vec::new(); for group in model.groups { @@ -424,7 +437,7 @@ impl GraphicsState{ if let Some(&i)=index_from_vertex.get(&vertex_index){ indices.push(i); }else{ - let i=vertices.len() as u16; + let i=vertices.len(); let vertex=&model.unique_vertices[vertex_index as usize]; vertices.push(GraphicsVertex{ pos: model.unique_pos[vertex.pos as usize], @@ -439,11 +452,14 @@ impl GraphicsState{ } } } - entities.push(indices); ModelGraphicsSingleTexture{ instances:model.instances, + entities:if (u16::MAX as usize)wgpu::IndexFormat::Uint32, + crate::model_graphics::Entities::U16(_)=>wgpu::IndexFormat::Uint16, + }, + entities:match &model.entities{ + crate::model_graphics::Entities::U32(entities)=>create_entities(device,entities), + crate::model_graphics::Entities::U16(entities)=>create_entities(device,entities), + }, bind_group: model_bind_group, model_buf, }); @@ -952,9 +965,9 @@ impl GraphicsState{ rpass.set_bind_group(2, &model.bind_group, &[]); rpass.set_vertex_buffer(0, model.vertex_buf.slice(..)); - for entity in model.entities.iter() { - rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16); - rpass.draw_indexed(0..entity.index_count, 0, 0..model.instances.len() as u32); + for entity in model.entities.iter(){ + rpass.set_index_buffer(entity.index_buf.slice(..),model.index_format); + rpass.draw_indexed(0..entity.index_count,0,0..model.instances.len() as u32); } } diff --git a/src/model_graphics.rs b/src/model_graphics.rs index f1a5acb..384e7a1 100644 --- a/src/model_graphics.rs +++ b/src/model_graphics.rs @@ -21,11 +21,15 @@ pub struct IndexedModelGraphicsSingleTexture{ pub groups: Vec, pub instances:Vec, } +pub enum Entities{ + U32(Vec>), + U16(Vec>), +} pub struct ModelGraphicsSingleTexture{ - pub instances: Vec, - pub vertices: Vec, - pub entities: Vec>, - pub texture: Option, + pub instances:Vec, + pub vertices:Vec, + pub entities:Entities, + pub texture:Option, } #[derive(Clone,PartialEq)] pub struct ModelGraphicsColor4(glam::Vec4);