]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Require the first keyframe of an animation to be at zero offset
[libs/gl.git] / source / extension.cpp
1 #include <set>
2 #include <cstdlib>
3 #if defined(__ANDROID__)
4 #include <EGL/egl.h>
5 #elif !defined(_WIN32) && !defined(__APPLE__)
6 #define GLX_GLXEXT_PROTOTYPES
7 #include <GL/glx.h>
8 #endif
9 #include <msp/strings/format.h>
10 #include <msp/strings/utils.h>
11 #include "error.h"
12 #include "extension.h"
13 #include "gl.h"
14
15 #ifndef GL_VERSION_3_0
16 #define GL_NUM_EXTENSIONS 0x821D
17 #endif
18
19 #ifndef GL_VERSION_3_2
20 #define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
21 #define GL_CONTEXT_PROFILE_MASK 0x9126
22 #endif
23
24 using namespace std;
25
26 namespace Msp {
27 namespace GL {
28
29 Version::Version()
30 {
31         major = 0;
32         minor = 0;
33 }
34
35 Version::Version(unsigned short a, unsigned short i)
36 {
37         major = a;
38         minor = i;
39 }
40
41 Version::Version(const string &s)
42 {
43         vector<string> parts = split(s, '.');
44         major = lexical_cast<unsigned>(parts[0]);
45         minor = lexical_cast<unsigned>(parts[1]);
46 }
47
48 bool Version::operator>=(const Version &other) const
49 {
50         return major>other.major || (major==other.major && minor>=other.minor);
51 }
52
53
54 Extension::Extension(const char *n, InitFunc f):
55         name(n),
56         init_func(f),
57         init_done(false),
58         support(UNSUPPORTED)
59 { }
60
61 Extension::operator bool() const
62 {
63         if(!init_done)
64         {
65                 support = init_func();
66                 init_done = true;
67         }
68
69         return support>UNSUPPORTED;
70 }
71
72
73 Require::Require(const Extension &ext)
74 {
75         if(!ext)
76                 throw unsupported_extension(ext.get_name());
77 }
78
79
80 bool is_supported(const string &ext)
81 {
82         if(is_disabled(ext))
83                 return false;
84
85         static set<string> extensions;
86         static bool init_done = false;
87
88         if(!init_done)
89         {
90                 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
91                 {
92                         typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
93                         FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
94                         int n_extensions;
95                         glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
96                         for(int i=0; i<n_extensions; ++i)
97                                 extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
98                 }
99                 else
100                 {
101                         if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
102                         {
103                                 vector<string> exts = split(gl_ext);
104                                 extensions.insert(exts.begin(), exts.end());
105                         }
106                 }
107
108                 init_done = true;
109         }
110
111         return extensions.count(ext);
112 }
113
114 bool is_supported(const Version &core_version, const Version &deprecated_version)
115 {
116         const Version &version = get_gl_version();
117         if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
118                 return false;
119         return (version>=core_version);
120 }
121
122 bool is_disabled(const string &ext)
123 {
124         static set<string> disabled_exts;
125         static bool init_done = false;
126
127         if(!init_done)
128         {
129                 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
130                 {
131                         vector<string> disable = split(disable_ptr);
132                         disabled_exts.insert(disable.begin(), disable.end());
133                 }
134
135                 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
136                 {
137                         string renderer = renderer_ptr;
138                         if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
139                         {
140                                 /* Radeon doesn't process NV_primitive_restart correctly and treats
141                                 the restart index as a normal element if the indices are stored in a
142                                 buffer. */
143                                 disabled_exts.insert("GL_NV_primitive_restart");
144
145                                 // The core primitive restart feature does not work either.
146                                 disabled_exts.insert("GL_MSP_primitive_restart");
147
148                                 /* AMD's uniform buffer objects only work with the core version of
149                                 shaders. */
150                                 if(get_gl_version()<Version(2, 0))
151                                         disabled_exts.insert("GL_ARB_uniform_buffer_object");
152                         }
153                 }
154
155                 init_done = true;
156         }
157
158         return disabled_exts.count(ext);
159 }
160
161 GLApi get_gl_api()
162 {
163 #ifdef GL_ES_VERSION_2_0
164         return OPENGL_ES2;
165 #else
166         return OPENGL;
167 #endif
168 }
169
170 inline GLProfile _get_gl_profile()
171 {
172         if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
173         {
174                 int mask;
175                 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
176                 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
177                         return CORE_PROFILE;
178         }
179
180         return COMPATIBILITY_PROFILE;
181 }
182
183 GLProfile get_gl_profile()
184 {
185         static GLProfile profile = _get_gl_profile();
186         return profile;
187 }
188
189 inline Version _get_gl_version()
190 {
191         const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
192         if(!gl_ver_ptr)
193                 throw runtime_error("OpenGL version not available");
194
195         string gl_ver = gl_ver_ptr;
196         if(!gl_ver.compare(0, 10, "OpenGL ES "))
197                 gl_ver.erase(0, 10);
198
199         Version ver(gl_ver.substr(0, gl_ver.find(' ')));
200
201         if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
202         {
203                 Version force_ver(force_ver_ptr);
204                 if(force_ver<ver)
205                         ver = force_ver;
206         }
207
208         return ver;
209 }
210
211 const Version &get_gl_version()
212 {
213         static Version version = _get_gl_version();
214         return version;
215 }
216
217 inline Version _get_glsl_version()
218 {
219         const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
220         if(!glsl_ver_ptr)
221                 throw runtime_error("GLSL version not available");
222
223         string glsl_ver = glsl_ver_ptr;
224         if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
225                 glsl_ver.erase(0, 18);
226
227         Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
228
229         if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
230         {
231                 Version force_ver(force_ver_ptr);
232                 if(force_ver<ver)
233                         ver = force_ver;
234         }
235
236         return ver;
237 }
238
239 const Version &get_glsl_version()
240 {
241         static Version version = _get_glsl_version();
242         return version;
243 }
244
245 ExtFunc *get_proc_address(const string &name)
246 {
247 #if defined(_WIN32)
248         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
249 #elif defined(__APPLE__)
250         (void)name;
251         return 0;  // Not supported
252 #elif defined(__ANDROID__)
253         return eglGetProcAddress(name.c_str());
254 #else
255         return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
256 #endif
257 }
258
259 } // namespace GL
260 } // namespace Msp