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