glcontext_ppapi.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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_postSwapBuffers(NULL)
  29. , m_forceSwap(true)
  30. {
  31. }
  32. bool setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers);
  33. void resize(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
  34. {
  35. m_graphicsInterface->ResizeBuffers(m_context, _width, _height);
  36. }
  37. void swap()
  38. {
  39. glSetCurrentContextPPAPI(m_context);
  40. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  41. }
  42. bool isValid() const
  43. {
  44. return 0 != m_context;
  45. }
  46. PP_Resource m_context;
  47. PP_Instance m_instance;
  48. const PPB_Instance* m_instInterface;
  49. const PPB_Graphics3D* m_graphicsInterface;
  50. const PPB_OpenGLES2InstancedArrays* m_instancedArrays;
  51. PostSwapBuffersFn m_postSwapBuffers;
  52. bool m_forceSwap;
  53. };
  54. static Ppapi s_ppapi;
  55. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
  56. {
  57. // For NaCl bgfx doesn't create render thread, but rendering is always
  58. // multithreaded. Frame rendering is done on main thread, and context
  59. // is initialized when PPAPI interfaces are set. Force swap is there to
  60. // keep calling swap complete callback, so that initialization doesn't
  61. // deadlock on semaphores.
  62. if (s_ppapi.m_forceSwap)
  63. {
  64. s_ppapi.swap();
  65. }
  66. renderFrame();
  67. }
  68. static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
  69. {
  70. s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor);
  71. }
  72. static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
  73. {
  74. s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount);
  75. }
  76. static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
  77. {
  78. s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount);
  79. }
  80. bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  81. {
  82. BX_TRACE("PPAPI Interfaces");
  83. m_instance = _instance;
  84. m_instInterface = _instInterface;
  85. m_graphicsInterface = _graphicsInterface;
  86. m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  87. m_postSwapBuffers = _postSwapBuffers;
  88. int32_t attribs[] =
  89. {
  90. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  91. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  92. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  93. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  94. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  95. PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
  96. PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
  97. PP_GRAPHICS3DATTRIB_NONE
  98. };
  99. m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
  100. if (0 == m_context)
  101. {
  102. BX_TRACE("Failed to create context!");
  103. return false;
  104. }
  105. m_instInterface->BindGraphics(m_instance, m_context);
  106. glSetCurrentContextPPAPI(m_context);
  107. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  108. glVertexAttribDivisor = naclVertexAttribDivisor;
  109. glDrawArraysInstanced = naclDrawArraysInstanced;
  110. glDrawElementsInstanced = naclDrawElementsInstanced;
  111. // Prevent render thread creation.
  112. RenderFrame::Enum result = renderFrame();
  113. return RenderFrame::NoContext == result;
  114. }
  115. void GlContext::create(uint32_t _width, uint32_t _height)
  116. {
  117. BX_UNUSED(_width, _height);
  118. BX_TRACE("GlContext::create");
  119. }
  120. void GlContext::destroy()
  121. {
  122. }
  123. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  124. {
  125. s_ppapi.m_forceSwap = false;
  126. s_ppapi.resize(_width, _height, _flags);
  127. }
  128. uint64_t GlContext::getCaps() const
  129. {
  130. return 0;
  131. }
  132. SwapChainGL* GlContext::createSwapChain(void* /*_nwh*/)
  133. {
  134. BX_CHECK(false, "Shouldn't be called!");
  135. return NULL;
  136. }
  137. void GlContext::destroySwapChain(SwapChainGL* /*_swapChain*/)
  138. {
  139. BX_CHECK(false, "Shouldn't be called!");
  140. }
  141. void GlContext::swap(SwapChainGL* /*_swapChain*/)
  142. {
  143. s_ppapi.swap();
  144. }
  145. void GlContext::makeCurrent(SwapChainGL* /*_swapChain*/)
  146. {
  147. }
  148. void GlContext::import()
  149. {
  150. }
  151. bool GlContext::isValid() const
  152. {
  153. return s_ppapi.isValid();
  154. }
  155. } /* namespace gl */ } // namespace bgfx
  156. namespace bgfx
  157. {
  158. bool naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  159. {
  160. return gl::s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
  161. }
  162. } // namespace bgfx
  163. #endif // BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)