Skip to content

Commit

Permalink
Preallocate storage for HashMap and Vec in NodeGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
meirbon committed Sep 4, 2020
1 parent 53174d7 commit c0690b0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
66 changes: 39 additions & 27 deletions scene/src/graph/mod.rs
@@ -1,6 +1,6 @@
use animation::{Animation, Channel};
use crate::utils::*;
use crate::{Instance, ObjectRef};
use animation::{Animation, Channel};
use glam::*;
use rayon::prelude::*;

Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct AnimationDescriptor {
#[derive(Debug, Clone)]
pub struct SceneDescriptor {
pub nodes: Vec<NodeDescriptor>,
pub animations: Vec<AnimationDescriptor>
pub animations: Vec<AnimationDescriptor>,
}

#[cfg_attr(feature = "object_caching", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -489,24 +489,26 @@ impl NodeGraph {
scene_descriptor: &SceneDescriptor,
instances: &mut TrackedStorage<Instance>,
) {
let mut node_map: HashMap<u32, u32> = HashMap::new();
let mut node_map: HashMap<u32, u32> = HashMap::with_capacity(scene_descriptor.nodes.len());

let mut root_nodes = vec![];
let mut root_nodes = Vec::with_capacity(scene_descriptor.nodes.len());
for node in &scene_descriptor.nodes {
let node_id = self.load_node_descriptor(
&mut node_map,
node, scene_descriptor, instances,
);
let node_id =
self.load_node_descriptor(&mut node_map, node, scene_descriptor, instances);

root_nodes.push(node_id);
self.root_nodes.push(node_id);
}

for animation in &scene_descriptor.animations {
let channels = animation.channels.iter().map(|(node_desc_id, channel)| {
let node_id = node_map[&node_desc_id];
(node_id, channel.clone())
}).collect();
let channels = animation
.channels
.iter()
.map(|(node_desc_id, channel)| {
let node_id = node_map[&node_desc_id];
(node_id, channel.clone())
})
.collect();

let animation_id = self.animations.push(Animation {
name: animation.name.clone(),
Expand All @@ -516,7 +518,7 @@ impl NodeGraph {
time: 0.0,
});

self.set_active_animation(animation_id);
self.set_active_animation(animation_id).unwrap();
self.update_animation(0.0);
}
}
Expand All @@ -528,7 +530,9 @@ impl NodeGraph {
scene_descriptor: &SceneDescriptor,
instances: &mut TrackedStorage<Instance>,
) -> u32 {
let child_nodes: Vec<u32> = descriptor.child_nodes.iter()
let child_nodes: Vec<u32> = descriptor
.child_nodes
.iter()
.map(|child_node_descriptor| {
self.load_node_descriptor(
node_map,
Expand All @@ -539,20 +543,28 @@ impl NodeGraph {
})
.collect();

let skin_id = descriptor.skin.as_ref().map(|s| {
let joint_nodes = s.joint_nodes.iter().map(|joint_node_id| {
node_map[joint_node_id]
}).collect();

self.skins.push(Skin {
name: s.name.clone(),
joint_nodes,
inverse_bind_matrices: s.inverse_bind_matrices.clone(),
joint_matrices: vec![Mat4::identity(); s.inverse_bind_matrices.len()],
let skin_id = descriptor
.skin
.as_ref()
.map(|s| {
let joint_nodes = s
.joint_nodes
.iter()
.map(|joint_node_id| node_map[joint_node_id])
.collect();

self.skins.push(Skin {
name: s.name.clone(),
joint_nodes,
inverse_bind_matrices: s.inverse_bind_matrices.clone(),
joint_matrices: vec![Mat4::identity(); s.inverse_bind_matrices.len()],
})
})
}).map(|id| id as u32);
.map(|id| id as u32);

let meshes: Vec<NodeMesh> = descriptor.meshes.iter()
let meshes: Vec<NodeMesh> = descriptor
.meshes
.iter()
.map(|mesh| {
let instance_id = instances.allocate();
instances[instance_id].object_id = *mesh;
Expand Down Expand Up @@ -729,7 +741,7 @@ impl SceneGraph {
if let Some(graph) = self.sub_graphs.get(id as usize) {
for (_, node) in graph.nodes.iter() {
if let Some(skin_id) = node.skin {
skins.write().unwrap().erase(skin_id as usize);
skins.write().unwrap().erase(skin_id as usize).unwrap();
}

for mesh in &node.meshes {
Expand Down
35 changes: 13 additions & 22 deletions scene/src/loaders/gltf.rs
@@ -1,7 +1,5 @@
use crate::{
graph::{
AnimationDescriptor, NodeDescriptor, SceneDescriptor, SkinDescriptor,
},
graph::{AnimationDescriptor, NodeDescriptor, SceneDescriptor, SkinDescriptor},
AnimatedMesh, Flip, Material, MaterialList, Mesh, ObjectLoader, ObjectRef, SceneError,
TextureFormat,
};
Expand All @@ -12,8 +10,8 @@ use std::{
sync::RwLock,
};

use crate::graph::animation::{Animation, Channel, Method, Target};
use crate::graph::{Node, NodeGraph, NodeMesh, Skin};
use crate::graph::animation::{Channel, Method, Target};
use crate::graph::{Node, NodeGraph};
use crate::utils::TrackedStorage;
use crate::{material::Texture, LoadResult, TextureSource};
use gltf::animation::util::{MorphTargetWeights, ReadOutputs, Rotations};
Expand Down Expand Up @@ -512,11 +510,9 @@ impl ObjectLoader for GltfLoader {
for scene in document.scenes().into_iter() {
// Iterate over root nodes.
for node in scene.nodes() {
node_descriptors.push(load_node(
&gltf, &gltf_buffers, &meshes, &node,
));
node_descriptors.push(load_node(&gltf, &gltf_buffers, &meshes, &node));
}
};
}

Ok(LoadResult::Scene(SceneDescriptor {
nodes: node_descriptors,
Expand All @@ -536,7 +532,7 @@ fn load_node(
Transform::Matrix { matrix } => {
let (scale, rotation, translation) =
Mat4::from_cols_array_2d(&matrix).to_scale_rotation_translation();

(scale.into(), rotation, translation.into())
}
Transform::Decomposed {
Expand All @@ -545,12 +541,7 @@ fn load_node(
scale,
} => {
let scale = Vec3A::from(scale);
let rotation = Quat::from_xyzw(
rotation[0],
rotation[1],
rotation[2],
rotation[3],
);
let rotation = Quat::from_xyzw(rotation[0], rotation[1], rotation[2], rotation[3]);
let translation = Vec3A::from(translation);

(scale, rotation, translation)
Expand All @@ -564,14 +555,16 @@ fn load_node(

let maybe_skin = node.skin().map(|s| {
let name = s.name().map(|n| n.into()).unwrap_or(String::new());
let joint_nodes = s.joints().map(|joint_node| joint_node.index() as u32).collect();
let joint_nodes = s
.joints()
.map(|joint_node| joint_node.index() as u32)
.collect();

let mut inverse_bind_matrices = vec![];
let reader = s.reader(|buffer| gltf_buffers.buffer(&gltf, &buffer));
if let Some(ibm) = reader.read_inverse_bind_matrices() {
ibm.for_each(|m| {
inverse_bind_matrices
.push(Mat4::from_cols_array_2d(&m));
inverse_bind_matrices.push(Mat4::from_cols_array_2d(&m));
});
}

Expand All @@ -586,9 +579,7 @@ fn load_node(
if node.children().len() > 0 {
child_nodes.reserve(node.children().len());
for child in node.children() {
child_nodes.push(load_node(
gltf, gltf_buffers, meshes, &child,
));
child_nodes.push(load_node(gltf, gltf_buffers, meshes, &child));
}
}

Expand Down

0 comments on commit c0690b0

Please sign in to comment.