Skip to content

Commit

Permalink
Reduce unnecessary copies of memory data to improve rendering perform…
Browse files Browse the repository at this point in the history
…ance (#296) modified line endings
  • Loading branch information
kwbm authored and OlorinMedas committed Sep 10, 2022
1 parent 13845ed commit bfdab85
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "runtime/function/render/render_mesh.h"
#include "runtime/function/render/rhi/vulkan/vulkan_rhi.h"
#include "runtime/function/render/rhi/vulkan/vulkan_util.h"
#include "runtime/function/render/glm_wrapper.h"

#include "runtime/function/render/passes/directional_light_pass.h"

Expand Down Expand Up @@ -472,9 +473,9 @@ namespace Piccolo
{
struct MeshNode
{
glm::mat4 model_matrix;
glm::mat4 joint_matrices[m_mesh_vertex_blending_max_joint_count];
bool enable_vertex_blending;
const Matrix4x4* model_matrix {nullptr};
const Matrix4x4* joint_matrices {nullptr};
uint32_t joint_count {0};
};

std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>>
Expand All @@ -487,14 +488,11 @@ namespace Piccolo
auto& mesh_nodes = mesh_instanced[node.ref_mesh];

MeshNode temp;
temp.model_matrix = node.model_matrix;
temp.enable_vertex_blending = node.enable_vertex_blending;
temp.model_matrix = node.model_matrix;
if (node.enable_vertex_blending)
{
for (uint32_t i = 0; i < m_mesh_vertex_blending_max_joint_count; ++i)
{
temp.joint_matrices[i] = node.joint_matrices[i];
}
temp.joint_matrices = node.joint_matrices;
temp.joint_count = node.joint_count;
}

mesh_nodes.push_back(temp);
Expand Down Expand Up @@ -629,10 +627,10 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
perdrawcall_storage_buffer_object.mesh_instances[i].model_matrix =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix;
GLMUtil::fromMat4x4(*mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix);
perdrawcall_storage_buffer_object.mesh_instances[i].enable_vertex_blending =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.enable_vertex_blending ?
.joint_matrices ?
1.0 :
-1.0;
}
Expand All @@ -643,7 +641,7 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (!mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.enable_vertex_blending)
.joint_matrices)
{
least_one_enable_vertex_blending = false;
break;
Expand Down Expand Up @@ -676,14 +674,14 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.enable_vertex_blending)
.joint_matrices)
{
for (uint32_t j = 0; j < m_mesh_vertex_blending_max_joint_count; ++j)
for (uint32_t j = 0; j < mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_count; ++j)
{
per_drawcall_vertex_blending_storage_buffer_object
.joint_matrices[m_mesh_vertex_blending_max_joint_count * i + j] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j];
GLMUtil::fromMat4x4(mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j]);
}
}
}
Expand Down
59 changes: 27 additions & 32 deletions engine/source/runtime/function/render/passes/main_camera_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "runtime/function/render/render_helper.h"
#include "runtime/function/render/render_mesh.h"
#include "runtime/function/render/render_resource.h"
#include "runtime/function/render/glm_wrapper.h"

#include "runtime/function/render/rhi/vulkan/vulkan_rhi.h"
#include "runtime/function/render/rhi/vulkan/vulkan_util.h"
Expand Down Expand Up @@ -2416,9 +2417,9 @@ namespace Piccolo
{
struct MeshNode
{
glm::mat4 model_matrix;
glm::mat4 joint_matrices[m_mesh_vertex_blending_max_joint_count];
bool enable_vertex_blending;
const Matrix4x4* model_matrix {nullptr};
const Matrix4x4* joint_matrices {nullptr};
uint32_t joint_count {0};
};

std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;
Expand All @@ -2430,14 +2431,11 @@ namespace Piccolo
auto& mesh_nodes = mesh_instanced[node.ref_mesh];

MeshNode temp;
temp.model_matrix = node.model_matrix;
temp.enable_vertex_blending = node.enable_vertex_blending;
temp.model_matrix = node.model_matrix;
if (node.enable_vertex_blending)
{
for (uint32_t i = 0; i < m_mesh_vertex_blending_max_joint_count; ++i)
{
temp.joint_matrices[i] = node.joint_matrices[i];
}
temp.joint_matrices = node.joint_matrices;
temp.joint_count = node.joint_count;
}

mesh_nodes.push_back(temp);
Expand Down Expand Up @@ -2561,9 +2559,9 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
perdrawcall_storage_buffer_object.mesh_instances[i].model_matrix =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix;
GLMUtil::fromMat4x4(*mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix);
perdrawcall_storage_buffer_object.mesh_instances[i].enable_vertex_blending =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending ?
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices ?
1.0 :
-1.0;
}
Expand All @@ -2573,7 +2571,7 @@ namespace Piccolo
bool least_one_enable_vertex_blending = true;
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (!mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending)
if (!mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices)
{
least_one_enable_vertex_blending = false;
break;
Expand Down Expand Up @@ -2604,14 +2602,14 @@ namespace Piccolo
per_drawcall_vertex_blending_dynamic_offset));
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending)
if (mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices)
{
for (uint32_t j = 0; j < m_mesh_vertex_blending_max_joint_count; ++j)
for (uint32_t j = 0; j < mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_count; ++j)
{
per_drawcall_vertex_blending_storage_buffer_object
.joint_matrices[m_mesh_vertex_blending_max_joint_count * i + j] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j];
GLMUtil::fromMat4x4(mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j]);
}
}
}
Expand Down Expand Up @@ -2700,9 +2698,9 @@ namespace Piccolo
{
struct MeshNode
{
glm::mat4 model_matrix;
glm::mat4 joint_matrices[m_mesh_vertex_blending_max_joint_count];
bool enable_vertex_blending;
const Matrix4x4* model_matrix {nullptr};
const Matrix4x4* joint_matrices {nullptr};
uint32_t joint_count {0};
};

std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;
Expand All @@ -2714,14 +2712,11 @@ namespace Piccolo
auto& mesh_nodes = mesh_instanced[node.ref_mesh];

MeshNode temp;
temp.model_matrix = node.model_matrix;
temp.enable_vertex_blending = node.enable_vertex_blending;
temp.model_matrix = node.model_matrix;
if (node.enable_vertex_blending)
{
for (uint32_t i = 0; i < m_mesh_vertex_blending_max_joint_count; ++i)
{
temp.joint_matrices[i] = node.joint_matrices[i];
}
temp.joint_matrices = node.joint_matrices;
temp.joint_count = node.joint_count;
}

mesh_nodes.push_back(temp);
Expand Down Expand Up @@ -2845,9 +2840,9 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
perdrawcall_storage_buffer_object.mesh_instances[i].model_matrix =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix;
GLMUtil::fromMat4x4(*mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix);
perdrawcall_storage_buffer_object.mesh_instances[i].enable_vertex_blending =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending ?
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices ?
1.0 :
-1.0;
}
Expand All @@ -2857,7 +2852,7 @@ namespace Piccolo
bool least_one_enable_vertex_blending = true;
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (!mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending)
if (!mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices)
{
least_one_enable_vertex_blending = false;
break;
Expand Down Expand Up @@ -2888,14 +2883,14 @@ namespace Piccolo
per_drawcall_vertex_blending_dynamic_offset));
for (uint32_t i = 0; i < current_instance_count; ++i)
{
if (mesh_nodes[drawcall_max_instance_count * drawcall_index + i].enable_vertex_blending)
if (mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices)
{
for (uint32_t j = 0; j < m_mesh_vertex_blending_max_joint_count; ++j)
for (uint32_t j = 0; j < mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_count; ++j)
{
per_drawcall_vertex_blending_storage_buffer_object
.joint_matrices[m_mesh_vertex_blending_max_joint_count * i + j] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j];
GLMUtil::fromMat4x4(mesh_nodes[drawcall_max_instance_count * drawcall_index + i]
.joint_matrices[j]);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions engine/source/runtime/function/render/passes/pick_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "runtime/function/render/rhi/vulkan/vulkan_util.h"

#include "runtime/function/render/render_helper.h"
#include "runtime/function/render/glm_wrapper.h"

#include "runtime/function/render/passes/pick_pass.h"

Expand Down Expand Up @@ -439,9 +440,10 @@ namespace Piccolo

struct MeshNode
{
glm::mat4 model_matrix;
uint32_t node_id;
glm::mat4 joint_matrices[m_mesh_vertex_blending_max_joint_count];
const Matrix4x4* model_matrix {nullptr};
const Matrix4x4* joint_matrices {nullptr};
uint32_t joint_count {0};
uint32_t node_id;
};

std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;
Expand All @@ -454,13 +456,11 @@ namespace Piccolo

MeshNode temp;
temp.model_matrix = node.model_matrix;
temp.node_id = node.node_id;
temp.node_id = node.node_id;
if (node.ref_mesh->enable_vertex_blending)
{
for (uint32_t i = 0; i < m_mesh_vertex_blending_max_joint_count; ++i)
{
temp.joint_matrices[i] = node.joint_matrices[i];
}
temp.joint_matrices = node.joint_matrices;
temp.joint_count = node.joint_count;
}

model_nodes.push_back(temp);
Expand Down Expand Up @@ -651,7 +651,7 @@ namespace Piccolo
for (uint32_t i = 0; i < current_instance_count; ++i)
{
perdrawcall_storage_buffer_object.model_matrices[i] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix;
GLMUtil::fromMat4x4(*mesh_nodes[drawcall_max_instance_count * drawcall_index + i].model_matrix);
perdrawcall_storage_buffer_object.node_ids[i] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].node_id;
}
Expand Down Expand Up @@ -684,11 +684,11 @@ namespace Piccolo
per_drawcall_vertex_blending_dynamic_offset));
for (uint32_t i = 0; i < current_instance_count; ++i)
{
for (uint32_t j = 0; j < m_mesh_vertex_blending_max_joint_count; ++j)
for (uint32_t j = 0; j < mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_count; ++j)
{
per_drawcall_vertex_blending_storage_buffer_object
.joint_matrices[m_mesh_vertex_blending_max_joint_count * i + j] =
mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices[j];
GLMUtil::fromMat4x4(mesh_nodes[drawcall_max_instance_count * drawcall_index + i].joint_matrices[j]);
}
}
}
Expand Down
Loading

0 comments on commit bfdab85

Please sign in to comment.