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