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 8. Pipelines and Pipeline State Management

In the previous chapter, we understood the buffer resource in Vulkan and used it to store geometry data information in the form of a vertex buffer on the physical device memory. We implemented a Render Pass and framebuffer object. Also, we learned about SPIR-V, which is a new way of specifying shaders in Vulkan. In addition, we used the SPIR-V tool library to convert GLSL shaders into the SPIR-V intermediate language at the time of compilation.

We will take this chapter one notch up from what we've learned so far—we'll understand the concept of a pipeline and pipeline state management. In this chapter, we will begin describing the types of pipeline supported by the Vulkan API. There are two types of pipeline—compute and graphics. These pipelines are created using pipeline cache objects, which will be the next topic. As we approach the end of this chapter, we will implement the graphics pipeline and thoroughly understand the various types...

Getting started with pipelines


A pipeline refers to a succession of fixed stages through which a data input flows; each stage processes the incoming data and hands it over to the next stage. The final product will be either a 2D raster drawing image (the graphics pipeline) or updated resources (buffers or images) with computational logic and calculations (the compute pipeline).

Vulkan supports two types of pipeline—graphics and compute.

  • The graphics pipeline: This pipeline takes in a set of Vulkan commands through command buffers and draws a 2D raster image of the 2D/3D scene.

  • The compute pipeline: This pipeline takes in Vulkan commands through command buffers and processes them for computational work.

The following redrawn diagram from the official Vulkan specification (https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html#pipelines-block-diagram) shows the Vulkan graphics and compute pipelines:

The pipeline journey begins at Input Assembler, where the input vertex data is...

Caching pipeline objects with a PCO


A pipeline cache is a pool that stores baked pipelines. It enables an application to reduce the pipeline creation overhead between pipeline runs and also between the subsequent application run. The following is the difference between the two:

  • Between pipelines: The pipeline construction can be reused when new pipelines are created. The pipeline cache object is passed as a parameter to the pipeline creator API (vkCreateGraphicsPipelines). By doing so, the underlying mechanism ensures that it reuses the pipeline if a similar one exists. This is widely useful in the creation of drawing objects that are redundant in nature, for example, paint brush strokes, sprites, mesh geometries, and so on.

  • Between applications: While creating a pipeline, you deal with a lot of pipeline state objects, which is an expensive path. Reusability across running applications is a wise design and is efficient in terms of execution time and memory space. A pipeline cache inside...

Creating a graphics pipeline


A graphics pipeline consists of programmable, fixed-function pipeline stages, Render Passes, subpasses, and pipeline layouts. The programmable stages include multiple shader stages, such as the vertex, fragment, tessellation, geometry, and compute shaders. The fixed-function states consist of multiple pipeline state objects (PSOs) that represent the dynamic, vertex input, input assembly, rasterization, blending, viewport, multisampling, and depth-stencil states.

A graphics pipeline object (VkPipeline) is created using the vkCreateGraphicsPipelines() API. This API intakes the programmable stages, fixed-function pipeline stages, and pipeline layouts through a metadata control structure called VkGraphicsPipelineCreateInfo. The following is the syntax of the vkCreateGraphicsPipelines() API:

VkResult vkCreateGraphicsPipelines( 
    VkDevice                                    device, 
    VkPipelineCache                             pipelineCache, 
   ...

Understanding compute pipelines


A compute pipeline consists of a single static compute shader stage and the pipeline layout. The compute shader stage is capable of doing massive parallel arbitrary computations. On the other hand, the pipeline layout connects the compute pipeline to the descriptor using the layout bindings. The vkCreateComputePipeline() API can be used to create a compute pipeline:

VkResult vkCreateComputePipelines( 
   VkDevice                            device, 
   VkPipelineCache                     pipelineCache, 
   uint32_t                            createInfoCount, 
   const VkComputePipelineCreateInfo*  pCreateInfos, 
   const VkAllocationCallbacks*        pAllocator, 
   VkPipeline*                         pPipelines); 

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

Pipeline State Objects (PSO) in Vulkan


Pipeline state objects in a pipeline are the means by which the hardware settings of the physical devices are controlled. There are various types of pipeline state objects specified in the pipeline; which work in a predefined order. The input data and resources in these stages are subjected to changes in line with user-specified behavior. Each stage processes the input and passes it on to the next one. Depending upon application requirements, the pipeline state stage can be bypassed as per the user's choice. This is entirely configurable through VkComputePipelineCreateInfo.

Let's have an overview of these pipeline state objects before we cover them in detail:

  • The dynamic state: This specifies the dynamic states used in this pipeline

  • The vertex input state: This specifies the data input rate and its interpretation

  • The input assembly state: This assembles the vertex data into the primitive's topology (line, point, and triangle variants)

  • The rasterization...

Implementing the pipeline


The graphics pipeline is implemented at the initialization stage of the application in the VulkanRenderer class. The VulkanRenderer::createPipelineStateManagement() function is responsible for executing the creation of the pipeline. Inside this function, the cmdPipelineStateMgmt command buffer object is created into which the pipeline creation process's commands are recorded. These recorded commands are then submitted to the graphics queue, that is, to the underlying implementation.

The following diagram shows the execution process. Here, first the command buffer is allocated for managing the pipeline operation. Under this, the pipeline cache object is created using the VulkanPipeline::createPipelineCache() function; once this is done, the pipeline is created in VulkanPipeline::createPipeline(). The following diagram shows the calling sequence:

Each drawing object (we will implement drawing objects in the next chapter) is associated with the created pipeline object...

Summary


In this chapter, we learned about the various pipelines available in the Vulkan API. We understood the graphics and compute pipelines and implemented one of the former in our sample example. We also learned about the pipeline cache object, which is a pool of pipelines that help in achieving better performance. The pipeline cache object can be stored in binary form and can be later uploaded and reused between application runs.

The graphics pipeline comprises many pipeline state objects. In this chapter, we covered all the states in detail along with their implementation. As part of these pipeline state objects, we discussed the dynamics state, vertex input state, input assembly state, rasterization, blending, viewport management, depth and stencil test, and multisampling state.

Finally, we used the pipeline cache object and pipeline state objects to build a graphics pipeline object.

In the next chapter, we will make use of the created graphics pipeline object and render our first Vulkan...

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 device (VkDevice) from which the compute pipeline object is going to...