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