Make opengl initialization thread-safe

svn path=/trunk/; revision=30429
This commit is contained in:
Thomas Bluemel 2007-11-13 23:30:17 +00:00
parent d3e6d6d283
commit 65636af627

View file

@ -46,46 +46,61 @@ static GETPIXELFMT glGetPixelFormat = NULL;
*/ */
HINSTANCE hOpenGL = NULL; HINSTANCE hOpenGL = NULL;
static BOOL OpenGLInitFunction(PCSTR name,
FARPROC *funcptr)
{
PVOID func;
func = (PVOID)GetProcAddress(hOpenGL, name);
if (func != NULL)
{
(void)InterlockedCompareExchangePointer((PVOID*)funcptr,
func,
NULL);
return TRUE;
}
return FALSE;
}
static BOOL OpenGLEnable(void) static BOOL OpenGLEnable(void)
{ {
if(hOpenGL == NULL) HMODULE hModOpengl32;
{ BOOL Ret = TRUE;
hOpenGL = LoadLibraryA("OPENGL32.DLL");
if(hOpenGL == NULL)
return(FALSE);
}
if(glChoosePixelFormat == NULL) { hModOpengl32 = LoadLibraryW(L"OPENGL32.DLL");
glChoosePixelFormat = (CHOOSEPIXELFMT)GetProcAddress(hOpenGL, "wglChoosePixelFormat"); if (hModOpengl32 == NULL)
if(glChoosePixelFormat == NULL) return FALSE;
return(FALSE);
}
if(glSetPixelFormat == NULL) { if (InterlockedCompareExchangePointer((PVOID*)&hOpenGL,
glSetPixelFormat = (SETPIXELFMT)GetProcAddress(hOpenGL, "wglSetPixelFormat"); (PVOID)hModOpengl32,
if(glSetPixelFormat == NULL) NULL) != NULL)
return(FALSE); {
} FreeLibrary(hModOpengl32);
if(glSwapBuffers == NULL) { /* NOTE: Even though another thread was faster loading the
glSwapBuffers = (SWAPBUFFERS)GetProcAddress(hOpenGL, "wglSwapBuffers"); library we can't just bail out here. We really need
if(glSwapBuffers == NULL) to *try* to locate every function. This is slow but
return(FALSE); thread-safe */
} }
if(glDescribePixelFormat == NULL) {
glDescribePixelFormat = (DESCRIBEPIXELFMT)GetProcAddress(hOpenGL, "wglDescribePixelFormat");
if(glDescribePixelFormat == NULL)
return(FALSE);
}
if(glGetPixelFormat == NULL) { if (!OpenGLInitFunction("wglChoosePixelFormat", &glChoosePixelFormat))
glGetPixelFormat = (GETPIXELFMT)GetProcAddress(hOpenGL, "wglGetPixelFormat"); Ret = FALSE;
if(glGetPixelFormat == NULL)
return(FALSE);
}
return(TRUE); /* OpenGL is initialized and enabled*/ if (!OpenGLInitFunction("wglSetPixelFormat", &glSetPixelFormat))
Ret = FALSE;
if (!OpenGLInitFunction("wglSwapBuffers", &glSwapBuffers))
Ret = FALSE;
if (!OpenGLInitFunction("wglDescribePixelFormat", &glDescribePixelFormat))
Ret = FALSE;
if (!OpenGLInitFunction("wglGetPixelFormat", &glGetPixelFormat))
Ret = FALSE;
return Ret;
} }