To support a dynamic number of textures in a single descriptor set (e.g., for bindless rendering), use the VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT flag.
Workflow:
- Create a
VkDescriptorSetLayout with VkDescriptorSetLayoutBindingFlagsCreateInfo passed via pNext to define the variable count flag. - Create a
VkDescriptorPool with enough capacity. - When allocating the descriptor set, use
VkDescriptorSetVariableDescriptorCountAllocateInfo (via pNext in VkDescriptorSetAllocateInfo) to specify the actual number of descriptors needed. - Update the set using
vkUpdateDescriptorSets.
// 1. Define binding flags
VkDescriptorBindingFlags descVariableFlag{ VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT };
VkDescriptorSetLayoutBindingFlagsCreateInfo descBindingFlags{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.bindingCount = 1,
.pBindingFlags = &descVariableFlag
};
// 2. Create Layout
VkDescriptorSetLayoutCreateInfo descLayoutTexCI{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &descBindingFlags,
.bindingCount = 1,
.pBindings = &descLayoutBindingTex
};
chk(vkCreateDescriptorSetLayout(device, &descLayoutTexCI, nullptr, &descriptorSetLayoutTex));
// 3. Allocate with variable count
uint32_t variableDescCount{ static_cast<uint32_t>(textures.size()) };
VkDescriptorSetVariableDescriptorCountAllocateInfo variableDescCountAI{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT,
.descriptorSetCount = 1,
.pDescriptorCounts = &variableDescCount
};
VkDescriptorSetAllocateInfo texDescSetAlloc{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = &variableDescCountAI,
.descriptorPool = descriptorPool,
.descriptorSetCount = 1,
.pSetLayouts = &descriptorSetLayoutTex
};
chk(vkAllocateDescriptorSets(device, &texDescSetAlloc, &descriptorSetTex));