glcontext_ppapi.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
  7. # include <bgfx/bgfxplatform.h>
  8. # include "renderer_gl.h"
  9. namespace bgfx { namespace gl
  10. {
  11. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
  12. # include "glimports.h"
  13. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/);
  14. PP_CompletionCallback naclSwapComplete =
  15. {
  16. naclSwapCompleteCb,
  17. NULL,
  18. PP_COMPLETIONCALLBACK_FLAG_NONE
  19. };
  20. struct Ppapi
  21. {
  22. Ppapi()
  23. : m_context(0)
  24. , m_instance(0)
  25. , m_instInterface(NULL)
  26. , m_graphicsInterface(NULL)
  27. , m_instancedArrays(NULL)
  28. , m_query(NULL)
  29. , m_postSwapBuffers(NULL)
  30. , m_forceSwap(true)
  31. {
  32. }
  33. bool setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers);
  34. void resize(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
  35. {
  36. m_graphicsInterface->ResizeBuffers(m_context, _width, _height);
  37. }
  38. void swap()
  39. {
  40. glSetCurrentContextPPAPI(m_context);
  41. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  42. }
  43. bool isValid() const
  44. {
  45. return 0 != m_context;
  46. }
  47. PP_Resource m_context;
  48. PP_Instance m_instance;
  49. const PPB_Instance* m_instInterface;
  50. const PPB_Graphics3D* m_graphicsInterface;
  51. const PPB_OpenGLES2InstancedArrays* m_instancedArrays;
  52. const PPB_OpenGLES2Query* m_query;
  53. PostSwapBuffersFn m_postSwapBuffers;
  54. bool m_forceSwap;
  55. };
  56. static Ppapi s_ppapi;
  57. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
  58. {
  59. // For NaCl bgfx doesn't create render thread, but rendering is always
  60. // multithreaded. Frame rendering is done on main thread, and context
  61. // is initialized when PPAPI interfaces are set. Force swap is there to
  62. // keep calling swap complete callback, so that initialization doesn't
  63. // deadlock on semaphores.
  64. if (s_ppapi.m_forceSwap)
  65. {
  66. s_ppapi.swap();
  67. }
  68. renderFrame();
  69. }
  70. static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
  71. {
  72. s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor);
  73. }
  74. static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
  75. {
  76. s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount);
  77. }
  78. static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
  79. {
  80. s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount);
  81. }
  82. static void GL_APIENTRY naclGenQueries(GLsizei _n, GLuint* _queries)
  83. {
  84. s_ppapi.m_query->GenQueriesEXT(s_ppapi.m_context, _n, _queries);
  85. }
  86. static void GL_APIENTRY naclDeleteQueries(GLsizei _n, const GLuint* _queries)
  87. {
  88. s_ppapi.m_query->DeleteQueriesEXT(s_ppapi.m_context, _n, _queries);
  89. }
  90. static void GL_APIENTRY naclBeginQuery(GLenum _target, GLuint _id)
  91. {
  92. BX_UNUSED(_target);
  93. s_ppapi.m_query->BeginQueryEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, _id);
  94. }
  95. static void GL_APIENTRY naclEndQuery(GLenum _target)
  96. {
  97. BX_UNUSED(_target);
  98. s_ppapi.m_query->EndQueryEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT);
  99. }
  100. static void GL_APIENTRY naclGetQueryObjectiv(GLuint _id, GLenum _pname, GLint* _params)
  101. {
  102. BX_UNUSED(_id, _pname);
  103. s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, _params);
  104. }
  105. static void GL_APIENTRY naclGetQueryObjectui64v(GLuint _id, GLenum _pname, GLuint64* _params)
  106. {
  107. BX_UNUSED(_id, _pname);
  108. GLint params;
  109. s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, &params);
  110. *_params = params;
  111. }
  112. bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  113. {
  114. BX_TRACE("PPAPI Interfaces");
  115. m_instance = _instance;
  116. m_instInterface = _instInterface;
  117. m_graphicsInterface = _graphicsInterface;
  118. m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  119. m_query = glGetQueryInterfacePPAPI();
  120. m_postSwapBuffers = _postSwapBuffers;
  121. int32_t attribs[] =
  122. {
  123. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  124. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  125. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  126. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  127. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  128. PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
  129. PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
  130. PP_GRAPHICS3DATTRIB_NONE
  131. };
  132. m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
  133. if (0 == m_context)
  134. {
  135. BX_TRACE("Failed to create context!");
  136. return false;
  137. }
  138. m_instInterface->BindGraphics(m_instance, m_context);
  139. glSetCurrentContextPPAPI(m_context);
  140. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  141. if (NULL != m_instancedArrays)
  142. {
  143. glVertexAttribDivisor = naclVertexAttribDivisor;
  144. glDrawArraysInstanced = naclDrawArraysInstanced;
  145. glDrawElementsInstanced = naclDrawElementsInstanced;
  146. }
  147. if (NULL != m_query)
  148. {
  149. glGenQueries = naclGenQueries;
  150. glDeleteQueries = naclDeleteQueries;
  151. glBeginQuery = naclBeginQuery;
  152. glEndQuery = naclEndQuery;
  153. glGetQueryObjectiv = naclGetQueryObjectiv;
  154. glGetQueryObjectui64v = naclGetQueryObjectui64v;
  155. }
  156. // Prevent render thread creation.
  157. RenderFrame::Enum result = renderFrame();
  158. return RenderFrame::NoContext == result;
  159. }
  160. void GlContext::create(uint32_t _width, uint32_t _height)
  161. {
  162. BX_UNUSED(_width, _height);
  163. BX_TRACE("GlContext::create");
  164. g_internalData.context = &s_ppapi.m_context;
  165. }
  166. void GlContext::destroy()
  167. {
  168. }
  169. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  170. {
  171. s_ppapi.m_forceSwap = false;
  172. s_ppapi.resize(_width, _height, _flags);
  173. }
  174. uint64_t GlContext::getCaps() const
  175. {
  176. return 0;
  177. }
  178. SwapChainGL* GlContext::createSwapChain(void* /*_nwh*/)
  179. {
  180. BX_CHECK(false, "Shouldn't be called!");
  181. return NULL;
  182. }
  183. void GlContext::destroySwapChain(SwapChainGL* /*_swapChain*/)
  184. {
  185. BX_CHECK(false, "Shouldn't be called!");
  186. }
  187. void GlContext::swap(SwapChainGL* /*_swapChain*/)
  188. {
  189. s_ppapi.swap();
  190. }
  191. void GlContext::makeCurrent(SwapChainGL* /*_swapChain*/)
  192. {
  193. }
  194. void GlContext::import()
  195. {
  196. }
  197. bool GlContext::isValid() const
  198. {
  199. return s_ppapi.isValid();
  200. }
  201. } /* namespace gl */ } // namespace bgfx
  202. namespace bgfx
  203. {
  204. bool naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  205. {
  206. return gl::s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
  207. }
  208. } // namespace bgfx
  209. #endif // BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)