display_layer.mm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* display_layer.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #import "display_layer.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/project_settings.h"
  33. #include "main/main.h"
  34. #include "os_iphone.h"
  35. #include "servers/audio_server.h"
  36. #import <AudioToolbox/AudioServices.h>
  37. #import <GameController/GameController.h>
  38. #import <OpenGLES/EAGL.h>
  39. #import <OpenGLES/ES1/gl.h>
  40. #import <OpenGLES/ES1/glext.h>
  41. #import <QuartzCore/QuartzCore.h>
  42. #import <UIKit/UIKit.h>
  43. int gl_view_base_fb;
  44. bool gles3_available = true;
  45. @implementation GodotOpenGLLayer {
  46. // The pixel dimensions of the backbuffer
  47. GLint backingWidth;
  48. GLint backingHeight;
  49. EAGLContext *context;
  50. GLuint viewRenderbuffer, viewFramebuffer;
  51. GLuint depthRenderbuffer;
  52. }
  53. - (void)initializeDisplayLayer {
  54. // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
  55. self.opaque = YES;
  56. self.drawableProperties = [NSDictionary
  57. dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
  58. kEAGLDrawablePropertyRetainedBacking,
  59. kEAGLColorFormatRGBA8,
  60. kEAGLDrawablePropertyColorFormat,
  61. nil];
  62. bool fallback_gl2 = false;
  63. // Create a GL ES 3 context based on the gl driver from project settings
  64. if (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3") {
  65. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
  66. NSLog(@"Setting up an OpenGL ES 3.0 context. Based on Project Settings \"rendering/quality/driver/driver_name\"");
  67. if (!context && GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
  68. gles3_available = false;
  69. fallback_gl2 = true;
  70. NSLog(@"Failed to create OpenGL ES 3.0 context. Falling back to OpenGL ES 2.0");
  71. }
  72. }
  73. // Create GL ES 2 context
  74. if (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES2" || fallback_gl2) {
  75. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  76. NSLog(@"Setting up an OpenGL ES 2.0 context.");
  77. if (!context) {
  78. NSLog(@"Failed to create OpenGL ES 2.0 context!");
  79. return;
  80. }
  81. }
  82. if (![EAGLContext setCurrentContext:context]) {
  83. NSLog(@"Failed to set EAGLContext!");
  84. return;
  85. }
  86. if (![self createFramebuffer]) {
  87. NSLog(@"Failed to create frame buffer!");
  88. return;
  89. }
  90. }
  91. - (void)layoutDisplayLayer {
  92. [EAGLContext setCurrentContext:context];
  93. [self destroyFramebuffer];
  94. [self createFramebuffer];
  95. }
  96. - (void)startRenderDisplayLayer {
  97. [EAGLContext setCurrentContext:context];
  98. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  99. }
  100. - (void)stopRenderDisplayLayer {
  101. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  102. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  103. #ifdef DEBUG_ENABLED
  104. GLenum err = glGetError();
  105. if (err) {
  106. NSLog(@"DrawView: %x error", err);
  107. }
  108. #endif
  109. }
  110. - (void)dealloc {
  111. if ([EAGLContext currentContext] == context) {
  112. [EAGLContext setCurrentContext:nil];
  113. }
  114. if (context) {
  115. context = nil;
  116. }
  117. }
  118. - (BOOL)createFramebuffer {
  119. // Generate IDs for a framebuffer object and a color renderbuffer
  120. glGenFramebuffersOES(1, &viewFramebuffer);
  121. glGenRenderbuffersOES(1, &viewRenderbuffer);
  122. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  123. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  124. // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
  125. // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
  126. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self];
  127. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  128. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  129. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  130. // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
  131. glGenRenderbuffersOES(1, &depthRenderbuffer);
  132. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  133. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  134. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  135. if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  136. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  137. return NO;
  138. }
  139. if (OS::get_singleton()) {
  140. OS::VideoMode vm;
  141. vm.fullscreen = true;
  142. vm.width = backingWidth;
  143. vm.height = backingHeight;
  144. vm.resizable = false;
  145. OS::get_singleton()->set_video_mode(vm);
  146. OSIPhone::get_singleton()->set_base_framebuffer(viewFramebuffer);
  147. }
  148. gl_view_base_fb = viewFramebuffer;
  149. return YES;
  150. }
  151. // Clean up any buffers we have allocated.
  152. - (void)destroyFramebuffer {
  153. glDeleteFramebuffersOES(1, &viewFramebuffer);
  154. viewFramebuffer = 0;
  155. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  156. viewRenderbuffer = 0;
  157. if (depthRenderbuffer) {
  158. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  159. depthRenderbuffer = 0;
  160. }
  161. }
  162. @end