]> git.tdb.fi Git - libs/gl.git/blob - source/extension.cpp
Add definitions for the GL_ARB_uniform_buffer_object extension
[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 "arb_shader_objects.h"
9 #include "arb_uniform_buffer_object.h"
10 #include "arb_vertex_buffer_object.h"
11 #include "arb_vertex_program.h"
12 #include "arb_vertex_shader.h"
13 #include "error.h"
14 #include "ext_framebuffer_blit.h"
15 #include "ext_framebuffer_multisample.h"
16 #include "ext_framebuffer_object.h"
17 #include "extension.h"
18 #include "gl.h"
19 #include "nv_primitive_restart.h"
20 #include "version_1_2.h"
21 #include "version_1_3.h"
22
23 using namespace std;
24
25 namespace Msp {
26 namespace GL {
27
28 bool is_supported(const string &ext)
29 {
30         static set<string> extensions;
31         static bool init_done = false;
32
33         if(!init_done)
34         {
35                 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
36                 {
37                         vector<string> exts = split(gl_ext);
38                         extensions.insert(exts.begin(), exts.end());
39                 }
40
41                 /* XXX Conceptually a bit weird place for this, but I couldn't really come up
42                    with anything better that would still be transparent. */
43                 if(extensions.count("GL_ARB_shader_objects"))
44                         init_arb_shader_objects();
45                 if(extensions.count("GL_ARB_vertex_shader"))
46                         init_arb_vertex_shader();
47                 if(extensions.count("GL_ARB_vertex_program"))
48                         init_arb_vertex_program();
49                 if(extensions.count("GL_EXT_framebuffer_object"))
50                         init_ext_framebuffer_object();
51                 if(extensions.count("GL_EXT_framebuffer_blit"))
52                         init_ext_framebuffer_blit();
53                 if(extensions.count("GL_EXT_framebuffer_multisample"))
54                         init_ext_framebuffer_multisample();
55                 if(extensions.count("GL_ARB_vertex_buffer_object"))
56                         init_arb_vertex_buffer_object();
57                 if(extensions.count("GL_NV_primitive_restart"))
58                         init_nv_primitive_restart();
59                 if(extensions.count("GL_ARB_uniform_buffer_object"))
60                         init_arb_uniform_buffer_object();
61
62                 init_done = true;
63         }
64
65         return extensions.count(ext);
66 }
67
68 void require_extension(const string &ext)
69 {
70         if(!is_supported(ext))
71                 throw unsupported_extension(ext);
72 }
73
74 const Version &get_gl_version()
75 {
76         static Version version;
77         static bool init_done = false;
78
79         if(!init_done)
80         {
81                 string gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
82                 vector<string> parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.');
83                 version.major = lexical_cast<unsigned>(parts[0]);
84                 version.minor = lexical_cast<unsigned>(parts[1]);
85
86                 unsigned combined = version.major*0x100+version.minor;
87                 if(combined>=0x102)
88                         init_version_1_2();
89                 if(combined>=0x103)
90                         init_version_1_3();
91
92                 init_done = true;
93         }
94
95         return version;
96 }
97
98 bool is_version_at_least(unsigned a, unsigned b)
99 {
100         const Version &ver = get_gl_version();
101         return (ver.major>a || (ver.major==a && ver.minor>=b));
102 }
103
104 void require_version(unsigned a, unsigned b)
105 {
106         if(!is_version_at_least(a, b))
107                 throw unsupported_extension(format("OpenGL %d.%d", a, b));
108 }
109
110 ExtFunc *get_proc_address(const string &name)
111 {
112 #ifndef WIN32
113         return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
114 #else
115         return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));
116 #endif
117 }
118
119 } // namespace GL
120 } // namespace Msp