-
Notifications
You must be signed in to change notification settings - Fork 237
/
OgreEglPBufferSupport.cpp
464 lines (391 loc) · 17.2 KB
/
OgreEglPBufferSupport.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "PBuffer/OgreEglPBufferSupport.h"
#include "OgreException.h"
#include "OgreGL3PlusRenderSystem.h"
#include "OgreLogManager.h"
#include "OgreStringConverter.h"
#include "PBuffer/OgreEglPBufferWindow.h"
typedef EGLBoolean ( *EGLQueryDevicesType )( EGLint, EGLDeviceEXT *, EGLint * );
static EGLQueryDevicesType eglQueryDevices;
static PFNEGLQUERYDEVICESTRINGEXTPROC eglQueryDeviceStringEXT;
namespace Ogre
{
//-------------------------------------------------------------------------
EglPBufferSupport::EglPBufferSupport()
{
eglQueryDevices = (EGLQueryDevicesType)eglGetProcAddress( "eglQueryDevicesEXT" );
eglQueryDeviceStringEXT =
(PFNEGLQUERYDEVICESTRINGEXTPROC)eglGetProcAddress( "eglQueryDeviceStringEXT" );
if( eglQueryDevices == 0 )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"eglQueryDevicesEXT not found. EGL driver too old. "
"Update your GPU drivers and try again",
"EglPBufferSupport::EglPBufferSupport" );
}
if( eglQueryDeviceStringEXT == 0 )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"eglQueryDeviceStringEXT not found. EGL driver too old. "
"Update your GPU drivers and try again",
"EglPBufferSupport::EglPBufferSupport" );
}
EGLint numDevices = 0;
eglQueryDevices( 0, 0, &numDevices );
if( numDevices > 0 )
{
mDevices.resize( static_cast<size_t>( numDevices ) );
mDeviceData.reserve( static_cast<size_t>( numDevices ) );
eglQueryDevices( numDevices, mDevices.begin(), &numDevices );
}
LogManager::getSingleton().logMessage( "Found Num EGL Devices: " +
StringConverter::toString( numDevices ) );
if( numDevices == 0 )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"No EGL devices found! Update your GPU drivers and try again",
"EglPBufferSupport::EglPBufferSupport" );
}
for( int i = 0u; i < numDevices; ++i )
{
EGLDeviceEXT device = mDevices[size_t( i )];
const char *name = eglQueryDeviceStringEXT( device, EGL_EXTENSIONS );
DeviceData deviceData;
deviceData.name = name + ( " #" + StringConverter::toString( i ) );
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
const char *gpuCard = eglQueryDeviceStringEXT( device, EGL_DRM_DEVICE_FILE_EXT );
if( gpuCard )
{
deviceData.name += " ";
deviceData.name += gpuCard;
}
#endif
LogManager::getSingleton().logMessage( "EGL Device: " + deviceData.name );
// Always support no MSAA (in case maxSamples returned invalid value of 0)
deviceData.fsaa.push_back( SampleDescription( 1u ) );
mDeviceData.push_back( deviceData );
try
{
// Initialize to get FSAA counts
initDevice( size_t( i ) );
// We don't know which sample counts are supported (and some colour
// formats may have further restrictions)
//
// However it's usually a safe guess that all powers of 2 between 1 and maxSamples
// ought to be supported
PFNGLGETINTEGERVPROC myGlGetIntegerv =
(PFNGLGETINTEGERVPROC)eglGetProcAddress( "glGetIntegerv" );
GLint maxSamples;
myGlGetIntegerv( GL_MAX_COLOR_TEXTURE_SAMPLES, &maxSamples );
for( GLint sample = 2u; sample <= maxSamples; sample <<= 1 )
{
mDeviceData.back().fsaa.push_back(
SampleDescription( static_cast<uint8>( sample ) ) );
}
destroyDevice( size_t( i ) );
}
catch( Exception &e )
{
LogManager::getSingleton().logMessage( e.getFullDescription(), LML_CRITICAL );
destroyDevice( size_t( i ) );
}
}
}
//-------------------------------------------------------------------------
EglPBufferSupport::~EglPBufferSupport() {}
//-------------------------------------------------------------------------
void EglPBufferSupport::addConfig( void )
{
ConfigOption optDevices;
ConfigOption optFSAA;
ConfigOption optSRGB;
optDevices.name = "Device";
FastArray<DeviceData>::const_iterator itor = mDeviceData.begin();
FastArray<DeviceData>::const_iterator endt = mDeviceData.end();
while( itor != endt )
{
optDevices.possibleValues.push_back( itor->name );
++itor;
}
optDevices.currentValue = mDeviceData.front().name;
optDevices.immutable = false;
optFSAA.name = "FSAA";
optFSAA.immutable = false;
optFSAA.currentValue = StringConverter::toString(
mDeviceData[getSelectedDeviceIdx()].fsaa.front().getColourSamples() );
optSRGB.name = "sRGB Gamma Conversion";
optSRGB.immutable = false;
optSRGB.possibleValues.push_back( "No" );
optSRGB.possibleValues.push_back( "Yes" );
optSRGB.currentValue = optSRGB.possibleValues[1];
mOptions[optDevices.name] = optDevices;
mOptions[optFSAA.name] = optFSAA;
mOptions[optSRGB.name] = optSRGB;
refreshConfig();
}
//-------------------------------------------------------------------------
void EglPBufferSupport::refreshConfig( void )
{
ConfigOptionMap::iterator optFSAA = mOptions.find( "FSAA" );
if( optFSAA != mOptions.end() )
{
optFSAA->second.possibleValues.clear();
const size_t deviceIdx = getSelectedDeviceIdx();
FastArray<SampleDescription>::const_iterator itor = mDeviceData[deviceIdx].fsaa.begin();
FastArray<SampleDescription>::const_iterator endt = mDeviceData[deviceIdx].fsaa.end();
while( itor != endt )
{
optFSAA->second.possibleValues.push_back(
StringConverter::toString( itor->getColourSamples() ) );
++itor;
}
optFSAA->second.currentValue = optFSAA->second.possibleValues.front();
}
}
//-------------------------------------------------------------------------
void EglPBufferSupport::setConfigOption( const String &name, const String &value )
{
ConfigOptionMap::iterator option = mOptions.find( name );
if( option == mOptions.end() )
{
OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS, "Option named " + name + " does not exist.",
"EglPBufferSupport::setConfigOption" );
}
else
{
option->second.currentValue = value;
}
if( name == "Device" )
refreshConfig();
}
//-------------------------------------------------------------------------
String EglPBufferSupport::validateConfig( void )
{
// TODO
return BLANKSTRING;
}
//-------------------------------------------------------------------------
const char *EglPBufferSupport::getPriorityConfigOption( size_t ) const { return "Device"; }
//-------------------------------------------------------------------------
size_t EglPBufferSupport::getNumPriorityConfigOptions( void ) const { return 1u; }
//-------------------------------------------------------------------------
Window *EglPBufferSupport::createWindow( bool autoCreateWindow, GL3PlusRenderSystem *renderSystem,
const String &windowTitle )
{
Window *window = 0;
if( autoCreateWindow )
{
ConfigOptionMap::iterator opt;
ConfigOptionMap::iterator end = mOptions.end();
NameValuePairList miscParams;
bool fullscreen = false;
uint w = 1280, h = 720;
if( ( opt = mOptions.find( "FSAA" ) ) != end )
miscParams["FSAA"] = opt->second.currentValue;
if( ( opt = mOptions.find( "sRGB Gamma Conversion" ) ) != end )
miscParams["gamma"] = opt->second.currentValue;
window = renderSystem->_createRenderWindow( windowTitle, w, h, fullscreen, &miscParams );
}
return window;
}
//-------------------------------------------------------------------------
Window *EglPBufferSupport::newWindow( const String &name, uint32 width, uint32 height,
bool fullscreen, const NameValuePairList *miscParams )
{
EglPBufferWindow *window =
new EglPBufferWindow( name, width, height, fullscreen, miscParams, this );
return window;
}
//-------------------------------------------------------------------------
void EglPBufferSupport::start()
{
LogManager::getSingleton().logMessage(
"******************************\n"
"*** Starting EGL Subsystem ***\n"
"******************************" );
}
//-------------------------------------------------------------------------
void EglPBufferSupport::stop()
{
LogManager::getSingleton().logMessage(
"******************************\n"
"*** Stopping EGL Subsystem ***\n"
"******************************" );
const size_t numDevices = mDevices.size();
for( size_t i = 0u; i < numDevices; ++i )
destroyDevice( i );
}
//-------------------------------------------------------------------------
void *EglPBufferSupport::getProcAddress( const char *procname ) const
{
return (void *)eglGetProcAddress( procname );
}
//-------------------------------------------------------------------------
void EglPBufferSupport::initDevice( const size_t deviceIdx )
{
DeviceData &deviceData = mDeviceData[deviceIdx];
LogManager::getSingleton().logMessage( "Trying to init device: " + deviceData.name + "..." );
EGLAttrib attribs[] = { EGL_NONE };
deviceData.eglDisplay =
eglGetPlatformDisplay( EGL_PLATFORM_DEVICE_EXT, mDevices[deviceIdx], attribs );
EGL_CHECK_ERROR
if( deviceData.eglDisplay == EGL_NO_DISPLAY )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"eglGetPlatformDisplay failed for device " + deviceData.name,
"EGLSupport::getGLDisplay" );
}
EGLint major = 0, minor = 0;
if( eglInitialize( deviceData.eglDisplay, &major, &minor ) == EGL_FALSE )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"eglInitialize failed for device " + deviceData.name,
"EGLSupport::getGLDisplay" );
}
EGL_CHECK_ERROR
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE
};
EGLint numConfigs;
if( eglChooseConfig( deviceData.eglDisplay, configAttribs, &deviceData.eglCfg, 1,
&numConfigs ) == EGL_FALSE )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
"eglChooseConfig for device " + deviceData.name, "EGLSupport::getGLDisplay" );
}
const EGLint pbufferAttribs[] = {
EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE,
};
deviceData.eglSurf =
eglCreatePbufferSurface( deviceData.eglDisplay, deviceData.eglCfg, pbufferAttribs );
// Beware ES must not ask EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
// eglBindAPI( EGL_OPENGL_ES_API );
eglBindAPI( EGL_OPENGL_API );
EGLint contextAttrs[] = {
EGL_CONTEXT_MAJOR_VERSION,
4,
EGL_CONTEXT_MINOR_VERSION,
5,
EGL_CONTEXT_OPENGL_PROFILE_MASK,
EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
#if OGRE_DEBUG_MODE
EGL_CONTEXT_FLAGS_KHR,
EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
#endif
EGL_NONE
};
while( !deviceData.eglCtx && ( contextAttrs[1] >= 3 ) )
{
deviceData.eglCtx =
eglCreateContext( deviceData.eglDisplay, deviceData.eglCfg, 0, contextAttrs );
if( deviceData.eglCtx )
{
LogManager::getSingleton().logMessage(
"Created GL " + StringConverter::toString( contextAttrs[1] ) + "." +
StringConverter::toString( contextAttrs[3] ) + " context for device " +
deviceData.name );
}
else
{
if( contextAttrs[3] == 0 )
{
contextAttrs[1] -= 1;
contextAttrs[3] = 5;
}
else
{
contextAttrs[3] -= 1;
}
}
}
if( !deviceData.eglCtx )
{
LogManager::getSingleton().logMessage(
"Failed to create an OpenGL 3+ context for device " + deviceData.name, LML_CRITICAL );
}
else
{
eglMakeCurrent( deviceData.eglDisplay, deviceData.eglSurf, deviceData.eglSurf,
deviceData.eglCtx );
}
}
//-------------------------------------------------------------------------
void EglPBufferSupport::destroyDevice( const size_t deviceIdx )
{
DeviceData &deviceData = mDeviceData[deviceIdx];
LogManager::getSingleton().logMessage( "Destroying device: " + deviceData.name + "..." );
if( deviceData.eglCtx )
{
eglMakeCurrent( deviceData.eglDisplay, 0, 0, 0 );
eglDestroyContext( deviceData.eglDisplay, deviceData.eglCtx );
deviceData.eglCtx = 0;
}
if( deviceData.eglSurf )
{
eglDestroySurface( deviceData.eglDisplay, deviceData.eglSurf );
deviceData.eglSurf = 0;
}
if( deviceData.eglDisplay )
{
eglTerminate( deviceData.eglDisplay );
EGL_CHECK_ERROR
}
deviceData.eglCfg = 0;
deviceData.eglDisplay = 0;
}
//-------------------------------------------------------------------------
const EglPBufferSupport::DeviceData *EglPBufferSupport::getCurrentDevice( void )
{
const size_t selectedDeviceIdx = getSelectedDeviceIdx();
if( !mDeviceData[selectedDeviceIdx].eglDisplay )
initDevice( selectedDeviceIdx );
return &mDeviceData[selectedDeviceIdx];
}
//-------------------------------------------------------------------------
EGLDisplay EglPBufferSupport::getGLDisplay( void )
{
const size_t selectedDeviceIdx = getSelectedDeviceIdx();
if( mDeviceData[selectedDeviceIdx].eglDisplay )
return mDeviceData[selectedDeviceIdx].eglDisplay;
initDevice( selectedDeviceIdx );
return mDeviceData[selectedDeviceIdx].eglDisplay;
}
//-------------------------------------------------------------------------
uint32 EglPBufferSupport::getSelectedDeviceIdx( void ) const
{
uint32 deviceIdx = 0u;
ConfigOptionMap::const_iterator it = mOptions.find( "Device" );
if( it != mOptions.end() )
{
const String deviceName = it->second.currentValue;
FastArray<DeviceData>::const_iterator itDevice =
std::find( mDeviceData.begin(), mDeviceData.end(), deviceName );
if( itDevice != mDeviceData.end() )
deviceIdx = static_cast<uint32>( itDevice - mDeviceData.begin() );
}
return deviceIdx;
}
} // namespace Ogre