LCOV - code coverage report
Current view: top level - source/renderer/backend/vulkan - DescriptorManager.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 36 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_DESCRIPTORMANAGER
      19             : #define INCLUDED_RENDERER_BACKEND_VULKAN_DESCRIPTORMANAGER
      20             : 
      21             : #include "lib/debug.h"
      22             : #include "ps/CStrIntern.h"
      23             : #include "renderer/backend/vulkan/Device.h"
      24             : #include "renderer/backend/vulkan/DeviceObjectUID.h"
      25             : 
      26             : #include <algorithm>
      27             : #include <array>
      28             : #include <cstddef>
      29             : #include <cstdint>
      30             : #include <glad/vulkan.h>
      31             : #include <limits>
      32             : #include <memory>
      33             : #include <unordered_map>
      34             : #include <utility>
      35             : #include <vector>
      36             : 
      37             : namespace Renderer::Backend { class ITexture; }
      38             : namespace Renderer::Backend::Vulkan { class CBuffer; }
      39             : namespace Renderer::Backend::Vulkan { class CTexture; }
      40             : 
      41             : namespace Renderer
      42             : {
      43             : 
      44             : namespace Backend
      45             : {
      46             : 
      47             : namespace Vulkan
      48             : {
      49             : 
      50             : class CDescriptorManager
      51             : {
      52             : public:
      53             :     CDescriptorManager(CDevice* device, const bool useDescriptorIndexing);
      54             :     ~CDescriptorManager();
      55             : 
      56           0 :     bool UseDescriptorIndexing() const { return m_UseDescriptorIndexing; }
      57             : 
      58             :     /**
      59             :      * @return a single type descriptor set layout with the number of bindings
      60             :      * equals to the size. The returned layout is owned by the manager.
      61             :      */
      62             :     VkDescriptorSetLayout GetSingleTypeDescritorSetLayout(
      63             :         VkDescriptorType type, const uint32_t size);
      64             : 
      65             :     VkDescriptorSet GetSingleTypeDescritorSet(
      66             :         VkDescriptorType type, VkDescriptorSetLayout layout,
      67             :         const std::vector<DeviceObjectUID>& texturesUID,
      68             :         const std::vector<CTexture*>& textures);
      69             : 
      70             :     VkDescriptorSet GetSingleTypeDescritorSet(
      71             :         VkDescriptorType type, VkDescriptorSetLayout layout,
      72             :         const std::vector<DeviceObjectUID>& buffersUID,
      73             :         const std::vector<CBuffer*>& buffers);
      74             : 
      75             :     uint32_t GetUniformSet() const;
      76             : 
      77             :     uint32_t GetTextureDescriptor(CTexture* texture);
      78             : 
      79             :     void OnTextureDestroy(const DeviceObjectUID uid);
      80             : 
      81             :     void OnBufferDestroy(const DeviceObjectUID uid);
      82             : 
      83             :     const VkDescriptorSetLayout& GetDescriptorIndexingSetLayout() const { return m_DescriptorIndexingSetLayout; }
      84           0 :     const VkDescriptorSetLayout& GetUniformDescriptorSetLayout() const { return m_UniformDescriptorSetLayout; }
      85           0 :     const VkDescriptorSet& GetDescriptorIndexingSet() { return m_DescriptorIndexingSet; }
      86             : 
      87           0 :     const std::vector<VkDescriptorSetLayout>& GetDescriptorSetLayouts() const { return m_DescriptorSetLayouts; }
      88             : 
      89             :     void CollectStatistics(IDevice::StatisticsVector& statistics) const;
      90             : 
      91             : private:
      92           0 :     struct SingleTypePool
      93             :     {
      94             :         VkDescriptorSetLayout layout;
      95             :         VkDescriptorPool pool;
      96             :         int16_t firstFreeIndex = 0;
      97             :         static constexpr int16_t INVALID_INDEX = -1;
      98             :         struct Element
      99             :         {
     100             :             VkDescriptorSet set = VK_NULL_HANDLE;
     101             :             uint32_t version = 0;
     102             :             int16_t nextFreeIndex = INVALID_INDEX;
     103             :         };
     104             :         std::vector<Element> elements;
     105             :     };
     106             :     SingleTypePool& GetSingleTypePool(const VkDescriptorType type, const uint32_t size);
     107             : 
     108             :     std::pair<VkDescriptorSet, bool> GetSingleTypeDescritorSetImpl(
     109             :         VkDescriptorType type, VkDescriptorSetLayout layout,
     110             :         const std::vector<DeviceObjectUID>& uids);
     111             : 
     112             :     void OnDeviceObjectDestroy(const DeviceObjectUID uid);
     113             : 
     114             :     CDevice* m_Device = nullptr;
     115             : 
     116             :     bool m_UseDescriptorIndexing = false;
     117             : 
     118             :     VkDescriptorPool m_DescriptorIndexingPool = VK_NULL_HANDLE;
     119             :     VkDescriptorSet m_DescriptorIndexingSet = VK_NULL_HANDLE;
     120             :     VkDescriptorSetLayout m_DescriptorIndexingSetLayout = VK_NULL_HANDLE;
     121             :     VkDescriptorSetLayout m_UniformDescriptorSetLayout = VK_NULL_HANDLE;
     122             :     std::vector<VkDescriptorSetLayout> m_DescriptorSetLayouts;
     123             : 
     124             :     static constexpr uint32_t DESCRIPTOR_INDEXING_BINDING_SIZE = 16384;
     125             :     static constexpr uint32_t NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET = 3;
     126             : 
     127           0 :     struct DescriptorIndexingBindingMap
     128             :     {
     129             :         static_assert(std::numeric_limits<int16_t>::max() >= DESCRIPTOR_INDEXING_BINDING_SIZE);
     130             :         int16_t firstFreeIndex = 0;
     131             :         std::vector<int16_t> elements;
     132             :         std::unordered_map<DeviceObjectUID, int16_t> map;
     133             :     };
     134             :     std::array<DescriptorIndexingBindingMap, NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET>
     135             :         m_DescriptorIndexingBindings;
     136             :     std::unordered_map<DeviceObjectUID, uint32_t> m_TextureToBindingMap;
     137             : 
     138             :     std::unordered_map<VkDescriptorType, std::vector<SingleTypePool>> m_SingleTypePools;
     139             :     struct SingleTypePoolReference
     140             :     {
     141             :         VkDescriptorType type = VK_DESCRIPTOR_TYPE_MAX_ENUM;
     142             :         uint32_t version = 0;
     143             :         int16_t elementIndex = SingleTypePool::INVALID_INDEX;
     144             :         uint8_t size = 0;
     145             :     };
     146             :     std::unordered_map<DeviceObjectUID, std::vector<SingleTypePoolReference>> m_UIDToSingleTypePoolMap;
     147             : 
     148             :     using SingleTypeCacheKey = std::pair<VkDescriptorSetLayout, std::vector<DeviceObjectUID>>;
     149             :     struct SingleTypeCacheKeyHash
     150             :     {
     151             :         size_t operator()(const SingleTypeCacheKey& key) const;
     152             :     };
     153             :     std::unordered_map<SingleTypeCacheKey, VkDescriptorSet, SingleTypeCacheKeyHash> m_SingleTypeSets;
     154             : 
     155             :     std::unique_ptr<ITexture> m_ErrorTexture;
     156             : };
     157             : 
     158             : // TODO: ideally we might want to separate a set and its mapping.
     159             : template<typename DeviceObject>
     160             : class CSingleTypeDescriptorSetBinding
     161             : {
     162             : public:
     163           0 :     CSingleTypeDescriptorSetBinding(CDevice* device, const VkDescriptorType type,
     164             :         const uint32_t size, std::unordered_map<CStrIntern, uint32_t> mapping)
     165           0 :         : m_Device{device}, m_Type{type}, m_Mapping{std::move(mapping)}
     166             :     {
     167           0 :         m_BoundDeviceObjects.resize(size);
     168           0 :         m_BoundUIDs.resize(size);
     169           0 :         m_DescriptorSetLayout =
     170           0 :             m_Device->GetDescriptorManager().GetSingleTypeDescritorSetLayout(m_Type, size);
     171           0 :     }
     172             : 
     173           0 :     int32_t GetBindingSlot(const CStrIntern name) const
     174             :     {
     175           0 :         const auto it = m_Mapping.find(name);
     176           0 :         return it != m_Mapping.end() ? it->second : -1;
     177             :     }
     178             : 
     179           0 :     void SetObject(const int32_t bindingSlot, DeviceObject* object)
     180             :     {
     181           0 :         if (m_BoundUIDs[bindingSlot] == object->GetUID())
     182             :             return;
     183           0 :         m_BoundUIDs[bindingSlot] = object->GetUID();
     184           0 :         m_BoundDeviceObjects[bindingSlot] = object;
     185           0 :         m_Outdated = true;
     186             :     }
     187             : 
     188           0 :     bool IsOutdated() const { return m_Outdated; }
     189             : 
     190           0 :     VkDescriptorSet UpdateAndReturnDescriptorSet()
     191             :     {
     192           0 :         ENSURE(m_Outdated);
     193           0 :         m_Outdated = false;
     194             : 
     195             :         VkDescriptorSet descriptorSet =
     196           0 :             m_Device->GetDescriptorManager().GetSingleTypeDescritorSet(
     197           0 :                 m_Type, m_DescriptorSetLayout, m_BoundUIDs, m_BoundDeviceObjects);
     198           0 :         ENSURE(descriptorSet != VK_NULL_HANDLE);
     199             : 
     200           0 :         return descriptorSet;
     201             :     }
     202             : 
     203           0 :     void Unbind()
     204             :     {
     205           0 :         std::fill(m_BoundDeviceObjects.begin(), m_BoundDeviceObjects.end(), nullptr);
     206           0 :         std::fill(m_BoundUIDs.begin(), m_BoundUIDs.end(), INVALID_DEVICE_OBJECT_UID);
     207           0 :         m_Outdated = true;
     208           0 :     }
     209             : 
     210           0 :     VkDescriptorSetLayout GetDescriptorSetLayout() { return m_DescriptorSetLayout; }
     211             : 
     212           0 :     const std::vector<DeviceObject*>& GetBoundDeviceObjects() const { return m_BoundDeviceObjects; }
     213             : 
     214             : private:
     215             :     CDevice* const m_Device;
     216             :     const VkDescriptorType m_Type;
     217             :     const std::unordered_map<CStrIntern, uint32_t> m_Mapping;
     218             : 
     219             :     bool m_Outdated{true};
     220             : 
     221             :     VkDescriptorSetLayout m_DescriptorSetLayout{VK_NULL_HANDLE};
     222             : 
     223             :     std::vector<DeviceObject*> m_BoundDeviceObjects;
     224             :     std::vector<DeviceObjectUID> m_BoundUIDs;
     225             : };
     226             : 
     227             : } // namespace Vulkan
     228             : 
     229             : } // namespace Backend
     230             : 
     231             : } // namespace Renderer
     232             : 
     233             : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_DESCRIPTORMANAGER

Generated by: LCOV version 1.16