Pyrogenesis  trunk
ogl.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * OpenGL helper functions.
25  */
26 
27 #ifndef INCLUDED_OGL
28 #define INCLUDED_OGL
29 
31 
32 
33 /**
34  * initialization: import extension function pointers and do feature detect.
35  * call before using any other function, and after each video mode change.
36  * fails if OpenGL not ready for use.
37  **/
38 extern void ogl_Init();
39 
40 
41 //-----------------------------------------------------------------------------
42 // extensions
43 
44 /**
45  * check if an extension is supported by the OpenGL implementation.
46  *
47  * takes subsequently added core support for some extensions into account
48  * (in case drivers forget to advertise extensions).
49  *
50  * @param ext extension string; exact case.
51  * @return bool.
52  **/
53 extern bool ogl_HaveExtension(const char* ext);
54 
55 /**
56  * make sure the OpenGL implementation version matches or is newer than
57  * the given version.
58  *
59  * @param version version string; format: ("%d.%d", major, minor).
60  * example: "1.2".
61  **/
62 extern bool ogl_HaveVersion(const char* version);
63 
64 /**
65  * check if a list of extensions are all supported (as determined by
66  * ogl_HaveExtension).
67  *
68  * @param dummy value ignored; varargs requires a placeholder.
69  * follow it by a list of const char* extension string parameters,
70  * terminated by a 0 pointer.
71  * @return 0 if all are present; otherwise, the first extension in the
72  * list that's not supported (useful for reporting errors).
73  **/
74 extern const char* ogl_HaveExtensions(int dummy, ...) SENTINEL_ARG;
75 
76 /**
77  * get a list of all supported extensions.
78  *
79  * useful for crash logs / system information.
80  *
81  * @return read-only C string of unspecified length containing all
82  * advertised extension names, separated by space.
83  **/
84 extern const char* ogl_ExtensionString();
85 
86 // The game wants to use some extension constants that aren't provided by
87 // glext.h on some old systems.
88 // Manually define all the necessary ones that are missing from
89 // GL_GLEXT_VERSION 39 (Mesa 7.0) since that's probably an old enough baseline:
90 #ifndef GL_VERSION_3_0
91 # define GL_MIN_PROGRAM_TEXEL_OFFSET 0x8904
92 # define GL_MAX_PROGRAM_TEXEL_OFFSET 0x8905
93 #endif
94 #ifndef GL_EXT_transform_feedback
95 # define GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS_EXT 0x8C8A
96 # define GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS_EXT 0x8C8B
97 # define GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS_EXT 0x8C80
98 #endif
99 #ifndef GL_ARB_geometry_shader4
100 # define GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS_ARB 0x8C29
101 # define GL_MAX_GEOMETRY_VARYING_COMPONENTS_ARB 0x8DDD
102 # define GL_MAX_VERTEX_VARYING_COMPONENTS_ARB 0x8DDE
103 # define GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_ARB 0x8DDF
104 # define GL_MAX_GEOMETRY_OUTPUT_VERTICES_ARB 0x8DE0
105 # define GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS_ARB 0x8DE1
106 #endif
107 #ifndef GL_ARB_timer_query
108 # define GL_TIME_ELAPSED 0x88BF
109 # define GL_TIMESTAMP 0x8E28
110 #endif
111 #ifndef GL_ARB_framebuffer_object
112 # define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
113 #endif
114 // Also need some more for OS X 10.5:
115 #ifndef GL_EXT_texture_array
116 # define GL_MAX_ARRAY_TEXTURE_LAYERS_EXT 0x88FF
117 #endif
118 // Also need some types not in old glext.h:
119 #ifndef GL_ARB_sync
120  typedef int64_t GLint64;
122 #endif
123 
124 // declare extension function pointers
125 #if OS_WIN
126 # define GL_CALL_CONV __stdcall
127 #else
128 # define GL_CALL_CONV
129 #endif
130 #define FUNC(ret, name, params) EXTERN_C ret (GL_CALL_CONV *p##name) params;
131 #define FUNC2(ret, nameARB, nameCore, version, params) EXTERN_C ret (GL_CALL_CONV *p##nameARB) params;
132 #define FUNC3(ret, nameARB, nameCore, version, params) EXTERN_C ret (GL_CALL_CONV *p##nameCore) params;
134 #undef FUNC3
135 #undef FUNC2
136 #undef FUNC
137 // leave GL_CALL_CONV defined for ogl.cpp
138 
139 
140 //-----------------------------------------------------------------------------
141 // errors
142 
143 /**
144  * raise a warning (break into the debugger) if an OpenGL error is pending.
145  * resets the OpenGL error state afterwards.
146  *
147  * when an error is reported, insert calls to this in a binary-search scheme
148  * to quickly narrow down the actual error location.
149  *
150  * reports a bogus invalid_operation error if called before OpenGL is
151  * initialized, so don't!
152  *
153  * disabled in release mode for efficiency and to avoid annoying errors.
154  **/
155 extern void ogl_WarnIfErrorLoc(const char *file, int line);
156 #ifdef NDEBUG
157 # define ogl_WarnIfError()
158 #else
159 # define ogl_WarnIfError() ogl_WarnIfErrorLoc(__FILE__, __LINE__)
160 #endif
161 
162 /**
163  * ignore and reset the specified OpenGL error.
164  *
165  * this is useful for suppressing annoying error messages, e.g.
166  * "invalid enum" for GL_CLAMP_TO_EDGE even though we've already
167  * warned the user that their OpenGL implementation is too old.
168  *
169  * call after the fact, i.e. the error has been raised. if another or
170  * different error is pending, those are reported immediately.
171  *
172  * @param err_to_ignore: one of the glGetError enums.
173  * @return true if the requested error was seen and ignored
174  **/
175 extern bool ogl_SquelchError(GLenum err_to_ignore);
176 
177 
178 //-----------------------------------------------------------------------------
179 // implementation limits / feature detect
180 
181 extern GLint ogl_max_tex_size; /// [pixels]
182 extern GLint ogl_max_tex_units; /// limit on GL_TEXTUREn
183 
184 #endif // #ifndef INCLUDED_OGL
GLint ogl_max_tex_units
[pixels]
Definition: ogl.cpp:466
int64_t GLint64
Definition: ogl.h:120
bool ogl_HaveVersion(const char *version)
make sure the OpenGL implementation version matches or is newer than the given version.
Definition: ogl.cpp:219
void ogl_WarnIfErrorLoc(const char *file, int line)
raise a warning (break into the debugger) if an OpenGL error is pending.
Definition: ogl.cpp:396
#define SENTINEL_ARG
Definition: code_annotation.h:254
uint64_t GLuint64
Definition: ogl.h:121
bool ogl_HaveExtension(const char *ext)
check if an extension is supported by the OpenGL implementation.
Definition: ogl.cpp:187
unsigned long long uint64_t
Definition: wposix_types.h:57
const char * ogl_ExtensionString()
get a list of all supported extensions.
Definition: ogl.cpp:68
bool ogl_SquelchError(GLenum err_to_ignore)
ignore and reset the specified OpenGL error.
Definition: ogl.cpp:427
GLint ogl_max_tex_size
Definition: ogl.cpp:465
const char * ogl_HaveExtensions(int dummy,...) SENTINEL_ARG
check if a list of extensions are all supported (as determined by ogl_HaveExtension).
Definition: ogl.cpp:266
void ogl_Init()
initialization: import extension function pointers and do feature detect.
Definition: ogl.cpp:470
long long int64_t
Definition: wposix_types.h:48