3 #if defined(__ANDROID__)
5 #elif !defined(_WIN32) && !defined(__APPLE__)
6 #define GLX_GLXEXT_PROTOTYPES
9 #include <msp/strings/format.h>
10 #include <msp/strings/utils.h>
12 #include "extension.h"
15 #ifndef GL_VERSION_3_0
16 #define GL_NUM_EXTENSIONS 0x821D
19 #ifndef GL_VERSION_3_2
20 #define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
21 #define GL_CONTEXT_PROFILE_MASK 0x9126
35 Version::Version(unsigned short a, unsigned short i)
41 Version::Version(const string &s)
43 vector<string> parts = split(s, '.');
44 major = lexical_cast<unsigned>(parts[0]);
45 minor = lexical_cast<unsigned>(parts[1]);
48 bool Version::operator>=(const Version &other) const
50 return major>other.major || (major==other.major && minor>=other.minor);
54 Extension::Extension(const char *n, InitFunc f):
61 Extension::operator bool() const
65 support = init_func();
69 return support>UNSUPPORTED;
73 Require::Require(const Extension &ext)
76 throw unsupported_extension(ext.get_name());
80 bool is_supported(const string &ext)
85 static set<string> extensions;
86 static bool init_done = false;
90 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
92 typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
93 FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
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)));
101 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
103 vector<string> exts = split(gl_ext);
104 extensions.insert(exts.begin(), exts.end());
111 return extensions.count(ext);
114 bool is_supported(const Version &core_version, const Version &deprecated_version)
116 const Version &version = get_gl_version();
117 if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
119 return (version>=core_version);
122 bool is_disabled(const string &ext)
124 static set<string> disabled_exts;
125 static bool init_done = false;
129 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
131 vector<string> disable = split(disable_ptr);
132 disabled_exts.insert(disable.begin(), disable.end());
135 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
137 string renderer = renderer_ptr;
138 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
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
143 disabled_exts.insert("GL_NV_primitive_restart");
145 // The core primitive restart feature does not work either.
146 disabled_exts.insert("GL_MSP_primitive_restart");
148 /* AMD's uniform buffer objects only work with the core version of
150 if(get_gl_version()<Version(2, 0))
151 disabled_exts.insert("GL_ARB_uniform_buffer_object");
158 return disabled_exts.count(ext);
163 #ifdef GL_ES_VERSION_2_0
170 inline GLProfile _get_gl_profile()
172 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
175 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
176 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
180 return COMPATIBILITY_PROFILE;
183 GLProfile get_gl_profile()
185 static GLProfile profile = _get_gl_profile();
189 inline Version _get_gl_version()
191 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
193 throw runtime_error("OpenGL version not available");
195 string gl_ver = gl_ver_ptr;
196 if(!gl_ver.compare(0, 10, "OpenGL ES "))
199 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
201 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
203 Version force_ver(force_ver_ptr);
211 const Version &get_gl_version()
213 static Version version = _get_gl_version();
217 inline Version _get_glsl_version()
219 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
221 throw runtime_error("GLSL version not available");
223 string glsl_ver = glsl_ver_ptr;
224 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
225 glsl_ver.erase(0, 18);
227 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
229 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
231 Version force_ver(force_ver_ptr);
239 const Version &get_glsl_version()
241 static Version version = _get_glsl_version();
245 ExtFunc *get_proc_address(const string &name)
248 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
249 #elif defined(__APPLE__)
251 return 0; // Not supported
252 #elif defined(__ANDROID__)
253 return eglGetProcAddress(name.c_str());
255 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));