blob: 6d1d844525b18e37d0666376be40eeb24e2e31a6 [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,
jvanverthfd7bd452016-03-25 06:29:52 -070016 VkPhysicalDevice physDev, uint32_t featureFlags) : INHERITED(contextOptions) {
Greg Daniel164a9f02016-02-22 09:56:40 -050017 /**************************************************************************
18 * GrDrawTargetCaps fields
19 **************************************************************************/
20 fMipMapSupport = false; //TODO: figure this out
21 fNPOTTextureTileSupport = false; //TODO: figure this out
22 fTwoSidedStencilSupport = false; //TODO: figure this out
23 fStencilWrapOpsSupport = false; //TODO: figure this out
24 fDiscardRenderTargetSupport = false; //TODO: figure this out
25 fReuseScratchTextures = true; //TODO: figure this out
26 fGpuTracingSupport = false; //TODO: figure this out
27 fCompressedTexSubImageSupport = false; //TODO: figure this out
28 fOversizedStencilSupport = false; //TODO: figure this out
29
30 fUseDrawInsteadOfClear = false; //TODO: figure this out
31
32 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070033 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050034
35 fMaxRenderTargetSize = 4096; // minimum required by spec
36 fMaxTextureSize = 4096; // minimum required by spec
37 fMaxColorSampleCount = 4; // minimum required by spec
38 fMaxStencilSampleCount = 4; // minimum required by spec
39
40
41 fShaderCaps.reset(new GrGLSLCaps(contextOptions));
42
jvanverthfd7bd452016-03-25 06:29:52 -070043 this->init(contextOptions, vkInterface, physDev, featureFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050044}
45
46void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
jvanverthfd7bd452016-03-25 06:29:52 -070047 VkPhysicalDevice physDev, uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050048
egdanield5e3b9e2016-03-08 12:19:54 -080049 VkPhysicalDeviceProperties properties;
50 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
51
egdanield5e3b9e2016-03-08 12:19:54 -080052 VkPhysicalDeviceMemoryProperties memoryProperties;
53 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
54
jvanverthfd7bd452016-03-25 06:29:52 -070055 this->initGrCaps(properties, memoryProperties, featureFlags);
56 this->initGLSLCaps(properties, featureFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050057 this->initConfigTexturableTable(vkInterface, physDev);
58 this->initConfigRenderableTable(vkInterface, physDev);
59 this->initStencilFormats(vkInterface, physDev);
60
Greg Daniel164a9f02016-02-22 09:56:40 -050061
Greg Daniel164a9f02016-02-22 09:56:40 -050062
63 this->applyOptionsOverrides(contextOptions);
64 // need to friend GrVkCaps in GrGLSLCaps.h
65 // GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
66 // glslCaps->applyOptionsOverrides(contextOptions);
67}
68
69int get_max_sample_count(VkSampleCountFlags flags) {
70 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
71 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
72 return 0;
73 }
74 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
75 return 2;
76 }
77 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
78 return 4;
79 }
80 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
81 return 8;
82 }
83 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
84 return 16;
85 }
86 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
87 return 32;
88 }
89 return 64;
90}
91
92void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
93 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
94 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
95
96 fMaxColorSampleCount = get_max_sample_count(colorSamples);
97 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
98}
99
egdanield5e3b9e2016-03-08 12:19:54 -0800100void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700101 const VkPhysicalDeviceMemoryProperties& memoryProperties,
102 uint32_t featureFlags) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700103 fMaxVertexAttributes = properties.limits.maxVertexInputAttributes;
egdanield5e3b9e2016-03-08 12:19:54 -0800104 // We could actually query and get a max size for each config, however maxImageDimension2D will
105 // give the minimum max size across all configs. So for simplicity we will use that for now.
106 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
107 fMaxTextureSize = properties.limits.maxImageDimension2D;
108
109 this->initSampleCount(properties);
110
egdanield5e3b9e2016-03-08 12:19:54 -0800111 // Assuming since we will always map in the end to upload the data we might as well just map
112 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700113 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800114
115 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
116
117 fStencilWrapOpsSupport = true;
118 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700119 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800120}
121
jvanverthfd7bd452016-03-25 06:29:52 -0700122void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties,
123 uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500124 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdanield5e3b9e2016-03-08 12:19:54 -0800125 glslCaps->fVersionDeclString = "#version 310 es\n";
Greg Daniel164a9f02016-02-22 09:56:40 -0500126
127 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
128 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
129 GrPixelConfig config = static_cast<GrPixelConfig>(i);
130 if (GrPixelConfigIsAlphaOnly(config)) {
131 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
132 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
133 } else {
134 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
135 }
136 }
egdaniel67acb832016-02-26 08:32:20 -0800137
egdanield5e3b9e2016-03-08 12:19:54 -0800138 // Vulkan is based off ES 3.0 so the following should all be supported
139 glslCaps->fUsesPrecisionModifiers = true;
140 glslCaps->fFlatInterpolationSupport = true;
141
142 // GrShaderCaps
143
egdaniel67acb832016-02-26 08:32:20 -0800144 glslCaps->fShaderDerivativeSupport = true;
jvanverthfd7bd452016-03-25 06:29:52 -0700145 glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800146#if 0
147 // For now disabling dual source blending till we get it hooked up in the rest of system
jvanverthfd7bd452016-03-25 06:29:52 -0700148 glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800149#endif
150 glslCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800151
152 glslCaps->fMaxVertexSamplers =
153 glslCaps->fMaxGeometrySamplers =
154 glslCaps->fMaxFragmentSamplers = SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
155 properties.limits.maxPerStageDescriptorSamplers);
156 glslCaps->fMaxCombinedSamplers = SkTMin(properties.limits.maxDescriptorSetSampledImages,
157 properties.limits.maxDescriptorSetSamplers);
Greg Daniel164a9f02016-02-22 09:56:40 -0500158}
159
160static void format_supported_for_feature(const GrVkInterface* interface,
161 VkPhysicalDevice physDev,
162 VkFormat format,
163 VkFormatFeatureFlagBits featureBit,
164 bool* linearSupport,
165 bool* optimalSupport) {
166 VkFormatProperties props;
167 memset(&props, 0, sizeof(VkFormatProperties));
168 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
169 *linearSupport = SkToBool(props.linearTilingFeatures & featureBit);
170 *optimalSupport = SkToBool(props.optimalTilingFeatures & featureBit);
171}
172
173static void config_supported_for_feature(const GrVkInterface* interface,
174 VkPhysicalDevice physDev,
175 GrPixelConfig config,
176 VkFormatFeatureFlagBits featureBit,
177 bool* linearSupport,
178 bool* optimalSupport) {
179 VkFormat format;
180 if (!GrPixelConfigToVkFormat(config, &format)) {
181 *linearSupport = false;
182 *optimalSupport = false;
183 return;
184 }
185 format_supported_for_feature(interface, physDev, format, featureBit,
186 linearSupport, optimalSupport);
187}
188
189// Currently just assumeing if something can be rendered to without MSAA it also works for MSAAA
190#define SET_CONFIG_IS_RENDERABLE(config) \
191 config_supported_for_feature(interface, \
192 physDev, \
193 config, \
194 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT, \
195 &fConfigLinearRenderSupport[config][kNo_MSAA], \
196 &fConfigRenderSupport[config][kNo_MSAA] ); \
197 fConfigRenderSupport[config][kYes_MSAA] = fConfigRenderSupport[config][kNo_MSAA]; \
198 fConfigLinearRenderSupport[config][kYes_MSAA] = fConfigLinearRenderSupport[config][kNo_MSAA];
199
200
201void GrVkCaps::initConfigRenderableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
202 enum {
203 kNo_MSAA = 0,
204 kYes_MSAA = 1,
205 };
206
207 // Base render support
208 SET_CONFIG_IS_RENDERABLE(kAlpha_8_GrPixelConfig);
209 SET_CONFIG_IS_RENDERABLE(kRGB_565_GrPixelConfig);
210 SET_CONFIG_IS_RENDERABLE(kRGBA_4444_GrPixelConfig);
211 SET_CONFIG_IS_RENDERABLE(kRGBA_8888_GrPixelConfig);
212 SET_CONFIG_IS_RENDERABLE(kBGRA_8888_GrPixelConfig);
213
214 SET_CONFIG_IS_RENDERABLE(kSRGBA_8888_GrPixelConfig);
215
216 // Float render support
217 SET_CONFIG_IS_RENDERABLE(kRGBA_float_GrPixelConfig);
218 SET_CONFIG_IS_RENDERABLE(kRGBA_half_GrPixelConfig);
219 SET_CONFIG_IS_RENDERABLE(kAlpha_half_GrPixelConfig);
220}
221
222#define SET_CONFIG_IS_TEXTURABLE(config) \
223 config_supported_for_feature(interface, \
224 physDev, \
225 config, \
226 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, \
227 &fConfigLinearTextureSupport[config], \
228 &fConfigTextureSupport[config]);
229
230void GrVkCaps::initConfigTexturableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
231 // Base texture support
232 SET_CONFIG_IS_TEXTURABLE(kAlpha_8_GrPixelConfig);
233 SET_CONFIG_IS_TEXTURABLE(kRGB_565_GrPixelConfig);
234 SET_CONFIG_IS_TEXTURABLE(kRGBA_4444_GrPixelConfig);
235 SET_CONFIG_IS_TEXTURABLE(kRGBA_8888_GrPixelConfig);
236 SET_CONFIG_IS_TEXTURABLE(kBGRA_8888_GrPixelConfig);
237
238 SET_CONFIG_IS_TEXTURABLE(kIndex_8_GrPixelConfig);
239 SET_CONFIG_IS_TEXTURABLE(kSRGBA_8888_GrPixelConfig);
240
241 // Compressed texture support
242 SET_CONFIG_IS_TEXTURABLE(kETC1_GrPixelConfig);
243 SET_CONFIG_IS_TEXTURABLE(kLATC_GrPixelConfig);
244 SET_CONFIG_IS_TEXTURABLE(kR11_EAC_GrPixelConfig);
245 SET_CONFIG_IS_TEXTURABLE(kASTC_12x12_GrPixelConfig);
246
247 // Float texture support
248 SET_CONFIG_IS_TEXTURABLE(kRGBA_float_GrPixelConfig);
249 SET_CONFIG_IS_TEXTURABLE(kRGBA_half_GrPixelConfig);
250 SET_CONFIG_IS_TEXTURABLE(kAlpha_half_GrPixelConfig);
251}
252
253#define SET_CONFIG_CAN_STENCIL(config) \
254 bool SK_MACRO_APPEND_LINE(linearSupported); \
255 bool SK_MACRO_APPEND_LINE(optimalSupported); \
256 format_supported_for_feature(interface, \
257 physDev, \
258 config.fInternalFormat, \
259 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, \
260 &SK_MACRO_APPEND_LINE(linearSupported), \
261 &SK_MACRO_APPEND_LINE(optimalSupported)); \
262 if (SK_MACRO_APPEND_LINE(linearSupported)) fLinearStencilFormats.push_back(config); \
263 if (SK_MACRO_APPEND_LINE(optimalSupported)) fStencilFormats.push_back(config);
264
265void GrVkCaps::initStencilFormats(const GrVkInterface* interface, VkPhysicalDevice physDev) {
266 // Build up list of legal stencil formats (though perhaps not supported on
267 // the particular gpu/driver) from most preferred to least.
268
269 static const StencilFormat
270 // internal Format stencil bits total bits packed?
271 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
272 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true };
273
274 // I'm simply assuming that these two will be supported since they are used in example code.
275 // TODO: Actaully figure this out
276 SET_CONFIG_CAN_STENCIL(gS8);
277 SET_CONFIG_CAN_STENCIL(gD24S8);
278}
279