3 #if defined(__ANDROID__)
7 #elif !defined(__APPLE__)
8 #define GLX_GLXEXT_PROTOTYPES
11 #include <msp/strings/format.h>
12 #include <msp/strings/utils.h>
14 #include "extension.h"
17 #ifndef GL_VERSION_3_0
18 #define GL_NUM_EXTENSIONS 0x821D
21 #ifndef GL_VERSION_3_2
22 #define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
23 #define GL_CONTEXT_PROFILE_MASK 0x9126
37 Version::Version(unsigned short a, unsigned short i)
43 Version::Version(const string &s)
45 vector<string> parts = split(s, '.');
46 major = lexical_cast<unsigned>(parts[0]);
47 minor = lexical_cast<unsigned>(parts[1]);
50 bool Version::operator>=(const Version &other) const
52 return major>other.major || (major==other.major && minor>=other.minor);
56 Extension::Extension(const char *n, InitFunc f):
63 Extension::operator bool() const
67 support = init_func();
71 return support>UNSUPPORTED;
75 Require::Require(const Extension &ext)
78 throw unsupported_extension(ext.get_name());
82 bool is_supported(const string &ext)
87 static set<string> extensions;
88 static bool init_done = false;
92 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
94 typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
95 FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
97 glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
98 for(int i=0; i<n_extensions; ++i)
99 extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
103 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
105 vector<string> exts = split(gl_ext);
106 extensions.insert(exts.begin(), exts.end());
113 return extensions.count(ext);
116 bool is_supported(const Version &core_version, const Version &deprecated_version)
118 const Version &version = get_gl_version();
119 if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
121 return (version>=core_version);
124 bool is_disabled(const string &ext)
126 static set<string> disabled_exts;
127 static bool init_done = false;
131 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
133 vector<string> disable = split(disable_ptr);
134 disabled_exts.insert(disable.begin(), disable.end());
137 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
139 string renderer = renderer_ptr;
140 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
142 // The core primitive restart feature does not work either.
143 disabled_exts.insert("GL_MSP_primitive_restart");
145 /* AMD's uniform buffer objects only work with the core version of
147 if(get_gl_version()<Version(2, 0))
148 disabled_exts.insert("GL_ARB_uniform_buffer_object");
155 return disabled_exts.count(ext);
160 #ifdef GL_ES_VERSION_2_0
167 inline GLProfile _get_gl_profile()
169 if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
172 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
173 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
177 return COMPATIBILITY_PROFILE;
180 GLProfile get_gl_profile()
182 static GLProfile profile = _get_gl_profile();
186 inline Version _get_gl_version()
188 const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
190 throw runtime_error("OpenGL version not available");
192 string gl_ver = gl_ver_ptr;
193 if(!gl_ver.compare(0, 10, "OpenGL ES "))
196 Version ver(gl_ver.substr(0, gl_ver.find(' ')));
198 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
200 Version force_ver(force_ver_ptr);
208 const Version &get_gl_version()
210 static Version version = _get_gl_version();
214 inline Version _get_glsl_version()
216 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
218 throw runtime_error("GLSL version not available");
220 string glsl_ver = glsl_ver_ptr;
221 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
222 glsl_ver.erase(0, 18);
224 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
226 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
228 Version force_ver(force_ver_ptr);
236 const Version &get_glsl_version()
238 static Version version = _get_glsl_version();
242 ExtFunc *get_proc_address(const string &name)
245 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
246 #elif defined(__APPLE__)
248 return 0; // Not supported
249 #elif defined(__ANDROID__)
250 return eglGetProcAddress(name.c_str());
252 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));