diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-25 15:22:30 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-25 15:22:30 -0500 |
| commit | 63467312ec8d5531930e6784fa94050b0d2c68e8 (patch) | |
| tree | 881f412d50b10502dc334aa7e56794cf52fa200d | |
| parent | 7b266e9763280dcfd07045868fc08238a8e117ba (diff) | |
pke: EntityType handle framebuffer length changes
| -rw-r--r-- | src/entities.cpp | 212 | ||||
| -rw-r--r-- | src/game.cpp | 2 | ||||
| -rw-r--r-- | src/window.cpp | 5 | ||||
| -rw-r--r-- | src/window.hpp | 3 |
4 files changed, 133 insertions, 89 deletions
diff --git a/src/entities.cpp b/src/entities.cpp index b80db95..2ace423 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -3,8 +3,10 @@ #include "bucketed-array.hpp" #include "ecs.hpp" +#include "game-settings.hpp" #include "math-helpers.hpp" #include "physics.hpp" +#include "pk.h" #include "plugins.hpp" #include "static-missing-texture.hpp" #include "vendor-cgltf-include.hpp" @@ -160,6 +162,120 @@ struct EntityType_LoadHelperStruct { VkMemoryRequirements textureMemoryRequirementsCombined{}; }; +void EntityType_Inner_DestroyDescriptors(EntityType *et) { + assert(et != nullptr); + for (long k = 0; k < et->detailsCount; ++k) { + if (et->details[k].grBinds == nullptr || et->details[k].grBinds == CAFE_BABE(CompGrBinds)) { + continue; + } + if (et->details[k].grBinds->vkDescriptorSets != nullptr && et->details[k].vkDescriptorPool != VK_NULL_HANDLE) { + // 2023-09-27 - JCB + // We are not setting the pool flag for allowing freeing descriptor sets + // so all we need to do is destroy the pool + // If we switch to a global pool, we will need to free here, and + // destroy the pool outside of this loop + vkDestroyDescriptorPool(vkDevice, et->details[k].vkDescriptorPool, vkAllocator); + et->details[k].grBinds->vkDescriptorSets = CAFE_BABE(VkDescriptorSet); + } + } +} + +void EntityType_Inner_UpdateDescriptorSets(EntityType *et) { + // descriptor pool & sets + VkDescriptorPoolSize descriptorPoolSizes[2]; + descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorPoolSizes[0].descriptorCount = swapchainLength; + descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + descriptorPoolSizes[1].descriptorCount = swapchainLength; + + VkDescriptorPoolCreateInfo vkDescriptorPoolCreateInfo; + vkDescriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + vkDescriptorPoolCreateInfo.pNext = nullptr; + vkDescriptorPoolCreateInfo.flags = 0; + vkDescriptorPoolCreateInfo.maxSets = swapchainLength; + vkDescriptorPoolCreateInfo.poolSizeCount = (uint32_t)2; + vkDescriptorPoolCreateInfo.pPoolSizes = descriptorPoolSizes; + + VkDescriptorSetLayout *descriptorSets = pk_new<VkDescriptorSetLayout>(swapchainLength, pkeSettings.mem.bkt); + for (long i = 0; i < swapchainLength; ++i) { + descriptorSets[i] = pkePipelines.descr_layouts.named.texture; + } + + VkDescriptorSetAllocateInfo vkDescriptorSetAllocateInfo; + vkDescriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + vkDescriptorSetAllocateInfo.pNext = nullptr; + vkDescriptorSetAllocateInfo.descriptorSetCount = swapchainLength; + vkDescriptorSetAllocateInfo.pSetLayouts = descriptorSets; + + VkWriteDescriptorSet *writeDescriptorSets = pk_new<VkWriteDescriptorSet>(2 * swapchainLength, pkeSettings.mem.bkt); + for (long i = 0; i < 2 * swapchainLength; ++i) { + writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writeDescriptorSets[i].pNext = nullptr; + writeDescriptorSets[i].dstSet = nullptr; + writeDescriptorSets[i].dstBinding = i % 2; + writeDescriptorSets[i].dstArrayElement = 0; + writeDescriptorSets[i].descriptorCount = 1; + writeDescriptorSets[i].descriptorType = (i % 2) == 0 + ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER : VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + writeDescriptorSets[i].pImageInfo = nullptr; + writeDescriptorSets[i].pBufferInfo = nullptr; + writeDescriptorSets[i].pTexelBufferView = nullptr; + } + + VkDescriptorImageInfo textureDescriptorInfo; + textureDescriptorInfo.sampler = global_sampler; + textureDescriptorInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkDescriptorBufferInfo *vkDescriptorBufferInfo = pk_new<VkDescriptorBufferInfo>(swapchainLength, pkeSettings.mem.bkt); + + for (long i = 0; i < et->detailsCount; ++i) { + EntityTypeDetails *etd = &et->details[i]; + // consider making me a global pool + auto vkResult = vkCreateDescriptorPool(vkDevice, &vkDescriptorPoolCreateInfo, vkAllocator, &etd->vkDescriptorPool); + assert(vkResult == VK_SUCCESS); + + vkDescriptorSetAllocateInfo.descriptorPool = etd->vkDescriptorPool; + + etd->grBinds->vkDescriptorSets = pk_new<VkDescriptorSet>(swapchainLength, MemBkt_Vulkan); + for (long i = 0; i < swapchainLength; ++i) { + etd->grBinds->vkDescriptorSets[i] = VkDescriptorSet{}; + } + vkResult = vkAllocateDescriptorSets(vkDevice, &vkDescriptorSetAllocateInfo, etd->grBinds->vkDescriptorSets); + assert(vkResult == VK_SUCCESS); + + textureDescriptorInfo.imageView = etd->textureImageView; + + for (long i = 0; i < swapchainLength; ++i) { + vkDescriptorBufferInfo[i].buffer = UniformBuffers[i]; + vkDescriptorBufferInfo[i].offset = 0; + vkDescriptorBufferInfo[i].range = sizeof(UniformBufferObject); + + long uboIndex = i * 2; + long samplerIndex = uboIndex + 1; + + writeDescriptorSets[uboIndex].pBufferInfo = &vkDescriptorBufferInfo[i]; + writeDescriptorSets[uboIndex].dstSet = etd->grBinds->vkDescriptorSets[i]; + + writeDescriptorSets[samplerIndex].pImageInfo = &textureDescriptorInfo; + writeDescriptorSets[samplerIndex].dstSet = etd->grBinds->vkDescriptorSets[i]; + } + + vkUpdateDescriptorSets(vkDevice, 2 * swapchainLength, writeDescriptorSets, 0, nullptr); + } +} + +void EntityType_Inner_UpdateDescriptorSets_EvCallabck(void *mgr_data, void *entity_data, void *ev_data) { + (void)mgr_data; + (void)ev_data; + EntityHandle eh = *reinterpret_cast<EntityHandle *>(entity_data); + EntityType *et = EntityType_FindByEntityHandle(eh); + assert(et != nullptr); + + EntityType_Inner_DestroyDescriptors(et); + + EntityType_Inner_UpdateDescriptorSets(et); +} + void EntityType_PreLoad(EntityType_LoadHelperStruct &helper) { const long expectedBufferCount = 4; @@ -589,86 +705,7 @@ void EntityType_LoadTexture(EntityType_LoadHelperStruct &helper, const int64_t i AM_Release(etdHelper.textureAssetHandle); } - // descriptor pool & sets - - VkDescriptorPoolSize descriptorPoolSizes[2]; - descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptorPoolSizes[0].descriptorCount = swapchainLength; - descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptorPoolSizes[1].descriptorCount = swapchainLength; - - VkDescriptorPoolCreateInfo vkDescriptorPoolCreateInfo; - vkDescriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - vkDescriptorPoolCreateInfo.pNext = nullptr; - vkDescriptorPoolCreateInfo.flags = 0; - vkDescriptorPoolCreateInfo.maxSets = swapchainLength; - vkDescriptorPoolCreateInfo.poolSizeCount = (uint32_t)2; - vkDescriptorPoolCreateInfo.pPoolSizes = descriptorPoolSizes; - - // consider making me a global pool - auto vkResult = vkCreateDescriptorPool(vkDevice, &vkDescriptorPoolCreateInfo, vkAllocator, &etdHelper.etd->vkDescriptorPool); - assert(vkResult == VK_SUCCESS); - - VkDescriptorSetLayout *descriptorSets = pk_new<VkDescriptorSetLayout>(swapchainLength); - for (long i = 0; i < swapchainLength; ++i) { - descriptorSets[i] = pkePipelines.descr_layouts.named.texture; - } - VkDescriptorSetAllocateInfo vkDescriptorSetAllocateInfo; - vkDescriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - vkDescriptorSetAllocateInfo.pNext = nullptr; - vkDescriptorSetAllocateInfo.descriptorPool = etdHelper.etd->vkDescriptorPool; - vkDescriptorSetAllocateInfo.descriptorSetCount = swapchainLength; - vkDescriptorSetAllocateInfo.pSetLayouts = descriptorSets; - - etdHelper.etd->grBinds->vkDescriptorSets = pk_new<VkDescriptorSet>(swapchainLength, MemBkt_Vulkan); - for (long i = 0; i < swapchainLength; ++i) { - etdHelper.etd->grBinds->vkDescriptorSets[i] = VkDescriptorSet{}; - } - vkResult = vkAllocateDescriptorSets(vkDevice, &vkDescriptorSetAllocateInfo, etdHelper.etd->grBinds->vkDescriptorSets); - pk_delete<VkDescriptorSetLayout>(descriptorSets, swapchainLength); - // descriptorSets = CAFE_BABE(VkDescriptorSetLayout); - assert(vkResult == VK_SUCCESS); - - VkWriteDescriptorSet *writeDescriptorSets = pk_new<VkWriteDescriptorSet>(2 * swapchainLength); - for (long i = 0; i < 2 * swapchainLength; ++i) { - writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - writeDescriptorSets[i].pNext = nullptr; - writeDescriptorSets[i].dstSet = nullptr; - writeDescriptorSets[i].dstBinding = i % 2; - writeDescriptorSets[i].dstArrayElement = 0; - writeDescriptorSets[i].descriptorCount = 1; - writeDescriptorSets[i].descriptorType = (i % 2) == 0 - ? VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER : VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - writeDescriptorSets[i].pImageInfo = nullptr; - writeDescriptorSets[i].pBufferInfo = nullptr; - writeDescriptorSets[i].pTexelBufferView = nullptr; - } - - VkDescriptorImageInfo textureDescriptorInfo; - textureDescriptorInfo.sampler = global_sampler; - textureDescriptorInfo.imageView = etdHelper.etd->textureImageView; - textureDescriptorInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - - VkDescriptorBufferInfo *vkDescriptorBufferInfo = pk_new<VkDescriptorBufferInfo>(swapchainLength); - - for (long i = 0; i < swapchainLength; ++i) { - vkDescriptorBufferInfo[i].buffer = UniformBuffers[i]; - vkDescriptorBufferInfo[i].offset = 0; - vkDescriptorBufferInfo[i].range = sizeof(UniformBufferObject); - - long uboIndex = i * 2; - long samplerIndex = uboIndex + 1; - - writeDescriptorSets[uboIndex].pBufferInfo = &vkDescriptorBufferInfo[i]; - writeDescriptorSets[uboIndex].dstSet = etdHelper.etd->grBinds->vkDescriptorSets[i]; - - writeDescriptorSets[samplerIndex].pImageInfo = &textureDescriptorInfo; - writeDescriptorSets[samplerIndex].dstSet = etdHelper.etd->grBinds->vkDescriptorSets[i]; - } - - vkUpdateDescriptorSets(vkDevice, 2 * swapchainLength, writeDescriptorSets, 0, nullptr); - pk_delete<VkDescriptorBufferInfo>(vkDescriptorBufferInfo, swapchainLength); - pk_delete<VkWriteDescriptorSet>(writeDescriptorSets, 2 * swapchainLength); + EntityType_Inner_UpdateDescriptorSets(&helper.et); } void EntityType_LoadMesh(EntityType_LoadHelperStruct &helper, const int64_t meshIndex) { @@ -1068,6 +1105,11 @@ void EntityType_Load(EntityType &et) { //handle mesh EntityType_LoadMesh(helper, i); } + uint64_t id = 0; + id |= ((uint64_t)helper.et.handle.bucketIndex << 32); + id |= ((uint64_t)helper.et.handle.itemIndex); + // TODO pk.h currently does not offer a way to unregister this callback (EntityType_Unload) + pk_ev_register_cb(pke_ev_mgr_id_window, pke_ev_id_framebuffer_length_changed, EntityType_Inner_UpdateDescriptorSets_EvCallabck, reinterpret_cast<void *>(id)); // TODO DeviceMemory @@ -1081,19 +1123,11 @@ void EntityType_Unload(EntityType &et, CompGrBinds *grBindsArr[1]) { if (et.modelAssetKey[0] == '\0') return; et.modelAssetKey[0] = '\0'; + EntityType_Inner_DestroyDescriptors(&et); for (long k = 0; k < et.detailsCount; ++k) { EntityTypeDetails &etd = et.details[k]; auto *grBinds = grBindsArr[k]; if (grBinds != nullptr) { - if (grBinds->vkDescriptorSets != nullptr && etd.vkDescriptorPool != VK_NULL_HANDLE) { - // 2023-09-27 - JCB - // We are not setting the pool flag for allowing freeing descriptor sets - // so all we need to do is destroy the pool - // If we switch to a global pool, we will need to free here, and - // destroy the pool outside of this loop - vkDestroyDescriptorPool(vkDevice, etd.vkDescriptorPool, vkAllocator); - grBinds->vkDescriptorSets = CAFE_BABE(VkDescriptorSet); - } if (grBinds->vertexBD.buffer != VK_NULL_HANDLE) vkDestroyBuffer(vkDevice, grBinds->vertexBD.buffer, vkAllocator); grBinds->vertexBD.buffer = VK_NULL_HANDLE; diff --git a/src/game.cpp b/src/game.cpp index 9a52e1b..8b6d23d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -587,6 +587,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { pkeSettings.executablePath = executablePath; fprintf(stdout, "Game_Main Entering\n"); try { + pk_ev_init(); PkeThreads_Init(); AM_Init(); ECS_Init(); @@ -738,6 +739,7 @@ void Game_Main(PKEWindowProperties windowProps, const char *executablePath) { AM_DebugPrint(); AM_Teardown(); PkeThreads_Teardown(); + pk_ev_teardown(); pk_memory_debug_print(); fprintf(stdout, "Game_Main Exiting\n"); } diff --git a/src/window.cpp b/src/window.cpp index 989c3cb..6a71fe1 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -152,6 +152,8 @@ VkDeviceSize paddedUboBufferSize; VkBuffer *UniformBuffers; DebugHitbox pkeDebugHitbox{}; ImplementedPKVK pkePipelines{}; +pk_ev_mgr_id_T pke_ev_mgr_id_window; +pk_ev_id_T pke_ev_id_framebuffer_length_changed; /* * ImGui @@ -3019,6 +3021,7 @@ void RecreateSwapchain() { if (prevSwapchainLength != swapchainLength) { DestroyUniformBuffers(); CreateUniformBuffers(); + pk_ev_emit(pke_ev_mgr_id_window, pke_ev_id_framebuffer_length_changed, NULL); } shouldRecreateSwapchain = false; prevSwapchainLength = swapchainLength; @@ -3036,6 +3039,8 @@ void FramebufferResizeCallback(GLFWwindow *window, int width, int height) { void CreateWindow(PKEWindowProperties wp) { if (vkInstance != nullptr) return; + pke_ev_mgr_id_window = pk_ev_create_mgr(); + pke_ev_id_framebuffer_length_changed = pk_ev_register_ev(pke_ev_mgr_id_window, NULL); MemBkt_Vulkan = pk_bucket_create("vulkan", PK_DEFAULT_BUCKET_SIZE, false); vulkanAllocs = pk_new<DynArray<pke_vkAllocData>>(MemBkt_Vulkan); new (vulkanAllocs) DynArray<pke_vkAllocData>(MemBkt_Vulkan); diff --git a/src/window.hpp b/src/window.hpp index f511d80..48da3c9 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -15,6 +15,9 @@ extern uint32_t swapchainLength; extern struct pk_membucket *MemBkt_Vulkan; extern bool shouldRecreateSwapchain; +extern pk_ev_mgr_id_T pke_ev_mgr_id_window; +extern pk_ev_id_T pke_ev_id_framebuffer_length_changed; + extern GLFWwindow *window; extern GLFWmonitor *monitor; extern GLFWvidmode vidMode; |
