diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-27 12:51:41 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-09-27 12:51:41 -0400 |
| commit | b14582679d0a113cb8571a7e6764436391d6af48 (patch) | |
| tree | c3cafdba13af3c29fb135643b48163894cc3ba82 /src | |
| parent | b11ef72f36fa933dd34eb43f53dc15d68ac85ff1 (diff) | |
checkpoint - EntityType descriptor pool and descriptor sets
Diffstat (limited to 'src')
| -rw-r--r-- | src/components.hpp | 13 | ||||
| -rw-r--r-- | src/entities.cpp | 94 | ||||
| -rw-r--r-- | src/entities.hpp | 1 | ||||
| -rw-r--r-- | src/game.cpp | 16 | ||||
| -rw-r--r-- | src/window.cpp | 20 |
5 files changed, 118 insertions, 26 deletions
diff --git a/src/components.hpp b/src/components.hpp index 70c36d5..40c8aae 100644 --- a/src/components.hpp +++ b/src/components.hpp @@ -32,26 +32,27 @@ struct CompGrBinds { EntityHandle entHandle = EntityHandle_MAX; GrBindsHandle grBindsHandle = GrBindsHandle_MAX; VkPipelineLayout vkPipelineLayout = VK_NULL_HANDLE; - VkDescriptorSet vkDescriptorSet = VK_NULL_HANDLE; + VkDescriptorSet *vkDescriptorSets = nullptr; VkBuffer vertexBuffer = VK_NULL_HANDLE; uint32_t vertexFirstBinding = 0; - uint32_t vertexCount = 0; + uint32_t vertexBindingCount = 0; VkDeviceSize vertexOffsets = 0; VkBuffer normalsBuffer = VK_NULL_HANDLE; uint32_t normalsFirstBinding = 0; - uint32_t normalsCount = 0; + uint32_t normalsBindingCount = 0; VkDeviceSize normalsOffsets = 0; VkBuffer uvBuffer = VK_NULL_HANDLE; uint32_t uvFirstBinding = 0; - uint32_t uvCount = 0; + uint32_t uvBindingCount = 0; VkDeviceSize uvOffsets = 0; VkBuffer indexBuffer = VK_NULL_HANDLE; uint32_t indexFirstBinding = 0; - uint32_t indexCount = 0; + uint32_t indexBindingCount = 0; VkDeviceSize indexOffsets = 0; + VkDeviceSize indexCount = 0; VkBuffer instanceBuffer = VK_NULL_HANDLE; uint32_t instanceFirstBinding = 0; - uint32_t instanceCount = 0; + uint32_t instanceBindingCount = 0; VkDeviceSize instanceOffsets = 0; DynArray<InstPos> instances{0}; bool isInstanceBufferNeedingUpdated = false; diff --git a/src/entities.cpp b/src/entities.cpp index c270be4..1b85784 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -579,8 +579,84 @@ void EntityType_Load(EntityType &et) { } stbi_image_free(pixels); - } + // descriptor pool & sets + + VkDescriptorPoolSize descriptorPoolSizes[2]; + descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorPoolSizes[0].descriptorCount = MAX_FRAMES_IN_FLIGHT; + descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + descriptorPoolSizes[1].descriptorCount = MAX_FRAMES_IN_FLIGHT; + + VkDescriptorPoolCreateInfo vkDescriptorPoolCreateInfo; + vkDescriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + vkDescriptorPoolCreateInfo.pNext = nullptr; + vkDescriptorPoolCreateInfo.flags = 0; + vkDescriptorPoolCreateInfo.maxSets = MAX_FRAMES_IN_FLIGHT; + vkDescriptorPoolCreateInfo.poolSizeCount = (uint32_t)2; + vkDescriptorPoolCreateInfo.pPoolSizes = descriptorPoolSizes; + + // consider making me a global pool + auto vkResult = vkCreateDescriptorPool(vkDevice, &vkDescriptorPoolCreateInfo, vkAllocator, &et.vkDescriptorPool); + assert(vkResult == VK_SUCCESS); + + VkDescriptorSetLayout descriptorSets[MAX_FRAMES_IN_FLIGHT]; + for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { + descriptorSets[i] = vkDescriptorSetLayout_Texture; + } + VkDescriptorSetAllocateInfo vkDescriptorSetAllocateInfo; + vkDescriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + vkDescriptorSetAllocateInfo.pNext = nullptr; + vkDescriptorSetAllocateInfo.descriptorPool = et.vkDescriptorPool; + vkDescriptorSetAllocateInfo.descriptorSetCount = MAX_FRAMES_IN_FLIGHT; + vkDescriptorSetAllocateInfo.pSetLayouts = descriptorSets; + + grBinds.vkDescriptorSets = Pke_New<VkDescriptorSet>(MAX_FRAMES_IN_FLIGHT); + for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { + grBinds.vkDescriptorSets[i] = VkDescriptorSet{}; + } + vkResult = vkAllocateDescriptorSets(vkDevice, &vkDescriptorSetAllocateInfo, grBinds.vkDescriptorSets); + assert(vkResult == VK_SUCCESS); + + VkWriteDescriptorSet writeDescriptorSets[2 * MAX_FRAMES_IN_FLIGHT]; + for (long i = 0; i < 2 * MAX_FRAMES_IN_FLIGHT; ++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 = vkSampler_Texture; + textureDescriptorInfo.imageView = et.textureImageView; + textureDescriptorInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkDescriptorBufferInfo vkDescriptorBufferInfo[MAX_FRAMES_IN_FLIGHT]; + + for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++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 = grBinds.vkDescriptorSets[i]; + + writeDescriptorSets[samplerIndex].pImageInfo = &textureDescriptorInfo; + writeDescriptorSets[samplerIndex].dstSet = grBinds.vkDescriptorSets[i]; + } + + vkUpdateDescriptorSets(vkDevice, 2 * MAX_FRAMES_IN_FLIGHT, writeDescriptorSets, 0, nullptr); + } // make sure cgltf can interpret our model for (long i = 0; i < gltfData->accessors_count; ++i) { @@ -603,7 +679,7 @@ void EntityType_Load(EntityType &et) { long index = 0; if (et.Importer_GLTF.AccessorIndexVertex > -1) { const auto &acc = gltfData->accessors[et.Importer_GLTF.AccessorIndexVertex]; - grBinds.vertexCount = acc.count; + grBinds.vertexBindingCount = 1; bufferCI.size = acc.buffer_view->size; bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &grBinds.vertexBuffer); @@ -612,7 +688,7 @@ void EntityType_Load(EntityType &et) { } if (et.Importer_GLTF.AccessorIndexNormal > -1) { const auto &acc = gltfData->accessors[et.Importer_GLTF.AccessorIndexNormal]; - grBinds.normalsCount = acc.count; + grBinds.normalsBindingCount = 1; bufferCI.size = acc.buffer_view->size; vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &grBinds.normalsBuffer); @@ -620,7 +696,7 @@ void EntityType_Load(EntityType &et) { } if (et.Importer_GLTF.AccessorIndexUV > -1) { const auto &acc = gltfData->accessors[et.Importer_GLTF.AccessorIndexUV]; - grBinds.uvCount = acc.count; + grBinds.uvBindingCount = 1; bufferCI.size = acc.buffer_view->size; vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &grBinds.uvBuffer); @@ -628,6 +704,7 @@ void EntityType_Load(EntityType &et) { } if (et.Importer_GLTF.AccessorIndexIndex > -1) { const auto &acc = gltfData->accessors[et.Importer_GLTF.AccessorIndexIndex]; + grBinds.indexBindingCount = 1; grBinds.indexCount = acc.count; bufferCI.size = acc.buffer_view->size; bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT; @@ -818,6 +895,15 @@ void EntityType_Teardown() { if (GlobalEntityTypes[i].modelFile == nullptr) continue; auto *et = &GlobalEntityTypes[i]; auto *grBinds = ECS_GetGrBinds(GlobalEntityTypes[i].entityHandle); + if (grBinds->vkDescriptorSets != nullptr && et->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->vkDescriptorPool, vkAllocator); + Pke_Delete(grBinds->vkDescriptorSets, MAX_FRAMES_IN_FLIGHT); + } if (grBinds->vertexBuffer != VK_NULL_HANDLE) vkDestroyBuffer(vkDevice, grBinds->vertexBuffer, vkAllocator); if (grBinds->normalsBuffer != VK_NULL_HANDLE) diff --git a/src/entities.hpp b/src/entities.hpp index c09e8f5..a83e0d5 100644 --- a/src/entities.hpp +++ b/src/entities.hpp @@ -21,6 +21,7 @@ struct EntityType { VkDeviceMemory deviceMemoryTexture = VK_NULL_HANDLE; VkImage textureImage = VK_NULL_HANDLE; VkImageView textureImageView = VK_NULL_HANDLE; + VkDescriptorPool vkDescriptorPool = VK_NULL_HANDLE; uint32_t startingInstanceCount = 1024; struct Importer_GLTF { int16_t AccessorIndexVertex = -1; diff --git a/src/game.cpp b/src/game.cpp index 9298b26..21b460f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -376,37 +376,39 @@ void RecordImGui_CompGrBinds(bool readonly, CompGrBinds *component) { if (component->vkPipelineLayout) ImGui::InputScalar("VkPipelineLayout", ImGuiDataType_U64, &component->vkPipelineLayout, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); - if (component->vkDescriptorSet) - ImGui::InputScalar("VkPipelineDescriptorSet", ImGuiDataType_U64, &component->vkDescriptorSet, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); + if (component->vkDescriptorSets){ + ImGui::InputScalarN("VkPipelineDescriptorSets", ImGuiDataType_U64, &component->vkDescriptorSets, MAX_FRAMES_IN_FLIGHT, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); + } if (component->vertexBuffer) ImGui::InputScalar("VkVertexBuffer", ImGuiDataType_U64, &component->vertexBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); ImGui::InputScalar("VertexFirstBinding", ImGuiDataType_U32, &component->vertexFirstBinding, nullptr, nullptr, nullptr, inputTextFlags); - ImGui::InputScalar("VertexCount", ImGuiDataType_U32, &component->vertexCount, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("VertexBindingCount", ImGuiDataType_U32, &component->vertexBindingCount, nullptr, nullptr, nullptr, inputTextFlags); ImGui::InputScalar("VertexOffsets", ImGuiDataType_U64, &component->vertexOffsets, nullptr, nullptr, nullptr, inputTextFlags); if (component->normalsBuffer) ImGui::InputScalar("VkNormalBuffer", ImGuiDataType_U64, &component->normalsBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); ImGui::InputScalar("NormalFirstBinding", ImGuiDataType_U32, &component->normalsFirstBinding, nullptr, nullptr, nullptr, inputTextFlags); - ImGui::InputScalar("NormalCount", ImGuiDataType_U32, &component->normalsCount, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("NormalBindingCount", ImGuiDataType_U32, &component->normalsBindingCount, nullptr, nullptr, nullptr, inputTextFlags); ImGui::InputScalar("NormalOffsets", ImGuiDataType_U64, &component->normalsOffsets, nullptr, nullptr, nullptr, inputTextFlags); if (component->uvBuffer) ImGui::InputScalar("VkUVBuffer", ImGuiDataType_U64, &component->uvBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); ImGui::InputScalar("UVFirstBinding", ImGuiDataType_U32, &component->uvFirstBinding, nullptr, nullptr, nullptr, inputTextFlags); - ImGui::InputScalar("UVCount", ImGuiDataType_U32, &component->uvCount, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("UVBindingCount", ImGuiDataType_U32, &component->uvBindingCount, nullptr, nullptr, nullptr, inputTextFlags); ImGui::InputScalar("UVOffsets", ImGuiDataType_U64, &component->uvOffsets, nullptr, nullptr, nullptr, inputTextFlags); if (component->indexBuffer) ImGui::InputScalar("VkIndexBuffer", ImGuiDataType_U64, &component->indexBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); ImGui::InputScalar("IndexFirstBinding", ImGuiDataType_U32, &component->indexFirstBinding, nullptr, nullptr, nullptr, inputTextFlags); - ImGui::InputScalar("IndexCount", ImGuiDataType_U32, &component->indexCount, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("IndexBindingCount", ImGuiDataType_U32, &component->indexBindingCount, nullptr, nullptr, nullptr, inputTextFlags); ImGui::InputScalar("IndexOffsets", ImGuiDataType_U64, &component->indexOffsets, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("IndexCount", ImGuiDataType_U32, &component->indexCount, nullptr, nullptr, nullptr, inputTextFlags); if (component->instanceBuffer) ImGui::InputScalar("VkInstanceBuffer", ImGuiDataType_U64, &component->instanceBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly); ImGui::InputScalar("InstanceFirstBinding", ImGuiDataType_U32, &component->instanceFirstBinding, nullptr, nullptr, nullptr, inputTextFlags); - ImGui::InputScalar("InstanceCount", ImGuiDataType_U32, &component->instanceCount, nullptr, nullptr, nullptr, inputTextFlags); + ImGui::InputScalar("InstanceBindingCount", ImGuiDataType_U32, &component->instanceBindingCount, nullptr, nullptr, nullptr, inputTextFlags); ImGui::InputScalar("InstanceOffsets", ImGuiDataType_U64, &component->instanceOffsets, nullptr, nullptr, nullptr, inputTextFlags); int64_t count = component->instances.Count(); diff --git a/src/window.cpp b/src/window.cpp index 8934184..66e2095 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1357,18 +1357,20 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { 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->vertexFirstBinding, binder->vertexCount, &binder->vertexBuffer, &binder->vertexOffsets); - if (binder->normalsCount > 0) - vkCmdBindVertexBuffers(commandBuffer, binder->normalsFirstBinding, binder->normalsCount, &binder->normalsBuffer, &binder->normalsOffsets); - if (binder->uvCount > 0) - vkCmdBindVertexBuffers(commandBuffer, binder->uvFirstBinding, binder->uvCount, &binder->uvBuffer, &binder->uvOffsets); - if (binder->indexCount > 0) + if (!binder->vkPipelineLayout) + continue; + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, binder->vkPipelineLayout, 0, 1, &binder->vkDescriptorSets[CURRENT_FRAME], 0, {}); + vkCmdBindVertexBuffers(commandBuffer, binder->vertexFirstBinding, binder->vertexBindingCount, &binder->vertexBuffer, &binder->vertexOffsets); + if (binder->normalsBindingCount > 0) + vkCmdBindVertexBuffers(commandBuffer, binder->normalsFirstBinding, binder->normalsBindingCount, &binder->normalsBuffer, &binder->normalsOffsets); + if (binder->uvBindingCount > 0) + vkCmdBindVertexBuffers(commandBuffer, binder->uvFirstBinding, binder->uvBindingCount, &binder->uvBuffer, &binder->uvOffsets); + if (binder->indexBindingCount > 0) vkCmdBindIndexBuffer(commandBuffer, binder->indexBuffer, binder->vertexOffsets, VK_INDEX_TYPE_UINT16); - vkCmdBindVertexBuffers(commandBuffer, binder->indexFirstBinding, binder->instanceCount, &binder->instanceBuffer, &binder->instanceOffsets); + vkCmdBindVertexBuffers(commandBuffer, binder->indexFirstBinding, binder->instanceBindingCount, &binder->instanceBuffer, &binder->instanceOffsets); - vkCmdDrawIndexed(commandBuffer, binder->indexCount, binder->instanceCount, 0, 0, 0); + vkCmdDrawIndexed(commandBuffer, binder->indexCount, binder->instances.Count(), 0, 0, 0); } } |
