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 | **************************************************************************/ |
| 23 | fMipMapSupport = false; //TODO: figure this out |
| 24 | fNPOTTextureTileSupport = false; //TODO: figure this out |
| 25 | fTwoSidedStencilSupport = false; //TODO: figure this out |
| 26 | fStencilWrapOpsSupport = false; //TODO: figure this out |
| 27 | fDiscardRenderTargetSupport = false; //TODO: figure this out |
| 28 | fReuseScratchTextures = true; //TODO: figure this out |
| 29 | fGpuTracingSupport = false; //TODO: figure this out |
| 30 | fCompressedTexSubImageSupport = false; //TODO: figure this out |
| 31 | fOversizedStencilSupport = false; //TODO: figure this out |
| 32 | |
| 33 | fUseDrawInsteadOfClear = false; //TODO: figure this out |
| 34 | |
| 35 | fMapBufferFlags = kNone_MapFlags; //TODO: figure this out |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 36 | fBufferMapThreshold = SK_MaxS32; //TODO: figure this out |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | |
| 38 | fMaxRenderTargetSize = 4096; // minimum required by spec |
| 39 | fMaxTextureSize = 4096; // minimum required by spec |
| 40 | fMaxColorSampleCount = 4; // minimum required by spec |
| 41 | fMaxStencilSampleCount = 4; // minimum required by spec |
| 42 | |
| 43 | |
| 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); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 60 | this->initConfigTexturableTable(vkInterface, physDev); |
| 61 | this->initConfigRenderableTable(vkInterface, physDev); |
| 62 | this->initStencilFormats(vkInterface, physDev); |
| 63 | |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame^] | 64 | if (SkToBool(extensionFlags & kNV_glsl_shader_GrVkExtensionFlag)) { |
| 65 | fCanUseGLSLForShaderModule = true; |
| 66 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 67 | |
| 68 | this->applyOptionsOverrides(contextOptions); |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame^] | 69 | GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get()); |
| 70 | glslCaps->applyOptionsOverrides(contextOptions); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int get_max_sample_count(VkSampleCountFlags flags) { |
| 74 | SkASSERT(flags & VK_SAMPLE_COUNT_1_BIT); |
| 75 | if (!(flags & VK_SAMPLE_COUNT_2_BIT)) { |
| 76 | return 0; |
| 77 | } |
| 78 | if (!(flags & VK_SAMPLE_COUNT_4_BIT)) { |
| 79 | return 2; |
| 80 | } |
| 81 | if (!(flags & VK_SAMPLE_COUNT_8_BIT)) { |
| 82 | return 4; |
| 83 | } |
| 84 | if (!(flags & VK_SAMPLE_COUNT_16_BIT)) { |
| 85 | return 8; |
| 86 | } |
| 87 | if (!(flags & VK_SAMPLE_COUNT_32_BIT)) { |
| 88 | return 16; |
| 89 | } |
| 90 | if (!(flags & VK_SAMPLE_COUNT_64_BIT)) { |
| 91 | return 32; |
| 92 | } |
| 93 | return 64; |
| 94 | } |
| 95 | |
| 96 | void GrVkCaps::initSampleCount(const VkPhysicalDeviceProperties& properties) { |
| 97 | VkSampleCountFlags colorSamples = properties.limits.framebufferColorSampleCounts; |
| 98 | VkSampleCountFlags stencilSamples = properties.limits.framebufferStencilSampleCounts; |
| 99 | |
| 100 | fMaxColorSampleCount = get_max_sample_count(colorSamples); |
| 101 | fMaxStencilSampleCount = get_max_sample_count(stencilSamples); |
| 102 | } |
| 103 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 104 | void GrVkCaps::initGrCaps(const VkPhysicalDeviceProperties& properties, |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 105 | const VkPhysicalDeviceMemoryProperties& memoryProperties, |
| 106 | uint32_t featureFlags) { |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 107 | fMaxVertexAttributes = properties.limits.maxVertexInputAttributes; |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 108 | // We could actually query and get a max size for each config, however maxImageDimension2D will |
| 109 | // give the minimum max size across all configs. So for simplicity we will use that for now. |
| 110 | fMaxRenderTargetSize = properties.limits.maxImageDimension2D; |
| 111 | fMaxTextureSize = properties.limits.maxImageDimension2D; |
| 112 | |
| 113 | this->initSampleCount(properties); |
| 114 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 115 | // Assuming since we will always map in the end to upload the data we might as well just map |
| 116 | // 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] | 117 | fBufferMapThreshold = 0; |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 118 | |
| 119 | fMapBufferFlags = kCanMap_MapFlag | kSubset_MapFlag; |
| 120 | |
| 121 | fStencilWrapOpsSupport = true; |
| 122 | fOversizedStencilSupport = true; |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 123 | fSampleShadingSupport = SkToBool(featureFlags & kSampleRateShading_GrVkFeatureFlag); |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 124 | } |
| 125 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 126 | void GrVkCaps::initGLSLCaps(const VkPhysicalDeviceProperties& properties, |
| 127 | uint32_t featureFlags) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 128 | GrGLSLCaps* glslCaps = static_cast<GrGLSLCaps*>(fShaderCaps.get()); |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 129 | glslCaps->fVersionDeclString = "#version 310 es\n"; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 130 | |
| 131 | // fConfigOutputSwizzle will default to RGBA so we only need to set it for alpha only config. |
| 132 | for (int i = 0; i < kGrPixelConfigCnt; ++i) { |
| 133 | GrPixelConfig config = static_cast<GrPixelConfig>(i); |
| 134 | if (GrPixelConfigIsAlphaOnly(config)) { |
| 135 | glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RRRR(); |
| 136 | glslCaps->fConfigOutputSwizzle[i] = GrSwizzle::AAAA(); |
| 137 | } else { |
| 138 | glslCaps->fConfigTextureSwizzle[i] = GrSwizzle::RGBA(); |
| 139 | } |
| 140 | } |
egdaniel | 67acb83 | 2016-02-26 08:32:20 -0800 | [diff] [blame] | 141 | |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 142 | // Vulkan is based off ES 3.0 so the following should all be supported |
| 143 | glslCaps->fUsesPrecisionModifiers = true; |
| 144 | glslCaps->fFlatInterpolationSupport = true; |
| 145 | |
| 146 | // GrShaderCaps |
| 147 | |
egdaniel | 67acb83 | 2016-02-26 08:32:20 -0800 | [diff] [blame] | 148 | glslCaps->fShaderDerivativeSupport = true; |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 149 | glslCaps->fGeometryShaderSupport = SkToBool(featureFlags & kGeometryShader_GrVkFeatureFlag); |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 150 | #if 0 |
| 151 | // For now disabling dual source blending till we get it hooked up in the rest of system |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 152 | glslCaps->fDualSourceBlendingSupport = SkToBool(featureFlags & kDualSrcBlend_GrVkFeatureFlag); |
egdaniel | d5e3b9e | 2016-03-08 12:19:54 -0800 | [diff] [blame] | 153 | #endif |
| 154 | glslCaps->fIntegerSupport = true; |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 155 | |
| 156 | glslCaps->fMaxVertexSamplers = |
| 157 | glslCaps->fMaxGeometrySamplers = |
| 158 | glslCaps->fMaxFragmentSamplers = SkTMin(properties.limits.maxPerStageDescriptorSampledImages, |
| 159 | properties.limits.maxPerStageDescriptorSamplers); |
| 160 | glslCaps->fMaxCombinedSamplers = SkTMin(properties.limits.maxDescriptorSetSampledImages, |
| 161 | properties.limits.maxDescriptorSetSamplers); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void format_supported_for_feature(const GrVkInterface* interface, |
| 165 | VkPhysicalDevice physDev, |
| 166 | VkFormat format, |
| 167 | VkFormatFeatureFlagBits featureBit, |
| 168 | bool* linearSupport, |
| 169 | bool* optimalSupport) { |
| 170 | VkFormatProperties props; |
| 171 | memset(&props, 0, sizeof(VkFormatProperties)); |
| 172 | GR_VK_CALL(interface, GetPhysicalDeviceFormatProperties(physDev, format, &props)); |
| 173 | *linearSupport = SkToBool(props.linearTilingFeatures & featureBit); |
| 174 | *optimalSupport = SkToBool(props.optimalTilingFeatures & featureBit); |
| 175 | } |
| 176 | |
| 177 | static void config_supported_for_feature(const GrVkInterface* interface, |
| 178 | VkPhysicalDevice physDev, |
| 179 | GrPixelConfig config, |
| 180 | VkFormatFeatureFlagBits featureBit, |
| 181 | bool* linearSupport, |
| 182 | bool* optimalSupport) { |
| 183 | VkFormat format; |
| 184 | if (!GrPixelConfigToVkFormat(config, &format)) { |
| 185 | *linearSupport = false; |
| 186 | *optimalSupport = false; |
| 187 | return; |
| 188 | } |
| 189 | format_supported_for_feature(interface, physDev, format, featureBit, |
| 190 | linearSupport, optimalSupport); |
| 191 | } |
| 192 | |
| 193 | // Currently just assumeing if something can be rendered to without MSAA it also works for MSAAA |
| 194 | #define SET_CONFIG_IS_RENDERABLE(config) \ |
| 195 | config_supported_for_feature(interface, \ |
| 196 | physDev, \ |
| 197 | config, \ |
| 198 | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT, \ |
| 199 | &fConfigLinearRenderSupport[config][kNo_MSAA], \ |
| 200 | &fConfigRenderSupport[config][kNo_MSAA] ); \ |
| 201 | fConfigRenderSupport[config][kYes_MSAA] = fConfigRenderSupport[config][kNo_MSAA]; \ |
| 202 | fConfigLinearRenderSupport[config][kYes_MSAA] = fConfigLinearRenderSupport[config][kNo_MSAA]; |
| 203 | |
| 204 | |
| 205 | void GrVkCaps::initConfigRenderableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) { |
| 206 | enum { |
| 207 | kNo_MSAA = 0, |
| 208 | kYes_MSAA = 1, |
| 209 | }; |
| 210 | |
| 211 | // Base render support |
| 212 | SET_CONFIG_IS_RENDERABLE(kAlpha_8_GrPixelConfig); |
| 213 | SET_CONFIG_IS_RENDERABLE(kRGB_565_GrPixelConfig); |
| 214 | SET_CONFIG_IS_RENDERABLE(kRGBA_4444_GrPixelConfig); |
| 215 | SET_CONFIG_IS_RENDERABLE(kRGBA_8888_GrPixelConfig); |
| 216 | SET_CONFIG_IS_RENDERABLE(kBGRA_8888_GrPixelConfig); |
| 217 | |
| 218 | SET_CONFIG_IS_RENDERABLE(kSRGBA_8888_GrPixelConfig); |
| 219 | |
| 220 | // Float render support |
| 221 | SET_CONFIG_IS_RENDERABLE(kRGBA_float_GrPixelConfig); |
| 222 | SET_CONFIG_IS_RENDERABLE(kRGBA_half_GrPixelConfig); |
| 223 | SET_CONFIG_IS_RENDERABLE(kAlpha_half_GrPixelConfig); |
| 224 | } |
| 225 | |
| 226 | #define SET_CONFIG_IS_TEXTURABLE(config) \ |
| 227 | config_supported_for_feature(interface, \ |
| 228 | physDev, \ |
| 229 | config, \ |
| 230 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, \ |
| 231 | &fConfigLinearTextureSupport[config], \ |
| 232 | &fConfigTextureSupport[config]); |
| 233 | |
| 234 | void GrVkCaps::initConfigTexturableTable(const GrVkInterface* interface, VkPhysicalDevice physDev) { |
| 235 | // Base texture support |
| 236 | SET_CONFIG_IS_TEXTURABLE(kAlpha_8_GrPixelConfig); |
| 237 | SET_CONFIG_IS_TEXTURABLE(kRGB_565_GrPixelConfig); |
| 238 | SET_CONFIG_IS_TEXTURABLE(kRGBA_4444_GrPixelConfig); |
| 239 | SET_CONFIG_IS_TEXTURABLE(kRGBA_8888_GrPixelConfig); |
| 240 | SET_CONFIG_IS_TEXTURABLE(kBGRA_8888_GrPixelConfig); |
| 241 | |
| 242 | SET_CONFIG_IS_TEXTURABLE(kIndex_8_GrPixelConfig); |
| 243 | SET_CONFIG_IS_TEXTURABLE(kSRGBA_8888_GrPixelConfig); |
| 244 | |
| 245 | // Compressed texture support |
| 246 | SET_CONFIG_IS_TEXTURABLE(kETC1_GrPixelConfig); |
| 247 | SET_CONFIG_IS_TEXTURABLE(kLATC_GrPixelConfig); |
| 248 | SET_CONFIG_IS_TEXTURABLE(kR11_EAC_GrPixelConfig); |
| 249 | SET_CONFIG_IS_TEXTURABLE(kASTC_12x12_GrPixelConfig); |
| 250 | |
| 251 | // Float texture support |
| 252 | SET_CONFIG_IS_TEXTURABLE(kRGBA_float_GrPixelConfig); |
| 253 | SET_CONFIG_IS_TEXTURABLE(kRGBA_half_GrPixelConfig); |
| 254 | SET_CONFIG_IS_TEXTURABLE(kAlpha_half_GrPixelConfig); |
| 255 | } |
| 256 | |
| 257 | #define SET_CONFIG_CAN_STENCIL(config) \ |
| 258 | bool SK_MACRO_APPEND_LINE(linearSupported); \ |
| 259 | bool SK_MACRO_APPEND_LINE(optimalSupported); \ |
| 260 | format_supported_for_feature(interface, \ |
| 261 | physDev, \ |
| 262 | config.fInternalFormat, \ |
| 263 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, \ |
| 264 | &SK_MACRO_APPEND_LINE(linearSupported), \ |
| 265 | &SK_MACRO_APPEND_LINE(optimalSupported)); \ |
| 266 | if (SK_MACRO_APPEND_LINE(linearSupported)) fLinearStencilFormats.push_back(config); \ |
| 267 | if (SK_MACRO_APPEND_LINE(optimalSupported)) fStencilFormats.push_back(config); |
| 268 | |
| 269 | void GrVkCaps::initStencilFormats(const GrVkInterface* interface, VkPhysicalDevice physDev) { |
| 270 | // Build up list of legal stencil formats (though perhaps not supported on |
| 271 | // the particular gpu/driver) from most preferred to least. |
| 272 | |
| 273 | static const StencilFormat |
| 274 | // internal Format stencil bits total bits packed? |
| 275 | gS8 = { VK_FORMAT_S8_UINT, 8, 8, false }, |
| 276 | gD24S8 = { VK_FORMAT_D24_UNORM_S8_UINT, 8, 32, true }; |
| 277 | |
| 278 | // I'm simply assuming that these two will be supported since they are used in example code. |
| 279 | // TODO: Actaully figure this out |
| 280 | SET_CONFIG_CAN_STENCIL(gS8); |
| 281 | SET_CONFIG_CAN_STENCIL(gD24S8); |
| 282 | } |
| 283 | |