1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "GrVkCaps.h"
9 #include "GrBackendSurface.h"
10 #include "GrRenderTargetProxy.h"
11 #include "GrRenderTarget.h"
12 #include "GrShaderCaps.h"
13 #include "GrVkInterface.h"
14 #include "GrVkUtil.h"
15 #include "SkGr.h"
16 #include "vk/GrVkBackendContext.h"
17 #include "vk/GrVkExtensions.h"
18
GrVkCaps(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t instanceVersion,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions)19 GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
20 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
21 uint32_t instanceVersion, uint32_t physicalDeviceVersion,
22 const GrVkExtensions& extensions)
23 : INHERITED(contextOptions) {
24
25 /**************************************************************************
26 * GrCaps fields
27 **************************************************************************/
28 fMipMapSupport = true; // always available in Vulkan
29 fSRGBSupport = true; // always available in Vulkan
30 fNPOTTextureTileSupport = true; // always available in Vulkan
31 fDiscardRenderTargetSupport = true;
32 fReuseScratchTextures = true; //TODO: figure this out
33 fGpuTracingSupport = false; //TODO: figure this out
34 fCompressedTexSubImageSupport = true;
35 fOversizedStencilSupport = false; //TODO: figure this out
36 fInstanceAttribSupport = true;
37
38 fFenceSyncSupport = true; // always available in Vulkan
39 fCrossContextTextureSupport = true;
40 fHalfFloatVertexAttributeSupport = true;
41
42 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
43 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
44
45 fMaxRenderTargetSize = 4096; // minimum required by spec
46 fMaxTextureSize = 4096; // minimum required by spec
47
48 fDynamicStateArrayGeometryProcessorTextureSupport = true;
49
50 fShaderCaps.reset(new GrShaderCaps(contextOptions));
51
52 this->init(contextOptions, vkInterface, physDev, features, physicalDeviceVersion, extensions);
53 }
54
initDescForDstCopy(const GrRenderTargetProxy * src,GrSurfaceDesc * desc,GrSurfaceOrigin * origin,bool * rectsMustMatch,bool * disallowSubrect) const55 bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
56 GrSurfaceOrigin* origin, bool* rectsMustMatch,
57 bool* disallowSubrect) const {
58 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
59 *rectsMustMatch = false;
60 *disallowSubrect = false;
61
62 // 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.
65 *origin = src->origin();
66 desc->fConfig = src->config();
67 if (src->numColorSamples() > 1 || src->asTextureProxy()) {
68 desc->fFlags = kRenderTarget_GrSurfaceFlag;
69 } else {
70 // Just going to use CopyImage here
71 desc->fFlags = kNone_GrSurfaceFlags;
72 }
73
74 return true;
75 }
76
canCopyImage(GrPixelConfig dstConfig,int dstSampleCnt,GrSurfaceOrigin dstOrigin,GrPixelConfig srcConfig,int srcSampleCnt,GrSurfaceOrigin srcOrigin) const77 bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, GrSurfaceOrigin dstOrigin,
78 GrPixelConfig srcConfig, int srcSampleCnt,
79 GrSurfaceOrigin srcOrigin) const {
80 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
81 return false;
82 }
83
84 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
85 // as image usage flags.
86 if (srcOrigin != dstOrigin || GrBytesPerPixel(srcConfig) != GrBytesPerPixel(dstConfig)) {
87 return false;
88 }
89
90 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
91 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
92 return false;
93 }
94
95 return true;
96 }
97
canCopyAsBlit(GrPixelConfig dstConfig,int dstSampleCnt,bool dstIsLinear,GrPixelConfig srcConfig,int srcSampleCnt,bool srcIsLinear) const98 bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
99 GrPixelConfig srcConfig, int srcSampleCnt, bool srcIsLinear) const {
100 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
101 // as image usage flags.
102 if (!this->configCanBeDstofBlit(dstConfig, dstIsLinear) ||
103 !this->configCanBeSrcofBlit(srcConfig, srcIsLinear)) {
104 return false;
105 }
106
107 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
108 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
109 return false;
110 }
111
112 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
113 // resolved msaa though.
114 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
115 return false;
116 }
117
118 return true;
119 }
120
canCopyAsResolve(GrPixelConfig dstConfig,int dstSampleCnt,GrSurfaceOrigin dstOrigin,GrPixelConfig srcConfig,int srcSampleCnt,GrSurfaceOrigin srcOrigin) const121 bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt,
122 GrSurfaceOrigin dstOrigin, GrPixelConfig srcConfig,
123 int srcSampleCnt, GrSurfaceOrigin srcOrigin) const {
124 // The src surface must be multisampled.
125 if (srcSampleCnt <= 1) {
126 return false;
127 }
128
129 // The dst must not be multisampled.
130 if (dstSampleCnt > 1) {
131 return false;
132 }
133
134 // Surfaces must have the same format.
135 if (dstConfig != srcConfig) {
136 return false;
137 }
138
139 // Surfaces must have the same origin.
140 if (srcOrigin != dstOrigin) {
141 return false;
142 }
143
144 return true;
145 }
146
canCopyAsDraw(GrPixelConfig dstConfig,bool dstIsRenderable,GrPixelConfig srcConfig,bool srcIsTextureable) const147 bool GrVkCaps::canCopyAsDraw(GrPixelConfig dstConfig, bool dstIsRenderable,
148 GrPixelConfig srcConfig, bool srcIsTextureable) const {
149 // TODO: Make copySurfaceAsDraw handle the swizzle
150 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
151 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
152 return false;
153 }
154
155 // Make sure the dst is a render target and the src is a texture.
156 if (!dstIsRenderable || !srcIsTextureable) {
157 return false;
158 }
159
160 return true;
161 }
162
onCanCopySurface(const GrSurfaceProxy * dst,const GrSurfaceProxy * src,const SkIRect & srcRect,const SkIPoint & dstPoint) const163 bool GrVkCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
164 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
165 GrSurfaceOrigin dstOrigin = dst->origin();
166 GrSurfaceOrigin srcOrigin = src->origin();
167
168 GrPixelConfig dstConfig = dst->config();
169 GrPixelConfig srcConfig = src->config();
170
171 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
172 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
173 // tracking of external or rectangle textures in GL? For now we don't create linear textures
174 // internally, and I don't believe anyone is wrapping them.
175 bool srcIsLinear = false;
176 bool dstIsLinear = false;
177
178 int dstSampleCnt = 0;
179 int srcSampleCnt = 0;
180 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
181 // Copying to or from render targets that wrap a secondary command buffer is not allowed
182 // since they would require us to know the VkImage, which we don't have, as well as need us
183 // to stop and start the VkRenderPass which we don't have access to.
184 if (rtProxy->wrapsVkSecondaryCB()) {
185 return false;
186 }
187 dstSampleCnt = rtProxy->numColorSamples();
188 }
189 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
190 // Copying to or from render targets that wrap a secondary command buffer is not allowed
191 // since they would require us to know the VkImage, which we don't have, as well as need us
192 // to stop and start the VkRenderPass which we don't have access to.
193 if (rtProxy->wrapsVkSecondaryCB()) {
194 return false;
195 }
196 srcSampleCnt = rtProxy->numColorSamples();
197 }
198 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
199 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
200
201 return this->canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
202 srcConfig, srcSampleCnt, srcOrigin) ||
203 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear,
204 srcConfig, srcSampleCnt, srcIsLinear) ||
205 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
206 srcConfig, srcSampleCnt, srcOrigin) ||
207 this->canCopyAsDraw(dstConfig, dstSampleCnt > 0,
208 srcConfig, SkToBool(src->asTextureProxy()));
209 }
210
get_extension_feature_struct(const VkPhysicalDeviceFeatures2 & features,VkStructureType type)211 template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
212 VkStructureType type) {
213 // All Vulkan structs that could be part of the features chain will start with the
214 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
215 // so we can get access to the pNext for the next struct.
216 struct CommonVulkanHeader {
217 VkStructureType sType;
218 void* pNext;
219 };
220
221 void* pNext = features.pNext;
222 while (pNext) {
223 CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
224 if (header->sType == type) {
225 return static_cast<T*>(pNext);
226 }
227 pNext = header->pNext;
228 }
229 return nullptr;
230 }
231
init(const GrContextOptions & contextOptions,const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceFeatures2 & features,uint32_t physicalDeviceVersion,const GrVkExtensions & extensions)232 void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
233 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
234 uint32_t physicalDeviceVersion, const GrVkExtensions& extensions) {
235
236 VkPhysicalDeviceProperties properties;
237 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
238
239 VkPhysicalDeviceMemoryProperties memoryProperties;
240 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
241
242 SkASSERT(physicalDeviceVersion <= properties.apiVersion);
243
244 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
245 extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
246 fSupportsPhysicalDeviceProperties2 = true;
247 }
248
249 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
250 extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
251 fSupportsMemoryRequirements2 = true;
252 }
253
254 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
255 extensions.hasExtension(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, 1)) {
256 fSupportsBindMemory2 = true;
257 }
258
259 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
260 extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
261 fSupportsMaintenance1 = true;
262 }
263
264 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
265 extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
266 fSupportsMaintenance2 = true;
267 }
268
269 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
270 extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
271 fSupportsMaintenance3 = true;
272 }
273
274 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
275 (extensions.hasExtension(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, 1) &&
276 this->supportsMemoryRequirements2())) {
277 fSupportsDedicatedAllocation = true;
278 }
279
280 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
281 (extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, 1) &&
282 this->supportsPhysicalDeviceProperties2() &&
283 extensions.hasExtension(VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, 1) &&
284 this->supportsDedicatedAllocation())) {
285 fSupportsExternalMemory = true;
286 }
287
288 #ifdef SK_BUILD_FOR_ANDROID
289 // Currently Adreno devices are not supporting the QUEUE_FAMILY_FOREIGN_EXTENSION, so until they
290 // do we don't explicitly require it here even the spec says it is required.
291 if (extensions.hasExtension(
292 VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, 2) &&
293 /* extensions.hasExtension(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, 1) &&*/
294 this->supportsExternalMemory() &&
295 this->supportsBindMemory2()) {
296 fSupportsAndroidHWBExternalMemory = true;
297 fSupportsAHardwareBufferImages = true;
298 }
299 #endif
300
301 auto ycbcrFeatures =
302 get_extension_feature_struct<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(
303 features,
304 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES);
305 if (ycbcrFeatures && ycbcrFeatures->samplerYcbcrConversion &&
306 fSupportsAndroidHWBExternalMemory &&
307 (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
308 (extensions.hasExtension(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME, 1) &&
309 this->supportsMaintenance1() &&
310 this->supportsBindMemory2() &&
311 this->supportsMemoryRequirements2() &&
312 this->supportsPhysicalDeviceProperties2()))) {
313 fSupportsYcbcrConversion = true;
314 }
315 // We always push back the default GrVkYcbcrConversionInfo so that the case of no conversion
316 // will return a key of 0.
317 fYcbcrInfos.push_back(GrVkYcbcrConversionInfo());
318
319 this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
320 this->initShaderCaps(properties, features);
321
322 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
323 #if defined(SK_CPU_X86)
324 // We need to do this before initing the config table since it uses fSRGBSupport
325 if (kImagination_VkVendor == properties.vendorID) {
326 fSRGBSupport = false;
327 }
328 #endif
329 }
330
331 if (kQualcomm_VkVendor == properties.vendorID) {
332 // A "clear" load for the CCPR atlas runs faster on QC than a "discard" load followed by a
333 // scissored clear.
334 // On NVIDIA and Intel, the discard load followed by clear is faster.
335 // TODO: Evaluate on ARM, Imagination, and ATI.
336 fPreferFullscreenClears = true;
337 }
338
339 this->initConfigTable(vkInterface, physDev, properties);
340 this->initStencilFormat(vkInterface, physDev);
341
342 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
343 this->applyDriverCorrectnessWorkarounds(properties);
344 }
345
346 // On nexus player we disable suballocating VkImage memory since we've seen large slow downs on
347 // bot run times.
348 if (kImagination_VkVendor == properties.vendorID) {
349 fShouldAlwaysUseDedicatedImageMemory = true;
350 }
351
352 this->applyOptionsOverrides(contextOptions);
353 fShaderCaps->applyOptionsOverrides(contextOptions);
354 }
355
applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties & properties)356 void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
357 if (kQualcomm_VkVendor == properties.vendorID) {
358 fMustDoCopiesFromOrigin = true;
359 }
360
361 #if defined(SK_BUILD_FOR_WIN)
362 if (kNvidia_VkVendor == properties.vendorID || kIntel_VkVendor == properties.vendorID) {
363 fMustSleepOnTearDown = true;
364 }
365 #elif defined(SK_BUILD_FOR_ANDROID)
366 if (kImagination_VkVendor == properties.vendorID) {
367 fMustSleepOnTearDown = true;
368 }
369 #endif
370
371 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
372 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
373 if (kAMD_VkVendor == properties.vendorID) {
374 fNewCBOnPipelineChange = true;
375 }
376
377 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
378 if (kARM_VkVendor == properties.vendorID) {
379 fShouldAlwaysUseDedicatedImageMemory = true;
380 }
381
382 ////////////////////////////////////////////////////////////////////////////
383 // GrCaps workarounds
384 ////////////////////////////////////////////////////////////////////////////
385
386 if (kARM_VkVendor == properties.vendorID) {
387 fInstanceAttribSupport = false;
388 fAvoidWritePixelsFastPath = true; // bugs.skia.org/8064
389 }
390
391 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
392 if (kAMD_VkVendor == properties.vendorID) {
393 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
394 }
395
396 ////////////////////////////////////////////////////////////////////////////
397 // GrShaderCaps workarounds
398 ////////////////////////////////////////////////////////////////////////////
399
400 if (kImagination_VkVendor == properties.vendorID) {
401 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
402 }
403 }
404
get_max_sample_count(VkSampleCountFlags flags)405 int get_max_sample_count(VkSampleCountFlags flags) {
406 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
407 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
408 return 0;
409 }
410 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
411 return 2;
412 }
413 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
414 return 4;
415 }
416 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
417 return 8;
418 }
419 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
420 return 16;
421 }
422 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
423 return 32;
424 }
425 return 64;
426 }
427
initGrCaps(const GrVkInterface * vkInterface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceMemoryProperties & memoryProperties,const VkPhysicalDeviceFeatures2 & features,const GrVkExtensions & extensions)428 void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
429 VkPhysicalDevice physDev,
430 const VkPhysicalDeviceProperties& properties,
431 const VkPhysicalDeviceMemoryProperties& memoryProperties,
432 const VkPhysicalDeviceFeatures2& features,
433 const GrVkExtensions& extensions) {
434 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
435 // need for us ever to support that amount, and it makes tests which tests all the vertex
436 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
437 // we ever find that need.
438 static const uint32_t kMaxVertexAttributes = 64;
439 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
440
441 // We could actually query and get a max size for each config, however maxImageDimension2D will
442 // give the minimum max size across all configs. So for simplicity we will use that for now.
443 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
444 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
445 if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
446 fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
447 }
448 // Our render targets are always created with textures as the color
449 // attachment, hence this min:
450 fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
451
452 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
453 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
454
455 // Assuming since we will always map in the end to upload the data we might as well just map
456 // from the get go. There is no hard data to suggest this is faster or slower.
457 fBufferMapThreshold = 0;
458
459 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
460
461 fOversizedStencilSupport = true;
462
463 if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
464 this->supportsPhysicalDeviceProperties2()) {
465
466 VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
467 blendProps.sType =
468 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
469 blendProps.pNext = nullptr;
470
471 VkPhysicalDeviceProperties2 props;
472 props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
473 props.pNext = &blendProps;
474
475 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
476
477 if (blendProps.advancedBlendAllOperations == VK_TRUE) {
478 fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
479
480 auto blendFeatures =
481 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
482 features,
483 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
484 if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
485 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
486 } else {
487 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
488 // require us to support self dependencies in our render passes.
489 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
490 }
491 }
492 }
493 }
494
initShaderCaps(const VkPhysicalDeviceProperties & properties,const VkPhysicalDeviceFeatures2 & features)495 void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
496 const VkPhysicalDeviceFeatures2& features) {
497 GrShaderCaps* shaderCaps = fShaderCaps.get();
498 shaderCaps->fVersionDeclString = "#version 330\n";
499
500
501 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
502 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
503 GrPixelConfig config = static_cast<GrPixelConfig>(i);
504 // Vulkan doesn't support a single channel format stored in alpha.
505 if (GrPixelConfigIsAlphaOnly(config) &&
506 kAlpha_8_as_Alpha_GrPixelConfig != config) {
507 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
508 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
509 } else {
510 if (kGray_8_GrPixelConfig == config ||
511 kGray_8_as_Red_GrPixelConfig == config) {
512 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
513 } else if (kRGBA_4444_GrPixelConfig == config) {
514 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
515 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
516 // or writing to outputs. Since we're not actually changing the data at all, the
517 // only extra work is the swizzle in the shader for all operations.
518 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
519 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
520 } else {
521 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
522 }
523 }
524 }
525
526 // Vulkan is based off ES 3.0 so the following should all be supported
527 shaderCaps->fUsesPrecisionModifiers = true;
528 shaderCaps->fFlatInterpolationSupport = true;
529 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
530 // to be true with Vulkan as well.
531 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
532
533 // GrShaderCaps
534
535 shaderCaps->fShaderDerivativeSupport = true;
536
537 // FIXME: http://skbug.com/7733: Disable geometry shaders until Intel/Radeon GMs draw correctly.
538 // shaderCaps->fGeometryShaderSupport =
539 // shaderCaps->fGSInvocationsSupport = features.features.geometryShader;
540
541 shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
542
543 shaderCaps->fIntegerSupport = true;
544 shaderCaps->fVertexIDSupport = true;
545 shaderCaps->fFPManipulationSupport = true;
546
547 // Assume the minimum precisions mandated by the SPIR-V spec.
548 shaderCaps->fFloatIs32Bits = true;
549 shaderCaps->fHalfIs32Bits = false;
550
551 // SPIR-V supports unsigned integers.
552 shaderCaps->fUnsignedSupport = true;
553
554 shaderCaps->fMaxFragmentSamplers = SkTMin(
555 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
556 properties.limits.maxPerStageDescriptorSamplers),
557 (uint32_t)INT_MAX);
558 }
559
stencil_format_supported(const GrVkInterface * interface,VkPhysicalDevice physDev,VkFormat format)560 bool stencil_format_supported(const GrVkInterface* interface,
561 VkPhysicalDevice physDev,
562 VkFormat format) {
563 VkFormatProperties props;
564 memset(&props, 0, sizeof(VkFormatProperties));
565 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
566 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
567 }
568
initStencilFormat(const GrVkInterface * interface,VkPhysicalDevice physDev)569 void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
570 // List of legal stencil formats (though perhaps not supported on
571 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
572 // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
573 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
574 static const StencilFormat
575 // internal Format stencil bits total bits packed?
576 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
577 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
578 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
579
580 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
581 fPreferredStencilFormat = gS8;
582 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
583 fPreferredStencilFormat = gD24S8;
584 } else {
585 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
586 fPreferredStencilFormat = gD32S8;
587 }
588 }
589
initConfigTable(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties)590 void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
591 const VkPhysicalDeviceProperties& properties) {
592 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
593 VkFormat format;
594 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
595 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
596 fConfigTable[i].init(interface, physDev, properties, format);
597 }
598 }
599 }
600 }
601
InitConfigFlags(VkFormatFeatureFlags vkFlags,uint16_t * flags)602 void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
603 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
604 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
605 *flags = *flags | kTextureable_Flag;
606
607 // Ganesh assumes that all renderable surfaces are also texturable
608 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
609 *flags = *flags | kRenderable_Flag;
610 }
611 }
612
613 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
614 *flags = *flags | kBlitSrc_Flag;
615 }
616
617 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
618 *flags = *flags | kBlitDst_Flag;
619 }
620 }
621
initSampleCounts(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & physProps,VkFormat format)622 void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
623 VkPhysicalDevice physDev,
624 const VkPhysicalDeviceProperties& physProps,
625 VkFormat format) {
626 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
627 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
628 VK_IMAGE_USAGE_SAMPLED_BIT |
629 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
630 VkImageFormatProperties properties;
631 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
632 format,
633 VK_IMAGE_TYPE_2D,
634 VK_IMAGE_TILING_OPTIMAL,
635 usage,
636 0, // createFlags
637 &properties));
638 VkSampleCountFlags flags = properties.sampleCounts;
639 if (flags & VK_SAMPLE_COUNT_1_BIT) {
640 fColorSampleCounts.push_back(1);
641 }
642 if (kImagination_VkVendor == physProps.vendorID) {
643 // MSAA does not work on imagination
644 return;
645 }
646 if (flags & VK_SAMPLE_COUNT_2_BIT) {
647 fColorSampleCounts.push_back(2);
648 }
649 if (flags & VK_SAMPLE_COUNT_4_BIT) {
650 fColorSampleCounts.push_back(4);
651 }
652 if (flags & VK_SAMPLE_COUNT_8_BIT) {
653 fColorSampleCounts.push_back(8);
654 }
655 if (flags & VK_SAMPLE_COUNT_16_BIT) {
656 fColorSampleCounts.push_back(16);
657 }
658 if (flags & VK_SAMPLE_COUNT_32_BIT) {
659 fColorSampleCounts.push_back(32);
660 }
661 if (flags & VK_SAMPLE_COUNT_64_BIT) {
662 fColorSampleCounts.push_back(64);
663 }
664 }
665
init(const GrVkInterface * interface,VkPhysicalDevice physDev,const VkPhysicalDeviceProperties & properties,VkFormat format)666 void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
667 VkPhysicalDevice physDev,
668 const VkPhysicalDeviceProperties& properties,
669 VkFormat format) {
670 VkFormatProperties props;
671 memset(&props, 0, sizeof(VkFormatProperties));
672 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
673 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
674 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
675 if (fOptimalFlags & kRenderable_Flag) {
676 this->initSampleCounts(interface, physDev, properties, format);
677 }
678 }
679
getRenderTargetSampleCount(int requestedCount,GrPixelConfig config) const680 int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
681 requestedCount = SkTMax(1, requestedCount);
682 int count = fConfigTable[config].fColorSampleCounts.count();
683
684 if (!count) {
685 return 0;
686 }
687
688 if (1 == requestedCount) {
689 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
690 fConfigTable[config].fColorSampleCounts[0] == 1);
691 return 1;
692 }
693
694 for (int i = 0; i < count; ++i) {
695 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
696 return fConfigTable[config].fColorSampleCounts[i];
697 }
698 }
699 return 0;
700 }
701
maxRenderTargetSampleCount(GrPixelConfig config) const702 int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
703 const auto& table = fConfigTable[config].fColorSampleCounts;
704 if (!table.count()) {
705 return 0;
706 }
707 return table[table.count() - 1];
708 }
709
onSurfaceSupportsWritePixels(const GrSurface * surface) const710 bool GrVkCaps::onSurfaceSupportsWritePixels(const GrSurface* surface) const {
711 if (auto rt = surface->asRenderTarget()) {
712 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
713 }
714 return true;
715 }
716
validate_image_info(VkFormat format,SkColorType ct,bool hasYcbcrConversion)717 GrPixelConfig validate_image_info(VkFormat format, SkColorType ct, bool hasYcbcrConversion) {
718 if (format == VK_FORMAT_UNDEFINED) {
719 // If the format is undefined then it is only valid as an external image which requires that
720 // we have a valid VkYcbcrConversion.
721 if (hasYcbcrConversion) {
722 // We don't actually care what the color type or config are since we won't use those
723 // values for external textures, but since our code requires setting a config here
724 // just default it to RGBA.
725 return kRGBA_8888_GrPixelConfig;
726 } else {
727 return kUnknown_GrPixelConfig;
728 }
729 }
730
731 if (hasYcbcrConversion) {
732 // We only support having a ycbcr conversion for external images.
733 return kUnknown_GrPixelConfig;
734 }
735
736 switch (ct) {
737 case kUnknown_SkColorType:
738 break;
739 case kAlpha_8_SkColorType:
740 if (VK_FORMAT_R8_UNORM == format) {
741 return kAlpha_8_as_Red_GrPixelConfig;
742 }
743 break;
744 case kRGB_565_SkColorType:
745 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
746 return kRGB_565_GrPixelConfig;
747 }
748 break;
749 case kARGB_4444_SkColorType:
750 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
751 return kRGBA_4444_GrPixelConfig;
752 }
753 break;
754 case kRGBA_8888_SkColorType:
755 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
756 return kRGBA_8888_GrPixelConfig;
757 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
758 return kSRGBA_8888_GrPixelConfig;
759 }
760 break;
761 case kRGB_888x_SkColorType:
762 if (VK_FORMAT_R8G8B8_UNORM == format) {
763 return kRGB_888_GrPixelConfig;
764 }
765 break;
766 case kBGRA_8888_SkColorType:
767 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
768 return kBGRA_8888_GrPixelConfig;
769 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
770 return kSBGRA_8888_GrPixelConfig;
771 }
772 break;
773 case kRGBA_1010102_SkColorType:
774 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
775 return kRGBA_1010102_GrPixelConfig;
776 }
777 break;
778 case kRGB_101010x_SkColorType:
779 return kUnknown_GrPixelConfig;
780 case kGray_8_SkColorType:
781 if (VK_FORMAT_R8_UNORM == format) {
782 return kGray_8_as_Red_GrPixelConfig;
783 }
784 break;
785 case kRGBA_F16_SkColorType:
786 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
787 return kRGBA_half_GrPixelConfig;
788 }
789 break;
790 case kRGBA_F32_SkColorType:
791 if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
792 return kRGBA_float_GrPixelConfig;
793 }
794 break;
795 }
796
797 return kUnknown_GrPixelConfig;
798 }
799
validateBackendRenderTarget(const GrBackendRenderTarget & rt,SkColorType ct) const800 GrPixelConfig GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt,
801 SkColorType ct) const {
802 GrVkImageInfo imageInfo;
803 if (!rt.getVkImageInfo(&imageInfo)) {
804 return kUnknown_GrPixelConfig;
805 }
806 return validate_image_info(imageInfo.fFormat, ct, imageInfo.fYcbcrConversionInfo.isValid());
807 }
808
getConfigFromBackendFormat(const GrBackendFormat & format,SkColorType ct) const809 GrPixelConfig GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format,
810 SkColorType ct) const {
811 const VkFormat* vkFormat = format.getVkFormat();
812 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
813 if (!vkFormat || !ycbcrInfo) {
814 return kUnknown_GrPixelConfig;
815 }
816 return validate_image_info(*vkFormat, ct, ycbcrInfo->isValid());
817 }
818
get_yuva_config(VkFormat vkFormat)819 static GrPixelConfig get_yuva_config(VkFormat vkFormat) {
820 switch (vkFormat) {
821 case VK_FORMAT_R8_UNORM:
822 return kAlpha_8_as_Red_GrPixelConfig;
823 case VK_FORMAT_R8G8B8A8_UNORM:
824 return kRGBA_8888_GrPixelConfig;
825 case VK_FORMAT_R8G8B8_UNORM:
826 return kRGB_888_GrPixelConfig;
827 case VK_FORMAT_R8G8_UNORM:
828 return kRG_88_GrPixelConfig;
829 case VK_FORMAT_B8G8R8A8_UNORM:
830 return kBGRA_8888_GrPixelConfig;
831 default:
832 return kUnknown_GrPixelConfig;
833 }
834 }
835
getYUVAConfigFromBackendFormat(const GrBackendFormat & format) const836 GrPixelConfig GrVkCaps::getYUVAConfigFromBackendFormat(const GrBackendFormat& format) const {
837 const VkFormat* vkFormat = format.getVkFormat();
838 if (!vkFormat) {
839 return kUnknown_GrPixelConfig;
840 }
841 return get_yuva_config(*vkFormat);
842 }
843
getBackendFormatFromGrColorType(GrColorType ct,GrSRGBEncoded srgbEncoded) const844 GrBackendFormat GrVkCaps::getBackendFormatFromGrColorType(GrColorType ct,
845 GrSRGBEncoded srgbEncoded) const {
846 GrPixelConfig config = GrColorTypeToPixelConfig(ct, srgbEncoded);
847 if (config == kUnknown_GrPixelConfig) {
848 return GrBackendFormat();
849 }
850 VkFormat format;
851 if (!GrPixelConfigToVkFormat(config, &format)) {
852 return GrBackendFormat();
853 }
854 return GrBackendFormat::MakeVk(format);
855 }
856
857