refactor OpenGL initialization

This commit is contained in:
Vadim Lopatin 2015-12-21 11:35:31 +03:00
parent 2cb21339e5
commit 349c6612e1
3 changed files with 77 additions and 64 deletions

View File

@ -455,6 +455,76 @@ __gshared GLSupport _glSupport;
return _glSupport;
}
/// initialize OpenGL suport helper (call when current OpenGL context is initialized)
bool initGLSupport(bool legacy = false) {
import dlangui.platforms.common.platform : setOpenglEnabled;
if (_glSupport && _glSupport.valid)
return true;
static bool DERELICT_GL3_RELOADED;
static bool gl3ReloadedOk;
static bool glReloadedOk;
if (!DERELICT_GL3_RELOADED) {
DERELICT_GL3_RELOADED = true;
try {
Log.v("Reloading DerelictGL3");
import derelict.opengl3.gl3;
DerelictGL3.missingSymbolCallback = &gl3MissingSymFunc;
DerelictGL3.reload();
gl3ReloadedOk = true;
} catch (Exception e) {
Log.e("Derelict exception while reloading DerelictGL3", e);
}
try {
Log.v("Reloading DerelictGL");
import derelict.opengl3.gl;
DerelictGL.missingSymbolCallback = &gl3MissingSymFunc;
DerelictGL.reload();
glReloadedOk = true;
} catch (Exception e) {
Log.e("Derelict exception while reloading DerelictGL", e);
}
}
if (!gl3ReloadedOk && !glReloadedOk) {
Log.e("Neither DerelictGL3 nor DerelictGL were reloaded successfully");
return false;
}
if (!gl3ReloadedOk)
legacy = true;
else if (!glReloadedOk)
legacy = false;
if (!_glSupport) {
_glSupport = new GLSupport(legacy);
if (_glSupport.valid || _glSupport.initShaders()) {
Log.v("shaders are ok");
setOpenglEnabled();
Log.v("OpenGL is initialized ok");
return true;
} else {
Log.e("Failed to compile shaders");
// try opposite legacy flag
if (_glSupport.legacyMode == legacy) {
Log.i("Trying to reinit GLSupport with legacy flag ", !legacy);
_glSupport = new GLSupport(!legacy);
if (_glSupport.valid || _glSupport.initShaders()) {
Log.v("shaders are ok");
setOpenglEnabled();
Log.v("OpenGL is initialized ok");
return true;
}
}
}
return false;
}
if (_glSupport.valid || _glSupport.initShaders()) {
setOpenglEnabled();
return true;
} else {
Log.e("Failed to compile shaders");
return false;
}
}
/// OpenGL suport helper
class GLSupport {
private bool _legacyMode;

View File

@ -152,14 +152,6 @@ class SDLWindow : Window {
static if (ENABLE_OPENGL) {
if (_enableOpengl)
windowFlags |= SDL_WINDOW_OPENGL;
if (!_glSupport) {
version(OSX) {
bool useLegacyOpengl = false; //true;
} else {
bool useLegacyOpengl = false;
}
_glSupport = new GLSupport(useLegacyOpengl);
}
}
_win = SDL_CreateWindow(toUTF8(_caption).toStringz, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
_dx, _dy,
@ -200,21 +192,9 @@ class SDLWindow : Window {
Log.w("OpenGL support is disabled");
}
}
if (_context && !_gl3Reloaded) {
try {
Log.d("Reloading DerelictGL3");
DerelictGL3.missingSymbolCallback = &gl3MissingSymFunc;
DerelictGL3.reload(GLVersion.GL21, GLVersion.GL40);
_gl3Reloaded = true;
if (!glSupport.valid && !glSupport.initShaders()) {
Log.e("Failed to initialize OpenGL - disabling OpenGL support");
_enableOpengl = false;
}
fixSize();
} catch (derelict.util.exception.SymbolLoadException e) {
Log.e("Exception in DerelictGL3.reload ", e);
_enableOpengl = false;
}
if (_context && !_glSupport) {
_enableOpengl = initGLSupport(false);
fixSize();
}
}
}

View File

@ -205,56 +205,19 @@ class Win32Window : Window {
/* initialize OpenGL rendering */
HDC hDC = GetDC(_hwnd);
if (!DERELICT_GL3_RELOADED || openglEnabled) {
if (openglEnabled || !_glSupport) {
if (setupPixelFormat(hDC)) {
_hPalette = setupPalette(hDC);
_hGLRC = wglCreateContext(hDC);
if (_hGLRC) {
wglMakeCurrent(hDC, _hGLRC);
//_glSupport = _gl;
if (!DERELICT_GL3_RELOADED) {
// run this code only once
if (!_glSupport) {
Log.v("Creating OpenGL support");
_glSupport = new GLSupport(true);
Log.v("OpenGL support created");
}
DERELICT_GL3_RELOADED = true;
try {
import derelict.opengl3.gl3;
DerelictGL3.reload();
// successful
Log.v("initializing shaders");
if (glSupport.valid || glSupport.initShaders()) {
Log.v("shaders are ok");
setOpenglEnabled();
useOpengl = true;
Log.v("OpenGL is initialized ok");
} else {
Log.e("Failed to compile shaders");
}
} catch (Exception e) {
Log.e("Derelict exception", e);
}
} else {
if (glSupport.valid || glSupport.initShaders()) {
setOpenglEnabled();
useOpengl = true;
} else {
Log.e("Failed to compile shaders");
}
}
useOpengl = initGLSupport(false);
wglMakeCurrent(hDC, null);
}
} else {
Log.e("Pixelformat failed");
// disable GL
DERELICT_GL3_RELOADED = true;
useOpengl = false;
}
}
}
@ -443,7 +406,7 @@ class Win32Window : Window {
void onSetCursorType() {
HANDLE winCursor = null;
switch (_cursorType) with(CursorType)
switch (_cursorType) with(CursorType)
{
case None:
winCursor = null;