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)
82 static set<string> extensions;
83 static bool init_done = false;
87 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
89 typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
90 FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
92 glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
93 for(int i=0; i<n_extensions; ++i)
94 extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
98 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
100 vector<string> exts = split(gl_ext);
101 extensions.insert(exts.begin(), exts.end());
108 return extensions.count(ext);
111 bool is_supported(const Version &core_version, const Version &deprecated_version)
113 const Version &version = get_gl_version();
114 if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
116 return (version>=core_version);
119 bool is_disabled(const string &ext)
121 static set<string> disabled_exts;
122 static bool init_done = false;
126 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
128 vector<string> disable = split(disable_ptr);
129 disabled_exts.insert(disable.begin(), disable.end());
132 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
134 string renderer = renderer_ptr;
135 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
137 /* Radeon doesn't process NV_primitive_restart correctly and treats
138 the restart index as a normal element if the indices are stored in a
140 disabled_exts.insert("GL_NV_primitive_restart");
142 /* AMD's uniform buffer objects only work with the core version of
144 if(get_gl_version()<Version(2, 0))
145 disabled_exts.insert("GL_ARB_uniform_buffer_object");
152 return disabled_exts.count(ext);
157 #ifdef GL_ES_VERSION_2_0
164 inline GLProfile _get_gl_profile()
166 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
169 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
170 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
174 return COMPATIBILITY_PROFILE;
177 GLProfile get_gl_profile()
179 static GLProfile profile = _get_gl_profile();
183 inline Version _get_gl_version()
185 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
187 throw runtime_error("OpenGL version not available");
189 string gl_ver = gl_ver_ptr;
190 if(!gl_ver.compare(0, 10, "OpenGL ES "))
193 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
195 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
197 Version force_ver(force_ver_ptr);
198 if(!(force_ver>=ver))
205 const Version &get_gl_version()
207 static Version version = _get_gl_version();
211 inline Version _get_glsl_version()
213 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
215 throw runtime_error("GLSL version not available");
217 string glsl_ver = glsl_ver_ptr;
218 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
219 glsl_ver.erase(0, 18);
221 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
223 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
225 Version force_ver(force_ver_ptr);
226 if(!(force_ver>=ver))
233 const Version &get_glsl_version()
235 static Version version = _get_glsl_version();
239 ExtFunc *get_proc_address(const string &name)
242 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
243 #elif defined(__APPLE__)
245 return 0; // Not supported
246 #elif defined(__ANDROID__)
247 return eglGetProcAddress(name.c_str());
249 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));