From d19ff3c04295d482461616568a488ddc98640825 Mon Sep 17 00:00:00 2001 From: Grim Maple Date: Sat, 16 Apr 2022 13:49:04 +0300 Subject: [PATCH] Fix opengl support, switch to bindbc --- dub.json | 20 ++-- examples/opengl/src/openglexample.d | 2 - src/dlangui/graphics/glsupport.d | 64 ++----------- src/dlangui/platforms/windows/winapp.d | 125 ++++++++----------------- 4 files changed, 59 insertions(+), 152 deletions(-) diff --git a/dub.json b/dub.json index 5ce8f741..851176d6 100644 --- a/dub.json +++ b/dub.json @@ -18,7 +18,7 @@ ], "libs-linux": ["z"], - + "stringImportPaths": [ "views" ], @@ -29,6 +29,12 @@ "sourceFiles-windows-x86-dmd": ["$PACKAGE_DIR/src/win_app.def"], "excludedSourceFiles-windows": ["3rdparty/fontconfig/*"], + "dependencies": { + "bindbc-opengl": "~>1.0.0" + }, + + "versions": ["GL_AllowDeprecated", "GL_30"], + "subPackages": [ "./examples/helloworld/", "./examples/example1/", @@ -50,7 +56,6 @@ "versions-windows": ["Unicode"], "libs-windows": ["opengl32"], "dependencies": { - "derelict-gl3": "~>2.0.0-beta.8", "derelict-ft": "~>2.0.0-beta.5", "icontheme": "~>1.2.3" }, @@ -74,7 +79,6 @@ "versions": ["USE_EXTERNAL"], "libs-windows": ["opengl32"], "dependencies": { - "derelict-gl3": "~>2.0.0-beta.8", "derelict-ft": "~>2.0.0-beta.5", "icontheme": "~>1.2.3" } @@ -86,7 +90,6 @@ "versions-windows": ["Unicode" ,"NO_OPENGL"], "libs-windows": ["opengl32"], "dependencies-posix": { - "derelict-gl3": "~>2.0.0-beta.8", "derelict-sdl2": "~>3.0.0-beta.8", "derelict-ft": "~>2.0.0-beta.5", "icontheme": "~>1.2.3" @@ -97,17 +100,16 @@ "versions": ["USE_SDL", "USE_OPENGL", "USE_FREETYPE", "EmbedStandardResources"], "versions-windows": ["Unicode"], "dependencies": { - "derelict-gl3": "~>2.0.0-beta.8", "derelict-ft": "~>2.0.0-beta.5", "derelict-sdl2": "~>3.0.0-beta.8", "icontheme": "~>1.2.3" }, "copyFiles-windows-x86_64": [ - "libs/windows/x86_64/libfreetype-6.dll", - "libs/windows/x86_64/SDL2.dll", + "libs/windows/x86_64/libfreetype-6.dll", + "libs/windows/x86_64/SDL2.dll" ], "copyFiles-windows-x86": [ - "libs/windows/x86/libfreetype-6.dll", + "libs/windows/x86/libfreetype-6.dll", "libs/windows/x86/SDL2.dll" ] }, @@ -116,7 +118,6 @@ "versions": ["USE_X11", "USE_FREETYPE", "EmbedStandardResources"], "versions-windows": ["Unicode"], "dependencies": { - "derelict-gl3": "~>2.0.0-beta.8", "derelict-ft": "~>2.0.0-beta.5", "x11": "~>1.0.21", "icontheme": "~>1.2.3" @@ -127,7 +128,6 @@ "versions": ["USE_DSFML", "USE_OPENGL", "USE_FREETYPE", "EmbedStandardResources"], "versions-windows": ["Unicode"], "dependencies": { - "derelict-gl3": "~>2.0.0-beta.7", "derelict-ft": "~>2.0.0-beta.4", "dsfml": "~>2.1.0", "icontheme": "~>1.2.3" diff --git a/examples/opengl/src/openglexample.d b/examples/opengl/src/openglexample.d index 0523afbc..6d47904b 100644 --- a/examples/opengl/src/openglexample.d +++ b/examples/opengl/src/openglexample.d @@ -33,8 +33,6 @@ extern (C) int UIAppMain(string[] args) { static if (ENABLE_OPENGL): -import derelict.opengl3.gl3; -import derelict.opengl3.gl; import dlangui.graphics.glsupport; import dlangui.graphics.gldrawbuf; diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d index 3232c134..3919afe4 100644 --- a/src/dlangui/graphics/glsupport.d +++ b/src/dlangui/graphics/glsupport.d @@ -48,31 +48,7 @@ version (Android) { } else { enum SUPPORT_LEGACY_OPENGL = false; //true; - public import derelict.opengl; - import derelict.util.exception; - //public import derelict.opengl.types; - //public import derelict.opengl.versions.base; - //public import derelict.opengl.versions.gl3x; - //public import derelict.opengl.gl; - -derelict.util.exception.ShouldThrow gl3MissingSymFunc( string symName ) { - import std.algorithm : equal; - static import derelict.util.exception; - foreach(s; ["glGetError", "glShaderSource", "glCompileShader", - "glGetShaderiv", "glGetShaderInfoLog", "glGetString", - "glCreateProgram", "glUseProgram", "glDeleteProgram", - "glDeleteShader", "glEnable", "glDisable", "glBlendFunc", - "glUniformMatrix4fv", "glGetAttribLocation", "glGetUniformLocation", - "glGenVertexArrays", "glBindVertexArray", "glBufferData", - "glBindBuffer", "glBufferSubData"]) { - if (symName.equal(s)) // Symbol is used - return derelict.util.exception.ShouldThrow.Yes; - } - // Don't throw for unused symbol - return derelict.util.exception.ShouldThrow.No; -} - - + public import bindbc.opengl; } import dlangui.graphics.scene.mesh; @@ -668,40 +644,16 @@ bool initGLSupport(bool legacy = false) { version(Android) { Log.d("initGLSupport"); } else { - 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.opengl; //.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.opengl; //.gl; - DerelictGL3.missingSymbolCallback = &gl3MissingSymFunc; - DerelictGL3.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"); + + auto support = loadOpenGL(); + if(support < bindbc.opengl.GLSupport.gl11) // No context! Error! + { + Log.e("OpenGL wasn't loaded successfully"); return false; } - if (!gl3ReloadedOk) - legacy = true; - else if (!glReloadedOk) - legacy = false; + legacy = false; } - if (!_glSupport) { + if (!_glSupport) { // TODO_GRIM: Legacy looks very broken to me. Log.d("glSupport not initialized: trying to create"); int major = *cast(int*)(glGetString(GL_VERSION)[0 .. 1].ptr); legacy = legacy || (major < 3); diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 8fbfa826..d96a905d 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -160,7 +160,7 @@ static if (ENABLE_OPENGL) { /// Shared opengl context helper struct SharedGLContext { - import derelict.opengl; //3.wgl; + import bindbc.opengl; HGLRC _hGLRC; // opengl context HPALETTE _hPalette; @@ -323,8 +323,6 @@ class Win32Window : Window { EndPaint(_hwnd, &ps); - import derelict.opengl; //3.gl3; - import derelict.opengl; //3.wgl; import dlangui.graphics.gldrawbuf; //Log.d("onPaint() start drawing opengl viewport: ", _dx, "x", _dy); //PAINTSTRUCT ps; @@ -392,7 +390,7 @@ class Win32Window : Window { destroy(_drawbuf); _drawbuf = null; } - + /* static if (ENABLE_OPENGL) { import derelict.opengl3.wgl; @@ -419,10 +417,10 @@ class Win32Window : Window { } /// set handler for files dropped to app window - override @property Window onFilesDropped(void delegate(string[]) handler) { + override @property Window onFilesDropped(void delegate(string[]) handler) { super.onFilesDropped(handler); DragAcceptFiles(_hwnd, handler ? TRUE : FALSE); - return this; + return this; } private long _nextExpectedTimerTs; @@ -436,7 +434,7 @@ class Win32Window : Window { if (_timerId && _nextExpectedTimerTs && _nextExpectedTimerTs < nextts + 10) return; // don't reschedule timer, timer event will be received soon if (_hwnd) { - //_timerId = + //_timerId = SetTimer(_hwnd, _timerId, cast(uint)intervalMillis, null); _nextExpectedTimerTs = nextts; } @@ -488,9 +486,9 @@ class Win32Window : Window { else adjustWindowOrContentSize(_mainWidget.measuredWidth, _mainWidget.measuredHeight); } - + adjustPositionDuringShow(); - + if (_flags & WindowFlag.Fullscreen) { Rect rc = getScreenDimensions(); SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, rc.width, rc.height, SWP_SHOWWINDOW); @@ -508,15 +506,15 @@ class Win32Window : Window { override @property Window parentWindow() { return _w32parent; } - + override protected void handleWindowActivityChange(bool isWindowActive) { super.handleWindowActivityChange(isWindowActive); } - + override @property bool isActive() { return _hwnd == GetForegroundWindow(); } - + override @property dstring windowCaption() const { return _caption; } @@ -584,7 +582,7 @@ class Win32Window : Window { res = true; } break; - case WindowState.normal: + case WindowState.normal: if (_windowState != WindowState.normal || activate) { ShowWindow(_hwnd, activate ? SW_SHOWNORMAL : SW_SHOWNA); // SW_RESTORE res = true; @@ -622,15 +620,15 @@ class Win32Window : Window { } } } - + if (rectChanged) { - handleWindowStateChange(newState, Rect(newWindowRect.left == int.min ? _windowRect.left : newWindowRect.left, - newWindowRect.top == int.min ? _windowRect.top : newWindowRect.top, newWindowRect.right == int.min ? _windowRect.right : newWindowRect.right, + handleWindowStateChange(newState, Rect(newWindowRect.left == int.min ? _windowRect.left : newWindowRect.left, + newWindowRect.top == int.min ? _windowRect.top : newWindowRect.top, newWindowRect.right == int.min ? _windowRect.right : newWindowRect.right, newWindowRect.bottom == int.min ? _windowRect.bottom : newWindowRect.bottom)); } else handleWindowStateChange(newState, RECT_VALUE_IS_NOT_SET); - + return res; } @@ -652,7 +650,7 @@ class Win32Window : Window { Log.d("Window.close()"); _platform.closeWindow(this); } - + override protected void handleWindowStateChange(WindowState newState, Rect newWindowRect = RECT_VALUE_IS_NOT_SET) { if (_destroying) return; @@ -1175,22 +1173,22 @@ class Win32Platform : Platform { if (mouseBuffer) return res; // not supporetd under win32 if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) - return res; - if (!OpenClipboard(NULL)) - return res; + return res; + if (!OpenClipboard(NULL)) + return res; - HGLOBAL hglb = GetClipboardData(CF_UNICODETEXT); - if (hglb != NULL) - { - LPWSTR lptstr = cast(LPWSTR)GlobalLock(hglb); - if (lptstr != NULL) - { + HGLOBAL hglb = GetClipboardData(CF_UNICODETEXT); + if (hglb != NULL) + { + LPWSTR lptstr = cast(LPWSTR)GlobalLock(hglb); + if (lptstr != NULL) + { wstring w = fromWStringz(lptstr); res = normalizeEndOfLineCharacters(toUTF32(w)); - GlobalUnlock(hglb); - } - } + GlobalUnlock(hglb); + } + } CloseClipboard(); //Log.d("getClipboardText(", res, ")"); @@ -1203,14 +1201,14 @@ class Win32Platform : Platform { if (text.length < 1 || mouseBuffer) return; if (!OpenClipboard(NULL)) - return; + return; EmptyClipboard(); wstring w = toUTF16(text); - HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, - cast(uint)((w.length + 1) * TCHAR.sizeof)); - if (hglbCopy == NULL) { - CloseClipboard(); - return; + HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, + cast(uint)((w.length + 1) * TCHAR.sizeof)); + if (hglbCopy == NULL) { + CloseClipboard(); + return; } LPWSTR lptstrCopy = cast(LPWSTR)GlobalLock(hglbCopy); for (int i = 0; i < w.length; i++) { @@ -1218,7 +1216,7 @@ class Win32Platform : Platform { } lptstrCopy[w.length] = 0; GlobalUnlock(hglbCopy); - SetClipboardData(CF_UNICODETEXT, hglbCopy); + SetClipboardData(CF_UNICODETEXT, hglbCopy); CloseClipboard(); } @@ -1254,7 +1252,7 @@ int DLANGUIWinMain(void* hInstance, void* hPrevInstance, } extern(Windows) -int DLANGUIWinMainProfile(string[] args) +int DLANGUIWinMainProfile(string[] args) { int result; @@ -1308,35 +1306,6 @@ string[] splitCmdLine(string line) { private __gshared Win32Platform w32platform; -static if (ENABLE_OPENGL) { - import derelict.opengl; //3.gl3; - //import derelict.opengl3.gl; - - void initOpenGL() { - try { - Log.d("Loading Derelict GL"); - //DerelictGL.load(); - DerelictGL3.load(); - Log.d("Derelict GL - loaded"); - // - //// just to check OpenGL context - //Log.i("Trying to setup OpenGL context"); - //Win32Window tmpWindow = new Win32Window(w32platform, ""d, null, 0); - //destroy(tmpWindow); - //if (openglEnabled) - // Log.i("OpenGL support is enabled"); - //else - // Log.w("OpenGL support is disabled"); - //// process messages - //platform.enterMessageLoop(); - } catch (Exception e) { - Log.e("Exception while trying to init OpenGL", e); - setOpenglEnabled(false); - } - } -} - - int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdShow) { initLogs(); @@ -1376,12 +1345,6 @@ int myWinMain(void* hInstance, void* hPrevInstance, char* lpCmdLine, int iCmdSho currentTheme = createDefaultTheme(); - static if (ENABLE_OPENGL) { - initOpenGL(); - } - - // Load versions 1.2+ and all supported ARB and EXT extensions. - Log.i("Entering UIAppMain: ", args); int result = -1; try { @@ -1443,12 +1406,6 @@ int myWinMainProfile(string[] args) currentTheme = createDefaultTheme(); - static if (ENABLE_OPENGL) { - initOpenGL(); - } - - // Load versions 1.2+ and all supported ARB and EXT extensions. - Log.i("Entering UIAppMain: ", args); int result = -1; try { @@ -1514,7 +1471,7 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_WINDOWPOSCHANGED: { if (window !is null) { - + if (IsIconic(hwnd)) { window.handleWindowStateChange(WindowState.minimized); } @@ -1547,7 +1504,7 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) case WM_ACTIVATE: { if (window) { - if (wParam == WA_INACTIVE) + if (wParam == WA_INACTIVE) window.handleWindowActivityChange(false); else if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE) window.handleWindowActivityChange(true); @@ -1617,7 +1574,7 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) // not a key we map from generic to left/right specialized // just return it. new_vk = vk; - break; + break; } if (window.onKey(message == WM_KEYDOWN || message == WM_SYSKEYDOWN ? KeyAction.KeyDown : KeyAction.KeyUp, cast(uint)new_vk, repeatCount, 0, message == WM_SYSKEYUP || message == WM_SYSKEYDOWN)) @@ -1657,9 +1614,9 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) wchar[] buf; auto count = DragQueryFileW(hdrop, 0xFFFFFFFF, cast(wchar*)NULL, 0); for (int i = 0; i < count; i++) { - auto sz = DragQueryFileW(hdrop, i, cast(wchar*)NULL, 0); + auto sz = DragQueryFileW(hdrop, i, cast(wchar*)NULL, 0); buf.length = sz + 2; - sz = DragQueryFileW(hdrop, i, buf.ptr, sz + 1); + sz = DragQueryFileW(hdrop, i, buf.ptr, sz + 1); files ~= toUTF8(buf[0..sz]); } if (files.length)