Initialising the Swapchain Images#
The swapchain images are created alongside the swapchain, but the application must create a corresponding set of image views before using them.
An image view is a view into a specified part of an image. An image view stores the following information:
A handle to the image.
The view type (for example, 2D, 3D, or cubemap).
The image format.
Any remapping of an image’s colour components.
The purpose of the image (colour, depth, stencil, and so on).
The number of mipmap levels and layers.
In the example code, initImagesAndViews()
, the application only uses 2D colour render targets, no remapping of components, no mipmapping, and only a single layer. View in context.
void VulkanHelloAPI::initImagesAndViews()
{
// This vector is used as a temporary vector to hold the retrieved images.
uint32_t swapchainImageCount;
std::vector<VkImage> images;
// Get the number of the images which are held by the swapchain. This is set in InitSwapchain function and is the minimum number of images supported.
debugAssertFunctionResult(vk::GetSwapchainImagesKHR(appManager.device, appManager.swapchain, &swapchainImageCount, nullptr), "SwapChain Images - Get Count");
// Resize the temporary images vector to hold the number of images.
images.resize(swapchainImageCount);
// Resize the application's permanent swapchain images vector to be able to hold the number of images.
appManager.swapChainImages.resize(swapchainImageCount);
// Get all of the images from the swapchain and save them in a temporary vector.
debugAssertFunctionResult(vk::GetSwapchainImagesKHR(appManager.device, appManager.swapchain, &swapchainImageCount, images.data()), "SwapChain Images - Allocate Data");
// Iterate over each image in order to create an image view for each one.
for (uint32_t i = 0; i < swapchainImageCount; ++i)
{
// Copy over the images to the permenant vector.
appManager.swapChainImages[i].image = images[i];
// Create the image view object itself, referencing one of the retrieved swapchain images.
VkImageViewCreateInfo image_view_info = {};
image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
image_view_info.pNext = nullptr;
image_view_info.flags = 0;
image_view_info.image = appManager.swapChainImages[i].image;
image_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
image_view_info.format = appManager.surfaceFormat.format;
image_view_info.components.r = VK_COMPONENT_SWIZZLE_R;
image_view_info.components.g = VK_COMPONENT_SWIZZLE_G;
image_view_info.components.b = VK_COMPONENT_SWIZZLE_B;
image_view_info.components.a = VK_COMPONENT_SWIZZLE_A;
image_view_info.subresourceRange.layerCount = 1;
image_view_info.subresourceRange.levelCount = 1;
image_view_info.subresourceRange.baseArrayLayer = 0;
image_view_info.subresourceRange.baseMipLevel = 0;
image_view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
debugAssertFunctionResult(vk::CreateImageView(appManager.device, &image_view_info, nullptr, &appManager.swapChainImages[i].view), "SwapChain Images View Creation");
}
}