11#include "glm/gtc/matrix_access.hpp"
12#include <glm/gtc/matrix_transform.hpp>
58 void drawFrame(
const std::function<
void(VkCommandBuffer)>& overlayPass = {}) {
64 glm::vec4 ambientLight = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
65 glm::vec4 lightDir = glm::vec4(0.0f, 1.0f, 0.0f, 0.0f);
66 glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
75 auto* light =
registry.get<Engine::LightComponent>(lightEnt);
76 auto* transform =
registry.get<Transform>(lightEnt);
78 glm::vec3 dir(0.0f, 1.0f, 0.0f);
80 if (glm::dot(transform->position, transform->position) > 0.0001f) {
81 dir = glm::normalize(-transform->position);
83 dir = transform->forward();
86 lightDir = glm::vec4(dir.x, dir.y, dir.z,
static_cast<float>(light->type));
87 lightColor = glm::vec4(light->color.x, light->color.y, light->color.z, light->intensity);
100 std::vector<InstanceDataGPU> gpuData(
renderer.instanceDataCPU.size());
101 for (
size_t i = 0; i <
renderer.instanceDataCPU.size(); i++) {
102 gpuData[i].model =
renderer.instanceDataCPU.models[i];
103 gpuData[i].color =
renderer.instanceDataCPU.colors[i];
105 renderer.instanceBuffer.uploadData(gpuData.data(), gpuData.size() *
sizeof(InstanceDataGPU));
157 if (
depth > 100)
return glm::mat4(1.0f);
158 glm::mat4 model = glm::mat4(1.0f);
160 model = transform->matrix();
166 if (!mesh->isSkinned) {
167 std::string targetBone = mesh->parentBoneName.empty() ? mesh->nodeName : mesh->parentBoneName;
168 if (!targetBone.empty()) {
173 for (
size_t i = 0; i < parentSkeleton->joints.size(); ++i) {
174 if (parentSkeleton->joints[i].name == targetBone) {
175 jointIdx =
static_cast<int>(i);
181 if (jointIdx == -1) {
182 auto toLower = [](std::string s) {
183 for (
char& c : s) c =
static_cast<char>(std::tolower(c));
186 std::string lowerTarget = toLower(targetBone);
187 for (
size_t i = 0; i < parentSkeleton->joints.size(); ++i) {
188 if (toLower(parentSkeleton->joints[i].name) == lowerTarget) {
189 jointIdx =
static_cast<int>(i);
196 if (jointIdx == -1) {
197 for (
size_t i = 0; i < parentSkeleton->joints.size(); ++i) {
198 if (parentSkeleton->joints[i].name.find(targetBone) != std::string::npos ||
199 targetBone.find(parentSkeleton->joints[i].name) != std::string::npos) {
200 jointIdx =
static_cast<int>(i);
206 if (jointIdx != -1 && jointIdx <
static_cast<int>(parentSkeleton->jointMatrices.size())) {
207 return getWorldMatrix(hierarchy->parent,
depth + 1) * parentSkeleton->jointMatrices[jointIdx];
227 for (
auto [entity, mesh, transform, material] :
230 if (!mesh.visible)
continue;
237 Entity entity = renderableEntities[idx];
238 auto* mesh = registry.get<Mesh>(entity);
239 auto* material = registry.get<Material>(entity);
243 inst.materialID = material ? material->id : 0;
244 inst.meshID = mesh ? mesh->id : 0;
245 inst.color = material ? material->color : glm::vec4(1.0f);
247 tempInstances[idx] = inst;
262 return renderer.getActiveCameraPosition();
264 return glm::vec3(0.0f);
271 VkCommandBuffer cmd =
renderer.getCurrentCommandBuffer();
272 VkDescriptorSet cameraSet =
renderer.getCameraDescriptorSet();
275 std::unordered_map<std::pair<Mesh*, Material*>, std::vector<std::pair<Entity, size_t>>,
pair_hash> batches;
281 if (!mesh || !mat || !transform)
continue;
285 size_t idx = it->second;
286 batches[{mesh, mat}].push_back({e, idx});
289 for (
auto& [
key, batch] : batches) {
293 if (!mesh || !mat || batch.empty() || mesh->
vertexBuffer == VK_NULL_HANDLE || mesh->
indexBuffer == VK_NULL_HANDLE || mat->
pipeline == VK_NULL_HANDLE)
continue;
297 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, mat->
pipeline);
300 VkDescriptorSet descriptorSets[2] = {
305 vkCmdBindDescriptorSets(cmd,
306 VK_PIPELINE_BIND_POINT_GRAPHICS,
316 VkDeviceSize vertexOffsets[] = { 0 };
317 vkCmdBindVertexBuffers(cmd, 0, 1, vertexBuffers, vertexOffsets);
324 vkCmdBindIndexBuffer(cmd, mesh->
indexBuffer, 0, VK_INDEX_TYPE_UINT32);
327 for (
auto& [e, instanceIdx] : batch) {
339 vkCmdBindDescriptorSets(cmd,
340 VK_PIPELINE_BIND_POINT_GRAPHICS,
364 vkCmdPushConstants(cmd,
366 VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
372 vkCmdDrawIndexed(cmd,
373 static_cast<uint32_t
>(mesh->
indices.size()),
386 VkCommandBuffer cmd =
renderer.getCurrentCommandBuffer();
387 VkDescriptorSet cameraSet =
renderer.getCameraDescriptorSet();
392 if (!grid || !mat || mat->pipeline == VK_NULL_HANDLE)
continue;
396 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, mat->pipeline);
400 if (mesh->vertexBuffer != VK_NULL_HANDLE) {
401 VkBuffer vertexBuffers[] = { mesh->vertexBuffer };
402 VkDeviceSize vertexOffsets[] = { 0 };
403 vkCmdBindVertexBuffers(cmd, 0, 1, vertexBuffers, vertexOffsets);
408 vkCmdBindDescriptorSets(cmd,
409 VK_PIPELINE_BIND_POINT_GRAPHICS,
421 struct GridPushConstants {
431 pc.viewProj =
renderer.getActiveCameraViewProj();
433 pc.viewProj = glm::mat4(1.0f);
436 pc.color = grid->color;
437 pc.camPos = glm::vec4(camPos, 1.0f);
438 pc.scale = grid->spacing;
439 pc.fade = grid->size;
441 vkCmdPushConstants(cmd,
443 VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
445 sizeof(GridPushConstants),
449 vkCmdDraw(cmd, 6, 1, 0, 0);
463 std::size_t
operator()(
const std::pair<Mesh*, Material*>& p)
const {
464 return std::hash<Mesh*>()(p.first) ^ (std::hash<Material*>()(p.second) << 1);
static JobSystem & getInstance()
Get the singleton instance.
Definition JobSystem.hpp:23
void parallelFor(int count, std::function< void(int)> func)
Splits a total loop count into chunks and evaluates them in parallel.
Definition JobSystem.cpp:143
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
static constexpr IdType INVALID_ENTITY
Constant representing a null or invalid entity.
Definition Entity.hpp:16
IdType getId() const
Retrieve the raw identifier of the entity.
Definition Entity.hpp:32
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
auto view()
Generates a filtered view of entities containing the requested components.
Definition Registry.hpp:279
VulkanRenderer & renderer
Reference to the renderer.
Definition RenderSystem.hpp:125
void removeEntity(Entity e)
Removes an entity from tracking when components are removed.
Definition RenderSystem.hpp:149
glm::mat4 getWorldMatrix(Entity entity, int depth=0)
Computes the absolute world matrix of an entity by traversing the hierarchy.
Definition RenderSystem.hpp:156
void buildInstanceData()
Rebuilds instance data by iterating over entities.
Definition RenderSystem.hpp:222
void update(float dt) override
System update called each frame to rebuild instance data.
Definition RenderSystem.hpp:49
std::vector< Entity > entities
List of tracked entities that have renderable components.
Definition RenderSystem.hpp:127
glm::vec3 getCameraPosition()
Retrieves the active camera's world position.
Definition RenderSystem.hpp:260
void drawBatches()
Binds pipelines, descriptor sets, and draw buffers for geometry batches.
Definition RenderSystem.hpp:270
void drawFrame(const std::function< void(VkCommandBuffer)> &overlayPass={})
Executes the rendering pass, drawing geometry, grid, and optional GUI/overlay passes.
Definition RenderSystem.hpp:58
void drawGrids()
Draws all active grid overlays.
Definition RenderSystem.hpp:385
void checkAndAdd(Entity e)
Verifies if an entity meets requirements for rendering and adds it to the tracked list.
Definition RenderSystem.hpp:137
RenderSystem(Registry ®, VulkanRenderer &renderer)
Construct a new Render System object and subscribes to component add/remove events.
Definition RenderSystem.hpp:31
std::unordered_map< Entity, size_t > entityToInstanceIndex
Mapping from entity to its instance index in CPU buffers.
Definition RenderSystem.hpp:129
Registry & registry
Reference to the entity registry.
Definition RenderSystem.hpp:123
std::vector< Entity > renderableEntities
Vector to track renderable entities to avoid allocation.
Definition RenderSystem.hpp:131
Base class for ECS systems that process sets of entities.
Definition System.hpp:12
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
void beginFrame()
Starts render pass recording for the current frame.
Definition VulkanRenderer.cpp:202
VkCommandBuffer getCurrentCommandBuffer()
Retrieves active Vulkan command buffer for current frame.
Definition VulkanRenderer.hpp:273
InstanceDataSoA instanceDataCPU
CPU-side instancing data in Structure-of-Arrays format.
Definition VulkanRenderer.hpp:374
glm::mat4 viewProj
View-projection matrix.
Definition VulkanRenderer.hpp:40
glm::vec4 camPos
Definition VulkanRenderer.hpp:41
glm::vec4 lightColor
Definition VulkanRenderer.hpp:44
glm::vec4 ambientLight
Definition VulkanRenderer.hpp:42
glm::vec4 lightDir
Definition VulkanRenderer.hpp:43
Component representing a light source in the scene.
Definition LightComponent.hpp:27
Represents a grid component used for rendering a grid overlay.
Definition Grid.hpp:9
Represents a parent-child relationship between ECS entities.
Definition Hierarchy.hpp:8
Layout of per-instance data uploaded to the GPU for instanced rendering.
Definition pushconstants.hpp:29
glm::vec4 color
Instance color.
Definition pushconstants.hpp:33
glm::mat4 model
Model transform matrix.
Definition pushconstants.hpp:31
size_t push(const InstanceData &inst)
Appends a new instance.
Definition VulkanRenderer.hpp:91
Represents a material component defining rendering properties.
Definition Material.hpp:17
VkPipelineLayout pipelineLayout
Optional per-material custom Vulkan pipeline layout.
Definition Material.hpp:31
float metallic
Definition Material.hpp:45
VkDescriptorSet descriptorSet
Vulkan descriptor set representing resource bindings.
Definition Material.hpp:27
float roughness
Definition Material.hpp:43
VkPipeline pipeline
Optional per-material custom Vulkan pipeline.
Definition Material.hpp:29
Represents a mesh component that holds geometry data and its Vulkan buffers.
Definition Mesh.hpp:95
VkBuffer vertexBuffer
Vulkan buffer holding the vertex data.
Definition Mesh.hpp:115
std::vector< uint32_t > indices
CPU-side index data.
Definition Mesh.hpp:112
VkBuffer indexBuffer
Vulkan buffer holding the index data.
Definition Mesh.hpp:119
Struct for passing constant data directly via Vulkan push constants to shaders.
Definition pushconstants.hpp:10
glm::vec4 camPos
Camera position in world space.
Definition pushconstants.hpp:18
float fade
Fade amount (e.g., for grids).
Definition pushconstants.hpp:22
glm::mat4 model
Model matrix of the object being rendered.
Definition pushconstants.hpp:12
glm::vec4 color
Base color multiplier.
Definition pushconstants.hpp:14
glm::mat4 viewProj
Precomputed view-projection matrix.
Definition pushconstants.hpp:16
float scale
Scale factor.
Definition pushconstants.hpp:20
Custom hash function for std::pair<Mesh*, Material*> used in batch rendering maps.
Definition RenderSystem.hpp:457
std::size_t operator()(const std::pair< Mesh *, Material * > &p) const
Computes hash for pair of Mesh and Material pointers.
Definition RenderSystem.hpp:463
Component managing the list of joints and generating the offset matrices palette.
Definition Skeleton.hpp:37
VkDescriptorSet descriptorSet
Definition Skeleton.hpp:45
static ufbxi_noinline int depth
Definition ufbx.cpp:7467
double scale
Definition ufbx.cpp:26002
const ufbx_keyframe * key
Definition ufbx.cpp:25982