• 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 "include/gpu/GrBackendSurface.h"
9 #include "include/gpu/vk/GrVkBackendContext.h"
10 #include "include/gpu/vk/GrVkExtensions.h"
11 #include "src/core/SkCompressedDataUtils.h"
12 #include "src/gpu/GrProgramDesc.h"
13 #include "src/gpu/GrRenderTarget.h"
14 #include "src/gpu/GrRenderTargetProxy.h"
15 #include "src/gpu/GrShaderCaps.h"
16 #include "src/gpu/GrStencilSettings.h"
17 #include "src/gpu/GrUtil.h"
18 #include "src/gpu/SkGr.h"
19 #include "src/gpu/vk/GrVkCaps.h"
20 #include "src/gpu/vk/GrVkGpu.h"
21 #include "src/gpu/vk/GrVkInterface.h"
22 #include "src/gpu/vk/GrVkRenderTarget.h"
23 #include "src/gpu/vk/GrVkTexture.h"
24 #include "src/gpu/vk/GrVkUniformHandler.h"
25 #include "src/gpu/vk/GrVkUtil.h"
26 
27 #ifdef SK_BUILD_FOR_ANDROID
28 #include <sys/system_properties.h>
29 #endif
30 
GrVkCaps(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t instanceVersion,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions,GrProtected isProtected)31 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
32                    VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
33                    uint32_t instanceVersion, uint32_t physicalDeviceVersion,
34                    const GrVkExtensions& extensions, GrProtected isProtected)
35         : INHERITED(contextOptions) {
36     /**************************************************************************
37      * GrCaps fields
38      **************************************************************************/
39     fMipMapSupport = true;   // always available in Vulkan
40     fNPOTTextureTileSupport = true;  // always available in Vulkan
41     fReuseScratchTextures = true; //TODO: figure this out
42     fGpuTracingSupport = false; //TODO: figure this out
43     fOversizedStencilSupport = false; //TODO: figure this out
44     fInstanceAttribSupport = true;
45 
46     fSemaphoreSupport = true;   // always available in Vulkan
47     fFenceSyncSupport = true;   // always available in Vulkan
48     fCrossContextTextureSupport = true;
49     fHalfFloatVertexAttributeSupport = true;
50 
51     // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
52     fReadPixelsRowBytesSupport = true;
53     fWritePixelsRowBytesSupport = true;
54 
55     fTransferFromBufferToTextureSupport = true;
56     fTransferFromSurfaceToBufferSupport = true;
57 
58     fMaxRenderTargetSize = 4096; // minimum required by spec
59     fMaxTextureSize = 4096; // minimum required by spec
60 
61     fDynamicStateArrayGeometryProcessorTextureSupport = true;
62 
63     fShaderCaps.reset(new GrShaderCaps(contextOptions));
64 
65     this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions,
66                isProtected);
67 }
68 
69 namespace {
70 /**
71  * This comes from section 37.1.6 of the Vulkan spec. Format is
72  * (<bits>|<tag>)_<block_size>_<texels_per_block>.
73  */
74 enum class FormatCompatibilityClass {
75     k8_1_1,
76     k16_2_1,
77     k24_3_1,
78     k32_4_1,
79     k64_8_1,
80     kBC1_RGB_8_16_1,
81     kBC1_RGBA_8_16,
82     kETC2_RGB_8_16,
83 };
84 }  // anonymous namespace
85 
format_compatibility_class(VkFormat format)86 static FormatCompatibilityClass format_compatibility_class(VkFormat format) {
87     switch (format) {
88         case VK_FORMAT_B8G8R8A8_UNORM:
89         case VK_FORMAT_R8G8B8A8_UNORM:
90         case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
91         case VK_FORMAT_R8G8B8A8_SRGB:
92         case VK_FORMAT_R16G16_UNORM:
93         case VK_FORMAT_R16G16_SFLOAT:
94             return FormatCompatibilityClass::k32_4_1;
95 
96         case VK_FORMAT_R8_UNORM:
97             return FormatCompatibilityClass::k8_1_1;
98 
99         case VK_FORMAT_R5G6B5_UNORM_PACK16:
100         case VK_FORMAT_R16_SFLOAT:
101         case VK_FORMAT_R8G8_UNORM:
102         case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
103         case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
104         case VK_FORMAT_R16_UNORM:
105             return FormatCompatibilityClass::k16_2_1;
106 
107         case VK_FORMAT_R16G16B16A16_SFLOAT:
108         case VK_FORMAT_R16G16B16A16_UNORM:
109             return FormatCompatibilityClass::k64_8_1;
110 
111         case VK_FORMAT_R8G8B8_UNORM:
112             return FormatCompatibilityClass::k24_3_1;
113 
114         case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
115             return FormatCompatibilityClass::kETC2_RGB_8_16;
116 
117         case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
118             return FormatCompatibilityClass::kBC1_RGB_8_16_1;
119 
120         case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
121             return FormatCompatibilityClass::kBC1_RGBA_8_16;
122 
123         default:
124             SK_ABORT("Unsupported VkFormat");
125     }
126 }
127 
canCopyImage(VkFormat dstFormat,int dstSampleCnt,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcHasYcbcr) const128 bool GrVkCaps::canCopyImage(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
129                             VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
130     if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
131         return false;
132     }
133 
134     if (dstHasYcbcr || srcHasYcbcr) {
135         return false;
136     }
137 
138     // We require that all Vulkan GrSurfaces have been created with transfer_dst and transfer_src
139     // as image usage flags.
140     return format_compatibility_class(srcFormat) == format_compatibility_class(dstFormat);
141 }
142 
canCopyAsBlit(VkFormat dstFormat,int dstSampleCnt,bool dstIsLinear,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcIsLinear,bool srcHasYcbcr) const143 bool GrVkCaps::canCopyAsBlit(VkFormat dstFormat, int dstSampleCnt, bool dstIsLinear,
144                              bool dstHasYcbcr, VkFormat srcFormat, int srcSampleCnt,
145                              bool srcIsLinear, bool srcHasYcbcr) const {
146     // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
147     // as image usage flags.
148     if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
149         !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
150         return false;
151     }
152 
153     // We cannot blit images that are multisampled. Will need to figure out if we can blit the
154     // resolved msaa though.
155     if (dstSampleCnt > 1 || srcSampleCnt > 1) {
156         return false;
157     }
158 
159     if (dstHasYcbcr || srcHasYcbcr) {
160         return false;
161     }
162 
163     return true;
164 }
165 
canCopyAsResolve(VkFormat dstFormat,int dstSampleCnt,bool dstHasYcbcr,VkFormat srcFormat,int srcSampleCnt,bool srcHasYcbcr) const166 bool GrVkCaps::canCopyAsResolve(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
167                                 VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
168     // The src surface must be multisampled.
169     if (srcSampleCnt <= 1) {
170         return false;
171     }
172 
173     // The dst must not be multisampled.
174     if (dstSampleCnt > 1) {
175         return false;
176     }
177 
178     // Surfaces must have the same format.
179     if (srcFormat != dstFormat) {
180         return false;
181     }
182 
183     if (dstHasYcbcr || srcHasYcbcr) {
184         return false;
185     }
186 
187     return true;
188 }
189 
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint) const190 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
191                                 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
192     if (src->isProtected() == GrProtected::kYes && dst->isProtected() != GrProtected::kYes) {
193         return false;
194     }
195 
196     // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
197     // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
198     // tracking of external or rectangle textures in GL? For now we don't create linear textures
199     // internally, and I don't believe anyone is wrapping them.
200     bool srcIsLinear = false;
201     bool dstIsLinear = false;
202 
203     int dstSampleCnt = 0;
204     int srcSampleCnt = 0;
205     if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
206         // Copying to or from render targets that wrap a secondary command buffer is not allowed
207         // since they would require us to know the VkImage, which we don't have, as well as need us
208         // to stop and start the VkRenderPass which we don't have access to.
209         if (rtProxy->wrapsVkSecondaryCB()) {
210             return false;
211         }
212         dstSampleCnt = rtProxy->numSamples();
213     }
214     if (const GrRenderTargetProxy* rtProxy = src->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         srcSampleCnt = rtProxy->numSamples();
222     }
223     SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
224     SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
225 
226     bool dstHasYcbcr = false;
227     if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
228         if (ycbcr->isValid()) {
229             dstHasYcbcr = true;
230         }
231     }
232 
233     bool srcHasYcbcr = false;
234     if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
235         if (ycbcr->isValid()) {
236             srcHasYcbcr = true;
237         }
238     }
239 
240     VkFormat dstFormat, srcFormat;
241     SkAssertResult(dst->backendFormat().asVkFormat(&dstFormat));
242     SkAssertResult(src->backendFormat().asVkFormat(&srcFormat));
243 
244     return this->canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
245                               srcFormat, srcSampleCnt, srcHasYcbcr) ||
246            this->canCopyAsBlit(dstFormat, dstSampleCnt, dstIsLinear, dstHasYcbcr,
247                                srcFormat, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
248            this->canCopyAsResolve(dstFormat, dstSampleCnt, dstHasYcbcr,
249                                   srcFormat, srcSampleCnt, srcHasYcbcr);
250 }
251 
get_extension_feature_struct(const VkPhysicalDeviceFeatures2 & features,VkStructureType type)252 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
253                                                      VkStructureType type) {
254     // All Vulkan structs that could be part of the features chain will start with the
255     // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
256     // so we can get access to the pNext for the next struct.
257     struct CommonVulkanHeader {
258         VkStructureType sType;
259         void*           pNext;
260     };
261 
262     void* pNext = features.pNext;
263     while (pNext) {
264         CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
265         if (header->sType == type) {
266             return static_cast<T*>(pNext);
267         }
268         pNext = header->pNext;
269     }
270     return nullptr;
271 }
272 
init(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions,GrProtected isProtected)273 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
274                     VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
275                     uint32_t physicalDeviceVersion, const GrVkExtensions& extensions,
276                     GrProtected isProtected) {
277     VkPhysicalDeviceProperties properties;
278     GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
279 
280     VkPhysicalDeviceMemoryProperties memoryProperties;
281     GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
282 
283     SkASSERT(physicalDeviceVersion <= properties.apiVersion);
284 
285     if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
286         fSupportsSwapchain = true;
287     }
288 
289     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
290         extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
291         fSupportsPhysicalDeviceProperties2 = true;
292     }
293 
294     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
295         extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
296         fSupportsMemoryRequirements2 = true;
297     }
298 
299     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
300         extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
301         fSupportsBindMemory2 = true;
302     }
303 
304     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
305         extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
306         fSupportsMaintenance1 = true;
307     }
308 
309     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
310         extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
311         fSupportsMaintenance2 = true;
312     }
313 
314     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
315         extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
316         fSupportsMaintenance3 = true;
317     }
318 
319     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
320         (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
321          this->supportsMemoryRequirements2())) {
322         fSupportsDedicatedAllocation = true;
323     }
324 
325     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
326         (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
327          this->supportsPhysicalDeviceProperties2() &&
328          extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
329          this->supportsDedicatedAllocation())) {
330         fSupportsExternalMemory = true;
331     }
332 
333 #ifdef SK_BUILD_FOR_ANDROID
334     // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
335     // do we don't explicitly require it here even the spec says it is required.
336     if (extensions.hasExtension(
337             VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
338        /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
339         this->supportsExternalMemory() &&
340         this->supportsBindMemory2()) {
341         fSupportsAndroidHWBExternalMemory = true;
342         fSupportsAHardwareBufferImages = true;
343     }
344 #endif
345 
346     auto ycbcrFeatures =
347             get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
348                     features, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
349     if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
350         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
351          (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
352           this->supportsMaintenance1() && this->supportsBindMemory2() &&
353           this->supportsMemoryRequirements2() && this->supportsPhysicalDeviceProperties2()))) {
354         fSupportsYcbcrConversion = true;
355     }
356 
357     // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
358     // will return a key of 0.
359     fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
360 
361     if ((isProtected == GrProtected::kYes) &&
362         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0))) {
363         fSupportsProtectedMemory = true;
364         fAvoidUpdateBuffers = true;
365         fShouldAlwaysUseDedicatedImageMemory = true;
366     }
367 
368     this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
369     this->initShaderCaps(properties, features);
370 
371     if (kQualcomm_VkVendor == properties.vendorID) {
372         // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
373         // scissored clear.
374         // On NVIDIA and Intel, the discard load followed by clear is faster.
375         // TODO: Evaluate on ARM, Imagination, and ATI.
376         fPreferFullscreenClears = true;
377     }
378 
379     if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
380         // On Qualcomm and ARM mapping a gpu buffer and doing both reads and writes to it is slow.
381         // Thus for index and vertex buffers we will force to use a cpu side buffer and then copy
382         // the whole buffer up to the gpu.
383         fBufferMapThreshold = SK_MaxS32;
384     }
385 
386     if (kQualcomm_VkVendor == properties.vendorID) {
387         // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
388         // even for small sizes.
389         fAvoidUpdateBuffers = true;
390     }
391 
392     if (kARM_VkVendor == properties.vendorID) {
393         // ARM seems to do better with more fine triangles as opposed to using the sample mask.
394         // (At least in our current round rect op.)
395         fPreferTrianglesOverSampleMask = true;
396     }
397 
398     this->initFormatTable(vkInterface, physDev, properties);
399     this->initStencilFormat(vkInterface, physDev);
400 
401     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
402         this->applyDriverCorrectnessWorkarounds(properties);
403     }
404 
405     this->finishInitialization(contextOptions);
406 }
407 
applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties & properties)408 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
409     if (kQualcomm_VkVendor == properties.vendorID) {
410         fMustDoCopiesFromOrigin = true;
411         // Transfer doesn't support this workaround.
412         fTransferFromSurfaceToBufferSupport = false;
413     }
414 
415 #if defined(SK_BUILD_FOR_WIN)
416     if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
417         fMustSleepOnTearDown = true;
418     }
419 #elif defined(SK_BUILD_FOR_ANDROID)
420     if (kImagination_VkVendor == properties.vendorID) {
421         fMustSleepOnTearDown = true;
422     }
423 #endif
424 
425 #if defined(SK_BUILD_FOR_ANDROID)
426     // Protected memory features have problems in Android P and earlier.
427     if (fSupportsProtectedMemory && (kQualcomm_VkVendor == properties.vendorID)) {
428         char androidAPIVersion[PROP_VALUE_MAX];
429         int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersion);
430         if (strLength == 0 || atoi(androidAPIVersion) <= 28) {
431             fSupportsProtectedMemory = false;
432         }
433     }
434 #endif
435 
436     // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
437     if (kARM_VkVendor == properties.vendorID) {
438         fShouldAlwaysUseDedicatedImageMemory = true;
439     }
440 
441     // On Mali galaxy s7 and s9 we see lots of rendering issues with image filters dropping out when
442     // using only primary command buffers.
443     if (kARM_VkVendor == properties.vendorID) {
444         fPreferPrimaryOverSecondaryCommandBuffers = false;
445     }
446 
447     // On various devices, when calling vkCmdClearAttachments on a primary command buffer, it
448     // corrupts the bound buffers on the command buffer. As a workaround we invalidate our knowledge
449     // of bound buffers so that we will rebind them on the next draw.
450     if (kQualcomm_VkVendor == properties.vendorID || kAMD_VkVendor == properties.vendorID) {
451         fMustInvalidatePrimaryCmdBufferStateAfterClearAttachments = true;
452     }
453 
454     ////////////////////////////////////////////////////////////////////////////
455     // GrCaps workarounds
456     ////////////////////////////////////////////////////////////////////////////
457 
458     // The GTX660 bot experiences crashes and incorrect rendering with MSAA CCPR. Block this path
459     // renderer on non-mixed-sampled NVIDIA.
460     // NOTE: We may lose mixed samples support later if the context options suppress dual source
461     // blending, but that shouldn't be an issue because MSAA CCPR seems to work fine (even without
462     // mixed samples) on later NVIDIA hardware where mixed samples would be supported.
463     if ((kNvidia_VkVendor == properties.vendorID) && !fMixedSamplesSupport) {
464         fDriverBlacklistMSAACCPR = true;
465     }
466 
467 #ifdef SK_BUILD_FOR_ANDROID
468     // MSAA CCPR is slow on Android. http://skbug.com/9676
469     fDriverBlacklistMSAACCPR = true;
470 #endif
471 
472     if (kARM_VkVendor == properties.vendorID) {
473         fInstanceAttribSupport = false;
474         fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
475     }
476 
477     // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
478     if (kAMD_VkVendor == properties.vendorID) {
479         fMaxVertexAttributes = std::min(fMaxVertexAttributes, 32);
480     }
481 
482     ////////////////////////////////////////////////////////////////////////////
483     // GrShaderCaps workarounds
484     ////////////////////////////////////////////////////////////////////////////
485 
486     if (kImagination_VkVendor == properties.vendorID) {
487         fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
488     }
489 
490     if (kQualcomm_VkVendor == properties.vendorID) {
491         // The sample mask round rect op draws nothing on Adreno for the srcmode gm.
492         // http://skbug.com/8921
493         fShaderCaps->fCanOnlyUseSampleMaskWithStencil = true;
494     }
495 }
496 
initGrCaps(const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceMemoryProperties & memoryProperties,const VkPhysicalDeviceFeatures2 & features,const GrVkExtensions & extensions)497 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
498                           VkPhysicalDevice physDev,
499                           const VkPhysicalDeviceProperties& properties,
500                           const VkPhysicalDeviceMemoryProperties& memoryProperties,
501                           const VkPhysicalDeviceFeatures2& features,
502                           const GrVkExtensions& extensions) {
503     // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
504     // need for us ever to support that amount, and it makes tests which tests all the vertex
505     // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
506     // we ever find that need.
507     static const uint32_t kMaxVertexAttributes = 64;
508     fMaxVertexAttributes = std::min(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
509 
510     if (properties.limits.standardSampleLocations) {
511         fSampleLocationsSupport = true;
512     }
513 
514     if (extensions.hasExtension(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, 1)) {
515         // We "disable" multisample by colocating all samples at pixel center.
516         fMultisampleDisableSupport = true;
517     }
518 
519     if (extensions.hasExtension(VK_NV_FRAMEBUFFER_MIXED_SAMPLES_EXTENSION_NAME, 1)) {
520         fMixedSamplesSupport = true;
521     }
522 
523     if (extensions.hasExtension(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, 1)) {
524         fConservativeRasterSupport = true;
525     }
526 
527     fWireframeSupport = true;
528 
529     // We could actually query and get a max size for each config, however maxImageDimension2D will
530     // give the minimum max size across all configs. So for simplicity we will use that for now.
531     fMaxRenderTargetSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
532     fMaxTextureSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
533     if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
534         fMaxTextureSize = std::min(fMaxTextureSize, 4096);
535     }
536     // Our render targets are always created with textures as the color
537     // attachment, hence this min:
538     fMaxRenderTargetSize = std::min(fMaxTextureSize, fMaxRenderTargetSize);
539 
540     // TODO: check if RT's larger than 4k incur a performance cost on ARM.
541     fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
542 
543     // Assuming since we will always map in the end to upload the data we might as well just map
544     // from the get go. There is no hard data to suggest this is faster or slower.
545     fBufferMapThreshold = 0;
546 
547     fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
548 
549     fOversizedStencilSupport = true;
550 
551     if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
552         this->supportsPhysicalDeviceProperties2()) {
553 
554         VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
555         blendProps.sType =
556                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
557         blendProps.pNext = nullptr;
558 
559         VkPhysicalDeviceProperties2 props;
560         props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
561         props.pNext = &blendProps;
562 
563         GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
564 
565         if (blendProps.advancedBlendAllOperations == VK_TRUE) {
566             fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
567 
568             auto blendFeatures =
569                 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
570                     features,
571                     VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
572             if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
573                 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
574             } else {
575                 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
576                 // require us to support self dependencies in our render passes.
577                 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
578             }
579         }
580     }
581 
582     if (kARM_VkVendor == properties.vendorID) {
583         fShouldCollapseSrcOverToSrcWhenAble = true;
584     }
585 }
586 
initShaderCaps(const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceFeatures2 & features)587 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
588                               const VkPhysicalDeviceFeatures2& features) {
589     GrShaderCaps* shaderCaps = fShaderCaps.get();
590     shaderCaps->fVersionDeclString = "#version 330\n";
591 
592     // Vulkan is based off ES 3.0 so the following should all be supported
593     shaderCaps->fUsesPrecisionModifiers = true;
594     shaderCaps->fFlatInterpolationSupport = true;
595     // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
596     // to be true with Vulkan as well.
597     shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
598 
599     shaderCaps->fSampleMaskSupport = true;
600 
601     shaderCaps->fShaderDerivativeSupport = true;
602 
603     // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
604     // shaderCaps->fGeometryShaderSupport =
605     //         shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
606 
607     shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
608 
609     shaderCaps->fIntegerSupport = true;
610     shaderCaps->fVertexIDSupport = true;
611     shaderCaps->fFPManipulationSupport = true;
612 
613     // Assume the minimum precisions mandated by the SPIR-V spec.
614     shaderCaps->fFloatIs32Bits = true;
615     shaderCaps->fHalfIs32Bits = false;
616 
617     shaderCaps->fMaxFragmentSamplers = std::min(
618                                        std::min(properties.limits.maxPerStageDescriptorSampledImages,
619                                               properties.limits.maxPerStageDescriptorSamplers),
620                                               (uint32_t)INT_MAX);
621 }
622 
stencil_format_supported(const GrVkInterface * interface,VkPhysicalDevice physDev,VkFormat format)623 bool stencil_format_supported(const GrVkInterface* interface,
624                               VkPhysicalDevice physDev,
625                               VkFormat format) {
626     VkFormatProperties props;
627     memset(&props, 0, sizeof(VkFormatProperties));
628     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
629     return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
630 }
631 
initStencilFormat(const GrVkInterface * interface,VkPhysicalDevice physDev)632 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
633     // List of legal stencil formats (though perhaps not supported on
634     // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
635     // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
636     // can optionally have 24 unused bits at the end so we assume the total bits is 64.
637     static const StencilFormat
638                   // internal Format             stencil bits      total bits        packed?
639         gS8    = { VK_FORMAT_S8_UINT,            8,                 8,               false },
640         gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT,  8,                32,               true },
641         gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8,                64,               true };
642 
643     if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
644         fPreferredStencilFormat = gS8;
645     } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
646         fPreferredStencilFormat = gD24S8;
647     } else {
648         SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
649         fPreferredStencilFormat = gD32S8;
650     }
651 }
652 
format_is_srgb(VkFormat format)653 static bool format_is_srgb(VkFormat format) {
654     SkASSERT(GrVkFormatIsSupported(format));
655 
656     switch (format) {
657         case VK_FORMAT_R8G8B8A8_SRGB:
658             return true;
659         default:
660             return false;
661     }
662 }
663 
664 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
665 // frequently used to least to improve look up times in arrays.
666 static constexpr VkFormat kVkFormats[] = {
667     VK_FORMAT_R8G8B8A8_UNORM,
668     VK_FORMAT_R8_UNORM,
669     VK_FORMAT_B8G8R8A8_UNORM,
670     VK_FORMAT_R5G6B5_UNORM_PACK16,
671     VK_FORMAT_R16G16B16A16_SFLOAT,
672     VK_FORMAT_R16_SFLOAT,
673     VK_FORMAT_R8G8B8_UNORM,
674     VK_FORMAT_R8G8_UNORM,
675     VK_FORMAT_A2B10G10R10_UNORM_PACK32,
676     VK_FORMAT_B4G4R4A4_UNORM_PACK16,
677     VK_FORMAT_R4G4B4A4_UNORM_PACK16,
678     VK_FORMAT_R8G8B8A8_SRGB,
679     VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
680     VK_FORMAT_BC1_RGB_UNORM_BLOCK,
681     VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
682     VK_FORMAT_R16_UNORM,
683     VK_FORMAT_R16G16_UNORM,
684     VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
685     VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
686     VK_FORMAT_R16G16B16A16_UNORM,
687     VK_FORMAT_R16G16_SFLOAT,
688 };
689 
setColorType(GrColorType colorType,std::initializer_list<VkFormat> formats)690 void GrVkCaps::setColorType(GrColorType colorType, std::initializer_list<VkFormat> formats) {
691 #ifdef SK_DEBUG
692     for (size_t i = 0; i < kNumVkFormats; ++i) {
693         const auto& formatInfo = fFormatTable[i];
694         for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
695             const auto& ctInfo = formatInfo.fColorTypeInfos[j];
696             if (ctInfo.fColorType == colorType &&
697                 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
698                 bool found = false;
699                 for (auto it = formats.begin(); it != formats.end(); ++it) {
700                     if (kVkFormats[i] == *it) {
701                         found = true;
702                     }
703                 }
704                 SkASSERT(found);
705             }
706         }
707     }
708 #endif
709     int idx = static_cast<int>(colorType);
710     for (auto it = formats.begin(); it != formats.end(); ++it) {
711         const auto& info = this->getFormatInfo(*it);
712         for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
713             if (info.fColorTypeInfos[i].fColorType == colorType) {
714                 fColorTypeToFormatTable[idx] = *it;
715                 return;
716             }
717         }
718     }
719 }
720 
getFormatInfo(VkFormat format) const721 const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
722     GrVkCaps* nonConstThis = const_cast<GrVkCaps*>(this);
723     return nonConstThis->getFormatInfo(format);
724 }
725 
getFormatInfo(VkFormat format)726 GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) {
727     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
728                   "Size of VkFormats array must match static value in header");
729     for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
730         if (kVkFormats[i] == format) {
731             return fFormatTable[i];
732         }
733     }
734     static FormatInfo kInvalidFormat;
735     return kInvalidFormat;
736 }
737 
initFormatTable(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties)738 void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
739                                const VkPhysicalDeviceProperties& properties) {
740     static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
741                   "Size of VkFormats array must match static value in header");
742 
743     std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, VK_FORMAT_UNDEFINED);
744 
745     // Go through all the formats and init their support surface and data GrColorTypes.
746     // Format: VK_FORMAT_R8G8B8A8_UNORM
747     {
748         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
749         auto& info = this->getFormatInfo(format);
750         info.init(interface, physDev, properties, format);
751         info.fBytesPerPixel = 4;
752         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
753             info.fColorTypeInfoCount = 2;
754             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
755             int ctIdx = 0;
756             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
757             {
758                 constexpr GrColorType ct = GrColorType::kRGBA_8888;
759                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
760                 ctInfo.fColorType = ct;
761                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
762             }
763             // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
764             {
765                 constexpr GrColorType ct = GrColorType::kRGB_888x;
766                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
767                 ctInfo.fColorType = ct;
768                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
769                 ctInfo.fReadSwizzle = GrSwizzle::RGB1();
770             }
771         }
772     }
773 
774     // Format: VK_FORMAT_R8_UNORM
775     {
776         constexpr VkFormat format = VK_FORMAT_R8_UNORM;
777         auto& info = this->getFormatInfo(format);
778         info.init(interface, physDev, properties, format);
779         info.fBytesPerPixel = 1;
780         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
781             info.fColorTypeInfoCount = 2;
782             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
783             int ctIdx = 0;
784             // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
785             {
786                 constexpr GrColorType ct = GrColorType::kAlpha_8;
787                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
788                 ctInfo.fColorType = ct;
789                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
790                 ctInfo.fReadSwizzle = GrSwizzle::RRRR();
791                 ctInfo.fOutputSwizzle = GrSwizzle::AAAA();
792             }
793             // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
794             {
795                 constexpr GrColorType ct = GrColorType::kGray_8;
796                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
797                 ctInfo.fColorType = ct;
798                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
799                 ctInfo.fReadSwizzle = GrSwizzle("rrr1");
800             }
801         }
802     }
803     // Format: VK_FORMAT_B8G8R8A8_UNORM
804     {
805         constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
806         auto& info = this->getFormatInfo(format);
807         info.init(interface, physDev, properties, format);
808         info.fBytesPerPixel = 4;
809         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
810             info.fColorTypeInfoCount = 1;
811             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
812             int ctIdx = 0;
813             // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
814             {
815                 constexpr GrColorType ct = GrColorType::kBGRA_8888;
816                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
817                 ctInfo.fColorType = ct;
818                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
819             }
820         }
821     }
822     // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
823     {
824         constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
825         auto& info = this->getFormatInfo(format);
826         info.init(interface, physDev, properties, format);
827         info.fBytesPerPixel = 2;
828         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
829             info.fColorTypeInfoCount = 1;
830             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
831             int ctIdx = 0;
832             // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kBGR_565
833             {
834                 constexpr GrColorType ct = GrColorType::kBGR_565;
835                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
836                 ctInfo.fColorType = ct;
837                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
838             }
839         }
840     }
841     // Format: VK_FORMAT_R16G16B16A16_SFLOAT
842     {
843         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
844         auto& info = this->getFormatInfo(format);
845         info.init(interface, physDev, properties, format);
846         info.fBytesPerPixel = 8;
847         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
848             info.fColorTypeInfoCount = 2;
849             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
850             int ctIdx = 0;
851             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16
852             {
853                 constexpr GrColorType ct = GrColorType::kRGBA_F16;
854                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
855                 ctInfo.fColorType = ct;
856                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
857             }
858             // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16_Clamped
859             {
860                 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
861                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
862                 ctInfo.fColorType = ct;
863                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
864             }
865         }
866     }
867     // Format: VK_FORMAT_R16_SFLOAT
868     {
869         constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
870         auto& info = this->getFormatInfo(format);
871         info.init(interface, physDev, properties, format);
872         info.fBytesPerPixel = 2;
873         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
874             info.fColorTypeInfoCount = 1;
875             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
876             int ctIdx = 0;
877             // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
878             {
879                 constexpr GrColorType ct = GrColorType::kAlpha_F16;
880                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
881                 ctInfo.fColorType = ct;
882                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
883                 ctInfo.fReadSwizzle = GrSwizzle::RRRR();
884                 ctInfo.fOutputSwizzle = GrSwizzle::AAAA();
885             }
886         }
887     }
888     // Format: VK_FORMAT_R8G8B8_UNORM
889     {
890         constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
891         auto& info = this->getFormatInfo(format);
892         info.init(interface, physDev, properties, format);
893         info.fBytesPerPixel = 3;
894         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
895             info.fColorTypeInfoCount = 1;
896             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
897             int ctIdx = 0;
898             // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
899             {
900                 constexpr GrColorType ct = GrColorType::kRGB_888x;
901                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
902                 ctInfo.fColorType = ct;
903                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
904             }
905         }
906     }
907     // Format: VK_FORMAT_R8G8_UNORM
908     {
909         constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
910         auto& info = this->getFormatInfo(format);
911         info.init(interface, physDev, properties, format);
912         info.fBytesPerPixel = 2;
913         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
914             info.fColorTypeInfoCount = 1;
915             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
916             int ctIdx = 0;
917             // Format: VK_FORMAT_R8G8_UNORM, Surface: kRG_88
918             {
919                 constexpr GrColorType ct = GrColorType::kRG_88;
920                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
921                 ctInfo.fColorType = ct;
922                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
923             }
924         }
925     }
926     // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
927     {
928         constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
929         auto& info = this->getFormatInfo(format);
930         info.init(interface, physDev, properties, format);
931         info.fBytesPerPixel = 4;
932         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
933             info.fColorTypeInfoCount = 1;
934             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
935             int ctIdx = 0;
936             // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
937             {
938                 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
939                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
940                 ctInfo.fColorType = ct;
941                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
942             }
943         }
944     }
945     // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
946     {
947         constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
948         auto& info = this->getFormatInfo(format);
949         info.init(interface, physDev, properties, format);
950         info.fBytesPerPixel = 2;
951         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
952             info.fColorTypeInfoCount = 1;
953             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
954             int ctIdx = 0;
955             // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kABGR_4444
956             {
957                 constexpr GrColorType ct = GrColorType::kABGR_4444;
958                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
959                 ctInfo.fColorType = ct;
960                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
961                 ctInfo.fReadSwizzle = GrSwizzle::BGRA();
962                 ctInfo.fOutputSwizzle = GrSwizzle::BGRA();
963             }
964         }
965     }
966     // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
967     {
968         constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
969         auto& info = this->getFormatInfo(format);
970         info.init(interface, physDev, properties, format);
971         info.fBytesPerPixel = 2;
972         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
973             info.fColorTypeInfoCount = 1;
974             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
975             int ctIdx = 0;
976             // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kABGR_4444
977             {
978                 constexpr GrColorType ct = GrColorType::kABGR_4444;
979                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
980                 ctInfo.fColorType = ct;
981                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
982             }
983         }
984     }
985     // Format: VK_FORMAT_R8G8B8A8_SRGB
986     {
987         constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
988         auto& info = this->getFormatInfo(format);
989         info.init(interface, physDev, properties, format);
990         info.fBytesPerPixel = 4;
991         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
992             info.fColorTypeInfoCount = 1;
993             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
994             int ctIdx = 0;
995             // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
996             {
997                 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
998                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
999                 ctInfo.fColorType = ct;
1000                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1001             }
1002         }
1003     }
1004     // Format: VK_FORMAT_R16_UNORM
1005     {
1006         constexpr VkFormat format = VK_FORMAT_R16_UNORM;
1007         auto& info = this->getFormatInfo(format);
1008         info.init(interface, physDev, properties, format);
1009         info.fBytesPerPixel = 2;
1010         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1011             info.fColorTypeInfoCount = 1;
1012             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1013             int ctIdx = 0;
1014             // Format: VK_FORMAT_R16_UNORM, Surface: kAlpha_16
1015             {
1016                 constexpr GrColorType ct = GrColorType::kAlpha_16;
1017                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1018                 ctInfo.fColorType = ct;
1019                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1020                 ctInfo.fReadSwizzle = GrSwizzle::RRRR();
1021                 ctInfo.fOutputSwizzle = GrSwizzle::AAAA();
1022             }
1023         }
1024     }
1025     // Format: VK_FORMAT_R16G16_UNORM
1026     {
1027         constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1028         auto& info = this->getFormatInfo(format);
1029         info.init(interface, physDev, properties, format);
1030         info.fBytesPerPixel = 4;
1031         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1032             info.fColorTypeInfoCount = 1;
1033             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1034             int ctIdx = 0;
1035             // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1036             {
1037                 constexpr GrColorType ct = GrColorType::kRG_1616;
1038                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1039                 ctInfo.fColorType = ct;
1040                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1041             }
1042         }
1043     }
1044     // Format: VK_FORMAT_R16G16B16A16_UNORM
1045     {
1046         constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1047         auto& info = this->getFormatInfo(format);
1048         info.init(interface, physDev, properties, format);
1049         info.fBytesPerPixel = 8;
1050         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1051             info.fColorTypeInfoCount = 1;
1052             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1053             int ctIdx = 0;
1054             // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1055             {
1056                 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
1057                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1058                 ctInfo.fColorType = ct;
1059                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1060             }
1061         }
1062     }
1063     // Format: VK_FORMAT_R16G16_SFLOAT
1064     {
1065         constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1066         auto& info = this->getFormatInfo(format);
1067         info.init(interface, physDev, properties, format);
1068         info.fBytesPerPixel = 4;
1069         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1070             info.fColorTypeInfoCount = 1;
1071             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1072             int ctIdx = 0;
1073             // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1074             {
1075                 constexpr GrColorType ct = GrColorType::kRG_F16;
1076                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1077                 ctInfo.fColorType = ct;
1078                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1079             }
1080         }
1081     }
1082     // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1083     {
1084         constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1085         auto& info = this->getFormatInfo(format);
1086         // Currently we are just over estimating this value to be used in gpu size calculations even
1087         // though the actually size is probably less. We should instead treat planar formats similar
1088         // to compressed textures that go through their own special query for calculating size.
1089         info.fBytesPerPixel = 3;
1090         if (fSupportsYcbcrConversion) {
1091             info.init(interface, physDev, properties, format);
1092         }
1093         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1094             info.fColorTypeInfoCount = 1;
1095             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1096             int ctIdx = 0;
1097             // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1098             {
1099                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1100                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1101                 ctInfo.fColorType = ct;
1102                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1103             }
1104         }
1105     }
1106     // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1107     {
1108         constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1109         auto& info = this->getFormatInfo(format);
1110         // Currently we are just over estimating this value to be used in gpu size calculations even
1111         // though the actually size is probably less. We should instead treat planar formats similar
1112         // to compressed textures that go through their own special query for calculating size.
1113         info.fBytesPerPixel = 3;
1114         if (fSupportsYcbcrConversion) {
1115             info.init(interface, physDev, properties, format);
1116         }
1117         if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1118             info.fColorTypeInfoCount = 1;
1119             info.fColorTypeInfos.reset(new ColorTypeInfo[info.fColorTypeInfoCount]());
1120             int ctIdx = 0;
1121             // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1122             {
1123                 constexpr GrColorType ct = GrColorType::kRGB_888x;
1124                 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1125                 ctInfo.fColorType = ct;
1126                 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1127             }
1128         }
1129     }
1130     // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1131     {
1132         constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1133         auto& info = this->getFormatInfo(format);
1134         info.init(interface, physDev, properties, format);
1135         info.fBytesPerPixel = 0;
1136         // No supported GrColorTypes.
1137     }
1138 
1139     // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK
1140     {
1141         constexpr VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
1142         auto& info = this->getFormatInfo(format);
1143         info.init(interface, physDev, properties, format);
1144         info.fBytesPerPixel = 0;
1145         // No supported GrColorTypes.
1146     }
1147 
1148     // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK
1149     {
1150         constexpr VkFormat format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
1151         auto& info = this->getFormatInfo(format);
1152         info.init(interface, physDev, properties, format);
1153         info.fBytesPerPixel = 0;
1154         // No supported GrColorTypes.
1155     }
1156 
1157     ////////////////////////////////////////////////////////////////////////////
1158     // Map GrColorTypes (used for creating GrSurfaces) to VkFormats. The order in which the formats
1159     // are passed into the setColorType function indicates the priority in selecting which format
1160     // we use for a given GrcolorType.
1161 
1162     this->setColorType(GrColorType::kAlpha_8,          { VK_FORMAT_R8_UNORM });
1163     this->setColorType(GrColorType::kBGR_565,          { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1164     this->setColorType(GrColorType::kABGR_4444,        { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1165                                                          VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1166     this->setColorType(GrColorType::kRGBA_8888,        { VK_FORMAT_R8G8B8A8_UNORM });
1167     this->setColorType(GrColorType::kRGBA_8888_SRGB,   { VK_FORMAT_R8G8B8A8_SRGB });
1168     this->setColorType(GrColorType::kRGB_888x,         { VK_FORMAT_R8G8B8_UNORM,
1169                                                          VK_FORMAT_R8G8B8A8_UNORM });
1170     this->setColorType(GrColorType::kRG_88,            { VK_FORMAT_R8G8_UNORM });
1171     this->setColorType(GrColorType::kBGRA_8888,        { VK_FORMAT_B8G8R8A8_UNORM });
1172     this->setColorType(GrColorType::kRGBA_1010102,     { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1173     this->setColorType(GrColorType::kGray_8,           { VK_FORMAT_R8_UNORM });
1174     this->setColorType(GrColorType::kAlpha_F16,        { VK_FORMAT_R16_SFLOAT });
1175     this->setColorType(GrColorType::kRGBA_F16,         { VK_FORMAT_R16G16B16A16_SFLOAT });
1176     this->setColorType(GrColorType::kRGBA_F16_Clamped, { VK_FORMAT_R16G16B16A16_SFLOAT });
1177     this->setColorType(GrColorType::kAlpha_16,         { VK_FORMAT_R16_UNORM });
1178     this->setColorType(GrColorType::kRG_1616,          { VK_FORMAT_R16G16_UNORM });
1179     this->setColorType(GrColorType::kRGBA_16161616,    { VK_FORMAT_R16G16B16A16_UNORM });
1180     this->setColorType(GrColorType::kRG_F16,           { VK_FORMAT_R16G16_SFLOAT });
1181 }
1182 
InitFormatFlags(VkFormatFeatureFlags vkFlags,uint16_t * flags)1183 void GrVkCaps::FormatInfo::InitFormatFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
1184     if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
1185         SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
1186         *flags = *flags | kTexturable_Flag;
1187 
1188         // Ganesh assumes that all renderable surfaces are also texturable
1189         if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
1190             *flags = *flags | kRenderable_Flag;
1191         }
1192     }
1193 
1194     if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
1195         *flags = *flags | kBlitSrc_Flag;
1196     }
1197 
1198     if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
1199         *flags = *flags | kBlitDst_Flag;
1200     }
1201 }
1202 
initSampleCounts(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & physProps,VkFormat format)1203 void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
1204                                             VkPhysicalDevice physDev,
1205                                             const VkPhysicalDeviceProperties& physProps,
1206                                             VkFormat format) {
1207     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1208                               VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1209                               VK_IMAGE_USAGE_SAMPLED_BIT |
1210                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1211     VkImageFormatProperties properties;
1212     GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
1213                                                                  format,
1214                                                                  VK_IMAGE_TYPE_2D,
1215                                                                  VK_IMAGE_TILING_OPTIMAL,
1216                                                                  usage,
1217                                                                  0,  // createFlags
1218                                                                  &properties));
1219     VkSampleCountFlags flags = properties.sampleCounts;
1220     if (flags & VK_SAMPLE_COUNT_1_BIT) {
1221         fColorSampleCounts.push_back(1);
1222     }
1223     if (kImagination_VkVendor == physProps.vendorID) {
1224         // MSAA does not work on imagination
1225         return;
1226     }
1227     if (kIntel_VkVendor == physProps.vendorID) {
1228         // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
1229         return;
1230     }
1231     if (flags & VK_SAMPLE_COUNT_2_BIT) {
1232         fColorSampleCounts.push_back(2);
1233     }
1234     if (flags & VK_SAMPLE_COUNT_4_BIT) {
1235         fColorSampleCounts.push_back(4);
1236     }
1237     if (flags & VK_SAMPLE_COUNT_8_BIT) {
1238         fColorSampleCounts.push_back(8);
1239     }
1240     if (flags & VK_SAMPLE_COUNT_16_BIT) {
1241         fColorSampleCounts.push_back(16);
1242     }
1243     // Standard sample locations are not defined for more than 16 samples, and we don't need more
1244     // than 16. Omit 32 and 64.
1245 }
1246 
init(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,VkFormat format)1247 void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
1248                                 VkPhysicalDevice physDev,
1249                                 const VkPhysicalDeviceProperties& properties,
1250                                 VkFormat format) {
1251     VkFormatProperties props;
1252     memset(&props, 0, sizeof(VkFormatProperties));
1253     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
1254     InitFormatFlags(props.linearTilingFeatures, &fLinearFlags);
1255     InitFormatFlags(props.optimalTilingFeatures, &fOptimalFlags);
1256     if (fOptimalFlags & kRenderable_Flag) {
1257         this->initSampleCounts(interface, physDev, properties, format);
1258     }
1259 }
1260 
1261 // For many checks in caps, we need to know whether the GrBackendFormat is external or not. If it is
1262 // external the VkFormat will be VK_NULL_HANDLE which is not handled by our various format
1263 // capability checks.
backend_format_is_external(const GrBackendFormat & format)1264 static bool backend_format_is_external(const GrBackendFormat& format) {
1265     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1266     SkASSERT(ycbcrInfo);
1267 
1268     // All external formats have a valid ycbcrInfo used for sampling and a non zero external format.
1269     if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1270 #ifdef SK_DEBUG
1271         VkFormat vkFormat;
1272         SkAssertResult(format.asVkFormat(&vkFormat));
1273         SkASSERT(vkFormat == VK_NULL_HANDLE);
1274 #endif
1275         return true;
1276     }
1277     return false;
1278 }
1279 
isFormatSRGB(const GrBackendFormat & format) const1280 bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
1281     VkFormat vkFormat;
1282     if (!format.asVkFormat(&vkFormat)) {
1283         return false;
1284     }
1285     if (backend_format_is_external(format)) {
1286         return false;
1287     }
1288 
1289     return format_is_srgb(vkFormat);
1290 }
1291 
compressionType(const GrBackendFormat & format) const1292 SkImage::CompressionType GrVkCaps::compressionType(const GrBackendFormat& format) const {
1293     VkFormat vkFormat;
1294     if (!format.asVkFormat(&vkFormat)) {
1295         return SkImage::CompressionType::kNone;
1296     }
1297 
1298     switch (vkFormat) {
1299         case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: return SkImage::CompressionType::kETC2_RGB8_UNORM;
1300         case VK_FORMAT_BC1_RGB_UNORM_BLOCK:     return SkImage::CompressionType::kBC1_RGB8_UNORM;
1301         case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:    return SkImage::CompressionType::kBC1_RGBA8_UNORM;
1302         default:                                return SkImage::CompressionType::kNone;
1303     }
1304 
1305     SkUNREACHABLE;
1306 }
1307 
isFormatTexturableAndUploadable(GrColorType ct,const GrBackendFormat & format) const1308 bool GrVkCaps::isFormatTexturableAndUploadable(GrColorType ct,
1309                                                const GrBackendFormat& format) const {
1310     VkFormat vkFormat;
1311     if (!format.asVkFormat(&vkFormat)) {
1312         return false;
1313     }
1314 
1315     uint32_t ctFlags = this->getFormatInfo(vkFormat).colorTypeFlags(ct);
1316     return this->isVkFormatTexturable(vkFormat) &&
1317            SkToBool(ctFlags & ColorTypeInfo::kUploadData_Flag);
1318 }
1319 
isFormatTexturable(const GrBackendFormat & format) const1320 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format) const {
1321     VkFormat vkFormat;
1322     if (!format.asVkFormat(&vkFormat)) {
1323         return false;
1324     }
1325     if (backend_format_is_external(format)) {
1326         // We can always texture from an external format (assuming we have the ycbcr conversion
1327         // info which we require to be passed in).
1328         return true;
1329     }
1330     return this->isVkFormatTexturable(vkFormat);
1331 }
1332 
isVkFormatTexturable(VkFormat format) const1333 bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
1334     const FormatInfo& info = this->getFormatInfo(format);
1335     return SkToBool(FormatInfo::kTexturable_Flag & info.fOptimalFlags);
1336 }
1337 
isFormatAsColorTypeRenderable(GrColorType ct,const GrBackendFormat & format,int sampleCount) const1338 bool GrVkCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
1339                                              int sampleCount) const {
1340     if (!this->isFormatRenderable(format, sampleCount)) {
1341         return false;
1342     }
1343     VkFormat vkFormat;
1344     if (!format.asVkFormat(&vkFormat)) {
1345         return false;
1346     }
1347     const auto& info = this->getFormatInfo(vkFormat);
1348     if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
1349         return false;
1350     }
1351     return true;
1352 }
1353 
isFormatRenderable(const GrBackendFormat & format,int sampleCount) const1354 bool GrVkCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
1355     VkFormat vkFormat;
1356     if (!format.asVkFormat(&vkFormat)) {
1357         return false;
1358     }
1359     return this->isFormatRenderable(vkFormat, sampleCount);
1360 }
1361 
isFormatRenderable(VkFormat format,int sampleCount) const1362 bool GrVkCaps::isFormatRenderable(VkFormat format, int sampleCount) const {
1363     return sampleCount <= this->maxRenderTargetSampleCount(format);
1364 }
1365 
getRenderTargetSampleCount(int requestedCount,const GrBackendFormat & format) const1366 int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
1367                                          const GrBackendFormat& format) const {
1368     VkFormat vkFormat;
1369     if (!format.asVkFormat(&vkFormat)) {
1370         return 0;
1371     }
1372 
1373     return this->getRenderTargetSampleCount(requestedCount, vkFormat);
1374 }
1375 
getRenderTargetSampleCount(int requestedCount,VkFormat format) const1376 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
1377     requestedCount = std::max(1, requestedCount);
1378 
1379     const FormatInfo& info = this->getFormatInfo(format);
1380 
1381     int count = info.fColorSampleCounts.count();
1382 
1383     if (!count) {
1384         return 0;
1385     }
1386 
1387     if (1 == requestedCount) {
1388         SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
1389         return 1;
1390     }
1391 
1392     for (int i = 0; i < count; ++i) {
1393         if (info.fColorSampleCounts[i] >= requestedCount) {
1394             return info.fColorSampleCounts[i];
1395         }
1396     }
1397     return 0;
1398 }
1399 
maxRenderTargetSampleCount(const GrBackendFormat & format) const1400 int GrVkCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
1401     VkFormat vkFormat;
1402     if (!format.asVkFormat(&vkFormat)) {
1403         return 0;
1404     }
1405     return this->maxRenderTargetSampleCount(vkFormat);
1406 }
1407 
maxRenderTargetSampleCount(VkFormat format) const1408 int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
1409     const FormatInfo& info = this->getFormatInfo(format);
1410 
1411     const auto& table = info.fColorSampleCounts;
1412     if (!table.count()) {
1413         return 0;
1414     }
1415     return table[table.count() - 1];
1416 }
1417 
bytesPerPixel(const GrBackendFormat & format) const1418 size_t GrVkCaps::bytesPerPixel(const GrBackendFormat& format) const {
1419     VkFormat vkFormat;
1420     if (!format.asVkFormat(&vkFormat)) {
1421         return 0;
1422     }
1423     return this->bytesPerPixel(vkFormat);
1424 }
1425 
bytesPerPixel(VkFormat format) const1426 size_t GrVkCaps::bytesPerPixel(VkFormat format) const {
1427     return this->getFormatInfo(format).fBytesPerPixel;
1428 }
1429 
align_to_4(size_t v)1430 static inline size_t align_to_4(size_t v) {
1431     switch (v & 0b11) {
1432         // v is already a multiple of 4.
1433         case 0:     return v;
1434         // v is a multiple of 2 but not 4.
1435         case 2:     return 2 * v;
1436         // v is not a multiple of 2.
1437         default:    return 4 * v;
1438     }
1439 }
1440 
supportedWritePixelsColorType(GrColorType surfaceColorType,const GrBackendFormat & surfaceFormat,GrColorType srcColorType) const1441 GrCaps::SupportedWrite GrVkCaps::supportedWritePixelsColorType(GrColorType surfaceColorType,
1442                                                                const GrBackendFormat& surfaceFormat,
1443                                                                GrColorType srcColorType) const {
1444     VkFormat vkFormat;
1445     if (!surfaceFormat.asVkFormat(&vkFormat)) {
1446         return {GrColorType::kUnknown, 0};
1447     }
1448 
1449     // We don't support the ability to upload to external formats or formats that require a ycbcr
1450     // sampler. In general these types of formats are only used for sampling in a shader.
1451     if (backend_format_is_external(surfaceFormat) || GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1452         return {GrColorType::kUnknown, 0};
1453     }
1454 
1455     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1456     size_t offsetAlignment = align_to_4(this->bytesPerPixel(vkFormat));
1457 
1458     const auto& info = this->getFormatInfo(vkFormat);
1459     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1460         const auto& ctInfo = info.fColorTypeInfos[i];
1461         if (ctInfo.fColorType == surfaceColorType) {
1462             return {surfaceColorType, offsetAlignment};
1463         }
1464     }
1465     return {GrColorType::kUnknown, 0};
1466 }
1467 
surfaceSupportsReadPixels(const GrSurface * surface) const1468 GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
1469         const GrSurface* surface) const {
1470     if (surface->isProtected()) {
1471         return SurfaceReadPixelsSupport::kUnsupported;
1472     }
1473     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1474         // We can't directly read from a VkImage that has a ycbcr sampler.
1475         if (tex->ycbcrConversionInfo().isValid()) {
1476             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1477         }
1478         // We can't directly read from a compressed format
1479         if (GrVkFormatIsCompressed(tex->imageFormat())) {
1480             return SurfaceReadPixelsSupport::kCopyToTexture2D;
1481         }
1482     }
1483     return SurfaceReadPixelsSupport::kSupported;
1484 }
1485 
onSurfaceSupportsWritePixels(const GrSurface * surface) const1486 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1487     if (auto rt = surface->asRenderTarget()) {
1488         return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1489     }
1490     // We can't write to a texture that has a ycbcr sampler.
1491     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1492         // We can't directly read from a VkImage that has a ycbcr sampler.
1493         if (tex->ycbcrConversionInfo().isValid()) {
1494             return false;
1495         }
1496     }
1497     return true;
1498 }
1499 
onAreColorTypeAndFormatCompatible(GrColorType ct,const GrBackendFormat & format) const1500 bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1501                                                  const GrBackendFormat& format) const {
1502     VkFormat vkFormat;
1503     if (!format.asVkFormat(&vkFormat)) {
1504         return false;
1505     }
1506     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1507     SkASSERT(ycbcrInfo);
1508 
1509     if (ycbcrInfo->isValid() && !GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1510         // Format may be undefined for external images, which are required to have YCbCr conversion.
1511         if (VK_FORMAT_UNDEFINED == vkFormat && ycbcrInfo->fExternalFormat != 0) {
1512             return true;
1513         }
1514         return false;
1515     }
1516 
1517     SkImage::CompressionType compression = GrVkFormatToCompressionType(vkFormat);
1518     if (compression != SkImage::CompressionType::kNone) {
1519         return ct == (SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1520                                                              : GrColorType::kRGBA_8888);
1521     }
1522 
1523     const auto& info = this->getFormatInfo(vkFormat);
1524     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1525         if (info.fColorTypeInfos[i].fColorType == ct) {
1526             return true;
1527         }
1528     }
1529     return false;
1530 }
1531 
getYUVAColorTypeFromBackendFormat(const GrBackendFormat & format,bool isAlphaChannel) const1532 GrColorType GrVkCaps::getYUVAColorTypeFromBackendFormat(const GrBackendFormat& format,
1533                                                         bool isAlphaChannel) const {
1534     VkFormat vkFormat;
1535     if (!format.asVkFormat(&vkFormat)) {
1536         return GrColorType::kUnknown;
1537     }
1538 
1539     switch (vkFormat) {
1540         case VK_FORMAT_R8_UNORM:                 return isAlphaChannel ? GrColorType::kAlpha_8
1541                                                                        : GrColorType::kGray_8;
1542         case VK_FORMAT_R8G8B8A8_UNORM:           return GrColorType::kRGBA_8888;
1543         case VK_FORMAT_R8G8B8_UNORM:             return GrColorType::kRGB_888x;
1544         case VK_FORMAT_R8G8_UNORM:               return GrColorType::kRG_88;
1545         case VK_FORMAT_B8G8R8A8_UNORM:           return GrColorType::kBGRA_8888;
1546         case VK_FORMAT_A2B10G10R10_UNORM_PACK32: return GrColorType::kRGBA_1010102;
1547         case VK_FORMAT_R16_UNORM:                return GrColorType::kAlpha_16;
1548         case VK_FORMAT_R16_SFLOAT:               return GrColorType::kAlpha_F16;
1549         case VK_FORMAT_R16G16_UNORM:             return GrColorType::kRG_1616;
1550         case VK_FORMAT_R16G16B16A16_UNORM:       return GrColorType::kRGBA_16161616;
1551         case VK_FORMAT_R16G16_SFLOAT:            return GrColorType::kRG_F16;
1552         default:                                 return GrColorType::kUnknown;
1553     }
1554 
1555     SkUNREACHABLE;
1556 }
1557 
onGetDefaultBackendFormat(GrColorType ct,GrRenderable renderable) const1558 GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct,
1559                                                     GrRenderable renderable) const {
1560     VkFormat format = this->getFormatFromColorType(ct);
1561     if (format == VK_FORMAT_UNDEFINED) {
1562         return GrBackendFormat();
1563     }
1564     return GrBackendFormat::MakeVk(format);
1565 }
1566 
getBackendFormatFromCompressionType(SkImage::CompressionType compressionType) const1567 GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1568         SkImage::CompressionType compressionType) const {
1569     switch (compressionType) {
1570         case SkImage::CompressionType::kNone:
1571             return {};
1572         case SkImage::CompressionType::kETC2_RGB8_UNORM:
1573             if (this->isVkFormatTexturable(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)) {
1574                 return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1575             }
1576             return {};
1577         case SkImage::CompressionType::kBC1_RGB8_UNORM:
1578             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGB_UNORM_BLOCK)) {
1579                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK);
1580             }
1581             return {};
1582         case SkImage::CompressionType::kBC1_RGBA8_UNORM:
1583             if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGBA_UNORM_BLOCK)) {
1584                 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK);
1585             }
1586             return {};
1587     }
1588 
1589     SkUNREACHABLE;
1590 }
1591 
getReadSwizzle(const GrBackendFormat & format,GrColorType colorType) const1592 GrSwizzle GrVkCaps::getReadSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1593     VkFormat vkFormat;
1594     SkAssertResult(format.asVkFormat(&vkFormat));
1595     const auto& info = this->getFormatInfo(vkFormat);
1596     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1597         const auto& ctInfo = info.fColorTypeInfos[i];
1598         if (ctInfo.fColorType == colorType) {
1599             return ctInfo.fReadSwizzle;
1600         }
1601     }
1602     return GrSwizzle::RGBA();
1603 }
1604 
getOutputSwizzle(const GrBackendFormat & format,GrColorType colorType) const1605 GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1606     VkFormat vkFormat;
1607     SkAssertResult(format.asVkFormat(&vkFormat));
1608     const auto& info = this->getFormatInfo(vkFormat);
1609     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1610         const auto& ctInfo = info.fColorTypeInfos[i];
1611         if (ctInfo.fColorType == colorType) {
1612             return ctInfo.fOutputSwizzle;
1613         }
1614     }
1615     return GrSwizzle::RGBA();
1616 }
1617 
computeFormatKey(const GrBackendFormat & format) const1618 uint64_t GrVkCaps::computeFormatKey(const GrBackendFormat& format) const {
1619     VkFormat vkFormat;
1620     SkAssertResult(format.asVkFormat(&vkFormat));
1621 
1622 #ifdef SK_DEBUG
1623     // We should never be trying to compute a key for an external format
1624     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1625     SkASSERT(ycbcrInfo);
1626     SkASSERT(!ycbcrInfo->isValid() || ycbcrInfo->fExternalFormat == 0);
1627 #endif
1628 
1629     // A VkFormat has a size of 64 bits.
1630     return (uint64_t)vkFormat;
1631 }
1632 
onSupportedReadPixelsColorType(GrColorType srcColorType,const GrBackendFormat & srcBackendFormat,GrColorType dstColorType) const1633 GrCaps::SupportedRead GrVkCaps::onSupportedReadPixelsColorType(
1634         GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1635         GrColorType dstColorType) const {
1636     VkFormat vkFormat;
1637     if (!srcBackendFormat.asVkFormat(&vkFormat)) {
1638         return {GrColorType::kUnknown, 0};
1639     }
1640 
1641     if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1642         return {GrColorType::kUnknown, 0};
1643     }
1644 
1645     SkImage::CompressionType compression = GrVkFormatToCompressionType(vkFormat);
1646     if (compression != SkImage::CompressionType::kNone) {
1647         return { SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1648                                                         : GrColorType::kRGBA_8888, 0 };
1649     }
1650 
1651     // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1652     size_t offsetAlignment = align_to_4(this->bytesPerPixel(vkFormat));
1653 
1654     const auto& info = this->getFormatInfo(vkFormat);
1655     for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1656         const auto& ctInfo = info.fColorTypeInfos[i];
1657         if (ctInfo.fColorType == srcColorType) {
1658             return {srcColorType, offsetAlignment};
1659         }
1660     }
1661     return {GrColorType::kUnknown, 0};
1662 }
1663 
getFragmentUniformBinding() const1664 int GrVkCaps::getFragmentUniformBinding() const {
1665     return GrVkUniformHandler::kUniformBinding;
1666 }
1667 
getFragmentUniformSet() const1668 int GrVkCaps::getFragmentUniformSet() const {
1669     return GrVkUniformHandler::kUniformBufferDescSet;
1670 }
1671 
addExtraSamplerKey(GrProcessorKeyBuilder * b,GrSamplerState samplerState,const GrBackendFormat & format) const1672 void GrVkCaps::addExtraSamplerKey(GrProcessorKeyBuilder* b,
1673                                   GrSamplerState samplerState,
1674                                   const GrBackendFormat& format) const {
1675     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1676     if (!ycbcrInfo) {
1677         return;
1678     }
1679 
1680     GrVkSampler::Key key = GrVkSampler::GenerateKey(samplerState, *ycbcrInfo);
1681 
1682     size_t numInts = (sizeof(key) + 3) / 4;
1683 
1684     uint32_t* tmp = b->add32n(numInts);
1685 
1686     tmp[numInts - 1] = 0;
1687     memcpy(tmp, &key, sizeof(key));
1688 }
1689 
1690 /**
1691  * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
1692  * the information needed to differentiate one pipeline from another.
1693  *
1694  * The GrProgramDesc contains all the information need to create the actual shaders for the
1695  * pipeline.
1696  *
1697  * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
1698  * pipline. This includes stencil settings, blending information, render pass format, draw face
1699  * information, and primitive type. Note that some state is set dynamically on the pipeline for
1700  * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
1701  * and blend constant.
1702  */
makeDesc(const GrRenderTarget * rt,const GrProgramInfo & programInfo) const1703 GrProgramDesc GrVkCaps::makeDesc(const GrRenderTarget* rt, const GrProgramInfo& programInfo) const {
1704     GrProgramDesc desc;
1705     if (!GrProgramDesc::Build(&desc, rt, programInfo, *this)) {
1706         SkASSERT(!desc.isValid());
1707         return desc;
1708     }
1709 
1710     GrProcessorKeyBuilder b(&desc.key());
1711 
1712     // This will become part of the sheared off key used to persistently cache
1713     // the SPIRV code. It needs to be added right after the base key so that,
1714     // when the base-key is sheared off, the shearing code can include it in the
1715     // reduced key (c.f. the +4s in the SkData::MakeWithCopy calls in
1716     // GrVkPipelineStateBuilder.cpp).
1717     b.add32(GrVkGpu::kShader_PersistentCacheKeyType);
1718 
1719     GrVkRenderTarget* vkRT = (GrVkRenderTarget*) rt;
1720     // TODO: support failure in getSimpleRenderPass
1721     SkASSERT(vkRT->getSimpleRenderPass());
1722     vkRT->getSimpleRenderPass()->genKey(&b);
1723 
1724     GrStencilSettings stencil = programInfo.nonGLStencilSettings();
1725     stencil.genKey(&b);
1726 
1727     programInfo.pipeline().genKey(&b, *this);
1728     b.add32(programInfo.numRasterSamples());
1729 
1730     // Vulkan requires the full primitive type as part of its key
1731     b.add32(programInfo.primitiveTypeKey());
1732 
1733     if (this->mixedSamplesSupport()) {
1734         // Add "0" to indicate that coverage modulation will not be enabled, or the (non-zero)
1735         // raster sample count if it will.
1736         b.add32(!programInfo.isMixedSampled() ? 0 : programInfo.numRasterSamples());
1737     }
1738 
1739     return desc;
1740 }
1741 
1742 #if GR_TEST_UTILS
getTestingCombinations() const1743 std::vector<GrCaps::TestFormatColorTypeCombination> GrVkCaps::getTestingCombinations() const {
1744     std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
1745         { GrColorType::kAlpha_8,          GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1746         { GrColorType::kBGR_565,          GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16)  },
1747         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_R4G4B4A4_UNORM_PACK16)},
1748         { GrColorType::kABGR_4444,        GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16)},
1749         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1750         { GrColorType::kRGBA_8888_SRGB,   GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB)        },
1751         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM)       },
1752         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM)         },
1753         { GrColorType::kRG_88,            GrBackendFormat::MakeVk(VK_FORMAT_R8G8_UNORM)           },
1754         { GrColorType::kBGRA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM)       },
1755         { GrColorType::kRGBA_1010102,     GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32)},
1756         { GrColorType::kGray_8,           GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM)             },
1757         { GrColorType::kAlpha_F16,        GrBackendFormat::MakeVk(VK_FORMAT_R16_SFLOAT)           },
1758         { GrColorType::kRGBA_F16,         GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1759         { GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT)  },
1760         { GrColorType::kAlpha_16,         GrBackendFormat::MakeVk(VK_FORMAT_R16_UNORM)            },
1761         { GrColorType::kRG_1616,          GrBackendFormat::MakeVk(VK_FORMAT_R16G16_UNORM)         },
1762         { GrColorType::kRGBA_16161616,    GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_UNORM)   },
1763         { GrColorType::kRG_F16,           GrBackendFormat::MakeVk(VK_FORMAT_R16G16_SFLOAT)        },
1764         // These two compressed formats both have an effective colorType of kRGB_888x
1765         { GrColorType::kRGB_888x,       GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)},
1766         { GrColorType::kRGB_888x,         GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK)  },
1767         { GrColorType::kRGBA_8888,        GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK) },
1768     };
1769 
1770     return combos;
1771 }
1772 #endif
1773