blob: 869d6e0e10ff553026918aae7705c2483871ee4a [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;
egdanielbc9b2962016-09-27 08:00:53 -070022 fSupportsCopiesAsDraws = 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 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 Daniela63e7ab2017-05-16 14:38:54 -040043 fCrossContextTextureSupport = false;
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
egdanielc5ec1402016-03-28 12:14:42 -070091 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070092 // Currently disabling this feature since it does not play well with validation layers which
93 // expect a SPIR-V shader
94 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070095 }
Greg Daniel164a9f02016-02-22 09:56:40 -050096
egdaniel6fa0a912016-09-12 11:51:29 -070097 if (kQualcomm_VkVendor == properties.vendorID) {
98 fMustDoCopiesFromOrigin = true;
99 }
100
egdanielfd016d72016-09-27 12:13:05 -0700101 if (kNvidia_VkVendor == properties.vendorID) {
egdanielfd016d72016-09-27 12:13:05 -0700102 fMustSubmitCommandsBeforeCopyOp = true;
103 }
104
Greg Daniel5ba448c2018-02-26 13:30:47 -0500105 if (kQualcomm_VkVendor != properties.vendorID &&
106 kARM_VkVendor != properties.vendorID) {
Greg Daniel1cdbdda2017-05-16 14:52:57 -0400107 fSupportsCopiesAsDraws = true;
108 }
109
Greg Daniela63e7ab2017-05-16 14:38:54 -0400110 if (fSupportsCopiesAsDraws) {
111 fCrossContextTextureSupport = true;
112 }
113
Greg Daniel5ba448c2018-02-26 13:30:47 -0500114 if (kARM_VkVendor == properties.vendorID) {
115 fInstanceAttribSupport = false;
116 }
117
Greg Daniel80a08dd2017-01-20 10:45:49 -0500118#if defined(SK_BUILD_FOR_WIN)
119 if (kNvidia_VkVendor == properties.vendorID) {
120 fMustSleepOnTearDown = true;
121 }
122#elif defined(SK_BUILD_FOR_ANDROID)
123 if (kImagination_VkVendor == properties.vendorID) {
124 fMustSleepOnTearDown = true;
125 }
126#endif
127
Greg Daniel164a9f02016-02-22 09:56:40 -0500128 this->applyOptionsOverrides(contextOptions);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500129 fShaderCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -0500130}
131
132int get_max_sample_count(VkSampleCountFlags flags) {
133 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
134 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
135 return 0;
136 }
137 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
138 return 2;
139 }
140 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
141 return 4;
142 }
143 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
144 return 8;
145 }
146 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
147 return 16;
148 }
149 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
150 return 32;
151 }
152 return 64;
153}
154
egdanield5e3b9e2016-03-08 12:19:54 -0800155void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700156 const VkPhysicalDeviceMemoryProperties& memoryProperties,
157 uint32_t featureFlags) {
Greg Danielc5cc2de2017-03-20 11:40:58 -0400158 // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
159 // need for us ever to support that amount, and it makes tests which tests all the vertex
160 // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
161 // we ever find that need.
162 static const uint32_t kMaxVertexAttributes = 64;
163 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
164 // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
165 if (kAMD_VkVendor == properties.vendorID) {
166 fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
167 }
168
egdanield5e3b9e2016-03-08 12:19:54 -0800169 // We could actually query and get a max size for each config, however maxImageDimension2D will
170 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700171 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
172 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800173
Chris Dalton2612bae2018-02-22 13:41:37 -0700174 // TODO: check if RT's larger than 4k incur a performance cost on ARM.
175 fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
176
egdanield5e3b9e2016-03-08 12:19:54 -0800177 // Assuming since we will always map in the end to upload the data we might as well just map
178 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700179 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800180
181 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
182
egdanield5e3b9e2016-03-08 12:19:54 -0800183 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700184 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
Greg Daniel22bc8652017-03-22 15:45:43 -0400185
186 // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
187 // Current workaround is to use a different secondary command buffer for each new VkPipeline.
188 if (kAMD_VkVendor == properties.vendorID) {
Greg Daniele3cd6912017-05-17 11:15:55 -0400189 fNewCBOnPipelineChange = true;
Greg Daniel22bc8652017-03-22 15:45:43 -0400190 }
Greg Daniel01907872017-05-23 15:21:02 -0400191
Greg Daniel8385a8a2018-02-26 13:29:37 -0500192 if (kIntel_VkVendor == properties.vendorID) {
193 fCanUseWholeSizeOnFlushMappedMemory = false;
194 }
195
Greg Daniel01907872017-05-23 15:21:02 -0400196#if defined(SK_CPU_X86)
197 if (kImagination_VkVendor == properties.vendorID) {
198 fSRGBSupport = false;
199 }
200#endif
egdanield5e3b9e2016-03-08 12:19:54 -0800201}
202
Brian Salomon1edc5b92016-11-29 13:43:46 -0500203void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
204 GrShaderCaps* shaderCaps = fShaderCaps.get();
205 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700206
Greg Daniel164a9f02016-02-22 09:56:40 -0500207
208 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
209 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
210 GrPixelConfig config = static_cast<GrPixelConfig>(i);
Greg Danielef59d872017-11-17 16:47:21 -0500211 // Vulkan doesn't support a single channel format stored in alpha.
212 if (GrPixelConfigIsAlphaOnly(config) &&
213 kAlpha_8_as_Alpha_GrPixelConfig != config) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500214 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
215 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500216 } else {
Greg Daniel7af060a2017-12-05 16:27:11 -0500217 if (kGray_8_GrPixelConfig == config ||
218 kGray_8_as_Red_GrPixelConfig == config) {
Brian Osman986563b2017-01-10 14:20:02 -0500219 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
220 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700221 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
222 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
223 // or writing to outputs. Since we're not actually changing the data at all, the
224 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500225 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
226 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700227 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500228 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700229 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500230 }
231 }
egdaniel67acb832016-02-26 08:32:20 -0800232
Greg Daniel80a08dd2017-01-20 10:45:49 -0500233 if (kImagination_VkVendor == properties.vendorID) {
234 shaderCaps->fAtan2ImplementedAsAtanYOverX = true;
235 }
236
egdanield5e3b9e2016-03-08 12:19:54 -0800237 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500238 shaderCaps->fUsesPrecisionModifiers = true;
239 shaderCaps->fFlatInterpolationSupport = true;
Brian Salomon41274562017-09-15 09:40:03 -0700240 // Flat interpolation appears to be slow on Qualcomm GPUs. This was tested in GL and is assumed
241 // to be true with Vulkan as well.
242 shaderCaps->fPreferFlatInterpolation = kQualcomm_VkVendor != properties.vendorID;
egdanield5e3b9e2016-03-08 12:19:54 -0800243
244 // GrShaderCaps
245
Brian Salomon1edc5b92016-11-29 13:43:46 -0500246 shaderCaps->fShaderDerivativeSupport = true;
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600247
Brian Salomon1edc5b92016-11-29 13:43:46 -0500248 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
Chris Daltonf1b47bb2017-10-06 11:57:51 -0600249 shaderCaps->fGSInvocationsSupport = shaderCaps->fGeometryShaderSupport;
egdanield632bb42016-03-30 12:06:48 -0700250
Brian Salomon1edc5b92016-11-29 13:43:46 -0500251 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
Greg Daniel84cff132017-03-22 16:25:53 -0400252 if (kAMD_VkVendor == properties.vendorID) {
253 // Currently DualSourceBlending is not working on AMD. vkCreateGraphicsPipeline fails when
254 // using a draw with dual source. Looking into whether it is driver bug or issue with our
255 // SPIR-V. Bug skia:6405
256 shaderCaps->fDualSourceBlendingSupport = false;
257 }
egdanield632bb42016-03-30 12:06:48 -0700258
Brian Salomon1edc5b92016-11-29 13:43:46 -0500259 shaderCaps->fIntegerSupport = true;
Chris Dalton6b65b982017-07-06 11:04:00 -0600260 shaderCaps->fTexelBufferSupport = true;
261 shaderCaps->fTexelFetchSupport = true;
Chris Dalton1d616352017-05-31 12:51:23 -0600262 shaderCaps->fVertexIDSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800263
cdaltona6b92ad2016-04-11 12:03:08 -0700264 // Assume the minimum precisions mandated by the SPIR-V spec.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700265 shaderCaps->fFloatIs32Bits = true;
266 shaderCaps->fHalfIs32Bits = false;
cdaltona6b92ad2016-04-11 12:03:08 -0700267
Brian Salomon1edc5b92016-11-29 13:43:46 -0500268 shaderCaps->fMaxVertexSamplers =
269 shaderCaps->fMaxGeometrySamplers =
270 shaderCaps->fMaxFragmentSamplers = SkTMin(
271 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
272 properties.limits.maxPerStageDescriptorSamplers),
273 (uint32_t)INT_MAX);
274 shaderCaps->fMaxCombinedSamplers = SkTMin(
275 SkTMin(properties.limits.maxDescriptorSetSampledImages,
276 properties.limits.maxDescriptorSetSamplers),
277 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500278}
279
egdaniel8f1dcaa2016-04-01 10:10:45 -0700280bool stencil_format_supported(const GrVkInterface* interface,
281 VkPhysicalDevice physDev,
282 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500283 VkFormatProperties props;
284 memset(&props, 0, sizeof(VkFormatProperties));
285 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700286 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500287}
288
egdaniel8f1dcaa2016-04-01 10:10:45 -0700289void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
290 // List of legal stencil formats (though perhaps not supported on
291 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700292 // 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 -0700293 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500294 static const StencilFormat
295 // internal Format stencil bits total bits packed?
296 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700297 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
298 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500299
egdaniel8f1dcaa2016-04-01 10:10:45 -0700300 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
301 fPreferedStencilFormat = gS8;
302 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
303 fPreferedStencilFormat = gD24S8;
304 } else {
305 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
306 fPreferedStencilFormat = gD32S8;
307 }
308}
309
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000310void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev,
311 const VkPhysicalDeviceProperties& properties) {
egdaniel8f1dcaa2016-04-01 10:10:45 -0700312 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
313 VkFormat format;
314 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
Greg Daniel01907872017-05-23 15:21:02 -0400315 if (!GrPixelConfigIsSRGB(static_cast<GrPixelConfig>(i)) || fSRGBSupport) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000316 fConfigTable[i].init(interface, physDev, properties, format);
Greg Daniel01907872017-05-23 15:21:02 -0400317 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700318 }
319 }
320}
321
322void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
323 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
324 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
325 *flags = *flags | kTextureable_Flag;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700326
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400327 // Ganesh assumes that all renderable surfaces are also texturable
328 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
329 *flags = *flags | kRenderable_Flag;
330 }
egdaniel8f1dcaa2016-04-01 10:10:45 -0700331 }
332
333 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
334 *flags = *flags | kBlitSrc_Flag;
335 }
336
337 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
338 *flags = *flags | kBlitDst_Flag;
339 }
340}
341
Greg Daniel81e7bf82017-07-19 14:47:42 -0400342void GrVkCaps::ConfigInfo::initSampleCounts(const GrVkInterface* interface,
343 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000344 const VkPhysicalDeviceProperties& physProps,
Greg Daniel81e7bf82017-07-19 14:47:42 -0400345 VkFormat format) {
346 VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
347 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
348 VK_IMAGE_USAGE_SAMPLED_BIT |
349 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
350 VkImageCreateFlags createFlags = GrVkFormatIsSRGB(format, nullptr)
351 ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
352 VkImageFormatProperties properties;
353 GR_VK_CALL(interface, GetPhysicalDeviceImageFormatProperties(physDev,
354 format,
355 VK_IMAGE_TYPE_2D,
356 VK_IMAGE_TILING_OPTIMAL,
357 usage,
358 createFlags,
359 &properties));
360 VkSampleCountFlags flags = properties.sampleCounts;
361 if (flags & VK_SAMPLE_COUNT_1_BIT) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500362 fColorSampleCounts.push(1);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400363 }
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000364 if (kImagination_VkVendor == physProps.vendorID) {
365 // MSAA does not work on imagination
366 return;
367 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400368 if (flags & VK_SAMPLE_COUNT_2_BIT) {
369 fColorSampleCounts.push(2);
370 }
371 if (flags & VK_SAMPLE_COUNT_4_BIT) {
372 fColorSampleCounts.push(4);
373 }
374 if (flags & VK_SAMPLE_COUNT_8_BIT) {
375 fColorSampleCounts.push(8);
376 }
377 if (flags & VK_SAMPLE_COUNT_16_BIT) {
378 fColorSampleCounts.push(16);
379 }
380 if (flags & VK_SAMPLE_COUNT_32_BIT) {
381 fColorSampleCounts.push(32);
382 }
383 if (flags & VK_SAMPLE_COUNT_64_BIT) {
384 fColorSampleCounts.push(64);
385 }
386}
387
egdaniel8f1dcaa2016-04-01 10:10:45 -0700388void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
389 VkPhysicalDevice physDev,
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000390 const VkPhysicalDeviceProperties& properties,
egdaniel8f1dcaa2016-04-01 10:10:45 -0700391 VkFormat format) {
392 VkFormatProperties props;
393 memset(&props, 0, sizeof(VkFormatProperties));
394 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
395 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
396 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400397 if (fOptimalFlags & kRenderable_Flag) {
Greg Daniel2bb6ecc2017-07-20 13:11:14 +0000398 this->initSampleCounts(interface, physDev, properties, format);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400399 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500400}
Greg Daniel81e7bf82017-07-19 14:47:42 -0400401
Brian Salomonbdecacf2018-02-02 20:32:49 -0500402int GrVkCaps::getRenderTargetSampleCount(int requestedCount, GrPixelConfig config) const {
403 requestedCount = SkTMax(1, requestedCount);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400404 int count = fConfigTable[config].fColorSampleCounts.count();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500405
406 if (!count) {
Greg Daniel81e7bf82017-07-19 14:47:42 -0400407 return 0;
408 }
409
Brian Salomonbdecacf2018-02-02 20:32:49 -0500410 if (1 == requestedCount) {
411 SkASSERT(fConfigTable[config].fColorSampleCounts.count() &&
412 fConfigTable[config].fColorSampleCounts[0] == 1);
413 return 1;
414 }
415
Greg Daniel81e7bf82017-07-19 14:47:42 -0400416 for (int i = 0; i < count; ++i) {
417 if (fConfigTable[config].fColorSampleCounts[i] >= requestedCount) {
418 return fConfigTable[config].fColorSampleCounts[i];
419 }
420 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500421 return 0;
422}
423
424int GrVkCaps::maxRenderTargetSampleCount(GrPixelConfig config) const {
425 const auto& table = fConfigTable[config].fColorSampleCounts;
426 if (!table.count()) {
427 return 0;
428 }
429 return table[table.count() - 1];
Brian Salomond653cac2018-02-01 13:58:00 -0500430}
431
Brian Salomon3d86a192018-02-27 16:46:11 -0500432bool GrVkCaps::surfaceSupportsWritePixels(const GrSurface* surface) const {
433 if (auto rt = surface->asRenderTarget()) {
434 return rt->numColorSamples() <= 1 && SkToBool(surface->asTexture());
435 }
436 return true;
437}
438
Robert Phillipsfc711a22018-02-13 17:03:00 -0500439bool validate_image_info(VkFormat format, SkColorType ct, GrPixelConfig* config) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500440 *config = kUnknown_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500441
442 switch (ct) {
443 case kUnknown_SkColorType:
444 return false;
445 case kAlpha_8_SkColorType:
446 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500447 *config = kAlpha_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500448 }
449 break;
450 case kRGB_565_SkColorType:
451 if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500452 *config = kRGB_565_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500453 }
454 break;
455 case kARGB_4444_SkColorType:
456 if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500457 *config = kRGBA_4444_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500458 }
459 break;
460 case kRGBA_8888_SkColorType:
461 if (VK_FORMAT_R8G8B8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500462 *config = kRGBA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500463 } else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500464 *config = kSRGBA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500465 }
466 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500467 case kRGB_888x_SkColorType:
468 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500469 case kBGRA_8888_SkColorType:
470 if (VK_FORMAT_B8G8R8A8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500471 *config = kBGRA_8888_GrPixelConfig;
Greg Daniel7b219ac2017-12-18 14:49:04 -0500472 } else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500473 *config = kSBGRA_8888_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500474 }
475 break;
Brian Salomone41e1762018-01-25 14:07:47 -0500476 case kRGBA_1010102_SkColorType:
477 return false;
478 case kRGB_101010x_SkColorType:
479 return false;
Greg Danielf5d87582017-12-18 14:48:15 -0500480 case kGray_8_SkColorType:
481 if (VK_FORMAT_R8_UNORM == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500482 *config = kGray_8_as_Red_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500483 }
484 break;
485 case kRGBA_F16_SkColorType:
486 if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
Greg Danielfaa095e2017-12-19 13:15:02 -0500487 *config = kRGBA_half_GrPixelConfig;
Greg Danielf5d87582017-12-18 14:48:15 -0500488 }
489 break;
490 }
491
Greg Danielfaa095e2017-12-19 13:15:02 -0500492 return kUnknown_GrPixelConfig != *config;
Greg Danielf5d87582017-12-18 14:48:15 -0500493}
494
Greg Danielfaa095e2017-12-19 13:15:02 -0500495bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
496 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500497 const GrVkImageInfo* imageInfo = tex.getVkImageInfo();
498 if (!imageInfo) {
499 return false;
500 }
501
502 return validate_image_info(imageInfo->fFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500503}
504
505bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
506 GrPixelConfig* config) const {
Robert Phillipsfc711a22018-02-13 17:03:00 -0500507 const GrVkImageInfo* imageInfo = rt.getVkImageInfo();
508 if (!imageInfo) {
509 return false;
510 }
511
512 return validate_image_info(imageInfo->fFormat, ct, config);
513}
514
515bool GrVkCaps::getConfigFromBackendFormat(const GrBackendFormat& format, SkColorType ct,
516 GrPixelConfig* config) const {
517 const VkFormat* vkFormat = format.getVkFormat();
518 if (!vkFormat) {
519 return false;
520 }
521 return validate_image_info(*vkFormat, ct, config);
Greg Danielfaa095e2017-12-19 13:15:02 -0500522}
Greg Danielf5d87582017-12-18 14:48:15 -0500523