initial commit

This commit is contained in:
Confideo-IOM
2026-07-17 15:30:29 +01:00
commit 7e8ec5a825
416 changed files with 40308 additions and 0 deletions
@@ -0,0 +1,90 @@
#ifndef HYDRACOMPONENTARRAY
#define HYDRACOMPONENTARRAY
#include "../engine/HydraObject.h"
#include <array>
#include <unordered_map>
#include <mutex>
class IHydraComponentArray
{
public:
virtual ~IHydraComponentArray() = default;
virtual void EntityDeleted(HydraID entity_id) = 0;
};
template<typename T>
class HydraComponentArray : public IHydraComponentArray
{
private:
std::array<T, MAX_ACTORS> m_components;
std::unordered_map<HydraID, HydraID> m_entity_to_component;
std::unordered_map<HydraID, HydraID> m_component_to_entity;
std::mutex m_mutex;
uint32_t m_component_count = 0;
public:
void RemoveComponent(HydraID entity_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_entity_to_component.find(entity_id) == m_entity_to_component.end() && "Trying to remove a component that doesnt exist");
HydraID component_index = m_entity_to_component[entity_id];
HydraID last_component_index = m_component_count -1;
HydraID last_entity_id = m_component_to_entity[last_component_index];
if(last_component_index != component_index)
{
m_components[component_index] = m_components[last_component_index];
m_entity_to_component[last_entity_id] = component_index;
m_components[last_component_index] = {};
}
else
{
m_components[component_index] = {};
}
m_entity_to_component.erase(entity_id);
m_component_to_entity.erase(component_index);
}
void AddComponent(HydraID entity_id, T component)
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_entity_to_component.find(entity_id) == m_entity_to_component.end() && "Trying to add a component that already exist");
m_components[m_component_count] = component;
m_entity_to_component.insert({entity_id, m_component_count});
m_component_to_entity.insert({m_component_count, entity_id});
m_component_count++;
}
bool HasComponent(HydraID entity_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_entity_to_component.find(entity_id) != m_entity_to_component.end();
}
T& GetComponent(HydraID entity_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_entity_to_component.find(entity_id) != m_entity_to_component.end() && "Trying to get a component that doesnt exist");
HydraID component_index = m_entity_to_component[entity_id];
return m_components[component_index];
}
void EntityDeleted(HydraID entity_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
if(m_entity_to_component.find(entity_id) != m_entity_to_component.end())
{
RemoveComponent(entity_id);
}
}
};
#endif /* HYDRACOMPONENTARRAY */
@@ -0,0 +1,83 @@
#ifndef HYDRAECSCOMPONENTMANAGER
#define HYDRAECSCOMPONENTMANAGER
#include "../engine/HydraObject.h"
#include "HydraComponentArray.h"
#include <unordered_map>
#include <memory>
using ComponentTypeID = uint8_t;
class HydraComponentManager :public HydraObject
{
private:
std::unordered_map<std::string, std::shared_ptr<IHydraComponentArray>> m_component_arrays;
std::unordered_map<std::string, ComponentTypeID> m_component_types;
ComponentTypeID m_current_component_type_id = 0;
template<typename T>
std::shared_ptr<HydraComponentArray<T>> _GetComponentArray()
{
std::string type_name = std::string(typeid(T).name());
assert(m_component_types.find(type_name) != m_component_types.end() && "Component type does not exist");
return std::static_pointer_cast<HydraComponentArray<T>>(m_component_arrays[type_name]);
}
public:
template<typename T>
ComponentTypeID RegisterComponentType()
{
std::string type_name = std::string(typeid(T).name());
assert(m_component_types.find(type_name) == m_component_types.end() && "Component name already in use");
m_component_types.insert({type_name, m_current_component_type_id});
m_component_arrays.insert({type_name, std::make_shared<HydraComponentArray<T>>()});
return m_current_component_type_id++;
}
template<typename T>
ComponentTypeID GetComponentTypeID()
{
std::string type_name = std::string(typeid(T).name());
assert(m_component_types.find(type_name) != m_component_types.end() && "Component type does not exist");
return m_component_types[type_name];
}
template<typename T>
void AddComponent(HydraID entity_id, T component)
{
_GetComponentArray<T>()->AddComponent(entity_id, component);
}
template<typename T>
T& GetComponent(HydraID entity_id)
{
return _GetComponentArray<T>()->GetComponent(entity_id);
}
template<typename T>
bool HasComponent(HydraID entity_id)
{
return _GetComponentArray<T>()->HasComponent(entity_id);
}
template<typename T>
void RemoveComponent(HydraID entity_id)
{
_GetComponentArray<T>()->RemoveComponent(entity_id);
}
void EntityDeleted(HydraID entity_id)
{
for(auto comp_array : m_component_arrays)
{
(*comp_array.second).EntityDeleted(entity_id);
}
}
};
#endif /* HYDRACOMPONENTMANAGER */
+17
View File
@@ -0,0 +1,17 @@
#include "HydraECS.h"
void HydraECS::Init()
{
m_component_manager = std::make_unique<HydraComponentManager>();
m_entity_manager = std::make_unique<HydraEntityManager>();
m_system_manager = std::make_unique<HydraSystemManager>();
}
void HydraECS::Shutdown()
{
m_system_manager->Shutdown();
m_component_manager.release();
m_entity_manager.release();
m_system_manager.release();
}
+105
View File
@@ -0,0 +1,105 @@
#ifndef HYDRAECS
#define HYDRAECS
#include "../engine/HydraObject.h"
#include "HydraComponentManager.h"
#include "HydraEntityManager.h"
#include "HydraSystemManager.h"
#include <memory>
class HydraECS : HydraObject
{
private:
std::unique_ptr<HydraEntityManager> m_entity_manager;
std::unique_ptr<HydraComponentManager> m_component_manager;
std::unique_ptr<HydraSystemManager> m_system_manager;
public:
void Init();
void Shutdown();
HydraID CreateEntity()
{
return m_entity_manager->CreateEntity();
}
void DestroyEntity(HydraID entity_id)
{
m_entity_manager->DeleteEntity(entity_id);
m_component_manager->EntityDeleted(entity_id);
m_system_manager->EntityDeleted(entity_id);
}
template <typename T>
void RegisterComponentType()
{
m_component_manager->RegisterComponentType<T>();
}
template <typename T>
HydraID GetComponentType()
{
return m_component_manager->GetComponentTypeID<T>();
}
template <typename T>
void AddComponent(HydraID entity_id, T component)
{
// Add teh component to the entity
m_component_manager->AddComponent<T>(entity_id, component);
// Get the current entity signature and update it to include
// the new component
auto signature = m_entity_manager->GetSignature(entity_id);
signature.set(m_component_manager->GetComponentTypeID<T>(), true);
m_entity_manager->SetSignature(entity_id, signature);
// Tell the sytem manager that the entities signature has changed
m_system_manager->EntitySignatureChanged(entity_id, signature);
}
template <typename T>
void RemoveComponent(HydraID entity_id)
{
// Remove the component from the entity
m_component_manager->RemoveComponent<T>(entity_id);
// Get the current signature, remove the component from it and update
auto signature = m_entity_manager->GetSignature(entity_id);
signature.set(m_component_manager->GetComponentTypeID<T>(), false);
m_entity_manager->SetSignature(entity_id, signature);
// Tell the system manager that the entitys signature has changed
m_system_manager->EntitySignatureChanged(entity_id, signature);
}
template <typename T>
bool HasComponent(HydraID entity_id)
{
return m_component_manager->HasComponent<T>(entity_id);
}
template <typename T>
T &GetComponent(HydraID entity_id)
{
return m_component_manager->GetComponent<T>(entity_id);
}
template <typename T>
std::shared_ptr<T> RegisterSystem()
{
return m_system_manager->RegisterSystem<T>();
}
template <typename T>
void SetSystemSignature(HydraSignature signature)
{
m_system_manager->SetSignature<T>(signature);
}
template <typename T>
std::shared_ptr<T> GetSystem()
{
return m_system_manager->GetSystem<T>();
}
void InitialiseComponents()
{
m_system_manager->InitialiseComponents();
}
};
#endif /* HYDRAECS */
+15
View File
@@ -0,0 +1,15 @@
#ifndef HYDRAENTITY
#define HYDRAENTITY
#include "../engine/HydraObject.h"
struct HydraEntity
{
HydraID entity_id = INVALID_HYDRA_ID;
HydraSignature signature;
};
#endif /* HYDRAENTITY */
@@ -0,0 +1,59 @@
#ifndef HYDRAENTITYMANAGER
#define HYDRAENTITYMANAGER
#include "../engine/HydraObject.h"
#include "HydraEntity.h"
#include <array>
#include <mutex>
#include <queue>
class HydraEntityManager : public HydraObject
{
private:
std::array<HydraSignature, MAX_ACTORS> m_entities;
std::mutex m_mutex;
std::queue<HydraID> m_available_entity_ids;
uint32_t m_entities_created;
public:
HydraEntityManager()
{
for(uint32_t i = 0; i < MAX_ACTORS; i ++)
{
m_available_entity_ids.push(i);
}
}
HydraID CreateEntity()
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(m_entities_created < MAX_ACTORS && "Too many entities created");
HydraID entity_id = m_available_entity_ids.front();
m_available_entity_ids.pop();
m_entities_created++;
return entity_id;
}
void DeleteEntity(HydraID entity_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
assert(entity_id < MAX_ACTORS && "Entity ID out of range");
m_available_entity_ids.push(entity_id);
m_entities[entity_id].reset();
m_entities_created--;
}
void SetSignature(HydraID entity_id, HydraSignature signature)
{
assert(entity_id < MAX_ACTORS && "Invalid entity it");
m_entities[entity_id] = signature;
}
HydraSignature GetSignature(HydraID entity_id)
{
assert(entity_id < MAX_ACTORS && "Invalid entity it");
return m_entities[entity_id];
}
};
#endif /* HYDRAENTITYMANAGER */
+18
View File
@@ -0,0 +1,18 @@
#include "HydraSystem.h"
void HydraSystem::Update(float delta_t_seconds)
{
UpdateSystem(delta_t_seconds);
}
void HydraSystem::InitialiseComponents()
{
for(auto it = EntitiesToCreate.begin(); it != EntitiesToCreate.end(); ++it)
{
HydraID actor_id = *it;
InitialiseComponent(actor_id);
}
EntitiesToCreate.clear();
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef HYDRASYSTEM
#define HYDRASYSTEM
#include "../engine/HydraObject.h"
#include <set>
class HydraSystem : public HydraObject
{
public:
std::set<HydraID> Entities;
std::set<HydraID> EntitiesToCreate;
void Update(float delta_t_seconds);
void InitialiseComponents();
virtual void Shutdown() {};
virtual void Initialise(){};
protected:
virtual void UpdateSystem(float delta_t_seconds) = 0;
virtual void InitialiseComponent(HydraID actor_id) {};
};
#endif /* HYDRASYSTEM */
@@ -0,0 +1 @@
#include "HydraSystemManager.h"
+106
View File
@@ -0,0 +1,106 @@
#ifndef HYDRASYSTEMMANAGER
#define HYDRASYSTEMMANAGER
#include "../engine/HydraObject.h"
#include "HydraSystem.h"
#include "HydraEntity.h"
//#include "../ecssystems/HydraECSRenderSystem.h"
#include <memory>
#include <unordered_map>
class HydraSystemManager : public HydraObject
{
private:
std::unordered_map<std::string, HydraSignature> m_signatures{};
std::unordered_map<std::string, std::shared_ptr<HydraSystem>> m_systems{};
public:
void Shutdown()
{
for (auto const& pair : m_systems)
{
auto const& system = pair.second;
system->Shutdown();
}
}
template <typename T> std::shared_ptr<T> GetSystem()
{
std::string type_name = std::string(typeid(T).name());
auto it = m_systems.find(type_name);
assert(it!= m_systems.end() && "System not found.");
return std::static_pointer_cast<T>( it->second);
}
template <typename T>
std::shared_ptr<T> RegisterSystem()
{
std::string type_name = std::string(typeid(T).name());
assert(m_systems.find(type_name) == m_systems.end() && "Registering system more than once.");
// Create a pointer to the system and return it so it can be used externally
auto system = std::make_shared<T>();
system->Initialise();
m_systems.insert({type_name, system});
return system;
}
template<typename T> void SetSignature(HydraSignature signature)
{
std::string type_name = std::string(typeid(T).name());
assert(m_systems.find(type_name) != m_systems.end() && "System used before registered.");
// Set the signature for this system
m_signatures.insert({type_name, signature});
}
void EntitySignatureChanged(HydraID entity_id, HydraSignature signature)
{
// Notify each system that an entity's signature changed
for (auto const& pair : m_systems)
{
//auto const& type = pair.first;
std::string type = pair.first;
auto const& system = pair.second;
auto const& system_signature = m_signatures[type];
// Entity signature matches system signature - insert into set
if ((signature & system_signature) == system_signature)
{
system->EntitiesToCreate.insert(entity_id);
system->Entities.insert(entity_id);
}
// Entity signature does not match system signature - erase from set
else
{
system->Entities.erase(entity_id);
system->EntitiesToCreate.erase(entity_id);
}
}
}
void EntityDeleted(HydraID entity_id)
{
for (auto const& pair : m_systems)
{
auto const& system = pair.second;
system->Entities.erase(entity_id);
system->EntitiesToCreate.erase(entity_id);
}
}
void InitialiseComponents()
{
for (auto const& pair : m_systems)
{
auto const& system = pair.second;
system->InitialiseComponents();
}
}
};
#endif /* HYDRASYSTEMMANAGER */
@@ -0,0 +1,14 @@
#ifndef HYDRAECSLIGHTCOMPONENT
#define HYDRAECSLIGHTCOMPONENT
#include "../../engine/HydraEngine.h"
struct HydraLightComponent
{
glm::vec4 light_colour;
float light_intensity;
HydraID entity_id;
uint32_t light_type;
float padding[1];
};
#endif /* HYDRAECSLIGHTCOMPONENT */
@@ -0,0 +1,11 @@
#ifndef HYDRAECSMATERIALCOMPONENT
#define HYDRAECSMATERIALCOMPONENT
#include "../../engine/HydraEngine.h"
struct HydraMaterialComponent
{
HydraID material_id = INVALID_HYDRA_ID;
};
#endif /* HYDRAECSMATERIALCOMPONENT */
@@ -0,0 +1,17 @@
#ifndef HYDRAECSMESHCOMPONENT
#define HYDRAECSMESHCOMPONENT
#include "../../engine/HydraObject.h"
struct HydraMeshComponent
{
HydraID mesh_id = INVALID_HYDRA_ID;
HydraID vertex_buffer_id = INVALID_HYDRA_ID;
HYDRA_STATE mesh_state = HYDRA_STATE::Empty;
};
#endif /* HYDRAECSMESHCOMPONENT */
@@ -0,0 +1,18 @@
#ifndef HYDRAECSPHYSICSCOMPONENT
#define HYDRAECSPHYSICSCOMPONENT
#include "../engine/HydraEngine.h"
//#include "../physics/HydraPhysicsDefinition.h"
struct HydraPhysicsComponent
{
//
HYDRA_STATE physics_state = HYDRA_STATE::Empty;
// HydraPhysicsDefinitionStruct physics_definition;
};
#endif /* HYDRAECSPHYSICSCOMPONENT */
@@ -0,0 +1,16 @@
#ifndef HYDRAECSPOSITIONCOMPONENT
#define HYDRAECSPOSITIONCOMPONENT
struct HydraPositionComponent
{
glm::dvec3 position = {};
glm::vec3 orientation = {};
glm::vec3 scale = {1,1,1};
glm::vec3 velocity = {};
glm::mat4 transform = glm::mat4(1);
glm::mat4 parent_transform = glm::mat4(1);
bool needs_update = true;
};
#endif /* HYDRAECSPOSITIONCOMPONENT */
@@ -0,0 +1,10 @@
#ifndef HYDRAECSRENDERABLECOMPONENT
#define HYDRAECSRENDERABLECOMPONENT
#include "../../engine/HydraEngine.h"
struct HydraRenderableComponent
{
HydraSignature renderable_types;
};
#endif /* HYDRAECSRENDERABLECOMPONENT */
@@ -0,0 +1,18 @@
#ifndef HYDRAECSSHADOWCOMPONENT
#define HYDRAECSSHADOWCOMPONENT
//#include "../helper/VulkanHelper.h"
//#include "../buffer/HydraShadowMapTransform.h"
struct HydrahadowComponent
{
// HydraID shadow_map_id; //4
// HydraID shadow_map_index; //8
// float padding0; //12
// float padding1; //16
// HydraShadowMapTransform shadow_transforms[6]; //880 (144*6)
// float padding[20]; //960
};
#endif /* HYDRAECSSHADOWCOMPONENT */
@@ -0,0 +1,11 @@
#ifndef HYDRATEXTURECOMPONENT
#define HYDRATEXTURECOMPONENT
#include "../engine/HydraEngine.h"
struct HydraTextureComponent
{
HydraID diffuse_texture_id = INVALID_HYDRA_ID;
};
#endif /* HYDRATEXTURECOMPONENT */
@@ -0,0 +1,55 @@
#include "HydraLightSystem.h"
#include "../../engine/HydraEngine.h"
#include "../HydraECS.h"
#include "../components/HydraLightComponent.h"
#include "../components/HydraPositionComponent.h"
#include "../../buffer/HydraLightBufferManager.h"
#include "../../buffer/HydraLightBuffer.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdatePartialGPUBufferTask.hpp"
#include "../../actor/HydraActorManager.h"
#include <GLMIncludes.h>
void HydraLightSystem::Initialise()
{
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));
task->Intialise("LightBuffer", 1, MAX_LIGHTS * sizeof(HydraLightComponent), &m_light_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
}
uint32_t HydraLightSystem::GetLightCount()
{
return m_lights.size();
}
void HydraLightSystem::InitialiseComponent(HydraID entity_id)
{
HydraLightBufferManager *light_man = GetEngine()->LightBufferManager();
uint32_t light_count = light_man->GetLightCount();
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(entity_id);
HydraLightComponent &light_comp = ecs->GetComponent<HydraLightComponent>(entity_id);
m_lights.push_back(light_comp);
}
void HydraLightSystem::UpdateSystem(float delta_t_seconds)
{
_WriteToGPU();
}
void HydraLightSystem::_WriteToGPU()
{
void *buffer = static_cast<void *>(m_lights.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));
task->Intialise(m_light_buffer_id, 0, sizeof(HydraLightComponent) * m_lights.size(), buffer);
task_scheduler->WaitForTask(update_buffer_task_id);
}
@@ -0,0 +1,27 @@
#ifndef HYDRALIGHTSYSTEM
#define HYDRALIGHTSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include "../components/HydraLightComponent.h"
#include <GLMIncludes.h>
#include <queue>
#include <vector>
class HydraLightSystem : public HydraSystem
{
public:
void Initialise() override;
uint32_t GetLightCount();
protected:
void InitialiseComponent(HydraID entity_id) override;
virtual void UpdateSystem(float delta_t_seconds);
private:
void _WriteToGPU();
HydraID m_light_buffer_id = INVALID_HYDRA_ID;
std::vector<HydraLightComponent> m_lights;
};
#endif /* HYDRALIGHTSYSTEM */
@@ -0,0 +1,57 @@
#include "HydraMaterialSystem.h"
#include "../../engine/HydraEngine.h"
#include "../HydraECS.h"
#include "../components/HydraMaterialComponent.h"
#include "../../buffer/HydraMaterialBufferManager.h"
#include "../../buffer/HydraTextureBufferManager.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp"
#include "../../actor/HydraActorManager.h"
void HydraMaterialSystem::Initialise()
{
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));
task->Intialise("MaterialBuffer", 1, MAX_MATERIALS * sizeof(HydraMaterialBuffer), &m_material_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
create_gpu_buffer_task_id = task_scheduler->CreateTask<HydraCreateGPUBufferTask>(HydraThreadAffinity::Render);
task = static_cast<HydraCreateGPUBufferTask *>(task_scheduler->GetTask(create_gpu_buffer_task_id));
task->Intialise("TextureBuffer", 1, MAX_TEXTURES * sizeof(uint64_t), &m_texture_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
}
void HydraMaterialSystem::InitialiseComponent(HydraID entity_id)
{
}
void HydraMaterialSystem::UpdateSystem(float delta_t_seconds)
{
_WriteToGPU();
}
void HydraMaterialSystem::_WriteToGPU()
{
void* buffer = GetEngine()->MaterialBufferManager()->GetMaterialBuffer();
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_material_buffer_id, buffer);
task_scheduler->WaitForTask(update_buffer_task_id);
buffer = GetEngine()->TextureBufferManager()->GetTextureBuffer();
update_buffer_task_id = task_scheduler->CreateTask<HydraUpdateWholeGPUBufferTask>(HydraThreadAffinity::Render);
task = static_cast<HydraUpdateWholeGPUBufferTask *>(task_scheduler->GetTask(update_buffer_task_id));
task->Intialise(m_texture_buffer_id, buffer);
task_scheduler->WaitForTask(update_buffer_task_id);
}
@@ -0,0 +1,27 @@
#ifndef HYDRAMATERIALSYSTEM
#define HYDRAMATERIALSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include <GLMIncludes.h>
#include <queue>
#include <vector>
class HydraMaterialSystem : public HydraSystem
{
public:
void Initialise() override;
protected:
void InitialiseComponent(HydraID entity_id) override;
virtual void UpdateSystem(float delta_t_seconds);
private:
void _WriteToGPU();
HydraID m_material_buffer_id = INVALID_HYDRA_ID;
HydraID m_texture_buffer_id = INVALID_HYDRA_ID;
};
#endif /* HYDRAMATERIALSYSTEM */
@@ -0,0 +1,44 @@
#include "HydraMeshSystem.h"
#include "../HydraECS.h"
#include "../../engine/HydraEngine.h"
#include "../components/HydraMeshComponent.h"
#include "../../buffer/HydraBufferManager.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateVertexBufferTask.hpp"
#include "../../task/buffer/HydraUpdateVertexBufferTask.hpp"
#include "../../mesh/HydraMeshManager.h"
void HydraMeshSystem::Initialise()
{
}
void HydraMeshSystem::InitialiseComponent(HydraID entity_id)
{
HydraECS* ecs = GetEngine()->ECS();
HydraBufferManager *buffer_manager = GetEngine()->BufferManager();
HydraMeshComponent& mesh_comp = ecs->GetComponent<HydraMeshComponent>(entity_id);
HydraMeshManager* mesh_man = GetEngine()->MeshManager();
HydraMesh* mesh = mesh_man->GetMesh(mesh_comp.mesh_id);
uint32_t vertex_buffer_size = sizeof(StandardVertex) * mesh->vertexes.size();
uint32_t index_buffer_size = sizeof(uint32_t) * mesh->indexes.size();
uint32_t triangle_count = mesh->indexes.size();// / 3;
HydraTaskScheduler *task_scheduler = GetEngine()->TaskScheduler();
//Create VAO and Vertex Buffer storage
HydraID create_vertex_task_id = task_scheduler->CreateTask<HydraCreateVertexBufferTask>(HydraThreadAffinity::Render);
HydraCreateVertexBufferTask *vertex_task = static_cast<HydraCreateVertexBufferTask *>(task_scheduler->GetTask(create_vertex_task_id));
vertex_task->Intialise(vertex_buffer_size, index_buffer_size, triangle_count, &mesh_comp.vertex_buffer_id);
task_scheduler->WaitForTask(create_vertex_task_id);
//Update vertex buffer
HydraID update_buffer_task_id = task_scheduler->CreateTask<HydraUpdateVertexBufferTask>(HydraThreadAffinity::Render);
HydraUpdateVertexBufferTask *vertex_update_task = static_cast<HydraUpdateVertexBufferTask *>(task_scheduler->GetTask(update_buffer_task_id));
vertex_update_task->Intialise(mesh_comp.vertex_buffer_id, &mesh->vertexes[0], &mesh->indexes[0]);
task_scheduler->WaitForTask(update_buffer_task_id);
}
@@ -0,0 +1,19 @@
#ifndef HYDRAMESHSYSTEM
#define HYDRAMESHSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include <GLMIncludes.h>
#include <queue>
class HydraMeshSystem : public HydraSystem
{
public:
void Initialise() override;
protected:
void InitialiseComponent(HydraID entity_id) override;
virtual void UpdateSystem(float delta_t_seconds){}
};
#endif /* HYDRAMESHSYSTEM */
@@ -0,0 +1,6 @@
#include "HydraRenderSystem.h"
void HydraRenderSystem::Initialise()
{
}
@@ -0,0 +1,18 @@
#ifndef HYDRARENDERSYSTEM
#define HYDRARENDERSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include <GLMIncludes.h>
#include <queue>
class HydraRenderSystem : public HydraSystem
{
public:
void Initialise() override;
protected:
virtual void UpdateSystem(float delta_t_seconds){}
};
#endif /* HYDRARENDERSYSTEM */
@@ -0,0 +1,93 @@
#include "HydraTransformSystem.h"
#include "../../engine/HydraEngine.h"
#include "../HydraECS.h"
#include "../components/HydraPositionComponent.h"
#include "../../scheduler/HydraTaskScheduler.h"
#include "../../task/buffer/HydraCreateGPUBufferTask.hpp"
#include "../../task/buffer/HydraUpdateWholeGPUBufferTask.hpp"
#include "../../actor/HydraActorManager.h"
void HydraTransformSystem::UpdateTransform(HydraID actor_id)
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
pos_comp.needs_update = true;
m_entities_to_update.push(actor_id);
}
void HydraTransformSystem::Initialise()
{
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));
task->Intialise("PositionsBuffer", 1, MAX_ACTORS * sizeof(glm::mat4),
&m_transform_buffer_id, HYDRA_BUFFER_TYPE::HYDRA_SSBO_BUFFER);
task_scheduler->WaitForTask(create_gpu_buffer_task_id);
m_model_matrix.resize(MAX_ACTORS);
}
void HydraTransformSystem::UpdateSystem(float delta_t_seconds)
{
HydraECS *ecs = GetEngine()->ECS();
while(m_entities_to_update.size() > 0)
{
HydraID actor_id = m_entities_to_update.front();
m_entities_to_update.pop();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
if(pos_comp.needs_update)
{
HydraID parent_id = GetEngine()->ActorManager()->GetRoot(actor_id);
glm::mat4 identity = glm::mat4(1);
_UpdateChild(INVALID_HYDRA_ID, parent_id, identity);
}
}
_WriteToGPU();
}
void HydraTransformSystem::InitialiseComponent(HydraID actor_id)
{
HydraECS *ecs = GetEngine()->ECS();
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
if (pos_comp.needs_update)
{
glm::mat4 identity = glm::mat4(1);
_UpdateChild(INVALID_HYDRA_ID, actor_id, identity);
}
}
void HydraTransformSystem::_UpdateChild(HydraID parent_id, HydraID actor_id, glm::mat4 parent_transform)
{
HydraECS *ecs = GetEngine()->ECS();
std::vector<HydraID> &child_ids = GetEngine()->ActorManager()->GetChildren(actor_id);
glm::mat4 trans = parent_transform;
if (ecs->HasComponent<HydraPositionComponent>(actor_id))
{
HydraPositionComponent &pos_comp = ecs->GetComponent<HydraPositionComponent>(actor_id);
glm::mat4 trans_matrix = glm::translate(glm::vec3(pos_comp.position.x, pos_comp.position.y, pos_comp.position.z));
glm::mat4 rot_matrix = glm::toMat4(glm::quat(pos_comp.orientation));
glm::mat4 scale_matrix = glm::scale(pos_comp.scale);
glm::mat4 model_matrix = trans_matrix * rot_matrix * scale_matrix;
pos_comp.transform = trans * model_matrix;
pos_comp.needs_update = false;
m_model_matrix[actor_id] = pos_comp.transform;
trans = pos_comp.transform;
}
for (uint32_t child_idx = 0; child_idx < child_ids.size(); child_idx++)
{
_UpdateChild(actor_id, child_ids[child_idx], trans);
}
}
void HydraTransformSystem::_WriteToGPU()
{
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_transform_buffer_id, &m_model_matrix[0]);
task_scheduler->WaitForTask(update_buffer_task_id);
}
@@ -0,0 +1,27 @@
#ifndef HYDRAECSTRANSFORMSYSTEM
#define HYDRAECSTRANSFORMSYSTEM
#include "../../engine/HydraObject.h"
#include "../HydraSystem.h"
#include <GLMIncludes.h>
#include <queue>
class HydraTransformSystem : public HydraSystem
{
public:
void UpdateTransform(HydraID actor_id);
void Initialise() override;
protected:
virtual void UpdateSystem(float delta_t_seconds) override;
void InitialiseComponent(HydraID actor_id) override;
private:
void _UpdateChild(HydraID parent_id, HydraID actor_id, glm::mat4 parent_transform);
void _WriteToGPU();
HydraID m_transform_buffer_id = INVALID_HYDRA_ID;
std::queue<HydraID> m_entities_to_update;
std::vector<glm::mat4> m_model_matrix;
};
#endif /* HYDRAECSTRANSFORMSYSTEM */