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 "GrVkCaps.h"
9 #include "GrBackendSurface.h"
10 #include "GrRenderTargetProxy.h"
11 #include "GrRenderTarget.h"
12 #include "GrShaderCaps.h"
13 #include "GrVkInterface.h"
14 #include "GrVkTexture.h"
15 #include "GrVkUtil.h"
16 #include "SkGr.h"
17 #include "vk/GrVkBackendContext.h"
18 #include "vk/GrVkExtensions.h"
19 
GrVkCaps(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t instanceVersion,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions)20 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
21                    VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
22                    uint32_t instanceVersion, uint32_t physicalDeviceVersion,
23                    const GrVkExtensions& extensions)
24     : INHERITED(contextOptions) {
25 
26     /**************************************************************************
27      * GrCaps fields
28      **************************************************************************/
29     fMipMapSupport = true;   // always available in Vulkan
30     fSRGBSupport = true;   // always available in Vulkan
31     fNPOTTextureTileSupport = true;  // always available in Vulkan
32     fDiscardRenderTargetSupport = true;
33     fReuseScratchTextures = true; //TODO: figure this out
34     fGpuTracingSupport = false; //TODO: figure this out
35     fCompressedTexSubImageSupport = true;
36     fOversizedStencilSupport = false; //TODO: figure this out
37     fInstanceAttribSupport = true;
38 
39     fFenceSyncSupport = true;   // always available in Vulkan
40     fCrossContextTextureSupport = true;
41     fHalfFloatVertexAttributeSupport = true;
42 
43     fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
44     fBufferMapThreshold = SK_MaxS32;  //TODO: figure this out
45 
46     fMaxRenderTargetSize = 4096; // minimum required by spec
47     fMaxTextureSize = 4096; // minimum required by spec
48 
49     fDynamicStateArrayGeometryProcessorTextureSupport = true;
50 
51     fShaderCaps.reset(new GrShaderCaps(contextOptions));
52 
53     this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions);
54 }
55 
initDescForDstCopy(const GrRenderTargetProxy * src,GrSurfaceDesc * desc,GrSurfaceOrigin * origin,bool * rectsMustMatch,bool * disallowSubrect) const56 bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
57                                   GrSurfaceOrigin* origin, bool* rectsMustMatch,
58                                   bool* disallowSubrect) const {
59     // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
60     *rectsMustMatch = false;
61     *disallowSubrect = false;
62 
63     // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
64     // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
65     // render target as well.
66     *origin = src->origin();
67     desc->fConfig = src->config();
68     if (src->numColorSamples() > 1 || src->asTextureProxy()) {
69         desc->fFlags = kRenderTarget_GrSurfaceFlag;
70     } else {
71         // Just going to use CopyImage here
72         desc->fFlags = kNone_GrSurfaceFlags;
73     }
74 
75     return true;
76 }
77 
canCopyImage(GrPixelConfig dstConfig,int dstSampleCnt,GrSurfaceOrigin dstOrigin,bool dstHasYcbcr,GrPixelConfig srcConfig,int srcSampleCnt,GrSurfaceOrigin srcOrigin,bool srcHasYcbcr) const78 bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, GrSurfaceOrigin dstOrigin,
79                             bool dstHasYcbcr, GrPixelConfig srcConfig, int srcSampleCnt,
80                             GrSurfaceOrigin srcOrigin, bool srcHasYcbcr) const {
81     if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
82         return false;
83     }
84 
85     if (dstHasYcbcr || srcHasYcbcr) {
86         return false;
87     }
88 
89     // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
90     // as image usage flags.
91     if (srcOrigin != dstOrigin || GrBytesPerPixel(srcConfig) != GrBytesPerPixel(dstConfig)) {
92         return false;
93     }
94 
95     if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
96         this->shaderCaps()->configOutputSwizzle(dstConfig)) {
97         return false;
98     }
99 
100     return true;
101 }
102 
canCopyAsBlit(GrPixelConfig dstConfig,int dstSampleCnt,bool dstIsLinear,bool dstHasYcbcr,GrPixelConfig srcConfig,int srcSampleCnt,bool srcIsLinear,bool srcHasYcbcr) const103 bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
104                              bool dstHasYcbcr, GrPixelConfig srcConfig, int srcSampleCnt,
105                              bool srcIsLinear, bool srcHasYcbcr) const {
106     // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
107     // as image usage flags.
108     if (!this->configCanBeDstofBlit(dstConfig, dstIsLinear) ||
109         !this->configCanBeSrcofBlit(srcConfig, srcIsLinear)) {
110         return false;
111     }
112 
113     if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
114         this->shaderCaps()->configOutputSwizzle(dstConfig)) {
115         return false;
116     }
117 
118     // We cannot blit images that are multisampled. Will need to figure out if we can blit the
119     // resolved msaa though.
120     if (dstSampleCnt > 1 || srcSampleCnt > 1) {
121         return false;
122     }
123 
124     if (dstHasYcbcr || srcHasYcbcr) {
125         return false;
126     }
127 
128     return true;
129 }
130 
canCopyAsResolve(GrPixelConfig dstConfig,int dstSampleCnt,GrSurfaceOrigin dstOrigin,bool dstHasYcbcr,GrPixelConfig srcConfig,int srcSampleCnt,GrSurfaceOrigin srcOrigin,bool srcHasYcbcr) const131 bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt,
132                                 GrSurfaceOrigin dstOrigin, bool dstHasYcbcr,
133                                 GrPixelConfig srcConfig, int srcSampleCnt,
134                                 GrSurfaceOrigin srcOrigin, bool srcHasYcbcr) const {
135     // The src surface must be multisampled.
136     if (srcSampleCnt <= 1) {
137         return false;
138     }
139 
140     // The dst must not be multisampled.
141     if (dstSampleCnt > 1) {
142         return false;
143     }
144 
145     // Surfaces must have the same format.
146     if (dstConfig != srcConfig) {
147         return false;
148     }
149 
150     // Surfaces must have the same origin.
151     if (srcOrigin != dstOrigin) {
152         return false;
153     }
154 
155     if (dstHasYcbcr || srcHasYcbcr) {
156         return false;
157     }
158 
159     return true;
160 }
161 
canCopyAsDraw(GrPixelConfig dstConfig,bool dstIsRenderable,bool dstHasYcbcr,GrPixelConfig srcConfig,bool srcIsTextureable,bool srcHasYcbcr) const162 bool GrVkCaps::canCopyAsDraw(GrPixelConfig dstConfig, bool dstIsRenderable, bool dstHasYcbcr,
163                              GrPixelConfig srcConfig, bool srcIsTextureable,
164                              bool srcHasYcbcr) const {
165     // TODO: Make copySurfaceAsDraw handle the swizzle
166     if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
167         this->shaderCaps()->configOutputSwizzle(dstConfig)) {
168         return false;
169     }
170 
171     // Make sure the dst is a render target and the src is a texture.
172     if (!dstIsRenderable || !srcIsTextureable) {
173         return false;
174     }
175 
176     if (dstHasYcbcr) {
177         return false;
178     }
179 
180     return true;
181 }
182 
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint) const183 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
184                                 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
185     GrSurfaceOrigin dstOrigin = dst->origin();
186     GrSurfaceOrigin srcOrigin = src->origin();
187 
188     GrPixelConfig dstConfig = dst->config();
189     GrPixelConfig srcConfig = src->config();
190 
191     // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
192     // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
193     // tracking of external or rectangle textures in GL? For now we don't create linear textures
194     // internally, and I don't believe anyone is wrapping them.
195     bool srcIsLinear = false;
196     bool dstIsLinear = false;
197 
198     int dstSampleCnt = 0;
199     int srcSampleCnt = 0;
200     if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
201         // Copying to or from render targets that wrap a secondary command buffer is not allowed
202         // since they would require us to know the VkImage, which we don't have, as well as need us
203         // to stop and start the VkRenderPass which we don't have access to.
204         if (rtProxy->wrapsVkSecondaryCB()) {
205             return false;
206         }
207         dstSampleCnt = rtProxy->numColorSamples();
208     }
209     if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
210         // Copying to or from render targets that wrap a secondary command buffer is not allowed
211         // since they would require us to know the VkImage, which we don't have, as well as need us
212         // to stop and start the VkRenderPass which we don't have access to.
213         if (rtProxy->wrapsVkSecondaryCB()) {
214             return false;
215         }
216         srcSampleCnt = rtProxy->numColorSamples();
217     }
218     SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
219     SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
220 
221     bool dstHasYcbcr = false;
222     if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
223         if (ycbcr->isValid()) {
224             dstHasYcbcr = true;
225         }
226     }
227 
228     bool srcHasYcbcr = false;
229     if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
230         if (ycbcr->isValid()) {
231             srcHasYcbcr = true;
232         }
233     }
234 
235     return this->canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
236                               srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr) ||
237            this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear, dstHasYcbcr,
238                                srcConfig, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
239            this->canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
240                                   srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr) ||
241            this->canCopyAsDraw(dstConfig, dstSampleCnt > 0, dstHasYcbcr,
242                                srcConfig, SkToBool(src->asTextureProxy()), srcHasYcbcr);
243 }
244 
get_extension_feature_struct(const VkPhysicalDeviceFeatures2 & features,VkStructureType type)245 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
246                                                      VkStructureType type) {
247     // All Vulkan structs that could be part of the features chain will start with the
248     // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
249     // so we can get access to the pNext for the next struct.
250     struct CommonVulkanHeader {
251         VkStructureType sType;
252         void*           pNext;
253     };
254 
255     void* pNext = features.pNext;
256     while (pNext) {
257         CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
258         if (header->sType == type) {
259             return static_cast<T*>(pNext);
260         }
261         pNext = header->pNext;
262     }
263     return nullptr;
264 }
265 
init(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions)266 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
267                     VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
268                     uint32_t physicalDeviceVersion, const GrVkExtensions& extensions) {
269     VkPhysicalDeviceProperties properties;
270     GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
271 
272     VkPhysicalDeviceMemoryProperties memoryProperties;
273     GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
274 
275     SkASSERT(physicalDeviceVersion <= properties.apiVersion);
276 
277     if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
278         fSupportsSwapchain = true;
279     }
280 
281     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
282         extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
283         fSupportsPhysicalDeviceProperties2 = true;
284     }
285 
286     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
287         extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
288         fSupportsMemoryRequirements2 = true;
289     }
290 
291     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
292         extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
293         fSupportsBindMemory2 = true;
294     }
295 
296     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
297         extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
298         fSupportsMaintenance1 = true;
299     }
300 
301     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
302         extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
303         fSupportsMaintenance2 = true;
304     }
305 
306     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
307         extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
308         fSupportsMaintenance3 = true;
309     }
310 
311     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
312         (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
313          this->supportsMemoryRequirements2())) {
314         fSupportsDedicatedAllocation = true;
315     }
316 
317     if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
318         (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
319          this->supportsPhysicalDeviceProperties2() &&
320          extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
321          this->supportsDedicatedAllocation())) {
322         fSupportsExternalMemory = true;
323     }
324 
325 #ifdef SK_BUILD_FOR_ANDROID
326     // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
327     // do we don't explicitly require it here even the spec says it is required.
328     if (extensions.hasExtension(
329             VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
330        /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
331         this->supportsExternalMemory() &&
332         this->supportsBindMemory2()) {
333         fSupportsAndroidHWBExternalMemory = true;
334         fSupportsAHardwareBufferImages = true;
335     }
336 #endif
337 
338     auto ycbcrFeatures =
339             get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
340                     features,
341                     VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
342     if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
343         fSupportsAndroidHWBExternalMemory &&
344         (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
345          (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
346           this->supportsMaintenance1() &&
347           this->supportsBindMemory2() &&
348           this->supportsMemoryRequirements2() &&
349           this->supportsPhysicalDeviceProperties2()))) {
350         fSupportsYcbcrConversion = true;
351     }
352     // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
353     // will return a key of 0.
354     fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
355 
356     this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
357     this->initShaderCaps(properties, features);
358 
359     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
360 #if defined(SK_CPU_X86)
361         // We need to do this before initing the config table since it uses fSRGBSupport
362         if (kImagination_VkVendor == properties.vendorID) {
363             fSRGBSupport = false;
364         }
365 #endif
366     }
367 
368     if (kQualcomm_VkVendor == properties.vendorID) {
369         // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
370         // scissored clear.
371         // On NVIDIA and Intel, the discard load followed by clear is faster.
372         // TODO: Evaluate on ARM, Imagination, and ATI.
373         fPreferFullscreenClears = true;
374     }
375 
376     if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
377         // On Qualcomm mapping a gpu buffer and doing both reads and writes to it is slow. Thus for
378         // index and vertex buffers we will force to use a cpu side buffer and then copy the whole
379         // buffer up to the gpu.
380         fBufferMapThreshold = SK_MaxS32;
381     }
382 
383     if (kQualcomm_VkVendor == properties.vendorID) {
384         // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
385         // even for small sizes.
386         fAvoidUpdateBuffers = true;
387     }
388 
389 
390     this->initConfigTable(vkInterface, physDev, properties);
391     this->initStencilFormat(vkInterface, physDev);
392 
393     if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
394         this->applyDriverCorrectnessWorkarounds(properties);
395     }
396 
397     // On nexus player we disable suballocating VkImage memory since we've seen large slow downs on
398     // bot run times.
399     if (kImagination_VkVendor == properties.vendorID) {
400         fShouldAlwaysUseDedicatedImageMemory = true;
401     }
402 
403     this->applyOptionsOverrides(contextOptions);
404     fShaderCaps->applyOptionsOverrides(contextOptions);
405 }
406 
applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties & properties)407 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
408     if (kQualcomm_VkVendor == properties.vendorID) {
409         fMustDoCopiesFromOrigin = true;
410     }
411 
412 #if defined(SK_BUILD_FOR_WIN)
413     if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
414         fMustSleepOnTearDown = true;
415     }
416 #elif defined(SK_BUILD_FOR_ANDROID)
417     if (kImagination_VkVendor == properties.vendorID) {
418         fMustSleepOnTearDown = true;
419     }
420 #endif
421 
422     // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
423     // Current workaround is to use a different secondary command buffer for each new VkPipeline.
424     if (kAMD_VkVendor == properties.vendorID) {
425         fNewCBOnPipelineChange = true;
426     }
427 
428     // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
429     if (kARM_VkVendor == properties.vendorID) {
430         fShouldAlwaysUseDedicatedImageMemory = true;
431     }
432 
433     ////////////////////////////////////////////////////////////////////////////
434     // GrCaps workarounds
435     ////////////////////////////////////////////////////////////////////////////
436 
437     if (kARM_VkVendor == properties.vendorID) {
438         fInstanceAttribSupport = false;
439         fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
440     }
441 
442     // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
443     if (kAMD_VkVendor == properties.vendorID) {
444         fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
445     }
446 
447     ////////////////////////////////////////////////////////////////////////////
448     // GrShaderCaps workarounds
449     ////////////////////////////////////////////////////////////////////////////
450 
451     if (kImagination_VkVendor == properties.vendorID) {
452         fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
453     }
454 }
455 
get_max_sample_count(VkSampleCountFlags flags)456 int get_max_sample_count(VkSampleCountFlags flags) {
457     SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
458     if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
459         return 0;
460     }
461     if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
462         return 2;
463     }
464     if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
465         return 4;
466     }
467     if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
468         return 8;
469     }
470     if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
471         return 16;
472     }
473     if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
474         return 32;
475     }
476     return 64;
477 }
478 
initGrCaps(const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceMemoryProperties & memoryProperties,const VkPhysicalDeviceFeatures2 & features,const GrVkExtensions & extensions)479 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
480                           VkPhysicalDevice physDev,
481                           const VkPhysicalDeviceProperties& properties,
482                           const VkPhysicalDeviceMemoryProperties& memoryProperties,
483                           const VkPhysicalDeviceFeatures2& features,
484                           const GrVkExtensions& extensions) {
485     // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
486     // need for us ever to support that amount, and it makes tests which tests all the vertex
487     // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
488     // we ever find that need.
489     static const uint32_t kMaxVertexAttributes = 64;
490     fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
491 
492     // We could actually query and get a max size for each config, however maxImageDimension2D will
493     // give the minimum max size across all configs. So for simplicity we will use that for now.
494     fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
495     fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
496     if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
497         fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
498     }
499     // Our render targets are always created with textures as the color
500     // attachment, hence this min:
501     fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
502 
503     // TODO: check if RT's larger than 4k incur a performance cost on ARM.
504     fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
505 
506     // Assuming since we will always map in the end to upload the data we might as well just map
507     // from the get go. There is no hard data to suggest this is faster or slower.
508     fBufferMapThreshold = 0;
509 
510     fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
511 
512     fOversizedStencilSupport = true;
513 
514     if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
515         this->supportsPhysicalDeviceProperties2()) {
516 
517         VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
518         blendProps.sType =
519                 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
520         blendProps.pNext = nullptr;
521 
522         VkPhysicalDeviceProperties2 props;
523         props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
524         props.pNext = &blendProps;
525 
526         GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
527 
528         if (blendProps.advancedBlendAllOperations == VK_TRUE) {
529             fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
530 
531             auto blendFeatures =
532                 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
533                     features,
534                     VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
535             if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
536                 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
537             } else {
538                 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
539                 // require us to support self dependencies in our render passes.
540                 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
541             }
542         }
543     }
544 }
545 
initShaderCaps(const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceFeatures2 & features)546 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
547                               const VkPhysicalDeviceFeatures2& features) {
548     GrShaderCaps* shaderCaps = fShaderCaps.get();
549     shaderCaps->fVersionDeclString = "#version 330\n";
550 
551 
552     // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
553     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
554         GrPixelConfig config = static_cast<GrPixelConfig>(i);
555         // Vulkan doesn't support a single channel format stored in alpha.
556         if (GrPixelConfigIsAlphaOnly(config) &&
557             kAlpha_8_as_Alpha_GrPixelConfig != config) {
558             shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
559             shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
560         } else {
561             if (kGray_8_GrPixelConfig == config ||
562                 kGray_8_as_Red_GrPixelConfig == config) {
563                 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
564             } else if (kRGBA_4444_GrPixelConfig == config) {
565                 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
566                 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
567                 // or writing to outputs. Since we're not actually changing the data at all, the
568                 // only extra work is the swizzle in the shader for all operations.
569                 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
570                 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
571             } else if (kRGB_888X_GrPixelConfig == config) {
572                 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGB1();
573             } else {
574                 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
575             }
576         }
577     }
578 
579     // Vulkan is based off ES 3.0 so the following should all be supported
580     shaderCaps->fUsesPrecisionModifiers = true;
581     shaderCaps->fFlatInterpolationSupport = true;
582     // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
583     // to be true with Vulkan as well.
584     shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
585 
586     // GrShaderCaps
587 
588     shaderCaps->fShaderDerivativeSupport = true;
589 
590     // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
591     // shaderCaps->fGeometryShaderSupport =
592     //         shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
593 
594     shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
595 
596     shaderCaps->fIntegerSupport = true;
597     shaderCaps->fVertexIDSupport = true;
598     shaderCaps->fFPManipulationSupport = true;
599 
600     // Assume the minimum precisions mandated by the SPIR-V spec.
601     shaderCaps->fFloatIs32Bits = true;
602     shaderCaps->fHalfIs32Bits = false;
603 
604     // SPIR-V supports unsigned integers.
605     shaderCaps->fUnsignedSupport = true;
606 
607     shaderCaps->fMaxFragmentSamplers = SkTMin(
608                                        SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
609                                               properties.limits.maxPerStageDescriptorSamplers),
610                                               (uint32_t)INT_MAX);
611 }
612 
stencil_format_supported(const GrVkInterface * interface,VkPhysicalDevice physDev,VkFormat format)613 bool stencil_format_supported(const GrVkInterface* interface,
614                               VkPhysicalDevice physDev,
615                               VkFormat format) {
616     VkFormatProperties props;
617     memset(&props, 0, sizeof(VkFormatProperties));
618     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
619     return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
620 }
621 
initStencilFormat(const GrVkInterface * interface,VkPhysicalDevice physDev)622 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
623     // List of legal stencil formats (though perhaps not supported on
624     // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
625     // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
626     // can optionally have 24 unused bits at the end so we assume the total bits is 64.
627     static const StencilFormat
628                   // internal Format             stencil bits      total bits        packed?
629         gS8    = { VK_FORMAT_S8_UINT,            8,                 8,               false },
630         gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT,  8,                32,               true },
631         gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8,                64,               true };
632 
633     if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
634         fPreferredStencilFormat = gS8;
635     } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
636         fPreferredStencilFormat = gD24S8;
637     } else {
638         SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
639         fPreferredStencilFormat = gD32S8;
640     }
641 }
642 
initConfigTable(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties)643 void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
644                                const VkPhysicalDeviceProperties& properties) {
645     for (int i = 0; i < kGrPixelConfigCnt; ++i) {
646         VkFormat format;
647         if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
648             if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
649                 bool disableRendering = false;
650                 if (static_cast<GrPixelConfig>(i) == kRGB_888X_GrPixelConfig) {
651                     // Currently we don't allow RGB_888X to be renderable because we don't have a
652                     // way to handle blends that reference dst alpha when the values in the dst
653                     // alpha channel are uninitialized.
654                     disableRendering = true;
655                 }
656                 fConfigTable[i].init(interface, physDev, properties, format, disableRendering);
657             }
658         }
659     }
660 }
661 
InitConfigFlags(VkFormatFeatureFlags vkFlags,uint16_t * flags,bool disableRendering)662 void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags,
663                                            bool disableRendering) {
664     if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
665         SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
666         *flags = *flags | kTextureable_Flag;
667 
668         // Ganesh assumes that all renderable surfaces are also texturable
669         if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags) & !disableRendering) {
670             *flags = *flags | kRenderable_Flag;
671         }
672     }
673 
674     if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
675         *flags = *flags | kBlitSrc_Flag;
676     }
677 
678     if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
679         *flags = *flags | kBlitDst_Flag;
680     }
681 }
682 
initSampleCounts(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & physProps,VkFormat format)683 void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
684                                             VkPhysicalDevice physDev,
685                                             const VkPhysicalDeviceProperties& physProps,
686                                             VkFormat format) {
687     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
688                               VK_IMAGE_USAGE_TRANSFER_DST_BIT |
689                               VK_IMAGE_USAGE_SAMPLED_BIT |
690                               VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
691     VkImageFormatProperties properties;
692     GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
693                                                                  format,
694                                                                  VK_IMAGE_TYPE_2D,
695                                                                  VK_IMAGE_TILING_OPTIMAL,
696                                                                  usage,
697                                                                  0,  // createFlags
698                                                                  &properties));
699     VkSampleCountFlags flags = properties.sampleCounts;
700     if (flags & VK_SAMPLE_COUNT_1_BIT) {
701         fColorSampleCounts.push_back(1);
702     }
703     if (kImagination_VkVendor == physProps.vendorID) {
704         // MSAA does not work on imagination
705         return;
706     }
707     if (flags & VK_SAMPLE_COUNT_2_BIT) {
708         fColorSampleCounts.push_back(2);
709     }
710     if (flags & VK_SAMPLE_COUNT_4_BIT) {
711         fColorSampleCounts.push_back(4);
712     }
713     if (flags & VK_SAMPLE_COUNT_8_BIT) {
714         fColorSampleCounts.push_back(8);
715     }
716     if (flags & VK_SAMPLE_COUNT_16_BIT) {
717         fColorSampleCounts.push_back(16);
718     }
719     if (flags & VK_SAMPLE_COUNT_32_BIT) {
720         fColorSampleCounts.push_back(32);
721     }
722     if (flags & VK_SAMPLE_COUNT_64_BIT) {
723         fColorSampleCounts.push_back(64);
724     }
725 }
726 
init(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,VkFormat format,bool disableRendering)727 void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
728                                 VkPhysicalDevice physDev,
729                                 const VkPhysicalDeviceProperties& properties,
730                                 VkFormat format,
731                                 bool disableRendering) {
732     VkFormatProperties props;
733     memset(&props, 0, sizeof(VkFormatProperties));
734     GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
735     InitConfigFlags(props.linearTilingFeatures, &fLinearFlags, disableRendering);
736     InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags, disableRendering);
737     if (fOptimalFlags & kRenderable_Flag) {
738         this->initSampleCounts(interface, physDev, properties, format);
739     }
740 }
741 
getRenderTargetSampleCount(int requestedCount,GrPixelConfig config) const742 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
743     requestedCount = SkTMax(1, requestedCount);
744     int count = fConfigTable[config].fColorSampleCounts.count();
745 
746     if (!count) {
747         return 0;
748     }
749 
750     if (1 == requestedCount) {
751         SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
752                  fConfigTable[config].fColorSampleCounts[0] == 1);
753         return 1;
754     }
755 
756     for (int i = 0; i < count; ++i) {
757         if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
758             return fConfigTable[config].fColorSampleCounts[i];
759         }
760     }
761     return 0;
762 }
763 
maxRenderTargetSampleCount(GrPixelConfig config) const764 int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
765     const auto& table = fConfigTable[config].fColorSampleCounts;
766     if (!table.count()) {
767         return 0;
768     }
769     return table[table.count() - 1];
770 }
771 
surfaceSupportsReadPixels(const GrSurface * surface) const772 bool GrVkCaps::surfaceSupportsReadPixels(const GrSurface* surface) const {
773     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
774         // We can't directly read from a VkImage that has a ycbcr sampler.
775         if (tex->ycbcrConversionInfo().isValid()) {
776             return false;
777         }
778     }
779     return true;
780 }
781 
onSurfaceSupportsWritePixels(const GrSurface * surface) const782 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
783     if (auto rt = surface->asRenderTarget()) {
784         return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
785     }
786     // We can't write to a texture that has a ycbcr sampler.
787     if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
788         // We can't directly read from a VkImage that has a ycbcr sampler.
789         if (tex->ycbcrConversionInfo().isValid()) {
790             return false;
791         }
792     }
793     return true;
794 }
795 
validate_image_info(VkFormat format,SkColorType ct,bool hasYcbcrConversion)796 static GrPixelConfig validate_image_info(VkFormat format, SkColorType ct, bool hasYcbcrConversion) {
797     if (format == VK_FORMAT_UNDEFINED) {
798         // If the format is undefined then it is only valid as an external image which requires that
799         // we have a valid VkYcbcrConversion.
800         if (hasYcbcrConversion) {
801             // We don't actually care what the color type or config are since we won't use those
802             // values for external textures. However, for read pixels we will draw to a non ycbcr
803             // texture of this config so we set RGBA here for that.
804             return kRGBA_8888_GrPixelConfig;
805         } else {
806             return kUnknown_GrPixelConfig;
807         }
808     }
809 
810     if (hasYcbcrConversion) {
811         // We only support having a ycbcr conversion for external images.
812         return kUnknown_GrPixelConfig;
813     }
814 
815     switch (ct) {
816         case kUnknown_SkColorType:
817             break;
818         case kAlpha_8_SkColorType:
819             if (VK_FORMAT_R8_UNORM == format) {
820                 return kAlpha_8_as_Red_GrPixelConfig;
821             }
822             break;
823         case kRGB_565_SkColorType:
824             if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
825                 return kRGB_565_GrPixelConfig;
826             }
827             break;
828         case kARGB_4444_SkColorType:
829             if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
830                 return kRGBA_4444_GrPixelConfig;
831             }
832             break;
833         case kRGBA_8888_SkColorType:
834             if (VK_FORMAT_R8G8B8A8_UNORM == format) {
835                 return kRGBA_8888_GrPixelConfig;
836             } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
837                 return kSRGBA_8888_GrPixelConfig;
838             }
839             break;
840         case kRGB_888x_SkColorType:
841             if (VK_FORMAT_R8G8B8_UNORM == format) {
842                 return kRGB_888_GrPixelConfig;
843             }
844             if (VK_FORMAT_R8G8B8A8_UNORM == format) {
845                 return kRGB_888X_GrPixelConfig;
846             }
847             break;
848         case kBGRA_8888_SkColorType:
849             if (VK_FORMAT_B8G8R8A8_UNORM == format) {
850                 return kBGRA_8888_GrPixelConfig;
851             } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
852                 return kSBGRA_8888_GrPixelConfig;
853             }
854             break;
855         case kRGBA_1010102_SkColorType:
856             if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
857                 return kRGBA_1010102_GrPixelConfig;
858             }
859             break;
860         case kRGB_101010x_SkColorType:
861             return kUnknown_GrPixelConfig;
862         case kGray_8_SkColorType:
863             if (VK_FORMAT_R8_UNORM == format) {
864                 return kGray_8_as_Red_GrPixelConfig;
865             }
866             break;
867         case kRGBA_F16Norm_SkColorType:
868             if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
869                 return kRGBA_half_Clamped_GrPixelConfig;
870             }
871             break;
872         case kRGBA_F16_SkColorType:
873             if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
874                 return kRGBA_half_GrPixelConfig;
875             }
876             break;
877         case kRGBA_F32_SkColorType:
878             if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
879                 return kRGBA_float_GrPixelConfig;
880             }
881             break;
882     }
883 
884     return kUnknown_GrPixelConfig;
885 }
886 
validateBackendRenderTarget(const GrBackendRenderTarget & rt,SkColorType ct) const887 GrPixelConfig GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt,
888                                                     SkColorType ct) const {
889     GrVkImageInfo imageInfo;
890     if (!rt.getVkImageInfo(&imageInfo)) {
891         return kUnknown_GrPixelConfig;
892     }
893     return validate_image_info(imageInfo.fFormat, ct, imageInfo.fYcbcrConversionInfo.isValid());
894 }
895 
getConfigFromBackendFormat(const GrBackendFormat & format,SkColorType ct) const896 GrPixelConfig GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format,
897                                                    SkColorType ct) const {
898     const VkFormat* vkFormat = format.getVkFormat();
899     const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
900     if (!vkFormat || !ycbcrInfo) {
901         return kUnknown_GrPixelConfig;
902     }
903     return validate_image_info(*vkFormat, ct, ycbcrInfo->isValid());
904 }
905 
get_yuva_config(VkFormat vkFormat)906 static GrPixelConfig get_yuva_config(VkFormat vkFormat) {
907     switch (vkFormat) {
908         case VK_FORMAT_R8_UNORM:
909             return kAlpha_8_as_Red_GrPixelConfig;
910         case VK_FORMAT_R8G8B8A8_UNORM:
911             return kRGBA_8888_GrPixelConfig;
912         case VK_FORMAT_R8G8B8_UNORM:
913             return kRGB_888_GrPixelConfig;
914         case VK_FORMAT_R8G8_UNORM:
915             return kRG_88_GrPixelConfig;
916         case VK_FORMAT_B8G8R8A8_UNORM:
917             return kBGRA_8888_GrPixelConfig;
918         default:
919             return kUnknown_GrPixelConfig;
920     }
921 }
922 
getYUVAConfigFromBackendFormat(const GrBackendFormat & format) const923 GrPixelConfig GrVkCaps::getYUVAConfigFromBackendFormat(const GrBackendFormat& format) const {
924     const VkFormat* vkFormat = format.getVkFormat();
925     if (!vkFormat) {
926         return kUnknown_GrPixelConfig;
927     }
928     return get_yuva_config(*vkFormat);
929 }
930 
getBackendFormatFromGrColorType(GrColorType ct,GrSRGBEncoded srgbEncoded) const931 GrBackendFormat GrVkCaps::getBackendFormatFromGrColorType(GrColorType ct,
932                                                           GrSRGBEncoded srgbEncoded) const {
933     GrPixelConfig config = GrColorTypeToPixelConfig(ct, srgbEncoded);
934     if (config == kUnknown_GrPixelConfig) {
935         return GrBackendFormat();
936     }
937     VkFormat format;
938     if (!GrPixelConfigToVkFormat(config, &format)) {
939         return GrBackendFormat();
940     }
941     return GrBackendFormat::MakeVk(format);
942 }
943 
944