GHOST: WGL: Make background rendering works on windows.

When creating an offscreen context we need wglCreatePbufferARB to create
a drawable. In non-background mode wglCreatePbufferARB would have been set
by the main window creation code. But in background mode this is not the
case so we need to create a dummy context using the screen window to init
wglew properly.
This commit is contained in:
Clément Foucault 2018-04-26 11:49:47 +02:00
parent 7fcdccbb6c
commit e0c088f8fb
Notes: blender-bot 2023-02-14 06:46:23 +01:00
Referenced by commit 8c77c36539, Ghost: Fix offline OGL render on windows.
1 changed files with 35 additions and 0 deletions

View File

@ -323,6 +323,33 @@ static HWND clone_window(HWND hWnd, LPVOID lpParam)
return hwndCloned;
}
/* It can happen that glew has not been init yet but we need some wgl functions.
* This create a dummy context on the screen window and init glew to have correct
* functions pointers. */
static GHOST_TSuccess forceInitWGLEW(int iPixelFormat, PIXELFORMATDESCRIPTOR &chosenPFD)
{
HDC dummyHDC = GetDC(NULL);
if (!WIN32_CHK(::SetPixelFormat(dummyHDC, iPixelFormat, &chosenPFD)))
return GHOST_kFailure;
HGLRC dummyHGLRC = ::wglCreateContext(dummyHDC);
if (!WIN32_CHK(dummyHGLRC != NULL))
return GHOST_kFailure;
if (!WIN32_CHK(::wglMakeCurrent(dummyHDC, dummyHGLRC)))
return GHOST_kFailure;
if (GLEW_CHK(glewInit()) != GLEW_OK)
return GHOST_kFailure;
WIN32_CHK(::wglDeleteContext(dummyHGLRC));
WIN32_CHK(ReleaseDC(NULL, dummyHDC));
return GHOST_kSuccess;
}
void GHOST_ContextWGL::initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD)
{
@ -364,6 +391,14 @@ void GHOST_ContextWGL::initContextWGLEW(PIXELFORMATDESCRIPTOR &preferredPFD)
}
else {
int iAttribList[] = {0};
if (wglCreatePbufferARB == NULL) {
/* This should only happen in background mode when rendering with opengl engine. */
if (forceInitWGLEW(iPixelFormat, chosenPFD) != GHOST_kSuccess) {
goto finalize;
}
}
dummyhBuffer = wglCreatePbufferARB(m_hDC, iPixelFormat, 1, 1, iAttribList);
dummyHDC = wglGetPbufferDCARB(dummyhBuffer);
}