summaryrefslogtreecommitdiff
path: root/tools/gfx/vulkan/vk-descriptor-allocator.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2021-02-24 15:43:43 -0800
committerGitHub <noreply@github.com>2021-02-24 15:43:43 -0800
commit9b7a007c31072bc9aebd1134aa4f1bfd28a4c541 (patch)
treeb71a48eb30b3b09ab4e77e40dc1c68ecd854ef82 /tools/gfx/vulkan/vk-descriptor-allocator.cpp
parentd66b30729029bdb43892e05c9c80fd56ac95a24f (diff)
Explicit swapchain interface in `gfx`. (#1726)
* Explicit swapchain interface in `gfx`. * Correctly return nullptr when `IRenderer` creation failed. * Fix crashes on CUDA tests. * Cleanups.
Diffstat (limited to 'tools/gfx/vulkan/vk-descriptor-allocator.cpp')
-rw-r--r--tools/gfx/vulkan/vk-descriptor-allocator.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/tools/gfx/vulkan/vk-descriptor-allocator.cpp b/tools/gfx/vulkan/vk-descriptor-allocator.cpp
new file mode 100644
index 000000000..c01e37642
--- /dev/null
+++ b/tools/gfx/vulkan/vk-descriptor-allocator.cpp
@@ -0,0 +1,70 @@
+#include "vk-descriptor-allocator.h"
+#include "vk-util.h"
+
+namespace gfx
+{
+VkDescriptorPool DescriptorSetAllocator::newPool()
+{
+ VkDescriptorPoolCreateInfo descriptorPoolInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
+ VkDescriptorPoolSize poolSizes[] = {
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1024},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 256},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 256},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4096},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4096},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 4096},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 4096},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 16},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT, 16},
+ VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 256}};
+ descriptorPoolInfo.maxSets = 4096;
+ descriptorPoolInfo.poolSizeCount = sizeof(poolSizes) / sizeof(VkDescriptorPoolSize);
+ descriptorPoolInfo.pPoolSizes = poolSizes;
+ descriptorPoolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
+
+ VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
+ SLANG_VK_CHECK(m_api->vkCreateDescriptorPool(
+ m_api->m_device, &descriptorPoolInfo, nullptr, &descriptorPool));
+ pools.add(descriptorPool);
+ return descriptorPool;
+}
+
+VulkanDescriptorSet DescriptorSetAllocator::allocate(VkDescriptorSetLayout layout)
+{
+ VulkanDescriptorSet rs = {};
+ VkDescriptorSetAllocateInfo allocInfo = {};
+ allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
+ allocInfo.descriptorPool = getPool();
+ allocInfo.descriptorSetCount = 1;
+ allocInfo.pSetLayouts = &layout;
+ if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
+ {
+ rs.pool = allocInfo.descriptorPool;
+ return rs;
+ }
+ // If allocation from last pool fails, try all existing pools.
+ for (Slang::Index i = 0; i < pools.getCount() - 1; i++)
+ {
+ allocInfo.descriptorPool = pools[i];
+ if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
+ {
+ rs.pool = allocInfo.descriptorPool;
+ return rs;
+ }
+ }
+ // If we still cannot allocate the descriptor set, add a new pool.
+ auto pool = newPool();
+ allocInfo.descriptorPool = pool;
+ if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
+ {
+ rs.pool = allocInfo.descriptorPool;
+ return rs;
+ }
+ // Failed to allocate from a new pool, we are in trouble.
+ assert(!"descriptor set allocation failed.");
+ return rs;
+}
+}