bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 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" |
bsalomon@google.com | 91bcc94 | 2012-05-07 17:28:41 +0000 | [diff] [blame] | 12 | #include "GrGLDefines.h" |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 13 | #include "GrStencil.h" |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 15 | class SkMatrix; |
| 16 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 17 | //////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| 19 | typedef uint32_t GrGLVersion; |
| 20 | typedef uint32_t GrGLSLVersion; |
| 21 | |
commit-bot@chromium.org | f4e67e3 | 2014-04-30 01:26:04 +0000 | [diff] [blame] | 22 | #define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \ |
| 23 | static_cast<int>(minor)) |
| 24 | #define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \ |
| 25 | static_cast<int>(minor)) |
| 26 | |
| 27 | #define GR_GL_INVALID_VER GR_GL_VER(0, 0) |
| 28 | #define GR_GLSL_INVALID_VER GR_GL_VER(0, 0) |
| 29 | |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 30 | /** |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 31 | * The Vendor and Renderer enum values are lazily updated as required. |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 32 | */ |
| 33 | enum GrGLVendor { |
bsalomon@google.com | 96966a5 | 2013-02-21 16:34:21 +0000 | [diff] [blame] | 34 | kARM_GrGLVendor, |
bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 35 | kImagination_GrGLVendor, |
| 36 | kIntel_GrGLVendor, |
commit-bot@chromium.org | 7a434a2 | 2013-08-21 14:01:56 +0000 | [diff] [blame] | 37 | kQualcomm_GrGLVendor, |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 38 | kNVIDIA_GrGLVendor, |
bsalomon@google.com | 3012ded | 2013-02-22 16:44:04 +0000 | [diff] [blame] | 39 | |
| 40 | kOther_GrGLVendor |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 43 | enum GrGLRenderer { |
commit-bot@chromium.org | 6dee875 | 2014-02-07 22:39:01 +0000 | [diff] [blame] | 44 | kTegra2_GrGLRenderer, |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 45 | kTegra3_GrGLRenderer, |
bsalomon | 63b2196 | 2014-11-05 07:05:34 -0800 | [diff] [blame] | 46 | kPowerVR54x_GrGLRenderer, |
| 47 | kPowerVRRogue_GrGLRenderer, |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 48 | kOther_GrGLRenderer |
| 49 | }; |
| 50 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 51 | //////////////////////////////////////////////////////////////////////////////// |
| 52 | |
| 53 | /** |
| 54 | * Some drivers want the var-int arg to be zero-initialized on input. |
| 55 | */ |
| 56 | #define GR_GL_INIT_ZERO 0 |
| 57 | #define GR_GL_GetIntegerv(gl, e, p) \ |
| 58 | do { \ |
| 59 | *(p) = GR_GL_INIT_ZERO; \ |
| 60 | GR_GL_CALL(gl, GetIntegerv(e, p)); \ |
| 61 | } while (0) |
| 62 | |
| 63 | #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \ |
| 64 | do { \ |
| 65 | *(p) = GR_GL_INIT_ZERO; \ |
| 66 | GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \ |
| 67 | } while (0) |
| 68 | |
| 69 | #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \ |
| 70 | do { \ |
| 71 | *(p) = GR_GL_INIT_ZERO; \ |
| 72 | GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \ |
| 73 | } while (0) |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 74 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 75 | #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \ |
| 76 | do { \ |
| 77 | *(p) = GR_GL_INIT_ZERO; \ |
| 78 | GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \ |
| 79 | } while (0) |
| 80 | |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame] | 81 | #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision) \ |
| 82 | do { \ |
| 83 | (range)[0] = GR_GL_INIT_ZERO; \ |
| 84 | (range)[1] = GR_GL_INIT_ZERO; \ |
| 85 | (*precision) = GR_GL_INIT_ZERO; \ |
| 86 | GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision)); \ |
| 87 | } while (0) |
| 88 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 89 | //////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | /** |
| 92 | * Helpers for glGetString() |
| 93 | */ |
| 94 | |
| 95 | // these variants assume caller already has a string from glGetString() |
| 96 | GrGLVersion GrGLGetVersionFromString(const char* versionString); |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 97 | GrGLStandard GrGLGetStandardInUseFromString(const char* versionString); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 98 | GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString); |
commit-bot@chromium.org | 459104c | 2013-06-14 14:42:56 +0000 | [diff] [blame] | 99 | bool GrGLIsMesaFromVersionString(const char* versionString); |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 100 | GrGLVendor GrGLGetVendorFromString(const char* vendorString); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 101 | GrGLRenderer GrGLGetRendererFromString(const char* rendererString); |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 102 | bool GrGLIsChromiumFromRendererString(const char* rendererString); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 103 | |
| 104 | // these variants call glGetString() |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 105 | GrGLVersion GrGLGetVersion(const GrGLInterface*); |
| 106 | GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*); |
bsalomon@google.com | 0b1e481 | 2012-10-23 13:52:43 +0000 | [diff] [blame] | 107 | GrGLVendor GrGLGetVendor(const GrGLInterface*); |
commit-bot@chromium.org | 0694ea7 | 2013-09-18 13:00:28 +0000 | [diff] [blame] | 108 | GrGLRenderer GrGLGetRenderer(const GrGLInterface*); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | c9424b8 | 2013-10-30 20:03:16 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Helpers for glGetError() |
| 113 | */ |
| 114 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 115 | void GrGLCheckErr(const GrGLInterface* gl, |
| 116 | const char* location, |
| 117 | const char* call); |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 118 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 119 | void GrGLClearErr(const GrGLInterface* gl); |
| 120 | |
commit-bot@chromium.org | 215a682 | 2013-09-05 18:28:42 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Helper for converting SkMatrix to a column-major GL float array |
| 123 | */ |
| 124 | template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src); |
| 125 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 126 | //////////////////////////////////////////////////////////////////////////////// |
| 127 | |
| 128 | /** |
| 129 | * Macros for using GrGLInterface to make GL calls |
| 130 | */ |
| 131 | |
| 132 | // internal macro to conditionally call glGetError based on compile-time and |
| 133 | // run-time flags. |
| 134 | #if GR_GL_CHECK_ERROR |
| 135 | extern bool gCheckErrorGL; |
| 136 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \ |
| 137 | if (gCheckErrorGL) \ |
| 138 | GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X) |
| 139 | #else |
| 140 | #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) |
| 141 | #endif |
| 142 | |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 143 | // internal macro to conditionally log the gl call using SkDebugf based on |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 144 | // compile-time and run-time flags. |
| 145 | #if GR_GL_LOG_CALLS |
| 146 | extern bool gLogCallsGL; |
| 147 | #define GR_GL_LOG_CALLS_IMPL(X) \ |
| 148 | if (gLogCallsGL) \ |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 149 | SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n") |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 150 | #else |
| 151 | #define GR_GL_LOG_CALLS_IMPL(X) |
| 152 | #endif |
| 153 | |
| 154 | // internal macro that does the per-GL-call callback (if necessary) |
| 155 | #if GR_GL_PER_GL_FUNC_CALLBACK |
| 156 | #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE) |
| 157 | #else |
| 158 | #define GR_GL_CALLBACK_IMPL(IFACE) |
| 159 | #endif |
| 160 | |
| 161 | // makes a GL call on the interface and does any error checking and logging |
| 162 | #define GR_GL_CALL(IFACE, X) \ |
| 163 | do { \ |
| 164 | GR_GL_CALL_NOERRCHECK(IFACE, X); \ |
| 165 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
| 166 | } while (false) |
| 167 | |
| 168 | // Variant of above that always skips the error check. This is useful when |
| 169 | // the caller wants to do its own glGetError() call and examine the error value. |
| 170 | #define GR_GL_CALL_NOERRCHECK(IFACE, X) \ |
| 171 | do { \ |
| 172 | GR_GL_CALLBACK_IMPL(IFACE); \ |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 173 | (IFACE)->fFunctions.f##X; \ |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 174 | GR_GL_LOG_CALLS_IMPL(X); \ |
| 175 | } while (false) |
| 176 | |
| 177 | // same as GR_GL_CALL but stores the return value of the gl call in RET |
| 178 | #define GR_GL_CALL_RET(IFACE, RET, X) \ |
| 179 | do { \ |
| 180 | GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \ |
| 181 | GR_GL_CHECK_ERROR_IMPL(IFACE, X); \ |
| 182 | } while (false) |
| 183 | |
| 184 | // same as GR_GL_CALL_RET but always skips the error check. |
| 185 | #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \ |
| 186 | do { \ |
| 187 | GR_GL_CALLBACK_IMPL(IFACE); \ |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 188 | (RET) = (IFACE)->fFunctions.f##X; \ |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 189 | GR_GL_LOG_CALLS_IMPL(X); \ |
| 190 | } while (false) |
| 191 | |
| 192 | // call glGetError without doing a redundant error check or logging. |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 193 | #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError() |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 194 | |
kkinnunen | ccdaa04 | 2014-08-20 01:36:23 -0700 | [diff] [blame] | 195 | GrGLenum GrToGLStencilFunc(GrStencilFunc basicFunc); |
| 196 | |
| 197 | |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 198 | #endif |