Debian Codegraph @

src/gpu/ganesh/vk/GrVkCaps.cpp

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