blob: 16a91c1a1f55f09577252ed4134e7820c4a55977 [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"
Brian Salomon467921e2017-03-06 16:17:12 -05009#include "GrRenderTarget.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkUtil.h"
jvanverthfd7bd452016-03-25 06:29:52 -070012#include "vk/GrVkBackendContext.h"
Brian Salomon467921e2017-03-06 16:17:12 -050013#include "vk/GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014
15GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070016 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags)
17 : INHERITED(contextOptions) {
18 fCanUseGLSLForShaderModule = false;
egdaniel6fa0a912016-09-12 11:51:29 -070019 fMustDoCopiesFromOrigin = false;
egdanielbc9b2962016-09-27 08:00:53 -070020 fSupportsCopiesAsDraws = false;
egdanielfd016d72016-09-27 12:13:05 -070021 fMustSubmitCommandsBeforeCopyOp = false;
Greg Daniel80a08dd2017-01-20 10:45:49 -050022 fMustSleepOnTearDown = false;
egdanielc5ec1402016-03-28 12:14:42 -070023
Greg Daniel164a9f02016-02-22 09:56:40 -050024 /**************************************************************************
25 * GrDrawTargetCaps fields
26 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070027 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070028 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070029 fNPOTTextureTileSupport = true; // always available in Vulkan
jvanverthc8ef0532016-05-03 10:45:48 -070030 fTwoSidedStencilSupport = true; // always available in Vulkan
egdaniel50134cc2016-05-23 12:34:38 -070031 fStencilWrapOpsSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070032 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050033 fReuseScratchTextures = true; //TODO: figure this out
34 fGpuTracingSupport = false; //TODO: figure this out
35 fCompressedTexSubImageSupport = false; //TODO: figure this out
36 fOversizedStencilSupport = false; //TODO: figure this out
37
egdaniel9cb63402016-06-23 08:37:05 -070038 fUseDrawInsteadOfClear = false;
jvanverth84741b32016-09-30 08:39:02 -070039 fFenceSyncSupport = true; // always available in Vulkan
Brian Osman2c2bc112017-02-28 10:02:49 -050040 fCrossContextTextureSupport = false; // TODO: Add thread-safe memory pools so we can enable this
Greg Daniel164a9f02016-02-22 09:56:40 -050041
42 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070043 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050044
45 fMaxRenderTargetSize = 4096; // minimum required by spec
46 fMaxTextureSize = 4096; // minimum required by spec
47 fMaxColorSampleCount = 4; // minimum required by spec
48 fMaxStencilSampleCount = 4; // minimum required by spec
49
Brian Salomon94efbf52016-11-29 13:43:05 -050050 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050051
egdanielc5ec1402016-03-28 12:14:42 -070052 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050053}
54
Brian Salomon467921e2017-03-06 16:17:12 -050055bool GrVkCaps::initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const {
56 // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
57 // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
58 // render target as well.
59 desc->fOrigin = src->origin();
60 desc->fConfig = src->config();
61 if (src->numColorSamples() > 1 || (src->asTexture() && this->supportsCopiesAsDraws())) {
62 desc->fFlags = kRenderTarget_GrSurfaceFlag;
63 } else {
64 // Just going to use CopyImage here
65 desc->fFlags = kNone_GrSurfaceFlags;
66 }
67
68 return true;
69}
70
Greg Daniel164a9f02016-02-22 09:56:40 -050071void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070072 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050073
egdanield5e3b9e2016-03-08 12:19:54 -080074 VkPhysicalDeviceProperties properties;
75 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
76
egdanield5e3b9e2016-03-08 12:19:54 -080077 VkPhysicalDeviceMemoryProperties memoryProperties;
78 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
79
jvanverthfd7bd452016-03-25 06:29:52 -070080 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -050081 this->initShaderCaps(properties, featureFlags);
egdaniel8f1dcaa2016-04-01 10:10:45 -070082 this->initConfigTable(vkInterface, physDev);
83 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050084
egdanielc5ec1402016-03-28 12:14:42 -070085 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070086 // Currently disabling this feature since it does not play well with validation layers which
87 // expect a SPIR-V shader
88 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070089 }
Greg Daniel164a9f02016-02-22 09:56:40 -050090
egdaniel6fa0a912016-09-12 11:51:29 -070091 if (kQualcomm_VkVendor == properties.vendorID) {
92 fMustDoCopiesFromOrigin = true;
93 }
94
egdanielfd016d72016-09-27 12:13:05 -070095 if (kNvidia_VkVendor == properties.vendorID) {
96 fSupportsCopiesAsDraws = true;
97 fMustSubmitCommandsBeforeCopyOp = true;
98 }
99
Greg Daniel80a08dd2017-01-20 10:45:49 -0500100#if defined(SK_BUILD_FOR_WIN)
101 if (kNvidia_VkVendor == properties.vendorID) {
102 fMustSleepOnTearDown = true;
103 }
104#elif defined(SK_BUILD_FOR_ANDROID)
105 if (kImagination_VkVendor == properties.vendorID) {
106 fMustSleepOnTearDown = true;
107 }
108#endif
109
Greg Daniel164a9f02016-02-22 09:56:40 -0500110 this->applyOptionsOverrides(contextOptions);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500111 fShaderCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -0500112}
113
114int get_max_sample_count(VkSampleCountFlags flags) {
115 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
116 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
117 return 0;
118 }
119 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
120 return 2;
121 }
122 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
123 return 4;
124 }
125 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
126 return 8;
127 }
128 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
129 return 16;
130 }
131 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
132 return 32;
133 }
134 return 64;
135}
136
137void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
138 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
139 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
140
141 fMaxColorSampleCount = get_max_sample_count(colorSamples);
142 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
143}
144
egdanield5e3b9e2016-03-08 12:19:54 -0800145void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700146 const VkPhysicalDeviceMemoryProperties& memoryProperties,
147 uint32_t featureFlags) {
jvanverthe78d4872016-09-27 03:33:05 -0700148 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800149 // We could actually query and get a max size for each config, however maxImageDimension2D will
150 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700151 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
152 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800153
154 this->initSampleCount(properties);
155
egdanield5e3b9e2016-03-08 12:19:54 -0800156 // Assuming since we will always map in the end to upload the data we might as well just map
157 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700158 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800159
160 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
161
162 fStencilWrapOpsSupport = true;
163 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700164 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800165}
166
Brian Salomon1edc5b92016-11-29 13:43:46 -0500167void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
168 GrShaderCaps* shaderCaps = fShaderCaps.get();
169 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700170
Greg Daniel164a9f02016-02-22 09:56:40 -0500171
172 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
173 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
174 GrPixelConfig config = static_cast<GrPixelConfig>(i);
175 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500176 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
177 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500178 } else {
Brian Osman986563b2017-01-10 14:20:02 -0500179 if (kGray_8_GrPixelConfig == config) {
180 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
181 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700182 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
183 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
184 // or writing to outputs. Since we're not actually changing the data at all, the
185 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500186 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
187 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700188 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500189 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700190 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500191 }
192 }
egdaniel67acb832016-02-26 08:32:20 -0800193
Greg Daniel80a08dd2017-01-20 10:45:49 -0500194 if (kImagination_VkVendor == properties.vendorID) {
195 shaderCaps->fAtan2ImplementedAsAtanYOverX = true;
196 }
197
egdanield5e3b9e2016-03-08 12:19:54 -0800198 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500199 shaderCaps->fUsesPrecisionModifiers = true;
200 shaderCaps->fFlatInterpolationSupport = true;
egdanield5e3b9e2016-03-08 12:19:54 -0800201
202 // GrShaderCaps
203
Brian Salomon1edc5b92016-11-29 13:43:46 -0500204 shaderCaps->fShaderDerivativeSupport = true;
205 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700206
Brian Salomon1edc5b92016-11-29 13:43:46 -0500207 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700208
Brian Salomon1edc5b92016-11-29 13:43:46 -0500209 shaderCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800210
cdaltona6b92ad2016-04-11 12:03:08 -0700211 // Assume the minimum precisions mandated by the SPIR-V spec.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500212 shaderCaps->fShaderPrecisionVaries = true;
cdaltona6b92ad2016-04-11 12:03:08 -0700213 for (int s = 0; s < kGrShaderTypeCount; ++s) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500214 auto& highp = shaderCaps->fFloatPrecisions[s][kHigh_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700215 highp.fLogRangeLow = highp.fLogRangeHigh = 127;
216 highp.fBits = 23;
217
Brian Salomon1edc5b92016-11-29 13:43:46 -0500218 auto& mediump = shaderCaps->fFloatPrecisions[s][kMedium_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700219 mediump.fLogRangeLow = mediump.fLogRangeHigh = 14;
220 mediump.fBits = 10;
221
Brian Salomon1edc5b92016-11-29 13:43:46 -0500222 shaderCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump;
cdaltona6b92ad2016-04-11 12:03:08 -0700223 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500224 shaderCaps->initSamplerPrecisionTable();
cdaltona6b92ad2016-04-11 12:03:08 -0700225
Brian Salomon1edc5b92016-11-29 13:43:46 -0500226 shaderCaps->fMaxVertexSamplers =
227 shaderCaps->fMaxGeometrySamplers =
228 shaderCaps->fMaxFragmentSamplers = SkTMin(
229 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
230 properties.limits.maxPerStageDescriptorSamplers),
231 (uint32_t)INT_MAX);
232 shaderCaps->fMaxCombinedSamplers = SkTMin(
233 SkTMin(properties.limits.maxDescriptorSetSampledImages,
234 properties.limits.maxDescriptorSetSamplers),
235 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500236}
237
egdaniel8f1dcaa2016-04-01 10:10:45 -0700238bool stencil_format_supported(const GrVkInterface* interface,
239 VkPhysicalDevice physDev,
240 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500241 VkFormatProperties props;
242 memset(&props, 0, sizeof(VkFormatProperties));
243 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700244 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500245}
246
egdaniel8f1dcaa2016-04-01 10:10:45 -0700247void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
248 // List of legal stencil formats (though perhaps not supported on
249 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700250 // 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 -0700251 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500252 static const StencilFormat
253 // internal Format stencil bits total bits packed?
254 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700255 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
256 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500257
egdaniel8f1dcaa2016-04-01 10:10:45 -0700258 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
259 fPreferedStencilFormat = gS8;
260 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
261 fPreferedStencilFormat = gD24S8;
262 } else {
263 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
264 fPreferedStencilFormat = gD32S8;
265 }
266}
267
268void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
269 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
270 VkFormat format;
271 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
272 fConfigTable[i].init(interface, physDev, format);
273 }
274 }
Robert Phillips351b0452017-02-01 14:45:04 -0500275
276 // We currently do not support compressed textures in Vulkan
277 const uint16_t kFlagsToRemove = ConfigInfo::kTextureable_Flag|ConfigInfo::kRenderable_Flag;
278 fConfigTable[kETC1_GrPixelConfig].fOptimalFlags &= ~kFlagsToRemove;
279 fConfigTable[kETC1_GrPixelConfig].fLinearFlags &= ~kFlagsToRemove;
egdaniel8f1dcaa2016-04-01 10:10:45 -0700280}
281
282void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
283 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
284 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
285 *flags = *flags | kTextureable_Flag;
286 }
287
288 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
289 *flags = *flags | kRenderable_Flag;
290 }
291
292 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
293 *flags = *flags | kBlitSrc_Flag;
294 }
295
296 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
297 *flags = *flags | kBlitDst_Flag;
298 }
299}
300
301void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
302 VkPhysicalDevice physDev,
303 VkFormat format) {
304 VkFormatProperties props;
305 memset(&props, 0, sizeof(VkFormatProperties));
306 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
307 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
308 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -0500309}