Context::Context(Context *shared) : ContextSysBase() { assert( Context::current() == shared ); mTarget = nullptr; mGLFormat = shared->mGLFormat; GPU::RenderTargetWin* previousSharedTarget = shared->mTarget; init(); if ( shared->mGLFormat.versionMajor >= 3 ) { initNewContext(shared->mHGLRC); } else { initOldContext(); wglShareLists(shared->mHGLRC, mHGLRC); } shared->doneCurrent(); makeCurrent(); { GLenum wglewInitResult = wglewInit(); GLenum glewInitResult = glewInit(); assert(GLEW_OK == wglewInitResult && GLEW_OK == glewInitResult); } doneCurrent(); shared->makeCurrent(previousSharedTarget); }
GLenum GLEWAPIENTRY glewInit (void) { GLenum r; r = glewContextInit(); if ( r != 0 ) return r; #if defined(_WIN32) return wglewInit(); #elif !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX)) /* _UNIX */ return glxewInit(); #else return r; #endif /* _WIN32 */ }
GLenum GLEWAPIENTRY glewInit (void) { GLenum r; #if defined(GLEW_EGL) PFNEGLGETCURRENTDISPLAYPROC getCurrentDisplay = NULL; #endif r = glewContextInit(); if ( r != 0 ) return r; #if defined(GLEW_EGL) getCurrentDisplay = (PFNEGLGETCURRENTDISPLAYPROC) glewGetProcAddress("eglGetCurrentDisplay"); return eglewInit(getCurrentDisplay()); #elif defined(GLEW_OSMESA) || defined(__ANDROID__) || defined(__native_client__) || defined(__HAIKU__) return r; #elif defined(_WIN32) return wglewInit(); #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) /* _UNIX */ return glxewInit(); #else return r; #endif /* _WIN32 */ }
Context::Context(const Format &fmnt) : ContextSysBase() { mTarget = nullptr; mGLFormat = fmnt; init(); if ( mGLFormat.versionMajor >= 3 ) { // Use a "sppringboard" context used to create an // OpenGL 3.0 or greater context. Format tempFormat; tempFormat.versionMajor = 2; tempFormat.versionMinor = 1; tempFormat.versionProfile = Profile::COMPATIBILITY; Context tempContext(tempFormat); tempContext.makeCurrent(); initNewContext(); tempContext.doneCurrent(); } else { // Simply initialize an old context for our window. initOldContext(); } // GLEW initialization. makeCurrent(); { GLenum wglewInitResult = wglewInit(); GLenum glewInitResult = glewInit(); assert(GLEW_OK == wglewInitResult && GLEW_OK == glewInitResult); } doneCurrent(); }
GLboolean glewCreateContext (struct createParams* params) { WNDCLASS wc; PIXELFORMATDESCRIPTOR pfd; /* register window class */ ZeroMemory(&wc, sizeof(WNDCLASS)); wc.hInstance = GetModuleHandle(NULL); wc.lpfnWndProc = DefWindowProc; wc.lpszClassName = "GLEW"; if (0 == RegisterClass(&wc)) return GL_TRUE; /* create window */ wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); if (NULL == wnd) return GL_TRUE; /* get the device context */ dc = GetDC(wnd); if (NULL == dc) return GL_TRUE; /* find pixel format */ ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR)); if (params->pixelformat == -1) /* find default */ { pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; params->pixelformat = ChoosePixelFormat(dc, &pfd); if (params->pixelformat == 0) return GL_TRUE; } /* set the pixel format for the dc */ if (FALSE == SetPixelFormat(dc, params->pixelformat, &pfd)) return GL_TRUE; /* create rendering context */ rc = wglCreateContext(dc); if (NULL == rc) return GL_TRUE; if (FALSE == wglMakeCurrent(dc, rc)) return GL_TRUE; if (params->major || params->profile || params->flags) { HGLRC oldRC = rc; int contextAttrs[20]; int i; wglewInit(); /* Intel HD 3000 has WGL_ARB_create_context, but not WGL_ARB_create_context_profile */ if (!wglewGetExtension("WGL_ARB_create_context")) return GL_TRUE; i = 0; if (params->major) { contextAttrs[i++] = WGL_CONTEXT_MAJOR_VERSION_ARB; contextAttrs[i++] = params->major; contextAttrs[i++] = WGL_CONTEXT_MINOR_VERSION_ARB; contextAttrs[i++] = params->minor; } if (params->profile) { contextAttrs[i++] = WGL_CONTEXT_PROFILE_MASK_ARB; contextAttrs[i++] = params->profile; } if (params->flags) { contextAttrs[i++] = WGL_CONTEXT_FLAGS_ARB; contextAttrs[i++] = params->flags; } contextAttrs[i++] = 0; rc = wglCreateContextAttribsARB(dc, 0, contextAttrs); if (NULL == rc) return GL_TRUE; if (!wglMakeCurrent(dc, rc)) return GL_TRUE; wglDeleteContext(oldRC); } return GL_FALSE; }
bool Pipe::_configInitWGLEW() { //----- Create and make current a temporary GL context to initialize WGLEW // window class std::ostringstream className; className << "TMP" << (void*)this; const std::string& classStr = className.str(); HINSTANCE instance = GetModuleHandle( 0 ); WNDCLASS wc = { 0 }; wc.lpfnWndProc = DefWindowProc; wc.hInstance = instance; wc.hIcon = LoadIcon( 0, IDI_WINLOGO ); wc.hCursor = LoadCursor( 0, IDC_ARROW ); wc.lpszClassName = classStr.c_str(); if( !RegisterClass( &wc )) { setError( ERROR_WGLPIPE_REGISTERCLASS_FAILED ); EQWARN << getError() << ": " << co::base::sysError << std::endl; return false; } // window DWORD windowStyleEx = WS_EX_APPWINDOW; DWORD windowStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW; HWND hWnd = CreateWindowEx( windowStyleEx, wc.lpszClassName, "TMP", windowStyle, 0, 0, 1, 1, 0, 0, // parent, menu instance, 0 ); if( !hWnd ) { setError( ERROR_SYSTEMPIPE_CREATEWINDOW_FAILED ); EQWARN << getError() << ": " << co::base::sysError << std::endl; UnregisterClass( classStr.c_str(), instance ); return false; } HDC dc = GetDC( hWnd ); PIXELFORMATDESCRIPTOR pfd = {0}; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; int pf = ChoosePixelFormat( dc, &pfd ); if( pf == 0 ) { setError( ERROR_SYSTEMPIPE_PIXELFORMAT_NOTFOUND ); EQWARN << getError() << ": " << co::base::sysError << std::endl; DestroyWindow( hWnd ); UnregisterClass( classStr.c_str(), instance ); return false; } if( !SetPixelFormat( dc, pf, &pfd )) { setError( ERROR_WGLPIPE_SETPF_FAILED ); EQWARN << getError() << ": " << co::base::sysError << std::endl; ReleaseDC( hWnd, dc ); DestroyWindow( hWnd ); UnregisterClass( classStr.c_str(), instance ); return false; } // context HGLRC context = wglCreateContext( dc ); if( !context ) { setError( ERROR_SYSTEMPIPE_CREATECONTEXT_FAILED ); EQWARN << getError() << ": " << co::base::sysError << std::endl; ReleaseDC( hWnd, dc ); DestroyWindow( hWnd ); UnregisterClass( classStr.c_str(), instance ); return false; } HDC oldDC = wglGetCurrentDC(); HGLRC oldContext = wglGetCurrentContext(); wglMakeCurrent( dc, context ); const GLenum result = wglewInit(); bool success = result == GLEW_OK; if( success ) { EQINFO << "Pipe WGLEW initialization successful" << std::endl; success = configInitGL(); } else { setError( ERROR_WGLPIPE_WGLEWINIT_FAILED ); EQWARN << getError() << ": " << result << std::endl; } wglDeleteContext( context ); ReleaseDC( hWnd, dc ); DestroyWindow( hWnd ); UnregisterClass( classStr.c_str(), instance ); wglMakeCurrent( oldDC, oldContext ); return success; }
void GHOST_ContextWGL::initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD) { HWND dummyHWND = NULL; HDC dummyHDC = NULL; HGLRC dummyHGLRC = NULL; HDC prevHDC; HGLRC prevHGLRC; int iPixelFormat; #ifdef WITH_GLEW_MX wglewContext = new WGLEWContext; memset(wglewContext, 0, sizeof(WGLEWContext)); delete m_wglewContext; m_wglewContext = wglewContext; #endif SetLastError(NO_ERROR); prevHDC = ::wglGetCurrentDC(); WIN32_CHK(GetLastError() == NO_ERROR); prevHGLRC = ::wglGetCurrentContext(); WIN32_CHK(GetLastError() == NO_ERROR); dummyHWND = clone_window(m_hWnd, NULL); if (dummyHWND == NULL) goto finalize; dummyHDC = GetDC(dummyHWND); if (!WIN32_CHK(dummyHDC != NULL)) goto finalize; iPixelFormat = choose_pixel_format_legacy(dummyHDC, preferredPFD); if (iPixelFormat == 0) goto finalize; PIXELFORMATDESCRIPTOR chosenPFD; if (!WIN32_CHK(::DescribePixelFormat(dummyHDC, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &chosenPFD))) goto finalize; if (!WIN32_CHK(::SetPixelFormat(dummyHDC, iPixelFormat, &chosenPFD))) goto finalize; dummyHGLRC = ::wglCreateContext(dummyHDC); if (!WIN32_CHK(dummyHGLRC != NULL)) goto finalize; if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC))) goto finalize; #ifdef WITH_GLEW_MX if (GLEW_CHK(wglewInit()) != GLEW_OK) fprintf(stderr, "Warning! WGLEW failed to initialize properly.\n"); #else if (GLEW_CHK(glewInit()) != GLEW_OK) fprintf(stderr, "Warning! Dummy GLEW/WGLEW failed to initialize properly.\n"); #endif // the following are not technially WGLEW, but they also require a context to work #ifndef NDEBUG delete m_dummyRenderer; delete m_dummyVendor; delete m_dummyVersion; m_dummyRenderer = _strdup(reinterpret_cast<const char *>(glGetString(GL_RENDERER))); m_dummyVendor = _strdup(reinterpret_cast<const char *>(glGetString(GL_VENDOR))); m_dummyVersion = _strdup(reinterpret_cast<const char *>(glGetString(GL_VERSION))); #endif s_singleContextMode = is_crappy_intel_card(); finalize: WIN32_CHK(::wglMakeCurrent(prevHDC, prevHGLRC)); if (dummyHGLRC != NULL) WIN32_CHK(::wglDeleteContext(dummyHGLRC)); if (dummyHWND != NULL) { if (dummyHDC != NULL) WIN32_CHK(::ReleaseDC(dummyHWND, dummyHDC)); WIN32_CHK(::DestroyWindow(dummyHWND)); } }