Spot works

This commit is contained in:
Confideo-IOM
2026-08-06 21:11:33 +01:00
parent 6d7d7571b5
commit f9b696f9e8
61 changed files with 701 additions and 418 deletions
@@ -8,31 +8,21 @@
#include "../../buffer/HydraShadowBuffer.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../buffer/HydraShadowBufferManager.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"
#include "../../task/renderer/HydraCreateCascadeShadowFrameBuffer.hpp"
#include "../../task/renderer/HydraCreateShadowFrameBuffer.hpp"
#include "../../actor/HydraActorManager.h"
void HydraSpotShadowSourceSystem::Initialise()
{
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
for(uint32_t i = 0; i < MAX_SHADOWS; i++)
{
m_available_shadows.push(i);
}
m_shadows.resize(MAX_SHADOWS);
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));
task->Intialise("ShadowBuffer", 1, MAX_SHADOWS * sizeof(HydraSpotShadowSourceComponent), &m_shadow_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
}
void HydraSpotShadowSourceSystem::InitialiseComponent(HydraID entity_id)
@@ -41,14 +31,15 @@ void HydraSpotShadowSourceSystem::InitialiseComponent(HydraID entity_id)
HydraECS *ecs = engine->ECS();
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_available_shadows.size() > 0 && "Run out of shadows");
uint32_t shad_id = m_available_shadows.front();
m_available_shadows.pop();
light_comp.shadow_map_id = shad_id;
light_comp.shadow_caster = 1;
}
HydraSpotShadowSourceComponent& shadow_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
HydraID shadow_id = GetEngine()->ShadowBufferManager()->AddSpotShadow(shadow_comp);
assert(shadow_id != INVALID_HYDRA_ID && "Add Spot Shadow - out of shadows!");
light_comp.shadow_map_id = shadow_id;
light_comp.shadow_caster = 1;
shadow_comp.shadow_id = shadow_id;
_InitialiseShadowMap(entity_id);
}
@@ -60,15 +51,9 @@ void HydraSpotShadowSourceSystem::UpdateSystem(float delta_t_seconds)
for (HydraID entity_id : Entities)
{
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
_UpdateShadowMap(entity_id);
HydraSpotShadowSourceComponent shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
m_shadows[light_comp.shadow_map_id] = shad_comp;
}
_WriteToGPU();
}
void HydraSpotShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
@@ -77,20 +62,16 @@ void HydraSpotShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
HydraECS *ecs = engine->ECS();
HydraPositionComponent pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
HydraSpotShadowSourceComponent &shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
HydraLightComponent light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
float view_size = 50.0f;
float view_near = 0.1f;
float view_near = 0.3f;
float view_far = 50.0f;
float PI = 3.1415927;
float TWO_PI = 6.2831854;
glm::mat4 light_rot = glm::eulerAngleXYZ(pos_comp.orientation.x, pos_comp.orientation.y, pos_comp.orientation.z);
glm::vec3 light_dir = glm::normalize(glm::vec3(light_rot * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
glm::vec3 camera_pos = GetEngine()->Camera()->GetPosition();
glm::vec3 light_pos = pos_comp.position;
glm::vec3 light_pos = pos_comp.position;
glm::vec3 target_pos = light_pos + light_dir;
glm::vec3 up_vector = glm::vec3(0.0f, 1.0f, 0.0f);
@@ -100,12 +81,15 @@ void HydraSpotShadowSourceSystem::_UpdateShadowMap(HydraID entity_id)
}
glm::mat4 view = glm::lookAt(light_pos, target_pos, up_vector);
glm::mat4 proj = glm::ortho(-view_size, view_size, -view_size, view_size, view_near, view_far);
float fovRad = glm::radians(55.0f);
glm::mat4 proj = glm::perspectiveFovRH<float>(fovRad, view_size, view_size, view_near, view_far);
// Save data to component
shad_comp.far_plane = view_far;
shad_comp.light_space_proj = proj;
shad_comp.light_space_view = view;
shad_comp.light_space_view = view;
GetEngine()->ShadowBufferManager()->UpdateSpotShadow(shad_comp.shadow_id, shad_comp);
}
@@ -116,11 +100,9 @@ void HydraSpotShadowSourceSystem::_InitialiseShadowMap(HydraID entity_id)
HydraTextureBufferManager *tex_man = engine->TextureBufferManager();
HydraSpotShadowSourceComponent &shad_comp = ecs->GetComponent<HydraSpotShadowSourceComponent>(entity_id);
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
shad_comp.entity_id = entity_id;
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
uint32_t depth_texture_id = 0;
@@ -132,16 +114,3 @@ void HydraSpotShadowSourceSystem::_InitialiseShadowMap(HydraID entity_id)
shad_comp.shadow_texture_id = depth_texture_id;
}
void HydraSpotShadowSourceSystem::_WriteToGPU()
{
HydraEngine *engine = GetEngine();
HydraECS *ecs = engine->ECS();
void *buffer = static_cast<void *>(m_shadows.data());
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
HydraID update_buffer_task_id = task_scheduler->CreateTask<HydraUpdateWholeGPUBufferTask>(HydraThreadAffinity::Render);
HydraUpdateWholeGPUBufferTask *task = static_cast<HydraUpdateWholeGPUBufferTask *>(task_scheduler->GetTask(update_buffer_task_id));
task->Intialise(m_shadow_buffer_id, buffer);
task_scheduler->WaitForTask(update_buffer_task_id);
}