UnityAppController+Rendering.mm
8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "UnityAppController+Rendering.h"
#include "UnityAppController+ViewHandling.h"
#include "Unity/InternalProfiler.h"
#include "Unity/UnityMetalSupport.h"
#include "Unity/DisplayManager.h"
#include "Unity/EAGLContextHelper.h"
#include "UI/UnityView.h"
#include <dlfcn.h>
// On some devices presenting render buffer may sporadically take long time to complete even with very simple scenes.
// In these cases display link still fires at steady frame rate but input processing becomes stuttering.
// As a workaround this switch disables display link during rendering a frame.
// If you are running a GPU bound scene and experience frame drop you may want to disable this switch.
#define ENABLE_DISPLAY_LINK_PAUSING 1
#define ENABLE_RUNLOOP_ACCEPT_INPUT 1
// _glesContextCreated was renamed to _renderingInited
extern bool _renderingInited;
extern bool _unityAppReady;
extern bool _skipPresent;
extern bool _didResignActive;
static int _renderingAPI = 0;
static int SelectRenderingAPIImpl();
static bool _enableRunLoopAcceptInput = false;
@implementation UnityAppController (Rendering)
- (void)createDisplayLink
{
_displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(repaintDisplayLink)];
[self callbackFramerateChange: -1];
[_displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes];
}
- (void)destroyDisplayLink
{
[_displayLink invalidate];
_displayLink = nil;
}
- (void)processTouchEvents
{
// On multicore devices running at 60 FPS some touch event delivery isn't properly interleaved with graphical frames.
// Running additional run loop here improves event handling in those cases.
// Passing here an NSDate from the past invokes run loop only once.
#if ENABLE_RUNLOOP_ACCEPT_INPUT
// We get "NSInternalInconsistencyException: unexpected start state" exception if there are events queued and app is
// going to background at the same time. This happens when we render additional frame after receiving
// applicationWillResignActive. So check if we are supposed to ignore input.
bool ignoreInput = [[UIApplication sharedApplication] isIgnoringInteractionEvents];
if (!ignoreInput && _enableRunLoopAcceptInput)
{
static NSDate* past = [NSDate dateWithTimeIntervalSince1970: 0]; // the oldest date we can get
[[NSRunLoop currentRunLoop] acceptInputForMode: NSDefaultRunLoopMode beforeDate: past];
}
#endif
}
- (void)repaintDisplayLink
{
#if ENABLE_DISPLAY_LINK_PAUSING
_displayLink.paused = YES;
#endif
if (!_didResignActive)
{
[self repaint];
[self processTouchEvents];
}
#if ENABLE_DISPLAY_LINK_PAUSING
_displayLink.paused = NO;
#endif
}
- (void)repaint
{
#if UNITY_SUPPORT_ROTATION
[self checkOrientationRequest];
#endif
[_unityView recreateRenderingSurfaceIfNeeded];
[_unityView processKeyboard];
UnityDeliverUIEvents();
if (!UnityIsPaused())
UnityRepaint();
}
- (void)callbackGfxInited
{
InitRendering();
_renderingInited = true;
[self shouldAttachRenderDelegate];
[_renderDelegate mainDisplayInited: _mainDisplay.surface];
[_unityView recreateRenderingSurface];
_mainDisplay.surface->allowScreenshot = 1;
}
- (void)callbackPresent:(const UnityFrameStats*)frameStats
{
if (_skipPresent || _didResignActive)
return;
// metal needs special processing, because in case of airplay we need extra command buffers to present non-main screen drawables
if (UnitySelectedRenderingAPI() == apiMetal)
{
#if UNITY_CAN_USE_METAL
[[DisplayManager Instance].mainDisplay present];
[[DisplayManager Instance] enumerateNonMainDisplaysWithBlock:^(DisplayConnection* conn) {
PreparePresentNonMainScreenMTL((UnityDisplaySurfaceMTL*)conn.surface);
}];
#endif
}
else
{
[[DisplayManager Instance] present];
}
Profiler_FramePresent(frameStats);
}
- (void)callbackFramerateChange:(int)targetFPS
{
int maxFPS = (int)[UIScreen mainScreen].maximumFramesPerSecond;
if (targetFPS <= 0)
targetFPS = UnityGetTargetFPS();
if (targetFPS > maxFPS)
targetFPS = maxFPS;
_enableRunLoopAcceptInput = (targetFPS == maxFPS && UnityDeviceCPUCount() > 1);
_displayLink.preferredFramesPerSecond = targetFPS;
}
- (void)selectRenderingAPI
{
NSAssert(_renderingAPI == 0, @"[UnityAppController selectRenderingApi] called twice");
_renderingAPI = SelectRenderingAPIImpl();
}
- (UnityRenderingAPI)renderingAPI
{
NSAssert(_renderingAPI != 0, @"[UnityAppController renderingAPI] called before [UnityAppController selectRenderingApi]");
return (UnityRenderingAPI)_renderingAPI;
}
@end
extern "C" void UnityGfxInitedCallback()
{
[GetAppController() callbackGfxInited];
}
extern "C" void UnityPresentContextCallback(struct UnityFrameStats const* unityFrameStats)
{
[GetAppController() callbackPresent: unityFrameStats];
}
extern "C" void UnityFramerateChangeCallback(int targetFPS)
{
[GetAppController() callbackFramerateChange: targetFPS];
}
extern "C" void UnityInitMainScreenRenderingCallback()
{
[GetAppController().mainDisplay initRendering];
}
static NSBundle* _MetalBundle = nil;
static id<MTLDevice> _MetalDevice = nil;
static EAGLContext* _GlesContext = nil;
static bool IsMetalSupported(int /*api*/)
{
// support for metal on the simulator is available starting with unity 2020.1
// for now explicitly disable checking for Metal.framework as it is available on Xcode11 simulator
#if !TARGET_IPHONE_SIMULATOR && !TARGET_TVOS_SIMULATOR
_MetalBundle = [NSBundle bundleWithPath: @"/System/Library/Frameworks/Metal.framework"];
if (_MetalBundle)
{
[_MetalBundle load];
_MetalDevice = ((MTLCreateSystemDefaultDeviceFunc)::dlsym(dlopen(0, RTLD_LOCAL | RTLD_LAZY), "MTLCreateSystemDefaultDevice"))();
if (_MetalDevice)
return true;
}
[_MetalBundle unload];
#endif
return false;
}
static bool IsGlesSupported(int api)
{
_GlesContext = [[EAGLContext alloc] initWithAPI: (EAGLRenderingAPI)api];
return _GlesContext != nil;
}
typedef bool(*CheckSupportedFunc)(int);
static int SelectRenderingAPIImpl()
{
// Get list of graphics APIs to try from player settings
const int kMaxAPIs = 3;
int apis[kMaxAPIs];
const int apiCount = UnityGetRenderingAPIs(kMaxAPIs, apis);
// Go over them and try each
for (int i = 0; i < apiCount; ++i)
{
int api = apis[i];
// Metal
if (api == apiMetal)
{
#if UNITY_CAN_USE_METAL
if (!IsMetalSupported(0))
continue;
return api;
#else
continue;
#endif
}
// GLES3
if (api == apiOpenGLES3)
{
if (!IsGlesSupported(kEAGLRenderingAPIOpenGLES3))
continue;
return api;
}
// GLES2
if (api == apiOpenGLES2)
{
if (!IsGlesSupported(kEAGLRenderingAPIOpenGLES2))
continue;
return api;
}
}
return 0;
}
extern "C" NSBundle* UnityGetMetalBundle()
{
return _MetalBundle;
}
extern "C" MTLDeviceRef UnityGetMetalDevice() { return _MetalDevice; }
extern "C" MTLCommandQueueRef UnityGetMetalCommandQueue() { return ((UnityDisplaySurfaceMTL*)GetMainDisplaySurface())->commandQueue; }
extern "C" MTLCommandQueueRef UnityGetMetalDrawableCommandQueue() { return ((UnityDisplaySurfaceMTL*)GetMainDisplaySurface())->drawableCommandQueue; }
extern "C" EAGLContext* UnityGetDataContextEAGL()
{
return _GlesContext;
}
extern "C" int UnitySelectedRenderingAPI() { return _renderingAPI; }
extern "C" UnityRenderBufferHandle UnityBackbufferColor() { return GetMainDisplaySurface()->unityColorBuffer; }
extern "C" UnityRenderBufferHandle UnityBackbufferDepth() { return GetMainDisplaySurface()->unityDepthBuffer; }
extern "C" void DisplayManagerEndFrameRendering() { [[DisplayManager Instance] endFrameRendering]; }
extern "C" void UnityPrepareScreenshot() { UnitySetRenderTarget(GetMainDisplaySurface()->unityColorBuffer, GetMainDisplaySurface()->unityDepthBuffer); }
extern "C" void UnityRepaint()
{
@autoreleasepool
{
// this will handle running on metal just fine (nop)
EAGLContextSetCurrentAutoRestore autorestore(GetMainDisplaySurface());
Profiler_FrameStart();
UnityInputProcess();
UnityPlayerLoop();
Profiler_FrameEnd();
}
}