blob: 9dbc40ce7fe5423f634ccc74bc1387cd6bc95ab5 [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"
13
14GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
15 VkPhysicalDevice physDev) : INHERITED(contextOptions) {
16 /**************************************************************************
17 * GrDrawTargetCaps fields
18 **************************************************************************/
19 fMipMapSupport = false; //TODO: figure this out
20 fNPOTTextureTileSupport = false; //TODO: figure this out
21 fTwoSidedStencilSupport = false; //TODO: figure this out
22 fStencilWrapOpsSupport = false; //TODO: figure this out
23 fDiscardRenderTargetSupport = false; //TODO: figure this out
24 fReuseScratchTextures = true; //TODO: figure this out
25 fGpuTracingSupport = false; //TODO: figure this out
26 fCompressedTexSubImageSupport = false; //TODO: figure this out
27 fOversizedStencilSupport = false; //TODO: figure this out
28
29 fUseDrawInsteadOfClear = false; //TODO: figure this out
30
31 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
32 fGeometryBufferMapThreshold = SK_MaxS32; //TODO: figure this out
33
34 fMaxRenderTargetSize = 4096; // minimum required by spec
35 fMaxTextureSize = 4096; // minimum required by spec
36 fMaxColorSampleCount = 4; // minimum required by spec
37 fMaxStencilSampleCount = 4; // minimum required by spec
38
39
40 fShaderCaps.reset(new GrGLSLCaps(contextOptions));
41
Greg Daniel164a9f02016-02-22 09:56:40 -050042 this->init(contextOptions, vkInterface, physDev);
43}
44
45void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
46 VkPhysicalDevice physDev) {
47
egdanield5e3b9e2016-03-08 12:19:54 -080048 VkPhysicalDeviceProperties properties;
49 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
50
51 VkPhysicalDeviceFeatures features;
52 GR_VK_CALL(vkInterface, GetPhysicalDeviceFeatures(physDev, &features));
53
54 VkPhysicalDeviceMemoryProperties memoryProperties;
55 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
56
57 this->initGrCaps(properties, features, memoryProperties);
cdalton9c3f1432016-03-11 10:07:37 -080058 this->initGLSLCaps(features, properties);
Greg Daniel164a9f02016-02-22 09:56:40 -050059 this->initConfigTexturableTable(vkInterface, physDev);
60 this->initConfigRenderableTable(vkInterface, physDev);
61 this->initStencilFormats(vkInterface, physDev);
62
Greg Daniel164a9f02016-02-22 09:56:40 -050063
Greg Daniel164a9f02016-02-22 09:56:40 -050064
65 this->applyOptionsOverrides(contextOptions);
66 // need to friend GrVkCaps in GrGLSLCaps.h
67 // GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
68 // glslCaps->applyOptionsOverrides(contextOptions);
69}
70
71int get_max_sample_count(VkSampleCountFlags flags) {
72 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
73 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
74 return 0;
75 }
76 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
77 return 2;
78 }
79 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
80 return 4;
81 }
82 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
83 return 8;
84 }
85 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
86 return 16;
87 }
88 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
89 return 32;
90 }
91 return 64;
92}
93
94void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
95 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
96 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
97
98 fMaxColorSampleCount = get_max_sample_count(colorSamples);
99 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
100}
101
egdanield5e3b9e2016-03-08 12:19:54 -0800102void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
103 const VkPhysicalDeviceFeatures& features,
104 const VkPhysicalDeviceMemoryProperties& memoryProperites) {
105 // We could actually query and get a max size for each config, however maxImageDimension2D will
106 // give the minimum max size across all configs. So for simplicity we will use that for now.
107 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
108 fMaxTextureSize = properties.limits.maxImageDimension2D;
109
110 this->initSampleCount(properties);
111
egdanield5e3b9e2016-03-08 12:19:54 -0800112 // Assuming since we will always map in the end to upload the data we might as well just map
113 // from the get go. There is no hard data to suggest this is faster or slower.
114 fGeometryBufferMapThreshold = 0;
115
116 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
117
118 fStencilWrapOpsSupport = true;
119 fOversizedStencilSupport = true;
120}
121
cdalton9c3f1432016-03-11 10:07:37 -0800122void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceFeatures& features,
123 const VkPhysicalDeviceProperties& properties) {
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;
egdanield5e3b9e2016-03-08 12:19:54 -0800145 glslCaps->fGeometryShaderSupport = features.geometryShader == VK_TRUE;
146#if 0
147 // For now disabling dual source blending till we get it hooked up in the rest of system
148 glslCaps->fDualSourceBlendingSupport = features.dualSrcBlend;
149#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