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