glcontext_ppapi.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, _params);
  103. }
  104. static void GL_APIENTRY naclGetQueryObjectui64v(GLuint _id, GLenum _pname, GLuint64* _params)
  105. {
  106. GLint params;
  107. s_ppapi.m_query->GetQueryivEXT(s_ppapi.m_context, GL_ANY_SAMPLES_PASSED_EXT, GL_CURRENT_QUERY_EXT, &params);
  108. *_params = params;
  109. }
  110. bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  111. {
  112. BX_TRACE("PPAPI Interfaces");
  113. m_instance = _instance;
  114. m_instInterface = _instInterface;
  115. m_graphicsInterface = _graphicsInterface;
  116. m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  117. m_query = glGetQueryInterfacePPAPI();
  118. m_postSwapBuffers = _postSwapBuffers;
  119. int32_t attribs[] =
  120. {
  121. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  122. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  123. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  124. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  125. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  126. PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
  127. PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
  128. PP_GRAPHICS3DATTRIB_NONE
  129. };
  130. m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
  131. if (0 == m_context)
  132. {
  133. BX_TRACE("Failed to create context!");
  134. return false;
  135. }
  136. m_instInterface->BindGraphics(m_instance, m_context);
  137. glSetCurrentContextPPAPI(m_context);
  138. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  139. if (NULL != m_instancedArrays)
  140. {
  141. glVertexAttribDivisor = naclVertexAttribDivisor;
  142. glDrawArraysInstanced = naclDrawArraysInstanced;
  143. glDrawElementsInstanced = naclDrawElementsInstanced;
  144. }
  145. if (NULL != m_query)
  146. {
  147. glGenQueries = naclGenQueries;
  148. glDeleteQueries = naclDeleteQueries;
  149. glBeginQuery = naclBeginQuery;
  150. glEndQuery = naclEndQuery;
  151. glGetQueryObjectiv = naclGetQueryObjectiv;
  152. glGetQueryObjectui64v = naclGetQueryObjectui64v;
  153. }
  154. // Prevent render thread creation.
  155. RenderFrame::Enum result = renderFrame();
  156. return RenderFrame::NoContext == result;
  157. }
  158. void GlContext::create(uint32_t _width, uint32_t _height)
  159. {
  160. BX_UNUSED(_width, _height);
  161. BX_TRACE("GlContext::create");
  162. g_internalData.context = m_context;
  163. }
  164. void GlContext::destroy()
  165. {
  166. }
  167. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  168. {
  169. s_ppapi.m_forceSwap = false;
  170. s_ppapi.resize(_width, _height, _flags);
  171. }
  172. uint64_t GlContext::getCaps() const
  173. {
  174. return 0;
  175. }
  176. SwapChainGL* GlContext::createSwapChain(void* /*_nwh*/)
  177. {
  178. BX_CHECK(false, "Shouldn't be called!");
  179. return NULL;
  180. }
  181. void GlContext::destroySwapChain(SwapChainGL* /*_swapChain*/)
  182. {
  183. BX_CHECK(false, "Shouldn't be called!");
  184. }
  185. void GlContext::swap(SwapChainGL* /*_swapChain*/)
  186. {
  187. s_ppapi.swap();
  188. }
  189. void GlContext::makeCurrent(SwapChainGL* /*_swapChain*/)
  190. {
  191. }
  192. void GlContext::import()
  193. {
  194. }
  195. bool GlContext::isValid() const
  196. {
  197. return s_ppapi.isValid();
  198. }
  199. } /* namespace gl */ } // namespace bgfx
  200. namespace bgfx
  201. {
  202. bool naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  203. {
  204. return gl::s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
  205. }
  206. } // namespace bgfx
  207. #endif // BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)