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

原文地址: vulkan-tutorial


Image views

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

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

std::vector> swapChainImageViews; //声明

需要填充的结构VkImageViewCreateInfo

typedef struct VkImageViewCreateInfo {
    VkStructureType sType
    const void* pNext;
    VkImageViewCreateFlags flags;
    VkImage image;
    VkImageViewType viewType;
    VkFormat format;
    VkComponentMapping components;
    VkImageSubresourceRange subresourceRange;
} VkImageViewCreateInfo;

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

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;

viewTypeVK_IMAGE_VIEW_TYPE_1/2/3D

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采用默认值。

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 ,不使用mipmappingmultiplayers。如果你要创建3D应用就是用mutiplayers,可以为一个image创建多个imageView表示左眼和右眼的不同访问层次。

现在我们创建VkImageView

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

整体代码如下:

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

为什么csdn 传不了资源了???

你可能感兴趣的:(Vulakn,教程,vulkan,imageView)