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 #ifndef GrGLUtil_DEFINED
      9 #define GrGLUtil_DEFINED
     10 
     11 #include "gl/GrGLInterface.h"
     12 #include "GrGLDefines.h"
     13 
     14 ////////////////////////////////////////////////////////////////////////////////
     15 
     16 typedef uint32_t GrGLVersion;
     17 typedef uint32_t GrGLSLVersion;
     18 
     19 /**
     20  * This list is lazily updated as required.
     21  */
     22 enum GrGLVendor {
     23     kIntel_GrGLVendor,
     24     kOther_GrGLVendor,
     25 };
     26 
     27 #define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \
     28                                  static_cast<int>(minor))
     29 #define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \
     30                                    static_cast<int>(minor))
     31 
     32 ////////////////////////////////////////////////////////////////////////////////
     33 
     34 /**
     35  *  Some drivers want the var-int arg to be zero-initialized on input.
     36  */
     37 #define GR_GL_INIT_ZERO     0
     38 #define GR_GL_GetIntegerv(gl, e, p)                                            \
     39     do {                                                                       \
     40         *(p) = GR_GL_INIT_ZERO;                                                \
     41         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
     42     } while (0)
     43 
     44 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
     45     do {                                                                       \
     46         *(p) = GR_GL_INIT_ZERO;                                                \
     47         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
     48     } while (0)
     49 
     50 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
     51     do {                                                                       \
     52         *(p) = GR_GL_INIT_ZERO;                                                \
     53         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
     54     } while (0)
     55 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
     56     do {                                                                       \
     57         *(p) = GR_GL_INIT_ZERO;                                                \
     58         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
     59     } while (0)
     60 
     61 ////////////////////////////////////////////////////////////////////////////////
     62 
     63 /**
     64  * Helpers for glGetString()
     65  */
     66 
     67 // these variants assume caller already has a string from glGetString()
     68 GrGLVersion GrGLGetVersionFromString(const char* versionString);
     69 GrGLBinding GrGLGetBindingInUseFromString(const char* versionString);
     70 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
     71 bool GrGLHasExtensionFromString(const char* ext, const char* extensionString);
     72 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
     73 
     74 // these variants call glGetString()
     75 bool GrGLHasExtension(const GrGLInterface*, const char* ext);
     76 GrGLBinding GrGLGetBindingInUse(const GrGLInterface*);
     77 GrGLVersion GrGLGetVersion(const GrGLInterface*);
     78 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
     79 GrGLVendor GrGLGetVendor(const GrGLInterface*);
     80 
     81 /**
     82  * Helpers for glGetError()
     83  */
     84 
     85 void GrGLCheckErr(const GrGLInterface* gl,
     86                   const char* location,
     87                   const char* call);
     88 
     89 void GrGLClearErr(const GrGLInterface* gl);
     90 
     91 ////////////////////////////////////////////////////////////////////////////////
     92 
     93 /**
     94  * Macros for using GrGLInterface to make GL calls
     95  */
     96 
     97 // internal macro to conditionally call glGetError based on compile-time and
     98 // run-time flags.
     99 #if GR_GL_CHECK_ERROR
    100     extern bool gCheckErrorGL;
    101     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
    102         if (gCheckErrorGL)                                      \
    103             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
    104 #else
    105     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
    106 #endif
    107 
    108 // internal macro to conditionally log the gl call using GrPrintf based on
    109 // compile-time and run-time flags.
    110 #if GR_GL_LOG_CALLS
    111     extern bool gLogCallsGL;
    112     #define GR_GL_LOG_CALLS_IMPL(X)                             \
    113         if (gLogCallsGL)                                        \
    114             GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
    115 #else
    116     #define GR_GL_LOG_CALLS_IMPL(X)
    117 #endif
    118 
    119 // internal macro that does the per-GL-call callback (if necessary)
    120 #if GR_GL_PER_GL_FUNC_CALLBACK
    121     #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
    122 #else
    123     #define GR_GL_CALLBACK_IMPL(IFACE)
    124 #endif
    125 
    126 // makes a GL call on the interface and does any error checking and logging
    127 #define GR_GL_CALL(IFACE, X)                                    \
    128     do {                                                        \
    129         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
    130         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
    131     } while (false)
    132 
    133 // Variant of above that always skips the error check. This is useful when
    134 // the caller wants to do its own glGetError() call and examine the error value.
    135 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
    136     do {                                                        \
    137         GR_GL_CALLBACK_IMPL(IFACE);                             \
    138         (IFACE)->f##X;                                          \
    139         GR_GL_LOG_CALLS_IMPL(X);                                \
    140     } while (false)
    141 
    142 // same as GR_GL_CALL but stores the return value of the gl call in RET
    143 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
    144     do {                                                        \
    145         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
    146         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
    147     } while (false)
    148 
    149 // same as GR_GL_CALL_RET but always skips the error check.
    150 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
    151     do {                                                        \
    152         GR_GL_CALLBACK_IMPL(IFACE);                             \
    153         (RET) = (IFACE)->f##X;                                  \
    154         GR_GL_LOG_CALLS_IMPL(X);                                \
    155     } while (false)
    156 
    157 // call glGetError without doing a redundant error check or logging.
    158 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError()
    159 
    160 #endif
    161