Archived
1
Fork 0

Remove bone vector allocation in hot loop

This commit is contained in:
redstrate 2020-08-11 16:21:42 -04:00
parent 8b164b23d8
commit 5f2862f524
2 changed files with 9 additions and 6 deletions

View file

@ -70,6 +70,8 @@ struct Transform {
struct Renderable { struct Renderable {
AssetPtr<Mesh> mesh; AssetPtr<Mesh> mesh;
std::vector<AssetPtr<Material>> materials; std::vector<AssetPtr<Material>> materials;
std::vector<Matrix4x4> temp_bone_data;
}; };
struct Light { struct Light {

View file

@ -532,24 +532,25 @@ void Engine::calculate_object(Scene& scene, Object object, const Object parent_o
auto& mesh = scene.get<Renderable>(object); auto& mesh = scene.get<Renderable>(object);
if(mesh.mesh && !mesh.mesh->bones.empty()) { 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) { for(auto& part : mesh.mesh->parts) {
std::vector<Matrix4x4> bones(mesh.mesh->bones.size());
if(scene.get(object).parent != NullObject && scene.has<Renderable>(scene.get(object).parent) && !scene.get<Renderable>(scene.get(object).parent).mesh->bones.empty()) { if(scene.get(object).parent != NullObject && scene.has<Renderable>(scene.get(object).parent) && !scene.get<Renderable>(scene.get(object).parent).mesh->bones.empty()) {
for(auto [i, ourBone] : utility::enumerate(mesh.mesh->bones)) { for(auto [i, ourBone] : utility::enumerate(mesh.mesh->bones)) {
for(auto& theirBone : scene.get<Renderable>(scene.get(object).parent).mesh->bones) { for(auto& theirBone : scene.get<Renderable>(scene.get(object).parent).mesh->bones) {
if(ourBone.name == theirBone.name) 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 { } else {
calculate_bone(*mesh.mesh.handle, part, *mesh.mesh->root_bone); calculate_bone(*mesh.mesh.handle, part, *mesh.mesh->root_bone);
for(auto [i, bone] : utility::enumerate(bones)) for(auto [i, bone] : utility::enumerate(mesh.temp_bone_data))
bones[i] = mesh.mesh->bones[i].final_transform; 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));
} }
} }
} }