blob: 2cc4f31edac9de499fd00cc009736ade31e4168c [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.com0a97be22011-11-08 19:20:57 +000080 *
bsalomon@google.coma85449d2011-11-19 02:36:05 +000081 * 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.
bsalomon@google.com56d11e02011-11-30 19:59:08 +000084 *
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.
bsalomon@google.com4f3c2532012-01-19 16:16:52 +000089 *
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.
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +000095 *
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.
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000102 */
103
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000104#if !defined(GR_GL_LOG_CALLS)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000105 #define GR_GL_LOG_CALLS GR_DEBUG
bsalomon@google.com3723a482011-02-17 21:47:25 +0000106#endif
107
108#if !defined(GR_GL_LOG_CALLS_START)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000109 #define GR_GL_LOG_CALLS_START 0
bsalomon@google.com3723a482011-02-17 21:47:25 +0000110#endif
111
reed@google.com27a1e772011-03-08 15:34:06 +0000112#if !defined(GR_GL_CHECK_ERROR)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000113 #define GR_GL_CHECK_ERROR GR_DEBUG
bsalomon@google.com3723a482011-02-17 21:47:25 +0000114#endif
115
116#if !defined(GR_GL_CHECK_ERROR_START)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000117 #define GR_GL_CHECK_ERROR_START 1
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000118#endif
119
120#if !defined(GR_GL_NO_CONSTANT_ATTRIBUTES)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000121 #define GR_GL_NO_CONSTANT_ATTRIBUTES 0
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000122#endif
123
124#if !defined(GR_GL_ATTRIBUTE_MATRICES)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000125 #define GR_GL_ATTRIBUTE_MATRICES 0
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000126#endif
127
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000128#if !defined(GR_GL_USE_BUFFER_DATA_NULL_HINT)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000129 #define GR_GL_USE_BUFFER_DATA_NULL_HINT 1
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000130#endif
131
132#if !defined(GR_GL_PER_GL_FUNC_CALLBACK)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000133 #define GR_GL_PER_GL_FUNC_CALLBACK 0
bsalomon@google.com9ae44292011-07-01 15:21:59 +0000134#endif
135
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000136#if !defined(GR_GL_RGBA_8888_PIXEL_OPS_SLOW)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000137 #define GR_GL_RGBA_8888_PIXEL_OPS_SLOW 0
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000138#endif
139
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000140#if !defined(GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000141 #define GR_GL_FULL_READPIXELS_FASTER_THAN_PARTIAL 0
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000142#endif
143
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000144#if !defined(GR_GL_CHECK_ALLOC_WITH_GET_ERROR)
bsalomon@google.com4bcb0c62012-02-07 16:06:47 +0000145 #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
bsalomon@google.com4f3c2532012-01-19 16:16:52 +0000150#endif
151
bsalomon@google.com5ffd5ba2012-03-01 15:29:07 +0000152/**
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 at the same (or perhaps just a small) size
156 * performance can fall off of a cliff. The driver winds up performing many
157 * DMA mapping / unmappings and chews up ~50% of the core. However, it has been
158 * observed that respecifiying the buffer with a larger size and then writing
159 * the small amount of actual data using glBufferSubData prevents the bad
160 * behavior.
161 *
162 * There is a lot of uncertainty around this issue. In Chrome backgrounding
163 * the tab somehow initiates this behavior and we don't know what the connection
164 * is. Another observation is that Chrome's cmd buffer server will actually
165 * create a buffer full of zeros when it sees a NULL data param (for security
166 * reasons). If this is disabled and NULL is actually passed all the way to the
167 * driver then the workaround doesn't help.
168 *
169 * The issue is tracked at:
170 * http://code.google.com/p/chromium/issues/detail?id=114865
171 *
172 * When GR_GL_USE_BUFFER_DATA_NULL_HINT is not set we encounter the bug
173 * if we keep uploading small vetex / index sizes. So, every 1K uploads we
174 * act as though GR_GL_USE_BUFFER_DATA_NULL_HINT was set: we specify the buffer
175 * at full size with a NULL data ptr and then do a partial update with
176 * glBufferSubData.
177 *
178 * Hopefully we will understand this better and have a cleaner fix or get a
179 * OS/driver level fix.
180 */
181#define GR_GL_MAC_BUFFER_OBJECT_PERFOMANCE_WORKAROUND \
182 (GR_MAC_BUILD && \
183 !GR_GL_USE_BUFFER_DATA_NULL_HINT)
184
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000185#if(GR_GL_NO_CONSTANT_ATTRIBUTES) && (GR_GL_ATTRIBUTE_MATRICES)
186 #error "Cannot combine GR_GL_NO_CONSTANT_ATTRIBUTES and GR_GL_ATTRIBUTE_MATRICES"
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000187#endif
188
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000189////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000190
reed@google.comac10a2d2010-12-22 21:39:39 +0000191#if GR_SCALAR_IS_FIXED
192 #define GrGLType GL_FIXED
193#elif GR_SCALAR_IS_FLOAT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000194 #define GrGLType GR_GL_FLOAT
reed@google.comac10a2d2010-12-22 21:39:39 +0000195#else
196 #error "unknown GR_SCALAR type"
197#endif
198
199#if GR_TEXT_SCALAR_IS_USHORT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000200 #define GrGLTextType GR_GL_UNSIGNED_SHORT
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 #define GR_GL_TEXT_TEXTURE_NORMALIZED 1
202#elif GR_TEXT_SCALAR_IS_FLOAT
twiz@google.com0f31ca72011-03-18 17:38:11 +0000203 #define GrGLTextType GR_GL_FLOAT
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
205#elif GR_TEXT_SCALAR_IS_FIXED
twiz@google.com0f31ca72011-03-18 17:38:11 +0000206 #define GrGLTextType GR_GL_FIXED
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
reed@google.com63100f92011-01-18 21:32:14 +0000208#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 #error "unknown GR_TEXT_SCALAR type"
210#endif
211
reed@google.comac10a2d2010-12-22 21:39:39 +0000212////////////////////////////////////////////////////////////////////////////////
reed@google.com63100f92011-01-18 21:32:14 +0000213
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000214struct GrGLInterface;
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000216extern void GrGLCheckErr(const GrGLInterface* gl,
217 const char* location,
218 const char* call);
219
220extern void GrGLClearErr(const GrGLInterface* gl);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
bsalomon@google.com3723a482011-02-17 21:47:25 +0000222#if GR_GL_CHECK_ERROR
223 extern bool gCheckErrorGL;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000224 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
225 if (gCheckErrorGL) \
226 GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000227#else
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000228 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000229#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
231#if GR_GL_LOG_CALLS
bsalomon@google.com3723a482011-02-17 21:47:25 +0000232 extern bool gLogCallsGL;
bsalomon@google.comdec9f2d2011-08-30 18:05:17 +0000233 #define GR_GL_LOG_CALLS_IMPL(X) \
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000234 if (gLogCallsGL) \
235 GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
reed@google.comac10a2d2010-12-22 21:39:39 +0000236#else
bsalomon@google.com3723a482011-02-17 21:47:25 +0000237 #define GR_GL_LOG_CALLS_IMPL(X)
reed@google.comac10a2d2010-12-22 21:39:39 +0000238#endif
239
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000240#if GR_GL_PER_GL_FUNC_CALLBACK
241 #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
242#else
243 #define GR_GL_CALLBACK_IMPL(IFACE)
244#endif
245
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000246#define GR_GL_CALL(IFACE, X) \
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000247 do { \
248 GR_GL_CALL_NOERRCHECK(IFACE, X); \
249 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
250 } while (false)
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000251
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000252#define GR_GL_CALL_NOERRCHECK(IFACE, X) \
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +0000253 do { \
254 GR_GL_CALLBACK_IMPL(IFACE); \
255 (IFACE)->f##X; \
256 GR_GL_LOG_CALLS_IMPL(X); \
257 } while (false)
258
259#define GR_GL_CALL_RET(IFACE, RET, X) \
260 do { \
261 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
262 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
263 } while (false)
264
265#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
266 do { \
267 GR_GL_CALLBACK_IMPL(IFACE); \
268 (RET) = (IFACE)->f##X; \
269 GR_GL_LOG_CALLS_IMPL(X); \
270 } while (false)
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000271
272#define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError()
twiz@google.comb65e0cb2011-03-18 20:41:44 +0000273
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000274////////////////////////////////////////////////////////////////////////////////
275
276/**
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000277 * Some drivers want the var-int arg to be zero-initialized on input.
278 */
279#define GR_GL_INIT_ZERO 0
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000280#define GR_GL_GetIntegerv(gl, e, p) \
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000281 do { \
282 *(p) = GR_GL_INIT_ZERO; \
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000283 GR_GL_CALL(gl, GetIntegerv(e, p)); \
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000284 } while (0)
285
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000286#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
287 do { \
288 *(p) = GR_GL_INIT_ZERO; \
289 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000290 } while (0)
291
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
293 do { \
294 *(p) = GR_GL_INIT_ZERO; \
295 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000296 } while (0)
297
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000298#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
299 do { \
300 *(p) = GR_GL_INIT_ZERO; \
301 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000302 } while (0)
303
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000304////////////////////////////////////////////////////////////////////////////////
305
reed@google.com27a1e772011-03-08 15:34:06 +0000306#endif