Debian Codegraph @

src/gpu/graphite/vk/VulkanCaps.cpp

raw
1 /*
2 * Copyright 2022 Google LLC
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/graphite/vk/VulkanCaps.h"
9
10 #include "include/core/SkTextureCompressionType.h"
11 #include "include/gpu/graphite/ContextOptions.h"
12 #include "include/gpu/graphite/TextureInfo.h"
13 #include "include/gpu/graphite/vk/VulkanGraphiteTypes.h"
14 #include "include/gpu/vk/VulkanExtensions.h"
15 #include "include/gpu/vk/VulkanTypes.h"
16 #include "include/private/base/SkMath.h"
17 #include "src/gpu/SwizzlePriv.h"
18 #include "src/gpu/graphite/ContextUtils.h"
19 #include "src/gpu/graphite/GraphicsPipelineDesc.h"
20 #include "src/gpu/graphite/GraphiteResourceKey.h"
21 #include "src/gpu/graphite/RenderPassDesc.h"
22 #include "src/gpu/graphite/RendererProvider.h"
23 #include "src/gpu/graphite/RuntimeEffectDictionary.h"
24 #include "src/gpu/graphite/TextureInfoPriv.h"
25 #include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h"
26 #include "src/gpu/graphite/vk/VulkanGraphiteUtils.h"
27 #include "src/gpu/graphite/vk/VulkanRenderPass.h"
28 #include "src/gpu/graphite/vk/VulkanResourceProvider.h"
29 #include "src/gpu/graphite/vk/VulkanSharedContext.h"
30 #include "src/gpu/graphite/vk/VulkanYcbcrConversion.h"
31 #include "src/gpu/vk/VulkanUtilsPriv.h"
32 #include "src/sksl/SkSLUtil.h"
33
34 #ifdef SK_BUILD_FOR_ANDROID
35 #include <sys/system_properties.h>
36 #endif
37
38 namespace skgpu::graphite {
39
40 namespace {
41 skgpu::UniqueKey::Domain get_pipeline_domain() {
42 static const skgpu::UniqueKey::Domain kVulkanGraphicsPipelineDomain =
43 skgpu::UniqueKey::GenerateDomain();
44
45 return kVulkanGraphicsPipelineDomain;
46 }
47 } // namespace
48
49 VulkanCaps::VulkanCaps(const ContextOptions& contextOptions,
50 const skgpu::VulkanInterface* vkInterface,
51 VkPhysicalDevice physDev,
52 uint32_t physicalDeviceVersion,
53 const VkPhysicalDeviceFeatures2* features,
54 const skgpu::VulkanExtensions* extensions,
55 Protected isProtected)
56 : Caps() {
57 this->init(contextOptions, vkInterface, physDev, physicalDeviceVersion, features, extensions,
58 isProtected);
59 }
60
61 VulkanCaps::~VulkanCaps() {}
62
63 namespace {
64 void populate_resource_binding_reqs(ResourceBindingRequirements& reqs) {
65 reqs.fBackendApi = BackendApi::kVulkan;
66 // We can enable std430 and ensure no array stride mismatch in functions because all bound
67 // buffers will either be a UBO or SSBO, depending on if storage buffers are enabled or not.
68 // Although intrinsic uniforms always use uniform buffers, they do not contain any arrays.
69 reqs.fStorageBufferLayout = Layout::kStd430;
70
71 // TODO(b/374997389): Somehow convey & enforce Layout::kStd430 for push constants.
72 reqs.fUniformBufferLayout = Layout::kStd140;
73 reqs.fSeparateTextureAndSamplerBinding = false;
74
75 // Vulkan uses push constants instead of an intrinsic UBO, so we do not need to assign
76 // reqs.fIntrinsicBufferBinding.
77 reqs.fUsePushConstantsForIntrinsicConstants = true;
78
79 // Assign uniform buffer binding values for shader generation
80 reqs.fCombinedUniformBufferBinding = VulkanGraphicsPipeline::kCombinedUniformIndex;
81 reqs.fGradientBufferBinding = VulkanGraphicsPipeline::kGradientBufferIndex;
82
83 // Assign descriptor set indices for shader generation
84 reqs.fUniformsSetIdx = VulkanGraphicsPipeline::kUniformBufferDescSetIndex;
85 reqs.fTextureSamplerSetIdx = VulkanGraphicsPipeline::kTextureBindDescSetIndex;
86 // Note: We use kDstAsInputDescSetIndex as opposed to kLoadMsaaFromResolveInputDescSetIndex
87 // because the former is what is needed for SkSL generation purposes at the graphite level. The
88 // latter is simply internally referenced when defining bespoke SkSL for loading MSAA from
89 // resolve.
90 reqs.fInputAttachmentSetIdx = VulkanGraphicsPipeline::kDstAsInputDescSetIndex;
91 }
92 } // anonymous
93
94 void VulkanCaps::init(const ContextOptions& contextOptions,
95 const skgpu::VulkanInterface* vkInterface,
96 VkPhysicalDevice physDev,
97 uint32_t physicalDeviceVersion,
98 const VkPhysicalDeviceFeatures2* features,
99 const skgpu::VulkanExtensions* extensions,
100 Protected isProtected) {
101 const EnabledFeatures enabledFeatures =
102 this->getEnabledFeatures(features, physicalDeviceVersion);
103
104 PhysicalDeviceProperties deviceProperties;
105 this->getProperties(vkInterface,
106 physDev,
107 physicalDeviceVersion,
108 extensions,
109 enabledFeatures,
110 &deviceProperties);
111
112 const VkPhysicalDeviceLimits& deviceLimits = deviceProperties.fBase.properties.limits;
113 const uint32_t vendorID = deviceProperties.fBase.properties.vendorID;
114
115 #if defined(GPU_TEST_UTILS)
116 this->setDeviceName(deviceProperties.fBase.properties.deviceName);
117 #endif
118
119 // Graphite requires Vulkan version 1.1 or later, which always has protected support. The
120 // protectedMemory feature is assumed enabled if isProtected is true.
121 if (isProtected == Protected::kYes) {
122 fProtectedSupport = true;
123 fShouldAlwaysUseDedicatedImageMemory = true;
124 }
125
126 fPhysicalDeviceMemoryProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
127 fPhysicalDeviceMemoryProperties2.pNext = nullptr;
128 VULKAN_CALL(vkInterface,
129 GetPhysicalDeviceMemoryProperties2(physDev, &fPhysicalDeviceMemoryProperties2));
130
131 // We could actually query and get a max size for each config, however maxImageDimension2D will
132 // give the minimum max size across all configs. So for simplicity we will use that for now.
133 fMaxTextureSize = std::min(deviceLimits.maxImageDimension2D, (uint32_t)INT_MAX);
134
135 // Assert that our push constant sizes are below the maximum allowed (which is guaranteed to be
136 // at least 128 bytes per spec).
137 static_assert(VulkanResourceProvider::kIntrinsicConstantSize < 128 &&
138 VulkanResourceProvider::kLoadMSAAPushConstantSize < 128);
139
140 fRequiredUniformBufferAlignment = deviceLimits.minUniformBufferOffsetAlignment;
141 fRequiredStorageBufferAlignment = deviceLimits.minStorageBufferOffsetAlignment;
142 fRequiredTransferBufferAlignment = 4;
143
144 fMaxVaryings = std::min(deviceLimits.maxVertexOutputComponents,
145 deviceLimits.maxFragmentInputComponents) / 4;
146
147 // Unlike D3D, WebGPU, and Metal, the Vulkan NDC coordinate space is aligned with the top-left
148 // Y-down coordinate space of the viewport.
149 fNDCYAxisPointsDown = true;
150
151 populate_resource_binding_reqs(fResourceBindingReqs);
152
153 // TODO(b/353983969): Enable storage buffers once perf regressions are addressed.
154 fStorageBufferSupport = false;
155
156 VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
157 VULKAN_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &deviceMemoryProperties));
158 fSupportsMemorylessAttachments = false;
159 VkMemoryPropertyFlags requiredLazyFlags = VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT;
160 if (fProtectedSupport) {
161 // If we have a protected context we can only use memoryless images if they also support
162 // being protected. With current devices we don't actually expect this combination to be
163 // supported, but this at least covers us for future devices that may allow it.
164 requiredLazyFlags |= VK_MEMORY_PROPERTY_PROTECTED_BIT;
165 }
166 for (uint32_t i = 0; i < deviceMemoryProperties.memoryTypeCount; ++i) {
167 const uint32_t& supportedFlags = deviceMemoryProperties.memoryTypes[i].propertyFlags;
168 if ((supportedFlags & requiredLazyFlags) == requiredLazyFlags) {
169 fSupportsMemorylessAttachments = true;
170 }
171 }
172
173 #ifdef SK_BUILD_FOR_UNIX
174 if (skgpu::kNvidia_VkVendor == vendorID) {
175 // On NVIDIA linux we see a big perf regression when not using dedicated image allocations.
176 fShouldAlwaysUseDedicatedImageMemory = true;
177 }
178 #endif
179
180 if (vendorID == skgpu::kNvidia_VkVendor || vendorID == skgpu::kAMD_VkVendor) {
181 // On discrete GPUs, it can be faster to read gpu-only memory compared to memory that is
182 // also mappable on the host.
183 fGpuOnlyBuffersMorePerformant = true;
184
185 // On discrete GPUs we try to use special DEVICE_LOCAL and HOST_VISIBLE memory for our
186 // cpu write, gpu read buffers. This memory is not ideal to be kept persistently mapped.
187 // Some discrete GPUs do not expose this special memory, however we still disable
188 // persistently mapped buffers for all of them since most GPUs with updated drivers do
189 // expose it. If this becomes an issue we can try to be more fine grained.
190 fShouldPersistentlyMapCpuToGpuBuffers = false;
191 }
192
193 // AMD advertises support for MAX_UINT vertex attributes but in reality only supports 32.
194 fMaxVertexAttributes =
195 vendorID == skgpu::kAMD_VkVendor ? 32 : deviceLimits.maxVertexInputAttributes;
196 fMaxUniformBufferRange = deviceLimits.maxUniformBufferRange;
197 fMaxStorageBufferRange = deviceLimits.maxStorageBufferRange;
198
199 #ifdef SK_BUILD_FOR_ANDROID
200 if (extensions->hasExtension(
201 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
202 extensions->hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1)) {
203 fSupportsAHardwareBufferImages = true;
204 }
205 #endif
206
207 fSupportsYcbcrConversion = enabledFeatures.fSamplerYcbcrConversion;
208 fSupportsDeviceFaultInfo = enabledFeatures.fDeviceFault;
209 fSupportsFrameBoundary = enabledFeatures.fFrameBoundary;
210
211 fSupportsPipelineCreationCacheControl = enabledFeatures.fPipelineCreationCacheControl;
212
213 if (enabledFeatures.fAdvancedBlendModes) {
214 fBlendEqSupport = enabledFeatures.fCoherentAdvancedBlendModes
215 ? BlendEquationSupport::kAdvancedCoherent
216 : BlendEquationSupport::kAdvancedNoncoherent;
217 fShaderCaps->fAdvBlendEqInteraction =
218 SkSL::ShaderCaps::AdvBlendEqInteraction::kAutomatic_AdvBlendEqInteraction;
219 }
220
221 // Note: ARM GPUs have always been coherent, do not add a subpass self-dependency even if the
222 // application hasn't enabled this feature as it comes with a performance cost on this GPU. Use
223 // of VK_EXT_rasterization_order_attachment_access is disabled on ARM due to an unexplained
224 // memory regression (b/437907749).
225 //
226 // Imagination GPUs are also coherent but only within the same sample when sample-shading.
227 // VK_EXT_rasterization_order_attachment_access indicates coherence when input attachment read
228 // is done from any samples of the same pixel, which is why Imagination drivers cannot expose
229 // this extension. This is not a problem for Graphite however, which does not enable sample
230 // shading (nor would it read color from other samples even if it did).
231 fSupportsRasterizationOrderColorAttachmentAccess =
232 enabledFeatures.fRasterizationOrderColorAttachmentAccess && vendorID != kARM_VkVendor;
233 fIsInputAttachmentReadCoherent = fSupportsRasterizationOrderColorAttachmentAccess ||
234 vendorID == kARM_VkVendor || vendorID == kImagination_VkVendor;
235
236 this->initShaderCaps(enabledFeatures, vendorID);
237
238 // Vulkan 1.0 dynamic state is always supported. Dynamic state based on features of
239 // VK_EXT_extended_dynamic_state and VK_EXT_extended_dynamic_state2 are also considered basic
240 // given the extensions' age and the fact that they are core since Vulkan 1.3.
241 fUseBasicDynamicState =
242 enabledFeatures.fExtendedDynamicState && enabledFeatures.fExtendedDynamicState2;
243
244 // Vertex input dynamic state depends on the main feature of
245 // VK_EXT_vertex_input_dynamic_state.
246 fUseVertexInputDynamicState = enabledFeatures.fVertexInputDynamicState;
247
248 // Graphics pipeline library usage depends on the main feature of
249 // VK_EXT_graphics_pipeline_library. The graphicsPipelineLibraryFastLinking property indicates
250 // whether linking libraries is cheap, without which the extension is not very useful. However,
251 // this property is currently ignored for known vendors that set it to false while link is still
252 // fast.
253 fUsePipelineLibraries =
254 enabledFeatures.fGraphicsPipelineLibrary &&
255 (deviceProperties.fGpl.graphicsPipelineLibraryFastLinking || vendorID == kARM_VkVendor);
256
257
258 fSupportsFrameBoundary = enabledFeatures.fFrameBoundary;
259
260 // Multisampled render to single-sampled usage depends on the mandatory feature of
261 // VK_EXT_multisampled_render_to_single_sampled. Per format queries are needed to determine if
262 // multisampled->single-sampled rendering is supported, which should in practice always be equal
263 // to whether multisampled rendering is supported for that format.
264 fMSAARenderToSingleSampledSupport = enabledFeatures.fMultisampledRenderToSingleSampled;
265
266 // Host image copy depends on the main feature of VK_EXT_host_image_copy, which is core since
267 // Vulkan 1.4. The identicalMemoryTypeRequirements property indicates whether host-copyable
268 // images require special (limited) memory types. If that property is not set, use of this
269 // extension is avoided to avoid incurring performance penalties or run out of the
270 // likely-much-smaller memory available on those devices. This property is expected to be set
271 // on UMA devices.
272 //
273 // The SHADER_READ_ONLY layout is ubiquitously found in pCopyDstLayouts, so we rely on it. If
274 // that is ever missing (unlikely, given the future direction with
275 // VK_KHR_unified_image_layouts), then Recorder::update*BackendTexture should first transition
276 // to GENERAL with vkTransitionImageLayout and at the end use a GPU barrier to SHADER_READ_ONLY
277 // (as opposed to current code that transitions directly to SHADER_READ_ONLY and uploads to it).
278 fSupportsHostImageCopy = enabledFeatures.fHostImageCopy &&
279 deviceProperties.fHic.identicalMemoryTypeRequirements &&
280 deviceProperties.fHicHasShaderReadOnlyDstLayout;
281
282 // Note: Do not add extension/feature checks after this; driver workarounds should be done last.
283 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
284 this->applyDriverCorrectnessWorkarounds(deviceProperties);
285 }
286
287 // Note that format table initialization should be performed at the end of this method to ensure
288 // all capability determinations are completed prior to populating the format tables.
289 this->initFormatTable(vkInterface, physDev, deviceProperties.fBase.properties, enabledFeatures);
290 this->initDepthStencilFormatTable(vkInterface, physDev, deviceProperties.fBase.properties);
291
292 this->finishInitialization(contextOptions);
293 }
294
295 // Walk the feature chain once and extract any enabled features that Graphite cares about.
296 VulkanCaps::EnabledFeatures VulkanCaps::getEnabledFeatures(
297 const VkPhysicalDeviceFeatures2* features, uint32_t physicalDeviceVersion) {
298 EnabledFeatures enabled;
299 if (features) {
300 // Base features:
301 enabled.fDualSrcBlend = features->features.dualSrcBlend;
302
303 if (physicalDeviceVersion >= VK_API_VERSION_1_3) {
304 enabled.fExtendedDynamicState = true;
305 enabled.fExtendedDynamicState2 = true;
306 }
307
308 // Extended features:
309 const VkBaseInStructure* pNext = static_cast<const VkBaseInStructure*>(features->pNext);
310 while (pNext) {
311 switch (pNext->sType) {
312 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES: {
313 const auto* feature =
314 reinterpret_cast<const VkPhysicalDeviceVulkan11Features*>(pNext);
315 enabled.fSamplerYcbcrConversion = feature->samplerYcbcrConversion;
316 break;
317 }
318 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES: {
319 const auto* feature =
320 reinterpret_cast<const VkPhysicalDeviceVulkan13Features*>(pNext);
321 enabled.fPipelineCreationCacheControl = feature->pipelineCreationCacheControl;
322 break;
323 }
324 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES: {
325 const auto* feature =
326 reinterpret_cast<const VkPhysicalDeviceVulkan14Features*>(pNext);
327 enabled.fHostImageCopy = feature->hostImageCopy;
328 break;
329 }
330 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_CREATION_CACHE_CONTROL_FEATURES: {
331 const auto* feature =
332 reinterpret_cast<
333 const VkPhysicalDevicePipelineCreationCacheControlFeatures*>(pNext);
334 enabled.fPipelineCreationCacheControl = feature->pipelineCreationCacheControl;
335 break;
336 }
337 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
338 const auto* feature =
339 reinterpret_cast<const VkPhysicalDeviceSamplerYcbcrConversionFeatures*>(
340 pNext);
341 enabled.fSamplerYcbcrConversion = feature->samplerYcbcrConversion;
342 break;
343 }
344 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FAULT_FEATURES_EXT: {
345 const auto* feature =
346 reinterpret_cast<const VkPhysicalDeviceFaultFeaturesEXT*>(pNext);
347 enabled.fDeviceFault = feature->deviceFault;
348 break;
349 }
350 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT: {
351 const auto* feature = reinterpret_cast<
352 const VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT*>(pNext);
353 // The feature struct being present at all indicated advanced blend mode
354 // support. A member of it indicates whether the device offers coherent or
355 // noncoherent support.
356 enabled.fAdvancedBlendModes = true;
357 enabled.fCoherentAdvancedBlendModes =
358 feature->advancedBlendCoherentOperations == VK_TRUE;
359 break;
360 }
361 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RASTERIZATION_ORDER_ATTACHMENT_ACCESS_FEATURES_EXT: {
362 const auto* feature = reinterpret_cast<
363 const VkPhysicalDeviceRasterizationOrderAttachmentAccessFeaturesEXT*>(
364 pNext);
365 enabled.fRasterizationOrderColorAttachmentAccess =
366 feature->rasterizationOrderColorAttachmentAccess;
367 break;
368 }
369 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT: {
370 const auto* feature = reinterpret_cast<
371 const VkPhysicalDeviceExtendedDynamicStateFeaturesEXT*>(pNext);
372 enabled.fExtendedDynamicState = feature->extendedDynamicState;
373 break;
374 }
375 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_2_FEATURES_EXT: {
376 const auto* feature = reinterpret_cast<
377 const VkPhysicalDeviceExtendedDynamicState2FeaturesEXT*>(pNext);
378 enabled.fExtendedDynamicState2 = feature->extendedDynamicState2;
379 break;
380 }
381 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT: {
382 const auto* feature = reinterpret_cast<
383 const VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT*>(pNext);
384 enabled.fVertexInputDynamicState = feature->vertexInputDynamicState;
385 break;
386 }
387 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GRAPHICS_PIPELINE_LIBRARY_FEATURES_EXT: {
388 const auto* feature = reinterpret_cast<
389 const VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT*>(pNext);
390 enabled.fGraphicsPipelineLibrary = feature->graphicsPipelineLibrary;
391 break;
392 }
393 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_FEATURES_EXT: {
394 const auto* feature = reinterpret_cast<
395 const VkPhysicalDeviceMultisampledRenderToSingleSampledFeaturesEXT*>(
396 pNext);
397 enabled.fMultisampledRenderToSingleSampled =
398 feature->multisampledRenderToSingleSampled;
399 break;
400 }
401 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES: {
402 const auto* feature =
403 reinterpret_cast<const VkPhysicalDeviceHostImageCopyFeatures*>(pNext);
404 enabled.fHostImageCopy = feature->hostImageCopy;
405 break;
406 }
407 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAME_BOUNDARY_FEATURES_EXT: {
408 const auto *feature = reinterpret_cast<
409 const VkPhysicalDeviceFrameBoundaryFeaturesEXT*>(pNext);
410 enabled.fFrameBoundary = feature->frameBoundary;
411 break;
412 }
413 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RGBA10X6_FORMATS_FEATURES_EXT: {
414 const auto *feature = reinterpret_cast<
415 const VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT*>(pNext);
416 enabled.fFormatRGBA10x6WithoutYCbCrSampler =
417 feature->formatRgba10x6WithoutYCbCrSampler;
418 break;
419 }
420 default:
421 break;
422 }
423
424 pNext = pNext->pNext;
425 }
426 }
427 return enabled;
428 }
429
430 // Query the physical device properties that Graphite cares about.
431 void VulkanCaps::getProperties(const skgpu::VulkanInterface* vkInterface,
432 VkPhysicalDevice physDev,
433 uint32_t physicalDeviceVersion,
434 const skgpu::VulkanExtensions* extensions,
435 const EnabledFeatures& features,
436 PhysicalDeviceProperties* props) {
437 props->fBase = {};
438 props->fBase.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
439
440 props->fDriver = {};
441 props->fDriver.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
442
443 props->fGpl = {};
444 props->fGpl.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GRAPHICS_PIPELINE_LIBRARY_PROPERTIES_EXT;
445
446 props->fHic = {};
447 props->fHic.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_PROPERTIES;
448
449 constexpr uint32_t kHicCopyDstLayoutMax = 50;
450 VkImageLayout hicCopyDstLayoutStorage[kHicCopyDstLayoutMax] = {};
451 props->fHic.copyDstLayoutCount = kHicCopyDstLayoutMax;
452 props->fHic.pCopyDstLayouts = hicCopyDstLayoutStorage;
453
454 const bool hasDriverProperties =
455 physicalDeviceVersion >= VK_API_VERSION_1_2 ||
456 extensions->hasExtension(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME, 1);
457 if (hasDriverProperties) {
458 AddToPNextChain(&props->fBase, &props->fDriver);
459 } else {
460 SKGPU_LOG_W("VK_KHR_driver_properties is not enabled, driver workarounds cannot "
461 "be correctly applied");
462 }
463
464 if (features.fGraphicsPipelineLibrary) {
465 AddToPNextChain(&props->fBase, &props->fGpl);
466 }
467
468 if (features.fHostImageCopy) {
469 AddToPNextChain(&props->fBase, &props->fHic);
470 }
471
472 // Graphite requires Vulkan version 1.1 or later, so vkGetPhysicalDeviceProperties2 should
473 // always be available.
474 VULKAN_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props->fBase));
475
476 if (features.fHostImageCopy) {
477 // vkTransitionImageLayout from this extension can be used to transition between image
478 // layouts on the host, with the allowed old and new layouts found in pCopySrcLayouts and
479 // pCopyDstLayouts respectively. The GENERAL layout is required to be found in both. Skia
480 // has two use cases for this function; initialization (UNDEFINED->GENERAL) and after
481 // texture updates (GENERAL->SHADER_READ_ONLY, per Recorder::update*BackendTexture).
482 props->fHicHasShaderReadOnlyDstLayout =
483 std::find(props->fHic.pCopyDstLayouts,
484 props->fHic.pCopyDstLayouts + props->fHic.copyDstLayoutCount,
485 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
486 }
487
488 // If this field is not filled, driver bug workarounds won't work correctly. It should always
489 // be filled, unless filling it itself is a driver bug, or the Vulkan driver is too old. In
490 // that case, make a guess of what the driver ID is, but the driver is likely to be too buggy to
491 // be used by Graphite either way.
492 if (props->fDriver.driverID == 0) {
493 switch (props->fBase.properties.vendorID) {
494 case kAMD_VkVendor:
495 props->fDriver.driverID = VK_DRIVER_ID_AMD_PROPRIETARY;
496 break;
497 case kARM_VkVendor:
498 props->fDriver.driverID = VK_DRIVER_ID_ARM_PROPRIETARY;
499 break;
500 case kBroadcom_VkVendor:
501 props->fDriver.driverID = VK_DRIVER_ID_BROADCOM_PROPRIETARY;
502 break;
503 case kGoogle_VkVendor:
504 props->fDriver.driverID = VK_DRIVER_ID_GOOGLE_SWIFTSHADER;
505 break;
506 case kImagination_VkVendor:
507 props->fDriver.driverID = VK_DRIVER_ID_IMAGINATION_PROPRIETARY;
508 break;
509 case kIntel_VkVendor:
510 #ifdef SK_BUILD_FOR_WIN
511 props->fDriver.driverID = VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS;
512 #else
513 props->fDriver.driverID = VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA;
514 #endif
515 break;
516 case kNvidia_VkVendor:
517 props->fDriver.driverID = VK_DRIVER_ID_NVIDIA_PROPRIETARY;
518 break;
519 case kQualcomm_VkVendor:
520 props->fDriver.driverID = VK_DRIVER_ID_QUALCOMM_PROPRIETARY;
521 break;
522 case kSamsung_VkVendor:
523 props->fDriver.driverID = VK_DRIVER_ID_SAMSUNG_PROPRIETARY;
524 break;
525 case kVeriSilicon_VkVendor:
526 props->fDriver.driverID = VK_DRIVER_ID_VERISILICON_PROPRIETARY;
527 break;
528 default:
529 // Unknown device, but this means no driver workarounds are provisioned for it so
530 // driver ID remaining 0 is not going to change anything.
531 break;
532 }
533 }
534 }
535
536 void VulkanCaps::applyDriverCorrectnessWorkarounds(const PhysicalDeviceProperties& properties) {
537 // By default, we initialize the Android API version to 0 since we consider certain things
538 // "fixed" only once above a certain version. This way, we default to enabling the workarounds.
539 int androidAPIVersion = 0;
540 #if defined(SK_BUILD_FOR_ANDROID)
541 char androidAPIVersionStr[PROP_VALUE_MAX];
542 int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersionStr);
543 // Defaults to zero since most checks care if it is greater than a specific value. So this will
544 // just default to it being less.
545 androidAPIVersion = (strLength == 0) ? 0 : atoi(androidAPIVersionStr);
546 #endif
547
548 const uint32_t vendorID = properties.fBase.properties.vendorID;
549 const VkDriverId driverID = properties.fDriver.driverID;
550 const skgpu::DriverVersion driverVersion =
551 skgpu::ParseVulkanDriverVersion(driverID, properties.fBase.properties.driverVersion);
552
553 const bool isARM = skgpu::kARM_VkVendor == vendorID;
554 const bool isIntel = skgpu::kIntel_VkVendor == vendorID;
555 const bool isQualcomm = skgpu::kQualcomm_VkVendor == vendorID;
556
557 const bool isARMProprietary = isARM && VK_DRIVER_ID_ARM_PROPRIETARY == driverID;
558 const bool isIntelWindowsProprietary =
559 isIntel && VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS == driverID;
560 const bool isQualcommProprietary = isQualcomm && VK_DRIVER_ID_QUALCOMM_PROPRIETARY == driverID;
561
562 // All Mali Job-Manager based GPUs have maxDrawIndirectCount==1 and all Commans-Stream Front
563 // GPUs have maxDrawIndirectCount>1. This is used as proxy to detect JM GPUs.
564 const bool isMaliJobManagerArch =
565 isARM && properties.fBase.properties.limits.maxDrawIndirectCount <= 1;
566
567 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
568 if (isARMProprietary && androidAPIVersion <= 28) {
569 fShouldAlwaysUseDedicatedImageMemory = true;
570 }
571
572 // On Qualcomm the gpu resolves an area larger than the render pass bounds when using
573 // discardable msaa attachments. This causes the resolve to resolve uninitialized data from the
574 // msaa image into the resolve image. This was reproed on a Pixel4 using the DstReadShuffle GM
575 // where the top half of the GM would drop out. In Ganesh we had also seen this on Arm devices,
576 // but the issue hasn't appeared yet in Graphite. It may just have occured on older Arm drivers
577 // that we don't even test any more. This also occurs on swiftshader: b/303705884 in Ganesh, but
578 // we aren't currently testing that in Graphite yet so leaving that off the workaround for now
579 // until we run into it.
580 if (isQualcommProprietary) {
581 fMustLoadFullImageForMSAA = true;
582 }
583
584 // MSAA doesn't work well on Intel GPUs crbug.com/40434119, crbug.com/41470715
585 if (isIntel) {
586 fAvoidMSAA = true;
587 }
588
589 // Too many bugs on older ARM drivers with CSF architecture. On JM GPUs, more bugs were
590 // encountered with newer drivers, unknown if ever fixed.
591 const bool avoidExtendedDynamicState =
592 (isARMProprietary && driverVersion < skgpu::DriverVersion(44, 1)) ||
593 isMaliJobManagerArch;
594
595 // Known bugs in addition to ARM bugs above:
596 //
597 // - Cull mode dynamic state on ARM drivers prior to r52; vkCmdSetCullMode incorrectly culls
598 // non-triangle topologies, according to the errata:
599 // https://developer.arm.com/documentation/SDEN-3735689/0100/?lang=en. However,
600 // Graphite only uses triangles and cull mode is always disabled so this driver bug is not
601 // relevant.
602 if (avoidExtendedDynamicState) {
603 fUseBasicDynamicState = false;
604 }
605
606 // Known bugs in vertex input dynamic state:
607 //
608 // - Intel windows driver, unknown if fixed: http://anglebug.com/42265637#comment9
609 // - Qualcomm drivers prior to 777: http://anglebug.com/381384988
610 // - In ARM drivers prior to r48, vkCmdBindVertexBuffers2 applies strides to the wrong index
611 // when the state is dynamic, according to the errata:
612 // https://developer.arm.com/documentation/SDEN-3735689/0100/?lang=en
613 if (isIntelWindowsProprietary ||
614 (isARMProprietary && driverVersion < skgpu::DriverVersion(48, 0)) ||
615 (isQualcommProprietary && driverVersion < skgpu::DriverVersion(512, 777))) {
616 fUseVertexInputDynamicState = false;
617 }
618
619 // Qualcomm driver 512.821 is known to have rendering bugs with
620 // VK_EXT_multisampled_render_to_single_sampled.
621 // http://crbug.com/413427770
622 if (isQualcommProprietary && driverVersion < skgpu::DriverVersion(512, 822)) {
623 fMSAARenderToSingleSampledSupport = false;
624 }
625 }
626
627 // These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
628 // frequently used to least to improve look up times in arrays.
629 static constexpr VkFormat kVkFormats[] = {
630 VK_FORMAT_R8G8B8A8_UNORM,
631 VK_FORMAT_R8_UNORM,
632 VK_FORMAT_B8G8R8A8_UNORM,
633 VK_FORMAT_R5G6B5_UNORM_PACK16,
634 VK_FORMAT_R16G16B16A16_SFLOAT,
635 VK_FORMAT_R16_SFLOAT,
636 VK_FORMAT_R8G8B8_UNORM,
637 VK_FORMAT_R8G8_UNORM,
638 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
639 VK_FORMAT_A2R10G10B10_UNORM_PACK32,
640 VK_FORMAT_B4G4R4A4_UNORM_PACK16,
641 VK_FORMAT_R4G4B4A4_UNORM_PACK16,
642 VK_FORMAT_R8G8B8A8_SRGB,
643 VK_FORMAT_B8G8R8A8_SRGB,
644 VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
645 VK_FORMAT_BC1_RGB_UNORM_BLOCK,
646 VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
647 VK_FORMAT_R16_UNORM,
648 VK_FORMAT_R16G16_UNORM,
649 VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM,
650 VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
651 VK_FORMAT_R16G16B16A16_UNORM,
652 VK_FORMAT_R16G16_SFLOAT,
653 VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16
654 };
655 // These are all the valid depth/stencil formats that we support in Skia.
656 static constexpr VkFormat kDepthStencilVkFormats[] = {
657 VK_FORMAT_S8_UINT,
658 VK_FORMAT_D16_UNORM,
659 VK_FORMAT_D32_SFLOAT,
660 VK_FORMAT_D24_UNORM_S8_UINT,
661 VK_FORMAT_D32_SFLOAT_S8_UINT,
662 };
663
664 bool VulkanCaps::isSampleCountSupported(TextureFormat format,
665 SampleCount requestedSampleCount) const {
666 VkFormat vkFormat = TextureFormatToVkFormat(format);
667 const SupportedSampleCounts* sampleCounts;
668
669 // TODO(b/390473370): When Caps stores the format tables, the color format and depth stencil
670 // format infos will be combined and this will be simplified.
671 if (TextureFormatIsDepthOrStencil(format)) {
672 const DepthStencilFormatInfo& formatInfo = this->getDepthStencilFormatInfo(vkFormat);
673 if (!formatInfo.isDepthStencilSupported()) {
674 return false;
675 }
676 sampleCounts = &formatInfo.fSupportedSampleCounts;
677 } else {
678 const FormatInfo& formatInfo = this->getFormatInfo(vkFormat);
679 if (!formatInfo.isRenderable(VK_IMAGE_TILING_OPTIMAL, SampleCount::k1)) {
680 return false;
681 }
682 sampleCounts = &formatInfo.fSupportedSampleCounts;
683 }
684
685 return sampleCounts->isSampleCountSupported(requestedSampleCount);
686 }
687
688 TextureFormat VulkanCaps::getDepthStencilFormat(SkEnumBitMask<DepthStencilFlags> flags) const {
689 VkFormat format = fDepthStencilFlagsToFormatTable[flags.value()];
690 return VkFormatToTextureFormat(format);
691 }
692
693 TextureInfo VulkanCaps::getDefaultAttachmentTextureInfo(AttachmentDesc desc,
694 Protected isProtected,
695 Discardable discardable) const {
696 if ((isProtected == Protected::kYes && !this->protectedSupport()) ||
697 !this->isSampleCountSupported(desc.fFormat, desc.fSampleCount)) {
698 return {};
699 }
700
701 const bool isDepthStencil = TextureFormatIsDepthOrStencil(desc.fFormat);
702
703 /**
704 * Graphite, unlike ganesh, does not require a dedicated MSAA attachment on every surface.
705 * MSAA textures now get resolved within the scope of a render pass, which can be done simply
706 * with the color attachment usage flag. So we no longer require transfer src/dst usage flags.
707 * All renderable textures in Vulkan are made with input attachment usage.
708 */
709 VkImageCreateFlags createFlags =
710 (isProtected == Protected::kYes) ? VK_IMAGE_CREATE_PROTECTED_BIT : 0;
711 VkImageUsageFlags usageFlags = isDepthStencil ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
712 : VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
713 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
714
715 if (discardable == Discardable::kYes && fSupportsMemorylessAttachments) {
716 usageFlags = usageFlags | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
717 }
718
719 /*
720 * When VK_EXT_multisampled_render_to_single_sampled is supported, proactively use the
721 * VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT flag. This flag is expected to
722 * be harmless (if not, it's a driver bug).
723 */
724 if (desc.fSampleCount == SampleCount::k1 && this->msaaRenderToSingleSampledSupport()) {
725 createFlags |= VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
726 }
727
728 VulkanTextureInfo info;
729 info.fSampleCount = desc.fSampleCount;
730 info.fMipmapped = Mipmapped::kNo;
731 info.fFlags = createFlags;
732 info.fFormat = TextureFormatToVkFormat(desc.fFormat);
733 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
734 info.fImageUsageFlags = usageFlags;
735 info.fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
736 info.fAspectMask = isDepthStencil
737 ? ((TextureFormatHasDepth(desc.fFormat) ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) |
738 (TextureFormatHasStencil(desc.fFormat) ? VK_IMAGE_ASPECT_STENCIL_BIT : 0))
739 : VK_IMAGE_ASPECT_COLOR_BIT;
740
741 return TextureInfos::MakeVulkan(info);
742 }
743
744 TextureInfo VulkanCaps::getDefaultSampledTextureInfo(SkColorType ct,
745 Mipmapped mipmapped,
746 Protected isProtected,
747 Renderable isRenderable) const {
748 VkFormat format = this->getFormatFromColorType(ct);
749 const FormatInfo& formatInfo = this->getFormatInfo(format);
750
751 if ((isProtected == Protected::kYes && !this->protectedSupport()) ||
752 !formatInfo.isTexturable(VK_IMAGE_TILING_OPTIMAL) ||
753 (isRenderable == Renderable::kYes &&
754 !formatInfo.isRenderable(VK_IMAGE_TILING_OPTIMAL, SampleCount::k1)) ) {
755 return {};
756 }
757
758 VulkanTextureInfo info;
759 info.fSampleCount = SampleCount::k1;
760 info.fMipmapped = mipmapped;
761 info.fFlags = (isProtected == Protected::kYes) ? VK_IMAGE_CREATE_PROTECTED_BIT : 0;
762 info.fFormat = format;
763 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
764 info.fImageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
765 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
766 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
767 if (isRenderable == Renderable::kYes) {
768 // We make all renderable images support being used as input attachment
769 info.fImageUsageFlags = info.fImageUsageFlags |
770 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
771 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
772 // Proactively prepare the image for multisampled rendering.
773 if (this->msaaRenderToSingleSampledSupport()) {
774 info.fFlags |= VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
775 }
776 } else {
777 // On every known driver where VK_EXT_host_image_copy is used by Skia, it is known that
778 // using the host-image-copy flag reduces the performance of renderable images. So, we don't
779 // even bother with a query in the `Renderable::kYes` case.
780 if (formatInfo.isEfficientWithHostImageCopy(info.fImageTiling, isProtected)) {
781 info.fImageUsageFlags |= VK_IMAGE_USAGE_HOST_TRANSFER_BIT;
782 }
783 }
784 info.fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
785 info.fAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
786
787 return TextureInfos::MakeVulkan(info);
788 }
789
790 TextureInfo VulkanCaps::getTextureInfoForSampledCopy(const TextureInfo& textureInfo,
791 Mipmapped mipmapped) const {
792 VulkanTextureInfo info;
793 info.fSampleCount = SampleCount::k1;
794 info.fMipmapped = mipmapped;
795 info.fFormat = TextureInfoPriv::Get<VulkanTextureInfo>(textureInfo).fFormat;
796 info.fFlags = (textureInfo.isProtected() == Protected::kYes) ?
797 VK_IMAGE_CREATE_PROTECTED_BIT : 0;
798 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
799 info.fImageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
800 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
801 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
802 const FormatInfo& formatInfo = this->getFormatInfo(info.fFormat);
803 if (formatInfo.isEfficientWithHostImageCopy(info.fImageTiling, textureInfo.isProtected())) {
804 info.fImageUsageFlags |= VK_IMAGE_USAGE_HOST_TRANSFER_BIT;
805 }
806 info.fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
807
808 return TextureInfos::MakeVulkan(info);
809 }
810
811 namespace {
812 VkFormat format_from_compression(SkTextureCompressionType compression) {
813 switch (compression) {
814 case SkTextureCompressionType::kETC2_RGB8_UNORM:
815 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
816 case SkTextureCompressionType::kBC1_RGB8_UNORM:
817 return VK_FORMAT_BC1_RGB_UNORM_BLOCK;
818 case SkTextureCompressionType::kBC1_RGBA8_UNORM:
819 return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
820 default:
821 return VK_FORMAT_UNDEFINED;
822 }
823 }
824 }
825
826 TextureInfo VulkanCaps::getDefaultCompressedTextureInfo(SkTextureCompressionType compression,
827 Mipmapped mipmapped,
828 Protected isProtected) const {
829 VkFormat format = format_from_compression(compression);
830 const FormatInfo& formatInfo = this->getFormatInfo(format);
831 if ((isProtected == Protected::kYes && !this->protectedSupport()) ||
832 !formatInfo.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
833 return {};
834 }
835
836 VulkanTextureInfo info;
837 info.fSampleCount = SampleCount::k1;
838 info.fMipmapped = mipmapped;
839 info.fFlags = (isProtected == Protected::kYes) ? VK_IMAGE_CREATE_PROTECTED_BIT : 0;
840 info.fFormat = format;
841 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
842 info.fImageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
843 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
844 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
845 if (formatInfo.isEfficientWithHostImageCopy(info.fImageTiling, isProtected)) {
846 info.fImageUsageFlags |= VK_IMAGE_USAGE_HOST_TRANSFER_BIT;
847 }
848 info.fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
849 info.fAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
850
851 return TextureInfos::MakeVulkan(info);
852 }
853
854 TextureInfo VulkanCaps::getDefaultStorageTextureInfo(SkColorType colorType) const {
855 VkFormat format = this->getFormatFromColorType(colorType);
856 const FormatInfo& formatInfo = this->getFormatInfo(format);
857 if (!formatInfo.isTexturable(VK_IMAGE_TILING_OPTIMAL) ||
858 !formatInfo.isStorage(VK_IMAGE_TILING_OPTIMAL)) {
859 return {};
860 }
861
862 VulkanTextureInfo info;
863 info.fSampleCount = SampleCount::k1;
864 info.fMipmapped = Mipmapped::kNo;
865 info.fFlags = 0;
866 info.fFormat = format;
867 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
868 // Storage textures are currently always sampleable from a shader
869 info.fImageUsageFlags = VK_IMAGE_USAGE_STORAGE_BIT |
870 VK_IMAGE_USAGE_SAMPLED_BIT |
871 VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
872 info.fSharingMode = VK_SHARING_MODE_EXCLUSIVE;
873 info.fAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
874
875 return TextureInfos::MakeVulkan(info);
876 }
877
878 void VulkanCaps::initShaderCaps(const EnabledFeatures enabledFeatures, const uint32_t vendorID) {
879 // TODO(skbug.com/40045541): We must force std430 array stride when using SSBOs since SPIR-V
880 // generation cannot handle mixed array strides being passed into functions.
881 fShaderCaps->fForceStd430ArrayLayout =
882 fStorageBufferSupport && fResourceBindingReqs.fStorageBufferLayout == Layout::kStd430;
883
884 // Avoid RelaxedPrecision with OpImageSampleImplicitLod due to driver bug with YCbCr sampling.
885 // (skbug.com/421927604)
886 fShaderCaps->fCannotUseRelaxedPrecisionOnImageSample = vendorID == kNvidia_VkVendor;
887
888 fShaderCaps->fDualSourceBlendingSupport = enabledFeatures.fDualSrcBlend;
889 }
890
891 void VulkanCaps::initFormatTable(const skgpu::VulkanInterface* interface,
892 VkPhysicalDevice physDev,
893 const VkPhysicalDeviceProperties& properties,
894 const EnabledFeatures& enabledFeatures) {
895 static_assert(std::size(kVkFormats) == VulkanCaps::kNumVkFormats,
896 "Size of VkFormats array must match static value in header");
897
898 std::fill_n(fColorTypeToFormatTable, kSkColorTypeCnt, VK_FORMAT_UNDEFINED);
899
900 // NOTE: VkFormat's naming convention orders channels from low address to high address when
901 // interpreting unpacked formats. For packed formats, the channels are ordered most significant
902 // to least significant (making them opposite of the unpacked).
903
904 // Go through all the formats and init their support surface and data ColorTypes.
905 // Format: VK_FORMAT_R8G8B8A8_UNORM
906 {
907 constexpr VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
908 auto& info = this->getFormatInfoForInit(format);
909 info.init(interface, *this, physDev, format);
910 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
911 info.fColorTypeInfoCount = 2;
912 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
913 int ctIdx = 0;
914 // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGBA_8888
915 {
916 constexpr SkColorType ct = SkColorType::kRGBA_8888_SkColorType;
917 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
918 ctInfo.fColorType = ct;
919 ctInfo.fTransferColorType = ct;
920 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
921 }
922 // Format: VK_FORMAT_R8G8B8A8_UNORM, Surface: kRGB_888x
923 {
924 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
925 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
926 ctInfo.fColorType = ct;
927 ctInfo.fTransferColorType = ct;
928 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
929 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
930 }
931 }
932 }
933
934 // Format: VK_FORMAT_R8_UNORM
935 {
936 constexpr VkFormat format = VK_FORMAT_R8_UNORM;
937 auto& info = this->getFormatInfoForInit(format);
938 info.init(interface, *this, physDev, format);
939 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
940 info.fColorTypeInfoCount = 3;
941 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
942 int ctIdx = 0;
943 // Format: VK_FORMAT_R8_UNORM, Surface: kAlpha_8
944 {
945 constexpr SkColorType ct = SkColorType::kAlpha_8_SkColorType;
946 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
947 ctInfo.fColorType = ct;
948 ctInfo.fTransferColorType = ct;
949 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
950 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
951 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
952 }
953 // Format: VK_FORMAT_R8_UNORM, Surface: kR_8
954 {
955 constexpr SkColorType ct = SkColorType::kR8_unorm_SkColorType;
956 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
957 ctInfo.fColorType = ct;
958 ctInfo.fTransferColorType = ct;
959 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
960 }
961 // Format: VK_FORMAT_R8_UNORM, Surface: kGray_8
962 {
963 constexpr SkColorType ct = SkColorType::kGray_8_SkColorType;
964 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
965 ctInfo.fColorType = ct;
966 ctInfo.fTransferColorType = ct;
967 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
968 ctInfo.fReadSwizzle = skgpu::Swizzle("rrr1");
969 }
970 }
971 }
972
973 // Format: VK_FORMAT_B8G8R8A8_UNORM
974 {
975 constexpr VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
976 auto& info = this->getFormatInfoForInit(format);
977 info.init(interface, *this, physDev, format);
978 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
979 info.fColorTypeInfoCount = 2;
980 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
981 int ctIdx = 0;
982 // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kBGRA_8888
983 {
984 constexpr SkColorType ct = SkColorType::kBGRA_8888_SkColorType;
985 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
986 ctInfo.fColorType = ct;
987 ctInfo.fTransferColorType = ct;
988 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
989 }
990 // TODO: This is currently brittle, but add kExternalFormatColorType as a valid color
991 // type for VK_FORMAT_B8G8R8A8_UNORM in order to pass texture format + color type
992 // compatibility checks.
993 //
994 // b/431290055 exposed an issue where we could end up using an SkColorType that is not
995 // compatible with the BackendTexture's VkFormat. In this case, the driver reported that
996 // an AHardwareBuffer with the format AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM (only
997 // present in Android framework builds) could be treated as VK_FORMAT_B8G8R8A8_UNORM.
998 // This is not an officially supported equivalency per the spec (
999 // https://docs.vulkan.org/spec/latest/chapters/memory.html#memory-external-android-hardware-buffer-formats),
1000 // but clients would benefit from having the driver's suggested VkFormat be used rather
1001 // than falling back to importing the AHwBuf using its external format (which is less
1002 // performant).
1003 //
1004 // Using the driver-recommended VkFormat led to failing Skia checks which rely upon
1005 // surfaces + images having an SkColorType that is compatible with a texture's native
1006 // format. When *creating* a Surface or Image from an AHwBuf-based BackendTexture, its
1007 // color type is determined by the *AHwBuf format* (using
1008 // `AHardwareBufferUtils::GetSkColorTypeFromBufferFormat`). However, when
1009 // *validating* the surface/image, Skia checks whether the color type is compatible with
1010 // the texture's *VkFormat* (which is distinct from an AndroidHardwareBuffer format).
1011 //
1012 // Ideally, we would simply add the AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM -->
1013 // VK_FORMAT_B8G8R8A8_UNORM mapping to `GetSkColorTypeFromBufferFormat`. However, this
1014 // utility is public and shared b/w both ganesh and graphite. Adding this introduced
1015 // nontrivial complexity in ganesh (e.g., its SkImages::DeferredFromAHardwareBuffer API
1016 // does not know the AHwBuf format at creation). This could be worked around, but the
1017 // change would be more invasive and no clients have requested that we allow the use of
1018 // a driver's non-spec reported VkFormat equivalency in ganesh. Therefore, we simply
1019 // default to assigning kExternalFormatColorType for
1020 // AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM and add it as a supported color type for
1021 // VK_FORMAT_B8G8R8A8_UNORM here in the Caps table. This should allow BackendTextures
1022 // based on AHwBufs with this format to pass validity checks and theoretically still be
1023 // used normally (see below for explanation as to why this works).
1024 //
1025 // Format: VK_FORMAT_B8G8R8A8_UNORM, Surface: kRGBA_8888
1026 {
1027 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1028 // `Caps::areColorTypeAndTextureInfoCompatible` consults the fColorType field, so
1029 // make sure it aligns with the color type we expect to see for AHardwareBuffers
1030 // that use AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM (kExternalFormatColorType).
1031 // This *MUST* match AHardwareBufferUtils::kExternalFormatColorType, which is
1032 // only conditionally compiled so not included here. If it becomes out sync,
1033 // wrapping BGRA textures from AHB's may fail unexpected.
1034 static constexpr SkColorType kExternalFormatColorType = kRGBA_8888_SkColorType;
1035 ctInfo.fColorType = kExternalFormatColorType;
1036 // fTransferColorType is currently not referenced, but the actual color type
1037 // (e.g. for readbacks) should be kBGRA_8888_SkColorType so use that here. Simply
1038 // omit assigning any read/write swizzles because we actually already know the
1039 // texture format is compatible with kBGRA_8888_SkColorType.
1040 constexpr SkColorType transferColorType = SkColorType::kBGRA_8888_SkColorType;
1041 ctInfo.fTransferColorType = transferColorType;
1042 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1043 }
1044 }
1045 }
1046 // Format: VK_FORMAT_R5G6B5_UNORM_PACK16
1047 {
1048 constexpr VkFormat format = VK_FORMAT_R5G6B5_UNORM_PACK16;
1049 auto& info = this->getFormatInfoForInit(format);
1050 info.init(interface, *this, physDev, format);
1051 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1052 info.fColorTypeInfoCount = 1;
1053 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1054 int ctIdx = 0;
1055 // Format: VK_FORMAT_R5G6B5_UNORM_PACK16, Surface: kRGB_565_SkColorType
1056 {
1057 constexpr SkColorType ct = SkColorType::kRGB_565_SkColorType;
1058 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1059 ctInfo.fColorType = ct;
1060 ctInfo.fTransferColorType = ct;
1061 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1062 }
1063 }
1064 }
1065 // Format: VK_FORMAT_R16G16B16A16_SFLOAT
1066 {
1067 constexpr VkFormat format = VK_FORMAT_R16G16B16A16_SFLOAT;
1068 auto& info = this->getFormatInfoForInit(format);
1069 info.init(interface, *this, physDev, format);
1070 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1071 info.fColorTypeInfoCount = 2;
1072 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1073 int ctIdx = 0;
1074 // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: kRGBA_F16_SkColorType
1075 {
1076 constexpr SkColorType ct = SkColorType::kRGBA_F16_SkColorType;
1077 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1078 ctInfo.fColorType = ct;
1079 ctInfo.fTransferColorType = ct;
1080 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1081 }
1082 // Format: VK_FORMAT_R16G16B16A16_SFLOAT, Surface: kRGB_F16F16F16x_SkColorType
1083 {
1084 constexpr SkColorType ct = SkColorType::kRGB_F16F16F16x_SkColorType;
1085 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1086 ctInfo.fColorType = ct;
1087 ctInfo.fTransferColorType = ct;
1088 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1089 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
1090 }
1091 }
1092 }
1093 // Format: VK_FORMAT_R16_SFLOAT
1094 {
1095 constexpr VkFormat format = VK_FORMAT_R16_SFLOAT;
1096 auto& info = this->getFormatInfoForInit(format);
1097 info.init(interface, *this, physDev, format);
1098 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1099 info.fColorTypeInfoCount = 1;
1100 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1101 int ctIdx = 0;
1102 // Format: VK_FORMAT_R16_SFLOAT, Surface: kAlpha_F16
1103 {
1104 constexpr SkColorType ct = SkColorType::kA16_float_SkColorType;
1105 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1106 ctInfo.fColorType = ct;
1107 ctInfo.fTransferColorType = ct;
1108 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1109 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
1110 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
1111 }
1112 }
1113 }
1114 // Format: VK_FORMAT_R8G8B8_UNORM
1115 {
1116 constexpr VkFormat format = VK_FORMAT_R8G8B8_UNORM;
1117 auto& info = this->getFormatInfoForInit(format);
1118 info.init(interface, *this, physDev, format);
1119 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1120 info.fColorTypeInfoCount = 1;
1121 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1122 int ctIdx = 0;
1123 // Format: VK_FORMAT_R8G8B8_UNORM, Surface: kRGB_888x
1124 {
1125 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
1126 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1127 ctInfo.fColorType = ct;
1128 // This SkColorType is a lie, but we don't have a kRGB_888_SkColorType. The Vulkan
1129 // format is 3 bpp so we must manualy convert to/from this and kRGB_888x when doing
1130 // transfers. We signal this need for manual conversions in the
1131 // supportedRead/WriteColorType calls.
1132 ctInfo.fTransferColorType = SkColorType::kRGB_888x_SkColorType;
1133 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1134 }
1135 }
1136 }
1137 // Format: VK_FORMAT_R8G8_UNORM
1138 {
1139 constexpr VkFormat format = VK_FORMAT_R8G8_UNORM;
1140 auto& info = this->getFormatInfoForInit(format);
1141 info.init(interface, *this, physDev, format);
1142 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1143 info.fColorTypeInfoCount = 1;
1144 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1145 int ctIdx = 0;
1146 // Format: VK_FORMAT_R8G8_UNORM, Surface: kR8G8_unorm
1147 {
1148 constexpr SkColorType ct = SkColorType::kR8G8_unorm_SkColorType;
1149 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1150 ctInfo.fColorType = ct;
1151 ctInfo.fTransferColorType = ct;
1152 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1153 }
1154 }
1155 }
1156 // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32
1157 {
1158 constexpr VkFormat format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
1159 auto& info = this->getFormatInfoForInit(format);
1160 info.init(interface, *this, physDev, format);
1161 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1162 info.fColorTypeInfoCount = 2;
1163 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1164 int ctIdx = 0;
1165 // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGBA_1010102
1166 {
1167 constexpr SkColorType ct = SkColorType::kRGBA_1010102_SkColorType;
1168 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1169 ctInfo.fColorType = ct;
1170 ctInfo.fTransferColorType = ct;
1171 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1172 }
1173 // Format: VK_FORMAT_A2B10G10R10_UNORM_PACK32, Surface: kRGB_101010x
1174 {
1175 constexpr SkColorType ct = SkColorType::kRGB_101010x_SkColorType;
1176 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1177 ctInfo.fColorType = ct;
1178 ctInfo.fTransferColorType = ct;
1179 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1180 ctInfo.fReadSwizzle = skgpu::Swizzle::RGB1();
1181 }
1182 }
1183 }
1184 // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32
1185 {
1186 constexpr VkFormat format = VK_FORMAT_A2R10G10B10_UNORM_PACK32;
1187 auto& info = this->getFormatInfoForInit(format);
1188 info.init(interface, *this, physDev, format);
1189 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1190 info.fColorTypeInfoCount = 1;
1191 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1192 int ctIdx = 0;
1193 // Format: VK_FORMAT_A2R10G10B10_UNORM_PACK32, Surface: kBGRA_1010102
1194 {
1195 constexpr SkColorType ct = SkColorType::kBGRA_1010102_SkColorType;
1196 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1197 ctInfo.fColorType = ct;
1198 ctInfo.fTransferColorType = ct;
1199 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1200 }
1201 }
1202 }
1203 // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16
1204 {
1205 constexpr VkFormat format = VK_FORMAT_B4G4R4A4_UNORM_PACK16;
1206 auto& info = this->getFormatInfoForInit(format);
1207 info.init(interface, *this, physDev, format);
1208 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1209 info.fColorTypeInfoCount = 1;
1210 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1211 int ctIdx = 0;
1212 // Format: VK_FORMAT_B4G4R4A4_UNORM_PACK16, Surface: kARGB_4444_SkColorType
1213 {
1214 constexpr SkColorType ct = SkColorType::kARGB_4444_SkColorType;
1215 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1216 ctInfo.fColorType = ct;
1217 ctInfo.fTransferColorType = ct;
1218 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1219 // The color type is misnamed and really stores ABGR data, but there is no
1220 // SkColorType that matches this actual ARGB VkFormat data. Swapping R and B when
1221 // rendering into it has it match the reported transfer color type, but we have to
1222 // swap R and B when sampling as well. This only works so long as we don't present
1223 // textures of this format to a screen that would not know about this swap.
1224 ctInfo.fReadSwizzle = skgpu::Swizzle::BGRA();
1225 ctInfo.fWriteSwizzle = skgpu::Swizzle::BGRA();
1226 }
1227 }
1228 }
1229
1230 // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16
1231 {
1232 constexpr VkFormat format = VK_FORMAT_R4G4B4A4_UNORM_PACK16;
1233 auto& info = this->getFormatInfoForInit(format);
1234 info.init(interface, *this, physDev, format);
1235 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1236 info.fColorTypeInfoCount = 1;
1237 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1238 int ctIdx = 0;
1239 // Format: VK_FORMAT_R4G4B4A4_UNORM_PACK16, Surface: kARGB_4444_SkColorType
1240 {
1241 constexpr SkColorType ct = SkColorType::kARGB_4444_SkColorType;
1242 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1243 ctInfo.fColorType = ct;
1244 ctInfo.fTransferColorType = ct;
1245 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1246 }
1247 }
1248 }
1249 // Format: VK_FORMAT_R8G8B8A8_SRGB
1250 {
1251 constexpr VkFormat format = VK_FORMAT_R8G8B8A8_SRGB;
1252 auto& info = this->getFormatInfoForInit(format);
1253 info.init(interface, *this, physDev, format);
1254 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1255 info.fColorTypeInfoCount = 1;
1256 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1257 int ctIdx = 0;
1258 // Format: VK_FORMAT_R8G8B8A8_SRGB, Surface: kRGBA_8888_SRGB
1259 {
1260 constexpr SkColorType ct = SkColorType::kSRGBA_8888_SkColorType;
1261 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1262 ctInfo.fColorType = ct;
1263 ctInfo.fTransferColorType = SkColorType::kSRGBA_8888_SkColorType;
1264 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1265 }
1266 }
1267 }
1268 // Format: VK_FORMAT_B8G8R8A8_SRGB
1269 {
1270 constexpr VkFormat format = VK_FORMAT_B8G8R8A8_SRGB;
1271 auto& info = this->getFormatInfoForInit(format);
1272 info.init(interface, *this, physDev, format);
1273 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1274 info.fColorTypeInfoCount = 1;
1275 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1276 int ctIdx = 0;
1277 // Format: VK_FORMAT_B8G8R8A8_SRGB, Surface: kRGBA_8888_SRGB
1278 {
1279 constexpr SkColorType ct = SkColorType::kSRGBA_8888_SkColorType;
1280 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1281 ctInfo.fColorType = ct;
1282 // Since the B and R channels are swapped and there's no BGRA sRGB color type,
1283 // just disable read/writes back to the CPU.
1284 ctInfo.fTransferColorType = SkColorType::kUnknown_SkColorType;
1285 ctInfo.fFlags = ColorTypeInfo::kRenderable_Flag;
1286 }
1287 }
1288 }
1289 // Format: VK_FORMAT_R16_UNORM
1290 {
1291 constexpr VkFormat format = VK_FORMAT_R16_UNORM;
1292 auto& info = this->getFormatInfoForInit(format);
1293 info.init(interface, *this, physDev, format);
1294 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1295 info.fColorTypeInfoCount = 2;
1296 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1297 int ctIdx = 0;
1298 // Format: VK_FORMAT_R16_UNORM, Surface: kAlpha_16
1299 {
1300 constexpr SkColorType ct = SkColorType::kA16_unorm_SkColorType;
1301 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1302 ctInfo.fColorType = ct;
1303 ctInfo.fTransferColorType = ct;
1304 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1305 ctInfo.fReadSwizzle = skgpu::Swizzle("000r");
1306 ctInfo.fWriteSwizzle = skgpu::Swizzle("a000");
1307 }
1308 // Format: VK_FORMAT_R16_UNORM, Surface: kR16_unorm
1309 {
1310 constexpr SkColorType ct = SkColorType::kR16_unorm_SkColorType;
1311 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1312 ctInfo.fColorType = ct;
1313 ctInfo.fTransferColorType = ct;
1314 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1315 }
1316 }
1317 }
1318 // Format: VK_FORMAT_R16G16_UNORM
1319 {
1320 constexpr VkFormat format = VK_FORMAT_R16G16_UNORM;
1321 auto& info = this->getFormatInfoForInit(format);
1322 info.init(interface, *this, physDev, format);
1323 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1324 info.fColorTypeInfoCount = 1;
1325 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1326 int ctIdx = 0;
1327 // Format: VK_FORMAT_R16G16_UNORM, Surface: kRG_1616
1328 {
1329 constexpr SkColorType ct = SkColorType::kR16G16_unorm_SkColorType;
1330 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1331 ctInfo.fColorType = ct;
1332 ctInfo.fTransferColorType = ct;
1333 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1334 }
1335 }
1336 }
1337 // Format: VK_FORMAT_R16G16B16A16_UNORM
1338 {
1339 constexpr VkFormat format = VK_FORMAT_R16G16B16A16_UNORM;
1340 auto& info = this->getFormatInfoForInit(format);
1341 info.init(interface, *this, physDev, format);
1342 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1343 info.fColorTypeInfoCount = 1;
1344 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1345 int ctIdx = 0;
1346 // Format: VK_FORMAT_R16G16B16A16_UNORM, Surface: kRGBA_16161616
1347 {
1348 constexpr SkColorType ct = SkColorType::kR16G16B16A16_unorm_SkColorType;
1349 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1350 ctInfo.fColorType = ct;
1351 ctInfo.fTransferColorType = ct;
1352 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1353 }
1354 }
1355 }
1356 // Format: VK_FORMAT_R16G16_SFLOAT
1357 {
1358 constexpr VkFormat format = VK_FORMAT_R16G16_SFLOAT;
1359 auto& info = this->getFormatInfoForInit(format);
1360 info.init(interface, *this, physDev, format);
1361 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1362 info.fColorTypeInfoCount = 1;
1363 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1364 int ctIdx = 0;
1365 // Format: VK_FORMAT_R16G16_SFLOAT, Surface: kRG_F16
1366 {
1367 constexpr SkColorType ct = SkColorType::kR16G16_float_SkColorType;
1368 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1369 ctInfo.fColorType = ct;
1370 ctInfo.fTransferColorType = ct;
1371 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1372 }
1373 }
1374 }
1375 // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM
1376 {
1377 constexpr VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
1378 auto& info = this->getFormatInfoForInit(format);
1379 if (fSupportsYcbcrConversion) {
1380 info.init(interface, *this, physDev, format);
1381 }
1382 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1383 info.fColorTypeInfoCount = 1;
1384 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1385 int ctIdx = 0;
1386 // Format: VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, Surface: kRGB_888x
1387 {
1388 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
1389 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1390 ctInfo.fColorType = ct;
1391 ctInfo.fTransferColorType = ct;
1392 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1393 }
1394 SkDEBUGCODE(info.fIsWrappedOnly = true;)
1395 }
1396 }
1397 // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM
1398 {
1399 constexpr VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
1400 auto& info = this->getFormatInfoForInit(format);
1401 if (fSupportsYcbcrConversion) {
1402 info.init(interface, *this, physDev, format);
1403 }
1404 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1405 info.fColorTypeInfoCount = 1;
1406 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1407 int ctIdx = 0;
1408 // Format: VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, Surface: kRGB_888x
1409 {
1410 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
1411 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1412 ctInfo.fColorType = ct;
1413 ctInfo.fTransferColorType = ct;
1414 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1415 }
1416 SkDEBUGCODE(info.fIsWrappedOnly = true;)
1417 }
1418 }
1419 // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
1420 {
1421 constexpr VkFormat format = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
1422 auto& info = this->getFormatInfoForInit(format);
1423 info.init(interface, *this, physDev, format);
1424 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1425 info.fColorTypeInfoCount = 1;
1426 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1427 int ctIdx = 0;
1428 // Format: VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, Surface: kRGB_888x
1429 {
1430 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
1431 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1432 ctInfo.fColorType = ct;
1433 ctInfo.fTransferColorType = ct;
1434 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1435 }
1436 }
1437 }
1438
1439 // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK
1440 {
1441 constexpr VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK;
1442 auto& info = this->getFormatInfoForInit(format);
1443 info.init(interface, *this, physDev, format);
1444 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1445 info.fColorTypeInfoCount = 1;
1446 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1447 int ctIdx = 0;
1448 // Format: VK_FORMAT_BC1_RGB_UNORM_BLOCK, Surface: kRGB_888x
1449 {
1450 constexpr SkColorType ct = SkColorType::kRGB_888x_SkColorType;
1451 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1452 ctInfo.fColorType = ct;
1453 ctInfo.fTransferColorType = ct;
1454 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1455 }
1456 }
1457 }
1458
1459 // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK
1460 {
1461 constexpr VkFormat format = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
1462 auto& info = this->getFormatInfoForInit(format);
1463 info.init(interface, *this, physDev, format);
1464 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1465 info.fColorTypeInfoCount = 1;
1466 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1467 int ctIdx = 0;
1468 // Format: VK_FORMAT_BC1_RGBA_UNORM_BLOCK, Surface: kRGBA_8888
1469 {
1470 constexpr SkColorType ct = SkColorType::kRGBA_8888_SkColorType;
1471 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1472 ctInfo.fColorType = ct;
1473 ctInfo.fTransferColorType = ct;
1474 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
1475 }
1476 }
1477 }
1478
1479 // Format: VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16
1480 // Technically without this extension and enabled feature we could still use this format to
1481 // sample with a ycbcr sampler. But for simplicity until we have clients requesting that, we
1482 // limit the use of this format to cases where we have the extension supported.
1483 if (enabledFeatures.fFormatRGBA10x6WithoutYCbCrSampler) {
1484 constexpr VkFormat format = VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16;
1485 auto& info = this->getFormatInfoForInit(format);
1486 info.init(interface, *this, physDev, format);
1487 if (info.isTexturable(VK_IMAGE_TILING_OPTIMAL)) {
1488 info.fColorTypeInfoCount = 1;
1489 info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
1490 int ctIdx = 0;
1491 // Format: VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16, Surface: kRGBA_10x6
1492 {
1493 constexpr SkColorType ct = SkColorType::kRGBA_10x6_SkColorType;
1494 auto& ctInfo = info.fColorTypeInfos[ctIdx++];
1495 ctInfo.fColorType = ct;
1496 ctInfo.fTransferColorType = ct;
1497 ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag | ColorTypeInfo::kRenderable_Flag;
1498 }
1499 }
1500 }
1501
1502 ////////////////////////////////////////////////////////////////////////////
1503 // Map SkColorType (used for creating Surfaces) to VkFormats. The order in which the formats are
1504 // passed into the setColorType function indicates the priority in selecting which format we use
1505 // for a given SkColorType.
1506 typedef SkColorType ct;
1507
1508 this->setColorType(ct::kAlpha_8_SkColorType, { VK_FORMAT_R8_UNORM });
1509 this->setColorType(ct::kRGB_565_SkColorType, { VK_FORMAT_R5G6B5_UNORM_PACK16 });
1510 this->setColorType(ct::kARGB_4444_SkColorType, { VK_FORMAT_R4G4B4A4_UNORM_PACK16,
1511 VK_FORMAT_B4G4R4A4_UNORM_PACK16 });
1512 this->setColorType(ct::kRGBA_8888_SkColorType, { VK_FORMAT_R8G8B8A8_UNORM });
1513 this->setColorType(ct::kSRGBA_8888_SkColorType, { VK_FORMAT_R8G8B8A8_SRGB,
1514 VK_FORMAT_B8G8R8A8_SRGB });
1515 this->setColorType(ct::kRGB_888x_SkColorType, { VK_FORMAT_R8G8B8_UNORM,
1516 VK_FORMAT_R8G8B8A8_UNORM });
1517 this->setColorType(ct::kR8G8_unorm_SkColorType, { VK_FORMAT_R8G8_UNORM });
1518 this->setColorType(ct::kBGRA_8888_SkColorType, { VK_FORMAT_B8G8R8A8_UNORM });
1519 this->setColorType(ct::kRGBA_1010102_SkColorType, { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1520 this->setColorType(ct::kBGRA_1010102_SkColorType, { VK_FORMAT_A2R10G10B10_UNORM_PACK32 });
1521 this->setColorType(ct::kRGB_101010x_SkColorType, { VK_FORMAT_A2B10G10R10_UNORM_PACK32 });
1522 this->setColorType(ct::kGray_8_SkColorType, { VK_FORMAT_R8_UNORM });
1523 this->setColorType(ct::kA16_float_SkColorType, { VK_FORMAT_R16_SFLOAT });
1524 this->setColorType(ct::kRGBA_F16_SkColorType, { VK_FORMAT_R16G16B16A16_SFLOAT });
1525 this->setColorType(ct::kRGB_F16F16F16x_SkColorType, { VK_FORMAT_R16G16B16A16_SFLOAT });
1526 this->setColorType(ct::kA16_unorm_SkColorType, { VK_FORMAT_R16_UNORM });
1527 this->setColorType(ct::kR16_unorm_SkColorType, { VK_FORMAT_R16_UNORM });
1528 this->setColorType(ct::kR16G16_unorm_SkColorType, { VK_FORMAT_R16G16_UNORM });
1529 this->setColorType(ct::kR16G16B16A16_unorm_SkColorType, { VK_FORMAT_R16G16B16A16_UNORM });
1530 this->setColorType(ct::kR16G16_float_SkColorType, { VK_FORMAT_R16G16_SFLOAT });
1531 this->setColorType(ct::kRGBA_10x6_SkColorType, { VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16 });
1532 }
1533
1534 namespace {
1535 void set_ds_flags_to_format(VkFormat& slot, VkFormat format) {
1536 if (slot == VK_FORMAT_UNDEFINED) {
1537 slot = format;
1538 }
1539 }
1540 } // namespace
1541
1542 void VulkanCaps::initDepthStencilFormatTable(const skgpu::VulkanInterface* interface,
1543 VkPhysicalDevice physDev,
1544 const VkPhysicalDeviceProperties& properties) {
1545 static_assert(std::size(kDepthStencilVkFormats) == VulkanCaps::kNumDepthStencilVkFormats,
1546 "Size of DepthStencilVkFormats array must match static value in header");
1547
1548 using DSFlags = SkEnumBitMask<DepthStencilFlags>;
1549 constexpr DSFlags stencilFlags = DepthStencilFlags::kStencil;
1550 constexpr DSFlags depthFlags = DepthStencilFlags::kDepth;
1551 constexpr DSFlags dsFlags = DepthStencilFlags::kDepthStencil;
1552
1553 std::fill_n(fDepthStencilFlagsToFormatTable, kNumDepthStencilFlags, VK_FORMAT_UNDEFINED);
1554 // Format: VK_FORMAT_S8_UINT
1555 {
1556 constexpr VkFormat format = VK_FORMAT_S8_UINT;
1557 auto& info = this->getDepthStencilFormatInfoForInit(format);
1558 info.init(interface, *this, physDev, format);
1559 if (info.fFormatProperties.optimalTilingFeatures &
1560 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1561 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[stencilFlags.value()], format);
1562 }
1563 }
1564 // Format: VK_FORMAT_D16_UNORM
1565 {
1566 // Qualcomm drivers will report OUT_OF_HOST_MEMORY when binding memory to a VkImage with
1567 // D16_UNORM in a protected context. Using D32_SFLOAT succeeds, so clearly it's not actually
1568 // out of memory. D16_UNORM appears to function correctly in unprotected contexts.
1569 const bool disableD16InProtected =
1570 this->protectedSupport() && skgpu::kQualcomm_VkVendor == properties.vendorID;
1571 if (!disableD16InProtected) {
1572 constexpr VkFormat format = VK_FORMAT_D16_UNORM;
1573 auto& info = this->getDepthStencilFormatInfoForInit(format);
1574 info.init(interface, *this, physDev, format);
1575 if (info.fFormatProperties.optimalTilingFeatures &
1576 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1577 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[depthFlags.value()], format);
1578 }
1579 }
1580 }
1581 // Format: VK_FORMAT_D32_SFLOAT
1582 {
1583 constexpr VkFormat format = VK_FORMAT_D32_SFLOAT;
1584 auto& info = this->getDepthStencilFormatInfoForInit(format);
1585 info.init(interface, *this, physDev, format);
1586 if (info.fFormatProperties.optimalTilingFeatures &
1587 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1588 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[depthFlags.value()], format);
1589 }
1590 }
1591 // Format: VK_FORMAT_D24_UNORM_S8_UINT
1592 {
1593 constexpr VkFormat format = VK_FORMAT_D24_UNORM_S8_UINT;
1594 auto& info = this->getDepthStencilFormatInfoForInit(format);
1595 info.init(interface, *this, physDev, format);
1596 if (info.fFormatProperties.optimalTilingFeatures &
1597 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1598 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[stencilFlags.value()], format);
1599 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[depthFlags.value()], format);
1600 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[dsFlags.value()], format);
1601 }
1602 }
1603 // Format: VK_FORMAT_D32_SFLOAT_S8_UINT
1604 {
1605 constexpr VkFormat format = VK_FORMAT_D32_SFLOAT_S8_UINT;
1606 auto& info = this->getDepthStencilFormatInfoForInit(format);
1607 info.init(interface, *this, physDev, format);
1608 if (info.fFormatProperties.optimalTilingFeatures &
1609 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1610 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[stencilFlags.value()], format);
1611 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[depthFlags.value()], format);
1612 set_ds_flags_to_format(fDepthStencilFlagsToFormatTable[dsFlags.value()], format);
1613 }
1614 }
1615 }
1616
1617 void VulkanCaps::SupportedSampleCounts::initSampleCounts(const skgpu::VulkanInterface* interface,
1618 const VulkanCaps& caps,
1619 VkPhysicalDevice physDev,
1620 VkFormat format,
1621 VkImageUsageFlags usage) {
1622 VkImageFormatProperties properties;
1623
1624 VkResult result;
1625 // VULKAN_CALL_RESULT requires a VulkanSharedContext for tracking DEVICE_LOST, but VulkanCaps
1626 // are initialized before a VulkanSharedContext is available. The _NOCHECK variant only requires
1627 // a VulkanInterface, so we can use that and log failures manually.
1628 VULKAN_CALL_RESULT_NOCHECK(interface,
1629 result,
1630 GetPhysicalDeviceImageFormatProperties(physDev,
1631 format,
1632 VK_IMAGE_TYPE_2D,
1633 VK_IMAGE_TILING_OPTIMAL,
1634 usage,
1635 0, // createFlags
1636 &properties));
1637 if (result != VK_SUCCESS) {
1638 SKGPU_LOG_W("Vulkan call GetPhysicalDeviceImageFormatProperties failed: %d", result);
1639 return;
1640 }
1641
1642 // Standard sample locations are not defined for more than 16 samples, and we don't need more
1643 // than 16. Omit 32 and 64.
1644 fSampleCounts = properties.sampleCounts &
1645 (VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT |
1646 VK_SAMPLE_COUNT_8_BIT | VK_SAMPLE_COUNT_16_BIT);
1647
1648 // Disable MSAA if driver workaround requires it, by pretending the format does not support any
1649 // sample count other than 1.
1650 if (caps.avoidMSAA()) {
1651 fSampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1652 }
1653
1654 // If VK_EXT_multisampled_render_to_single_sampled is used, verify that the
1655 // VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT flag does not alter the
1656 // supported sample counts. If it does, it's not against the spec but it also doesn't make
1657 // practical sense (the extension is all about load (unresolve) and store (resolve) ops, it
1658 // shouldn't affect multisampled rendering itself). In that case, issue a warning and mask out
1659 // unsupported bits.
1660 if (caps.msaaRenderToSingleSampledSupport() && fSampleCounts > VK_SAMPLE_COUNT_1_BIT) {
1661 properties.sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1662 VULKAN_CALL_RESULT_NOCHECK(
1663 interface,
1664 result,
1665 GetPhysicalDeviceImageFormatProperties(
1666 physDev,
1667 format,
1668 VK_IMAGE_TYPE_2D,
1669 VK_IMAGE_TILING_OPTIMAL,
1670 usage,
1671 VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT,
1672 &properties));
1673 if (result != VK_SUCCESS && result != VK_ERROR_FORMAT_NOT_SUPPORTED) {
1674 SKGPU_LOG_W("Vulkan call GetPhysicalDeviceImageFormatProperties failed: %d", result);
1675 return;
1676 }
1677 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED ||
1678 properties.sampleCounts <= VK_SAMPLE_COUNT_1_BIT ||
1679 (fSampleCounts & properties.sampleCounts) != fSampleCounts) {
1680 SKGPU_LOG_W(
1681 "Inconsistent MSAA rendering support in the presence of "
1682 "VK_EXT_multisampled_render_to_single_sampled (Supported MSAA bits: %#X vs "
1683 "with MSRTSS: %#X)",
1684 fSampleCounts,
1685 result == VK_ERROR_FORMAT_NOT_SUPPORTED ? 0 : properties.sampleCounts);
1686
1687 // Mask out the unsupported bits
1688 if (result == VK_SUCCESS) {
1689 fSampleCounts &= (properties.sampleCounts | VK_SAMPLE_COUNT_1_BIT);
1690 } else {
1691 fSampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1692 }
1693 }
1694 }
1695 }
1696
1697 bool VulkanCaps::SupportedSampleCounts::isSampleCountSupported(SampleCount requestedCount) const {
1698 VkSampleCountFlagBits vkCount = SampleCountToVkSampleCount(requestedCount);
1699 return (fSampleCounts & vkCount) != 0;
1700 }
1701
1702
1703 namespace {
1704 bool is_texturable(VkFormatFeatureFlags flags) {
1705 return SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & flags) &&
1706 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & flags);
1707 }
1708
1709 bool is_renderable(VkFormatFeatureFlags flags) {
1710 return SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & flags);
1711 }
1712
1713 bool is_storage(VkFormatFeatureFlags flags) {
1714 return SkToBool(VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT & flags);
1715 }
1716
1717 bool is_transfer_src(VkFormatFeatureFlags flags) {
1718 return SkToBool(VK_FORMAT_FEATURE_TRANSFER_SRC_BIT & flags);
1719 }
1720
1721 bool is_transfer_dst(VkFormatFeatureFlags flags) {
1722 return SkToBool(VK_FORMAT_FEATURE_TRANSFER_DST_BIT & flags);
1723 }
1724 }
1725
1726 void VulkanCaps::FormatInfo::init(const skgpu::VulkanInterface* interface,
1727 const VulkanCaps& caps,
1728 VkPhysicalDevice physDev,
1729 VkFormat format) {
1730 VULKAN_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &fFormatProperties));
1731
1732 if (is_renderable(fFormatProperties.optimalTilingFeatures)) {
1733 // We make all renderable images support being used as input attachment
1734 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1735 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1736 VK_IMAGE_USAGE_SAMPLED_BIT |
1737 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1738 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
1739 this->fSupportedSampleCounts.initSampleCounts(interface, caps, physDev, format, usageFlags);
1740 }
1741
1742 if (caps.supportsHostImageCopy()) {
1743 VkHostImageCopyDevicePerformanceQuery perfQuery = {};
1744 perfQuery.sType = VK_STRUCTURE_TYPE_HOST_IMAGE_COPY_DEVICE_PERFORMANCE_QUERY_EXT;
1745
1746 VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {};
1747 imageFormatInfo.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;
1748 imageFormatInfo.format = format;
1749 imageFormatInfo.type = VK_IMAGE_TYPE_2D;
1750 imageFormatInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
1751 imageFormatInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1752 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_HOST_TRANSFER_BIT;
1753 imageFormatInfo.flags = 0;
1754
1755 VkImageFormatProperties2 imageFormatProperties2 = {};
1756 imageFormatProperties2.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
1757 imageFormatProperties2.pNext = &perfQuery;
1758
1759 if (VULKAN_CALL(interface,
1760 GetPhysicalDeviceImageFormatProperties2(
1761 physDev, &imageFormatInfo, &imageFormatProperties2)) ==
1762 VK_SUCCESS) {
1763 // There are two results returned in `perfQuery`:
1764 //
1765 // * `identicalMemoryLayout` indicates that the added flag does not affect the physical
1766 // layout of the image. We can definitely add the flag in this case.
1767 // * `optimalDeviceAccess` indicates that the added flag _does_ change the physical
1768 // layout of the image, but that according to the driver authors the fallback layout
1769 // is still "pretty good, you won't know the difference".
1770 //
1771 // For now, host image copy is only used if `identicalMemoryLayout` is true, but we
1772 // could consider enabling it when only `optimalDeviceAccess` is true based on
1773 // experimenting on different vendors.
1774 fIsEfficientWithHostImageCopy = perfQuery.identicalMemoryLayout;
1775 }
1776 }
1777 }
1778
1779 bool VulkanCaps::FormatInfo::isTexturable(VkImageTiling imageTiling) const {
1780 switch (imageTiling) {
1781 case VK_IMAGE_TILING_OPTIMAL:
1782 return is_texturable(fFormatProperties.optimalTilingFeatures);
1783 case VK_IMAGE_TILING_LINEAR:
1784 return is_texturable(fFormatProperties.linearTilingFeatures);
1785 default:
1786 return false;
1787 }
1788 SkUNREACHABLE;
1789 }
1790
1791 bool VulkanCaps::FormatInfo::isRenderable(VkImageTiling imageTiling,
1792 SampleCount sampleCount) const {
1793 if (!fSupportedSampleCounts.isSampleCountSupported(sampleCount)) {
1794 return false;
1795 }
1796 switch (imageTiling) {
1797 case VK_IMAGE_TILING_OPTIMAL:
1798 return is_renderable(fFormatProperties.optimalTilingFeatures);
1799 case VK_IMAGE_TILING_LINEAR:
1800 return is_renderable(fFormatProperties.linearTilingFeatures);
1801 default:
1802 return false;
1803 }
1804 SkUNREACHABLE;
1805 }
1806
1807 bool VulkanCaps::FormatInfo::isStorage(VkImageTiling imageTiling) const {
1808 switch (imageTiling) {
1809 case VK_IMAGE_TILING_OPTIMAL:
1810 return is_storage(fFormatProperties.optimalTilingFeatures);
1811 case VK_IMAGE_TILING_LINEAR:
1812 return is_storage(fFormatProperties.linearTilingFeatures);
1813 default:
1814 return false;
1815 }
1816 SkUNREACHABLE;
1817 }
1818
1819 bool VulkanCaps::FormatInfo::isTransferSrc(VkImageTiling imageTiling) const {
1820 switch (imageTiling) {
1821 case VK_IMAGE_TILING_OPTIMAL:
1822 return is_transfer_src(fFormatProperties.optimalTilingFeatures);
1823 case VK_IMAGE_TILING_LINEAR:
1824 return is_transfer_src(fFormatProperties.linearTilingFeatures);
1825 default:
1826 return false;
1827 }
1828 SkUNREACHABLE;
1829 }
1830
1831 bool VulkanCaps::FormatInfo::isTransferDst(VkImageTiling imageTiling) const {
1832 switch (imageTiling) {
1833 case VK_IMAGE_TILING_OPTIMAL:
1834 return is_transfer_dst(fFormatProperties.optimalTilingFeatures);
1835 case VK_IMAGE_TILING_LINEAR:
1836 return is_transfer_dst(fFormatProperties.linearTilingFeatures);
1837 default:
1838 return false;
1839 }
1840 SkUNREACHABLE;
1841 }
1842
1843 bool VulkanCaps::FormatInfo::isEfficientWithHostImageCopy(VkImageTiling imageTiling,
1844 Protected isProtected) const {
1845 if (isProtected == Protected::kYes) {
1846 // Currently, we don't query whether protected textures can be used with host image copy;
1847 // that is unlikely to be the case.
1848 return false;
1849 }
1850
1851 switch (imageTiling) {
1852 case VK_IMAGE_TILING_OPTIMAL:
1853 return fIsEfficientWithHostImageCopy;
1854 case VK_IMAGE_TILING_LINEAR:
1855 // Host-image-copy is always efficient with linear tiling, as it's just a series of
1856 // `memcpy`s.
1857 return true;
1858 default:
1859 break;
1860 }
1861
1862 return false;
1863 }
1864
1865 void VulkanCaps::setColorType(SkColorType colorType, std::initializer_list<VkFormat> formats) {
1866 int idx = static_cast<int>(colorType);
1867 for (auto it = formats.begin(); it != formats.end(); ++it) {
1868 const auto& info = this->getFormatInfo(*it);
1869 for (int i = 0; i < info.fColorTypeInfoCount; ++i) {
1870 if (info.fColorTypeInfos[i].fColorType == colorType) {
1871 fColorTypeToFormatTable[idx] = *it;
1872 return;
1873 }
1874 }
1875 }
1876 }
1877
1878 VkFormat VulkanCaps::getFormatFromColorType(SkColorType colorType) const {
1879 int idx = static_cast<int>(colorType);
1880 return fColorTypeToFormatTable[idx];
1881 }
1882
1883 VulkanCaps::FormatInfo& VulkanCaps::getFormatInfoForInit(VkFormat format) {
1884 static_assert(std::size(kVkFormats) == VulkanCaps::kNumVkFormats,
1885 "Size of VkFormats array must match static value in header");
1886 for (size_t i = 0; i < std::size(kVkFormats); ++i) {
1887 if (kVkFormats[i] == format) {
1888 return fFormatTable[i];
1889 }
1890 }
1891 SkUNREACHABLE;
1892 }
1893
1894 const VulkanCaps::FormatInfo& VulkanCaps::getFormatInfo(VkFormat format) const {
1895 static_assert(std::size(kVkFormats) == VulkanCaps::kNumVkFormats,
1896 "Size of VkFormats array must match static value in header");
1897
1898 static const FormatInfo kInvalidFormat;
1899 if (format == VK_FORMAT_UNDEFINED) {
1900 return kInvalidFormat;
1901 }
1902
1903 for (size_t i = 0; i < std::size(kVkFormats); ++i) {
1904 if (kVkFormats[i] == format) {
1905 return fFormatTable[i];
1906 }
1907 }
1908
1909 return kInvalidFormat;
1910 }
1911
1912 void VulkanCaps::DepthStencilFormatInfo::init(const skgpu::VulkanInterface* interface,
1913 const VulkanCaps& caps,
1914 VkPhysicalDevice physDev,
1915 VkFormat format) {
1916 VULKAN_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &fFormatProperties));
1917 if (this->isDepthStencilSupported()) {
1918 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1919 fSupportedSampleCounts.initSampleCounts(interface, caps, physDev, format, usageFlags);
1920 }
1921 }
1922
1923 bool VulkanCaps::DepthStencilFormatInfo::isDepthStencilSupported() const {
1924 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT &
1925 fFormatProperties.optimalTilingFeatures);
1926 }
1927
1928 VulkanCaps::DepthStencilFormatInfo& VulkanCaps::getDepthStencilFormatInfoForInit(VkFormat format) {
1929 static_assert(std::size(kDepthStencilVkFormats) == VulkanCaps::kNumDepthStencilVkFormats,
1930 "Size of VkFormats array must match static value in header");
1931 for (size_t i = 0; i < std::size(kDepthStencilVkFormats); ++i) {
1932 if (kDepthStencilVkFormats[i] == format) {
1933 return fDepthStencilFormatTable[i];
1934 }
1935 }
1936 SkUNREACHABLE;
1937 }
1938
1939 const VulkanCaps::DepthStencilFormatInfo& VulkanCaps::getDepthStencilFormatInfo(VkFormat format)
1940 const {
1941 static_assert(std::size(kDepthStencilVkFormats) == VulkanCaps::kNumDepthStencilVkFormats,
1942 "Size of VkFormats array must match static value in header");
1943 for (size_t i = 0; i < std::size(kDepthStencilVkFormats); ++i) {
1944 if (kDepthStencilVkFormats[i] == format) {
1945 return fDepthStencilFormatTable[i];
1946 }
1947 }
1948 static const DepthStencilFormatInfo kInvalidFormat;
1949 return kInvalidFormat;
1950 }
1951
1952 SkSpan<const Caps::ColorTypeInfo> VulkanCaps::getColorTypeInfos(
1953 const TextureInfo& textureInfo) const {
1954 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(textureInfo);
1955 VkFormat vkFormat = vkInfo.fFormat;
1956 if (vkFormat == VK_FORMAT_UNDEFINED) {
1957 // If VkFormat is undefined but there is a valid YCbCr conversion associated with the
1958 // texture, then we know we are using an external format and can return color type
1959 // info representative of external format color information.
1960 static const ColorTypeInfo kExternalColorTypeInfos[2] = {
1961 {/*ct=*/kRGBA_8888_SkColorType,
1962 /*transferCt=*/kUnknown_SkColorType,
1963 /*flags=*/0,
1964 /*readSwizzle=*/Swizzle::RGBA(),
1965 /*writeSwizzle=*/{}},
1966 {/*ct=*/kRGB_888x_SkColorType,
1967 /*transferCt=*/kUnknown_SkColorType,
1968 /*flags=*/0,
1969 /*readSwizzle=*/Swizzle::RGB1(),
1970 /*writeSwizzle=*/{}}};
1971 return vkInfo.fYcbcrConversionInfo.isValid() ? SkSpan(kExternalColorTypeInfos, 2)
1972 : SkSpan<const ColorTypeInfo>();
1973 }
1974
1975 const FormatInfo& formatInfo = this->getFormatInfo(vkFormat);
1976 return {formatInfo.fColorTypeInfos.get(), formatInfo.fColorTypeInfoCount};
1977 }
1978
1979 bool VulkanCaps::onIsTexturable(const TextureInfo& texInfo) const {
1980 return texInfo.isValid() &&
1981 this->isTexturable(TextureInfoPriv::Get<VulkanTextureInfo>(texInfo));
1982 }
1983
1984 bool VulkanCaps::isRenderable(const TextureInfo& texInfo) const {
1985 return texInfo.isValid() &&
1986 this->isRenderable(TextureInfoPriv::Get<VulkanTextureInfo>(texInfo));
1987 }
1988
1989 bool VulkanCaps::isStorage(const TextureInfo& texInfo) const {
1990 if (!texInfo.isValid()) {
1991 return false;
1992 }
1993 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(texInfo);
1994
1995 const FormatInfo& info = this->getFormatInfo(vkInfo.fFormat);
1996 return info.isStorage(vkInfo.fImageTiling);
1997 }
1998
1999 bool VulkanCaps::isFormatSupported(VkFormat format) const {
2000 const FormatInfo& formatInfo = this->getFormatInfo(format);
2001
2002 // If Skia claims support for a VkFormat we should have a nonzero fColorTypeInfoCount and valid
2003 // fColorTypeInfos ptr. Therefore, just checking these should be more than sufficient to confirm
2004 // that the format is supported by Skia.
2005 return formatInfo.fColorTypeInfoCount != 0 && formatInfo.fColorTypeInfos != nullptr;
2006 }
2007
2008 bool VulkanCaps::isTexturable(const VulkanTextureInfo& vkInfo) const {
2009 // All images using external formats are required to be able to be sampled per Vulkan spec.
2010 // https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkAndroidHardwareBufferFormatPropertiesANDROID.html#_description
2011 if (vkInfo.fFormat == VK_FORMAT_UNDEFINED && vkInfo.fYcbcrConversionInfo.isValid()) {
2012 return true;
2013 }
2014
2015 // Otherwise, we are working with a known format and can simply reference the format table info.
2016 const FormatInfo& info = this->getFormatInfo(vkInfo.fFormat);
2017 return info.isTexturable(vkInfo.fImageTiling);
2018 }
2019
2020 bool VulkanCaps::isRenderable(const VulkanTextureInfo& vkInfo) const {
2021 const FormatInfo& info = this->getFormatInfo(vkInfo.fFormat);
2022 // All renderable vulkan textures within graphite must also support input attachment usage
2023 return info.isRenderable(vkInfo.fImageTiling, (SampleCount) vkInfo.fSampleCount) &&
2024 SkToBool(vkInfo.fImageUsageFlags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT);
2025 }
2026
2027 bool VulkanCaps::isTransferSrc(const VulkanTextureInfo& vkInfo) const {
2028 const FormatInfo& info = this->getFormatInfo(vkInfo.fFormat);
2029 return info.isTransferSrc(vkInfo.fImageTiling);
2030 }
2031
2032 bool VulkanCaps::isTransferDst(const VulkanTextureInfo& vkInfo) const {
2033 const FormatInfo& info = this->getFormatInfo(vkInfo.fFormat);
2034 return info.isTransferDst(vkInfo.fImageTiling);
2035 }
2036
2037 bool VulkanCaps::supportsWritePixels(const TextureInfo& texInfo) const {
2038 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(texInfo);
2039
2040 // Can't write if it needs a YCbCr sampler
2041 if (VkFormatNeedsYcbcrSampler(vkInfo.fFormat)) {
2042 return false;
2043 }
2044
2045 if (vkInfo.fSampleCount > SampleCount::k1) {
2046 return false;
2047 }
2048
2049 if (!SkToBool(vkInfo.fImageUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
2050 return false;
2051 }
2052
2053 return true;
2054 }
2055
2056 bool VulkanCaps::supportsReadPixels(const TextureInfo& texInfo) const {
2057 if (texInfo.isProtected() == Protected::kYes) {
2058 return false;
2059 }
2060
2061 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(texInfo);
2062
2063 // Can't read if it needs a YCbCr sampler
2064 if (VkFormatNeedsYcbcrSampler(vkInfo.fFormat)) {
2065 return false;
2066 }
2067
2068 if (VkFormatIsCompressed(vkInfo.fFormat)) {
2069 return false;
2070 }
2071
2072 if (vkInfo.fSampleCount > SampleCount::k1) {
2073 return false;
2074 }
2075
2076 if (!SkToBool(vkInfo.fImageUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) {
2077 return false;
2078 }
2079
2080 return true;
2081 }
2082
2083 bool VulkanCaps::msaaTextureRenderToSingleSampledSupport(const TextureInfo& info) const {
2084 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info);
2085 return vkInfo.fFlags & VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
2086 }
2087
2088 // 4 uint32s for the render step id, paint id, compatible render pass description, and write
2089 // swizzle.
2090 static constexpr uint16_t kPipelineKeyData32Count = 4;
2091
2092 static constexpr int kPipelineKeyRenderStepIDIndex = 0;
2093 static constexpr int kPipelineKeyPaintParamsIDIndex = 1;
2094 static constexpr int kPipelineKeyRenderPassDescIndex = 2;
2095 static constexpr int kPipelineKeyWriteSwizzleIndex = 3;
2096
2097 UniqueKey VulkanCaps::makeGraphicsPipelineKey(const GraphicsPipelineDesc& pipelineDesc,
2098 const RenderPassDesc& renderPassDesc) const {
2099 UniqueKey pipelineKey;
2100 {
2101 UniqueKey::Builder builder(
2102 &pipelineKey, get_pipeline_domain(), kPipelineKeyData32Count, "GraphicsPipeline");
2103
2104 // Add GraphicsPipelineDesc information
2105 builder[kPipelineKeyRenderStepIDIndex] = static_cast<uint32_t>(pipelineDesc.renderStepID());
2106 builder[kPipelineKeyPaintParamsIDIndex] = pipelineDesc.paintParamsID().asUInt();
2107 // Add RenderPassDesc information
2108 builder[kPipelineKeyRenderPassDescIndex] = VulkanRenderPass::GetRenderPassKey(
2109 renderPassDesc, /*compatibleForPipelineKey=*/true);
2110 // Add RenderPass info relevant for pipeline creation that's not captured in RenderPass keys
2111 builder[kPipelineKeyWriteSwizzleIndex] = renderPassDesc.fWriteSwizzle.asKey();
2112
2113 builder.finish();
2114 }
2115
2116 return pipelineKey;
2117 }
2118
2119 bool VulkanCaps::extractGraphicsDescs(const UniqueKey& key,
2120 GraphicsPipelineDesc* pipelineDesc,
2121 RenderPassDesc* renderPassDesc,
2122 const RendererProvider* rendererProvider) const {
2123 SkASSERT(key.domain() == get_pipeline_domain());
2124 SkASSERT(key.dataSize() == 4 * kPipelineKeyData32Count);
2125
2126 const uint32_t* rawKeyData = key.data();
2127
2128 SkASSERT(RenderStep::IsValidRenderStepID(rawKeyData[kPipelineKeyRenderStepIDIndex]));
2129 RenderStep::RenderStepID renderStepID =
2130 static_cast<RenderStep::RenderStepID>(rawKeyData[kPipelineKeyRenderStepIDIndex]);
2131
2132 *pipelineDesc =
2133 GraphicsPipelineDesc(renderStepID,
2134 UniquePaintParamsID(rawKeyData[kPipelineKeyPaintParamsIDIndex]));
2135
2136 const uint32_t rpDescBits = rawKeyData[kPipelineKeyRenderPassDescIndex];
2137 VulkanRenderPass::ExtractRenderPassDesc(
2138 rpDescBits,
2139 SwizzleCtorAccessor::Make(rawKeyData[kPipelineKeyWriteSwizzleIndex]),
2140 this->getDstReadStrategy(),
2141 renderPassDesc);
2142
2143 return true;
2144 }
2145
2146 void VulkanCaps::buildKeyForTexture(SkISize dimensions,
2147 const TextureInfo& info,
2148 ResourceType type,
2149 GraphiteResourceKey* key) const {
2150 SkASSERT(!dimensions.isEmpty());
2151
2152 const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info);
2153 // We expect that the VkFormat enum is at most a 32-bit value.
2154 static_assert(VK_FORMAT_MAX_ENUM == 0x7FFFFFFF);
2155 // We should either be using a known VkFormat or have a valid ycbcr conversion.
2156 SkASSERT(vkInfo.fFormat != VK_FORMAT_UNDEFINED || vkInfo.fYcbcrConversionInfo.isValid());
2157
2158 uint32_t format = static_cast<uint32_t>(vkInfo.fFormat);
2159 uint32_t samples = SamplesToKey(info.sampleCount());
2160 // We don't have to key the number of mip levels because it is inherit in the combination of
2161 // isMipped and dimensions.
2162 bool isMipped = info.mipmapped() == Mipmapped::kYes;
2163 Protected isProtected = info.isProtected();
2164
2165 // Confirm all the below parts of the key can fit in a single uint32_t. The sum of the shift
2166 // amounts in the asserts must be less than or equal to 32. vkInfo.fFlags and
2167 // vkInfo.fImageUsageFlags will go into their own 32-bit block.
2168 SkASSERT(samples < (1u << 3)); // sample key is first 3 bits
2169 SkASSERT(static_cast<uint32_t>(isMipped) < (1u << 1)); // isMapped is 4th bit
2170 SkASSERT(static_cast<uint32_t>(isProtected) < (1u << 1)); // isProtected is 5th bit
2171 SkASSERT(vkInfo.fImageTiling < (1u << 1)); // imageTiling is 6th bit
2172 SkASSERT(vkInfo.fSharingMode < (1u << 1)); // sharingMode is 7th bit
2173 SkASSERT(vkInfo.fAspectMask < (1u << 11)); // aspectMask is bits 8 - 19
2174
2175 // We need two uint32_ts for dimensions and 3 for miscellaneous information.
2176 static constexpr uint16_t kNum32DimensionDataCnt = 2;
2177 static constexpr uint16_t kNum32MiscDataCnt = 3;
2178 // Non-YCbCr formats need 1 int for format.
2179 // YCbCr conversion needs 1 int for non-format flags, and a 64-bit format (external or regular).
2180 static constexpr uint16_t kNum32FormatDataCntNoYcbcr = 1;
2181 static constexpr uint16_t kNum32FormatDataCntYcbcr = 3;
2182
2183 const VulkanYcbcrConversionInfo& ycbcrInfo = vkInfo.fYcbcrConversionInfo;
2184 const uint16_t num32DataCnt =
2185 kNum32DimensionDataCnt + kNum32MiscDataCnt +
2186 (ycbcrInfo.isValid() ? kNum32FormatDataCntYcbcr : kNum32FormatDataCntNoYcbcr);
2187
2188 GraphiteResourceKey::Builder builder(key, type, num32DataCnt);
2189
2190 int i = 0;
2191 builder[i++] = dimensions.width();
2192 builder[i++] = dimensions.height();
2193
2194 if (ycbcrInfo.isValid()) {
2195 SkASSERT(ycbcrInfo.format() != VK_FORMAT_UNDEFINED || ycbcrInfo.hasExternalFormat());
2196 ImmutableSamplerInfo packedInfo = VulkanYcbcrConversion::ToImmutableSamplerInfo(ycbcrInfo);
2197
2198 builder[i++] = packedInfo.fNonFormatYcbcrConversionInfo;
2199 builder[i++] = (uint32_t) packedInfo.fFormat;
2200 builder[i++] = (uint32_t) (packedInfo.fFormat >> 32);
2201 } else {
2202 builder[i++] = format;
2203 }
2204
2205 builder[i++] = static_cast<uint32_t>(vkInfo.fFlags);
2206 builder[i++] = static_cast<uint32_t>(vkInfo.fImageUsageFlags);
2207 builder[i++] = (samples << 0) |
2208 (static_cast<uint32_t>(isMipped) << kNumSampleKeyBits) |
2209 (static_cast<uint32_t>(isProtected) << 4) |
2210 (static_cast<uint32_t>(vkInfo.fImageTiling) << 5) |
2211 (static_cast<uint32_t>(vkInfo.fSharingMode) << 6) |
2212 (static_cast<uint32_t>(vkInfo.fAspectMask) << 7);
2213 SkASSERT(i == num32DataCnt);
2214 }
2215
2216 DstReadStrategy VulkanCaps::getDstReadStrategy() const {
2217 // We know the graphite Vulkan backend does not support frame buffer fetch, so make sure it is
2218 // not marked as supported and skip checking for it.
2219 SkASSERT(!this->shaderCaps()->fFBFetchSupport);
2220
2221 // All render target textures are expected to have VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT.
2222 return DstReadStrategy::kReadFromInput;
2223 }
2224
2225 ImmutableSamplerInfo VulkanCaps::getImmutableSamplerInfo(const TextureInfo& textureInfo) const {
2226 const skgpu::VulkanYcbcrConversionInfo& ycbcrConversionInfo =
2227 TextureInfoPriv::Get<VulkanTextureInfo>(textureInfo).fYcbcrConversionInfo;
2228
2229 if (ycbcrConversionInfo.isValid()) {
2230 return VulkanYcbcrConversion::ToImmutableSamplerInfo(ycbcrConversionInfo);
2231 }
2232
2233 // If the YCbCr conversion for the TextureInfo is invalid, then return a default
2234 // ImmutableSamplerInfo struct.
2235 return {};
2236 }
2237
2238 static constexpr const char* vk_chromafilter_to_str(VkFilter f) {
2239 switch (f) {
2240 case VK_FILTER_NEAREST: return "nearest";
2241 case VK_FILTER_LINEAR: return "linear";
2242 case VK_FILTER_CUBIC_EXT: return "cubic";
2243 default: return "unknown";
2244 }
2245 SkUNREACHABLE;
2246 }
2247
2248 std::string VulkanCaps::toString(const ImmutableSamplerInfo& immutableSamplerInfo) const {
2249 const skgpu::VulkanYcbcrConversionInfo info =
2250 VulkanYcbcrConversion::FromImmutableSamplerInfo(immutableSamplerInfo);
2251 if (!info.isValid()) {
2252 return "";
2253 }
2254
2255 std::string result;
2256
2257 if (info.hasExternalFormat()) {
2258 result += 'x';
2259 result += std::to_string(info.externalFormat());
2260 } else {
2261 result += std::to_string(info.format());
2262 }
2263
2264 result += " ";
2265 result += VkModelToStr(info.model());
2266 result += "+";
2267 result += VkRangeToStr(info.range());
2268 result += info.xChromaOffset() ? " mid" : " cos"; // midpoint or cosited-even
2269 result += info.yChromaOffset() ? " mid " : " cos "; // midpoint or cosited-even
2270 result += vk_chromafilter_to_str(info.chromaFilter());
2271 result += info.forceExplicitReconstruction() ? " T " : " F ";
2272 result += VkSwizzleToStr(info.components().r, 'r');
2273 result += VkSwizzleToStr(info.components().g, 'g');
2274 result += VkSwizzleToStr(info.components().b, 'b');
2275 result += VkSwizzleToStr(info.components().a, 'a');
2276 result += " cf";
2277 result += info.samplerFilterMustMatchChromaFilter() ? '1' : '0';
2278 result += "lf";
2279 result += info.supportsLinearFilter() ? '1' : '0';
2280 return result;
2281 }
2282
2283 } // namespace skgpu::graphite