blob: d8ac8757e5c378315dd14b45f58a764634e47c2c [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
53 this->initGLSLCaps(vkInterface, physDev);
54 this->initConfigTexturableTable(vkInterface, physDev);
55 this->initConfigRenderableTable(vkInterface, physDev);
56 this->initStencilFormats(vkInterface, physDev);
57
58 VkPhysicalDeviceProperties properties;
59 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
60
61 // We could actually querey and get a max size for each config, however maxImageDimension2D will
62 // give the minimum max size across all configs. So for simplicity we will use that for now.
63 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
64 fMaxTextureSize = properties.limits.maxImageDimension2D;
65
66 this->initSampleCount(properties);
67
68 fMaxSampledTextures = SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
69 properties.limits.maxPerStageDescriptorSamplers);
70
71 this->applyOptionsOverrides(contextOptions);
72 // need to friend GrVkCaps in GrGLSLCaps.h
73 // GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
74 // glslCaps->applyOptionsOverrides(contextOptions);
75}
76
77int get_max_sample_count(VkSampleCountFlags flags) {
78 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
79 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
80 return 0;
81 }
82 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
83 return 2;
84 }
85 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
86 return 4;
87 }
88 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
89 return 8;
90 }
91 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
92 return 16;
93 }
94 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
95 return 32;
96 }
97 return 64;
98}
99
100void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
101 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
102 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
103
104 fMaxColorSampleCount = get_max_sample_count(colorSamples);
105 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
106}
107
108void GrVkCaps::initGLSLCaps(const GrVkInterface* interface, VkPhysicalDevice physDev) {
109 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
110 // TODO: actually figure out a correct version here
111 glslCaps->fVersionDeclString = "#version 140\n";
112
113 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
114 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
115 GrPixelConfig config = static_cast<GrPixelConfig>(i);
116 if (GrPixelConfigIsAlphaOnly(config)) {
117 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
118 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
119 } else {
120 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
121 }
122 }
egdaniel67acb832016-02-26 08:32:20 -0800123
124 glslCaps->fShaderDerivativeSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500125}
126
127static void format_supported_for_feature(const GrVkInterface* interface,
128 VkPhysicalDevice physDev,
129 VkFormat format,
130 VkFormatFeatureFlagBits featureBit,
131 bool* linearSupport,
132 bool* optimalSupport) {
133 VkFormatProperties props;
134 memset(&props, 0, sizeof(VkFormatProperties));
135 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
136 *linearSupport = SkToBool(props.linearTilingFeatures & featureBit);
137 *optimalSupport = SkToBool(props.optimalTilingFeatures & featureBit);
138}
139
140static void config_supported_for_feature(const GrVkInterface* interface,
141 VkPhysicalDevice physDev,
142 GrPixelConfig config,
143 VkFormatFeatureFlagBits featureBit,
144 bool* linearSupport,
145 bool* optimalSupport) {
146 VkFormat format;
147 if (!GrPixelConfigToVkFormat(config, &format)) {
148 *linearSupport = false;
149 *optimalSupport = false;
150 return;
151 }
152 format_supported_for_feature(interface, physDev, format, featureBit,
153 linearSupport, optimalSupport);
154}
155
156// Currently just assumeing if something can be rendered to without MSAA it also works for MSAAA
157#define SET_CONFIG_IS_RENDERABLE(config) \
158 config_supported_for_feature(interface, \
159 physDev, \
160 config, \
161 VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT, \
162 &fConfigLinearRenderSupport[config][kNo_MSAA], \
163 &fConfigRenderSupport[config][kNo_MSAA] ); \
164 fConfigRenderSupport[config][kYes_MSAA] = fConfigRenderSupport[config][kNo_MSAA]; \
165 fConfigLinearRenderSupport[config][kYes_MSAA] = fConfigLinearRenderSupport[config][kNo_MSAA];
166
167
168void GrVkCaps::initConfigRenderableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
169 enum {
170 kNo_MSAA = 0,
171 kYes_MSAA = 1,
172 };
173
174 // Base render support
175 SET_CONFIG_IS_RENDERABLE(kAlpha_8_GrPixelConfig);
176 SET_CONFIG_IS_RENDERABLE(kRGB_565_GrPixelConfig);
177 SET_CONFIG_IS_RENDERABLE(kRGBA_4444_GrPixelConfig);
178 SET_CONFIG_IS_RENDERABLE(kRGBA_8888_GrPixelConfig);
179 SET_CONFIG_IS_RENDERABLE(kBGRA_8888_GrPixelConfig);
180
181 SET_CONFIG_IS_RENDERABLE(kSRGBA_8888_GrPixelConfig);
182
183 // Float render support
184 SET_CONFIG_IS_RENDERABLE(kRGBA_float_GrPixelConfig);
185 SET_CONFIG_IS_RENDERABLE(kRGBA_half_GrPixelConfig);
186 SET_CONFIG_IS_RENDERABLE(kAlpha_half_GrPixelConfig);
187}
188
189#define SET_CONFIG_IS_TEXTURABLE(config) \
190 config_supported_for_feature(interface, \
191 physDev, \
192 config, \
193 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, \
194 &fConfigLinearTextureSupport[config], \
195 &fConfigTextureSupport[config]);
196
197void GrVkCaps::initConfigTexturableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
198 // Base texture support
199 SET_CONFIG_IS_TEXTURABLE(kAlpha_8_GrPixelConfig);
200 SET_CONFIG_IS_TEXTURABLE(kRGB_565_GrPixelConfig);
201 SET_CONFIG_IS_TEXTURABLE(kRGBA_4444_GrPixelConfig);
202 SET_CONFIG_IS_TEXTURABLE(kRGBA_8888_GrPixelConfig);
203 SET_CONFIG_IS_TEXTURABLE(kBGRA_8888_GrPixelConfig);
204
205 SET_CONFIG_IS_TEXTURABLE(kIndex_8_GrPixelConfig);
206 SET_CONFIG_IS_TEXTURABLE(kSRGBA_8888_GrPixelConfig);
207
208 // Compressed texture support
209 SET_CONFIG_IS_TEXTURABLE(kETC1_GrPixelConfig);
210 SET_CONFIG_IS_TEXTURABLE(kLATC_GrPixelConfig);
211 SET_CONFIG_IS_TEXTURABLE(kR11_EAC_GrPixelConfig);
212 SET_CONFIG_IS_TEXTURABLE(kASTC_12x12_GrPixelConfig);
213
214 // Float texture support
215 SET_CONFIG_IS_TEXTURABLE(kRGBA_float_GrPixelConfig);
216 SET_CONFIG_IS_TEXTURABLE(kRGBA_half_GrPixelConfig);
217 SET_CONFIG_IS_TEXTURABLE(kAlpha_half_GrPixelConfig);
218}
219
220#define SET_CONFIG_CAN_STENCIL(config) \
221 bool SK_MACRO_APPEND_LINE(linearSupported); \
222 bool SK_MACRO_APPEND_LINE(optimalSupported); \
223 format_supported_for_feature(interface, \
224 physDev, \
225 config.fInternalFormat, \
226 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, \
227 &SK_MACRO_APPEND_LINE(linearSupported), \
228 &SK_MACRO_APPEND_LINE(optimalSupported)); \
229 if (SK_MACRO_APPEND_LINE(linearSupported)) fLinearStencilFormats.push_back(config); \
230 if (SK_MACRO_APPEND_LINE(optimalSupported)) fStencilFormats.push_back(config);
231
232void GrVkCaps::initStencilFormats(const GrVkInterface* interface, VkPhysicalDevice physDev) {
233 // Build up list of legal stencil formats (though perhaps not supported on
234 // the particular gpu/driver) from most preferred to least.
235
236 static const StencilFormat
237 // internal Format stencil bits total bits packed?
238 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
239 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true };
240
241 // I'm simply assuming that these two will be supported since they are used in example code.
242 // TODO: Actaully figure this out
243 SET_CONFIG_CAN_STENCIL(gS8);
244 SET_CONFIG_CAN_STENCIL(gD24S8);
245}
246