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());
99 return extensions.count(ext);
102 bool is_supported(const Version &core_version, const Version &deprecated_version)
104 const Version &version = get_gl_version();
105 if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
107 return (version>=core_version);
110 bool is_disabled(const string &ext)
112 static set<string> disabled_exts;
113 static bool init_done = false;
117 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
119 vector<string> disable = split(disable_ptr);
120 disabled_exts.insert(disable.begin(), disable.end());
123 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
125 string renderer = renderer_ptr;
126 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
128 /* Radeon doesn't process NV_primitive_restart correctly and treats
129 the restart index as a normal element if the indices are stored in a
131 disabled_exts.insert("GL_NV_primitive_restart");
133 /* AMD's uniform buffer objects only work with the core version of
135 if(get_gl_version()<Version(2, 0))
136 disabled_exts.insert("GL_ARB_uniform_buffer_object");
143 return disabled_exts.count(ext);
148 #ifdef GL_ES_VERSION_2_0
155 inline GLProfile _get_gl_profile()
157 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
160 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
161 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
165 return COMPATIBILITY_PROFILE;
168 GLProfile get_gl_profile()
170 static GLProfile profile = _get_gl_profile();
174 inline Version _get_gl_version()
176 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
178 throw runtime_error("OpenGL version not available");
180 string gl_ver = gl_ver_ptr;
181 if(!gl_ver.compare(0, 10, "OpenGL ES "))
184 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
186 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
188 Version force_ver(force_ver_ptr);
189 if(!(force_ver>=ver))
196 const Version &get_gl_version()
198 static Version version = _get_gl_version();
202 inline Version _get_glsl_version()
204 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
206 throw runtime_error("GLSL version not available");
208 string glsl_ver = glsl_ver_ptr;
209 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
210 glsl_ver.erase(0, 18);
212 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
214 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
216 Version force_ver(force_ver_ptr);
217 if(!(force_ver>=ver))
224 const Version &get_glsl_version()
226 static Version version = _get_glsl_version();
230 ExtFunc *get_proc_address(const string &name)
233 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
234 #elif defined(__APPLE__)
236 return 0; // Not supported
237 #elif defined(__ANDROID__)
238 return eglGetProcAddress(name.c_str());
240 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));