context_egl_uwp.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**************************************************************************/
  2. /* context_egl_uwp.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "context_egl_uwp.h"
  31. #include <EGL/eglext.h>
  32. using Platform::Exception;
  33. void ContextEGL_UWP::release_current() {
  34. eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEglContext);
  35. }
  36. void ContextEGL_UWP::make_current() {
  37. eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
  38. }
  39. int ContextEGL_UWP::get_window_width() {
  40. return width;
  41. }
  42. int ContextEGL_UWP::get_window_height() {
  43. return height;
  44. }
  45. void ContextEGL_UWP::reset() {
  46. cleanup();
  47. window = CoreWindow::GetForCurrentThread();
  48. initialize();
  49. }
  50. void ContextEGL_UWP::swap_buffers() {
  51. if (eglSwapBuffers(mEglDisplay, mEglSurface) != EGL_TRUE) {
  52. cleanup();
  53. window = CoreWindow::GetForCurrentThread();
  54. initialize();
  55. // tell rasterizer to reload textures and stuff?
  56. }
  57. }
  58. Error ContextEGL_UWP::initialize() {
  59. EGLint configAttribList[] = {
  60. EGL_RED_SIZE, 8,
  61. EGL_GREEN_SIZE, 8,
  62. EGL_BLUE_SIZE, 8,
  63. EGL_ALPHA_SIZE, 8,
  64. EGL_DEPTH_SIZE, 8,
  65. EGL_STENCIL_SIZE, 8,
  66. EGL_SAMPLE_BUFFERS, 0,
  67. EGL_NONE
  68. };
  69. EGLint surfaceAttribList[] = {
  70. EGL_NONE, EGL_NONE
  71. };
  72. EGLint numConfigs = 0;
  73. EGLint majorVersion = 1;
  74. EGLint minorVersion;
  75. if (driver == GLES_2_0) {
  76. minorVersion = 0;
  77. } else {
  78. minorVersion = 5;
  79. }
  80. EGLDisplay display = EGL_NO_DISPLAY;
  81. EGLContext context = EGL_NO_CONTEXT;
  82. EGLSurface surface = EGL_NO_SURFACE;
  83. EGLConfig config = nullptr;
  84. EGLint contextAttribs[3];
  85. if (driver == GLES_2_0) {
  86. contextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
  87. contextAttribs[1] = 2;
  88. contextAttribs[2] = EGL_NONE;
  89. } else {
  90. contextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
  91. contextAttribs[1] = 3;
  92. contextAttribs[2] = EGL_NONE;
  93. }
  94. try {
  95. const EGLint displayAttributes[] = {
  96. /*EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  97. EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
  98. EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
  99. EGL_NONE,*/
  100. // These are the default display attributes, used to request ANGLE's D3D11 renderer.
  101. // eglInitialize will only succeed with these attributes if the hardware supports D3D11 Feature Level 10_0+.
  102. EGL_PLATFORM_ANGLE_TYPE_ANGLE,
  103. EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
  104. // EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices.
  105. // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it.
  106. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER,
  107. EGL_TRUE,
  108. // EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call
  109. // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended.
  110. // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement.
  111. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
  112. EGL_TRUE,
  113. EGL_NONE,
  114. };
  115. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
  116. if (!eglGetPlatformDisplayEXT) {
  117. throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
  118. }
  119. display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
  120. if (display == EGL_NO_DISPLAY) {
  121. throw Exception::CreateException(E_FAIL, L"Failed to get default EGL display");
  122. }
  123. if (eglInitialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
  124. throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
  125. }
  126. if (eglGetConfigs(display, nullptr, 0, &numConfigs) == EGL_FALSE) {
  127. throw Exception::CreateException(E_FAIL, L"Failed to get EGLConfig count");
  128. }
  129. if (eglChooseConfig(display, configAttribList, &config, 1, &numConfigs) == EGL_FALSE) {
  130. throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig count");
  131. }
  132. surface = eglCreateWindowSurface(display, config, reinterpret_cast<IInspectable *>(window), surfaceAttribList);
  133. if (surface == EGL_NO_SURFACE) {
  134. throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
  135. }
  136. context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  137. if (context == EGL_NO_CONTEXT) {
  138. throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
  139. }
  140. if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
  141. throw Exception::CreateException(E_FAIL, L"Failed to make fullscreen EGLSurface current");
  142. }
  143. } catch (...) {
  144. return FAILED;
  145. }
  146. mEglDisplay = display;
  147. mEglSurface = surface;
  148. mEglContext = context;
  149. eglQuerySurface(display, surface, EGL_WIDTH, &width);
  150. eglQuerySurface(display, surface, EGL_HEIGHT, &height);
  151. return OK;
  152. }
  153. void ContextEGL_UWP::cleanup() {
  154. if (mEglDisplay != EGL_NO_DISPLAY && mEglSurface != EGL_NO_SURFACE) {
  155. eglDestroySurface(mEglDisplay, mEglSurface);
  156. mEglSurface = EGL_NO_SURFACE;
  157. }
  158. if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) {
  159. eglDestroyContext(mEglDisplay, mEglContext);
  160. mEglContext = EGL_NO_CONTEXT;
  161. }
  162. if (mEglDisplay != EGL_NO_DISPLAY) {
  163. eglTerminate(mEglDisplay);
  164. mEglDisplay = EGL_NO_DISPLAY;
  165. }
  166. }
  167. ContextEGL_UWP::ContextEGL_UWP(CoreWindow ^ p_window, Driver p_driver) :
  168. mEglDisplay(EGL_NO_DISPLAY),
  169. mEglContext(EGL_NO_CONTEXT),
  170. mEglSurface(EGL_NO_SURFACE),
  171. driver(p_driver),
  172. window(p_window),
  173. vsync(false) {}
  174. ContextEGL_UWP::~ContextEGL_UWP() {
  175. cleanup();
  176. }