跳到主要内容

vkCreateCommandPool

函数原型

VkResult vkCreateCommandPool(
VkDevice device,
const VkCommandPoolCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkCommandPool* pCommandPool);

描述

创建命令缓冲池。

参数

  • device : 用于创建命令缓冲池的逻辑设备。
  • pCreateInfo : VkCommandPoolCreateInfo 结构体指针,包含创建命令缓冲池的信息。
  • pAllocator : host memory 分配器。
  • pCommandPool : 创建返回的命令缓冲池句柄。

补充

VkCommandPoolCreateInfo 结构体定义:

typedef struct VkCommandPoolCreateInfo {
VkStructureType sType;
const void* pNext;
VkCommandPoolCreateFlags flags; //设置命令缓冲的分配行为
uint32_t queueFamilyIndex; //队列族索引
} VkCommandPoolCreateInfo;

VkCommandPoolCreateFlags 枚举定义:

typedef enum VkCommandPoolCreateFlagBits {
VK_COMMAND_POOL_CREATE_TRANSIENT_BIT = 0x00000001, //在较短时间内会重复重置或释放
VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT = 0x00000002, //命令缓冲允许使用vkResetCommandBuffer或vkBeginCommandBuffer进行重置,否则不允许使用
VK_COMMAND_POOL_CREATE_PROTECTED_BIT = 0x00000004, //受保护的命令缓冲
} VkCommandPoolCreateFlagBits;

返回值

  • VK_SUCCESS : 成功创建命令缓冲池。
  • 其他 : 创建失败。

代码示例

// 查询设备的可用队列族,-1代表无效
struct QueueFamilyIndices {
// 支持图像绘制的队列
uint32_t graphicsFamily = -1;

// 支持图形呈现的队列
uint32_t presentFamily = -1;

bool isComplete() {
return (graphicsFamily >= 0) && (presentFamily >= 0);
}
};

// 查询可用的图形队列和呈现队列
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
QueueFamilyIndices indices;

uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());

int index = 0;
for (const auto& queueFamily : queueFamilies) {
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { // 目前,我们只要寻找图形队列即可
indices.graphicsFamily = index;
}

VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, index, surface, &presentSupport);
if (presentSupport) {
indices.presentFamily = index;
}

if (indices.isComplete()) {
break;
}
index++;
}

return indices;
}

// 创建指令池
void createCommandPool() {
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);

VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;

if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
throw std::runtime_error("Failed to create command pool!");
}
}