From 5f2862f524e9741058a37c994e13a23f92730c29 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Tue, 11 Aug 2020 16:21:42 -0400 Subject: [PATCH] Remove bone vector allocation in hot loop --- engine/core/include/components.hpp | 2 ++ engine/core/src/engine.cpp | 13 +++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/engine/core/include/components.hpp b/engine/core/include/components.hpp index f7cd0d2..285e9c1 100755 --- a/engine/core/include/components.hpp +++ b/engine/core/include/components.hpp @@ -70,6 +70,8 @@ struct Transform { struct Renderable { AssetPtr mesh; std::vector> materials; + + std::vector temp_bone_data; }; struct Light { diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index afc3d9c..68462bf 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -532,24 +532,25 @@ void Engine::calculate_object(Scene& scene, Object object, const Object parent_o auto& mesh = scene.get(object); if(mesh.mesh && !mesh.mesh->bones.empty()) { + if(mesh.temp_bone_data.empty()) + mesh.temp_bone_data.resize(mesh.mesh->bones.size()); + for(auto& part : mesh.mesh->parts) { - std::vector bones(mesh.mesh->bones.size()); - if(scene.get(object).parent != NullObject && scene.has(scene.get(object).parent) && !scene.get(scene.get(object).parent).mesh->bones.empty()) { for(auto [i, ourBone] : utility::enumerate(mesh.mesh->bones)) { for(auto& theirBone : scene.get(scene.get(object).parent).mesh->bones) { if(ourBone.name == theirBone.name) - bones[i] = mesh.mesh->global_inverse_transformation * theirBone.local_transform * part.offset_matrices[ourBone.index]; + mesh.temp_bone_data[i] = mesh.mesh->global_inverse_transformation * theirBone.local_transform * part.offset_matrices[ourBone.index]; } } } else { calculate_bone(*mesh.mesh.handle, part, *mesh.mesh->root_bone); - for(auto [i, bone] : utility::enumerate(bones)) - bones[i] = mesh.mesh->bones[i].final_transform; + for(auto [i, bone] : utility::enumerate(mesh.temp_bone_data)) + mesh.temp_bone_data[i] = mesh.mesh->bones[i].final_transform; } - engine->get_gfx()->copy_buffer(part.bone_batrix_buffer, bones.data(), 0, bones.size() * sizeof(Matrix4x4)); + engine->get_gfx()->copy_buffer(part.bone_batrix_buffer, mesh.temp_bone_data.data(), 0, mesh.temp_bone_data.size() * sizeof(Matrix4x4)); } } }