-#include <set>
-#if !defined(WIN32) && !defined(__APPLE__)
-#define GLX_GLXEXT_PROTOTYPES
-#include <GL/glx.h>
-#endif
-#include <msp/strings/format.h>
-#include <msp/strings/utils.h>
-#include "error.h"
-#include "extension.h"
-#include "gl.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-Version::Version()
-{
- major = 0;
- minor = 0;
-}
-
-Version::Version(unsigned short a, unsigned short i)
-{
- major = a;
- minor = i;
-}
-
-Version::Version(const string &s)
-{
- vector<string> parts = split(s, '.');
- major = lexical_cast<unsigned>(parts[0]);
- minor = lexical_cast<unsigned>(parts[1]);
-}
-
-bool Version::operator>=(const Version &other) const
-{
- return major>other.major || (major==other.major && minor>=other.minor);
-}
-
-
-Extension::Extension(const char *n, InitFunc f):
- name(n),
- init_func(f),
- init_done(false),
- support(UNSUPPORTED)
-{ }
-
-Extension::operator bool() const
-{
- if(!init_done)
- {
- support = init_func();
- init_done = true;
- }
-
- return support>UNSUPPORTED;
-}
-
-
-Require::Require(const Extension &ext)
-{
- if(!ext)
- throw unsupported_extension(ext.get_name());
-}
-
-
-bool is_supported(const string &ext)
-{
- static set<string> extensions;
- static bool init_done = false;
-
- if(!init_done)
- {
- if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
- {
- vector<string> exts = split(gl_ext);
- extensions.insert(exts.begin(), exts.end());
- }
-
- string renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
- if(renderer.find("Radeon")!=string::npos)
- /* Radeon doesn't process NV_primitive_restart correctly and treats
- the restart index as a normal element if the indices are stored in a
- buffer. */
- extensions.erase("GL_NV_primitive_restart");
-
- if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
- {
- vector<string> disable = split(disable_ptr);
- for(vector<string>::const_iterator i=disable.begin(); i!=disable.end(); ++i)
- extensions.erase(*i);
- }
-
- init_done = true;
- }
-
- return extensions.count(ext);
-}
-
-inline Version _get_gl_version()
-{
- string gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
- Version ver(gl_ver.substr(0, gl_ver.find(' ')));
-
- if(const char *force_ver_ptr = getenv("MSPGL_FORCE_VERSION"))
- {
- Version force_ver(force_ver_ptr);
- if(!(force_ver>=ver))
- ver = force_ver;
- }
-
- return ver;
-}
-
-const Version &get_gl_version()
-{
- static Version version = _get_gl_version();
- return version;
-}
-
-inline Version _get_glsl_version()
-{
- string glsl_ver = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
- Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
-
- if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
- {
- Version force_ver(force_ver_ptr);
- if(!(force_ver>=ver))
- ver = force_ver;
- }
-
- return ver;
-}
-
-const Version &get_glsl_version()
-{
- static Version version = _get_glsl_version();
- return version;
-}
-
-bool is_version_at_least(unsigned a, unsigned b)
-{
- return get_gl_version()>=Version(a, b);
-}
-
-ExtFunc *get_proc_address(const string &name)
-{
-#if defined(WIN32)
- return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
-#elif defined(__APPLE__)
- (void)name;
- return 0; // Not supported
-#else
- return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
-#endif
-}
-
-} // namespace GL
-} // namespace Msp