This section will explain the different APIs that can be used to manage the command pool and command buffers. Here, we will understand the process of creating a command buffer pool that will be used for command buffer allocation. We will also look at the process of resetting and destroying APIs.
The next section of this chapter will be based on implementing these APIs in a ready-to-use wrapper class. The wrapper implementation will be highly useful in the remaining chapters of this book, where we will extensively make use of the command buffer.
A command pool is created using the vkCreateCommandPool()
API. It accepts a VkCommandPoolCreateInfo
control structure, which guides the implementation about the nature of command buffers that are going to be allocated from this pool. It also indicates which queue family it should belong to. This information, which is provided in advance, is useful to allocate compatible command pools; these pools can be used to optimize the command buffer allocation process for a typical queue submission. Here's the syntax of this:
typedef struct VkCommandPoolCreateInfo { VkStructureType sType; const void* next; VkCommandPoolCreateFlags flags; uint32_t queueFamilyIndex; } VkCommandPoolCreateInfo;
The following table describes the various fields of VkCommandPoolCreateInfo
:
Parameters |
Description |
|
This is the type information of this control structure. It must be specified as |
|
This could be a valid pointer to an extension-specific structure or |
|
This represents a bitwise enum flag that indicates the behavior of the command pool usage and the command buffer being allocated from it. This enum flag is of the type |
|
This indicates the queue family to which the command buffer is intended to be submitted. |
The VK_COMMAND_POOL_CREATE_TRANSIENT_BIT
enum flag indicates that the command buffers allocated from this pool will be changed frequently and have a shorter lifespan. This means that the buffers will be reset or freed in a relatively short timeframe. This flag notifies the implementation about the nature of the command buffer, and this can be used to control the memory allocation behavior within the pool.
When the VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT
flag is set, it indicates that a command buffer allocated from the pool can be individually reset in two ways: either explicitly by calling vkResetCommandBuffer
or implicitly by calling vkBeginCommandBuffer
. If this flag is not set, then these two APIs must not be called on the executable command buffers allocated from the pool. This indicates that they can only be reset in bulk by calling vkResetCommandPool
.
The command pool in Vulkan is represented by the VkCommandPool
object. It is created using the vkCreateCommandPool()
API. The following is the syntax and description of the API:
VkResult vkCreateCommandPool( VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool);
The following table describes the various fields of this API:
Parameters |
Description |
|
This is the handle of the device that will create the command pool. |
|
This refers to the |
|
This controls host memory allocation. |
|
This represents the |
The command pool (VkCommandPool
) can be reset using the vkResetCommandPool()
API. Here is the syntax of this API:
VkResult vkResetCommandPool ( VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags);
The following are the parameters of this API:
Parameters |
Description |
|
This is the handle of the device that owns the command pool. |
|
This refers to the |
|
This flag controls the behavior of resetting the pool. |
A command pool can be destroyed using the vkDestroyCommandPool()
API. Here's the syntax of this:
VkResult vkDestroyCommandPool( VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* allocator);
The following are the parameters of this API:
Parameters |
Description |
|
This is the handle of the device that destroys the command pool. |
|
This refers to the |
|
This controls host memory allocation. For more information, refer to the Host memory section in Chapter 5, Command Buffer and Memory Management in Vulkan. |
Command buffers are allocated from the command buffer pool (VkCommandPool
) using the vkAllocateCommandBuffers()
API. This API needs the VkCommandBufferAllocateInfo
control structure object, which specifies the various parameters that will be helpful in the allocation process. Upon successful execution of the API, it returns the VkCommandBuffer
object. The following is the syntax of this API:
VkResult vkAllocateCommandBuffers( VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Let's take a look at the API fields:
Parameters |
Description |
|
This is the handle of the logical device object that owns the command pool. |
|
This is a pointer to a |
|
This refers to the allocated command buffer object array. |
The VkCommandBufferAllocateInfo
structure has the following fields.
Parameters |
Description |
|
This refers to the type information telling Vulkan that it is a |
|
This refers to |
|
This is the command pool handle from which memory needs to be allocated for the requested command buffers. |
|
This is the bitwise enum flag of the type typedef enum VkCommandBufferLevel { VK_COMMAND_BUFFER_LEVEL_PRIMARY=0, VK_COMMAND_BUFFER_LEVEL_SECONDARY=1, } VkCommandBufferLevel;
|
|
This refers to the number of command buffers that need to be allocated. |
The allocated command buffer can be reset using the vkResetCommandBuffer()
API. This API accepts the VkCommandBuffer
object as the first parameter that needs to be reset. The second parameter is a bitwise mask, VkCommandBufferResetFlag
, which controls the behavior of the reset operation. This structure has one enum value, namely VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT
. When this value is set, it means that the memory held by the command buffer will be returned to the parent command pool.
The following is the syntax of the API:
VkResult vkResetCommandBuffer( VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags);
One or more command buffers can be released using the vkFreeCommandBuffer()
API. Here's the syntax of this API:
void vkFreeCommandBuffers( VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers);
The following are the parameters of the vkFreeCommandBuffers()
API along with their respective descriptions:
Parameters |
Description |
|
This refers to the logical device that holds the command pool. |
|
This refers to the associated command pool from which the memory has to be released. |
|
This refers to the number of command buffers that need to be released. |
|
This is an array of command buffer handles that needs to be released. |
3.15.221.133