From e9bf4db43e278f4e1c296db88badb4667d99778f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 6 Oct 2023 15:46:02 -0700 Subject: [PATCH] deduplicate models --- src/main.rs | 181 +++++++++++++++++++++++++++++++++++++++++- src/model_graphics.rs | 22 ++++- 2 files changed, 198 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 761b8f2..bbd7fe1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -230,7 +230,7 @@ impl GlobalState{ Some(ModelGraphicsInstance{ transform: instance.transform.into(), normal_transform: Into::::into(instance.transform.matrix3).inverse().transpose(), - color: instance.color, + color:model_graphics::ModelGraphicsColor4::from(instance.color), }) } }).collect(); @@ -262,9 +262,182 @@ impl GlobalState{ }); } } + //check every model to see if it's using the same (texture,color) but has few instances, if it is combine it into one model + //1. collect unique instances of texture and color, note model id + //2. for each model id, check if removing it from the pool decreases both the model count and instance count by more than one + //3. transpose all models that stay in the set + + //best plan: benchmark set_bind_group, set_vertex_buffer, set_index_buffer and draw_indexed + //check if the estimated render performance is better by transposing multiple model instances into one model instance + + //for now: just deduplicate single models... + let mut deduplicated_models=Vec::with_capacity(indexed_models_len);//use indexed_models_len because the list will likely get smaller instead of bigger + let mut unique_texture_color=std::collections::HashMap::new();//texture->color->vec![(model_id,instance_id)] + for (model_id,model) in unique_texture_models.iter().enumerate(){ + //for now: filter out models with more than one instance + if 1 let mut entities = Vec::new(); @@ -393,7 +566,7 @@ fn get_instances_buffer_data(instances:&[ModelGraphicsInstance]) -> Vec { raw.extend_from_slice(AsRef::<[f32; 3]>::as_ref(&mi.normal_transform.z_axis)); raw.extend_from_slice(&[0.0]); //color - raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color)); + raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color.get())); raw.append(&mut v); } raw diff --git a/src/model_graphics.rs b/src/model_graphics.rs index 57bbdd8..8bf538f 100644 --- a/src/model_graphics.rs +++ b/src/model_graphics.rs @@ -27,9 +27,29 @@ pub struct ModelGraphicsSingleTexture{ pub entities: Vec>, pub texture: Option, } +#[derive(Clone,PartialEq)] +pub struct ModelGraphicsColor4(glam::Vec4); +impl ModelGraphicsColor4{ + pub const fn get(&self)->glam::Vec4{ + self.0 + } +} +impl From for ModelGraphicsColor4{ + fn from(value:glam::Vec4)->Self{ + Self(value) + } +} +impl std::hash::Hash for ModelGraphicsColor4{ + fn hash(&self,state:&mut H) { + for &f in self.0.as_ref(){ + u32::from_ne_bytes(f.to_ne_bytes()).hash(state); + } + } +} +impl Eq for ModelGraphicsColor4{} #[derive(Clone)] pub struct ModelGraphicsInstance{ pub transform:glam::Mat4, pub normal_transform:glam::Mat3, - pub color:glam::Vec4, + pub color:ModelGraphicsColor4, } \ No newline at end of file