Home | History | Annotate | Download | only in gl
      1 
      2 /*
      3  * Copyright 2011 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 
     11 #ifndef GrGLConfig_DEFINED
     12 #define GrGLConfig_DEFINED
     13 
     14 #include "GrTypes.h"
     15 #include "GrGLDefines.h"
     16 
     17 /**
     18  * Optional GL config file.
     19  */
     20 #ifdef GR_GL_CUSTOM_SETUP_HEADER
     21     #include GR_GL_CUSTOM_SETUP_HEADER
     22 #endif
     23 
     24 #if !defined(GR_GL_FUNCTION_TYPE)
     25     #define GR_GL_FUNCTION_TYPE
     26 #endif
     27 
     28 /**
     29  * The following are optional defines that can be enabled at the compiler
     30  * command line, in a IDE project, in a GrUserConfig.h file, or in a GL custom
     31  * file (if one is in use). If a GR_GL_CUSTOM_SETUP_HEADER is used they can
     32  * also be placed there.
     33  *
     34  * GR_GL_LOG_CALLS: if 1 Gr can print every GL call using GrPrintf. Defaults to
     35  * 0. Logging can be enabled and disabled at runtime using a debugger via to
     36  * global gLogCallsGL. The initial value of gLogCallsGL is controlled by
     37  * GR_GL_LOG_CALLS_START.
     38  *
     39  * GR_GL_LOG_CALLS_START: controls the initial value of gLogCallsGL when
     40  * GR_GL_LOG_CALLS is 1. Defaults to 0.
     41  *
     42  * GR_GL_CHECK_ERROR: if enabled Gr can do a glGetError() after every GL call.
     43  * Defaults to 1 if GR_DEBUG is set, otherwise 0. When GR_GL_CHECK_ERROR is 1
     44  * this can be toggled in a debugger using the gCheckErrorGL global. The initial
     45  * value of gCheckErrorGL is controlled by by GR_GL_CHECK_ERROR_START.
     46  *
     47  * GR_GL_CHECK_ERROR_START: controls the initial value of gCheckErrorGL
     48  * when GR_GL_CHECK_ERROR is 1.  Defaults to 1.
     49  *
     50  * GR_GL_NO_CONSTANT_ATTRIBUTES: if this evaluates to true then the GL backend
     51  * will use uniforms instead of attributes in all cases when there is not
     52  * per-vertex data. This is important when the underlying GL implementation
     53  * doesn't actually support immediate style attribute values (e.g. when
     54  * the GL stream is converted to DX as in ANGLE on Chrome). Defaults to 0.
     55  *
     56  * GR_GL_ATTRIBUTE_MATRICES: If changing uniforms is very expensive it may be
     57  * faster to use vertex attributes for matrices (set via glVertexAttrib3fv).
     58  * Setting this build flag enables this behavior. GR_GL_NO_CONSTANT_ATTRIBUTES
     59  * must not be set since this uses constant attributes for the matrices.
     60  * Defaults to 0.
     61  *
     62  * GR_GL_USE_BUFFER_DATA_NULL_HINT: When specifing new data for a vertex/index
     63  * buffer that replaces old data Ganesh can give a hint to the driver that the
     64  * previous data will not be used in future draws like this:
     65  *  glBufferData(GL_..._BUFFER, size, NULL, usage);       //<--hint, NULL means
     66  *  glBufferSubData(GL_..._BUFFER, 0, lessThanSize, data) //   old data can't be
     67  *                                                        //   used again.
     68  * However, this can be an unoptimization on some platforms, esp. Chrome.
     69  * Chrome's cmd buffer will create a new allocation and memset the whole thing
     70  * to zero (for security reasons). Defaults to 1 (enabled).
     71  *
     72  * GR_GL_PER_GL_FUNC_CALLBACK: When set to 1 the GrGLInterface object provides
     73  * a function pointer that is called just before every gl function. The ptr must
     74  * be valid (i.e. there is no NULL check). However, by default the callback will
     75  * be set to a function that does nothing. The signature of the function is:
     76  *    void function(const GrGLInterface*)
     77  * It is not extern "C".
     78  * The GrGLInterface field fCallback specifies the function ptr and there is an
     79  * additional field fCallbackData of type intptr_t for client data.
     80  *
     81  * GR_GL_RGBA_8888_PIXEL_OPS_SLOW: Set this to 1 if it is known that performing
     82  * glReadPixels / glTex(Sub)Image with format=GL_RGBA, type=GL_UNISIGNED_BYTE is
     83  * significantly slower than format=GL_BGRA, type=GL_UNISIGNED_BYTE.
     84  *
     85  * GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL: Set this to 1 if calling
     86  * glReadPixels to read the entire framebuffer is faster than calling it with
     87  * the same sized rectangle but with a framebuffer bound that is larger than
     88  * the rectangle read.
     89  *
     90  * GR_GL_CHECK_ALLOC_WITH_GET_ERROR: If set to 1 this will then glTexImage,
     91  * glBufferData, glRenderbufferStorage, etc will be checked for errors. This
     92  * amounts to ensuring the error is GL_NO_ERROR, calling the allocating
     93  * function, and then checking that the error is still GL_NO_ERROR. When the
     94  * value is 0 we will assume no error was generated without checking.
     95  *
     96  * GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT: We will normally check the FBO status
     97  * every time we bind a texture or renderbuffer to an FBO. However, in some
     98  * environments CheckFrameBufferStatus is very expensive. If this is set we will
     99  * check the first time we use a color format or a combination of color /
    100  * stencil formats as attachments. If the FBO is complete we will assume
    101  * subsequent attachments with the same formats are complete as well.
    102  */
    103 
    104 #if !defined(GR_GL_LOG_CALLS)
    105     #define GR_GL_LOG_CALLS                             GR_DEBUG
    106 #endif
    107 
    108 #if !defined(GR_GL_LOG_CALLS_START)
    109     #define GR_GL_LOG_CALLS_START                       0
    110 #endif
    111 
    112 #if !defined(GR_GL_CHECK_ERROR)
    113     #define GR_GL_CHECK_ERROR                           GR_DEBUG
    114 #endif
    115 
    116 #if !defined(GR_GL_CHECK_ERROR_START)
    117     #define GR_GL_CHECK_ERROR_START                     1
    118 #endif
    119 
    120 #if !defined(GR_GL_NO_CONSTANT_ATTRIBUTES)
    121     #define GR_GL_NO_CONSTANT_ATTRIBUTES                0
    122 #endif
    123 
    124 #if !defined(GR_GL_ATTRIBUTE_MATRICES)
    125     #define GR_GL_ATTRIBUTE_MATRICES                    0
    126 #endif
    127 
    128 #if !defined(GR_GL_USE_BUFFER_DATA_NULL_HINT)
    129     #define GR_GL_USE_BUFFER_DATA_NULL_HINT             1
    130 #endif
    131 
    132 #if !defined(GR_GL_PER_GL_FUNC_CALLBACK)
    133     #define GR_GL_PER_GL_FUNC_CALLBACK                  0
    134 #endif
    135 
    136 #if !defined(GR_GL_RGBA_8888_PIXEL_OPS_SLOW)
    137     #define GR_GL_RGBA_8888_PIXEL_OPS_SLOW              0
    138 #endif
    139 
    140 #if !defined(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL)
    141     #define GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL   0
    142 #endif
    143 
    144 #if !defined(GR_GL_CHECK_ALLOC_WITH_GET_ERROR)
    145     #define GR_GL_CHECK_ALLOC_WITH_GET_ERROR            1
    146 #endif
    147 
    148 #if !defined(GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT)
    149     #define GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT      0
    150 #endif
    151 
    152 /**
    153  * There is a strange bug that occurs on Macs with NVIDIA GPUs. We don't
    154  * fully understand it. When (element) array buffers are continually
    155  * respecified using glBufferData performance can fall off of a cliff. The
    156  * driver winds up performing many DMA mapping / unmappings and chews up ~50% of
    157  * the core. However, it has been observed that occaisonally respecifiying the
    158  * buffer using glBufferData and then writing data using glBufferSubData
    159  * prevents the bad behavior.
    160  *
    161  * There is a lot of uncertainty around this issue. In Chrome backgrounding
    162  * the tab somehow initiates this behavior and we don't know what the connection
    163  * is. Another observation is that Chrome's cmd buffer server will actually
    164  * create a buffer full of zeros when it sees a NULL data param (for security
    165  * reasons). If this is disabled and NULL is actually passed all the way to the
    166  * driver then the workaround doesn't help.
    167  *
    168  * The issue is tracked at:
    169  * http://code.google.com/p/chromium/issues/detail?id=114865
    170  *
    171  * When the workaround is enabled we will use the glBufferData / glBufferSubData
    172  * trick every 128 array buffer uploads.
    173  *
    174  * Hopefully we will understand this better and have a cleaner fix or get a
    175  * OS/driver level fix.
    176  */
    177 #define GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND   \
    178     (GR_MAC_BUILD &&                                    \
    179      !GR_GL_USE_BUFFER_DATA_NULL_HINT)
    180 
    181 #if(GR_GL_NO_CONSTANT_ATTRIBUTES) && (GR_GL_ATTRIBUTE_MATRICES)
    182     #error "Cannot combine GR_GL_NO_CONSTANT_ATTRIBUTES and GR_GL_ATTRIBUTE_MATRICES"
    183 #endif
    184 
    185 ////////////////////////////////////////////////////////////////////////////////
    186 
    187 #if GR_SCALAR_IS_FIXED
    188     #define GrGLType   GL_FIXED
    189 #elif GR_SCALAR_IS_FLOAT
    190     #define GrGLType   GR_GL_FLOAT
    191 #else
    192     #error "unknown GR_SCALAR type"
    193 #endif
    194 
    195 #if GR_TEXT_SCALAR_IS_USHORT
    196     #define GrGLTextType                    GR_GL_UNSIGNED_SHORT
    197     #define GR_GL_TEXT_TEXTURE_NORMALIZED   1
    198 #elif GR_TEXT_SCALAR_IS_FLOAT
    199     #define GrGLTextType                    GR_GL_FLOAT
    200     #define GR_GL_TEXT_TEXTURE_NORMALIZED   0
    201 #elif GR_TEXT_SCALAR_IS_FIXED
    202     #define GrGLTextType                    GR_GL_FIXED
    203     #define GR_GL_TEXT_TEXTURE_NORMALIZED   0
    204 #else
    205     #error "unknown GR_TEXT_SCALAR type"
    206 #endif
    207 
    208 ////////////////////////////////////////////////////////////////////////////////
    209 
    210 struct GrGLInterface;
    211 
    212 extern void GrGLCheckErr(const GrGLInterface* gl,
    213                          const char* location,
    214                          const char* call);
    215 
    216 extern void GrGLClearErr(const GrGLInterface* gl);
    217 
    218 #if GR_GL_CHECK_ERROR
    219     extern bool gCheckErrorGL;
    220     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
    221         if (gCheckErrorGL)                                      \
    222             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
    223 #else
    224     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
    225 #endif
    226 
    227 #if GR_GL_LOG_CALLS
    228     extern bool gLogCallsGL;
    229     #define GR_GL_LOG_CALLS_IMPL(X)                             \
    230         if (gLogCallsGL)                                        \
    231             GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
    232 #else
    233     #define GR_GL_LOG_CALLS_IMPL(X)
    234 #endif
    235 
    236 #if GR_GL_PER_GL_FUNC_CALLBACK
    237     #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
    238 #else
    239     #define GR_GL_CALLBACK_IMPL(IFACE)
    240 #endif
    241 
    242 #define GR_GL_CALL(IFACE, X)                                    \
    243     do {                                                        \
    244         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
    245         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
    246     } while (false)
    247 
    248 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
    249     do {                                                        \
    250         GR_GL_CALLBACK_IMPL(IFACE);                             \
    251         (IFACE)->f##X;                                          \
    252         GR_GL_LOG_CALLS_IMPL(X);                                \
    253     } while (false)
    254 
    255 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
    256     do {                                                        \
    257         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
    258         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
    259     } while (false)
    260 
    261 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
    262     do {                                                        \
    263         GR_GL_CALLBACK_IMPL(IFACE);                             \
    264         (RET) = (IFACE)->f##X;                                  \
    265         GR_GL_LOG_CALLS_IMPL(X);                                \
    266     } while (false)
    267 
    268 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError()
    269 
    270 ////////////////////////////////////////////////////////////////////////////////
    271 
    272 /**
    273  *  Some drivers want the var-int arg to be zero-initialized on input.
    274  */
    275 #define GR_GL_INIT_ZERO     0
    276 #define GR_GL_GetIntegerv(gl, e, p)     \
    277     do {                            \
    278         *(p) = GR_GL_INIT_ZERO;     \
    279         GR_GL_CALL(gl, GetIntegerv(e, p));   \
    280     } while (0)
    281 
    282 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)           \
    283     do {                                                                        \
    284         *(p) = GR_GL_INIT_ZERO;                                                 \
    285         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));    \
    286     } while (0)
    287 
    288 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                       \
    289     do {                                                                        \
    290         *(p) = GR_GL_INIT_ZERO;                                                 \
    291         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));                \
    292     } while (0)
    293 
    294 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                        \
    295     do {                                                                        \
    296         *(p) = GR_GL_INIT_ZERO;                                                 \
    297         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                 \
    298     } while (0)
    299 
    300 ////////////////////////////////////////////////////////////////////////////////
    301 
    302 #endif
    303