Deduplicate vertices for model rendering

This commit is contained in:
Timothy Warren 2023-04-20 11:06:52 -04:00
parent e5a30c50a9
commit e61e15770e
2 changed files with 32 additions and 3 deletions

View File

@ -8,7 +8,6 @@ use ::std::collections::HashMap;
use ::std::collections::HashSet; use ::std::collections::HashSet;
use ::std::ffi::CStr; use ::std::ffi::CStr;
use ::std::fs::File; use ::std::fs::File;
use ::std::hash::{Hash, Hasher};
use ::std::io::BufReader; use ::std::io::BufReader;
use ::std::os::raw::c_void; use ::std::os::raw::c_void;
use ::std::ptr::copy_nonoverlapping as memcpy; use ::std::ptr::copy_nonoverlapping as memcpy;
@ -926,6 +925,8 @@ impl AppData {
|_| Ok(Default::default()), |_| Ok(Default::default()),
)?; )?;
let mut unique_vertices = HashMap::new();
for model in &models { for model in &models {
for index in &model.mesh.indices { for index in &model.mesh.indices {
let pos_offset = (3 * index) as usize; let pos_offset = (3 * index) as usize;
@ -944,8 +945,14 @@ impl AppData {
), ),
); );
if let Some(index) = unique_vertices.get(&vertex) {
self.indices.push(*index as u32);
} else {
let index = self.vertices.len();
unique_vertices.insert(vertex, index);
self.vertices.push(vertex); self.vertices.push(vertex);
self.indices.push(self.indices.len() as u32); self.indices.push(index as u32);
}
} }
} }

View File

@ -1,6 +1,7 @@
use ::anyhow::{anyhow, Result}; use ::anyhow::{anyhow, Result};
use ::log::*; use ::log::*;
use ::nalgebra_glm as glm; use ::nalgebra_glm as glm;
use ::std::hash::{Hash, Hasher};
use ::std::mem::size_of; use ::std::mem::size_of;
use ::thiserror::Error; use ::thiserror::Error;
use ::vulkanalia::prelude::v1_0::*; use ::vulkanalia::prelude::v1_0::*;
@ -145,6 +146,27 @@ impl Vertex {
} }
} }
impl PartialEq for Vertex {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos && self.color == other.color && self.tex_coord == other.tex_coord
}
}
impl Eq for Vertex {}
impl Hash for Vertex {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pos[0].to_bits().hash(state);
self.pos[1].to_bits().hash(state);
self.pos[2].to_bits().hash(state);
self.color[0].to_bits().hash(state);
self.color[1].to_bits().hash(state);
self.color[2].to_bits().hash(state);
self.tex_coord[0].to_bits().hash(state);
self.tex_coord[1].to_bits().hash(state);
}
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Resource Descriptors // Resource Descriptors
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------