bgfx/src/glcontext_wgl.cpp

377 lines
10 KiB
C++
Raw Normal View History

2013-01-13 21:39:25 -05:00
/*
2014-02-11 01:07:04 -05:00
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
2013-01-13 21:39:25 -05:00
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#include "bgfx_p.h"
#if (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)
2013-01-13 21:39:25 -05:00
# include "renderer_gl.h"
# if BGFX_USE_WGL
namespace bgfx
{
PFNWGLGETPROCADDRESSPROC wglGetProcAddress;
PFNWGLMAKECURRENTPROC wglMakeCurrent;
PFNWGLCREATECONTEXTPROC wglCreateContext;
PFNWGLDELETECONTEXTPROC wglDeleteContext;
2013-02-17 21:04:40 -05:00
PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
2013-01-13 21:39:25 -05:00
2013-12-08 01:01:32 -05:00
# define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
# include "glimports.h"
2013-01-13 21:39:25 -05:00
2014-09-07 20:17:38 -04:00
struct SwapChainGL
{
SwapChainGL(void* _nwh)
: m_hwnd( (HWND)_nwh)
{
m_hdc = GetDC(m_hwnd);
}
~SwapChainGL()
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_context);
ReleaseDC(m_hwnd, m_hdc);
}
void makeCurrent()
{
wglMakeCurrent(m_hdc, m_context);
2014-10-29 01:08:55 -04:00
GLenum err = glGetError();
2014-10-31 00:06:52 -04:00
BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err);
2014-09-07 20:17:38 -04:00
}
void swapBuffers()
{
SwapBuffers(m_hdc);
}
HWND m_hwnd;
HDC m_hdc;
HGLRC m_context;
};
static HGLRC createContext(HDC _hdc)
2013-01-13 21:39:25 -05:00
{
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd) );
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
2013-01-13 21:39:25 -05:00
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cAlphaBits = 8;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int pixelFormat = ChoosePixelFormat(_hdc, &pfd);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(0 != pixelFormat, Fatal::UnableToInitialize, "ChoosePixelFormat failed!");
DescribePixelFormat(_hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
2013-01-13 21:39:25 -05:00
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
);
int result;
result = SetPixelFormat(_hdc, pixelFormat, &pfd);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "SetPixelFormat failed!");
HGLRC context = wglCreateContext(_hdc);
BGFX_FATAL(NULL != context, Fatal::UnableToInitialize, "wglCreateContext failed!");
2013-01-13 21:39:25 -05:00
result = wglMakeCurrent(_hdc, context);
2013-01-13 21:39:25 -05:00
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
return context;
}
2013-03-26 00:13:54 -04:00
void GlContext::create(uint32_t /*_width*/, uint32_t /*_height*/)
{
2013-07-27 01:55:13 -04:00
m_opengl32dll = bx::dlopen("opengl32.dll");
BGFX_FATAL(NULL != m_opengl32dll, Fatal::UnableToInitialize, "Failed to load opengl32.dll.");
2013-07-27 01:55:13 -04:00
wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress");
BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress.");
// If g_bgfxHwnd is NULL, the assumption is that GL context was created
// by user (for example, using SDL, GLFW, etc.)
BX_WARN(NULL != g_bgfxHwnd
, "bgfx::winSetHwnd with valid window is not called. This might "
"be intentional when GL context is created by the user."
);
if (NULL != g_bgfxHwnd)
{
wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext");
BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext.");
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);
BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!");
HGLRC context = createContext(hdc);
2013-02-17 21:04:40 -05:00
2014-09-07 20:17:38 -04:00
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
2014-09-07 20:17:38 -04:00
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
2013-02-17 21:04:40 -05:00
if (NULL != wglGetExtensionsStringARB)
2013-02-17 21:04:40 -05:00
{
const char* extensions = (const char*)wglGetExtensionsStringARB(hdc);
BX_TRACE("WGL extensions:");
dumpExtensions(extensions);
}
2013-02-17 21:04:40 -05:00
if (NULL != wglChoosePixelFormatARB
&& NULL != wglCreateContextAttribsARB)
2013-02-17 21:04:40 -05:00
{
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;
uint32_t numFormats = 0;
do
{
2014-09-07 20:17:38 -04:00
result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &m_pixelFormat, &numFormats);
if (0 == result
|| 0 == numFormats)
{
attrs[3] >>= 1;
attrs[1] = attrs[3] == 0 ? 0 : 1;
}
} while (0 == numFormats);
2014-09-07 20:17:38 -04:00
DescribePixelFormat(m_hdc, m_pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &m_pfd);
BX_TRACE("Pixel format:\n"
"\tiPixelType %d\n"
"\tcColorBits %d\n"
"\tcAlphaBits %d\n"
"\tcDepthBits %d\n"
"\tcStencilBits %d\n"
2014-09-07 20:17:38 -04:00
, m_pfd.iPixelType
, m_pfd.cColorBits
, m_pfd.cAlphaBits
, m_pfd.cDepthBits
, m_pfd.cStencilBits
);
2014-09-07 20:17:38 -04:00
result = SetPixelFormat(m_hdc, m_pixelFormat, &m_pfd);
// When window is created by SDL and SDL_WINDOW_OPENGL is set SetPixelFormat
// will fail. Just warn and continue. In case it failed for some other reason
// create context will fail and it will error out there.
BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() );
2014-11-04 23:01:42 -05:00
int32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0;
BX_UNUSED(flags);
int32_t contextAttrs[9] =
{
2013-03-07 00:33:35 -05:00
#if BGFX_CONFIG_RENDERER_OPENGL >= 31
2014-08-26 23:56:53 -04:00
WGL_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10,
WGL_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10,
WGL_CONTEXT_FLAGS_ARB, flags,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
2013-03-04 01:30:50 -05:00
#else
WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
0, 0,
0, 0,
2013-03-07 00:33:35 -05:00
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
0
};
2013-02-17 21:04:40 -05:00
2013-03-06 00:37:31 -05:00
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() );
2014-09-07 20:17:38 -04:00
BX_STATIC_ASSERT(sizeof(contextAttrs) == sizeof(m_contextAttrs) );
memcpy(m_contextAttrs, contextAttrs, sizeof(contextAttrs) );
2013-03-06 00:37:31 -05:00
}
2013-02-17 21:04:40 -05:00
wglMakeCurrent(NULL, NULL);
wglDeleteContext(context);
DestroyWindow(hwnd);
if (NULL == m_context)
{
m_context = createContext(m_hdc);
}
2013-02-17 21:04:40 -05:00
int result = wglMakeCurrent(m_hdc, m_context);
BGFX_FATAL(0 != result, Fatal::UnableToInitialize, "wglMakeCurrent failed!");
if (NULL != wglSwapIntervalEXT)
{
wglSwapIntervalEXT(0);
}
}
2013-01-13 21:39:25 -05:00
import();
}
void GlContext::destroy()
{
if (NULL != g_bgfxHwnd)
{
wglMakeCurrent(NULL, NULL);
2013-01-13 21:39:25 -05:00
wglDeleteContext(m_context);
m_context = NULL;
2013-01-13 21:39:25 -05:00
ReleaseDC(g_bgfxHwnd, m_hdc);
m_hdc = NULL;
}
2013-01-13 21:39:25 -05:00
2013-07-27 01:55:13 -04:00
bx::dlclose(m_opengl32dll);
2013-01-13 21:39:25 -05:00
m_opengl32dll = NULL;
}
2013-04-27 18:16:05 -04:00
void GlContext::resize(uint32_t /*_width*/, uint32_t /*_height*/, bool _vsync)
2013-01-13 21:39:25 -05:00
{
2013-04-27 18:16:05 -04:00
if (NULL != wglSwapIntervalEXT)
{
wglSwapIntervalEXT(_vsync ? 1 : 0);
}
2013-01-13 21:39:25 -05:00
}
2014-09-23 23:35:39 -04:00
bool GlContext::isSwapChainSupported()
{
return true;
}
2014-09-07 20:17:38 -04:00
SwapChainGL* GlContext::createSwapChain(void* _nwh)
2013-01-13 21:39:25 -05:00
{
2014-09-07 20:17:38 -04:00
SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(_nwh);
int result = SetPixelFormat(swapChain->m_hdc, m_pixelFormat, &m_pfd);
2014-09-08 02:21:17 -04:00
BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() ); BX_UNUSED(result);
2014-09-07 20:17:38 -04:00
swapChain->m_context = wglCreateContextAttribsARB(swapChain->m_hdc, m_context, m_contextAttrs);
BX_CHECK(NULL != swapChain->m_context, "Create swap chain failed: %x", glGetError() );
return swapChain;
}
2014-11-30 12:06:47 -05:00
void GlContext::destroySwapChain(SwapChainGL* _swapChain)
2014-09-07 20:17:38 -04:00
{
BX_DELETE(g_allocator, _swapChain);
}
void GlContext::makeCurrent(SwapChainGL* _swapChain)
{
if (NULL == _swapChain)
{
wglMakeCurrent(m_hdc, m_context);
2014-10-29 01:08:55 -04:00
GLenum err = glGetError();
2014-10-31 00:06:52 -04:00
BX_WARN(0 == err, "wglMakeCurrent failed with GL error: 0x%04x.", err); BX_UNUSED(err);
2014-09-07 20:17:38 -04:00
}
else
{
_swapChain->makeCurrent();
}
}
void GlContext::swap(SwapChainGL* _swapChain)
{
if (NULL == _swapChain)
{
if (NULL != g_bgfxHwnd)
{
wglMakeCurrent(m_hdc, m_context);
SwapBuffers(m_hdc);
}
}
else
{
_swapChain->makeCurrent();
_swapChain->swapBuffers();
}
2013-01-13 21:39:25 -05:00
}
void GlContext::import()
{
BX_TRACE("Import:");
# define GL_EXTENSION(_optional, _proto, _func, _import) \
{ \
if (NULL == _func) \
{ \
_func = (_proto)wglGetProcAddress(#_import); \
if (_func == NULL) \
{ \
_func = (_proto)bx::dlsym(m_opengl32dll, #_import); \
BX_TRACE(" %p " #_func " (" #_import ")", _func); \
} \
else \
{ \
BX_TRACE("wgl %p " #_func " (" #_import ")", _func); \
} \
2014-06-08 23:57:39 -04:00
BGFX_FATAL(BX_IGNORE_C4127(_optional) || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. wglGetProcAddress(\"%s\")", #_import); \
} \
}
2013-01-13 21:39:25 -05:00
# include "glimports.h"
}
} // namespace bgfx
# endif // BGFX_USE_WGL
#endif // (BGFX_CONFIG_RENDERER_OPENGLES|BGFX_CONFIG_RENDERER_OPENGL)