| 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/VulkanYcbcrConversion.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/vk/VulkanGraphiteTypes.h" |
| 11 |
#include "src/gpu/graphite/vk/VulkanCaps.h" |
| 12 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 13 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 14 |
|
| 15 |
namespace skgpu::graphite { |
| 16 |
|
| 17 |
namespace { |
| 18 |
|
| 19 |
static constexpr int kUsesExternalFormatBits = 1; |
| 20 |
static constexpr int kYcbcrModelBits = 3; |
| 21 |
static constexpr int kYcbcrRangeBits = 1; |
| 22 |
static constexpr int kXChromaOffsetBits = 1; |
| 23 |
static constexpr int kYChromaOffsetBits = 1; |
| 24 |
static constexpr int kChromaFilterBits = 1; |
| 25 |
static constexpr int kForceExplicitReconBits = 1; |
| 26 |
static constexpr int kComponentBits = 3; |
| 27 |
static constexpr int kMatchChromaFilterBits = 1; |
| 28 |
static constexpr int kSupportsLinearFilterBits = 1; |
| 29 |
|
| 30 |
|
| 31 |
static constexpr int kUsesExternalFormatShift = 0; |
| 32 |
static constexpr int kYcbcrModelShift = kUsesExternalFormatShift + |
| 33 |
kUsesExternalFormatBits; |
| 34 |
static constexpr int kYcbcrRangeShift = kYcbcrModelShift + kYcbcrModelBits; |
| 35 |
static constexpr int kXChromaOffsetShift = kYcbcrRangeShift + kYcbcrRangeBits; |
| 36 |
static constexpr int kYChromaOffsetShift = kXChromaOffsetShift + kXChromaOffsetBits; |
| 37 |
static constexpr int kChromaFilterShift = kYChromaOffsetShift + kYChromaOffsetBits; |
| 38 |
static constexpr int kForceExplicitReconShift = kChromaFilterShift + kChromaFilterBits; |
| 39 |
static constexpr int kComponentRShift = kForceExplicitReconShift + |
| 40 |
kForceExplicitReconBits; |
| 41 |
static constexpr int kComponentGShift = kComponentRShift + kComponentBits; |
| 42 |
static constexpr int kComponentBShift = kComponentGShift + kComponentBits; |
| 43 |
static constexpr int kComponentAShift = kComponentBShift + kComponentBits; |
| 44 |
static constexpr int kMatchChromaFilterShift = kComponentAShift + kComponentBits; |
| 45 |
static constexpr int kSupportsLinearFilterShift = kMatchChromaFilterShift + kMatchChromaFilterBits; |
| 46 |
|
| 47 |
static constexpr uint32_t kUseExternalFormatMask = |
| 48 |
((1 << kUsesExternalFormatBits) - 1) << kUsesExternalFormatShift; |
| 49 |
static constexpr uint32_t kYcbcrModelMask = |
| 50 |
((1 << kYcbcrModelBits) - 1) << kYcbcrModelShift; |
| 51 |
static constexpr uint32_t kYcbcrRangeMask = |
| 52 |
((1 << kYcbcrRangeBits) - 1) << kYcbcrRangeShift; |
| 53 |
static constexpr uint32_t kXChromaOffsetMask = |
| 54 |
((1 << kXChromaOffsetBits) - 1) << kXChromaOffsetShift; |
| 55 |
static constexpr uint32_t kYChromaOffsetMask = |
| 56 |
((1 << kYChromaOffsetBits) - 1) << kYChromaOffsetShift; |
| 57 |
static constexpr uint32_t kChromaFilterMask = |
| 58 |
((1 << kChromaFilterBits) - 1) << kChromaFilterShift; |
| 59 |
static constexpr uint32_t kForceExplicitReconMask = |
| 60 |
((1 << kForceExplicitReconBits) - 1) << kForceExplicitReconShift; |
| 61 |
static constexpr uint32_t kComponentRMask = ((1 << kComponentBits) - 1) << kComponentRShift; |
| 62 |
static constexpr uint32_t kComponentGMask = ((1 << kComponentBits) - 1) << kComponentGShift; |
| 63 |
static constexpr uint32_t kComponentBMask = ((1 << kComponentBits) - 1) << kComponentBShift; |
| 64 |
static constexpr uint32_t kComponentAMask = ((1 << kComponentBits) - 1) << kComponentAShift; |
| 65 |
static constexpr uint32_t kMatchChromaFilterMask = |
| 66 |
((1 << kMatchChromaFilterBits) - 1) << kMatchChromaFilterShift; |
| 67 |
static constexpr uint32_t kSupportsLinearFilterMask = |
| 68 |
((1 << kSupportsLinearFilterBits) - 1) << kSupportsLinearFilterShift; |
| 69 |
|
| 70 |
} // anonymous namespace |
| 71 |
|
| 72 |
sk_sp<VulkanYcbcrConversion> VulkanYcbcrConversion::Make( |
| 73 |
const VulkanSharedContext* context, const VulkanYcbcrConversionInfo& conversionInfo) { |
| 74 |
if (!context->vulkanCaps().supportsYcbcrConversion()) { |
| 75 |
return nullptr; |
| 76 |
} |
| 77 |
|
| 78 |
VkSamplerYcbcrConversionCreateInfo ycbcrCreateInfo; |
| 79 |
std::optional<VkFilter> requiredSamplerFilter; |
| 80 |
conversionInfo.toVkSamplerYcbcrConversionCreateInfo(&ycbcrCreateInfo, &requiredSamplerFilter); |
| 81 |
|
| 82 |
#ifdef SK_BUILD_FOR_ANDROID |
| 83 |
VkExternalFormatANDROID externalFormat; |
| 84 |
if (conversionInfo.hasExternalFormat()) { |
| 85 |
// Format must not be specified for external images. |
| 86 |
SkASSERT(conversionInfo.fFormat == VK_FORMAT_UNDEFINED); |
| 87 |
externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID; |
| 88 |
externalFormat.pNext = nullptr; |
| 89 |
externalFormat.externalFormat = conversionInfo.externalFormat(); |
| 90 |
SkASSERT(ycbcrCreateInfo.pNext == nullptr); |
| 91 |
ycbcrCreateInfo.pNext = &externalFormat; |
| 92 |
} |
| 93 |
#else |
| 94 |
// External images are supported only on Android. |
| 95 |
SkASSERT(!conversionInfo.hasExternalFormat()); |
| 96 |
#endif |
| 97 |
|
| 98 |
if (!conversionInfo.hasExternalFormat()) { |
| 99 |
SkASSERT(conversionInfo.format() != VK_FORMAT_UNDEFINED); |
| 100 |
} |
| 101 |
|
| 102 |
VkSamplerYcbcrConversion conversion; |
| 103 |
VkResult result; |
| 104 |
VULKAN_CALL_RESULT(context, |
| 105 |
result, |
| 106 |
CreateSamplerYcbcrConversion( |
| 107 |
context->device(), &ycbcrCreateInfo, nullptr, &conversion)); |
| 108 |
if (result != VK_SUCCESS) { |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
return sk_sp<VulkanYcbcrConversion>( |
| 112 |
new VulkanYcbcrConversion(context, conversion, requiredSamplerFilter)); |
| 113 |
} |
| 114 |
|
| 115 |
ImmutableSamplerInfo VulkanYcbcrConversion::ToImmutableSamplerInfo( |
| 116 |
const VulkanYcbcrConversionInfo& conversionInfo) { |
| 117 |
SkASSERT(conversionInfo.isValid()); |
| 118 |
|
| 119 |
static_assert(kSupportsLinearFilterShift + kSupportsLinearFilterBits <= 32); |
| 120 |
|
| 121 |
SkASSERT(conversionInfo.model() < (1u << kYcbcrModelBits )); |
| 122 |
SkASSERT(conversionInfo.range() < (1u << kYcbcrRangeBits )); |
| 123 |
SkASSERT(conversionInfo.xChromaOffset() < (1u << kXChromaOffsetBits )); |
| 124 |
SkASSERT(conversionInfo.yChromaOffset() < (1u << kYChromaOffsetBits )); |
| 125 |
SkASSERT(conversionInfo.chromaFilter() < (1u << kChromaFilterBits )); |
| 126 |
SkASSERT(conversionInfo.forceExplicitReconstruction() < (1u << kForceExplicitReconBits )); |
| 127 |
SkASSERT(conversionInfo.components().r < (1u << kComponentBits )); |
| 128 |
SkASSERT(conversionInfo.components().g < (1u << kComponentBits )); |
| 129 |
SkASSERT(conversionInfo.components().b < (1u << kComponentBits )); |
| 130 |
SkASSERT(conversionInfo.components().a < (1u << kComponentBits )); |
| 131 |
|
| 132 |
const bool usesExternalFormat = conversionInfo.hasExternalFormat(); |
| 133 |
const bool matchChroma = conversionInfo.fSamplerFilterMustMatchChromaFilter; |
| 134 |
const bool supportsLinear = conversionInfo.fSupportsLinearFilter; |
| 135 |
|
| 136 |
ImmutableSamplerInfo info; |
| 137 |
info.fNonFormatYcbcrConversionInfo = |
| 138 |
(((uint32_t)(usesExternalFormat ) << kUsesExternalFormatShift ) | |
| 139 |
((uint32_t)(conversionInfo.model() ) << kYcbcrModelShift ) | |
| 140 |
((uint32_t)(conversionInfo.range() ) << kYcbcrRangeShift ) | |
| 141 |
((uint32_t)(conversionInfo.xChromaOffset() ) << kXChromaOffsetShift ) | |
| 142 |
((uint32_t)(conversionInfo.yChromaOffset() ) << kYChromaOffsetShift ) | |
| 143 |
((uint32_t)(conversionInfo.chromaFilter() ) << kChromaFilterShift ) | |
| 144 |
((uint32_t)(conversionInfo.forceExplicitReconstruction()) << kForceExplicitReconShift ) | |
| 145 |
((uint32_t)(conversionInfo.components().r ) << kComponentRShift ) | |
| 146 |
((uint32_t)(conversionInfo.components().g ) << kComponentGShift ) | |
| 147 |
((uint32_t)(conversionInfo.components().b ) << kComponentBShift ) | |
| 148 |
((uint32_t)(conversionInfo.components().a ) << kComponentAShift ) | |
| 149 |
((uint32_t)(matchChroma ) << kMatchChromaFilterShift ) | |
| 150 |
((uint32_t)(supportsLinear ) << kSupportsLinearFilterShift)); |
| 151 |
|
| 152 |
SkASSERT(info.fNonFormatYcbcrConversionInfo >> SamplerDesc::kMaxNumConversionInfoBits == 0); |
| 153 |
|
| 154 |
info.fFormat = usesExternalFormat ? conversionInfo.externalFormat() |
| 155 |
: static_cast<uint64_t>(conversionInfo.format()); |
| 156 |
return info; |
| 157 |
} |
| 158 |
|
| 159 |
VulkanYcbcrConversionInfo VulkanYcbcrConversion::FromImmutableSamplerInfo( |
| 160 |
ImmutableSamplerInfo info) { |
| 161 |
const uint32_t nonFormatInfo = info.fNonFormatYcbcrConversionInfo; |
| 162 |
|
| 163 |
VkFormat format = VK_FORMAT_UNDEFINED; |
| 164 |
uint64_t externalFormat = 0; |
| 165 |
|
| 166 |
const bool usesExternalFormat = |
| 167 |
(nonFormatInfo >> kUsesExternalFormatShift) & kUseExternalFormatMask; |
| 168 |
if (usesExternalFormat) { |
| 169 |
externalFormat = info.fFormat; |
| 170 |
} else { |
| 171 |
format = static_cast<VkFormat>(info.fFormat); |
| 172 |
} |
| 173 |
|
| 174 |
VkSamplerYcbcrModelConversion model = |
| 175 |
static_cast<VkSamplerYcbcrModelConversion>( |
| 176 |
(nonFormatInfo & kYcbcrModelMask) >> kYcbcrModelShift); |
| 177 |
VkSamplerYcbcrRange range = |
| 178 |
static_cast<VkSamplerYcbcrRange>( |
| 179 |
(nonFormatInfo & kYcbcrRangeMask) >> kYcbcrRangeShift); |
| 180 |
VkComponentMapping components = |
| 181 |
{ static_cast<VkComponentSwizzle>( |
| 182 |
(nonFormatInfo & kComponentRMask) >> kComponentRShift), |
| 183 |
static_cast<VkComponentSwizzle>( |
| 184 |
(nonFormatInfo & kComponentGMask) >> kComponentGShift), |
| 185 |
static_cast<VkComponentSwizzle>( |
| 186 |
(nonFormatInfo & kComponentBMask) >> kComponentBShift), |
| 187 |
static_cast<VkComponentSwizzle>( |
| 188 |
(nonFormatInfo & kComponentAMask) >> kComponentAShift) }; |
| 189 |
VkChromaLocation xChromaOffset = |
| 190 |
static_cast<VkChromaLocation>( |
| 191 |
(nonFormatInfo & kXChromaOffsetMask) >> kXChromaOffsetShift); |
| 192 |
VkChromaLocation yChromaOffset = |
| 193 |
static_cast<VkChromaLocation>( |
| 194 |
(nonFormatInfo & kYChromaOffsetMask) >> kYChromaOffsetShift); |
| 195 |
VkFilter chromaFilter = static_cast<VkFilter>( |
| 196 |
(nonFormatInfo & kChromaFilterMask) >> kChromaFilterShift); |
| 197 |
VkBool32 forceExplicitReconstruction = |
| 198 |
static_cast<VkBool32>( |
| 199 |
(nonFormatInfo & kForceExplicitReconMask) >> kForceExplicitReconShift); |
| 200 |
|
| 201 |
bool chromaMustMatch = |
| 202 |
static_cast<bool>((nonFormatInfo & kMatchChromaFilterMask) >> kMatchChromaFilterShift); |
| 203 |
bool supportsLinearFilter = |
| 204 |
static_cast<bool>( |
| 205 |
(nonFormatInfo & kSupportsLinearFilterMask) >> kSupportsLinearFilterShift); |
| 206 |
|
| 207 |
|
| 208 |
VulkanYcbcrConversionInfo vkInfo(format, externalFormat, model, range, xChromaOffset, |
| 209 |
yChromaOffset, chromaFilter, forceExplicitReconstruction, |
| 210 |
components, chromaMustMatch, supportsLinearFilter); |
| 211 |
|
| 212 |
return vkInfo; |
| 213 |
} |
| 214 |
|
| 215 |
VulkanYcbcrConversion::VulkanYcbcrConversion(const VulkanSharedContext* context, |
| 216 |
VkSamplerYcbcrConversion ycbcrConversion, |
| 217 |
std::optional<VkFilter> requiredFilter) |
| 218 |
: Resource(context, |
| 219 |
Ownership::kOwned, |
| 220 |
/*gpuMemorySize=*/0, |
| 221 |
/*reusableRequiresPurgeable=*/false) |
| 222 |
, fYcbcrConversion(ycbcrConversion) |
| 223 |
, fRequiredFilter(requiredFilter) {} |
| 224 |
|
| 225 |
void VulkanYcbcrConversion::freeGpuData() { |
| 226 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 227 |
SkASSERT(fYcbcrConversion != VK_NULL_HANDLE); |
| 228 |
VULKAN_CALL(sharedContext->interface(), |
| 229 |
DestroySamplerYcbcrConversion(sharedContext->device(), fYcbcrConversion, nullptr)); |
| 230 |
} |
| 231 |
|
| 232 |
} // namespace skgpu::graphite |