dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 1 | // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gpu/vulkan/vulkan_image_view.h" |
| 6 | |
| 7 | #include "base/logging.h" |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 8 | #include "gpu/vulkan/vulkan_device_queue.h" |
dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 9 | |
| 10 | namespace gpu { |
| 11 | |
| 12 | namespace { |
| 13 | const VkImageAspectFlags kAspectFlags[] = { |
| 14 | // IMAGE_TYPE_COLOR, |
| 15 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 16 | |
| 17 | // IMAGE_TYPE_DEPTH, |
| 18 | VK_IMAGE_ASPECT_DEPTH_BIT, |
| 19 | |
| 20 | // IMAGE_TYPE_STENCIL, |
| 21 | VK_IMAGE_ASPECT_STENCIL_BIT, |
| 22 | |
| 23 | // IMAGE_TYPE_DEPTH_STENCIL, |
| 24 | VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT, |
| 25 | }; |
| 26 | static_assert(arraysize(kAspectFlags) == VulkanImageView::NUM_IMAGE_TYPES, |
| 27 | "Array size for kAspectFlags must match image types."); |
| 28 | } // namespace |
| 29 | |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 30 | VulkanImageView::VulkanImageView(VulkanDeviceQueue* device_queue) |
| 31 | : device_queue_(device_queue) {} |
dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 32 | |
| 33 | VulkanImageView::~VulkanImageView() { |
| 34 | DCHECK_EQ(static_cast<VkImageView>(VK_NULL_HANDLE), handle_); |
| 35 | DCHECK_EQ(IMAGE_TYPE_INVALID, image_type_); |
| 36 | } |
| 37 | |
| 38 | bool VulkanImageView::Initialize(VkImage image, |
| 39 | VkImageViewType image_view_type, |
| 40 | ImageType image_type, |
| 41 | VkFormat format, |
| 42 | uint32_t width, |
| 43 | uint32_t height, |
| 44 | uint32_t base_mip_level, |
| 45 | uint32_t num_mips, |
| 46 | uint32_t base_layer_level, |
| 47 | uint32_t num_layers) { |
| 48 | DCHECK_GT(image_type, IMAGE_TYPE_INVALID); |
| 49 | DCHECK_LT(image_type, NUM_IMAGE_TYPES); |
| 50 | VkImageSubresourceRange image_subresource_range = {}; |
| 51 | image_subresource_range.aspectMask = kAspectFlags[image_type]; |
| 52 | image_subresource_range.baseMipLevel = base_mip_level; |
| 53 | image_subresource_range.levelCount = num_mips; |
| 54 | image_subresource_range.baseArrayLayer = base_layer_level; |
| 55 | image_subresource_range.layerCount = num_layers; |
| 56 | |
| 57 | VkImageViewCreateInfo image_view_create_info = {}; |
| 58 | image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 59 | image_view_create_info.image = image; |
| 60 | image_view_create_info.viewType = image_view_type; |
| 61 | image_view_create_info.format = format; |
| 62 | image_view_create_info.components = { |
| 63 | VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, |
| 64 | VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}; |
| 65 | image_view_create_info.subresourceRange = image_subresource_range; |
| 66 | |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 67 | VkResult result = |
| 68 | vkCreateImageView(device_queue_->GetVulkanDevice(), |
| 69 | &image_view_create_info, nullptr, &handle_); |
dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 70 | if (VK_SUCCESS != result) { |
| 71 | DLOG(ERROR) << "vkCreateImageView() failed: " << result; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | image_type_ = image_type; |
| 76 | width_ = width; |
| 77 | height_ = height; |
| 78 | mips_ = num_mips; |
| 79 | layers_ = num_layers; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void VulkanImageView::Destroy() { |
| 84 | if (VK_NULL_HANDLE != handle_) { |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 85 | vkDestroyImageView(device_queue_->GetVulkanDevice(), handle_, nullptr); |
dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 86 | image_type_ = IMAGE_TYPE_INVALID; |
| 87 | handle_ = VK_NULL_HANDLE; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace gpu |