blob: ca9cf359acd98ed85a59c77dad79afd821cc224f [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
42 /**************************************************************************
43 * GrVkCaps fields
44 **************************************************************************/
45 fMaxSampledTextures = 16; // Spec requires a minimum of 16 sampled textures per stage
46
47 this->init(contextOptions, vkInterface, physDev);
48}
49
50void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
51 VkPhysicalDevice physDev) {
52
egdanield5e3b9e2016-03-08 12:19:54 -080053 VkPhysicalDeviceProperties properties;
54 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
55
56 VkPhysicalDeviceFeatures features;
57 GR_VK_CALL(vkInterface, GetPhysicalDeviceFeatures(physDev, &features));
58
59 VkPhysicalDeviceMemoryProperties memoryProperties;
60 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
61
62 this->initGrCaps(properties, features, memoryProperties);
63 this->initGLSLCaps(features);
Greg Daniel164a9f02016-02-22 09:56:40 -050064 this->initConfigTexturableTable(vkInterface, physDev);
65 this->initConfigRenderableTable(vkInterface, physDev);
66 this->initStencilFormats(vkInterface, physDev);
67
Greg Daniel164a9f02016-02-22 09:56:40 -050068
Greg Daniel164a9f02016-02-22 09:56:40 -050069
70 this->applyOptionsOverrides(contextOptions);
71 // need to friend GrVkCaps in GrGLSLCaps.h
72 // GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
73 // glslCaps->applyOptionsOverrides(contextOptions);
74}
75
76int get_max_sample_count(VkSampleCountFlags flags) {
77 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
78 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
79 return 0;
80 }
81 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
82 return 2;
83 }
84 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
85 return 4;
86 }
87 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
88 return 8;
89 }
90 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
91 return 16;
92 }
93 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
94 return 32;
95 }
96 return 64;
97}
98
99void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
100 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
101 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
102
103 fMaxColorSampleCount = get_max_sample_count(colorSamples);
104 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
105}
106
egdanield5e3b9e2016-03-08 12:19:54 -0800107void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
108 const VkPhysicalDeviceFeatures& features,
109 const VkPhysicalDeviceMemoryProperties& memoryProperites) {
110 // 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
117 fMaxSampledTextures = SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
118 properties.limits.maxPerStageDescriptorSamplers);
119
120 // Assuming since we will always map in the end to upload the data we might as well just map
121 // from the get go. There is no hard data to suggest this is faster or slower.
122 fGeometryBufferMapThreshold = 0;
123
124 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
125
126 fStencilWrapOpsSupport = true;
127 fOversizedStencilSupport = true;
128}
129
130void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceFeatures& features) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500131 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdanield5e3b9e2016-03-08 12:19:54 -0800132 glslCaps->fVersionDeclString = "#version 310 es\n";
Greg Daniel164a9f02016-02-22 09:56:40 -0500133
134 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
135 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
136 GrPixelConfig config = static_cast<GrPixelConfig>(i);
137 if (GrPixelConfigIsAlphaOnly(config)) {
138 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
139 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
140 } else {
141 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
142 }
143 }
egdaniel67acb832016-02-26 08:32:20 -0800144
egdanield5e3b9e2016-03-08 12:19:54 -0800145 // Vulkan is based off ES 3.0 so the following should all be supported
146 glslCaps->fUsesPrecisionModifiers = true;
147 glslCaps->fFlatInterpolationSupport = true;
148
149 // GrShaderCaps
150
egdaniel67acb832016-02-26 08:32:20 -0800151 glslCaps->fShaderDerivativeSupport = true;
egdanield5e3b9e2016-03-08 12:19:54 -0800152 glslCaps->fGeometryShaderSupport = features.geometryShader == VK_TRUE;
153#if 0
154 // For now disabling dual source blending till we get it hooked up in the rest of system
155 glslCaps->fDualSourceBlendingSupport = features.dualSrcBlend;
156#endif
157 glslCaps->fIntegerSupport = true;
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