2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/gpu/ganesh/vk/GrVkCaps.h"
12 #include "include/gpu/GrBackendSurface.h"
13 #include "include/gpu/GrContextOptions.h"
14 #include "include/gpu/vk/GrVkBackendContext.h"
15 #include "include/gpu/vk/GrVkExtensions.h"
16 #include "src/core/SkCompressedDataUtils.h"
17 #include "src/gpu/KeyBuilder.h"
18 #include "src/gpu/ganesh/GrBackendUtils.h"
19 #include "src/gpu/ganesh/GrProgramDesc.h"
20 #include "src/gpu/ganesh/GrRenderTarget.h"
21 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
22 #include "src/gpu/ganesh/GrShaderCaps.h"
23 #include "src/gpu/ganesh/GrStencilSettings.h"
24 #include "src/gpu/ganesh/GrUtil.h"
25 #include "src/gpu/ganesh/SkGr.h"
26 #include "src/gpu/ganesh/vk/GrVkGpu.h"
27 #include "src/gpu/ganesh/vk/GrVkImage.h"
28 #include "src/gpu/ganesh/vk/GrVkInterface.h"
29 #include "src/gpu/ganesh/vk/GrVkRenderTarget.h"
30 #include "src/gpu/ganesh/vk/GrVkTexture.h"
31 #include "src/gpu/ganesh/vk/GrVkUniformHandler.h"
32 #include "src/gpu/ganesh/vk/GrVkUtil.h"
34 #ifdef SK_BUILD_FOR_ANDROID
35 #include <sys/system_properties.h>
38 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
39 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
40 uint32_t instanceVersion, uint32_t physicalDeviceVersion,
41 const GrVkExtensions& extensions, GrProtected isProtected)
42 : INHERITED(contextOptions) {
43 /**************************************************************************
45 **************************************************************************/
46 fMipmapSupport = true; // always available in Vulkan
47 fAnisoSupport = true; // always available in Vulkan
48 fNPOTTextureTileSupport = true; // always available in Vulkan
49 fReuseScratchTextures = true; //TODO: figure this out
50 fGpuTracingSupport = false; //TODO: figure this out
51 fOversizedStencilSupport = false; //TODO: figure this out
52 fDrawInstancedSupport = true;
54 fSemaphoreSupport = true; // always available in Vulkan
55 fFenceSyncSupport = true; // always available in Vulkan
56 fCrossContextTextureSupport = true;
57 fHalfFloatVertexAttributeSupport = true;
59 // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
60 fReadPixelsRowBytesSupport = true;
61 fWritePixelsRowBytesSupport = true;
63 fTransferFromBufferToTextureSupport = true;
64 fTransferFromSurfaceToBufferSupport = true;
66 fMaxRenderTargetSize = 4096; // minimum required by spec
67 fMaxTextureSize = 4096; // minimum required by spec
69 fDynamicStateArrayGeometryProcessorTextureSupport = true;
71 fTextureBarrierSupport = true;
73 fShaderCaps = std::make_unique<GrShaderCaps>();
75 this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions,
81 * This comes from section 37.1.6 of the Vulkan spec. Format is
82 * (<bits>|<tag>)_<block_size>_<texels_per_block>.
84 enum class FormatCompatibilityClass {
94 } // anonymous namespace
96 static FormatCompatibilityClass format_compatibility_class(VkFormat format) {
98 case VK_FORMAT_B8G8R8A8_UNORM:
99 case VK_FORMAT_R8G8B8A8_UNORM:
100 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
101 case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
102 case VK_FORMAT_R8G8B8A8_SRGB:
103 case VK_FORMAT_R16G16_UNORM:
104 case VK_FORMAT_R16G16_SFLOAT:
105 return FormatCompatibilityClass::k32_4_1;
107 case VK_FORMAT_R8_UNORM:
108 return FormatCompatibilityClass::k8_1_1;
110 case VK_FORMAT_R5G6B5_UNORM_PACK16:
111 case VK_FORMAT_R16_SFLOAT:
112 case VK_FORMAT_R8G8_UNORM:
113 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
114 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
115 case VK_FORMAT_R16_UNORM:
116 return FormatCompatibilityClass::k16_2_1;
118 case VK_FORMAT_R16G16B16A16_SFLOAT:
119 case VK_FORMAT_R16G16B16A16_UNORM:
120 return FormatCompatibilityClass::k64_8_1;
122 case VK_FORMAT_R8G8B8_UNORM:
123 return FormatCompatibilityClass::k24_3_1;
125 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
126 return FormatCompatibilityClass::kETC2_RGB_8_16;
128 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
129 return FormatCompatibilityClass::kBC1_RGB_8_16_1;
131 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
132 return FormatCompatibilityClass::kBC1_RGBA_8_16;
135 SK_ABORT("Unsupported VkFormat");
139 bool GrVkCaps::canCopyImage(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
140 VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
141 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
145 if (dstHasYcbcr || srcHasYcbcr) {
149 // We require that all Vulkan GrSurfaces have been created with transfer_dst and transfer_src
150 // as image usage flags.
151 return format_compatibility_class(srcFormat) == format_compatibility_class(dstFormat);
154 bool GrVkCaps::canCopyAsBlit(VkFormat dstFormat, int dstSampleCnt, bool dstIsLinear,
155 bool dstHasYcbcr, VkFormat srcFormat, int srcSampleCnt,
156 bool srcIsLinear, bool srcHasYcbcr) const {
157 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
158 // as image usage flags.
159 if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
160 !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
164 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
165 // resolved msaa though.
166 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
170 if (dstHasYcbcr || srcHasYcbcr) {
177 bool GrVkCaps::canCopyAsResolve(VkFormat dstFormat, int dstSampleCnt, bool dstHasYcbcr,
178 VkFormat srcFormat, int srcSampleCnt, bool srcHasYcbcr) const {
179 // The src surface must be multisampled.
180 if (srcSampleCnt <= 1) {
184 // The dst must not be multisampled.
185 if (dstSampleCnt > 1) {
189 // Surfaces must have the same format.
190 if (srcFormat != dstFormat) {
194 if (dstHasYcbcr || srcHasYcbcr) {
201 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
202 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
203 if (src->isProtected() == GrProtected::kYes && dst->isProtected() != GrProtected::kYes) {
207 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
208 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
209 // tracking of external or rectangle textures in GL? For now we don't create linear textures
210 // internally, and I don't believe anyone is wrapping them.
211 bool srcIsLinear = false;
212 bool dstIsLinear = false;
214 int dstSampleCnt = 0;
215 int srcSampleCnt = 0;
216 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
217 // Copying to or from render targets that wrap a secondary command buffer is not allowed
218 // since they would require us to know the VkImage, which we don't have, as well as need us
219 // to stop and start the VkRenderPass which we don't have access to.
220 if (rtProxy->wrapsVkSecondaryCB()) {
223 if (this->preferDiscardableMSAAAttachment() && dst->asTextureProxy() &&
224 rtProxy->supportsVkInputAttachment()) {
227 dstSampleCnt = rtProxy->numSamples();
230 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
231 // Copying to or from render targets that wrap a secondary command buffer is not allowed
232 // since they would require us to know the VkImage, which we don't have, as well as need us
233 // to stop and start the VkRenderPass which we don't have access to.
234 if (rtProxy->wrapsVkSecondaryCB()) {
237 if (this->preferDiscardableMSAAAttachment() && src->asTextureProxy() &&
238 rtProxy->supportsVkInputAttachment()) {
241 srcSampleCnt = rtProxy->numSamples();
244 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
245 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
247 bool dstHasYcbcr = false;
248 if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
249 if (ycbcr->isValid()) {
254 bool srcHasYcbcr = false;
255 if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
256 if (ycbcr->isValid()) {
261 VkFormat dstFormat, srcFormat;
262 SkAssertResult(dst->backendFormat().asVkFormat(&dstFormat));
263 SkAssertResult(src->backendFormat().asVkFormat(&srcFormat));
265 return this->canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
266 srcFormat, srcSampleCnt, srcHasYcbcr) ||
267 this->canCopyAsBlit(dstFormat, dstSampleCnt, dstIsLinear, dstHasYcbcr,
268 srcFormat, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
269 this->canCopyAsResolve(dstFormat, dstSampleCnt, dstHasYcbcr,
270 srcFormat, srcSampleCnt, srcHasYcbcr);
273 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
274 VkStructureType type) {
275 // All Vulkan structs that could be part of the features chain will start with the
276 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
277 // so we can get access to the pNext for the next struct.
278 struct CommonVulkanHeader {
279 VkStructureType sType;
283 void* pNext = features.pNext;
285 CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
286 if (header->sType == type) {
287 return static_cast<T*>(pNext);
289 pNext = header->pNext;
294 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
295 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
296 uint32_t physicalDeviceVersion, const GrVkExtensions& extensions,
297 GrProtected isProtected) {
298 VkPhysicalDeviceProperties properties;
299 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
301 VkPhysicalDeviceMemoryProperties memoryProperties;
302 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
304 SkASSERT(physicalDeviceVersion <= properties.apiVersion);
306 if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
307 fSupportsSwapchain = true;
310 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
311 extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
312 fSupportsPhysicalDeviceProperties2 = true;
315 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
316 extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
317 fSupportsMemoryRequirements2 = true;
320 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
321 extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
322 fSupportsBindMemory2 = true;
325 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
326 extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
327 fSupportsMaintenance1 = true;
330 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
331 extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
332 fSupportsMaintenance2 = true;
335 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
336 extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
337 fSupportsMaintenance3 = true;
340 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
341 (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
342 this->supportsMemoryRequirements2())) {
343 fSupportsDedicatedAllocation = true;
346 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
347 (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
348 this->supportsPhysicalDeviceProperties2() &&
349 extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
350 this->supportsDedicatedAllocation())) {
351 fSupportsExternalMemory = true;
354 #ifdef SK_BUILD_FOR_ANDROID
355 // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
356 // do we don't explicitly require it here even the spec says it is required.
357 if (extensions.hasExtension(
358 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
359 /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
360 this->supportsExternalMemory() &&
361 this->supportsBindMemory2()) {
362 fSupportsAndroidHWBExternalMemory = true;
363 fSupportsAHardwareBufferImages = true;
368 get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
369 features, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
370 if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
371 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
372 (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
373 this->supportsMaintenance1() && this->supportsBindMemory2() &&
374 this->supportsMemoryRequirements2() && this->supportsPhysicalDeviceProperties2()))) {
375 fSupportsYcbcrConversion = true;
378 // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
379 // will return a key of 0.
380 fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
382 if ((isProtected == GrProtected::kYes) &&
383 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0))) {
384 fSupportsProtectedMemory = true;
385 fAvoidUpdateBuffers = true;
386 fShouldAlwaysUseDedicatedImageMemory = true;
389 if (extensions.hasExtension(VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, 1)) {
390 fSupportsDRMFormatModifiers = true;
393 fMaxInputAttachmentDescriptors = properties.limits.maxDescriptorSetInputAttachments;
395 fMaxSamplerAnisotropy = properties.limits.maxSamplerAnisotropy;
397 // On desktop GPUs we have found that this does not provide much benefit. The perf results show
398 // a mix of regressions, some improvements, and lots of no changes. Thus it is no worth enabling
399 // this (especially with the rendering artifacts) on desktop.
401 // On Adreno devices we were expecting to see perf gains. But instead there were actually a lot
402 // of perf regressions and only a few perf wins. This needs some follow up with qualcomm since
403 // we do expect this to be a big win on tilers.
405 // On ARM devices we are seeing an average perf win of around 50%-60% across the board.
406 if (kARM_VkVendor == properties.vendorID) {
407 fPreferDiscardableMSAAAttachment = true;
408 fSupportsMemorylessAttachments = true;
411 this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
412 this->initShaderCaps(properties, features);
414 if (kQualcomm_VkVendor == properties.vendorID) {
415 // A "clear" load for atlases runs faster on QC than a "discard" load followed by a
417 // On NVIDIA and Intel, the discard load followed by clear is faster.
418 // TODO: Evaluate on ARM, Imagination, and ATI.
419 fPreferFullscreenClears = true;
422 if (properties.vendorID == kNvidia_VkVendor || properties.vendorID == kAMD_VkVendor) {
423 // On discrete GPUs it can be faster to read gpu only memory compared to memory that is also
424 // mappable on the host.
425 fGpuOnlyBuffersMorePerformant = true;
427 // On discrete GPUs we try to use special DEVICE_LOCAL and HOST_VISIBLE memory for our
428 // cpu write, gpu read buffers. This memory is not ideal to be kept persistently mapped.
429 // Some discrete GPUs do not expose this special memory, however we still disable
430 // persistently mapped buffers for all of them since most GPUs with updated drivers do
431 // expose it. If this becomes an issue we can try to be more fine grained.
432 fShouldPersistentlyMapCpuToGpuBuffers = false;
435 if (kQualcomm_VkVendor == properties.vendorID) {
436 // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
437 // even for small sizes.
438 fAvoidUpdateBuffers = true;
441 if (kQualcomm_VkVendor == properties.vendorID) {
442 // Adreno devices don't support push constants well
443 fMaxPushConstantsSize = 0;
446 fNativeDrawIndirectSupport = features.features.drawIndirectFirstInstance;
447 if (properties.vendorID == kQualcomm_VkVendor) {
448 // Indirect draws seem slow on QC. Disable until we can investigate. http://skbug.com/11139
449 fNativeDrawIndirectSupport = false;
452 if (fNativeDrawIndirectSupport) {
453 fMaxDrawIndirectDrawCount = properties.limits.maxDrawIndirectCount;
454 SkASSERT(fMaxDrawIndirectDrawCount == 1 || features.features.multiDrawIndirect);
457 #ifdef SK_BUILD_FOR_UNIX
458 if (kNvidia_VkVendor == properties.vendorID) {
459 // On nvidia linux we see a big perf regression when not using dedicated image allocations.
460 fShouldAlwaysUseDedicatedImageMemory = true;
464 this->initFormatTable(vkInterface, physDev, properties);
465 this->initStencilFormat(vkInterface, physDev);
467 if (contextOptions.fMaxCachedVulkanSecondaryCommandBuffers >= 0) {
468 fMaxPerPoolCachedSecondaryCommandBuffers =
469 contextOptions.fMaxCachedVulkanSecondaryCommandBuffers;
472 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
473 this->applyDriverCorrectnessWorkarounds(properties);
476 this->finishInitialization(contextOptions);
479 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
480 #if defined(SK_BUILD_FOR_WIN)
481 if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
482 fMustSyncCommandBuffersWithQueue = true;
484 #elif defined(SK_BUILD_FOR_ANDROID)
485 if (kImagination_VkVendor == properties.vendorID) {
486 fMustSyncCommandBuffersWithQueue = true;
490 // Defaults to zero since all our workaround checks that use this consider things "fixed" once
491 // above a certain api level. So this will just default to it being less which will enable
493 int androidAPIVersion = 0;
494 #if defined(SK_BUILD_FOR_ANDROID)
495 char androidAPIVersionStr[PROP_VALUE_MAX];
496 int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersionStr);
497 // Defaults to zero since most checks care if it is greater than a specific value. So this will
498 // just default to it being less.
499 androidAPIVersion = (strLength == 0) ? 0 : atoi(androidAPIVersionStr);
502 // Protected memory features have problems in Android P and earlier.
503 if (fSupportsProtectedMemory && (kQualcomm_VkVendor == properties.vendorID)) {
504 if (androidAPIVersion <= 28) {
505 fSupportsProtectedMemory = false;
509 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
510 if (kARM_VkVendor == properties.vendorID && androidAPIVersion <= 28) {
511 fShouldAlwaysUseDedicatedImageMemory = true;
514 // On Mali galaxy s7 and s9 we see lots of rendering issues with image filters dropping out when
515 // using only primary command buffers. We also see issues on the P30 running android 28.
516 if (kARM_VkVendor == properties.vendorID && androidAPIVersion <= 28) {
517 fPreferPrimaryOverSecondaryCommandBuffers = false;
518 // If we are using secondary command buffers our code isn't setup to insert barriers into
519 // the secondary cb so we need to disable support for them.
520 fTextureBarrierSupport = false;
521 fBlendEquationSupport = kBasic_BlendEquationSupport;
524 // We've seen numerous driver bugs on qualcomm devices running on android P (api 28) or earlier
525 // when trying to using discardable msaa attachments and loading from resolve. So we disable the
526 // feature for those devices.
527 if (properties.vendorID == kQualcomm_VkVendor && androidAPIVersion <= 28) {
528 fPreferDiscardableMSAAAttachment = false;
529 fSupportsDiscardableMSAAForDMSAA = false;
532 // On Mali G series GPUs, applying transfer functions in the fragment shader with half-floats
533 // produces answers that are much less accurate than expected/required. This forces full floats
534 // for some intermediate values to get acceptable results.
535 if (kARM_VkVendor == properties.vendorID) {
536 fShaderCaps->fColorSpaceMathNeedsFloat = true;
539 // On various devices, when calling vkCmdClearAttachments on a primary command buffer, it
540 // corrupts the bound buffers on the command buffer. As a workaround we invalidate our knowledge
541 // of bound buffers so that we will rebind them on the next draw.
542 if (kQualcomm_VkVendor == properties.vendorID || kAMD_VkVendor == properties.vendorID) {
543 fMustInvalidatePrimaryCmdBufferStateAfterClearAttachments = true;
546 // On Qualcomm and Arm the gpu resolves an area larger than the render pass bounds when using
547 // discardable msaa attachments. This causes the resolve to resolve uninitialized data from the
548 // msaa image into the resolve image.
549 if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
550 fMustLoadFullImageWithDiscardableMSAA = true;
553 #ifdef SK_BUILD_FOR_UNIX
554 if (kIntel_VkVendor == properties.vendorID) {
555 // At least on our linux Debug Intel HD405 bot we are seeing issues doing read pixels with
556 // non-conherent memory. It seems like the device is not properly honoring the
557 // vkInvalidateMappedMemoryRanges calls correctly. Other linux intel devices seem to work
558 // okay. However, since I'm not sure how to target a specific intel devices or driver
559 // version I am going to stop all intel linux from using non-coherent memory. Currently we
560 // are not shipping anything on these platforms and the only real thing that will regress is
561 // read backs. If we find later we do care about this performance we can come back to figure
562 // out how to do a more narrow workaround.
563 fMustUseCoherentHostVisibleMemory = true;
567 ////////////////////////////////////////////////////////////////////////////
568 // GrCaps workarounds
569 ////////////////////////////////////////////////////////////////////////////
571 if (kARM_VkVendor == properties.vendorID) {
572 fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
575 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
576 if (kAMD_VkVendor == properties.vendorID) {
577 fMaxVertexAttributes = std::min(fMaxVertexAttributes, 32);
580 // Adreno devices fail when trying to read the dest using an input attachment and texture
582 if (kQualcomm_VkVendor == properties.vendorID) {
583 fTextureBarrierSupport = false;
586 // On ARM indirect draws are broken on Android 9 and earlier. This was tested on a P30 and
587 // Mate 20x running android 9.
588 if (properties.vendorID == kARM_VkVendor && androidAPIVersion <= 28) {
589 fNativeDrawIndirectSupport = false;
592 ////////////////////////////////////////////////////////////////////////////
593 // GrShaderCaps workarounds
594 ////////////////////////////////////////////////////////////////////////////
596 if (kImagination_VkVendor == properties.vendorID) {
597 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
601 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
602 VkPhysicalDevice physDev,
603 const VkPhysicalDeviceProperties& properties,
604 const VkPhysicalDeviceMemoryProperties& memoryProperties,
605 const VkPhysicalDeviceFeatures2& features,
606 const GrVkExtensions& extensions) {
607 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
608 // need for us ever to support that amount, and it makes tests which tests all the vertex
609 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
610 // we ever find that need.
611 static const uint32_t kMaxVertexAttributes = 64;
612 fMaxVertexAttributes = std::min(properties.limits.maxVertexInputAttributes,
613 kMaxVertexAttributes);
615 // GrCaps::fSampleLocationsSupport refers to the ability to *query* the sample locations (not
616 // program them). For now we just set this to true if the device uses standard locations, and
617 // return the standard locations back when queried.
618 if (properties.limits.standardSampleLocations) {
619 fSampleLocationsSupport = true;
622 if (extensions.hasExtension(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, 1)) {
623 fConservativeRasterSupport = true;
626 fWireframeSupport = true;
628 // We could actually query and get a max size for each config, however maxImageDimension2D will
629 // give the minimum max size across all configs. So for simplicity we will use that for now.
630 fMaxRenderTargetSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
631 fMaxTextureSize = std::min(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
633 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
634 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
636 fMaxPushConstantsSize = std::min(properties.limits.maxPushConstantsSize, (uint32_t)INT_MAX);
638 // Assuming since we will always map in the end to upload the data we might as well just map
639 // from the get go. There is no hard data to suggest this is faster or slower.
640 fBufferMapThreshold = 0;
642 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
644 fOversizedStencilSupport = true;
646 if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
647 this->supportsPhysicalDeviceProperties2()) {
649 VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
651 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
652 blendProps.pNext = nullptr;
654 VkPhysicalDeviceProperties2 props;
655 props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
656 props.pNext = &blendProps;
658 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
660 if (blendProps.advancedBlendAllOperations == VK_TRUE) {
661 fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
664 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
666 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
667 if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
668 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
670 fBlendEquationSupport = kAdvanced_BlendEquationSupport;
675 if (kARM_VkVendor == properties.vendorID) {
676 fShouldCollapseSrcOverToSrcWhenAble = true;
680 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
681 const VkPhysicalDeviceFeatures2& features) {
682 GrShaderCaps* shaderCaps = fShaderCaps.get();
683 shaderCaps->fVersionDeclString = "#version 330\n";
685 // Vulkan is based off ES 3.0 so the following should all be supported
686 shaderCaps->fUsesPrecisionModifiers = true;
687 shaderCaps->fFlatInterpolationSupport = true;
688 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
689 // to be true with Vulkan as well.
690 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
692 shaderCaps->fSampleMaskSupport = true;
694 shaderCaps->fShaderDerivativeSupport = true;
696 // ARM GPUs calculate `matrix * vector` in SPIR-V at full precision, even when the inputs are
697 // RelaxedPrecision. Rewriting the multiply as a sum of vector*scalar fixes this. (skia:11769)
698 shaderCaps->fRewriteMatrixVectorMultiply = (kARM_VkVendor == properties.vendorID);
700 shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
702 shaderCaps->fIntegerSupport = true;
703 shaderCaps->fNonsquareMatrixSupport = true;
704 shaderCaps->fInverseHyperbolicSupport = true;
705 shaderCaps->fVertexIDSupport = true;
706 shaderCaps->fInfinitySupport = true;
707 shaderCaps->fNonconstantArrayIndexSupport = true;
708 shaderCaps->fBitManipulationSupport = true;
710 // Assume the minimum precisions mandated by the SPIR-V spec.
711 shaderCaps->fFloatIs32Bits = true;
712 shaderCaps->fHalfIs32Bits = false;
714 shaderCaps->fMaxFragmentSamplers = std::min(
715 std::min(properties.limits.maxPerStageDescriptorSampledImages,
716 properties.limits.maxPerStageDescriptorSamplers),
720 bool stencil_format_supported(const GrVkInterface* interface,
721 VkPhysicalDevice physDev,
723 VkFormatProperties props;
724 memset(&props, 0, sizeof(VkFormatProperties));
725 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
726 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
729 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
730 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
731 fPreferredStencilFormat = VK_FORMAT_S8_UINT;
732 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
733 fPreferredStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;
735 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
736 fPreferredStencilFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
740 static bool format_is_srgb(VkFormat format) {
741 SkASSERT(GrVkFormatIsSupported(format));
744 case VK_FORMAT_R8G8B8A8_SRGB:
751 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
752 // frequently used to least to improve look up times in arrays.
753 static constexpr VkFormat kVkFormats[] = {
754 VK_FORMAT_R8G8B8A8_UNORM,
756 VK_FORMAT_B8G8R8A8_UNORM,
757 VK_FORMAT_R5G6B5_UNORM_PACK16,
758 VK_FORMAT_R16G16B16A16_SFLOAT,
759 VK_FORMAT_R16_SFLOAT,
760 VK_FORMAT_R8G8B8_UNORM,
761 VK_FORMAT_R8G8_UNORM,
762 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
763 VK_FORMAT_A2R10G10B10_UNORM_PACK32,
764 VK_FORMAT_B4G4R4A4_UNORM_PACK16,
765 VK_FORMAT_R4G4B4A4_UNORM_PACK16,
766 VK_FORMAT_R8G8B8A8_SRGB,
767 VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
768 VK_FORMAT_BC1_RGB_UNORM_BLOCK,
769 VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
771 VK_FORMAT_R16G16_UNORM,
772 VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
773 VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
774 VK_FORMAT_R16G16B16A16_UNORM,
775 VK_FORMAT_R16G16_SFLOAT,
778 void GrVkCaps::setColorType(GrColorType colorType, std::initializer_list<VkFormat> formats) {
780 for (size_t i = 0; i < kNumVkFormats; ++i) {
781 const auto& formatInfo = fFormatTable[i];
782 for (int j = 0; j < formatInfo.fColorTypeInfoCount; ++j) {
783 const auto& ctInfo = formatInfo.fColorTypeInfos[j];
784 if (ctInfo.fColorType == colorType &&
785 !SkToBool(ctInfo.fFlags & ColorTypeInfo::kWrappedOnly_Flag)) {
787 for (auto it = formats.begin(); it != formats.end(); ++it) {
788 if (kVkFormats[i] == *it) {
797 int idx = static_cast<int>(colorType);
798 for (auto it = formats.begin(); it != formats.end(); ++it) {
799 const auto& info = this->getFormatInfo(*it);
800 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
801 if (info.fColorTypeInfos[i].fColorType == colorType) {
802 fColorTypeToFormatTable[idx] = *it;
809 const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
810 GrVkCaps* nonConstThis = const_cast<GrVkCaps*>(this);
811 return nonConstThis->getFormatInfo(format);
814 GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) {
815 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
816 "Size of VkFormats array must match static value in header");
817 for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
818 if (kVkFormats[i] == format) {
819 return fFormatTable[i];
822 static FormatInfo kInvalidFormat;
823 return kInvalidFormat;
826 void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
827 const VkPhysicalDeviceProperties& properties) {
828 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
829 "Size of VkFormats array must match static value in header");
831 std::fill_n(fColorTypeToFormatTable, kGrColorTypeCnt, VK_FORMAT_UNDEFINED);
833 // Go through all the formats and init their support surface and data GrColorTypes.
834 // Format: VK_FORMAT_R8G8B8A8_UNORM
836 constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
837 auto& info = this->getFormatInfo(format);
838 info.init(interface, physDev, properties, format);
839 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
840 info.fColorTypeInfoCount = 2;
841 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
843 // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
845 constexpr GrColorType ct = GrColorType::kRGBA_8888;
846 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
847 ctInfo.fColorType = ct;
848 ctInfo.fTransferColorType = ct;
849 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
851 // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
853 constexpr GrColorType ct = GrColorType::kRGB_888x;
854 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
855 ctInfo.fColorType = ct;
856 ctInfo.fTransferColorType = ct;
857 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
858 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
863 // Format: VK_FORMAT_R8_UNORM
865 constexpr VkFormat format = VK_FORMAT_R8_UNORM;
866 auto& info = this->getFormatInfo(format);
867 info.init(interface, physDev, properties, format);
868 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
869 info.fColorTypeInfoCount = 3;
870 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
872 // Format: VK_FORMAT_R8_UNORM, Surface: kR_8
874 constexpr GrColorType ct = GrColorType::kR_8;
875 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
876 ctInfo.fColorType = ct;
877 ctInfo.fTransferColorType = ct;
878 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
880 // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
882 constexpr GrColorType ct = GrColorType::kAlpha_8;
883 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
884 ctInfo.fColorType = ct;
885 ctInfo.fTransferColorType = ct;
886 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
887 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
888 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
890 // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
892 constexpr GrColorType ct = GrColorType::kGray_8;
893 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
894 ctInfo.fColorType = ct;
895 ctInfo.fTransferColorType = ct;
896 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
897 ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1");
901 // Format: VK_FORMAT_B8G8R8A8_UNORM
903 constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
904 auto& info = this->getFormatInfo(format);
905 info.init(interface, physDev, properties, format);
906 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
907 info.fColorTypeInfoCount = 1;
908 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
910 // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
912 constexpr GrColorType ct = GrColorType::kBGRA_8888;
913 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
914 ctInfo.fColorType = ct;
915 ctInfo.fTransferColorType = ct;
916 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
920 // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
922 constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
923 auto& info = this->getFormatInfo(format);
924 info.init(interface, physDev, properties, format);
925 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
926 info.fColorTypeInfoCount = 1;
927 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
929 // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kBGR_565
931 constexpr GrColorType ct = GrColorType::kBGR_565;
932 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
933 ctInfo.fColorType = ct;
934 ctInfo.fTransferColorType = ct;
935 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
939 // Format: VK_FORMAT_R16G16B16A16_SFLOAT
941 constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
942 auto& info = this->getFormatInfo(format);
943 info.init(interface, physDev, properties, format);
944 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
945 info.fColorTypeInfoCount = 2;
946 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
948 // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16
950 constexpr GrColorType ct = GrColorType::kRGBA_F16;
951 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
952 ctInfo.fColorType = ct;
953 ctInfo.fTransferColorType = ct;
954 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
956 // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: GrColorType::kRGBA_F16_Clamped
958 constexpr GrColorType ct = GrColorType::kRGBA_F16_Clamped;
959 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
960 ctInfo.fColorType = ct;
961 ctInfo.fTransferColorType = ct;
962 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
966 // Format: VK_FORMAT_R16_SFLOAT
968 constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
969 auto& info = this->getFormatInfo(format);
970 info.init(interface, physDev, properties, format);
971 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
972 info.fColorTypeInfoCount = 1;
973 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
975 // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
977 constexpr GrColorType ct = GrColorType::kAlpha_F16;
978 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
979 ctInfo.fColorType = ct;
980 ctInfo.fTransferColorType = ct;
981 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
982 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
983 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
987 // Format: VK_FORMAT_R8G8B8_UNORM
989 constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
990 auto& info = this->getFormatInfo(format);
991 info.init(interface, physDev, properties, format);
992 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
993 info.fColorTypeInfoCount = 1;
994 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
996 // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
998 constexpr GrColorType ct = GrColorType::kRGB_888x;
999 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1000 ctInfo.fColorType = ct;
1001 // The Vulkan format is 3 bpp so we must convert to/from that when transferring.
1002 ctInfo.fTransferColorType = GrColorType::kRGB_888;
1003 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1007 // Format: VK_FORMAT_R8G8_UNORM
1009 constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
1010 auto& info = this->getFormatInfo(format);
1011 info.init(interface, physDev, properties, format);
1012 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1013 info.fColorTypeInfoCount = 1;
1014 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1016 // Format: VK_FORMAT_R8G8_UNORM, Surface: kRG_88
1018 constexpr GrColorType ct = GrColorType::kRG_88;
1019 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1020 ctInfo.fColorType = ct;
1021 ctInfo.fTransferColorType = ct;
1022 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1026 // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
1028 constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
1029 auto& info = this->getFormatInfo(format);
1030 info.init(interface, physDev, properties, format);
1031 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1032 info.fColorTypeInfoCount = 1;
1033 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1035 // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
1037 constexpr GrColorType ct = GrColorType::kRGBA_1010102;
1038 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1039 ctInfo.fColorType = ct;
1040 ctInfo.fTransferColorType = ct;
1041 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1045 // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32
1047 constexpr VkFormat format = VK_FORMAT_A2R10G10B10_UNORM_PACK32;
1048 auto& info = this->getFormatInfo(format);
1049 info.init(interface, physDev, properties, format);
1050 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1051 info.fColorTypeInfoCount = 1;
1052 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1054 // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32, Surface: kBGRA_1010102
1056 constexpr GrColorType ct = GrColorType::kBGRA_1010102;
1057 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1058 ctInfo.fColorType = ct;
1059 ctInfo.fTransferColorType = ct;
1060 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1064 // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
1066 constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
1067 auto& info = this->getFormatInfo(format);
1068 info.init(interface, physDev, properties, format);
1069 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1070 info.fColorTypeInfoCount = 1;
1071 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1073 // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kABGR_4444
1075 constexpr GrColorType ct = GrColorType::kABGR_4444;
1076 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1077 ctInfo.fColorType = ct;
1078 ctInfo.fTransferColorType = ct;
1079 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1080 ctInfo.fReadSwizzle = skgpu::Swizzle::BGRA();
1081 ctInfo.fWriteSwizzle = skgpu::Swizzle::BGRA();
1086 // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
1088 constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
1089 auto& info = this->getFormatInfo(format);
1090 info.init(interface, physDev, properties, format);
1091 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1092 info.fColorTypeInfoCount = 1;
1093 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1095 // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kABGR_4444
1097 constexpr GrColorType ct = GrColorType::kABGR_4444;
1098 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1099 ctInfo.fColorType = ct;
1100 ctInfo.fTransferColorType = ct;
1101 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1105 // Format: VK_FORMAT_R8G8B8A8_SRGB
1107 constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
1108 auto& info = this->getFormatInfo(format);
1109 info.init(interface, physDev, properties, format);
1110 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1111 info.fColorTypeInfoCount = 1;
1112 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1114 // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
1116 constexpr GrColorType ct = GrColorType::kRGBA_8888_SRGB;
1117 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1118 ctInfo.fColorType = ct;
1119 ctInfo.fTransferColorType = ct;
1120 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1124 // Format: VK_FORMAT_R16_UNORM
1126 constexpr VkFormat format = VK_FORMAT_R16_UNORM;
1127 auto& info = this->getFormatInfo(format);
1128 info.init(interface, physDev, properties, format);
1129 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1130 info.fColorTypeInfoCount = 1;
1131 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1133 // Format: VK_FORMAT_R16_UNORM, Surface: kAlpha_16
1135 constexpr GrColorType ct = GrColorType::kAlpha_16;
1136 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1137 ctInfo.fColorType = ct;
1138 ctInfo.fTransferColorType = ct;
1139 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1140 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
1141 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
1145 // Format: VK_FORMAT_R16G16_UNORM
1147 constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1148 auto& info = this->getFormatInfo(format);
1149 info.init(interface, physDev, properties, format);
1150 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1151 info.fColorTypeInfoCount = 1;
1152 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1154 // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1156 constexpr GrColorType ct = GrColorType::kRG_1616;
1157 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1158 ctInfo.fColorType = ct;
1159 ctInfo.fTransferColorType = ct;
1160 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1164 // Format: VK_FORMAT_R16G16B16A16_UNORM
1166 constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1167 auto& info = this->getFormatInfo(format);
1168 info.init(interface, physDev, properties, format);
1169 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1170 info.fColorTypeInfoCount = 1;
1171 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1173 // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1175 constexpr GrColorType ct = GrColorType::kRGBA_16161616;
1176 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1177 ctInfo.fColorType = ct;
1178 ctInfo.fTransferColorType = ct;
1179 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1183 // Format: VK_FORMAT_R16G16_SFLOAT
1185 constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1186 auto& info = this->getFormatInfo(format);
1187 info.init(interface, physDev, properties, format);
1188 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1189 info.fColorTypeInfoCount = 1;
1190 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1192 // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1194 constexpr GrColorType ct = GrColorType::kRG_F16;
1195 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1196 ctInfo.fColorType = ct;
1197 ctInfo.fTransferColorType = ct;
1198 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1202 // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1204 constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1205 auto& info = this->getFormatInfo(format);
1206 if (fSupportsYcbcrConversion) {
1207 info.init(interface, physDev, properties, format);
1209 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1210 info.fColorTypeInfoCount = 1;
1211 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1213 // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1215 constexpr GrColorType ct = GrColorType::kRGB_888x;
1216 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1217 ctInfo.fColorType = ct;
1218 ctInfo.fTransferColorType = ct;
1219 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1223 // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1225 constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1226 auto& info = this->getFormatInfo(format);
1227 if (fSupportsYcbcrConversion) {
1228 info.init(interface, physDev, properties, format);
1230 if (SkToBool(info.fOptimalFlags & FormatInfo::kTexturable_Flag)) {
1231 info.fColorTypeInfoCount = 1;
1232 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1234 // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1236 constexpr GrColorType ct = GrColorType::kRGB_888x;
1237 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1238 ctInfo.fColorType = ct;
1239 ctInfo.fTransferColorType = ct;
1240 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kWrappedOnly_Flag;
1244 // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1246 constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1247 auto& info = this->getFormatInfo(format);
1248 info.init(interface, physDev, properties, format);
1249 // Setting this to texel block size
1250 // No supported GrColorTypes.
1253 // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK
1255 constexpr VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
1256 auto& info = this->getFormatInfo(format);
1257 info.init(interface, physDev, properties, format);
1258 // Setting this to texel block size
1259 // No supported GrColorTypes.
1262 // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK
1264 constexpr VkFormat format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
1265 auto& info = this->getFormatInfo(format);
1266 info.init(interface, physDev, properties, format);
1267 // Setting this to texel block size
1268 // No supported GrColorTypes.
1271 ////////////////////////////////////////////////////////////////////////////
1272 // Map GrColorTypes (used for creating GrSurfaces) to VkFormats. The order in which the formats
1273 // are passed into the setColorType function indicates the priority in selecting which format
1274 // we use for a given GrcolorType.
1276 this->setColorType(GrColorType::kAlpha_8, { VK_FORMAT_R8_UNORM });
1277 this->setColorType(GrColorType::kBGR_565, { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1278 this->setColorType(GrColorType::kABGR_4444, { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1279 VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1280 this->setColorType(GrColorType::kRGBA_8888, { VK_FORMAT_R8G8B8A8_UNORM });
1281 this->setColorType(GrColorType::kRGBA_8888_SRGB, { VK_FORMAT_R8G8B8A8_SRGB });
1282 this->setColorType(GrColorType::kRGB_888x, { VK_FORMAT_R8G8B8_UNORM,
1283 VK_FORMAT_R8G8B8A8_UNORM });
1284 this->setColorType(GrColorType::kRG_88, { VK_FORMAT_R8G8_UNORM });
1285 this->setColorType(GrColorType::kBGRA_8888, { VK_FORMAT_B8G8R8A8_UNORM });
1286 this->setColorType(GrColorType::kRGBA_1010102, { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1287 this->setColorType(GrColorType::kBGRA_1010102, { VK_FORMAT_A2R10G10B10_UNORM_PACK32 });
1288 this->setColorType(GrColorType::kGray_8, { VK_FORMAT_R8_UNORM });
1289 this->setColorType(GrColorType::kAlpha_F16, { VK_FORMAT_R16_SFLOAT });
1290 this->setColorType(GrColorType::kRGBA_F16, { VK_FORMAT_R16G16B16A16_SFLOAT });
1291 this->setColorType(GrColorType::kRGBA_F16_Clamped, { VK_FORMAT_R16G16B16A16_SFLOAT });
1292 this->setColorType(GrColorType::kAlpha_16, { VK_FORMAT_R16_UNORM });
1293 this->setColorType(GrColorType::kRG_1616, { VK_FORMAT_R16G16_UNORM });
1294 this->setColorType(GrColorType::kRGBA_16161616, { VK_FORMAT_R16G16B16A16_UNORM });
1295 this->setColorType(GrColorType::kRG_F16, { VK_FORMAT_R16G16_SFLOAT });
1298 void GrVkCaps::FormatInfo::InitFormatFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
1299 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
1300 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
1301 *flags = *flags | kTexturable_Flag;
1303 // Ganesh assumes that all renderable surfaces are also texturable
1304 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
1305 *flags = *flags | kRenderable_Flag;
1308 // TODO: For Vk w/ VK_KHR_maintenance1 extension support, check
1309 // VK_FORMAT_FEATURE_TRANSFER_[SRC|DST]_BIT_KHR explicitly to set copy flags
1310 // Can do similar check for VK_KHR_sampler_ycbcr_conversion added bits
1312 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
1313 *flags = *flags | kBlitSrc_Flag;
1316 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
1317 *flags = *flags | kBlitDst_Flag;
1321 void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
1322 VkPhysicalDevice physDev,
1323 const VkPhysicalDeviceProperties& physProps,
1325 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1326 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1327 VK_IMAGE_USAGE_SAMPLED_BIT |
1328 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1329 VkImageFormatProperties properties;
1330 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
1333 VK_IMAGE_TILING_OPTIMAL,
1337 VkSampleCountFlags flags = properties.sampleCounts;
1338 if (flags & VK_SAMPLE_COUNT_1_BIT) {
1339 fColorSampleCounts.push_back(1);
1341 if (kImagination_VkVendor == physProps.vendorID) {
1342 // MSAA does not work on imagination
1345 if (kIntel_VkVendor == physProps.vendorID) {
1346 // MSAA doesn't work well on Intel GPUs chromium:527565, chromium:983926
1349 if (flags & VK_SAMPLE_COUNT_2_BIT) {
1350 fColorSampleCounts.push_back(2);
1352 if (flags & VK_SAMPLE_COUNT_4_BIT) {
1353 fColorSampleCounts.push_back(4);
1355 if (flags & VK_SAMPLE_COUNT_8_BIT) {
1356 fColorSampleCounts.push_back(8);
1358 if (flags & VK_SAMPLE_COUNT_16_BIT) {
1359 fColorSampleCounts.push_back(16);
1361 // Standard sample locations are not defined for more than 16 samples, and we don't need more
1362 // than 16. Omit 32 and 64.
1365 void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
1366 VkPhysicalDevice physDev,
1367 const VkPhysicalDeviceProperties& properties,
1369 VkFormatProperties props;
1370 memset(&props, 0, sizeof(VkFormatProperties));
1371 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
1372 InitFormatFlags(props.linearTilingFeatures, &fLinearFlags);
1373 InitFormatFlags(props.optimalTilingFeatures, &fOptimalFlags);
1374 if (fOptimalFlags & kRenderable_Flag) {
1375 this->initSampleCounts(interface, physDev, properties, format);
1379 // For many checks in caps, we need to know whether the GrBackendFormat is external or not. If it is
1380 // external the VkFormat will be VK_NULL_HANDLE which is not handled by our various format
1381 // capability checks.
1382 static bool backend_format_is_external(const GrBackendFormat& format) {
1383 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1384 SkASSERT(ycbcrInfo);
1386 // All external formats have a valid ycbcrInfo used for sampling and a non zero external format.
1387 if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1390 SkAssertResult(format.asVkFormat(&vkFormat));
1391 SkASSERT(vkFormat == VK_FORMAT_UNDEFINED);
1398 bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
1400 if (!format.asVkFormat(&vkFormat)) {
1403 if (backend_format_is_external(format)) {
1407 return format_is_srgb(vkFormat);
1410 bool GrVkCaps::isFormatTexturable(const GrBackendFormat& format, GrTextureType) const {
1412 if (!format.asVkFormat(&vkFormat)) {
1415 if (backend_format_is_external(format)) {
1416 // We can always texture from an external format (assuming we have the ycbcr conversion
1417 // info which we require to be passed in).
1420 return this->isVkFormatTexturable(vkFormat);
1423 bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
1424 const FormatInfo& info = this->getFormatInfo(format);
1425 return SkToBool(FormatInfo::kTexturable_Flag & info.fOptimalFlags);
1428 bool GrVkCaps::isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
1429 int sampleCount) const {
1430 if (!this->isFormatRenderable(format, sampleCount)) {
1434 if (!format.asVkFormat(&vkFormat)) {
1437 const auto& info = this->getFormatInfo(vkFormat);
1438 if (!SkToBool(info.colorTypeFlags(ct) & ColorTypeInfo::kRenderable_Flag)) {
1444 bool GrVkCaps::isFormatRenderable(const GrBackendFormat& format, int sampleCount) const {
1446 if (!format.asVkFormat(&vkFormat)) {
1449 return this->isFormatRenderable(vkFormat, sampleCount);
1452 bool GrVkCaps::isFormatRenderable(VkFormat format, int sampleCount) const {
1453 return sampleCount <= this->maxRenderTargetSampleCount(format);
1456 int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
1457 const GrBackendFormat& format) const {
1459 if (!format.asVkFormat(&vkFormat)) {
1463 return this->getRenderTargetSampleCount(requestedCount, vkFormat);
1466 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
1467 requestedCount = std::max(1, requestedCount);
1469 const FormatInfo& info = this->getFormatInfo(format);
1471 int count = info.fColorSampleCounts.count();
1477 if (1 == requestedCount) {
1478 SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
1482 for (int i = 0; i < count; ++i) {
1483 if (info.fColorSampleCounts[i] >= requestedCount) {
1484 return info.fColorSampleCounts[i];
1490 int GrVkCaps::maxRenderTargetSampleCount(const GrBackendFormat& format) const {
1492 if (!format.asVkFormat(&vkFormat)) {
1495 return this->maxRenderTargetSampleCount(vkFormat);
1498 int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
1499 const FormatInfo& info = this->getFormatInfo(format);
1501 const auto& table = info.fColorSampleCounts;
1502 if (!table.count()) {
1505 return table[table.count() - 1];
1508 static inline size_t align_to_4(size_t v) {
1510 // v is already a multiple of 4.
1512 // v is a multiple of 2 but not 4.
1513 case 2: return 2 * v;
1514 // v is not a multiple of 2.
1515 default: return 4 * v;
1519 GrCaps::SupportedWrite GrVkCaps::supportedWritePixelsColorType(GrColorType surfaceColorType,
1520 const GrBackendFormat& surfaceFormat,
1521 GrColorType srcColorType) const {
1523 if (!surfaceFormat.asVkFormat(&vkFormat)) {
1524 return {GrColorType::kUnknown, 0};
1527 // We don't support the ability to upload to external formats or formats that require a ycbcr
1528 // sampler. In general these types of formats are only used for sampling in a shader.
1529 if (backend_format_is_external(surfaceFormat) || GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1530 return {GrColorType::kUnknown, 0};
1533 // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1534 size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1536 const auto& info = this->getFormatInfo(vkFormat);
1537 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1538 const auto& ctInfo = info.fColorTypeInfos[i];
1539 if (ctInfo.fColorType == surfaceColorType) {
1540 return {ctInfo.fTransferColorType, offsetAlignment};
1543 return {GrColorType::kUnknown, 0};
1546 GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
1547 const GrSurface* surface) const {
1548 if (surface->isProtected()) {
1549 return SurfaceReadPixelsSupport::kUnsupported;
1551 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1552 auto texImage = tex->textureImage();
1554 return SurfaceReadPixelsSupport::kUnsupported;
1556 // We can't directly read from a VkImage that has a ycbcr sampler.
1557 if (texImage->ycbcrConversionInfo().isValid()) {
1558 return SurfaceReadPixelsSupport::kCopyToTexture2D;
1560 // We can't directly read from a compressed format
1561 if (GrVkFormatIsCompressed(texImage->imageFormat())) {
1562 return SurfaceReadPixelsSupport::kCopyToTexture2D;
1564 return SurfaceReadPixelsSupport::kSupported;
1565 } else if (auto rt = surface->asRenderTarget()) {
1566 if (rt->numSamples() > 1) {
1567 return SurfaceReadPixelsSupport::kCopyToTexture2D;
1569 return SurfaceReadPixelsSupport::kSupported;
1571 return SurfaceReadPixelsSupport::kUnsupported;
1574 GrColorType GrVkCaps::transferColorType(VkFormat vkFormat, GrColorType surfaceColorType) const {
1575 const auto& info = this->getFormatInfo(vkFormat);
1576 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1577 if (info.fColorTypeInfos[i].fColorType == surfaceColorType) {
1578 return info.fColorTypeInfos[i].fTransferColorType;
1581 return GrColorType::kUnknown;
1584 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
1585 if (auto rt = surface->asRenderTarget()) {
1586 return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
1588 // We can't write to a texture that has a ycbcr sampler.
1589 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
1590 auto texImage = tex->textureImage();
1594 // We can't directly read from a VkImage that has a ycbcr sampler.
1595 if (texImage->ycbcrConversionInfo().isValid()) {
1602 bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1603 const GrBackendFormat& format) const {
1605 if (!format.asVkFormat(&vkFormat)) {
1608 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1609 SkASSERT(ycbcrInfo);
1611 if (ycbcrInfo->isValid() && !GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1612 // Format may be undefined for external images, which are required to have YCbCr conversion.
1613 if (VK_FORMAT_UNDEFINED == vkFormat && ycbcrInfo->fExternalFormat != 0) {
1619 const auto& info = this->getFormatInfo(vkFormat);
1620 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1621 if (info.fColorTypeInfos[i].fColorType == ct) {
1628 GrBackendFormat GrVkCaps::onGetDefaultBackendFormat(GrColorType ct) const {
1629 VkFormat format = this->getFormatFromColorType(ct);
1630 if (format == VK_FORMAT_UNDEFINED) {
1633 return GrBackendFormat::MakeVk(format);
1636 bool GrVkCaps::onSupportsDynamicMSAA(const GrRenderTargetProxy* rtProxy) const {
1637 // We must be able to use the rtProxy as an input attachment to load into the discardable msaa
1638 // attachment. Also the rtProxy should have a sample count of 1 so that it can be used as a
1639 // resolve attachment.
1640 return this->supportsDiscardableMSAAForDMSAA() &&
1641 rtProxy->supportsVkInputAttachment() &&
1642 rtProxy->numSamples() == 1;
1645 bool GrVkCaps::renderTargetSupportsDiscardableMSAA(const GrVkRenderTarget* rt) const {
1646 return rt->resolveAttachment() &&
1647 rt->resolveAttachment()->supportsInputAttachmentUsage() &&
1648 ((rt->numSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1649 (rt->numSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1652 bool GrVkCaps::programInfoWillUseDiscardableMSAA(const GrProgramInfo& programInfo) const {
1653 return programInfo.targetHasVkResolveAttachmentWithInput() &&
1654 programInfo.numSamples() > 1 &&
1655 ((programInfo.targetsNumSamples() > 1 && this->preferDiscardableMSAAAttachment()) ||
1656 (programInfo.targetsNumSamples() == 1 && this->supportsDiscardableMSAAForDMSAA()));
1659 GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1660 SkImage::CompressionType compressionType) const {
1661 switch (compressionType) {
1662 case SkImage::CompressionType::kNone:
1664 case SkImage::CompressionType::kETC2_RGB8_UNORM:
1665 if (this->isVkFormatTexturable(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)) {
1666 return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1669 case SkImage::CompressionType::kBC1_RGB8_UNORM:
1670 if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGB_UNORM_BLOCK)) {
1671 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK);
1674 case SkImage::CompressionType::kBC1_RGBA8_UNORM:
1675 if (this->isVkFormatTexturable(VK_FORMAT_BC1_RGBA_UNORM_BLOCK)) {
1676 return GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK);
1684 skgpu::Swizzle GrVkCaps::onGetReadSwizzle(const GrBackendFormat& format,
1685 GrColorType colorType) const {
1687 SkAssertResult(format.asVkFormat(&vkFormat));
1688 const auto* ycbcrInfo = format.getVkYcbcrConversionInfo();
1689 SkASSERT(ycbcrInfo);
1690 if (ycbcrInfo->isValid() && ycbcrInfo->fExternalFormat != 0) {
1691 // We allow these to work with any color type and never swizzle. See
1692 // onAreColorTypeAndFormatCompatible.
1693 return skgpu::Swizzle{"rgba"};
1696 const auto& info = this->getFormatInfo(vkFormat);
1697 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1698 const auto& ctInfo = info.fColorTypeInfos[i];
1699 if (ctInfo.fColorType == colorType) {
1700 return ctInfo.fReadSwizzle;
1703 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1704 (int)colorType, (int)vkFormat);
1708 skgpu::Swizzle GrVkCaps::getWriteSwizzle(const GrBackendFormat& format,
1709 GrColorType colorType) const {
1711 SkAssertResult(format.asVkFormat(&vkFormat));
1712 const auto& info = this->getFormatInfo(vkFormat);
1713 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1714 const auto& ctInfo = info.fColorTypeInfos[i];
1715 if (ctInfo.fColorType == colorType) {
1716 return ctInfo.fWriteSwizzle;
1719 SkDEBUGFAILF("Illegal color type (%d) and format (%d) combination.",
1720 (int)colorType, (int)vkFormat);
1724 GrDstSampleFlags GrVkCaps::onGetDstSampleFlagsForProxy(const GrRenderTargetProxy* rt) const {
1725 bool isMSAAWithResolve = rt->numSamples() > 1 && rt->asTextureProxy();
1726 // TODO: Currently if we have an msaa rt with a resolve, the supportsVkInputAttachment call
1727 // references whether the resolve is supported as an input attachment. We need to add a check to
1728 // allow checking the color attachment (msaa or not) supports input attachment specifically.
1729 if (!isMSAAWithResolve && rt->supportsVkInputAttachment()) {
1730 return GrDstSampleFlags::kRequiresTextureBarrier | GrDstSampleFlags::kAsInputAttachment;
1732 return GrDstSampleFlags::kNone;
1735 uint64_t GrVkCaps::computeFormatKey(const GrBackendFormat& format) const {
1737 SkAssertResult(format.asVkFormat(&vkFormat));
1740 // We should never be trying to compute a key for an external format
1741 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1742 SkASSERT(ycbcrInfo);
1743 SkASSERT(!ycbcrInfo->isValid() || ycbcrInfo->fExternalFormat == 0);
1746 // A VkFormat has a size of 64 bits.
1747 return (uint64_t)vkFormat;
1750 GrCaps::SupportedRead GrVkCaps::onSupportedReadPixelsColorType(
1751 GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1752 GrColorType dstColorType) const {
1754 if (!srcBackendFormat.asVkFormat(&vkFormat)) {
1755 return {GrColorType::kUnknown, 0};
1758 if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1759 return {GrColorType::kUnknown, 0};
1762 SkImage::CompressionType compression = GrBackendFormatToCompressionType(srcBackendFormat);
1763 if (compression != SkImage::CompressionType::kNone) {
1764 return { SkCompressionTypeIsOpaque(compression) ? GrColorType::kRGB_888x
1765 : GrColorType::kRGBA_8888, 0 };
1768 // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1769 size_t offsetAlignment = align_to_4(GrVkFormatBytesPerBlock(vkFormat));
1771 const auto& info = this->getFormatInfo(vkFormat);
1772 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1773 const auto& ctInfo = info.fColorTypeInfos[i];
1774 if (ctInfo.fColorType == srcColorType) {
1775 return {ctInfo.fTransferColorType, offsetAlignment};
1778 return {GrColorType::kUnknown, 0};
1781 int GrVkCaps::getFragmentUniformBinding() const {
1782 return GrVkUniformHandler::kUniformBinding;
1785 int GrVkCaps::getFragmentUniformSet() const {
1786 return GrVkUniformHandler::kUniformBufferDescSet;
1789 void GrVkCaps::addExtraSamplerKey(skgpu::KeyBuilder* b,
1790 GrSamplerState samplerState,
1791 const GrBackendFormat& format) const {
1792 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1797 GrVkSampler::Key key = GrVkSampler::GenerateKey(samplerState, *ycbcrInfo);
1799 constexpr size_t numInts = (sizeof(key) + 3) / 4;
1800 uint32_t tmp[numInts];
1801 memcpy(tmp, &key, sizeof(key));
1803 for (size_t i = 0; i < numInts; ++i) {
1809 * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
1810 * the information needed to differentiate one pipeline from another.
1812 * The GrProgramDesc contains all the information need to create the actual shaders for the
1815 * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the
1816 * pipline. This includes stencil settings, blending information, render pass format, draw face
1817 * information, and primitive type. Note that some state is set dynamically on the pipeline for
1818 * each draw and thus is not included in this descriptor. This includes the viewport, scissor,
1819 * and blend constant.
1821 GrProgramDesc GrVkCaps::makeDesc(GrRenderTarget* rt,
1822 const GrProgramInfo& programInfo,
1823 ProgramDescOverrideFlags overrideFlags) const {
1825 GrProgramDesc::Build(&desc, programInfo, *this);
1827 skgpu::KeyBuilder b(desc.key());
1829 // This will become part of the sheared off key used to persistently cache
1830 // the SPIRV code. It needs to be added right after the base key so that,
1831 // when the base-key is sheared off, the shearing code can include it in the
1832 // reduced key (c.f. the +4s in the SkData::MakeWithCopy calls in
1833 // GrVkPipelineStateBuilder.cpp).
1834 b.add32(GrVkGpu::kShader_PersistentCacheKeyType);
1836 GrVkRenderPass::SelfDependencyFlags selfDepFlags = GrVkRenderPass::SelfDependencyFlags::kNone;
1837 if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kBlend) {
1838 selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForNonCoherentAdvBlend;
1840 if (programInfo.renderPassBarriers() & GrXferBarrierFlags::kTexture) {
1841 selfDepFlags |= GrVkRenderPass::SelfDependencyFlags::kForInputAttachment;
1844 bool needsResolve = this->programInfoWillUseDiscardableMSAA(programInfo);
1846 bool forceLoadFromResolve =
1847 overrideFlags & GrCaps::ProgramDescOverrideFlags::kVulkanHasResolveLoadSubpass;
1848 SkASSERT(!forceLoadFromResolve || needsResolve);
1850 GrVkRenderPass::LoadFromResolve loadFromResolve = GrVkRenderPass::LoadFromResolve::kNo;
1851 if (needsResolve && (programInfo.colorLoadOp() == GrLoadOp::kLoad || forceLoadFromResolve)) {
1852 loadFromResolve = GrVkRenderPass::LoadFromResolve::kLoad;
1856 GrVkRenderTarget* vkRT = (GrVkRenderTarget*) rt;
1858 SkASSERT(!needsResolve || (vkRT->resolveAttachment() &&
1859 vkRT->resolveAttachment()->supportsInputAttachmentUsage()));
1861 bool needsStencil = programInfo.needsStencil() || programInfo.isStencilEnabled();
1862 // TODO: support failure in getSimpleRenderPass
1863 auto rp = vkRT->getSimpleRenderPass(needsResolve, needsStencil, selfDepFlags,
1869 if (!rp->isExternal()) {
1870 // This is to ensure ReconstructAttachmentsDescriptor keeps matching
1871 // getSimpleRenderPass' result
1872 GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1873 GrVkRenderPass::AttachmentFlags attachmentFlags;
1874 GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1875 &attachmentsDescriptor,
1877 SkASSERT(rp->isCompatible(attachmentsDescriptor, attachmentFlags, selfDepFlags,
1882 GrVkRenderPass::AttachmentsDescriptor attachmentsDescriptor;
1883 GrVkRenderPass::AttachmentFlags attachmentFlags;
1884 GrVkRenderTarget::ReconstructAttachmentsDescriptor(*this, programInfo,
1885 &attachmentsDescriptor,
1888 // kExternal_AttachmentFlag is only set for wrapped secondary command buffers - which
1889 // will always go through the above 'rt' path (i.e., we can always pass 0 as the final
1890 // parameter to GenKey).
1891 GrVkRenderPass::GenKey(&b, attachmentFlags, attachmentsDescriptor, selfDepFlags,
1892 loadFromResolve, 0);
1895 GrStencilSettings stencil = programInfo.nonGLStencilSettings();
1896 stencil.genKey(&b, true);
1898 programInfo.pipeline().genKey(&b, *this);
1899 b.add32(programInfo.numSamples());
1901 // Vulkan requires the full primitive type as part of its key
1902 b.add32(programInfo.primitiveTypeKey());
1908 GrInternalSurfaceFlags GrVkCaps::getExtraSurfaceFlagsForDeferredRT() const {
1909 // We always create vulkan RT with the input attachment flag;
1910 return GrInternalSurfaceFlags::kVkRTSupportsInputAttachment;
1913 VkShaderStageFlags GrVkCaps::getPushConstantStageFlags() const {
1914 VkShaderStageFlags stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
1919 std::vector<GrCaps::TestFormatColorTypeCombination> GrVkCaps::getTestingCombinations() const {
1920 std::vector<GrCaps::TestFormatColorTypeCombination> combos = {
1921 { GrColorType::kAlpha_8, GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM) },
1922 { GrColorType::kBGR_565, GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16) },
1923 { GrColorType::kABGR_4444, GrBackendFormat::MakeVk(VK_FORMAT_R4G4B4A4_UNORM_PACK16)},
1924 { GrColorType::kABGR_4444, GrBackendFormat::MakeVk(VK_FORMAT_B4G4R4A4_UNORM_PACK16)},
1925 { GrColorType::kRGBA_8888, GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM) },
1926 { GrColorType::kRGBA_8888_SRGB, GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_SRGB) },
1927 { GrColorType::kRGB_888x, GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM) },
1928 { GrColorType::kRGB_888x, GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM) },
1929 { GrColorType::kRG_88, GrBackendFormat::MakeVk(VK_FORMAT_R8G8_UNORM) },
1930 { GrColorType::kBGRA_8888, GrBackendFormat::MakeVk(VK_FORMAT_B8G8R8A8_UNORM) },
1931 { GrColorType::kRGBA_1010102, GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32)},
1932 { GrColorType::kBGRA_1010102, GrBackendFormat::MakeVk(VK_FORMAT_A2R10G10B10_UNORM_PACK32)},
1933 { GrColorType::kGray_8, GrBackendFormat::MakeVk(VK_FORMAT_R8_UNORM) },
1934 { GrColorType::kAlpha_F16, GrBackendFormat::MakeVk(VK_FORMAT_R16_SFLOAT) },
1935 { GrColorType::kRGBA_F16, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT) },
1936 { GrColorType::kRGBA_F16_Clamped, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT) },
1937 { GrColorType::kAlpha_16, GrBackendFormat::MakeVk(VK_FORMAT_R16_UNORM) },
1938 { GrColorType::kRG_1616, GrBackendFormat::MakeVk(VK_FORMAT_R16G16_UNORM) },
1939 { GrColorType::kRGBA_16161616, GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_UNORM) },
1940 { GrColorType::kRG_F16, GrBackendFormat::MakeVk(VK_FORMAT_R16G16_SFLOAT) },
1941 // These two compressed formats both have an effective colorType of kRGB_888x
1942 { GrColorType::kRGB_888x, GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK)},
1943 { GrColorType::kRGB_888x, GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGB_UNORM_BLOCK) },
1944 { GrColorType::kRGBA_8888, GrBackendFormat::MakeVk(VK_FORMAT_BC1_RGBA_UNORM_BLOCK) },