From 9b7a007c31072bc9aebd1134aa4f1bfd28a4c541 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 24 Feb 2021 15:43:43 -0800 Subject: Explicit swapchain interface in `gfx`. (#1726) * Explicit swapchain interface in `gfx`. * Correctly return nullptr when `IRenderer` creation failed. * Fix crashes on CUDA tests. * Cleanups. --- tools/gfx/vulkan/vk-descriptor-allocator.cpp | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tools/gfx/vulkan/vk-descriptor-allocator.cpp (limited to 'tools/gfx/vulkan/vk-descriptor-allocator.cpp') 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; +} +} -- cgit v1.2.3