Creating an image view
Image views provide a way to interpret images in terms of size, location, and format, except in terms of their layout, which needs to be transformed explicitly and transitioned using image barriers. In this recipe, you will learn how to create an image view object in Vulkan.
Getting ready
Image views are stored and managed by the VulkanCore::Texture
class in the repository.
How to do it…
Creating an image view is easy; all you need is the handle of the image it is associated with and the region of the image that you would like to represent:
VkDevice device; // Valid Vulkan Device VkImage image; // Valid Image object VkFormat format; uint32_t numMipLevels; // Number of mip levels uint32_t layers; // Number of layers (cubemap faces) const VkImageViewCreateInfo imageViewInfo = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = image, .viewType = VK_IMAGE_VIEW_TYPE_2D, // 1D, 2D, 3D, Cubemap // and arrays .format = format, .components = { .r = VK_COMPONENT_SWIZZLE_IDENTITY, .g = VK_COMPONENT_SWIZZLE_IDENTITY, .b = VK_COMPONENT_SWIZZLE_IDENTITY, .a = VK_COMPONENT_SWIZZLE_IDENTITY, }, .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = numMipLevels, .baseArrayLayer = 0, .layerCount = layers, }}; VkImageView imageView{VK_NULL_HANDLE}; VK_CHECK(vkCreateImageView(device, &imageViewInfo, nullptr, &imageView));
Without an image view, a texture cannot be used by shaders. Even when used as color attachments, images need image views.