blob: cdcc97f0ca4755cea5433fb833b98b70bce893ea [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
71 */
72
73#if GR_GL_CUSTOM_SETUP
74
75 #ifdef GR_SUPPORT_GLES1
76 #include GR_INCLUDE_GLES1
77 #if defined(GR_INCLUDE_GLES1ext)
78 #include GR_INCLUDE_GLES1ext
79 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +000080 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000081
82 #ifdef GR_SUPPORT_GLES2
83 #include GR_INCLUDE_GLES2
84 #if defined(GR_INCLUDE_GLES2ext)
85 #include GR_INCLUDE_GLES2ext
86 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +000087 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000088
89 #ifdef GR_SUPPORT_GLDESKTOP
90 #include GR_INCLUDE_GLDESKTOP
91 #if defined(GR_INCLUDE_GLDESKTOPext)
92 #include GR_INCLUDE_GLDESKTOPext
93 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +000094 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +000095
96#elif defined(GR_GL_CUSTOM_SETUP_HEADER)
97
98 #include GR_GL_CUSTOM_SETUP_HEADER
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000102 #if GR_WIN32_BUILD
103 #define GR_SUPPORT_GLDESKTOP 1
104 // glew has to be included before gl
105 #include <GL/glew.h>
106 #include <GL/gl.h>
107 // remove stupid windows defines
108 #undef near
109 #undef far
110 #define GR_GL_FUNC __stdcall
111 #define GR_GL_PROC_ADDRESS(X) wglGetProcAddress(#X)
112 #define GR_GL_PROC_ADDRESS_HEADER <windows.h>
113 #elif GR_MAC_BUILD
114 #define GR_SUPPORT_GLDESKTOP 1
115 #include <OpenGL/gl.h>
116 #include <OpenGL/glext.h>
117 #define GR_GL_PROC_ADDRESS(X) &X
118 #elif GR_IOS_BUILD
119 #define GR_SUPPORT_GLES1 1
120 #include <OpenGLES/ES1/gl.h>
121 #include <OpenGLES/ES1/glext.h>
122 #define GR_SUPPORT_GLES2 1
123 #include <OpenGLES/ES2/gl.h>
124 #include <OpenGLES/ES2/glext.h>
125 #define GR_GL_PROC_ADDRESS(X) &X
126 #elif GR_ANDROID_BUILD
127 #ifndef GL_GLEXT_PROTOTYPES
128 #define GL_GLEXT_PROTOTYPES
129 #endif
130 #define GR_SUPPORT_GLES2 1
131 #include <GLES2/gl2.h>
132 #include <GLES2/gl2ext.h>
133 #define GR_GL_PROC_ADDRESS(X) eglGetProcAddress(#X)
134 #define GR_GL_PROC_ADDRESS_HEADER <EGL/egl.h>
135 #elif GR_QNX_BUILD
136 #ifndef GL_GLEXT_PROTOTYPES
137 #define GL_GLEXT_PROTOTYPES
138 #endif
139 #define GR_SUPPORT_GLES2 1
140 // This is needed by the QNX GLES2 headers
141 #define GL_API_EXT
142 #include <GLES2/gl2.h>
143 #include <GLES2/gl2ext.h>
144 #define GR_GL_PROC_ADDRESS(X) eglGetProcAddress(#X)
145 #define GR_GL_PROC_ADDRESS_HEADER <EGL/egl.h>
146 #elif GR_LINUX_BUILD
147 #define GR_SUPPORT_GLDESKTOP 1
148 #include <GL/gl.h>
149 #include <GL/glext.h>
150 #define GR_GL_PROC_ADDRESS(X) eglGetProcAddress(#X)
151 #define GR_GL_PROC_ADDRESS_HEADER <EGL/egl.h>
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 #else
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000153 #error "unsupported GR_???_BUILD"
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 #endif
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000155
reed@google.comac10a2d2010-12-22 21:39:39 +0000156#endif
157
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000158#if !defined(GR_SUPPORT_GLDESKTOP)
159 #define GR_SUPPORT_GLDESKTOP 0
160#endif
161#if !defined(GR_SUPPORT_GLES1)
162 #define GR_SUPPORT_GLES1 0
163#endif
164#if !defined(GR_SUPPORT_GLES2)
165 #define GR_SUPPORT_GLES2 0
reed@google.comac10a2d2010-12-22 21:39:39 +0000166#endif
167
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000168#define GR_SUPPORT_GLES ((GR_SUPPORT_GLES1) || (GR_SUPPORT_GLES2))
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000169
reed@google.combf858b72011-01-19 19:04:04 +0000170#if !GR_SUPPORT_GLES && !GR_SUPPORT_GLDESKTOP
171 #error "Either desktop or ES GL must be supported"
172#elif GR_SUPPORT_GLES && GR_SUPPORT_GLDESKTOP
173 #error "Cannot support both desktop and ES GL"
reed@google.comac10a2d2010-12-22 21:39:39 +0000174#endif
175
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000176#if !defined(GR_GL_FUNC)
177 #define GR_GL_FUNC
reed@google.comac10a2d2010-12-22 21:39:39 +0000178#endif
179
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000180#if !defined(GR_GL_PROC_ADDRESS)
181 #error "Must define GR_GL_PROC_ADDRESS"
reed@google.comac10a2d2010-12-22 21:39:39 +0000182#endif
183
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000184////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
reed@google.comac10a2d2010-12-22 21:39:39 +0000186#if GR_SCALAR_IS_FIXED
187 #define GrGLType GL_FIXED
188#elif GR_SCALAR_IS_FLOAT
189 #define GrGLType GL_FLOAT
190#else
191 #error "unknown GR_SCALAR type"
192#endif
193
194#if GR_TEXT_SCALAR_IS_USHORT
195 #define GrGLTextType GL_UNSIGNED_SHORT
196 #define GR_GL_TEXT_TEXTURE_NORMALIZED 1
197#elif GR_TEXT_SCALAR_IS_FLOAT
198 #define GrGLTextType GL_FLOAT
199 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
200#elif GR_TEXT_SCALAR_IS_FIXED
201 #define GrGLTextType GL_FIXED
202 #define GR_GL_TEXT_TEXTURE_NORMALIZED 0
reed@google.com63100f92011-01-18 21:32:14 +0000203#else
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 #error "unknown GR_TEXT_SCALAR type"
205#endif
206
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000207// Pick a pixel config for 32bit bitmaps. Our default is GL_RGBA (expect on
208// Windows where we match GDI's order).
209#ifndef GR_GL_32BPP_COLOR_FORMAT
210 #if GR_WIN32_BUILD
bsalomon@google.comed3a0682011-01-18 16:54:04 +0000211 #define GR_GL_32BPP_COLOR_FORMAT GR_BGRA //use GR prefix because this
212 #else //may be an extension.
bsalomon@google.com2fbc7fa2011-01-05 16:34:41 +0000213 #define GR_GL_32BPP_COLOR_FORMAT GL_RGBA
214 #endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000215#endif
216
217////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000218// Setup for opengl ES/desktop extensions
219// We make a struct of function pointers so that each GL context
reed@google.com63100f92011-01-18 21:32:14 +0000220// can have it's own struct. (Some environments may have different proc
reed@google.comac10a2d2010-12-22 21:39:39 +0000221// addresses for different contexts).
222
223extern "C" {
224struct GrGLExts {
225// FBO
226 GLvoid (GR_GL_FUNC *GenFramebuffers)(GLsizei n, GLuint *framebuffers);
227 GLvoid (GR_GL_FUNC *BindFramebuffer)(GLenum target, GLuint framebuffer);
228 GLvoid (GR_GL_FUNC *FramebufferTexture2D)(GLenum target, GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000229 GLenum textarget, GLuint texture,
reed@google.comac10a2d2010-12-22 21:39:39 +0000230 GLint level);
231 GLenum (GR_GL_FUNC *CheckFramebufferStatus)(GLenum target);
reed@google.com63100f92011-01-18 21:32:14 +0000232 GLvoid (GR_GL_FUNC *DeleteFramebuffers)(GLsizei n, const
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 GLuint *framebuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000234 GLvoid (GR_GL_FUNC *RenderbufferStorage)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 GLenum internalformat,
236 GLsizei width, GLsizei height);
237 GLvoid (GR_GL_FUNC *GenRenderbuffers)(GLsizei n, GLuint *renderbuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000238 GLvoid (GR_GL_FUNC *DeleteRenderbuffers)(GLsizei n,
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 const GLuint *renderbuffers);
reed@google.com63100f92011-01-18 21:32:14 +0000240 GLvoid (GR_GL_FUNC *FramebufferRenderbuffer)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000242 GLenum renderbuffertarget,
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 GLuint renderbuffer);
244 GLvoid (GR_GL_FUNC *BindRenderbuffer)(GLenum target, GLuint renderbuffer);
245
246// Multisampling
247 // same prototype for ARB_FBO, EXT_FBO, GL 3.0, & Apple ES extension
reed@google.com63100f92011-01-18 21:32:14 +0000248 GLvoid (GR_GL_FUNC *RenderbufferStorageMultisample)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 GLsizei samples,
250 GLenum internalformat,
reed@google.com63100f92011-01-18 21:32:14 +0000251 GLsizei width,
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 GLsizei height);
253 // desktop: ext_fbo_blit, arb_fbo, gl 3.0
reed@google.com63100f92011-01-18 21:32:14 +0000254 GLvoid (GR_GL_FUNC *BlitFramebuffer)(GLint srcX0, GLint srcY0,
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 GLint srcX1, GLint srcY1,
reed@google.com63100f92011-01-18 21:32:14 +0000256 GLint dstX0, GLint dstY0,
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 GLint dstX1, GLint dstY1,
258 GLbitfield mask, GLenum filter);
259 // apple's es extension
260 GLvoid (GR_GL_FUNC *ResolveMultisampleFramebuffer)();
261
262 // IMG'e es extension
reed@google.com63100f92011-01-18 21:32:14 +0000263 GLvoid (GR_GL_FUNC *FramebufferTexture2DMultisample)(GLenum target,
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 GLenum attachment,
reed@google.com63100f92011-01-18 21:32:14 +0000265 GLenum textarget,
266 GLuint texture,
267 GLint level,
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 GLsizei samples);
269
270// Buffer mapping (extension in ES).
271 GLvoid* (GR_GL_FUNC *MapBuffer)(GLenum target, GLenum access);
272 GLboolean (GR_GL_FUNC *UnmapBuffer)(GLenum target);
273};
274}
275
bsalomon@google.comed3a0682011-01-18 16:54:04 +0000276// BGRA format
277
278#define GR_BGRA 0x80E1
279
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000280// FBO / stencil formats
reed@google.comac10a2d2010-12-22 21:39:39 +0000281#define GR_FRAMEBUFFER 0x8D40
282#define GR_FRAMEBUFFER_COMPLETE 0x8CD5
283#define GR_COLOR_ATTACHMENT0 0x8CE0
reed@google.com63100f92011-01-18 21:32:14 +0000284#define GR_FRAMEBUFFER_BINDING 0x8CA6
reed@google.comac10a2d2010-12-22 21:39:39 +0000285#define GR_RENDERBUFFER 0x8D41
286#define GR_STENCIL_ATTACHMENT 0x8D20
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000287#define GR_STENCIL_INDEX4 0x8D47
reed@google.comac10a2d2010-12-22 21:39:39 +0000288#define GR_STENCIL_INDEX8 0x8D48
289#define GR_STENCIL_INDEX16 0x8D49
bsalomon@google.com3f3ffd62011-01-18 17:14:52 +0000290#define GR_DEPTH24_STENCIL8 0x88F0
reed@google.comac10a2d2010-12-22 21:39:39 +0000291#define GR_MAX_RENDERBUFFER_SIZE 0x84E8
292#define GR_DEPTH_STENCIL_ATTACHMENT 0x821A
reed@google.comac10a2d2010-12-22 21:39:39 +0000293#define GR_DEPTH_STENCIL 0x84F9
294#define GR_RGBA8 0x8058
295#define GR_RGB565 0x8D62
296
297
298// Multisampling
299
300// IMG MAX_SAMPLES uses a different value than desktop, Apple ES extension.
301#define GR_MAX_SAMPLES 0x8D57
302#define GR_MAX_SAMPLES_IMG 0x9135
303#define GR_READ_FRAMEBUFFER 0x8CA8
304#define GR_DRAW_FRAMEBUFFER 0x8CA9
305
306// Buffer mapping
307#define GR_WRITE_ONLY 0x88B9
308#define GR_BUFFER_MAPPED 0x88BC
309
310// Palette texture
311#define GR_PALETTE8_RGBA8 0x8B91
312
313extern void GrGLInitExtensions(GrGLExts* exts);
bsalomon@google.com42ab7ea2011-01-19 17:19:40 +0000314
reed@google.comac10a2d2010-12-22 21:39:39 +0000315////////////////////////////////////////////////////////////////////////////////
reed@google.com63100f92011-01-18 21:32:14 +0000316
reed@google.comac10a2d2010-12-22 21:39:39 +0000317extern void GrGLCheckErr(const char* location, const char* call);
318
319static inline void GrGLClearErr() {
reed@google.com63100f92011-01-18 21:32:14 +0000320 while (GL_NO_ERROR != glGetError()) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000321}
322
323// GR_FORCE_GLCHECKERR can be defined by GrUserConfig.h
324#if defined(GR_FORCE_GLCHECKERR)
325 #define GR_LOCAL_CALL_CHECKERR GR_FORCE_GLCHECKERR
326#else
327 #define GR_LOCAL_CALL_CHECKERR GR_DEBUG
328#endif
329static inline void GrDebugGLCheckErr(const char* location, const char* call) {
330#if GR_LOCAL_CALL_CHECKERR
331 GrGLCheckErr(location, call);
332#endif
333}
334#undef GR_LOCAL_CALL_CHECKERR
335
336#if GR_GL_LOG_CALLS
337 extern bool gPrintGL;
338 #define GR_GL(X) gl ## X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X); if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
339 #define GR_GL_NO_ERR(X) GrGLClearErr(); gl ## X; if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
340 #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")
341 #define GR_GLEXT_NO_ERR(exts, X) GrGLClearErr(); exts. X; if (gPrintGL) GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
342#else
343 #define GR_GL(X) gl ## X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X)
344 #define GR_GL_NO_ERR(X) GrGLClearErr(); gl ## X
345 #define GR_GLEXT(exts, X) exts. X; GrDebugGLCheckErr(GR_FILE_AND_LINE_STR, #X)
346 #define GR_GLEXT_NO_ERR(exts, X) GrGLClearErr(); exts. X
347#endif
348
349#endif
350