1 /* 2 * Copyright 2012 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 9 #ifndef GrGLCaps_DEFINED 10 #define GrGLCaps_DEFINED 11 12 #include "GrDrawTargetCaps.h" 13 #include "GrGLStencilBuffer.h" 14 #include "SkTArray.h" 15 #include "SkTDArray.h" 16 17 class GrGLContextInfo; 18 19 /** 20 * Stores some capabilities of a GL context. Most are determined by the GL 21 * version and the extensions string. It also tracks formats that have passed 22 * the FBO completeness test. 23 */ 24 class GrGLCaps : public GrDrawTargetCaps { 25 public: 26 SK_DECLARE_INST_COUNT(GrGLCaps) 27 28 typedef GrGLStencilBuffer::Format StencilFormat; 29 30 /** 31 * Represents a supported multisampling/coverage-sampling mode. 32 */ 33 struct MSAACoverageMode { 34 // "Coverage samples" includes samples that actually have color, depth, 35 // stencil, ... as well as those that don't (coverage only). All samples 36 // are coverage samples. (We're using the word "coverage sample" to 37 // match the NV extension language.) 38 int fCoverageSampleCnt; 39 40 // Color samples are samples that store data values (color, stencil, 41 // depth) rather than just representing coverage. They are a subset 42 // of coverage samples. (Again the wording was chosen to match the 43 // extension.) 44 int fColorSampleCnt; 45 }; 46 47 /** 48 * The type of MSAA for FBOs supported. Different extensions have different 49 * semantics of how / when a resolve is performed. 50 */ 51 enum MSFBOType { 52 /** 53 * no support for MSAA FBOs 54 */ 55 kNone_MSFBOType = 0, 56 /** 57 * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object) 58 */ 59 kDesktop_ARB_MSFBOType, 60 /** 61 * earlier GL_EXT_framebuffer* extensions 62 */ 63 kDesktop_EXT_MSFBOType, 64 /** 65 * GL_APPLE_framebuffer_multisample ES extension 66 */ 67 kES_Apple_MSFBOType, 68 /** 69 * GL_IMG_multisampled_render_to_texture. This variation does not have MSAA renderbuffers. 70 * Instead the texture is multisampled when bound to the FBO and then resolved automatically 71 * when read. It also defines an alternate value for GL_MAX_SAMPLES (which we call 72 * GR_GL_MAX_SAMPLES_IMG). 73 */ 74 kES_IMG_MsToTexture_MSFBOType, 75 /** 76 * GL_EXT_multisampled_render_to_texture. Same as the IMG one above but uses the standard 77 * GL_MAX_SAMPLES value. 78 */ 79 kES_EXT_MsToTexture_MSFBOType, 80 81 kLast_MSFBOType = kES_EXT_MsToTexture_MSFBOType 82 }; 83 84 enum FBFetchType { 85 kNone_FBFetchType, 86 /** GL_EXT_shader_framebuffer_fetch */ 87 kEXT_FBFetchType, 88 /** GL_NV_shader_framebuffer_fetch */ 89 kNV_FBFetchType, 90 91 kLast_FBFetchType = kNV_FBFetchType, 92 }; 93 94 enum CoverageAAType { 95 /** 96 * No coverage sample support 97 */ 98 kNone_CoverageAAType, 99 100 /** 101 * GL_NV_framebuffer_multisample_coverage 102 */ 103 kNVDesktop_CoverageAAType, 104 }; 105 106 /** 107 * Creates a GrGLCaps that advertises no support for any extensions, 108 * formats, etc. Call init to initialize from a GrGLContextInfo. 109 */ 110 GrGLCaps(); 111 112 GrGLCaps(const GrGLCaps& caps); 113 114 GrGLCaps& operator = (const GrGLCaps& caps); 115 116 /** 117 * Resets the caps such that nothing is supported. 118 */ 119 virtual void reset() SK_OVERRIDE; 120 121 /** 122 * Initializes the GrGLCaps to the set of features supported in the current 123 * OpenGL context accessible via ctxInfo. 124 */ 125 void init(const GrGLContextInfo& ctxInfo, const GrGLInterface* interface); 126 127 /** 128 * Call to note that a color config has been verified as a valid color 129 * attachment. This may save future calls to glCheckFramebufferStatus 130 * using isConfigVerifiedColorAttachment(). 131 */ 132 void markConfigAsValidColorAttachment(GrPixelConfig config) { 133 fVerifiedColorConfigs.markVerified(config); 134 } 135 136 /** 137 * Call to check whether a config has been verified as a valid color 138 * attachment. 139 */ 140 bool isConfigVerifiedColorAttachment(GrPixelConfig config) const { 141 return fVerifiedColorConfigs.isVerified(config); 142 } 143 144 /** 145 * Call to note that a color config / stencil format pair passed 146 * FBO status check. We may skip calling glCheckFramebufferStatus for 147 * this combination in the future using 148 * isColorConfigAndStencilFormatVerified(). 149 */ 150 void markColorConfigAndStencilFormatAsVerified( 151 GrPixelConfig config, 152 const GrGLStencilBuffer::Format& format); 153 154 /** 155 * Call to check whether color config / stencil format pair has already 156 * passed FBO status check. 157 */ 158 bool isColorConfigAndStencilFormatVerified( 159 GrPixelConfig config, 160 const GrGLStencilBuffer::Format& format) const; 161 162 /** 163 * Reports the type of MSAA FBO support. 164 */ 165 MSFBOType msFBOType() const { return fMSFBOType; } 166 167 /** 168 * Does the supported MSAA FBO extension have MSAA renderbuffers? 169 */ 170 bool usesMSAARenderBuffers() const { 171 return kNone_MSFBOType != fMSFBOType && 172 kES_IMG_MsToTexture_MSFBOType != fMSFBOType && 173 kES_EXT_MsToTexture_MSFBOType != fMSFBOType; 174 } 175 176 /** 177 * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and 178 * then implicitly resolved when read. 179 */ 180 bool usesImplicitMSAAResolve() const { 181 return kES_IMG_MsToTexture_MSFBOType == fMSFBOType || 182 kES_EXT_MsToTexture_MSFBOType == fMSFBOType; 183 } 184 185 /** 186 * Reports the type of coverage sample AA support. 187 */ 188 CoverageAAType coverageAAType() const { return fCoverageAAType; } 189 190 /** 191 * Chooses a supported coverage mode based on a desired sample count. The 192 * desired sample count is rounded up the next supported coverage sample 193 * count unless a it is larger than the max in which case it is rounded 194 * down. Once a coverage sample count is decided, the supported mode with 195 * the fewest color samples is chosen. 196 */ 197 const MSAACoverageMode& getMSAACoverageMode(int desiredSampleCount) const; 198 199 FBFetchType fbFetchType() const { return fFBFetchType; } 200 201 /** 202 * Prints the caps info using GrPrintf. 203 */ 204 virtual void print() const SK_OVERRIDE; 205 206 /** 207 * Gets an array of legal stencil formats. These formats are not guaranteed 208 * to be supported by the driver but are legal GLenum names given the GL 209 * version and extensions supported. 210 */ 211 const SkTArray<StencilFormat, true>& stencilFormats() const { 212 return fStencilFormats; 213 } 214 215 /// The maximum number of fragment uniform vectors (GLES has min. 16). 216 int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; } 217 218 /// maximum number of attribute values per vertex 219 int maxVertexAttributes() const { return fMaxVertexAttributes; } 220 221 /// maximum number of texture units accessible in the fragment shader. 222 int maxFragmentTextureUnits() const { return fMaxFragmentTextureUnits; } 223 224 /// ES requires an extension to support RGBA8 in RenderBufferStorage 225 bool rgba8RenderbufferSupport() const { return fRGBA8RenderbufferSupport; } 226 227 /// Is GL_BGRA supported 228 bool bgraFormatSupport() const { return fBGRAFormatSupport; } 229 230 /** 231 * Depending on the ES extensions present the BGRA external format may 232 * correspond either a BGRA or RGBA internalFormat. On desktop GL it is 233 * RGBA. 234 */ 235 bool bgraIsInternalFormat() const { return fBGRAIsInternalFormat; } 236 237 /// GL_ARB_texture_swizzle support 238 bool textureSwizzleSupport() const { return fTextureSwizzleSupport; } 239 240 /// Is there support for GL_UNPACK_ROW_LENGTH 241 bool unpackRowLengthSupport() const { return fUnpackRowLengthSupport; } 242 243 /// Is there support for GL_UNPACK_FLIP_Y 244 bool unpackFlipYSupport() const { return fUnpackFlipYSupport; } 245 246 /// Is there support for GL_PACK_ROW_LENGTH 247 bool packRowLengthSupport() const { return fPackRowLengthSupport; } 248 249 /// Is there support for GL_PACK_REVERSE_ROW_ORDER 250 bool packFlipYSupport() const { return fPackFlipYSupport; } 251 252 /// Is there support for texture parameter GL_TEXTURE_USAGE 253 bool textureUsageSupport() const { return fTextureUsageSupport; } 254 255 /// Is there support for glTexStorage 256 bool texStorageSupport() const { return fTexStorageSupport; } 257 258 /// Is there support for GL_RED and GL_R8 259 bool textureRedSupport() const { return fTextureRedSupport; } 260 261 /// Is GL_ARB_IMAGING supported 262 bool imagingSupport() const { return fImagingSupport; } 263 264 /// Is GL_ARB_fragment_coord_conventions supported? 265 bool fragCoordConventionsSupport() const { return fFragCoordsConventionSupport; } 266 267 /// Is there support for Vertex Array Objects? 268 bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; } 269 270 /// Use indices or vertices in CPU arrays rather than VBOs for dynamic content. 271 bool useNonVBOVertexAndIndexDynamicData() const { 272 return fUseNonVBOVertexAndIndexDynamicData; 273 } 274 275 /// Does ReadPixels support the provided format/type combo? 276 bool readPixelsSupported(const GrGLInterface* intf, 277 GrGLenum format, 278 GrGLenum type) const; 279 280 bool isCoreProfile() const { return fIsCoreProfile; } 281 282 /// Is there support for discarding the frame buffer 283 bool discardFBSupport() const { return fDiscardFBSupport; } 284 285 private: 286 /** 287 * Maintains a bit per GrPixelConfig. It is used to avoid redundantly 288 * performing glCheckFrameBufferStatus for the same config. 289 */ 290 struct VerifiedColorConfigs { 291 VerifiedColorConfigs() { 292 this->reset(); 293 } 294 295 void reset() { 296 for (int i = 0; i < kNumUints; ++i) { 297 fVerifiedColorConfigs[i] = 0; 298 } 299 } 300 301 static const int kNumUints = (kGrPixelConfigCnt + 31) / 32; 302 uint32_t fVerifiedColorConfigs[kNumUints]; 303 304 void markVerified(GrPixelConfig config) { 305 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT 306 return; 307 #endif 308 int u32Idx = config / 32; 309 int bitIdx = config % 32; 310 fVerifiedColorConfigs[u32Idx] |= 1 << bitIdx; 311 } 312 313 bool isVerified(GrPixelConfig config) const { 314 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT 315 return false; 316 #endif 317 int u32Idx = config / 32; 318 int bitIdx = config % 32; 319 return SkToBool(fVerifiedColorConfigs[u32Idx] & (1 << bitIdx)); 320 } 321 }; 322 323 void initFSAASupport(const GrGLContextInfo& ctxInfo, const GrGLInterface* gli); 324 void initStencilFormats(const GrGLContextInfo& ctxInfo); 325 326 // tracks configs that have been verified to pass the FBO completeness when 327 // used as a color attachment 328 VerifiedColorConfigs fVerifiedColorConfigs; 329 330 SkTArray<StencilFormat, true> fStencilFormats; 331 // tracks configs that have been verified to pass the FBO completeness when 332 // used as a color attachment when a particular stencil format is used 333 // as a stencil attachment. 334 SkTArray<VerifiedColorConfigs, true> fStencilVerifiedColorConfigs; 335 336 int fMaxFragmentUniformVectors; 337 int fMaxVertexAttributes; 338 int fMaxFragmentTextureUnits; 339 340 MSFBOType fMSFBOType; 341 CoverageAAType fCoverageAAType; 342 SkTDArray<MSAACoverageMode> fMSAACoverageModes; 343 344 FBFetchType fFBFetchType; 345 346 bool fRGBA8RenderbufferSupport : 1; 347 bool fBGRAFormatSupport : 1; 348 bool fBGRAIsInternalFormat : 1; 349 bool fTextureSwizzleSupport : 1; 350 bool fUnpackRowLengthSupport : 1; 351 bool fUnpackFlipYSupport : 1; 352 bool fPackRowLengthSupport : 1; 353 bool fPackFlipYSupport : 1; 354 bool fTextureUsageSupport : 1; 355 bool fTexStorageSupport : 1; 356 bool fTextureRedSupport : 1; 357 bool fImagingSupport : 1; 358 bool fTwoFormatLimit : 1; 359 bool fFragCoordsConventionSupport : 1; 360 bool fVertexArrayObjectSupport : 1; 361 bool fUseNonVBOVertexAndIndexDynamicData : 1; 362 bool fIsCoreProfile : 1; 363 bool fDiscardFBSupport : 1; 364 365 typedef GrDrawTargetCaps INHERITED; 366 }; 367 368 #endif 369