From 57f61b7e3255f61ed32f9957fe4c68e83c54ef8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 21 May 2015 16:45:24 -0700 Subject: [PATCH] Added ability to run in single-thread mode without rebuild. --- src/bgfx.cpp | 21 +++++++++++++-------- src/bgfx_p.h | 34 ++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 42daed16..235e3978 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -7,7 +7,7 @@ namespace bgfx { -#define BGFX_MAIN_THREAD_MAGIC 0x78666762 +#define BGFX_MAIN_THREAD_MAGIC UINT32_C(0x78666762) #if BGFX_CONFIG_MULTITHREADED && !BX_PLATFORM_OSX && !BX_PLATFORM_IOS # define BGFX_CHECK_MAIN_THREAD() \ @@ -353,7 +353,6 @@ namespace bgfx void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem& _mem) { - BGFX_CHECK_RENDER_THREAD(); struct Vertex { float m_x; @@ -762,6 +761,7 @@ namespace bgfx if (NULL == s_ctx) { s_renderFrameCalled = true; + s_threadIndex = ~BGFX_MAIN_THREAD_MAGIC; return RenderFrame::NoContext; } @@ -919,16 +919,21 @@ namespace bgfx // When bgfx::renderFrame is called before init render thread // should not be created. BX_TRACE("Application called bgfx::renderFrame directly, not creating render thread."); + m_singleThreaded = ~BGFX_MAIN_THREAD_MAGIC == s_threadIndex; } else { BX_TRACE("Creating rendering thread."); m_thread.init(renderThread, this); + m_singleThreaded = false; } #else BX_TRACE("Multithreaded renderer is disabled."); + m_singleThreaded = true; #endif // BGFX_CONFIG_MULTITHREADED + s_threadIndex = BGFX_MAIN_THREAD_MAGIC; + for (uint32_t ii = 0; ii < BX_COUNTOF(m_viewRemap); ++ii) { m_viewRemap[ii] = uint8_t(ii); @@ -977,6 +982,10 @@ namespace bgfx g_caps.rendererType = m_renderCtx->getRendererType(); initAttribTypeSizeTable(g_caps.rendererType); + g_caps.supported |= 0 + | (BX_ENABLED(BGFX_CONFIG_MULTITHREADED) && !m_singleThreaded ? BGFX_CAPS_RENDERER_MULTITHREADED : 0) + ; + dumpCaps(); m_textVideoMemBlitter.init(); @@ -1166,7 +1175,8 @@ namespace bgfx bx::xchg(m_render, m_submit); - if (!BX_ENABLED(BGFX_CONFIG_MULTITHREADED) ) + if (!BX_ENABLED(BGFX_CONFIG_MULTITHREADED) + || m_singleThreaded) { renderFrame(); } @@ -1986,9 +1996,6 @@ again: BX_TRACE("Init..."); memset(&g_caps, 0, sizeof(g_caps) ); - g_caps.supported = 0 - | (BGFX_CONFIG_MULTITHREADED ? BGFX_CAPS_RENDERER_MULTITHREADED : 0) - ; g_caps.maxViews = BGFX_CONFIG_MAX_VIEWS; g_caps.maxDrawCalls = BGFX_CONFIG_MAX_DRAW_CALLS; g_caps.maxFBAttachments = 1; @@ -2016,8 +2023,6 @@ again: s_callbackStub = BX_NEW(g_allocator, CallbackStub); } - s_threadIndex = BGFX_MAIN_THREAD_MAGIC; - s_ctx = BX_ALIGNED_NEW(g_allocator, Context, 16); s_ctx->init(_type); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index c098f743..22410045 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1910,6 +1910,7 @@ namespace bgfx , m_rendererInitialized(false) , m_exit(false) , m_flipAfterRender(false) + , m_singleThreaded(false) { } @@ -3446,28 +3447,40 @@ namespace bgfx #if BGFX_CONFIG_MULTITHREADED void gameSemPost() { - m_gameSem.post(); + if (!m_singleThreaded) + { + m_gameSem.post(); + } } void gameSemWait() { - int64_t start = bx::getHPCounter(); - bool ok = m_gameSem.wait(); - BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok); - m_render->m_waitSubmit = bx::getHPCounter()-start; + if (!m_singleThreaded) + { + int64_t start = bx::getHPCounter(); + bool ok = m_gameSem.wait(); + BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok); + m_render->m_waitSubmit = bx::getHPCounter()-start; + } } void renderSemPost() { - m_renderSem.post(); + if (!m_singleThreaded) + { + m_renderSem.post(); + } } void renderSemWait() { - int64_t start = bx::getHPCounter(); - bool ok = m_renderSem.wait(); - BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok); - m_submit->m_waitRender = bx::getHPCounter() - start; + if (!m_singleThreaded) + { + int64_t start = bx::getHPCounter(); + bool ok = m_renderSem.wait(); + BX_CHECK(ok, "Semaphore wait failed."); BX_UNUSED(ok); + m_submit->m_waitRender = bx::getHPCounter() - start; + } } bx::Semaphore m_renderSem; @@ -3598,6 +3611,7 @@ namespace bgfx bool m_rendererInitialized; bool m_exit; bool m_flipAfterRender; + bool m_singleThreaded; typedef UpdateBatchT<256> TextureUpdateBatch; BX_ALIGN_DECL_CACHE_LINE(TextureUpdateBatch m_textureUpdateBatch);