diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 5ee89e3a7..902d90e32 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -289,8 +289,8 @@ namespace bgfx const float texelWidthHalf = texelWidth*0.5f; const float texelHeight = 1.0f/24.0f; const float texelHeightHalf = texelHeight*0.5f; - const float utop = (_mem.m_small ? 0.0f : 8.0f)*texelHeight - texelHeightHalf; - const float ubottom = (_mem.m_small ? 8.0f : 24.0f)*texelHeight - texelHeightHalf; + const float utop = (_mem.m_small ? 0.0f : 8.0f)*texelHeight + texelHeightHalf; + const float ubottom = (_mem.m_small ? 8.0f : 24.0f)*texelHeight + texelHeightHalf; const float fontHeight = (_mem.m_small ? 8.0f : 16.0f); setup(); @@ -313,7 +313,7 @@ namespace bgfx uint8_t attr = line[1]; if (0 != (ch|attr) - && (' ' != ch || 0 != (attr&0xf0) ) ) + && (' ' != ch || 0 != (attr&0xf0) ) ) { uint32_t fg = palette[attr&0xf]; uint32_t bg = palette[(attr>>4)&0xf]; @@ -492,11 +492,8 @@ namespace bgfx void renderFrame() { - if (s_ctx.m_initialized) - { - BGFX_RENDER_THREAD(); - s_ctx.renderFrame(); - } + BGFX_RENDER_THREAD(); + s_ctx.renderFrame(); } static const uint32_t s_attribTypeSize[AttribType::Count] = @@ -649,7 +646,6 @@ namespace bgfx memset(m_seqMask, 0, sizeof(m_seqMask) ); gameSemPost(); - m_initialized = true; getCommandBuffer(CommandBuffer::RendererInit); @@ -669,7 +665,6 @@ namespace bgfx getCommandBuffer(CommandBuffer::RendererShutdown); frame(); - m_initialized = false; #if BGFX_CONFIG_MULTITHREADED if (NULL != m_renderThread) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index fee1dadd9..a2cb5fed6 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -56,6 +56,7 @@ extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format # include extern HWND g_bgfxHwnd; #elif BX_PLATFORM_XBOX360 +# include # include #endif // BX_PLATFORM_WINDOWS @@ -1402,7 +1403,6 @@ namespace bgfx , m_uniformHandle(BGFX_CONFIG_MAX_UNIFORMS) , m_frames(0) , m_debug(BGFX_DEBUG_NONE) - , m_initialized(false) , m_rendererInitialized(false) { } @@ -1962,14 +1962,7 @@ namespace bgfx // render thread void renderFrame() { -#if BX_PLATFORM_NACL -// on NaCl swap buffers generates callback and this is handled inside callback -#else - if (m_rendererInitialized) - { - flip(); - } -#endif // BX_PLATFORM_ + flip(); gameSemWait(); @@ -2549,7 +2542,6 @@ namespace bgfx Window m_window; #endif // BX_PLATFORM_WINDOWS - bool m_initialized; bool m_rendererInitialized; }; diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 2f75c774f..e8d6d77d1 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -337,34 +337,37 @@ namespace bgfx void flip() { + if (NULL != m_device) + { #if BGFX_CONFIG_RENDERER_DIRECT3D_EX - DX_CHECK(m_device->WaitForVBlank(0) ); + DX_CHECK(m_device->WaitForVBlank(0) ); #endif // BGFX_CONFIG_RENDERER_DIRECT3D_EX - HRESULT hr; - hr = m_device->Present(NULL, NULL, NULL, NULL); + HRESULT hr; + hr = m_device->Present(NULL, NULL, NULL, NULL); #if BX_PLATFORM_WINDOWS - if (isLost(hr) ) - { - do + if (isLost(hr) ) { - do + do { + do + { + hr = m_device->TestCooperativeLevel(); + } + while (D3DERR_DEVICENOTRESET != hr); + + reset(); hr = m_device->TestCooperativeLevel(); } - while (D3DERR_DEVICENOTRESET != hr); - - reset(); - hr = m_device->TestCooperativeLevel(); + while (FAILED(hr) ); + } + else if (FAILED(hr) ) + { + BX_TRACE("Present failed with err 0x%08x.", hr); } - while (FAILED(hr) ); - } - else if (FAILED(hr) ) - { - BX_TRACE("Present failed with err 0x%08x.", hr); - } #endif // BX_PLATFORM_ + } } void preReset() diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 927851f6b..c614ac087 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -16,10 +16,33 @@ namespace bgfx { + typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); + +#if BX_PLATFORM_NACL + void naclSwapCompleteCb(void* _data, int32_t _result); + + PP_CompletionCallback naclSwapComplete = + { + naclSwapCompleteCb, + NULL, + PP_COMPLETIONCALLBACK_FLAG_NONE + }; +#endif // BX_PLATFORM_NACL + struct RendererContext { RendererContext() : m_dxtSupport(false) + , m_postSwapBuffers(NULL) +#if BX_PLATFORM_NACL + , m_context(0) + , m_instance(0) + , m_instInterface(NULL) + , m_graphicsInterface(NULL) +#elif BX_PLATFORM_WINDOWS + , m_hdc(NULL) + , m_hglrc(NULL) +#endif // BX_PLATFORM_ { memset(&m_resolution, 0, sizeof(m_resolution) ); } @@ -35,12 +58,99 @@ namespace bgfx m_resolution = _resolution; #if BX_PLATFORM_NACL - extern void naclSetRenderContextSize(uint32_t _width, uint32_t _height); - naclSetRenderContextSize(_resolution.m_width, _resolution.m_height); + setRenderContextSize(_resolution.m_width, _resolution.m_height); #endif // BX_PLATFORM_NACL } } + void setRenderContextSize(uint32_t _width, uint32_t _height) + { + BX_TRACE("1"); + if (_width != 0 + || _height != 0) + { +#if BX_PLATFORM_NACL + if (0 == m_context) + { + BX_TRACE("create context"); + + int32_t attribs[] = + { + PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, + PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24, + PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8, + PP_GRAPHICS3DATTRIB_SAMPLES, 0, + PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, + PP_GRAPHICS3DATTRIB_WIDTH, _width, + PP_GRAPHICS3DATTRIB_HEIGHT, _height, + PP_GRAPHICS3DATTRIB_NONE + }; + + m_context = m_graphicsInterface->Create(m_instance, 0, attribs); + m_instInterface->BindGraphics(m_instance, m_context); + glSetCurrentContextPPAPI(m_context); + m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete); + } + else + { + m_graphicsInterface->ResizeBuffers(m_context, _width, _height); + } + +#elif BX_PLATFORM_WINDOWS + if (NULL == m_hdc) + { + PIXELFORMATDESCRIPTOR pfd = + { + sizeof(PIXELFORMATDESCRIPTOR), + 1, + PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, + PFD_TYPE_RGBA, + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 16, 0, 0, + PFD_MAIN_PLANE, + 0, 0, 0, 0 + }; + + m_hdc = GetDC(g_bgfxHwnd); + + int pixelFormat = ChoosePixelFormat(m_hdc, &pfd); + BX_CHECK(0 != pixelFormat, "ChoosePixelFormat failed!"); + + int result; + result = SetPixelFormat(m_hdc, pixelFormat, &pfd); + BX_CHECK(0 != result, "SetPixelFormat failed!"); + + m_hglrc = wglCreateContext(m_hdc); + BX_CHECK(NULL != g_hglrc, "wglCreateContext failed!"); + + result = wglMakeCurrent(m_hdc, m_hglrc); + BX_CHECK(0 != result, "wglMakeCurrent failed!"); + } +#endif // BX_PLATFORM_ + } + } + + void flip() + { +#if BX_PLATFORM_NACL + glSetCurrentContextPPAPI(m_context); + m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete); +#elif BX_PLATFORM_WINDOWS + wglMakeCurrent(m_hdc, m_hglrc); + SwapBuffers(m_hdc); +#endif // BX_PLATFORM_ + + if (NULL != m_postSwapBuffers) + { + m_postSwapBuffers(m_resolution.m_width, m_resolution.m_height); + } + } + + void init() + { + setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT); + } + IndexBuffer m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS]; VertexBuffer m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS]; Shader m_vertexShaders[BGFX_CONFIG_MAX_VERTEX_SHADERS]; @@ -56,10 +166,38 @@ namespace bgfx Resolution m_resolution; bool m_dxtSupport; + + PostSwapBuffersFn m_postSwapBuffers; + +#if BX_PLATFORM_NACL + PP_Resource m_context; + PP_Instance m_instance; + const PPB_Instance* m_instInterface; + const PPB_Graphics3D* m_graphicsInterface; +#elif BX_PLATFORM_WINDOWS + HDC m_hdc; + HGLRC m_hglrc; +#endif // BX_PLATFORM_NACL }; RendererContext s_renderCtx; +#if BX_PLATFORM_NACL + void naclSetIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers) + { + s_renderCtx.m_instance = _instance; + s_renderCtx.m_instInterface = _instInterface; + s_renderCtx.m_graphicsInterface = _graphicsInterface; + s_renderCtx.m_postSwapBuffers = _postSwapBuffers; + s_renderCtx.setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT); + } + + void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/) + { + renderFrame(); + } +#endif // BX_PLATFORM_NACL + struct Extension { enum Enum @@ -775,6 +913,8 @@ namespace bgfx m_depth.destroy(); } + static bool s_exit = false; + void ConstantBuffer::commit(bool _force) { reset(); @@ -855,63 +995,6 @@ namespace bgfx } while (true); } -#if BX_PLATFORM_WINDOWS - HDC g_hdc; - HGLRC g_hglrc; -#endif // BX_PLATFORM_WINDOWS - - void initGl(uint32_t _width, uint32_t _height) - { -#if BX_PLATFORM_WINDOWS - static PIXELFORMATDESCRIPTOR pfd = - { - sizeof(PIXELFORMATDESCRIPTOR), - 1, - PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, - PFD_TYPE_RGBA, - 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 16, 0, 0, - PFD_MAIN_PLANE, - 0, 0, 0, 0 - }; - - g_hdc = GetDC(hwnd); - - int pixelFormat = ChoosePixelFormat(g_hdc, &pfd); - BX_CHECK(0 != pixelFormat, "ChoosePixelFormat failed!"); - - int result; - result = SetPixelFormat(g_hdc, pixelFormat, &pfd); - BX_CHECK(0 != result, "SetPixelFormat failed!"); - - g_hglrc = wglCreateContext(g_hdc); - BX_CHECK(NULL != g_hglrc, "wglCreateContext failed!"); - - result = wglMakeCurrent(g_hdc, g_hglrc); - BX_CHECK(0 != result, "wglMakeCurrent failed!"); - - glewInit(); -#endif // BX_PLATFORM_WINDOWS - } - - static bool s_exit = false; - -#if BX_PLATFORM_WINDOWS - DWORD WINAPI renderThread(LPVOID _arg) - { - wglMakeCurrent(g_hdc, g_hglrc); - - while (!s_exit) - { - renderFrame(); - } - - s_exit = false; - - return EXIT_SUCCESS; - } -#endif // BX_PLATFORM_WINDOWS - void TextVideoMemBlitter::setup() { uint32_t width = s_renderCtx.m_resolution.m_width; @@ -967,10 +1050,7 @@ namespace bgfx void Context::flip() { -#if BX_PLATFORM_WINDOWS - wglMakeCurrent(g_hdc, g_hglrc); - SwapBuffers(g_hdc); -#endif // BX_PLATFORM_WINDOWS + s_renderCtx.flip(); } GLint glGet(GLenum _pname) @@ -982,6 +1062,8 @@ namespace bgfx void Context::rendererInit() { + s_renderCtx.init(); + #if BGFX_DEBUG GLint numCmpFormats; GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCmpFormats) ); diff --git a/src/renderer_gl.h b/src/renderer_gl.h index da4f0588b..6b948e64d 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -6,7 +6,13 @@ #ifndef __RENDERER_GL_H__ #define __RENDERER_GL_H__ -#if BX_PLATFORM_NACL || BX_PLATFORM_ANDROID +#if BX_PLATFORM_NACL +# include +# include +# include +# include +# include +#elif BX_PLATFORM_ANDROID # include #elif BX_PLATFORM_WINDOWS # include