| 1 |
/* |
| 2 |
* Copyright 2023 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/vk/VulkanImageView.h" |
| 9 |
|
| 10 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 11 |
#include "src/gpu/graphite/vk/VulkanResourceProvider.h" |
| 12 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 13 |
#include "src/gpu/graphite/vk/VulkanYcbcrConversion.h" |
| 14 |
|
| 15 |
namespace skgpu::graphite { |
| 16 |
|
| 17 |
std::unique_ptr<const VulkanImageView> VulkanImageView::Make( |
| 18 |
const VulkanSharedContext* sharedCtx, |
| 19 |
VkImage image, |
| 20 |
VkFormat format, |
| 21 |
Usage usage, |
| 22 |
uint32_t miplevels, |
| 23 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion) { |
| 24 |
|
| 25 |
void* pNext = nullptr; |
| 26 |
VkSamplerYcbcrConversionInfo conversionInfo; |
| 27 |
if (ycbcrConversion) { |
| 28 |
conversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO; |
| 29 |
conversionInfo.pNext = nullptr; |
| 30 |
conversionInfo.conversion = ycbcrConversion->ycbcrConversion(); |
| 31 |
pNext = &conversionInfo; |
| 32 |
} |
| 33 |
|
| 34 |
VkImageView imageView; |
| 35 |
// Create the VkImageView |
| 36 |
VkImageAspectFlags aspectFlags; |
| 37 |
if (Usage::kAttachment == usage) { |
| 38 |
switch (format) { |
| 39 |
case VK_FORMAT_S8_UINT: |
| 40 |
aspectFlags = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 41 |
break; |
| 42 |
case VK_FORMAT_D16_UNORM: |
| 43 |
case VK_FORMAT_D32_SFLOAT: |
| 44 |
aspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 45 |
break; |
| 46 |
case VK_FORMAT_D24_UNORM_S8_UINT: |
| 47 |
case VK_FORMAT_D32_SFLOAT_S8_UINT: |
| 48 |
aspectFlags = VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT; |
| 49 |
break; |
| 50 |
default: |
| 51 |
aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 52 |
break; |
| 53 |
} |
| 54 |
// Attachments can only expose the top level MIP |
| 55 |
miplevels = 1; |
| 56 |
} else { |
| 57 |
aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 58 |
} |
| 59 |
VkImageViewCreateInfo viewInfo = { |
| 60 |
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // sType |
| 61 |
pNext, // pNext |
| 62 |
0, // flags |
| 63 |
image, // image |
| 64 |
VK_IMAGE_VIEW_TYPE_2D, // viewType |
| 65 |
format, // format |
| 66 |
{ VK_COMPONENT_SWIZZLE_IDENTITY, |
| 67 |
VK_COMPONENT_SWIZZLE_IDENTITY, |
| 68 |
VK_COMPONENT_SWIZZLE_IDENTITY, |
| 69 |
VK_COMPONENT_SWIZZLE_IDENTITY }, // components |
| 70 |
{ aspectFlags, 0, miplevels, 0, 1 }, // subresourceRange |
| 71 |
}; |
| 72 |
|
| 73 |
VkResult result; |
| 74 |
VULKAN_CALL_RESULT(sharedCtx, |
| 75 |
result, |
| 76 |
CreateImageView(sharedCtx->device(), &viewInfo, nullptr, &imageView)); |
| 77 |
if (result != VK_SUCCESS) { |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
|
| 81 |
return std::unique_ptr<VulkanImageView>(new VulkanImageView(sharedCtx, imageView, usage, |
| 82 |
ycbcrConversion)); |
| 83 |
} |
| 84 |
|
| 85 |
VulkanImageView::VulkanImageView(const VulkanSharedContext* sharedContext, |
| 86 |
VkImageView imageView, |
| 87 |
Usage usage, |
| 88 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion) |
| 89 |
: fSharedContext(sharedContext) |
| 90 |
, fImageView(imageView) |
| 91 |
, fUsage(usage) |
| 92 |
, fYcbcrConversion(std::move(ycbcrConversion)) {} |
| 93 |
|
| 94 |
VulkanImageView::~VulkanImageView() { |
| 95 |
VULKAN_CALL(fSharedContext->interface(), |
| 96 |
DestroyImageView(fSharedContext->device(), fImageView, nullptr)); |
| 97 |
} |
| 98 |
|
| 99 |
} // namespace skgpu::graphite |