glcontext_html5.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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)
  52. {
  53. // assert?
  54. if (m_primary != NULL)
  55. return;
  56. const char* canvas = (const char*) g_platformData.nwh;
  57. m_primary = createSwapChain((void*)canvas);
  58. if (0 != _width
  59. && 0 != _height)
  60. {
  61. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) );
  62. }
  63. makeCurrent(m_primary);
  64. }
  65. void GlContext::destroy()
  66. {
  67. if (m_primary)
  68. {
  69. if (m_current == m_primary)
  70. {
  71. m_current = NULL;
  72. }
  73. BX_DELETE(g_allocator, m_primary);
  74. m_primary = NULL;
  75. }
  76. }
  77. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t /* _flags */)
  78. {
  79. if (m_primary == NULL)
  80. {
  81. return;
  82. }
  83. EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) );
  84. }
  85. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  86. {
  87. emscripten_webgl_init_context_attributes(&s_attrs);
  88. // Work around bug https://bugs.chromium.org/p/chromium/issues/detail?id=1045643 in Chrome
  89. // by having alpha always enabled.
  90. s_attrs.alpha = true;
  91. s_attrs.premultipliedAlpha = false;
  92. s_attrs.depth = true;
  93. s_attrs.stencil = true;
  94. s_attrs.enableExtensionsByDefault = true;
  95. s_attrs.antialias = false;
  96. s_attrs.minorVersion = 0;
  97. const char* canvas = (const char*) _nwh;
  98. int error = 0;
  99. for (int version = 2; version >= 1; --version)
  100. {
  101. s_attrs.majorVersion = version;
  102. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvas, &s_attrs);
  103. if (context > 0)
  104. {
  105. EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(context) );
  106. SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(context, canvas);
  107. import(version);
  108. return swapChain;
  109. }
  110. error = (int) context;
  111. }
  112. BX_TRACE("Failed to create WebGL context. (Canvas handle: '%s', last attempt error %d)", canvas, error);
  113. return NULL;
  114. }
  115. uint64_t GlContext::getCaps() const
  116. {
  117. return BGFX_CAPS_SWAP_CHAIN;
  118. }
  119. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  120. {
  121. BX_DELETE(g_allocator, _swapChain);
  122. }
  123. void GlContext::swap(SwapChainGL* /* _swapChain */)
  124. {
  125. }
  126. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  127. {
  128. if (m_current != _swapChain)
  129. {
  130. m_current = _swapChain;
  131. if (NULL == _swapChain)
  132. {
  133. if (NULL != m_primary)
  134. {
  135. m_primary->makeCurrent();
  136. }
  137. }
  138. else
  139. {
  140. _swapChain->makeCurrent();
  141. }
  142. }
  143. }
  144. template<typename Fn>
  145. static Fn getProcAddress(int _version, const char* _name)
  146. {
  147. Fn func = reinterpret_cast<Fn>(emscripten_webgl1_get_proc_address(_name) );
  148. if (NULL == func && _version >= 2)
  149. {
  150. func = reinterpret_cast<Fn>(emscripten_webgl2_get_proc_address(_name) );
  151. }
  152. return func;
  153. }
  154. void GlContext::import(int _webGLVersion)
  155. {
  156. BX_TRACE("Import:");
  157. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  158. { \
  159. if (NULL == _func) \
  160. { \
  161. _func = getProcAddress<_proto>(_webGLVersion, #_import); \
  162. BX_TRACE("\t%p " #_func " (" #_import ")", _func); \
  163. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize \
  164. , "Failed to create WebGL/OpenGLES context. GetProcAddress(\"%s\")" \
  165. , #_import \
  166. ); \
  167. } \
  168. }
  169. # include "glimports.h"
  170. # undef GL_EXTENSION
  171. }
  172. } /* namespace gl */ } // namespace bgfx
  173. # endif // BGFX_USE_EGL
  174. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)