• 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 
initShaderCaps(const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceFeatures2 & features)700 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
701                               const VkPhysicalDeviceFeatures2& features) {
702     GrShaderCaps* shaderCaps = fShaderCaps.get();
703     shaderCaps->fVersionDeclString = "#version 330\n";
704 
705     // Vulkan is based off ES 3.0 so the following should all be supported
706     shaderCaps->fUsesPrecisionModifiers = true;
707     shaderCaps->fFlatInterpolationSupport = true;
708     // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
709     // to be true with Vulkan as well.
710     shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
711 
712     shaderCaps->fSampleMaskSupport = true;
713 
714     shaderCaps->fShaderDerivativeSupport = true;
715 
716     // ARM GPUs calculate `matrix * vector` in SPIR-V at full precision, even when the inputs are
717     // RelaxedPrecision. Rewriting the multiply as a sum of vector*scalar fixes this. (skia:11769)
718     shaderCaps->fRewriteMatrixVectorMultiply = (kARM_VkVendor == properties.vendorID || kHisi_VkVendor == properties.vendorID);
719 
720     shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
721 
722     shaderCaps->fIntegerSupport = true;
723     shaderCaps->fNonsquareMatrixSupport = true;
724     shaderCaps->fInverseHyperbolicSupport = true;
725     shaderCaps->fVertexIDSupport = true;
726     shaderCaps->fInfinitySupport = true;
727     shaderCaps->fNonconstantArrayIndexSupport = true;
728     shaderCaps->fBitManipulationSupport = true;
729 
730     // Assume the minimum precisions mandated by the SPIR-V spec.
731     shaderCaps->fFloatIs32Bits = true;
732     shaderCaps->fHalfIs32Bits = false;
733 
734     shaderCaps->fMaxFragmentSamplers = std::min(
735                                        std::min(properties.limits.maxPerStageDescriptorSampledImages,
736                                               properties.limits.maxPerStageDescriptorSamplers),
737                                               (uint32_t)INT_MAX);
738 }
739 
stencil_format_supported(const GrVkInterface * interface,VkPhysicalDevice physDev,VkFormat format)740 bool stencil_format_supported(const GrVkInterface* interface,
741                               VkPhysicalDevice physDev,
742                               VkFormat format) {
743     VkFormatProperties props;
744     memset(&props, 0, sizeof(VkFormatProperties));
745     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
746     return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
747 }
748 
initStencilFormat(const GrVkInterface * interface,VkPhysicalDevice physDev)749 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
750     if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
751         fPreferredStencilFormat = VK_FORMAT_S8_UINT;
752     } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
753         fPreferredStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;
754     } else {
755         SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
756         fPreferredStencilFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
757     }
758 }
759 
format_is_srgb(VkFormat format)760 static bool format_is_srgb(VkFormat format) {
761     SkASSERT(GrVkFormatIsSupported(format));
762 
763     switch (format) {
764         case VK_FORMAT_R8G8B8A8_SRGB:
765             return true;
766         default:
767             return false;
768     }
769 }
770 
771 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
772 // frequently used to least to improve look up times in arrays.
773 static constexpr VkFormat kVkFormats[] = {
774     VK_FORMAT_R8G8B8A8_UNORM,
775     VK_FORMAT_R8_UNORM,
776     VK_FORMAT_B8G8R8A8_UNORM,
777     VK_FORMAT_R5G6B5_UNORM_PACK16,
778     VK_FORMAT_R16G16B16A16_SFLOAT,
779     VK_FORMAT_R16_SFLOAT,
780     VK_FORMAT_R8G8B8_UNORM,
781     VK_FORMAT_R8G8_UNORM,
782     VK_FORMAT_A2B10G10R10_UNORM_PACK32,
783     VK_FORMAT_A2R10G10B10_UNORM_PACK32,
784     VK_FORMAT_B4G4R4A4_UNORM_PACK16,
785     VK_FORMAT_R4G4B4A4_UNORM_PACK16,
786     VK_FORMAT_R8G8B8A8_SRGB,
787     VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
788     VK_FORMAT_BC1_RGB_UNORM_BLOCK,
789     VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
790     VK_FORMAT_R16_UNORM,
791     VK_FORMAT_R16G16_UNORM,
792     VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
793     VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
794     VK_FORMAT_R16G16B16A16_UNORM,
795     VK_FORMAT_R16G16_SFLOAT,
796     VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
797     VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
798     VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
799 };
800 
setColorType(GrColorType colorType,std::initializer_list<VkFormat> formats)801 void GrVkCaps::setColorType(GrColorType colorType, std::initializer_list<VkFormat> formats) {
802 #ifdef SK_DEBUG
803     for (size_t i = 0; i < kNumVkFormats; ++i) {
804         const auto& formatInfo = fFormatTable[i];
805         for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
806             const auto& ctInfo = formatInfo.fColorTypeInfos[j];
807             if (ctInfo.fColorType == colorType &&
808                 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
809                 bool found = false;
810                 for (auto it = formats.begin(); it != formats.end(); ++it) {
811                     if (kVkFormats[i] == *it) {
812                         found = true;
813                     }
814                 }
815                 SkASSERT(found);
816             }
817         }
818     }
819 #endif
820     int idx = static_cast<int>(colorType);
821     for (auto it = formats.begin(); it != formats.end(); ++it) {
822         const auto& info = this->getFormatInfo(*it);
823         for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
824             if (info.fColorTypeInfos[i].fColorType == colorType) {
825                 fColorTypeToFormatTable[idx] = *it;
826                 return;
827             }
828         }
829     }
830 }
831 
getFormatInfo(VkFormat format) const832 const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
833     GrVkCaps* nonConstThis = const_cast<GrVkCaps*>(this);
834     return nonConstThis->getFormatInfo(format);
835 }
836 
getFormatInfo(VkFormat format)837 GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) {
838     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
839                   "Size of VkFormats array must match static value in header");
840     for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
841         if (kVkFormats[i] == format) {
842             return fFormatTable[i];
843         }
844     }
845     static FormatInfo kInvalidFormat;
846     return kInvalidFormat;
847 }
848 
initFormatTable(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties)849 void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
850                                const VkPhysicalDeviceProperties& properties) {
851     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
852                   "Size of VkFormats array must match static value in header");
853 
854     std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, VK_FORMAT_UNDEFINED);
855 
856     // Go through all the formats and init their support surface and data GrColorTypes.
857     // Format: VK_FORMAT_R8G8B8A8_UNORM
858     {
859         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
860         auto& info = this->getFormatInfo(format);
861         info.init(interface, physDev, properties, format);
862         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
863             info.fColorTypeInfoCount = 2;
864             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
865             int ctIdx = 0;
866             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
867             {
868                 constexpr GrColorType ct = GrColorType::kRGBA_8888;
869                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
870                 ctInfo.fColorType = ct;
871                 ctInfo.fTransferColorType = ct;
872                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
873             }
874             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
875             {
876                 constexpr GrColorType ct = GrColorType::kRGB_888x;
877                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
878                 ctInfo.fColorType = ct;
879                 ctInfo.fTransferColorType = ct;
880                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
881                 ctInfo.fReadSwizzle = GrSwizzle::RGB1();
882             }
883         }
884     }
885 
886     // Format: VK_FORMAT_R8_UNORM
887     {
888         constexpr VkFormat format = VK_FORMAT_R8_UNORM;
889         auto& info = this->getFormatInfo(format);
890         info.init(interface, physDev, properties, format);
891         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
892             info.fColorTypeInfoCount = 2;
893             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
894             int ctIdx = 0;
895             // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
896             {
897                 constexpr GrColorType ct = GrColorType::kAlpha_8;
898                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
899                 ctInfo.fColorType = ct;
900                 ctInfo.fTransferColorType = ct;
901                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
902                 ctInfo.fReadSwizzle = GrSwizzle("000r");
903                 ctInfo.fWriteSwizzle = GrSwizzle("a000");
904             }
905             // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
906             {
907                 constexpr GrColorType ct = GrColorType::kGray_8;
908                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
909                 ctInfo.fColorType = ct;
910                 ctInfo.fTransferColorType = ct;
911                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
912                 ctInfo.fReadSwizzle = GrSwizzle("rrr1");
913             }
914         }
915     }
916     // Format: VK_FORMAT_B8G8R8A8_UNORM
917     {
918         constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
919         auto& info = this->getFormatInfo(format);
920         info.init(interface, physDev, properties, format);
921         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
922             info.fColorTypeInfoCount = 1;
923             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
924             int ctIdx = 0;
925             // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
926             {
927                 constexpr GrColorType ct = GrColorType::kBGRA_8888;
928                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
929                 ctInfo.fColorType = ct;
930                 ctInfo.fTransferColorType = ct;
931                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
932             }
933         }
934     }
935     // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
936     {
937         constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
938         auto& info = this->getFormatInfo(format);
939         info.init(interface, physDev, properties, format);
940         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
941             info.fColorTypeInfoCount = 1;
942             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
943             int ctIdx = 0;
944             // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kBGR_565
945             {
946                 constexpr GrColorType ct = GrColorType::kBGR_565;
947                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
948                 ctInfo.fColorType = ct;
949                 ctInfo.fTransferColorType = ct;
950                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
951             }
952         }
953     }
954     // Format: VK_FORMAT_R16G16B16A16_SFLOAT
955     {
956         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
957         auto& info = this->getFormatInfo(format);
958         info.init(interface, physDev, properties, format);
959         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
960             info.fColorTypeInfoCount = 2;
961             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
962             int ctIdx = 0;
963             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16
964             {
965                 constexpr GrColorType ct = GrColorType::kRGBA_F16;
966                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
967                 ctInfo.fColorType = ct;
968                 ctInfo.fTransferColorType = ct;
969                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
970             }
971             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16_Clamped
972             {
973                 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
974                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
975                 ctInfo.fColorType = ct;
976                 ctInfo.fTransferColorType = ct;
977                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
978             }
979         }
980     }
981     // Format: VK_FORMAT_R16_SFLOAT
982     {
983         constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
984         auto& info = this->getFormatInfo(format);
985         info.init(interface, physDev, properties, format);
986         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
987             info.fColorTypeInfoCount = 1;
988             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
989             int ctIdx = 0;
990             // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
991             {
992                 constexpr GrColorType ct = GrColorType::kAlpha_F16;
993                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
994                 ctInfo.fColorType = ct;
995                 ctInfo.fTransferColorType = ct;
996                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
997                 ctInfo.fReadSwizzle = GrSwizzle("000r");
998                 ctInfo.fWriteSwizzle = GrSwizzle("a000");
999             }
1000         }
1001     }
1002     // Format: VK_FORMAT_R8G8B8_UNORM
1003     {
1004         constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
1005         auto& info = this->getFormatInfo(format);
1006         info.init(interface, physDev, properties, format);
1007         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1008             info.fColorTypeInfoCount = 1;
1009             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1010             int ctIdx = 0;
1011             // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
1012             {
1013                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1014                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1015                 ctInfo.fColorType = ct;
1016                 // The Vulkan format is 3 bpp so we must convert to/from that when transferring.
1017                 ctInfo.fTransferColorType = GrColorType::kRGB_888;
1018                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1019             }
1020         }
1021     }
1022     // Format: VK_FORMAT_R8G8_UNORM
1023     {
1024         constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
1025         auto& info = this->getFormatInfo(format);
1026         info.init(interface, physDev, properties, format);
1027         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1028             info.fColorTypeInfoCount = 1;
1029             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1030             int ctIdx = 0;
1031             // Format: VK_FORMAT_R8G8_UNORM, Surface: kRG_88
1032             {
1033                 constexpr GrColorType ct = GrColorType::kRG_88;
1034                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1035                 ctInfo.fColorType = ct;
1036                 ctInfo.fTransferColorType = ct;
1037                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1038             }
1039         }
1040     }
1041     // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
1042     {
1043         constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
1044         auto& info = this->getFormatInfo(format);
1045         info.init(interface, physDev, properties, format);
1046         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1047             info.fColorTypeInfoCount = 1;
1048             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1049             int ctIdx = 0;
1050             // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
1051             {
1052                 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
1053                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1054                 ctInfo.fColorType = ct;
1055                 ctInfo.fTransferColorType = ct;
1056                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1057             }
1058         }
1059     }
1060     // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32
1061     {
1062         constexpr VkFormat format = VK_FORMAT_A2R10G10B10_UNORM_PACK32;
1063         auto& info = this->getFormatInfo(format);
1064         info.init(interface, physDev, properties, format);
1065         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1066             info.fColorTypeInfoCount = 1;
1067             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1068             int ctIdx = 0;
1069             // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32, Surface: kBGRA_1010102
1070             {
1071                 constexpr GrColorType ct = GrColorType::kBGRA_1010102;
1072                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1073                 ctInfo.fColorType = ct;
1074                 ctInfo.fTransferColorType = ct;
1075                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1076             }
1077         }
1078     }
1079     // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
1080     {
1081         constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
1082         auto& info = this->getFormatInfo(format);
1083         info.init(interface, physDev, properties, format);
1084         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1085             info.fColorTypeInfoCount = 1;
1086             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1087             int ctIdx = 0;
1088             // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kABGR_4444
1089             {
1090                 constexpr GrColorType ct = GrColorType::kABGR_4444;
1091                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1092                 ctInfo.fColorType = ct;
1093                 ctInfo.fTransferColorType = ct;
1094                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1095                 ctInfo.fReadSwizzle = GrSwizzle::BGRA();
1096                 ctInfo.fWriteSwizzle = GrSwizzle::BGRA();
1097             }
1098         }
1099     }
1100 
1101     // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
1102     {
1103         constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
1104         auto& info = this->getFormatInfo(format);
1105         info.init(interface, physDev, properties, format);
1106         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1107             info.fColorTypeInfoCount = 1;
1108             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1109             int ctIdx = 0;
1110             // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kABGR_4444
1111             {
1112                 constexpr GrColorType ct = GrColorType::kABGR_4444;
1113                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1114                 ctInfo.fColorType = ct;
1115                 ctInfo.fTransferColorType = ct;
1116                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1117             }
1118         }
1119     }
1120     // Format: VK_FORMAT_R8G8B8A8_SRGB
1121     {
1122         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
1123         auto& info = this->getFormatInfo(format);
1124         info.init(interface, physDev, properties, format);
1125         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1126             info.fColorTypeInfoCount = 1;
1127             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1128             int ctIdx = 0;
1129             // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
1130             {
1131                 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
1132                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1133                 ctInfo.fColorType = ct;
1134                 ctInfo.fTransferColorType = ct;
1135                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1136             }
1137         }
1138     }
1139     // Format: VK_FORMAT_R16_UNORM
1140     {
1141         constexpr VkFormat format = VK_FORMAT_R16_UNORM;
1142         auto& info = this->getFormatInfo(format);
1143         info.init(interface, physDev, properties, format);
1144         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1145             info.fColorTypeInfoCount = 1;
1146             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1147             int ctIdx = 0;
1148             // Format: VK_FORMAT_R16_UNORM, Surface: kAlpha_16
1149             {
1150                 constexpr GrColorType ct = GrColorType::kAlpha_16;
1151                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1152                 ctInfo.fColorType = ct;
1153                 ctInfo.fTransferColorType = ct;
1154                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1155                 ctInfo.fReadSwizzle = GrSwizzle("000r");
1156                 ctInfo.fWriteSwizzle = GrSwizzle("a000");
1157             }
1158         }
1159     }
1160     // Format: VK_FORMAT_R16G16_UNORM
1161     {
1162         constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1163         auto& info = this->getFormatInfo(format);
1164         info.init(interface, physDev, properties, format);
1165         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1166             info.fColorTypeInfoCount = 1;
1167             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1168             int ctIdx = 0;
1169             // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1170             {
1171                 constexpr GrColorType ct = GrColorType::kRG_1616;
1172                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1173                 ctInfo.fColorType = ct;
1174                 ctInfo.fTransferColorType = ct;
1175                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1176             }
1177         }
1178     }
1179     // Format: VK_FORMAT_R16G16B16A16_UNORM
1180     {
1181         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1182         auto& info = this->getFormatInfo(format);
1183         info.init(interface, physDev, properties, format);
1184         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1185             info.fColorTypeInfoCount = 1;
1186             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1187             int ctIdx = 0;
1188             // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1189             {
1190                 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
1191                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1192                 ctInfo.fColorType = ct;
1193                 ctInfo.fTransferColorType = ct;
1194                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1195             }
1196         }
1197     }
1198     // Format: VK_FORMAT_R16G16_SFLOAT
1199     {
1200         constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1201         auto& info = this->getFormatInfo(format);
1202         info.init(interface, physDev, properties, format);
1203         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1204             info.fColorTypeInfoCount = 1;
1205             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1206             int ctIdx = 0;
1207             // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1208             {
1209                 constexpr GrColorType ct = GrColorType::kRG_F16;
1210                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1211                 ctInfo.fColorType = ct;
1212                 ctInfo.fTransferColorType = ct;
1213                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1214             }
1215         }
1216     }
1217     // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1218     {
1219         constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1220         auto& info = this->getFormatInfo(format);
1221         if (fSupportsYcbcrConversion) {
1222             info.init(interface, physDev, properties, format);
1223         }
1224         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1225             info.fColorTypeInfoCount = 1;
1226             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1227             int ctIdx = 0;
1228             // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1229             {
1230                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1231                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1232                 ctInfo.fColorType = ct;
1233                 ctInfo.fTransferColorType = ct;
1234                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1235             }
1236         }
1237     }
1238     // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1239     {
1240         constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1241         auto& info = this->getFormatInfo(format);
1242         if (fSupportsYcbcrConversion) {
1243             info.init(interface, physDev, properties, format);
1244         }
1245         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1246             info.fColorTypeInfoCount = 1;
1247             info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1248             int ctIdx = 0;
1249             // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1250             {
1251                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1252                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1253                 ctInfo.fColorType = ct;
1254                 ctInfo.fTransferColorType = ct;
1255                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1256             }
1257         }
1258     }
1259     // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1260     {
1261         constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1262         auto& info = this->getFormatInfo(format);
1263         info.init(interface, physDev, properties, format);
1264         // Setting this to texel block size
1265         // No supported GrColorTypes.
1266     }
1267 
1268     // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK
1269     {
1270         constexpr VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
1271         auto& info = this->getFormatInfo(format);
1272         info.init(interface, physDev, properties, format);
1273         // Setting this to texel block size
1274         // No supported GrColorTypes.
1275     }
1276 
1277     // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK
1278     {
1279         constexpr VkFormat format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
1280         auto& info = this->getFormatInfo(format);
1281         info.init(interface, physDev, properties, format);
1282         // Setting this to texel block size
1283         // No supported GrColorTypes.
1284     }
1285 
1286     // Format: VK_FORMAT_ASTC_4x4_UNORM_BLOCK
1287     {
1288         constexpr VkFormat format = VK_FORMAT_ASTC_4x4_UNORM_BLOCK;
1289         auto& info = this->getFormatInfo(format);
1290         info.init(interface, physDev, properties, format);
1291         // Setting this to texel block size
1292         // No supported GrColorTypes.
1293     }
1294 
1295     // Format: VK_FORMAT_ASTC_6x6_UNORM_BLOCK
1296     {
1297         constexpr VkFormat format = VK_FORMAT_ASTC_6x6_UNORM_BLOCK;
1298         auto& info = this->getFormatInfo(format);
1299         info.init(interface, physDev, properties, format);
1300         // Setting this to texel block size
1301         // No supported GrColorTypes.
1302     }
1303 
1304     // Format: VK_FORMAT_ASTC_8x8_UNORM_BLOCK
1305     {
1306         constexpr VkFormat format = VK_FORMAT_ASTC_8x8_UNORM_BLOCK;
1307         auto& info = this->getFormatInfo(format);
1308         info.init(interface, physDev, properties, format);
1309         // Setting this to texel block size
1310         // No supported GrColorTypes.
1311     }
1312 
1313     ////////////////////////////////////////////////////////////////////////////
1314     // Map GrColorTypes (used for creating GrSurfaces) to VkFormats. The order in which the formats
1315     // are passed into the setColorType function indicates the priority in selecting which format
1316     // we use for a given GrcolorType.
1317 
1318     this->setColorType(GrColorType::kAlpha_8,          { VK_FORMAT_R8_UNORM });
1319     this->setColorType(GrColorType::kBGR_565,          { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1320     this->setColorType(GrColorType::kABGR_4444,        { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1321                                                          VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1322     this->setColorType(GrColorType::kRGBA_8888,        { VK_FORMAT_R8G8B8A8_UNORM });
1323     this->setColorType(GrColorType::kRGBA_8888_SRGB,   { VK_FORMAT_R8G8B8A8_SRGB });
1324     this->setColorType(GrColorType::kRGB_888x,         { VK_FORMAT_R8G8B8_UNORM,
1325                                                          VK_FORMAT_R8G8B8A8_UNORM });
1326     this->setColorType(GrColorType::kRG_88,            { VK_FORMAT_R8G8_UNORM });
1327     this->setColorType(GrColorType::kBGRA_8888,        { VK_FORMAT_B8G8R8A8_UNORM });
1328     this->setColorType(GrColorType::kRGBA_1010102,     { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1329     this->setColorType(GrColorType::kBGRA_1010102,     { VK_FORMAT_A2R10G10B10_UNORM_PACK32 });
1330     this->setColorType(GrColorType::kGray_8,           { VK_FORMAT_R8_UNORM });
1331     this->setColorType(GrColorType::kAlpha_F16,        { VK_FORMAT_R16_SFLOAT });
1332     this->setColorType(GrColorType::kRGBA_F16,         { VK_FORMAT_R16G16B16A16_SFLOAT });
1333     this->setColorType(GrColorType::kRGBA_F16_Clamped, { VK_FORMAT_R16G16B16A16_SFLOAT });
1334     this->setColorType(GrColorType::kAlpha_16,         { VK_FORMAT_R16_UNORM });
1335     this->setColorType(GrColorType::kRG_1616,          { VK_FORMAT_R16G16_UNORM });
1336     this->setColorType(GrColorType::kRGBA_16161616,    { VK_FORMAT_R16G16B16A16_UNORM });
1337     this->setColorType(GrColorType::kRG_F16,           { VK_FORMAT_R16G16_SFLOAT });
1338 }
1339 
InitFormatFlags(VkFormatFeatureFlags vkFlags,uint16_t * flags)1340 void GrVkCaps::FormatInfo::InitFormatFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
1341     if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
1342         SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
1343         *flags = *flags | kTexturable_Flag;
1344 
1345         // Ganesh assumes that all renderable surfaces are also texturable
1346         if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
1347             *flags = *flags | kRenderable_Flag;
1348         }
1349     }
1350     // TODO: For Vk w/ VK_KHR_maintenance1 extension support, check
1351     //  VK_FORMAT_FEATURE_TRANSFER_[SRC|DST]_BIT_KHR explicitly to set copy flags
1352     //  Can do similar check for VK_KHR_sampler_ycbcr_conversion added bits
1353 
1354     if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
1355         *flags = *flags | kBlitSrc_Flag;
1356     }
1357 
1358     if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
1359         *flags = *flags | kBlitDst_Flag;
1360     }
1361 }
1362 
initSampleCounts(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & physProps,VkFormat format)1363 void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
1364                                             VkPhysicalDevice physDev,
1365                                             const VkPhysicalDeviceProperties& physProps,
1366                                             VkFormat format) {
1367     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1368                               VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1369                               VK_IMAGE_USAGE_SAMPLED_BIT |
1370                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1371     VkImageFormatProperties properties;
1372     GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
1373                                                                  format,
1374                                                                  VK_IMAGE_TYPE_2D,
1375                                                                  VK_IMAGE_TILING_OPTIMAL,
1376                                                                  usage,
1377                                                                  0,  // createFlags
1378                                                                  &properties));
1379     VkSampleCountFlags flags = properties.sampleCounts;
1380     if (flags & VK_SAMPLE_COUNT_1_BIT) {
1381         fColorSampleCounts.push_back(1);
1382     }
1383     if (kImagination_VkVendor == physProps.vendorID) {
1384         // MSAA does not work on imagination
1385         return;
1386     }
1387     if (kIntel_VkVendor == physProps.vendorID) {
1388         // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
1389         return;
1390     }
1391     if (flags & VK_SAMPLE_COUNT_2_BIT) {
1392         fColorSampleCounts.push_back(2);
1393     }
1394     if (flags & VK_SAMPLE_COUNT_4_BIT) {
1395         fColorSampleCounts.push_back(4);
1396     }
1397     if (flags & VK_SAMPLE_COUNT_8_BIT) {
1398         fColorSampleCounts.push_back(8);
1399     }
1400     if (flags & VK_SAMPLE_COUNT_16_BIT) {
1401         fColorSampleCounts.push_back(16);
1402     }
1403     // Standard sample locations are not defined for more than 16 samples, and we don't need more
1404     // than 16. Omit 32 and 64.
1405 }
1406 
init(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,VkFormat format)1407 void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
1408                                 VkPhysicalDevice physDev,
1409                                 const VkPhysicalDeviceProperties& properties,
1410                                 VkFormat format) {
1411     VkFormatProperties props;
1412     memset(&props, 0, sizeof(VkFormatProperties));
1413     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
1414     InitFormatFlags(props.linearTilingFeatures, &fLinearFlags);
1415     InitFormatFlags(props.optimalTilingFeatures, &fOptimalFlags);
1416     if (fOptimalFlags & kRenderable_Flag) {
1417         this->initSampleCounts(interface, physDev, properties, format);
1418     }
1419 }
1420 
1421 // For many checks in caps, we need to know whether the GrBackendFormat is external or not. If it is
1422 // external the VkFormat will be VK_NULL_HANDLE which is not handled by our various format
1423 // capability checks.
backend_format_is_external(const GrBackendFormat & format)1424 static bool backend_format_is_external(const GrBackendFormat& format) {
1425     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1426     SkASSERT(ycbcrInfo);
1427 
1428     // All external formats have a valid ycbcrInfo used for sampling and a non zero external format.
1429     if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1430 #ifdef SK_DEBUG
1431         VkFormat vkFormat;
1432         SkAssertResult(format.asVkFormat(&vkFormat));
1433         SkASSERT(vkFormat == VK_FORMAT_UNDEFINED);
1434 #endif
1435         return true;
1436     }
1437     return false;
1438 }
1439 
isFormatSRGB(const GrBackendFormat & format) const1440 bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
1441     VkFormat vkFormat;
1442     if (!format.asVkFormat(&vkFormat)) {
1443         return false;
1444     }
1445     if (backend_format_is_external(format)) {
1446         return false;
1447     }
1448 
1449     return format_is_srgb(vkFormat);
1450 }
1451 
isFormatTexturable(const GrBackendFormat & format,GrTextureType) const1452 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format, GrTextureType) const {
1453     VkFormat vkFormat;
1454     if (!format.asVkFormat(&vkFormat)) {
1455         return false;
1456     }
1457     if (backend_format_is_external(format)) {
1458         // We can always texture from an external format (assuming we have the ycbcr conversion
1459         // info which we require to be passed in).
1460         return true;
1461     }
1462     return this->isVkFormatTexturable(vkFormat);
1463 }
1464 
isVkFormatTexturable(VkFormat format) const1465 bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
1466     const FormatInfo& info = this->getFormatInfo(format);
1467     return SkToBool(FormatInfo::kTexturable_Flag & info.fOptimalFlags);
1468 }
1469 
isFormatAsColorTypeRenderable(GrColorType ct,const GrBackendFormat & format,int sampleCount) const1470 bool GrVkCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
1471                                              int sampleCount) const {
1472     if (!this->isFormatRenderable(format, sampleCount)) {
1473         return false;
1474     }
1475     VkFormat vkFormat;
1476     if (!format.asVkFormat(&vkFormat)) {
1477         return false;
1478     }
1479     const auto& info = this->getFormatInfo(vkFormat);
1480     if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
1481         return false;
1482     }
1483     return true;
1484 }
1485 
isFormatRenderable(const GrBackendFormat & format,int sampleCount) const1486 bool GrVkCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
1487     VkFormat vkFormat;
1488     if (!format.asVkFormat(&vkFormat)) {
1489         return false;
1490     }
1491     return this->isFormatRenderable(vkFormat, sampleCount);
1492 }
1493 
isFormatRenderable(VkFormat format,int sampleCount) const1494 bool GrVkCaps::isFormatRenderable(VkFormat format, int sampleCount) const {
1495     return sampleCount <= this->maxRenderTargetSampleCount(format);
1496 }
1497 
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & format) const1498 int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
1499                                          const GrBackendFormat& format) const {
1500     VkFormat vkFormat;
1501     if (!format.asVkFormat(&vkFormat)) {
1502         return 0;
1503     }
1504 
1505     return this->getRenderTargetSampleCount(requestedCount, vkFormat);
1506 }
1507 
getRenderTargetSampleCount(int requestedCount,VkFormat format) const1508 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
1509     requestedCount = std::max(1, requestedCount);
1510 
1511     const FormatInfo& info = this->getFormatInfo(format);
1512 
1513     int count = info.fColorSampleCounts.count();
1514 
1515     if (!count) {
1516         return 0;
1517     }
1518 
1519     if (1 == requestedCount) {
1520         SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
1521         return 1;
1522     }
1523 
1524     for (int i = 0; i < count; ++i) {
1525         if (info.fColorSampleCounts[i] >= requestedCount) {
1526             return info.fColorSampleCounts[i];
1527         }
1528     }
1529     return 0;
1530 }
1531 
maxRenderTargetSampleCount(const GrBackendFormat & format) const1532 int GrVkCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
1533     VkFormat vkFormat;
1534     if (!format.asVkFormat(&vkFormat)) {
1535         return 0;
1536     }
1537     return this->maxRenderTargetSampleCount(vkFormat);
1538 }
1539 
maxRenderTargetSampleCount(VkFormat format) const1540 int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
1541     const FormatInfo& info = this->getFormatInfo(format);
1542 
1543     const auto& table = info.fColorSampleCounts;
1544     if (!table.count()) {
1545         return 0;
1546     }
1547     return table[table.count() - 1];
1548 }
1549 
align_to_4(size_t v)1550 static inline size_t align_to_4(size_t v) {
1551     switch (v & 0b11) {
1552         // v is already a multiple of 4.
1553         case 0:     return v;
1554         // v is a multiple of 2 but not 4.
1555         case 2:     return 2 * v;
1556         // v is not a multiple of 2.
1557         default:    return 4 * v;
1558     }
1559 }
1560 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType) const1561 GrCaps::SupportedWrite GrVkCaps::supportedWritePixelsColorType(GrColorType surfaceColorType,
1562                                                                const GrBackendFormat& surfaceFormat,
1563                                                                GrColorType srcColorType) const {
1564     VkFormat vkFormat;
1565     if (!surfaceFormat.asVkFormat(&vkFormat)) {
1566         return {GrColorType::kUnknown, 0};
1567     }
1568 
1569     // We don't support the ability to upload to external formats or formats that require a ycbcr
1570     // sampler. In general these types of formats are only used for sampling in a shader.
1571     if (backend_format_is_external(surfaceFormat) || GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1572         return {GrColorType::kUnknown, 0};
1573     }
1574 
1575     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1576     size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1577 
1578     const auto& info = this->getFormatInfo(vkFormat);
1579     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1580         const auto& ctInfo = info.fColorTypeInfos[i];
1581         if (ctInfo.fColorType == surfaceColorType) {
1582             return {ctInfo.fTransferColorType, offsetAlignment};
1583         }
1584     }
1585     return {GrColorType::kUnknown, 0};
1586 }
1587 
surfaceSupportsReadPixels(const GrSurface * surface) const1588 GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
1589         const GrSurface* surface) const {
1590     if (surface->isProtected()) {
1591         return SurfaceReadPixelsSupport::kUnsupported;
1592     }
1593     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1594         auto texImage = tex->textureImage();
1595         if (!texImage) {
1596             return SurfaceReadPixelsSupport::kUnsupported;
1597         }
1598         // We can't directly read from a VkImage that has a ycbcr sampler.
1599         if (texImage->ycbcrConversionInfo().isValid()) {
1600             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1601         }
1602         // We can't directly read from a compressed format
1603         if (GrVkFormatIsCompressed(texImage->imageFormat())) {
1604             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1605         }
1606         return SurfaceReadPixelsSupport::kSupported;
1607     } else if (auto rt = surface->asRenderTarget()) {
1608         if (rt->numSamples() > 1) {
1609             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1610         }
1611         return SurfaceReadPixelsSupport::kSupported;
1612     }
1613     return SurfaceReadPixelsSupport::kUnsupported;
1614 }
1615 
transferColorType(VkFormat vkFormat,GrColorType surfaceColorType) const1616 GrColorType GrVkCaps::transferColorType(VkFormat vkFormat, GrColorType surfaceColorType) const {
1617     const auto& info = this->getFormatInfo(vkFormat);
1618     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1619         if (info.fColorTypeInfos[i].fColorType == surfaceColorType) {
1620             return info.fColorTypeInfos[i].fTransferColorType;
1621         }
1622     }
1623     return GrColorType::kUnknown;
1624 }
1625 
onSurfaceSupportsWritePixels(const GrSurface * surface) const1626 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1627     if (auto rt = surface->asRenderTarget()) {
1628         return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1629     }
1630     // We can't write to a texture that has a ycbcr sampler.
1631     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1632         auto texImage = tex->textureImage();
1633         if (!texImage) {
1634             return false;
1635         }
1636         // We can't directly read from a VkImage that has a ycbcr sampler.
1637         if (texImage->ycbcrConversionInfo().isValid()) {
1638             return false;
1639         }
1640     }
1641     return true;
1642 }
1643 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format) const1644 bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1645                                                  const GrBackendFormat& format) const {
1646     VkFormat vkFormat;
1647     if (!format.asVkFormat(&vkFormat)) {
1648         return false;
1649     }
1650     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1651     SkASSERT(ycbcrInfo);
1652 
1653     if (ycbcrInfo->isValid() && !GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1654         // Format may be undefined for external images, which are required to have YCbCr conversion.
1655         if (VK_FORMAT_UNDEFINED == vkFormat && ycbcrInfo->fExternalFormat != 0) {
1656             return true;
1657         }
1658         return false;
1659     }
1660 
1661     const auto& info = this->getFormatInfo(vkFormat);
1662     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1663         if (info.fColorTypeInfos[i].fColorType == ct) {
1664             return true;
1665         }
1666     }
1667     return false;
1668 }
1669 
onGetDefaultBackendFormat(GrColorType ct) const1670 GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct) const {
1671     VkFormat format = this->getFormatFromColorType(ct);
1672     if (format == VK_FORMAT_UNDEFINED) {
1673         return {};
1674     }
1675     return GrBackendFormat::MakeVk(format);
1676 }
1677 
onSupportsDynamicMSAA(const GrRenderTargetProxy * rtProxy) const1678 bool GrVkCaps::onSupportsDynamicMSAA(const GrRenderTargetProxy* rtProxy) const {
1679     // We must be able to use the rtProxy as an input attachment to load into the discardable msaa
1680     // attachment. Also the rtProxy should have a sample count of 1 so that it can be used as a
1681     // resolve attachment.
1682     return this->supportsDiscardableMSAAForDMSAA() &&
1683            rtProxy->supportsVkInputAttachment() &&
1684            rtProxy->numSamples() == 1;
1685 }
1686 
renderTargetSupportsDiscardableMSAA(const GrVkRenderTarget * rt) const1687 bool GrVkCaps::renderTargetSupportsDiscardableMSAA(const GrVkRenderTarget* rt) const {
1688     return rt->resolveAttachment() &&
1689            rt->resolveAttachment()->supportsInputAttachmentUsage() &&
1690            ((rt->numSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1691             (rt->numSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1692 }
1693 
programInfoWillUseDiscardableMSAA(const GrProgramInfo & programInfo) const1694 bool GrVkCaps::programInfoWillUseDiscardableMSAA(const GrProgramInfo& programInfo) const {
1695     return programInfo.targetHasVkResolveAttachmentWithInput() &&
1696            programInfo.numSamples() > 1 &&
1697            ((programInfo.targetsNumSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1698             (programInfo.targetsNumSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1699 }
1700 
getBackendFormatFromCompressionType(SkImage::CompressionType compressionType) const1701 GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1702         SkImage::CompressionType compressionType) const {
1703     switch (compressionType) {
1704         case SkImage::CompressionType::kNone:
1705             return {};
1706         case SkImage::CompressionType::kETC2_RGB8_UNORM:
1707             if (this->isVkFormatTexturable(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)) {
1708                 return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1709             }
1710             return {};
1711         case SkImage::CompressionType::kBC1_RGB8_UNORM:
1712             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGB_UNORM_BLOCK)) {
1713                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK);
1714             }
1715             return {};
1716         case SkImage::CompressionType::kBC1_RGBA8_UNORM:
1717             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGBA_UNORM_BLOCK)) {
1718                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK);
1719             }
1720             return {};
1721         case SkImage::CompressionType::kASTC_RGBA8_4x4:
1722             if (this->isVkFormatTexturable(VK_FORMAT_ASTC_4x4_UNORM_BLOCK)) {
1723                 return GrBackendFormat::MakeVk(VK_FORMAT_ASTC_4x4_UNORM_BLOCK);
1724             }
1725             return {};
1726         case SkImage::CompressionType::kASTC_RGBA8_6x6:
1727             if (this->isVkFormatTexturable(VK_FORMAT_ASTC_6x6_UNORM_BLOCK)) {
1728                 return GrBackendFormat::MakeVk(VK_FORMAT_ASTC_6x6_UNORM_BLOCK);
1729             }
1730             return {};
1731         case SkImage::CompressionType::kASTC_RGBA8_8x8:
1732             if (this->isVkFormatTexturable(VK_FORMAT_ASTC_8x8_UNORM_BLOCK)) {
1733                 return GrBackendFormat::MakeVk(VK_FORMAT_ASTC_8x8_UNORM_BLOCK);
1734             }
1735             return {};
1736     }
1737 
1738     SkUNREACHABLE;
1739 }
1740 
onGetReadSwizzle(const GrBackendFormat & format,GrColorType colorType) const1741 GrSwizzle GrVkCaps::onGetReadSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1742     VkFormat vkFormat;
1743     SkAssertResult(format.asVkFormat(&vkFormat));
1744     const auto* ycbcrInfo = format.getVkYcbcrConversionInfo();
1745     SkASSERT(ycbcrInfo);
1746     if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1747         // We allow these to work with any color type and never swizzle. See
1748         // onAreColorTypeAndFormatCompatible.
1749         return GrSwizzle{"rgba"};
1750     }
1751 
1752     const auto& info = this->getFormatInfo(vkFormat);
1753     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1754         const auto& ctInfo = info.fColorTypeInfos[i];
1755         if (ctInfo.fColorType == colorType) {
1756             return ctInfo.fReadSwizzle;
1757         }
1758     }
1759     SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1760                  (int)colorType, (int)vkFormat);
1761     return {};
1762 }
1763 
getWriteSwizzle(const GrBackendFormat & format,GrColorType colorType) const1764 GrSwizzle GrVkCaps::getWriteSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1765     VkFormat vkFormat;
1766     SkAssertResult(format.asVkFormat(&vkFormat));
1767     const auto& info = this->getFormatInfo(vkFormat);
1768     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1769         const auto& ctInfo = info.fColorTypeInfos[i];
1770         if (ctInfo.fColorType == colorType) {
1771             return ctInfo.fWriteSwizzle;
1772         }
1773     }
1774     SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1775                  (int)colorType, (int)vkFormat);
1776     return {};
1777 }
1778 
onGetDstSampleFlagsForProxy(const GrRenderTargetProxy * rt) const1779 GrDstSampleFlags GrVkCaps::onGetDstSampleFlagsForProxy(const GrRenderTargetProxy* rt) const {
1780     bool isMSAAWithResolve = rt->numSamples() > 1 && rt->asTextureProxy();
1781     // TODO: Currently if we have an msaa rt with a resolve, the supportsVkInputAttachment call
1782     // references whether the resolve is supported as an input attachment. We need to add a check to
1783     // allow checking the color attachment (msaa or not) supports input attachment specifically.
1784     if (!isMSAAWithResolve && rt->supportsVkInputAttachment()) {
1785         return GrDstSampleFlags::kRequiresTextureBarrier | GrDstSampleFlags::kAsInputAttachment;
1786     }
1787     return GrDstSampleFlags::kNone;
1788 }
1789 
computeFormatKey(const GrBackendFormat & format) const1790 uint64_t GrVkCaps::computeFormatKey(const GrBackendFormat& format) const {
1791     VkFormat vkFormat;
1792     SkAssertResult(format.asVkFormat(&vkFormat));
1793 
1794 #ifdef SK_DEBUG
1795     // We should never be trying to compute a key for an external format
1796     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1797     SkASSERT(ycbcrInfo);
1798     SkASSERT(!ycbcrInfo->isValid() || ycbcrInfo->fExternalFormat == 0);
1799 #endif
1800 
1801     // A VkFormat has a size of 64 bits.
1802     return (uint64_t)vkFormat;
1803 }
1804 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat & srcBackendFormat,GrColorType dstColorType) const1805 GrCaps::SupportedRead GrVkCaps::onSupportedReadPixelsColorType(
1806         GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1807         GrColorType dstColorType) const {
1808     VkFormat vkFormat;
1809     if (!srcBackendFormat.asVkFormat(&vkFormat)) {
1810         return {GrColorType::kUnknown, 0};
1811     }
1812 
1813     if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1814         return {GrColorType::kUnknown, 0};
1815     }
1816 
1817     SkImage::CompressionType compression = GrBackendFormatToCompressionType(srcBackendFormat);
1818     if (compression != SkImage::CompressionType::kNone) {
1819         return { SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1820                                                         : GrColorType::kRGBA_8888, 0 };
1821     }
1822 
1823     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1824     size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1825 
1826     const auto& info = this->getFormatInfo(vkFormat);
1827     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1828         const auto& ctInfo = info.fColorTypeInfos[i];
1829         if (ctInfo.fColorType == srcColorType) {
1830             return {ctInfo.fTransferColorType, offsetAlignment};
1831         }
1832     }
1833     return {GrColorType::kUnknown, 0};
1834 }
1835 
getFragmentUniformBinding() const1836 int GrVkCaps::getFragmentUniformBinding() const {
1837     return GrVkUniformHandler::kUniformBinding;
1838 }
1839 
getFragmentUniformSet() const1840 int GrVkCaps::getFragmentUniformSet() const {
1841     return GrVkUniformHandler::kUniformBufferDescSet;
1842 }
1843 
addExtraSamplerKey(GrProcessorKeyBuilder * b,GrSamplerState samplerState,const GrBackendFormat & format) const1844 void GrVkCaps::addExtraSamplerKey(GrProcessorKeyBuilder* b,
1845                                   GrSamplerState samplerState,
1846                                   const GrBackendFormat& format) const {
1847     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1848     if (!ycbcrInfo) {
1849         return;
1850     }
1851 
1852     GrVkSampler::Key key = GrVkSampler::GenerateKey(samplerState, *ycbcrInfo);
1853 
1854     constexpr size_t numInts = (sizeof(key) + 3) / 4;
1855     uint32_t tmp[numInts];
1856     memcpy(tmp, &key, sizeof(key));
1857 
1858     for (size_t i = 0; i < numInts; ++i) {
1859         b->add32(tmp[i]);
1860     }
1861 }
1862 
1863 /**
1864  * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
1865  * the information needed to differentiate one pipeline from another.
1866  *
1867  * The GrProgramDesc contains all the information need to create the actual shaders for the
1868  * pipeline.
1869  *
1870  * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
1871  * pipline. This includes stencil settings, blending information, render pass format, draw face
1872  * information, and primitive type. Note that some state is set dynamically on the pipeline for
1873  * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
1874  * and blend constant.
1875  */
makeDesc(GrRenderTarget * rt,const GrProgramInfo & programInfo,ProgramDescOverrideFlags overrideFlags) const1876 GrProgramDesc GrVkCaps::makeDesc(GrRenderTarget* rt,
1877                                  const GrProgramInfo& programInfo,
1878                                  ProgramDescOverrideFlags overrideFlags) const {
1879     GrProgramDesc desc;
1880     GrProgramDesc::Build(&desc, programInfo, *this);
1881 
1882     GrProcessorKeyBuilder b(desc.key());
1883 
1884     // This will become part of the sheared off key used to persistently cache
1885     // the SPIRV code. It needs to be added right after the base key so that,
1886     // when the base-key is sheared off, the shearing code can include it in the
1887     // reduced key (c.f. the +4s in the SkData::MakeWithCopy calls in
1888     // GrVkPipelineStateBuilder.cpp).
1889     b.add32(GrVkGpu::kShader_PersistentCacheKeyType);
1890 
1891     GrVkRenderPass::SelfDependencyFlags selfDepFlags = GrVkRenderPass::SelfDependencyFlags::kNone;
1892     if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kBlend) {
1893         selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForNonCoherentAdvBlend;
1894     }
1895     if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kTexture) {
1896         selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForInputAttachment;
1897     }
1898 
1899     bool needsResolve = this->programInfoWillUseDiscardableMSAA(programInfo);
1900 
1901     bool forceLoadFromResolve =
1902             overrideFlags & GrCaps::ProgramDescOverrideFlags::kVulkanHasResolveLoadSubpass;
1903     SkASSERT(!forceLoadFromResolve || needsResolve);
1904 
1905     GrVkRenderPass::LoadFromResolve loadFromResolve = GrVkRenderPass::LoadFromResolve::kNo;
1906     if (needsResolve && (programInfo.colorLoadOp() == GrLoadOp::kLoad || forceLoadFromResolve)) {
1907         loadFromResolve = GrVkRenderPass::LoadFromResolve::kLoad;
1908     }
1909 
1910     if (rt) {
1911         GrVkRenderTarget* vkRT = (GrVkRenderTarget*) rt;
1912 
1913         SkASSERT(!needsResolve || (vkRT->resolveAttachment() &&
1914                                    vkRT->resolveAttachment()->supportsInputAttachmentUsage()));
1915 
1916         bool needsStencil = programInfo.needsStencil() || programInfo.isStencilEnabled();
1917         // TODO: support failure in getSimpleRenderPass
1918         auto rp = vkRT->getSimpleRenderPass(needsResolve, needsStencil, selfDepFlags,
1919                                             loadFromResolve);
1920         SkASSERT(rp);
1921         rp->genKey(&b);
1922 
1923 #ifdef SK_DEBUG
1924         if (!rp->isExternal()) {
1925             // This is to ensure ReconstructAttachmentsDescriptor keeps matching
1926             // getSimpleRenderPass' result
1927             GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1928             GrVkRenderPass::AttachmentFlags attachmentFlags;
1929             GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1930                                                                &attachmentsDescriptor,
1931                                                                &attachmentFlags);
1932             SkASSERT(rp->isCompatible(attachmentsDescriptor, attachmentFlags, selfDepFlags,
1933                                       loadFromResolve));
1934         }
1935 #endif
1936     } else {
1937         GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1938         GrVkRenderPass::AttachmentFlags attachmentFlags;
1939         GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1940                                                            &attachmentsDescriptor,
1941                                                            &attachmentFlags);
1942 
1943         // kExternal_AttachmentFlag is only set for wrapped secondary command buffers - which
1944         // will always go through the above 'rt' path (i.e., we can always pass 0 as the final
1945         // parameter to GenKey).
1946         GrVkRenderPass::GenKey(&b, attachmentFlags, attachmentsDescriptor, selfDepFlags,
1947                                loadFromResolve, 0);
1948     }
1949 
1950     GrStencilSettings stencil = programInfo.nonGLStencilSettings();
1951     stencil.genKey(&b, true);
1952 
1953     programInfo.pipeline().genKey(&b, *this);
1954     b.add32(programInfo.numSamples());
1955 
1956     // Vulkan requires the full primitive type as part of its key
1957     b.add32(programInfo.primitiveTypeKey());
1958 
1959     b.flush();
1960     return desc;
1961 }
1962 
getExtraSurfaceFlagsForDeferredRT() const1963 GrInternalSurfaceFlags GrVkCaps::getExtraSurfaceFlagsForDeferredRT() const {
1964     // We always create vulkan RT with the input attachment flag;
1965     return GrInternalSurfaceFlags::kVkRTSupportsInputAttachment;
1966 }
1967 
getPushConstantStageFlags() const1968 VkShaderStageFlags GrVkCaps::getPushConstantStageFlags() const {
1969     VkShaderStageFlags stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
1970     return stageFlags;
1971 }
1972 
1973 #if GR_TEST_UTILS
getTestingCombinations() const1974 std::vector<GrCaps::TestFormatColorTypeCombination> GrVkCaps::getTestingCombinations() const {
1975     std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
1976         { GrColorType::kAlpha_8,          GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1977         { GrColorType::kBGR_565,          GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16)  },
1978         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_R4G4B4A4_UNORM_PACK16)},
1979         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16)},
1980         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1981         { GrColorType::kRGBA_8888_SRGB,   GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB)        },
1982         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1983         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM)         },
1984         { GrColorType::kRG_88,            GrBackendFormat::MakeVk(VK_FORMAT_R8G8_UNORM)           },
1985         { GrColorType::kBGRA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM)       },
1986         { GrColorType::kRGBA_1010102,  GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32)},
1987         { GrColorType::kBGRA_1010102,  GrBackendFormat::MakeVk(VK_FORMAT_A2R10G10B10_UNORM_PACK32)},
1988         { GrColorType::kGray_8,           GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1989         { GrColorType::kAlpha_F16,        GrBackendFormat::MakeVk(VK_FORMAT_R16_SFLOAT)           },
1990         { GrColorType::kRGBA_F16,         GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1991         { GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1992         { GrColorType::kAlpha_16,         GrBackendFormat::MakeVk(VK_FORMAT_R16_UNORM)            },
1993         { GrColorType::kRG_1616,          GrBackendFormat::MakeVk(VK_FORMAT_R16G16_UNORM)         },
1994         { GrColorType::kRGBA_16161616,    GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_UNORM)   },
1995         { GrColorType::kRG_F16,           GrBackendFormat::MakeVk(VK_FORMAT_R16G16_SFLOAT)        },
1996         // These two compressed formats both have an effective colorType of kRGB_888x
1997         { GrColorType::kRGB_888x,       GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)},
1998         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK)  },
1999         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK) },
2000         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_ASTC_4x4_UNORM_BLOCK) },
2001         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_ASTC_6x6_UNORM_BLOCK) },
2002         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_ASTC_8x8_UNORM_BLOCK) },
2003     };
2004 
2005     return combos;
2006 }
2007 #endif
2008