1/*
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#include "GrRenderTarget.h"
10#include "GrShaderCaps.h"
11#include "GrVkUtil.h"
12#include "vk/GrVkBackendContext.h"
13#include "vk/GrVkInterface.h"
14
15GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
16                   VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags)
17    : INHERITED(contextOptions) {
18    fCanUseGLSLForShaderModule = false;
19    fMustDoCopiesFromOrigin = false;
20    fSupportsCopiesAsDraws = false;
21    fMustSubmitCommandsBeforeCopyOp = false;
22    fMustSleepOnTearDown  = false;
23    fNewSecondaryCBOnPipelineChange = false;
24
25    /**************************************************************************
26    * GrDrawTargetCaps fields
27    **************************************************************************/
28    fMipMapSupport = true;   // always available in Vulkan
29    fSRGBSupport = true;   // always available in Vulkan
30    fNPOTTextureTileSupport = true;  // always available in Vulkan
31    fTwoSidedStencilSupport = true;  // always available in Vulkan
32    fStencilWrapOpsSupport = true; // always available in Vulkan
33    fDiscardRenderTargetSupport = true;
34    fReuseScratchTextures = true; //TODO: figure this out
35    fGpuTracingSupport = false; //TODO: figure this out
36    fCompressedTexSubImageSupport = false; //TODO: figure this out
37    fOversizedStencilSupport = false; //TODO: figure this out
38
39    fUseDrawInsteadOfClear = false;
40    fFenceSyncSupport = true;   // always available in Vulkan
41    fCrossContextTextureSupport = false; // TODO: Add thread-safe memory pools so we can enable this
42
43    fMapBufferFlags = kNone_MapFlags; //TODO: figure this out
44    fBufferMapThreshold = SK_MaxS32;  //TODO: figure this out
45
46    fMaxRenderTargetSize = 4096; // minimum required by spec
47    fMaxTextureSize = 4096; // minimum required by spec
48    fMaxColorSampleCount = 4; // minimum required by spec
49    fMaxStencilSampleCount = 4; // minimum required by spec
50
51    fShaderCaps.reset(new GrShaderCaps(contextOptions));
52
53    this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags);
54}
55
56bool GrVkCaps::initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const {
57    // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa).
58    // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a
59    // render target as well.
60    desc->fOrigin = src->origin();
61    desc->fConfig = src->config();
62    if (src->numColorSamples() > 1 || (src->asTexture() && this->supportsCopiesAsDraws())) {
63        desc->fFlags = kRenderTarget_GrSurfaceFlag;
64    } else {
65        // Just going to use CopyImage here
66        desc->fFlags = kNone_GrSurfaceFlags;
67    }
68
69    return true;
70}
71
72void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface,
73                    VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) {
74
75    VkPhysicalDeviceProperties properties;
76    GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties));
77
78    VkPhysicalDeviceMemoryProperties memoryProperties;
79    GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties));
80
81    this->initGrCaps(properties, memoryProperties, featureFlags);
82    this->initShaderCaps(properties, featureFlags);
83    this->initConfigTable(vkInterface, physDev);
84    this->initStencilFormat(vkInterface, physDev);
85
86    if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) {
87        // Currently disabling this feature since it does not play well with validation layers which
88        // expect a SPIR-V shader
89        // fCanUseGLSLForShaderModule = true;
90    }
91
92    if (kQualcomm_VkVendor == properties.vendorID) {
93        fMustDoCopiesFromOrigin = true;
94    }
95
96    if (kNvidia_VkVendor == properties.vendorID) {
97        fSupportsCopiesAsDraws = true;
98        fMustSubmitCommandsBeforeCopyOp = true;
99    }
100
101#if defined(SK_BUILD_FOR_WIN)
102    if (kNvidia_VkVendor == properties.vendorID) {
103        fMustSleepOnTearDown = true;
104    }
105#elif defined(SK_BUILD_FOR_ANDROID)
106    if (kImagination_VkVendor == properties.vendorID) {
107        fMustSleepOnTearDown = true;
108    }
109#endif
110
111    this->applyOptionsOverrides(contextOptions);
112    fShaderCaps->applyOptionsOverrides(contextOptions);
113}
114
115int get_max_sample_count(VkSampleCountFlags flags) {
116    SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT);
117    if (!(flags & VK_SAMPLE_COUNT_2_BIT)) {
118        return 0;
119    }
120    if (!(flags & VK_SAMPLE_COUNT_4_BIT)) {
121        return 2;
122    }
123    if (!(flags & VK_SAMPLE_COUNT_8_BIT)) {
124        return 4;
125    }
126    if (!(flags & VK_SAMPLE_COUNT_16_BIT)) {
127        return 8;
128    }
129    if (!(flags & VK_SAMPLE_COUNT_32_BIT)) {
130        return 16;
131    }
132    if (!(flags & VK_SAMPLE_COUNT_64_BIT)) {
133        return 32;
134    }
135    return 64;
136}
137
138void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) {
139    VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts;
140    VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts;
141
142    fMaxColorSampleCount = get_max_sample_count(colorSamples);
143    fMaxStencilSampleCount = get_max_sample_count(stencilSamples);
144}
145
146void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties,
147                          const VkPhysicalDeviceMemoryProperties& memoryProperties,
148                          uint32_t featureFlags) {
149    // So GPUs, like AMD, are reporting MAX_INT support vertex attributes. In general, there is no
150    // need for us ever to support that amount, and it makes tests which tests all the vertex
151    // attribs timeout looping over that many. For now, we'll cap this at 64 max and can raise it if
152    // we ever find that need.
153    static const uint32_t kMaxVertexAttributes = 64;
154    fMaxVertexAttributes = SkTMin(properties.limits.maxVertexInputAttributes, kMaxVertexAttributes);
155    // AMD advertises support for MAX_UINT vertex input attributes, but in reality only supports 32.
156    if (kAMD_VkVendor == properties.vendorID) {
157        fMaxVertexAttributes = SkTMin(fMaxVertexAttributes, 32);
158    }
159
160    // We could actually query and get a max size for each config, however maxImageDimension2D will
161    // give the minimum max size across all configs. So for simplicity we will use that for now.
162    fMaxRenderTargetSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
163    fMaxTextureSize = SkTMin(properties.limits.maxImageDimension2D, (uint32_t)INT_MAX);
164
165    this->initSampleCount(properties);
166
167    // Assuming since we will always map in the end to upload the data we might as well just map
168    // from the get go. There is no hard data to suggest this is faster or slower.
169    fBufferMapThreshold = 0;
170
171    fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag;
172
173    fStencilWrapOpsSupport = true;
174    fOversizedStencilSupport = true;
175    fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag);
176
177    // AMD seems to have issues binding new VkPipelines inside a secondary command buffer.
178    // Current workaround is to use a different secondary command buffer for each new VkPipeline.
179    if (kAMD_VkVendor == properties.vendorID) {
180        fNewSecondaryCBOnPipelineChange = true;
181    }
182}
183
184void GrVkCaps::initShaderCaps(const VkPhysicalDeviceProperties& properties, uint32_t featureFlags) {
185    GrShaderCaps* shaderCaps = fShaderCaps.get();
186    shaderCaps->fVersionDeclString = "#version 330\n";
187
188
189    // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config.
190    for (int i = 0; i < kGrPixelConfigCnt; ++i) {
191        GrPixelConfig config = static_cast<GrPixelConfig>(i);
192        if (GrPixelConfigIsAlphaOnly(config)) {
193            shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR();
194            shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA();
195        } else {
196            if (kGray_8_GrPixelConfig == config) {
197                shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRA();
198            } else if (kRGBA_4444_GrPixelConfig == config) {
199                // The vulkan spec does not require R4G4B4A4 to be supported for texturing so we
200                // store the data in a B4G4R4A4 texture and then swizzle it when doing texture reads
201                // or writing to outputs. Since we're not actually changing the data at all, the
202                // only extra work is the swizzle in the shader for all operations.
203                shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::BGRA();
204                shaderCaps->fConfigOutputSwizzle[i] = GrSwizzle::BGRA();
205            } else {
206                shaderCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA();
207            }
208        }
209    }
210
211    if (kImagination_VkVendor == properties.vendorID) {
212        shaderCaps->fAtan2ImplementedAsAtanYOverX = true;
213    }
214
215    // Vulkan is based off ES 3.0 so the following should all be supported
216    shaderCaps->fUsesPrecisionModifiers = true;
217    shaderCaps->fFlatInterpolationSupport = true;
218
219    // GrShaderCaps
220
221    shaderCaps->fShaderDerivativeSupport = true;
222    shaderCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag);
223
224    shaderCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag);
225    if (kAMD_VkVendor == properties.vendorID) {
226        // Currently DualSourceBlending is not working on AMD. vkCreateGraphicsPipeline fails when
227        // using a draw with dual source. Looking into whether it is driver bug or issue with our
228        // SPIR-V. Bug skia:6405
229        shaderCaps->fDualSourceBlendingSupport = false;
230    }
231
232    shaderCaps->fIntegerSupport = true;
233
234    // Assume the minimum precisions mandated by the SPIR-V spec.
235    shaderCaps->fShaderPrecisionVaries = true;
236    for (int s = 0; s < kGrShaderTypeCount; ++s) {
237        auto& highp = shaderCaps->fFloatPrecisions[s][kHigh_GrSLPrecision];
238        highp.fLogRangeLow = highp.fLogRangeHigh = 127;
239        highp.fBits = 23;
240
241        auto& mediump = shaderCaps->fFloatPrecisions[s][kMedium_GrSLPrecision];
242        mediump.fLogRangeLow = mediump.fLogRangeHigh = 14;
243        mediump.fBits = 10;
244
245        shaderCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump;
246    }
247    shaderCaps->initSamplerPrecisionTable();
248
249    shaderCaps->fMaxVertexSamplers =
250    shaderCaps->fMaxGeometrySamplers =
251    shaderCaps->fMaxFragmentSamplers = SkTMin(
252                                       SkTMin(properties.limits.maxPerStageDescriptorSampledImages,
253                                              properties.limits.maxPerStageDescriptorSamplers),
254                                              (uint32_t)INT_MAX);
255    shaderCaps->fMaxCombinedSamplers = SkTMin(
256                                       SkTMin(properties.limits.maxDescriptorSetSampledImages,
257                                              properties.limits.maxDescriptorSetSamplers),
258                                              (uint32_t)INT_MAX);
259}
260
261bool stencil_format_supported(const GrVkInterface* interface,
262                              VkPhysicalDevice physDev,
263                              VkFormat format) {
264    VkFormatProperties props;
265    memset(&props, 0, sizeof(VkFormatProperties));
266    GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
267    return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures);
268}
269
270void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) {
271    // List of legal stencil formats (though perhaps not supported on
272    // the particular gpu/driver) from most preferred to least. We are guaranteed to have either
273    // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT
274    // can optionally have 24 unused bits at the end so we assume the total bits is 64.
275    static const StencilFormat
276                  // internal Format             stencil bits      total bits        packed?
277        gS8    = { VK_FORMAT_S8_UINT,            8,                 8,               false },
278        gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT,  8,                32,               true },
279        gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8,                64,               true };
280
281    if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) {
282        fPreferedStencilFormat = gS8;
283    } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) {
284        fPreferedStencilFormat = gD24S8;
285    } else {
286        SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT));
287        fPreferedStencilFormat = gD32S8;
288    }
289}
290
291void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) {
292    for (int i = 0; i < kGrPixelConfigCnt; ++i) {
293        VkFormat format;
294        if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) {
295            fConfigTable[i].init(interface, physDev, format);
296        }
297    }
298
299    // We currently do not support compressed textures in Vulkan
300    const uint16_t kFlagsToRemove = ConfigInfo::kTextureable_Flag|ConfigInfo::kRenderable_Flag;
301    fConfigTable[kETC1_GrPixelConfig].fOptimalFlags &= ~kFlagsToRemove;
302    fConfigTable[kETC1_GrPixelConfig].fLinearFlags &= ~kFlagsToRemove;
303}
304
305void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) {
306    if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) &&
307        SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) {
308        *flags = *flags | kTextureable_Flag;
309    }
310
311    if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) {
312        *flags = *flags | kRenderable_Flag;
313    }
314
315    if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) {
316        *flags = *flags | kBlitSrc_Flag;
317    }
318
319    if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) {
320        *flags = *flags | kBlitDst_Flag;
321    }
322}
323
324void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface,
325                                VkPhysicalDevice physDev,
326                                VkFormat format) {
327    VkFormatProperties props;
328    memset(&props, 0, sizeof(VkFormatProperties));
329    GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props));
330    InitConfigFlags(props.linearTilingFeatures, &fLinearFlags);
331    InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags);
332}
333