diff --git a/filament/src/driver/opengl/ContextManagerWGL.cpp b/filament/src/driver/opengl/ContextManagerWGL.cpp index 8a1ab51158..4e96240f88 100644 --- a/filament/src/driver/opengl/ContextManagerWGL.cpp +++ b/filament/src/driver/opengl/ContextManagerWGL.cpp @@ -77,7 +77,7 @@ std::unique_ptr ContextManagerWGL::createDriver(void* const sharedGLCont } PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = - (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); + (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB"); mContext = wglCreateContextAttribs(whdc, nullptr, attribs); if (!mContext) { utils::slog.e << "wglCreateContextAttribs() failed, whdc=" << whdc << utils::io::endl; diff --git a/libs/bluegl/tests/OpenGLSupport.cpp b/libs/bluegl/tests/OpenGLSupport.cpp index ef5d46ade6..c25be7e5df 100644 --- a/libs/bluegl/tests/OpenGLSupport.cpp +++ b/libs/bluegl/tests/OpenGLSupport.cpp @@ -110,19 +110,20 @@ OpenGLContext createOpenGLContext() { // The attributes don't really matter here but we choose a configuration // that would make sense if we were to perform actual rendering CGLPixelFormatAttribute attributes[] = { - kCGLPFAColorSize, (CGLPixelFormatAttribute) 24, - kCGLPFAAlphaSize, (CGLPixelFormatAttribute) 8, - kCGLPFAAccelerated, (CGLPixelFormatAttribute) 0 + kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_GL4_Core, + kCGLPFAColorSize, (CGLPixelFormatAttribute) 24, + kCGLPFAAlphaSize, (CGLPixelFormatAttribute) 8, + kCGLPFAAccelerated, (CGLPixelFormatAttribute) 0 }; - GLint virtual_screen_count; - CGLPixelFormatObj pixel_format; - (*g_cgl.choosePixelFormat)(attributes, &pixel_format, &virtual_screen_count); + GLint pixelFormatCount; + CGLPixelFormatObj pixelFormat; + (*g_cgl.choosePixelFormat)(attributes, &pixelFormat, &pixelFormatCount); OpenGLContext context; - (*g_cgl.createContext)(pixel_format, nullptr, (CGLContextObj*) &context); + (*g_cgl.createContext)(pixelFormat, nullptr, (CGLContextObj*) &context); - (*g_cgl.destroyPixelFormat)(pixel_format); + (*g_cgl.destroyPixelFormat)(pixelFormat); return context; } @@ -144,10 +145,14 @@ void destroyOpenGLContext(OpenGLContext context) { #include "GL/wglext.h" struct wglLocalContext { - wglLocalContext(HWND ohwnd, HGLRC ocontext) : hwnd(ohwnd), context(ocontext){}; + wglLocalContext(HWND ohwnd, HDC owhdc, HGLRC ocontext) : + hwnd(ohwnd), whdc(owhdc), context(ocontext) { + }; HWND hwnd; + HDC whdc; HGLRC context; }; + bool loadLibraries() { return true; } @@ -157,7 +162,6 @@ bool unloadLibraries() { } OpenGLContext createOpenGLContext() { - if (!loadLibraries()) return nullptr; PIXELFORMATDESCRIPTOR pfd = { @@ -179,43 +183,45 @@ OpenGLContext createOpenGLContext() { 0, 0, 0 }; - HWND hwnd= CreateWindowA("STATIC", "dummy", 0, 0, 0, 100, 100, NULL, NULL, NULL, NULL); - HDC hdc = GetDC(hwnd); - - int pixelFormat = ChoosePixelFormat(hdc, &pfd); - SetPixelFormat(hdc, pixelFormat, &pfd); - int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4, WGL_CONTEXT_MINOR_VERSION_ARB, 1, - WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_PROFILE_MASK_ARB , + WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_PROFILE_MASK_ARB, 0 }; + HWND hwnd = CreateWindowA("STATIC", "dummy", 0, 0, 0, 1, 1, NULL, NULL, NULL, NULL); + HDC whdc = mWhdc = GetDC(hwnd); + + int pixelFormat = ChoosePixelFormat(whdc, &pfd); + SetPixelFormat(whdc, pixelFormat, &pfd); + // We need a tmp context to retrieve and call wglCreateContextAttribsARB. - HGLRC tempContext = wglCreateContext(hdc); - wglMakeCurrent(hdc, tempContext); + HGLRC tempContext = wglCreateContext(whdc); + wglMakeCurrent(whdc, tempContext); PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = - (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); + (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB"); HGLRC context = wglCreateContextAttribs(hdc, nullptr, attribs); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempContext); wglMakeCurrent(hdc, context); - return new wglLocalContext(hwnd, context); + return new wglLocalContext(hwnd, whdc, context); } void setCurrentOpenGLContext(OpenGLContext context) { wglLocalContext* wContext = static_cast(context); HDC hdc = GetDC(wContext->hwnd); - wglMakeCurrent (hdc, wContext->context); + wglMakeCurrent(hdc, wContext->context); } void destroyOpenGLContext(OpenGLContext context) { wglLocalContext* wContext = static_cast(context); + wglMakeCurrent(NULL, NULL); wglDeleteContext(wContext->context); + ReleaseDC(wContext->hwnd, wContext->whdc); DestroyWindow(wContext->hwnd); delete static_cast(context); unloadLibraries(); diff --git a/libs/bluegl/tests/test_bluegl.cpp b/libs/bluegl/tests/test_bluegl.cpp index afa3235975..2de80f4bb6 100644 --- a/libs/bluegl/tests/test_bluegl.cpp +++ b/libs/bluegl/tests/test_bluegl.cpp @@ -20,29 +20,6 @@ #include "OpenGLSupport.hpp" -const char * GetGLErrorStr(GLenum err) -{ - switch (err) - { - case GL_NO_ERROR: return "No error"; - case GL_INVALID_ENUM: return "Invalid enum"; - case GL_INVALID_VALUE: return "Invalid value"; - case GL_INVALID_OPERATION: return "Invalid operation"; - case GL_STACK_OVERFLOW: return "Stack overflow"; - case GL_STACK_UNDERFLOW: return "Stack underflow"; - case GL_OUT_OF_MEMORY: return "Out of memory"; - default: return "Unknown error"; - } -} - -void checkError() { - const GLenum err = glGetError(); - if (GL_NO_ERROR == err) - return; - - std::cout << "GL Error: " << GetGLErrorStr(err) << std::endl; -} - namespace bluegl { TEST(BlueGLTest, Initialization) { @@ -53,6 +30,7 @@ TEST(BlueGLTest, Initialization) { TEST(BlueGLTest, GetVersion) { EXPECT_EQ(0, bind()); gl::OpenGLContext context = gl::createOpenGLContext(); + gl::setCurrentOpenGLContext(context); GLint major = 0, minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); @@ -98,16 +76,18 @@ TEST(BlueGLTest, CallOpenGLDoubleInit) { unbind(); } -TEST(BlueGLTest, CallBindAndGetBinded) { +TEST(BlueGLTest, CallBindAndGetBound) { + EXPECT_EQ(0, bind()); gl::OpenGLContext context = gl::createOpenGLContext(); - bind(); - GLuint texId; + gl::setCurrentOpenGLContext(context); + + GLuint texId = 0; glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); - GLint bindedTexId; - glGetIntegerv(GL_TEXTURE_BINDING_2D, &bindedTexId); - EXPECT_TRUE(texId == bindedTexId); + GLint boundTexId = -1; // different from texId, just to be sure + glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTexId); + ASSERT_EQ(texId, boundTexId); gl::destroyOpenGLContext(context); unbind();