Skip to content

Commit

Permalink
Code Cleanup: move swap_data and render_camera into render system (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomingTechDev authored May 26, 2022
1 parent 204567e commit f81a352
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include "runtime/function/input/input_system.h"

#include "runtime/function/render/render_camera.h"
#include "runtime/function/render/render_swap_context.h"
#include "runtime/function/render/render_system.h"

namespace Pilot
{
RenderCamera* CameraComponent::m_render_camera = nullptr;

void CameraComponent::postLoadResource(std::weak_ptr<GObject> parent_object)
{
m_parent_object = parent_object;
Expand All @@ -39,10 +39,10 @@ namespace Pilot
LOG_ERROR("invalid camera type");
}

if (m_render_camera)
{
m_render_camera->setFOVx(m_camera_res.m_parameter->m_fov);
}
RenderSwapContext& swap_context = g_runtime_global_context.m_render_system->getSwapContext();
CameraSwapData camera_swap_data;
camera_swap_data.fov_x = m_camera_res.m_parameter->m_fov;
swap_context.getLogicSwapData().camera_swap_data = camera_swap_data;
}

void CameraComponent::tick(float delta_time)
Expand Down Expand Up @@ -94,10 +94,11 @@ namespace Pilot

Matrix4x4 desired_mat = Math::makeLookAtMatrix(eye_pos, m_foward, m_up);

if (m_render_camera)
{
m_render_camera->setMainViewMatrix(desired_mat, CurrentCameraType::Motor);
}
RenderSwapContext& swap_context = g_runtime_global_context.m_render_system->getSwapContext();
CameraSwapData camera_swap_data;
camera_swap_data.camera_type = RenderCameraType::Motor;
camera_swap_data.view_matrix = desired_mat;
swap_context.getLogicSwapData().camera_swap_data = camera_swap_data;

Vector3 object_facing = m_foward - m_foward.dotProduct(Vector3::UNIT_Z) * Vector3::UNIT_Z;
Vector3 object_left = Vector3::UNIT_Z.crossProduct(object_facing);
Expand Down Expand Up @@ -136,9 +137,10 @@ namespace Pilot

Matrix4x4 desired_mat = Math::makeLookAtMatrix(camera_pos, camera_pos + camera_forward, camera_up);

if (m_render_camera)
{
m_render_camera->setMainViewMatrix(desired_mat, CurrentCameraType::Motor);
}
RenderSwapContext& swap_context = g_runtime_global_context.m_render_system->getSwapContext();
CameraSwapData camera_swap_data;
camera_swap_data.camera_type = RenderCameraType::Motor;
camera_swap_data.view_matrix = desired_mat;
swap_context.getLogicSwapData().camera_swap_data = camera_swap_data;
}
} // namespace Pilot
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ namespace Pilot

void tick(float delta_time) override;

static RenderCamera* m_render_camera;

private:
void tickFirstPersonCamera(float delta_time);
void tickThirdPersonCamera(float delta_time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#include "runtime/function/global/global_context.h"

#include "runtime/function/render/render_swap_context.h"
#include "runtime/function/render/render_system.h"

namespace Pilot
{
RenderSwapContext* MeshComponent::m_swap_context = nullptr;

void MeshComponent::postLoadResource(std::weak_ptr<GObject> parent_object)
{
m_parent_object = parent_object;
Expand Down Expand Up @@ -94,7 +93,7 @@ namespace Pilot
mesh_component.transform_desc.transform_matrix = object_transform_matrix;
}

m_swap_context->getLogicSwapData().addDirtyGameObject(
g_runtime_global_context.m_render_system->getSwapContext().getLogicSwapData().addDirtyGameObject(
GameObjectDesc {m_parent_object.lock()->getID(), mesh_components});

transform_component->setDirtyFlag(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace Pilot

void tick(float delta_time) override;

static RenderSwapContext* m_swap_context;

private:
META(Enable)
MeshComponentRes m_mesh_res;
Expand Down
3 changes: 0 additions & 3 deletions engine/source/runtime/function/global/global_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ namespace Pilot
RenderSystemInitInfo render_init_info;
render_init_info.window_system = m_window_system;
m_render_system->initialize(render_init_info);

MeshComponent::m_swap_context = &(m_render_system->getSwapContext());
CameraComponent::m_render_camera = &(*m_render_system->getRenderCamera());
}

void RuntimeGlobalContext::shutdownSystems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ namespace Pilot
directional_light_mesh_drawcall_batch;

// reorganize mesh
for (VulkanMeshNode& node : *(m_visiable_nodes.p_directional_light_visible_mesh_nodes))
for (RenderMeshNode& node : *(m_visiable_nodes.p_directional_light_visible_mesh_nodes))
{
auto& mesh_instanced = directional_light_mesh_drawcall_batch[node.ref_material];
auto& mesh_nodes = mesh_instanced[node.ref_mesh];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ namespace Pilot
std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;

// reorganize mesh
for (VulkanMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
for (RenderMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
{
auto& mesh_instanced = main_camera_mesh_drawcall_batch[node.ref_material];
auto& mesh_nodes = mesh_instanced[node.ref_mesh];
Expand Down Expand Up @@ -2688,7 +2688,7 @@ namespace Pilot
std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;

// reorganize mesh
for (VulkanMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
for (RenderMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
{
auto& mesh_instanced = main_camera_mesh_drawcall_batch[node.ref_material];
auto& mesh_nodes = mesh_instanced[node.ref_mesh];
Expand Down Expand Up @@ -2998,7 +2998,7 @@ namespace Pilot
m_global_render_resource->_storage_buffer._global_upload_ringbuffer_memory_pointer) +
perframe_dynamic_offset)) = m_particlebillboard_perframe_storage_buffer_object;

for (VulkanParticleBillboardNode& node : *(m_visiable_nodes.p_main_camera_visible_particlebillboard_nodes))
for (RenderParticleBillboardNode& node : *(m_visiable_nodes.p_main_camera_visible_particlebillboard_nodes))
{
uint32_t total_instance_count = static_cast<uint32_t>(node.positions.size());

Expand Down
2 changes: 1 addition & 1 deletion engine/source/runtime/function/render/passes/pick_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ namespace Pilot
std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> main_camera_mesh_drawcall_batch;

// reorganize mesh
for (VulkanMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
for (RenderMeshNode& node : *(m_visiable_nodes.p_main_camera_visible_mesh_nodes))
{
auto& mesh_instanced = main_camera_mesh_drawcall_batch[node.ref_material];
auto& model_nodes = mesh_instanced[node.ref_mesh];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ namespace Pilot
std::map<VulkanPBRMaterial*, std::map<VulkanMesh*, std::vector<MeshNode>>> point_lights_mesh_drawcall_batch;

// reorganize mesh
for (VulkanMeshNode& node : *(m_visiable_nodes.p_point_lights_visible_mesh_nodes))
for (RenderMeshNode& node : *(m_visiable_nodes.p_point_lights_visible_mesh_nodes))
{
auto& mesh_instanced = point_lights_mesh_drawcall_batch[node.ref_material];
auto& mesh_nodes = mesh_instanced[node.ref_mesh];
Expand Down
8 changes: 4 additions & 4 deletions engine/source/runtime/function/render/render_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Pilot
{
void RenderCamera::setCurrentCameraType(CurrentCameraType type)
void RenderCamera::setCurrentCameraType(RenderCameraType type)
{
std::lock_guard<std::mutex> lock_guard(m_view_matrix_mutex);
m_current_camera_type = type;
}

void RenderCamera::setMainViewMatrix(const Matrix4x4& view_matrix, CurrentCameraType type)
void RenderCamera::setMainViewMatrix(const Matrix4x4& view_matrix, RenderCameraType type)
{
std::lock_guard<std::mutex> lock_guard(m_view_matrix_mutex);
m_current_camera_type = type;
Expand Down Expand Up @@ -80,10 +80,10 @@ namespace Pilot
auto view_matrix = Matrix4x4::IDENTITY;
switch (m_current_camera_type)
{
case Pilot::CurrentCameraType::Editor:
case Pilot::RenderCameraType::Editor:
view_matrix = Math::makeLookAtMatrix(position(), position() + forward(), up());
break;
case Pilot::CurrentCameraType::Motor:
case Pilot::RenderCameraType::Motor:
view_matrix = m_view_matrices[MAIN_VIEW_MATRIX_INDEX];
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions engine/source/runtime/function/render/render_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Pilot
{
enum class CurrentCameraType : int
enum class RenderCameraType : int
{
Editor,
Motor
Expand All @@ -21,7 +21,7 @@ namespace Pilot
std::mutex m_view_matrix_mutex;

public:
CurrentCameraType m_current_camera_type {CurrentCameraType::Editor};
RenderCameraType m_current_camera_type {RenderCameraType::Editor};

static const Vector3 X, Y, Z;

Expand All @@ -38,8 +38,8 @@ namespace Pilot

std::vector<Matrix4x4> m_view_matrices {Matrix4x4::IDENTITY};

void setCurrentCameraType(CurrentCameraType type);
void setMainViewMatrix(const Matrix4x4& view_matrix, CurrentCameraType type = CurrentCameraType::Editor);
void setCurrentCameraType(RenderCameraType type);
void setMainViewMatrix(const Matrix4x4& view_matrix, RenderCameraType type = RenderCameraType::Editor);

void move(Vector3 delta);
void rotate(Vector2 delta);
Expand Down
6 changes: 3 additions & 3 deletions engine/source/runtime/function/render/render_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace Pilot
};

// nodes
struct VulkanMeshNode
struct RenderMeshNode
{
glm::mat4 model_matrix;
glm::mat4 joint_matrices[m_mesh_vertex_blending_max_joint_count];
Expand All @@ -234,15 +234,15 @@ namespace Pilot
bool enable_vertex_blending = false;
};

struct VulkanAxisNode
struct RenderAxisNode
{
glm::mat4 model_matrix {glm::mat4(1.0f)};
VulkanMesh* ref_mesh {nullptr};
uint32_t node_id;
bool enable_vertex_blending {false};
};

struct VulkanParticleBillboardNode
struct RenderParticleBillboardNode
{
std::vector<Vector4> positions;
};
Expand Down
10 changes: 5 additions & 5 deletions engine/source/runtime/function/render/render_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ namespace Pilot

struct VisiableNodes
{
std::vector<VulkanMeshNode>* p_directional_light_visible_mesh_nodes = nullptr;
std::vector<VulkanMeshNode>* p_point_lights_visible_mesh_nodes = nullptr;
std::vector<VulkanMeshNode>* p_main_camera_visible_mesh_nodes = nullptr;
VulkanAxisNode* p_axis_node = nullptr;
std::vector<VulkanParticleBillboardNode>* p_main_camera_visible_particlebillboard_nodes = nullptr;
std::vector<RenderMeshNode>* p_directional_light_visible_mesh_nodes {nullptr};
std::vector<RenderMeshNode>* p_point_lights_visible_mesh_nodes {nullptr};
std::vector<RenderMeshNode>* p_main_camera_visible_mesh_nodes {nullptr};
RenderAxisNode* p_axis_node {nullptr};
std::vector<RenderParticleBillboardNode>* p_main_camera_visible_particlebillboard_nodes {nullptr};
};

class RenderPass : public RenderPassBase
Expand Down
Loading

0 comments on commit f81a352

Please sign in to comment.