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