blob: bb10567607f130eb24580dc7f4ad17658f1929b8 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#ifndef GrGLConfig_DEFINED
19#define GrGLConfig_DEFINED
20
21#include "GrTypes.h"
22
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000023#if !defined(GR_GL_CUSTOM_SETUP)
24 #define GR_GL_CUSTOM_SETUP 0
25#endif
26/**
27 * We need to pull in the right GL headers and determine whether we are
28 * compiling for ES1, ES2, or desktop GL. (We allow ES1 and ES2 to both be
29 * supported in the same build but not ESx and desktop). We also need to know
30 * the platform-specific way to get extension function pointers (e.g.
31 * eglGetProcAddress). The port specifies this info explicitly or we will infer
32 * it from the GR_*_BUILD flag.
33 *
34 * To specify GL setup directly define GR_GL_CUSTOM_SETUP to 1 and define:
35 * GR_SUPPORT_GLDESKTOP or (GR_SUPPORT_GLES1 and/or GR_SUPPORT_GLES2) to 1
36 *
37 * if GR_SUPPORT_GLDESKTOP is 1 then provide:
38 * 1. The name of your GL header in GR_INCLUDE_GLDESKTOP
39 * 2. If necessary, the name of a file that includes extension
40 * definitions in GR_INCLUDE_GLDESKTOPext.
41 * if GR_SUPPORT_GLES1 is 1 then provide:
42 * 1. The name of your GL header in GR_INCLUDE_GLES1
43 * 2. If necessary, the name of a file that includes extension
44 * definitions in GR_INCLUDE_GLES1ext.
45 * if GR_SUPPORT_GLES2 is 1 then provide:
46 * 1. The name of your GL header in GR_INCLUDE_GLES2
47 * 2. If necessary, the name of a file that includes extension
48 * definitions in GR_INCLUDE_GLES2ext.
49 *
50 * Optionally, define GR_GL_FUNC to any qualifier needed on GL function
51 * pointer declarations (e.g. __stdcall).
52 *
53 * Define GR_GL_PROC_ADDRESS to take a gl function and produce a
54 * function pointer. Two examples:
55 * 1. Your platform doesn't require a proc address function, just take
56 * the address of the function:
57 * #define GR_GL_PROC_ADDRESS(X) &X
58 * 2. Your platform uses eglGetProcAddress:
59 * #define GR_GL_PROC_ADDRESS eglGetProcAddress(#X)
60 *
61 * Optionally define GR_GL_PROC_ADDRESS_HEADER to include any additional
62 * header necessary to use GR_GL_PROC_ADDRESS (e.g. <EGL/egl.h>)
63 *
64 * Alternatively, define GR_GL_CUSTOM_SETUP_HEADER (and not GR_GL_CUSTOM_SETUP)
65 * to a header that can be included. This file should:
66 * 1. Define the approprate GR_SUPPORT_GL* macro(s) to 1
67 * 2. Includes all necessary GL headers.
68 * 3. Optionally define GR_GL_FUNC.
69 * 4. Define GR_GL_PROC_ADDRESS.
70 * 5. Optionally define GR_GL_PROC_ADDRESS_HEADER
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000071 *
72 *
73 * The following are optional defines that can be enabled as command line macros
74 * defines, in a IDE project, in a GrUserConfig.h file, or in a GL custom setup
75 * file (if one is in use). They don't require GR_GL_CUSTOM_SETUP or
76 * GR_GL_CUSTOM_SETUP_HEADER to be enabled:
77 *
78 * GR_GL_NO_CLIENT_SIDE_ARRAYS can be defined to 1 to disable the use of client
79 * side vertex and index arrays.
80 *
81 * GR_GL_LOG_CALLS if 1 GrPrintf every GL call (for debugging purposes) when the
82 * global gPrintGL is true (it is initially true).
83
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000084 */
85
86#if GR_GL_CUSTOM_SETUP
87
88 #ifdef GR_SUPPORT_GLES1
89 #include GR_INCLUDE_GLES1
90 #if defined(GR_INCLUDE_GLES1ext)
91 #include GR_INCLUDE_GLES1ext
92 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +000093 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000094
95 #ifdef GR_SUPPORT_GLES2
96 #include GR_INCLUDE_GLES2
97 #if defined(GR_INCLUDE_GLES2ext)
98 #include GR_INCLUDE_GLES2ext
99 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000101
102 #ifdef GR_SUPPORT_GLDESKTOP
103 #include GR_INCLUDE_GLDESKTOP
104 #if defined(GR_INCLUDE_GLDESKTOPext)
105 #include GR_INCLUDE_GLDESKTOPext
106 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000108
109#elif defined(GR_GL_CUSTOM_SETUP_HEADER)
110
111 #include GR_GL_CUSTOM_SETUP_HEADER
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113#else
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000114 #undef GR_GL_FUNC
115 #undef GR_GL_PROC_ADDRESS
116 #undef GR_GL_PROC_ADDRESS_HEADER
reed@google.comac10a2d2010-12-22 21:39:39 +0000117
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000118 #if GR_WIN32_BUILD
119 #define GR_SUPPORT_GLDESKTOP 1
120 // glew has to be included before gl
121 #include <GL/glew.h>
122 #include <GL/gl.h>
123 // remove stupid windows defines
124 #undef near
125 #undef far
126 #define GR_GL_FUNC __stdcall
127 #define GR_GL_PROC_ADDRESS(X) wglGetProcAddress(#X)
128 #define GR_GL_PROC_ADDRESS_HEADER <windows.h>
129 #elif GR_MAC_BUILD
130 #define GR_SUPPORT_GLDESKTOP 1
131 #include <OpenGL/gl.h>
132 #include <OpenGL/glext.h>
133 #define GR_GL_PROC_ADDRESS(X) &X
134 #elif GR_IOS_BUILD
135 #define GR_SUPPORT_GLES1 1
136 #include <OpenGLES/ES1/gl.h>
137 #include <OpenGLES/ES1/glext.h>
138 #define GR_SUPPORT_GLES2 1
139 #include <OpenGLES/ES2/gl.h>
140 #include <OpenGLES/ES2/glext.h>
141 #define GR_GL_PROC_ADDRESS(X) &X
142 #elif GR_ANDROID_BUILD
143 #ifndef GL_GLEXT_PROTOTYPES
144 #define GL_GLEXT_PROTOTYPES
145 #endif
146 #define GR_SUPPORT_GLES2 1
147 #include <GLES2/gl2.h>
148 #include <GLES2/gl2ext.h>
149 #define GR_GL_PROC_ADDRESS(X) eglGetProcAddress(#X)
150 #define GR_GL_PROC_ADDRESS_HEADER <EGL/egl.h>
151 #elif GR_QNX_BUILD
152 #ifndef GL_GLEXT_PROTOTYPES
153 #define GL_GLEXT_PROTOTYPES
154 #endif
155 #define GR_SUPPORT_GLES2 1
156 // This is needed by the QNX GLES2 headers
157 #define GL_API_EXT
158 #include <GLES2/gl2.h>
159 #include <GLES2/gl2ext.h>
160 #define GR_GL_PROC_ADDRESS(X) eglGetProcAddress(#X)
161 #define GR_GL_PROC_ADDRESS_HEADER <EGL/egl.h>
162 #elif GR_LINUX_BUILD
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000163 #ifndef GL_GLEXT_PROTOTYPES
164 #define GL_GLEXT_PROTOTYPES
reed@google.com9341bb62011-01-26 17:11:51 +0000165 #endif
reed@google.come42e8452011-01-26 17:20:51 +0000166 #define GL_EXT_framebuffer_blit 0
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000167 #include <GL/gl.h>
168 #include <GL/glext.h>
reed@google.com9341bb62011-01-26 17:11:51 +0000169 #define GR_GL_PROC_ADDRESS(X) &X
reed@google.come42e8452011-01-26 17:20:51 +0000170 #define GR_SUPPORT_GLDESKTOP 1
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 #else
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000172 #error "unsupported GR_???_BUILD"
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000174
reed@google.comac10a2d2010-12-22 21:39:39 +0000175#endif
176
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000177#if !defined(GR_SUPPORT_GLDESKTOP)
178 #define GR_SUPPORT_GLDESKTOP 0
179#endif
180#if !defined(GR_SUPPORT_GLES1)
181 #define GR_SUPPORT_GLES1 0
182#endif
183#if !defined(GR_SUPPORT_GLES2)
184 #define GR_SUPPORT_GLES2 0
reed@google.comac10a2d2010-12-22 21:39:39 +0000185#endif
186
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000187#define GR_SUPPORT_GLES ((GR_SUPPORT_GLES1) || (GR_SUPPORT_GLES2))
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000188
reed@google.combf858b72011-01-19 19:04:04 +0000189#if !GR_SUPPORT_GLES && !GR_SUPPORT_GLDESKTOP
190 #error "Either desktop or ES GL must be supported"
191#elif GR_SUPPORT_GLES && GR_SUPPORT_GLDESKTOP
192 #error "Cannot support both desktop and ES GL"
reed@google.comac10a2d2010-12-22 21:39:39 +0000193#endif
194
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000195#if !defined(GR_GL_FUNC)
196 #define GR_GL_FUNC
reed@google.comac10a2d2010-12-22 21:39:39 +0000197#endif
198
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000199#if !defined(GR_GL_PROC_ADDRESS)
200 #error "Must define GR_GL_PROC_ADDRESS"
reed@google.comac10a2d2010-12-22 21:39:39 +0000201#endif
202
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000203#if !defined(GR_GL_LOG_CALLS)
204 #define GR_GL_LOG_CALLS 0
205#endif
206
207#if !defined(GR_GL_NO_CLIENT_SIDE_ARRAYS)
bsalomon@google.com1b7c1b62011-02-11 14:45:11 +0000208 #define GR_GL_NO_CLIENT_SIDE_ARRAYS 0
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000209#endif
210
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000211////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213#if GR_SCALAR_IS_FIXED
214 #define GrGLType GL_FIXED
215#elif GR_SCALAR_IS_FLOAT
216 #define GrGLType GL_FLOAT
217#else
218 #error "unknown GR_SCALAR type"
219#endif
220
221#if GR_TEXT_SCALAR_IS_USHORT
222 #define GrGLTextType GL_UNSIGNED_SHORT
223 #define GR_GL_TEXT_TEXTURE_NORMALIZED 1
224#elif GR_TEXT_SCALAR_IS_FLOAT
225 #define GrGLTextType GL_FLOAT
226 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
227#elif GR_TEXT_SCALAR_IS_FIXED
228 #define GrGLTextType GL_FIXED
229 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
reed@google.com63100f92011-01-18 21:32:14 +0000230#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 #error "unknown GR_TEXT_SCALAR type"
232#endif
233
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000234// Pick a pixel config for 32bit bitmaps. Our default is GL_RGBA (expect on
235// Windows where we match GDI's order).
236#ifndef GR_GL_32BPP_COLOR_FORMAT
237 #if GR_WIN32_BUILD
bsalomon@google.comed3a0682011-01-18 16:54:04 +0000238 #define GR_GL_32BPP_COLOR_FORMAT GR_BGRA //use GR prefix because this
239 #else //may be an extension.
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000240 #define GR_GL_32BPP_COLOR_FORMAT GL_RGBA
241 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000242#endif
243
244////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000245// Setup for opengl ES/desktop extensions
246// We make a struct of function pointers so that each GL context
reed@google.com63100f92011-01-18 21:32:14 +0000247// can have it's own struct. (Some environments may have different proc
reed@google.comac10a2d2010-12-22 21:39:39 +0000248// addresses for different contexts).
249
250extern "C" {
251struct GrGLExts {
252// FBO
253 GLvoid (GR_GL_FUNC *GenFramebuffers)(GLsizei n, GLuint *framebuffers);
254 GLvoid (GR_GL_FUNC *BindFramebuffer)(GLenum target, GLuint framebuffer);
255 GLvoid (GR_GL_FUNC *FramebufferTexture2D)(GLenum target, GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000256 GLenum textarget, GLuint texture,
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 GLint level);
258 GLenum (GR_GL_FUNC *CheckFramebufferStatus)(GLenum target);
reed@google.com63100f92011-01-18 21:32:14 +0000259 GLvoid (GR_GL_FUNC *DeleteFramebuffers)(GLsizei n, const
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 GLuint *framebuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000261 GLvoid (GR_GL_FUNC *RenderbufferStorage)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000262 GLenum internalformat,
263 GLsizei width, GLsizei height);
264 GLvoid (GR_GL_FUNC *GenRenderbuffers)(GLsizei n, GLuint *renderbuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000265 GLvoid (GR_GL_FUNC *DeleteRenderbuffers)(GLsizei n,
reed@google.comac10a2d2010-12-22 21:39:39 +0000266 const GLuint *renderbuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000267 GLvoid (GR_GL_FUNC *FramebufferRenderbuffer)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000269 GLenum renderbuffertarget,
reed@google.comac10a2d2010-12-22 21:39:39 +0000270 GLuint renderbuffer);
271 GLvoid (GR_GL_FUNC *BindRenderbuffer)(GLenum target, GLuint renderbuffer);
272
273// Multisampling
274 // same prototype for ARB_FBO, EXT_FBO, GL 3.0, & Apple ES extension
reed@google.com63100f92011-01-18 21:32:14 +0000275 GLvoid (GR_GL_FUNC *RenderbufferStorageMultisample)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 GLsizei samples,
277 GLenum internalformat,
reed@google.com63100f92011-01-18 21:32:14 +0000278 GLsizei width,
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 GLsizei height);
280 // desktop: ext_fbo_blit, arb_fbo, gl 3.0
reed@google.com63100f92011-01-18 21:32:14 +0000281 GLvoid (GR_GL_FUNC *BlitFramebuffer)(GLint srcX0, GLint srcY0,
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 GLint srcX1, GLint srcY1,
reed@google.com63100f92011-01-18 21:32:14 +0000283 GLint dstX0, GLint dstY0,
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 GLint dstX1, GLint dstY1,
285 GLbitfield mask, GLenum filter);
286 // apple's es extension
287 GLvoid (GR_GL_FUNC *ResolveMultisampleFramebuffer)();
288
289 // IMG'e es extension
reed@google.com63100f92011-01-18 21:32:14 +0000290 GLvoid (GR_GL_FUNC *FramebufferTexture2DMultisample)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000292 GLenum textarget,
293 GLuint texture,
294 GLint level,
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 GLsizei samples);
296
297// Buffer mapping (extension in ES).
298 GLvoid* (GR_GL_FUNC *MapBuffer)(GLenum target, GLenum access);
299 GLboolean (GR_GL_FUNC *UnmapBuffer)(GLenum target);
300};
301}
302
bsalomon@google.comed3a0682011-01-18 16:54:04 +0000303// BGRA format
304
305#define GR_BGRA 0x80E1
306
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000307// FBO / stencil formats
reed@google.comac10a2d2010-12-22 21:39:39 +0000308#define GR_FRAMEBUFFER 0x8D40
309#define GR_FRAMEBUFFER_COMPLETE 0x8CD5
310#define GR_COLOR_ATTACHMENT0 0x8CE0
reed@google.com63100f92011-01-18 21:32:14 +0000311#define GR_FRAMEBUFFER_BINDING 0x8CA6
reed@google.comac10a2d2010-12-22 21:39:39 +0000312#define GR_RENDERBUFFER 0x8D41
313#define GR_STENCIL_ATTACHMENT 0x8D20
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000314#define GR_STENCIL_INDEX4 0x8D47
reed@google.comac10a2d2010-12-22 21:39:39 +0000315#define GR_STENCIL_INDEX8 0x8D48
316#define GR_STENCIL_INDEX16 0x8D49
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000317#define GR_DEPTH24_STENCIL8 0x88F0
reed@google.comac10a2d2010-12-22 21:39:39 +0000318#define GR_MAX_RENDERBUFFER_SIZE 0x84E8
319#define GR_DEPTH_STENCIL_ATTACHMENT 0x821A
reed@google.comac10a2d2010-12-22 21:39:39 +0000320#define GR_DEPTH_STENCIL 0x84F9
321#define GR_RGBA8 0x8058
322#define GR_RGB565 0x8D62
323
324
325// Multisampling
326
327// IMG MAX_SAMPLES uses a different value than desktop, Apple ES extension.
328#define GR_MAX_SAMPLES 0x8D57
329#define GR_MAX_SAMPLES_IMG 0x9135
330#define GR_READ_FRAMEBUFFER 0x8CA8
331#define GR_DRAW_FRAMEBUFFER 0x8CA9
332
333// Buffer mapping
334#define GR_WRITE_ONLY 0x88B9
335#define GR_BUFFER_MAPPED 0x88BC
336
337// Palette texture
338#define GR_PALETTE8_RGBA8 0x8B91
339
340extern void GrGLInitExtensions(GrGLExts* exts);
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000341
reed@google.comac10a2d2010-12-22 21:39:39 +0000342////////////////////////////////////////////////////////////////////////////////
reed@google.com63100f92011-01-18 21:32:14 +0000343
reed@google.comac10a2d2010-12-22 21:39:39 +0000344extern void GrGLCheckErr(const char* location, const char* call);
345
346static inline void GrGLClearErr() {
reed@google.com63100f92011-01-18 21:32:14 +0000347 while (GL_NO_ERROR != glGetError()) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000348}
349
350// GR_FORCE_GLCHECKERR can be defined by GrUserConfig.h
351#if defined(GR_FORCE_GLCHECKERR)
352 #define GR_LOCAL_CALL_CHECKERR GR_FORCE_GLCHECKERR
353#else
354 #define GR_LOCAL_CALL_CHECKERR GR_DEBUG
355#endif
356static inline void GrDebugGLCheckErr(const char* location, const char* call) {
357#if GR_LOCAL_CALL_CHECKERR
358 GrGLCheckErr(location, call);
359#endif
360}
361#undef GR_LOCAL_CALL_CHECKERR
362
363#if GR_GL_LOG_CALLS
364 extern bool gPrintGL;
365 #define GR_GL(X) gl ## X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X); if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
366 #define GR_GL_NO_ERR(X) GrGLClearErr(); gl ## X; if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
367 #define GR_GLEXT(exts, X) exts. X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X); if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
368 #define GR_GLEXT_NO_ERR(exts, X) GrGLClearErr(); exts. X; if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
369#else
370 #define GR_GL(X) gl ## X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X)
371 #define GR_GL_NO_ERR(X) GrGLClearErr(); gl ## X
372 #define GR_GLEXT(exts, X) exts. X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X)
373 #define GR_GLEXT_NO_ERR(exts, X) GrGLClearErr(); exts. X
374#endif
375
376#endif
377