-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
sys_opengl.cpp
384 lines (311 loc) · 10.4 KB
/
sys_opengl.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/** @file sys_opengl.cpp
*
* @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2007-2015 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <yagisan@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_base.h"
#include "gl/sys_opengl.h"
#include <QSet>
#include <QStringList>
#include <de/libcore.h>
#include <de/concurrency.h>
#include <de/GLInfo>
#include <de/GLState>
#include "sys_system.h"
#include "gl/gl_main.h"
#ifdef WIN32
# define GETPROC(Type, x) x = de::function_cast<Type>(wglGetProcAddress(#x))
#elif defined (DENG_X11)
# include <GL/glx.h>
# undef None
# define GETPROC(Type, x) x = de::function_cast<Type>(glXGetProcAddress((GLubyte const *)#x))
#endif
gl_state_t GL_state;
static dd_bool doneEarlyInit = false;
static dd_bool sysOpenGLInited = false;
static dd_bool firstTimeInit = true;
static void initialize(void)
{
de::GLInfo::Extensions const &ext = de::GLInfo::extensions();
if(CommandLine_Exists("-noanifilter"))
{
GL_state.features.texFilterAniso = false;
}
if (CommandLine_Exists("-texcomp") && !CommandLine_Exists("-notexcomp"))
{
GL_state.features.texCompression = true;
}
#ifdef USE_TEXTURE_COMPRESSION_S3
// Enabled by default if available.
if (ext.EXT_texture_compression_s3tc)
{
GLint iVal;
LIBGUI_GL.glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &iVal);
LIBGUI_ASSERT_GL_OK();
if (iVal == 0)// || LIBGUI_GL.glGetError() != GL_NO_ERROR)
GL_state.features.texCompression = false;
}
#else
GL_state.features.texCompression = false;
#endif
}
#define TABBED(A, B) _E(Ta) " " _E(l) A _E(.) " " _E(Tb) << B << "\n"
de::String Sys_GLDescription()
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
de::String str;
QTextStream os(&str);
os << _E(b) "OpenGL information:\n" << _E(.);
os << TABBED("Version:", (char const *) LIBGUI_GL.glGetString(GL_VERSION));
os << TABBED("Renderer:", (char const *) LIBGUI_GL.glGetString(GL_RENDERER));
os << TABBED("Vendor:", (char const *) LIBGUI_GL.glGetString(GL_VENDOR));
LIBGUI_ASSERT_GL_OK();
os << _E(T`) "Capabilities:\n";
GLint iVal;
#ifdef USE_TEXTURE_COMPRESSION_S3
if(de::GLInfo::extensions().EXT_texture_compression_s3tc)
{
LIBGUI_GL.glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &iVal);
LIBGUI_ASSERT_GL_OK();
os << TABBED("Compressed texture formats:", iVal);
}
#endif
os << TABBED("Use texture compression:", (GL_state.features.texCompression? "yes" : "no"));
os << TABBED("Available texture units:", de::GLInfo::limits().maxTexUnits);
if(de::GLInfo::extensions().EXT_texture_filter_anisotropic)
{
os << TABBED("Maximum texture anisotropy:", de::GLInfo::limits().maxTexFilterAniso);
}
else
{
os << _E(Ta) " Variable texture anisotropy unavailable.";
}
os << TABBED("Maximum texture size:", de::GLInfo::limits().maxTexSize);
os << TABBED("Line width granularity:", de::GLInfo::limits().smoothLineWidthGranularity);
os << TABBED("Line width range:",
de::GLInfo::limits().smoothLineWidth.start << "..." <<
de::GLInfo::limits().smoothLineWidth.end);
return str.rightStrip();
#undef TABBED
}
static void printGLUInfo(void)
{
LOG_GL_MSG("%s") << Sys_GLDescription();
Sys_GLPrintExtensions();
}
dd_bool Sys_GLPreInit(void)
{
if(novideo) return true;
if(doneEarlyInit) return true; // Already been here??
GL_state.features.texCompression = false;
GL_state.features.texFilterAniso = true;
GL_state.currentLineWidth = 1.5f;
GL_state.currentPointSize = 1.5f;
doneEarlyInit = true;
return true;
}
dd_bool Sys_GLInitialize(void)
{
if(novideo) return true;
LOG_AS("Sys_GLInitialize");
assert(doneEarlyInit);
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
assert(!Sys_GLCheckError());
if(firstTimeInit)
{
#if defined (DENG_OPENGL)
const GLubyte* versionStr = LIBGUI_GL.glGetString(GL_VERSION);
double version = (versionStr? strtod((const char*) versionStr, NULL) : 0);
if(version == 0)
{
LOG_GL_WARNING("Failed to determine OpenGL version; driver reports: %s")
<< LIBGUI_GL.glGetString(GL_VERSION);
}
else if(version < 3.3)
{
if(!CommandLine_Exists("-noglcheck"))
{
Sys_CriticalMessagef("Your OpenGL is too old!\n"
" Driver version: %s\n"
" The minimum supported version is 2.0",
LIBGUI_GL.glGetString(GL_VERSION));
return false;
}
else
{
LOG_GL_WARNING("OpenGL may be too old (3.3+ required, "
"but driver reports %s)") << LIBGUI_GL.glGetString(GL_VERSION);
}
}
#endif
initialize();
printGLUInfo();
firstTimeInit = false;
}
// GL system is now fully initialized.
sysOpenGLInited = true;
/**
* We can now (re)configure GL state that is dependent upon extensions
* which may or may not be present on the host system.
*/
// Use nice quality for mipmaps please.
//if(GL_state.features.genMipmap && de::GLInfo::extensions().SGIS_generate_mipmap)
//LIBGUI_GL.glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
assert(!Sys_GLCheckError());
return true;
}
void Sys_GLShutdown(void)
{
if(!sysOpenGLInited) return;
// No cleanup.
sysOpenGLInited = false;
}
void Sys_GLConfigureDefaultState(void)
{
LIBGUI_ASSERT_GL_OK();
GLfloat fogcol[4] = { .54f, .54f, .54f, 1 };
/**
* @note Only core OpenGL features can be configured at this time
* because we have not yet queried for available extensions,
* or configured our prefered feature default state.
*
* This means that GL_state.extensions and GL_state.features
* cannot be accessed here during initial startup.
*/
assert(doneEarlyInit);
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
LIBGUI_GL.glFrontFace(GL_CW);
LIBGUI_ASSERT_GL_OK();
de::GLState::current()
.setCull(de::gl::None)
.setDepthTest(false)
.setDepthFunc(de::gl::Less);
DGL_Disable(DGL_TEXTURE_2D);
//LIBGUI_GL.glDisable(GL_TEXTURE_CUBE_MAP);
#if defined (DENG_OPENGL)
LIBGUI_GL.glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
LIBGUI_ASSERT_GL_OK();
#endif
// The projection matrix.
DGL_MatrixMode(DGL_PROJECTION);
DGL_LoadIdentity();
// Initialize the modelview matrix.
DGL_MatrixMode(DGL_MODELVIEW);
DGL_LoadIdentity();
// Also clear the texture matrix.
DGL_MatrixMode(DGL_TEXTURE);
DGL_LoadIdentity();
de::GLInfo::setLineWidth(GL_state.currentLineWidth);
#if defined (DENG_OPENGL)
// Setup for antialiased lines/points.
LIBGUI_GL.glEnable(GL_LINE_SMOOTH);
LIBGUI_ASSERT_GL_OK();
LIBGUI_GL.glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
LIBGUI_ASSERT_GL_OK();
LIBGUI_GL.glPointSize(GL_state.currentPointSize);
LIBGUI_ASSERT_GL_OK();
// Prefer good quality in texture compression.
LIBGUI_GL.glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
LIBGUI_ASSERT_GL_OK();
#endif
// Default state for the white fog is off.
DGL_Disable(DGL_FOG);
DGL_Fogi(DGL_FOG_MODE, GL_LINEAR);
DGL_Fogi(DGL_FOG_END, 2100); // This should be tweaked a bit.
DGL_Fogfv(DGL_FOG_COLOR, fogcol);
LIBGUI_ASSERT_GL_OK();
// Configure the default GLState (bottom of the stack).
de::GLState::current()
.setBlendFunc(de::gl::SrcAlpha, de::gl::OneMinusSrcAlpha);
}
static de::String omitGLPrefix(de::String str)
{
if(str.startsWith("GL_")) return str.substr(3);
return str;
}
static void printExtensions(QStringList extensions)
{
qSort(extensions);
// Find all the prefixes.
QSet<QString> prefixes;
foreach(QString ext, extensions)
{
ext = omitGLPrefix(ext);
int pos = ext.indexOf("_");
if(pos > 0)
{
prefixes.insert(ext.left(pos));
}
}
QStringList sortedPrefixes = prefixes.toList();
qSort(sortedPrefixes);
foreach(QString prefix, sortedPrefixes)
{
de::String str;
QTextStream os(&str);
os << " " << prefix << " extensions:\n " _E(>) _E(2);
bool first = true;
foreach(QString ext, extensions)
{
ext = omitGLPrefix(ext);
if(ext.startsWith(prefix + "_"))
{
ext.remove(0, prefix.size() + 1);
if(!first) os << ", ";
os << ext;
first = false;
}
}
LOG_GL_MSG("%s") << str;
}
}
void Sys_GLPrintExtensions(void)
{
LOG_GL_MSG(_E(b) "OpenGL Extensions:");
QStringList exts;
foreach (QByteArray extName, QOpenGLContext::currentContext()->extensions())
{
exts << extName;
}
printExtensions(exts);
/*
#if WIN32
// List the WGL extensions too.
if(wglGetExtensionsStringARB)
{
LOG_GL_MSG(" Extensions (WGL):");
printExtensions(QString((char const *) ((GLubyte const *(__stdcall *)(HDC))wglGetExtensionsStringARB)(wglGetCurrentDC())).split(" ", QString::SkipEmptyParts));
}
#endif
#ifdef Q_WS_X11
// List GLX extensions.
LOG_GL_MSG(" Extensions (GLX):");
printExtensions(QString(getGLXExtensionsString()).split(" ", QString::SkipEmptyParts));
#endif
*/
}
dd_bool Sys_GLCheckErrorArgs(char const *file, int line)
{
if (novideo) return false;
#ifdef DENG_DEBUG
de::GLInfo::checkError(file, line);
#endif
return false;
}