blob: 28440b1bc82d6346884da8f4f29cdd0ac11c3749 [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
8#include "GrVkCaps.h"
Greg Danielf5d87582017-12-18 14:48:15 -05009#include "GrBackendSurface.h"
Robert Phillipsbf25d432017-04-07 10:08:53 -040010#include "GrRenderTargetProxy.h"
Brian Salomon3d86a192018-02-27 16:46:11 -050011#include "GrRenderTarget.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050012#include "GrShaderCaps.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040013#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkUtil.h"
jvanverthfd7bd452016-03-25 06:29:52 -070015#include "vk/GrVkBackendContext.h"
Greg Danielc0b03d82018-08-03 14:41:15 -040016#include "vk/GrVkExtensions.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017
18GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -040019 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Greg Danielc0b03d82018-08-03 14:41:15 -040020 uint32_t instanceVersion, const GrVkExtensions& extensions)
egdanielc5ec1402016-03-28 12:14:42 -070021 : INHERITED(contextOptions) {
egdaniel6fa0a912016-09-12 11:51:29 -070022 fMustDoCopiesFromOrigin = false;
egdanielfd016d72016-09-27 12:13:05 -070023 fMustSubmitCommandsBeforeCopyOp = false;
Greg Daniel80a08dd2017-01-20 10:45:49 -050024 fMustSleepOnTearDown = false;
Greg Daniele3cd6912017-05-17 11:15:55 -040025 fNewCBOnPipelineChange = false;
Greg Danielddc0c602018-06-18 11:26:30 -040026 fShouldAlwaysUseDedicatedImageMemory = false;
egdanielc5ec1402016-03-28 12:14:42 -070027
Greg Danielc0b03d82018-08-03 14:41:15 -040028 fSupportsPhysicalDeviceProperties2 = false;
29 fSupportsMemoryRequirements2 = false;
30 fSupportsMaintenance1 = false;
31 fSupportsMaintenance2 = false;
32 fSupportsMaintenance3 = false;
33
Greg Daniel164a9f02016-02-22 09:56:40 -050034 /**************************************************************************
35 * GrDrawTargetCaps fields
36 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070037 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070038 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070039 fNPOTTextureTileSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070040 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050041 fReuseScratchTextures = true; //TODO: figure this out
42 fGpuTracingSupport = false; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050043 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060044 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050045
Greg Daniel0fe4ed82018-03-15 12:27:29 +000046 fBlacklistCoverageCounting = true; // blacklisting ccpr until we work through a few issues.
jvanverth84741b32016-09-30 08:39:02 -070047 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050048 fCrossContextTextureSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050049
50 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070051 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050052
53 fMaxRenderTargetSize = 4096; // minimum required by spec
54 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050055
Brian Salomon94efbf52016-11-29 13:43:05 -050056 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050057
Greg Danielc0b03d82018-08-03 14:41:15 -040058 this->init(contextOptions, vkInterface, physDev, features, extensions);
Greg Daniel164a9f02016-02-22 09:56:40 -050059}
60
Robert Phillipsbf25d432017-04-07 10:08:53 -040061bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
Brian Salomon2a4f9832018-03-03 22:43:43 -050062 GrSurfaceOrigin* origin, bool* rectsMustMatch,
63 bool* disallowSubrect) const {
Eric Karl74480882017-04-03 14:49:05 -070064 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
65 *rectsMustMatch = false;
66 *disallowSubrect = false;
67
Brian Salomon467921e2017-03-06 16:17:12 -050068 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
69 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
70 // render target as well.
Brian Salomon2a4f9832018-03-03 22:43:43 -050071 *origin = src->origin();
Brian Salomon467921e2017-03-06 16:17:12 -050072 desc->fConfig = src->config();
Greg Daniel55fa6472018-03-16 16:13:10 -040073 if (src->numColorSamples() > 1 || src->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -050074 desc->fFlags = kRenderTarget_GrSurfaceFlag;
75 } else {
76 // Just going to use CopyImage here
77 desc->fFlags = kNone_GrSurfaceFlags;
78 }
79
80 return true;
81}
82
Greg Daniel25af6712018-04-25 10:44:38 -040083bool GrVkCaps::canCopyImage(GrPixelConfig dstConfig, int dstSampleCnt, GrSurfaceOrigin dstOrigin,
84 GrPixelConfig srcConfig, int srcSampleCnt,
85 GrSurfaceOrigin srcOrigin) const {
86 if ((dstSampleCnt > 1 || srcSampleCnt > 1) && dstSampleCnt != srcSampleCnt) {
87 return false;
88 }
89
90 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
91 // as image usage flags.
92 if (srcOrigin != dstOrigin || GrBytesPerPixel(srcConfig) != GrBytesPerPixel(dstConfig)) {
93 return false;
94 }
95
96 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
97 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
98 return false;
99 }
100
101 return true;
102}
103
104bool GrVkCaps::canCopyAsBlit(GrPixelConfig dstConfig, int dstSampleCnt, bool dstIsLinear,
105 GrPixelConfig srcConfig, int srcSampleCnt, bool srcIsLinear) const {
106 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
107 // as image usage flags.
108 if (!this->configCanBeDstofBlit(dstConfig, dstIsLinear) ||
109 !this->configCanBeSrcofBlit(srcConfig, srcIsLinear)) {
110 return false;
111 }
112
113 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
114 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
115 return false;
116 }
117
118 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
119 // resolved msaa though.
120 if (dstSampleCnt > 1 || srcSampleCnt > 1) {
121 return false;
122 }
123
124 return true;
125}
126
127bool GrVkCaps::canCopyAsResolve(GrPixelConfig dstConfig, int dstSampleCnt,
128 GrSurfaceOrigin dstOrigin, GrPixelConfig srcConfig,
129 int srcSampleCnt, GrSurfaceOrigin srcOrigin) const {
130 // The src surface must be multisampled.
131 if (srcSampleCnt <= 1) {
132 return false;
133 }
134
135 // The dst must not be multisampled.
136 if (dstSampleCnt > 1) {
137 return false;
138 }
139
140 // Surfaces must have the same format.
141 if (dstConfig != srcConfig) {
142 return false;
143 }
144
145 // Surfaces must have the same origin.
146 if (srcOrigin != dstOrigin) {
147 return false;
148 }
149
150 return true;
151}
152
153bool GrVkCaps::canCopyAsDraw(GrPixelConfig dstConfig, bool dstIsRenderable,
154 GrPixelConfig srcConfig, bool srcIsTextureable) const {
155 // TODO: Make copySurfaceAsDraw handle the swizzle
156 if (this->shaderCaps()->configOutputSwizzle(srcConfig) !=
157 this->shaderCaps()->configOutputSwizzle(dstConfig)) {
158 return false;
159 }
160
161 // Make sure the dst is a render target and the src is a texture.
162 if (!dstIsRenderable || !srcIsTextureable) {
163 return false;
164 }
165
166 return true;
167}
168
169bool GrVkCaps::canCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
170 const SkIRect& srcRect, const SkIPoint& dstPoint) const {
171 GrSurfaceOrigin dstOrigin = dst->origin();
172 GrSurfaceOrigin srcOrigin = src->origin();
173
174 GrPixelConfig dstConfig = dst->config();
175 GrPixelConfig srcConfig = src->config();
176
177 // TODO: Figure out a way to track if we've wrapped a linear texture in a proxy (e.g.
178 // PromiseImage which won't get instantiated right away. Does this need a similar thing like the
179 // tracking of external or rectangle textures in GL? For now we don't create linear textures
180 // internally, and I don't believe anyone is wrapping them.
181 bool srcIsLinear = false;
182 bool dstIsLinear = false;
183
184 int dstSampleCnt = 0;
185 int srcSampleCnt = 0;
186 if (const GrRenderTargetProxy* rtProxy = dst->asRenderTargetProxy()) {
187 dstSampleCnt = rtProxy->numColorSamples();
188 }
189 if (const GrRenderTargetProxy* rtProxy = src->asRenderTargetProxy()) {
190 srcSampleCnt = rtProxy->numColorSamples();
191 }
192 SkASSERT((dstSampleCnt > 0) == SkToBool(dst->asRenderTargetProxy()));
193 SkASSERT((srcSampleCnt > 0) == SkToBool(src->asRenderTargetProxy()));
194
195 return this->canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
196 srcConfig, srcSampleCnt, srcOrigin) ||
197 this->canCopyAsBlit(dstConfig, dstSampleCnt, dstIsLinear,
198 srcConfig, srcSampleCnt, srcIsLinear) ||
199 this->canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
200 srcConfig, srcSampleCnt, srcOrigin) ||
201 this->canCopyAsDraw(dstConfig, dstSampleCnt > 0,
202 srcConfig, SkToBool(src->asTextureProxy()));
203}
204
Greg Daniel164a9f02016-02-22 09:56:40 -0500205void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
Greg Daniela0651ac2018-08-08 09:23:18 -0400206 VkPhysicalDevice physDev, const VkPhysicalDeviceFeatures2& features,
Greg Danielc0b03d82018-08-03 14:41:15 -0400207 const GrVkExtensions& extensions) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000209 VkPhysicalDeviceProperties properties;
210 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -0800211
egdanield5e3b9e2016-03-08 12:19:54 -0800212 VkPhysicalDeviceMemoryProperties memoryProperties;
213 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
214
Greg Danielc0b03d82018-08-03 14:41:15 -0400215 uint32_t physicalDeviceVersion = properties.apiVersion;
216
217 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
218 extensions.hasExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 1)) {
219 fSupportsPhysicalDeviceProperties2 = true;
220 }
221
222 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
223 extensions.hasExtension(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, 1)) {
224 fSupportsMemoryRequirements2 = true;
225 }
226
227 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
228 extensions.hasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME, 1)) {
229 fSupportsMaintenance1 = true;
230 }
231
232 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
233 extensions.hasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME, 1)) {
234 fSupportsMaintenance2 = true;
235 }
236
237 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
238 extensions.hasExtension(VK_KHR_MAINTENANCE3_EXTENSION_NAME, 1)) {
239 fSupportsMaintenance3 = true;
240 }
241
Greg Daniel313c6952018-08-08 09:24:08 -0400242 this->initGrCaps(vkInterface, physDev, properties, memoryProperties, features, extensions);
Greg Daniel36443602018-08-02 12:51:52 -0400243 this->initShaderCaps(properties, features);
Greg Danielf3b11622018-03-01 15:01:27 -0500244
245 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
246#if defined(SK_CPU_X86)
247 // We need to do this before initing the config table since it uses fSRGBSupport
248 if (kImagination_VkVendor == properties.vendorID) {
249 fSRGBSupport = false;
250 }
251#endif
252 }
253
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000254 this->initConfigTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700255 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500256
Greg Daniel691f5e72018-02-28 14:21:34 -0500257 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
258 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700259 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500260
Greg Danielddc0c602018-06-18 11:26:30 -0400261 // On nexus player we disable suballocating VkImage memory since we've seen large slow downs on
262 // bot run times.
263 if (kImagination_VkVendor == properties.vendorID) {
264 fShouldAlwaysUseDedicatedImageMemory = true;
265 }
266
Greg Daniel691f5e72018-02-28 14:21:34 -0500267 this->applyOptionsOverrides(contextOptions);
268 fShaderCaps->applyOptionsOverrides(contextOptions);
269}
270
271void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700272 if (kQualcomm_VkVendor == properties.vendorID) {
273 fMustDoCopiesFromOrigin = true;
274 }
275
egdanielfd016d72016-09-27 12:13:05 -0700276 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700277 fMustSubmitCommandsBeforeCopyOp = true;
278 }
279
Greg Daniel80a08dd2017-01-20 10:45:49 -0500280#if defined(SK_BUILD_FOR_WIN)
281 if (kNvidia_VkVendor == properties.vendorID) {
282 fMustSleepOnTearDown = true;
283 }
284#elif defined(SK_BUILD_FOR_ANDROID)
285 if (kImagination_VkVendor == properties.vendorID) {
286 fMustSleepOnTearDown = true;
287 }
288#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500289
290 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
291 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
292 if (kAMD_VkVendor == properties.vendorID) {
293 fNewCBOnPipelineChange = true;
294 }
295
Greg Danielddc0c602018-06-18 11:26:30 -0400296 // On Mali galaxy s7 we see lots of rendering issues when we suballocate VkImages.
297 if (kARM_VkVendor == properties.vendorID) {
298 fShouldAlwaysUseDedicatedImageMemory = true;
299 }
300
Greg Danielbce5eb92018-03-01 13:13:44 -0500301 ////////////////////////////////////////////////////////////////////////////
302 // GrCaps workarounds
303 ////////////////////////////////////////////////////////////////////////////
304
305 if (kARM_VkVendor == properties.vendorID) {
306 fInstanceAttribSupport = false;
307 }
308
309 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
310 if (kAMD_VkVendor == properties.vendorID) {
311 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
312 }
313
Greg Danielbce5eb92018-03-01 13:13:44 -0500314 ////////////////////////////////////////////////////////////////////////////
315 // GrShaderCaps workarounds
316 ////////////////////////////////////////////////////////////////////////////
317
Greg Danielbce5eb92018-03-01 13:13:44 -0500318 if (kImagination_VkVendor == properties.vendorID) {
319 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
320 }
321
Greg Daniel164a9f02016-02-22 09:56:40 -0500322}
323
324int get_max_sample_count(VkSampleCountFlags flags) {
325 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
326 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
327 return 0;
328 }
329 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
330 return 2;
331 }
332 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
333 return 4;
334 }
335 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
336 return 8;
337 }
338 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
339 return 16;
340 }
341 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
342 return 32;
343 }
344 return 64;
345}
346
Greg Daniel313c6952018-08-08 09:24:08 -0400347template<typename T> T* get_extension_feature_struct(const VkPhysicalDeviceFeatures2& features,
348 VkStructureType type) {
349 // All Vulkan structs that could be part of the features chain will start with the
350 // structure type followed by the pNext pointer. We cast to the CommonVulkanHeader
351 // so we can get access to the pNext for the next struct.
352 struct CommonVulkanHeader {
353 VkStructureType sType;
354 void* pNext;
355 };
356
357 void* pNext = features.pNext;
358 while (pNext) {
359 CommonVulkanHeader* header = static_cast<CommonVulkanHeader*>(pNext);
360 if (header->sType == type) {
361 return static_cast<T*>(pNext);
362 }
363 pNext = header->pNext;
364 }
365 return nullptr;
366}
367
368void GrVkCaps::initGrCaps(const GrVkInterface* vkInterface,
369 VkPhysicalDevice physDev,
370 const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700371 const VkPhysicalDeviceMemoryProperties& memoryProperties,
Greg Daniel313c6952018-08-08 09:24:08 -0400372 const VkPhysicalDeviceFeatures2& features,
373 const GrVkExtensions& extensions) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400374 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
375 // need for us ever to support that amount, and it makes tests which tests all the vertex
376 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
377 // we ever find that need.
378 static const uint32_t kMaxVertexAttributes = 64;
379 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400380
egdanield5e3b9e2016-03-08 12:19:54 -0800381 // We could actually query and get a max size for each config, however maxImageDimension2D will
382 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700383 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
384 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
Adrienne Walker724afe82018-05-15 11:36:26 -0700385 if (fDriverBugWorkarounds.max_texture_size_limit_4096) {
386 fMaxTextureSize = SkTMin(fMaxTextureSize, 4096);
387 }
388 // Our render targets are always created with textures as the color
389 // attachment, hence this min:
390 fMaxRenderTargetSize = SkTMin(fMaxTextureSize, fMaxRenderTargetSize);
egdanield5e3b9e2016-03-08 12:19:54 -0800391
Chris Dalton2612bae2018-02-22 13:41:37 -0700392 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
393 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
394
egdanield5e3b9e2016-03-08 12:19:54 -0800395 // Assuming since we will always map in the end to upload the data we might as well just map
396 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700397 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800398
399 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
400
egdanield5e3b9e2016-03-08 12:19:54 -0800401 fOversizedStencilSupport = true;
Greg Daniela0651ac2018-08-08 09:23:18 -0400402 fSampleShadingSupport = features.features.sampleRateShading;
Greg Daniel313c6952018-08-08 09:24:08 -0400403
404 if (extensions.hasExtension(VK_EXT_BLEND_OPERATION_ADVANCED_EXTENSION_NAME, 2) &&
405 this->supportsPhysicalDeviceProperties2()) {
406
407 VkPhysicalDeviceBlendOperationAdvancedPropertiesEXT blendProps;
408 blendProps.sType =
409 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_PROPERTIES_EXT;
410 blendProps.pNext = nullptr;
411
412 VkPhysicalDeviceProperties2 props;
413 props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
414 props.pNext = &blendProps;
415
416 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties2(physDev, &props));
417
418 if (blendProps.advancedBlendAllOperations == VK_TRUE) {
419 fShaderCaps->fAdvBlendEqInteraction = GrShaderCaps::kAutomatic_AdvBlendEqInteraction;
420
421 auto blendFeatures =
422 get_extension_feature_struct<VkPhysicalDeviceBlendOperationAdvancedFeaturesEXT>(
423 features,
424 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BLEND_OPERATION_ADVANCED_FEATURES_EXT);
425 if (blendFeatures && blendFeatures->advancedBlendCoherentOperations == VK_TRUE) {
426 fBlendEquationSupport = kAdvancedCoherent_BlendEquationSupport;
427 } else {
428 // TODO: Currently non coherent blends are not supported in our vulkan backend. They
429 // require us to support self dependencies in our render passes.
430 // fBlendEquationSupport = kAdvanced_BlendEquationSupport;
431 }
432 }
433 }
egdanield5e3b9e2016-03-08 12:19:54 -0800434}
435
Greg Daniel36443602018-08-02 12:51:52 -0400436void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties,
Greg Daniela0651ac2018-08-08 09:23:18 -0400437 const VkPhysicalDeviceFeatures2& features) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500438 GrShaderCaps* shaderCaps = fShaderCaps.get();
439 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700440
Greg Daniel164a9f02016-02-22 09:56:40 -0500441
442 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
443 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
444 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500445 // Vulkan doesn't support a single channel format stored in alpha.
446 if (GrPixelConfigIsAlphaOnly(config) &&
447 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500448 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
449 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500450 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500451 if (kGray_8_GrPixelConfig == config ||
452 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500453 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
454 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700455 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
456 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
457 // or writing to outputs. Since we're not actually changing the data at all, the
458 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500459 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
460 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700461 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500462 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700463 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500464 }
465 }
egdaniel67acb832016-02-26 08:32:20 -0800466
egdanield5e3b9e2016-03-08 12:19:54 -0800467 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500468 shaderCaps->fUsesPrecisionModifiers = true;
469 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700470 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
471 // to be true with Vulkan as well.
472 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800473
474 // GrShaderCaps
475
Brian Salomon1edc5b92016-11-29 13:43:46 -0500476 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600477
Greg Daniela0651ac2018-08-08 09:23:18 -0400478 shaderCaps->fGeometryShaderSupport = features.features.geometryShader;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600479 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700480
Greg Daniela0651ac2018-08-08 09:23:18 -0400481 shaderCaps->fDualSourceBlendingSupport = features.features.dualSrcBlend;
egdanield632bb42016-03-30 12:06:48 -0700482
Brian Salomon1edc5b92016-11-29 13:43:46 -0500483 shaderCaps->fIntegerSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600484 shaderCaps->fVertexIDSupport = true;
Chris Dalton7c7ff032018-03-28 20:09:58 -0600485 shaderCaps->fFPManipulationSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800486
cdaltona6b92ad2016-04-11 12:03:08 -0700487 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700488 shaderCaps->fFloatIs32Bits = true;
489 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700490
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400491 // SPIR-V supports unsigned integers.
492 shaderCaps->fUnsignedSupport = true;
493
Brian Salomon1edc5b92016-11-29 13:43:46 -0500494 shaderCaps->fMaxVertexSamplers =
495 shaderCaps->fMaxGeometrySamplers =
496 shaderCaps->fMaxFragmentSamplers = SkTMin(
497 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
498 properties.limits.maxPerStageDescriptorSamplers),
499 (uint32_t)INT_MAX);
500 shaderCaps->fMaxCombinedSamplers = SkTMin(
501 SkTMin(properties.limits.maxDescriptorSetSampledImages,
502 properties.limits.maxDescriptorSetSamplers),
503 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500504}
505
egdaniel8f1dcaa2016-04-01 10:10:45 -0700506bool stencil_format_supported(const GrVkInterface* interface,
507 VkPhysicalDevice physDev,
508 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500509 VkFormatProperties props;
510 memset(&props, 0, sizeof(VkFormatProperties));
511 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700512 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500513}
514
egdaniel8f1dcaa2016-04-01 10:10:45 -0700515void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
516 // List of legal stencil formats (though perhaps not supported on
517 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700518 // 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 -0700519 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500520 static const StencilFormat
521 // internal Format stencil bits total bits packed?
522 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700523 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
524 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500525
egdaniel8f1dcaa2016-04-01 10:10:45 -0700526 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
527 fPreferedStencilFormat = gS8;
528 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
529 fPreferedStencilFormat = gD24S8;
530 } else {
531 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
532 fPreferedStencilFormat = gD32S8;
533 }
534}
535
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000536void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
537 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700538 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
539 VkFormat format;
540 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400541 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000542 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400543 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700544 }
545 }
546}
547
548void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
549 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
550 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
551 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700552
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400553 // Ganesh assumes that all renderable surfaces are also texturable
554 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
555 *flags = *flags | kRenderable_Flag;
556 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700557 }
558
559 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
560 *flags = *flags | kBlitSrc_Flag;
561 }
562
563 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
564 *flags = *flags | kBlitDst_Flag;
565 }
566}
567
Greg Daniel81e7bf82017-07-19 14:47:42 -0400568void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
569 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000570 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400571 VkFormat format) {
572 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
573 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
574 VK_IMAGE_USAGE_SAMPLED_BIT |
575 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
Greg Daniel81e7bf82017-07-19 14:47:42 -0400576 VkImageFormatProperties properties;
577 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
578 format,
579 VK_IMAGE_TYPE_2D,
580 VK_IMAGE_TILING_OPTIMAL,
581 usage,
Brian Osman2b23c4b2018-06-01 12:25:08 -0400582 0, // createFlags
Greg Daniel81e7bf82017-07-19 14:47:42 -0400583 &properties));
584 VkSampleCountFlags flags = properties.sampleCounts;
585 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500586 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400587 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000588 if (kImagination_VkVendor == physProps.vendorID) {
589 // MSAA does not work on imagination
590 return;
591 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400592 if (flags & VK_SAMPLE_COUNT_2_BIT) {
593 fColorSampleCounts.push(2);
594 }
595 if (flags & VK_SAMPLE_COUNT_4_BIT) {
596 fColorSampleCounts.push(4);
597 }
598 if (flags & VK_SAMPLE_COUNT_8_BIT) {
599 fColorSampleCounts.push(8);
600 }
601 if (flags & VK_SAMPLE_COUNT_16_BIT) {
602 fColorSampleCounts.push(16);
603 }
604 if (flags & VK_SAMPLE_COUNT_32_BIT) {
605 fColorSampleCounts.push(32);
606 }
607 if (flags & VK_SAMPLE_COUNT_64_BIT) {
608 fColorSampleCounts.push(64);
609 }
610}
611
egdaniel8f1dcaa2016-04-01 10:10:45 -0700612void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
613 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000614 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700615 VkFormat format) {
616 VkFormatProperties props;
617 memset(&props, 0, sizeof(VkFormatProperties));
618 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
619 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
620 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400621 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000622 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400623 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500624}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400625
Brian Salomonbdecacf2018-02-02 20:32:49 -0500626int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
627 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400628 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500629
630 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400631 return 0;
632 }
633
Brian Salomonbdecacf2018-02-02 20:32:49 -0500634 if (1 == requestedCount) {
635 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
636 fConfigTable[config].fColorSampleCounts[0] == 1);
637 return 1;
638 }
639
Greg Daniel81e7bf82017-07-19 14:47:42 -0400640 for (int i = 0; i < count; ++i) {
641 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
642 return fConfigTable[config].fColorSampleCounts[i];
643 }
644 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500645 return 0;
646}
647
648int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
649 const auto& table = fConfigTable[config].fColorSampleCounts;
650 if (!table.count()) {
651 return 0;
652 }
653 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500654}
655
Brian Salomon3d86a192018-02-27 16:46:11 -0500656bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
657 if (auto rt = surface->asRenderTarget()) {
658 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
659 }
660 return true;
661}
662
Robert Phillipsfc711a22018-02-13 17:03:00 -0500663bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500664 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500665
666 switch (ct) {
667 case kUnknown_SkColorType:
668 return false;
669 case kAlpha_8_SkColorType:
670 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500671 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500672 }
673 break;
674 case kRGB_565_SkColorType:
675 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500676 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500677 }
678 break;
679 case kARGB_4444_SkColorType:
680 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500681 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500682 }
683 break;
684 case kRGBA_8888_SkColorType:
685 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500686 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500687 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500688 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500689 }
690 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500691 case kRGB_888x_SkColorType:
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400692 // TODO: VK_FORMAT_R8G8B8_UNORM
Brian Salomone41e1762018-01-25 14:07:47 -0500693 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500694 case kBGRA_8888_SkColorType:
695 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500696 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500697 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500698 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500699 }
700 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500701 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500702 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
703 *config = kRGBA_1010102_GrPixelConfig;
704 }
705 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500706 case kRGB_101010x_SkColorType:
707 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500708 case kGray_8_SkColorType:
709 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500710 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500711 }
712 break;
713 case kRGBA_F16_SkColorType:
714 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500715 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500716 }
717 break;
Mike Klein37854712018-06-26 11:43:06 -0400718 case kRGBA_F32_SkColorType:
719 if (VK_FORMAT_R32G32B32A32_SFLOAT == format) {
720 *config = kRGBA_float_GrPixelConfig;
721 }
722 break;
Greg Danielf5d87582017-12-18 14:48:15 -0500723 }
724
Greg Danielfaa095e2017-12-19 13:15:02 -0500725 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500726}
727
Greg Danielfaa095e2017-12-19 13:15:02 -0500728bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
729 GrPixelConfig* config) const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400730 GrVkImageInfo imageInfo;
731 if (!tex.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500732 return false;
733 }
734
Greg Daniel52e16d92018-04-10 09:34:07 -0400735 return validate_image_info(imageInfo.fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500736}
737
738bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
739 GrPixelConfig* config) const {
Greg Daniel323fbcf2018-04-10 13:46:30 -0400740 GrVkImageInfo imageInfo;
741 if (!rt.getVkImageInfo(&imageInfo)) {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500742 return false;
743 }
744
Greg Daniel323fbcf2018-04-10 13:46:30 -0400745 return validate_image_info(imageInfo.fFormat, ct, config);
Robert Phillipsfc711a22018-02-13 17:03:00 -0500746}
747
748bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
749 GrPixelConfig* config) const {
750 const VkFormat* vkFormat = format.getVkFormat();
751 if (!vkFormat) {
752 return false;
753 }
754 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500755}
Greg Danielf5d87582017-12-18 14:48:15 -0500756
Timothy Liang036fdfe2018-06-28 15:50:36 -0400757#ifdef GR_TEST_UTILS
758GrBackendFormat GrVkCaps::onCreateFormatFromBackendTexture(
759 const GrBackendTexture& backendTex) const {
760 GrVkImageInfo vkInfo;
761 SkAssertResult(backendTex.getVkImageInfo(&vkInfo));
762 return GrBackendFormat::MakeVk(vkInfo.fFormat);
763}
764#endif
765