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
31 Extension::Extension(const char *n, InitFunc f):
38 Extension::operator bool() const
42 support = init_func();
46 return support>UNSUPPORTED;
50 Require::Require(const Extension &ext)
53 throw unsupported_extension(ext.get_name());
57 bool is_supported(const string &ext)
62 static set<string> extensions;
63 static bool init_done = false;
67 if(get_backend_api()==OPENGL && get_backend_version()>=Version(3, 0))
69 typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
70 FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
72 glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
73 for(int i=0; i<n_extensions; ++i)
74 extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
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());
88 return extensions.count(ext);
91 bool is_supported(const Version &core_version, const Version &deprecated_version)
93 const Version &version = get_backend_version();
94 if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
96 return (version>=core_version);
99 bool is_disabled(const string &ext)
101 static set<string> disabled_exts;
102 static bool init_done = false;
106 if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
108 vector<string> disable = split(disable_ptr);
109 disabled_exts.insert(disable.begin(), disable.end());
112 if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
114 string renderer = renderer_ptr;
115 if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=string::npos)
117 // The core primitive restart feature does not work either.
118 disabled_exts.insert("GL_MSP_primitive_restart");
120 /* AMD's uniform buffer objects only work with the core version of
122 if(get_backend_version()<Version(2, 0))
123 disabled_exts.insert("GL_ARB_uniform_buffer_object");
130 return disabled_exts.count(ext);
133 inline GLProfile _get_gl_profile()
135 if(get_backend_api()==OPENGL && get_backend_version()>=Version(3, 0))
138 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
139 if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
143 return COMPATIBILITY_PROFILE;
146 GLProfile get_gl_profile()
148 static GLProfile profile = _get_gl_profile();
152 inline Version _get_glsl_version()
154 const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
156 throw runtime_error("GLSL version not available");
158 string glsl_ver = glsl_ver_ptr;
159 if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
160 glsl_ver.erase(0, 18);
162 Version ver(glsl_ver.substr(0, glsl_ver.find(' ')));
164 if(const char *force_ver_ptr = getenv("MSPGL_FORCE_GLSL_VERSION"))
166 Version force_ver(force_ver_ptr);
174 const Version &get_glsl_version()
176 static Version version = _get_glsl_version();
180 ExtFunc *get_proc_address(const string &name)
183 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
184 #elif defined(__APPLE__)
186 return 0; // Not supported
187 #elif defined(__ANDROID__)
188 return eglGetProcAddress(name.c_str());
190 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));