Skip to content

Commit

Permalink
Add logic to capture/restore existing GL context.
Browse files Browse the repository at this point in the history
This fixes asserts in debug builds where Qt's GL context is set when
a ScopedGLContext is created. This can happen when we have a project
open and the user tries to open another project. When the event
callback is dispatched, the Qt GL context is still set. The fix
justs removes the assert and adds logic to save and restore any
existing GL context that is set.
  • Loading branch information
acolwell committed Jul 2, 2024
1 parent eb1a3a8 commit ba49dce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 25 additions & 8 deletions Engine/OSGLContext_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;

NATRON_NAMESPACE_ENTER

namespace {

bool makeDCAndContextCurrent(HDC dc, HGLRC context)
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();

assert(wglInfo);
return wglInfo->MakeCurrent(dc, context);
}

} // namespace

bool
OSGLContext_win::extensionSupported(const char* extension,
OSGLContext_wgl_data* data)
Expand Down Expand Up @@ -96,6 +108,7 @@ OSGLContext_win::initWGLData(OSGLContext_wgl_data* wglInfo)
wglInfo->CreateContext = (WGLCREATECONTEXT_T)GetProcAddress(wglInfo->instance, "wglCreateContext");
wglInfo->DeleteContext = (WGLDELETECONTEXT_T)GetProcAddress(wglInfo->instance, "wglDeleteContext");
wglInfo->GetCurrentContext = (WGLGETCURRENTCONTEXT_T)GetProcAddress(wglInfo->instance, "wglGetCurrentContext");
wglInfo->GetCurrentDC = (WGLGETCURRENTDC_T)GetProcAddress(wglInfo->instance, "wglGetCurrentDC");
wglInfo->GetProcAddress = (WGLGETPROCADDRESS_T)GetProcAddress(wglInfo->instance, "wglGetProcAddress");
wglInfo->MakeCurrent = (WGLMAKECURRENT_T)GetProcAddress(wglInfo->instance, "wglMakeCurrent");
wglInfo->ShareLists = (WGLSHARELISTS_T)GetProcAddress(wglInfo->instance, "wglShareLists");
Expand Down Expand Up @@ -606,18 +619,16 @@ OSGLContext_win::~OSGLContext_win()
bool
OSGLContext_win::makeContextCurrent(const OSGLContext_win* context)
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();

assert(wglInfo);
if (context) {
return wglInfo->MakeCurrent(context->_dc, context->_handle);
return makeDCAndContextCurrent(context->_dc, context->_handle);
} else {
return wglInfo->MakeCurrent(0, 0);
return makeDCAndContextCurrent(0, 0);
}
}

bool
OSGLContext_win::threadHasACurrentContext() {
OSGLContext_win::threadHasACurrentContext()
{
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();
assert(wglInfo);
return wglInfo->GetCurrentContext() != nullptr;
Expand Down Expand Up @@ -722,7 +733,11 @@ namespace {
class ScopedGLContext {
public:
ScopedGLContext(const GLRendererID& gid) {
assert(!OSGLContext_win::threadHasACurrentContext());
const OSGLContext_wgl_data* wglInfo = appPTR->getWGLData();
assert(wglInfo);
_oldDc = wglInfo->GetCurrentDC();
_oldContext = wglInfo->GetCurrentContext();

try {
_context = std::make_unique<OSGLContext_win>(FramebufferConfig(), GLVersion.major, GLVersion.minor, false, gid, nullptr);
} catch (const std::exception& e) {
Expand All @@ -737,14 +752,16 @@ class ScopedGLContext {

~ScopedGLContext() {
if (_context) {
OSGLContext_win::makeContextCurrent(nullptr);
makeDCAndContextCurrent(_oldDc, _oldContext);
}
}

explicit operator bool() const { return (bool)_context; }

private:
std::unique_ptr<OSGLContext_win> _context;
HDC _oldDc = nullptr;
HGLRC _oldContext = nullptr;
};
} // namespace

Expand Down
2 changes: 2 additions & 0 deletions Engine/OSGLContext_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ typedef PROC (WINAPI * WGLGETPROCADDRESS_T)(LPCSTR);
typedef BOOL (WINAPI * WGLMAKECURRENT_T)(HDC, HGLRC);
typedef BOOL (WINAPI * WGLSHARELISTS_T)(HGLRC, HGLRC);
typedef HGLRC (WINAPI * WGLGETCURRENTCONTEXT_T)();
typedef HDC (WINAPI * WGLGETCURRENTDC_T)();

////////// https://www.opengl.org/registry/specs/NV/gpu_affinity.txt

Expand Down Expand Up @@ -157,6 +158,7 @@ struct OSGLContext_wgl_data
WGLCREATECONTEXT_T CreateContext;
WGLDELETECONTEXT_T DeleteContext;
WGLGETCURRENTCONTEXT_T GetCurrentContext;
WGLGETCURRENTDC_T GetCurrentDC;
WGLGETPROCADDRESS_T GetProcAddress;
WGLMAKECURRENT_T MakeCurrent;
WGLSHARELISTS_T ShareLists;
Expand Down

0 comments on commit ba49dce

Please sign in to comment.