blob: 8a1cc6088bec52cb3d6414ebba685ab3aaed5414 [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"
12#include "include/private/GrRenderTargetProxy.h"
13#include "src/gpu/GrShaderCaps.h"
14#include "src/gpu/SkGr.h"
15#include "src/gpu/vk/GrVkCaps.h"
16#include "src/gpu/vk/GrVkInterface.h"
17#include "src/gpu/vk/GrVkTexture.h"
18#include "src/gpu/vk/GrVkUtil.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019
20GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -040021 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Greg Daniel41f0e282019-01-28 13:15:05 -050022 uint32_t instanceVersion, uint32_t physicalDeviceVersion,
23 const GrVkExtensions& extensions)
egdanielc5ec1402016-03-28 12:14:42 -070024 : INHERITED(contextOptions) {
Greg Danielc0b03d82018-08-03 14:41:15 -040025
Greg Daniel164a9f02016-02-22 09:56:40 -050026 /**************************************************************************
Brian Salomonf7232642018-09-19 08:58:08 -040027 * GrCaps fields
28 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070029 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070030 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070031 fNPOTTextureTileSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070032 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050033 fReuseScratchTextures = true; //TODO: figure this out
34 fGpuTracingSupport = false; //TODO: figure this out
Jim Van Verth1676cb92019-01-15 13:24:45 -050035 fCompressedTexSubImageSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050036 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060037 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050038
Brian Salomon9ff5acb2019-05-08 09:04:47 -040039 fSemaphoreSupport = true; // always available in Vulkan
jvanverth84741b32016-09-30 08:39:02 -070040 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050041 fCrossContextTextureSupport = true;
Brian Osman499bf1a2018-09-17 11:32:42 -040042 fHalfFloatVertexAttributeSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050043
Brian Salomone05ba5a2019-04-08 11:59:07 -040044 fTransferBufferSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050045
46 fMaxRenderTargetSize = 4096; // minimum required by spec
47 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050048
Brian Salomonf7232642018-09-19 08:58:08 -040049 fDynamicStateArrayGeometryProcessorTextureSupport = true;
50
Brian Salomon94efbf52016-11-29 13:43:05 -050051 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050052
Greg Daniel41f0e282019-01-28 13:15:05 -050053 this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions);
Greg Daniel164a9f02016-02-22 09:56:40 -050054}
55
Robert Phillipsbf25d432017-04-07 10:08:53 -040056bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
Greg Daniel46cfbc62019-06-07 11:43:30 -040057 bool* rectsMustMatch, bool* disallowSubrect) const {
Eric Karl74480882017-04-03 14:49:05 -070058 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
59 *rectsMustMatch = false;
60 *disallowSubrect = false;
61
Brian Salomon467921e2017-03-06 16:17:12 -050062 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
63 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
64 // render target as well.
Brian Salomon467921e2017-03-06 16:17:12 -050065 desc->fConfig = src->config();
Greg Daniel55fa6472018-03-16 16:13:10 -040066 if (src->numColorSamples() > 1 || src->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -050067 desc->fFlags = kRenderTarget_GrSurfaceFlag;
68 } else {
69 // Just going to use CopyImage here
70 desc->fFlags = kNone_GrSurfaceFlags;
71 }
72
73 return true;
74}
75
Greg Daniel5c7b5412019-05-10 11:39:55 -040076static int get_compatible_format_class(GrPixelConfig config) {
77 switch (config) {
78 case kAlpha_8_GrPixelConfig:
79 case kAlpha_8_as_Red_GrPixelConfig:
80 case kGray_8_GrPixelConfig:
81 case kGray_8_as_Red_GrPixelConfig:
82 return 1;
83 case kRGB_565_GrPixelConfig:
84 case kRGBA_4444_GrPixelConfig:
85 case kRG_88_GrPixelConfig:
86 case kAlpha_half_GrPixelConfig:
87 case kAlpha_half_as_Red_GrPixelConfig:
88 return 2;
89 case kRGB_888_GrPixelConfig:
90 return 3;
91 case kRGBA_8888_GrPixelConfig:
92 case kRGB_888X_GrPixelConfig:
93 case kBGRA_8888_GrPixelConfig:
94 case kSRGBA_8888_GrPixelConfig:
95 case kSBGRA_8888_GrPixelConfig:
96 case kRGBA_1010102_GrPixelConfig:
97 return 4;
98 case kRGBA_half_GrPixelConfig:
99 case kRGBA_half_Clamped_GrPixelConfig:
100 case kRG_float_GrPixelConfig:
101 return 5;
102 case kRGBA_float_GrPixelConfig:
103 return 6;
104 case kRGB_ETC1_GrPixelConfig:
105 return 7;
106 case kUnknown_GrPixelConfig:
107 case kAlpha_8_as_Alpha_GrPixelConfig:
108 case kGray_8_as_Lum_GrPixelConfig:
109 SK_ABORT("Unsupported Vulkan pixel config");
110 return 0;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400111 // Experimental (for P016 and P010)
112 case kR_16_GrPixelConfig:
113 return 2;
114 case kRG_1616_GrPixelConfig:
115 return 4;
Greg Daniel5c7b5412019-05-10 11:39:55 -0400116 }
117 SK_ABORT("Invalid pixel config");
118 return 0;
119}
120
Greg Daniel46cfbc62019-06-07 11:43:30 -0400121bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, bool dstHasYcbcr,
122 GrPixelConfig srcConfig, int srcSampleCnt, bool srcHasYcbcr) const {
Greg Daniel25af6712018-04-25 10:44:38 -0400123 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
124 return false;
125 }
126
Greg Daniela51e93c2019-03-25 12:30:45 -0400127 if (dstHasYcbcr || srcHasYcbcr) {
128 return false;
129 }
130
Greg Daniel25af6712018-04-25 10:44:38 -0400131 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
132 // as image usage flags.
Greg Daniel46cfbc62019-06-07 11:43:30 -0400133 if (get_compatible_format_class(srcConfig) != get_compatible_format_class(dstConfig)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400134 return false;
135 }
136
137 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
138 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
139 return false;
140 }
141
142 return true;
143}
144
145bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
Greg Daniela51e93c2019-03-25 12:30:45 -0400146 bool dstHasYcbcr, GrPixelConfig srcConfig, int srcSampleCnt,
147 bool srcIsLinear, bool srcHasYcbcr) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400148
149 VkFormat dstFormat;
150 SkAssertResult(GrPixelConfigToVkFormat(dstConfig, &dstFormat));
151 VkFormat srcFormat;
152 SkAssertResult(GrPixelConfigToVkFormat(srcConfig, &srcFormat));
Greg Daniel25af6712018-04-25 10:44:38 -0400153 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
154 // as image usage flags.
Greg Danielcaa795f2019-05-14 11:54:25 -0400155 if (!this->formatCanBeDstofBlit(dstFormat, dstIsLinear) ||
156 !this->formatCanBeSrcofBlit(srcFormat, srcIsLinear)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400157 return false;
158 }
159
160 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
161 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
162 return false;
163 }
164
165 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
166 // resolved msaa though.
167 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
168 return false;
169 }
170
Greg Daniela51e93c2019-03-25 12:30:45 -0400171 if (dstHasYcbcr || srcHasYcbcr) {
172 return false;
173 }
174
Greg Daniel25af6712018-04-25 10:44:38 -0400175 return true;
176}
177
Greg Daniel46cfbc62019-06-07 11:43:30 -0400178bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt, bool dstHasYcbcr,
179 GrPixelConfig srcConfig, int srcSampleCnt, bool srcHasYcbcr) const {
Greg Daniel25af6712018-04-25 10:44:38 -0400180 // The src surface must be multisampled.
181 if (srcSampleCnt <= 1) {
182 return false;
183 }
184
185 // The dst must not be multisampled.
186 if (dstSampleCnt > 1) {
187 return false;
188 }
189
190 // Surfaces must have the same format.
191 if (dstConfig != srcConfig) {
192 return false;
193 }
194
Greg Daniela51e93c2019-03-25 12:30:45 -0400195 if (dstHasYcbcr || srcHasYcbcr) {
196 return false;
197 }
198
Greg Daniel25af6712018-04-25 10:44:38 -0400199 return true;
200}
201
Brian Salomonc67c31c2018-12-06 10:00:03 -0500202bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
203 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
Greg Daniel25af6712018-04-25 10:44:38 -0400204 GrPixelConfig dstConfig = dst->config();
205 GrPixelConfig srcConfig = src->config();
206
207 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
208 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
209 // tracking of external or rectangle textures in GL? For now we don't create linear textures
210 // internally, and I don't believe anyone is wrapping them.
211 bool srcIsLinear = false;
212 bool dstIsLinear = false;
213
214 int dstSampleCnt = 0;
215 int srcSampleCnt = 0;
216 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
Greg Danielbe7fc462019-01-03 16:40:42 -0500217 // Copying to or from render targets that wrap a secondary command buffer is not allowed
218 // since they would require us to know the VkImage, which we don't have, as well as need us
219 // to stop and start the VkRenderPass which we don't have access to.
220 if (rtProxy->wrapsVkSecondaryCB()) {
221 return false;
222 }
Greg Daniel25af6712018-04-25 10:44:38 -0400223 dstSampleCnt = rtProxy->numColorSamples();
224 }
225 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
Greg Danielbe7fc462019-01-03 16:40:42 -0500226 // Copying to or from render targets that wrap a secondary command buffer is not allowed
227 // since they would require us to know the VkImage, which we don't have, as well as need us
228 // to stop and start the VkRenderPass which we don't have access to.
229 if (rtProxy->wrapsVkSecondaryCB()) {
230 return false;
231 }
Greg Daniel25af6712018-04-25 10:44:38 -0400232 srcSampleCnt = rtProxy->numColorSamples();
233 }
234 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
235 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
236
Greg Daniela51e93c2019-03-25 12:30:45 -0400237 bool dstHasYcbcr = false;
238 if (auto ycbcr = dst->backendFormat().getVkYcbcrConversionInfo()) {
239 if (ycbcr->isValid()) {
240 dstHasYcbcr = true;
241 }
242 }
243
244 bool srcHasYcbcr = false;
245 if (auto ycbcr = src->backendFormat().getVkYcbcrConversionInfo()) {
246 if (ycbcr->isValid()) {
247 srcHasYcbcr = true;
248 }
249 }
250
Greg Daniel46cfbc62019-06-07 11:43:30 -0400251 return this->canCopyImage(dstConfig, dstSampleCnt, dstHasYcbcr,
252 srcConfig, srcSampleCnt, srcHasYcbcr) ||
Greg Daniela51e93c2019-03-25 12:30:45 -0400253 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear, dstHasYcbcr,
254 srcConfig, srcSampleCnt, srcIsLinear, srcHasYcbcr) ||
Greg Daniel46cfbc62019-06-07 11:43:30 -0400255 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstHasYcbcr,
256 srcConfig, srcSampleCnt, srcHasYcbcr);
Greg Daniel25af6712018-04-25 10:44:38 -0400257}
258
Greg Daniel7e000222018-12-03 10:08:21 -0500259template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
260 VkStructureType type) {
261 // All Vulkan structs that could be part of the features chain will start with the
262 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
263 // so we can get access to the pNext for the next struct.
264 struct CommonVulkanHeader {
265 VkStructureType sType;
266 void* pNext;
267 };
268
269 void* pNext = features.pNext;
270 while (pNext) {
271 CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
272 if (header->sType == type) {
273 return static_cast<T*>(pNext);
274 }
275 pNext = header->pNext;
276 }
277 return nullptr;
278}
279
Greg Daniel164a9f02016-02-22 09:56:40 -0500280void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -0400281 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Greg Daniel41f0e282019-01-28 13:15:05 -0500282 uint32_t physicalDeviceVersion, const GrVkExtensions& extensions) {
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000283 VkPhysicalDeviceProperties properties;
284 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -0800285
egdanield5e3b9e2016-03-08 12:19:54 -0800286 VkPhysicalDeviceMemoryProperties memoryProperties;
287 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
288
Greg Daniel41f0e282019-01-28 13:15:05 -0500289 SkASSERT(physicalDeviceVersion <= properties.apiVersion);
Greg Danielc0b03d82018-08-03 14:41:15 -0400290
Greg Danielcb324152019-02-25 11:36:53 -0500291 if (extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 1)) {
292 fSupportsSwapchain = true;
293 }
294
Greg Danielc0b03d82018-08-03 14:41:15 -0400295 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
296 extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
297 fSupportsPhysicalDeviceProperties2 = true;
298 }
299
300 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
301 extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
302 fSupportsMemoryRequirements2 = true;
303 }
304
305 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Daniel637c06a2018-09-12 09:44:25 -0400306 extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
307 fSupportsBindMemory2 = true;
308 }
309
310 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Danielc0b03d82018-08-03 14:41:15 -0400311 extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
312 fSupportsMaintenance1 = true;
313 }
314
315 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
316 extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
317 fSupportsMaintenance2 = true;
318 }
319
320 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
321 extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
322 fSupportsMaintenance3 = true;
323 }
324
Greg Daniela9979d12018-08-27 15:56:46 -0400325 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
Greg Daniel637c06a2018-09-12 09:44:25 -0400326 (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
Greg Daniela9979d12018-08-27 15:56:46 -0400327 this->supportsMemoryRequirements2())) {
328 fSupportsDedicatedAllocation = true;
329 }
330
331 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
332 (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
333 this->supportsPhysicalDeviceProperties2() &&
334 extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
335 this->supportsDedicatedAllocation())) {
336 fSupportsExternalMemory = true;
337 }
338
339#ifdef SK_BUILD_FOR_ANDROID
Greg Daniel637c06a2018-09-12 09:44:25 -0400340 // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
341 // do we don't explicitly require it here even the spec says it is required.
Greg Daniela9979d12018-08-27 15:56:46 -0400342 if (extensions.hasExtension(
Greg Daniel637c06a2018-09-12 09:44:25 -0400343 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
344 /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
345 this->supportsExternalMemory() &&
346 this->supportsBindMemory2()) {
Greg Daniela9979d12018-08-27 15:56:46 -0400347 fSupportsAndroidHWBExternalMemory = true;
Greg Daniel637c06a2018-09-12 09:44:25 -0400348 fSupportsAHardwareBufferImages = true;
Greg Daniela9979d12018-08-27 15:56:46 -0400349 }
350#endif
351
Greg Daniel7e000222018-12-03 10:08:21 -0500352 auto ycbcrFeatures =
353 get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
354 features,
355 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
356 if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
357 fSupportsAndroidHWBExternalMemory &&
358 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
359 (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
360 this->supportsMaintenance1() &&
361 this->supportsBindMemory2() &&
362 this->supportsMemoryRequirements2() &&
363 this->supportsPhysicalDeviceProperties2()))) {
364 fSupportsYcbcrConversion = true;
365 }
366 // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
367 // will return a key of 0.
368 fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
369
Greg Daniel313c6952018-08-08 09:24:08 -0400370 this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
Greg Daniel36443602018-08-02 12:51:52 -0400371 this->initShaderCaps(properties, features);
Greg Danielf3b11622018-03-01 15:01:27 -0500372
373 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
374#if defined(SK_CPU_X86)
375 // We need to do this before initing the config table since it uses fSRGBSupport
376 if (kImagination_VkVendor == properties.vendorID) {
377 fSRGBSupport = false;
378 }
379#endif
380 }
381
Chris Dalton8e738a22018-10-05 16:41:44 -0600382 if (kQualcomm_VkVendor == properties.vendorID) {
383 // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
384 // scissored clear.
385 // On NVIDIA and Intel, the discard load followed by clear is faster.
386 // TODO: Evaluate on ARM, Imagination, and ATI.
387 fPreferFullscreenClears = true;
388 }
389
Greg Daniel44e69f92019-03-20 11:18:25 -0400390 if (kQualcomm_VkVendor == properties.vendorID || kARM_VkVendor == properties.vendorID) {
391 // On Qualcomm and ARM mapping a gpu buffer and doing both reads and writes to it is slow.
392 // Thus for index and vertex buffers we will force to use a cpu side buffer and then copy
393 // the whole buffer up to the gpu.
Greg Daniel78e6a4c2019-03-19 14:13:36 -0400394 fBufferMapThreshold = SK_MaxS32;
395 }
396
397 if (kQualcomm_VkVendor == properties.vendorID) {
398 // On Qualcomm it looks like using vkCmdUpdateBuffer is slower than using a transfer buffer
399 // even for small sizes.
400 fAvoidUpdateBuffers = true;
401 }
402
Chris Dalton0dffbab2019-03-27 13:08:50 -0600403 if (kARM_VkVendor == properties.vendorID) {
404 // ARM seems to do better with more fine triangles as opposed to using the sample mask.
405 // (At least in our current round rect op.)
406 fPreferTrianglesOverSampleMask = true;
407 }
Greg Daniel78e6a4c2019-03-19 14:13:36 -0400408
Greg Danielcaa795f2019-05-14 11:54:25 -0400409 this->initFormatTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700410 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500411
Greg Daniel691f5e72018-02-28 14:21:34 -0500412 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
413 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700414 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500415
Greg Danielddc0c602018-06-18 11:26:30 -0400416 // On nexus player we disable suballocating VkImage memory since we've seen large slow downs on
417 // bot run times.
418 if (kImagination_VkVendor == properties.vendorID) {
419 fShouldAlwaysUseDedicatedImageMemory = true;
420 }
421
Greg Daniel691f5e72018-02-28 14:21:34 -0500422 this->applyOptionsOverrides(contextOptions);
423 fShaderCaps->applyOptionsOverrides(contextOptions);
424}
425
426void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700427 if (kQualcomm_VkVendor == properties.vendorID) {
428 fMustDoCopiesFromOrigin = true;
Brian Salomona585fe92019-04-09 14:57:00 -0400429 // Transfer doesn't support this workaround.
430 fTransferBufferSupport = false;
egdaniel6fa0a912016-09-12 11:51:29 -0700431 }
432
Greg Daniel80a08dd2017-01-20 10:45:49 -0500433#if defined(SK_BUILD_FOR_WIN)
Greg Daniel900e5c82018-08-28 10:59:24 -0400434 if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
Greg Daniel80a08dd2017-01-20 10:45:49 -0500435 fMustSleepOnTearDown = true;
436 }
437#elif defined(SK_BUILD_FOR_ANDROID)
438 if (kImagination_VkVendor == properties.vendorID) {
439 fMustSleepOnTearDown = true;
440 }
441#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500442
443 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
444 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
445 if (kAMD_VkVendor == properties.vendorID) {
446 fNewCBOnPipelineChange = true;
447 }
448
Greg Danielddc0c602018-06-18 11:26:30 -0400449 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
450 if (kARM_VkVendor == properties.vendorID) {
451 fShouldAlwaysUseDedicatedImageMemory = true;
452 }
453
Greg Danielbce5eb92018-03-01 13:13:44 -0500454 ////////////////////////////////////////////////////////////////////////////
455 // GrCaps workarounds
456 ////////////////////////////////////////////////////////////////////////////
457
458 if (kARM_VkVendor == properties.vendorID) {
459 fInstanceAttribSupport = false;
Greg Daniel4374e962018-09-28 15:09:47 -0400460 fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
Greg Danielbce5eb92018-03-01 13:13:44 -0500461 }
462
463 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
464 if (kAMD_VkVendor == properties.vendorID) {
465 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
466 }
467
Greg Danielbce5eb92018-03-01 13:13:44 -0500468 ////////////////////////////////////////////////////////////////////////////
469 // GrShaderCaps workarounds
470 ////////////////////////////////////////////////////////////////////////////
471
Greg Danielbce5eb92018-03-01 13:13:44 -0500472 if (kImagination_VkVendor == properties.vendorID) {
473 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
474 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500475}
476
477int get_max_sample_count(VkSampleCountFlags flags) {
478 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
479 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
480 return 0;
481 }
482 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
483 return 2;
484 }
485 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
486 return 4;
487 }
488 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
489 return 8;
490 }
491 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
492 return 16;
493 }
494 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
495 return 32;
496 }
497 return 64;
498}
499
Greg Daniel313c6952018-08-08 09:24:08 -0400500void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
501 VkPhysicalDevice physDev,
502 const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700503 const VkPhysicalDeviceMemoryProperties& memoryProperties,
Greg Daniel313c6952018-08-08 09:24:08 -0400504 const VkPhysicalDeviceFeatures2& features,
505 const GrVkExtensions& extensions) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400506 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
507 // need for us ever to support that amount, and it makes tests which tests all the vertex
508 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
509 // we ever find that need.
510 static const uint32_t kMaxVertexAttributes = 64;
511 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400512
egdanield5e3b9e2016-03-08 12:19:54 -0800513 // We could actually query and get a max size for each config, however maxImageDimension2D will
514 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700515 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
516 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
Adrienne Walker724afe82018-05-15 11:36:26 -0700517 if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
518 fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
519 }
520 // Our render targets are always created with textures as the color
521 // attachment, hence this min:
522 fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
egdanield5e3b9e2016-03-08 12:19:54 -0800523
Chris Dalton2612bae2018-02-22 13:41:37 -0700524 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
525 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
526
egdanield5e3b9e2016-03-08 12:19:54 -0800527 // Assuming since we will always map in the end to upload the data we might as well just map
528 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700529 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800530
Brian Salomon105d7c22019-04-16 13:46:14 -0400531 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag | kAsyncRead_MapFlag;
egdanield5e3b9e2016-03-08 12:19:54 -0800532
egdanield5e3b9e2016-03-08 12:19:54 -0800533 fOversizedStencilSupport = true;
Greg Daniel313c6952018-08-08 09:24:08 -0400534
535 if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
536 this->supportsPhysicalDeviceProperties2()) {
537
538 VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
539 blendProps.sType =
540 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
541 blendProps.pNext = nullptr;
542
543 VkPhysicalDeviceProperties2 props;
544 props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
545 props.pNext = &blendProps;
546
547 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
548
549 if (blendProps.advancedBlendAllOperations == VK_TRUE) {
550 fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
551
552 auto blendFeatures =
553 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
554 features,
555 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
556 if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
557 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
558 } else {
559 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
560 // require us to support self dependencies in our render passes.
561 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
562 }
563 }
564 }
egdanield5e3b9e2016-03-08 12:19:54 -0800565}
566
Greg Daniel36443602018-08-02 12:51:52 -0400567void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
Greg Daniela0651ac2018-08-08 09:23:18 -0400568 const VkPhysicalDeviceFeatures2& features) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500569 GrShaderCaps* shaderCaps = fShaderCaps.get();
570 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700571
Greg Daniel164a9f02016-02-22 09:56:40 -0500572
573 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
574 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
575 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500576 // Vulkan doesn't support a single channel format stored in alpha.
577 if (GrPixelConfigIsAlphaOnly(config) &&
578 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500579 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
580 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500581 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500582 if (kGray_8_GrPixelConfig == config ||
583 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500584 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
585 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700586 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
587 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
588 // or writing to outputs. Since we're not actually changing the data at all, the
589 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500590 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
591 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
Greg Danielf259b8b2019-02-14 09:03:43 -0500592 } else if (kRGB_888X_GrPixelConfig == config) {
593 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGB1();
egdaniel3fe03272016-08-15 10:59:17 -0700594 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500595 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700596 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500597 }
598 }
egdaniel67acb832016-02-26 08:32:20 -0800599
egdanield5e3b9e2016-03-08 12:19:54 -0800600 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500601 shaderCaps->fUsesPrecisionModifiers = true;
602 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700603 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
604 // to be true with Vulkan as well.
605 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800606
607 // GrShaderCaps
608
Brian Salomon1edc5b92016-11-29 13:43:46 -0500609 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600610
Ethan Nicholas6ac8d362019-01-22 21:43:55 +0000611 // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
612 // shaderCaps->fGeometryShaderSupport =
613 // shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
egdanield632bb42016-03-30 12:06:48 -0700614
Greg Daniela0651ac2018-08-08 09:23:18 -0400615 shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
egdanield632bb42016-03-30 12:06:48 -0700616
Brian Salomon1edc5b92016-11-29 13:43:46 -0500617 shaderCaps->fIntegerSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600618 shaderCaps->fVertexIDSupport = true;
Chris Dalton7c7ff032018-03-28 20:09:58 -0600619 shaderCaps->fFPManipulationSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800620
cdaltona6b92ad2016-04-11 12:03:08 -0700621 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700622 shaderCaps->fFloatIs32Bits = true;
623 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700624
Brian Salomon1edc5b92016-11-29 13:43:46 -0500625 shaderCaps->fMaxFragmentSamplers = SkTMin(
626 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
627 properties.limits.maxPerStageDescriptorSamplers),
628 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500629}
630
egdaniel8f1dcaa2016-04-01 10:10:45 -0700631bool stencil_format_supported(const GrVkInterface* interface,
632 VkPhysicalDevice physDev,
633 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500634 VkFormatProperties props;
635 memset(&props, 0, sizeof(VkFormatProperties));
636 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700637 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500638}
639
egdaniel8f1dcaa2016-04-01 10:10:45 -0700640void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
641 // List of legal stencil formats (though perhaps not supported on
642 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700643 // 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 -0700644 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500645 static const StencilFormat
646 // internal Format stencil bits total bits packed?
647 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700648 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
649 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500650
egdaniel8f1dcaa2016-04-01 10:10:45 -0700651 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400652 fPreferredStencilFormat = gS8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700653 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400654 fPreferredStencilFormat = gD24S8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700655 } else {
656 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
Ethan Nicholasf610bae2018-09-20 16:55:21 -0400657 fPreferredStencilFormat = gD32S8;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700658 }
659}
660
Greg Danielcaa795f2019-05-14 11:54:25 -0400661static bool format_is_srgb(VkFormat format) {
662 switch (format) {
663 case VK_FORMAT_R8G8B8A8_SRGB:
664 case VK_FORMAT_B8G8R8A8_SRGB:
665 return true;
666 case VK_FORMAT_R8G8B8A8_UNORM:
667 case VK_FORMAT_B8G8R8A8_UNORM:
668 case VK_FORMAT_R8G8B8A8_SINT:
669 case VK_FORMAT_R8G8B8_UNORM:
670 case VK_FORMAT_R8G8_UNORM:
671 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
672 case VK_FORMAT_R5G6B5_UNORM_PACK16:
673 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
Greg Danieleb4a8272019-05-16 16:52:55 -0400674 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
Greg Danielcaa795f2019-05-14 11:54:25 -0400675 case VK_FORMAT_R8_UNORM:
676 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
677 case VK_FORMAT_R32G32B32A32_SFLOAT:
678 case VK_FORMAT_R32G32_SFLOAT:
679 case VK_FORMAT_R16G16B16A16_SFLOAT:
680 case VK_FORMAT_R16_SFLOAT:
Robert Phillipsfe18de52019-06-06 17:21:50 -0400681 // Experimental (for P016 and P010)
682 case VK_FORMAT_R16_UNORM:
683 case VK_FORMAT_R16G16_UNORM:
Greg Danielcaa795f2019-05-14 11:54:25 -0400684 return false;
685 default:
686 SK_ABORT("Unsupported VkFormat");
687 return false;
688 }
689}
690
691// These are all the valid VkFormats that we support in Skia. They are roughly order from most
692// frequently used to least to improve look up times in arrays.
693static constexpr VkFormat kVkFormats[] = {
694 VK_FORMAT_R8G8B8A8_UNORM,
695 VK_FORMAT_R8_UNORM,
696 VK_FORMAT_B8G8R8A8_UNORM,
697 VK_FORMAT_R5G6B5_UNORM_PACK16,
698 VK_FORMAT_R16G16B16A16_SFLOAT,
699 VK_FORMAT_R16_SFLOAT,
700 VK_FORMAT_R8G8B8A8_SINT,
701 VK_FORMAT_R8G8B8_UNORM,
702 VK_FORMAT_R8G8_UNORM,
703 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
704 VK_FORMAT_B4G4R4A4_UNORM_PACK16,
Greg Danieleb4a8272019-05-16 16:52:55 -0400705 VK_FORMAT_R4G4B4A4_UNORM_PACK16,
Greg Danielcaa795f2019-05-14 11:54:25 -0400706 VK_FORMAT_R32G32B32A32_SFLOAT,
707 VK_FORMAT_R32G32_SFLOAT,
708 VK_FORMAT_R8G8B8A8_SRGB,
709 VK_FORMAT_B8G8R8A8_SRGB,
Robert Phillipsfe18de52019-06-06 17:21:50 -0400710 VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK,
711
712 // Experimental (for P016 and P010)
713 VK_FORMAT_R16_UNORM,
714 VK_FORMAT_R16G16_UNORM,
Greg Danielcaa795f2019-05-14 11:54:25 -0400715};
716
717const GrVkCaps::FormatInfo& GrVkCaps::getFormatInfo(VkFormat format) const {
718 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
719 "Size of VkFormats array must match static value in header");
720 for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
721 if (kVkFormats[i] == format) {
722 return fFormatTable[i];
723 }
724 }
725 SK_ABORT("Invalid VkFormat");
726 static const FormatInfo kInvalidConfig;
727 return kInvalidConfig;
728}
729
730void GrVkCaps::initFormatTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000731 const VkPhysicalDeviceProperties& properties) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400732 static_assert(SK_ARRAY_COUNT(kVkFormats) == GrVkCaps::kNumVkFormats,
733 "Size of VkFormats array must match static value in header");
734 for (size_t i = 0; i < SK_ARRAY_COUNT(kVkFormats); ++i) {
735 VkFormat format = kVkFormats[i];
736 if (!format_is_srgb(format) || fSRGBSupport) {
737 fFormatTable[i].init(interface, physDev, properties, format);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700738 }
739 }
740}
741
Greg Danielcaa795f2019-05-14 11:54:25 -0400742void GrVkCaps::FormatInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700743 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
744 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
745 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700746
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400747 // Ganesh assumes that all renderable surfaces are also texturable
Greg Danielcaa795f2019-05-14 11:54:25 -0400748 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400749 *flags = *flags | kRenderable_Flag;
750 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700751 }
752
753 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
754 *flags = *flags | kBlitSrc_Flag;
755 }
756
757 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
758 *flags = *flags | kBlitDst_Flag;
759 }
760}
761
Greg Danielcaa795f2019-05-14 11:54:25 -0400762void GrVkCaps::FormatInfo::initSampleCounts(const GrVkInterface* interface,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400763 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000764 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400765 VkFormat format) {
766 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
767 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
768 VK_IMAGE_USAGE_SAMPLED_BIT |
769 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400770 VkImageFormatProperties properties;
771 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
772 format,
773 VK_IMAGE_TYPE_2D,
774 VK_IMAGE_TILING_OPTIMAL,
775 usage,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400776 0, // createFlags
Greg Daniel81e7bf82017-07-19 14:47:42 -0400777 &properties));
778 VkSampleCountFlags flags = properties.sampleCounts;
779 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400780 fColorSampleCounts.push_back(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400781 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000782 if (kImagination_VkVendor == physProps.vendorID) {
783 // MSAA does not work on imagination
784 return;
785 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400786 if (flags & VK_SAMPLE_COUNT_2_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400787 fColorSampleCounts.push_back(2);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400788 }
789 if (flags & VK_SAMPLE_COUNT_4_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400790 fColorSampleCounts.push_back(4);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400791 }
792 if (flags & VK_SAMPLE_COUNT_8_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400793 fColorSampleCounts.push_back(8);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400794 }
795 if (flags & VK_SAMPLE_COUNT_16_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400796 fColorSampleCounts.push_back(16);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400797 }
798 if (flags & VK_SAMPLE_COUNT_32_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400799 fColorSampleCounts.push_back(32);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400800 }
801 if (flags & VK_SAMPLE_COUNT_64_BIT) {
Mike Reedb5475792018-08-08 16:17:42 -0400802 fColorSampleCounts.push_back(64);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400803 }
804}
805
Greg Danielcaa795f2019-05-14 11:54:25 -0400806void GrVkCaps::FormatInfo::init(const GrVkInterface* interface,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700807 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000808 const VkPhysicalDeviceProperties& properties,
Greg Danielcaa795f2019-05-14 11:54:25 -0400809 VkFormat format) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700810 VkFormatProperties props;
811 memset(&props, 0, sizeof(VkFormatProperties));
812 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
Greg Danielcaa795f2019-05-14 11:54:25 -0400813 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
814 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400815 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000816 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400817 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500818}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400819
Robert Phillips39ef2ef2019-05-15 08:45:53 -0400820bool GrVkCaps::isFormatTexturable(VkFormat format) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400821 if (!GrVkFormatIsSupported(format)) {
822 return false;
823 }
824
825 const FormatInfo& info = this->getFormatInfo(format);
826 return SkToBool(FormatInfo::kTextureable_Flag & info.fOptimalFlags);
827}
828
829bool GrVkCaps::isConfigTexturable(GrPixelConfig config) const {
830 VkFormat format;
831 if (!GrPixelConfigToVkFormat(config, &format)) {
832 return false;
833 }
Robert Phillips39ef2ef2019-05-15 08:45:53 -0400834 return this->isFormatTexturable(format);
835}
836
837bool GrVkCaps::isFormatRenderable(VkFormat format) const {
838 return this->maxRenderTargetSampleCount(format) > 0;
Greg Danielcaa795f2019-05-14 11:54:25 -0400839}
840
Brian Salomonbdecacf2018-02-02 20:32:49 -0500841int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400842 // Currently we don't allow RGB_888X to be renderable because we don't have a way to handle
843 // blends that reference dst alpha when the values in the dst alpha channel are uninitialized.
844 if (config == kRGB_888X_GrPixelConfig) {
845 return 0;
846 }
847
848 VkFormat format;
849 if (!GrPixelConfigToVkFormat(config, &format)) {
850 return 0;
851 }
852
853 return this->getRenderTargetSampleCount(requestedCount, format);
854}
855
856int GrVkCaps::getRenderTargetSampleCount(int requestedCount, VkFormat format) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500857 requestedCount = SkTMax(1, requestedCount);
Greg Danielcaa795f2019-05-14 11:54:25 -0400858
859 const FormatInfo& info = this->getFormatInfo(format);
860
861 int count = info.fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500862
863 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400864 return 0;
865 }
866
Brian Salomonbdecacf2018-02-02 20:32:49 -0500867 if (1 == requestedCount) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400868 SkASSERT(info.fColorSampleCounts.count() && info.fColorSampleCounts[0] == 1);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500869 return 1;
870 }
871
Greg Daniel81e7bf82017-07-19 14:47:42 -0400872 for (int i = 0; i < count; ++i) {
Greg Danielcaa795f2019-05-14 11:54:25 -0400873 if (info.fColorSampleCounts[i] >= requestedCount) {
874 return info.fColorSampleCounts[i];
Greg Daniel81e7bf82017-07-19 14:47:42 -0400875 }
876 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500877 return 0;
878}
879
880int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
Greg Danielcaa795f2019-05-14 11:54:25 -0400881 // Currently we don't allow RGB_888X to be renderable because we don't have a way to handle
882 // blends that reference dst alpha when the values in the dst alpha channel are uninitialized.
883 if (config == kRGB_888X_GrPixelConfig) {
884 return 0;
885 }
886
887 VkFormat format;
888 if (!GrPixelConfigToVkFormat(config, &format)) {
889 return 0;
890 }
891 return this->maxRenderTargetSampleCount(format);
892}
893
894int GrVkCaps::maxRenderTargetSampleCount(VkFormat format) const {
895 const FormatInfo& info = this->getFormatInfo(format);
896
897 const auto& table = info.fColorSampleCounts;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500898 if (!table.count()) {
899 return 0;
900 }
901 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500902}
903
Greg Daniela51e93c2019-03-25 12:30:45 -0400904bool GrVkCaps::surfaceSupportsReadPixels(const GrSurface* surface) const {
905 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
906 // We can't directly read from a VkImage that has a ycbcr sampler.
907 if (tex->ycbcrConversionInfo().isValid()) {
908 return false;
909 }
910 }
911 return true;
912}
913
Brian Salomonc67c31c2018-12-06 10:00:03 -0500914bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
Brian Salomon3d86a192018-02-27 16:46:11 -0500915 if (auto rt = surface->asRenderTarget()) {
916 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
917 }
Greg Daniela51e93c2019-03-25 12:30:45 -0400918 // We can't write to a texture that has a ycbcr sampler.
919 if (auto tex = static_cast<const GrVkTexture*>(surface->asTexture())) {
920 // We can't directly read from a VkImage that has a ycbcr sampler.
921 if (tex->ycbcrConversionInfo().isValid()) {
922 return false;
923 }
924 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500925 return true;
926}
927
Robert Phillipsc1bee132019-02-06 16:04:34 -0500928static GrPixelConfig validate_image_info(VkFormat format, SkColorType ct, bool hasYcbcrConversion) {
Greg Daniel14c55c22018-12-04 11:25:03 -0500929 if (format == VK_FORMAT_UNDEFINED) {
930 // If the format is undefined then it is only valid as an external image which requires that
931 // we have a valid VkYcbcrConversion.
932 if (hasYcbcrConversion) {
933 // 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 -0400934 // values for external textures. However, for read pixels we will draw to a non ycbcr
935 // texture of this config so we set RGBA here for that.
Brian Salomonf391d0f2018-12-14 09:18:50 -0500936 return kRGBA_8888_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500937 } else {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500938 return kUnknown_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500939 }
940 }
941
942 if (hasYcbcrConversion) {
943 // We only support having a ycbcr conversion for external images.
Brian Salomonf391d0f2018-12-14 09:18:50 -0500944 return kUnknown_GrPixelConfig;
Greg Daniel14c55c22018-12-04 11:25:03 -0500945 }
946
Greg Danielf5d87582017-12-18 14:48:15 -0500947 switch (ct) {
948 case kUnknown_SkColorType:
Brian Salomonf391d0f2018-12-14 09:18:50 -0500949 break;
Greg Danielf5d87582017-12-18 14:48:15 -0500950 case kAlpha_8_SkColorType:
951 if (VK_FORMAT_R8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500952 return kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500953 }
954 break;
955 case kRGB_565_SkColorType:
956 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500957 return kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500958 }
959 break;
960 case kARGB_4444_SkColorType:
Greg Danieleb4a8272019-05-16 16:52:55 -0400961 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format ||
962 VK_FORMAT_R4G4B4A4_UNORM_PACK16 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500963 return kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500964 }
965 break;
966 case kRGBA_8888_SkColorType:
967 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500968 return kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500969 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500970 return kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500971 }
972 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500973 case kRGB_888x_SkColorType:
Greg Daniel475eb702018-09-28 14:16:50 -0400974 if (VK_FORMAT_R8G8B8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500975 return kRGB_888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -0400976 }
Greg Danielf259b8b2019-02-14 09:03:43 -0500977 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
978 return kRGB_888X_GrPixelConfig;
979 }
Greg Daniel475eb702018-09-28 14:16:50 -0400980 break;
Greg Danielf5d87582017-12-18 14:48:15 -0500981 case kBGRA_8888_SkColorType:
982 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500983 return kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500984 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500985 return kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500986 }
987 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500988 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500989 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -0500990 return kRGBA_1010102_GrPixelConfig;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500991 }
992 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500993 case kRGB_101010x_SkColorType:
Brian Salomonf391d0f2018-12-14 09:18:50 -0500994 return kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500995 case kGray_8_SkColorType:
996 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;
Brian Osmand0626aa2019-03-11 15:28:06 -04001000 case kRGBA_F16Norm_SkColorType:
Mike Kleinb70990e2019-02-28 10:03:27 -06001001 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Brian Osmand0626aa2019-03-11 15:28:06 -04001002 return kRGBA_half_Clamped_GrPixelConfig;
Mike Kleinb70990e2019-02-28 10:03:27 -06001003 }
1004 break;
Greg Danielf5d87582017-12-18 14:48:15 -05001005 case kRGBA_F16_SkColorType:
1006 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;
Mike Klein37854712018-06-26 11:43:06 -04001010 case kRGBA_F32_SkColorType:
1011 if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001012 return kRGBA_float_GrPixelConfig;
Mike Klein37854712018-06-26 11:43:06 -04001013 }
1014 break;
Greg Danielf5d87582017-12-18 14:48:15 -05001015 }
1016
Brian Salomonf391d0f2018-12-14 09:18:50 -05001017 return kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -05001018}
1019
Brian Salomonf391d0f2018-12-14 09:18:50 -05001020GrPixelConfig GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt,
1021 SkColorType ct) const {
Greg Daniel323fbcf2018-04-10 13:46:30 -04001022 GrVkImageInfo imageInfo;
1023 if (!rt.getVkImageInfo(&imageInfo)) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001024 return kUnknown_GrPixelConfig;
Robert Phillipsfc711a22018-02-13 17:03:00 -05001025 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001026 return validate_image_info(imageInfo.fFormat, ct, imageInfo.fYcbcrConversionInfo.isValid());
Robert Phillipsfc711a22018-02-13 17:03:00 -05001027}
1028
Brian Salomonf391d0f2018-12-14 09:18:50 -05001029GrPixelConfig GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format,
1030 SkColorType ct) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -05001031 const VkFormat* vkFormat = format.getVkFormat();
Greg Daniel14c55c22018-12-04 11:25:03 -05001032 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
1033 if (!vkFormat || !ycbcrInfo) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001034 return kUnknown_GrPixelConfig;
Robert Phillipsfc711a22018-02-13 17:03:00 -05001035 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001036 return validate_image_info(*vkFormat, ct, ycbcrInfo->isValid());
Greg Danielfaa095e2017-12-19 13:15:02 -05001037}
Greg Danielf5d87582017-12-18 14:48:15 -05001038
Brian Salomonf391d0f2018-12-14 09:18:50 -05001039static GrPixelConfig get_yuva_config(VkFormat vkFormat) {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001040 switch (vkFormat) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001041 case VK_FORMAT_R8_UNORM:
1042 return kAlpha_8_as_Red_GrPixelConfig;
1043 case VK_FORMAT_R8G8B8A8_UNORM:
1044 return kRGBA_8888_GrPixelConfig;
1045 case VK_FORMAT_R8G8B8_UNORM:
1046 return kRGB_888_GrPixelConfig;
1047 case VK_FORMAT_R8G8_UNORM:
1048 return kRG_88_GrPixelConfig;
1049 case VK_FORMAT_B8G8R8A8_UNORM:
1050 return kBGRA_8888_GrPixelConfig;
Robert Phillips2dd1b472019-03-21 09:00:20 -04001051 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1052 return kRGBA_1010102_GrPixelConfig;
Robert Phillipsfe18de52019-06-06 17:21:50 -04001053 // Experimental (for P016 and P010)
1054 case VK_FORMAT_R16_UNORM:
1055 return kR_16_GrPixelConfig;
1056 case VK_FORMAT_R16G16_UNORM:
1057 return kRG_1616_GrPixelConfig;
Brian Salomonf391d0f2018-12-14 09:18:50 -05001058 default:
1059 return kUnknown_GrPixelConfig;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -04001060 }
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -04001061}
1062
Brian Salomonf391d0f2018-12-14 09:18:50 -05001063GrPixelConfig GrVkCaps::getYUVAConfigFromBackendFormat(const GrBackendFormat& format) const {
Jim Van Verth9bf81202018-10-30 15:53:36 -04001064 const VkFormat* vkFormat = format.getVkFormat();
1065 if (!vkFormat) {
Brian Salomonf391d0f2018-12-14 09:18:50 -05001066 return kUnknown_GrPixelConfig;
Jim Van Verth9bf81202018-10-30 15:53:36 -04001067 }
Brian Salomonf391d0f2018-12-14 09:18:50 -05001068 return get_yuva_config(*vkFormat);
Timothy Liang036fdfe2018-06-28 15:50:36 -04001069}
Greg Daniel4065d452018-11-16 15:43:41 -05001070
1071GrBackendFormat GrVkCaps::getBackendFormatFromGrColorType(GrColorType ct,
1072 GrSRGBEncoded srgbEncoded) const {
1073 GrPixelConfig config = GrColorTypeToPixelConfig(ct, srgbEncoded);
1074 if (config == kUnknown_GrPixelConfig) {
1075 return GrBackendFormat();
1076 }
1077 VkFormat format;
1078 if (!GrPixelConfigToVkFormat(config, &format)) {
1079 return GrBackendFormat();
1080 }
1081 return GrBackendFormat::MakeVk(format);
1082}
Timothy Liang036fdfe2018-06-28 15:50:36 -04001083
Greg Danieleb4a8272019-05-16 16:52:55 -04001084#ifdef SK_DEBUG
1085static bool format_color_type_valid_pair(VkFormat vkFormat, GrColorType colorType) {
1086 switch (colorType) {
1087 case GrColorType::kUnknown:
1088 return false;
1089 case GrColorType::kAlpha_8:
1090 return VK_FORMAT_R8_UNORM == vkFormat;
1091 case GrColorType::kRGB_565:
1092 return VK_FORMAT_R5G6B5_UNORM_PACK16 == vkFormat;
1093 case GrColorType::kABGR_4444:
1094 return VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat ||
1095 VK_FORMAT_R4G4B4A4_UNORM_PACK16 == vkFormat;
1096 case GrColorType::kRGBA_8888:
1097 return VK_FORMAT_R8G8B8A8_UNORM == vkFormat || VK_FORMAT_R8G8B8A8_SRGB == vkFormat;
1098 case GrColorType::kRGB_888x:
1099 return VK_FORMAT_R8G8B8_UNORM == vkFormat || VK_FORMAT_R8G8B8A8_UNORM == vkFormat;
1100 case GrColorType::kRG_88:
1101 return VK_FORMAT_R8G8_UNORM == vkFormat;
1102 case GrColorType::kBGRA_8888:
1103 return VK_FORMAT_B8G8R8A8_UNORM == vkFormat || VK_FORMAT_B8G8R8A8_SRGB == vkFormat;
1104 case GrColorType::kRGBA_1010102:
1105 return VK_FORMAT_A2B10G10R10_UNORM_PACK32 == vkFormat;
1106 case GrColorType::kGray_8:
1107 return VK_FORMAT_R8_UNORM == vkFormat;
1108 case GrColorType::kAlpha_F16:
1109 return VK_FORMAT_R16_SFLOAT == vkFormat;
1110 case GrColorType::kRGBA_F16:
1111 return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
1112 case GrColorType::kRGBA_F16_Clamped:
1113 return VK_FORMAT_R16G16B16A16_SFLOAT == vkFormat;
1114 case GrColorType::kRG_F32:
1115 return VK_FORMAT_R32G32_SFLOAT == vkFormat;
1116 case GrColorType::kRGBA_F32:
1117 return VK_FORMAT_R32G32B32A32_SFLOAT == vkFormat;
1118 case GrColorType::kRGB_ETC1:
1119 return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK == vkFormat;
Robert Phillipsfe18de52019-06-06 17:21:50 -04001120 // Experimental (for P016 and P010)
1121 case GrColorType::kR_16:
1122 return VK_FORMAT_R16_UNORM == vkFormat;
1123 case GrColorType::kRG_1616:
1124 return VK_FORMAT_R16G16_UNORM == vkFormat;
Greg Danieleb4a8272019-05-16 16:52:55 -04001125 }
Greg Daniel4f71ccc2019-05-17 10:56:46 -04001126 SK_ABORT("Unknown color type");
1127 return false;
Greg Danieleb4a8272019-05-16 16:52:55 -04001128}
1129#endif
1130
1131static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
1132 bool forOutput) {
1133 SkASSERT(format.getVkFormat());
1134 VkFormat vkFormat = *format.getVkFormat();
1135
1136 SkASSERT(format_color_type_valid_pair(vkFormat, colorType));
1137
1138 switch (colorType) {
1139 case GrColorType::kAlpha_8: // fall through
1140 case GrColorType::kAlpha_F16:
1141 if (forOutput) {
1142 return GrSwizzle::AAAA();
1143 } else {
1144 return GrSwizzle::RRRR();
1145 }
1146 case GrColorType::kGray_8:
1147 if (!forOutput) {
1148 return GrSwizzle::RRRA();
1149 }
1150 break;
1151 case GrColorType::kABGR_4444:
1152 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == vkFormat) {
1153 return GrSwizzle::BGRA();
1154 }
1155 break;
1156 case GrColorType::kRGB_888x:
1157 if (!forOutput) {
1158 return GrSwizzle::RGB1();
1159 }
1160 default:
1161 return GrSwizzle::RGBA();
1162 }
1163 return GrSwizzle::RGBA();
1164}
1165
1166GrSwizzle GrVkCaps::getTextureSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1167 return get_swizzle(format, colorType, false);
1168}
1169GrSwizzle GrVkCaps::getOutputSwizzle(const GrBackendFormat& format, GrColorType colorType) const {
1170 return get_swizzle(format, colorType, true);
1171}
1172
Brian Salomon26de56e2019-04-10 12:14:26 -04001173size_t GrVkCaps::onTransferFromOffsetAlignment(GrColorType bufferColorType) const {
Brian Salomona585fe92019-04-09 14:57:00 -04001174 // This GrColorType has 32 bpp but the Vulkan pixel format we use for with may have 24bpp
1175 // (VK_FORMAT_R8G8B8_...) or may be 32 bpp. We don't support post transforming the pixel data
1176 // for transfer-from currently and don't want to have to pass info about the src surface here.
1177 if (bufferColorType == GrColorType::kRGB_888x) {
1178 return false;
1179 }
1180 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
1181 // The VkBufferImageCopy bufferOffset field must be both a multiple of 4 and of a single texel.
1182 switch (bpp & 0b11) {
Brian Salomon26de56e2019-04-10 12:14:26 -04001183 // bpp is already a multiple of 4.
1184 case 0: return bpp;
1185 // bpp is a multiple of 2 but not 4.
1186 case 2: return 2 * bpp;
1187 // bpp is not a multiple of 2.
1188 default: return 4 * bpp;
Brian Salomona585fe92019-04-09 14:57:00 -04001189 }
Brian Salomona585fe92019-04-09 14:57:00 -04001190}