Skip to content

Commit

Permalink
Fix a bug in the OpenGL backend that causes win32 errors not to be lo…
Browse files Browse the repository at this point in the history
…gged #8214 (#8216)

* Capture the last win32 error immediately after failing win32 API functions are called in order to log it correctly. Prior to this change, intervening win32 API calls could clear the error code and it would not be logged.

* Oops fix bad whitespace in previous commit
  • Loading branch information
emezeske authored Oct 21, 2024
1 parent 2456299 commit edece8f
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions filament/backend/src/opengl/platforms/PlatformWGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include <Wingdi.h>

#ifdef _MSC_VER
// this variable is checked in BlueGL.h (included from "gl_headers.h" right after this),
// and prevents duplicate definition of OpenGL apis when building this file.
// this variable is checked in BlueGL.h (included from "gl_headers.h" right after this),
// and prevents duplicate definition of OpenGL apis when building this file.
// However, GL_GLEXT_PROTOTYPES need to be defined in BlueGL.h when included from other files.
#define FILAMENT_PLATFORM_WGL
#endif
Expand All @@ -37,9 +37,8 @@

namespace {

void reportLastWindowsError() {
void reportWindowsError(DWORD dwError) {
LPSTR lpMessageBuffer = nullptr;
DWORD dwError = GetLastError();

if (dwError == 0) {
return;
Expand Down Expand Up @@ -80,6 +79,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
const Platform::DriverConfig& driverConfig) noexcept {
int result = 0;
int pixelFormat = 0;
DWORD dwError = 0;

mPfd = {
sizeof(PIXELFORMATDESCRIPTOR),
Expand All @@ -105,6 +105,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
mHWnd = CreateWindowA("STATIC", "dummy", 0, 0, 0, 1, 1, NULL, NULL, NULL, NULL);
HDC whdc = mWhdc = GetDC(mHWnd);
if (whdc == NULL) {
dwError = GetLastError();
utils::slog.e << "CreateWindowA() failed" << utils::io::endl;
goto error;
}
Expand All @@ -115,6 +116,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
// We need a tmp context to retrieve and call wglCreateContextAttribsARB.
tempContext = wglCreateContext(whdc);
if (!wglMakeCurrent(whdc, tempContext)) {
dwError = GetLastError();
utils::slog.e << "wglMakeCurrent() failed, whdc=" << whdc << ", tempContext=" <<
tempContext << utils::io::endl;
goto error;
Expand All @@ -136,6 +138,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
if (mContext) {
break;
}
dwError = GetLastError();
}

if (!mContext) {
Expand All @@ -148,6 +151,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
tempContext = NULL;

if (!wglMakeCurrent(whdc, mContext)) {
dwError = GetLastError();
utils::slog.e << "wglMakeCurrent() failed, whdc=" << whdc << ", mContext=" <<
mContext << utils::io::endl;
goto error;
Expand All @@ -162,7 +166,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext,
if (tempContext) {
wglDeleteContext(tempContext);
}
reportLastWindowsError();
reportWindowsError(dwError);
terminate();
return NULL;
}
Expand Down Expand Up @@ -205,9 +209,11 @@ Platform::SwapChain* PlatformWGL::createSwapChain(void* nativeWindow, uint64_t f
// on Windows, the nativeWindow maps to a HWND
swapChain->hWnd = (HWND) nativeWindow;
swapChain->hDc = GetDC(swapChain->hWnd);
if (!ASSERT_POSTCONDITION_NON_FATAL(swapChain->hDc,
"Unable to create the SwapChain (nativeWindow = %p)", nativeWindow)) {
reportLastWindowsError();
if (!swapChain->hDc) {
DWORD dwError = GetLastError();
ASSERT_POSTCONDITION_NON_FATAL(swapChain->hDc,
"Unable to create the SwapChain (nativeWindow = %p)", nativeWindow);
reportWindowsError(dwError);
}

// We have to match pixel formats across the HDC and HGLRC (mContext)
Expand Down Expand Up @@ -264,8 +270,10 @@ bool PlatformWGL::makeCurrent(ContextType type, SwapChain* drawSwapChain,
HDC hdc = wglSwapChain->hDc;
if (hdc != NULL) {
BOOL success = wglMakeCurrent(hdc, mContext);
if (!ASSERT_POSTCONDITION_NON_FATAL(success, "wglMakeCurrent() failed. hdc = %p", hdc)) {
reportLastWindowsError();
if (!success) {
DWORD dwError = GetLastError();
ASSERT_POSTCONDITION_NON_FATAL(success, "wglMakeCurrent() failed. hdc = %p", hdc);
reportWindowsError(dwError);
wglMakeCurrent(0, NULL);
}
}
Expand Down

0 comments on commit edece8f

Please sign in to comment.