Skip to content

Commit

Permalink
win32: migrate to imgui's opengl loader [p=2656531]
Browse files Browse the repository at this point in the history
glbindings has a fatal bug when initializing and releasing multiple contexts (cginternals/glbinding#343)
+ it adds an excessive 1.4MB to the size of release builds
  • Loading branch information
cfillion committed Mar 11, 2023
1 parent c56fc7a commit cbecce5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
5 changes: 1 addition & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ if(WIN32)
win32_window.cpp
)

target_link_libraries(src Dwmapi Imm32)

find_package(glbinding REQUIRED)
target_link_libraries(src glbinding::glbinding Opengl32)
target_link_libraries(src Dwmapi Imm32 Opengl32)

set(D3D_SHADERS
"d3d10_pixel.hlsl\;ps_4_0"
Expand Down
6 changes: 4 additions & 2 deletions src/opengl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#ifdef __APPLE__
# include <OpenGL/gl3.h>
#elif _WIN32
# include <glbinding/gl32/gl.h>
using namespace gl32;
# include <imgui/backends/imgui_impl_opengl3_loader.h>
constexpr int GL_TEXTURE_WRAP_S { 0x2802 },
GL_TEXTURE_WRAP_T { 0x2803 },
GL_REPEAT { 0x2901 };
#else
# include <epoxy/gl.h>
#endif
Expand Down
47 changes: 20 additions & 27 deletions src/win32_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#include "error.hpp"
#include "window.hpp"

#include <glbinding/glbinding.h>
#define IMGL3W_IMPL
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_opengl3_loader.h>

// https://registry.khronos.org/OpenGL/api/GL/wglext.h
constexpr int WGL_CONTEXT_MAJOR_VERSION_ARB { 0x2091 },
Expand All @@ -31,11 +32,6 @@ constexpr int WGL_CONTEXT_MAJOR_VERSION_ARB { 0x2091 },
typedef HGLRC (WINAPI *PFNWGLCREATECONTEXTATTRIBSARBPROC)
(HDC hDC, HGLRC hShareContext, const int *attribList);

static glbinding::ProcAddress getProcAddress(const char *funcName)
{
return reinterpret_cast<glbinding::ProcAddress>(wglGetProcAddress(funcName));
}

class Win32OpenGL : public OpenGLRenderer {
public:
Win32OpenGL(RendererFactory *, Window *);
Expand All @@ -59,7 +55,6 @@ class MakeCurrent {
: m_gl { gl }
{
wglMakeCurrent(dc, m_gl);
glbinding::useContext(reinterpret_cast<glbinding::ContextHandle>(m_gl));
}

~MakeCurrent()
Expand All @@ -74,7 +69,6 @@ class MakeCurrent {
struct GLDeleter {
void operator()(HGLRC gl)
{
glbinding::releaseContext(reinterpret_cast<glbinding::ContextHandle>(gl));
wglDeleteContext(gl);
}
};
Expand Down Expand Up @@ -135,31 +129,30 @@ void Win32OpenGL::setPixelFormat()
void Win32OpenGL::createContext()
{
HGLRC dummyGl { wglCreateContext(m_dc) }; // creates a legacy (< 2.1) context
wglMakeCurrent(m_dc, dummyGl);
wglMakeCurrent(m_dc, m_gl = dummyGl);

PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB
{ reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>
(wglGetProcAddress("wglCreateContextAttribsARB")) };

if(!wglCreateContextAttribsARB)
throw backend_error { "OpenGL 3 is not available on this system" };

// https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt
constexpr int attrs[] {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};

m_gl = wglCreateContextAttribsARB(m_dc, nullptr, attrs);
wglMakeCurrent(m_dc, m_gl);
wglDeleteContext(dummyGl);

if(!m_gl)
throw backend_error { "failed to initialize OpenGL 3.2 core context" };
if(wglCreateContextAttribsARB) {
// https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt
constexpr int attrs[] {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 2,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};

if(HGLRC coreGl { wglCreateContextAttribsARB(m_dc, nullptr, attrs) }) {
wglMakeCurrent(m_dc, m_gl = coreGl);
wglDeleteContext(dummyGl);
}
}

glbinding::useContext(reinterpret_cast<glbinding::ContextHandle>(m_gl));
glbinding::initialize(getProcAddress, false);
if(imgl3wInit()) {
wglDeleteContext(m_gl);
throw backend_error { "OpenGL 3.2 is not available on this system" };
}
}

void Win32OpenGL::render(void *)
Expand Down
1 change: 0 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
{ "name": "boost-variant2", "platform": "!windows" },
{ "name": "freetype", "default-features": false,
"features": ["png", "zlib"], "platform": "!linux" },
{ "name": "glbinding", "platform": "windows" },
{ "name": "libjpeg-turbo" },
{ "name": "libpng", "platform": "!linux" },
"cmark",
Expand Down

0 comments on commit cbecce5

Please sign in to comment.