From 0809cf99eb1ec6457000d6cfa67f5392880525a9 Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Mon, 25 Apr 2022 14:59:25 +0200
Subject: [PATCH] Adjusted some doxygen comments and fixed multi-usage of
 descriptor set layouts

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 include/vkcv/Core.hpp             | 95 ++++++++++++++++++++++++++-----
 include/vkcv/DescriptorConfig.hpp |  3 +-
 src/vkcv/Core.cpp                 |  4 +-
 src/vkcv/DescriptorManager.cpp    | 20 +++++--
 src/vkcv/DescriptorManager.hpp    | 39 +++++++------
 5 files changed, 121 insertions(+), 40 deletions(-)

diff --git a/include/vkcv/Core.hpp b/include/vkcv/Core.hpp
index e34b625b..e7e50ca8 100644
--- a/include/vkcv/Core.hpp
+++ b/include/vkcv/Core.hpp
@@ -85,7 +85,7 @@ namespace vkcv
 		uint32_t            m_currentSwapchainImageIndex;
 
 		/**
-		 * sets up swapchain images
+		 * Sets up swapchain images
 		 * @param handle Handle of swapchain
 		 */
 		void setSwapchainImages(SwapchainHandle handle);
@@ -234,7 +234,7 @@ namespace vkcv
 			Multisampling   multisampling = Multisampling::None);
 
         /**
-         * creates a new window and returns it's handle
+         * Creates a new window and returns it's handle
          * @param applicationName window name
          * @param windowWidth
          * @param windowHeight
@@ -249,7 +249,7 @@ namespace vkcv
 				bool resizeable);
 
 		/**
-		 * getter for window reference
+		 * Getter for window reference
 		 * @param handle of the window
 		 * @return the window
 		 */
@@ -257,14 +257,14 @@ namespace vkcv
 		Window& getWindow(const WindowHandle& handle );
 
 		/**
-		 * gets the swapchain of the current focused window
+		 * Gets the swapchain of the current focused window
 		 * @return swapchain
 		 */
 		[[nodiscard]]
 		Swapchain& getSwapchainOfCurrentWindow();
 
 		/**
-		 * returns the swapchain reference
+		 * Returns the swapchain reference
 		 * @param handle of the swapchain
 		 * @return swapchain
 		 */
@@ -272,7 +272,7 @@ namespace vkcv
 		Swapchain& getSwapchain(const SwapchainHandle& handle);
 
 		/**
-		 * gets the swapchain handle from the window
+		 * Gets the swapchain handle from the window
 		 * @param handle of the window
 		 * @return the swapchain from getSwapchain( SwapchainHandle )
 		 */
@@ -280,7 +280,7 @@ namespace vkcv
 		Swapchain& getSwapchain(const WindowHandle& handle);
 
 		/**
-		 * returns the image width
+		 * Returns the image width
 		 * @param image handle
 		 * @return imageWidth
 		 */
@@ -288,7 +288,7 @@ namespace vkcv
         uint32_t getImageWidth(const ImageHandle& image);
 
         /**
-         * returns the image height
+         * Returns the image height
          * @param image handle
          * @return imageHeight
          */
@@ -296,19 +296,28 @@ namespace vkcv
         uint32_t getImageHeight(const ImageHandle& image);
 
         /**
-         * returns the image format of the image
+         * Returns the image format of the image
          * @param image handle
          * @return imageFormat
          */
 		[[nodiscard]]
 		vk::Format getImageFormat(const ImageHandle& image);
 
-		/** TODO:
-		 * @param bindings
-		 * @return
+		/**
+		 * @brief Creates a descriptor set layout handle by a set of descriptor bindings.
+		 *
+		 * @param bindings Descriptor bindings
+		 * @return Descriptor set layout handle
 		 */
 		[[nodiscard]]
-		DescriptorSetLayoutHandle createDescriptorSetLayout(const std::unordered_map<uint32_t, DescriptorBinding> &bindingsMap);
+		DescriptorSetLayoutHandle createDescriptorSetLayout(const DescriptorBindings &bindings);
+		
+		/**
+		 * @brief Returns the descriptor set layout of a descriptor set layout handle.
+		 *
+		 * @param handle Descriptor set layout handle
+		 * @return Descriptor set layout
+		 */
 		DescriptorSetLayout getDescriptorSetLayout(const DescriptorSetLayoutHandle handle) const;
 
 		// TODO: existsDescriptorSetLayout function that checks and returns fitting layout upon existence.
@@ -341,7 +350,7 @@ namespace vkcv
 
 
 		/**
-		 * @brief start recording command buffers and increment frame index
+		 * @brief Start recording command buffers and increment frame index
 		*/
 		bool beginFrame(uint32_t& width, uint32_t& height, const WindowHandle &windowHandle);
 
@@ -415,6 +424,7 @@ namespace vkcv
         /**
          * Records the rtx ray generation to the @p cmdStreamHandle.
          * Currently only supports @p closestHit, @p rayGen and @c miss shaderstages @c.
+         *
          * @param cmdStreamHandle The command stream handle which receives relevant commands for drawing.
          * @param rtxPipeline The raytracing pipeline from the RTXModule.
          * @param rtxPipelineLayout The raytracing pipeline layout from the RTXModule.
@@ -491,7 +501,7 @@ namespace vkcv
 			const PushConstants&                    pushConstants);
 
 		/**
-		 * @brief end recording and present image
+		 * @brief End recording and present image
 		 */
 		void endFrame( const WindowHandle& windowHandle );
 
@@ -635,13 +645,68 @@ namespace vkcv
 		void recordBlitImage(const CommandStreamHandle& cmdStream, const ImageHandle& src, const ImageHandle& dst,
 							 SamplerFilterType filterType);
 	
+		/**
+		 * @brief Sets a debug label to a buffer handle.
+		 *
+		 * @param handle Buffer handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const BufferHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a pass handle.
+		 *
+		 * @param handle Pass handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const PassHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a graphics pipeline handle.
+		 *
+		 * @param handle Graphics pipeline handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const GraphicsPipelineHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a compute pipeline handle.
+		 *
+		 * @param handle Compute pipeline handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const ComputePipelineHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a descriptor set handle.
+		 *
+		 * @param handle Descriptor set handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const DescriptorSetHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a sampler handle.
+		 *
+		 * @param handle Sampler handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const SamplerHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to an image handle.
+		 *
+		 * @param handle Image handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const ImageHandle &handle, const std::string &label);
+		
+		/**
+		 * @brief Sets a debug label to a command stream handle.
+		 *
+		 * @param handle Command stream handle
+		 * @param label Debug label
+		 */
 		void setDebugLabel(const CommandStreamHandle &handle, const std::string &label);
 		
     };
diff --git a/include/vkcv/DescriptorConfig.hpp b/include/vkcv/DescriptorConfig.hpp
index a557c453..4bebb50b 100644
--- a/include/vkcv/DescriptorConfig.hpp
+++ b/include/vkcv/DescriptorConfig.hpp
@@ -80,7 +80,8 @@ namespace vkcv
     struct DescriptorSetLayout
     {
         vk::DescriptorSetLayout vulkanHandle;
-        DescriptorBindings descriptorBindings;
+        DescriptorBindings      descriptorBindings;
+        size_t                  layoutUsageCount;
     };
 
     struct DescriptorSet
diff --git a/src/vkcv/Core.cpp b/src/vkcv/Core.cpp
index 69648843..158411a1 100644
--- a/src/vkcv/Core.cpp
+++ b/src/vkcv/Core.cpp
@@ -889,9 +889,9 @@ namespace vkcv
 		return getSwapchain(swapchainHandle);
 	}
 
-	DescriptorSetLayoutHandle Core::createDescriptorSetLayout(const DescriptorBindings &bindingsMap)
+	DescriptorSetLayoutHandle Core::createDescriptorSetLayout(const DescriptorBindings &bindings)
 	{
-	    return m_DescriptorManager->createDescriptorSetLayout(bindingsMap);
+	    return m_DescriptorManager->createDescriptorSetLayout(bindings);
 	}
 
 	DescriptorSetLayout Core::getDescriptorSetLayout(const DescriptorSetLayoutHandle handle) const
diff --git a/src/vkcv/DescriptorManager.cpp b/src/vkcv/DescriptorManager.cpp
index 8343fde6..c04ddb47 100644
--- a/src/vkcv/DescriptorManager.cpp
+++ b/src/vkcv/DescriptorManager.cpp
@@ -35,6 +35,8 @@ namespace vkcv
         }
 		
 		for (uint64_t id = 0; id < m_DescriptorSetLayouts.size(); id++) {
+			// Resets the usage count to zero for destruction.
+			m_DescriptorSetLayouts[id].layoutUsageCount = 0;
 			destroyDescriptorSetLayoutById(id);
 		}
         
@@ -48,15 +50,16 @@ namespace vkcv
 		}
     }
 
-    DescriptorSetLayoutHandle DescriptorManager::createDescriptorSetLayout(const DescriptorBindings &setBindingsMap)
+    DescriptorSetLayoutHandle DescriptorManager::createDescriptorSetLayout(const DescriptorBindings &bindings)
     {
         for (size_t i = 0; i < m_DescriptorSetLayouts.size(); i++)
         {
-            if(m_DescriptorSetLayouts[i].descriptorBindings.size() != setBindingsMap.size())
+            if(m_DescriptorSetLayouts[i].descriptorBindings.size() != bindings.size())
                 continue;
 
-            if (m_DescriptorSetLayouts[i].descriptorBindings == setBindingsMap)
+            if (m_DescriptorSetLayouts[i].descriptorBindings == bindings)
             {
+				m_DescriptorSetLayouts[i].layoutUsageCount++;
                 return DescriptorSetLayoutHandle(i, [&](uint64_t id) { destroyDescriptorSetLayoutById(id); });
             }
         }
@@ -65,7 +68,7 @@ namespace vkcv
         std::vector<vk::DescriptorSetLayoutBinding> bindingsVector = {};
 		std::vector<vk::DescriptorBindingFlags> bindingsFlags = {};
 		
-        for (auto bindingElem : setBindingsMap)
+        for (auto bindingElem : bindings)
         {
             DescriptorBinding binding = bindingElem.second;
             uint32_t bindingID = bindingElem.first;
@@ -104,7 +107,7 @@ namespace vkcv
         };
 
         const uint64_t id = m_DescriptorSetLayouts.size();
-        m_DescriptorSetLayouts.push_back({vulkanHandle, setBindingsMap});
+        m_DescriptorSetLayouts.push_back({vulkanHandle, bindings, 1});
         return DescriptorSetLayoutHandle(id, [&](uint64_t id) { destroyDescriptorSetLayoutById(id); });
     }
 
@@ -341,6 +344,13 @@ namespace vkcv
 	    }
 
 	    auto& layout = m_DescriptorSetLayouts[id];
+
+		if (layout.layoutUsageCount > 1) {
+			layout.layoutUsageCount--;
+			return;
+		} else {
+			layout.layoutUsageCount = 0;
+		}
 		
 	    if (layout.vulkanHandle){
 	        m_Device.destroy(layout.vulkanHandle);
diff --git a/src/vkcv/DescriptorManager.hpp b/src/vkcv/DescriptorManager.hpp
index d60a6d5c..4f8bff93 100644
--- a/src/vkcv/DescriptorManager.hpp
+++ b/src/vkcv/DescriptorManager.hpp
@@ -1,7 +1,7 @@
 #pragma once
 
 /**
- * @authors Artur Wasmut, Susanne D�tsch, Simeon Hermann
+ * @authors Artur Wasmut, Susanne D�tsch, Simeon Hermann, Tobias Frisch
  * @file src/vkcv/DescriptorManager.cpp
  * @brief Creation and handling of descriptor sets and the respective descriptor pools
  */
@@ -23,7 +23,7 @@ namespace vkcv
 	    explicit DescriptorManager(vk::Device device) noexcept;
 	    ~DescriptorManager() noexcept;
 
-	    DescriptorSetLayoutHandle createDescriptorSetLayout(const DescriptorBindings &setBindingsMap);
+	    DescriptorSetLayoutHandle createDescriptorSetLayout(const DescriptorBindings &bindings);
         DescriptorSetHandle createDescriptorSet(const DescriptorSetLayoutHandle &setLayoutHandle);
 
 		void writeDescriptorSet(
@@ -45,33 +45,38 @@ namespace vkcv
 		vk::DescriptorPoolCreateInfo m_PoolInfo;
 
 		/**
-        * Contains all the descriptor set layout descriptions
-        * that were requested by the user in calls of createDescriptorSetLayout.
-        */
+         * Contains all the descriptor set layout descriptions
+         * that were requested by the user in calls of createDescriptorSetLayout.
+         */
         std::vector<DescriptorSetLayout> m_DescriptorSetLayouts;
 
         /**
-		* Contains all the descriptor sets that were created by the user in calls of createDescriptorSet.
-		*/
+		 * Contains all the descriptor sets that were created by the user in calls of createDescriptorSet.
+		 */
         std::vector<DescriptorSet> m_DescriptorSets;
 
 		/**
-		* Destroys a specific descriptor set
-		* @param[in] the DescriptorSetHandle
-		*/
+		 * @brief Destroys a specific descriptor set.
+		 *
+		 * @param[in] the DescriptorSetHandle
+		 */
 		void destroyDescriptorSetById(uint64_t id);
 
 		/**
-        * Destroys a specific descriptor set LAYOUT (not the set)
-        * @param[in] the DescriptorSetLayoutHandle
-        */
+         * @brief Revokes the usage of a specific descriptor set layout and
+         * destroys it once the usage count is at zero.
+         *
+         * @param[in] the DescriptorSetLayoutHandle
+         */
 		void destroyDescriptorSetLayoutById(uint64_t id);
 
 		/**
-		* creates a descriptor pool based on the poolSizes and poolInfo defined in the constructor
-		* is called initially in the constructor and then every time the pool runs out memory
-		* @return a DescriptorPool object
-		*/
+		 * @brief Creates a descriptor pool based on the poolSizes and poolInfo defined in the
+		 * constructor is called initially in the constructor and then every time the pool runs
+		 * out memory.
+		 *
+		 * @return a DescriptorPool object
+		 */
 		vk::DescriptorPool allocateDescriptorPool();
 		
 	};
-- 
GitLab