跳到主要内容

vkAllocateDescriptorSets

函数原型

VkResult vkAllocateDescriptorSets(
VkDevice device,
const VkDescriptorSetAllocateInfo* pAllocateInfo,
VkDescriptorSet* pDescriptorSets);

描述

分配描述符集(descriptor sets)。

参数

  • device : 包含描述符集合池的逻辑设备。
  • pAllocateInfo : VkDescriptorSetAllocateInfo 结构体指针,描述了如何分配描述符集。
  • pDescriptorSets : VkDescriptorSet句柄数组的指针,每个数组成员表示生成的描述符集对象。数组的长度为pAllocateInfo.descriptorSetCount。

补充

VkDescriptorSetAllocateInfo 结构体定义:

typedef struct VkDescriptorSetAllocateInfo {
VkStructureType sType;
const void* pNext;
VkDescriptorPool descriptorPool;
uint32_t descriptorSetCount;
const VkDescriptorSetLayout* pSetLayouts;
} VkDescriptorSetAllocateInfo;
  • sType : VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
  • descriptorPool : 分配描述符集的集合池。
  • descriptorSetCount : 想从描述符集合池中分配的描述符集数量。
  • pSetLayouts : 描述符集布局数组的指针,每个数组成员指定如何分配相应的描述符集合。

返回值

  • VK_SUCCESS : 成功分配命令缓冲区。
  • 其他 : 分配描述符集合失败。

代码示例

VkDescriptorPoolSize descriptorPoolSizes[2] = {0};
descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptorPoolSizes[0].descriptorCount = 1;
descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorPoolSizes[1].descriptorCount = 1;

VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
VkDescriptorPoolCreateInfo descriptorPoolInfo = {};
descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolInfo.poolSizeCount = 2;
descriptorPoolInfo.pPoolSizes = descriptorPoolSizes;
descriptorPoolInfo.maxSets = 1;
VK_CHECK(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));

VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
samplerLayoutBinding.binding = 1;
samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
samplerLayoutBinding.descriptorCount = 1;
samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;

VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo = {};
descriptorSetLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutInfo.bindingCount = 1;
descriptorSetLayoutInfo.pBindings = &samplerLayoutBinding;
VK_CHECK( vkCreateDescriptorSetLayout(device, &descriptorSetLayoutInfo, nullptr, &descriptorSetLayout) );

VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
VkDescriptorSetLayout descriptorSetLayouts[] = { descriptorSetLayout };
VkDescriptorSetAllocateInfo descriptorSetInfo = {};
descriptorSetInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO
descriptorSetInfo.descriptorPool = descriptorPool;
descriptorSetInfo.descriptorSetCount = 1;
descriptorSetInfo.pSetLayouts = descriptorSetLayouts;
VK_CHECK(vkAllocateDescriptorSets(device, &descriptorSetInfo, &descriptorSet));