blob: be25c889e3827ea78e704e5a102b5bd370c0feae [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;
egdaniel6fa0a912016-09-12 11:51:29 -070019 fMustDoCopiesFromOrigin = false;
egdanielbe9d8212016-09-20 08:54:23 -070020 fAllowInitializationErrorOnTearDown = false;
egdanielc5ec1402016-03-28 12:14:42 -070021
Greg Daniel164a9f02016-02-22 09:56:40 -050022 /**************************************************************************
23 * GrDrawTargetCaps fields
24 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070025 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070026 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070027 fNPOTTextureTileSupport = true; // always available in Vulkan
jvanverthc8ef0532016-05-03 10:45:48 -070028 fTwoSidedStencilSupport = true; // always available in Vulkan
egdaniel50134cc2016-05-23 12:34:38 -070029 fStencilWrapOpsSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070030 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050031 fReuseScratchTextures = true; //TODO: figure this out
32 fGpuTracingSupport = false; //TODO: figure this out
33 fCompressedTexSubImageSupport = false; //TODO: figure this out
34 fOversizedStencilSupport = false; //TODO: figure this out
35
egdaniel9cb63402016-06-23 08:37:05 -070036 fUseDrawInsteadOfClear = false;
Greg Daniel164a9f02016-02-22 09:56:40 -050037
38 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070039 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050040
41 fMaxRenderTargetSize = 4096; // minimum required by spec
42 fMaxTextureSize = 4096; // minimum required by spec
43 fMaxColorSampleCount = 4; // minimum required by spec
44 fMaxStencilSampleCount = 4; // minimum required by spec
45
Greg Daniel164a9f02016-02-22 09:56:40 -050046 fShaderCaps.reset(new GrGLSLCaps(contextOptions));
47
egdanielc5ec1402016-03-28 12:14:42 -070048 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050049}
50
51void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070052 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050053
egdanield5e3b9e2016-03-08 12:19:54 -080054 VkPhysicalDeviceProperties properties;
55 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
56
egdanield5e3b9e2016-03-08 12:19:54 -080057 VkPhysicalDeviceMemoryProperties memoryProperties;
58 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
59
jvanverthfd7bd452016-03-25 06:29:52 -070060 this->initGrCaps(properties, memoryProperties, featureFlags);
61 this->initGLSLCaps(properties, featureFlags);
egdaniel8f1dcaa2016-04-01 10:10:45 -070062 this->initConfigTable(vkInterface, physDev);
63 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050064
egdanielc5ec1402016-03-28 12:14:42 -070065 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070066 // Currently disabling this feature since it does not play well with validation layers which
67 // expect a SPIR-V shader
68 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070069 }
Greg Daniel164a9f02016-02-22 09:56:40 -050070
egdaniel6fa0a912016-09-12 11:51:29 -070071 if (kQualcomm_VkVendor == properties.vendorID) {
72 fMustDoCopiesFromOrigin = true;
egdanielbe9d8212016-09-20 08:54:23 -070073 fAllowInitializationErrorOnTearDown = true;
egdaniel6fa0a912016-09-12 11:51:29 -070074 }
75
Greg Daniel164a9f02016-02-22 09:56:40 -050076 this->applyOptionsOverrides(contextOptions);
egdanielc5ec1402016-03-28 12:14:42 -070077 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
78 glslCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050079}
80
81int get_max_sample_count(VkSampleCountFlags flags) {
82 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
83 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
84 return 0;
85 }
86 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
87 return 2;
88 }
89 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
90 return 4;
91 }
92 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
93 return 8;
94 }
95 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
96 return 16;
97 }
98 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
99 return 32;
100 }
101 return 64;
102}
103
104void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
105 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
106 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
107
108 fMaxColorSampleCount = get_max_sample_count(colorSamples);
109 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
110}
111
egdanield5e3b9e2016-03-08 12:19:54 -0800112void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700113 const VkPhysicalDeviceMemoryProperties& memoryProperties,
114 uint32_t featureFlags) {
jvanverthe78d4872016-09-27 03:33:05 -0700115 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800116 // We could actually query and get a max size for each config, however maxImageDimension2D will
117 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700118 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
119 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800120
121 this->initSampleCount(properties);
122
egdanield5e3b9e2016-03-08 12:19:54 -0800123 // Assuming since we will always map in the end to upload the data we might as well just map
124 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700125 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800126
127 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
128
129 fStencilWrapOpsSupport = true;
130 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700131 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800132}
133
jvanverthfd7bd452016-03-25 06:29:52 -0700134void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties,
135 uint32_t featureFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500136 GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get());
egdaniel3a15fd42016-04-05 11:00:29 -0700137 glslCaps->fVersionDeclString = "#version 330\n";
138
Greg Daniel164a9f02016-02-22 09:56:40 -0500139
140 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
141 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
142 GrPixelConfig config = static_cast<GrPixelConfig>(i);
143 if (GrPixelConfigIsAlphaOnly(config)) {
144 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
145 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
146 } else {
egdaniel3fe03272016-08-15 10:59:17 -0700147 if (kRGBA_4444_GrPixelConfig == config) {
148 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
149 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
150 // or writing to outputs. Since we're not actually changing the data at all, the
151 // only extra work is the swizzle in the shader for all operations.
152 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
153 glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
154 } else {
155 glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
156 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500157 }
158 }
egdaniel67acb832016-02-26 08:32:20 -0800159
egdanield5e3b9e2016-03-08 12:19:54 -0800160 // Vulkan is based off ES 3.0 so the following should all be supported
161 glslCaps->fUsesPrecisionModifiers = true;
162 glslCaps->fFlatInterpolationSupport = true;
163
164 // GrShaderCaps
165
egdaniel67acb832016-02-26 08:32:20 -0800166 glslCaps->fShaderDerivativeSupport = true;
jvanverthfd7bd452016-03-25 06:29:52 -0700167 glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700168
jvanverthfd7bd452016-03-25 06:29:52 -0700169 glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700170
egdanield5e3b9e2016-03-08 12:19:54 -0800171 glslCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800172
cdaltona6b92ad2016-04-11 12:03:08 -0700173 // Assume the minimum precisions mandated by the SPIR-V spec.
174 glslCaps->fShaderPrecisionVaries = true;
175 for (int s = 0; s < kGrShaderTypeCount; ++s) {
176 auto& highp = glslCaps->fFloatPrecisions[s][kHigh_GrSLPrecision];
177 highp.fLogRangeLow = highp.fLogRangeHigh = 127;
178 highp.fBits = 23;
179
180 auto& mediump = glslCaps->fFloatPrecisions[s][kMedium_GrSLPrecision];
181 mediump.fLogRangeLow = mediump.fLogRangeHigh = 14;
182 mediump.fBits = 10;
183
184 glslCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump;
185 }
186 glslCaps->initSamplerPrecisionTable();
187
cdalton9c3f1432016-03-11 10:07:37 -0800188 glslCaps->fMaxVertexSamplers =
189 glslCaps->fMaxGeometrySamplers =
jvanverthe78d4872016-09-27 03:33:05 -0700190 glslCaps->fMaxFragmentSamplers = SkTMin(SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
191 properties.limits.maxPerStageDescriptorSamplers),
192 (uint32_t)INT_MAX);
193 glslCaps->fMaxCombinedSamplers = SkTMin(SkTMin(properties.limits.maxDescriptorSetSampledImages,
194 properties.limits.maxDescriptorSetSamplers),
195 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500196}
197
egdaniel8f1dcaa2016-04-01 10:10:45 -0700198bool stencil_format_supported(const GrVkInterface* interface,
199 VkPhysicalDevice physDev,
200 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500201 VkFormatProperties props;
202 memset(&props, 0, sizeof(VkFormatProperties));
203 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700204 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500205}
206
egdaniel8f1dcaa2016-04-01 10:10:45 -0700207void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
208 // List of legal stencil formats (though perhaps not supported on
209 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700210 // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
egdaniel8f1dcaa2016-04-01 10:10:45 -0700211 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500212 static const StencilFormat
213 // internal Format stencil bits total bits packed?
214 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700215 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
216 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500217
egdaniel8f1dcaa2016-04-01 10:10:45 -0700218 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
219 fPreferedStencilFormat = gS8;
220 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
221 fPreferedStencilFormat = gD24S8;
222 } else {
223 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
224 fPreferedStencilFormat = gD32S8;
225 }
226}
227
228void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
229 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
230 VkFormat format;
231 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
232 fConfigTable[i].init(interface, physDev, format);
233 }
234 }
235}
236
237void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
238 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
239 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
240 *flags = *flags | kTextureable_Flag;
241 }
242
243 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
244 *flags = *flags | kRenderable_Flag;
245 }
246
247 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
248 *flags = *flags | kBlitSrc_Flag;
249 }
250
251 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
252 *flags = *flags | kBlitDst_Flag;
253 }
254}
255
256void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
257 VkPhysicalDevice physDev,
258 VkFormat format) {
259 VkFormatProperties props;
260 memset(&props, 0, sizeof(VkFormatProperties));
261 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
262 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
263 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -0500264}