-
Notifications
You must be signed in to change notification settings - Fork 59
/
Context.cpp
337 lines (250 loc) · 9.12 KB
/
Context.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <common/Context.h>
#include <cassert>
#include <map>
#include <glbinding/gl/gl.h>
#include <glbinding/ContextInfo.h>
#include <glbinding/ProcAddress.h>
#include <glbinding/Binding.h>
#include <glbinding/Version.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> // specifies APIENTRY, should be after Error.h include,
// which requires APIENTRY in windows..
#include <globjects/globjects.h>
#include <globjects/base/baselogging.h>
#include <common/ContextFormat.h>
using namespace gl;
using namespace globjects;
glbinding::Version Context::retrieveVersion()
{
assert(0 != glbinding::getCurrentContext());
GLint minorVersion = -1;
GLint majorVersion = -1;
glGetIntegerv(GLenum::GL_MAJOR_VERSION, &majorVersion); // major version
glGetIntegerv(GLenum::GL_MINOR_VERSION, &minorVersion); // minor version
if (minorVersion < 0 && majorVersion < 0) // probably a context < 3.0 with no support for GL_MAJOR/MINOR_VERSION
{
const GLubyte * vstr = glGetString(GLenum::GL_VERSION);
if (!vstr)
return glbinding::Version();
assert(vstr[1] == '.');
assert(vstr[0] >= '0' && vstr[0] <= '9');
majorVersion = vstr[0] - '0';
assert(vstr[2] >= '0' && vstr[2] <= '9');
minorVersion = vstr[2] - '0';
}
return glbinding::Version(majorVersion, minorVersion);
}
glbinding::Version Context::maxSupportedVersion()
{
glbinding::Version version;
/* GLFW3 does not set default hint values on window creation so at least
the default values must be set before glfwCreateWindow can be called.
cf. http://www.glfw.org/docs/latest/group__window.html#ga4fd9e504bb937e79588a0ffdca9f620b
*/
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, false);
#ifdef __APPLE__
/*
* Using OS X the following hints must be set for proper context initialization
* (cf. http://stackoverflow.com/questions/19969937/getting-a-glsl-330-context-on-osx-10-9-mavericks)
*/
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
#endif
// create window for version check
GLFWwindow * window = glfwCreateWindow(1, 1, "VersionCheck", nullptr, nullptr);
if (window)
{
glfwMakeContextCurrent(window);
glbinding::Binding::initialize(false);
version = retrieveVersion();
glfwMakeContextCurrent(nullptr);
glfwDestroyWindow(window);
}
return version;
}
GLFWwindow * Context::create(
const ContextFormat & format
, bool verify
, int width
, int height
, GLFWmonitor * monitor)
{
glbinding::Version version = format.version();
if (verify) // check if version is valid and supported
version = ContextFormat::validateVersion(format.version(), maxSupportedVersion());
/** GLFW3 does not set default hint values on window creation so at least
the default values must be set before glfwCreateWindow can be called.
cf. http://www.glfw.org/docs/latest/group__window.html#ga4fd9e504bb937e79588a0ffdca9f620b
*/
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, true);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, version.m_major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, version.m_minor);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#else
if (version >= glbinding::Version(3, 0))
{
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, format.forwardCompatible());
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, format.debugContext());
}
if (version >= glbinding::Version(3, 2))
{
glfwWindowHint(GLFW_OPENGL_PROFILE, format.profile() == ContextFormat::Profile::Core ? GLFW_OPENGL_CORE_PROFILE :
(format.profile() == ContextFormat::Profile::Compatibility ? GLFW_OPENGL_COMPAT_PROFILE : GLFW_OPENGL_ANY_PROFILE));
}
#endif
glfwWindowHint(GLFW_DEPTH_BITS, format.depthBufferSize());
glfwWindowHint(GLFW_STENCIL_BITS, format.stencilBufferSize());
glfwWindowHint(GLFW_RED_BITS, format.redBufferSize());
glfwWindowHint(GLFW_GREEN_BITS, format.greenBufferSize());
glfwWindowHint(GLFW_BLUE_BITS, format.blueBufferSize());
glfwWindowHint(GLFW_ALPHA_BITS, format.alphaBufferSize());
glfwWindowHint(GLFW_STEREO, format.stereo());
glfwWindowHint(GLFW_SAMPLES, format.samples());
GLFWwindow * window = glfwCreateWindow(width, height, "", monitor, nullptr);
if (window)
{
glfwMakeContextCurrent(window);
glbinding::Binding::initialize(false);
glfwMakeContextCurrent(nullptr);
}
return window;
}
const std::string & Context::swapIntervalString(const SwapInterval interval)
{
static const std::map<SwapInterval, std::string> swapiIdentifier = {
{ SwapInterval::NoVerticalSyncronization, "NoVerticalSyncronization" }
, { SwapInterval::VerticalSyncronization, "VerticalSyncronization" }
, { SwapInterval::AdaptiveVerticalSyncronization, "AdaptiveVerticalSyncronization" } };
return swapiIdentifier.at(interval);
}
Context::Context(GLFWwindow * window)
: m_swapInterval(SwapInterval::NoVerticalSyncronization)
, m_format(nullptr)
, m_window(window)
, m_handle(0)
{
assert(window);
if (!window)
return;
GLFWwindow * current = glfwGetCurrentContext();
if (current != m_window)
glfwMakeContextCurrent(m_window);
m_handle = tryFetchHandle();
if (current != m_window)
glfwMakeContextCurrent(current);
}
Context::~Context()
{
delete m_format;
}
std::string Context::version()
{
assert(0 != glbinding::getCurrentContext());
return glbinding::ContextInfo::version().toString();
}
std::string Context::vendor()
{
assert(0 != glbinding::getCurrentContext());
return glbinding::ContextInfo::vendor();
}
std::string Context::renderer()
{
assert(0 != glbinding::getCurrentContext());
return glbinding::ContextInfo::renderer();
}
bool Context::isValid() const
{
return 0 < handle();
}
glbinding::ContextHandle Context::tryFetchHandle()
{
const glbinding::ContextHandle handle = glbinding::getCurrentContext();
if (0 == handle)
critical("Acquiring OpenGL context handle failed.");
return handle;
}
Context::SwapInterval Context::swapInterval() const
{
return m_swapInterval;
}
void Context::setSwapInterval(const SwapInterval interval)
{
//if (interval == m_swapInterval) // initialized value might not match or explicit reset might be required
// return true;
GLFWwindow * current = glfwGetCurrentContext();
if (current != m_window)
glfwMakeContextCurrent(m_window);
#ifdef WIN32
using SWAPINTERVALEXTPROC = bool(*)(int);
static SWAPINTERVALEXTPROC wglSwapIntervalEXT(nullptr);
bool result(false);
if (!wglSwapIntervalEXT)
wglSwapIntervalEXT = reinterpret_cast<SWAPINTERVALEXTPROC>(glbinding::getProcAddress("wglSwapIntervalEXT"));
if (wglSwapIntervalEXT)
result = wglSwapIntervalEXT(static_cast<int>(interval));
if(!result)
warning("Setting swap interval to % failed.", swapIntervalString(interval));
#else
glfwSwapInterval(static_cast<int>(interval));
#endif
if (current != m_window)
glfwMakeContextCurrent(current);
m_swapInterval = interval;
}
glbinding::ContextHandle Context::handle() const
{
return m_handle;
}
const ContextFormat & Context::format() const
{
assert(isValid());
if (m_format)
return *m_format;
// create and retrive format if not done already
m_format = new ContextFormat();
GLFWwindow * current = glfwGetCurrentContext();
if (current != m_window)
glfwMakeContextCurrent(m_window);
m_format->setVersion(retrieveVersion());
if (m_format->version() >= glbinding::Version(3, 2))
m_format->setProfile(isCoreProfile() ? ContextFormat::Profile::Core : ContextFormat::Profile::Compatibility);
else
m_format->setProfile(ContextFormat::Profile::None);
if (current != m_window)
glfwMakeContextCurrent(current);
//GLint i;
//GLboolean b;
//i = -1; glGetIntegerv(GLenum::GL_DEPTH_BITS, &i);
//m_format->setDepthBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_STENCIL_BITS, &i);
//m_format->setStencilBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_RED_BITS, &i);
//m_format->setRedBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_GREEN_BITS, &i);
//m_format->setGreenBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_BLUE_BITS, &i);
//m_format->setBlueBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_ALPHA_BITS, &i);
//m_format->setAlphaBufferSize(i);
//i = -1; glGetIntegerv(GLenum::GL_SAMPLES, &i);
//m_format->setSamples(i);
//b = GL_FALSE; glGetBooleanv(GLenum::GL_STEREO, &b);
//m_format->setStereo(b == GL_TRUE);
return *m_format;
}
void Context::makeCurrent() const
{
if (m_window)
glfwMakeContextCurrent(m_window);
}
void Context::doneCurrent() const
{
if (m_window && m_window == glfwGetCurrentContext())
glfwMakeContextCurrent(nullptr);
}