blob: 41bf1ad9e47e41f1bed0a2d2c1379d817f57a6a7 [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)) {
65 fCanUseGLSLForShaderModule = true;
66 }
Greg Daniel164a9f02016-02-22 09:56:40 -050067
68 this->applyOptionsOverrides(contextOptions);
egdanielc5ec1402016-03-28 12:14:42 -070069 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
70 glslCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050071}
72
73int get_max_sample_count(VkSampleCountFlags flags) {
74 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
75 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
76 return 0;
77 }
78 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
79 return 2;
80 }
81 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
82 return 4;
83 }
84 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
85 return 8;
86 }
87 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
88 return 16;
89 }
90 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
91 return 32;
92 }
93 return 64;
94}
95
96void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
97 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
98 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
99
100 fMaxColorSampleCount = get_max_sample_count(colorSamples);
101 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
102}
103
egdanield5e3b9e2016-03-08 12:19:54 -0800104void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700105 const VkPhysicalDeviceMemoryProperties& memoryProperties,
106 uint32_t featureFlags) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700107 fMaxVertexAttributes = properties.limits.maxVertexInputAttributes;
egdanield5e3b9e2016-03-08 12:19:54 -0800108 // We could actually query and get a max size for each config, however maxImageDimension2D will
109 // give the minimum max size across all configs. So for simplicity we will use that for now.
110 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
111 fMaxTextureSize = properties.limits.maxImageDimension2D;
112
113 this->initSampleCount(properties);
114
egdanield5e3b9e2016-03-08 12:19:54 -0800115 // Assuming since we will always map in the end to upload the data we might as well just map
116 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700117 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800118
119 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
120
121 fStencilWrapOpsSupport = true;
122 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700123 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800124}
125
jvanverthfd7bd452016-03-25 06:29:52 -0700126void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties,
127 uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500128 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdanield5e3b9e2016-03-08 12:19:54 -0800129 glslCaps->fVersionDeclString = "#version 310 es\n";
Greg Daniel164a9f02016-02-22 09:56:40 -0500130
131 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
132 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
133 GrPixelConfig config = static_cast<GrPixelConfig>(i);
134 if (GrPixelConfigIsAlphaOnly(config)) {
135 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
136 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
137 } else {
138 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
139 }
140 }
egdaniel67acb832016-02-26 08:32:20 -0800141
egdanield5e3b9e2016-03-08 12:19:54 -0800142 // Vulkan is based off ES 3.0 so the following should all be supported
143 glslCaps->fUsesPrecisionModifiers = true;
144 glslCaps->fFlatInterpolationSupport = true;
145
146 // GrShaderCaps
147
egdaniel67acb832016-02-26 08:32:20 -0800148 glslCaps->fShaderDerivativeSupport = true;
jvanverthfd7bd452016-03-25 06:29:52 -0700149 glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800150#if 0
151 // For now disabling dual source blending till we get it hooked up in the rest of system
jvanverthfd7bd452016-03-25 06:29:52 -0700152 glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800153#endif
154 glslCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800155
156 glslCaps->fMaxVertexSamplers =
157 glslCaps->fMaxGeometrySamplers =
158 glslCaps->fMaxFragmentSamplers = SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
159 properties.limits.maxPerStageDescriptorSamplers);
160 glslCaps->fMaxCombinedSamplers = SkTMin(properties.limits.maxDescriptorSetSampledImages,
161 properties.limits.maxDescriptorSetSamplers);
Greg Daniel164a9f02016-02-22 09:56:40 -0500162}
163
164static void format_supported_for_feature(const GrVkInterface* interface,
165 VkPhysicalDevice physDev,
166 VkFormat format,
167 VkFormatFeatureFlagBits featureBit,
168 bool* linearSupport,
169 bool* optimalSupport) {
170 VkFormatProperties props;
171 memset(&props, 0, sizeof(VkFormatProperties));
172 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
173 *linearSupport = SkToBool(props.linearTilingFeatures & featureBit);
174 *optimalSupport = SkToBool(props.optimalTilingFeatures & featureBit);
175}
176
177static void config_supported_for_feature(const GrVkInterface* interface,
178 VkPhysicalDevice physDev,
179 GrPixelConfig config,
180 VkFormatFeatureFlagBits featureBit,
181 bool* linearSupport,
182 bool* optimalSupport) {
183 VkFormat format;
184 if (!GrPixelConfigToVkFormat(config, &format)) {
185 *linearSupport = false;
186 *optimalSupport = false;
187 return;
188 }
189 format_supported_for_feature(interface, physDev, format, featureBit,
190 linearSupport, optimalSupport);
191}
192
193// Currently just assumeing if something can be rendered to without MSAA it also works for MSAAA
194#define SET_CONFIG_IS_RENDERABLE(config) \
195 config_supported_for_feature(interface, \
196 physDev, \
197 config, \
198 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT, \
199 &fConfigLinearRenderSupport[config][kNo_MSAA], \
200 &fConfigRenderSupport[config][kNo_MSAA] ); \
201 fConfigRenderSupport[config][kYes_MSAA] = fConfigRenderSupport[config][kNo_MSAA]; \
202 fConfigLinearRenderSupport[config][kYes_MSAA] = fConfigLinearRenderSupport[config][kNo_MSAA];
203
204
205void GrVkCaps::initConfigRenderableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
206 enum {
207 kNo_MSAA = 0,
208 kYes_MSAA = 1,
209 };
210
211 // Base render support
212 SET_CONFIG_IS_RENDERABLE(kAlpha_8_GrPixelConfig);
213 SET_CONFIG_IS_RENDERABLE(kRGB_565_GrPixelConfig);
214 SET_CONFIG_IS_RENDERABLE(kRGBA_4444_GrPixelConfig);
215 SET_CONFIG_IS_RENDERABLE(kRGBA_8888_GrPixelConfig);
216 SET_CONFIG_IS_RENDERABLE(kBGRA_8888_GrPixelConfig);
217
218 SET_CONFIG_IS_RENDERABLE(kSRGBA_8888_GrPixelConfig);
219
220 // Float render support
221 SET_CONFIG_IS_RENDERABLE(kRGBA_float_GrPixelConfig);
222 SET_CONFIG_IS_RENDERABLE(kRGBA_half_GrPixelConfig);
223 SET_CONFIG_IS_RENDERABLE(kAlpha_half_GrPixelConfig);
224}
225
226#define SET_CONFIG_IS_TEXTURABLE(config) \
227 config_supported_for_feature(interface, \
228 physDev, \
229 config, \
230 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, \
231 &fConfigLinearTextureSupport[config], \
232 &fConfigTextureSupport[config]);
233
234void GrVkCaps::initConfigTexturableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
235 // Base texture support
236 SET_CONFIG_IS_TEXTURABLE(kAlpha_8_GrPixelConfig);
237 SET_CONFIG_IS_TEXTURABLE(kRGB_565_GrPixelConfig);
238 SET_CONFIG_IS_TEXTURABLE(kRGBA_4444_GrPixelConfig);
239 SET_CONFIG_IS_TEXTURABLE(kRGBA_8888_GrPixelConfig);
240 SET_CONFIG_IS_TEXTURABLE(kBGRA_8888_GrPixelConfig);
241
242 SET_CONFIG_IS_TEXTURABLE(kIndex_8_GrPixelConfig);
243 SET_CONFIG_IS_TEXTURABLE(kSRGBA_8888_GrPixelConfig);
244
245 // Compressed texture support
246 SET_CONFIG_IS_TEXTURABLE(kETC1_GrPixelConfig);
247 SET_CONFIG_IS_TEXTURABLE(kLATC_GrPixelConfig);
248 SET_CONFIG_IS_TEXTURABLE(kR11_EAC_GrPixelConfig);
249 SET_CONFIG_IS_TEXTURABLE(kASTC_12x12_GrPixelConfig);
250
251 // Float texture support
252 SET_CONFIG_IS_TEXTURABLE(kRGBA_float_GrPixelConfig);
253 SET_CONFIG_IS_TEXTURABLE(kRGBA_half_GrPixelConfig);
254 SET_CONFIG_IS_TEXTURABLE(kAlpha_half_GrPixelConfig);
255}
256
257#define SET_CONFIG_CAN_STENCIL(config) \
258 bool SK_MACRO_APPEND_LINE(linearSupported); \
259 bool SK_MACRO_APPEND_LINE(optimalSupported); \
260 format_supported_for_feature(interface, \
261 physDev, \
262 config.fInternalFormat, \
263 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, \
264 &SK_MACRO_APPEND_LINE(linearSupported), \
265 &SK_MACRO_APPEND_LINE(optimalSupported)); \
266 if (SK_MACRO_APPEND_LINE(linearSupported)) fLinearStencilFormats.push_back(config); \
267 if (SK_MACRO_APPEND_LINE(optimalSupported)) fStencilFormats.push_back(config);
268
269void GrVkCaps::initStencilFormats(const GrVkInterface* interface, VkPhysicalDevice physDev) {
270 // Build up list of legal stencil formats (though perhaps not supported on
271 // the particular gpu/driver) from most preferred to least.
272
273 static const StencilFormat
274 // internal Format stencil bits total bits packed?
275 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
276 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true };
277
278 // I'm simply assuming that these two will be supported since they are used in example code.
279 // TODO: Actaully figure this out
280 SET_CONFIG_CAN_STENCIL(gS8);
281 SET_CONFIG_CAN_STENCIL(gD24S8);
282}
283