blob: f614ae39c69ea14ea065678c9568a26542224d02 [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
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkUtil.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#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;
egdanielbc9b2962016-09-27 08:00:53 -070020 fSupportsCopiesAsDraws = false;
egdanielfd016d72016-09-27 12:13:05 -070021 fMustSubmitCommandsBeforeCopyOp = false;
egdanielc5ec1402016-03-28 12:14:42 -070022
Greg Daniel164a9f02016-02-22 09:56:40 -050023 /**************************************************************************
24 * GrDrawTargetCaps fields
25 **************************************************************************/
jvanverth62340062016-04-26 08:01:44 -070026 fMipMapSupport = true; // always available in Vulkan
brianosmanf05ab1b2016-05-12 11:01:10 -070027 fSRGBSupport = true; // always available in Vulkan
brianosman88791862016-05-23 10:15:27 -070028 fNPOTTextureTileSupport = true; // always available in Vulkan
jvanverthc8ef0532016-05-03 10:45:48 -070029 fTwoSidedStencilSupport = true; // always available in Vulkan
egdaniel50134cc2016-05-23 12:34:38 -070030 fStencilWrapOpsSupport = true; // always available in Vulkan
egdaniel37535c92016-06-30 08:23:30 -070031 fDiscardRenderTargetSupport = true;
Greg Daniel164a9f02016-02-22 09:56:40 -050032 fReuseScratchTextures = true; //TODO: figure this out
33 fGpuTracingSupport = false; //TODO: figure this out
34 fCompressedTexSubImageSupport = false; //TODO: figure this out
35 fOversizedStencilSupport = false; //TODO: figure this out
36
egdaniel9cb63402016-06-23 08:37:05 -070037 fUseDrawInsteadOfClear = false;
jvanverth84741b32016-09-30 08:39:02 -070038 fFenceSyncSupport = true; // always available in Vulkan
Greg Daniel164a9f02016-02-22 09:56:40 -050039
40 fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
cdalton397536c2016-03-25 12:15:03 -070041 fBufferMapThreshold = SK_MaxS32; //TODO: figure this out
Greg Daniel164a9f02016-02-22 09:56:40 -050042
43 fMaxRenderTargetSize = 4096; // minimum required by spec
44 fMaxTextureSize = 4096; // minimum required by spec
45 fMaxColorSampleCount = 4; // minimum required by spec
46 fMaxStencilSampleCount = 4; // minimum required by spec
47
Brian Salomon94efbf52016-11-29 13:43:05 -050048 fShaderCaps.reset(new GrShaderCaps(contextOptions));
Greg Daniel164a9f02016-02-22 09:56:40 -050049
egdanielc5ec1402016-03-28 12:14:42 -070050 this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -050051}
52
53void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
egdanielc5ec1402016-03-28 12:14:42 -070054 VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
Greg Daniel164a9f02016-02-22 09:56:40 -050055
egdanield5e3b9e2016-03-08 12:19:54 -080056 VkPhysicalDeviceProperties properties;
57 GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
58
egdanield5e3b9e2016-03-08 12:19:54 -080059 VkPhysicalDeviceMemoryProperties memoryProperties;
60 GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
61
jvanverthfd7bd452016-03-25 06:29:52 -070062 this->initGrCaps(properties, memoryProperties, featureFlags);
Brian Salomon1edc5b92016-11-29 13:43:46 -050063 this->initShaderCaps(properties, featureFlags);
egdaniel8f1dcaa2016-04-01 10:10:45 -070064 this->initConfigTable(vkInterface, physDev);
65 this->initStencilFormat(vkInterface, physDev);
Greg Daniel164a9f02016-02-22 09:56:40 -050066
egdanielc5ec1402016-03-28 12:14:42 -070067 if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
egdanield632bb42016-03-30 12:06:48 -070068 // Currently disabling this feature since it does not play well with validation layers which
69 // expect a SPIR-V shader
70 // fCanUseGLSLForShaderModule = true;
egdanielc5ec1402016-03-28 12:14:42 -070071 }
Greg Daniel164a9f02016-02-22 09:56:40 -050072
egdaniel6fa0a912016-09-12 11:51:29 -070073 if (kQualcomm_VkVendor == properties.vendorID) {
74 fMustDoCopiesFromOrigin = true;
75 }
76
egdanielfd016d72016-09-27 12:13:05 -070077 if (kNvidia_VkVendor == properties.vendorID) {
78 fSupportsCopiesAsDraws = true;
79 fMustSubmitCommandsBeforeCopyOp = true;
80 }
81
Greg Daniel164a9f02016-02-22 09:56:40 -050082 this->applyOptionsOverrides(contextOptions);
Brian Salomon1edc5b92016-11-29 13:43:46 -050083 fShaderCaps->applyOptionsOverrides(contextOptions);
Greg Daniel164a9f02016-02-22 09:56:40 -050084}
85
86int get_max_sample_count(VkSampleCountFlags flags) {
87 SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
88 if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
89 return 0;
90 }
91 if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
92 return 2;
93 }
94 if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
95 return 4;
96 }
97 if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
98 return 8;
99 }
100 if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
101 return 16;
102 }
103 if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
104 return 32;
105 }
106 return 64;
107}
108
109void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
110 VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
111 VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
112
113 fMaxColorSampleCount = get_max_sample_count(colorSamples);
114 fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
115}
116
egdanield5e3b9e2016-03-08 12:19:54 -0800117void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
jvanverthfd7bd452016-03-25 06:29:52 -0700118 const VkPhysicalDeviceMemoryProperties& memoryProperties,
119 uint32_t featureFlags) {
jvanverthe78d4872016-09-27 03:33:05 -0700120 fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800121 // We could actually query and get a max size for each config, however maxImageDimension2D will
122 // give the minimum max size across all configs. So for simplicity we will use that for now.
jvanverthe78d4872016-09-27 03:33:05 -0700123 fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
124 fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
egdanield5e3b9e2016-03-08 12:19:54 -0800125
126 this->initSampleCount(properties);
127
egdanield5e3b9e2016-03-08 12:19:54 -0800128 // Assuming since we will always map in the end to upload the data we might as well just map
129 // from the get go. There is no hard data to suggest this is faster or slower.
cdalton397536c2016-03-25 12:15:03 -0700130 fBufferMapThreshold = 0;
egdanield5e3b9e2016-03-08 12:19:54 -0800131
132 fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
133
134 fStencilWrapOpsSupport = true;
135 fOversizedStencilSupport = true;
ethannicholas28ef4452016-03-25 09:26:03 -0700136 fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
egdanield5e3b9e2016-03-08 12:19:54 -0800137}
138
Brian Salomon1edc5b92016-11-29 13:43:46 -0500139void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
140 GrShaderCaps* shaderCaps = fShaderCaps.get();
141 shaderCaps->fVersionDeclString = "#version 330\n";
egdaniel3a15fd42016-04-05 11:00:29 -0700142
Greg Daniel164a9f02016-02-22 09:56:40 -0500143
144 // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
145 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
146 GrPixelConfig config = static_cast<GrPixelConfig>(i);
147 if (GrPixelConfigIsAlphaOnly(config)) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500148 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
149 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
Greg Daniel164a9f02016-02-22 09:56:40 -0500150 } else {
Brian Osman986563b2017-01-10 14:20:02 -0500151 if (kGray_8_GrPixelConfig == config) {
152 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
153 } else if (kRGBA_4444_GrPixelConfig == config) {
egdaniel3fe03272016-08-15 10:59:17 -0700154 // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
155 // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
156 // or writing to outputs. Since we're not actually changing the data at all, the
157 // only extra work is the swizzle in the shader for all operations.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500158 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
159 shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
egdaniel3fe03272016-08-15 10:59:17 -0700160 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500161 shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
egdaniel3fe03272016-08-15 10:59:17 -0700162 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500163 }
164 }
egdaniel67acb832016-02-26 08:32:20 -0800165
egdanield5e3b9e2016-03-08 12:19:54 -0800166 // Vulkan is based off ES 3.0 so the following should all be supported
Brian Salomon1edc5b92016-11-29 13:43:46 -0500167 shaderCaps->fUsesPrecisionModifiers = true;
168 shaderCaps->fFlatInterpolationSupport = true;
egdanield5e3b9e2016-03-08 12:19:54 -0800169
170 // GrShaderCaps
171
Brian Salomon1edc5b92016-11-29 13:43:46 -0500172 shaderCaps->fShaderDerivativeSupport = true;
173 shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700174
Brian Salomon1edc5b92016-11-29 13:43:46 -0500175 shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
egdanield632bb42016-03-30 12:06:48 -0700176
Brian Salomon1edc5b92016-11-29 13:43:46 -0500177 shaderCaps->fIntegerSupport = true;
cdalton9c3f1432016-03-11 10:07:37 -0800178
cdaltona6b92ad2016-04-11 12:03:08 -0700179 // Assume the minimum precisions mandated by the SPIR-V spec.
Brian Salomon1edc5b92016-11-29 13:43:46 -0500180 shaderCaps->fShaderPrecisionVaries = true;
cdaltona6b92ad2016-04-11 12:03:08 -0700181 for (int s = 0; s < kGrShaderTypeCount; ++s) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500182 auto& highp = shaderCaps->fFloatPrecisions[s][kHigh_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700183 highp.fLogRangeLow = highp.fLogRangeHigh = 127;
184 highp.fBits = 23;
185
Brian Salomon1edc5b92016-11-29 13:43:46 -0500186 auto& mediump = shaderCaps->fFloatPrecisions[s][kMedium_GrSLPrecision];
cdaltona6b92ad2016-04-11 12:03:08 -0700187 mediump.fLogRangeLow = mediump.fLogRangeHigh = 14;
188 mediump.fBits = 10;
189
Brian Salomon1edc5b92016-11-29 13:43:46 -0500190 shaderCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump;
cdaltona6b92ad2016-04-11 12:03:08 -0700191 }
Brian Salomon1edc5b92016-11-29 13:43:46 -0500192 shaderCaps->initSamplerPrecisionTable();
cdaltona6b92ad2016-04-11 12:03:08 -0700193
Brian Salomon1edc5b92016-11-29 13:43:46 -0500194 shaderCaps->fMaxVertexSamplers =
195 shaderCaps->fMaxGeometrySamplers =
196 shaderCaps->fMaxFragmentSamplers = SkTMin(
197 SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
198 properties.limits.maxPerStageDescriptorSamplers),
199 (uint32_t)INT_MAX);
200 shaderCaps->fMaxCombinedSamplers = SkTMin(
201 SkTMin(properties.limits.maxDescriptorSetSampledImages,
202 properties.limits.maxDescriptorSetSamplers),
203 (uint32_t)INT_MAX);
Greg Daniel164a9f02016-02-22 09:56:40 -0500204}
205
egdaniel8f1dcaa2016-04-01 10:10:45 -0700206bool stencil_format_supported(const GrVkInterface* interface,
207 VkPhysicalDevice physDev,
208 VkFormat format) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500209 VkFormatProperties props;
210 memset(&props, 0, sizeof(VkFormatProperties));
211 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
egdaniel8f1dcaa2016-04-01 10:10:45 -0700212 return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
Greg Daniel164a9f02016-02-22 09:56:40 -0500213}
214
egdaniel8f1dcaa2016-04-01 10:10:45 -0700215void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
216 // List of legal stencil formats (though perhaps not supported on
217 // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
jvanvertha4b0fed2016-04-27 11:42:21 -0700218 // 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 -0700219 // can optionally have 24 unused bits at the end so we assume the total bits is 64.
Greg Daniel164a9f02016-02-22 09:56:40 -0500220 static const StencilFormat
221 // internal Format stencil bits total bits packed?
222 gS8 = { VK_FORMAT_S8_UINT, 8, 8, false },
egdaniel8f1dcaa2016-04-01 10:10:45 -0700223 gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true },
224 gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true };
Greg Daniel164a9f02016-02-22 09:56:40 -0500225
egdaniel8f1dcaa2016-04-01 10:10:45 -0700226 if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
227 fPreferedStencilFormat = gS8;
228 } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
229 fPreferedStencilFormat = gD24S8;
230 } else {
231 SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
232 fPreferedStencilFormat = gD32S8;
233 }
234}
235
236void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
237 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
238 VkFormat format;
239 if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
240 fConfigTable[i].init(interface, physDev, format);
241 }
242 }
243}
244
245void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
246 if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
247 SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
248 *flags = *flags | kTextureable_Flag;
249 }
250
251 if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
252 *flags = *flags | kRenderable_Flag;
253 }
254
255 if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
256 *flags = *flags | kBlitSrc_Flag;
257 }
258
259 if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
260 *flags = *flags | kBlitDst_Flag;
261 }
262}
263
264void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
265 VkPhysicalDevice physDev,
266 VkFormat format) {
267 VkFormatProperties props;
268 memset(&props, 0, sizeof(VkFormatProperties));
269 GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
270 InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
271 InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
Greg Daniel164a9f02016-02-22 09:56:40 -0500272}