From edece8f3dc0223e445cbbc768fb3e0fa2facd8f1 Mon Sep 17 00:00:00 2001 From: Evan Mezeske Date: Mon, 21 Oct 2024 16:35:54 -0700 Subject: [PATCH] Fix a bug in the OpenGL backend that causes win32 errors not to be logged #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 --- .../src/opengl/platforms/PlatformWGL.cpp | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/filament/backend/src/opengl/platforms/PlatformWGL.cpp b/filament/backend/src/opengl/platforms/PlatformWGL.cpp index b8fee3e121..57c3e4668d 100644 --- a/filament/backend/src/opengl/platforms/PlatformWGL.cpp +++ b/filament/backend/src/opengl/platforms/PlatformWGL.cpp @@ -19,8 +19,8 @@ #include #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 @@ -37,9 +37,8 @@ namespace { -void reportLastWindowsError() { +void reportWindowsError(DWORD dwError) { LPSTR lpMessageBuffer = nullptr; - DWORD dwError = GetLastError(); if (dwError == 0) { return; @@ -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), @@ -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; } @@ -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; @@ -136,6 +138,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext, if (mContext) { break; } + dwError = GetLastError(); } if (!mContext) { @@ -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; @@ -162,7 +166,7 @@ error: if (tempContext) { wglDeleteContext(tempContext); } - reportLastWindowsError(); + reportWindowsError(dwError); terminate(); return NULL; } @@ -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) @@ -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); } }