vulakn教程--Drawing a Triangle--Presentation-- Image views

发表于2016-12-09
评论0 1.6k浏览

原文地址: vulkan-tutorial

Image views

为了使用VkImage,不管是在Swap Chain 还是在Pipeline 中,我们都必须创建VkImageView,就如同它的字面意思一样,imageView是image的一个 view.他描述了我们如何访问image、访问image的哪一部分等。

这一部分我们将创建VkImageViews , 并把它用作 color targets.

1
std::vector> swapChainImageViews; //声明

需要填充的结构VkImageViewCreateInfo :

1
2
3
4
5
6
7
8
9
10
typedef struct VkImageViewCreateInfo {
    VkStructureType sType
    const void* pNext;
    VkImageViewCreateFlags flags;
    VkImage image;
    VkImageViewType viewType;
    VkFormat format;
    VkComponentMapping components;
    VkImageSubresourceRange subresourceRange;
} VkImageViewCreateInfo;

我们使用for 循环来为每一个Image创建 ImageView :

1
2
3
for (uint32_t i = 0; i < swapChainImages.size(); i++) {
    ...
}

1
2
3
4
5
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = swapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = swapChainImageFormat;

viewType 有VK_IMAGE_VIEW_TYPE_1/2/3D

1
2
3
4
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;

Component采用默认值。

1
2
3
4
5
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;

subresourceRange描述image的使用目的和要被访问的部分。我们的image 将被用作color targets ,不使用mipmapping 和 multiplayers。如果你要创建3D应用就是用mutiplayers,可以为一个image创建多个imageView表示左眼和右眼的不同访问层次。

现在我们创建VkImageView :

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

整体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void createImageViews() {
    swapChainImageViews.resize(swapChainImages.size(),VDeleter""> {device, vkDestroyImageView});
    for (uint32_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;
        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;
        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!");
        }
    }
}


原文源码: source code

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引