Reader small image

You're reading from  Learning Vulkan

Product typeBook
Published inDec 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781786469809
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Parminder Singh
Parminder Singh
author image
Parminder Singh

Parminder Singh is a computation graphics engineer with Blackmagic Design, Singapore. He has been working and developing graphic applications in the fields of network simulations, geo-modeling, navigation, automotive, infotainment systems, image processing, and post-production for the past decade. His research interests include GPU programming for scalable graphics and compute applications, porting, and performance optimization techniques. He is a Vulkan, Metal and OpenGL ES trainer and has also authored OpenGL ES 3.0 Cookbook, Packt. His hobbies include traveling, light cooking, and spending quality time with his baby girl. Feel free to connect Parminder at https://www.linkedin.com/in/parmindersingh18 or you can reach him at http://openglescookbook.com.
Read more about Parminder Singh

Right arrow

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...

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...

Creating geometry with a buffer resource


In this section, we will create a simple geometrical shape--a triangle. This will be stored in the GPU memory with the help of a buffer resource. Most applications will consume buffers through uniform or storage blocks. The implementation of a buffer resource is very similar to that of an image resource, except the fact that here we would not need to create the buffer view (VkBufferView).

Preparing geometry data

Create MeshData.h and define the geometry data inside it. Declare the following structures:

/*---------------------MeshData.h-----------------------*/

// Mesh data structure and Vertex Data 
struct VertexWithColor 
{ 
    float x, y, z, w;   // Vertex Position 
    float r, g, b, a;   // Color format Red, Green, Blue, Alpha 
}; 
 
// Interleaved data containing position and color information 
static const VertexWithColor triangleData[] = 
{ 
   {  0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f...

Understanding a Render Pass


A Render Pass tells us about the framebuffer attachments and subpasses that will be used while rendering. Attachments, such as color and depth, indicate how many color and depth images will be there. It specifies what should be the sample bits used to represent each of them and how the contents will be used in the rendering process. It also confirms how the contents would be treated at the beginning and end of each Render Pass instance. A Render Pass used in a command buffer is called a Render Pass instance. It manages the dependencies between the subpasses and defines the protocols on how the attachments should be used over the course of the subpasses.

A Render Pass consists of mainly two type of components: attachments and subpasses. The following are some facts about attachments and subpasses.

Attachments

An attachment refers to a surface region (such as color, depth/stencil, or resolve attachment to perform resolve operations) used at the time of rendering a...

Using the Render Pass and creating the framebuffer


Once the Render Pass is created, it is used to create the framebuffer. Ideally, for each swapchain color image, we need a framebuffer associated with it. For example, if we have a double buffer swapchain image, then we need two framebuffers: one for the front buffer and another for the back buffer image.

The framebuffer in Vulkan is created using the vkCreateFrameBuffer() API. Like with other Vulkan APIs, this also has a create info control structure called VkFrameBufferCreateInfo. Refer to the following for more information on its syntax and usage:

VkResult vkCreateFrameBuffer( 
    VkDevice                           device, 
    const VkFramebufferCreateInfo*     pCreateInfo, 
    const VkAllocationCallbacks*       pAllocator, 
    VkFramebuffer*                     pFrameBuffer); 

The following table describes the various fields of the vkCreateFrameBuffer() API:

Clearing the background color


In this section, we use the created Render Pass and framebuffer object and implement the Render Pass instance. This Render Pass instance is very simple and will only clear the background image with a specified color. For each swapchain image, different colors can be specified using the pClearValues field of the VkRenderPassBeginInfo structure; this structure is then passed to the Render Pass instance.

The Render Pass instance is implemented during the preparation stage in which the command buffers are created. For each swapchain image, a corresponding command buffer object is created. This means, for n swapchain images, we need to create n command buffer objects.

The preparation is done using the VulkanDrawable::prepare() function, and the rendering of the swapchain images will be performed using the VulkanDrawable::render() function.

The following diagram shows the call stack of the prepare() and render() function:

The following class declaration shows the new...

Working with a shader in Vulkan


Shaders are the means by which the programmable stage is controlled in the graphics and compute pipeline.

The graphics pipeline includes vertex, tessellation, geometry, and fragment shaders. Collectively, the first four-vertex, tessellation control, tessellation evaluation, and geometry shaders-are responsible for the vertex-processing stages. These are followed by the fragment shader, which is executed after rasterization.

Here's a bit about the graphics pipeline shaders:

  • Vertex shaders: The graphics pipeline executes the vertex shader as a result of the primitive assembly. It is used to process geometric vertices. It manipulates the data that may be required by the subsequent shaders (if enabled), such as lighting information by the fragment shader.

  • Tessellation shaders: This is a vertex-processing stage. When enabled, it subdivides the patches of vertex data into smaller primitives governed by the control and evaluate shaders.

  • Geometry shaders: This shader...

Summary


Following the image resource creation process from the previous chapter, this chapter begins with another type of Vulkan resource called a buffer resource. We not only understood the concept, but also implemented the geometry vertex buffer using it and also looked into the Render Pass and framebuffer to define a unit render job in Vulkan. Finally, we closed the chapter down with the introduction of SPIR-V, which is a new way of specifying the shaders and kernels in the Vulkan. We implemented our first shader in the SPIR-V form, where we input the vertex and fragment shader into GLSL and converted them into the SPIR-V format using the Lunar-G SDK's glslangValidator.

In the next chapter, we will look at the descriptor and descriptor sets. These are the interfaces between the created resources and the shaders. We will use a descriptor to connect our created vertex buffer resource information to the SPIR-V shader implemented in this chapter.

In the next chapter we will cover the pipeline...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Vulkan
Published in: Dec 2016Publisher: PacktISBN-13: 9781786469809
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Parminder Singh

Parminder Singh is a computation graphics engineer with Blackmagic Design, Singapore. He has been working and developing graphic applications in the fields of network simulations, geo-modeling, navigation, automotive, infotainment systems, image processing, and post-production for the past decade. His research interests include GPU programming for scalable graphics and compute applications, porting, and performance optimization techniques. He is a Vulkan, Metal and OpenGL ES trainer and has also authored OpenGL ES 3.0 Cookbook, Packt. His hobbies include traveling, light cooking, and spending quality time with his baby girl. Feel free to connect Parminder at https://www.linkedin.com/in/parmindersingh18 or you can reach him at http://openglescookbook.com.
Read more about Parminder Singh

Parameters

Description

device

This is the logical...