diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-07 22:44:12 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-07 22:44:12 -0400 |
| commit | 9d6c681037eb4fb3c6d9eca6b1ff1d01d429614e (patch) | |
| tree | 5d5b8070a4a67a1004458d8588cd103323cb6950 | |
| parent | 72c0e8e946c1d3e391986eeece474f3d2529f160 (diff) | |
entities and memory allocation checkpoint
| -rw-r--r-- | src/entities.cpp | 41 | ||||
| -rw-r--r-- | src/entities.hpp | 21 | ||||
| -rw-r--r-- | src/memory-allocator.hpp | 54 |
3 files changed, 116 insertions, 0 deletions
diff --git a/src/entities.cpp b/src/entities.cpp new file mode 100644 index 0000000..fb2bc60 --- /dev/null +++ b/src/entities.cpp @@ -0,0 +1,41 @@ + +#include "entities.hpp" +#include "ecs.hpp" +#include <vulkan/vulkan_core.h> + +void EntityType_Init() {} + +void EntityType_Load(EntityType &et) { + if (et.modelFile != nullptr && et.modelFile != CAFE_BABE(char)) { + assert(et.vkPipelineLayoutCreateInfo != nullptr && et.vkPipelineLayoutCreateInfo != CAFE_BABE(VkPipelineLayoutCreateInfo) && "EntityType with a defined model must also contain appropriate Vulkan CreateInfos"); + AssetHandle assetHandle{AM_Register(et.modelFile)}; + const Asset *asset = AM_Get(assetHandle); + + CompGrBinds &grBinds = ECS_CreateGrBinds(et.entityHandle); + auto vkResult = vkCreatePipelineLayout(vkDevice, et.vkPipelineLayoutCreateInfo, vkAllocator, &grBinds.vkPipelineLayout); + assert(vkResult == VK_SUCCESS); + // TODO grBinds.vkDescriptorSet + + cgltf_options options{}; + cgltf_data *gltfData = nullptr; + cgltf_result result = cgltf_parse(&options, asset->ptr, asset->size, &gltfData); + assert(result == cgltf_result_success); + result = cgltf_validate(gltfData); + assert(result == cgltf_result_success); + + // create buffers + // TODO + + // create VkDeviceMemory + VkMemoryAllocateInfo vkMemoryAllocateInfo; + vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + vkMemoryAllocateInfo.pNext = nullptr; + vkMemoryAllocateInfo.allocationSize = gltfData->buffers[0].size;; + vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(0, 0); // TODO + + vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, &grBinds.deviceMemory); + + // TODO bind buffers to memory + } + +} diff --git a/src/entities.hpp b/src/entities.hpp new file mode 100644 index 0000000..5e80616 --- /dev/null +++ b/src/entities.hpp @@ -0,0 +1,21 @@ +#ifndef PKE_ENTITIES_HPP +#define PKE_ENTITIES_HPP + +#include "vendor/cgltf-include.hpp" +#include "ecs.hpp" +#include "components.hpp" +#include "asset-manager.hpp" +#include "memory.hpp" +#include "window.hpp" + +struct EntityType { + char *modelFile = nullptr; + char entityTypeCode[16] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; + EntityHandle entityHandle = EntityHandle_MAX; + VkPipelineLayoutCreateInfo *vkPipelineLayoutCreateInfo = nullptr; +}; + +void EntityType_Init(); +void EntityType_Load(EntityType et); + +#endif /* PKE_ENTITIES_HPP */ diff --git a/src/memory-allocator.hpp b/src/memory-allocator.hpp new file mode 100644 index 0000000..c14304a --- /dev/null +++ b/src/memory-allocator.hpp @@ -0,0 +1,54 @@ +#ifndef PKE_MEMORY_ALLOCATOR_HPP +#define PKE_MEMORY_ALLOCATOR_HPP + +#include "memory.hpp" + +template <typename T, std::size_t SZ = DEFAULT_BUCKET_SIZE> class PkeTransAllocator { +public: + typedef T value_type; + MemBucket *transientBucket = nullptr; + + PkeTransAllocator() : transientBucket(Pke_BeginTransientBucket(SZ)) { + } + ~PkeTransAllocator() { + Pke_EndTransientBucket(this->transientBucket); + } + + template <class U> struct rebind {typedef PkeTransAllocator<U, SZ> other;}; + template <typename U> explicit PkeTransAllocator(const PkeTransAllocator<U> &other) { + (void)other; + } + + T *allocate(std::size_t n) { + auto *ptr = reinterpret_cast<T *>(Pke_New(sizeof(T) * n, this->transientBucket)); + if (ptr) return ptr; + throw "Pke-Allocator Failed to allocate"; + } + + void deallocate(const T *ptr, std::size_t n) { + Pke_Delete(ptr, sizeof(T) * n, this->transientBucket); + } +}; + +template <typename T> class PkeAllocator { +public: + typedef T value_type; + + PkeAllocator() = default; + + template <typename U> explicit PkeAllocator(const PkeAllocator<U> &other) { + (void)other; + } + + T *allocate(std::size_t n) { + auto *ptr = Pke_New<T>(n); + if (ptr) return ptr; + throw "Pke-Allocator Failed to allocate"; + } + + void deallocate(const T *ptr, std::size_t n) { + Pke_Delete<T>(ptr, n); + } +}; + +#endif /* PKE_MEMORY_ALLOCATOR_HPP */ |
