blob: 7a1577e66c322e348993e97cade21b3202ebbf92 [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);
egdaniel8f1dcaa2016-04-01 10:10:45 -070060 this->initConfigTable(vkInterface, physDev);
61 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050062
egdanielc5ec1402016-03-28 12:14:42 -070063 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070064 // Currently disabling this feature since it does not play well with validation layers which
65 // expect a SPIR-V shader
66 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070067 }
Greg Daniel164a9f02016-02-22 09:56:40 -050068
69 this->applyOptionsOverrides(contextOptions);
egdanielc5ec1402016-03-28 12:14:42 -070070 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
71 glslCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050072}
73
74int get_max_sample_count(VkSampleCountFlags flags) {
75 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
76 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
77 return 0;
78 }
79 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
80 return 2;
81 }
82 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
83 return 4;
84 }
85 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
86 return 8;
87 }
88 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
89 return 16;
90 }
91 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
92 return 32;
93 }
94 return 64;
95}
96
97void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
98 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
99 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
100
101 fMaxColorSampleCount = get_max_sample_count(colorSamples);
102 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
103}
104
egdanield5e3b9e2016-03-08 12:19:54 -0800105void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700106 const VkPhysicalDeviceMemoryProperties& memoryProperties,
107 uint32_t featureFlags) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700108 fMaxVertexAttributes = properties.limits.maxVertexInputAttributes;
egdanield5e3b9e2016-03-08 12:19:54 -0800109 // We could actually query and get a max size for each config, however maxImageDimension2D will
110 // give the minimum max size across all configs. So for simplicity we will use that for now.
111 fMaxRenderTargetSize = properties.limits.maxImageDimension2D;
112 fMaxTextureSize = properties.limits.maxImageDimension2D;
113
114 this->initSampleCount(properties);
115
egdanield5e3b9e2016-03-08 12:19:54 -0800116 // Assuming since we will always map in the end to upload the data we might as well just map
117 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700118 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800119
120 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
121
122 fStencilWrapOpsSupport = true;
123 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700124 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800125}
126
jvanverthfd7bd452016-03-25 06:29:52 -0700127void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties,
128 uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500129 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdanield5e3b9e2016-03-08 12:19:54 -0800130 glslCaps->fVersionDeclString = "#version 310 es\n";
Greg Daniel164a9f02016-02-22 09:56:40 -0500131
132 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
133 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
134 GrPixelConfig config = static_cast<GrPixelConfig>(i);
135 if (GrPixelConfigIsAlphaOnly(config)) {
136 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
137 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
138 } else {
139 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
140 }
141 }
egdaniel67acb832016-02-26 08:32:20 -0800142
egdanield5e3b9e2016-03-08 12:19:54 -0800143 // Vulkan is based off ES 3.0 so the following should all be supported
144 glslCaps->fUsesPrecisionModifiers = true;
145 glslCaps->fFlatInterpolationSupport = true;
146
147 // GrShaderCaps
148
egdaniel67acb832016-02-26 08:32:20 -0800149 glslCaps->fShaderDerivativeSupport = true;
jvanverthfd7bd452016-03-25 06:29:52 -0700150 glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700151
jvanverthfd7bd452016-03-25 06:29:52 -0700152 glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700153
egdanield5e3b9e2016-03-08 12:19:54 -0800154 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
egdaniel8f1dcaa2016-04-01 10:10:45 -0700164bool stencil_format_supported(const GrVkInterface* interface,
165 VkPhysicalDevice physDev,
166 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500167 VkFormatProperties props;
168 memset(&props, 0, sizeof(VkFormatProperties));
169 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700170 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500171}
172
egdaniel8f1dcaa2016-04-01 10:10:45 -0700173void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
174 // List of legal stencil formats (though perhaps not supported on
175 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
176 // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D24_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
177 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500178 static const StencilFormat
179 // internal Format stencil bits total bits packed?
180 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700181 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
182 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500183
egdaniel8f1dcaa2016-04-01 10:10:45 -0700184 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
185 fPreferedStencilFormat = gS8;
186 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
187 fPreferedStencilFormat = gD24S8;
188 } else {
189 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
190 fPreferedStencilFormat = gD32S8;
191 }
192}
193
194void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
195 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
196 VkFormat format;
197 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
198 fConfigTable[i].init(interface, physDev, format);
199 }
200 }
201}
202
203void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
204 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
205 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
206 *flags = *flags | kTextureable_Flag;
207 }
208
209 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
210 *flags = *flags | kRenderable_Flag;
211 }
212
213 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
214 *flags = *flags | kBlitSrc_Flag;
215 }
216
217 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
218 *flags = *flags | kBlitDst_Flag;
219 }
220}
221
222void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
223 VkPhysicalDevice physDev,
224 VkFormat format) {
225 VkFormatProperties props;
226 memset(&props, 0, sizeof(VkFormatProperties));
227 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
228 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
229 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -0500230}