Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / ganesh / vk / GrVkCaps.cpp
1 /*
2  * Copyright 2015 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/ganesh/vk/GrVkCaps.h"
9
10 #include <memory>
11
12 #include "include/gpu/GrBackendSurface.h"
13 #include "include/gpu/GrContextOptions.h"
14 #include "include/gpu/vk/GrVkBackendContext.h"
15 #include "include/gpu/vk/GrVkExtensions.h"
16 #include "src/core/SkCompressedDataUtils.h"
17 #include "src/gpu/KeyBuilder.h"
18 #include "src/gpu/ganesh/GrBackendUtils.h"
19 #include "src/gpu/ganesh/GrProgramDesc.h"
20 #include "src/gpu/ganesh/GrRenderTarget.h"
21 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
22 #include "src/gpu/ganesh/GrShaderCaps.h"
23 #include "src/gpu/ganesh/GrStencilSettings.h"
24 #include "src/gpu/ganesh/GrUtil.h"
25 #include "src/gpu/ganesh/SkGr.h"
26 #include "src/gpu/ganesh/vk/GrVkGpu.h"
27 #include "src/gpu/ganesh/vk/GrVkImage.h"
28 #include "src/gpu/ganesh/vk/GrVkInterface.h"
29 #include "src/gpu/ganesh/vk/GrVkRenderTarget.h"
30 #include "src/gpu/ganesh/vk/GrVkTexture.h"
31 #include "src/gpu/ganesh/vk/GrVkUniformHandler.h"
32 #include "src/gpu/ganesh/vk/GrVkUtil.h"
33
34 #ifdef SK_BUILD_FOR_ANDROID
35 #include <sys/system_properties.h>
36 #endif
37
38 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
39                    VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
40                    uint32_t instanceVersion, uint32_t physicalDeviceVersion,
41                    const GrVkExtensions& extensions, GrProtected isProtected)
42         : INHERITED(contextOptions) {
43     /**************************************************************************
44      * GrCaps fields
45      **************************************************************************/
46     fMipmapSupport = true;   // always available in Vulkan
47     fAnisoSupport = true;   // always available in Vulkan
48     fNPOTTextureTileSupport = true;  // always available in Vulkan
49     fReuseScratchTextures = true; //TODO: figure this out
50     fGpuTracingSupport = false; //TODO: figure this out
51     fOversizedStencilSupport = false; //TODO: figure this out
52     fDrawInstancedSupport = true;
53
54     fSemaphoreSupport = true;   // always available in Vulkan
55     fFenceSyncSupport = true;   // always available in Vulkan
56     fCrossContextTextureSupport = true;
57     fHalfFloatVertexAttributeSupport = true;
58
59     // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
60     fReadPixelsRowBytesSupport = true;
61     fWritePixelsRowBytesSupport = true;
62
63     fTransferFromBufferToTextureSupport = true;
64     fTransferFromSurfaceToBufferSupport = true;
65
66     fMaxRenderTargetSize = 4096; // minimum required by spec
67     fMaxTextureSize = 4096; // minimum required by spec
68
69     fDynamicStateArrayGeometryProcessorTextureSupport = true;
70
71     fTextureBarrierSupport = true;
72
73     fShaderCaps = std::make_unique<GrShaderCaps>();
74
75     this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions,
76                isProtected);
77 }
78
79 namespace {
80 /**
81  * This comes from section 37.1.6 of the Vulkan spec. Format is
82  * (<bits>|<tag>)_<block_size>_<texels_per_block>.
83  */
84 enum class FormatCompatibilityClass {
85     k8_1_1,
86     k16_2_1,
87     k24_3_1,
88     k32_4_1,
89     k64_8_1,
90     kBC1_RGB_8_16_1,
91     kBC1_RGBA_8_16,
92     kETC2_RGB_8_16,
93 };
94 }  // anonymous namespace
95
96 static FormatCompatibilityClass format_compatibility_class(VkFormat format) {
97     switch (format) {
98         case VK_FORMAT_B8G8R8A8_UNORM:
99         case VK_FORMAT_R8G8B8A8_UNORM:
100         case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
101         case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
102         case VK_FORMAT_R8G8B8A8_SRGB:
103         case VK_FORMAT_R16G16_UNORM:
104         case VK_FORMAT_R16G16_SFLOAT:
105             return FormatCompatibilityClass::k32_4_1;
106
107         case VK_FORMAT_R8_UNORM:
108             return FormatCompatibilityClass::k8_1_1;
109
110         case VK_FORMAT_R5G6B5_UNORM_PACK16:
111         case VK_FORMAT_R16_SFLOAT:
112         case VK_FORMAT_R8G8_UNORM:
113         case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
114         case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
115         case VK_FORMAT_R16_UNORM:
116             return FormatCompatibilityClass::k16_2_1;
117
118         case VK_FORMAT_R16G16B16A16_SFLOAT:
119         case VK_FORMAT_R16G16B16A16_UNORM:
120             return FormatCompatibilityClass::k64_8_1;
121
122         case VK_FORMAT_R8G8B8_UNORM:
123             return FormatCompatibilityClass::k24_3_1;
124
125         case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
126             return FormatCompatibilityClass::kETC2_RGB_8_16;
127
128         case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
129             return FormatCompatibilityClass::kBC1_RGB_8_16_1;
130
131         case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
132             return FormatCompatibilityClass::kBC1_RGBA_8_16;
133
134         default:
135             SK_ABORT("Unsupported VkFormat");
136     }
137 }
138
139 bool GrVkCaps::canCopyImage(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
140                             VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
141     if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
142         return false;
143     }
144
145     if (dstHasYcbcr || srcHasYcbcr) {
146         return false;
147     }
148
149     // We require that all Vulkan GrSurfaces have been created with transfer_dst and transfer_src
150     // as image usage flags.
151     return format_compatibility_class(srcFormat) == format_compatibility_class(dstFormat);
152 }
153
154 bool GrVkCaps::canCopyAsBlit(VkFormat dstFormat, int dstSampleCnt, bool dstIsLinear,
155                              bool dstHasYcbcr, VkFormat srcFormat, int srcSampleCnt,
156                              bool srcIsLinear, bool srcHasYcbcr) const {
157     // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
158     // as image usage flags.
159     if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
160         !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
161         return false;
162     }
163
164     // We cannot blit images that are multisampled. Will need to figure out if we can blit the
165     // resolved msaa though.
166     if (dstSampleCnt > 1 || srcSampleCnt > 1) {
167         return false;
168     }
169
170     if (dstHasYcbcr || srcHasYcbcr) {
171         return false;
172     }
173
174     return true;
175 }
176
177 bool GrVkCaps::canCopyAsResolve(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
178                                 VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
179     // The src surface must be multisampled.
180     if (srcSampleCnt <= 1) {
181         return false;
182     }
183
184     // The dst must not be multisampled.
185     if (dstSampleCnt > 1) {
186         return false;
187     }
188
189     // Surfaces must have the same format.
190     if (srcFormat != dstFormat) {
191         return false;
192     }
193
194     if (dstHasYcbcr || srcHasYcbcr) {
195         return false;
196     }
197
198     return true;
199 }
200
201 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
202                                 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
203     if (src->isProtected() == GrProtected::kYes && dst->isProtected() != GrProtected::kYes) {
204         return false;
205     }
206
207     // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
208     // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
209     // tracking of external or rectangle textures in GL? For now we don't create linear textures
210     // internally, and I don't believe anyone is wrapping them.
211     bool srcIsLinear = false;
212     bool dstIsLinear = false;
213
214     int dstSampleCnt = 0;
215     int srcSampleCnt = 0;
216     if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
217         // Copying to or from render targets that wrap a secondary command buffer is not allowed
218         // since they would require us to know the VkImage, which we don't have, as well as need us
219         // to stop and start the VkRenderPass which we don't have access to.
220         if (rtProxy->wrapsVkSecondaryCB()) {
221             return false;
222         }
223         if (this->preferDiscardableMSAAAttachment() && dst->asTextureProxy() &&
224             rtProxy->supportsVkInputAttachment()) {
225             dstSampleCnt = 1;
226         } else {
227             dstSampleCnt = rtProxy->numSamples();
228         }
229     }
230     if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
231         // Copying to or from render targets that wrap a secondary command buffer is not allowed
232         // since they would require us to know the VkImage, which we don't have, as well as need us
233         // to stop and start the VkRenderPass which we don't have access to.
234         if (rtProxy->wrapsVkSecondaryCB()) {
235             return false;
236         }
237         if (this->preferDiscardableMSAAAttachment() && src->asTextureProxy() &&
238             rtProxy->supportsVkInputAttachment()) {
239             srcSampleCnt = 1;
240         } else {
241             srcSampleCnt = rtProxy->numSamples();
242         }
243     }
244     SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
245     SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
246
247     bool dstHasYcbcr = false;
248     if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
249         if (ycbcr->isValid()) {
250             dstHasYcbcr = true;
251         }
252     }
253
254     bool srcHasYcbcr = false;
255     if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
256         if (ycbcr->isValid()) {
257             srcHasYcbcr = true;
258         }
259     }
260
261     VkFormat dstFormat, srcFormat;
262     SkAssertResult(dst->backendFormat().asVkFormat(&dstFormat));
263     SkAssertResult(src->backendFormat().asVkFormat(&srcFormat));
264
265     return this->canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
266                               srcFormat, srcSampleCnt, srcHasYcbcr) ||
267            this->canCopyAsBlit(dstFormat, dstSampleCnt, dstIsLinear, dstHasYcbcr,
268                                srcFormat, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
269            this->canCopyAsResolve(dstFormat, dstSampleCnt, dstHasYcbcr,
270                                   srcFormat, srcSampleCnt, srcHasYcbcr);
271 }
272
273 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
274                                                      VkStructureType type) {
275     // All Vulkan structs that could be part of the features chain will start with the
276     // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
277     // so we can get access to the pNext for the next struct.
278     struct CommonVulkanHeader {
279         VkStructureType sType;
280         void*           pNext;
281     };
282
283     void* pNext = features.pNext;
284     while (pNext) {
285         CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
286         if (header->sType == type) {
287             return static_cast<T*>(pNext);
288         }
289         pNext = header->pNext;
290     }
291     return nullptr;
292 }
293
294 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
295                     VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
296                     uint32_t physicalDeviceVersion, const GrVkExtensions& extensions,
297                     GrProtected isProtected) {
298     VkPhysicalDeviceProperties properties;
299     GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
300
301     VkPhysicalDeviceMemoryProperties memoryProperties;
302     GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
303
304     SkASSERT(physicalDeviceVersion <= properties.apiVersion);
305
306     if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
307         fSupportsSwapchain = true;
308     }
309
310     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
311         extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
312         fSupportsPhysicalDeviceProperties2 = true;
313     }
314
315     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
316         extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
317         fSupportsMemoryRequirements2 = true;
318     }
319
320     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
321         extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
322         fSupportsBindMemory2 = true;
323     }
324
325     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
326         extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
327         fSupportsMaintenance1 = true;
328     }
329
330     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
331         extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
332         fSupportsMaintenance2 = true;
333     }
334
335     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
336         extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
337         fSupportsMaintenance3 = true;
338     }
339
340     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
341         (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
342          this->supportsMemoryRequirements2())) {
343         fSupportsDedicatedAllocation = true;
344     }
345
346     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
347         (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
348          this->supportsPhysicalDeviceProperties2() &&
349          extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
350          this->supportsDedicatedAllocation())) {
351         fSupportsExternalMemory = true;
352     }
353
354 #ifdef SK_BUILD_FOR_ANDROID
355     // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
356     // do we don't explicitly require it here even the spec says it is required.
357     if (extensions.hasExtension(
358             VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
359        /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
360         this->supportsExternalMemory() &&
361         this->supportsBindMemory2()) {
362         fSupportsAndroidHWBExternalMemory = true;
363         fSupportsAHardwareBufferImages = true;
364     }
365 #endif
366
367     auto ycbcrFeatures =
368             get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
369                     features, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
370     if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
371         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
372          (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
373           this->supportsMaintenance1() && this->supportsBindMemory2() &&
374           this->supportsMemoryRequirements2() && this->supportsPhysicalDeviceProperties2()))) {
375         fSupportsYcbcrConversion = true;
376     }
377
378     // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
379     // will return a key of 0.
380     fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
381
382     if ((isProtected == GrProtected::kYes) &&
383         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0))) {
384         fSupportsProtectedMemory = true;
385         fAvoidUpdateBuffers = true;
386         fShouldAlwaysUseDedicatedImageMemory = true;
387     }
388
389     if (extensions.hasExtension(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, 1)) {
390         fSupportsDRMFormatModifiers = true;
391     }
392
393     fMaxInputAttachmentDescriptors = properties.limits.maxDescriptorSetInputAttachments;
394
395     fMaxSamplerAnisotropy = properties.limits.maxSamplerAnisotropy;
396
397     // On desktop GPUs we have found that this does not provide much benefit. The perf results show
398     // a mix of regressions, some improvements, and lots of no changes. Thus it is no worth enabling
399     // this (especially with the rendering artifacts) on desktop.
400     //
401     // On Adreno devices we were expecting to see perf gains. But instead there were actually a lot
402     // of perf regressions and only a few perf wins. This needs some follow up with qualcomm since
403     // we do expect this to be a big win on tilers.
404     //
405     // On ARM devices we are seeing an average perf win of around 50%-60% across the board.
406     if (kARM_VkVendor == properties.vendorID) {
407         fPreferDiscardableMSAAAttachment = true;
408         fSupportsMemorylessAttachments = true;
409     }
410
411     this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
412     this->initShaderCaps(properties, features);
413
414     if (kQualcomm_VkVendor == properties.vendorID) {
415         // A "clear" load for atlases runs faster on QC than a "discard" load followed by a
416         // scissored clear.
417         // On NVIDIA and Intel, the discard load followed by clear is faster.
418         // TODO: Evaluate on ARM, Imagination, and ATI.
419         fPreferFullscreenClears = true;
420     }
421
422     if (properties.vendorID == kNvidia_VkVendor || properties.vendorID == kAMD_VkVendor) {
423         // On discrete GPUs it can be faster to read gpu only memory compared to memory that is also
424         // mappable on the host.
425         fGpuOnlyBuffersMorePerformant = true;
426
427         // On discrete GPUs we try to use special DEVICE_LOCAL and HOST_VISIBLE memory for our
428         // cpu write, gpu read buffers. This memory is not ideal to be kept persistently mapped.
429         // Some discrete GPUs do not expose this special memory, however we still disable
430         // persistently mapped buffers for all of them since most GPUs with updated drivers do
431         // expose it. If this becomes an issue we can try to be more fine grained.
432         fShouldPersistentlyMapCpuToGpuBuffers = false;
433     }
434
435     if (kQualcomm_VkVendor == properties.vendorID) {
436         // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
437         // even for small sizes.
438         fAvoidUpdateBuffers = true;
439     }
440
441     if (kQualcomm_VkVendor == properties.vendorID) {
442         // Adreno devices don't support push constants well
443         fMaxPushConstantsSize = 0;
444     }
445
446     fNativeDrawIndirectSupport = features.features.drawIndirectFirstInstance;
447     if (properties.vendorID == kQualcomm_VkVendor) {
448         // Indirect draws seem slow on QC. Disable until we can investigate. http://skbug.com/11139
449         fNativeDrawIndirectSupport = false;
450     }
451
452     if (fNativeDrawIndirectSupport) {
453         fMaxDrawIndirectDrawCount = properties.limits.maxDrawIndirectCount;
454         SkASSERT(fMaxDrawIndirectDrawCount == 1 || features.features.multiDrawIndirect);
455     }
456
457 #ifdef SK_BUILD_FOR_UNIX
458     if (kNvidia_VkVendor == properties.vendorID) {
459         // On nvidia linux we see a big perf regression when not using dedicated image allocations.
460         fShouldAlwaysUseDedicatedImageMemory = true;
461     }
462 #endif
463
464     this->initFormatTable(vkInterface, physDev, properties);
465     this->initStencilFormat(vkInterface, physDev);
466
467     if (contextOptions.fMaxCachedVulkanSecondaryCommandBuffers >= 0) {
468         fMaxPerPoolCachedSecondaryCommandBuffers =
469                 contextOptions.fMaxCachedVulkanSecondaryCommandBuffers;
470     }
471
472     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
473         this->applyDriverCorrectnessWorkarounds(properties);
474     }
475
476     this->finishInitialization(contextOptions);
477 }
478
479 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
480 #if defined(SK_BUILD_FOR_WIN)
481     if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
482         fMustSyncCommandBuffersWithQueue = true;
483     }
484 #elif defined(SK_BUILD_FOR_ANDROID)
485     if (kImagination_VkVendor == properties.vendorID) {
486         fMustSyncCommandBuffersWithQueue = true;
487     }
488 #endif
489
490     // Defaults to zero since all our workaround checks that use this consider things "fixed" once
491     // above a certain api level. So this will just default to it being less which will enable
492     // workarounds.
493     int androidAPIVersion = 0;
494 #if defined(SK_BUILD_FOR_ANDROID)
495     char androidAPIVersionStr[PROP_VALUE_MAX];
496     int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersionStr);
497     // Defaults to zero since most checks care if it is greater than a specific value. So this will
498     // just default to it being less.
499     androidAPIVersion = (strLength == 0) ? 0 : atoi(androidAPIVersionStr);
500 #endif
501
502     // Protected memory features have problems in Android P and earlier.
503     if (fSupportsProtectedMemory && (kQualcomm_VkVendor == properties.vendorID)) {
504         if (androidAPIVersion <= 28) {
505             fSupportsProtectedMemory = false;
506         }
507     }
508
509     // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
510     if (kARM_VkVendor == properties.vendorID && androidAPIVersion <= 28) {
511         fShouldAlwaysUseDedicatedImageMemory = true;
512     }
513
514     // On Mali galaxy s7 and s9 we see lots of rendering issues with image filters dropping out when
515     // using only primary command buffers. We also see issues on the P30 running android 28.
516     if (kARM_VkVendor == properties.vendorID && androidAPIVersion <= 28) {
517         fPreferPrimaryOverSecondaryCommandBuffers = false;
518         // If we are using secondary command buffers our code isn't setup to insert barriers into
519         // the secondary cb so we need to disable support for them.
520         fTextureBarrierSupport = false;
521         fBlendEquationSupport = kBasic_BlendEquationSupport;
522     }
523
524     // We've seen numerous driver bugs on qualcomm devices running on android P (api 28) or earlier
525     // when trying to using discardable msaa attachments and loading from resolve. So we disable the
526     // feature for those devices.
527     if (properties.vendorID == kQualcomm_VkVendor && androidAPIVersion <= 28) {
528         fPreferDiscardableMSAAAttachment = false;
529         fSupportsDiscardableMSAAForDMSAA = false;
530     }
531
532     // On Mali G series GPUs, applying transfer functions in the fragment shader with half-floats
533     // produces answers that are much less accurate than expected/required. This forces full floats
534     // for some intermediate values to get acceptable results.
535     if (kARM_VkVendor == properties.vendorID) {
536         fShaderCaps->fColorSpaceMathNeedsFloat = true;
537     }
538
539     // On various devices, when calling vkCmdClearAttachments on a primary command buffer, it
540     // corrupts the bound buffers on the command buffer. As a workaround we invalidate our knowledge
541     // of bound buffers so that we will rebind them on the next draw.
542     if (kQualcomm_VkVendor == properties.vendorID || kAMD_VkVendor == properties.vendorID) {
543         fMustInvalidatePrimaryCmdBufferStateAfterClearAttachments = true;
544     }
545
546     // On Qualcomm and Arm the gpu resolves an area larger than the render pass bounds when using
547     // discardable msaa attachments. This causes the resolve to resolve uninitialized data from the
548     // msaa image into the resolve image.
549     if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
550         fMustLoadFullImageWithDiscardableMSAA = true;
551     }
552
553 #ifdef SK_BUILD_FOR_UNIX
554     if (kIntel_VkVendor == properties.vendorID) {
555         // At least on our linux Debug Intel HD405 bot we are seeing issues doing read pixels with
556         // non-conherent memory. It seems like the device is not properly honoring the
557         // vkInvalidateMappedMemoryRanges calls correctly. Other linux intel devices seem to work
558         // okay. However, since I'm not sure how to target a specific intel devices or driver
559         // version I am going to stop all intel linux from using non-coherent memory. Currently we
560         // are not shipping anything on these platforms and the only real thing that will regress is
561         // read backs. If we find later we do care about this performance we can come back to figure
562         // out how to do a more narrow workaround.
563         fMustUseCoherentHostVisibleMemory = true;
564     }
565 #endif
566
567     ////////////////////////////////////////////////////////////////////////////
568     // GrCaps workarounds
569     ////////////////////////////////////////////////////////////////////////////
570
571     if (kARM_VkVendor == properties.vendorID) {
572         fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
573     }
574
575     // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
576     if (kAMD_VkVendor == properties.vendorID) {
577         fMaxVertexAttributes = std::min(fMaxVertexAttributes, 32);
578     }
579
580     // Adreno devices fail when trying to read the dest using an input attachment and texture
581     // barriers.
582     if (kQualcomm_VkVendor == properties.vendorID) {
583         fTextureBarrierSupport = false;
584     }
585
586     // On ARM indirect draws are broken on Android 9 and earlier. This was tested on a P30 and
587     // Mate 20x running android 9.
588     if (properties.vendorID == kARM_VkVendor && androidAPIVersion <= 28) {
589         fNativeDrawIndirectSupport = false;
590     }
591
592     ////////////////////////////////////////////////////////////////////////////
593     // GrShaderCaps workarounds
594     ////////////////////////////////////////////////////////////////////////////
595
596     if (kImagination_VkVendor == properties.vendorID) {
597         fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
598     }
599 }
600
601 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
602                           VkPhysicalDevice physDev,
603                           const VkPhysicalDeviceProperties& properties,
604                           const VkPhysicalDeviceMemoryProperties& memoryProperties,
605                           const VkPhysicalDeviceFeatures2& features,
606                           const GrVkExtensions& extensions) {
607     // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
608     // need for us ever to support that amount, and it makes tests which tests all the vertex
609     // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
610     // we ever find that need.
611     static const uint32_t kMaxVertexAttributes = 64;
612     fMaxVertexAttributes = std::min(properties.limits.maxVertexInputAttributes,
613                                     kMaxVertexAttributes);
614
615     // GrCaps::fSampleLocationsSupport refers to the ability to *query* the sample locations (not
616     // program them). For now we just set this to true if the device uses standard locations, and
617     // return the standard locations back when queried.
618     if (properties.limits.standardSampleLocations) {
619         fSampleLocationsSupport = true;
620     }
621
622     if (extensions.hasExtension(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, 1)) {
623         fConservativeRasterSupport = true;
624     }
625
626     fWireframeSupport = true;
627
628     // We could actually query and get a max size for each config, however maxImageDimension2D will
629     // give the minimum max size across all configs. So for simplicity we will use that for now.
630     fMaxRenderTargetSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
631     fMaxTextureSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
632
633     // TODO: check if RT's larger than 4k incur a performance cost on ARM.
634     fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
635
636     fMaxPushConstantsSize = std::min(properties.limits.maxPushConstantsSize, (uint32_t)INT_MAX);
637
638     // Assuming since we will always map in the end to upload the data we might as well just map
639     // from the get go. There is no hard data to suggest this is faster or slower.
640     fBufferMapThreshold = 0;
641
642     fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
643
644     fOversizedStencilSupport = true;
645
646     if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
647         this->supportsPhysicalDeviceProperties2()) {
648
649         VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
650         blendProps.sType =
651                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
652         blendProps.pNext = nullptr;
653
654         VkPhysicalDeviceProperties2 props;
655         props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
656         props.pNext = &blendProps;
657
658         GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
659
660         if (blendProps.advancedBlendAllOperations == VK_TRUE) {
661             fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
662
663             auto blendFeatures =
664                 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
665                     features,
666                     VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
667             if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
668                 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
669             } else {
670                 fBlendEquationSupport = kAdvanced_BlendEquationSupport;
671             }
672         }
673     }
674
675     if (kARM_VkVendor == properties.vendorID) {
676         fShouldCollapseSrcOverToSrcWhenAble = true;
677     }
678 }
679
680 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
681                               const VkPhysicalDeviceFeatures2& features) {
682     GrShaderCaps* shaderCaps = fShaderCaps.get();
683     shaderCaps->fVersionDeclString = "#version 330\n";
684
685     // Vulkan is based off ES 3.0 so the following should all be supported
686     shaderCaps->fUsesPrecisionModifiers = true;
687     shaderCaps->fFlatInterpolationSupport = true;
688     // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
689     // to be true with Vulkan as well.
690     shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
691
692     shaderCaps->fSampleMaskSupport = true;
693
694     shaderCaps->fShaderDerivativeSupport = true;
695
696     // ARM GPUs calculate `matrix * vector` in SPIR-V at full precision, even when the inputs are
697     // RelaxedPrecision. Rewriting the multiply as a sum of vector*scalar fixes this. (skia:11769)
698     shaderCaps->fRewriteMatrixVectorMultiply = (kARM_VkVendor == properties.vendorID);
699
700     shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
701
702     shaderCaps->fIntegerSupport = true;
703     shaderCaps->fNonsquareMatrixSupport = true;
704     shaderCaps->fInverseHyperbolicSupport = true;
705     shaderCaps->fVertexIDSupport = true;
706     shaderCaps->fInfinitySupport = true;
707     shaderCaps->fNonconstantArrayIndexSupport = true;
708     shaderCaps->fBitManipulationSupport = true;
709
710     // Assume the minimum precisions mandated by the SPIR-V spec.
711     shaderCaps->fFloatIs32Bits = true;
712     shaderCaps->fHalfIs32Bits = false;
713
714     shaderCaps->fMaxFragmentSamplers = std::min(
715                                        std::min(properties.limits.maxPerStageDescriptorSampledImages,
716                                               properties.limits.maxPerStageDescriptorSamplers),
717                                               (uint32_t)INT_MAX);
718 }
719
720 bool stencil_format_supported(const GrVkInterface* interface,
721                               VkPhysicalDevice physDev,
722                               VkFormat format) {
723     VkFormatProperties props;
724     memset(&props, 0, sizeof(VkFormatProperties));
725     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
726     return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
727 }
728
729 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
730     if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
731         fPreferredStencilFormat = VK_FORMAT_S8_UINT;
732     } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
733         fPreferredStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;
734     } else {
735         SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
736         fPreferredStencilFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
737     }
738 }
739
740 static bool format_is_srgb(VkFormat format) {
741     SkASSERT(GrVkFormatIsSupported(format));
742
743     switch (format) {
744         case VK_FORMAT_R8G8B8A8_SRGB:
745             return true;
746         default:
747             return false;
748     }
749 }
750
751 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
752 // frequently used to least to improve look up times in arrays.
753 static constexpr VkFormat kVkFormats[] = {
754     VK_FORMAT_R8G8B8A8_UNORM,
755     VK_FORMAT_R8_UNORM,
756     VK_FORMAT_B8G8R8A8_UNORM,
757     VK_FORMAT_R5G6B5_UNORM_PACK16,
758     VK_FORMAT_R16G16B16A16_SFLOAT,
759     VK_FORMAT_R16_SFLOAT,
760     VK_FORMAT_R8G8B8_UNORM,
761     VK_FORMAT_R8G8_UNORM,
762     VK_FORMAT_A2B10G10R10_UNORM_PACK32,
763     VK_FORMAT_A2R10G10B10_UNORM_PACK32,
764     VK_FORMAT_B4G4R4A4_UNORM_PACK16,
765     VK_FORMAT_R4G4B4A4_UNORM_PACK16,
766     VK_FORMAT_R8G8B8A8_SRGB,
767     VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
768     VK_FORMAT_BC1_RGB_UNORM_BLOCK,
769     VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
770     VK_FORMAT_R16_UNORM,
771     VK_FORMAT_R16G16_UNORM,
772     VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
773     VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
774     VK_FORMAT_R16G16B16A16_UNORM,
775     VK_FORMAT_R16G16_SFLOAT,
776 };
777
778 void GrVkCaps::setColorType(GrColorType colorType, std::initializer_list<VkFormat> formats) {
779 #ifdef SK_DEBUG
780     for (size_t i = 0; i < kNumVkFormats; ++i) {
781         const auto& formatInfo = fFormatTable[i];
782         for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
783             const auto& ctInfo = formatInfo.fColorTypeInfos[j];
784             if (ctInfo.fColorType == colorType &&
785                 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
786                 bool found = false;
787                 for (auto it = formats.begin(); it != formats.end(); ++it) {
788                     if (kVkFormats[i] == *it) {
789                         found = true;
790                     }
791                 }
792                 SkASSERT(found);
793             }
794         }
795     }
796 #endif
797     int idx = static_cast<int>(colorType);
798     for (auto it = formats.begin(); it != formats.end(); ++it) {
799         const auto& info = this->getFormatInfo(*it);
800         for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
801             if (info.fColorTypeInfos[i].fColorType == colorType) {
802                 fColorTypeToFormatTable[idx] = *it;
803                 return;
804             }
805         }
806     }
807 }
808
809 const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
810     GrVkCaps* nonConstThis = const_cast<GrVkCaps*>(this);
811     return nonConstThis->getFormatInfo(format);
812 }
813
814 GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) {
815     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
816                   "Size of VkFormats array must match static value in header");
817     for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
818         if (kVkFormats[i] == format) {
819             return fFormatTable[i];
820         }
821     }
822     static FormatInfo kInvalidFormat;
823     return kInvalidFormat;
824 }
825
826 void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
827                                const VkPhysicalDeviceProperties& properties) {
828     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
829                   "Size of VkFormats array must match static value in header");
830
831     std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, VK_FORMAT_UNDEFINED);
832
833     // Go through all the formats and init their support surface and data GrColorTypes.
834     // Format: VK_FORMAT_R8G8B8A8_UNORM
835     {
836         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
837         auto& info = this->getFormatInfo(format);
838         info.init(interface, physDev, properties, format);
839         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
840             info.fColorTypeInfoCount = 2;
841             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
842             int ctIdx = 0;
843             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
844             {
845                 constexpr GrColorType ct = GrColorType::kRGBA_8888;
846                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
847                 ctInfo.fColorType = ct;
848                 ctInfo.fTransferColorType = ct;
849                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
850             }
851             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
852             {
853                 constexpr GrColorType ct = GrColorType::kRGB_888x;
854                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
855                 ctInfo.fColorType = ct;
856                 ctInfo.fTransferColorType = ct;
857                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
858                 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
859             }
860         }
861     }
862
863     // Format: VK_FORMAT_R8_UNORM
864     {
865         constexpr VkFormat format = VK_FORMAT_R8_UNORM;
866         auto& info = this->getFormatInfo(format);
867         info.init(interface, physDev, properties, format);
868         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
869             info.fColorTypeInfoCount = 3;
870             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
871             int ctIdx = 0;
872             // Format: VK_FORMAT_R8_UNORM, Surface: kR_8
873             {
874                 constexpr GrColorType ct = GrColorType::kR_8;
875                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
876                 ctInfo.fColorType = ct;
877                 ctInfo.fTransferColorType = ct;
878                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
879             }
880             // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
881             {
882                 constexpr GrColorType ct = GrColorType::kAlpha_8;
883                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
884                 ctInfo.fColorType = ct;
885                 ctInfo.fTransferColorType = ct;
886                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
887                 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
888                 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
889             }
890             // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
891             {
892                 constexpr GrColorType ct = GrColorType::kGray_8;
893                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
894                 ctInfo.fColorType = ct;
895                 ctInfo.fTransferColorType = ct;
896                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
897                 ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1");
898             }
899         }
900     }
901     // Format: VK_FORMAT_B8G8R8A8_UNORM
902     {
903         constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
904         auto& info = this->getFormatInfo(format);
905         info.init(interface, physDev, properties, format);
906         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
907             info.fColorTypeInfoCount = 1;
908             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
909             int ctIdx = 0;
910             // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
911             {
912                 constexpr GrColorType ct = GrColorType::kBGRA_8888;
913                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
914                 ctInfo.fColorType = ct;
915                 ctInfo.fTransferColorType = ct;
916                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
917             }
918         }
919     }
920     // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
921     {
922         constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
923         auto& info = this->getFormatInfo(format);
924         info.init(interface, physDev, properties, format);
925         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
926             info.fColorTypeInfoCount = 1;
927             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
928             int ctIdx = 0;
929             // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kBGR_565
930             {
931                 constexpr GrColorType ct = GrColorType::kBGR_565;
932                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
933                 ctInfo.fColorType = ct;
934                 ctInfo.fTransferColorType = ct;
935                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
936             }
937         }
938     }
939     // Format: VK_FORMAT_R16G16B16A16_SFLOAT
940     {
941         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
942         auto& info = this->getFormatInfo(format);
943         info.init(interface, physDev, properties, format);
944         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
945             info.fColorTypeInfoCount = 2;
946             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
947             int ctIdx = 0;
948             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16
949             {
950                 constexpr GrColorType ct = GrColorType::kRGBA_F16;
951                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
952                 ctInfo.fColorType = ct;
953                 ctInfo.fTransferColorType = ct;
954                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
955             }
956             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16_Clamped
957             {
958                 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
959                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
960                 ctInfo.fColorType = ct;
961                 ctInfo.fTransferColorType = ct;
962                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
963             }
964         }
965     }
966     // Format: VK_FORMAT_R16_SFLOAT
967     {
968         constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
969         auto& info = this->getFormatInfo(format);
970         info.init(interface, physDev, properties, format);
971         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
972             info.fColorTypeInfoCount = 1;
973             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
974             int ctIdx = 0;
975             // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
976             {
977                 constexpr GrColorType ct = GrColorType::kAlpha_F16;
978                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
979                 ctInfo.fColorType = ct;
980                 ctInfo.fTransferColorType = ct;
981                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
982                 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
983                 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
984             }
985         }
986     }
987     // Format: VK_FORMAT_R8G8B8_UNORM
988     {
989         constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
990         auto& info = this->getFormatInfo(format);
991         info.init(interface, physDev, properties, format);
992         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
993             info.fColorTypeInfoCount = 1;
994             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
995             int ctIdx = 0;
996             // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
997             {
998                 constexpr GrColorType ct = GrColorType::kRGB_888x;
999                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1000                 ctInfo.fColorType = ct;
1001                 // The Vulkan format is 3 bpp so we must convert to/from that when transferring.
1002                 ctInfo.fTransferColorType = GrColorType::kRGB_888;
1003                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1004             }
1005         }
1006     }
1007     // Format: VK_FORMAT_R8G8_UNORM
1008     {
1009         constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
1010         auto& info = this->getFormatInfo(format);
1011         info.init(interface, physDev, properties, format);
1012         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1013             info.fColorTypeInfoCount = 1;
1014             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1015             int ctIdx = 0;
1016             // Format: VK_FORMAT_R8G8_UNORM, Surface: kRG_88
1017             {
1018                 constexpr GrColorType ct = GrColorType::kRG_88;
1019                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1020                 ctInfo.fColorType = ct;
1021                 ctInfo.fTransferColorType = ct;
1022                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1023             }
1024         }
1025     }
1026     // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
1027     {
1028         constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
1029         auto& info = this->getFormatInfo(format);
1030         info.init(interface, physDev, properties, format);
1031         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1032             info.fColorTypeInfoCount = 1;
1033             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1034             int ctIdx = 0;
1035             // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
1036             {
1037                 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
1038                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1039                 ctInfo.fColorType = ct;
1040                 ctInfo.fTransferColorType = ct;
1041                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1042             }
1043         }
1044     }
1045     // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32
1046     {
1047         constexpr VkFormat format = VK_FORMAT_A2R10G10B10_UNORM_PACK32;
1048         auto& info = this->getFormatInfo(format);
1049         info.init(interface, physDev, properties, format);
1050         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1051             info.fColorTypeInfoCount = 1;
1052             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1053             int ctIdx = 0;
1054             // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32, Surface: kBGRA_1010102
1055             {
1056                 constexpr GrColorType ct = GrColorType::kBGRA_1010102;
1057                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1058                 ctInfo.fColorType = ct;
1059                 ctInfo.fTransferColorType = ct;
1060                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1061             }
1062         }
1063     }
1064     // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
1065     {
1066         constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
1067         auto& info = this->getFormatInfo(format);
1068         info.init(interface, physDev, properties, format);
1069         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1070             info.fColorTypeInfoCount = 1;
1071             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1072             int ctIdx = 0;
1073             // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kABGR_4444
1074             {
1075                 constexpr GrColorType ct = GrColorType::kABGR_4444;
1076                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1077                 ctInfo.fColorType = ct;
1078                 ctInfo.fTransferColorType = ct;
1079                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1080                 ctInfo.fReadSwizzle = skgpu::Swizzle::BGRA();
1081                 ctInfo.fWriteSwizzle = skgpu::Swizzle::BGRA();
1082             }
1083         }
1084     }
1085
1086     // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
1087     {
1088         constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
1089         auto& info = this->getFormatInfo(format);
1090         info.init(interface, physDev, properties, format);
1091         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1092             info.fColorTypeInfoCount = 1;
1093             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1094             int ctIdx = 0;
1095             // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kABGR_4444
1096             {
1097                 constexpr GrColorType ct = GrColorType::kABGR_4444;
1098                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1099                 ctInfo.fColorType = ct;
1100                 ctInfo.fTransferColorType = ct;
1101                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1102             }
1103         }
1104     }
1105     // Format: VK_FORMAT_R8G8B8A8_SRGB
1106     {
1107         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
1108         auto& info = this->getFormatInfo(format);
1109         info.init(interface, physDev, properties, format);
1110         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1111             info.fColorTypeInfoCount = 1;
1112             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1113             int ctIdx = 0;
1114             // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
1115             {
1116                 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
1117                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1118                 ctInfo.fColorType = ct;
1119                 ctInfo.fTransferColorType = ct;
1120                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1121             }
1122         }
1123     }
1124     // Format: VK_FORMAT_R16_UNORM
1125     {
1126         constexpr VkFormat format = VK_FORMAT_R16_UNORM;
1127         auto& info = this->getFormatInfo(format);
1128         info.init(interface, physDev, properties, format);
1129         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1130             info.fColorTypeInfoCount = 1;
1131             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1132             int ctIdx = 0;
1133             // Format: VK_FORMAT_R16_UNORM, Surface: kAlpha_16
1134             {
1135                 constexpr GrColorType ct = GrColorType::kAlpha_16;
1136                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1137                 ctInfo.fColorType = ct;
1138                 ctInfo.fTransferColorType = ct;
1139                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1140                 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
1141                 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
1142             }
1143         }
1144     }
1145     // Format: VK_FORMAT_R16G16_UNORM
1146     {
1147         constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1148         auto& info = this->getFormatInfo(format);
1149         info.init(interface, physDev, properties, format);
1150         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1151             info.fColorTypeInfoCount = 1;
1152             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1153             int ctIdx = 0;
1154             // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1155             {
1156                 constexpr GrColorType ct = GrColorType::kRG_1616;
1157                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1158                 ctInfo.fColorType = ct;
1159                 ctInfo.fTransferColorType = ct;
1160                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1161             }
1162         }
1163     }
1164     // Format: VK_FORMAT_R16G16B16A16_UNORM
1165     {
1166         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1167         auto& info = this->getFormatInfo(format);
1168         info.init(interface, physDev, properties, format);
1169         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1170             info.fColorTypeInfoCount = 1;
1171             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1172             int ctIdx = 0;
1173             // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1174             {
1175                 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
1176                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1177                 ctInfo.fColorType = ct;
1178                 ctInfo.fTransferColorType = ct;
1179                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1180             }
1181         }
1182     }
1183     // Format: VK_FORMAT_R16G16_SFLOAT
1184     {
1185         constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1186         auto& info = this->getFormatInfo(format);
1187         info.init(interface, physDev, properties, format);
1188         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1189             info.fColorTypeInfoCount = 1;
1190             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1191             int ctIdx = 0;
1192             // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1193             {
1194                 constexpr GrColorType ct = GrColorType::kRG_F16;
1195                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1196                 ctInfo.fColorType = ct;
1197                 ctInfo.fTransferColorType = ct;
1198                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1199             }
1200         }
1201     }
1202     // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1203     {
1204         constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1205         auto& info = this->getFormatInfo(format);
1206         if (fSupportsYcbcrConversion) {
1207             info.init(interface, physDev, properties, format);
1208         }
1209         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1210             info.fColorTypeInfoCount = 1;
1211             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1212             int ctIdx = 0;
1213             // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1214             {
1215                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1216                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1217                 ctInfo.fColorType = ct;
1218                 ctInfo.fTransferColorType = ct;
1219                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1220             }
1221         }
1222     }
1223     // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1224     {
1225         constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1226         auto& info = this->getFormatInfo(format);
1227         if (fSupportsYcbcrConversion) {
1228             info.init(interface, physDev, properties, format);
1229         }
1230         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1231             info.fColorTypeInfoCount = 1;
1232             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1233             int ctIdx = 0;
1234             // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1235             {
1236                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1237                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1238                 ctInfo.fColorType = ct;
1239                 ctInfo.fTransferColorType = ct;
1240                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1241             }
1242         }
1243     }
1244     // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1245     {
1246         constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1247         auto& info = this->getFormatInfo(format);
1248         info.init(interface, physDev, properties, format);
1249         // Setting this to texel block size
1250         // No supported GrColorTypes.
1251     }
1252
1253     // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK
1254     {
1255         constexpr VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
1256         auto& info = this->getFormatInfo(format);
1257         info.init(interface, physDev, properties, format);
1258         // Setting this to texel block size
1259         // No supported GrColorTypes.
1260     }
1261
1262     // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK
1263     {
1264         constexpr VkFormat format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
1265         auto& info = this->getFormatInfo(format);
1266         info.init(interface, physDev, properties, format);
1267         // Setting this to texel block size
1268         // No supported GrColorTypes.
1269     }
1270
1271     ////////////////////////////////////////////////////////////////////////////
1272     // Map GrColorTypes (used for creating GrSurfaces) to VkFormats. The order in which the formats
1273     // are passed into the setColorType function indicates the priority in selecting which format
1274     // we use for a given GrcolorType.
1275
1276     this->setColorType(GrColorType::kAlpha_8,          { VK_FORMAT_R8_UNORM });
1277     this->setColorType(GrColorType::kBGR_565,          { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1278     this->setColorType(GrColorType::kABGR_4444,        { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1279                                                          VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1280     this->setColorType(GrColorType::kRGBA_8888,        { VK_FORMAT_R8G8B8A8_UNORM });
1281     this->setColorType(GrColorType::kRGBA_8888_SRGB,   { VK_FORMAT_R8G8B8A8_SRGB });
1282     this->setColorType(GrColorType::kRGB_888x,         { VK_FORMAT_R8G8B8_UNORM,
1283                                                          VK_FORMAT_R8G8B8A8_UNORM });
1284     this->setColorType(GrColorType::kRG_88,            { VK_FORMAT_R8G8_UNORM });
1285     this->setColorType(GrColorType::kBGRA_8888,        { VK_FORMAT_B8G8R8A8_UNORM });
1286     this->setColorType(GrColorType::kRGBA_1010102,     { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1287     this->setColorType(GrColorType::kBGRA_1010102,     { VK_FORMAT_A2R10G10B10_UNORM_PACK32 });
1288     this->setColorType(GrColorType::kGray_8,           { VK_FORMAT_R8_UNORM });
1289     this->setColorType(GrColorType::kAlpha_F16,        { VK_FORMAT_R16_SFLOAT });
1290     this->setColorType(GrColorType::kRGBA_F16,         { VK_FORMAT_R16G16B16A16_SFLOAT });
1291     this->setColorType(GrColorType::kRGBA_F16_Clamped, { VK_FORMAT_R16G16B16A16_SFLOAT });
1292     this->setColorType(GrColorType::kAlpha_16,         { VK_FORMAT_R16_UNORM });
1293     this->setColorType(GrColorType::kRG_1616,          { VK_FORMAT_R16G16_UNORM });
1294     this->setColorType(GrColorType::kRGBA_16161616,    { VK_FORMAT_R16G16B16A16_UNORM });
1295     this->setColorType(GrColorType::kRG_F16,           { VK_FORMAT_R16G16_SFLOAT });
1296 }
1297
1298 void GrVkCaps::FormatInfo::InitFormatFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
1299     if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
1300         SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
1301         *flags = *flags | kTexturable_Flag;
1302
1303         // Ganesh assumes that all renderable surfaces are also texturable
1304         if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
1305             *flags = *flags | kRenderable_Flag;
1306         }
1307     }
1308     // TODO: For Vk w/ VK_KHR_maintenance1 extension support, check
1309     //  VK_FORMAT_FEATURE_TRANSFER_[SRC|DST]_BIT_KHR explicitly to set copy flags
1310     //  Can do similar check for VK_KHR_sampler_ycbcr_conversion added bits
1311
1312     if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
1313         *flags = *flags | kBlitSrc_Flag;
1314     }
1315
1316     if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
1317         *flags = *flags | kBlitDst_Flag;
1318     }
1319 }
1320
1321 void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
1322                                             VkPhysicalDevice physDev,
1323                                             const VkPhysicalDeviceProperties& physProps,
1324                                             VkFormat format) {
1325     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1326                               VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1327                               VK_IMAGE_USAGE_SAMPLED_BIT |
1328                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1329     VkImageFormatProperties properties;
1330     GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
1331                                                                  format,
1332                                                                  VK_IMAGE_TYPE_2D,
1333                                                                  VK_IMAGE_TILING_OPTIMAL,
1334                                                                  usage,
1335                                                                  0,  // createFlags
1336                                                                  &properties));
1337     VkSampleCountFlags flags = properties.sampleCounts;
1338     if (flags & VK_SAMPLE_COUNT_1_BIT) {
1339         fColorSampleCounts.push_back(1);
1340     }
1341     if (kImagination_VkVendor == physProps.vendorID) {
1342         // MSAA does not work on imagination
1343         return;
1344     }
1345     if (kIntel_VkVendor == physProps.vendorID) {
1346         // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
1347         return;
1348     }
1349     if (flags & VK_SAMPLE_COUNT_2_BIT) {
1350         fColorSampleCounts.push_back(2);
1351     }
1352     if (flags & VK_SAMPLE_COUNT_4_BIT) {
1353         fColorSampleCounts.push_back(4);
1354     }
1355     if (flags & VK_SAMPLE_COUNT_8_BIT) {
1356         fColorSampleCounts.push_back(8);
1357     }
1358     if (flags & VK_SAMPLE_COUNT_16_BIT) {
1359         fColorSampleCounts.push_back(16);
1360     }
1361     // Standard sample locations are not defined for more than 16 samples, and we don't need more
1362     // than 16. Omit 32 and 64.
1363 }
1364
1365 void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
1366                                 VkPhysicalDevice physDev,
1367                                 const VkPhysicalDeviceProperties& properties,
1368                                 VkFormat format) {
1369     VkFormatProperties props;
1370     memset(&props, 0, sizeof(VkFormatProperties));
1371     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
1372     InitFormatFlags(props.linearTilingFeatures, &fLinearFlags);
1373     InitFormatFlags(props.optimalTilingFeatures, &fOptimalFlags);
1374     if (fOptimalFlags & kRenderable_Flag) {
1375         this->initSampleCounts(interface, physDev, properties, format);
1376     }
1377 }
1378
1379 // For many checks in caps, we need to know whether the GrBackendFormat is external or not. If it is
1380 // external the VkFormat will be VK_NULL_HANDLE which is not handled by our various format
1381 // capability checks.
1382 static bool backend_format_is_external(const GrBackendFormat& format) {
1383     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1384     SkASSERT(ycbcrInfo);
1385
1386     // All external formats have a valid ycbcrInfo used for sampling and a non zero external format.
1387     if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1388 #ifdef SK_DEBUG
1389         VkFormat vkFormat;
1390         SkAssertResult(format.asVkFormat(&vkFormat));
1391         SkASSERT(vkFormat == VK_FORMAT_UNDEFINED);
1392 #endif
1393         return true;
1394     }
1395     return false;
1396 }
1397
1398 bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
1399     VkFormat vkFormat;
1400     if (!format.asVkFormat(&vkFormat)) {
1401         return false;
1402     }
1403     if (backend_format_is_external(format)) {
1404         return false;
1405     }
1406
1407     return format_is_srgb(vkFormat);
1408 }
1409
1410 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format, GrTextureType) const {
1411     VkFormat vkFormat;
1412     if (!format.asVkFormat(&vkFormat)) {
1413         return false;
1414     }
1415     if (backend_format_is_external(format)) {
1416         // We can always texture from an external format (assuming we have the ycbcr conversion
1417         // info which we require to be passed in).
1418         return true;
1419     }
1420     return this->isVkFormatTexturable(vkFormat);
1421 }
1422
1423 bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
1424     const FormatInfo& info = this->getFormatInfo(format);
1425     return SkToBool(FormatInfo::kTexturable_Flag & info.fOptimalFlags);
1426 }
1427
1428 bool GrVkCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
1429                                              int sampleCount) const {
1430     if (!this->isFormatRenderable(format, sampleCount)) {
1431         return false;
1432     }
1433     VkFormat vkFormat;
1434     if (!format.asVkFormat(&vkFormat)) {
1435         return false;
1436     }
1437     const auto& info = this->getFormatInfo(vkFormat);
1438     if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
1439         return false;
1440     }
1441     return true;
1442 }
1443
1444 bool GrVkCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
1445     VkFormat vkFormat;
1446     if (!format.asVkFormat(&vkFormat)) {
1447         return false;
1448     }
1449     return this->isFormatRenderable(vkFormat, sampleCount);
1450 }
1451
1452 bool GrVkCaps::isFormatRenderable(VkFormat format, int sampleCount) const {
1453     return sampleCount <= this->maxRenderTargetSampleCount(format);
1454 }
1455
1456 int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
1457                                          const GrBackendFormat& format) const {
1458     VkFormat vkFormat;
1459     if (!format.asVkFormat(&vkFormat)) {
1460         return 0;
1461     }
1462
1463     return this->getRenderTargetSampleCount(requestedCount, vkFormat);
1464 }
1465
1466 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
1467     requestedCount = std::max(1, requestedCount);
1468
1469     const FormatInfo& info = this->getFormatInfo(format);
1470
1471     int count = info.fColorSampleCounts.count();
1472
1473     if (!count) {
1474         return 0;
1475     }
1476
1477     if (1 == requestedCount) {
1478         SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
1479         return 1;
1480     }
1481
1482     for (int i = 0; i < count; ++i) {
1483         if (info.fColorSampleCounts[i] >= requestedCount) {
1484             return info.fColorSampleCounts[i];
1485         }
1486     }
1487     return 0;
1488 }
1489
1490 int GrVkCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
1491     VkFormat vkFormat;
1492     if (!format.asVkFormat(&vkFormat)) {
1493         return 0;
1494     }
1495     return this->maxRenderTargetSampleCount(vkFormat);
1496 }
1497
1498 int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
1499     const FormatInfo& info = this->getFormatInfo(format);
1500
1501     const auto& table = info.fColorSampleCounts;
1502     if (!table.count()) {
1503         return 0;
1504     }
1505     return table[table.count() - 1];
1506 }
1507
1508 static inline size_t align_to_4(size_t v) {
1509     switch (v & 0b11) {
1510         // v is already a multiple of 4.
1511         case 0:     return v;
1512         // v is a multiple of 2 but not 4.
1513         case 2:     return 2 * v;
1514         // v is not a multiple of 2.
1515         default:    return 4 * v;
1516     }
1517 }
1518
1519 GrCaps::SupportedWrite GrVkCaps::supportedWritePixelsColorType(GrColorType surfaceColorType,
1520                                                                const GrBackendFormat& surfaceFormat,
1521                                                                GrColorType srcColorType) const {
1522     VkFormat vkFormat;
1523     if (!surfaceFormat.asVkFormat(&vkFormat)) {
1524         return {GrColorType::kUnknown, 0};
1525     }
1526
1527     // We don't support the ability to upload to external formats or formats that require a ycbcr
1528     // sampler. In general these types of formats are only used for sampling in a shader.
1529     if (backend_format_is_external(surfaceFormat) || GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1530         return {GrColorType::kUnknown, 0};
1531     }
1532
1533     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1534     size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1535
1536     const auto& info = this->getFormatInfo(vkFormat);
1537     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1538         const auto& ctInfo = info.fColorTypeInfos[i];
1539         if (ctInfo.fColorType == surfaceColorType) {
1540             return {ctInfo.fTransferColorType, offsetAlignment};
1541         }
1542     }
1543     return {GrColorType::kUnknown, 0};
1544 }
1545
1546 GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
1547         const GrSurface* surface) const {
1548     if (surface->isProtected()) {
1549         return SurfaceReadPixelsSupport::kUnsupported;
1550     }
1551     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1552         auto texImage = tex->textureImage();
1553         if (!texImage) {
1554             return SurfaceReadPixelsSupport::kUnsupported;
1555         }
1556         // We can't directly read from a VkImage that has a ycbcr sampler.
1557         if (texImage->ycbcrConversionInfo().isValid()) {
1558             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1559         }
1560         // We can't directly read from a compressed format
1561         if (GrVkFormatIsCompressed(texImage->imageFormat())) {
1562             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1563         }
1564         return SurfaceReadPixelsSupport::kSupported;
1565     } else if (auto rt = surface->asRenderTarget()) {
1566         if (rt->numSamples() > 1) {
1567             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1568         }
1569         return SurfaceReadPixelsSupport::kSupported;
1570     }
1571     return SurfaceReadPixelsSupport::kUnsupported;
1572 }
1573
1574 GrColorType GrVkCaps::transferColorType(VkFormat vkFormat, GrColorType surfaceColorType) const {
1575     const auto& info = this->getFormatInfo(vkFormat);
1576     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1577         if (info.fColorTypeInfos[i].fColorType == surfaceColorType) {
1578             return info.fColorTypeInfos[i].fTransferColorType;
1579         }
1580     }
1581     return GrColorType::kUnknown;
1582 }
1583
1584 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1585     if (auto rt = surface->asRenderTarget()) {
1586         return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1587     }
1588     // We can't write to a texture that has a ycbcr sampler.
1589     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1590         auto texImage = tex->textureImage();
1591         if (!texImage) {
1592             return false;
1593         }
1594         // We can't directly read from a VkImage that has a ycbcr sampler.
1595         if (texImage->ycbcrConversionInfo().isValid()) {
1596             return false;
1597         }
1598     }
1599     return true;
1600 }
1601
1602 bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1603                                                  const GrBackendFormat& format) const {
1604     VkFormat vkFormat;
1605     if (!format.asVkFormat(&vkFormat)) {
1606         return false;
1607     }
1608     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1609     SkASSERT(ycbcrInfo);
1610
1611     if (ycbcrInfo->isValid() && !GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1612         // Format may be undefined for external images, which are required to have YCbCr conversion.
1613         if (VK_FORMAT_UNDEFINED == vkFormat && ycbcrInfo->fExternalFormat != 0) {
1614             return true;
1615         }
1616         return false;
1617     }
1618
1619     const auto& info = this->getFormatInfo(vkFormat);
1620     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1621         if (info.fColorTypeInfos[i].fColorType == ct) {
1622             return true;
1623         }
1624     }
1625     return false;
1626 }
1627
1628 GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct) const {
1629     VkFormat format = this->getFormatFromColorType(ct);
1630     if (format == VK_FORMAT_UNDEFINED) {
1631         return {};
1632     }
1633     return GrBackendFormat::MakeVk(format);
1634 }
1635
1636 bool GrVkCaps::onSupportsDynamicMSAA(const GrRenderTargetProxy* rtProxy) const {
1637     // We must be able to use the rtProxy as an input attachment to load into the discardable msaa
1638     // attachment. Also the rtProxy should have a sample count of 1 so that it can be used as a
1639     // resolve attachment.
1640     return this->supportsDiscardableMSAAForDMSAA() &&
1641            rtProxy->supportsVkInputAttachment() &&
1642            rtProxy->numSamples() == 1;
1643 }
1644
1645 bool GrVkCaps::renderTargetSupportsDiscardableMSAA(const GrVkRenderTarget* rt) const {
1646     return rt->resolveAttachment() &&
1647            rt->resolveAttachment()->supportsInputAttachmentUsage() &&
1648            ((rt->numSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1649             (rt->numSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1650 }
1651
1652 bool GrVkCaps::programInfoWillUseDiscardableMSAA(const GrProgramInfo& programInfo) const {
1653     return programInfo.targetHasVkResolveAttachmentWithInput() &&
1654            programInfo.numSamples() > 1 &&
1655            ((programInfo.targetsNumSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1656             (programInfo.targetsNumSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1657 }
1658
1659 GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1660         SkImage::CompressionType compressionType) const {
1661     switch (compressionType) {
1662         case SkImage::CompressionType::kNone:
1663             return {};
1664         case SkImage::CompressionType::kETC2_RGB8_UNORM:
1665             if (this->isVkFormatTexturable(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)) {
1666                 return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1667             }
1668             return {};
1669         case SkImage::CompressionType::kBC1_RGB8_UNORM:
1670             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGB_UNORM_BLOCK)) {
1671                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK);
1672             }
1673             return {};
1674         case SkImage::CompressionType::kBC1_RGBA8_UNORM:
1675             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGBA_UNORM_BLOCK)) {
1676                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK);
1677             }
1678             return {};
1679     }
1680
1681     SkUNREACHABLE;
1682 }
1683
1684 skgpu::Swizzle GrVkCaps::onGetReadSwizzle(const GrBackendFormat& format,
1685                                           GrColorType colorType) const {
1686     VkFormat vkFormat;
1687     SkAssertResult(format.asVkFormat(&vkFormat));
1688     const auto* ycbcrInfo = format.getVkYcbcrConversionInfo();
1689     SkASSERT(ycbcrInfo);
1690     if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1691         // We allow these to work with any color type and never swizzle. See
1692         // onAreColorTypeAndFormatCompatible.
1693         return skgpu::Swizzle{"rgba"};
1694     }
1695
1696     const auto& info = this->getFormatInfo(vkFormat);
1697     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1698         const auto& ctInfo = info.fColorTypeInfos[i];
1699         if (ctInfo.fColorType == colorType) {
1700             return ctInfo.fReadSwizzle;
1701         }
1702     }
1703     SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1704                  (int)colorType, (int)vkFormat);
1705     return {};
1706 }
1707
1708 skgpu::Swizzle GrVkCaps::getWriteSwizzle(const GrBackendFormat& format,
1709                                          GrColorType colorType) const {
1710     VkFormat vkFormat;
1711     SkAssertResult(format.asVkFormat(&vkFormat));
1712     const auto& info = this->getFormatInfo(vkFormat);
1713     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1714         const auto& ctInfo = info.fColorTypeInfos[i];
1715         if (ctInfo.fColorType == colorType) {
1716             return ctInfo.fWriteSwizzle;
1717         }
1718     }
1719     SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1720                  (int)colorType, (int)vkFormat);
1721     return {};
1722 }
1723
1724 GrDstSampleFlags GrVkCaps::onGetDstSampleFlagsForProxy(const GrRenderTargetProxy* rt) const {
1725     bool isMSAAWithResolve = rt->numSamples() > 1 && rt->asTextureProxy();
1726     // TODO: Currently if we have an msaa rt with a resolve, the supportsVkInputAttachment call
1727     // references whether the resolve is supported as an input attachment. We need to add a check to
1728     // allow checking the color attachment (msaa or not) supports input attachment specifically.
1729     if (!isMSAAWithResolve && rt->supportsVkInputAttachment()) {
1730         return GrDstSampleFlags::kRequiresTextureBarrier | GrDstSampleFlags::kAsInputAttachment;
1731     }
1732     return GrDstSampleFlags::kNone;
1733 }
1734
1735 uint64_t GrVkCaps::computeFormatKey(const GrBackendFormat& format) const {
1736     VkFormat vkFormat;
1737     SkAssertResult(format.asVkFormat(&vkFormat));
1738
1739 #ifdef SK_DEBUG
1740     // We should never be trying to compute a key for an external format
1741     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1742     SkASSERT(ycbcrInfo);
1743     SkASSERT(!ycbcrInfo->isValid() || ycbcrInfo->fExternalFormat == 0);
1744 #endif
1745
1746     // A VkFormat has a size of 64 bits.
1747     return (uint64_t)vkFormat;
1748 }
1749
1750 GrCaps::SupportedRead GrVkCaps::onSupportedReadPixelsColorType(
1751         GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1752         GrColorType dstColorType) const {
1753     VkFormat vkFormat;
1754     if (!srcBackendFormat.asVkFormat(&vkFormat)) {
1755         return {GrColorType::kUnknown, 0};
1756     }
1757
1758     if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1759         return {GrColorType::kUnknown, 0};
1760     }
1761
1762     SkImage::CompressionType compression = GrBackendFormatToCompressionType(srcBackendFormat);
1763     if (compression != SkImage::CompressionType::kNone) {
1764         return { SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1765                                                         : GrColorType::kRGBA_8888, 0 };
1766     }
1767
1768     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1769     size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1770
1771     const auto& info = this->getFormatInfo(vkFormat);
1772     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1773         const auto& ctInfo = info.fColorTypeInfos[i];
1774         if (ctInfo.fColorType == srcColorType) {
1775             return {ctInfo.fTransferColorType, offsetAlignment};
1776         }
1777     }
1778     return {GrColorType::kUnknown, 0};
1779 }
1780
1781 int GrVkCaps::getFragmentUniformBinding() const {
1782     return GrVkUniformHandler::kUniformBinding;
1783 }
1784
1785 int GrVkCaps::getFragmentUniformSet() const {
1786     return GrVkUniformHandler::kUniformBufferDescSet;
1787 }
1788
1789 void GrVkCaps::addExtraSamplerKey(skgpu::KeyBuilder* b,
1790                                   GrSamplerState samplerState,
1791                                   const GrBackendFormat& format) const {
1792     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1793     if (!ycbcrInfo) {
1794         return;
1795     }
1796
1797     GrVkSampler::Key key = GrVkSampler::GenerateKey(samplerState, *ycbcrInfo);
1798
1799     constexpr size_t numInts = (sizeof(key) + 3) / 4;
1800     uint32_t tmp[numInts];
1801     memcpy(tmp, &key, sizeof(key));
1802
1803     for (size_t i = 0; i < numInts; ++i) {
1804         b->add32(tmp[i]);
1805     }
1806 }
1807
1808 /**
1809  * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
1810  * the information needed to differentiate one pipeline from another.
1811  *
1812  * The GrProgramDesc contains all the information need to create the actual shaders for the
1813  * pipeline.
1814  *
1815  * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
1816  * pipline. This includes stencil settings, blending information, render pass format, draw face
1817  * information, and primitive type. Note that some state is set dynamically on the pipeline for
1818  * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
1819  * and blend constant.
1820  */
1821 GrProgramDesc GrVkCaps::makeDesc(GrRenderTarget* rt,
1822                                  const GrProgramInfo& programInfo,
1823                                  ProgramDescOverrideFlags overrideFlags) const {
1824     GrProgramDesc desc;
1825     GrProgramDesc::Build(&desc, programInfo, *this);
1826
1827     skgpu::KeyBuilder b(desc.key());
1828
1829     // This will become part of the sheared off key used to persistently cache
1830     // the SPIRV code. It needs to be added right after the base key so that,
1831     // when the base-key is sheared off, the shearing code can include it in the
1832     // reduced key (c.f. the +4s in the SkData::MakeWithCopy calls in
1833     // GrVkPipelineStateBuilder.cpp).
1834     b.add32(GrVkGpu::kShader_PersistentCacheKeyType);
1835
1836     GrVkRenderPass::SelfDependencyFlags selfDepFlags = GrVkRenderPass::SelfDependencyFlags::kNone;
1837     if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kBlend) {
1838         selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForNonCoherentAdvBlend;
1839     }
1840     if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kTexture) {
1841         selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForInputAttachment;
1842     }
1843
1844     bool needsResolve = this->programInfoWillUseDiscardableMSAA(programInfo);
1845
1846     bool forceLoadFromResolve =
1847             overrideFlags & GrCaps::ProgramDescOverrideFlags::kVulkanHasResolveLoadSubpass;
1848     SkASSERT(!forceLoadFromResolve || needsResolve);
1849
1850     GrVkRenderPass::LoadFromResolve loadFromResolve = GrVkRenderPass::LoadFromResolve::kNo;
1851     if (needsResolve && (programInfo.colorLoadOp() == GrLoadOp::kLoad || forceLoadFromResolve)) {
1852         loadFromResolve = GrVkRenderPass::LoadFromResolve::kLoad;
1853     }
1854
1855     if (rt) {
1856         GrVkRenderTarget* vkRT = (GrVkRenderTarget*) rt;
1857
1858         SkASSERT(!needsResolve || (vkRT->resolveAttachment() &&
1859                                    vkRT->resolveAttachment()->supportsInputAttachmentUsage()));
1860
1861         bool needsStencil = programInfo.needsStencil() || programInfo.isStencilEnabled();
1862         // TODO: support failure in getSimpleRenderPass
1863         auto rp = vkRT->getSimpleRenderPass(needsResolve, needsStencil, selfDepFlags,
1864                                             loadFromResolve);
1865         SkASSERT(rp);
1866         rp->genKey(&b);
1867
1868 #ifdef SK_DEBUG
1869         if (!rp->isExternal()) {
1870             // This is to ensure ReconstructAttachmentsDescriptor keeps matching
1871             // getSimpleRenderPass' result
1872             GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1873             GrVkRenderPass::AttachmentFlags attachmentFlags;
1874             GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1875                                                                &attachmentsDescriptor,
1876                                                                &attachmentFlags);
1877             SkASSERT(rp->isCompatible(attachmentsDescriptor, attachmentFlags, selfDepFlags,
1878                                       loadFromResolve));
1879         }
1880 #endif
1881     } else {
1882         GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1883         GrVkRenderPass::AttachmentFlags attachmentFlags;
1884         GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1885                                                            &attachmentsDescriptor,
1886                                                            &attachmentFlags);
1887
1888         // kExternal_AttachmentFlag is only set for wrapped secondary command buffers - which
1889         // will always go through the above 'rt' path (i.e., we can always pass 0 as the final
1890         // parameter to GenKey).
1891         GrVkRenderPass::GenKey(&b, attachmentFlags, attachmentsDescriptor, selfDepFlags,
1892                                loadFromResolve, 0);
1893     }
1894
1895     GrStencilSettings stencil = programInfo.nonGLStencilSettings();
1896     stencil.genKey(&b, true);
1897
1898     programInfo.pipeline().genKey(&b, *this);
1899     b.add32(programInfo.numSamples());
1900
1901     // Vulkan requires the full primitive type as part of its key
1902     b.add32(programInfo.primitiveTypeKey());
1903
1904     b.flush();
1905     return desc;
1906 }
1907
1908 GrInternalSurfaceFlags GrVkCaps::getExtraSurfaceFlagsForDeferredRT() const {
1909     // We always create vulkan RT with the input attachment flag;
1910     return GrInternalSurfaceFlags::kVkRTSupportsInputAttachment;
1911 }
1912
1913 VkShaderStageFlags GrVkCaps::getPushConstantStageFlags() const {
1914     VkShaderStageFlags stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
1915     return stageFlags;
1916 }
1917
1918 #if GR_TEST_UTILS
1919 std::vector<GrCaps::TestFormatColorTypeCombination> GrVkCaps::getTestingCombinations() const {
1920     std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
1921         { GrColorType::kAlpha_8,          GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1922         { GrColorType::kBGR_565,          GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16)  },
1923         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_R4G4B4A4_UNORM_PACK16)},
1924         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16)},
1925         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1926         { GrColorType::kRGBA_8888_SRGB,   GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB)        },
1927         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1928         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM)         },
1929         { GrColorType::kRG_88,            GrBackendFormat::MakeVk(VK_FORMAT_R8G8_UNORM)           },
1930         { GrColorType::kBGRA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM)       },
1931         { GrColorType::kRGBA_1010102,  GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32)},
1932         { GrColorType::kBGRA_1010102,  GrBackendFormat::MakeVk(VK_FORMAT_A2R10G10B10_UNORM_PACK32)},
1933         { GrColorType::kGray_8,           GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1934         { GrColorType::kAlpha_F16,        GrBackendFormat::MakeVk(VK_FORMAT_R16_SFLOAT)           },
1935         { GrColorType::kRGBA_F16,         GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1936         { GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1937         { GrColorType::kAlpha_16,         GrBackendFormat::MakeVk(VK_FORMAT_R16_UNORM)            },
1938         { GrColorType::kRG_1616,          GrBackendFormat::MakeVk(VK_FORMAT_R16G16_UNORM)         },
1939         { GrColorType::kRGBA_16161616,    GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_UNORM)   },
1940         { GrColorType::kRG_F16,           GrBackendFormat::MakeVk(VK_FORMAT_R16G16_SFLOAT)        },
1941         // These two compressed formats both have an effective colorType of kRGB_888x
1942         { GrColorType::kRGB_888x,       GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)},
1943         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK)  },
1944         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK) },
1945     };
1946
1947     return combos;
1948 }
1949 #endif