blob: e5037dfb3e4e8d50d02a62315c63c92aabe8a497 [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 Daniel164a9f02016-02-22 09:56:40 -050013#include "GrVkUtil.h"
jvanverthfd7bd452016-03-25 06:29:52 -070014#include "vk/GrVkBackendContext.h"
Brian Salomon467921e2017-03-06 16:17:12 -050015#include "vk/GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016
17GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070018 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags)
19 : INHERITED(contextOptions) {
20 fCanUseGLSLForShaderModule = false;
egdaniel6fa0a912016-09-12 11:51:29 -070021 fMustDoCopiesFromOrigin = false;
Greg Daniel691f5e72018-02-28 14:21:34 -050022 fSupportsCopiesAsDraws = true;
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 Daniel8385a8a2018-02-26 13:29:37 -050026 fCanUseWholeSizeOnFlushMappedMemory = true;
egdanielc5ec1402016-03-28 12:14:42 -070027
Greg Daniel164a9f02016-02-22 09:56:40 -050028 /**************************************************************************
29 * GrDrawTargetCaps fields
30 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070031 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070032 fSRGBSupport = true; // always available in Vulkan
Brian Osman57bc3ea2017-07-27 09:58:11 -040033 fSRGBDecodeDisableSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070034 fNPOTTextureTileSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070035 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050036 fReuseScratchTextures = true; //TODO: figure this out
37 fGpuTracingSupport = false; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050038 fOversizedStencilSupport = false; //TODO: figure this out
Chris Dalton1d616352017-05-31 12:51:23 -060039 fInstanceAttribSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050040
Chris Daltone4679fa2017-09-29 13:58:26 -060041 fBlacklistCoverageCounting = true; // blacklisting ccpr until we work through a few issues.
jvanverth84741b32016-09-30 08:39:02 -070042 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel691f5e72018-02-28 14:21:34 -050043 fCrossContextTextureSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050044
45 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070046 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050047
48 fMaxRenderTargetSize = 4096; // minimum required by spec
49 fMaxTextureSize = 4096; // minimum required by spec
Greg Daniel164a9f02016-02-22 09:56:40 -050050
Brian Salomon94efbf52016-11-29 13:43:05 -050051 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050052
egdanielc5ec1402016-03-28 12:14:42 -070053 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050054}
55
Robert Phillipsbf25d432017-04-07 10:08:53 -040056bool GrVkCaps::initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
Eric Karl74480882017-04-03 14:49:05 -070057 bool* rectsMustMatch, bool* disallowSubrect) const {
58 // 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.
65 desc->fOrigin = src->origin();
66 desc->fConfig = src->config();
Robert Phillipsbf25d432017-04-07 10:08:53 -040067 if (src->numColorSamples() > 1 || (src->asTextureProxy() && this->supportsCopiesAsDraws())) {
Brian Salomon467921e2017-03-06 16:17:12 -050068 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
Greg Daniel164a9f02016-02-22 09:56:40 -050077void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070078 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050079
Jim Van Verth6a40abc2017-11-02 16:56:09 +000080 VkPhysicalDeviceProperties properties;
81 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -080082
egdanield5e3b9e2016-03-08 12:19:54 -080083 VkPhysicalDeviceMemoryProperties memoryProperties;
84 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
85
jvanverthfd7bd452016-03-25 06:29:52 -070086 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -050087 this->initShaderCaps(properties, featureFlags);
Greg Daniel2bb6ecc2017-07-20 13:11:14 +000088 this->initConfigTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -070089 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050090
Greg Daniel691f5e72018-02-28 14:21:34 -050091 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
92 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -070093 }
Greg Daniel164a9f02016-02-22 09:56:40 -050094
Greg Daniel691f5e72018-02-28 14:21:34 -050095 this->applyOptionsOverrides(contextOptions);
96 fShaderCaps->applyOptionsOverrides(contextOptions);
97}
98
99void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700100 if (kQualcomm_VkVendor == properties.vendorID) {
101 fMustDoCopiesFromOrigin = true;
102 }
103
egdanielfd016d72016-09-27 12:13:05 -0700104 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700105 fMustSubmitCommandsBeforeCopyOp = true;
106 }
107
Greg Daniel691f5e72018-02-28 14:21:34 -0500108 if (kQualcomm_VkVendor == properties.vendorID ||
109 kARM_VkVendor == properties.vendorID) {
110 fSupportsCopiesAsDraws = false;
111 // We require copies as draws to support cross context textures.
112 fCrossContextTextureSupport = false;
Greg Daniela63e7ab2017-05-16 14:38:54 -0400113 }
114
Greg Daniel5ba448c2018-02-26 13:30:47 -0500115 if (kARM_VkVendor == properties.vendorID) {
116 fInstanceAttribSupport = false;
117 }
118
Greg Daniel80a08dd2017-01-20 10:45:49 -0500119#if defined(SK_BUILD_FOR_WIN)
120 if (kNvidia_VkVendor == properties.vendorID) {
121 fMustSleepOnTearDown = true;
122 }
123#elif defined(SK_BUILD_FOR_ANDROID)
124 if (kImagination_VkVendor == properties.vendorID) {
125 fMustSleepOnTearDown = true;
126 }
127#endif
Greg Daniel164a9f02016-02-22 09:56:40 -0500128}
129
130int get_max_sample_count(VkSampleCountFlags flags) {
131 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
132 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
133 return 0;
134 }
135 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
136 return 2;
137 }
138 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
139 return 4;
140 }
141 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
142 return 8;
143 }
144 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
145 return 16;
146 }
147 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
148 return 32;
149 }
150 return 64;
151}
152
egdanield5e3b9e2016-03-08 12:19:54 -0800153void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700154 const VkPhysicalDeviceMemoryProperties& memoryProperties,
155 uint32_t featureFlags) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400156 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
157 // need for us ever to support that amount, and it makes tests which tests all the vertex
158 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
159 // we ever find that need.
160 static const uint32_t kMaxVertexAttributes = 64;
161 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
162 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
163 if (kAMD_VkVendor == properties.vendorID) {
164 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
165 }
166
egdanield5e3b9e2016-03-08 12:19:54 -0800167 // We could actually query and get a max size for each config, however maxImageDimension2D will
168 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700169 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
170 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800171
Chris Dalton2612bae2018-02-22 13:41:37 -0700172 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
173 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
174
egdanield5e3b9e2016-03-08 12:19:54 -0800175 // Assuming since we will always map in the end to upload the data we might as well just map
176 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700177 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800178
179 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
180
egdanield5e3b9e2016-03-08 12:19:54 -0800181 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700182 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
Greg Daniel22bc8652017-03-22 15:45:43 -0400183
184 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
185 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
186 if (kAMD_VkVendor == properties.vendorID) {
Greg Daniele3cd6912017-05-17 11:15:55 -0400187 fNewCBOnPipelineChange = true;
Greg Daniel22bc8652017-03-22 15:45:43 -0400188 }
Greg Daniel01907872017-05-23 15:21:02 -0400189
Greg Daniel8385a8a2018-02-26 13:29:37 -0500190 if (kIntel_VkVendor == properties.vendorID) {
191 fCanUseWholeSizeOnFlushMappedMemory = false;
192 }
193
Greg Daniel01907872017-05-23 15:21:02 -0400194#if defined(SK_CPU_X86)
195 if (kImagination_VkVendor == properties.vendorID) {
196 fSRGBSupport = false;
197 }
198#endif
egdanield5e3b9e2016-03-08 12:19:54 -0800199}
200
Brian Salomon1edc5b92016-11-29 13:43:46 -0500201void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
202 GrShaderCaps* shaderCaps = fShaderCaps.get();
203 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700204
Greg Daniel164a9f02016-02-22 09:56:40 -0500205
206 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
207 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
208 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500209 // Vulkan doesn't support a single channel format stored in alpha.
210 if (GrPixelConfigIsAlphaOnly(config) &&
211 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500212 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
213 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500214 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500215 if (kGray_8_GrPixelConfig == config ||
216 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500217 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
218 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700219 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
220 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
221 // or writing to outputs. Since we're not actually changing the data at all, the
222 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500223 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
224 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700225 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500226 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700227 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500228 }
229 }
egdaniel67acb832016-02-26 08:32:20 -0800230
Greg Daniel80a08dd2017-01-20 10:45:49 -0500231 if (kImagination_VkVendor == properties.vendorID) {
232 shaderCaps->fAtan2ImplementedAsAtanYOverX = true;
233 }
234
egdanield5e3b9e2016-03-08 12:19:54 -0800235 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500236 shaderCaps->fUsesPrecisionModifiers = true;
237 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700238 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
239 // to be true with Vulkan as well.
240 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800241
242 // GrShaderCaps
243
Brian Salomon1edc5b92016-11-29 13:43:46 -0500244 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600245
Brian Salomon1edc5b92016-11-29 13:43:46 -0500246 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600247 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700248
Brian Salomon1edc5b92016-11-29 13:43:46 -0500249 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
Greg Daniel84cff132017-03-22 16:25:53 -0400250 if (kAMD_VkVendor == properties.vendorID) {
251 // Currently DualSourceBlending is not working on AMD. vkCreateGraphicsPipeline fails when
252 // using a draw with dual source. Looking into whether it is driver bug or issue with our
253 // SPIR-V. Bug skia:6405
254 shaderCaps->fDualSourceBlendingSupport = false;
255 }
egdanield632bb42016-03-30 12:06:48 -0700256
Brian Salomon1edc5b92016-11-29 13:43:46 -0500257 shaderCaps->fIntegerSupport = true;
Chris Dalton6b65b982017-07-06 11:04:00 -0600258 shaderCaps->fTexelBufferSupport = true;
259 shaderCaps->fTexelFetchSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600260 shaderCaps->fVertexIDSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800261
cdaltona6b92ad2016-04-11 12:03:08 -0700262 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700263 shaderCaps->fFloatIs32Bits = true;
264 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700265
Brian Salomon1edc5b92016-11-29 13:43:46 -0500266 shaderCaps->fMaxVertexSamplers =
267 shaderCaps->fMaxGeometrySamplers =
268 shaderCaps->fMaxFragmentSamplers = SkTMin(
269 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
270 properties.limits.maxPerStageDescriptorSamplers),
271 (uint32_t)INT_MAX);
272 shaderCaps->fMaxCombinedSamplers = SkTMin(
273 SkTMin(properties.limits.maxDescriptorSetSampledImages,
274 properties.limits.maxDescriptorSetSamplers),
275 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500276}
277
egdaniel8f1dcaa2016-04-01 10:10:45 -0700278bool stencil_format_supported(const GrVkInterface* interface,
279 VkPhysicalDevice physDev,
280 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500281 VkFormatProperties props;
282 memset(&props, 0, sizeof(VkFormatProperties));
283 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700284 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500285}
286
egdaniel8f1dcaa2016-04-01 10:10:45 -0700287void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
288 // List of legal stencil formats (though perhaps not supported on
289 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700290 // 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 -0700291 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500292 static const StencilFormat
293 // internal Format stencil bits total bits packed?
294 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700295 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
296 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500297
egdaniel8f1dcaa2016-04-01 10:10:45 -0700298 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
299 fPreferedStencilFormat = gS8;
300 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
301 fPreferedStencilFormat = gD24S8;
302 } else {
303 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
304 fPreferedStencilFormat = gD32S8;
305 }
306}
307
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000308void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
309 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700310 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
311 VkFormat format;
312 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400313 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000314 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400315 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700316 }
317 }
318}
319
320void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
321 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
322 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
323 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700324
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400325 // Ganesh assumes that all renderable surfaces are also texturable
326 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
327 *flags = *flags | kRenderable_Flag;
328 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700329 }
330
331 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
332 *flags = *flags | kBlitSrc_Flag;
333 }
334
335 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
336 *flags = *flags | kBlitDst_Flag;
337 }
338}
339
Greg Daniel81e7bf82017-07-19 14:47:42 -0400340void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
341 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000342 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400343 VkFormat format) {
344 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
345 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
346 VK_IMAGE_USAGE_SAMPLED_BIT |
347 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
348 VkImageCreateFlags createFlags = GrVkFormatIsSRGB(format, nullptr)
349 ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
350 VkImageFormatProperties properties;
351 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
352 format,
353 VK_IMAGE_TYPE_2D,
354 VK_IMAGE_TILING_OPTIMAL,
355 usage,
356 createFlags,
357 &properties));
358 VkSampleCountFlags flags = properties.sampleCounts;
359 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500360 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400361 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000362 if (kImagination_VkVendor == physProps.vendorID) {
363 // MSAA does not work on imagination
364 return;
365 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400366 if (flags & VK_SAMPLE_COUNT_2_BIT) {
367 fColorSampleCounts.push(2);
368 }
369 if (flags & VK_SAMPLE_COUNT_4_BIT) {
370 fColorSampleCounts.push(4);
371 }
372 if (flags & VK_SAMPLE_COUNT_8_BIT) {
373 fColorSampleCounts.push(8);
374 }
375 if (flags & VK_SAMPLE_COUNT_16_BIT) {
376 fColorSampleCounts.push(16);
377 }
378 if (flags & VK_SAMPLE_COUNT_32_BIT) {
379 fColorSampleCounts.push(32);
380 }
381 if (flags & VK_SAMPLE_COUNT_64_BIT) {
382 fColorSampleCounts.push(64);
383 }
384}
385
egdaniel8f1dcaa2016-04-01 10:10:45 -0700386void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
387 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000388 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700389 VkFormat format) {
390 VkFormatProperties props;
391 memset(&props, 0, sizeof(VkFormatProperties));
392 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
393 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
394 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400395 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000396 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400397 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500398}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400399
Brian Salomonbdecacf2018-02-02 20:32:49 -0500400int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
401 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400402 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500403
404 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400405 return 0;
406 }
407
Brian Salomonbdecacf2018-02-02 20:32:49 -0500408 if (1 == requestedCount) {
409 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
410 fConfigTable[config].fColorSampleCounts[0] == 1);
411 return 1;
412 }
413
Greg Daniel81e7bf82017-07-19 14:47:42 -0400414 for (int i = 0; i < count; ++i) {
415 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
416 return fConfigTable[config].fColorSampleCounts[i];
417 }
418 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500419 return 0;
420}
421
422int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
423 const auto& table = fConfigTable[config].fColorSampleCounts;
424 if (!table.count()) {
425 return 0;
426 }
427 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500428}
429
Brian Salomon3d86a192018-02-27 16:46:11 -0500430bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
431 if (auto rt = surface->asRenderTarget()) {
432 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
433 }
434 return true;
435}
436
Robert Phillipsfc711a22018-02-13 17:03:00 -0500437bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500438 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500439
440 switch (ct) {
441 case kUnknown_SkColorType:
442 return false;
443 case kAlpha_8_SkColorType:
444 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500445 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500446 }
447 break;
448 case kRGB_565_SkColorType:
449 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500450 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500451 }
452 break;
453 case kARGB_4444_SkColorType:
454 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500455 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500456 }
457 break;
458 case kRGBA_8888_SkColorType:
459 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500460 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500461 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500462 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500463 }
464 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500465 case kRGB_888x_SkColorType:
466 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500467 case kBGRA_8888_SkColorType:
468 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500469 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500470 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500471 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500472 }
473 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500474 case kRGBA_1010102_SkColorType:
475 return false;
476 case kRGB_101010x_SkColorType:
477 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500478 case kGray_8_SkColorType:
479 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500480 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500481 }
482 break;
483 case kRGBA_F16_SkColorType:
484 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500485 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500486 }
487 break;
488 }
489
Greg Danielfaa095e2017-12-19 13:15:02 -0500490 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500491}
492
Greg Danielfaa095e2017-12-19 13:15:02 -0500493bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
494 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500495 const GrVkImageInfo* imageInfo = tex.getVkImageInfo();
496 if (!imageInfo) {
497 return false;
498 }
499
500 return validate_image_info(imageInfo->fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500501}
502
503bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
504 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500505 const GrVkImageInfo* imageInfo = rt.getVkImageInfo();
506 if (!imageInfo) {
507 return false;
508 }
509
510 return validate_image_info(imageInfo->fFormat, ct, config);
511}
512
513bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
514 GrPixelConfig* config) const {
515 const VkFormat* vkFormat = format.getVkFormat();
516 if (!vkFormat) {
517 return false;
518 }
519 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500520}
Greg Danielf5d87582017-12-18 14:48:15 -0500521