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