show memory usage of loaded model(s)

This commit is contained in:
2023-10-23 04:50:08 +02:00
parent c06b7d9366
commit 2923cf1250
4 changed files with 40 additions and 9 deletions
+2 -2
View File
@@ -166,8 +166,8 @@ pub fn load_obj(obj_path: &PathBuf, file: std::fs::File) -> Result<Object, Box<d
let mut meshes = Vec::new(); let mut meshes = Vec::new();
let mut materials: HashMap<String, Material> = HashMap::new(); let mut materials: HashMap<String, Material> = HashMap::new();
let mut current_material: Option<Material> = None; let mut current_material: Option<Material> = None;
let mut min_aabb = glm::vec3(std::f32::MAX, std::f32::MAX, std::f32::MAX); let mut min_aabb = glm::vec3(f32::MAX, f32::MAX, f32::MAX);
let mut max_aabb = glm::vec3(std::f32::MIN, std::f32::MIN, std::f32::MIN); let mut max_aabb = glm::vec3(f32::MIN, f32::MIN, f32::MIN);
for line in reader.lines() { for line in reader.lines() {
let line = line?; let line = line?;
+30 -2
View File
@@ -9,6 +9,7 @@ pub struct Model {
pub meshes: Vec<Mesh>, pub meshes: Vec<Mesh>,
pub aabb: aabb::AABB, pub aabb: aabb::AABB,
pub scaling_factor: f32, pub scaling_factor: f32,
pub mem_usage: usize,
} }
// TODO: texture support, kept for reference // TODO: texture support, kept for reference
@@ -75,13 +76,18 @@ impl Model {
meshes.push(Mesh::new(&mesh.name, mesh.vertices, mesh.indices, mesh.material)); meshes.push(Mesh::new(&mesh.name, mesh.vertices, mesh.indices, mesh.material));
} }
Model { let mut model = Model {
id: state.get_next_id(), id: state.get_next_id(),
name: obj.name.to_owned(), name: obj.name.to_owned(),
aabb: obj.aabb, aabb: obj.aabb,
scaling_factor: scale, scaling_factor: scale,
meshes, meshes,
} mem_usage: 0,
};
model.set_mem_usage();
return model;
} }
pub fn draw(&self, shader: &Shader, draw_aabb: bool) { pub fn draw(&self, shader: &Shader, draw_aabb: bool) {
@@ -118,4 +124,26 @@ impl Model {
} }
self self
} }
fn set_mem_usage(&mut self) {
let mut size: usize = 0;
size += std::mem::size_of_val(self);
for mesh in &self.meshes {
size += std::mem::size_of_val(mesh);
size += std::mem::size_of::<importer::Material>();
for texture in &mesh.material.textures {
size += std::mem::size_of_val(&texture);
}
for vertex in &mesh.vertices {
size += std::mem::size_of_val(vertex);
}
for index in &mesh.indices {
size += std::mem::size_of_val(index);
}
}
self.mem_usage = size;
}
} }
+5 -4
View File
@@ -116,10 +116,11 @@ pub fn draw_main_menu_bar(ui: &imgui::Ui, state: &mut State, window: &mut glfw::
state.show_help_menu_about = !state.show_help_menu_about; state.show_help_menu_about = !state.show_help_menu_about;
} }
}); });
let fps = format!("FPS: {:.1}", 1.0 / delta_time); let mem = state.objects.iter().fold(0 as usize, |acc, m| acc + m.mem_usage) as f32;
let avail_size = [*ui.content_region_avail().get(0).unwrap() - ui.calc_text_size(&fps)[0], 0.0]; let mem_fps = format!("Mem: {:.1}MB | FPS: {:.1}", mem / (1024.0 * 1024.0), 1.0 / delta_time);
let avail_size = [*ui.content_region_avail().get(0).unwrap() - ui.calc_text_size(&mem_fps)[0], 0.0];
ui.dummy(avail_size); ui.dummy(avail_size);
ui.text(&fps); ui.text(&mem_fps);
}); });
} }
@@ -169,7 +170,7 @@ fn draw_object_hierarchy(ui: &imgui::Ui, state: &mut State, idx: usize) -> bool
if let Some(..) = ui.begin_table_with_sizing("Objects Table", 2, imgui::TableFlags::SIZING_STRETCH_PROP, [0.0, 0.0], 0.0) { if let Some(..) = ui.begin_table_with_sizing("Objects Table", 2, imgui::TableFlags::SIZING_STRETCH_PROP, [0.0, 0.0], 0.0) {
ui.table_next_row(); ui.table_next_row();
ui.table_next_column(); ui.table_next_column();
ui.tree_node_config(format!("{}###{}", object.name.as_str(), idx)) ui.tree_node_config(format!("{} ({:.1}MB)###{}", object.name.as_str(), object.mem_usage as f32 / (1024.0 * 1024.0), idx))
.build(|| { .build(|| {
for (j, mesh) in &mut object.meshes.iter_mut().enumerate() { for (j, mesh) in &mut object.meshes.iter_mut().enumerate() {
draw_mesh_hierarchy(ui, mesh, j); draw_mesh_hierarchy(ui, mesh, j);
+3 -1
View File
@@ -88,7 +88,9 @@ pub fn import_models_from_paths(paths: &Vec<PathBuf>, state: &mut ui::ui::State)
state.active_model = Some(m.id); state.active_model = Some(m.id);
if let Some(model_name) = filename { if let Some(model_name) = filename {
state.logger.log(&format!("Loaded model \"{}\"", model_name.to_str().unwrap()), log::LogLevel::Info); let msg = format!("Loaded model \"{}\"", model_name.to_str().unwrap());
println!("{}", msg);
state.logger.log(&msg, log::LogLevel::Info);
m.name = model_name.to_str().unwrap().to_string(); m.name = model_name.to_str().unwrap().to_string();
} }
state.objects.push(m); state.objects.push(m);