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