blob: 383d3caf4947f2aaf8c3347cd1c215e10a4c251c [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"
9
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkUtil.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "vk/GrVkInterface.h"
jvanverthfd7bd452016-03-25 06:29:52 -070013#include "vk/GrVkBackendContext.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
Greg Daniel164a9f02016-02-22 09:56:40 -050040
41 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070042 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050043
44 fMaxRenderTargetSize = 4096; // minimum required by spec
45 fMaxTextureSize = 4096; // minimum required by spec
46 fMaxColorSampleCount = 4; // minimum required by spec
47 fMaxStencilSampleCount = 4; // minimum required by spec
48
Brian Salomon94efbf52016-11-29 13:43:05 -050049 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050050
egdanielc5ec1402016-03-28 12:14:42 -070051 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050052}
53
54void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070055 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050056
egdanield5e3b9e2016-03-08 12:19:54 -080057 VkPhysicalDeviceProperties properties;
58 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
59
egdanield5e3b9e2016-03-08 12:19:54 -080060 VkPhysicalDeviceMemoryProperties memoryProperties;
61 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
62
jvanverthfd7bd452016-03-25 06:29:52 -070063 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -050064 this->initShaderCaps(properties, featureFlags);
egdaniel8f1dcaa2016-04-01 10:10:45 -070065 this->initConfigTable(vkInterface, physDev);
66 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050067
egdanielc5ec1402016-03-28 12:14:42 -070068 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070069 // Currently disabling this feature since it does not play well with validation layers which
70 // expect a SPIR-V shader
71 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070072 }
Greg Daniel164a9f02016-02-22 09:56:40 -050073
egdaniel6fa0a912016-09-12 11:51:29 -070074 if (kQualcomm_VkVendor == properties.vendorID) {
75 fMustDoCopiesFromOrigin = true;
76 }
77
egdanielfd016d72016-09-27 12:13:05 -070078 if (kNvidia_VkVendor == properties.vendorID) {
79 fSupportsCopiesAsDraws = true;
80 fMustSubmitCommandsBeforeCopyOp = true;
81 }
82
Greg Daniel80a08dd2017-01-20 10:45:49 -050083#if defined(SK_BUILD_FOR_WIN)
84 if (kNvidia_VkVendor == properties.vendorID) {
85 fMustSleepOnTearDown = true;
86 }
87#elif defined(SK_BUILD_FOR_ANDROID)
88 if (kImagination_VkVendor == properties.vendorID) {
89 fMustSleepOnTearDown = true;
90 }
91#endif
92
Greg Daniel164a9f02016-02-22 09:56:40 -050093 this->applyOptionsOverrides(contextOptions);
Brian Salomon1edc5b92016-11-29 13:43:46 -050094 fShaderCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050095}
96
97int get_max_sample_count(VkSampleCountFlags flags) {
98 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
99 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
100 return 0;
101 }
102 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
103 return 2;
104 }
105 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
106 return 4;
107 }
108 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
109 return 8;
110 }
111 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
112 return 16;
113 }
114 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
115 return 32;
116 }
117 return 64;
118}
119
120void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
121 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
122 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
123
124 fMaxColorSampleCount = get_max_sample_count(colorSamples);
125 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
126}
127
egdanield5e3b9e2016-03-08 12:19:54 -0800128void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700129 const VkPhysicalDeviceMemoryProperties& memoryProperties,
130 uint32_t featureFlags) {
jvanverthe78d4872016-09-27 03:33:05 -0700131 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800132 // We could actually query and get a max size for each config, however maxImageDimension2D will
133 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700134 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
135 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800136
137 this->initSampleCount(properties);
138
egdanield5e3b9e2016-03-08 12:19:54 -0800139 // Assuming since we will always map in the end to upload the data we might as well just map
140 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700141 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800142
143 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
144
145 fStencilWrapOpsSupport = true;
146 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700147 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800148}
149
Brian Salomon1edc5b92016-11-29 13:43:46 -0500150void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
151 GrShaderCaps* shaderCaps = fShaderCaps.get();
152 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700153
Greg Daniel164a9f02016-02-22 09:56:40 -0500154
155 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
156 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
157 GrPixelConfig config = static_cast<GrPixelConfig>(i);
158 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500159 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
160 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500161 } else {
Brian Osman986563b2017-01-10 14:20:02 -0500162 if (kGray_8_GrPixelConfig == config) {
163 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
164 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700165 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
166 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
167 // or writing to outputs. Since we're not actually changing the data at all, the
168 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500169 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
170 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700171 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500172 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700173 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500174 }
175 }
egdaniel67acb832016-02-26 08:32:20 -0800176
Greg Daniel80a08dd2017-01-20 10:45:49 -0500177 if (kImagination_VkVendor == properties.vendorID) {
178 shaderCaps->fAtan2ImplementedAsAtanYOverX = true;
179 }
180
egdanield5e3b9e2016-03-08 12:19:54 -0800181 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500182 shaderCaps->fUsesPrecisionModifiers = true;
183 shaderCaps->fFlatInterpolationSupport = true;
egdanield5e3b9e2016-03-08 12:19:54 -0800184
185 // GrShaderCaps
186
Brian Salomon1edc5b92016-11-29 13:43:46 -0500187 shaderCaps->fShaderDerivativeSupport = true;
188 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700189
Brian Salomon1edc5b92016-11-29 13:43:46 -0500190 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700191
Brian Salomon1edc5b92016-11-29 13:43:46 -0500192 shaderCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800193
cdaltona6b92ad2016-04-11 12:03:08 -0700194 // Assume the minimum precisions mandated by the SPIR-V spec.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500195 shaderCaps->fShaderPrecisionVaries = true;
cdaltona6b92ad2016-04-11 12:03:08 -0700196 for (int s = 0; s < kGrShaderTypeCount; ++s) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500197 auto& highp = shaderCaps->fFloatPrecisions[s][kHigh_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700198 highp.fLogRangeLow = highp.fLogRangeHigh = 127;
199 highp.fBits = 23;
200
Brian Salomon1edc5b92016-11-29 13:43:46 -0500201 auto& mediump = shaderCaps->fFloatPrecisions[s][kMedium_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700202 mediump.fLogRangeLow = mediump.fLogRangeHigh = 14;
203 mediump.fBits = 10;
204
Brian Salomon1edc5b92016-11-29 13:43:46 -0500205 shaderCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump;
cdaltona6b92ad2016-04-11 12:03:08 -0700206 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500207 shaderCaps->initSamplerPrecisionTable();
cdaltona6b92ad2016-04-11 12:03:08 -0700208
Brian Salomon1edc5b92016-11-29 13:43:46 -0500209 shaderCaps->fMaxVertexSamplers =
210 shaderCaps->fMaxGeometrySamplers =
211 shaderCaps->fMaxFragmentSamplers = SkTMin(
212 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
213 properties.limits.maxPerStageDescriptorSamplers),
214 (uint32_t)INT_MAX);
215 shaderCaps->fMaxCombinedSamplers = SkTMin(
216 SkTMin(properties.limits.maxDescriptorSetSampledImages,
217 properties.limits.maxDescriptorSetSamplers),
218 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500219}
220
egdaniel8f1dcaa2016-04-01 10:10:45 -0700221bool stencil_format_supported(const GrVkInterface* interface,
222 VkPhysicalDevice physDev,
223 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500224 VkFormatProperties props;
225 memset(&props, 0, sizeof(VkFormatProperties));
226 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700227 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500228}
229
egdaniel8f1dcaa2016-04-01 10:10:45 -0700230void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
231 // List of legal stencil formats (though perhaps not supported on
232 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700233 // 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 -0700234 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500235 static const StencilFormat
236 // internal Format stencil bits total bits packed?
237 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700238 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
239 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500240
egdaniel8f1dcaa2016-04-01 10:10:45 -0700241 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
242 fPreferedStencilFormat = gS8;
243 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
244 fPreferedStencilFormat = gD24S8;
245 } else {
246 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
247 fPreferedStencilFormat = gD32S8;
248 }
249}
250
251void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
252 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
253 VkFormat format;
254 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
255 fConfigTable[i].init(interface, physDev, format);
256 }
257 }
258}
259
260void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
261 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
262 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
263 *flags = *flags | kTextureable_Flag;
264 }
265
266 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
267 *flags = *flags | kRenderable_Flag;
268 }
269
270 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
271 *flags = *flags | kBlitSrc_Flag;
272 }
273
274 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
275 *flags = *flags | kBlitDst_Flag;
276 }
277}
278
279void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
280 VkPhysicalDevice physDev,
281 VkFormat format) {
282 VkFormatProperties props;
283 memset(&props, 0, sizeof(VkFormatProperties));
284 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
285 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
286 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -0500287}