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