blob: 88ff8ee3e060858256684533bc9766a974467e1e [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
10#include "GrVkUtil.h"
11#include "glsl/GrGLSLCaps.h"
12#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;
19
Greg Daniel164a9f02016-02-22 09:56:40 -050020 /**************************************************************************
21 * GrDrawTargetCaps fields
22 **************************************************************************/
23 fMipMapSupport = false; //TODO: figure this out
24 fNPOTTextureTileSupport = false; //TODO: figure this out
25 fTwoSidedStencilSupport = false; //TODO: figure this out
26 fStencilWrapOpsSupport = false; //TODO: figure this out
27 fDiscardRenderTargetSupport = false; //TODO: figure this out
28 fReuseScratchTextures = true; //TODO: figure this out
29 fGpuTracingSupport = false; //TODO: figure this out
30 fCompressedTexSubImageSupport = false; //TODO: figure this out
31 fOversizedStencilSupport = false; //TODO: figure this out
32
33 fUseDrawInsteadOfClear = false; //TODO: figure this out
34
35 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070036 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050037
38 fMaxRenderTargetSize = 4096; // minimum required by spec
39 fMaxTextureSize = 4096; // minimum required by spec
40 fMaxColorSampleCount = 4; // minimum required by spec
41 fMaxStencilSampleCount = 4; // minimum required by spec
42
43
44 fShaderCaps.reset(new GrGLSLCaps(contextOptions));
45
egdanielc5ec1402016-03-28 12:14:42 -070046 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050047}
48
49void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070050 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050051
egdanield5e3b9e2016-03-08 12:19:54 -080052 VkPhysicalDeviceProperties properties;
53 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
54
egdanield5e3b9e2016-03-08 12:19:54 -080055 VkPhysicalDeviceMemoryProperties memoryProperties;
56 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
57
jvanverthfd7bd452016-03-25 06:29:52 -070058 this->initGrCaps(properties, memoryProperties, featureFlags);
59 this->initGLSLCaps(properties, featureFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050060 this->initConfigTexturableTable(vkInterface, physDev);
61 this->initConfigRenderableTable(vkInterface, physDev);
62 this->initStencilFormats(vkInterface, physDev);
63
egdanielc5ec1402016-03-28 12:14:42 -070064 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070065 // Currently disabling this feature since it does not play well with validation layers which
66 // expect a SPIR-V shader
67 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070068 }
Greg Daniel164a9f02016-02-22 09:56:40 -050069
70 this->applyOptionsOverrides(contextOptions);
egdanielc5ec1402016-03-28 12:14:42 -070071 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
72 glslCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050073}
74
75int get_max_sample_count(VkSampleCountFlags flags) {
76 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
77 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
78 return 0;
79 }
80 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
81 return 2;
82 }
83 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
84 return 4;
85 }
86 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
87 return 8;
88 }
89 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
90 return 16;
91 }
92 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
93 return 32;
94 }
95 return 64;
96}
97
98void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
99 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
100 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
101
102 fMaxColorSampleCount = get_max_sample_count(colorSamples);
103 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
104}
105
egdanield5e3b9e2016-03-08 12:19:54 -0800106void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700107 const VkPhysicalDeviceMemoryProperties& memoryProperties,
108 uint32_t featureFlags) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700109 fMaxVertexAttributes = properties.limits.maxVertexInputAttributes;
egdanield5e3b9e2016-03-08 12:19:54 -0800110 // We could actually query and get a max size for each config, however maxImageDimension2D will
111 // give the minimum max size across all configs. So for simplicity we will use that for now.
112 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
113 fMaxTextureSize = properties.limits.maxImageDimension2D;
114
115 this->initSampleCount(properties);
116
egdanield5e3b9e2016-03-08 12:19:54 -0800117 // Assuming since we will always map in the end to upload the data we might as well just map
118 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700119 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800120
121 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
122
123 fStencilWrapOpsSupport = true;
124 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700125 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800126}
127
jvanverthfd7bd452016-03-25 06:29:52 -0700128void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties,
129 uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdanield5e3b9e2016-03-08 12:19:54 -0800131 glslCaps->fVersionDeclString = "#version 310 es\n";
Greg Daniel164a9f02016-02-22 09:56:40 -0500132
133 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
134 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
135 GrPixelConfig config = static_cast<GrPixelConfig>(i);
136 if (GrPixelConfigIsAlphaOnly(config)) {
137 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
138 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
139 } else {
140 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
141 }
142 }
egdaniel67acb832016-02-26 08:32:20 -0800143
egdanield5e3b9e2016-03-08 12:19:54 -0800144 // Vulkan is based off ES 3.0 so the following should all be supported
145 glslCaps->fUsesPrecisionModifiers = true;
146 glslCaps->fFlatInterpolationSupport = true;
147
148 // GrShaderCaps
149
egdaniel67acb832016-02-26 08:32:20 -0800150 glslCaps->fShaderDerivativeSupport = true;
jvanverthfd7bd452016-03-25 06:29:52 -0700151 glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700152
jvanverthfd7bd452016-03-25 06:29:52 -0700153 glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700154
egdanield5e3b9e2016-03-08 12:19:54 -0800155 glslCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800156
157 glslCaps->fMaxVertexSamplers =
158 glslCaps->fMaxGeometrySamplers =
159 glslCaps->fMaxFragmentSamplers = SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
160 properties.limits.maxPerStageDescriptorSamplers);
161 glslCaps->fMaxCombinedSamplers = SkTMin(properties.limits.maxDescriptorSetSampledImages,
162 properties.limits.maxDescriptorSetSamplers);
Greg Daniel164a9f02016-02-22 09:56:40 -0500163}
164
165static void format_supported_for_feature(const GrVkInterface* interface,
166 VkPhysicalDevice physDev,
167 VkFormat format,
168 VkFormatFeatureFlagBits featureBit,
169 bool* linearSupport,
170 bool* optimalSupport) {
171 VkFormatProperties props;
172 memset(&props, 0, sizeof(VkFormatProperties));
173 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
174 *linearSupport = SkToBool(props.linearTilingFeatures & featureBit);
175 *optimalSupport = SkToBool(props.optimalTilingFeatures & featureBit);
176}
177
178static void config_supported_for_feature(const GrVkInterface* interface,
179 VkPhysicalDevice physDev,
180 GrPixelConfig config,
181 VkFormatFeatureFlagBits featureBit,
182 bool* linearSupport,
183 bool* optimalSupport) {
184 VkFormat format;
185 if (!GrPixelConfigToVkFormat(config, &format)) {
186 *linearSupport = false;
187 *optimalSupport = false;
188 return;
189 }
190 format_supported_for_feature(interface, physDev, format, featureBit,
191 linearSupport, optimalSupport);
192}
193
194// Currently just assumeing if something can be rendered to without MSAA it also works for MSAAA
195#define SET_CONFIG_IS_RENDERABLE(config) \
196 config_supported_for_feature(interface, \
197 physDev, \
198 config, \
199 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT, \
200 &fConfigLinearRenderSupport[config][kNo_MSAA], \
201 &fConfigRenderSupport[config][kNo_MSAA] ); \
202 fConfigRenderSupport[config][kYes_MSAA] = fConfigRenderSupport[config][kNo_MSAA]; \
203 fConfigLinearRenderSupport[config][kYes_MSAA] = fConfigLinearRenderSupport[config][kNo_MSAA];
204
205
206void GrVkCaps::initConfigRenderableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
207 enum {
208 kNo_MSAA = 0,
209 kYes_MSAA = 1,
210 };
211
212 // Base render support
213 SET_CONFIG_IS_RENDERABLE(kAlpha_8_GrPixelConfig);
214 SET_CONFIG_IS_RENDERABLE(kRGB_565_GrPixelConfig);
215 SET_CONFIG_IS_RENDERABLE(kRGBA_4444_GrPixelConfig);
216 SET_CONFIG_IS_RENDERABLE(kRGBA_8888_GrPixelConfig);
217 SET_CONFIG_IS_RENDERABLE(kBGRA_8888_GrPixelConfig);
218
219 SET_CONFIG_IS_RENDERABLE(kSRGBA_8888_GrPixelConfig);
220
221 // Float render support
222 SET_CONFIG_IS_RENDERABLE(kRGBA_float_GrPixelConfig);
223 SET_CONFIG_IS_RENDERABLE(kRGBA_half_GrPixelConfig);
224 SET_CONFIG_IS_RENDERABLE(kAlpha_half_GrPixelConfig);
225}
226
227#define SET_CONFIG_IS_TEXTURABLE(config) \
228 config_supported_for_feature(interface, \
229 physDev, \
230 config, \
231 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, \
232 &fConfigLinearTextureSupport[config], \
233 &fConfigTextureSupport[config]);
234
235void GrVkCaps::initConfigTexturableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
236 // Base texture support
237 SET_CONFIG_IS_TEXTURABLE(kAlpha_8_GrPixelConfig);
238 SET_CONFIG_IS_TEXTURABLE(kRGB_565_GrPixelConfig);
239 SET_CONFIG_IS_TEXTURABLE(kRGBA_4444_GrPixelConfig);
240 SET_CONFIG_IS_TEXTURABLE(kRGBA_8888_GrPixelConfig);
241 SET_CONFIG_IS_TEXTURABLE(kBGRA_8888_GrPixelConfig);
242
243 SET_CONFIG_IS_TEXTURABLE(kIndex_8_GrPixelConfig);
244 SET_CONFIG_IS_TEXTURABLE(kSRGBA_8888_GrPixelConfig);
245
246 // Compressed texture support
247 SET_CONFIG_IS_TEXTURABLE(kETC1_GrPixelConfig);
248 SET_CONFIG_IS_TEXTURABLE(kLATC_GrPixelConfig);
249 SET_CONFIG_IS_TEXTURABLE(kR11_EAC_GrPixelConfig);
250 SET_CONFIG_IS_TEXTURABLE(kASTC_12x12_GrPixelConfig);
251
252 // Float texture support
253 SET_CONFIG_IS_TEXTURABLE(kRGBA_float_GrPixelConfig);
254 SET_CONFIG_IS_TEXTURABLE(kRGBA_half_GrPixelConfig);
255 SET_CONFIG_IS_TEXTURABLE(kAlpha_half_GrPixelConfig);
256}
257
258#define SET_CONFIG_CAN_STENCIL(config) \
259 bool SK_MACRO_APPEND_LINE(linearSupported); \
260 bool SK_MACRO_APPEND_LINE(optimalSupported); \
261 format_supported_for_feature(interface, \
262 physDev, \
263 config.fInternalFormat, \
264 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, \
265 &SK_MACRO_APPEND_LINE(linearSupported), \
266 &SK_MACRO_APPEND_LINE(optimalSupported)); \
267 if (SK_MACRO_APPEND_LINE(linearSupported)) fLinearStencilFormats.push_back(config); \
268 if (SK_MACRO_APPEND_LINE(optimalSupported)) fStencilFormats.push_back(config);
269
270void GrVkCaps::initStencilFormats(const GrVkInterface* interface, VkPhysicalDevice physDev) {
271 // Build up list of legal stencil formats (though perhaps not supported on
272 // the particular gpu/driver) from most preferred to least.
273
274 static const StencilFormat
275 // internal Format stencil bits total bits packed?
276 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
277 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true };
278
279 // I'm simply assuming that these two will be supported since they are used in example code.
280 // TODO: Actaully figure this out
281 SET_CONFIG_CAN_STENCIL(gS8);
282 SET_CONFIG_CAN_STENCIL(gD24S8);
283}