Cleanup passing platform window/context data.

This commit is contained in:
Branimir Karadžić 2015-04-20 16:22:40 -07:00
parent 7904b9d9dd
commit a528554e67
13 changed files with 425 additions and 459 deletions

View file

@ -31,55 +31,15 @@ typedef enum bgfx_render_frame
*/ */
BGFX_C_API bgfx_render_frame_t bgfx_render_frame(); BGFX_C_API bgfx_render_frame_t bgfx_render_frame();
#if BX_PLATFORM_ANDROID typedef struct bgfx_platform_data
# include <android/native_window.h> {
void* ndt;
void* nwh;
void* context;
void* backbuffer;
/** } bgfx_platform_data_t;
*
*/
BGFX_C_API void bgfx_android_set_window(ANativeWindow* _window);
#elif BX_PLATFORM_IOS BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd);
/**
*
*/
BGFX_C_API void bgfx_ios_set_eagl_layer(void* _layer);
#elif BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI
# include <X11/Xlib.h>
/**
*
*/
BGFX_C_API void bgfx_x11_set_display_window(Display* _display, Window _window);
#elif BX_PLATFORM_NACL
# include <ppapi/c/ppb_graphics_3d.h>
# include <ppapi/c/ppb_instance.h>
typedef void (*bgfx_post_swap_buffers_fn)(uint32_t _width, uint32_t _height);
/**
*
*/
BGFX_C_API bool bgfx_nacl_set_interfaces(PP_Instance, const PPB_Instance*, const PPB_Graphics3D*, bgfx_post_swap_buffers_fn);
#elif BX_PLATFORM_OSX
/**
*
*/
BGFX_C_API void bgfx_osx_set_ns_window(void* _window);
#elif BX_PLATFORM_WINDOWS
# include <windows.h>
/**
*
*/
BGFX_C_API void bgfx_win_set_hwnd(HWND _window);
#endif // BX_PLATFORM_
#endif // BGFX_PLATFORM_C99_H_HEADER_GUARD #endif // BGFX_PLATFORM_C99_H_HEADER_GUARD

View file

@ -30,7 +30,18 @@ namespace bgfx
/// allow creating separate rendering thread. If it is called before /// allow creating separate rendering thread. If it is called before
/// to bgfx::init, render thread won't be created by bgfx::init call. /// to bgfx::init, render thread won't be created by bgfx::init call.
RenderFrame::Enum renderFrame(); RenderFrame::Enum renderFrame();
}
struct PlatformData
{
void* ndt; //< Native display type
void* nwh; //< Native window handle
void* context; //< GL context, or D3D device
void* backbuffer; //< GL backbuffer, or D3D render target view
};
void setPlatformData(const PlatformData& _hooks);
} // namespace bgfx
#if BX_PLATFORM_ANDROID #if BX_PLATFORM_ANDROID
# include <android/native_window.h> # include <android/native_window.h>
@ -38,7 +49,13 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void androidSetWindow(::ANativeWindow* _window); inline void androidSetWindow(::ANativeWindow* _window)
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.nwh = _window;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx
@ -46,7 +63,13 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void iosSetEaglLayer(void* _layer); inline void iosSetEaglLayer(void* _window)
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.nwh = _window;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx
@ -55,7 +78,15 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL); inline void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL);
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.ndt = _display;
pd.nwh = _window;
pd.context = _glx;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx
@ -76,7 +107,14 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void osxSetNSWindow(void* _window, void* _nsgl = NULL); inline void osxSetNSWindow(void* _window, void* _nsgl = NULL)
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.nwh = _window;
pd.context = _nsgl;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx
@ -86,7 +124,13 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void winSetHwnd(::HWND _window); inline void winSetHwnd(::HWND _window)
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.nwh = _window;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx
@ -96,7 +140,13 @@ namespace bgfx
namespace bgfx namespace bgfx
{ {
/// ///
void winrtSetWindow(IUnknown* _window); inline void winrtSetWindow(::IUnknown* _window)
{
PlatformData pd;
memset(&pd, 0, sizeof(pd) );
pd.nwh = _window;
setPlatformData(pd);
}
} // namespace bgfx } // namespace bgfx

View file

@ -19,56 +19,6 @@ namespace bgfx
# define BGFX_CHECK_RENDER_THREAD() # define BGFX_CHECK_RENDER_THREAD()
#endif // BGFX_CONFIG_MULTITHREADED && !BX_PLATFORM_OSX && !BX_PLATFORM_IOS #endif // BGFX_CONFIG_MULTITHREADED && !BX_PLATFORM_OSX && !BX_PLATFORM_IOS
#if BX_PLATFORM_ANDROID
::ANativeWindow* g_bgfxAndroidWindow = NULL;
void androidSetWindow(::ANativeWindow* _window)
{
g_bgfxAndroidWindow = _window;
}
#elif BX_PLATFORM_IOS
void* g_bgfxEaglLayer = NULL;
void iosSetEaglLayer(void* _layer)
{
g_bgfxEaglLayer = _layer;
}
#elif BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
void* g_bgfxX11Display;
uint32_t g_bgfxX11Window;
void* g_bgfxGLX;
void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx)
{
g_bgfxX11Display = _display;
g_bgfxX11Window = _window;
g_bgfxGLX = _glx;
}
#elif BX_PLATFORM_OSX
void* g_bgfxNSWindow = NULL;
void* g_bgfxNSGL = NULL;
void osxSetNSWindow(void* _window, void* _nsgl)
{
g_bgfxNSWindow = _window;
g_bgfxNSGL = _nsgl;
}
#elif BX_PLATFORM_WINDOWS
::HWND g_bgfxHwnd = NULL;
void winSetHwnd(::HWND _window)
{
g_bgfxHwnd = _window;
}
#elif BX_PLATFORM_WINRT
::IUnknown* g_bgfxCoreWindow = NULL;
void winrtSetWindow(::IUnknown* _window)
{
g_bgfxCoreWindow = _window;
}
#endif // BX_PLATFORM_*
#if BGFX_CONFIG_USE_TINYSTL #if BGFX_CONFIG_USE_TINYSTL
void* TinyStlAllocator::static_allocate(size_t _bytes) void* TinyStlAllocator::static_allocate(size_t _bytes)
{ {
@ -252,6 +202,13 @@ namespace bgfx
static BX_THREAD uint32_t s_threadIndex = 0; static BX_THREAD uint32_t s_threadIndex = 0;
static Context* s_ctx = NULL; static Context* s_ctx = NULL;
static bool s_renderFrameCalled = false; static bool s_renderFrameCalled = false;
PlatformData g_platformData;
void setPlatformData(const PlatformData& _pd)
{
BGFX_FATAL(NULL == s_ctx, Fatal::UnableToInitialize, "Must be set prior to initialization!");
memcpy(&g_platformData, &_pd, sizeof(PlatformData) );
}
void setGraphicsDebuggerPresent(bool _present) void setGraphicsDebuggerPresent(bool _present)
{ {
@ -3033,6 +2990,7 @@ BX_STATIC_ASSERT(sizeof(bgfx::TransientVertexBuffer) == sizeof(bgfx_transient_ve
BX_STATIC_ASSERT(sizeof(bgfx::InstanceDataBuffer) == sizeof(bgfx_instance_data_buffer_t) ); BX_STATIC_ASSERT(sizeof(bgfx::InstanceDataBuffer) == sizeof(bgfx_instance_data_buffer_t) );
BX_STATIC_ASSERT(sizeof(bgfx::TextureInfo) == sizeof(bgfx_texture_info_t) ); BX_STATIC_ASSERT(sizeof(bgfx::TextureInfo) == sizeof(bgfx_texture_info_t) );
BX_STATIC_ASSERT(sizeof(bgfx::Caps) == sizeof(bgfx_caps_t) ); BX_STATIC_ASSERT(sizeof(bgfx::Caps) == sizeof(bgfx_caps_t) );
BX_STATIC_ASSERT(sizeof(bgfx::PlatformData) == sizeof(bgfx_platform_data_t) );
BGFX_C_API void bgfx_vertex_decl_begin(bgfx_vertex_decl_t* _decl, bgfx_renderer_type_t _renderer) BGFX_C_API void bgfx_vertex_decl_begin(bgfx_vertex_decl_t* _decl, bgfx_renderer_type_t _renderer)
{ {
@ -3645,40 +3603,7 @@ BGFX_C_API bgfx_render_frame_t bgfx_render_frame()
return bgfx_render_frame_t(bgfx::renderFrame() ); return bgfx_render_frame_t(bgfx::renderFrame() );
} }
#if BX_PLATFORM_ANDROID BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd)
BGFX_C_API void bgfx_android_set_window(ANativeWindow* _window)
{ {
bgfx::androidSetWindow(_window); bgfx::setPlatformData(*(bgfx::PlatformData*)_pd);
} }
#elif BX_PLATFORM_IOS
BGFX_C_API void bgfx_ios_set_eagl_layer(void* _layer)
{
bgfx::iosSetEaglLayer(_layer);
}
#elif BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI
BGFX_C_API void bgfx_x11_set_display_window(::Display* _display, ::Window _window)
{
bgfx::x11SetDisplayWindow(_display, _window);
}
#elif BX_PLATFORM_NACL
BGFX_C_API bool bgfx_nacl_set_interfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, bgfx_post_swap_buffers_fn _postSwapBuffers)
{
return bgfx::naclSetInterfaces(_instance, _instInterface, _graphicsInterface, _postSwapBuffers);
}
#elif BX_PLATFORM_OSX
BGFX_C_API void bgfx_osx_set_ns_window(void* _window)
{
bgfx::osxSetNSWindow(_window);
}
#elif BX_PLATFORM_WINDOWS
BGFX_C_API void bgfx_win_set_hwnd(HWND _window)
{
bgfx::winSetHwnd(_window);
}
#endif // BX_PLATFORM_*

View file

@ -217,22 +217,7 @@ namespace stl
namespace bgfx namespace bgfx
{ {
#if BX_PLATFORM_ANDROID extern PlatformData g_platformData;
extern ::ANativeWindow* g_bgfxAndroidWindow;
#elif BX_PLATFORM_IOS
extern void* g_bgfxEaglLayer;
#elif BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
extern void* g_bgfxX11Display;
extern uint32_t g_bgfxX11Window;
extern void* g_bgfxGLX;
#elif BX_PLATFORM_OSX
extern void* g_bgfxNSWindow;
extern void* g_bgfxNSGL;
#elif BX_PLATFORM_WINDOWS
extern ::HWND g_bgfxHwnd;
#elif BX_PLATFORM_WINRT
extern ::IUnknown* g_bgfxCoreWindow;
#endif // BX_PLATFORM_*
#if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10) #if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)
typedef uint16_t RenderItemCount; typedef uint16_t RenderItemCount;

View file

@ -18,7 +18,7 @@ namespace bgfx { namespace gl
void GlContext::create(uint32_t _width, uint32_t _height) void GlContext::create(uint32_t _width, uint32_t _height)
{ {
BX_UNUSED(_width, _height); BX_UNUSED(_width, _height);
CAEAGLLayer* layer = (CAEAGLLayer*)g_bgfxEaglLayer; CAEAGLLayer* layer = (CAEAGLLayer*)g_platformData.nwh;
layer.opaque = true; layer.opaque = true;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys

View file

@ -178,97 +178,91 @@ EGL_IMPORT
m_eglLibrary = eglOpen(); m_eglLibrary = eglOpen();
if (NULL == g_platformData.context)
{
BX_UNUSED(_width, _height);
EGLNativeDisplayType ndt = (EGLNativeDisplayType)g_platformData.ndt;
EGLNativeWindowType nwh = (EGLNativeWindowType )g_platformData.nwh;
# if BX_PLATFORM_ANDROID
if (!g_bgfxAndroidWindow)
{
BX_TRACE("androidSetWindow() was not called, assuming EGLContext and buffer-swapping are managed outside bgfx.");
}
else
{
# endif
BX_UNUSED(_width, _height);
EGLNativeDisplayType ndt = EGL_DEFAULT_DISPLAY;
EGLNativeWindowType nwh = (EGLNativeWindowType)NULL;
# if BX_PLATFORM_WINDOWS # if BX_PLATFORM_WINDOWS
ndt = GetDC(g_bgfxHwnd); if (NULL == g_platformData.ndt)
nwh = g_bgfxHwnd; {
# elif BX_PLATFORM_LINUX ndt = GetDC( (HWND)g_platformData.nwh);
ndt = (EGLNativeDisplayType)g_bgfxX11Display; }
nwh = (EGLNativeWindowType)g_bgfxX11Window; # endif // BX_PLATFORM_WINDOWS
# endif // BX_PLATFORM_
m_display = eglGetDisplay(ndt);
BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
EGLint major = 0; m_display = eglGetDisplay(ndt);
EGLint minor = 0; BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display);
EGLBoolean success = eglInitialize(m_display, &major, &minor);
BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
EGLint attrs[] = EGLint major = 0;
{ EGLint minor = 0;
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGLBoolean success = eglInitialize(m_display, &major, &minor);
BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor);
EGLint attrs[] =
{
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
# if BX_PLATFORM_ANDROID # if BX_PLATFORM_ANDROID
EGL_DEPTH_SIZE, 16, EGL_DEPTH_SIZE, 16,
# else # else
EGL_DEPTH_SIZE, 24, EGL_DEPTH_SIZE, 24,
# endif // BX_PLATFORM_ # endif // BX_PLATFORM_
EGL_STENCIL_SIZE, 8, EGL_STENCIL_SIZE, 8,
EGL_NONE EGL_NONE
}; };
EGLint numConfig = 0; EGLint numConfig = 0;
success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig); success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig);
BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig"); BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig");
# if BX_PLATFORM_ANDROID # if BX_PLATFORM_ANDROID
EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); EGLint format;
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format); eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
nwh = g_bgfxAndroidWindow; ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
# elif BX_PLATFORM_RPI # elif BX_PLATFORM_RPI
DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0); DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0);
DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0); DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0);
VC_RECT_T dstRect = { 0, 0, _width, _height }; VC_RECT_T dstRect = { 0, 0, _width, _height };
VC_RECT_T srcRect = { 0, 0, _width << 16, _height << 16 }; VC_RECT_T srcRect = { 0, 0, _width << 16, _height << 16 };
DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate
, dispmanDisplay , dispmanDisplay
, 0 , 0
, &dstRect , &dstRect
, 0 , 0
, &srcRect , &srcRect
, DISPMANX_PROTECTION_NONE , DISPMANX_PROTECTION_NONE
, NULL , NULL
, NULL , NULL
, DISPMANX_NO_ROTATE , DISPMANX_NO_ROTATE
); );
s_dispmanWindow.element = dispmanElement; s_dispmanWindow.element = dispmanElement;
s_dispmanWindow.width = _width; s_dispmanWindow.width = _width;
s_dispmanWindow.height = _height; s_dispmanWindow.height = _height;
nwh = &s_dispmanWindow; nwh = &s_dispmanWindow;
vc_dispmanx_update_submit_sync(dispmanUpdate); vc_dispmanx_update_submit_sync(dispmanUpdate);
# endif // BX_PLATFORM_ANDROID # endif // BX_PLATFORM_ANDROID
m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL);
BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface."); BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface.");
m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs); m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, s_contextAttrs);
BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context."); BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context.");
success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context.");
m_current = NULL; m_current = NULL;
eglSwapInterval(m_display, 0); eglSwapInterval(m_display, 0);
# if BX_PLATFORM_ANDROID
} }
# endif
# if BX_PLATFORM_EMSCRIPTEN # if BX_PLATFORM_EMSCRIPTEN
emscripten_set_canvas_size(_width, _height); emscripten_set_canvas_size(_width, _height);
# endif // BX_PLATFORM_EMSCRIPTEN # endif // BX_PLATFORM_EMSCRIPTEN
@ -303,7 +297,7 @@ EGL_IMPORT
{ {
EGLint format; EGLint format;
eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format); ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format);
} }
# endif // BX_PLATFORM_ANDROID # endif // BX_PLATFORM_ANDROID

View file

@ -29,23 +29,23 @@ namespace bgfx { namespace gl
SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context) SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context)
: m_window(_window) : m_window(_window)
{ {
m_context = glXCreateContext( (::Display*)g_bgfxX11Display, _visualInfo, _context, GL_TRUE); m_context = glXCreateContext( (::Display*)g_platformData.ndt, _visualInfo, _context, GL_TRUE);
} }
~SwapChainGL() ~SwapChainGL()
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0); glXMakeCurrent( (::Display*)g_platformData.ndt, 0, 0);
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); glXDestroyContext( (::Display*)g_platformData.ndt, m_context);
} }
void makeCurrent() void makeCurrent()
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, m_window, m_context); glXMakeCurrent( (::Display*)g_platformData.ndt, m_window, m_context);
} }
void swapBuffers() void swapBuffers()
{ {
glXSwapBuffers( (::Display*)g_bgfxX11Display, m_window); glXSwapBuffers( (::Display*)g_platformData.ndt, m_window);
} }
Window m_window; Window m_window;
@ -56,14 +56,14 @@ namespace bgfx { namespace gl
{ {
BX_UNUSED(_width, _height); BX_UNUSED(_width, _height);
m_context = (GLXContext)g_bgfxGLX; m_context = (GLXContext)g_platformData.context;
if (NULL == g_bgfxGLX) if (NULL == g_platformData.context)
{ {
XLockDisplay( (::Display*)g_bgfxX11Display); XLockDisplay( (::Display*)g_platformData.ndt);
int major, minor; int major, minor;
bool version = glXQueryVersion( (::Display*)g_bgfxX11Display, &major, &minor); bool version = glXQueryVersion( (::Display*)g_platformData.ndt, &major, &minor);
BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version"); BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version");
BGFX_FATAL( (major == 1 && minor >= 2) || major > 1 BGFX_FATAL( (major == 1 && minor >= 2) || major > 1
, Fatal::UnableToInitialize , Fatal::UnableToInitialize
@ -72,9 +72,9 @@ namespace bgfx { namespace gl
, minor , minor
); );
int32_t screen = DefaultScreen( (::Display*)g_bgfxX11Display); int32_t screen = DefaultScreen( (::Display*)g_platformData.ndt);
const char* extensions = glXQueryExtensionsString( (::Display*)g_bgfxX11Display, screen); const char* extensions = glXQueryExtensionsString( (::Display*)g_platformData.ndt, screen);
BX_TRACE("GLX extensions:"); BX_TRACE("GLX extensions:");
dumpExtensions(extensions); dumpExtensions(extensions);
@ -96,13 +96,13 @@ namespace bgfx { namespace gl
GLXFBConfig bestConfig = NULL; GLXFBConfig bestConfig = NULL;
int numConfigs; int numConfigs;
GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_bgfxX11Display, screen, attrsGlx, &numConfigs); GLXFBConfig* configs = glXChooseFBConfig( (::Display*)g_platformData.ndt, screen, attrsGlx, &numConfigs);
BX_TRACE("glX num configs %d", numConfigs); BX_TRACE("glX num configs %d", numConfigs);
for (int ii = 0; ii < numConfigs; ++ii) for (int ii = 0; ii < numConfigs; ++ii)
{ {
m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_bgfxX11Display, configs[ii]); m_visualInfo = glXGetVisualFromFBConfig( (::Display*)g_platformData.ndt, configs[ii]);
if (NULL != m_visualInfo) if (NULL != m_visualInfo)
{ {
BX_TRACE("---"); BX_TRACE("---");
@ -110,7 +110,7 @@ namespace bgfx { namespace gl
for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2) for (uint32_t attr = 6; attr < BX_COUNTOF(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2)
{ {
int value; int value;
glXGetFBConfigAttrib( (::Display*)g_bgfxX11Display, configs[ii], attrsGlx[attr], &value); glXGetFBConfigAttrib( (::Display*)g_platformData.ndt, configs[ii], attrsGlx[attr], &value);
BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)" BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)"
, ii , ii
, numConfigs , numConfigs
@ -146,7 +146,7 @@ namespace bgfx { namespace gl
BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration."); BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration.");
BX_TRACE("Create GL 2.1 context."); BX_TRACE("Create GL 2.1 context.");
m_context = glXCreateContext( (::Display*)g_bgfxX11Display, m_visualInfo, 0, GL_TRUE); m_context = glXCreateContext( (::Display*)g_platformData.ndt, m_visualInfo, 0, GL_TRUE);
BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context."); BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context.");
#if BGFX_CONFIG_RENDERER_OPENGL >= 31 #if BGFX_CONFIG_RENDERER_OPENGL >= 31
@ -163,11 +163,11 @@ namespace bgfx { namespace gl
0, 0,
}; };
GLXContext context = glXCreateContextAttribsARB( (::Display*)g_bgfxX11Display, bestConfig, 0, true, contextAttrs); GLXContext context = glXCreateContextAttribsARB( (::Display*)g_platformData.ndt, bestConfig, 0, true, contextAttrs);
if (NULL != context) if (NULL != context)
{ {
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); glXDestroyContext( (::Display*)g_platformData.ndt, m_context);
m_context = context; m_context = context;
} }
} }
@ -175,19 +175,19 @@ namespace bgfx { namespace gl
BX_UNUSED(bestConfig); BX_UNUSED(bestConfig);
#endif // BGFX_CONFIG_RENDERER_OPENGL >= 31 #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
XUnlockDisplay( (::Display*)g_bgfxX11Display); XUnlockDisplay( (::Display*)g_platformData.ndt);
} }
import(); import();
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); glXMakeCurrent( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, m_context);
m_current = NULL; m_current = NULL;
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT");
if (NULL != glXSwapIntervalEXT) if (NULL != glXSwapIntervalEXT)
{ {
BX_TRACE("Using glXSwapIntervalEXT."); BX_TRACE("Using glXSwapIntervalEXT.");
glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, 0); glXSwapIntervalEXT( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, 0);
} }
else else
{ {
@ -210,15 +210,15 @@ namespace bgfx { namespace gl
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); glXSwapBuffers( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh);
} }
void GlContext::destroy() void GlContext::destroy()
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0); glXMakeCurrent( (::Display*)g_platformData.ndt, 0, 0);
if (NULL == g_bgfxGLX) if (NULL == g_platformData.context)
{ {
glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); glXDestroyContext( (::Display*)g_platformData.ndt, m_context);
XFree(m_visualInfo); XFree(m_visualInfo);
} }
m_context = NULL; m_context = NULL;
@ -232,7 +232,7 @@ namespace bgfx { namespace gl
if (NULL != glXSwapIntervalEXT) if (NULL != glXSwapIntervalEXT)
{ {
glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, interval); glXSwapIntervalEXT( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, interval);
} }
else if (NULL != glXSwapIntervalMESA) else if (NULL != glXSwapIntervalMESA)
{ {
@ -265,7 +265,7 @@ namespace bgfx { namespace gl
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); glXSwapBuffers( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh);
} }
else else
{ {
@ -281,7 +281,7 @@ namespace bgfx { namespace gl
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); glXMakeCurrent( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, m_context);
} }
else else
{ {

View file

@ -65,10 +65,10 @@ namespace bgfx { namespace gl
BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!"); BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!");
const AutoreleasePoolHolder pool; const AutoreleasePoolHolder pool;
NSWindow* nsWindow = (NSWindow*)g_bgfxNSWindow; NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
m_context = g_bgfxNSGL; m_context = g_platformData.context;
if (NULL == g_bgfxNSGL) if (NULL == g_platformData.context)
{ {
#if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070) #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
NSOpenGLPixelFormatAttribute profile = NSOpenGLPixelFormatAttribute profile =
@ -120,7 +120,7 @@ namespace bgfx { namespace gl
void GlContext::destroy() void GlContext::destroy()
{ {
if (NULL == g_bgfxNSGL) if (NULL == g_platformData.context)
{ {
NSOpenGLView* glView = (NSOpenGLView*)m_view; NSOpenGLView* glView = (NSOpenGLView*)m_view;
[glView release]; [glView release];

View file

@ -109,14 +109,14 @@ namespace bgfx { namespace gl
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.");
// If g_bgfxHwnd is NULL, the assumption is that GL context was created // If g_platformHooks.nwh is NULL, the assumption is that GL context was created
// by user (for example, using SDL, GLFW, etc.) // by user (for example, using SDL, GLFW, etc.)
BX_WARN(NULL != g_bgfxHwnd BX_WARN(NULL != g_platformData.nwh
, "bgfx::winSetHwnd with valid window is not called. This might " , "bgfx::setPlatform with valid window is not called. This might "
"be intentional when GL context is created by the user." "be intentional when GL context is created by the user."
); );
if (NULL != g_bgfxHwnd) if (NULL != g_platformData.nwh)
{ {
wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent"); wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent");
BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent."); BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent.");
@ -127,7 +127,7 @@ namespace bgfx { namespace gl
wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext"); wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext");
BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext."); BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext.");
m_hdc = GetDC(g_bgfxHwnd); m_hdc = GetDC( (HWND)g_platformData.nwh);
BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!"); BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!");
// Dummy window to peek into WGL functionality. // Dummy window to peek into WGL functionality.
@ -272,14 +272,14 @@ namespace bgfx { namespace gl
void GlContext::destroy() void GlContext::destroy()
{ {
if (NULL != g_bgfxHwnd) if (NULL != g_platformData.nwh)
{ {
wglMakeCurrent(NULL, NULL); wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_context); wglDeleteContext(m_context);
m_context = NULL; m_context = NULL;
ReleaseDC(g_bgfxHwnd, m_hdc); ReleaseDC( (HWND)g_platformData.nwh, m_hdc);
m_hdc = NULL; m_hdc = NULL;
} }
@ -324,7 +324,7 @@ namespace bgfx { namespace gl
if (NULL == _swapChain) if (NULL == _swapChain)
{ {
if (NULL != g_bgfxHwnd) if (NULL != g_platformData.nwh)
{ {
SwapBuffers(m_hdc); SwapBuffers(m_hdc);
} }

View file

@ -430,21 +430,39 @@ namespace bgfx { namespace d3d11
struct RendererContextD3D11 : public RendererContextI struct RendererContextD3D11 : public RendererContextI
{ {
RendererContextD3D11() RendererContextD3D11()
: m_renderdocdll(NULL) : m_d3d9dll(NULL)
, m_d3d11dll(NULL)
, m_dxgidll(NULL)
, m_renderdocdll(NULL)
, m_driverType(D3D_DRIVER_TYPE_NULL)
, m_featureLevel(D3D_FEATURE_LEVEL(0) )
, m_adapter(NULL)
, m_factory(NULL)
, m_swapChain(NULL)
, m_lost(0) , m_lost(0)
, m_numWindows(0)
, m_device(NULL)
, m_deviceCtx(NULL)
, m_backBufferColor(NULL) , m_backBufferColor(NULL)
, m_backBufferDepthStencil(NULL) , m_backBufferDepthStencil(NULL)
, m_currentColor(NULL)
, m_currentDepthStencil(NULL)
, m_captureTexture(NULL) , m_captureTexture(NULL)
, m_captureResolve(NULL) , m_captureResolve(NULL)
, m_wireframe(false) , m_wireframe(false)
, m_flags(BGFX_RESET_NONE) , m_flags(BGFX_RESET_NONE)
, m_maxAnisotropy(1) , m_maxAnisotropy(1)
, m_currentProgram(NULL)
, m_vsChanges(0) , m_vsChanges(0)
, m_fsChanges(0) , m_fsChanges(0)
, m_rtMsaa(false) , m_rtMsaa(false)
, m_ovrRtv(NULL) , m_ovrRtv(NULL)
, m_ovrDsv(NULL) , m_ovrDsv(NULL)
{ {
m_fbh.idx = invalidHandle;
memset(&m_adapterDesc, 0, sizeof(m_adapterDesc) );
memset(&m_scd, 0, sizeof(m_scd) );
memset(&m_windows, 0xff, sizeof(m_windows) );
} }
~RendererContextD3D11() ~RendererContextD3D11()
@ -509,114 +527,125 @@ namespace bgfx { namespace d3d11
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create DXGI factory."); BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create DXGI factory.");
#endif // BX_PLATFORM_WINRT #endif // BX_PLATFORM_WINRT
m_adapter = NULL; m_device = (ID3D11Device*)g_platformData.context;
m_driverType = D3D_DRIVER_TYPE_HARDWARE;
IDXGIAdapter* adapter; if (NULL == m_device)
for (uint32_t ii = 0
; DXGI_ERROR_NOT_FOUND != factory->EnumAdapters(ii, &adapter) && ii < BX_COUNTOF(g_caps.gpu)
; ++ii
)
{ {
DXGI_ADAPTER_DESC desc; m_adapter = NULL;
hr = adapter->GetDesc(&desc); m_driverType = D3D_DRIVER_TYPE_HARDWARE;
if (SUCCEEDED(hr) )
IDXGIAdapter* adapter;
for (uint32_t ii = 0
; DXGI_ERROR_NOT_FOUND != factory->EnumAdapters(ii, &adapter) && ii < BX_COUNTOF(g_caps.gpu)
; ++ii
)
{ {
BX_TRACE("Adapter #%d", ii); DXGI_ADAPTER_DESC desc;
hr = adapter->GetDesc(&desc);
char description[BX_COUNTOF(desc.Description)]; if (SUCCEEDED(hr) )
wcstombs(description, desc.Description, BX_COUNTOF(desc.Description) );
BX_TRACE("\tDescription: %s", description);
BX_TRACE("\tVendorId: 0x%08x, DeviceId: 0x%08x, SubSysId: 0x%08x, Revision: 0x%08x"
, desc.VendorId
, desc.DeviceId
, desc.SubSysId
, desc.Revision
);
BX_TRACE("\tMemory: %" PRIi64 " (video), %" PRIi64 " (system), %" PRIi64 " (shared)"
, desc.DedicatedVideoMemory
, desc.DedicatedSystemMemory
, desc.SharedSystemMemory
);
g_caps.gpu[ii].vendorId = (uint16_t)desc.VendorId;
g_caps.gpu[ii].deviceId = (uint16_t)desc.DeviceId;
++g_caps.numGPUs;
if (NULL == m_adapter)
{ {
if ( (BGFX_PCI_ID_NONE != g_caps.vendorId || 0 != g_caps.deviceId) BX_TRACE("Adapter #%d", ii);
&& (BGFX_PCI_ID_NONE == g_caps.vendorId || desc.VendorId == g_caps.vendorId)
&& ( 0 == g_caps.deviceId || desc.DeviceId == g_caps.deviceId) )
{
m_adapter = adapter;
m_adapter->AddRef();
m_driverType = D3D_DRIVER_TYPE_UNKNOWN;
}
if (BX_ENABLED(BGFX_CONFIG_DEBUG_PERFHUD) char description[BX_COUNTOF(desc.Description)];
&& 0 != strstr(description, "PerfHUD") ) wcstombs(description, desc.Description, BX_COUNTOF(desc.Description) );
BX_TRACE("\tDescription: %s", description);
BX_TRACE("\tVendorId: 0x%08x, DeviceId: 0x%08x, SubSysId: 0x%08x, Revision: 0x%08x"
, desc.VendorId
, desc.DeviceId
, desc.SubSysId
, desc.Revision
);
BX_TRACE("\tMemory: %" PRIi64 " (video), %" PRIi64 " (system), %" PRIi64 " (shared)"
, desc.DedicatedVideoMemory
, desc.DedicatedSystemMemory
, desc.SharedSystemMemory
);
g_caps.gpu[ii].vendorId = (uint16_t)desc.VendorId;
g_caps.gpu[ii].deviceId = (uint16_t)desc.DeviceId;
++g_caps.numGPUs;
if (NULL == m_adapter)
{ {
m_adapter = adapter; if ( (BGFX_PCI_ID_NONE != g_caps.vendorId || 0 != g_caps.deviceId)
m_driverType = D3D_DRIVER_TYPE_REFERENCE; && (BGFX_PCI_ID_NONE == g_caps.vendorId || desc.VendorId == g_caps.vendorId)
&& ( 0 == g_caps.deviceId || desc.DeviceId == g_caps.deviceId) )
{
m_adapter = adapter;
m_adapter->AddRef();
m_driverType = D3D_DRIVER_TYPE_UNKNOWN;
}
if (BX_ENABLED(BGFX_CONFIG_DEBUG_PERFHUD)
&& 0 != strstr(description, "PerfHUD") )
{
m_adapter = adapter;
m_driverType = D3D_DRIVER_TYPE_REFERENCE;
}
} }
} }
DX_RELEASE(adapter, adapter == m_adapter ? 1 : 0);
} }
DX_RELEASE(factory, NULL != m_adapter ? 1 : 0);
DX_RELEASE(adapter, adapter == m_adapter ? 1 : 0); D3D_FEATURE_LEVEL features[] =
}
DX_RELEASE(factory, NULL != m_adapter ? 1 : 0);
D3D_FEATURE_LEVEL features[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1,
};
uint32_t flags = 0
| D3D11_CREATE_DEVICE_SINGLETHREADED
| D3D11_CREATE_DEVICE_BGRA_SUPPORT
| (BX_ENABLED(BGFX_CONFIG_DEBUG) ? D3D11_CREATE_DEVICE_DEBUG : 0)
;
hr = E_FAIL;
for (uint32_t ii = 0; ii < 3 && FAILED(hr);)
{
hr = D3D11CreateDevice(m_adapter
, m_driverType
, NULL
, flags
, &features[ii]
, BX_COUNTOF(features)-ii
, D3D11_SDK_VERSION
, &m_device
, &m_featureLevel
, &m_deviceCtx
);
if (FAILED(hr)
&& 0 != (flags & D3D11_CREATE_DEVICE_DEBUG) )
{ {
// Try without debug in case D3D11 SDK Layers D3D_FEATURE_LEVEL_11_1,
// is not present? D3D_FEATURE_LEVEL_11_0,
flags &= ~D3D11_CREATE_DEVICE_DEBUG; D3D_FEATURE_LEVEL_10_1,
continue; D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1,
};
uint32_t flags = 0
| D3D11_CREATE_DEVICE_SINGLETHREADED
| D3D11_CREATE_DEVICE_BGRA_SUPPORT
| (BX_ENABLED(BGFX_CONFIG_DEBUG) ? D3D11_CREATE_DEVICE_DEBUG : 0)
;
hr = E_FAIL;
for (uint32_t ii = 0; ii < 3 && FAILED(hr);)
{
hr = D3D11CreateDevice(m_adapter
, m_driverType
, NULL
, flags
, &features[ii]
, BX_COUNTOF(features)-ii
, D3D11_SDK_VERSION
, &m_device
, &m_featureLevel
, &m_deviceCtx
);
if (FAILED(hr)
&& 0 != (flags & D3D11_CREATE_DEVICE_DEBUG) )
{
// Try without debug in case D3D11 SDK Layers
// is not present?
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
continue;
}
++ii;
} }
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device.");
++ii; if (NULL != m_adapter)
{
DX_RELEASE(m_adapter, 2);
}
} }
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); else
if (NULL != m_adapter)
{ {
DX_RELEASE(m_adapter, 2); m_device->GetImmediateContext(&m_deviceCtx);
BGFX_FATAL(NULL != m_deviceCtx, Fatal::UnableToInitialize, "Unable to create Direct3D11 device.");
} }
IDXGIDevice* device = NULL; IDXGIDevice* device = NULL;
IDXGIAdapter* adapter;
hr = E_FAIL; hr = E_FAIL;
for (uint32_t ii = 0; ii < BX_COUNTOF(s_deviceIIDs) && FAILED(hr); ++ii) for (uint32_t ii = 0; ii < BX_COUNTOF(s_deviceIIDs) && FAILED(hr); ++ii)
{ {
@ -660,60 +689,68 @@ BX_PRAGMA_DIAGNOSTIC_POP();
g_caps.vendorId = (uint16_t)m_adapterDesc.VendorId; g_caps.vendorId = (uint16_t)m_adapterDesc.VendorId;
g_caps.deviceId = (uint16_t)m_adapterDesc.DeviceId; g_caps.deviceId = (uint16_t)m_adapterDesc.DeviceId;
if (NULL == g_platformData.backbuffer)
{
#if BX_PLATFORM_WINRT #if BX_PLATFORM_WINRT
hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&m_factory); hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&m_factory);
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device.");
DX_RELEASE(adapter, 2); DX_RELEASE(adapter, 2);
memset(&m_scd, 0, sizeof(m_scd) ); memset(&m_scd, 0, sizeof(m_scd) );
m_scd.Width = BGFX_DEFAULT_WIDTH; m_scd.Width = BGFX_DEFAULT_WIDTH;
m_scd.Height = BGFX_DEFAULT_HEIGHT; m_scd.Height = BGFX_DEFAULT_HEIGHT;
m_scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; m_scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
m_scd.Stereo = false; m_scd.Stereo = false;
m_scd.SampleDesc.Count = 1; m_scd.SampleDesc.Count = 1;
m_scd.SampleDesc.Quality = 0; m_scd.SampleDesc.Quality = 0;
m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
m_scd.BufferCount = 2; m_scd.BufferCount = 2;
m_scd.Scaling = DXGI_SCALING_NONE; m_scd.Scaling = DXGI_SCALING_NONE;
m_scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; m_scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
m_scd.AlphaMode = DXGI_ALPHA_MODE_IGNORE; m_scd.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
hr = m_factory->CreateSwapChainForCoreWindow(m_device hr = m_factory->CreateSwapChainForCoreWindow(m_device
, g_bgfxCoreWindow , (::IUnknown*)g_platformData.nwh
, &m_scd , &m_scd
, NULL , NULL
, &m_swapChain , &m_swapChain
); );
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain.");
#else #else
hr = adapter->GetParent(IID_IDXGIFactory, (void**)&m_factory); hr = adapter->GetParent(IID_IDXGIFactory, (void**)&m_factory);
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device.");
DX_RELEASE(adapter, 2); DX_RELEASE(adapter, 2);
memset(&m_scd, 0, sizeof(m_scd) ); memset(&m_scd, 0, sizeof(m_scd) );
m_scd.BufferDesc.Width = BGFX_DEFAULT_WIDTH; m_scd.BufferDesc.Width = BGFX_DEFAULT_WIDTH;
m_scd.BufferDesc.Height = BGFX_DEFAULT_HEIGHT; m_scd.BufferDesc.Height = BGFX_DEFAULT_HEIGHT;
m_scd.BufferDesc.RefreshRate.Numerator = 60; m_scd.BufferDesc.RefreshRate.Numerator = 60;
m_scd.BufferDesc.RefreshRate.Denominator = 1; m_scd.BufferDesc.RefreshRate.Denominator = 1;
m_scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; m_scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
m_scd.SampleDesc.Count = 1; m_scd.SampleDesc.Count = 1;
m_scd.SampleDesc.Quality = 0; m_scd.SampleDesc.Quality = 0;
m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
m_scd.BufferCount = 1; m_scd.BufferCount = 1;
m_scd.OutputWindow = g_bgfxHwnd; m_scd.OutputWindow = (HWND)g_platformData.nwh;
m_scd.Windowed = true; m_scd.Windowed = true;
hr = m_factory->CreateSwapChain(m_device hr = m_factory->CreateSwapChain(m_device
, &m_scd , &m_scd
, &m_swapChain , &m_swapChain
); );
BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain.");
DX_CHECK(m_factory->MakeWindowAssociation(g_bgfxHwnd, 0 DX_CHECK(m_factory->MakeWindowAssociation((HWND)g_platformData.nwh, 0
| DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_WINDOW_CHANGES
| DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_ALT_ENTER
) ); ));
#endif // BX_PLATFORM_WINRT #endif // BX_PLATFORM_WINRT
}
else
{
memset(&m_scd, 0, sizeof(m_scd) );
m_backBufferColor = (ID3D11RenderTargetView*)g_platformData.backbuffer;
}
m_numWindows = 1; m_numWindows = 1;
@ -1147,6 +1184,12 @@ BX_PRAGMA_DIAGNOSTIC_POP();
void saveScreenShot(const char* _filePath) BX_OVERRIDE void saveScreenShot(const char* _filePath) BX_OVERRIDE
{ {
BX_WARN(NULL != m_swapChain, "Unable to capture screenshot %s.", _filePath);
if (NULL == m_swapChain)
{
return;
}
ID3D11Texture2D* backBuffer; ID3D11Texture2D* backBuffer;
DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backBuffer)); DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backBuffer));
@ -1317,7 +1360,11 @@ BX_PRAGMA_DIAGNOSTIC_POP();
ovrPreReset(); ovrPreReset();
DX_RELEASE(m_backBufferDepthStencil, 0); DX_RELEASE(m_backBufferDepthStencil, 0);
DX_RELEASE(m_backBufferColor, 0);
if (NULL != m_swapChain)
{
DX_RELEASE(m_backBufferColor, 0);
}
for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii) for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
{ {
@ -1331,11 +1378,14 @@ BX_PRAGMA_DIAGNOSTIC_POP();
void postReset() void postReset()
{ {
ID3D11Texture2D* color; if (NULL != m_swapChain)
DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&color)); {
ID3D11Texture2D* color;
DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&color));
DX_CHECK(m_device->CreateRenderTargetView(color, NULL, &m_backBufferColor) ); DX_CHECK(m_device->CreateRenderTargetView(color, NULL, &m_backBufferColor) );
DX_RELEASE(color, 0); DX_RELEASE(color, 0);
}
ovrPostReset(); ovrPostReset();
@ -1502,38 +1552,41 @@ BX_PRAGMA_DIAGNOSTIC_POP();
preReset(); preReset();
if (resize) if (NULL != m_swapChain)
{ {
DX_CHECK(m_swapChain->ResizeBuffers(2 if (resize)
, getBufferWidth() {
, getBufferHeight() DX_CHECK(m_swapChain->ResizeBuffers(2
, getBufferFormat() , getBufferWidth()
, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH , getBufferHeight()
) ); , getBufferFormat()
} , DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH
else ));
{ }
updateMsaa(); else
m_scd.SampleDesc = s_msaa[(m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT]; {
updateMsaa();
m_scd.SampleDesc = s_msaa[(m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT];
DX_RELEASE(m_swapChain, 0); DX_RELEASE(m_swapChain, 0);
#if BX_PLATFORM_WINRT #if BX_PLATFORM_WINRT
HRESULT hr; HRESULT hr;
hr = m_factory->CreateSwapChainForCoreWindow(m_device hr = m_factory->CreateSwapChainForCoreWindow(m_device
, g_bgfxCoreWindow , (::IUnknown*)g_platformData.nwh
, &m_scd , &m_scd
, NULL , NULL
, &m_swapChain , &m_swapChain
); );
#else #else
HRESULT hr; HRESULT hr;
hr = m_factory->CreateSwapChain(m_device hr = m_factory->CreateSwapChain(m_device
, &m_scd , &m_scd
, &m_swapChain , &m_swapChain
); );
#endif // BX_PLATFORM_WINRT #endif // BX_PLATFORM_WINRT
BGFX_FATAL(SUCCEEDED(hr), bgfx::Fatal::UnableToInitialize, "Failed to create swap chain."); BGFX_FATAL(SUCCEEDED(hr), bgfx::Fatal::UnableToInitialize, "Failed to create swap chain.");
}
} }
postReset(); postReset();
@ -2042,7 +2095,7 @@ BX_PRAGMA_DIAGNOSTIC_POP();
config.D3D11.pDeviceContext = m_deviceCtx; config.D3D11.pDeviceContext = m_deviceCtx;
config.D3D11.pBackBufferRT = m_backBufferColor; config.D3D11.pBackBufferRT = m_backBufferColor;
config.D3D11.pSwapChain = m_swapChain; config.D3D11.pSwapChain = m_swapChain;
if (m_ovr.postReset(g_bgfxHwnd, &config.Config, !!(m_flags & BGFX_RESET_HMD_DEBUG) ) ) if (m_ovr.postReset(g_platformData.nwh, &config.Config, !!(m_flags & BGFX_RESET_HMD_DEBUG) ) )
{ {
uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate); uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
const Memory* mem = alloc(size); const Memory* mem = alloc(size);
@ -2404,32 +2457,30 @@ BX_PRAGMA_DIAGNOSTIC_POP();
} }
} }
#if USE_D3D11_DYNAMIC_LIB
void* m_d3d9dll; void* m_d3d9dll;
void* m_d3d11dll; void* m_d3d11dll;
void* m_dxgidll; void* m_dxgidll;
#endif // USE_D3D11_DYNAMIC_LIB
void* m_renderdocdll; void* m_renderdocdll;
D3D_DRIVER_TYPE m_driverType; D3D_DRIVER_TYPE m_driverType;
D3D_FEATURE_LEVEL m_featureLevel; D3D_FEATURE_LEVEL m_featureLevel;
IDXGIAdapter* m_adapter; IDXGIAdapter* m_adapter;
DXGI_ADAPTER_DESC m_adapterDesc; DXGI_ADAPTER_DESC m_adapterDesc;
#if BX_PLATFORM_WINRT #if BX_PLATFORM_WINRT
IDXGIFactory2* m_factory; IDXGIFactory2* m_factory;
IDXGISwapChain1* m_swapChain; IDXGISwapChain1* m_swapChain;
#else #else
IDXGIFactory* m_factory; IDXGIFactory* m_factory;
IDXGISwapChain* m_swapChain; IDXGISwapChain* m_swapChain;
#endif #endif // BX_PLATFORM_WINRT
uint16_t m_lost; uint16_t m_lost;
uint16_t m_numWindows; uint16_t m_numWindows;
FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS]; FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
ID3D11Device* m_device; ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceCtx; ID3D11DeviceContext* m_deviceCtx;
ID3D11RenderTargetView* m_backBufferColor; ID3D11RenderTargetView* m_backBufferColor;
ID3D11DepthStencilView* m_backBufferDepthStencil; ID3D11DepthStencilView* m_backBufferDepthStencil;
ID3D11RenderTargetView* m_currentColor; ID3D11RenderTargetView* m_currentColor;

View file

@ -19,10 +19,10 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wpragmas");
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4005) // warning C4005: '' : macro redefinition BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4005) // warning C4005: '' : macro redefinition
#define D3D11_NO_HELPERS #define D3D11_NO_HELPERS
#if BX_PLATFORM_WINRT #if BX_PLATFORM_WINRT
#include <d3d11_2.h> # include <d3d11_2.h>
#else #else
#include <d3d11.h> # include <d3d11.h>
#endif #endif // BX_PLATFORM_WINRT
BX_PRAGMA_DIAGNOSTIC_POP() BX_PRAGMA_DIAGNOSTIC_POP()
#include "renderer.h" #include "renderer.h"

View file

@ -272,6 +272,7 @@ namespace bgfx { namespace d3d9
RendererContextD3D9() RendererContextD3D9()
: m_d3d9(NULL) : m_d3d9(NULL)
, m_device(NULL) , m_device(NULL)
, m_swapChain(NULL)
, m_captureTexture(NULL) , m_captureTexture(NULL)
, m_captureSurface(NULL) , m_captureSurface(NULL)
, m_captureResolve(NULL) , m_captureResolve(NULL)
@ -316,7 +317,7 @@ namespace bgfx { namespace d3d9
m_params.Windowed = true; m_params.Windowed = true;
RECT rect; RECT rect;
GetWindowRect(g_bgfxHwnd, &rect); GetWindowRect( (HWND)g_platformData.nwh, &rect);
m_params.BackBufferWidth = rect.right-rect.left; m_params.BackBufferWidth = rect.right-rect.left;
m_params.BackBufferHeight = rect.bottom-rect.top; m_params.BackBufferHeight = rect.bottom-rect.top;
@ -417,7 +418,7 @@ namespace bgfx { namespace d3d9
#if 0 // BGFX_CONFIG_RENDERER_DIRECT3D9EX #if 0 // BGFX_CONFIG_RENDERER_DIRECT3D9EX
DX_CHECK(m_d3d9->CreateDeviceEx(m_adapter DX_CHECK(m_d3d9->CreateDeviceEx(m_adapter
, m_deviceType , m_deviceType
, g_bgfxHwnd , g_platformHooks.nwh
, behaviorFlags[ii] , behaviorFlags[ii]
, &m_params , &m_params
, NULL , NULL
@ -426,7 +427,7 @@ namespace bgfx { namespace d3d9
#else #else
DX_CHECK(m_d3d9->CreateDevice(m_adapter DX_CHECK(m_d3d9->CreateDevice(m_adapter
, m_deviceType , m_deviceType
, g_bgfxHwnd , (HWND)g_platformData.nwh
, behaviorFlags[ii] , behaviorFlags[ii]
, &m_params , &m_params
, &m_device , &m_device
@ -860,11 +861,11 @@ namespace bgfx { namespace d3d9
) ); ) );
RECT rc; RECT rc;
GetClientRect(g_bgfxHwnd, &rc); GetClientRect( (HWND)g_platformData.nwh, &rc);
POINT point; POINT point;
point.x = rc.left; point.x = rc.left;
point.y = rc.top; point.y = rc.top;
ClientToScreen(g_bgfxHwnd, &point); ClientToScreen( (HWND)g_platformData.nwh, &point);
uint8_t* data = (uint8_t*)rect.pBits; uint8_t* data = (uint8_t*)rect.pBits;
uint32_t bytesPerPixel = rect.Pitch/dm.Width; uint32_t bytesPerPixel = rect.Pitch/dm.Width;
@ -1146,7 +1147,7 @@ namespace bgfx { namespace d3d9
void flip(HMD& /*_hmd*/) BX_OVERRIDE void flip(HMD& /*_hmd*/) BX_OVERRIDE
{ {
if (NULL != m_device) if (NULL != m_swapChain)
{ {
#if BGFX_CONFIG_RENDERER_DIRECT3D9EX #if BGFX_CONFIG_RENDERER_DIRECT3D9EX
if (NULL != m_deviceEx) if (NULL != m_deviceEx)
@ -1160,7 +1161,7 @@ namespace bgfx { namespace d3d9
HRESULT hr; HRESULT hr;
if (0 == ii) if (0 == ii)
{ {
hr = m_swapChain->Present(NULL, NULL, g_bgfxHwnd, NULL, 0); hr = m_swapChain->Present(NULL, NULL, (HWND)g_platformData.nwh, NULL, 0);
} }
else else
{ {

View file

@ -2239,9 +2239,9 @@ namespace bgfx { namespace gl
config.OGL.Header.RTSize.h = m_resolution.m_height; config.OGL.Header.RTSize.h = m_resolution.m_height;
# endif // OVR_VERSION > OVR_VERSION_043 # endif // OVR_VERSION > OVR_VERSION_043
config.OGL.Header.Multisample = 0; config.OGL.Header.Multisample = 0;
config.OGL.Window = g_bgfxHwnd; config.OGL.Window = g_platformData.nwh;
config.OGL.DC = GetDC(g_bgfxHwnd); config.OGL.DC = GetDC(g_platformData.nwh);
if (m_ovr.postReset(g_bgfxHwnd, &config.Config, !!(m_resolution.m_flags & BGFX_RESET_HMD_DEBUG) ) ) if (m_ovr.postReset(g_platformData.nwh, &config.Config, !!(m_resolution.m_flags & BGFX_RESET_HMD_DEBUG) ) )
{ {
uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate); uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
const Memory* mem = alloc(size); const Memory* mem = alloc(size);