diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-01 10:10:51 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-06 17:24:27 -0400 |
| commit | b9f90793c8c0468d5f35d7af976a6e3bcd206aad (patch) | |
| tree | bff30852d5bb594c7ad84c5f7f3d5a385e18fc4a /src | |
| parent | 092e287ba5669f6ed40b721c0bdf2450dae95af8 (diff) | |
add first graphics binding component
Diffstat (limited to 'src')
| -rw-r--r-- | src/components.cpp | 5 | ||||
| -rw-r--r-- | src/components.hpp | 38 | ||||
| -rw-r--r-- | src/ecs.cpp | 41 | ||||
| -rw-r--r-- | src/ecs.hpp | 17 | ||||
| -rw-r--r-- | src/window.cpp | 14 | ||||
| -rw-r--r-- | src/window.hpp | 1 |
6 files changed, 109 insertions, 7 deletions
diff --git a/src/components.cpp b/src/components.cpp new file mode 100644 index 0000000..7531dd2 --- /dev/null +++ b/src/components.cpp @@ -0,0 +1,5 @@ + +#include "components.hpp" + +TypeSafeInt_B(EntityHandle); +TypeSafeInt_B(GrBindsHandle); diff --git a/src/components.hpp b/src/components.hpp new file mode 100644 index 0000000..4c40927 --- /dev/null +++ b/src/components.hpp @@ -0,0 +1,38 @@ +#ifndef PKE_COMPONENTS_HPP +#define PKE_COMPONENTS_HPP + +#include"macros.hpp" +#include <vulkan/vulkan_core.h> + +const uint64_t ECS_UNSET_VAL = 0xFFFFFFFFFFFFFFFF; + +TypeSafeInt_H(EntityHandle, uint64_t, UINT64_MAX); +TypeSafeInt_H(GrBindsHandle, uint64_t, UINT64_MAX); + +struct Entity { + EntityHandle handle = EntityHandle{EntityHandle_T{ECS_UNSET_VAL}}; + EntityHandle parentHandle = EntityHandle{EntityHandle_T{ECS_UNSET_VAL}}; + bool isMarkedForRemoval = false; + GrBindsHandle grBindsHandle = GrBindsHandle{GrBindsHandle_T{ECS_UNSET_VAL}}; +}; + +struct CompGrBinds { + EntityHandle entityHandle = EntityHandle{EntityHandle_T{ECS_UNSET_VAL}}; + GrBindsHandle grBindsHandle = GrBindsHandle{GrBindsHandle_T{ECS_UNSET_VAL}}; + VkBuffer vertexBuffer = VK_NULL_HANDLE; + uint32_t vertexFirstBinding = 0; + uint32_t vertexCount = 0; + VkDeviceSize vertexOffsets = 0; + VkBuffer indexBuffer = VK_NULL_HANDLE; + uint32_t indexFirstBinding = 0; + uint32_t indexCount = 0; + VkDeviceSize indexOffsets = 0; + VkBuffer instanceBuffer = VK_NULL_HANDLE; + uint32_t instanceFirstBinding = 0; + uint32_t instanceCount = 0; + VkDeviceSize instanceOffsets = 0; + VkPipelineLayout vkPipelineLayout = VK_NULL_HANDLE; + VkDescriptorSet vkDescriptorSet = VK_NULL_HANDLE; +}; + +#endif /* PKE_COMPONENTS_HPP */ diff --git a/src/ecs.cpp b/src/ecs.cpp index aa85873..33fec2b 100644 --- a/src/ecs.cpp +++ b/src/ecs.cpp @@ -7,6 +7,9 @@ const uint64_t bucketItemCount = 256; struct EntityBucket{ Entity entities[bucketItemCount]; }; +struct GrBindsBucket{ + CompGrBinds compGrBinds[bucketItemCount]; +}; uint64_t entityBucketIncrementer = 2; EntityHandle_T entityBucketCounter{0}; @@ -15,8 +18,17 @@ EntityBucket *entityBuckets = nullptr; DynArray<EntityHandle> entitiesMarkedForRemoval{16}; DynArray<EntityHandle> EntitiesToBeRemoved{16}; +BucketContainer<GrBindsBucket, GrBindsHandle_T> Comp_GrBinds_BucketContainer{}; + +void ECS_GetEntity_Inner(EntityHandle entHandle, Entity*& ent) { + EntityHandle_T entHandle_t{static_cast<EntityHandle_T>(entHandle)}; + assert(entHandle_t == ECS_UNSET_VAL && "Unknown entity handle"); + ent = &entityBuckets[Buckets_GetBucketIndex(entHandle_t)].entities[Buckets_GetItemIndex(entHandle_t)]; +} + void ECS_Init() { entityBuckets = Pke_New<EntityBucket>(entityBucketIncrementer); + Buckets_Init(Comp_GrBinds_BucketContainer); } EntityHandle ECS_CreateEntity_Inner(EntityHandle parentEntityHandle) { @@ -69,3 +81,32 @@ void ECS_Tick(double delta) { } entitiesMarkedForRemoval.Resize(0); } + +CompGrBinds &ECS_CreateGrBinds(EntityHandle handle) { + Entity *ent = nullptr; + ECS_GetEntity_Inner(handle, ent); + GrBindsHandle_T newHandle{Buckets_NewHandle(bucketItemCount, Comp_GrBinds_BucketContainer)}; + ent->grBindsHandle = GrBindsHandle{newHandle}; + return Comp_GrBinds_BucketContainer.buckets[Buckets_GetBucketIndex(newHandle)].compGrBinds[Buckets_GetItemIndex(newHandle)]; +} + +CompGrBinds *ECS_GetGrBinds(EntityHandle entHandle) { + Entity *ent = nullptr; + ECS_GetEntity_Inner(entHandle, ent); + GrBindsHandle_T grBindsHandle_t{static_cast<GrBindsHandle_T>(ent->grBindsHandle)}; + if (grBindsHandle_t == ECS_UNSET_VAL) return nullptr; + return &Comp_GrBinds_BucketContainer.buckets[Buckets_GetBucketIndex(grBindsHandle_t)].compGrBinds[Buckets_GetItemIndex(grBindsHandle_t)]; +} + +uint64_t ECS_GetGrBinds_BucketCount() { + return Comp_GrBinds_BucketContainer.bucketCounter; +} + +CompGrBinds *ECS_GetGrBinds(uint64_t bucketIndex, uint64_t &itemCount) { + if (bucketIndex == Comp_GrBinds_BucketContainer.bucketCounter) { + itemCount = Comp_GrBinds_BucketContainer.itemCounter; + } else { + itemCount = bucketItemCount; + } + return Comp_GrBinds_BucketContainer.buckets[bucketIndex].compGrBinds; +} diff --git a/src/ecs.hpp b/src/ecs.hpp index d37ff57..a777a4a 100644 --- a/src/ecs.hpp +++ b/src/ecs.hpp @@ -4,22 +4,25 @@ #include "dynamic-array.hpp" #include "macros.hpp" #include "memory.hpp" +#include "components.hpp" #include "glm/vec3.hpp" -TypeSafeInt_H(EntityHandle, uint64_t, UINT64_MAX); - extern DynArray<EntityHandle> EntitiesToBeRemoved; -struct Entity { - EntityHandle handle = EntityHandle{EntityHandle_T{0xFFFFFFFFFFFFFFFF}}; - EntityHandle parentHandle = EntityHandle{EntityHandle_T{0xFFFFFFFFFFFFFFFF}}; - bool isMarkedForRemoval = false; -}; +static struct { + uint64_t Entity = 1ULL << 0; + uint64_t GrBinds = 1ULL << 1; +} ComponentTypes; void ECS_Init(); void ECS_Tick(double delta); EntityHandle ECS_CreateEntity(EntityHandle parentEntityHandle = EntityHandle{EntityHandle_T{0xFFFFFFFFFFFFFFFF}}); void ECS_MarkForRemoval(EntityHandle entityHandle); +CompGrBinds &ECS_CreateGrBinds(EntityHandle entHandle); +CompGrBinds *ECS_GetGrBinds(EntityHandle entHandle); +uint64_t ECS_GetGrBinds_BucketCount(); +CompGrBinds *ECS_GetGrBinds(uint64_t bucketIndex, uint64_t &itemCount); + #endif /* PKE_ECS_HPP */ diff --git a/src/window.cpp b/src/window.cpp index eb2ecff..8293c81 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,3 +1,4 @@ +#include <vulkan/vulkan_core.h> #define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_VULKAN @@ -997,6 +998,19 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetScissor(commandBuffer, 0, 1, &scissor); + const uint64_t bindBucketCount = ECS_GetGrBinds_BucketCount(); + for (long b = 0; b < bindBucketCount; ++b) { + uint64_t itemCount; + CompGrBinds *items = ECS_GetGrBinds(b, itemCount); + for (long i = 0; i < itemCount; ++i) { + CompGrBinds *binder = &items[i]; + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, binder->vkPipelineLayout, 0U, 1U, &binder->vkDescriptorSet, 0, {}); + vkCmdBindVertexBuffers(commandBuffer, binder->indexFirstBinding, binder->vertexCount, &binder->vertexBuffer, &binder->vertexOffsets); + vkCmdBindIndexBuffer(commandBuffer, binder->indexBuffer, binder->vertexOffsets, VK_INDEX_TYPE_UINT16); + vkCmdBindVertexBuffers(commandBuffer, binder->indexFirstBinding, binder->instanceCount, &binder->instanceBuffer, &binder->instanceOffsets); + } + } + // reminder that present.vert is a triangle vkCmdDraw(commandBuffer, 3, 1, 0, 0); diff --git a/src/window.hpp b/src/window.hpp index 6dc690b..05a5ae9 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -7,6 +7,7 @@ #include "event.hpp" #include "imgui.h" #include "memory.hpp" +#include "ecs.hpp" #include <cstring> #include <cstdio> |
