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