]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Disable NV_primitive_restart on Radeon cards because it's buggy
[libs/gl.git] / source / extension.cpp
1 #include <set>
2 #ifndef WIN32
3 #define GLX_GLXEXT_PROTOTYPES
4 #include <GL/glx.h>
5 #endif
6 #include <msp/strings/format.h>
7 #include <msp/strings/utils.h>
8 #include "error.h"
9 #include "extension.h"
10 #include "gl.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Extension::Extension(const char *n, InitFunc f):
18         name(n),
19         init_func(f),
20         init_done(false),
21         support(UNSUPPORTED)
22 { }
23
24 Extension::operator bool() const
25 {
26         if(!init_done)
27         {
28                 support = init_func();
29                 init_done = true;
30         }
31
32         return support>UNSUPPORTED;
33 }
34
35
36 Require::Require(const Extension &ext)
37 {
38         if(!ext)
39                 throw unsupported_extension(ext.get_name());
40 }
41
42
43 bool is_supported(const string &ext)
44 {
45         static set<string> extensions;
46         static bool init_done = false;
47
48         if(!init_done)
49         {
50                 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
51                 {
52                         vector<string> exts = split(gl_ext);
53                         extensions.insert(exts.begin(), exts.end());
54                 }
55
56                 string renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
57                 if(renderer.find("Radeon")!=string::npos)
58                         /* Radeon doesn't process NV_primitive_restart correctly and treats
59                         the restart index as a normal element if the indices are stored in a
60                         buffer. */
61                         extensions.erase("GL_NV_primitive_restart");
62
63                 init_done = true;
64         }
65
66         return extensions.count(ext);
67 }
68
69 const Version &get_gl_version()
70 {
71         static Version version;
72         static bool init_done = false;
73
74         if(!init_done)
75         {
76                 string gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
77                 vector<string> parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.');
78                 version.major = lexical_cast<unsigned>(parts[0]);
79                 version.minor = lexical_cast<unsigned>(parts[1]);
80
81                 init_done = true;
82         }
83
84         return version;
85 }
86
87 bool is_version_at_least(unsigned a, unsigned b)
88 {
89         const Version &ver = get_gl_version();
90         return (ver.major>a || (ver.major==a && ver.minor>=b));
91 }
92
93 ExtFunc *get_proc_address(const string &name)
94 {
95 #ifndef WIN32
96         return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
97 #else
98         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
99 #endif
100 }
101
102 } // namespace GL
103 } // namespace Msp