| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/vk/VulkanDescriptorPool.h" |
| 9 |
|
| 10 |
#include "include/private/base/SkTArray.h" |
| 11 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 12 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 13 |
|
| 14 |
namespace skgpu::graphite { |
| 15 |
|
| 16 |
sk_sp<VulkanDescriptorPool> VulkanDescriptorPool::Make(const VulkanSharedContext* context, |
| 17 |
SkSpan<DescriptorData> requestedDescCounts, |
| 18 |
VkDescriptorSetLayout layout) { |
| 19 |
|
| 20 |
if (requestedDescCounts.empty()) { |
| 21 |
return nullptr; |
| 22 |
} |
| 23 |
|
| 24 |
// For each requested descriptor type and count, create a VkDescriptorPoolSize struct which |
| 25 |
// specifies the descriptor type and quantity for pool creation. Multiple pool size structures |
| 26 |
// may contain the same descriptor type - the pool will be created with enough storage for the |
| 27 |
// total number of descriptors of each type. Source: |
| 28 |
// https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VkDescriptorPoolSize |
| 29 |
// Note: The kMaxNumDescriptors limit could be evaded since we do not currently track and check |
| 30 |
// the cumulative quantities of each type of descriptor, but this is an internal call and it is |
| 31 |
// highly unexpected for us to exceed this limit in practice. |
| 32 |
skia_private::STArray<kDescriptorTypeCount, VkDescriptorPoolSize> poolSizes; |
| 33 |
for (size_t i = 0; i < requestedDescCounts.size(); i++) { |
| 34 |
SkASSERT(requestedDescCounts[i].fCount > 0); |
| 35 |
if (requestedDescCounts[i].fCount > kMaxNumDescriptors) { |
| 36 |
SkDebugf("The number of descriptors requested, %u, exceeds the maximum allowed (%d).\n", |
| 37 |
requestedDescCounts[i].fCount, |
| 38 |
kMaxNumDescriptors); |
| 39 |
return nullptr; |
| 40 |
} |
| 41 |
VkDescriptorPoolSize& poolSize = poolSizes.push_back(); |
| 42 |
poolSize = {}; |
| 43 |
// Map each DescriptorSetType to the appropriate backend VkDescriptorType |
| 44 |
poolSize.type = DsTypeEnumToVkDs(requestedDescCounts[i].fType); |
| 45 |
// Create a pool large enough to accommodate the maximum possible number of descriptor sets |
| 46 |
poolSize.descriptorCount = requestedDescCounts[i].fCount * kMaxNumSets; |
| 47 |
} |
| 48 |
|
| 49 |
VkDescriptorPoolCreateInfo createInfo = {}; |
| 50 |
createInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; |
| 51 |
createInfo.maxSets = kMaxNumSets; |
| 52 |
createInfo.poolSizeCount = requestedDescCounts.size(); |
| 53 |
createInfo.pPoolSizes = &poolSizes.front(); |
| 54 |
|
| 55 |
VkDescriptorPool pool; |
| 56 |
VkResult result; |
| 57 |
VULKAN_CALL_RESULT(context, |
| 58 |
result, |
| 59 |
CreateDescriptorPool(context->device(), |
| 60 |
&createInfo, |
| 61 |
/*const VkAllocationCallbacks*=*/nullptr, |
| 62 |
&pool)); |
| 63 |
if (result != VK_SUCCESS) { |
| 64 |
VULKAN_CALL(context->interface(), |
| 65 |
DestroyDescriptorSetLayout(context->device(), layout, nullptr)); |
| 66 |
return nullptr; |
| 67 |
} |
| 68 |
return sk_sp<VulkanDescriptorPool>(new VulkanDescriptorPool(context, pool, layout)); |
| 69 |
} |
| 70 |
|
| 71 |
VulkanDescriptorPool::VulkanDescriptorPool(const VulkanSharedContext* context, |
| 72 |
VkDescriptorPool pool, |
| 73 |
VkDescriptorSetLayout layout) |
| 74 |
: fSharedContext(context) |
| 75 |
, fDescPool(pool) |
| 76 |
, fDescSetLayout(layout) {} |
| 77 |
|
| 78 |
VulkanDescriptorPool::~VulkanDescriptorPool() { |
| 79 |
// Destroying the VkDescriptorPool will automatically free and delete any VkDescriptorSets |
| 80 |
// allocated from the pool. |
| 81 |
VULKAN_CALL(fSharedContext->interface(), |
| 82 |
DestroyDescriptorPool(fSharedContext->device(), fDescPool, nullptr)); |
| 83 |
if (fDescSetLayout != VK_NULL_HANDLE) { |
| 84 |
VULKAN_CALL(fSharedContext->interface(), |
| 85 |
DestroyDescriptorSetLayout(fSharedContext->device(), fDescSetLayout, nullptr)); |
| 86 |
fDescSetLayout = VK_NULL_HANDLE; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace skgpu::graphite |