blob: 7e2213509742e16a301196db0939c31891cbad0e [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
Greg Daniel0fe4ed82018-03-15 12:27:29 +000041 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,
Brian Salomon2a4f9832018-03-03 22:43:43 -050057 GrSurfaceOrigin* origin, bool* rectsMustMatch,
58 bool* disallowSubrect) const {
Eric Karl74480882017-04-03 14:49:05 -070059 // Vk doesn't use rectsMustMatch or disallowSubrect. Always return false.
60 *rectsMustMatch = false;
61 *disallowSubrect = false;
62
Brian Salomon467921e2017-03-06 16:17:12 -050063 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
64 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
65 // render target as well.
Brian Salomon2a4f9832018-03-03 22:43:43 -050066 *origin = src->origin();
Brian Salomon467921e2017-03-06 16:17:12 -050067 desc->fConfig = src->config();
Robert Phillipsbf25d432017-04-07 10:08:53 -040068 if (src->numColorSamples() > 1 || (src->asTextureProxy() && this->supportsCopiesAsDraws())) {
Brian Salomon467921e2017-03-06 16:17:12 -050069 desc->fFlags = kRenderTarget_GrSurfaceFlag;
70 } else {
71 // Just going to use CopyImage here
72 desc->fFlags = kNone_GrSurfaceFlags;
73 }
74
75 return true;
76}
77
Greg Daniel164a9f02016-02-22 09:56:40 -050078void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070079 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050080
Jim Van Verth6a40abc2017-11-02 16:56:09 +000081 VkPhysicalDeviceProperties properties;
82 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
egdanield5e3b9e2016-03-08 12:19:54 -080083
egdanield5e3b9e2016-03-08 12:19:54 -080084 VkPhysicalDeviceMemoryProperties memoryProperties;
85 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
86
jvanverthfd7bd452016-03-25 06:29:52 -070087 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -050088 this->initShaderCaps(properties, featureFlags);
Greg Danielf3b11622018-03-01 15:01:27 -050089
90 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
91#if defined(SK_CPU_X86)
92 // We need to do this before initing the config table since it uses fSRGBSupport
93 if (kImagination_VkVendor == properties.vendorID) {
94 fSRGBSupport = false;
95 }
96#endif
97 }
98
Greg Daniel2bb6ecc2017-07-20 13:11:14 +000099 this->initConfigTable(vkInterface, physDev, properties);
egdaniel8f1dcaa2016-04-01 10:10:45 -0700100 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -0500101
Greg Daniel691f5e72018-02-28 14:21:34 -0500102 if (!contextOptions.fDisableDriverCorrectnessWorkarounds) {
103 this->applyDriverCorrectnessWorkarounds(properties);
egdanielc5ec1402016-03-28 12:14:42 -0700104 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500105
Greg Daniel691f5e72018-02-28 14:21:34 -0500106 this->applyOptionsOverrides(contextOptions);
107 fShaderCaps->applyOptionsOverrides(contextOptions);
108}
109
110void GrVkCaps::applyDriverCorrectnessWorkarounds(const VkPhysicalDeviceProperties& properties) {
egdaniel6fa0a912016-09-12 11:51:29 -0700111 if (kQualcomm_VkVendor == properties.vendorID) {
112 fMustDoCopiesFromOrigin = true;
113 }
114
egdanielfd016d72016-09-27 12:13:05 -0700115 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700116 fMustSubmitCommandsBeforeCopyOp = true;
117 }
118
Greg Daniel691f5e72018-02-28 14:21:34 -0500119 if (kQualcomm_VkVendor == properties.vendorID ||
120 kARM_VkVendor == properties.vendorID) {
121 fSupportsCopiesAsDraws = false;
122 // We require copies as draws to support cross context textures.
123 fCrossContextTextureSupport = false;
Greg Daniela63e7ab2017-05-16 14:38:54 -0400124 }
125
Greg Daniel80a08dd2017-01-20 10:45:49 -0500126#if defined(SK_BUILD_FOR_WIN)
127 if (kNvidia_VkVendor == properties.vendorID) {
128 fMustSleepOnTearDown = true;
129 }
130#elif defined(SK_BUILD_FOR_ANDROID)
131 if (kImagination_VkVendor == properties.vendorID) {
132 fMustSleepOnTearDown = true;
133 }
134#endif
Greg Danielbce5eb92018-03-01 13:13:44 -0500135
136 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
137 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
138 if (kAMD_VkVendor == properties.vendorID) {
139 fNewCBOnPipelineChange = true;
140 }
141
142 ////////////////////////////////////////////////////////////////////////////
143 // GrCaps workarounds
144 ////////////////////////////////////////////////////////////////////////////
145
146 if (kARM_VkVendor == properties.vendorID) {
147 fInstanceAttribSupport = false;
148 }
149
150 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
151 if (kAMD_VkVendor == properties.vendorID) {
152 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
153 }
154
155 if (kIntel_VkVendor == properties.vendorID) {
156 fCanUseWholeSizeOnFlushMappedMemory = false;
157 }
158
Greg Danielbce5eb92018-03-01 13:13:44 -0500159 ////////////////////////////////////////////////////////////////////////////
160 // GrShaderCaps workarounds
161 ////////////////////////////////////////////////////////////////////////////
162
Greg Danielbce5eb92018-03-01 13:13:44 -0500163 if (kImagination_VkVendor == properties.vendorID) {
164 fShaderCaps->fAtan2ImplementedAsAtanYOverX = true;
165 }
166
Greg Daniel164a9f02016-02-22 09:56:40 -0500167}
168
169int get_max_sample_count(VkSampleCountFlags flags) {
170 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
171 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
172 return 0;
173 }
174 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
175 return 2;
176 }
177 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
178 return 4;
179 }
180 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
181 return 8;
182 }
183 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
184 return 16;
185 }
186 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
187 return 32;
188 }
189 return 64;
190}
191
egdanield5e3b9e2016-03-08 12:19:54 -0800192void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700193 const VkPhysicalDeviceMemoryProperties& memoryProperties,
194 uint32_t featureFlags) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400195 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
196 // need for us ever to support that amount, and it makes tests which tests all the vertex
197 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
198 // we ever find that need.
199 static const uint32_t kMaxVertexAttributes = 64;
200 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
Greg Danielc5cc2de2017-03-20 11:40:58 -0400201
egdanield5e3b9e2016-03-08 12:19:54 -0800202 // We could actually query and get a max size for each config, however maxImageDimension2D will
203 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700204 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
205 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800206
Chris Dalton2612bae2018-02-22 13:41:37 -0700207 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
208 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
209
egdanield5e3b9e2016-03-08 12:19:54 -0800210 // Assuming since we will always map in the end to upload the data we might as well just map
211 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700212 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800213
214 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
215
egdanield5e3b9e2016-03-08 12:19:54 -0800216 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700217 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
Greg Daniel22bc8652017-03-22 15:45:43 -0400218
Greg Daniel01907872017-05-23 15:21:02 -0400219
egdanield5e3b9e2016-03-08 12:19:54 -0800220}
221
Brian Salomon1edc5b92016-11-29 13:43:46 -0500222void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
223 GrShaderCaps* shaderCaps = fShaderCaps.get();
224 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700225
Greg Daniel164a9f02016-02-22 09:56:40 -0500226
227 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
228 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
229 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500230 // Vulkan doesn't support a single channel format stored in alpha.
231 if (GrPixelConfigIsAlphaOnly(config) &&
232 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500233 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
234 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500235 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500236 if (kGray_8_GrPixelConfig == config ||
237 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500238 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
239 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700240 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
241 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
242 // or writing to outputs. Since we're not actually changing the data at all, the
243 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500244 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
245 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700246 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500247 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700248 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500249 }
250 }
egdaniel67acb832016-02-26 08:32:20 -0800251
egdanield5e3b9e2016-03-08 12:19:54 -0800252 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500253 shaderCaps->fUsesPrecisionModifiers = true;
254 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700255 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
256 // to be true with Vulkan as well.
257 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800258
259 // GrShaderCaps
260
Brian Salomon1edc5b92016-11-29 13:43:46 -0500261 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600262
Brian Salomon1edc5b92016-11-29 13:43:46 -0500263 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600264 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700265
Brian Salomon1edc5b92016-11-29 13:43:46 -0500266 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700267
Brian Salomon1edc5b92016-11-29 13:43:46 -0500268 shaderCaps->fIntegerSupport = true;
Chris Dalton6b65b982017-07-06 11:04:00 -0600269 shaderCaps->fTexelBufferSupport = true;
270 shaderCaps->fTexelFetchSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600271 shaderCaps->fVertexIDSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800272
cdaltona6b92ad2016-04-11 12:03:08 -0700273 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700274 shaderCaps->fFloatIs32Bits = true;
275 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700276
Brian Salomon1edc5b92016-11-29 13:43:46 -0500277 shaderCaps->fMaxVertexSamplers =
278 shaderCaps->fMaxGeometrySamplers =
279 shaderCaps->fMaxFragmentSamplers = SkTMin(
280 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
281 properties.limits.maxPerStageDescriptorSamplers),
282 (uint32_t)INT_MAX);
283 shaderCaps->fMaxCombinedSamplers = SkTMin(
284 SkTMin(properties.limits.maxDescriptorSetSampledImages,
285 properties.limits.maxDescriptorSetSamplers),
286 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500287}
288
egdaniel8f1dcaa2016-04-01 10:10:45 -0700289bool stencil_format_supported(const GrVkInterface* interface,
290 VkPhysicalDevice physDev,
291 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500292 VkFormatProperties props;
293 memset(&props, 0, sizeof(VkFormatProperties));
294 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700295 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500296}
297
egdaniel8f1dcaa2016-04-01 10:10:45 -0700298void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
299 // List of legal stencil formats (though perhaps not supported on
300 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700301 // 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 -0700302 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500303 static const StencilFormat
304 // internal Format stencil bits total bits packed?
305 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700306 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
307 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500308
egdaniel8f1dcaa2016-04-01 10:10:45 -0700309 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
310 fPreferedStencilFormat = gS8;
311 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
312 fPreferedStencilFormat = gD24S8;
313 } else {
314 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
315 fPreferedStencilFormat = gD32S8;
316 }
317}
318
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000319void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
320 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700321 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
322 VkFormat format;
323 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400324 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000325 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400326 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700327 }
328 }
329}
330
331void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
332 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
333 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
334 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700335
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400336 // Ganesh assumes that all renderable surfaces are also texturable
337 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
338 *flags = *flags | kRenderable_Flag;
339 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700340 }
341
342 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
343 *flags = *flags | kBlitSrc_Flag;
344 }
345
346 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
347 *flags = *flags | kBlitDst_Flag;
348 }
349}
350
Greg Daniel81e7bf82017-07-19 14:47:42 -0400351void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
352 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000353 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400354 VkFormat format) {
355 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
356 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
357 VK_IMAGE_USAGE_SAMPLED_BIT |
358 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
359 VkImageCreateFlags createFlags = GrVkFormatIsSRGB(format, nullptr)
360 ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
361 VkImageFormatProperties properties;
362 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
363 format,
364 VK_IMAGE_TYPE_2D,
365 VK_IMAGE_TILING_OPTIMAL,
366 usage,
367 createFlags,
368 &properties));
369 VkSampleCountFlags flags = properties.sampleCounts;
370 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500371 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400372 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000373 if (kImagination_VkVendor == physProps.vendorID) {
374 // MSAA does not work on imagination
375 return;
376 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400377 if (flags & VK_SAMPLE_COUNT_2_BIT) {
378 fColorSampleCounts.push(2);
379 }
380 if (flags & VK_SAMPLE_COUNT_4_BIT) {
381 fColorSampleCounts.push(4);
382 }
383 if (flags & VK_SAMPLE_COUNT_8_BIT) {
384 fColorSampleCounts.push(8);
385 }
386 if (flags & VK_SAMPLE_COUNT_16_BIT) {
387 fColorSampleCounts.push(16);
388 }
389 if (flags & VK_SAMPLE_COUNT_32_BIT) {
390 fColorSampleCounts.push(32);
391 }
392 if (flags & VK_SAMPLE_COUNT_64_BIT) {
393 fColorSampleCounts.push(64);
394 }
395}
396
egdaniel8f1dcaa2016-04-01 10:10:45 -0700397void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
398 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000399 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700400 VkFormat format) {
401 VkFormatProperties props;
402 memset(&props, 0, sizeof(VkFormatProperties));
403 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
404 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
405 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400406 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000407 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400408 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500409}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400410
Brian Salomonbdecacf2018-02-02 20:32:49 -0500411int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
412 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400413 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500414
415 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400416 return 0;
417 }
418
Brian Salomonbdecacf2018-02-02 20:32:49 -0500419 if (1 == requestedCount) {
420 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
421 fConfigTable[config].fColorSampleCounts[0] == 1);
422 return 1;
423 }
424
Greg Daniel81e7bf82017-07-19 14:47:42 -0400425 for (int i = 0; i < count; ++i) {
426 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
427 return fConfigTable[config].fColorSampleCounts[i];
428 }
429 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500430 return 0;
431}
432
433int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
434 const auto& table = fConfigTable[config].fColorSampleCounts;
435 if (!table.count()) {
436 return 0;
437 }
438 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500439}
440
Brian Salomon3d86a192018-02-27 16:46:11 -0500441bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
442 if (auto rt = surface->asRenderTarget()) {
443 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
444 }
445 return true;
446}
447
Robert Phillipsfc711a22018-02-13 17:03:00 -0500448bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500449 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500450
451 switch (ct) {
452 case kUnknown_SkColorType:
453 return false;
454 case kAlpha_8_SkColorType:
455 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500456 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500457 }
458 break;
459 case kRGB_565_SkColorType:
460 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500461 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500462 }
463 break;
464 case kARGB_4444_SkColorType:
465 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500466 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500467 }
468 break;
469 case kRGBA_8888_SkColorType:
470 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500471 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500472 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500473 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500474 }
475 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500476 case kRGB_888x_SkColorType:
477 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500478 case kBGRA_8888_SkColorType:
479 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500480 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500481 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500482 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500483 }
484 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500485 case kRGBA_1010102_SkColorType:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500486 if (VK_FORMAT_A2B10G10R10_UNORM_PACK32 == format) {
487 *config = kRGBA_1010102_GrPixelConfig;
488 }
489 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500490 case kRGB_101010x_SkColorType:
491 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500492 case kGray_8_SkColorType:
493 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500494 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500495 }
496 break;
497 case kRGBA_F16_SkColorType:
498 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500499 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500500 }
501 break;
502 }
503
Greg Danielfaa095e2017-12-19 13:15:02 -0500504 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500505}
506
Greg Danielfaa095e2017-12-19 13:15:02 -0500507bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
508 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500509 const GrVkImageInfo* imageInfo = tex.getVkImageInfo();
510 if (!imageInfo) {
511 return false;
512 }
513
514 return validate_image_info(imageInfo->fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500515}
516
517bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
518 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500519 const GrVkImageInfo* imageInfo = rt.getVkImageInfo();
520 if (!imageInfo) {
521 return false;
522 }
523
524 return validate_image_info(imageInfo->fFormat, ct, config);
525}
526
527bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
528 GrPixelConfig* config) const {
529 const VkFormat* vkFormat = format.getVkFormat();
530 if (!vkFormat) {
531 return false;
532 }
533 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500534}
Greg Danielf5d87582017-12-18 14:48:15 -0500535