mirror of
https://github.com/scratchfoundation/bgfx.git
synced 2024-11-28 18:45:54 -05:00
WGL: Skip context initialization when window is not set.
This commit is contained in:
parent
d265b9b0d4
commit
e047232292
1 changed files with 147 additions and 131 deletions
|
@ -78,147 +78,157 @@ namespace bgfx
|
||||||
wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
|
wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
|
||||||
BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
|
BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
|
||||||
|
|
||||||
wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
|
// If g_bgfxHwnd is NULL, the assumption is that GL context was created
|
||||||
BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
|
// by user (for example, using SDL, GLFW, etc.)
|
||||||
|
BX_WARN(NULL != g_bgfxHwnd
|
||||||
wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext");
|
, "bgfx::winSetHwnd with valid window is not called. This might "
|
||||||
BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
|
"be intentional when GL context is created by the user."
|
||||||
|
|
||||||
wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext");
|
|
||||||
BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
|
|
||||||
|
|
||||||
m_hdc = GetDC(g_bgfxHwnd);
|
|
||||||
BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!");
|
|
||||||
|
|
||||||
// Dummy window to peek into WGL functionality.
|
|
||||||
//
|
|
||||||
// An application can only set the pixel format of a window one time.
|
|
||||||
// Once a window's pixel format is set, it cannot be changed.
|
|
||||||
// MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx
|
|
||||||
HWND hwnd = CreateWindowA("STATIC"
|
|
||||||
, ""
|
|
||||||
, WS_POPUP|WS_DISABLED
|
|
||||||
, -32000
|
|
||||||
, -32000
|
|
||||||
, 0
|
|
||||||
, 0
|
|
||||||
, NULL
|
|
||||||
, NULL
|
|
||||||
, GetModuleHandle(NULL)
|
|
||||||
, 0
|
|
||||||
);
|
);
|
||||||
|
|
||||||
HDC hdc = GetDC(hwnd);
|
if (NULL != g_bgfxHwnd)
|
||||||
BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!");
|
|
||||||
|
|
||||||
HGLRC context = createContext(hdc);
|
|
||||||
|
|
||||||
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
|
|
||||||
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
|
|
||||||
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
|
||||||
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
|
|
||||||
|
|
||||||
if (NULL != wglGetExtensionsStringARB)
|
|
||||||
{
|
{
|
||||||
const char* extensions = (const char*)wglGetExtensionsStringARB(hdc);
|
wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
|
||||||
BX_TRACE("WGL extensions:");
|
BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
|
||||||
dumpExtensions(extensions);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (NULL != wglChoosePixelFormatARB
|
wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext");
|
||||||
&& NULL != wglCreateContextAttribsARB)
|
BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
|
||||||
{
|
|
||||||
int32_t attrs[] =
|
|
||||||
{
|
|
||||||
WGL_SAMPLE_BUFFERS_ARB, 0,
|
|
||||||
WGL_SAMPLES_ARB, 0,
|
|
||||||
WGL_SUPPORT_OPENGL_ARB, true,
|
|
||||||
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
|
|
||||||
WGL_DRAW_TO_WINDOW_ARB, true,
|
|
||||||
WGL_DOUBLE_BUFFER_ARB, true,
|
|
||||||
WGL_COLOR_BITS_ARB, 32,
|
|
||||||
WGL_DEPTH_BITS_ARB, 24,
|
|
||||||
WGL_STENCIL_BITS_ARB, 8,
|
|
||||||
0
|
|
||||||
};
|
|
||||||
|
|
||||||
int result;
|
wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext");
|
||||||
int pixelFormat;
|
BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
|
||||||
uint32_t numFormats = 0;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &pixelFormat, &numFormats);
|
|
||||||
if (0 == result
|
|
||||||
|| 0 == numFormats)
|
|
||||||
{
|
|
||||||
attrs[3] >>= 1;
|
|
||||||
attrs[1] = attrs[3] == 0 ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (0 == numFormats);
|
m_hdc = GetDC(g_bgfxHwnd);
|
||||||
|
BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!");
|
||||||
|
|
||||||
PIXELFORMATDESCRIPTOR pfd;
|
// Dummy window to peek into WGL functionality.
|
||||||
DescribePixelFormat(m_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
//
|
||||||
|
// An application can only set the pixel format of a window one time.
|
||||||
BX_TRACE("Pixel format:\n"
|
// Once a window's pixel format is set, it cannot be changed.
|
||||||
"\tiPixelType %d\n"
|
// MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx
|
||||||
"\tcColorBits %d\n"
|
HWND hwnd = CreateWindowA("STATIC"
|
||||||
"\tcAlphaBits %d\n"
|
, ""
|
||||||
"\tcDepthBits %d\n"
|
, WS_POPUP|WS_DISABLED
|
||||||
"\tcStencilBits %d\n"
|
, -32000
|
||||||
, pfd.iPixelType
|
, -32000
|
||||||
, pfd.cColorBits
|
, 0
|
||||||
, pfd.cAlphaBits
|
, 0
|
||||||
, pfd.cDepthBits
|
, NULL
|
||||||
, pfd.cStencilBits
|
, NULL
|
||||||
|
, GetModuleHandle(NULL)
|
||||||
|
, 0
|
||||||
);
|
);
|
||||||
|
|
||||||
result = SetPixelFormat(m_hdc, pixelFormat, &pfd);
|
HDC hdc = GetDC(hwnd);
|
||||||
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
|
BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!");
|
||||||
|
|
||||||
uint32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
|
HGLRC context = createContext(hdc);
|
||||||
BX_UNUSED(flags);
|
|
||||||
int32_t contextAttrs[9] =
|
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
|
||||||
|
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
|
||||||
|
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
||||||
|
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
|
||||||
|
|
||||||
|
if (NULL != wglGetExtensionsStringARB)
|
||||||
{
|
{
|
||||||
#if BGFX_CONFIG_RENDERER_OPENGL >= 31
|
const char* extensions = (const char*)wglGetExtensionsStringARB(hdc);
|
||||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
BX_TRACE("WGL extensions:");
|
||||||
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
dumpExtensions(extensions);
|
||||||
WGL_CONTEXT_FLAGS_ARB, flags,
|
}
|
||||||
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
|
||||||
#else
|
if (NULL != wglChoosePixelFormatARB
|
||||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
|
&& NULL != wglCreateContextAttribsARB)
|
||||||
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
{
|
||||||
0, 0,
|
int32_t attrs[] =
|
||||||
0, 0,
|
{
|
||||||
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
|
WGL_SAMPLE_BUFFERS_ARB, 0,
|
||||||
0
|
WGL_SAMPLES_ARB, 0,
|
||||||
};
|
WGL_SUPPORT_OPENGL_ARB, true,
|
||||||
|
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
|
||||||
|
WGL_DRAW_TO_WINDOW_ARB, true,
|
||||||
|
WGL_DOUBLE_BUFFER_ARB, true,
|
||||||
|
WGL_COLOR_BITS_ARB, 32,
|
||||||
|
WGL_DEPTH_BITS_ARB, 24,
|
||||||
|
WGL_STENCIL_BITS_ARB, 8,
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
int result;
|
||||||
|
int pixelFormat;
|
||||||
|
uint32_t numFormats = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &pixelFormat, &numFormats);
|
||||||
|
if (0 == result
|
||||||
|
|| 0 == numFormats)
|
||||||
|
{
|
||||||
|
attrs[3] >>= 1;
|
||||||
|
attrs[1] = attrs[3] == 0 ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (0 == numFormats);
|
||||||
|
|
||||||
|
PIXELFORMATDESCRIPTOR pfd;
|
||||||
|
DescribePixelFormat(m_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
|
||||||
|
|
||||||
|
BX_TRACE("Pixel format:\n"
|
||||||
|
"\tiPixelType %d\n"
|
||||||
|
"\tcColorBits %d\n"
|
||||||
|
"\tcAlphaBits %d\n"
|
||||||
|
"\tcDepthBits %d\n"
|
||||||
|
"\tcStencilBits %d\n"
|
||||||
|
, pfd.iPixelType
|
||||||
|
, pfd.cColorBits
|
||||||
|
, pfd.cAlphaBits
|
||||||
|
, pfd.cDepthBits
|
||||||
|
, pfd.cStencilBits
|
||||||
|
);
|
||||||
|
|
||||||
|
result = SetPixelFormat(m_hdc, pixelFormat, &pfd);
|
||||||
|
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
|
||||||
|
|
||||||
|
uint32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
|
||||||
|
BX_UNUSED(flags);
|
||||||
|
int32_t contextAttrs[9] =
|
||||||
|
{
|
||||||
|
#if BGFX_CONFIG_RENDERER_OPENGL >= 31
|
||||||
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||||
|
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
||||||
|
WGL_CONTEXT_FLAGS_ARB, flags,
|
||||||
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
||||||
|
#else
|
||||||
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
|
||||||
|
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
|
||||||
|
0, 0,
|
||||||
|
0, 0,
|
||||||
|
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
|
||||||
|
if (NULL == m_context)
|
||||||
|
{
|
||||||
|
// nVidia doesn't like context profile mask for contexts below 3.2?
|
||||||
|
contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6];
|
||||||
|
m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
|
||||||
|
}
|
||||||
|
BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() );
|
||||||
|
}
|
||||||
|
|
||||||
|
wglMakeCurrent(NULL, NULL);
|
||||||
|
wglDeleteContext(context);
|
||||||
|
DestroyWindow(hwnd);
|
||||||
|
|
||||||
m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
|
|
||||||
if (NULL == m_context)
|
if (NULL == m_context)
|
||||||
{
|
{
|
||||||
// nVidia doesn't like context profile mask for contexts below 3.2?
|
m_context = createContext(m_hdc);
|
||||||
contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6];
|
|
||||||
m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs);
|
|
||||||
}
|
}
|
||||||
BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() );
|
|
||||||
}
|
|
||||||
|
|
||||||
wglMakeCurrent(NULL, NULL);
|
int result = wglMakeCurrent(m_hdc, m_context);
|
||||||
wglDeleteContext(context);
|
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
|
||||||
DestroyWindow(hwnd);
|
|
||||||
|
|
||||||
if (NULL == m_context)
|
if (NULL != wglSwapIntervalEXT)
|
||||||
{
|
{
|
||||||
m_context = createContext(m_hdc);
|
wglSwapIntervalEXT(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = wglMakeCurrent(m_hdc, m_context);
|
|
||||||
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
|
|
||||||
|
|
||||||
if (NULL != wglSwapIntervalEXT)
|
|
||||||
{
|
|
||||||
wglSwapIntervalEXT(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
import();
|
import();
|
||||||
|
@ -226,13 +236,16 @@ namespace bgfx
|
||||||
|
|
||||||
void GlContext::destroy()
|
void GlContext::destroy()
|
||||||
{
|
{
|
||||||
wglMakeCurrent(NULL, NULL);
|
if (NULL != g_bgfxHwnd)
|
||||||
|
{
|
||||||
|
wglMakeCurrent(NULL, NULL);
|
||||||
|
|
||||||
wglDeleteContext(m_context);
|
wglDeleteContext(m_context);
|
||||||
m_context = NULL;
|
m_context = NULL;
|
||||||
|
|
||||||
ReleaseDC(g_bgfxHwnd, m_hdc);
|
ReleaseDC(g_bgfxHwnd, m_hdc);
|
||||||
m_hdc = NULL;
|
m_hdc = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
bx::dlclose(m_opengl32dll);
|
bx::dlclose(m_opengl32dll);
|
||||||
m_opengl32dll = NULL;
|
m_opengl32dll = NULL;
|
||||||
|
@ -248,8 +261,11 @@ namespace bgfx
|
||||||
|
|
||||||
void GlContext::swap()
|
void GlContext::swap()
|
||||||
{
|
{
|
||||||
wglMakeCurrent(m_hdc, m_context);
|
if (NULL != g_bgfxHwnd)
|
||||||
SwapBuffers(m_hdc);
|
{
|
||||||
|
wglMakeCurrent(m_hdc, m_context);
|
||||||
|
SwapBuffers(m_hdc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlContext::import()
|
void GlContext::import()
|
||||||
|
|
Loading…
Reference in a new issue