Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy OpenGL example is broken #620

Open
GrimMaple opened this issue Jul 26, 2022 · 7 comments
Open

Legacy OpenGL example is broken #620

GrimMaple opened this issue Jul 26, 2022 · 7 comments
Assignees

Comments

@GrimMaple
Copy link
Collaborator

Currenty it draws just some small bit on the bottom of the window.

@GrimMaple GrimMaple self-assigned this Jul 26, 2022
@Superbelko
Copy link
Contributor

Oh, I think it was like that for eternity. IIRC in platform code it basically sticks to whatever API is available and won't ever try to recreate anything.

But if you say that there is small bit of actual drawing maybe it is just glViewport or projection/model matrices or something was accidentally removed somewhere?

@GrimMaple
Copy link
Collaborator Author

I recently rewritten the window creation code to create a compatibility GL context. Which doesn't seem to work, unfortunately :(
Creating an "old" version context (eg 2.0) also doesn't bring much joy to the world, as DlangUI itself requires modern API to draw itself. As a result, I'm a bit stuck. Either have to implement the pipeline for fixed API as well, or have to figure out compatibility context :/

@Superbelko
Copy link
Contributor

I recently rewritten the window creation code to create a compatibility GL context.

You mean core (aka forward compatible) context? I have implemented that locally for testing, Windows only though, can add PR for that, basically 20-30 lines of code including declarations and few changes to be able to reset internal stuff.

@GrimMaple
Copy link
Collaborator Author

I recently rewritten the window creation code to create a compatibility GL context.

You mean core (aka forward compatible) context? I have implemented that locally for testing, Windows only though, can add PR for that, basically 20-30 lines of code including declarations and few changes to be able to reset internal stuff.

There's no need to, as I already implemented it in c9ccaad . Multisampling is done the same way as getting compatibility GL context. I just need to figure out why said compatibility doesn't really work :)

@Superbelko
Copy link
Contributor

I recently rewritten the window creation code to create a compatibility GL context.

You mean core (aka forward compatible) context? I have implemented that locally for testing, Windows only though, can add PR for that, basically 20-30 lines of code including declarations and few changes to be able to reset internal stuff.

There's no need to, as I already implemented it in c9ccaad . Multisampling is done the same way as getting compatibility GL context. I just need to figure out why said compatibility doesn't really work :)

Umm, where do you call wglCreateContextAttribsARB?
It is required for core contexts as by default it sticks to legacy fixed-functions pipeline compatible contexts.

Or I am thinking about different thing here?

@GrimMaple
Copy link
Collaborator Author

I recently rewritten the window creation code to create a compatibility GL context.

You mean core (aka forward compatible) context? I have implemented that locally for testing, Windows only though, can add PR for that, basically 20-30 lines of code including declarations and few changes to be able to reset internal stuff.

There's no need to, as I already implemented it in c9ccaad . Multisampling is done the same way as getting compatibility GL context. I just need to figure out why said compatibility doesn't really work :)

Umm, where do you call wglCreateContextAttribsARB? It is required for core contexts as by default it sticks to legacy fixed-functions pipeline compatible contexts.

Or I am thinking about different thing here?

I think that might be it, actually. I used wglChoosePixelFormatARB, but for context creation, I used the non-extension funtion. I'll test it when I get the chance, thank you!

@Superbelko
Copy link
Contributor

Here is my local stuff I did for testing, patch file for reference.

Windows only, other platforms does something similar except using their own variants like glXCreateContextAttribsARB for Linux and whatever it is on Mac OS.

Basically my intent was to be able to use it in a game or at least use it as editor UI

 src/dlangui/graphics/glsupport.d       |  7 +++++
 src/dlangui/platforms/windows/winapp.d | 48 ++++++++++++++++++++++++++++++----
 2 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/src/dlangui/graphics/glsupport.d b/src/dlangui/graphics/glsupport.d
index e71f0f47..48cb32b3 100644
--- a/src/dlangui/graphics/glsupport.d
+++ b/src/dlangui/graphics/glsupport.d
@@ -662,6 +662,13 @@ private __gshared GLSupport _glSupport;
 
 __gshared bool glNoContext;
 
+void resetGlSupport()
+{
+    if (_glSupport)
+        destroy(_glSupport);
+    _glSupport = null;
+}
+
 /// initialize OpenGL support helper (call when current OpenGL context is initialized)
 bool initGLSupport(bool legacy = false) {
     import dlangui.platforms.common.platform : setOpenglEnabled;
diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d
index d5af722e..3e04c763 100644
--- a/src/dlangui/platforms/windows/winapp.d
+++ b/src/dlangui/platforms/windows/winapp.d
@@ -45,6 +45,20 @@ import dlangui.core.files;
 
 static if (ENABLE_OPENGL) {
     import dlangui.graphics.glsupport;
+
+    alias PFNWGLSWAPINTERVALEXTPROC = extern(C) void function(GLint);
+    PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
+
+    // context creation
+
+    alias PFN_wglCreateContextAttribsARB = extern(C) HGLRC function(HDC hdc, HGLRC hShareContext, const int *attribList);
+    PFN_wglCreateContextAttribsARB wglCreateContextAttribsARB;
+
+    enum WGL_CONTEXT_MAJOR_VERSION_ARB =             0x2091;
+    enum WGL_CONTEXT_MINOR_VERSION_ARB =             0x2092;
+    enum WGL_CONTEXT_PROFILE_MASK_ARB =              0x9126;
+
+    enum WGL_CONTEXT_CORE_PROFILE_BIT_ARB =          0x00000001;
 }
 
 // specify debug=DebugMouseEvents for logging mouse handling
@@ -72,14 +86,14 @@ static if (ENABLE_OPENGL) {
                 PFD_DRAW_TO_WINDOW |
                 PFD_DOUBLEBUFFER,               /* support double-buffering */
             PFD_TYPE_RGBA,                  /* color type */
-            16,                             /* prefered color depth */
+            32,                             /* prefered color depth */
             0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
             0,                              /* no alpha buffer */
             0,                              /* alpha bits (ignored) */
             0,                              /* no accumulation buffer */
             0, 0, 0, 0,                     /* accum bits (ignored) */
-            16,                             /* depth buffer */
-            0,                              /* no stencil buffer */
+            24,                             /* depth buffer */
+            8,                              /* no stencil buffer */
             0,                              /* no auxiliary buffers */
             0,                              /* main layer PFD_MAIN_PLANE */
             0,                              /* reserved */
@@ -184,6 +198,30 @@ static if (ENABLE_OPENGL) {
                 if (_hGLRC) {
                     bind(hDC);
                     bool initialized = initGLSupport(Platform.instance.GLVersionMajor < 3);
+                    
+                    wglSwapIntervalEXT = cast(typeof(wglSwapIntervalEXT)) wglGetProcAddress("wglSwapIntervalEXT");
+                    wglCreateContextAttribsARB = cast(PFN_wglCreateContextAttribsARB) wglGetProcAddress("wglCreateContextAttribsARB");
+
+                    // Specify that we want to create an OpenGL 3.3 core profile context
+                    int[] gl33_attribs = [
+                        WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
+                        WGL_CONTEXT_MINOR_VERSION_ARB, 3,
+                        WGL_CONTEXT_PROFILE_MASK_ARB,  WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
+                        0,
+                    ];
+
+                    // try to create core context if possible
+                    if (auto coreGLRC = wglCreateContextAttribsARB(hDC, null, gl33_attribs.ptr)) {
+                        resetGlSupport();
+                        wglMakeCurrent(hDC, null);
+                        wglDeleteContext(_hGLRC);
+                        _hGLRC = coreGLRC;
+                        wglMakeCurrent(hDC, _hGLRC);
+                        initialized = initGLSupport();
+                    }
+
+                    if (wglSwapIntervalEXT) 
+                        wglSwapIntervalEXT(0); // no vsync
                     unbind(hDC);
                     if (!initialized) {
                         uninit();
@@ -340,9 +378,9 @@ class Win32Window : Window {
             float g = ((_backgroundColor >> 8) & 255) / 255.0f;
             float b = ((_backgroundColor >> 0) & 255) / 255.0f;
             glClearColor(r, g, b, a);
-            glClear(GL_COLOR_BUFFER_BIT);
+            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);           
 
-            GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false);
+            scope GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false);
             buf.beforeDrawing();
             static if (false) {
                 // for testing for render

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants