In the previous chapter, we learned about Vulkan resource types; we understood what image resources (VkImage
) are and implemented them in the swapchain image. In this chapter, we will discuss the second type of Vulkan resource called buffer resources (VkBuffer
) and use them to prepare a simple geometry buffer.
This chapter will also introduce and implement a Render Pass and framebuffer. A Render Pass helps in assembling a single unit of work. It defines the attachments and subpasses associated with it that influence a single render job. A framebuffer consumes the created Render Pass and creates single-frame information for each corresponding swapchain image. Framebuffers associate a set of image views with the set of attachments described in a Render Pass.
Additionally, we will implement our first shader in Vulkan using SPIR-V, which is a binary intermediate language for shaders and kernels.
So, we will cover the following topics:
A buffer resource represents a contiguous array of data in a linear fashion. Buffer resources are commonly stored attribute data information, such as vertex coordinates, texture coordinates, associated colors, and more. The buffer resource in Vulkan is represented by the VkBuffer
object, unlike the image resource (VkImage
), which is represented in the view form (image view, VkImageView
), the buffer resources can be used directly as the source of vertex data or accessed by shaders through descriptors. They need to be converted explicitly into a buffer view (VkBufferView
) to allow the shaders to use buffer data contents in the formatted form. In this section, we will make use of the buffer resource directly using the API commands.
First, this section will discuss the buffer resource concepts covering the API specifications to use them in the implementation. Next, we will use these APIs and implement the buffer resources to store the geometry data of a simple triangle. This will be used in the upcoming chapters to render the geometry into the application.
The buffer resource (VkBuffer
) is created using the vkCreateBuffer
API. The following is the syntax:
VkResult vkCreateBuffer( VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBuffer* buffer);
This table describes the various fields of the vkCreateBuffer()
API:
Parameters |
Description |
|
This is the logical device responsible for creating the buffer resource. |
|
This refers to a |
|
This controls the host memory allocation process. |
|
This returns the |
The VkBufferCreateInfo
syntax is defined here. The VkBufferCreateInfo
syntax is defined here:
typedef struct VkBufferCreateInfo { VkStructureType type; const void* pNext; VkBufferCreateFlags flags; VkDeviceSize size; VkBufferUsageFlags usage; VkSharingMode sharingMode; uint32_t queueFamilyIndexCount; const uint32_t* pQueueFamilyIndices; } VkBufferCreateInfo;
The following table describes the various fields of VkBufferCreateInfo
:
Parameters |
Description |
|
This specifies the type of the structure; this must be of type |
|
This is an extension-specific structure. It can also be |
|
These are |
|
This refers to the total size of the buffer to be created; the size is specified in bytes. |
|
This is |
|
This specifies the sharing mode of the buffer when it will be accessed by multiple queue families. This must be one of these values: |
|
This represents the number of entries in the |
|
This is an array of queue families that will access the buffer. The |
When the buffer is no longer required, it can be destroyed using vkDestroyBuffer()
:
void vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* allocator);
This API accepts three parameters, which are described in the following table:
Parameters |
Description |
|
This is the logical device that destroys the buffer object. |
|
This refers to the |
|
This controls the host memory deallocation process; refer to the Host memory section in Chapter 5, Command Buffer and Memory Management in Vulkan. |
A buffer view is created using vkCreateBufferView()
. The following is the syntax of this API:
VkResult vkCreateBufferView( VkDevice device, const VkBufferViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBufferView* pView);
This table describes the various fields of vkCreateBufferView
:
Parameters |
Description |
|
This is the handle of the logical device that creates the image buffer. |
|
This is a pointer to |
|
This controls the host memory allocation process; for more information, refer to the Host memory section in Chapter 5, Command Buffer and Memory Management in Vulkan. |
|
This returns the handle of the created |
The buffer view is created using the vkCreateBufferView()
API. The following is the syntax information:
typedef struct VkBufferViewCreateInfo { VkStructureType type; const void* pNext; VkBufferViewCreateFlags flags; VkBuffer buffer; VkFormat format; VkDeviceSize offset; VkDeviceSize range; } VkBufferViewCreateInfo;
This table describes the various fields of VkBufferViewCreateInfo
:
Parameters |
Description |
|
This refers to the type information of the structure; it must be of type |
|
This is an extension-specific structure. This field could be |
|
This field is reserved for future use. |
|
This is the handle of |
|
This specifies the format ( |
|
This is used for the remapping of color/depth/stencil after they have been converted into color components. |
|
This is used for selecting a range of mipmap levels and array layers, making them accessible to the view. |
The buffer view could be destroyed using the vkDestroBufferView
() API. This intakes three parameters. The first specifies the logical device that is responsible for destroying the buffer view indicated by the second parameter. Here is the syntax of this:
void vkDestroyBufferView(VkDevice device, VkBufferView bufferView, VkAllocationCallbacks* pAllocator);
3.139.239.41