]> git.tdb.fi Git - libs/gl.git/blobdiff - source/extension.cpp
Make ambient occlusion edge detection threshold adjustable
[libs/gl.git] / source / extension.cpp
index a5ad3191b88e518bae3116039a200a784bc0d6a8..a095986123dd535d192881c2870649ae8e936e43 100644 (file)
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <set>
-#ifndef WIN32
+#include <cstdlib>
+#if defined(__ANDROID__)
+#include <EGL/egl.h>
+#elif !defined(_WIN32) && !defined(__APPLE__)
+#define GLX_GLXEXT_PROTOTYPES
 #include <GL/glx.h>
 #endif
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/strings/utils.h>
-#include "arb_shader_objects.h"
-#include "arb_vertex_buffer_object.h"
-#include "arb_vertex_shader.h"
-#include "ext_framebuffer_object.h"
-#include "except.h"
+#include "error.h"
 #include "extension.h"
 #include "gl.h"
-#include "version_1_2.h"
-#include "version_1_3.h"
+
+#ifndef GL_VERSION_3_0
+#define GL_NUM_EXTENSIONS 0x821D
+#endif
+
+#ifndef GL_VERSION_3_2
+#define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001
+#define GL_CONTEXT_PROFILE_MASK 0x9126
+#endif
 
 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)
 {
+       if(is_disabled(ext))
+               return false;
+
        static set<string> extensions;
-       static bool init_done=false;
+       static bool init_done = false;
 
        if(!init_done)
        {
-               if(const char *gl_ext=reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
+               if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
+               {
+                       typedef GLubyte *(APIENTRY *FPtr_glGetStringi)(GLenum, GLuint);
+                       FPtr_glGetStringi glGetStringi = reinterpret_cast<FPtr_glGetStringi>(get_proc_address("glGetStringi"));
+                       int n_extensions;
+                       glGetIntegerv(GL_NUM_EXTENSIONS, &n_extensions);
+                       for(int i=0; i<n_extensions; ++i)
+                               extensions.insert(reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)));
+               }
+               else
                {
-                       vector<string> exts=split(gl_ext);
-                       extensions.insert(exts.begin(), exts.end());
+                       if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
+                       {
+                               vector<string> exts = split(gl_ext);
+                               extensions.insert(exts.begin(), exts.end());
+                       }
                }
 
-               /* XXX Conceptually a bit weird place for this, but I couldn't really come up
-                  with anything better that would still be transparent. */
-               if(extensions.count("GL_ARB_shader_objects"))
-                       init_arb_shader_objects();
-               if(extensions.count("GL_ARB_vertex_shader"))
-                       init_arb_vertex_shader();
-               if(extensions.count("GL_EXT_framebuffer_object"))
-                       init_ext_framebuffer_object();
-               if(extensions.count("GL_ARB_vertex_buffer_object"))
-                       init_arb_vertex_buffer_object();
-
-               init_done=true;
+               init_done = true;
        }
 
        return extensions.count(ext);
 }
 
-const Version &get_gl_version()
+bool is_supported(const Version &core_version, const Version &deprecated_version)
+{
+       const Version &version = get_gl_version();
+       if(deprecated_version && version>=deprecated_version && get_gl_profile()==CORE_PROFILE)
+               return false;
+       return (version>=core_version);
+}
+
+bool is_disabled(const string &ext)
 {
-       static Version version;
-       static bool init_done=false;
+       static set<string> disabled_exts;
+       static bool init_done = false;
 
        if(!init_done)
        {
-               string gl_ver=reinterpret_cast<const char *>(glGetString(GL_VERSION));
-               vector<string> parts=split(gl_ver.substr(0, gl_ver.find(' ')), '.');
-               version.major=lexical_cast<unsigned>(parts[0]);
-               version.minor=lexical_cast<unsigned>(parts[1]);
-
-               unsigned combined=version.major*0x100+version.minor;
-               if(combined>=0x102)
-                       init_version_1_2();
-               if(combined>=0x103)
-                       init_version_1_3();
+               if(const char *disable_ptr = getenv("MSPGL_DISABLE_EXTENSIONS"))
+               {
+                       vector<string> disable = split(disable_ptr);
+                       disabled_exts.insert(disable.begin(), disable.end());
+               }
+
+               if(const char *renderer_ptr = reinterpret_cast<const char *>(glGetString(GL_RENDERER)))
+               {
+                       string renderer = renderer_ptr;
+                       if(renderer.find("Radeon")!=string::npos || renderer.find("AMD")!=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. */
+                               disabled_exts.insert("GL_NV_primitive_restart");
+
+                               // The core primitive restart feature does not work either.
+                               disabled_exts.insert("GL_MSP_primitive_restart");
+
+                               /* AMD's uniform buffer objects only work with the core version of
+                               shaders. */
+                               if(get_gl_version()<Version(2, 0))
+                                       disabled_exts.insert("GL_ARB_uniform_buffer_object");
+                       }
+               }
+
+               init_done = true;
        }
 
+       return disabled_exts.count(ext);
+}
+
+GLApi get_gl_api()
+{
+#ifdef GL_ES_VERSION_2_0
+       return OPENGL_ES2;
+#else
+       return OPENGL;
+#endif
+}
+
+inline GLProfile _get_gl_profile()
+{
+       if(get_gl_api()==OPENGL && get_gl_version()>=Version(3, 0))
+       {
+               int mask;
+               glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
+               if(mask==GL_CONTEXT_CORE_PROFILE_BIT)
+                       return CORE_PROFILE;
+       }
+
+       return COMPATIBILITY_PROFILE;
+}
+
+GLProfile get_gl_profile()
+{
+       static GLProfile profile = _get_gl_profile();
+       return profile;
+}
+
+inline Version _get_gl_version()
+{
+       const char *gl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_VERSION));
+       if(!gl_ver_ptr)
+               throw runtime_error("OpenGL version not available");
+
+       string gl_ver = gl_ver_ptr;
+       if(!gl_ver.compare(0, 10, "OpenGL ES "))
+               gl_ver.erase(0, 10);
+
+       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;
 }
 
-void require_extension(const string &ext)
+inline Version _get_glsl_version()
 {
-       if(!is_supported(ext))
-               throw UnsupportedExtension(ext);
+       const char *glsl_ver_ptr = reinterpret_cast<const char *>(glGetString(GL_SHADING_LANGUAGE_VERSION));
+       if(!glsl_ver_ptr)
+               throw runtime_error("GLSL version not available");
+
+       string glsl_ver = glsl_ver_ptr;
+       if(!glsl_ver.compare(0, 18, "OpenGL ES GLSL ES "))
+               glsl_ver.erase(0, 18);
+
+       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;
 }
 
-void require_version(unsigned a, unsigned b)
+const Version &get_glsl_version()
 {
-       const Version &ver=get_gl_version();
-       if(ver.major<a || (ver.major==a && ver.minor<b))
-               throw UnsupportedExtension(format("OpenGL %d.%d", a, b));
+       static Version version = _get_glsl_version();
+       return version;
 }
 
 ExtFunc *get_proc_address(const string &name)
 {
-#ifndef WIN32
-       return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(name.c_str()));
-#else
+#if defined(_WIN32)
        return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
+#elif defined(__APPLE__)
+       (void)name;
+       return 0;  // Not supported
+#elif defined(__ANDROID__)
+       return eglGetProcAddress(name.c_str());
+#else
+       return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
 #endif
 }