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