glcontext_html5.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2011-2022 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "bgfx_p.h"
  6. #if BGFX_CONFIG_RENDERER_OPENGLES
  7. # include "renderer_gl.h"
  8. # if BGFX_USE_HTML5
  9. # include "emscripten.h"
  10. // from emscripten gl.c, because we're not going to go
  11. // through egl
  12. extern "C" void* emscripten_GetProcAddress(const char *name_);
  13. extern "C" void* emscripten_webgl1_get_proc_address(const char *name_);
  14. extern "C" void* emscripten_webgl2_get_proc_address(const char *name_);
  15. namespace bgfx { namespace gl
  16. {
  17. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func = NULL
  18. # include "glimports.h"
  19. static EmscriptenWebGLContextAttributes s_attrs;
  20. struct SwapChainGL
  21. {
  22. SwapChainGL(int _context, const char* _canvas)
  23. : m_context(_context)
  24. {
  25. m_canvas = (char*)BX_ALLOC(g_allocator, strlen(_canvas) + 1);
  26. strcpy(m_canvas, _canvas);
  27. makeCurrent();
  28. GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 0.0f) );
  29. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  30. swapBuffers();
  31. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT) );
  32. swapBuffers();
  33. }
  34. ~SwapChainGL()
  35. {
  36. EMSCRIPTEN_CHECK(emscripten_webgl_destroy_context(m_context) );
  37. BX_FREE(g_allocator, m_canvas);
  38. }
  39. void makeCurrent()
  40. {
  41. EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(m_context) );
  42. }
  43. void swapBuffers()
  44. {
  45. // There is no swapBuffers equivalent. A swap happens whenever control is returned back
  46. // to the browser.
  47. }
  48. int m_context;
  49. char* m_canvas;
  50. };
  51. void GlContext::create(uint32_t _width, uint32_t _height, uint32_t /*_flags*/)
  52. {
  53. // assert?
  54. if (m_primary != NULL)
  55. return;
  56. const char* canvas = (const char*) g_platformData.nwh;
  57. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE) g_platformData.context;
  58. if (context > 0)
  59. {
  60. if (emscripten_webgl_get_context_attributes(context, &s_attrs) >= 0)
  61. {
  62. import(s_attrs.majorVersion);
  63. m_primary = BX_NEW(g_allocator, SwapChainGL)(context, canvas);
  64. }
  65. else
  66. {
  67. BX_TRACE("Invalid WebGL context. (Canvas handle: '%s', context handle: %d)", canvas, context);
  68. }
  69. }
  70. else
  71. {
  72. m_primary = createSwapChain((void*)canvas);
  73. }
  74. if (0 != _width
  75. && 0 != _height)
  76. {
  77. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) );
  78. }
  79. makeCurrent(m_primary);
  80. }
  81. void GlContext::destroy()
  82. {
  83. if (m_primary)
  84. {
  85. if (m_current == m_primary)
  86. {
  87. m_current = NULL;
  88. }
  89. BX_DELETE(g_allocator, m_primary);
  90. m_primary = NULL;
  91. }
  92. }
  93. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t /* _flags */)
  94. {
  95. if (m_primary == NULL)
  96. {
  97. return;
  98. }
  99. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) );
  100. }
  101. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  102. {
  103. emscripten_webgl_init_context_attributes(&s_attrs);
  104. // Work around bug https://bugs.chromium.org/p/chromium/issues/detail?id=1045643 in Chrome
  105. // by having alpha always enabled.
  106. s_attrs.alpha = true;
  107. s_attrs.premultipliedAlpha = false;
  108. s_attrs.depth = true;
  109. s_attrs.stencil = true;
  110. s_attrs.enableExtensionsByDefault = true;
  111. s_attrs.antialias = false;
  112. s_attrs.minorVersion = 0;
  113. const char* canvas = (const char*) _nwh;
  114. int error = 0;
  115. for (int version = 2; version >= 1; --version)
  116. {
  117. s_attrs.majorVersion = version;
  118. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvas, &s_attrs);
  119. if (context > 0)
  120. {
  121. EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(context) );
  122. SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(context, canvas);
  123. import(version);
  124. return swapChain;
  125. }
  126. error = (int) context;
  127. }
  128. BX_TRACE("Failed to create WebGL context. (Canvas handle: '%s', last attempt error %d)", canvas, error);
  129. return NULL;
  130. }
  131. uint64_t GlContext::getCaps() const
  132. {
  133. return BGFX_CAPS_SWAP_CHAIN;
  134. }
  135. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  136. {
  137. BX_DELETE(g_allocator, _swapChain);
  138. }
  139. void GlContext::swap(SwapChainGL* /* _swapChain */)
  140. {
  141. }
  142. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  143. {
  144. if (m_current != _swapChain)
  145. {
  146. m_current = _swapChain;
  147. if (NULL == _swapChain)
  148. {
  149. if (NULL != m_primary)
  150. {
  151. m_primary->makeCurrent();
  152. }
  153. }
  154. else
  155. {
  156. _swapChain->makeCurrent();
  157. }
  158. }
  159. }
  160. template<typename Fn>
  161. static Fn getProcAddress(int _version, const char* _name)
  162. {
  163. Fn func = reinterpret_cast<Fn>(emscripten_webgl1_get_proc_address(_name) );
  164. if (NULL == func && _version >= 2)
  165. {
  166. func = reinterpret_cast<Fn>(emscripten_webgl2_get_proc_address(_name) );
  167. }
  168. return func;
  169. }
  170. void GlContext::import(int _webGLVersion)
  171. {
  172. BX_TRACE("Import:");
  173. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  174. { \
  175. if (NULL == _func) \
  176. { \
  177. _func = getProcAddress<_proto>(_webGLVersion, #_import); \
  178. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  179. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize \
  180. , "Failed to create WebGL/OpenGLES context. GetProcAddress(\"%s\")" \
  181. , #_import \
  182. ); \
  183. } \
  184. }
  185. # include "glimports.h"
  186. # undef GL_EXTENSION
  187. }
  188. } /* namespace gl */ } // namespace bgfx
  189. # endif // BGFX_USE_EGL
  190. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)