UnityAppController+Rendering.mm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "UnityAppController+Rendering.h"
  2. #include "UnityAppController+ViewHandling.h"
  3. #include "Unity/InternalProfiler.h"
  4. #include "Unity/UnityMetalSupport.h"
  5. #include "Unity/DisplayManager.h"
  6. #include "UI/UnityView.h"
  7. #include <dlfcn.h>
  8. // _glesContextCreated was renamed to _renderingInited
  9. extern bool _renderingInited;
  10. extern bool _unityAppReady;
  11. extern bool _skipPresent;
  12. extern bool _didResignActive;
  13. static int _renderingAPI = 0;
  14. static int SelectRenderingAPIImpl();
  15. static bool _enableRunLoopAcceptInput = false;
  16. @implementation UnityAppController (Rendering)
  17. - (void)createDisplayLink
  18. {
  19. _displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(repaintDisplayLink)];
  20. [self callbackFramerateChange: -1];
  21. [_displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes];
  22. }
  23. - (void)destroyDisplayLink
  24. {
  25. [_displayLink invalidate];
  26. _displayLink = nil;
  27. }
  28. - (void)repaintDisplayLink
  29. {
  30. if (!_didResignActive)
  31. {
  32. UnityDisplayLinkCallback(_displayLink.timestamp);
  33. [self repaint];
  34. }
  35. }
  36. - (void)repaint
  37. {
  38. if (_unityView.skipRendering)
  39. return;
  40. #if UNITY_SUPPORT_ROTATION
  41. [self checkOrientationRequest];
  42. #endif
  43. [_unityView recreateRenderingSurfaceIfNeeded];
  44. [_unityView processKeyboard];
  45. UnityDeliverUIEvents();
  46. if (!UnityIsPaused())
  47. UnityRepaint();
  48. }
  49. - (void)callbackGfxInited
  50. {
  51. InitRendering();
  52. _renderingInited = true;
  53. [self shouldAttachRenderDelegate];
  54. [_unityView recreateRenderingSurface];
  55. [_renderDelegate mainDisplayInited: _mainDisplay.surface];
  56. _mainDisplay.surface->allowScreenshot = 1;
  57. }
  58. - (void)callbackPresent:(const UnityFrameStats*)frameStats
  59. {
  60. if (_skipPresent)
  61. return;
  62. // metal needs special processing, because in case of airplay we need extra command buffers to present non-main screen drawables
  63. if (UnitySelectedRenderingAPI() == apiMetal)
  64. {
  65. [[DisplayManager Instance].mainDisplay present];
  66. #if !PLATFORM_VISIONOS
  67. [[DisplayManager Instance] enumerateNonMainDisplaysWithBlock:^(DisplayConnection* conn) {
  68. PreparePresentNonMainScreenMTL((UnityDisplaySurfaceMTL*)conn.surface);
  69. }];
  70. #endif
  71. }
  72. else
  73. {
  74. [[DisplayManager Instance] present];
  75. }
  76. Profiler_FramePresent(frameStats);
  77. }
  78. - (void)callbackFramerateChange:(int)targetFPS
  79. {
  80. if (targetFPS <= 0)
  81. targetFPS = UnityGetTargetFPS();
  82. #if !PLATFORM_VISIONOS
  83. // on tvos it is possible to start application without a screen attached
  84. // alas, mainScreen is set in this case, but the values provided are bogus
  85. // and in the case of maxFPS = 0 we will end up in endless recursion
  86. const int maxFPS = (int)[UIScreen mainScreen].maximumFramesPerSecond;
  87. #else
  88. // hardcode for visionOS?
  89. const int maxFPS = 90;
  90. #endif
  91. if (maxFPS > 0 && targetFPS > maxFPS)
  92. {
  93. targetFPS = maxFPS;
  94. // note that this changes FPS, resulting in UnityFramerateChangeCallback call, calling this method recursively recursively
  95. UnitySetTargetFPS(targetFPS);
  96. return;
  97. }
  98. _enableRunLoopAcceptInput = (targetFPS == maxFPS && UnityDeviceCPUCount() > 1);
  99. if (@available(iOS 15.0, tvOS 15.0, *))
  100. _displayLink.preferredFrameRateRange = CAFrameRateRangeMake(targetFPS, targetFPS, targetFPS);
  101. else
  102. _displayLink.preferredFramesPerSecond = targetFPS;
  103. }
  104. - (void)selectRenderingAPI
  105. {
  106. NSAssert(_renderingAPI == 0, @"[UnityAppController selectRenderingApi] called twice");
  107. _renderingAPI = SelectRenderingAPIImpl();
  108. }
  109. - (UnityRenderingAPI)renderingAPI
  110. {
  111. NSAssert(_renderingAPI != 0, @"[UnityAppController renderingAPI] called before [UnityAppController selectRenderingApi]");
  112. return (UnityRenderingAPI)_renderingAPI;
  113. }
  114. @end
  115. extern "C" void UnityGfxInitedCallback()
  116. {
  117. [GetAppController() callbackGfxInited];
  118. }
  119. extern "C" void UnityPresentContextCallback(struct UnityFrameStats const* unityFrameStats)
  120. {
  121. [GetAppController() callbackPresent: unityFrameStats];
  122. }
  123. extern "C" void UnityFramerateChangeCallback(int targetFPS)
  124. {
  125. [GetAppController() callbackFramerateChange: targetFPS];
  126. }
  127. static NSBundle* _MetalBundle = nil;
  128. static id<MTLDevice> _MetalDevice = nil;
  129. static bool IsMetalSupported(int /*api*/)
  130. {
  131. _MetalBundle = [NSBundle bundleWithPath: @"/System/Library/Frameworks/Metal.framework"];
  132. if (_MetalBundle)
  133. {
  134. [_MetalBundle load];
  135. _MetalDevice = ((MTLCreateSystemDefaultDeviceFunc)::dlsym(dlopen(0, RTLD_LOCAL | RTLD_LAZY), "MTLCreateSystemDefaultDevice"))();
  136. if (_MetalDevice)
  137. return true;
  138. }
  139. [_MetalBundle unload];
  140. return false;
  141. }
  142. static int SelectRenderingAPIImpl()
  143. {
  144. const int api = UnityGetRenderingAPI();
  145. if (api == apiMetal && IsMetalSupported(0))
  146. return api;
  147. #if TARGET_IPHONE_SIMULATOR || TARGET_TVOS_SIMULATOR
  148. printf_console("On Simulator, Metal is supported only from iOS 13, and it requires at least macOS 10.15 and Xcode 11. Setting no graphics device.\n");
  149. #endif
  150. return apiNoGraphics;
  151. }
  152. extern "C" NSBundle* UnityGetMetalBundle()
  153. {
  154. return _MetalBundle;
  155. }
  156. extern "C" MTLDeviceRef UnityGetMetalDevice() { return _MetalDevice; }
  157. extern "C" MTLCommandQueueRef UnityGetMetalCommandQueue() { return ((UnityDisplaySurfaceMTL*)GetMainDisplaySurface())->commandQueue; }
  158. extern "C" int UnitySelectedRenderingAPI() { return _renderingAPI; }
  159. // deprecated and no longer used by unity itself (will soon be removed)
  160. extern "C" MTLCommandQueueRef UnityGetMetalDrawableCommandQueue() { return UnityGetMetalCommandQueue(); }
  161. extern "C" UnityRenderBufferHandle UnityBackbufferColor() { return GetMainDisplaySurface()->unityColorBuffer; }
  162. extern "C" UnityRenderBufferHandle UnityBackbufferDepth() { return GetMainDisplaySurface()->unityDepthBuffer; }
  163. extern "C" void DisplayManagerEndFrameRendering() { [[DisplayManager Instance] endFrameRendering]; }
  164. extern "C" void UnityPrepareScreenshot() { UnitySetRenderTarget(GetMainDisplaySurface()->unityColorBuffer, GetMainDisplaySurface()->unityDepthBuffer); }
  165. extern "C" void UnityRepaint()
  166. {
  167. @autoreleasepool
  168. {
  169. Profiler_FrameStart();
  170. if (UnityIsBatchmode())
  171. UnityBatchPlayerLoop();
  172. else
  173. UnityPlayerLoop();
  174. Profiler_FrameEnd();
  175. }
  176. }