Files
HydraV3/HydraEngine/source/ecs/systems/HydraShadowSourceSystem.cpp
T

137 lines
5.5 KiB
C++
Raw Normal View History

2026-07-25 16:40:34 +01:00
#include "HydraShadowSourceSystem.h"
2026-07-28 00:26:04 +01:00
#include "../../engine/HydraEngine.h"
#include "../../camera/HydraCamera.h"
#include "../HydraECS.h"
#include "../components/HydraShadowSourceComponent.h"
#include "../components/HydraPositionComponent.h"
#include "../components/HydraLightComponent.h"
#include "../../buffer/HydraShadowBuffer.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdatePartialGPUBufferTask.hpp"
#include "../../task/buffer/HydraCreateUnBoundTextureTask.hpp"
2026-07-28 19:02:45 +01:00
#include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp"
2026-07-28 00:26:04 +01:00
#include "../../actor/HydraActorManager.h"
2026-07-25 16:40:34 +01:00
void HydraShadowSourceSystem::Initialise()
{
2026-07-28 00:26:04 +01:00
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID create_gpu_buffer_task_id = task_scheduler->CreateTask<HydraCreateGPUBufferTask>(HydraThreadAffinity::Render);
HydraCreateGPUBufferTask *task = static_cast<HydraCreateGPUBufferTask *>(task_scheduler->GetTask(create_gpu_buffer_task_id));
2026-07-28 19:02:45 +01:00
task->Intialise("ShadowBuffer", 1, MAX_SHADOWS * sizeof(HydraShadowSourceComponent), &m_shadow_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
2026-07-28 00:26:04 +01:00
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
2026-07-25 16:40:34 +01:00
}
void HydraShadowSourceSystem::InitialiseComponent(HydraID entity_id)
2026-07-28 19:02:45 +01:00
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
switch (light_comp.light_type)
2026-07-28 00:26:04 +01:00
{
2026-07-28 19:02:45 +01:00
case (int)HYDRA_LIGHT_TYPE::HYDRA_LIGHT_DIRECTIONAL:
_InitialiseDirectionalLight(entity_id);
break;
2026-07-28 00:26:04 +01:00
}
2026-07-25 16:40:34 +01:00
}
2026-07-28 00:26:04 +01:00
void HydraShadowSourceSystem::UpdateSystem(float delta_t_seconds)
{
2026-07-28 19:02:45 +01:00
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
2026-07-28 00:26:04 +01:00
std::vector<HydraShadowSourceComponent> shadows;
2026-07-28 19:02:45 +01:00
for (HydraID entity_id : Entities)
2026-07-28 00:26:04 +01:00
{
2026-07-28 19:02:45 +01:00
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
switch (light_comp.light_type)
2026-07-28 00:26:04 +01:00
{
2026-07-28 19:02:45 +01:00
case (int)HYDRA_LIGHT_TYPE::HYDRA_LIGHT_DIRECTIONAL:
_UpdateDirectionalLight(entity_id);
break;
2026-07-28 00:26:04 +01:00
}
HydraShadowSourceComponent shad_comp = ecs->GetComponent<HydraShadowSourceComponent>(entity_id);
shadows.push_back(shad_comp);
}
_WriteToGPU(shadows);
}
void HydraShadowSourceSystem::_UpdateDirectionalLight(HydraID entity_id)
{
2026-07-28 19:02:45 +01:00
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
2026-07-28 00:26:04 +01:00
HydraPositionComponent pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
2026-07-28 19:02:45 +01:00
HydraShadowSourceComponent &shad_comp = ecs->GetComponent<HydraShadowSourceComponent>(entity_id);
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
float view_size = 5.0f;
float view_near = -5.0f;
float view_far = 5.0f;
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
float PI = 3.1415927;
float TWO_PI = 6.2831854;
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
glm::vec3 light_dir = glm::vec3(0, 0, -1);
glm::mat3 light_rot = glm::mat3(1);
glm::vec3 light_euler = pos_comp.orientation;
light_rot = glm::eulerAngleXYZ(light_euler.x, light_euler.y, light_euler.z);
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
light_dir = light_rot * light_dir;
light_dir = glm::normalize(light_dir);
glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition();
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
for (uint32_t i = 0; i < 6; i++)
{
glm::vec3 light_pos = camera_pos; // - (light_dir * light_distance);
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
glm::mat4 light_trans = glm::translate(light_pos * glm::vec3(-1, -1, -1));
glm::mat4 light_orient = glm::eulerAngleXYZ(light_euler.x, light_euler.y, light_euler.z);
glm::mat4 view = light_orient * light_trans;
glm::mat4 proj = glm::orthoRH<float>(view_size, -view_size, -view_size, view_size, -15000, 15000);
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
shad_comp.far_plane[i] = view_far;
shad_comp.near_plane[i] = view_near;
shad_comp.light_space_proj[i] = proj;
shad_comp.light_space_view[i] = view;
2026-07-28 00:26:04 +01:00
2026-07-28 19:02:45 +01:00
view_far *= 3.0f;
view_near = -view_far;
view_size *= 3.0f;
}
2026-07-28 00:26:04 +01:00
}
void HydraShadowSourceSystem::_InitialiseDirectionalLight(HydraID entity_id)
{
2026-07-28 19:02:45 +01:00
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
HydraTextureBufferManager *tex_man = engine->TextureBufferManager();
HydraShadowSourceComponent &shad_comp = ecs->GetComponent<HydraShadowSourceComponent>(entity_id);
2026-07-28 00:26:04 +01:00
shad_comp.entity_id = entity_id;
shad_comp.cascade_count = 6;
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
2026-07-28 19:02:45 +01:00
HydraID create_framebuffer_task_id = task_scheduler->CreateTask<HydraCreateShadowFrameBuffer>(HydraThreadAffinity::Render);
HydraCreateShadowFrameBuffer *fb_task = static_cast<HydraCreateShadowFrameBuffer *>(task_scheduler->GetTask(create_framebuffer_task_id));
fb_task->Initialise(&shad_comp.shadow_texture_id, &shad_comp.frame_buffer_id);
task_scheduler->WaitForTask(create_framebuffer_task_id);
2026-07-28 00:26:04 +01:00
}
void HydraShadowSourceSystem::_WriteToGPU(std::vector<HydraShadowSourceComponent> shadows)
{
2026-07-28 19:02:45 +01:00
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
2026-07-28 00:26:04 +01:00
void *buffer = static_cast<void *>(shadows.data());
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID update_buffer_task_id = task_scheduler->CreateTask<HydraUpdatePartialGPUBufferTask>(HydraThreadAffinity::Render);
HydraUpdatePartialGPUBufferTask *task = static_cast<HydraUpdatePartialGPUBufferTask *>(task_scheduler->GetTask(update_buffer_task_id));
2026-07-28 19:02:45 +01:00
task->Intialise(m_shadow_buffer_id, 0, sizeof(HydraShadowSourceComponent) * shadows.size(), buffer);
2026-07-28 00:26:04 +01:00
task_scheduler->WaitForTask(update_buffer_task_id);
}