glcontext_ppapi.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2011-2013 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_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|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. # undef GL_IMPORT
  14. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/);
  15. PP_CompletionCallback naclSwapComplete =
  16. {
  17. naclSwapCompleteCb,
  18. NULL,
  19. PP_COMPLETIONCALLBACK_FLAG_NONE
  20. };
  21. struct Ppapi
  22. {
  23. Ppapi()
  24. : m_context(0)
  25. , m_instance(0)
  26. , m_instInterface(NULL)
  27. , m_graphicsInterface(NULL)
  28. , m_instancedArrays(NULL)
  29. , m_postSwapBuffers(NULL)
  30. , m_forceSwap(true)
  31. {
  32. }
  33. void setIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers);
  34. void resize(uint32_t _width, uint32_t _height, bool /*_vsync*/)
  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. PostSwapBuffersFn m_postSwapBuffers;
  53. bool m_forceSwap;
  54. };
  55. static Ppapi s_ppapi;
  56. void naclSwapCompleteCb(void* /*_data*/, int32_t /*_result*/)
  57. {
  58. // For NaCl bgfx doesn't create render thread, but rendering is always
  59. // multithreaded. Frame rendering is done on main thread, and context
  60. // is initialized when PPAPI interfaces are set. Force swap is there to
  61. // keep calling swap complete callback, so that initialization doesn't
  62. // deadlock on semaphores.
  63. if (s_ppapi.m_forceSwap)
  64. {
  65. s_ppapi.swap();
  66. }
  67. renderFrame();
  68. }
  69. static void GL_APIENTRY naclVertexAttribDivisor(GLuint _index, GLuint _divisor)
  70. {
  71. s_ppapi.m_instancedArrays->VertexAttribDivisorANGLE(s_ppapi.m_context, _index, _divisor);
  72. }
  73. static void GL_APIENTRY naclDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei _primcount)
  74. {
  75. s_ppapi.m_instancedArrays->DrawArraysInstancedANGLE(s_ppapi.m_context, _mode, _first, _count, _primcount);
  76. }
  77. static void GL_APIENTRY naclDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei _primcount)
  78. {
  79. s_ppapi.m_instancedArrays->DrawElementsInstancedANGLE(s_ppapi.m_context, _mode, _count, _type, _indices, _primcount);
  80. }
  81. void naclSetIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  82. {
  83. s_ppapi.setIntefraces( _instance, _instInterface, _graphicsInterface, _postSwapBuffers);
  84. }
  85. void Ppapi::setIntefraces(PP_Instance _instance, const PPB_Instance* _instInterface, const PPB_Graphics3D* _graphicsInterface, PostSwapBuffersFn _postSwapBuffers)
  86. {
  87. BX_TRACE("PPAPI Interfaces");
  88. m_instance = _instance;
  89. m_instInterface = _instInterface;
  90. m_graphicsInterface = _graphicsInterface;
  91. m_instancedArrays = glGetInstancedArraysInterfacePPAPI();
  92. m_postSwapBuffers = _postSwapBuffers;
  93. int32_t attribs[] =
  94. {
  95. PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
  96. PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
  97. PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
  98. PP_GRAPHICS3DATTRIB_SAMPLES, 0,
  99. PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
  100. PP_GRAPHICS3DATTRIB_WIDTH, BGFX_DEFAULT_WIDTH,
  101. PP_GRAPHICS3DATTRIB_HEIGHT, BGFX_DEFAULT_HEIGHT,
  102. PP_GRAPHICS3DATTRIB_NONE
  103. };
  104. m_context = m_graphicsInterface->Create(m_instance, 0, attribs);
  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. }
  112. void GlContext::create(uint32_t _width, uint32_t _height)
  113. {
  114. BX_UNUSED(_width, _height);
  115. BX_TRACE("GlContext::create");
  116. }
  117. void GlContext::destroy()
  118. {
  119. }
  120. void GlContext::resize(uint32_t _width, uint32_t _height, bool _vsync)
  121. {
  122. s_ppapi.m_forceSwap = false;
  123. s_ppapi.resize(_width, _height, _vsync);
  124. }
  125. void GlContext::swap()
  126. {
  127. s_ppapi.swap();
  128. }
  129. void GlContext::import()
  130. {
  131. }
  132. bool GlContext::isValid() const
  133. {
  134. return s_ppapi.isValid();
  135. }
  136. } // namespace bgfx
  137. #endif // BX_PLATFORM_NACL & (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)