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