yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.6 KiB85 linesraw
1#include "vk-descriptor-allocator.h"
2
3#include "vk-util.h"
4
5namespace gfx
6{
7VkDescriptorPool DescriptorSetAllocator::newPool()
8{
9    VkDescriptorPoolCreateInfo descriptorPoolInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
10    Slang::Array<VkDescriptorPoolSize, 32> poolSizes;
11    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_SAMPLER, 1024});
12    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1024});
13    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096});
14    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024});
15    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 256});
16    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 256});
17    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 4096});
18    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4096});
19    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 4096});
20    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 4096});
21    poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 16});
22    if (m_api->m_extendedFeatures.inlineUniformBlockFeatures.inlineUniformBlock)
23    {
24        poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT, 16});
25    }
26    if (m_api->m_extendedFeatures.accelerationStructureFeatures.accelerationStructure)
27    {
28        poolSizes.add(VkDescriptorPoolSize{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 256});
29    }
30    descriptorPoolInfo.maxSets = 4096;
31    descriptorPoolInfo.poolSizeCount = (uint32_t)poolSizes.getCount();
32    descriptorPoolInfo.pPoolSizes = poolSizes.getBuffer();
33    descriptorPoolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
34
35    VkDescriptorPoolInlineUniformBlockCreateInfo inlineUniformBlockInfo = {
36        VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO};
37    inlineUniformBlockInfo.maxInlineUniformBlockBindings = 16;
38    descriptorPoolInfo.pNext = &inlineUniformBlockInfo;
39
40    VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
41    SLANG_VK_CHECK(m_api->vkCreateDescriptorPool(
42        m_api->m_device,
43        &descriptorPoolInfo,
44        nullptr,
45        &descriptorPool));
46    pools.add(descriptorPool);
47    return descriptorPool;
48}
49
50VulkanDescriptorSet DescriptorSetAllocator::allocate(VkDescriptorSetLayout layout)
51{
52    VulkanDescriptorSet rs = {};
53    VkDescriptorSetAllocateInfo allocInfo = {};
54    allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
55    allocInfo.descriptorPool = getPool();
56    allocInfo.descriptorSetCount = 1;
57    allocInfo.pSetLayouts = &layout;
58    if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
59    {
60        rs.pool = allocInfo.descriptorPool;
61        return rs;
62    }
63    // If allocation from last pool fails, try all existing pools.
64    for (Slang::Index i = 0; i < pools.getCount() - 1; i++)
65    {
66        allocInfo.descriptorPool = pools[i];
67        if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
68        {
69            rs.pool = allocInfo.descriptorPool;
70            return rs;
71        }
72    }
73    // If we still cannot allocate the descriptor set, add a new pool.
74    auto pool = newPool();
75    allocInfo.descriptorPool = pool;
76    if (m_api->vkAllocateDescriptorSets(m_api->m_device, &allocInfo, &rs.handle) == VK_SUCCESS)
77    {
78        rs.pool = allocInfo.descriptorPool;
79        return rs;
80    }
81    // Failed to allocate from a new pool, we are in trouble.
82    assert(!"descriptor set allocation failed.");
83    return rs;
84}
85} // namespace gfx