bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrGLCaps.h" |
| 11 | #include "GrGLContextInfo.h" |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 12 | #include "SkTSearch.h" |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 13 | |
| 14 | GrGLCaps::GrGLCaps() { |
| 15 | this->reset(); |
| 16 | } |
| 17 | |
| 18 | void GrGLCaps::reset() { |
| 19 | fVerifiedColorConfigs.reset(); |
| 20 | fStencilFormats.reset(); |
| 21 | fStencilVerifiedColorConfigs.reset(); |
| 22 | fMSFBOType = kNone_MSFBOType; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 23 | fCoverageAAType = kNone_CoverageAAType; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 24 | fMaxFragmentUniformVectors = 0; |
| 25 | fRGBA8RenderbufferSupport = false; |
| 26 | fBGRAFormatSupport = false; |
| 27 | fBGRAIsInternalFormat = false; |
| 28 | fTextureSwizzleSupport = false; |
| 29 | fUnpackRowLengthSupport = false; |
| 30 | fUnpackFlipYSupport = false; |
| 31 | fPackRowLengthSupport = false; |
| 32 | fPackFlipYSupport = false; |
| 33 | fTextureUsageSupport = false; |
| 34 | fTexStorageSupport = false; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 35 | fTextureRedSupport = false; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | GrGLCaps::GrGLCaps(const GrGLCaps& caps) { |
| 39 | *this = caps; |
| 40 | } |
| 41 | |
| 42 | GrGLCaps& GrGLCaps::operator = (const GrGLCaps& caps) { |
| 43 | fVerifiedColorConfigs = caps.fVerifiedColorConfigs; |
| 44 | fStencilFormats = caps.fStencilFormats; |
| 45 | fStencilVerifiedColorConfigs = caps.fStencilVerifiedColorConfigs; |
| 46 | fMaxFragmentUniformVectors = caps.fMaxFragmentUniformVectors; |
| 47 | fMSFBOType = caps.fMSFBOType; |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 48 | fCoverageAAType = caps.fCoverageAAType; |
| 49 | fMSAACoverageModes = caps.fMSAACoverageModes; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 50 | fRGBA8RenderbufferSupport = caps.fRGBA8RenderbufferSupport; |
| 51 | fBGRAFormatSupport = caps.fBGRAFormatSupport; |
| 52 | fBGRAIsInternalFormat = caps.fBGRAIsInternalFormat; |
| 53 | fTextureSwizzleSupport = caps.fTextureSwizzleSupport; |
| 54 | fUnpackRowLengthSupport = caps.fUnpackRowLengthSupport; |
| 55 | fUnpackFlipYSupport = caps.fUnpackFlipYSupport; |
| 56 | fPackRowLengthSupport = caps.fPackRowLengthSupport; |
| 57 | fPackFlipYSupport = caps.fPackFlipYSupport; |
| 58 | fTextureUsageSupport = caps.fTextureUsageSupport; |
| 59 | fTexStorageSupport = caps.fTexStorageSupport; |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 60 | fTextureRedSupport = caps.fTextureRedSupport; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 61 | |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | void GrGLCaps::init(const GrGLContextInfo& ctxInfo) { |
| 66 | |
| 67 | this->reset(); |
| 68 | if (!ctxInfo.isInitialized()) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const GrGLInterface* gli = ctxInfo.interface(); |
| 73 | GrGLBinding binding = ctxInfo.binding(); |
| 74 | GrGLVersion version = ctxInfo.version(); |
| 75 | |
| 76 | if (kES2_GrGLBinding == binding) { |
| 77 | GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS, |
| 78 | &fMaxFragmentUniformVectors); |
| 79 | } else { |
| 80 | GrAssert(kDesktop_GrGLBinding == binding); |
| 81 | GrGLint max; |
| 82 | GR_GL_GetIntegerv(gli, GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &max); |
| 83 | fMaxFragmentUniformVectors = max / 4; |
| 84 | } |
| 85 | |
| 86 | if (kDesktop_GrGLBinding == binding) { |
| 87 | fRGBA8RenderbufferSupport = true; |
| 88 | } else { |
| 89 | fRGBA8RenderbufferSupport = ctxInfo.hasExtension("GL_OES_rgb8_rgba8") || |
| 90 | ctxInfo.hasExtension("GL_ARM_rgba8"); |
| 91 | } |
| 92 | |
| 93 | if (kDesktop_GrGLBinding == binding) { |
| 94 | fBGRAFormatSupport = version >= GR_GL_VER(1,2) || |
| 95 | ctxInfo.hasExtension("GL_EXT_bgra"); |
| 96 | } else { |
| 97 | bool hasBGRAExt = false; |
| 98 | if (ctxInfo.hasExtension("GL_APPLE_texture_format_BGRA8888")) { |
| 99 | fBGRAFormatSupport = true; |
| 100 | } else if (ctxInfo.hasExtension("GL_EXT_texture_format_BGRA8888")) { |
| 101 | fBGRAFormatSupport = true; |
| 102 | fBGRAIsInternalFormat = true; |
| 103 | } |
| 104 | GrAssert(fBGRAFormatSupport || |
| 105 | kSkia8888_PM_GrPixelConfig != kBGRA_8888_PM_GrPixelConfig); |
| 106 | } |
| 107 | |
| 108 | if (kDesktop_GrGLBinding == binding) { |
| 109 | fTextureSwizzleSupport = version >= GR_GL_VER(3,3) || |
| 110 | ctxInfo.hasExtension("GL_ARB_texture_swizzle"); |
| 111 | } else { |
| 112 | fTextureSwizzleSupport = false; |
| 113 | } |
| 114 | |
| 115 | if (kDesktop_GrGLBinding == binding) { |
| 116 | fUnpackRowLengthSupport = true; |
| 117 | fUnpackFlipYSupport = false; |
| 118 | fPackRowLengthSupport = true; |
| 119 | fPackFlipYSupport = false; |
| 120 | } else { |
| 121 | fUnpackRowLengthSupport =ctxInfo.hasExtension("GL_EXT_unpack_subimage"); |
| 122 | fUnpackFlipYSupport = ctxInfo.hasExtension("GL_CHROMIUM_flipy"); |
| 123 | // no extension for pack row length |
| 124 | fPackRowLengthSupport = false; |
| 125 | fPackFlipYSupport = |
| 126 | ctxInfo.hasExtension("GL_ANGLE_pack_reverse_row_order"); |
| 127 | } |
| 128 | |
| 129 | fTextureUsageSupport = (kES2_GrGLBinding == binding) && |
| 130 | ctxInfo.hasExtension("GL_ANGLE_texture_usage"); |
| 131 | |
| 132 | // Tex storage is in desktop 4.2 and can be an extension to desktop or ES. |
| 133 | fTexStorageSupport = (kDesktop_GrGLBinding == binding && |
| 134 | version >= GR_GL_VER(4,2)) || |
| 135 | ctxInfo.hasExtension("GL_ARB_texture_storage") || |
| 136 | ctxInfo.hasExtension("GL_EXT_texture_storage"); |
| 137 | |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 138 | // ARB_texture_rg is part of OpenGL 3.0 |
| 139 | if (kDesktop_GrGLBinding == binding) { |
| 140 | fTextureRedSupport = version >= GR_GL_VER(3,0) || |
| 141 | ctxInfo.hasExtension("GL_ARB_texture_rg"); |
| 142 | } else { |
robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 143 | fTextureRedSupport = ctxInfo.hasExtension("GL_EXT_texture_rg"); |
robertphillips@google.com | 6995068 | 2012-04-06 18:06:10 +0000 | [diff] [blame] | 144 | } |
| 145 | |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 146 | this->initFSAASupport(ctxInfo); |
| 147 | this->initStencilFormats(ctxInfo); |
| 148 | } |
| 149 | |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 150 | namespace { |
| 151 | int coverage_mode_compare(const GrGLCaps::MSAACoverageMode* left, |
| 152 | const GrGLCaps::MSAACoverageMode* right) { |
| 153 | if (left->fCoverageSampleCnt < right->fCoverageSampleCnt) { |
| 154 | return -1; |
| 155 | } else if (right->fCoverageSampleCnt < left->fCoverageSampleCnt) { |
| 156 | return 1; |
| 157 | } else if (left->fColorSampleCnt < right->fColorSampleCnt) { |
| 158 | return -1; |
| 159 | } else if (right->fColorSampleCnt < left->fColorSampleCnt) { |
| 160 | return 1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | } |
| 165 | |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 166 | void GrGLCaps::initFSAASupport(const GrGLContextInfo& ctxInfo) { |
| 167 | |
| 168 | fMSFBOType = kNone_MSFBOType; |
| 169 | if (kDesktop_GrGLBinding != ctxInfo.binding()) { |
| 170 | if (ctxInfo.hasExtension("GL_CHROMIUM_framebuffer_multisample")) { |
| 171 | // chrome's extension is equivalent to the EXT msaa |
| 172 | // and fbo_blit extensions. |
| 173 | fMSFBOType = kDesktopEXT_MSFBOType; |
| 174 | } else if (ctxInfo.hasExtension("GL_APPLE_framebuffer_multisample")) { |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 175 | fMSFBOType = kAppleES_MSFBOType; |
| 176 | } |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 177 | } else { |
| 178 | if ((ctxInfo.version() >= GR_GL_VER(3,0)) || |
| 179 | ctxInfo.hasExtension("GL_ARB_framebuffer_object")) { |
| 180 | fMSFBOType = GrGLCaps::kDesktopARB_MSFBOType; |
| 181 | } else if (ctxInfo.hasExtension("GL_EXT_framebuffer_multisample") && |
| 182 | ctxInfo.hasExtension("GL_EXT_framebuffer_blit")) { |
| 183 | fMSFBOType = GrGLCaps::kDesktopEXT_MSFBOType; |
| 184 | } |
bsalomon@google.com | c9668ec | 2012-04-11 18:16:41 +0000 | [diff] [blame^] | 185 | // TODO: We could populate fMSAACoverageModes using GetInternalformativ |
| 186 | // on GL 4.2+. It's format-specific, though. See also |
| 187 | // http://code.google.com/p/skia/issues/detail?id=470 about using actual |
| 188 | // rather than requested sample counts in cache key. |
| 189 | if (ctxInfo.hasExtension("GL_NV_framebuffer_multisample_coverage")) { |
| 190 | fCoverageAAType = kNVDesktop_CoverageAAType; |
| 191 | GrGLint count; |
| 192 | GR_GL_GetIntegerv(ctxInfo.interface(), |
| 193 | GR_GL_MAX_MULTISAMPLE_COVERAGE_MODES, |
| 194 | &count); |
| 195 | fMSAACoverageModes.setCount(count); |
| 196 | GR_GL_GetIntegerv(ctxInfo.interface(), |
| 197 | GR_GL_MULTISAMPLE_COVERAGE_MODES, |
| 198 | (int*)&fMSAACoverageModes[0]); |
| 199 | // The NV driver seems to return the modes already sorted but the |
| 200 | // spec doesn't require this. So we sort. |
| 201 | SkQSortCompareProc compareProc = |
| 202 | reinterpret_cast<SkQSortCompareProc>(&coverage_mode_compare); |
| 203 | SkQSort(&fMSAACoverageModes[0], |
| 204 | count, |
| 205 | sizeof(MSAACoverageMode), |
| 206 | compareProc); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | const GrGLCaps::MSAACoverageMode& GrGLCaps::getMSAACoverageMode( |
| 212 | int desiredSampleCount) const { |
| 213 | static const MSAACoverageMode kNoneMode = {0, 0}; |
| 214 | if (0 == fMSAACoverageModes.count()) { |
| 215 | return kNoneMode; |
| 216 | } else { |
| 217 | GrAssert(kNone_CoverageAAType != fCoverageAAType); |
| 218 | int max = (fMSAACoverageModes.end() - 1)->fCoverageSampleCnt; |
| 219 | desiredSampleCount = GrMin(desiredSampleCount, max); |
| 220 | MSAACoverageMode desiredMode = {desiredSampleCount, 0}; |
| 221 | int idx = SkTSearch<MSAACoverageMode>(&fMSAACoverageModes[0], |
| 222 | fMSAACoverageModes.count(), |
| 223 | desiredMode, |
| 224 | sizeof(MSAACoverageMode), |
| 225 | &coverage_mode_compare); |
| 226 | if (idx < 0) { |
| 227 | idx = ~idx; |
| 228 | } |
| 229 | GrAssert(idx >= 0 && idx < fMSAACoverageModes.count()); |
| 230 | return fMSAACoverageModes[idx]; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | namespace { |
| 235 | const GrGLuint kUnknownBitCount = GrGLStencilBuffer::kUnknownBitCount; |
| 236 | } |
| 237 | |
| 238 | void GrGLCaps::initStencilFormats(const GrGLContextInfo& ctxInfo) { |
| 239 | |
| 240 | // Build up list of legal stencil formats (though perhaps not supported on |
| 241 | // the particular gpu/driver) from most preferred to least. |
| 242 | |
| 243 | // these consts are in order of most preferred to least preferred |
| 244 | // we don't bother with GL_STENCIL_INDEX1 or GL_DEPTH32F_STENCIL8 |
| 245 | |
| 246 | static const StencilFormat |
| 247 | // internal Format stencil bits total bits packed? |
| 248 | gS8 = {GR_GL_STENCIL_INDEX8, 8, 8, false}, |
| 249 | gS16 = {GR_GL_STENCIL_INDEX16, 16, 16, false}, |
| 250 | gD24S8 = {GR_GL_DEPTH24_STENCIL8, 8, 32, true }, |
| 251 | gS4 = {GR_GL_STENCIL_INDEX4, 4, 4, false}, |
| 252 | gS = {GR_GL_STENCIL_INDEX, kUnknownBitCount, kUnknownBitCount, false}, |
| 253 | gDS = {GR_GL_DEPTH_STENCIL, kUnknownBitCount, kUnknownBitCount, true }; |
| 254 | |
| 255 | if (kDesktop_GrGLBinding == ctxInfo.binding()) { |
| 256 | bool supportsPackedDS = |
| 257 | ctxInfo.version() >= GR_GL_VER(3,0) || |
| 258 | ctxInfo.hasExtension("GL_EXT_packed_depth_stencil") || |
| 259 | ctxInfo.hasExtension("GL_ARB_framebuffer_object"); |
| 260 | |
| 261 | // S1 thru S16 formats are in GL 3.0+, EXT_FBO, and ARB_FBO since we |
| 262 | // require FBO support we can expect these are legal formats and don't |
| 263 | // check. These also all support the unsized GL_STENCIL_INDEX. |
| 264 | fStencilFormats.push_back() = gS8; |
| 265 | fStencilFormats.push_back() = gS16; |
| 266 | if (supportsPackedDS) { |
| 267 | fStencilFormats.push_back() = gD24S8; |
| 268 | } |
| 269 | fStencilFormats.push_back() = gS4; |
| 270 | if (supportsPackedDS) { |
| 271 | fStencilFormats.push_back() = gDS; |
| 272 | } |
| 273 | } else { |
| 274 | // ES2 has STENCIL_INDEX8 without extensions but requires extensions |
| 275 | // for other formats. |
| 276 | // ES doesn't support using the unsized format. |
| 277 | |
| 278 | fStencilFormats.push_back() = gS8; |
| 279 | //fStencilFormats.push_back() = gS16; |
| 280 | if (ctxInfo.hasExtension("GL_OES_packed_depth_stencil")) { |
| 281 | fStencilFormats.push_back() = gD24S8; |
| 282 | } |
| 283 | if (ctxInfo.hasExtension("GL_OES_stencil4")) { |
| 284 | fStencilFormats.push_back() = gS4; |
| 285 | } |
| 286 | } |
| 287 | GrAssert(0 == fStencilVerifiedColorConfigs.count()); |
| 288 | fStencilVerifiedColorConfigs.push_back_n(fStencilFormats.count()); |
| 289 | } |
| 290 | |
| 291 | void GrGLCaps::markColorConfigAndStencilFormatAsVerified( |
| 292 | GrPixelConfig config, |
| 293 | const GrGLStencilBuffer::Format& format) { |
| 294 | #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT |
| 295 | return; |
| 296 | #endif |
| 297 | GrAssert((unsigned)config < kGrPixelConfigCount); |
| 298 | GrAssert(fStencilFormats.count() == fStencilVerifiedColorConfigs.count()); |
| 299 | int count = fStencilFormats.count(); |
| 300 | // we expect a really small number of possible formats so linear search |
| 301 | // should be OK |
| 302 | GrAssert(count < 16); |
| 303 | for (int i = 0; i < count; ++i) { |
| 304 | if (format.fInternalFormat == |
| 305 | fStencilFormats[i].fInternalFormat) { |
| 306 | fStencilVerifiedColorConfigs[i].markVerified(config); |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | GrCrash("Why are we seeing a stencil format that " |
| 311 | "GrGLCaps doesn't know about."); |
| 312 | } |
| 313 | |
| 314 | bool GrGLCaps::isColorConfigAndStencilFormatVerified( |
| 315 | GrPixelConfig config, |
| 316 | const GrGLStencilBuffer::Format& format) const { |
| 317 | #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT |
| 318 | return false; |
| 319 | #endif |
| 320 | GrAssert((unsigned)config < kGrPixelConfigCount); |
| 321 | int count = fStencilFormats.count(); |
| 322 | // we expect a really small number of possible formats so linear search |
| 323 | // should be OK |
| 324 | GrAssert(count < 16); |
| 325 | for (int i = 0; i < count; ++i) { |
| 326 | if (format.fInternalFormat == |
| 327 | fStencilFormats[i].fInternalFormat) { |
| 328 | return fStencilVerifiedColorConfigs[i].isVerified(config); |
| 329 | } |
| 330 | } |
| 331 | GrCrash("Why are we seeing a stencil format that " |
| 332 | "GLCaps doesn't know about."); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | void GrGLCaps::print() const { |
| 337 | for (int i = 0; i < fStencilFormats.count(); ++i) { |
| 338 | GrPrintf("Stencil Format %d, stencil bits: %02d, total bits: %02d\n", |
| 339 | i, |
| 340 | fStencilFormats[i].fStencilBits, |
| 341 | fStencilFormats[i].fTotalBits); |
| 342 | } |
| 343 | |
| 344 | GR_STATIC_ASSERT(0 == kNone_MSFBOType); |
| 345 | GR_STATIC_ASSERT(1 == kDesktopARB_MSFBOType); |
| 346 | GR_STATIC_ASSERT(2 == kDesktopEXT_MSFBOType); |
| 347 | GR_STATIC_ASSERT(3 == kAppleES_MSFBOType); |
| 348 | static const char* gMSFBOExtStr[] = { |
| 349 | "None", |
| 350 | "ARB", |
| 351 | "EXT", |
| 352 | "Apple", |
| 353 | }; |
| 354 | GrPrintf("MSAA Type: %s\n", gMSFBOExtStr[fMSFBOType]); |
| 355 | GrPrintf("Max FS Uniform Vectors: %d\n", fMaxFragmentUniformVectors); |
| 356 | GrPrintf("Support RGBA8 Render Buffer: %s\n", |
| 357 | (fRGBA8RenderbufferSupport ? "YES": "NO")); |
| 358 | GrPrintf("BGRA is an internal format: %s\n", |
| 359 | (fBGRAIsInternalFormat ? "YES": "NO")); |
| 360 | GrPrintf("Support texture swizzle: %s\n", |
| 361 | (fTextureSwizzleSupport ? "YES": "NO")); |
| 362 | GrPrintf("Unpack Row length support: %s\n", |
| 363 | (fUnpackRowLengthSupport ? "YES": "NO")); |
| 364 | GrPrintf("Unpack Flip Y support: %s\n", |
| 365 | (fUnpackFlipYSupport ? "YES": "NO")); |
| 366 | GrPrintf("Pack Row length support: %s\n", |
| 367 | (fPackRowLengthSupport ? "YES": "NO")); |
| 368 | GrPrintf("Pack Flip Y support: %s\n", |
| 369 | (fPackFlipYSupport ? "YES": "NO")); |
| 370 | } |
| 371 | |