Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
| 10 | #include "GrVkUtil.h" |
| 11 | #include "glsl/GrGLSLCaps.h" |
| 12 | #include "vk/GrVkInterface.h" |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 13 | #include "vk/GrVkBackendContext.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | |
| 15 | GrVkCaps::GrVkCaps(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface, |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 16 | VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) |
| 17 | : INHERITED(contextOptions) { |
| 18 | fCanUseGLSLForShaderModule = false; |
| 19 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 20 | /************************************************************************** |
| 21 | * GrDrawTargetCaps fields |
| 22 | **************************************************************************/ |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 23 | fMipMapSupport = true; // always available in Vulkan |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 24 | fSRGBSupport = true; // always available in Vulkan |
brianosman | 8879186 | 2016-05-23 10:15:27 -0700 | [diff] [blame^] | 25 | fNPOTTextureTileSupport = true; // always available in Vulkan |
jvanverth | c8ef053 | 2016-05-03 10:45:48 -0700 | [diff] [blame] | 26 | fTwoSidedStencilSupport = true; // always available in Vulkan |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | fStencilWrapOpsSupport = false; //TODO: figure this out |
| 28 | fDiscardRenderTargetSupport = false; //TODO: figure this out |
| 29 | fReuseScratchTextures = true; //TODO: figure this out |
| 30 | fGpuTracingSupport = false; //TODO: figure this out |
| 31 | fCompressedTexSubImageSupport = false; //TODO: figure this out |
| 32 | fOversizedStencilSupport = false; //TODO: figure this out |
| 33 | |
| 34 | fUseDrawInsteadOfClear = false; //TODO: figure this out |
| 35 | |
| 36 | fMapBufferFlags = kNone_MapFlags; //TODO: figure this out |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 37 | fBufferMapThreshold = SK_MaxS32; //TODO: figure this out |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | |
| 39 | fMaxRenderTargetSize = 4096; // minimum required by spec |
| 40 | fMaxTextureSize = 4096; // minimum required by spec |
| 41 | fMaxColorSampleCount = 4; // minimum required by spec |
| 42 | fMaxStencilSampleCount = 4; // minimum required by spec |
| 43 | |
| 44 | |
| 45 | fShaderCaps.reset(new GrGLSLCaps(contextOptions)); |
| 46 | |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 47 | this->init(contextOptions, vkInterface, physDev, featureFlags, extensionFlags); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void GrVkCaps::init(const GrContextOptions& contextOptions, const GrVkInterface* vkInterface, |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 51 | VkPhysicalDevice physDev, uint32_t featureFlags, uint32_t extensionFlags) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 52 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 53 | VkPhysicalDeviceProperties properties; |
| 54 | GR_VK_CALL(vkInterface, GetPhysicalDeviceProperties(physDev, &properties)); |
| 55 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 56 | VkPhysicalDeviceMemoryProperties memoryProperties; |
| 57 | GR_VK_CALL(vkInterface, GetPhysicalDeviceMemoryProperties(physDev, &memoryProperties)); |
| 58 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 59 | this->initGrCaps(properties, memoryProperties, featureFlags); |
| 60 | this->initGLSLCaps(properties, featureFlags); |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 61 | this->initConfigTable(vkInterface, physDev); |
| 62 | this->initStencilFormat(vkInterface, physDev); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 63 | |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 64 | if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) { |
egdaniel | d632bb4 | 2016-03-30 12:06:48 -0700 | [diff] [blame] | 65 | // Currently disabling this feature since it does not play well with validation layers which |
| 66 | // expect a SPIR-V shader |
| 67 | // fCanUseGLSLForShaderModule = true; |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 68 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 69 | |
| 70 | this->applyOptionsOverrides(contextOptions); |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 71 | GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get()); |
| 72 | glslCaps->applyOptionsOverrides(contextOptions); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int get_max_sample_count(VkSampleCountFlags flags) { |
| 76 | SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT); |
| 77 | if (!(flags & VK_SAMPLE_COUNT_2_BIT)) { |
| 78 | return 0; |
| 79 | } |
| 80 | if (!(flags & VK_SAMPLE_COUNT_4_BIT)) { |
| 81 | return 2; |
| 82 | } |
| 83 | if (!(flags & VK_SAMPLE_COUNT_8_BIT)) { |
| 84 | return 4; |
| 85 | } |
| 86 | if (!(flags & VK_SAMPLE_COUNT_16_BIT)) { |
| 87 | return 8; |
| 88 | } |
| 89 | if (!(flags & VK_SAMPLE_COUNT_32_BIT)) { |
| 90 | return 16; |
| 91 | } |
| 92 | if (!(flags & VK_SAMPLE_COUNT_64_BIT)) { |
| 93 | return 32; |
| 94 | } |
| 95 | return 64; |
| 96 | } |
| 97 | |
| 98 | void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) { |
| 99 | VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts; |
| 100 | VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts; |
| 101 | |
| 102 | fMaxColorSampleCount = get_max_sample_count(colorSamples); |
| 103 | fMaxStencilSampleCount = get_max_sample_count(stencilSamples); |
| 104 | } |
| 105 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 106 | void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties, |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 107 | const VkPhysicalDeviceMemoryProperties& memoryProperties, |
| 108 | uint32_t featureFlags) { |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 109 | fMaxVertexAttributes = properties.limits.maxVertexInputAttributes; |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 110 | // We could actually query and get a max size for each config, however maxImageDimension2D will |
| 111 | // give the minimum max size across all configs. So for simplicity we will use that for now. |
| 112 | fMaxRenderTargetSize = properties.limits.maxImageDimension2D; |
| 113 | fMaxTextureSize = properties.limits.maxImageDimension2D; |
| 114 | |
| 115 | this->initSampleCount(properties); |
| 116 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 117 | // Assuming since we will always map in the end to upload the data we might as well just map |
| 118 | // from the get go. There is no hard data to suggest this is faster or slower. |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 119 | fBufferMapThreshold = 0; |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 120 | |
| 121 | fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag; |
| 122 | |
| 123 | fStencilWrapOpsSupport = true; |
| 124 | fOversizedStencilSupport = true; |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 125 | fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag); |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 126 | } |
| 127 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 128 | void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties, |
| 129 | uint32_t featureFlags) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 130 | GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get()); |
egdaniel | 3a15fd4 | 2016-04-05 11:00:29 -0700 | [diff] [blame] | 131 | glslCaps->fVersionDeclString = "#version 330\n"; |
| 132 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 133 | |
| 134 | // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config. |
| 135 | for (int i = 0; i < kGrPixelConfigCnt; ++i) { |
| 136 | GrPixelConfig config = static_cast<GrPixelConfig>(i); |
| 137 | if (GrPixelConfigIsAlphaOnly(config)) { |
| 138 | glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR(); |
| 139 | glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA(); |
| 140 | } else { |
| 141 | glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA(); |
| 142 | } |
| 143 | } |
egdaniel | 67acb83 | 2016-02-26 08:32:20 -0800 | [diff] [blame] | 144 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 145 | // Vulkan is based off ES 3.0 so the following should all be supported |
| 146 | glslCaps->fUsesPrecisionModifiers = true; |
| 147 | glslCaps->fFlatInterpolationSupport = true; |
| 148 | |
| 149 | // GrShaderCaps |
| 150 | |
egdaniel | 67acb83 | 2016-02-26 08:32:20 -0800 | [diff] [blame] | 151 | glslCaps->fShaderDerivativeSupport = true; |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 152 | glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag); |
egdaniel | d632bb4 | 2016-03-30 12:06:48 -0700 | [diff] [blame] | 153 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 154 | glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag); |
egdaniel | d632bb4 | 2016-03-30 12:06:48 -0700 | [diff] [blame] | 155 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 156 | glslCaps->fIntegerSupport = true; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 157 | |
cdalton | a6b92ad | 2016-04-11 12:03:08 -0700 | [diff] [blame] | 158 | // Assume the minimum precisions mandated by the SPIR-V spec. |
| 159 | glslCaps->fShaderPrecisionVaries = true; |
| 160 | for (int s = 0; s < kGrShaderTypeCount; ++s) { |
| 161 | auto& highp = glslCaps->fFloatPrecisions[s][kHigh_GrSLPrecision]; |
| 162 | highp.fLogRangeLow = highp.fLogRangeHigh = 127; |
| 163 | highp.fBits = 23; |
| 164 | |
| 165 | auto& mediump = glslCaps->fFloatPrecisions[s][kMedium_GrSLPrecision]; |
| 166 | mediump.fLogRangeLow = mediump.fLogRangeHigh = 14; |
| 167 | mediump.fBits = 10; |
| 168 | |
| 169 | glslCaps->fFloatPrecisions[s][kLow_GrSLPrecision] = mediump; |
| 170 | } |
| 171 | glslCaps->initSamplerPrecisionTable(); |
| 172 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 173 | glslCaps->fMaxVertexSamplers = |
| 174 | glslCaps->fMaxGeometrySamplers = |
| 175 | glslCaps->fMaxFragmentSamplers = SkTMin(properties.limits.maxPerStageDescriptorSampledImages, |
| 176 | properties.limits.maxPerStageDescriptorSamplers); |
| 177 | glslCaps->fMaxCombinedSamplers = SkTMin(properties.limits.maxDescriptorSetSampledImages, |
| 178 | properties.limits.maxDescriptorSetSamplers); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 179 | } |
| 180 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 181 | bool stencil_format_supported(const GrVkInterface* interface, |
| 182 | VkPhysicalDevice physDev, |
| 183 | VkFormat format) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 184 | VkFormatProperties props; |
| 185 | memset(&props, 0, sizeof(VkFormatProperties)); |
| 186 | GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props)); |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 187 | return SkToBool(VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT & props.optimalTilingFeatures); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 188 | } |
| 189 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 190 | void GrVkCaps::initStencilFormat(const GrVkInterface* interface, VkPhysicalDevice physDev) { |
| 191 | // List of legal stencil formats (though perhaps not supported on |
| 192 | // the particular gpu/driver) from most preferred to least. We are guaranteed to have either |
jvanverth | a4b0fed | 2016-04-27 11:42:21 -0700 | [diff] [blame] | 193 | // VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. VK_FORMAT_D32_SFLOAT_S8_UINT |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 194 | // can optionally have 24 unused bits at the end so we assume the total bits is 64. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 195 | static const StencilFormat |
| 196 | // internal Format stencil bits total bits packed? |
| 197 | gS8 = { VK_FORMAT_S8_UINT, 8, 8, false }, |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 198 | gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true }, |
| 199 | gD32S8 = { VK_FORMAT_D32_SFLOAT_S8_UINT, 8, 64, true }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 200 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 201 | if (stencil_format_supported(interface, physDev, VK_FORMAT_S8_UINT)) { |
| 202 | fPreferedStencilFormat = gS8; |
| 203 | } else if (stencil_format_supported(interface, physDev, VK_FORMAT_D24_UNORM_S8_UINT)) { |
| 204 | fPreferedStencilFormat = gD24S8; |
| 205 | } else { |
| 206 | SkASSERT(stencil_format_supported(interface, physDev, VK_FORMAT_D32_SFLOAT_S8_UINT)); |
| 207 | fPreferedStencilFormat = gD32S8; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void GrVkCaps::initConfigTable(const GrVkInterface* interface, VkPhysicalDevice physDev) { |
| 212 | for (int i = 0; i < kGrPixelConfigCnt; ++i) { |
| 213 | VkFormat format; |
| 214 | if (GrPixelConfigToVkFormat(static_cast<GrPixelConfig>(i), &format)) { |
| 215 | fConfigTable[i].init(interface, physDev, format); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void GrVkCaps::ConfigInfo::InitConfigFlags(VkFormatFeatureFlags vkFlags, uint16_t* flags) { |
| 221 | if (SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & vkFlags) && |
| 222 | SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT & vkFlags)) { |
| 223 | *flags = *flags | kTextureable_Flag; |
| 224 | } |
| 225 | |
| 226 | if (SkToBool(VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT & vkFlags)) { |
| 227 | *flags = *flags | kRenderable_Flag; |
| 228 | } |
| 229 | |
| 230 | if (SkToBool(VK_FORMAT_FEATURE_BLIT_SRC_BIT & vkFlags)) { |
| 231 | *flags = *flags | kBlitSrc_Flag; |
| 232 | } |
| 233 | |
| 234 | if (SkToBool(VK_FORMAT_FEATURE_BLIT_DST_BIT & vkFlags)) { |
| 235 | *flags = *flags | kBlitDst_Flag; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void GrVkCaps::ConfigInfo::init(const GrVkInterface* interface, |
| 240 | VkPhysicalDevice physDev, |
| 241 | VkFormat format) { |
| 242 | VkFormatProperties props; |
| 243 | memset(&props, 0, sizeof(VkFormatProperties)); |
| 244 | GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props)); |
| 245 | InitConfigFlags(props.linearTilingFeatures, &fLinearFlags); |
| 246 | InitConfigFlags(props.optimalTilingFeatures, &fOptimalFlags); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 247 | } |