blob: 09798d904f1c92eb2ddcf564d37b82d4020290d4 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrBackendSurface.h"
9#include "include/gpu/GrRenderTarget.h"
10#include "include/gpu/vk/GrVkBackendContext.h"
11#include "include/gpu/vk/GrVkExtensions.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrShaderCaps.h"
Brian Osman7fbb3632019-07-19 11:38:57 -040014#include "src/gpu/GrUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/SkGr.h"
16#include "src/gpu/vk/GrVkCaps.h"
17#include "src/gpu/vk/GrVkInterface.h"
18#include "src/gpu/vk/GrVkTexture.h"
19#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040021#ifdef SK_BUILD_FOR_ANDROID
22#include <sys/system_properties.h>
23#endif
24
Greg Daniel164a9f02016-02-22 09:56:40 -050025GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -040026 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Greg Daniel41f0e282019-01-28 13:15:05 -050027 uint32_t instanceVersion, uint32_t physicalDeviceVersion,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040028 const GrVkExtensions& extensions, GrProtected isProtected)
29 : INHERITED(contextOptions) {
Greg Daniel164a9f02016-02-22 09:56:40 -050030 /**************************************************************************
Brian Salomonf7232642018-09-19 08:58:08 -040031 * GrCaps fields
32 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070033 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070034 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070035 fNPOTTextureTileSupport = true; // always available in Vulkan
Greg Daniel164a9f02016-02-22 09:56:40 -050036 fReuseScratchTextures = true; //TODO: figure this out
37 fGpuTracingSupport = false; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050038 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060039 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050040
Brian Salomon9ff5acb2019-05-08 09:04:47 -040041 fSemaphoreSupport = true; // always available in Vulkan
jvanverth84741b32016-09-30 08:39:02 -070042 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050043 fCrossContextTextureSupport = true;
Brian Osman499bf1a2018-09-17 11:32:42 -040044 fHalfFloatVertexAttributeSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050045
Brian Salomon1047a492019-07-02 12:25:21 -040046 // We always copy in/out of a transfer buffer so it's trivial to support row bytes.
47 fReadPixelsRowBytesSupport = true;
48 fWritePixelsRowBytesSupport = true;
49
Brian Salomone05ba5a2019-04-08 11:59:07 -040050 fTransferBufferSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050051
52 fMaxRenderTargetSize = 4096; // minimum required by spec
53 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050054
Brian Salomonf7232642018-09-19 08:58:08 -040055 fDynamicStateArrayGeometryProcessorTextureSupport = true;
56
Brian Salomon94efbf52016-11-29 13:43:05 -050057 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050058
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040059 this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions,
60 isProtected);
Greg Daniel164a9f02016-02-22 09:56:40 -050061}
62
Greg Daniel5c7b5412019-05-10 11:39:55 -040063static int get_compatible_format_class(GrPixelConfig config) {
64 switch (config) {
65 case kAlpha_8_GrPixelConfig:
66 case kAlpha_8_as_Red_GrPixelConfig:
67 case kGray_8_GrPixelConfig:
68 case kGray_8_as_Red_GrPixelConfig:
69 return 1;
70 case kRGB_565_GrPixelConfig:
71 case kRGBA_4444_GrPixelConfig:
72 case kRG_88_GrPixelConfig:
73 case kAlpha_half_GrPixelConfig:
74 case kAlpha_half_as_Red_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -040075 case kR_16_GrPixelConfig:
Greg Daniel5c7b5412019-05-10 11:39:55 -040076 return 2;
77 case kRGB_888_GrPixelConfig:
78 return 3;
79 case kRGBA_8888_GrPixelConfig:
80 case kRGB_888X_GrPixelConfig:
81 case kBGRA_8888_GrPixelConfig:
82 case kSRGBA_8888_GrPixelConfig:
Greg Daniel5c7b5412019-05-10 11:39:55 -040083 case kRGBA_1010102_GrPixelConfig:
Robert Phillips66a46032019-06-18 08:00:42 -040084 case kRG_1616_GrPixelConfig:
Greg Daniel5c7b5412019-05-10 11:39:55 -040085 return 4;
86 case kRGBA_half_GrPixelConfig:
87 case kRGBA_half_Clamped_GrPixelConfig:
Greg Daniel5c7b5412019-05-10 11:39:55 -040088 return 5;
89 case kRGBA_float_GrPixelConfig:
90 return 6;
91 case kRGB_ETC1_GrPixelConfig:
92 return 7;
93 case kUnknown_GrPixelConfig:
94 case kAlpha_8_as_Alpha_GrPixelConfig:
95 case kGray_8_as_Lum_GrPixelConfig:
Robert Phillipsebab03f2019-07-22 08:48:18 -040096 case kAlpha_half_as_Lum_GrPixelConfig:
Greg Daniel5c7b5412019-05-10 11:39:55 -040097 SK_ABORT("Unsupported Vulkan pixel config");
98 return 0;
Robert Phillips66a46032019-06-18 08:00:42 -040099
100 // Experimental (for Y416 and mutant P016/P010)
101 case kRGBA_16161616_GrPixelConfig:
102 return 8;
103 case kRG_half_GrPixelConfig:
Robert Phillipsfe18de52019-06-06 17:21:50 -0400104 return 4;
Greg Daniel5c7b5412019-05-10 11:39:55 -0400105 }
106 SK_ABORT("Invalid pixel config");
107 return 0;
108}
109
Greg Daniel46cfbc62019-06-07 11:43:30 -0400110bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, bool dstHasYcbcr,
111 GrPixelConfig srcConfig, int srcSampleCnt, bool srcHasYcbcr) const {
Greg Daniel25af6712018-04-25 10:44:38 -0400112 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
113 return false;
114 }
115
Greg Daniela51e93c2019-03-25 12:30:45 -0400116 if (dstHasYcbcr || srcHasYcbcr) {
117 return false;
118 }
119
Greg Daniel25af6712018-04-25 10:44:38 -0400120 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
121 // as image usage flags.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400122 if (get_compatible_format_class(srcConfig) != get_compatible_format_class(dstConfig)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400123 return false;
124 }
125
Greg Daniel25af6712018-04-25 10:44:38 -0400126 return true;
127}
128
129bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
Greg Daniela51e93c2019-03-25 12:30:45 -0400130 bool dstHasYcbcr, GrPixelConfig srcConfig, int srcSampleCnt,
131 bool srcIsLinear, bool srcHasYcbcr) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400132
133 VkFormat dstFormat;
134 SkAssertResult(GrPixelConfigToVkFormat(dstConfig, &dstFormat));
135 VkFormat srcFormat;
136 SkAssertResult(GrPixelConfigToVkFormat(srcConfig, &srcFormat));
Greg Daniel25af6712018-04-25 10:44:38 -0400137 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
138 // as image usage flags.
Greg Danielcaa795f2019-05-14 11:54:25 -0400139 if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
140 !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400141 return false;
142 }
143
Greg Daniel25af6712018-04-25 10:44:38 -0400144 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
145 // resolved msaa though.
146 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
147 return false;
148 }
149
Greg Daniela51e93c2019-03-25 12:30:45 -0400150 if (dstHasYcbcr || srcHasYcbcr) {
151 return false;
152 }
153
Greg Daniel25af6712018-04-25 10:44:38 -0400154 return true;
155}
156
Greg Daniel46cfbc62019-06-07 11:43:30 -0400157bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt, bool dstHasYcbcr,
158 GrPixelConfig srcConfig, int srcSampleCnt, bool srcHasYcbcr) const {
Greg Daniel25af6712018-04-25 10:44:38 -0400159 // The src surface must be multisampled.
160 if (srcSampleCnt <= 1) {
161 return false;
162 }
163
164 // The dst must not be multisampled.
165 if (dstSampleCnt > 1) {
166 return false;
167 }
168
169 // Surfaces must have the same format.
170 if (dstConfig != srcConfig) {
171 return false;
172 }
173
Greg Daniela51e93c2019-03-25 12:30:45 -0400174 if (dstHasYcbcr || srcHasYcbcr) {
175 return false;
176 }
177
Greg Daniel25af6712018-04-25 10:44:38 -0400178 return true;
179}
180
Brian Salomonc67c31c2018-12-06 10:00:03 -0500181bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
182 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400183 if (src->isProtected() && !dst->isProtected()) {
184 return false;
185 }
186
Greg Daniel25af6712018-04-25 10:44:38 -0400187 GrPixelConfig dstConfig = dst->config();
188 GrPixelConfig srcConfig = src->config();
189
190 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
191 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
192 // tracking of external or rectangle textures in GL? For now we don't create linear textures
193 // internally, and I don't believe anyone is wrapping them.
194 bool srcIsLinear = false;
195 bool dstIsLinear = false;
196
197 int dstSampleCnt = 0;
198 int srcSampleCnt = 0;
199 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
Greg Danielbe7fc462019-01-03 16:40:42 -0500200 // Copying to or from render targets that wrap a secondary command buffer is not allowed
201 // since they would require us to know the VkImage, which we don't have, as well as need us
202 // to stop and start the VkRenderPass which we don't have access to.
203 if (rtProxy->wrapsVkSecondaryCB()) {
204 return false;
205 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206 dstSampleCnt = rtProxy->numSamples();
Greg Daniel25af6712018-04-25 10:44:38 -0400207 }
208 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
Greg Danielbe7fc462019-01-03 16:40:42 -0500209 // Copying to or from render targets that wrap a secondary command buffer is not allowed
210 // since they would require us to know the VkImage, which we don't have, as well as need us
211 // to stop and start the VkRenderPass which we don't have access to.
212 if (rtProxy->wrapsVkSecondaryCB()) {
213 return false;
214 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600215 srcSampleCnt = rtProxy->numSamples();
Greg Daniel25af6712018-04-25 10:44:38 -0400216 }
217 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
218 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
219
Greg Daniela51e93c2019-03-25 12:30:45 -0400220 bool dstHasYcbcr = false;
221 if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
222 if (ycbcr->isValid()) {
223 dstHasYcbcr = true;
224 }
225 }
226
227 bool srcHasYcbcr = false;
228 if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
229 if (ycbcr->isValid()) {
230 srcHasYcbcr = true;
231 }
232 }
233
Greg Daniel46cfbc62019-06-07 11:43:30 -0400234 return this->canCopyImage(dstConfig, dstSampleCnt, dstHasYcbcr,
235 srcConfig, srcSampleCnt, srcHasYcbcr) ||
Greg Daniela51e93c2019-03-25 12:30:45 -0400236 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear, dstHasYcbcr,
237 srcConfig, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
Greg Daniel46cfbc62019-06-07 11:43:30 -0400238 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstHasYcbcr,
239 srcConfig, srcSampleCnt, srcHasYcbcr);
Greg Daniel25af6712018-04-25 10:44:38 -0400240}
241
Greg Daniel7e000222018-12-03 10:08:21 -0500242template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
243 VkStructureType type) {
244 // All Vulkan structs that could be part of the features chain will start with the
245 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
246 // so we can get access to the pNext for the next struct.
247 struct CommonVulkanHeader {
248 VkStructureType sType;
249 void* pNext;
250 };
251
252 void* pNext = features.pNext;
253 while (pNext) {
254 CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
255 if (header->sType == type) {
256 return static_cast<T*>(pNext);
257 }
258 pNext = header->pNext;
259 }
260 return nullptr;
261}
262
Greg Daniel164a9f02016-02-22 09:56:40 -0500263void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -0400264 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400265 uint32_t physicalDeviceVersion, const GrVkExtensions& extensions,
266 GrProtected isProtected) {
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000267 VkPhysicalDeviceProperties properties;
268 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -0800269
egdanield5e3b9e2016-03-08 12:19:54 -0800270 VkPhysicalDeviceMemoryProperties memoryProperties;
271 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
272
Greg Daniel41f0e282019-01-28 13:15:05 -0500273 SkASSERT(physicalDeviceVersion <= properties.apiVersion);
Greg Danielc0b03d82018-08-03 14:41:15 -0400274
Greg Danielcb324152019-02-25 11:36:53 -0500275 if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
276 fSupportsSwapchain = true;
277 }
278
Greg Danielc0b03d82018-08-03 14:41:15 -0400279 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
280 extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
281 fSupportsPhysicalDeviceProperties2 = true;
282 }
283
284 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
285 extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
286 fSupportsMemoryRequirements2 = true;
287 }
288
289 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Daniel637c06a2018-09-12 09:44:25 -0400290 extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
291 fSupportsBindMemory2 = true;
292 }
293
294 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Danielc0b03d82018-08-03 14:41:15 -0400295 extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
296 fSupportsMaintenance1 = true;
297 }
298
299 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
300 extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
301 fSupportsMaintenance2 = true;
302 }
303
304 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
305 extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
306 fSupportsMaintenance3 = true;
307 }
308
Greg Daniela9979d12018-08-27 15:56:46 -0400309 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Daniel637c06a2018-09-12 09:44:25 -0400310 (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
Greg Daniela9979d12018-08-27 15:56:46 -0400311 this->supportsMemoryRequirements2())) {
312 fSupportsDedicatedAllocation = true;
313 }
314
315 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
316 (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
317 this->supportsPhysicalDeviceProperties2() &&
318 extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
319 this->supportsDedicatedAllocation())) {
320 fSupportsExternalMemory = true;
321 }
322
323#ifdef SK_BUILD_FOR_ANDROID
Greg Daniel637c06a2018-09-12 09:44:25 -0400324 // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
325 // do we don't explicitly require it here even the spec says it is required.
Greg Daniela9979d12018-08-27 15:56:46 -0400326 if (extensions.hasExtension(
Greg Daniel637c06a2018-09-12 09:44:25 -0400327 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
328 /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
329 this->supportsExternalMemory() &&
330 this->supportsBindMemory2()) {
Greg Daniela9979d12018-08-27 15:56:46 -0400331 fSupportsAndroidHWBExternalMemory = true;
Greg Daniel637c06a2018-09-12 09:44:25 -0400332 fSupportsAHardwareBufferImages = true;
Greg Daniela9979d12018-08-27 15:56:46 -0400333 }
334#endif
335
Greg Daniel7e000222018-12-03 10:08:21 -0500336 auto ycbcrFeatures =
337 get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
338 features,
339 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
340 if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
341 fSupportsAndroidHWBExternalMemory &&
342 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
343 (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
344 this->supportsMaintenance1() &&
345 this->supportsBindMemory2() &&
346 this->supportsMemoryRequirements2() &&
347 this->supportsPhysicalDeviceProperties2()))) {
348 fSupportsYcbcrConversion = true;
349 }
350 // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
351 // will return a key of 0.
352 fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
353
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400354 if ((isProtected == GrProtected::kYes) &&
355 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0))) {
356 fSupportsProtectedMemory = true;
357 fAvoidUpdateBuffers = true;
358 fShouldAlwaysUseDedicatedImageMemory = true;
359 }
360
Greg Daniel313c6952018-08-08 09:24:08 -0400361 this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
Greg Daniel36443602018-08-02 12:51:52 -0400362 this->initShaderCaps(properties, features);
Greg Danielf3b11622018-03-01 15:01:27 -0500363
364 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
365#if defined(SK_CPU_X86)
366 // We need to do this before initing the config table since it uses fSRGBSupport
367 if (kImagination_VkVendor == properties.vendorID) {
368 fSRGBSupport = false;
369 }
370#endif
371 }
372
Chris Dalton8e738a22018-10-05 16:41:44 -0600373 if (kQualcomm_VkVendor == properties.vendorID) {
374 // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
375 // scissored clear.
376 // On NVIDIA and Intel, the discard load followed by clear is faster.
377 // TODO: Evaluate on ARM, Imagination, and ATI.
378 fPreferFullscreenClears = true;
379 }
380
Greg Daniel44e69f92019-03-20 11:18:25 -0400381 if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
382 // On Qualcomm and ARM mapping a gpu buffer and doing both reads and writes to it is slow.
383 // Thus for index and vertex buffers we will force to use a cpu side buffer and then copy
384 // the whole buffer up to the gpu.
Greg Daniel78e6a4c2019-03-19 14:13:36 -0400385 fBufferMapThreshold = SK_MaxS32;
386 }
387
388 if (kQualcomm_VkVendor == properties.vendorID) {
389 // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
390 // even for small sizes.
391 fAvoidUpdateBuffers = true;
392 }
393
Chris Dalton0dffbab2019-03-27 13:08:50 -0600394 if (kARM_VkVendor == properties.vendorID) {
395 // ARM seems to do better with more fine triangles as opposed to using the sample mask.
396 // (At least in our current round rect op.)
397 fPreferTrianglesOverSampleMask = true;
398 }
Greg Daniel78e6a4c2019-03-19 14:13:36 -0400399
Greg Danielcaa795f2019-05-14 11:54:25 -0400400 this->initFormatTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700401 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500402
Greg Daniel691f5e72018-02-28 14:21:34 -0500403 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
404 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700405 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500406
Greg Daniel691f5e72018-02-28 14:21:34 -0500407 this->applyOptionsOverrides(contextOptions);
408 fShaderCaps->applyOptionsOverrides(contextOptions);
409}
410
411void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700412 if (kQualcomm_VkVendor == properties.vendorID) {
413 fMustDoCopiesFromOrigin = true;
Brian Salomona585fe92019-04-09 14:57:00 -0400414 // Transfer doesn't support this workaround.
415 fTransferBufferSupport = false;
egdaniel6fa0a912016-09-12 11:51:29 -0700416 }
417
Greg Daniel80a08dd2017-01-20 10:45:49 -0500418#if defined(SK_BUILD_FOR_WIN)
Greg Daniel900e5c82018-08-28 10:59:24 -0400419 if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
Greg Daniel80a08dd2017-01-20 10:45:49 -0500420 fMustSleepOnTearDown = true;
421 }
422#elif defined(SK_BUILD_FOR_ANDROID)
423 if (kImagination_VkVendor == properties.vendorID) {
424 fMustSleepOnTearDown = true;
425 }
426#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500427
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400428#if defined(SK_BUILD_FOR_ANDROID)
429 // Protected memory features have problems in Android P and earlier.
430 if (fSupportsProtectedMemory && (kQualcomm_VkVendor == properties.vendorID)) {
431 char androidAPIVersion[PROP_VALUE_MAX];
432 int strLength = __system_property_get("ro.build.version.sdk", androidAPIVersion);
433 if (strLength == 0 || atoi(androidAPIVersion) <= 28) {
434 fSupportsProtectedMemory = false;
435 }
436 }
437#endif
438
Greg Danielbce5eb92018-03-01 13:13:44 -0500439 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
440 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
441 if (kAMD_VkVendor == properties.vendorID) {
442 fNewCBOnPipelineChange = true;
443 }
444
Greg Danielddc0c602018-06-18 11:26:30 -0400445 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
446 if (kARM_VkVendor == properties.vendorID) {
447 fShouldAlwaysUseDedicatedImageMemory = true;
448 }
449
Greg Danielbce5eb92018-03-01 13:13:44 -0500450 ////////////////////////////////////////////////////////////////////////////
451 // GrCaps workarounds
452 ////////////////////////////////////////////////////////////////////////////
453
454 if (kARM_VkVendor == properties.vendorID) {
455 fInstanceAttribSupport = false;
Greg Daniel4374e962018-09-28 15:09:47 -0400456 fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
Greg Danielbce5eb92018-03-01 13:13:44 -0500457 }
458
459 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
460 if (kAMD_VkVendor == properties.vendorID) {
461 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
462 }
463
Greg Danielbce5eb92018-03-01 13:13:44 -0500464 ////////////////////////////////////////////////////////////////////////////
465 // GrShaderCaps workarounds
466 ////////////////////////////////////////////////////////////////////////////
467
Greg Danielbce5eb92018-03-01 13:13:44 -0500468 if (kImagination_VkVendor == properties.vendorID) {
469 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
470 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500471}
472
473int get_max_sample_count(VkSampleCountFlags flags) {
474 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
475 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
476 return 0;
477 }
478 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
479 return 2;
480 }
481 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
482 return 4;
483 }
484 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
485 return 8;
486 }
487 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
488 return 16;
489 }
490 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
491 return 32;
492 }
493 return 64;
494}
495
Greg Daniel313c6952018-08-08 09:24:08 -0400496void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
497 VkPhysicalDevice physDev,
498 const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700499 const VkPhysicalDeviceMemoryProperties& memoryProperties,
Greg Daniel313c6952018-08-08 09:24:08 -0400500 const VkPhysicalDeviceFeatures2& features,
501 const GrVkExtensions& extensions) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400502 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
503 // need for us ever to support that amount, and it makes tests which tests all the vertex
504 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
505 // we ever find that need.
506 static const uint32_t kMaxVertexAttributes = 64;
507 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400508
egdanield5e3b9e2016-03-08 12:19:54 -0800509 // We could actually query and get a max size for each config, however maxImageDimension2D will
510 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700511 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
512 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
Adrienne Walker724afe82018-05-15 11:36:26 -0700513 if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
514 fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
515 }
516 // Our render targets are always created with textures as the color
517 // attachment, hence this min:
518 fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
egdanield5e3b9e2016-03-08 12:19:54 -0800519
Chris Dalton2612bae2018-02-22 13:41:37 -0700520 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
521 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
522
egdanield5e3b9e2016-03-08 12:19:54 -0800523 // Assuming since we will always map in the end to upload the data we might as well just map
524 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700525 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800526
Brian Salomon105d7c22019-04-16 13:46:14 -0400527 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
egdanield5e3b9e2016-03-08 12:19:54 -0800528
egdanield5e3b9e2016-03-08 12:19:54 -0800529 fOversizedStencilSupport = true;
Greg Daniel313c6952018-08-08 09:24:08 -0400530
531 if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
532 this->supportsPhysicalDeviceProperties2()) {
533
534 VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
535 blendProps.sType =
536 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
537 blendProps.pNext = nullptr;
538
539 VkPhysicalDeviceProperties2 props;
540 props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
541 props.pNext = &blendProps;
542
543 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
544
545 if (blendProps.advancedBlendAllOperations == VK_TRUE) {
546 fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
547
548 auto blendFeatures =
549 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
550 features,
551 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
552 if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
553 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
554 } else {
555 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
556 // require us to support self dependencies in our render passes.
557 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
558 }
559 }
560 }
egdanield5e3b9e2016-03-08 12:19:54 -0800561}
562
Greg Daniel36443602018-08-02 12:51:52 -0400563void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
Greg Daniela0651ac2018-08-08 09:23:18 -0400564 const VkPhysicalDeviceFeatures2& features) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500565 GrShaderCaps* shaderCaps = fShaderCaps.get();
566 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700567
egdanield5e3b9e2016-03-08 12:19:54 -0800568 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500569 shaderCaps->fUsesPrecisionModifiers = true;
570 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700571 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
572 // to be true with Vulkan as well.
573 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800574
575 // GrShaderCaps
576
Brian Salomon1edc5b92016-11-29 13:43:46 -0500577 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600578
Ethan Nicholas6ac8d362019-01-22 21:43:55 +0000579 // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
580 // shaderCaps->fGeometryShaderSupport =
581 // shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
egdanield632bb42016-03-30 12:06:48 -0700582
Greg Daniela0651ac2018-08-08 09:23:18 -0400583 shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
egdanield632bb42016-03-30 12:06:48 -0700584
Brian Salomon1edc5b92016-11-29 13:43:46 -0500585 shaderCaps->fIntegerSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600586 shaderCaps->fVertexIDSupport = true;
Chris Dalton7c7ff032018-03-28 20:09:58 -0600587 shaderCaps->fFPManipulationSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800588
cdaltona6b92ad2016-04-11 12:03:08 -0700589 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700590 shaderCaps->fFloatIs32Bits = true;
591 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700592
Brian Salomon1edc5b92016-11-29 13:43:46 -0500593 shaderCaps->fMaxFragmentSamplers = SkTMin(
594 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
595 properties.limits.maxPerStageDescriptorSamplers),
596 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500597}
598
egdaniel8f1dcaa2016-04-01 10:10:45 -0700599bool stencil_format_supported(const GrVkInterface* interface,
600 VkPhysicalDevice physDev,
601 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500602 VkFormatProperties props;
603 memset(&props, 0, sizeof(VkFormatProperties));
604 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700605 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500606}
607
egdaniel8f1dcaa2016-04-01 10:10:45 -0700608void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
609 // List of legal stencil formats (though perhaps not supported on
610 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700611 // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
egdaniel8f1dcaa2016-04-01 10:10:45 -0700612 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500613 static const StencilFormat
614 // internal Format stencil bits total bits packed?
615 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700616 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
617 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500618
egdaniel8f1dcaa2016-04-01 10:10:45 -0700619 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400620 fPreferredStencilFormat = gS8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700621 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400622 fPreferredStencilFormat = gD24S8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700623 } else {
624 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400625 fPreferredStencilFormat = gD32S8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700626 }
627}
628
Greg Danielcaa795f2019-05-14 11:54:25 -0400629static bool format_is_srgb(VkFormat format) {
Robert Phillipsf209e882019-06-25 15:59:50 -0400630 SkASSERT(GrVkFormatIsSupported(format));
631
Greg Danielcaa795f2019-05-14 11:54:25 -0400632 switch (format) {
633 case VK_FORMAT_R8G8B8A8_SRGB:
Greg Danielcaa795f2019-05-14 11:54:25 -0400634 return true;
Greg Danielcaa795f2019-05-14 11:54:25 -0400635 default:
Greg Danielcaa795f2019-05-14 11:54:25 -0400636 return false;
637 }
638}
639
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400640// These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
Greg Danielcaa795f2019-05-14 11:54:25 -0400641// frequently used to least to improve look up times in arrays.
642static constexpr VkFormat kVkFormats[] = {
643 VK_FORMAT_R8G8B8A8_UNORM,
644 VK_FORMAT_R8_UNORM,
645 VK_FORMAT_B8G8R8A8_UNORM,
646 VK_FORMAT_R5G6B5_UNORM_PACK16,
647 VK_FORMAT_R16G16B16A16_SFLOAT,
648 VK_FORMAT_R16_SFLOAT,
Greg Danielcaa795f2019-05-14 11:54:25 -0400649 VK_FORMAT_R8G8B8_UNORM,
650 VK_FORMAT_R8G8_UNORM,
651 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
652 VK_FORMAT_B4G4R4A4_UNORM_PACK16,
Greg Danieleb4a8272019-05-16 16:52:55 -0400653 VK_FORMAT_R4G4B4A4_UNORM_PACK16,
Greg Danielcaa795f2019-05-14 11:54:25 -0400654 VK_FORMAT_R32G32B32A32_SFLOAT,
Greg Danielcaa795f2019-05-14 11:54:25 -0400655 VK_FORMAT_R8G8B8A8_SRGB,
Robert Phillipsfe18de52019-06-06 17:21:50 -0400656 VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
Robert Phillipsfe18de52019-06-06 17:21:50 -0400657 VK_FORMAT_R16_UNORM,
658 VK_FORMAT_R16G16_UNORM,
Robert Phillips66a46032019-06-18 08:00:42 -0400659 // Experimental (for Y416 and mutant P016/P010)
660 VK_FORMAT_R16G16B16A16_UNORM,
661 VK_FORMAT_R16G16_SFLOAT,
Greg Danielcaa795f2019-05-14 11:54:25 -0400662};
663
664const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
665 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
666 "Size of VkFormats array must match static value in header");
667 for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
668 if (kVkFormats[i] == format) {
669 return fFormatTable[i];
670 }
671 }
672 SK_ABORT("Invalid VkFormat");
Greg Daniel52ee5f62019-06-20 13:38:18 -0400673 static const FormatInfo kInvalidFormat;
674 return kInvalidFormat;
Greg Danielcaa795f2019-05-14 11:54:25 -0400675}
676
677void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000678 const VkPhysicalDeviceProperties& properties) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400679 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
680 "Size of VkFormats array must match static value in header");
681 for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
682 VkFormat format = kVkFormats[i];
683 if (!format_is_srgb(format) || fSRGBSupport) {
684 fFormatTable[i].init(interface, physDev, properties, format);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700685 }
686 }
687}
688
Greg Danielcaa795f2019-05-14 11:54:25 -0400689void GrVkCaps::FormatInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700690 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
691 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
692 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700693
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400694 // Ganesh assumes that all renderable surfaces are also texturable
Greg Danielcaa795f2019-05-14 11:54:25 -0400695 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400696 *flags = *flags | kRenderable_Flag;
697 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700698 }
699
700 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
701 *flags = *flags | kBlitSrc_Flag;
702 }
703
704 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
705 *flags = *flags | kBlitDst_Flag;
706 }
707}
708
Greg Danielcaa795f2019-05-14 11:54:25 -0400709void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400710 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000711 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400712 VkFormat format) {
713 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
714 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
715 VK_IMAGE_USAGE_SAMPLED_BIT |
716 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400717 VkImageFormatProperties properties;
718 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
719 format,
720 VK_IMAGE_TYPE_2D,
721 VK_IMAGE_TILING_OPTIMAL,
722 usage,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400723 0, // createFlags
Greg Daniel81e7bf82017-07-19 14:47:42 -0400724 &properties));
725 VkSampleCountFlags flags = properties.sampleCounts;
726 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400727 fColorSampleCounts.push_back(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400728 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000729 if (kImagination_VkVendor == physProps.vendorID) {
730 // MSAA does not work on imagination
731 return;
732 }
Brian Osman7fbb3632019-07-19 11:38:57 -0400733 if (kIntel_VkVendor == physProps.vendorID) {
734 // MSAA on Intel before Gen 9 is slow and/or buggy
735 if (GrGetIntelGpuFamily(physProps.deviceID) < kFirstGen9_IntelGpuFamily) {
736 return;
737 }
738 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400739 if (flags & VK_SAMPLE_COUNT_2_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400740 fColorSampleCounts.push_back(2);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400741 }
742 if (flags & VK_SAMPLE_COUNT_4_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400743 fColorSampleCounts.push_back(4);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400744 }
745 if (flags & VK_SAMPLE_COUNT_8_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400746 fColorSampleCounts.push_back(8);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400747 }
748 if (flags & VK_SAMPLE_COUNT_16_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400749 fColorSampleCounts.push_back(16);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400750 }
751 if (flags & VK_SAMPLE_COUNT_32_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400752 fColorSampleCounts.push_back(32);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400753 }
754 if (flags & VK_SAMPLE_COUNT_64_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400755 fColorSampleCounts.push_back(64);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400756 }
757}
758
Greg Danielcaa795f2019-05-14 11:54:25 -0400759void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700760 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000761 const VkPhysicalDeviceProperties& properties,
Greg Danielcaa795f2019-05-14 11:54:25 -0400762 VkFormat format) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700763 VkFormatProperties props;
764 memset(&props, 0, sizeof(VkFormatProperties));
765 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
Greg Danielcaa795f2019-05-14 11:54:25 -0400766 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
767 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400768 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000769 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400770 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500771}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400772
Robert Phillipsf209e882019-06-25 15:59:50 -0400773bool GrVkCaps::isFormatSRGB(const GrBackendFormat& format) const {
774 if (!format.getVkFormat()) {
775 return false;
776 }
777
778 return format_is_srgb(*format.getVkFormat());
779}
780
Greg Daniel2f2caea2019-07-08 14:24:47 -0400781bool GrVkCaps::isFormatTexturable(GrColorType, const GrBackendFormat& format) const {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400782 if (!format.getVkFormat()) {
783 return false;
784 }
785
Greg Daniel2f2caea2019-07-08 14:24:47 -0400786 return this->isVkFormatTexturable(*format.getVkFormat());
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400787}
788
Greg Daniel2f2caea2019-07-08 14:24:47 -0400789bool GrVkCaps::isVkFormatTexturable(VkFormat format) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400790 if (!GrVkFormatIsSupported(format)) {
791 return false;
792 }
793
794 const FormatInfo& info = this->getFormatInfo(format);
795 return SkToBool(FormatInfo::kTextureable_Flag & info.fOptimalFlags);
796}
797
798bool GrVkCaps::isConfigTexturable(GrPixelConfig config) const {
799 VkFormat format;
800 if (!GrPixelConfigToVkFormat(config, &format)) {
801 return false;
802 }
Greg Daniel2f2caea2019-07-08 14:24:47 -0400803 return this->isVkFormatTexturable(format);
Robert Phillips39ef2ef2019-05-15 08:45:53 -0400804}
805
806bool GrVkCaps::isFormatRenderable(VkFormat format) const {
807 return this->maxRenderTargetSampleCount(format) > 0;
Greg Danielcaa795f2019-05-14 11:54:25 -0400808}
809
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400810int GrVkCaps::getRenderTargetSampleCount(int requestedCount,
Greg Daniel5c96db82019-07-09 14:06:58 -0400811 GrColorType, const GrBackendFormat& format) const {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400812 if (!format.getVkFormat()) {
813 return 0;
814 }
815
816 return this->getRenderTargetSampleCount(requestedCount, *format.getVkFormat());
817}
818
Brian Salomonbdecacf2018-02-02 20:32:49 -0500819int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400820 // Currently we don't allow RGB_888X to be renderable because we don't have a way to handle
821 // blends that reference dst alpha when the values in the dst alpha channel are uninitialized.
822 if (config == kRGB_888X_GrPixelConfig) {
823 return 0;
824 }
825
826 VkFormat format;
827 if (!GrPixelConfigToVkFormat(config, &format)) {
828 return 0;
829 }
830
831 return this->getRenderTargetSampleCount(requestedCount, format);
832}
833
834int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500835 requestedCount = SkTMax(1, requestedCount);
Greg Danielcaa795f2019-05-14 11:54:25 -0400836
837 const FormatInfo& info = this->getFormatInfo(format);
838
839 int count = info.fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500840
841 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400842 return 0;
843 }
844
Brian Salomonbdecacf2018-02-02 20:32:49 -0500845 if (1 == requestedCount) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400846 SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500847 return 1;
848 }
849
Greg Daniel81e7bf82017-07-19 14:47:42 -0400850 for (int i = 0; i < count; ++i) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400851 if (info.fColorSampleCounts[i] >= requestedCount) {
852 return info.fColorSampleCounts[i];
Greg Daniel81e7bf82017-07-19 14:47:42 -0400853 }
854 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500855 return 0;
856}
857
Greg Daniel5c96db82019-07-09 14:06:58 -0400858int GrVkCaps::maxRenderTargetSampleCount(GrColorType, const GrBackendFormat& format) const {
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400859 if (!format.getVkFormat()) {
860 return 0;
861 }
862
863 return this->maxRenderTargetSampleCount(*format.getVkFormat());
864}
865
Brian Salomonbdecacf2018-02-02 20:32:49 -0500866int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400867 // Currently we don't allow RGB_888X to be renderable because we don't have a way to handle
868 // blends that reference dst alpha when the values in the dst alpha channel are uninitialized.
869 if (config == kRGB_888X_GrPixelConfig) {
870 return 0;
871 }
872
873 VkFormat format;
874 if (!GrPixelConfigToVkFormat(config, &format)) {
875 return 0;
876 }
877 return this->maxRenderTargetSampleCount(format);
878}
879
880int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
881 const FormatInfo& info = this->getFormatInfo(format);
882
883 const auto& table = info.fColorSampleCounts;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500884 if (!table.count()) {
885 return 0;
886 }
887 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500888}
889
Brian Salomondc0710f2019-07-01 14:59:32 -0400890GrCaps::SurfaceReadPixelsSupport GrVkCaps::surfaceSupportsReadPixels(
891 const GrSurface* surface) const {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400892 if (surface->isProtected()) {
Brian Salomondc0710f2019-07-01 14:59:32 -0400893 return SurfaceReadPixelsSupport::kUnsupported;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400894 }
Greg Daniela51e93c2019-03-25 12:30:45 -0400895 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
896 // We can't directly read from a VkImage that has a ycbcr sampler.
897 if (tex->ycbcrConversionInfo().isValid()) {
Brian Salomondc0710f2019-07-01 14:59:32 -0400898 return SurfaceReadPixelsSupport::kCopyToTexture2D;
Greg Daniela51e93c2019-03-25 12:30:45 -0400899 }
Greg Daniel00fb7242019-07-18 14:28:01 -0400900 // We can't directly read from a compressed format
901 SkImage::CompressionType compressionType;
902 if (GrVkFormatToCompressionType(tex->imageFormat(), &compressionType)) {
903 return SurfaceReadPixelsSupport::kCopyToTexture2D;
904 }
Greg Daniela51e93c2019-03-25 12:30:45 -0400905 }
Brian Salomondc0710f2019-07-01 14:59:32 -0400906 return SurfaceReadPixelsSupport::kSupported;
Greg Daniela51e93c2019-03-25 12:30:45 -0400907}
908
Brian Salomonc67c31c2018-12-06 10:00:03 -0500909bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
Brian Salomon3d86a192018-02-27 16:46:11 -0500910 if (auto rt = surface->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600911 return rt->numSamples() <= 1 && SkToBool(surface->asTexture());
Brian Salomon3d86a192018-02-27 16:46:11 -0500912 }
Greg Daniela51e93c2019-03-25 12:30:45 -0400913 // We can't write to a texture that has a ycbcr sampler.
914 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
915 // We can't directly read from a VkImage that has a ycbcr sampler.
916 if (tex->ycbcrConversionInfo().isValid()) {
917 return false;
918 }
919 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500920 return true;
921}
922
Robert Phillips1e2cb442019-07-02 15:51:28 -0400923// A near clone of format_color_type_valid_pair
924static GrPixelConfig validate_image_info(VkFormat format, GrColorType ct, bool hasYcbcrConversion) {
Greg Daniel14c55c22018-12-04 11:25:03 -0500925 if (format == VK_FORMAT_UNDEFINED) {
926 // If the format is undefined then it is only valid as an external image which requires that
927 // we have a valid VkYcbcrConversion.
928 if (hasYcbcrConversion) {
929 // We don't actually care what the color type or config are since we won't use those
Greg Daniela51e93c2019-03-25 12:30:45 -0400930 // values for external textures. However, for read pixels we will draw to a non ycbcr
931 // texture of this config so we set RGBA here for that.
Brian Salomonf391d0f2018-12-14 09:18:50 -0500932 return kRGBA_8888_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500933 } else {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500934 return kUnknown_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500935 }
936 }
937
938 if (hasYcbcrConversion) {
939 // We only support having a ycbcr conversion for external images.
Brian Salomonf391d0f2018-12-14 09:18:50 -0500940 return kUnknown_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500941 }
942
Greg Danielf5d87582017-12-18 14:48:15 -0500943 switch (ct) {
Robert Phillips1e2cb442019-07-02 15:51:28 -0400944 case GrColorType::kUnknown:
Brian Salomonf391d0f2018-12-14 09:18:50 -0500945 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400946 case GrColorType::kAlpha_8:
Greg Danielf5d87582017-12-18 14:48:15 -0500947 if (VK_FORMAT_R8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500948 return kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500949 }
950 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400951 case GrColorType::kBGR_565:
Greg Danielf5d87582017-12-18 14:48:15 -0500952 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500953 return kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500954 }
955 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400956 case GrColorType::kABGR_4444:
Greg Danieleb4a8272019-05-16 16:52:55 -0400957 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format ||
958 VK_FORMAT_R4G4B4A4_UNORM_PACK16 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500959 return kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500960 }
961 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400962 case GrColorType::kRGBA_8888:
Greg Danielf5d87582017-12-18 14:48:15 -0500963 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500964 return kRGBA_8888_GrPixelConfig;
Greg Daniele877dce2019-07-11 10:52:43 -0400965 }
966 break;
967 case GrColorType::kRGBA_8888_SRGB:
968 if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500969 return kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500970 }
971 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400972 case GrColorType::kRGB_888x:
Greg Daniel475eb702018-09-28 14:16:50 -0400973 if (VK_FORMAT_R8G8B8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500974 return kRGB_888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -0400975 }
Greg Danielf259b8b2019-02-14 09:03:43 -0500976 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
977 return kRGB_888X_GrPixelConfig;
978 }
Greg Daniel475eb702018-09-28 14:16:50 -0400979 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400980 case GrColorType::kRG_88:
981 if (VK_FORMAT_R8G8_UNORM == format) {
982 return kRG_88_GrPixelConfig;
983 }
984 break;
985 case GrColorType::kBGRA_8888:
Greg Danielf5d87582017-12-18 14:48:15 -0500986 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500987 return kBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500988 }
989 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400990 case GrColorType::kRGBA_1010102:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500991 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500992 return kRGBA_1010102_GrPixelConfig;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500993 }
994 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -0400995 case GrColorType::kGray_8:
Greg Danielf5d87582017-12-18 14:48:15 -0500996 if (VK_FORMAT_R8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500997 return kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500998 }
999 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -04001000 case GrColorType::kAlpha_F16:
1001 if (VK_FORMAT_R16_SFLOAT == format) {
Robert Phillips0902c982019-07-16 07:47:56 -04001002 return kAlpha_half_as_Red_GrPixelConfig;
Mike Kleinb70990e2019-02-28 10:03:27 -06001003 }
1004 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -04001005 case GrColorType::kRGBA_F16:
Greg Danielf5d87582017-12-18 14:48:15 -05001006 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001007 return kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -05001008 }
1009 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -04001010 case GrColorType::kRGBA_F16_Clamped:
1011 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
1012 return kRGBA_half_Clamped_GrPixelConfig;
1013 }
1014 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -04001015 case GrColorType::kRGBA_F32:
Mike Klein37854712018-06-26 11:43:06 -04001016 if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001017 return kRGBA_float_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -04001018 }
1019 break;
Robert Phillips1e2cb442019-07-02 15:51:28 -04001020 case GrColorType::kR_16:
1021 if (VK_FORMAT_R16_UNORM == format) {
1022 return kR_16_GrPixelConfig;
1023 }
1024 break;
1025 case GrColorType::kRG_1616:
1026 if (VK_FORMAT_R16G16_UNORM == format) {
1027 return kRG_1616_GrPixelConfig;
1028 }
1029 break;
1030 case GrColorType::kRGBA_16161616:
1031 if (VK_FORMAT_R16G16B16A16_UNORM == format) {
1032 return kRGBA_16161616_GrPixelConfig;
1033 }
1034 break;
1035 case GrColorType::kRG_F16:
1036 if (VK_FORMAT_R16G16_SFLOAT == format) {
1037 return kRG_half_GrPixelConfig;
1038 }
1039 break;
Greg Danielf5d87582017-12-18 14:48:15 -05001040 }
1041
Brian Salomonf391d0f2018-12-14 09:18:50 -05001042 return kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -05001043}
1044
Brian Salomonf391d0f2018-12-14 09:18:50 -05001045GrPixelConfig GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt,
Robert Phillips1e2cb442019-07-02 15:51:28 -04001046 GrColorType ct) const {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001047 GrVkImageInfo imageInfo;
1048 if (!rt.getVkImageInfo(&imageInfo)) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001049 return kUnknown_GrPixelConfig;
Robert Phillipsfc711a22018-02-13 17:03:00 -05001050 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001051 return validate_image_info(imageInfo.fFormat, ct, imageInfo.fYcbcrConversionInfo.isValid());
Robert Phillipsfc711a22018-02-13 17:03:00 -05001052}
1053
Greg Daniel627d0532019-07-08 16:48:14 -04001054bool GrVkCaps::onAreColorTypeAndFormatCompatible(GrColorType ct,
1055 const GrBackendFormat& format) const {
Robert Phillipsc046ff02019-07-01 10:34:03 -04001056 const VkFormat* vkFormat = format.getVkFormat();
1057 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1058 if (!vkFormat || !ycbcrInfo) {
1059 return false;
1060 }
1061
1062 return kUnknown_GrPixelConfig != validate_image_info(*vkFormat, ct, ycbcrInfo->isValid());
1063}
1064
1065
Greg Daniel627d0532019-07-08 16:48:14 -04001066GrPixelConfig GrVkCaps::onGetConfigFromBackendFormat(const GrBackendFormat& format,
1067 GrColorType ct) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -05001068 const VkFormat* vkFormat = format.getVkFormat();
Greg Daniel14c55c22018-12-04 11:25:03 -05001069 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1070 if (!vkFormat || !ycbcrInfo) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001071 return kUnknown_GrPixelConfig;
Robert Phillipsfc711a22018-02-13 17:03:00 -05001072 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001073 return validate_image_info(*vkFormat, ct, ycbcrInfo->isValid());
Greg Danielfaa095e2017-12-19 13:15:02 -05001074}
Greg Danielf5d87582017-12-18 14:48:15 -05001075
Brian Salomonf391d0f2018-12-14 09:18:50 -05001076static GrPixelConfig get_yuva_config(VkFormat vkFormat) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001077 switch (vkFormat) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001078 case VK_FORMAT_R8_UNORM:
1079 return kAlpha_8_as_Red_GrPixelConfig;
1080 case VK_FORMAT_R8G8B8A8_UNORM:
1081 return kRGBA_8888_GrPixelConfig;
1082 case VK_FORMAT_R8G8B8_UNORM:
1083 return kRGB_888_GrPixelConfig;
1084 case VK_FORMAT_R8G8_UNORM:
1085 return kRG_88_GrPixelConfig;
1086 case VK_FORMAT_B8G8R8A8_UNORM:
1087 return kBGRA_8888_GrPixelConfig;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001088 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1089 return kRGBA_1010102_GrPixelConfig;
Robert Phillipsfe18de52019-06-06 17:21:50 -04001090 case VK_FORMAT_R16_UNORM:
1091 return kR_16_GrPixelConfig;
1092 case VK_FORMAT_R16G16_UNORM:
1093 return kRG_1616_GrPixelConfig;
Robert Phillips66a46032019-06-18 08:00:42 -04001094 // Experimental (for Y416 and mutant P016/P010)
1095 case VK_FORMAT_R16G16B16A16_UNORM:
1096 return kRGBA_16161616_GrPixelConfig;
1097 case VK_FORMAT_R16G16_SFLOAT:
1098 return kRG_half_GrPixelConfig;
Brian Salomonf391d0f2018-12-14 09:18:50 -05001099 default:
1100 return kUnknown_GrPixelConfig;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -04001101 }
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -04001102}
1103
Brian Salomonf391d0f2018-12-14 09:18:50 -05001104GrPixelConfig GrVkCaps::getYUVAConfigFromBackendFormat(const GrBackendFormat& format) const {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001105 const VkFormat* vkFormat = format.getVkFormat();
1106 if (!vkFormat) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001107 return kUnknown_GrPixelConfig;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001108 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001109 return get_yuva_config(*vkFormat);
Timothy Liang036fdfe2018-06-28 15:50:36 -04001110}
Greg Daniel4065d452018-11-16 15:43:41 -05001111
Greg Daniele877dce2019-07-11 10:52:43 -04001112GrBackendFormat GrVkCaps::getBackendFormatFromColorType(GrColorType ct) const {
1113 GrPixelConfig config = GrColorTypeToPixelConfig(ct);
Greg Daniel4065d452018-11-16 15:43:41 -05001114 if (config == kUnknown_GrPixelConfig) {
1115 return GrBackendFormat();
1116 }
1117 VkFormat format;
1118 if (!GrPixelConfigToVkFormat(config, &format)) {
1119 return GrBackendFormat();
1120 }
1121 return GrBackendFormat::MakeVk(format);
1122}
Timothy Liang036fdfe2018-06-28 15:50:36 -04001123
Brian Salomonbb8dde82019-06-27 10:52:13 -04001124GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType(
1125 SkImage::CompressionType compressionType) const {
1126 switch (compressionType) {
1127 case SkImage::kETC1_CompressionType:
1128 return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);
1129 }
1130 SK_ABORT("Invalid compression type");
1131 return {};
1132}
1133
Brian Salomona3e29962019-07-16 11:52:08 -04001134bool GrVkCaps::canClearTextureOnCreation() const { return true; }
1135
Greg Danieleb4a8272019-05-16 16:52:55 -04001136#ifdef SK_DEBUG
1137static bool format_color_type_valid_pair(VkFormat vkFormat, GrColorType colorType) {
1138 switch (colorType) {
1139 case GrColorType::kUnknown:
1140 return false;
1141 case GrColorType::kAlpha_8:
1142 return VK_FORMAT_R8_UNORM == vkFormat;
Greg Daniel48fec762019-06-18 17:06:43 -04001143 case GrColorType::kBGR_565:
Greg Danieleb4a8272019-05-16 16:52:55 -04001144 return VK_FORMAT_R5G6B5_UNORM_PACK16 == vkFormat;
1145 case GrColorType::kABGR_4444:
1146 return VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat ||
1147 VK_FORMAT_R4G4B4A4_UNORM_PACK16 == vkFormat;
1148 case GrColorType::kRGBA_8888:
Greg Daniele877dce2019-07-11 10:52:43 -04001149 return VK_FORMAT_R8G8B8A8_UNORM == vkFormat;
1150 case GrColorType::kRGBA_8888_SRGB:
1151 return VK_FORMAT_R8G8B8A8_SRGB == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001152 case GrColorType::kRGB_888x:
Brian Salomonbb8dde82019-06-27 10:52:13 -04001153 GR_STATIC_ASSERT(GrCompressionTypeClosestColorType(SkImage::kETC1_CompressionType) ==
1154 GrColorType::kRGB_888x);
1155 return VK_FORMAT_R8G8B8_UNORM == vkFormat || VK_FORMAT_R8G8B8A8_UNORM == vkFormat ||
1156 VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001157 case GrColorType::kRG_88:
1158 return VK_FORMAT_R8G8_UNORM == vkFormat;
1159 case GrColorType::kBGRA_8888:
Greg Daniel00fb7242019-07-18 14:28:01 -04001160 return VK_FORMAT_B8G8R8A8_UNORM == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001161 case GrColorType::kRGBA_1010102:
1162 return VK_FORMAT_A2B10G10R10_UNORM_PACK32 == vkFormat;
1163 case GrColorType::kGray_8:
1164 return VK_FORMAT_R8_UNORM == vkFormat;
1165 case GrColorType::kAlpha_F16:
1166 return VK_FORMAT_R16_SFLOAT == vkFormat;
1167 case GrColorType::kRGBA_F16:
1168 return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
1169 case GrColorType::kRGBA_F16_Clamped:
1170 return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001171 case GrColorType::kRGBA_F32:
1172 return VK_FORMAT_R32G32B32A32_SFLOAT == vkFormat;
Robert Phillipsfe18de52019-06-06 17:21:50 -04001173 case GrColorType::kR_16:
1174 return VK_FORMAT_R16_UNORM == vkFormat;
1175 case GrColorType::kRG_1616:
1176 return VK_FORMAT_R16G16_UNORM == vkFormat;
Robert Phillips66a46032019-06-18 08:00:42 -04001177 // Experimental (for Y416 and mutant P016/P010)
1178 case GrColorType::kRGBA_16161616:
1179 return VK_FORMAT_R16G16B16A16_UNORM == vkFormat;
Brian Salomone14cfbe2019-06-24 15:00:58 -04001180 case GrColorType::kRG_F16:
Robert Phillips66a46032019-06-18 08:00:42 -04001181 return VK_FORMAT_R16G16_SFLOAT == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001182 }
Greg Daniel4f71ccc2019-05-17 10:56:46 -04001183 SK_ABORT("Unknown color type");
1184 return false;
Greg Danieleb4a8272019-05-16 16:52:55 -04001185}
1186#endif
1187
1188static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
1189 bool forOutput) {
1190 SkASSERT(format.getVkFormat());
1191 VkFormat vkFormat = *format.getVkFormat();
1192
1193 SkASSERT(format_color_type_valid_pair(vkFormat, colorType));
1194
1195 switch (colorType) {
1196 case GrColorType::kAlpha_8: // fall through
1197 case GrColorType::kAlpha_F16:
1198 if (forOutput) {
1199 return GrSwizzle::AAAA();
1200 } else {
1201 return GrSwizzle::RRRR();
1202 }
1203 case GrColorType::kGray_8:
1204 if (!forOutput) {
1205 return GrSwizzle::RRRA();
1206 }
1207 break;
1208 case GrColorType::kABGR_4444:
1209 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat) {
1210 return GrSwizzle::BGRA();
1211 }
1212 break;
1213 case GrColorType::kRGB_888x:
1214 if (!forOutput) {
1215 return GrSwizzle::RGB1();
1216 }
1217 default:
1218 return GrSwizzle::RGBA();
1219 }
1220 return GrSwizzle::RGBA();
1221}
1222
1223GrSwizzle GrVkCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1224 return get_swizzle(format, colorType, false);
1225}
1226GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1227 return get_swizzle(format, colorType, true);
1228}
1229
Brian Salomon26de56e2019-04-10 12:14:26 -04001230size_t GrVkCaps::onTransferFromOffsetAlignment(GrColorType bufferColorType) const {
Brian Salomona585fe92019-04-09 14:57:00 -04001231 // This GrColorType has 32 bpp but the Vulkan pixel format we use for with may have 24bpp
1232 // (VK_FORMAT_R8G8B8_...) or may be 32 bpp. We don't support post transforming the pixel data
1233 // for transfer-from currently and don't want to have to pass info about the src surface here.
1234 if (bufferColorType == GrColorType::kRGB_888x) {
1235 return false;
1236 }
1237 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
1238 // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1239 switch (bpp & 0b11) {
Brian Salomon26de56e2019-04-10 12:14:26 -04001240 // bpp is already a multiple of 4.
1241 case 0: return bpp;
1242 // bpp is a multiple of 2 but not 4.
1243 case 2: return 2 * bpp;
1244 // bpp is not a multiple of 2.
1245 default: return 4 * bpp;
Brian Salomona585fe92019-04-09 14:57:00 -04001246 }
Brian Salomona585fe92019-04-09 14:57:00 -04001247}
Greg Daniel00fb7242019-07-18 14:28:01 -04001248
1249GrCaps::SupportedRead GrVkCaps::supportedReadPixelsColorType(
1250 GrColorType srcColorType, const GrBackendFormat& srcBackendFormat,
1251 GrColorType dstColorType) const {
1252 const VkFormat* vkFormat = srcBackendFormat.getVkFormat();
1253 if (!vkFormat) {
1254 return {GrSwizzle(), GrColorType::kUnknown};
1255 }
1256
1257 switch (*vkFormat) {
1258 case VK_FORMAT_R8G8B8A8_UNORM:
1259 return {GrSwizzle::RGBA(), GrColorType::kRGBA_8888};
1260 case VK_FORMAT_R8_UNORM:
1261 if (srcColorType == GrColorType::kAlpha_8) {
1262 return {GrSwizzle::RGBA(), GrColorType::kAlpha_8};
1263 } else if (srcColorType == GrColorType::kGray_8) {
1264 return {GrSwizzle::RGBA(), GrColorType::kGray_8};
1265 }
1266 case VK_FORMAT_B8G8R8A8_UNORM:
1267 return {GrSwizzle::RGBA(), GrColorType::kBGRA_8888};
1268 case VK_FORMAT_R5G6B5_UNORM_PACK16:
1269 return {GrSwizzle::RGBA(), GrColorType::kBGR_565};
1270 case VK_FORMAT_R16G16B16A16_SFLOAT:
1271 if (srcColorType == GrColorType::kRGBA_F16) {
1272 return {GrSwizzle::RGBA(), GrColorType::kRGBA_F16};
1273 } else if (srcColorType == GrColorType::kRGBA_F16_Clamped){
1274 return {GrSwizzle::RGBA(), GrColorType::kRGBA_F16_Clamped};
1275 }
1276 case VK_FORMAT_R16_SFLOAT:
1277 return {GrSwizzle::RGBA(), GrColorType::kAlpha_F16};
1278 case VK_FORMAT_R8G8B8_UNORM:
1279 return {GrSwizzle::RGBA(), GrColorType::kRGB_888x};
1280 case VK_FORMAT_R8G8_UNORM:
1281 return {GrSwizzle::RGBA(), GrColorType::kRG_88};
1282 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1283 return {GrSwizzle::RGBA(), GrColorType::kRGBA_1010102};
1284 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
1285 return {GrSwizzle::RGBA(), GrColorType::kABGR_4444};
1286 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
1287 return {GrSwizzle::RGBA(), GrColorType::kABGR_4444};
1288 case VK_FORMAT_R32G32B32A32_SFLOAT:
1289 return {GrSwizzle::RGBA(), GrColorType::kRGBA_F32};
1290 case VK_FORMAT_R8G8B8A8_SRGB:
1291 return {GrSwizzle::RGBA(), GrColorType::kRGBA_8888_SRGB};
1292 case VK_FORMAT_R16_UNORM:
1293 return {GrSwizzle::RGBA(), GrColorType::kR_16};
1294 case VK_FORMAT_R16G16_UNORM:
1295 return {GrSwizzle::RGBA(), GrColorType::kRG_1616};
1296 // Experimental (for Y416 and mutant P016/P010)
1297 case VK_FORMAT_R16G16B16A16_UNORM:
1298 return {GrSwizzle::RGBA(), GrColorType::kRGBA_16161616};
1299 case VK_FORMAT_R16G16_SFLOAT:
1300 return {GrSwizzle::RGBA(), GrColorType::kRG_F16};
1301 default:
1302 return {GrSwizzle(), GrColorType::kUnknown};
1303 }
1304}