display_layer.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**************************************************************************/
  2. /* display_layer.mm */
  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. #import "display_layer.h"
  31. #import "display_server_ios.h"
  32. #import "os_ios.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/os/keyboard.h"
  35. #include "main/main.h"
  36. #include "servers/audio_server.h"
  37. #import <AudioToolbox/AudioServices.h>
  38. #import <GameController/GameController.h>
  39. #import <OpenGLES/EAGL.h>
  40. #import <OpenGLES/ES1/gl.h>
  41. #import <OpenGLES/ES1/glext.h>
  42. #import <QuartzCore/QuartzCore.h>
  43. #import <UIKit/UIKit.h>
  44. @implementation GodotMetalLayer
  45. - (void)initializeDisplayLayer {
  46. #if defined(TARGET_OS_SIMULATOR) && TARGET_OS_SIMULATOR
  47. if (@available(iOS 13, *)) {
  48. // Simulator supports Metal since iOS 13
  49. } else {
  50. NSLog(@"iOS Simulator prior to iOS 13 does not support Metal rendering.");
  51. }
  52. #endif
  53. }
  54. - (void)layoutDisplayLayer {
  55. }
  56. - (void)startRenderDisplayLayer {
  57. }
  58. - (void)stopRenderDisplayLayer {
  59. }
  60. @end
  61. @implementation GodotOpenGLLayer {
  62. // The pixel dimensions of the backbuffer
  63. GLint backingWidth;
  64. GLint backingHeight;
  65. EAGLContext *context;
  66. GLuint viewRenderbuffer, viewFramebuffer;
  67. GLuint depthRenderbuffer;
  68. }
  69. - (void)initializeDisplayLayer {
  70. // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
  71. self.opaque = YES;
  72. self.drawableProperties = [NSDictionary
  73. dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
  74. kEAGLDrawablePropertyRetainedBacking,
  75. kEAGLColorFormatRGBA8,
  76. kEAGLDrawablePropertyColorFormat,
  77. nil];
  78. // Create GL ES 3 context
  79. if (GLOBAL_GET("rendering/renderer/rendering_method") == "gl_compatibility") {
  80. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
  81. NSLog(@"Setting up an OpenGL ES 3.0 context.");
  82. if (!context) {
  83. NSLog(@"Failed to create OpenGL ES 3.0 context!");
  84. return;
  85. }
  86. }
  87. if (![EAGLContext setCurrentContext:context]) {
  88. NSLog(@"Failed to set EAGLContext!");
  89. return;
  90. }
  91. if (![self createFramebuffer]) {
  92. NSLog(@"Failed to create frame buffer!");
  93. return;
  94. }
  95. }
  96. - (void)layoutDisplayLayer {
  97. [EAGLContext setCurrentContext:context];
  98. [self destroyFramebuffer];
  99. [self createFramebuffer];
  100. }
  101. - (void)startRenderDisplayLayer {
  102. [EAGLContext setCurrentContext:context];
  103. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  104. }
  105. - (void)stopRenderDisplayLayer {
  106. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  107. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  108. #ifdef DEBUG_ENABLED
  109. GLenum err = glGetError();
  110. if (err) {
  111. NSLog(@"DrawView: %x error", err);
  112. }
  113. #endif
  114. }
  115. - (void)dealloc {
  116. if ([EAGLContext currentContext] == context) {
  117. [EAGLContext setCurrentContext:nil];
  118. }
  119. if (context) {
  120. context = nil;
  121. }
  122. }
  123. - (BOOL)createFramebuffer {
  124. glGenFramebuffersOES(1, &viewFramebuffer);
  125. glGenRenderbuffersOES(1, &viewRenderbuffer);
  126. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  127. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  128. // This call associates the storage for the current render buffer with the EAGLDrawable (our CAself)
  129. // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
  130. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self];
  131. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  132. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  133. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  134. // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
  135. glGenRenderbuffersOES(1, &depthRenderbuffer);
  136. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  137. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  138. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  139. if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  140. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  141. return NO;
  142. }
  143. GLES3::TextureStorage::system_fbo = viewFramebuffer;
  144. return YES;
  145. }
  146. // Clean up any buffers we have allocated.
  147. - (void)destroyFramebuffer {
  148. GLES3::TextureStorage::system_fbo = 0;
  149. glDeleteFramebuffersOES(1, &viewFramebuffer);
  150. viewFramebuffer = 0;
  151. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  152. viewRenderbuffer = 0;
  153. if (depthRenderbuffer) {
  154. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  155. depthRenderbuffer = 0;
  156. }
  157. }
  158. @end