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 6. Allocating Image Resources and Building a Swapchain with WSI

In the previous chapter, we covered concepts related to memory management and command buffers. We learned about host and device memory and ways to allocate in the Vulkan API. We also covered command buffers; we implemented command buffer recording API calls and submitted them to queues for processing.

In this chapter, we will make use of our knowledge of command buffers and memory allocation to implement a swapchain and depth image. A swapchain provides the mechanism by which we can render drawing primitives to swapchain color images, which is then passed on to the presentation layer in order to display the primitives in the window. Images are a prerequisite of swap buffer creation; therefore, this chapter will help you gain in-depth knowledge of image resources and their uses in the Vulkan application.

We will cover the following topics:

  • Getting started with image resources

  • Understanding an image resource

  • Memory allocation...

Getting started with image resources


A Vulkan resource is simply a representation of a memory view that contains data. Vulkan primarily has two types of resource: buffers and images. In this chapter, we will only discuss the concept of an image resource; this will be used to implement the swapchain. For more information on the buffer resource type, refer to Chapter 7, Buffer Resource, Render Pass, Framebuffer, and Shaders with SPIR-V. In order to get an overview of this, you may want to revisit the Resource objects - managing images and buffers section in Chapter 2, Your First Vulkan Pseudo Program.

Vulkan images represent contiguous texture data in 1D/2D/3D form. These images are primarily used as either an attachment or texture:

  • Attachment: The image is attached to the pipeline for the framebuffer's color or depth attachment and can also be used as an auxiliary surface for multipass processing purposes

  • Texture: The image is used as a descriptor interface and shared at the shader stage ...

Understanding image resources


This section will cover the Vulkan APIs used to create an image resource. Here, we'll look into the concept of a data image, image view, and image layout in detail.

Creating images

An image resource in Vulkan is represented using the VkImage object. This object supports a multidimensional image up to three-dimensional data arrays. Images are created using the vkCreateImage() API. Here's the syntax to do this:

VkResult vkCreateImage( 
    VkDevice                     device, 
    const VkImageCreateInfo*     pCreateInfo, 
    const VkAllocationCallbacks* pAllocator, 
    VkImage*                     pImage); 

The following table describes the various fields of VkCommandPoolCreateInfo:

Memory allocation and binding image resources


When an image resource object (VkImage) is created, it contains a logical allocation. The image has no physical association with the device memory at that point. The actual memory backing is provided separately at a later stage. The physical allocation is very type-dependent; the images can be categorized into sparse and non-sparse. The sparse resource is specified using sparse creation flags (VkImageCreateFlagBits in VkImageCreateInfo); however, if the flag is not specified, it is a non-sparse image resource. This chapter will only address non-sparse memory as a reference. For more information on sparse resource allocation, refer to the official Vulkan 1.0 specification.

The association of an image with memory is a three-step process: gathering memory allocation requirements for image allocation, allocating the physical chunk on the device memory, and binding the allocated memory to the image resource. Let's take a look at this in detail.

Gathering...

Introducing swapchains


Swapchains are a mechanism by which the rendering of the drawing primitive is shown on a platform-specific presentation window/surface. A swapchain may contain single or multiple drawing images. These drawing images are called color images. A color image is simply an array of pixel information that resides in a special layout in the memory. The number of draw images in a swapchain is very specific to the implementation. When double images are used, it's called double buffering, and when three surfaces are used, triple buffering.

Among these images, when one image completes the drawing process in the background, it is swapped to the presentation window. In order to fully utilize the GPU, a different image is then treated as a background buffer for the drawing process. This process is repeated back and forth and the swapping of the images takes place continuously. Making use of multiple images improves the frame rate output as the GPU is always constantly busy with the...

Creating a depth image


The depth image surface plays an important role in 3D graphics application. It brings the perception of depth in a rendered scene using depth testing. In depth testing, each fragment's depth is stored in a special buffer called a depth image. Unlike the color image that stores the color information, the depth image stores depth information of the primitive's corresponding fragment from the camera view. The depth image's dimension is usually the same as the color image. Not a hard-and-fast rule, but in general, the depth image stores the depth information as 16-, 24-, or 32-bit float values.

Note

The creation of a depth image is different from the color image. You must have noticed that we did not use the vkCreateImage() API to obtain color image objects while retrieving swapchain images. These images were directly returned from the fpGetSwapchainImagesKHR() extension API. In this section, we will go through a step-by-step process to create the depth image.

Introduction...

Summarizing the application flow


In this section, we will summarize the flow of swapchain creation and the building of the presentation window. It consists of two parts: initialization and rendering.

Initialization

The initialization process initializes, creates, and processes the swapchain. The swapchain is not yet connected to the framebuffer render process and the primitives. Therefore, the rendered output will be a blank presentation window for now.

First, VulkanRenderer initializes the presentation window and creates a native platform-specific empty window (500 x 500). This window renders the swapchain's front buffer drawing image. Next, it initializes the swapchain to meet swapchain prerequisites. The swapchain image view layouts are created using command buffers that are allocated from the preallocated pool of command buffers.

During the initialization of the swapchain, the WSI extensions are queried and stored in the form of function pointers. The logical swapchain surface object is...

Summary


This chapter was full of image resources. We started with a basic understanding of image resource in Vulkan and learning about image objects, image layouts, and image views. Then we created image objects and allocated device memory to them. We also used WSI extensions to implement the swapchain and retrieved the swapchain images; these images were then associated with the presentation window. Finally, we created image views out of the swapchain images.

Next in this chapter, we implemented the depth buffer image. We also understood the different Vulkan image tilings and the basic difference between them. In addition to this, we also understood image layouts and their implementations using memory barriers.

In the next chapter, we will introduce the framebuffer and render pass. The framebuffer consumes the image views of a swapchain and depth image and associates them with a color and depth attachment. This information is then used by the render pass to define a unit of work. We will...

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 refers to the logical device responsible for creating an image.

pCreateInfo

This refers to a VkImageCreateInfo pointer.

pAllocator

This controls the host memory allocation process.

pImage

This returns the...