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"
26 Version::Version(unsigned short a, unsigned short i)
32 Version::Version(const string &s)
34 vector<string> parts = split(s, '.');
35 major = lexical_cast<unsigned>(parts[0]);
36 minor = lexical_cast<unsigned>(parts[1]);
39 bool Version::operator>=(const Version &other) const
41 return major>other.major || (major==other.major && minor>=other.minor);
45 Extension::Extension(const char *n, InitFunc f):
52 Extension::operator bool() const
56 support = init_func();
60 return support>UNSUPPORTED;
64 Require::Require(const Extension &ext)
67 throw unsupported_extension(ext.get_name());
71 bool is_supported(const string &ext)
73 static set<string> extensions;
74 static bool init_done = false;
78 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
80 vector<string> exts = split(gl_ext);
81 extensions.insert(exts.begin(), exts.end());
84 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
86 string renderer = renderer_ptr;
87 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
89 /* Radeon doesn't process NV_primitive_restart correctly and treats
90 the restart index as a normal element if the indices are stored in a
92 extensions.erase("GL_NV_primitive_restart");
94 /* AMD's uniform buffer objects only work with the core version of
96 if(!(get_gl_version()>=Version(2, 0)))
97 extensions.erase("GL_ARB_uniform_buffer_object");
101 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
103 vector<string> disable = split(disable_ptr);
104 for(vector<string>::const_iterator i=disable.begin(); i!=disable.end(); ++i)
105 extensions.erase(*i);
111 return extensions.count(ext);
116 #ifdef GL_ES_VERSION_2_0
123 inline Version _get_gl_version()
125 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
127 throw runtime_error("OpenGL version not available");
129 string gl_ver = gl_ver_ptr;
130 if(!gl_ver.compare(0, 10, "OpenGL ES "))
133 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
135 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
137 Version force_ver(force_ver_ptr);
138 if(!(force_ver>=ver))
145 const Version &get_gl_version()
147 static Version version = _get_gl_version();
151 inline Version _get_glsl_version()
153 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
155 throw runtime_error("GLSL version not available");
157 string glsl_ver = glsl_ver_ptr;
158 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
159 glsl_ver.erase(0, 18);
161 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
163 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
165 Version force_ver(force_ver_ptr);
166 if(!(force_ver>=ver))
173 const Version &get_glsl_version()
175 static Version version = _get_glsl_version();
179 bool is_version_at_least(unsigned a, unsigned b)
181 return get_gl_version()>=Version(a, b);
184 ExtFunc *get_proc_address(const string &name)
187 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
188 #elif defined(__APPLE__)
190 return 0; // Not supported
191 #elif defined(__ANDROID__)
192 return eglGetProcAddress(name.c_str());
194 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));