Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
DescriptorManager.h
Go to the documentation of this file.
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"
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
37namespace Renderer::Backend { class ITexture; }
38namespace Renderer::Backend::Vulkan { class CBuffer; }
39namespace Renderer::Backend::Vulkan { class CTexture; }
40
41namespace Renderer
42{
43
44namespace Backend
45{
46
47namespace Vulkan
48{
49
51{
52public:
53 CDescriptorManager(CDevice* device, const bool useDescriptorIndexing);
55
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 const VkDescriptorSetLayout& GetUniformDescriptorSetLayout() const { return m_UniformDescriptorSetLayout; }
85 const VkDescriptorSet& GetDescriptorIndexingSet() { return m_DescriptorIndexingSet; }
86
87 const std::vector<VkDescriptorSetLayout>& GetDescriptorSetLayouts() const { return m_DescriptorSetLayouts; }
88
89 void CollectStatistics(IDevice::StatisticsVector& statistics) const;
90
91private:
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;
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
113
114 CDevice* m_Device = nullptr;
115
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;
126
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>
136 std::unordered_map<DeviceObjectUID, uint32_t> m_TextureToBindingMap;
137
138 std::unordered_map<VkDescriptorType, std::vector<SingleTypePool>> m_SingleTypePools;
140 {
141 VkDescriptorType type = VK_DESCRIPTOR_TYPE_MAX_ENUM;
142 uint32_t version = 0;
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>>;
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.
159template<typename DeviceObject>
161{
162public:
163 CSingleTypeDescriptorSetBinding(CDevice* device, const VkDescriptorType type,
164 const uint32_t size, std::unordered_map<CStrIntern, uint32_t> mapping)
165 : m_Device{device}, m_Type{type}, m_Mapping{std::move(mapping)}
166 {
167 m_BoundDeviceObjects.resize(size);
168 m_BoundUIDs.resize(size);
171 }
172
173 int32_t GetBindingSlot(const CStrIntern name) const
174 {
175 const auto it = m_Mapping.find(name);
176 return it != m_Mapping.end() ? it->second : -1;
177 }
178
179 void SetObject(const int32_t bindingSlot, DeviceObject* object)
180 {
181 if (m_BoundUIDs[bindingSlot] == object->GetUID())
182 return;
183 m_BoundUIDs[bindingSlot] = object->GetUID();
184 m_BoundDeviceObjects[bindingSlot] = object;
185 m_Outdated = true;
186 }
187
188 bool IsOutdated() const { return m_Outdated; }
189
191 {
193 m_Outdated = false;
194
195 VkDescriptorSet descriptorSet =
198 ENSURE(descriptorSet != VK_NULL_HANDLE);
199
200 return descriptorSet;
201 }
202
203 void Unbind()
204 {
205 std::fill(m_BoundDeviceObjects.begin(), m_BoundDeviceObjects.end(), nullptr);
206 std::fill(m_BoundUIDs.begin(), m_BoundUIDs.end(), INVALID_DEVICE_OBJECT_UID);
207 m_Outdated = true;
208 }
209
210 VkDescriptorSetLayout GetDescriptorSetLayout() { return m_DescriptorSetLayout; }
211
212 const std::vector<DeviceObject*>& GetBoundDeviceObjects() const { return m_BoundDeviceObjects; }
213
214private:
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
Interned 8-bit strings.
Definition: CStrIntern.h:42
Represents a texture object.
Definition: TextureManager.h:271
std::vector< StatisticsItem > StatisticsVector
Definition: IDevice.h:228
Definition: DescriptorManager.h:51
std::pair< VkDescriptorSetLayout, std::vector< DeviceObjectUID > > SingleTypeCacheKey
Definition: DescriptorManager.h:148
static constexpr uint32_t NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET
Definition: DescriptorManager.h:125
CDevice * m_Device
Definition: DescriptorManager.h:114
bool UseDescriptorIndexing() const
Definition: DescriptorManager.h:56
const VkDescriptorSet & GetDescriptorIndexingSet()
Definition: DescriptorManager.h:85
uint32_t GetUniformSet() const
Definition: DescriptorManager.cpp:366
VkDescriptorSetLayout GetSingleTypeDescritorSetLayout(VkDescriptorType type, const uint32_t size)
Definition: DescriptorManager.cpp:212
std::unique_ptr< ITexture > m_ErrorTexture
Definition: DescriptorManager.h:155
const VkDescriptorSetLayout & GetUniformDescriptorSetLayout() const
Definition: DescriptorManager.h:84
std::unordered_map< SingleTypeCacheKey, VkDescriptorSet, SingleTypeCacheKeyHash > m_SingleTypeSets
Definition: DescriptorManager.h:153
const VkDescriptorSetLayout & GetDescriptorIndexingSetLayout() const
Definition: DescriptorManager.h:83
static constexpr uint32_t DESCRIPTOR_INDEXING_BINDING_SIZE
Definition: DescriptorManager.h:124
bool m_UseDescriptorIndexing
Definition: DescriptorManager.h:116
std::vector< VkDescriptorSetLayout > m_DescriptorSetLayouts
Definition: DescriptorManager.h:122
std::pair< VkDescriptorSet, bool > GetSingleTypeDescritorSetImpl(VkDescriptorType type, VkDescriptorSetLayout layout, const std::vector< DeviceObjectUID > &uids)
Definition: DescriptorManager.cpp:227
std::unordered_map< DeviceObjectUID, uint32_t > m_TextureToBindingMap
Definition: DescriptorManager.h:136
uint32_t GetTextureDescriptor(CTexture *texture)
Definition: DescriptorManager.cpp:371
VkDescriptorSetLayout m_UniformDescriptorSetLayout
Definition: DescriptorManager.h:121
VkDescriptorPool m_DescriptorIndexingPool
Definition: DescriptorManager.h:118
void OnBufferDestroy(const DeviceObjectUID uid)
Definition: DescriptorManager.cpp:441
std::array< DescriptorIndexingBindingMap, NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET > m_DescriptorIndexingBindings
Definition: DescriptorManager.h:135
VkDescriptorSetLayout m_DescriptorIndexingSetLayout
Definition: DescriptorManager.h:120
VkDescriptorSet m_DescriptorIndexingSet
Definition: DescriptorManager.h:119
void OnDeviceObjectDestroy(const DeviceObjectUID uid)
Definition: DescriptorManager.cpp:446
std::unordered_map< VkDescriptorType, std::vector< SingleTypePool > > m_SingleTypePools
Definition: DescriptorManager.h:138
void OnTextureDestroy(const DeviceObjectUID uid)
Definition: DescriptorManager.cpp:418
VkDescriptorSet GetSingleTypeDescritorSet(VkDescriptorType type, VkDescriptorSetLayout layout, const std::vector< DeviceObjectUID > &texturesUID, const std::vector< CTexture * > &textures)
Definition: DescriptorManager.cpp:269
CDescriptorManager(CDevice *device, const bool useDescriptorIndexing)
Definition: DescriptorManager.cpp:48
std::unordered_map< DeviceObjectUID, std::vector< SingleTypePoolReference > > m_UIDToSingleTypePoolMap
Definition: DescriptorManager.h:146
SingleTypePool & GetSingleTypePool(const VkDescriptorType type, const uint32_t size)
Definition: DescriptorManager.cpp:165
~CDescriptorManager()
Definition: DescriptorManager.cpp:141
void CollectStatistics(IDevice::StatisticsVector &statistics) const
Definition: DescriptorManager.cpp:467
const std::vector< VkDescriptorSetLayout > & GetDescriptorSetLayouts() const
Definition: DescriptorManager.h:87
Definition: Device.h:67
CDescriptorManager & GetDescriptorManager()
Definition: Device.h:184
VkDescriptorSetLayout GetDescriptorSetLayout()
Definition: DescriptorManager.h:210
std::vector< DeviceObject * > m_BoundDeviceObjects
Definition: DescriptorManager.h:223
CSingleTypeDescriptorSetBinding(CDevice *device, const VkDescriptorType type, const uint32_t size, std::unordered_map< CStrIntern, uint32_t > mapping)
Definition: DescriptorManager.h:163
bool m_Outdated
Definition: DescriptorManager.h:219
void Unbind()
Definition: DescriptorManager.h:203
VkDescriptorSetLayout m_DescriptorSetLayout
Definition: DescriptorManager.h:221
int32_t GetBindingSlot(const CStrIntern name) const
Definition: DescriptorManager.h:173
void SetObject(const int32_t bindingSlot, DeviceObject *object)
Definition: DescriptorManager.h:179
const std::vector< DeviceObject * > & GetBoundDeviceObjects() const
Definition: DescriptorManager.h:212
const VkDescriptorType m_Type
Definition: DescriptorManager.h:216
VkDescriptorSet UpdateAndReturnDescriptorSet()
Definition: DescriptorManager.h:190
CDevice *const m_Device
Definition: DescriptorManager.h:215
const std::unordered_map< CStrIntern, uint32_t > m_Mapping
Definition: DescriptorManager.h:217
std::vector< DeviceObjectUID > m_BoundUIDs
Definition: DescriptorManager.h:224
bool IsOutdated() const
Definition: DescriptorManager.h:188
Definition: Texture.h:43
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Definition: Buffer.cpp:37
static constexpr DeviceObjectUID INVALID_DEVICE_OBJECT_UID
Definition: DeviceObjectUID.h:41
uint32_t DeviceObjectUID
Unique identifier for a device object.
Definition: DeviceObjectUID.h:40
Definition: Canvas2D.h:30
Backend
Definition: Backend.h:28
Definition: Canvas2D.h:30
Definition: ShaderDefines.cpp:43
std::vector< int16_t > elements
Definition: DescriptorManager.h:131
std::unordered_map< DeviceObjectUID, int16_t > map
Definition: DescriptorManager.h:132
size_t operator()(const SingleTypeCacheKey &key) const
Definition: DescriptorManager.cpp:218
VkDescriptorType type
Definition: DescriptorManager.h:141
VkDescriptorSet set
Definition: DescriptorManager.h:100
static constexpr int16_t INVALID_INDEX
Definition: DescriptorManager.h:97
int16_t firstFreeIndex
Definition: DescriptorManager.h:96
VkDescriptorPool pool
Definition: DescriptorManager.h:95
std::vector< Element > elements
Definition: DescriptorManager.h:104
VkDescriptorSetLayout layout
Definition: DescriptorManager.h:94
pthread_key_t key
Definition: wpthread.cpp:141