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(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
80 typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
81 FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
83 glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
84 for(int i=0; i<n_extensions; ++i)
85 extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
89 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
91 vector<string> exts = split(gl_ext);
92 extensions.insert(exts.begin(), exts.end());
96 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
98 string renderer = renderer_ptr;
99 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
101 /* Radeon doesn't process NV_primitive_restart correctly and treats
102 the restart index as a normal element if the indices are stored in a
104 extensions.erase("GL_NV_primitive_restart");
106 /* AMD's uniform buffer objects only work with the core version of
108 if(!(get_gl_version()>=Version(2, 0)))
109 extensions.erase("GL_ARB_uniform_buffer_object");
113 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
115 vector<string> disable = split(disable_ptr);
116 for(vector<string>::const_iterator i=disable.begin(); i!=disable.end(); ++i)
117 extensions.erase(*i);
123 return extensions.count(ext);
128 #ifdef GL_ES_VERSION_2_0
135 inline Version _get_gl_version()
137 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
139 throw runtime_error("OpenGL version not available");
141 string gl_ver = gl_ver_ptr;
142 if(!gl_ver.compare(0, 10, "OpenGL ES "))
145 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
147 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
149 Version force_ver(force_ver_ptr);
150 if(!(force_ver>=ver))
157 const Version &get_gl_version()
159 static Version version = _get_gl_version();
163 inline Version _get_glsl_version()
165 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
167 throw runtime_error("GLSL version not available");
169 string glsl_ver = glsl_ver_ptr;
170 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
171 glsl_ver.erase(0, 18);
173 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
175 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
177 Version force_ver(force_ver_ptr);
178 if(!(force_ver>=ver))
185 const Version &get_glsl_version()
187 static Version version = _get_glsl_version();
191 bool is_version_at_least(unsigned a, unsigned b)
193 return get_gl_version()>=Version(a, b);
196 ExtFunc *get_proc_address(const string &name)
199 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
200 #elif defined(__APPLE__)
202 return 0; // Not supported
203 #elif defined(__ANDROID__)
204 return eglGetProcAddress(name.c_str());
206 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));