This publication provides a translation of the Image view chapter from the Drawing a triangle section, the Presentation subsection.
Content
1.
2.
3.
4.
5.
6. Uniform-
7.
8.
9.
10. -
11. Multisampling
FAQ
2.
3.
4.
-
-
- Window surface
- Swap chain
- Image views
- (pipeline)
5.
- Staging
6. Uniform-
- layout
- sets
7.
- Image view image sampler
- image sampler
8.
9.
10. -
11. Multisampling
FAQ
Image views
To use VkImage, we have to create a VkImageView object in the graphics pipeline. Image view is literally a look into image. It describes how to interpret the image and which part of the image will be used.
In this chapter, we will write a function
createImageViews
that will create a base image view for each image in the swap chain to be used as a color target later on.
First of all, add a member to hold the image views:
std::vector<VkImageView> swapChainImageViews;
Let's create a function
createImageView
and call it right after creating the swap chain.
void initVulkan() {
createInstance();
setupDebugMessenger();
createSurface();
pickPhysicalDevice();
createLogicalDevice();
createSwapChain();
createImageViews();
}
void createImageViews() {
}
The first thing we will do is allocate the necessary space in the container to contain all the image views.
void createImageViews() {
swapChainImageViews.resize(swapChainImages.size());
}
Next, let's create a loop that traverses all images from the swap chain.
for (size_t i = 0; i < swapChainImages.size(); i++) {
}
The parameters for creating the image view are passed to the VkImageViewCreateInfo structure . The first few parameters are straightforward.
VkImageViewCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; createInfo.image = swapChainImages[i];
The
viewType
and fields
format
indicate how the image data should be interpreted. The parameter
viewType
allows you to use images as 1D, 2D, 3D textures or cube maps.
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; createInfo.format = swapChainImageFormat;
The field
components
allows you to switch color channels among themselves. For example, we can read all color channels only from a
r
component, thereby obtaining a monochrome image. Or, for example, assign
1
or
0
as a constant for the alpha channel. Here we will use the default settings.
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
The field
subresourceRange
describes which part of the image will be used. Our images consist of only 1 layer with no levels of detail and will be used as a color buffer.
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
If you are working with stereo images, you need to create a swap chain with multiple layers. Then for each image create several image views with a separate image for each eye.
To create the image view, it remains to call the vkCreateImageView function :
if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create image views!");
}
Unlike objects
VkImage
, image views were created by us, so we need to describe a similar cycle to destroy them before exiting the program:
void cleanup() {
for (auto imageView : swapChainImageViews) {
vkDestroyImageView(device, imageView, nullptr);
}
...
}
The image view is enough for us to use the image as a texture, but to use the image as the render target, we need to create a framebuffer. But first, let's set up the graphics pipeline.
C ++