跳到主要内容

vkCreateImageView

函数原型

VkResult vkCreateImageView(
VkDevice device,
const VkImageViewCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkImageView* pView);

描述

创建一个imageview。

参数

  • device : 用于创建imageview的逻辑设备。
  • pCreateInfo : VkImageViewCreateInfo 结构体指针,包含创建imageview的信息。
  • pAllocator : host memory 分配器。
  • pView : 创建返回的imageview句柄。

补充

VkImageViewCreateInfo 结构体定义:

typedef struct VkImageViewCreateInfo {
VkStructureType sType;
const void* pNext;
VkImageViewCreateFlags flags; //描述imageview的额外标记
VkImage image; //用于创建imageview的image
VkImageViewType viewType; //指定imageview的类型
VkFormat format; //描述image中纹素的格式和类型
VkComponentMapping components; //描述颜色分量的重新映射(或深度模板分量在转换为颜色分量后)
VkImageSubresourceRange subresourceRange; //mipmap级别以及imageview可访问的layers
} VkImageViewCreateInfo;

VkImageViewCreateFlags枚举定义:

typedef enum VkImageViewCreateFlagBits {
// Provided by VK_EXT_fragment_density_map
VK_IMAGE_VIEW_CREATE_FRAGMENT_DENSITY_MAP_DYNAMIC_BIT_EXT = 0x00000001,
// Provided by VK_EXT_fragment_density_map2
VK_IMAGE_VIEW_CREATE_FRAGMENT_DENSITY_MAP_DEFERRED_BIT_EXT = 0x00000002,
} VkImageViewCreateFlagBits;

VkImageViewType枚举定义:

typedef enum VkImageViewType {
VK_IMAGE_VIEW_TYPE_1D = 0,
VK_IMAGE_VIEW_TYPE_2D = 1,
VK_IMAGE_VIEW_TYPE_3D = 2,
VK_IMAGE_VIEW_TYPE_CUBE = 3,
VK_IMAGE_VIEW_TYPE_1D_ARRAY = 4,
VK_IMAGE_VIEW_TYPE_2D_ARRAY = 5,
VK_IMAGE_VIEW_TYPE_CUBE_ARRAY = 6,
} VkImageViewType;

VkComponentMapping结构体定义:

typedef struct VkComponentMapping {
VkComponentSwizzle r;
VkComponentSwizzle g;
VkComponentSwizzle b;
VkComponentSwizzle a;
} VkComponentMapping;

VkComponentSwizzle枚举定义:

typedef enum VkComponentSwizzle {
VK_COMPONENT_SWIZZLE_IDENTITY = 0,
VK_COMPONENT_SWIZZLE_ZERO = 1,
VK_COMPONENT_SWIZZLE_ONE = 2,
VK_COMPONENT_SWIZZLE_R = 3,
VK_COMPONENT_SWIZZLE_G = 4,
VK_COMPONENT_SWIZZLE_B = 5,
VK_COMPONENT_SWIZZLE_A = 6,
} VkComponentSwizzle;

设置为VK_COMPONENT_SWIZZLE_IDENTITY等同于设置下面的映射关系

ComponentIdentity Mapping
components.rVK_COMPONENT_SWIZZLE_R
components.gVK_COMPONENT_SWIZZLE_G
components.bVK_COMPONENT_SWIZZLE_B
components.aVK_COMPONENT_SWIZZLE_A

VkImageSubresourceRange结构体定义:

typedef struct VkImageSubresourceRange {
VkImageAspectFlags aspectMask; //指定image的哪些层面包含在imageview中
uint32_t baseMipLevel; //imageview可访问的第一个mipmap level
uint32_t levelCount; //imageview可访问的mipmap level数量
uint32_t baseArrayLayer; //imageview可访问的第一个layer
uint32_t layerCount; //imageview可访问的layer数量
} VkImageSubresourceRange;

VkImageAspectFlagBits枚举定义:

typedef enum VkImageAspectFlagBits {
VK_IMAGE_ASPECT_COLOR_BIT = 0x00000001,
VK_IMAGE_ASPECT_DEPTH_BIT = 0x00000002,
VK_IMAGE_ASPECT_STENCIL_BIT = 0x00000004,
VK_IMAGE_ASPECT_METADATA_BIT = 0x00000008,
// Provided by VK_VERSION_1_1
VK_IMAGE_ASPECT_PLANE_0_BIT = 0x00000010,
// Provided by VK_VERSION_1_1
VK_IMAGE_ASPECT_PLANE_1_BIT = 0x00000020,
// Provided by VK_VERSION_1_1
VK_IMAGE_ASPECT_PLANE_2_BIT = 0x00000040,
// Provided by VK_VERSION_1_3
VK_IMAGE_ASPECT_NONE = 0,
// Provided by VK_EXT_image_drm_format_modifier
VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT = 0x00000080,
// Provided by VK_EXT_image_drm_format_modifier
VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT = 0x00000100,
// Provided by VK_EXT_image_drm_format_modifier
VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT = 0x00000200,
// Provided by VK_EXT_image_drm_format_modifier
VK_IMAGE_ASPECT_MEMORY_PLANE_3_BIT_EXT = 0x00000400,
// Provided by VK_KHR_sampler_ycbcr_conversion
VK_IMAGE_ASPECT_PLANE_0_BIT_KHR = VK_IMAGE_ASPECT_PLANE_0_BIT,
// Provided by VK_KHR_sampler_ycbcr_conversion
VK_IMAGE_ASPECT_PLANE_1_BIT_KHR = VK_IMAGE_ASPECT_PLANE_1_BIT,
// Provided by VK_KHR_sampler_ycbcr_conversion
VK_IMAGE_ASPECT_PLANE_2_BIT_KHR = VK_IMAGE_ASPECT_PLANE_2_BIT,
// Provided by VK_KHR_maintenance4
VK_IMAGE_ASPECT_NONE_KHR = VK_IMAGE_ASPECT_NONE,
} VkImageAspectFlagBits;

返回值

  • VK_SUCCESS : 成功创建imageview。
  • 其他 : 创建失败。

代码示例

vector<VkImageView> swapChainImageViews;
swapChainImageViews.resize(swapChainImages.size());

// 遍历创建ImageView---图像视图,该图像可以作为纹理使用,但是作为渲染目标,还需要帧缓冲对象
for (size_t i = 0; i < swapChainImages.size(); i++) {
VkImageViewCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = swapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; // viewType和fromat成员变量用于指定图像数据的解释方式
createInfo.format = swapChainImageFormat; // 一维纹理、二维纹理、三维纹理或者立方体贴图
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;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // 用于指定图像的用途和图像哪一部分可以被访问
createInfo.subresourceRange.baseMipLevel = 0; // 此处图像用作渲染,没有细分级别,只存在一个图层
createInfo.subresourceRange.levelCount = 1; // 不是VR应用,可以保持默认
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;

if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create image views!");
}
}