Home | History | Annotate | Download | only in gl
      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      * The type of MSAA for FBOs supported. Different extensions have different
     32      * semantics of how / when a resolve is performed.
     33      */
     34     enum MSFBOType {
     35         /**
     36          * no support for MSAA FBOs
     37          */
     38         kNone_MSFBOType = 0,
     39         /**
     40          * GL3.0-style MSAA FBO (GL_ARB_framebuffer_object).
     41          */
     42         kDesktop_ARB_MSFBOType,
     43         /**
     44          * earlier GL_EXT_framebuffer* extensions
     45          */
     46         kDesktop_EXT_MSFBOType,
     47         /**
     48          * Similar to kDesktop_ARB but with additional restrictions on glBlitFramebuffer.
     49          */
     50         kES_3_0_MSFBOType,
     51         /**
     52          * GL_APPLE_framebuffer_multisample ES extension
     53          */
     54         kES_Apple_MSFBOType,
     55         /**
     56          * GL_IMG_multisampled_render_to_texture. This variation does not have MSAA renderbuffers.
     57          * Instead the texture is multisampled when bound to the FBO and then resolved automatically
     58          * when read. It also defines an alternate value for GL_MAX_SAMPLES (which we call
     59          * GR_GL_MAX_SAMPLES_IMG).
     60          */
     61         kES_IMG_MsToTexture_MSFBOType,
     62         /**
     63          * GL_EXT_multisampled_render_to_texture. Same as the IMG one above but uses the standard
     64          * GL_MAX_SAMPLES value.
     65          */
     66         kES_EXT_MsToTexture_MSFBOType,
     67 
     68         kLast_MSFBOType = kES_EXT_MsToTexture_MSFBOType
     69     };
     70 
     71     enum InvalidateFBType {
     72         kNone_InvalidateFBType,
     73         kDiscard_InvalidateFBType,       //<! glDiscardFramebuffer()
     74         kInvalidate_InvalidateFBType,     //<! glInvalidateFramebuffer()
     75 
     76         kLast_InvalidateFBType = kInvalidate_InvalidateFBType
     77     };
     78 
     79     enum MapBufferType {
     80         kNone_MapBufferType,
     81         kMapBuffer_MapBufferType,         // glMapBuffer()
     82         kMapBufferRange_MapBufferType,    // glMapBufferRange()
     83         kChromium_MapBufferType,          // GL_CHROMIUM_map_sub
     84 
     85         kLast_MapBufferType = kChromium_MapBufferType,
     86     };
     87 
     88     /**
     89      * Creates a GrGLCaps that advertises no support for any extensions,
     90      * formats, etc. Call init to initialize from a GrGLContextInfo.
     91      */
     92     GrGLCaps();
     93 
     94     GrGLCaps(const GrGLCaps& caps);
     95 
     96     GrGLCaps& operator = (const GrGLCaps& caps);
     97 
     98     /**
     99      * Resets the caps such that nothing is supported.
    100      */
    101     virtual void reset() SK_OVERRIDE;
    102 
    103     /**
    104      * Initializes the GrGLCaps to the set of features supported in the current
    105      * OpenGL context accessible via ctxInfo.
    106      */
    107     bool init(const GrGLContextInfo& ctxInfo, const GrGLInterface* glInterface);
    108 
    109     /**
    110      * Call to note that a color config has been verified as a valid color
    111      * attachment. This may save future calls to glCheckFramebufferStatus
    112      * using isConfigVerifiedColorAttachment().
    113      */
    114     void markConfigAsValidColorAttachment(GrPixelConfig config) {
    115         fVerifiedColorConfigs.markVerified(config);
    116     }
    117 
    118     /**
    119      * Call to check whether a config has been verified as a valid color
    120      * attachment.
    121      */
    122     bool isConfigVerifiedColorAttachment(GrPixelConfig config) const {
    123         return fVerifiedColorConfigs.isVerified(config);
    124     }
    125 
    126     /**
    127      * Call to note that a color config / stencil format pair passed
    128      * FBO status check. We may skip calling glCheckFramebufferStatus for
    129      * this combination in the future using
    130      * isColorConfigAndStencilFormatVerified().
    131      */
    132     void markColorConfigAndStencilFormatAsVerified(
    133                     GrPixelConfig config,
    134                     const GrGLStencilBuffer::Format& format);
    135 
    136     /**
    137      * Call to check whether color config / stencil format pair has already
    138      * passed FBO status check.
    139      */
    140     bool isColorConfigAndStencilFormatVerified(
    141                     GrPixelConfig config,
    142                     const GrGLStencilBuffer::Format& format) const;
    143 
    144     /**
    145      * Reports the type of MSAA FBO support.
    146      */
    147     MSFBOType msFBOType() const { return fMSFBOType; }
    148 
    149     /**
    150      * Does the supported MSAA FBO extension have MSAA renderbuffers?
    151      */
    152     bool usesMSAARenderBuffers() const {
    153         return kNone_MSFBOType != fMSFBOType &&
    154                kES_IMG_MsToTexture_MSFBOType != fMSFBOType &&
    155                kES_EXT_MsToTexture_MSFBOType != fMSFBOType;
    156     }
    157 
    158     /**
    159      * Is the MSAA FBO extension one where the texture is multisampled when bound to an FBO and
    160      * then implicitly resolved when read.
    161      */
    162     bool usesImplicitMSAAResolve() const {
    163         return kES_IMG_MsToTexture_MSFBOType == fMSFBOType ||
    164                kES_EXT_MsToTexture_MSFBOType == fMSFBOType;
    165     }
    166 
    167     /**
    168      * Some helper functions for encapsulating various extensions to read FB Buffer on openglES
    169      *
    170      * TODO On desktop opengl 4.2+ we can achieve something similar to this effect
    171      */
    172     bool fbFetchSupport() const { return fFBFetchSupport; }
    173 
    174     const char* fbFetchColorName() const { return fFBFetchColorName; }
    175 
    176     const char* fbFetchExtensionString() const { return fFBFetchExtensionString; }
    177 
    178     InvalidateFBType invalidateFBType() const { return fInvalidateFBType; }
    179 
    180     /// What type of buffer mapping is supported?
    181     MapBufferType mapBufferType() const { return fMapBufferType; }
    182 
    183     /**
    184      * Gets an array of legal stencil formats. These formats are not guaranteed
    185      * to be supported by the driver but are legal GLenum names given the GL
    186      * version and extensions supported.
    187      */
    188     const SkTArray<StencilFormat, true>& stencilFormats() const {
    189         return fStencilFormats;
    190     }
    191 
    192     /// The maximum number of fragment uniform vectors (GLES has min. 16).
    193     int maxFragmentUniformVectors() const { return fMaxFragmentUniformVectors; }
    194 
    195     /// maximum number of attribute values per vertex
    196     int maxVertexAttributes() const { return fMaxVertexAttributes; }
    197 
    198     /// maximum number of texture units accessible in the fragment shader.
    199     int maxFragmentTextureUnits() const { return fMaxFragmentTextureUnits; }
    200 
    201     /// maximum number of fixed-function texture coords, or zero if no fixed-function.
    202     int maxFixedFunctionTextureCoords() const { return fMaxFixedFunctionTextureCoords; }
    203 
    204     /// ES requires an extension to support RGBA8 in RenderBufferStorage
    205     bool rgba8RenderbufferSupport() const { return fRGBA8RenderbufferSupport; }
    206 
    207     /**
    208      * Depending on the ES extensions present the BGRA external format may
    209      * correspond either a BGRA or RGBA internalFormat. On desktop GL it is
    210      * RGBA.
    211      */
    212     bool bgraIsInternalFormat() const { return fBGRAIsInternalFormat; }
    213 
    214     /// GL_ARB_texture_swizzle support
    215     bool textureSwizzleSupport() const { return fTextureSwizzleSupport; }
    216 
    217     /// Is there support for GL_UNPACK_ROW_LENGTH
    218     bool unpackRowLengthSupport() const { return fUnpackRowLengthSupport; }
    219 
    220     /// Is there support for GL_UNPACK_FLIP_Y
    221     bool unpackFlipYSupport() const { return fUnpackFlipYSupport; }
    222 
    223     /// Is there support for GL_PACK_ROW_LENGTH
    224     bool packRowLengthSupport() const { return fPackRowLengthSupport; }
    225 
    226     /// Is there support for GL_PACK_REVERSE_ROW_ORDER
    227     bool packFlipYSupport() const { return fPackFlipYSupport; }
    228 
    229     /// Is there support for texture parameter GL_TEXTURE_USAGE
    230     bool textureUsageSupport() const { return fTextureUsageSupport; }
    231 
    232     /// Is there support for glTexStorage
    233     bool texStorageSupport() const { return fTexStorageSupport; }
    234 
    235     /// Is there support for GL_RED and GL_R8
    236     bool textureRedSupport() const { return fTextureRedSupport; }
    237 
    238     /// Is GL_ARB_IMAGING supported
    239     bool imagingSupport() const { return fImagingSupport; }
    240 
    241     /// Is GL_ARB_fragment_coord_conventions supported?
    242     bool fragCoordConventionsSupport() const { return fFragCoordsConventionSupport; }
    243 
    244     /// Is there support for Vertex Array Objects?
    245     bool vertexArrayObjectSupport() const { return fVertexArrayObjectSupport; }
    246 
    247     /// Use indices or vertices in CPU arrays rather than VBOs for dynamic content.
    248     bool useNonVBOVertexAndIndexDynamicData() const {
    249         return fUseNonVBOVertexAndIndexDynamicData;
    250     }
    251 
    252     /// Does ReadPixels support the provided format/type combo?
    253     bool readPixelsSupported(const GrGLInterface* intf,
    254                              GrGLenum format,
    255                              GrGLenum type) const;
    256 
    257     bool isCoreProfile() const { return fIsCoreProfile; }
    258 
    259 
    260     bool fullClearIsFree() const { return fFullClearIsFree; }
    261 
    262     bool dropsTileOnZeroDivide() const { return fDropsTileOnZeroDivide; }
    263 
    264     /**
    265      * Returns a string containing the caps info.
    266      */
    267     virtual SkString dump() const SK_OVERRIDE;
    268 
    269     /**
    270      * LATC can appear under one of three possible names. In order to know
    271      * which GL internal format to use, we need to keep track of which name
    272      * we found LATC under. The default is LATC.
    273      */
    274     enum LATCAlias {
    275         kLATC_LATCAlias,
    276         kRGTC_LATCAlias,
    277         k3DC_LATCAlias
    278     };
    279 
    280     LATCAlias latcAlias() const { return fLATCAlias; }
    281 
    282 private:
    283     /**
    284      * Maintains a bit per GrPixelConfig. It is used to avoid redundantly
    285      * performing glCheckFrameBufferStatus for the same config.
    286      */
    287     struct VerifiedColorConfigs {
    288         VerifiedColorConfigs() {
    289             this->reset();
    290         }
    291 
    292         void reset() {
    293             for (int i = 0; i < kNumUints; ++i) {
    294                 fVerifiedColorConfigs[i] = 0;
    295             }
    296         }
    297 
    298         static const int kNumUints = (kGrPixelConfigCnt  + 31) / 32;
    299         uint32_t fVerifiedColorConfigs[kNumUints];
    300 
    301         void markVerified(GrPixelConfig config) {
    302 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
    303                 return;
    304 #endif
    305             int u32Idx = config / 32;
    306             int bitIdx = config % 32;
    307             fVerifiedColorConfigs[u32Idx] |= 1 << bitIdx;
    308         }
    309 
    310         bool isVerified(GrPixelConfig config) const {
    311 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
    312             return false;
    313 #endif
    314             int u32Idx = config / 32;
    315             int bitIdx = config % 32;
    316             return SkToBool(fVerifiedColorConfigs[u32Idx] & (1 << bitIdx));
    317         }
    318     };
    319 
    320     void initFSAASupport(const GrGLContextInfo&, const GrGLInterface*);
    321     void initStencilFormats(const GrGLContextInfo&);
    322     // This must be called after initFSAASupport().
    323     void initConfigRenderableTable(const GrGLContextInfo&);
    324     void initConfigTexturableTable(const GrGLContextInfo&, const GrGLInterface*);
    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     int fMaxFixedFunctionTextureCoords;
    340 
    341     MSFBOType           fMSFBOType;
    342     InvalidateFBType    fInvalidateFBType;
    343     MapBufferType       fMapBufferType;
    344     LATCAlias           fLATCAlias;
    345 
    346     bool fRGBA8RenderbufferSupport : 1;
    347     bool fBGRAIsInternalFormat : 1;
    348     bool fTextureSwizzleSupport : 1;
    349     bool fUnpackRowLengthSupport : 1;
    350     bool fUnpackFlipYSupport : 1;
    351     bool fPackRowLengthSupport : 1;
    352     bool fPackFlipYSupport : 1;
    353     bool fTextureUsageSupport : 1;
    354     bool fTexStorageSupport : 1;
    355     bool fTextureRedSupport : 1;
    356     bool fImagingSupport  : 1;
    357     bool fTwoFormatLimit : 1;
    358     bool fFragCoordsConventionSupport : 1;
    359     bool fVertexArrayObjectSupport : 1;
    360     bool fUseNonVBOVertexAndIndexDynamicData : 1;
    361     bool fIsCoreProfile : 1;
    362     bool fFullClearIsFree : 1;
    363     bool fDropsTileOnZeroDivide : 1;
    364     bool fFBFetchSupport : 1;
    365 
    366     const char* fFBFetchColorName;
    367     const char* fFBFetchExtensionString;
    368 
    369     typedef GrDrawTargetCaps INHERITED;
    370 };
    371 
    372 #endif
    373