Pyrogenesis  trunk
ogl.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 
30 #include "lib/config2.h" // CONFIG2_GLES
31 #include "lib/sysdep/os.h" // OS_WIN
32 
33 
34 #if CONFIG2_GLES
36 #else
37 # include <glad/gl.h>
38 #endif
39 
40 /**
41  * initialization: import extension function pointers and do feature detect.
42  * call before using any other function.
43  * fails if OpenGL not ready for use.
44  * TODO: move loading functionality to GL backend.
45  **/
46 #if OS_WIN
47 extern bool ogl_Init(void* (load)(const char*), void* hdc);
48 #elif !OS_MACOSX && !OS_MAC && !CONFIG2_GLES
49 extern bool ogl_Init(void* (load)(const char*), void* display, int subsystem);
50 #else
51 extern bool ogl_Init(void* (load)(const char*));
52 #endif
53 
54 /**
55  * Change vsync state.
56  **/
57 extern void ogl_SetVsyncEnabled(bool enabled);
58 
59 //-----------------------------------------------------------------------------
60 // extensions
61 
62 /**
63  * Check whether the given OpenGL extension is supported.
64  * NOTE: this does not check whether the extensions is *loaded*.
65  * for that, check whether GLAD_<extension name> is not null.
66  *
67  * @param ext extension string; exact case.
68  * @return bool.
69  **/
70 extern bool ogl_HaveExtension(const char* ext);
71 
72 /**
73  * make sure the OpenGL implementation version matches or is newer than
74  * the given version.
75  */
76 extern bool ogl_HaveVersion(int major, int minor);
77 /**
78  * check if a list of extensions are all supported (as determined by
79  * ogl_HaveExtension).
80  *
81  * @param dummy value ignored; varargs requires a placeholder.
82  * follow it by a list of const char* extension string parameters,
83  * terminated by a 0 pointer.
84  * @return 0 if all are present; otherwise, the first extension in the
85  * list that's not supported (useful for reporting errors).
86  **/
87 extern const char* ogl_HaveExtensions(int dummy, ...) SENTINEL_ARG;
88 
89 /**
90  * get a list of all supported extensions.
91  *
92  * useful for crash logs / system information.
93  *
94  * @return read-only C string of unspecified length containing all
95  * advertised extension names, separated by space.
96  **/
97 extern const char* ogl_ExtensionString();
98 
99 
100 //-----------------------------------------------------------------------------
101 // errors
102 
103 /**
104  * raise a warning (break into the debugger) if an OpenGL error is pending.
105  * resets the OpenGL error state afterwards.
106  *
107  * when an error is reported, insert calls to this in a binary-search scheme
108  * to quickly narrow down the actual error location.
109  *
110  * reports a bogus invalid_operation error if called before OpenGL is
111  * initialized, so don't!
112  *
113  * disabled in release mode for efficiency and to avoid annoying errors.
114  **/
115 extern void ogl_WarnIfErrorLoc(const char *file, int line);
116 #ifdef NDEBUG
117 # define ogl_WarnIfError()
118 #else
119 # define ogl_WarnIfError() ogl_WarnIfErrorLoc(__FILE__, __LINE__)
120 #endif
121 
122 /**
123 * get a name of the error.
124 *
125 * useful for debug.
126 *
127 * @return read-only C string of unspecified length containing
128 * the error's name.
129 **/
130 extern const char* ogl_GetErrorName(GLenum err);
131 
132 /**
133  * ignore and reset the specified OpenGL error.
134  *
135  * this is useful for suppressing annoying error messages, e.g.
136  * "invalid enum" for GL_CLAMP_TO_EDGE even though we've already
137  * warned the user that their OpenGL implementation is too old.
138  *
139  * call after the fact, i.e. the error has been raised. if another or
140  * different error is pending, those are reported immediately.
141  *
142  * @param err_to_ignore: one of the glGetError enums.
143  * @return true if the requested error was seen and ignored
144  **/
145 extern bool ogl_SquelchError(GLenum err_to_ignore);
146 
147 #endif // INCLUDED_OGL
void ogl_WarnIfErrorLoc(const char *file, int line)
raise a warning (break into the debugger) if an OpenGL error is pending.
Definition: ogl.cpp:361
#define SENTINEL_ARG
Definition: code_annotation.h:266
bool ogl_Init(void *(load)(const char *), void *display, int subsystem)
initialization: import extension function pointers and do feature detect.
Definition: ogl.cpp:432
bool ogl_HaveExtension(const char *ext)
Check whether the given OpenGL extension is supported.
Definition: ogl.cpp:191
const char * ogl_ExtensionString()
get a list of all supported extensions.
Definition: ogl.cpp:72
bool ogl_SquelchError(GLenum err_to_ignore)
ignore and reset the specified OpenGL error.
Definition: ogl.cpp:391
bool ogl_HaveVersion(int major, int minor)
make sure the OpenGL implementation version matches or is newer than the given version.
Definition: ogl.cpp:232
const char * ogl_GetErrorName(GLenum err)
get a name of the error.
Definition: ogl.cpp:337
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:256
void ogl_SetVsyncEnabled(bool enabled)
Change vsync state.
Definition: ogl.cpp:519