Chapter 7. Buffer Resource, Render Pass, Framebuffer, and Shaders with SPIR-V

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:

  • Understanding the Vulkan buffer resource type
  • Creating geometry with the buffer resource
  • Understanding a Render Pass
  • Using the Render Pass and creating the framebuffer
  • Clearing the background color
  • Working with a shader in Vulkan

Understanding the Vulkan buffer resource type

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.

Creating the buffer resource object

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

device

This is the logical device responsible for creating the buffer resource.

pCreateInfo

This refers to a VkBufferCreateInfo pointer; check the following sections for more information.

pAllocator

This controls the host memory allocation process.

buffer

This returns the VkBuffer pointer after it's created.

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

type

This specifies the type of the structure; this must be of type VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO.

pNext

This is an extension-specific structure. It can also be NULL.

flags

These are VkBufferCreateFlagBits bit field flags. Refer to VkBufferCreateFlagBits in the following sections for more information on flags.

size

This refers to the total size of the buffer to be created; the size is specified in bytes.

usage

This is VkBufferUsageFlagBits specifying the bit field that describes the intended usage of the buffer resource. Refer to the following sections for more information on the usage of VkBufferUsageFlagBits.

sharingMode

This specifies the sharing mode of the buffer when it will be accessed by multiple queue families. This must be one of these values: VK_SHARING_MODE_EXCLUSIVE or VK_SHARING_MODE_CONCURRENT from VkSharingMode.

queueFamilyIndexCount

This represents the number of entries in the queueFamilyIndices array.

pQueueFamilyIndices

This is an array of queue families that will access the buffer. The sharingMode must be VK_SHARING_MODE_CONCURRENT; otherwise, just ignore it.

Destroying the buffer

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

device

This is the logical device that destroys the buffer object.

buffer

This refers to the VkBuffer object that needs to be destroyed.

pAllocator

This controls the host memory deallocation process; refer to the Host memory section in Chapter 5, Command Buffer and Memory Management in Vulkan.

Creating a buffer view

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

device

This is the handle of the logical device that creates the image buffer.

pCreateInfo

This is a pointer to VkCreateBufferViewInfo; this controls the creation of VkBufferView.

pAllocator

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.

pView

This returns the handle of the created VkBufferView object.

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

type

This refers to the type information of the structure; it must be of type VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO.

next

This is an extension-specific structure. This field could be NULL as well.

flags

This field is reserved for future use.

buffer

This is the handle of VkBuffer.

format

This specifies the format (VkFormat) of the buffer data element.

offset

This is used for the remapping of color/depth/stencil after they have been converted into color components.

range

This is used for selecting a range of mipmap levels and array layers, making them accessible to the view.

Destroying the buffer 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); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.239.41