display_layer.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/config/project_settings.h"
  32. #include "core/os/keyboard.h"
  33. #include "display_server_iphone.h"
  34. #include "main/main.h"
  35. #include "os_iphone.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)renderDisplayLayer {
  57. }
  58. @end
  59. @implementation GodotOpenGLLayer {
  60. // The pixel dimensions of the backbuffer
  61. GLint backingWidth;
  62. GLint backingHeight;
  63. EAGLContext *context;
  64. GLuint viewRenderbuffer, viewFramebuffer;
  65. GLuint depthRenderbuffer;
  66. }
  67. - (void)initializeDisplayLayer {
  68. // Get our backing layer
  69. // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
  70. self.opaque = YES;
  71. self.drawableProperties = [NSDictionary
  72. dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
  73. kEAGLDrawablePropertyRetainedBacking,
  74. kEAGLColorFormatRGBA8,
  75. kEAGLDrawablePropertyColorFormat,
  76. nil];
  77. // FIXME: Add Vulkan support via MoltenVK. Add fallback code back?
  78. // Create GL ES 2 context
  79. if (GLOBAL_GET("rendering/driver/driver_name") == "opengl3") {
  80. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  81. NSLog(@"Setting up an OpenGL ES 2.0 context.");
  82. if (!context) {
  83. NSLog(@"Failed to create OpenGL ES 2.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)renderDisplayLayer {
  102. [EAGLContext setCurrentContext:context];
  103. }
  104. - (void)dealloc {
  105. if ([EAGLContext currentContext] == context) {
  106. [EAGLContext setCurrentContext:nil];
  107. }
  108. if (context) {
  109. context = nil;
  110. }
  111. }
  112. - (BOOL)createFramebuffer {
  113. glGenFramebuffersOES(1, &viewFramebuffer);
  114. glGenRenderbuffersOES(1, &viewRenderbuffer);
  115. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  116. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  117. // This call associates the storage for the current render buffer with the EAGLDrawable (our CAself)
  118. // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
  119. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self];
  120. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  121. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  122. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  123. // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
  124. glGenRenderbuffersOES(1, &depthRenderbuffer);
  125. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  126. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  127. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  128. if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  129. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  130. return NO;
  131. }
  132. // if (OS::get_singleton()) {
  133. // OS::VideoMode vm;
  134. // vm.fullscreen = true;
  135. // vm.width = backingWidth;
  136. // vm.height = backingHeight;
  137. // vm.resizable = false;
  138. // OS::get_singleton()->set_video_mode(vm);
  139. // OSIPhone::get_singleton()->set_base_framebuffer(viewFramebuffer);
  140. // };
  141. // gl_view_base_fb = viewFramebuffer;
  142. return YES;
  143. }
  144. // Clean up any buffers we have allocated.
  145. - (void)destroyFramebuffer {
  146. glDeleteFramebuffersOES(1, &viewFramebuffer);
  147. viewFramebuffer = 0;
  148. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  149. viewRenderbuffer = 0;
  150. if (depthRenderbuffer) {
  151. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  152. depthRenderbuffer = 0;
  153. }
  154. }
  155. @end