vulakn教程--Drawing a Triangle--Presentation-- Image views
发表于2016-12-09
原文地址: vulkan-tutorial
Image views
为了使用VkImage
,不管是在Swap Chain 还是在Pipeline 中,我们都必须创建VkImageView
,就如同它的字面意思一样,imageView
是image的一个 view.他描述了我们如何访问image、访问image的哪一部分等。
这一部分我们将创建VkImageViews
, 并把它用作 color targets.
1 std::vector> swapChainImageViews;
//声明
需要填充的结构VkImageViewCreateInfo
:
12345678910 typedef
struct
VkImageViewCreateInfo {
VkStructureType sType
const
void
* pNext;
VkImageViewCreateFlags flags;
VkImage image;
VkImageViewType viewType;
VkFormat format;
VkComponentMapping components;
VkImageSubresourceRange subresourceRange;
} VkImageViewCreateInfo;
我们使用for
循环来为每一个Image
创建 ImageView
:
123 for
(uint32_t i = 0; i < swapChainImages.size(); i++) {
...
}
12345 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
。
1234 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
采用默认值。
12345 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
:
123 if
(vkCreateImageView(device, &createInfo, nullptr,&swapChainImageViews[i]) != VK_SUCCESS) {
throw
std::runtime_error(
"failed to create image views!"
);
}
整体代码如下:
1234567891011121314151617181920212223 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