diff --git a/include/bgfxplatform.c99.h b/include/bgfxplatform.c99.h index 5adff243..ce60185d 100644 --- a/include/bgfxplatform.c99.h +++ b/include/bgfxplatform.c99.h @@ -31,55 +31,15 @@ typedef enum bgfx_render_frame */ BGFX_C_API bgfx_render_frame_t bgfx_render_frame(); -#if BX_PLATFORM_ANDROID -# include +typedef struct bgfx_platform_data +{ + void* ndt; + void* nwh; + void* context; + void* backbuffer; -/** - * - */ -BGFX_C_API void bgfx_android_set_window(ANativeWindow* _window); +} bgfx_platform_data_t; -#elif BX_PLATFORM_IOS - -/** - * - */ -BGFX_C_API void bgfx_ios_set_eagl_layer(void* _layer); - -#elif BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI -# include - -/** - * - */ -BGFX_C_API void bgfx_x11_set_display_window(Display* _display, Window _window); - -#elif BX_PLATFORM_NACL -# include -# include - -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 - -/** - * - */ -BGFX_C_API void bgfx_win_set_hwnd(HWND _window); - -#endif // BX_PLATFORM_ +BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd); #endif // BGFX_PLATFORM_C99_H_HEADER_GUARD diff --git a/include/bgfxplatform.h b/include/bgfxplatform.h index 4598fc1e..76294726 100644 --- a/include/bgfxplatform.h +++ b/include/bgfxplatform.h @@ -30,7 +30,18 @@ namespace bgfx /// allow creating separate rendering thread. If it is called before /// to bgfx::init, render thread won't be created by bgfx::init call. 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 # include @@ -38,7 +49,13 @@ 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 @@ -46,7 +63,13 @@ 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 @@ -55,7 +78,15 @@ 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 @@ -76,7 +107,14 @@ 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 @@ -86,7 +124,13 @@ 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 @@ -96,7 +140,13 @@ 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 diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 55a41dfb..b55d9568 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -19,56 +19,6 @@ namespace bgfx # define BGFX_CHECK_RENDER_THREAD() #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 void* TinyStlAllocator::static_allocate(size_t _bytes) { @@ -252,6 +202,13 @@ namespace bgfx static BX_THREAD uint32_t s_threadIndex = 0; static Context* s_ctx = NULL; 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) { @@ -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::TextureInfo) == sizeof(bgfx_texture_info_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) { @@ -3645,40 +3603,7 @@ BGFX_C_API bgfx_render_frame_t bgfx_render_frame() return bgfx_render_frame_t(bgfx::renderFrame() ); } -#if BX_PLATFORM_ANDROID -BGFX_C_API void bgfx_android_set_window(ANativeWindow* _window) +BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd) { - 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_* diff --git a/src/bgfx_p.h b/src/bgfx_p.h index c9e57421..8b9f12ea 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -217,22 +217,7 @@ namespace stl namespace bgfx { -#if BX_PLATFORM_ANDROID - 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_* + extern PlatformData g_platformData; #if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10) typedef uint16_t RenderItemCount; diff --git a/src/glcontext_eagl.mm b/src/glcontext_eagl.mm index 73eedd2b..1f1c0880 100644 --- a/src/glcontext_eagl.mm +++ b/src/glcontext_eagl.mm @@ -18,7 +18,7 @@ namespace bgfx { namespace gl void GlContext::create(uint32_t _width, uint32_t _height) { BX_UNUSED(_width, _height); - CAEAGLLayer* layer = (CAEAGLLayer*)g_bgfxEaglLayer; + CAEAGLLayer* layer = (CAEAGLLayer*)g_platformData.nwh; layer.opaque = true; layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 4bd42305..d1fccdd4 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -178,97 +178,91 @@ EGL_IMPORT 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 - ndt = GetDC(g_bgfxHwnd); - nwh = g_bgfxHwnd; -# elif BX_PLATFORM_LINUX - ndt = (EGLNativeDisplayType)g_bgfxX11Display; - nwh = (EGLNativeWindowType)g_bgfxX11Window; -# endif // BX_PLATFORM_ - m_display = eglGetDisplay(ndt); - BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display); + if (NULL == g_platformData.ndt) + { + ndt = GetDC( (HWND)g_platformData.nwh); + } +# endif // BX_PLATFORM_WINDOWS - EGLint major = 0; - EGLint minor = 0; - EGLBoolean success = eglInitialize(m_display, &major, &minor); - BGFX_FATAL(success && major >= 1 && minor >= 3, Fatal::UnableToInitialize, "Failed to initialize %d.%d", major, minor); + m_display = eglGetDisplay(ndt); + BGFX_FATAL(m_display != EGL_NO_DISPLAY, Fatal::UnableToInitialize, "Failed to create display %p", m_display); - EGLint attrs[] = - { - EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGLint major = 0; + EGLint minor = 0; + 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 - EGL_DEPTH_SIZE, 16, + EGL_DEPTH_SIZE, 16, # else - EGL_DEPTH_SIZE, 24, + EGL_DEPTH_SIZE, 24, # endif // BX_PLATFORM_ - EGL_STENCIL_SIZE, 8, + EGL_STENCIL_SIZE, 8, - EGL_NONE - }; + EGL_NONE + }; - EGLint numConfig = 0; - success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig); - BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig"); + EGLint numConfig = 0; + success = eglChooseConfig(m_display, attrs, &m_config, 1, &numConfig); + BGFX_FATAL(success, Fatal::UnableToInitialize, "eglChooseConfig"); # if BX_PLATFORM_ANDROID - EGLint format; - eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); - ANativeWindow_setBuffersGeometry(g_bgfxAndroidWindow, _width, _height, format); - nwh = g_bgfxAndroidWindow; + + EGLint format; + eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format); + ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format); + # elif BX_PLATFORM_RPI - DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0); - DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0); + DISPMANX_DISPLAY_HANDLE_T dispmanDisplay = vc_dispmanx_display_open(0); + DISPMANX_UPDATE_HANDLE_T dispmanUpdate = vc_dispmanx_update_start(0); - VC_RECT_T dstRect = { 0, 0, _width, _height }; - VC_RECT_T srcRect = { 0, 0, _width << 16, _height << 16 }; + VC_RECT_T dstRect = { 0, 0, _width, _height }; + VC_RECT_T srcRect = { 0, 0, _width << 16, _height << 16 }; - DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate - , dispmanDisplay - , 0 - , &dstRect - , 0 - , &srcRect - , DISPMANX_PROTECTION_NONE - , NULL - , NULL - , DISPMANX_NO_ROTATE - ); + DISPMANX_ELEMENT_HANDLE_T dispmanElement = vc_dispmanx_element_add(dispmanUpdate + , dispmanDisplay + , 0 + , &dstRect + , 0 + , &srcRect + , DISPMANX_PROTECTION_NONE + , NULL + , NULL + , DISPMANX_NO_ROTATE + ); - s_dispmanWindow.element = dispmanElement; - s_dispmanWindow.width = _width; - s_dispmanWindow.height = _height; - nwh = &s_dispmanWindow; + s_dispmanWindow.element = dispmanElement; + s_dispmanWindow.width = _width; + s_dispmanWindow.height = _height; + nwh = &s_dispmanWindow; - vc_dispmanx_update_submit_sync(dispmanUpdate); + vc_dispmanx_update_submit_sync(dispmanUpdate); # endif // BX_PLATFORM_ANDROID - m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); - BGFX_FATAL(m_surface != EGL_NO_SURFACE, Fatal::UnableToInitialize, "Failed to create surface."); + m_surface = eglCreateWindowSurface(m_display, m_config, nwh, NULL); + 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); - BGFX_FATAL(m_context != EGL_NO_CONTEXT, Fatal::UnableToInitialize, "Failed to create context."); + 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."); - success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); - BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); - m_current = NULL; + success = eglMakeCurrent(m_display, m_surface, m_surface, m_context); + BGFX_FATAL(success, Fatal::UnableToInitialize, "Failed to set context."); + m_current = NULL; - eglSwapInterval(m_display, 0); -# if BX_PLATFORM_ANDROID + eglSwapInterval(m_display, 0); } -# endif + # if BX_PLATFORM_EMSCRIPTEN emscripten_set_canvas_size(_width, _height); # endif // BX_PLATFORM_EMSCRIPTEN @@ -303,7 +297,7 @@ EGL_IMPORT { EGLint 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 diff --git a/src/glcontext_glx.cpp b/src/glcontext_glx.cpp index 17ee8e82..270e14db 100644 --- a/src/glcontext_glx.cpp +++ b/src/glcontext_glx.cpp @@ -29,23 +29,23 @@ namespace bgfx { namespace gl SwapChainGL(::Window _window, XVisualInfo* _visualInfo, GLXContext _context) : 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() { - glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0); - glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); + glXMakeCurrent( (::Display*)g_platformData.ndt, 0, 0); + glXDestroyContext( (::Display*)g_platformData.ndt, m_context); } void makeCurrent() { - glXMakeCurrent( (::Display*)g_bgfxX11Display, m_window, m_context); + glXMakeCurrent( (::Display*)g_platformData.ndt, m_window, m_context); } void swapBuffers() { - glXSwapBuffers( (::Display*)g_bgfxX11Display, m_window); + glXSwapBuffers( (::Display*)g_platformData.ndt, m_window); } Window m_window; @@ -56,14 +56,14 @@ namespace bgfx { namespace gl { 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; - 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( (major == 1 && minor >= 2) || major > 1 , Fatal::UnableToInitialize @@ -72,9 +72,9 @@ namespace bgfx { namespace gl , 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:"); dumpExtensions(extensions); @@ -96,13 +96,13 @@ namespace bgfx { namespace gl GLXFBConfig bestConfig = NULL; 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); 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) { 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) { 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)" , ii , numConfigs @@ -146,7 +146,7 @@ namespace bgfx { namespace gl BGFX_FATAL(m_visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration."); 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."); #if BGFX_CONFIG_RENDERER_OPENGL >= 31 @@ -163,11 +163,11 @@ namespace bgfx { namespace gl 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) { - glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); + glXDestroyContext( (::Display*)g_platformData.ndt, m_context); m_context = context; } } @@ -175,19 +175,19 @@ namespace bgfx { namespace gl BX_UNUSED(bestConfig); #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31 - XUnlockDisplay( (::Display*)g_bgfxX11Display); + XUnlockDisplay( (::Display*)g_platformData.ndt); } import(); - glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); + glXMakeCurrent( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, m_context); m_current = NULL; glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress( (const GLubyte*)"glXSwapIntervalEXT"); if (NULL != glXSwapIntervalEXT) { BX_TRACE("Using glXSwapIntervalEXT."); - glXSwapIntervalEXT( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, 0); + glXSwapIntervalEXT( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, 0); } else { @@ -210,15 +210,15 @@ namespace bgfx { namespace gl glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); + glXSwapBuffers( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh); } void GlContext::destroy() { - glXMakeCurrent( (::Display*)g_bgfxX11Display, 0, 0); - if (NULL == g_bgfxGLX) + glXMakeCurrent( (::Display*)g_platformData.ndt, 0, 0); + if (NULL == g_platformData.context) { - glXDestroyContext( (::Display*)g_bgfxX11Display, m_context); + glXDestroyContext( (::Display*)g_platformData.ndt, m_context); XFree(m_visualInfo); } m_context = NULL; @@ -232,7 +232,7 @@ namespace bgfx { namespace gl 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) { @@ -265,7 +265,7 @@ namespace bgfx { namespace gl if (NULL == _swapChain) { - glXSwapBuffers( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window); + glXSwapBuffers( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh); } else { @@ -281,7 +281,7 @@ namespace bgfx { namespace gl if (NULL == _swapChain) { - glXMakeCurrent( (::Display*)g_bgfxX11Display, (::Window)g_bgfxX11Window, m_context); + glXMakeCurrent( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh, m_context); } else { diff --git a/src/glcontext_nsgl.mm b/src/glcontext_nsgl.mm index 23680922..d39f7da3 100644 --- a/src/glcontext_nsgl.mm +++ b/src/glcontext_nsgl.mm @@ -65,10 +65,10 @@ namespace bgfx { namespace gl BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!"); const AutoreleasePoolHolder pool; - NSWindow* nsWindow = (NSWindow*)g_bgfxNSWindow; - m_context = g_bgfxNSGL; + NSWindow* nsWindow = (NSWindow*)g_platformData.nwh; + 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) NSOpenGLPixelFormatAttribute profile = @@ -120,7 +120,7 @@ namespace bgfx { namespace gl void GlContext::destroy() { - if (NULL == g_bgfxNSGL) + if (NULL == g_platformData.context) { NSOpenGLView* glView = (NSOpenGLView*)m_view; [glView release]; diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index 19ca6d99..23deb174 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -109,14 +109,14 @@ namespace bgfx { namespace gl 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 + // If g_platformHooks.nwh 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 " + BX_WARN(NULL != g_platformData.nwh + , "bgfx::setPlatform with valid window is not called. This might " "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"); BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent."); @@ -127,7 +127,7 @@ namespace bgfx { namespace gl wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "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!"); // Dummy window to peek into WGL functionality. @@ -272,14 +272,14 @@ namespace bgfx { namespace gl void GlContext::destroy() { - if (NULL != g_bgfxHwnd) + if (NULL != g_platformData.nwh) { wglMakeCurrent(NULL, NULL); wglDeleteContext(m_context); m_context = NULL; - ReleaseDC(g_bgfxHwnd, m_hdc); + ReleaseDC( (HWND)g_platformData.nwh, m_hdc); m_hdc = NULL; } @@ -324,7 +324,7 @@ namespace bgfx { namespace gl if (NULL == _swapChain) { - if (NULL != g_bgfxHwnd) + if (NULL != g_platformData.nwh) { SwapBuffers(m_hdc); } diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 87591b03..2ff11614 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -430,21 +430,39 @@ namespace bgfx { namespace d3d11 struct RendererContextD3D11 : public RendererContextI { 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_numWindows(0) + , m_device(NULL) + , m_deviceCtx(NULL) , m_backBufferColor(NULL) , m_backBufferDepthStencil(NULL) + , m_currentColor(NULL) + , m_currentDepthStencil(NULL) , m_captureTexture(NULL) , m_captureResolve(NULL) , m_wireframe(false) , m_flags(BGFX_RESET_NONE) , m_maxAnisotropy(1) + , m_currentProgram(NULL) , m_vsChanges(0) , m_fsChanges(0) , m_rtMsaa(false) , m_ovrRtv(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() @@ -509,114 +527,125 @@ namespace bgfx { namespace d3d11 BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create DXGI factory."); #endif // BX_PLATFORM_WINRT - m_adapter = NULL; - m_driverType = D3D_DRIVER_TYPE_HARDWARE; + m_device = (ID3D11Device*)g_platformData.context; - IDXGIAdapter* adapter; - for (uint32_t ii = 0 - ; DXGI_ERROR_NOT_FOUND != factory->EnumAdapters(ii, &adapter) && ii < BX_COUNTOF(g_caps.gpu) - ; ++ii - ) + if (NULL == m_device) { - DXGI_ADAPTER_DESC desc; - hr = adapter->GetDesc(&desc); - if (SUCCEEDED(hr) ) + m_adapter = NULL; + m_driverType = D3D_DRIVER_TYPE_HARDWARE; + + 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); - - char description[BX_COUNTOF(desc.Description)]; - 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) + DXGI_ADAPTER_DESC desc; + hr = adapter->GetDesc(&desc); + if (SUCCEEDED(hr) ) { - if ( (BGFX_PCI_ID_NONE != g_caps.vendorId || 0 != g_caps.deviceId) - && (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; - } + BX_TRACE("Adapter #%d", ii); - if (BX_ENABLED(BGFX_CONFIG_DEBUG_PERFHUD) - && 0 != strstr(description, "PerfHUD") ) + char description[BX_COUNTOF(desc.Description)]; + 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; - m_driverType = D3D_DRIVER_TYPE_REFERENCE; + if ( (BGFX_PCI_ID_NONE != g_caps.vendorId || 0 != g_caps.deviceId) + && (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); - } - 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) ) + D3D_FEATURE_LEVEL features[] = { - // Try without debug in case D3D11 SDK Layers - // is not present? - flags &= ~D3D11_CREATE_DEVICE_DEBUG; - continue; + 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 + // 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."); - - if (NULL != m_adapter) + else { - 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; 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.deviceId = (uint16_t)m_adapterDesc.DeviceId; + if (NULL == g_platformData.backbuffer) + { #if BX_PLATFORM_WINRT - hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&m_factory); - BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); - DX_RELEASE(adapter, 2); + hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&m_factory); + BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); + DX_RELEASE(adapter, 2); - memset(&m_scd, 0, sizeof(m_scd) ); - m_scd.Width = BGFX_DEFAULT_WIDTH; - m_scd.Height = BGFX_DEFAULT_HEIGHT; - m_scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - m_scd.Stereo = false; - m_scd.SampleDesc.Count = 1; - m_scd.SampleDesc.Quality = 0; - m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - m_scd.BufferCount = 2; - m_scd.Scaling = DXGI_SCALING_NONE; - m_scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; - m_scd.AlphaMode = DXGI_ALPHA_MODE_IGNORE; + memset(&m_scd, 0, sizeof(m_scd) ); + m_scd.Width = BGFX_DEFAULT_WIDTH; + m_scd.Height = BGFX_DEFAULT_HEIGHT; + m_scd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + m_scd.Stereo = false; + m_scd.SampleDesc.Count = 1; + m_scd.SampleDesc.Quality = 0; + m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + m_scd.BufferCount = 2; + m_scd.Scaling = DXGI_SCALING_NONE; + m_scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; + m_scd.AlphaMode = DXGI_ALPHA_MODE_IGNORE; - hr = m_factory->CreateSwapChainForCoreWindow(m_device - , g_bgfxCoreWindow - , &m_scd - , NULL - , &m_swapChain - ); - BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); + hr = m_factory->CreateSwapChainForCoreWindow(m_device + , (::IUnknown*)g_platformData.nwh + , &m_scd + , NULL + , &m_swapChain + ); + BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); #else - hr = adapter->GetParent(IID_IDXGIFactory, (void**)&m_factory); - BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); - DX_RELEASE(adapter, 2); + hr = adapter->GetParent(IID_IDXGIFactory, (void**)&m_factory); + BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Unable to create Direct3D11 device."); + DX_RELEASE(adapter, 2); - memset(&m_scd, 0, sizeof(m_scd) ); - m_scd.BufferDesc.Width = BGFX_DEFAULT_WIDTH; - m_scd.BufferDesc.Height = BGFX_DEFAULT_HEIGHT; - m_scd.BufferDesc.RefreshRate.Numerator = 60; - m_scd.BufferDesc.RefreshRate.Denominator = 1; - m_scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - m_scd.SampleDesc.Count = 1; - m_scd.SampleDesc.Quality = 0; - m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - m_scd.BufferCount = 1; - m_scd.OutputWindow = g_bgfxHwnd; - m_scd.Windowed = true; + memset(&m_scd, 0, sizeof(m_scd) ); + m_scd.BufferDesc.Width = BGFX_DEFAULT_WIDTH; + m_scd.BufferDesc.Height = BGFX_DEFAULT_HEIGHT; + m_scd.BufferDesc.RefreshRate.Numerator = 60; + m_scd.BufferDesc.RefreshRate.Denominator = 1; + m_scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + m_scd.SampleDesc.Count = 1; + m_scd.SampleDesc.Quality = 0; + m_scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + m_scd.BufferCount = 1; + m_scd.OutputWindow = (HWND)g_platformData.nwh; + m_scd.Windowed = true; - hr = m_factory->CreateSwapChain(m_device - , &m_scd - , &m_swapChain - ); - BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); + hr = m_factory->CreateSwapChain(m_device + , &m_scd + , &m_swapChain + ); + BGFX_FATAL(SUCCEEDED(hr), Fatal::UnableToInitialize, "Failed to create swap chain."); - DX_CHECK(m_factory->MakeWindowAssociation(g_bgfxHwnd, 0 - | DXGI_MWA_NO_WINDOW_CHANGES - | DXGI_MWA_NO_ALT_ENTER - ) ); + DX_CHECK(m_factory->MakeWindowAssociation((HWND)g_platformData.nwh, 0 + | DXGI_MWA_NO_WINDOW_CHANGES + | DXGI_MWA_NO_ALT_ENTER + )); #endif // BX_PLATFORM_WINRT + } + else + { + memset(&m_scd, 0, sizeof(m_scd) ); + m_backBufferColor = (ID3D11RenderTargetView*)g_platformData.backbuffer; + } m_numWindows = 1; @@ -1147,6 +1184,12 @@ BX_PRAGMA_DIAGNOSTIC_POP(); 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; DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backBuffer)); @@ -1317,7 +1360,11 @@ BX_PRAGMA_DIAGNOSTIC_POP(); ovrPreReset(); 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) { @@ -1331,11 +1378,14 @@ BX_PRAGMA_DIAGNOSTIC_POP(); void postReset() { - ID3D11Texture2D* color; - DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&color)); + if (NULL != m_swapChain) + { + ID3D11Texture2D* color; + DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&color)); - DX_CHECK(m_device->CreateRenderTargetView(color, NULL, &m_backBufferColor) ); - DX_RELEASE(color, 0); + DX_CHECK(m_device->CreateRenderTargetView(color, NULL, &m_backBufferColor) ); + DX_RELEASE(color, 0); + } ovrPostReset(); @@ -1502,38 +1552,41 @@ BX_PRAGMA_DIAGNOSTIC_POP(); preReset(); - if (resize) + if (NULL != m_swapChain) { - DX_CHECK(m_swapChain->ResizeBuffers(2 - , getBufferWidth() - , getBufferHeight() - , getBufferFormat() - , DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH - ) ); - } - else - { - updateMsaa(); - m_scd.SampleDesc = s_msaa[(m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT]; + if (resize) + { + DX_CHECK(m_swapChain->ResizeBuffers(2 + , getBufferWidth() + , getBufferHeight() + , getBufferFormat() + , DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH + )); + } + else + { + 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 - HRESULT hr; - hr = m_factory->CreateSwapChainForCoreWindow(m_device - , g_bgfxCoreWindow - , &m_scd - , NULL - , &m_swapChain - ); + HRESULT hr; + hr = m_factory->CreateSwapChainForCoreWindow(m_device + , (::IUnknown*)g_platformData.nwh + , &m_scd + , NULL + , &m_swapChain + ); #else - HRESULT hr; - hr = m_factory->CreateSwapChain(m_device - , &m_scd - , &m_swapChain - ); + HRESULT hr; + hr = m_factory->CreateSwapChain(m_device + , &m_scd + , &m_swapChain + ); #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(); @@ -2042,7 +2095,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); config.D3D11.pDeviceContext = m_deviceCtx; config.D3D11.pBackBufferRT = m_backBufferColor; 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); const Memory* mem = alloc(size); @@ -2404,32 +2457,30 @@ BX_PRAGMA_DIAGNOSTIC_POP(); } } -#if USE_D3D11_DYNAMIC_LIB void* m_d3d9dll; void* m_d3d11dll; void* m_dxgidll; -#endif // USE_D3D11_DYNAMIC_LIB void* m_renderdocdll; - D3D_DRIVER_TYPE m_driverType; + D3D_DRIVER_TYPE m_driverType; D3D_FEATURE_LEVEL m_featureLevel; - IDXGIAdapter* m_adapter; + IDXGIAdapter* m_adapter; DXGI_ADAPTER_DESC m_adapterDesc; #if BX_PLATFORM_WINRT - IDXGIFactory2* m_factory; - IDXGISwapChain1* m_swapChain; + IDXGIFactory2* m_factory; + IDXGISwapChain1* m_swapChain; #else - IDXGIFactory* m_factory; - IDXGISwapChain* m_swapChain; -#endif + IDXGIFactory* m_factory; + IDXGISwapChain* m_swapChain; +#endif // BX_PLATFORM_WINRT uint16_t m_lost; uint16_t m_numWindows; FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS]; - ID3D11Device* m_device; - ID3D11DeviceContext* m_deviceCtx; + ID3D11Device* m_device; + ID3D11DeviceContext* m_deviceCtx; ID3D11RenderTargetView* m_backBufferColor; ID3D11DepthStencilView* m_backBufferDepthStencil; ID3D11RenderTargetView* m_currentColor; diff --git a/src/renderer_d3d11.h b/src/renderer_d3d11.h index 0376ae9f..68c8e5b5 100644 --- a/src/renderer_d3d11.h +++ b/src/renderer_d3d11.h @@ -19,10 +19,10 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wpragmas"); BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4005) // warning C4005: '' : macro redefinition #define D3D11_NO_HELPERS #if BX_PLATFORM_WINRT -#include +# include #else -#include -#endif +# include +#endif // BX_PLATFORM_WINRT BX_PRAGMA_DIAGNOSTIC_POP() #include "renderer.h" diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index faf5f98a..828ad777 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -272,6 +272,7 @@ namespace bgfx { namespace d3d9 RendererContextD3D9() : m_d3d9(NULL) , m_device(NULL) + , m_swapChain(NULL) , m_captureTexture(NULL) , m_captureSurface(NULL) , m_captureResolve(NULL) @@ -316,7 +317,7 @@ namespace bgfx { namespace d3d9 m_params.Windowed = true; RECT rect; - GetWindowRect(g_bgfxHwnd, &rect); + GetWindowRect( (HWND)g_platformData.nwh, &rect); m_params.BackBufferWidth = rect.right-rect.left; m_params.BackBufferHeight = rect.bottom-rect.top; @@ -417,7 +418,7 @@ namespace bgfx { namespace d3d9 #if 0 // BGFX_CONFIG_RENDERER_DIRECT3D9EX DX_CHECK(m_d3d9->CreateDeviceEx(m_adapter , m_deviceType - , g_bgfxHwnd + , g_platformHooks.nwh , behaviorFlags[ii] , &m_params , NULL @@ -426,7 +427,7 @@ namespace bgfx { namespace d3d9 #else DX_CHECK(m_d3d9->CreateDevice(m_adapter , m_deviceType - , g_bgfxHwnd + , (HWND)g_platformData.nwh , behaviorFlags[ii] , &m_params , &m_device @@ -860,11 +861,11 @@ namespace bgfx { namespace d3d9 ) ); RECT rc; - GetClientRect(g_bgfxHwnd, &rc); + GetClientRect( (HWND)g_platformData.nwh, &rc); POINT point; point.x = rc.left; point.y = rc.top; - ClientToScreen(g_bgfxHwnd, &point); + ClientToScreen( (HWND)g_platformData.nwh, &point); uint8_t* data = (uint8_t*)rect.pBits; uint32_t bytesPerPixel = rect.Pitch/dm.Width; @@ -1146,7 +1147,7 @@ namespace bgfx { namespace d3d9 void flip(HMD& /*_hmd*/) BX_OVERRIDE { - if (NULL != m_device) + if (NULL != m_swapChain) { #if BGFX_CONFIG_RENDERER_DIRECT3D9EX if (NULL != m_deviceEx) @@ -1160,7 +1161,7 @@ namespace bgfx { namespace d3d9 HRESULT hr; 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 { diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 6d4eabe5..2cf109f1 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -2239,9 +2239,9 @@ namespace bgfx { namespace gl config.OGL.Header.RTSize.h = m_resolution.m_height; # endif // OVR_VERSION > OVR_VERSION_043 config.OGL.Header.Multisample = 0; - config.OGL.Window = g_bgfxHwnd; - config.OGL.DC = GetDC(g_bgfxHwnd); - if (m_ovr.postReset(g_bgfxHwnd, &config.Config, !!(m_resolution.m_flags & BGFX_RESET_HMD_DEBUG) ) ) + config.OGL.Window = g_platformData.nwh; + config.OGL.DC = GetDC(g_platformData.nwh); + 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); const Memory* mem = alloc(size);