context_egl_uwp.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifdef EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER
  105. // EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices.
  106. // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it.
  107. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER,
  108. EGL_TRUE,
  109. #endif
  110. // EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call
  111. // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended.
  112. // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement.
  113. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
  114. EGL_TRUE,
  115. EGL_NONE,
  116. };
  117. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
  118. if (!eglGetPlatformDisplayEXT) {
  119. throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
  120. }
  121. display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
  122. if (display == EGL_NO_DISPLAY) {
  123. throw Exception::CreateException(E_FAIL, L"Failed to get default EGL display");
  124. }
  125. if (eglInitialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
  126. throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
  127. }
  128. if (eglGetConfigs(display, NULL, 0, &numConfigs) == EGL_FALSE) {
  129. throw Exception::CreateException(E_FAIL, L"Failed to get EGLConfig count");
  130. }
  131. if (eglChooseConfig(display, configAttribList, &config, 1, &numConfigs) == EGL_FALSE) {
  132. throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig count");
  133. }
  134. surface = eglCreateWindowSurface(display, config, reinterpret_cast<IInspectable *>(window), surfaceAttribList);
  135. if (surface == EGL_NO_SURFACE) {
  136. throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
  137. }
  138. context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
  139. if (context == EGL_NO_CONTEXT) {
  140. throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
  141. }
  142. if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
  143. throw Exception::CreateException(E_FAIL, L"Failed to make fullscreen EGLSurface current");
  144. }
  145. } catch (...) {
  146. return FAILED;
  147. };
  148. mEglDisplay = display;
  149. mEglSurface = surface;
  150. mEglContext = context;
  151. eglQuerySurface(display, surface, EGL_WIDTH, &width);
  152. eglQuerySurface(display, surface, EGL_HEIGHT, &height);
  153. return OK;
  154. };
  155. void ContextEGL_UWP::cleanup() {
  156. if (mEglDisplay != EGL_NO_DISPLAY && mEglSurface != EGL_NO_SURFACE) {
  157. eglDestroySurface(mEglDisplay, mEglSurface);
  158. mEglSurface = EGL_NO_SURFACE;
  159. }
  160. if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) {
  161. eglDestroyContext(mEglDisplay, mEglContext);
  162. mEglContext = EGL_NO_CONTEXT;
  163. }
  164. if (mEglDisplay != EGL_NO_DISPLAY) {
  165. eglTerminate(mEglDisplay);
  166. mEglDisplay = EGL_NO_DISPLAY;
  167. }
  168. };
  169. ContextEGL_UWP::ContextEGL_UWP(CoreWindow ^ p_window, Driver p_driver) :
  170. mEglDisplay(EGL_NO_DISPLAY),
  171. mEglContext(EGL_NO_CONTEXT),
  172. mEglSurface(EGL_NO_SURFACE),
  173. driver(p_driver),
  174. window(p_window) {}
  175. ContextEGL_UWP::~ContextEGL_UWP() {
  176. cleanup();
  177. };