From 717f45290899daecf06575d15bb352fd3d19f738 Mon Sep 17 00:00:00 2001 From: Thraix Date: Tue, 12 Aug 2025 22:18:23 +0200 Subject: [PATCH] Set all descriptor sets when calling Pipeline::SetDescriptorSets - When calling Pipeline::SetDescriptorSets, it now sets all descriptor sets for all frames that can be in flight. Meaning SetDescriptorSets doesn't need to be called each render pass. --- .../src/copium/pipeline/DescriptorSet.cpp | 7 ++++++- .../src/copium/pipeline/DescriptorSet.h | 1 + CopiumEngine/src/copium/pipeline/Pipeline.cpp | 17 ++++++++++++----- CopiumEngine/src/copium/pipeline/Pipeline.h | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp index 2beae72..bd552f4 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.cpp @@ -114,6 +114,11 @@ namespace Copium return bindings.begin()->set; } + VkDescriptorSet DescriptorSet::GetVkDescriptorSet(int flightIndex) const + { + return descriptorSets[flightIndex]; + } + DescriptorSet::operator VkDescriptorSet() const { return descriptorSets[Vulkan::GetSwapChain().GetFlightIndex()]; @@ -138,4 +143,4 @@ namespace Copium } } -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/pipeline/DescriptorSet.h b/CopiumEngine/src/copium/pipeline/DescriptorSet.h index f86be3a..9d513dd 100644 --- a/CopiumEngine/src/copium/pipeline/DescriptorSet.h +++ b/CopiumEngine/src/copium/pipeline/DescriptorSet.h @@ -36,6 +36,7 @@ namespace Copium void SetSamplersDynamic(const std::vector& samplers, uint32_t binding); UniformBuffer& GetUniformBuffer(const std::string& uniformBuffer); uint32_t GetSetIndex() const; + VkDescriptorSet GetVkDescriptorSet(int flightIndex) const; operator VkDescriptorSet() const; private: diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index 6d224d0..ac64845 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -83,13 +83,16 @@ namespace Copium void Pipeline::SetDescriptorSet(const DescriptorSet& descriptorSet) { - CP_ASSERT(descriptorSet.GetSetIndex() < boundDescriptorSets.size(), "DescriptorSet index is out of bounds"); - boundDescriptorSets[descriptorSet.GetSetIndex()] = descriptorSet; + CP_ASSERT(descriptorSet.GetSetIndex() < GetDescriptorSetCount(), "DescriptorSet index is out of bounds"); + for (int i = 0; i < SwapChain::MAX_FRAMES_IN_FLIGHT; i++) + { + boundDescriptorSetsPerFlightIndex[i][descriptorSet.GetSetIndex()] = descriptorSet.GetVkDescriptorSet(i); + } } void Pipeline::BindDescriptorSets(const CommandBuffer& commandBuffer) { - vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, boundDescriptorSets.size(), boundDescriptorSets.data(), 0, nullptr); + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, GetDescriptorSetCount(), boundDescriptorSetsPerFlightIndex[Vulkan::GetSwapChain().GetFlightIndex()].data(), 0, nullptr); } std::unique_ptr Pipeline::CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const @@ -121,12 +124,16 @@ namespace Copium int Pipeline::GetDescriptorSetCount() const { - return boundDescriptorSets.size(); + return boundDescriptorSetsPerFlightIndex.front().size(); } void Pipeline::InitializeDescriptorSetLayout(const PipelineCreator& creator) { - boundDescriptorSets.resize(creator.descriptorSetLayouts.size()); + boundDescriptorSetsPerFlightIndex.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); + for (auto&& boundDescriptorSets : boundDescriptorSetsPerFlightIndex) + { + boundDescriptorSets.resize(creator.descriptorSetLayouts.size()); + } descriptorSetLayouts.resize(creator.descriptorSetLayouts.size()); int i = 0; for (auto&& bindings : creator.descriptorSetLayouts) diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.h b/CopiumEngine/src/copium/pipeline/Pipeline.h index 6fda160..03e544b 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.h +++ b/CopiumEngine/src/copium/pipeline/Pipeline.h @@ -19,7 +19,7 @@ namespace Copium private: ShaderReflector shaderReflector; std::vector descriptorSetLayouts{}; - std::vector boundDescriptorSets; + std::vector> boundDescriptorSetsPerFlightIndex; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; AssetRef framebuffer; @@ -31,6 +31,7 @@ namespace Copium ~Pipeline(); void Bind(const CommandBuffer& commandBuffer); void SetDescriptorSet(const DescriptorSet& descriptorSet); + void SetDescriptorSetDynamic(const DescriptorSet& descriptorSet); void BindDescriptorSets(const CommandBuffer& commandBuffer); std::unique_ptr CreateDescriptorSet(DescriptorPool& descriptorPool, int setIndex) const;