From 2cf5c45224fb846f6dae1861c2bb2f42635a3a4e Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Mon, 30 Sep 2013 20:09:09 -0700 Subject: [PATCH] Fixed NaCl initialization. --- src/bgfx.cpp | 9 ++- src/bgfx_p.h | 1 + src/glcontext_ppapi.cpp | 121 +++++++++++++++++++++++++++++++++++++--- src/glcontext_ppapi.h | 17 +----- src/renderer_gl.cpp | 67 +++++----------------- 5 files changed, 135 insertions(+), 80 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 9cb8e8ea..cdb1e873 100755 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -168,7 +168,12 @@ namespace bgfx bx::ReallocatorI* g_allocator = NULL; static BX_THREAD uint32_t s_threadIndex = 0; - static Context* s_ctx; + static Context* s_ctx = NULL; + + bool hasContext() + { + return NULL != s_ctx; + } void fatal(Fatal::Enum _code, const char* _format, ...) { @@ -611,8 +616,6 @@ namespace bgfx void init(CallbackI* _callback, bx::ReallocatorI* _allocator) { - BX_TRACE("Init"); - if (NULL != _allocator) { g_allocator = _allocator; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index fd69dca7..09958eb4 100755 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -3187,6 +3187,7 @@ namespace bgfx VertexShaderHandle m_vsh; FragmentShaderHandle m_fsh; }; + ProgramRef m_programRef[BGFX_CONFIG_MAX_PROGRAMS]; VertexDeclRef m_declRef; diff --git a/src/glcontext_ppapi.cpp b/src/glcontext_ppapi.cpp index 3ba6a622..fcecce54 100644 --- a/src/glcontext_ppapi.cpp +++ b/src/glcontext_ppapi.cpp @@ -6,6 +6,7 @@ #include "bgfx_p.h" #if BX_PLATFORM_NACL & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL) +# include # include "renderer_gl.h" namespace bgfx @@ -14,7 +15,9 @@ namespace bgfx # include "glimports.h" # undef GL_IMPORT - void naclSwapCompleteCb(void* _data, int32_t _result); + extern bool hasContext(); + + void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/); PP_CompletionCallback naclSwapComplete = { @@ -23,8 +26,95 @@ namespace bgfx PP_COMPLETIONCALLBACK_FLAG_NONE }; - void GlContext::create(uint32_t _width, uint32_t _height) + struct Ppapi { + Ppapi() + : m_context(0) + , m_instance(0) + , m_instInterface(NULL) + , m_graphicsInterface(NULL) + , m_instancedArrays(NULL) + , m_postSwapBuffers(NULL) + , m_forceSwap(true) + { + } + + void setIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers); + + void resize(uint32_t _width, uint32_t _height, bool /*_vsync*/) + { + m_graphicsInterface->ResizeBuffers(m_context, _width, _height); + } + + void swap() + { + glSetCurrentContextPPAPI(m_context); + m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete); + } + + bool isValid() const + { + return 0 != m_context; + } + + PP_Resource m_context; + PP_Instance m_instance; + const PPB_Instance* m_instInterface; + const PPB_Graphics3D* m_graphicsInterface; + const PPB_OpenGLES2InstancedArrays* m_instancedArrays; + PostSwapBuffersFn m_postSwapBuffers; + bool m_forceSwap; + }; + + static Ppapi s_ppapi; + + void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/) + { + // For NaCl bgfx doesn't create render thread, but rendering is always + // multithreaded. Frame rendering is done on main thread, and context + // is initialized when PPAPI interfaces are set. Force swap is there to + // keep calling swap complete callback, so that initialization doesn't + // deadlock on semaphores. + if (s_ppapi.m_forceSwap) + { + s_ppapi.swap(); + } + + if (hasContext() ) + { + renderFrame(); + } + } + + static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor) + { + s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor); + } + + static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount) + { + s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount); + } + + static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount) + { + s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount); + } + + void naclSetIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers) + { + s_ppapi.setIntefraces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers); + } + + void Ppapi::setIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers) + { + BX_TRACE("PPAPI Interfaces"); + m_instance = _instance; + m_instInterface = _instInterface; + m_graphicsInterface = _graphicsInterface; + m_instancedArrays = glGetInstancedArraysInterfacePPAPI(); + m_postSwapBuffers = _postSwapBuffers; + int32_t attribs[] = { PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, @@ -32,8 +122,8 @@ namespace bgfx PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8, PP_GRAPHICS3DATTRIB_SAMPLES, 0, PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, - PP_GRAPHICS3DATTRIB_WIDTH, int32_t(_width), - PP_GRAPHICS3DATTRIB_HEIGHT, int32_t(_height), + PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH, + PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT, PP_GRAPHICS3DATTRIB_NONE }; @@ -41,26 +131,41 @@ namespace bgfx m_instInterface->BindGraphics(m_instance, m_context); glSetCurrentContextPPAPI(m_context); m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete); + + glVertexAttribDivisor = naclVertexAttribDivisor; + glDrawArraysInstanced = naclDrawArraysInstanced; + glDrawElementsInstanced = naclDrawElementsInstanced; + } + + void GlContext::create(uint32_t _width, uint32_t _height) + { + BX_TRACE("GlContext::create"); } void GlContext::destroy() { } - void GlContext::resize(uint32_t _width, uint32_t _height, bool /*_vsync*/) + void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync) { - m_graphicsInterface->ResizeBuffers(m_context, _width, _height); + s_ppapi.m_forceSwap = false; + s_ppapi.resize(_width, _height, _vsync); } void GlContext::swap() { - glSetCurrentContextPPAPI(m_context); - m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete); + s_ppapi.swap(); } void GlContext::import() { } + + bool GlContext::isValid() const + { + return s_ppapi.isValid(); + } + } // namespace bgfx #endif // BX_PLATFORM_NACL & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL) diff --git a/src/glcontext_ppapi.h b/src/glcontext_ppapi.h index 053fde2e..937e0155 100644 --- a/src/glcontext_ppapi.h +++ b/src/glcontext_ppapi.h @@ -18,11 +18,6 @@ namespace bgfx struct GlContext { GlContext() - : m_context(0) - , m_instance(0) - , m_instInterface(NULL) - , m_graphicsInterface(NULL) - , m_instancedArrays(NULL) { } @@ -31,17 +26,7 @@ namespace bgfx void resize(uint32_t _width, uint32_t _height, bool _vsync); void swap(); void import(); - - bool isValid() const - { - return 0 != m_context; - } - - PP_Resource m_context; - PP_Instance m_instance; - const PPB_Instance* m_instInterface; - const PPB_Graphics3D* m_graphicsInterface; - const PPB_OpenGLES2InstancedArrays* m_instancedArrays; + bool isValid() const; }; } // namespace bgfx diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 4576ca9c..2067736b 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -513,7 +513,6 @@ namespace bgfx , m_textureSwizzleSupport(false) , m_useClearQuad(true) , m_flip(false) - , m_postSwapBuffers(NULL) , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT) , m_backBufferFbo(0) , m_msaaBackBufferFbo(0) @@ -669,11 +668,6 @@ namespace bgfx { m_glctx.swap(); } - - if (NULL != m_postSwapBuffers) - { - m_postSwapBuffers(m_resolution.m_width, m_resolution.m_height); - } } void invalidateCache() @@ -880,7 +874,6 @@ namespace bgfx bool m_useClearQuad; bool m_flip; - PostSwapBuffersFn m_postSwapBuffers; uint64_t m_hash; GLenum m_readPixelsFmt; @@ -897,45 +890,6 @@ namespace bgfx RendererContext* s_renderCtx; -#if BX_PLATFORM_NACL - static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor) - { - s_renderCtx->m_glctx.m_instancedArrays->VertexAttribDivisorANGLE(s_renderCtx->m_glctx.m_context, _index, _divisor); - } - - static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount) - { - s_renderCtx->m_glctx.m_instancedArrays->DrawArraysInstancedANGLE(s_renderCtx->m_glctx.m_context, _mode, _first, _count, _primcount); - } - - static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount) - { - s_renderCtx->m_glctx.m_instancedArrays->DrawElementsInstancedANGLE(s_renderCtx->m_glctx.m_context, _mode, _count, _type, _indices, _primcount); - } - - void naclSetIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers) - { - s_renderCtx->m_glctx.m_instance = _instance; - s_renderCtx->m_glctx.m_instInterface = _instInterface; - s_renderCtx->m_glctx.m_graphicsInterface = _graphicsInterface; - s_renderCtx->m_postSwapBuffers = _postSwapBuffers; - s_renderCtx->m_glctx.m_instancedArrays = glGetInstancedArraysInterfacePPAPI(); - s_renderCtx->setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT); - - if (NULL != s_renderCtx->m_glctx.m_instancedArrays) - { - s_vertexAttribDivisor = naclVertexAttribDivisor; - s_drawArraysInstanced = naclDrawArraysInstanced; - s_drawElementsInstanced = naclDrawElementsInstanced; - } - } - - void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/) - { - renderFrame(); - } -#endif // BX_PLATFORM_ - const char* glslTypeName(GLuint _type) { #define GLSL_TYPE(_ty) case _ty: return #_ty @@ -2445,6 +2399,18 @@ namespace bgfx || s_extension[Extension::OES_vertex_array_object].m_supported ; +#if BX_PLATFORM_NACL + s_renderCtx->m_vaoSupport &= NULL != glGenVertexArrays + && NULL != glDeleteVertexArrays + && NULL != glBindVertexArray + ; +#endif // BX_PLATFORM_NACL + + if (s_renderCtx->m_vaoSupport) + { + GL_CHECK(glGenVertexArrays(1, &s_renderCtx->m_vao) ); + } + s_renderCtx->m_samplerObjectSupport = !!BGFX_CONFIG_RENDERER_OPENGLES3 || s_extension[Extension::ARB_sampler_objects].m_supported ; @@ -2528,19 +2494,14 @@ namespace bgfx && NULL != glDrawArraysInstanced && NULL != glDrawElementsInstanced) { - s_vertexAttribDivisor = glVertexAttribDivisor; - s_drawArraysInstanced = glDrawArraysInstanced; + s_vertexAttribDivisor = glVertexAttribDivisor; + s_drawArraysInstanced = glDrawArraysInstanced; s_drawElementsInstanced = glDrawElementsInstanced; } } # endif // !BX_PLATFORM_IOS #endif // !BGFX_CONFIG_RENDERER_OPENGLES3 - if (s_renderCtx->m_vaoSupport) - { - GL_CHECK(glGenVertexArrays(1, &s_renderCtx->m_vao) ); - } - #if BGFX_CONFIG_RENDERER_OPENGL # if BGFX_CONFIG_RENDERER_OPENGL >= 31 s_textureFormat[TextureFormat::L8].m_internalFmt = GL_R8;