From eb7684da7e9de1a0ccf3ff2d2ddd3284a671eba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 25 Aug 2019 15:43:40 +0200 Subject: [PATCH] Platform: reuse the dynamically loaded opengl32.dll. Instead of using wglGetProcAddress directly and thus forcing a link-time dependency on that DLL (and because this is a static lib also forcing all users to link to it). --- .../Implementation/OpenGLFunctionLoader.cpp | 22 +++++++++++++------ .../Implementation/OpenGLFunctionLoader.h | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.cpp b/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.cpp index fb051ad37..2587c90a0 100644 --- a/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.cpp +++ b/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.cpp @@ -86,20 +86,28 @@ auto OpenGLFunctionLoader::load(const char* const name) -> FunctionPointer { /* WGL-specific implementation */ #elif defined(CORRADE_TARGET_WINDOWS) OpenGLFunctionLoader::OpenGLFunctionLoader() { - library = GetModuleHandleA("OpenGL32.dll"); + library = GetModuleHandleA("opengl32.dll"); + getProcAddress = reinterpret_cast(GetProcAddress(library, "wglGetProcAddress")); } -/** @todo closing the library is not needed? */ +/* FreeLibrary() should not be called: + https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlea */ OpenGLFunctionLoader::~OpenGLFunctionLoader() = default; auto OpenGLFunctionLoader::load(const char* const name) -> FunctionPointer { - /** @todo Is this shit real?! OH MY GOD. */ - const PROC address = wglGetProcAddress(reinterpret_cast(name)); - const auto integerAddress = reinterpret_cast(address); - if(address && integerAddress != 1 && integerAddress != 2 && + /* First try wglGetProcAddress that we extracted above, then a + normal GetProcAddress(). Not using wglGetProcAddress() directly because + that would mean we need to explicitly link to opengl32.dll (and since + this is inside a static library, also forcing our users to link to it + too). And why do that when `library` has that already anyway. */ + const PROC address = reinterpret_cast(getProcAddress)(reinterpret_cast(name)); + /* This actually is real, it seems -- OH MY GOD + https://community.khronos.org/t/wglgetprocaddress/77122 */ + const auto integerAddress = reinterpret_cast(address); + if(address && integerAddress != 1 && integerAddress != 2 && integerAddress != 3 && integerAddress != -1) return reinterpret_cast(address); - return reinterpret_cast(GetProcAddress(library, reinterpret_cast(name))); + return reinterpret_cast(GetProcAddress(library, reinterpret_cast(name))); } /* GLX-specific implementation */ diff --git a/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.h b/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.h index 0d9173c80..2bcbf13f5 100644 --- a/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.h +++ b/src/Magnum/Platform/Implementation/OpenGLFunctionLoader.h @@ -55,6 +55,7 @@ class OpenGLFunctionLoader { /* WGL-specific handles */ #elif defined(CORRADE_TARGET_WINDOWS) HMODULE library; + FunctionPointer getProcAddress; /* GLX-specific handles (nothing needed) */ #elif defined(CORRADE_TARGET_UNIX) && defined(MAGNUM_PLATFORM_USE_GLX)