blob: 44a3b8647cb4087f824eb13794bdbd8144a0d067 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrGLConfig_DEFINED
12#define GrGLConfig_DEFINED
13
14#include "GrTypes.h"
twiz@google.com0f31ca72011-03-18 17:38:11 +000015#include "GrGLDefines.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000017/**
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000018 * Optional GL config file.
twiz@google.comb65e0cb2011-03-18 20:41:44 +000019 */
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000020#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
twiz@google.comb65e0cb2011-03-18 20:41:44 +000027
28/**
bsalomon@google.com3723a482011-02-17 21:47:25 +000029 * 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
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000031 * file (if one is in use). If a GR_GL_CUSTOM_SETUP_HEADER is used they can
32 * also be placed there.
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000033 *
bsalomon@google.com3723a482011-02-17 21:47:25 +000034 * GR_GL_LOG_CALLS: if 1 Gr can print every GL call using GrPrintf. Defaults to
reed@google.com27a1e772011-03-08 15:34:06 +000035 * 0. Logging can be enabled and disabled at runtime using a debugger via to
bsalomon@google.com3723a482011-02-17 21:47:25 +000036 * 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.
reed@google.com27a1e772011-03-08 15:34:06 +000043 * Defaults to 1 if GR_DEBUG is set, otherwise 0. When GR_GL_CHECK_ERROR is 1
bsalomon@google.com3723a482011-02-17 21:47:25 +000044 * 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.
bsalomon@google.com4be283f2011-04-19 21:15:09 +000049 *
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.
bsalomon@google.com9ae44292011-07-01 15:21:59 +000061 *
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.
bsalomon@google.com96e96df2011-10-10 14:49:29 +000068 * 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).
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000071 *
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.
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000080 */
81
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000082#if !defined(GR_GL_LOG_CALLS)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000083 #define GR_GL_LOG_CALLS GR_DEBUG
bsalomon@google.com3723a482011-02-17 21:47:25 +000084#endif
85
86#if !defined(GR_GL_LOG_CALLS_START)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000087 #define GR_GL_LOG_CALLS_START 0
bsalomon@google.com3723a482011-02-17 21:47:25 +000088#endif
89
reed@google.com27a1e772011-03-08 15:34:06 +000090#if !defined(GR_GL_CHECK_ERROR)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000091 #define GR_GL_CHECK_ERROR GR_DEBUG
bsalomon@google.com3723a482011-02-17 21:47:25 +000092#endif
93
94#if !defined(GR_GL_CHECK_ERROR_START)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000095 #define GR_GL_CHECK_ERROR_START 1
bsalomon@google.com4be283f2011-04-19 21:15:09 +000096#endif
97
98#if !defined(GR_GL_NO_CONSTANT_ATTRIBUTES)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +000099 #define GR_GL_NO_CONSTANT_ATTRIBUTES 0
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000100#endif
101
102#if !defined(GR_GL_ATTRIBUTE_MATRICES)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000103 #define GR_GL_ATTRIBUTE_MATRICES 0
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000104#endif
105
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000106#if !defined(GR_GL_USE_BUFFER_DATA_NULL_HINT)
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000107 #define GR_GL_USE_BUFFER_DATA_NULL_HINT 1
108#endif
109
110#if !defined(GR_GL_PER_GL_FUNC_CALLBACK)
111 #define GR_GL_PER_GL_FUNC_CALLBACK 0
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000112#endif
113
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000114#if(GR_GL_NO_CONSTANT_ATTRIBUTES) && (GR_GL_ATTRIBUTE_MATRICES)
115 #error "Cannot combine GR_GL_NO_CONSTANT_ATTRIBUTES and GR_GL_ATTRIBUTE_MATRICES"
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000116#endif
117
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
bsalomon@google.comf987d1b2011-04-04 17:13:52 +0000120/**
121 * The following macros are used to staticlly configure the default
122 * GrGLInterface, but should not be used outside of the GrGLInterface
123 * scaffolding. Undefine here to prevent accidental use.
124 */
125#undef GR_SUPPORT_GLDESKTOP
126#undef GR_SUPPORT_GLES1
127#undef GR_SUPPORT_GLES2
128#undef GR_SUPPORT_GLES
129
130////////////////////////////////////////////////////////////////////////////////
131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132#if GR_SCALAR_IS_FIXED
133 #define GrGLType GL_FIXED
134#elif GR_SCALAR_IS_FLOAT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000135 #define GrGLType GR_GL_FLOAT
reed@google.comac10a2d2010-12-22 21:39:39 +0000136#else
137 #error "unknown GR_SCALAR type"
138#endif
139
140#if GR_TEXT_SCALAR_IS_USHORT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000141 #define GrGLTextType GR_GL_UNSIGNED_SHORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 #define GR_GL_TEXT_TEXTURE_NORMALIZED 1
143#elif GR_TEXT_SCALAR_IS_FLOAT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000144 #define GrGLTextType GR_GL_FLOAT
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
146#elif GR_TEXT_SCALAR_IS_FIXED
twiz@google.com0f31ca72011-03-18 17:38:11 +0000147 #define GrGLTextType GR_GL_FIXED
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
reed@google.com63100f92011-01-18 21:32:14 +0000149#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 #error "unknown GR_TEXT_SCALAR type"
151#endif
152
twiz@google.com59a190b2011-03-14 21:23:01 +0000153// Pick a pixel config for 32bit bitmaps. Our default is GL_RGBA (except on
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000154// Windows where we match GDI's order).
155#ifndef GR_GL_32BPP_COLOR_FORMAT
senorblanco@chromium.org99c2a8b2011-05-04 20:12:01 +0000156 #if GR_WIN32_BUILD || GR_LINUX_BUILD
bsalomon@google.comc312bf92011-03-21 21:10:33 +0000157 #define GR_GL_32BPP_COLOR_FORMAT GR_GL_BGRA
158 #else
twiz@google.com0f31ca72011-03-18 17:38:11 +0000159 #define GR_GL_32BPP_COLOR_FORMAT GR_GL_RGBA
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000160 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000161#endif
162
reed@google.comac10a2d2010-12-22 21:39:39 +0000163////////////////////////////////////////////////////////////////////////////////
reed@google.com63100f92011-01-18 21:32:14 +0000164
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000165struct GrGLInterface;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000167extern void GrGLCheckErr(const GrGLInterface* gl,
168 const char* location,
169 const char* call);
170
171extern void GrGLClearErr(const GrGLInterface* gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000172
bsalomon@google.com3723a482011-02-17 21:47:25 +0000173#if GR_GL_CHECK_ERROR
174 extern bool gCheckErrorGL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000175 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
176 if (gCheckErrorGL) \
177 GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000178#else
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000179 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000180#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
182#if GR_GL_LOG_CALLS
bsalomon@google.com3723a482011-02-17 21:47:25 +0000183 extern bool gLogCallsGL;
bsalomon@google.comdec9f2d2011-08-30 18:05:17 +0000184 #define GR_GL_LOG_CALLS_IMPL(X) \
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000185 if (gLogCallsGL) \
186 GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
reed@google.comac10a2d2010-12-22 21:39:39 +0000187#else
bsalomon@google.com3723a482011-02-17 21:47:25 +0000188 #define GR_GL_LOG_CALLS_IMPL(X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000189#endif
190
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000191#if GR_GL_PER_GL_FUNC_CALLBACK
192 #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
193#else
194 #define GR_GL_CALLBACK_IMPL(IFACE)
195#endif
196
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000197#define GR_GL_CALL(IFACE, X) \
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000198 do { \
199 GR_GL_CALL_NOERRCHECK(IFACE, X); \
200 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
201 } while (false)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000202
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000203#define GR_GL_CALL_NOERRCHECK(IFACE, X) \
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000204 do { \
205 GR_GL_CALLBACK_IMPL(IFACE); \
206 (IFACE)->f##X; \
207 GR_GL_LOG_CALLS_IMPL(X); \
208 } while (false)
209
210#define GR_GL_CALL_RET(IFACE, RET, X) \
211 do { \
212 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
213 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
214 } while (false)
215
216#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
217 do { \
218 GR_GL_CALLBACK_IMPL(IFACE); \
219 (RET) = (IFACE)->f##X; \
220 GR_GL_LOG_CALLS_IMPL(X); \
221 } while (false)
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000222
223#define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError()
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000224
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000225////////////////////////////////////////////////////////////////////////////////
226
227/**
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000228 * GrGLResetRowLength() will reset GL_UNPACK_ROW_LENGTH to 0. We write
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000229 * this wrapper, since GL_UNPACK_ROW_LENGTH is not available on all GL versions
230 */
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000231extern void GrGLResetRowLength(const GrGLInterface*);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000232
233////////////////////////////////////////////////////////////////////////////////
234
235/**
236 * Some drivers want the var-int arg to be zero-initialized on input.
237 */
238#define GR_GL_INIT_ZERO 0
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000239#define GR_GL_GetIntegerv(gl, e, p) \
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000240 do { \
241 *(p) = GR_GL_INIT_ZERO; \
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000242 GR_GL_CALL(gl, GetIntegerv(e, p)); \
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000243 } while (0)
244
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000245#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
246 do { \
247 *(p) = GR_GL_INIT_ZERO; \
248 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000249 } while (0)
250
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000251#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
252 do { \
253 *(p) = GR_GL_INIT_ZERO; \
254 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000255 } while (0)
256
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000257#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
258 do { \
259 *(p) = GR_GL_INIT_ZERO; \
260 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000261 } while (0)
262
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000263////////////////////////////////////////////////////////////////////////////////
264
reed@google.com27a1e772011-03-08 15:34:06 +0000265#endif