SDL_emscriptenopengles.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_EMSCRIPTEN
  20. #include <emscripten/emscripten.h>
  21. #include <emscripten/html5_webgl.h>
  22. #include <GLES2/gl2.h>
  23. #include "SDL_emscriptenvideo.h"
  24. #include "SDL_emscriptenopengles.h"
  25. int Emscripten_GLES_LoadLibrary(_THIS, const char *path)
  26. {
  27. return 0;
  28. }
  29. void Emscripten_GLES_UnloadLibrary(_THIS)
  30. {
  31. }
  32. SDL_FunctionPointer Emscripten_GLES_GetProcAddress(_THIS, const char *proc)
  33. {
  34. return emscripten_webgl_get_proc_address(proc);
  35. }
  36. int Emscripten_GLES_SetSwapInterval(_THIS, int interval)
  37. {
  38. if (interval < 0) {
  39. return SDL_SetError("Late swap tearing currently unsupported");
  40. } else if (interval == 0) {
  41. emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, 0);
  42. } else {
  43. emscripten_set_main_loop_timing(EM_TIMING_RAF, interval);
  44. }
  45. return 0;
  46. }
  47. int Emscripten_GLES_GetSwapInterval(_THIS, int *interval)
  48. {
  49. int mode, value;
  50. emscripten_get_main_loop_timing(&mode, &value);
  51. if (mode == EM_TIMING_RAF) {
  52. *interval = value;
  53. return 0;
  54. } else {
  55. *interval = 0;
  56. return 0;
  57. }
  58. }
  59. SDL_GLContext Emscripten_GLES_CreateContext(_THIS, SDL_Window *window)
  60. {
  61. SDL_WindowData *window_data;
  62. EmscriptenWebGLContextAttributes attribs;
  63. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context;
  64. emscripten_webgl_init_context_attributes(&attribs);
  65. attribs.alpha = _this->gl_config.alpha_size > 0;
  66. attribs.depth = _this->gl_config.depth_size > 0;
  67. attribs.stencil = _this->gl_config.stencil_size > 0;
  68. attribs.antialias = _this->gl_config.multisamplebuffers == 1;
  69. if (_this->gl_config.major_version == 3)
  70. attribs.majorVersion = 2; /* WebGL 2.0 ~= GLES 3.0 */
  71. window_data = window->driverdata;
  72. if (window_data->gl_context) {
  73. SDL_SetError("Cannot create multiple webgl contexts per window");
  74. return NULL;
  75. }
  76. context = emscripten_webgl_create_context(window_data->canvas_id, &attribs);
  77. if (context < 0) {
  78. SDL_SetError("Could not create webgl context");
  79. return NULL;
  80. }
  81. if (emscripten_webgl_make_context_current(context) != EMSCRIPTEN_RESULT_SUCCESS) {
  82. emscripten_webgl_destroy_context(context);
  83. return NULL;
  84. }
  85. window_data->gl_context = (SDL_GLContext)context;
  86. return (SDL_GLContext)context;
  87. }
  88. void Emscripten_GLES_DeleteContext(_THIS, SDL_GLContext context)
  89. {
  90. SDL_Window *window;
  91. /* remove the context from its window */
  92. for (window = _this->windows; window != NULL; window = window->next) {
  93. SDL_WindowData *window_data = window->driverdata;
  94. if (window_data->gl_context == context) {
  95. window_data->gl_context = NULL;
  96. }
  97. }
  98. emscripten_webgl_destroy_context((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context);
  99. }
  100. int Emscripten_GLES_SwapWindow(_THIS, SDL_Window *window)
  101. {
  102. if (emscripten_has_asyncify() && SDL_GetHintBoolean(SDL_HINT_EMSCRIPTEN_ASYNCIFY, SDL_TRUE)) {
  103. /* give back control to browser for screen refresh */
  104. emscripten_sleep(0);
  105. }
  106. return 0;
  107. }
  108. int Emscripten_GLES_MakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
  109. {
  110. /* it isn't possible to reuse contexts across canvases */
  111. if (window && context) {
  112. SDL_WindowData *window_data = window->driverdata;
  113. if (context != window_data->gl_context) {
  114. return SDL_SetError("Cannot make context current to another window");
  115. }
  116. }
  117. if (emscripten_webgl_make_context_current((EMSCRIPTEN_WEBGL_CONTEXT_HANDLE)context) != EMSCRIPTEN_RESULT_SUCCESS) {
  118. return SDL_SetError("Unable to make context current");
  119. }
  120. return 0;
  121. }
  122. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */