]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Return false from is_supported for disabled extensions
[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                                 /* AMD's uniform buffer objects only work with the core version of
146                                 shaders. */
147                                 if(get_gl_version()<Version(2, 0))
148                                         disabled_exts.insert("GL_ARB_uniform_buffer_object");
149                         }
150                 }
151
152                 init_done = true;
153         }
154
155         return disabled_exts.count(ext);
156 }
157
158 GLApi get_gl_api()
159 {
160 #ifdef GL_ES_VERSION_2_0
161         return OPENGL_ES2;
162 #else
163         return OPENGL;
164 #endif
165 }
166
167 inline GLProfile _get_gl_profile()
168 {
169         if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
170         {
171                 int mask;
172                 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
173                 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
174                         return CORE_PROFILE;
175         }
176
177         return COMPATIBILITY_PROFILE;
178 }
179
180 GLProfile get_gl_profile()
181 {
182         static GLProfile profile = _get_gl_profile();
183         return profile;
184 }
185
186 inline Version _get_gl_version()
187 {
188         const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
189         if(!gl_ver_ptr)
190                 throw runtime_error("OpenGL version not available");
191
192         string gl_ver = gl_ver_ptr;
193         if(!gl_ver.compare(0, 10, "OpenGL ES "))
194                 gl_ver.erase(0, 10);
195
196         Version ver(gl_ver.substr(0, gl_ver.find(' ')));
197
198         if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
199         {
200                 Version force_ver(force_ver_ptr);
201                 if(!(force_ver>=ver))
202                         ver = force_ver;
203         }
204
205         return ver;
206 }
207
208 const Version &get_gl_version()
209 {
210         static Version version = _get_gl_version();
211         return version;
212 }
213
214 inline Version _get_glsl_version()
215 {
216         const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
217         if(!glsl_ver_ptr)
218                 throw runtime_error("GLSL version not available");
219
220         string glsl_ver = glsl_ver_ptr;
221         if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
222                 glsl_ver.erase(0, 18);
223
224         Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
225
226         if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
227         {
228                 Version force_ver(force_ver_ptr);
229                 if(!(force_ver>=ver))
230                         ver = force_ver;
231         }
232
233         return ver;
234 }
235
236 const Version &get_glsl_version()
237 {
238         static Version version = _get_glsl_version();
239         return version;
240 }
241
242 ExtFunc *get_proc_address(const string &name)
243 {
244 #if defined(_WIN32)
245         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
246 #elif defined(__APPLE__)
247         (void)name;
248         return 0;  // Not supported
249 #elif defined(__ANDROID__)
250         return eglGetProcAddress(name.c_str());
251 #else
252         return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
253 #endif
254 }
255
256 } // namespace GL
257 } // namespace Msp