glcontext_ppapi.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2011-2014 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 <bgfxplatform.h>
  8. # include "renderer_gl.h"
  9. namespace bgfx
  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, bool /*_vsync*/)
  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 naclSetInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  81. {
  82. return s_ppapi.setInterfaces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
  83. }
  84. bool Ppapi::setInterfaces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  85. {
  86. BX_TRACE("PPAPI Interfaces");
  87. m_instance = _instance;
  88. m_instInterface = _instInterface;
  89. m_graphicsInterface = _graphicsInterface;
  90. m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  91. m_postSwapBuffers = _postSwapBuffers;
  92. int32_t attribs[] =
  93. {
  94. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  95. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  96. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  97. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  98. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  99. PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
  100. PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
  101. PP_GRAPHICS3DATTRIB_NONE
  102. };
  103. m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
  104. if (0 == m_context)
  105. {
  106. BX_TRACE("Failed to create context!");
  107. return false;
  108. }
  109. m_instInterface->BindGraphics(m_instance, m_context);
  110. glSetCurrentContextPPAPI(m_context);
  111. m_graphicsInterface->SwapBuffers(m_context, naclSwapComplete);
  112. glVertexAttribDivisor = naclVertexAttribDivisor;
  113. glDrawArraysInstanced = naclDrawArraysInstanced;
  114. glDrawElementsInstanced = naclDrawElementsInstanced;
  115. // Prevent render thread creation.
  116. RenderFrame::Enum result = renderFrame();
  117. return RenderFrame::NoContext == result;
  118. }
  119. void GlContext::create(uint32_t _width, uint32_t _height)
  120. {
  121. BX_UNUSED(_width, _height);
  122. BX_TRACE("GlContext::create");
  123. }
  124. void GlContext::destroy()
  125. {
  126. }
  127. void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
  128. {
  129. s_ppapi.m_forceSwap = false;
  130. s_ppapi.resize(_width, _height, _vsync);
  131. }
  132. void GlContext::swap()
  133. {
  134. s_ppapi.swap();
  135. }
  136. void GlContext::import()
  137. {
  138. }
  139. bool GlContext::isValid() const
  140. {
  141. return s_ppapi.isValid();
  142. }
  143. } // namespace bgfx
  144. #endif // BX_PLATFORM_NACL && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)