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 : #include "precompiled.h"
19 :
20 : #include "DescriptorManager.h"
21 :
22 : #include "lib/hash.h"
23 : #include "ps/CLogger.h"
24 : #include "ps/containers/StaticVector.h"
25 : #include "renderer/backend/Format.h"
26 : #include "renderer/backend/IBuffer.h"
27 : #include "renderer/backend/ITexture.h"
28 : #include "renderer/backend/Sampler.h"
29 : #include "renderer/backend/vulkan/Buffer.h"
30 : #include "renderer/backend/vulkan/Device.h"
31 : #include "renderer/backend/vulkan/DeviceSelection.h"
32 : #include "renderer/backend/vulkan/Texture.h"
33 : #include "renderer/backend/vulkan/Utilities.h"
34 :
35 : #include <array>
36 : #include <iterator>
37 : #include <numeric>
38 :
39 : namespace Renderer
40 : {
41 :
42 : namespace Backend
43 : {
44 :
45 : namespace Vulkan
46 : {
47 :
48 0 : CDescriptorManager::CDescriptorManager(CDevice* device, const bool useDescriptorIndexing)
49 0 : : m_Device(device), m_UseDescriptorIndexing(useDescriptorIndexing),
50 0 : m_ErrorTexture(device->CreateTexture(
51 : "DescriptorManagerErrorTexture", ITexture::Type::TEXTURE_2D,
52 : ITexture::Usage::TRANSFER_DST | ITexture::Usage::SAMPLED,
53 : Format::R8G8B8A8_UNORM, 1, 1,
54 0 : Sampler::MakeDefaultSampler(Sampler::Filter::NEAREST, Sampler::AddressMode::REPEAT),
55 0 : 1, 1))
56 : {
57 0 : if (useDescriptorIndexing)
58 : {
59 0 : VkDescriptorPoolSize descriptorPoolSize{};
60 0 : descriptorPoolSize.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
61 0 : descriptorPoolSize.descriptorCount = DESCRIPTOR_INDEXING_BINDING_SIZE * NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET;
62 :
63 0 : VkDescriptorPoolCreateInfo descriptorPoolCreateInfo{};
64 0 : descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
65 0 : descriptorPoolCreateInfo.poolSizeCount = 1;
66 0 : descriptorPoolCreateInfo.pPoolSizes = &descriptorPoolSize;
67 0 : descriptorPoolCreateInfo.maxSets = 1;
68 0 : descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT;
69 0 : ENSURE_VK_SUCCESS(vkCreateDescriptorPool(
70 : device->GetVkDevice(), &descriptorPoolCreateInfo, nullptr, &m_DescriptorIndexingPool));
71 :
72 0 : const VkShaderStageFlags stageFlags =
73 : VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT;
74 0 : const std::array<VkDescriptorSetLayoutBinding, NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET> bindings{{
75 : {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, DESCRIPTOR_INDEXING_BINDING_SIZE, stageFlags},
76 : {1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, DESCRIPTOR_INDEXING_BINDING_SIZE, stageFlags},
77 : {2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, DESCRIPTOR_INDEXING_BINDING_SIZE, stageFlags}
78 : }};
79 :
80 0 : const VkDescriptorBindingFlagsEXT baseBindingFlags =
81 : VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT
82 : | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT
83 : | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT;
84 0 : const std::array<VkDescriptorBindingFlagsEXT, NUMBER_OF_BINDINGS_PER_DESCRIPTOR_INDEXING_SET> bindingFlags{{
85 : baseBindingFlags, baseBindingFlags, baseBindingFlags
86 : }};
87 :
88 0 : VkDescriptorSetLayoutBindingFlagsCreateInfoEXT bindingFlagsCreateInfo{};
89 0 : bindingFlagsCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT;
90 0 : bindingFlagsCreateInfo.bindingCount = bindingFlags.size();
91 0 : bindingFlagsCreateInfo.pBindingFlags = bindingFlags.data();
92 :
93 0 : VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo{};
94 0 : descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
95 0 : descriptorSetLayoutCreateInfo.bindingCount = bindings.size();
96 0 : descriptorSetLayoutCreateInfo.pBindings = bindings.data();
97 0 : descriptorSetLayoutCreateInfo.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT;
98 0 : descriptorSetLayoutCreateInfo.pNext = &bindingFlagsCreateInfo;
99 :
100 0 : ENSURE_VK_SUCCESS(vkCreateDescriptorSetLayout(
101 : device->GetVkDevice(), &descriptorSetLayoutCreateInfo,
102 : nullptr, &m_DescriptorIndexingSetLayout));
103 :
104 0 : m_DescriptorSetLayouts.emplace_back(m_DescriptorIndexingSetLayout);
105 :
106 0 : VkDescriptorSetAllocateInfo descriptorSetAllocateInfo{};
107 0 : descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
108 0 : descriptorSetAllocateInfo.descriptorPool = m_DescriptorIndexingPool;
109 0 : descriptorSetAllocateInfo.descriptorSetCount = 1;
110 0 : descriptorSetAllocateInfo.pSetLayouts = &m_DescriptorIndexingSetLayout;
111 :
112 0 : ENSURE_VK_SUCCESS(vkAllocateDescriptorSets(
113 : device->GetVkDevice(), &descriptorSetAllocateInfo, &m_DescriptorIndexingSet));
114 :
115 0 : for (DescriptorIndexingBindingMap& bindingMap : m_DescriptorIndexingBindings)
116 : {
117 0 : bindingMap.firstFreeIndex = 0;
118 0 : bindingMap.elements.resize(DESCRIPTOR_INDEXING_BINDING_SIZE);
119 0 : std::iota(bindingMap.elements.begin(), std::prev(bindingMap.elements.end()), 1);
120 0 : bindingMap.elements.back() = -1;
121 : }
122 : }
123 :
124 : // Currently we hard-code the layout for uniforms.
125 0 : const VkDescriptorSetLayoutBinding bindings[] =
126 : {
127 : {0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT}
128 : };
129 :
130 0 : VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo{};
131 0 : descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
132 0 : descriptorSetLayoutCreateInfo.bindingCount = std::size(bindings);
133 0 : descriptorSetLayoutCreateInfo.pBindings = bindings;
134 :
135 0 : ENSURE_VK_SUCCESS(vkCreateDescriptorSetLayout(
136 : device->GetVkDevice(), &descriptorSetLayoutCreateInfo,
137 : nullptr, &m_UniformDescriptorSetLayout));
138 0 : m_DescriptorSetLayouts.emplace_back(m_UniformDescriptorSetLayout);
139 0 : }
140 :
141 0 : CDescriptorManager::~CDescriptorManager()
142 : {
143 0 : VkDevice device = m_Device->GetVkDevice();
144 :
145 0 : for (auto& pair: m_SingleTypePools)
146 : {
147 0 : for (SingleTypePool& pool : pair.second)
148 : {
149 0 : if (pool.pool != VK_NULL_HANDLE)
150 0 : vkDestroyDescriptorPool(device, pool.pool, nullptr);
151 0 : if (pool.layout != VK_NULL_HANDLE)
152 0 : vkDestroyDescriptorSetLayout(device, pool.layout, nullptr);
153 : }
154 : }
155 0 : m_SingleTypePools.clear();
156 :
157 0 : for (VkDescriptorSetLayout descriptorSetLayout : m_DescriptorSetLayouts)
158 0 : vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
159 0 : m_DescriptorSetLayouts.clear();
160 :
161 0 : if (m_DescriptorIndexingPool != VK_NULL_HANDLE)
162 0 : vkDestroyDescriptorPool(device, m_DescriptorIndexingPool, nullptr);
163 0 : }
164 :
165 0 : CDescriptorManager::SingleTypePool& CDescriptorManager::GetSingleTypePool(
166 : const VkDescriptorType type, const uint32_t size)
167 : {
168 0 : ENSURE(size > 0 && size <= 16);
169 0 : std::vector<SingleTypePool>& pools = m_SingleTypePools[type];
170 0 : if (pools.size() <= size)
171 0 : pools.resize(size + 1);
172 0 : SingleTypePool& pool = pools[size];
173 0 : if (pool.pool == VK_NULL_HANDLE)
174 : {
175 0 : constexpr uint32_t maxSets = 16384;
176 :
177 0 : VkDescriptorPoolSize descriptorPoolSize{};
178 0 : descriptorPoolSize.type = type;
179 0 : descriptorPoolSize.descriptorCount = maxSets * size;
180 :
181 0 : VkDescriptorPoolCreateInfo descriptorPoolCreateInfo{};
182 0 : descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
183 0 : descriptorPoolCreateInfo.poolSizeCount = 1;
184 0 : descriptorPoolCreateInfo.pPoolSizes = &descriptorPoolSize;
185 0 : descriptorPoolCreateInfo.maxSets = maxSets;
186 0 : ENSURE_VK_SUCCESS(vkCreateDescriptorPool(
187 : m_Device->GetVkDevice(), &descriptorPoolCreateInfo, nullptr, &pool.pool));
188 :
189 0 : const VkPipelineStageFlags stageFlags =
190 : VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT;
191 0 : PS::StaticVector<VkDescriptorSetLayoutBinding, 16> bindings;
192 0 : for (uint32_t index = 0; index < size; ++index)
193 0 : bindings.emplace_back(VkDescriptorSetLayoutBinding{index, type, 1, stageFlags});
194 :
195 0 : VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo{};
196 0 : descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
197 0 : descriptorSetLayoutCreateInfo.bindingCount = size;
198 0 : descriptorSetLayoutCreateInfo.pBindings = bindings.data();
199 :
200 0 : ENSURE_VK_SUCCESS(vkCreateDescriptorSetLayout(
201 : m_Device->GetVkDevice(), &descriptorSetLayoutCreateInfo, nullptr, &pool.layout));
202 :
203 0 : pool.firstFreeIndex = 0;
204 0 : pool.elements.reserve(maxSets);
205 0 : for (uint32_t index = 0; index < maxSets; ++index)
206 0 : pool.elements.push_back({VK_NULL_HANDLE, 1, static_cast<int16_t>(index + 1)});
207 0 : pool.elements.back().nextFreeIndex = SingleTypePool::INVALID_INDEX;
208 : }
209 0 : return pool;
210 : }
211 :
212 0 : VkDescriptorSetLayout CDescriptorManager::GetSingleTypeDescritorSetLayout(
213 : VkDescriptorType type, const uint32_t size)
214 : {
215 0 : return GetSingleTypePool(type, size).layout;
216 : }
217 :
218 0 : size_t CDescriptorManager::SingleTypeCacheKeyHash::operator()(const SingleTypeCacheKey& key) const
219 : {
220 0 : size_t seed = 0;
221 0 : hash_combine(seed, key.first);
222 0 : for (DeviceObjectUID uid : key.second)
223 0 : hash_combine(seed, uid);
224 0 : return seed;
225 : }
226 :
227 0 : std::pair<VkDescriptorSet, bool> CDescriptorManager::GetSingleTypeDescritorSetImpl(
228 : VkDescriptorType type, VkDescriptorSetLayout layout,
229 : const std::vector<DeviceObjectUID>& uids)
230 : {
231 0 : ENSURE(!uids.empty());
232 0 : const SingleTypeCacheKey key{layout, uids};
233 0 : auto it = m_SingleTypeSets.find(key);
234 0 : bool created = false;
235 0 : if (it == m_SingleTypeSets.end())
236 : {
237 0 : SingleTypePool& pool = GetSingleTypePool(type, uids.size());
238 0 : const int16_t elementIndex = pool.firstFreeIndex;
239 0 : ENSURE(elementIndex != SingleTypePool::INVALID_INDEX);
240 0 : SingleTypePool::Element& element = pool.elements[elementIndex];
241 0 : ENSURE(pool.firstFreeIndex != element.nextFreeIndex);
242 0 : pool.firstFreeIndex = element.nextFreeIndex;
243 0 : ++element.version;
244 : // Occupy the index.
245 0 : element.nextFreeIndex = SingleTypePool::INVALID_INDEX;
246 :
247 0 : if (element.set == VK_NULL_HANDLE)
248 : {
249 0 : VkDescriptorSetAllocateInfo descriptorSetAllocateInfo{};
250 0 : descriptorSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
251 0 : descriptorSetAllocateInfo.descriptorPool = pool.pool;
252 0 : descriptorSetAllocateInfo.descriptorSetCount = 1;
253 0 : descriptorSetAllocateInfo.pSetLayouts = &layout;
254 :
255 0 : ENSURE_VK_SUCCESS(vkAllocateDescriptorSets(
256 : m_Device->GetVkDevice(), &descriptorSetAllocateInfo, &element.set));
257 : }
258 :
259 0 : it = m_SingleTypeSets.emplace(key, element.set).first;
260 0 : created = true;
261 :
262 0 : for (const DeviceObjectUID uid : uids)
263 0 : if (uid != INVALID_DEVICE_OBJECT_UID)
264 0 : m_UIDToSingleTypePoolMap[uid].push_back({type, element.version, elementIndex, static_cast<uint8_t>(uids.size())});
265 : }
266 0 : return {it->second, created};
267 : }
268 :
269 0 : VkDescriptorSet CDescriptorManager::GetSingleTypeDescritorSet(
270 : VkDescriptorType type, VkDescriptorSetLayout layout,
271 : const std::vector<DeviceObjectUID>& texturesUID,
272 : const std::vector<CTexture*>& textures)
273 : {
274 0 : ENSURE(texturesUID.size() == textures.size());
275 0 : ENSURE(!texturesUID.empty());
276 0 : const auto [set, justCreated] = GetSingleTypeDescritorSetImpl(type, layout, texturesUID);
277 0 : if (!justCreated)
278 0 : return set;
279 :
280 0 : const VkImageLayout imageLayout = type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
281 0 : ? VK_IMAGE_LAYOUT_GENERAL
282 : : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
283 0 : PS::StaticVector<VkDescriptorImageInfo, 16> infos;
284 0 : for (CTexture* texture : textures)
285 : {
286 0 : if (!texture)
287 : {
288 : // We can use a default texture only for read-only bindings.
289 0 : ENSURE(type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER || type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
290 0 : texture = m_ErrorTexture->As<CTexture>();
291 : }
292 0 : ENSURE(texture->GetUsage() & ITexture::Usage::SAMPLED);
293 :
294 0 : VkDescriptorImageInfo descriptorImageInfo{};
295 0 : descriptorImageInfo.imageLayout = imageLayout;
296 0 : descriptorImageInfo.imageView = texture->GetSamplerImageView();
297 0 : descriptorImageInfo.sampler = texture->GetSampler();
298 0 : infos.emplace_back(std::move(descriptorImageInfo));
299 : }
300 :
301 0 : VkWriteDescriptorSet writeDescriptorSet{};
302 0 : writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
303 0 : writeDescriptorSet.dstSet = set;
304 0 : writeDescriptorSet.dstBinding = 0;
305 0 : writeDescriptorSet.dstArrayElement = 0;
306 0 : writeDescriptorSet.descriptorType = type;
307 0 : writeDescriptorSet.descriptorCount = static_cast<uint32_t>(infos.size());
308 0 : writeDescriptorSet.pImageInfo = infos.data();
309 :
310 0 : vkUpdateDescriptorSets(
311 0 : m_Device->GetVkDevice(), 1, &writeDescriptorSet, 0, nullptr);
312 :
313 0 : return set;
314 : }
315 :
316 0 : VkDescriptorSet CDescriptorManager::GetSingleTypeDescritorSet(
317 : VkDescriptorType type, VkDescriptorSetLayout layout,
318 : const std::vector<DeviceObjectUID>& buffersUID,
319 : const std::vector<CBuffer*>& buffers)
320 : {
321 0 : ENSURE(buffersUID.size() == buffers.size());
322 0 : ENSURE(!buffersUID.empty());
323 0 : const auto[set, justCreated] = GetSingleTypeDescritorSetImpl(type, layout, buffersUID);
324 0 : if (!justCreated)
325 0 : return set;
326 :
327 0 : ENSURE(
328 : type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
329 : type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER || type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
330 0 : const VkPhysicalDeviceLimits& physicalDeviceLimits = m_Device->GetChoosenPhysicalDevice().properties.limits;
331 0 : const uint32_t maxBufferRange =
332 0 : type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER || type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
333 0 : ? physicalDeviceLimits.maxStorageBufferRange
334 : : physicalDeviceLimits.maxUniformBufferRange;
335 :
336 0 : PS::StaticVector<VkDescriptorBufferInfo, 16> infos;
337 0 : std::transform(buffers.begin(), buffers.end(), std::back_inserter(infos),
338 0 : [maxBufferRange](CBuffer* buffer)
339 : {
340 0 : ENSURE(buffer);
341 0 : ENSURE(buffer->GetUsage() & IBuffer::Usage::STORAGE);
342 0 : ENSURE(buffer->GetSize() <= maxBufferRange);
343 :
344 0 : VkDescriptorBufferInfo descriptorBufferInfo{};
345 0 : descriptorBufferInfo.buffer = buffer->GetVkBuffer();
346 0 : descriptorBufferInfo.offset = 0;
347 0 : descriptorBufferInfo.range = buffer->GetSize();
348 0 : return descriptorBufferInfo;
349 : });
350 :
351 0 : VkWriteDescriptorSet writeDescriptorSet{};
352 0 : writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
353 0 : writeDescriptorSet.dstSet = set;
354 0 : writeDescriptorSet.dstBinding = 0;
355 0 : writeDescriptorSet.dstArrayElement = 0;
356 0 : writeDescriptorSet.descriptorType = type;
357 0 : writeDescriptorSet.descriptorCount = static_cast<uint32_t>(infos.size());
358 0 : writeDescriptorSet.pBufferInfo = infos.data();
359 :
360 0 : vkUpdateDescriptorSets(
361 0 : m_Device->GetVkDevice(), 1, &writeDescriptorSet, 0, nullptr);
362 :
363 0 : return set;
364 : }
365 :
366 0 : uint32_t CDescriptorManager::GetUniformSet() const
367 : {
368 0 : return m_UseDescriptorIndexing ? 1 : 0;
369 : }
370 :
371 0 : uint32_t CDescriptorManager::GetTextureDescriptor(CTexture* texture)
372 : {
373 0 : ENSURE(m_UseDescriptorIndexing);
374 0 : ENSURE(texture->GetUsage() & ITexture::Usage::SAMPLED);
375 :
376 0 : uint32_t binding = 0;
377 0 : if (texture->GetType() == ITexture::Type::TEXTURE_2D &&
378 0 : IsDepthFormat(texture->GetFormat()) &&
379 0 : texture->IsCompareEnabled())
380 : binding = 2;
381 0 : else if (texture->GetType() == ITexture::Type::TEXTURE_CUBE)
382 0 : binding = 1;
383 :
384 0 : DescriptorIndexingBindingMap& bindingMap = m_DescriptorIndexingBindings[binding];
385 0 : auto it = bindingMap.map.find(texture->GetUID());
386 0 : if (it != bindingMap.map.end())
387 0 : return it->second;
388 0 : m_TextureToBindingMap[texture->GetUID()] = binding;
389 :
390 0 : ENSURE(bindingMap.firstFreeIndex != -1);
391 0 : uint32_t descriptorSetIndex = bindingMap.firstFreeIndex;
392 0 : bindingMap.firstFreeIndex = bindingMap.elements[bindingMap.firstFreeIndex];
393 :
394 0 : ENSURE(texture->GetType() != ITexture::Type::TEXTURE_2D_MULTISAMPLE);
395 :
396 0 : VkDescriptorImageInfo descriptorImageInfo{};
397 0 : descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
398 0 : descriptorImageInfo.imageView = texture->GetSamplerImageView();
399 0 : descriptorImageInfo.sampler = texture->GetSampler();
400 :
401 0 : VkWriteDescriptorSet writeDescriptorSet{};
402 0 : writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
403 0 : writeDescriptorSet.dstSet = m_DescriptorIndexingSet;
404 0 : writeDescriptorSet.dstBinding = binding;
405 0 : writeDescriptorSet.dstArrayElement = descriptorSetIndex;
406 0 : writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
407 0 : writeDescriptorSet.descriptorCount = 1;
408 0 : writeDescriptorSet.pImageInfo = &descriptorImageInfo;
409 :
410 0 : vkUpdateDescriptorSets(
411 0 : m_Device->GetVkDevice(), 1, &writeDescriptorSet, 0, nullptr);
412 :
413 0 : bindingMap.map[texture->GetUID()] = descriptorSetIndex;
414 :
415 0 : return descriptorSetIndex;
416 : }
417 :
418 0 : void CDescriptorManager::OnTextureDestroy(const DeviceObjectUID uid)
419 : {
420 0 : ENSURE(uid != INVALID_DEVICE_OBJECT_UID);
421 0 : if (m_UseDescriptorIndexing)
422 : {
423 0 : DescriptorIndexingBindingMap& bindingMap =
424 0 : m_DescriptorIndexingBindings[m_TextureToBindingMap[uid]];
425 0 : auto it = bindingMap.map.find(uid);
426 : // It's possible to not have the texture in the map. Because a texture will
427 : // be added to it only in case of usage.
428 0 : if (it == bindingMap.map.end())
429 0 : return;
430 0 : const int16_t index = it->second;
431 0 : bindingMap.elements[index] = bindingMap.firstFreeIndex;
432 0 : bindingMap.firstFreeIndex = index;
433 0 : bindingMap.map.erase(it);
434 : }
435 : else
436 : {
437 0 : OnDeviceObjectDestroy(uid);
438 : }
439 : }
440 :
441 0 : void CDescriptorManager::OnBufferDestroy(const DeviceObjectUID uid)
442 : {
443 0 : OnDeviceObjectDestroy(uid);
444 0 : }
445 :
446 0 : void CDescriptorManager::OnDeviceObjectDestroy(const DeviceObjectUID uid)
447 : {
448 0 : auto it = m_UIDToSingleTypePoolMap.find(uid);
449 0 : if (it == m_UIDToSingleTypePoolMap.end())
450 0 : return;
451 0 : for (const auto& entry : it->second)
452 : {
453 0 : SingleTypePool& pool = GetSingleTypePool(entry.type, entry.size);
454 0 : SingleTypePool::Element& element = pool.elements[entry.elementIndex];
455 : // Multiple textures might be used by the same descriptor set and
456 : // we don't need to reset it if it was already.
457 0 : if (element.version == entry.version && element.nextFreeIndex == SingleTypePool::INVALID_INDEX)
458 : {
459 0 : ENSURE(pool.firstFreeIndex != entry.elementIndex);
460 0 : element.nextFreeIndex = pool.firstFreeIndex;
461 0 : pool.firstFreeIndex = entry.elementIndex;
462 : }
463 : }
464 0 : m_UIDToSingleTypePoolMap.erase(it);
465 : }
466 :
467 0 : void CDescriptorManager::CollectStatistics(IDevice::StatisticsVector& statistics) const
468 : {
469 0 : const uint32_t descriptorPoolCount{std::transform_reduce(
470 : m_SingleTypePools.begin(), m_SingleTypePools.end(), 0u, std::plus(),
471 0 : [](const auto& cacheItem)
472 : {
473 0 : return static_cast<uint32_t>(cacheItem.second.size());
474 0 : })};
475 :
476 0 : statistics.emplace_back("Cached VkDescriptorPool count", "", descriptorPoolCount);
477 0 : statistics.emplace_back("Cached VkDescriptorSet count", "", static_cast<uint32_t>(m_SingleTypeSets.size()));
478 0 : }
479 :
480 : } // namespace Vulkan
481 :
482 : } // namespace Backend
483 :
484 : } // namespace Renderer
|