GL/Context: Fix crash in some GLX drivers

This commit is contained in:
Connor McLaughlin 2020-06-27 17:23:43 +10:00
parent 15afe4f436
commit 745b53e4cb
2 changed files with 19 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#include "context_glx.h" #include "context_glx.h"
#include "../assert.h" #include "../assert.h"
#include "../log.h" #include "../log.h"
#include <dlfcn.h>
Log_SetChannel(GL::ContextGLX); Log_SetChannel(GL::ContextGLX);
namespace GL { namespace GL {
@ -13,6 +14,9 @@ ContextGLX::~ContextGLX()
if (m_context) if (m_context)
glXDestroyContext(GetDisplay(), m_context); glXDestroyContext(GetDisplay(), m_context);
if (m_libGL_handle)
dlclose(m_libGL_handle);
} }
std::unique_ptr<Context> ContextGLX::Create(const WindowInfo& wi, const Version* versions_to_try, std::unique_ptr<Context> ContextGLX::Create(const WindowInfo& wi, const Version* versions_to_try,
@ -27,6 +31,18 @@ std::unique_ptr<Context> ContextGLX::Create(const WindowInfo& wi, const Version*
bool ContextGLX::Initialize(const Version* versions_to_try, size_t num_versions_to_try) bool ContextGLX::Initialize(const Version* versions_to_try, size_t num_versions_to_try)
{ {
// We need libGL loaded, because GLAD loads its own, then releases it.
m_libGL_handle = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);
if (!m_libGL_handle)
{
m_libGL_handle = dlopen("libGL.so", RTLD_NOW | RTLD_GLOBAL);
if (!m_libGL_handle)
{
Log_ErrorPrintf("Failed to load libGL.so: %s", dlerror());
return false;
}
}
const int screen = DefaultScreen(GetDisplay()); const int screen = DefaultScreen(GetDisplay());
if (!gladLoadGLX(GetDisplay(), screen)) if (!gladLoadGLX(GetDisplay(), screen))
{ {

View file

@ -36,6 +36,9 @@ private:
GLXFBConfig m_fb_config = {}; GLXFBConfig m_fb_config = {};
XVisualInfo* m_vi = nullptr; XVisualInfo* m_vi = nullptr;
X11Window m_window; X11Window m_window;
// GLAD releases its reference to libGL.so, so we need to maintain our own.
void* m_libGL_handle = nullptr;
}; };
} // namespace GL } // namespace GL