MetalHelper.mm
16 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "UnityTrampolineCompatibility.h"
#include "UnityRendering.h"
#if UNITY_CAN_USE_METAL
#include "UnityMetalSupport.h"
#include <QuartzCore/QuartzCore.h>
#include <libkern/OSAtomic.h>
#if UNITY_TRAMPOLINE_IN_USE
#include "UnityAppController.h"
#include "CVTextureCache.h"
#endif
#include "ObjCRuntime.h"
#if UNITY_TRAMPOLINE_IN_USE
static Class MTLTextureDescriptorClass;
#else
extern Class MTLTextureDescriptorClass;
#endif
extern "C" void InitRenderingMTL()
{
#if UNITY_TRAMPOLINE_IN_USE
extern bool _supportsMSAA;
_supportsMSAA = true;
MTLTextureDescriptorClass = [UnityGetMetalBundle() classNamed: @"MTLTextureDescriptor"];
#endif
}
static MTLPixelFormat GetColorFormatForSurface(const UnityDisplaySurfaceMTL* surface)
{
MTLPixelFormat colorFormat = surface->srgb ? MTLPixelFormatBGRA8Unorm_sRGB : MTLPixelFormatBGRA8Unorm;
#if PLATFORM_IOS || PLATFORM_TVOS
if (surface->wideColor && UnityIsWideColorSupported())
colorFormat = surface->srgb ? MTLPixelFormatBGR10_XR_sRGB : MTLPixelFormatBGR10_XR;
#elif PLATFORM_OSX
if (surface->wideColor)
colorFormat = MTLPixelFormatRGBA16Float;
#endif
return colorFormat;
}
extern "C" void CreateSystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface)
{
DestroySystemRenderingSurfaceMTL(surface);
MTLPixelFormat colorFormat = GetColorFormatForSurface(surface);
surface->layer.presentsWithTransaction = NO;
surface->layer.drawsAsynchronously = YES;
#if !PLATFORM_OSX
if (UnityPreserveFramebufferAlpha())
{
const CGFloat components[] = {1.0f, 1.0f, 1.0f, 0.0f};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(colorSpace, components);
surface->layer.opaque = NO;
surface->layer.backgroundColor = color;
CGColorRelease(color);
CGColorSpaceRelease(colorSpace);
}
#endif
#if PLATFORM_OSX
surface->layer.opaque = YES;
MetalUpdateDisplaySync();
#endif
#if PLATFORM_OSX
CGColorSpaceRef colorSpaceRef = nil;
if (surface->wideColor)
colorSpaceRef = CGColorSpaceCreateWithName(surface->srgb ? kCGColorSpaceExtendedLinearSRGB : kCGColorSpaceExtendedSRGB);
else
colorSpaceRef = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
surface->layer.colorspace = colorSpaceRef;
CGColorSpaceRelease(colorSpaceRef);
#endif
// Update the native screen resolution
UnityUpdateDrawableSize(surface);
surface->layer.device = surface->device;
surface->layer.pixelFormat = colorFormat;
surface->layer.framebufferOnly = (surface->framebufferOnly != 0);
surface->colorFormat = colorFormat;
MTLTextureDescriptor* txDesc = [MTLTextureDescriptorClass texture2DDescriptorWithPixelFormat: colorFormat width: surface->systemW height: surface->systemH mipmapped: NO];
#if PLATFORM_OSX
txDesc.resourceOptions = MTLResourceCPUCacheModeDefaultCache | MTLResourceStorageModeManaged;
#endif
txDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
@synchronized(surface->layer)
{
OSAtomicCompareAndSwap32Barrier(0, 0, &surface->bufferCompleted);
OSAtomicCompareAndSwap32Barrier(0, 0, &surface->bufferSwap);
for (int i = 0; i < kUnityNumOffscreenSurfaces; i++)
{
// Allocating a proxy texture is cheap until it's being rendered to and the GPU driver does allocation
surface->drawableProxyRT[i] = [surface->device newTextureWithDescriptor: txDesc];
surface->drawableProxyRT[i].label = @"DrawableProxy";
// We mostly need the proxy for some of its state like width, height and pixelFormat (not the actual memory) before we can get the real drawable
// Making it empty discards its backed memory/contents
for (int i = 0; i < kUnityNumOffscreenSurfaces; ++i)
[surface->drawableProxyRT[i] setPurgeableState: MTLPurgeableStateEmpty];
}
}
}
extern "C" void CreateRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface)
{
DestroyRenderingSurfaceMTL(surface);
MTLPixelFormat colorFormat = GetColorFormatForSurface(surface);
const int w = surface->targetW, h = surface->targetH;
if (w != surface->systemW || h != surface->systemH || surface->useCVTextureCache)
{
#if PLATFORM_IOS || PLATFORM_TVOS
if (surface->useCVTextureCache)
surface->cvTextureCache = CreateCVTextureCache();
if (surface->cvTextureCache)
{
surface->cvTextureCacheTexture = CreateReadableRTFromCVTextureCache(surface->cvTextureCache, surface->targetW, surface->targetH, &surface->cvPixelBuffer);
surface->targetColorRT = GetMetalTextureFromCVTextureCache(surface->cvTextureCacheTexture);
}
else
#endif
{
MTLTextureDescriptor* txDesc = [MTLTextureDescriptorClass new];
txDesc.textureType = MTLTextureType2D;
txDesc.width = w;
txDesc.height = h;
txDesc.depth = 1;
txDesc.pixelFormat = colorFormat;
txDesc.arrayLength = 1;
txDesc.mipmapLevelCount = 1;
#if PLATFORM_OSX
txDesc.resourceOptions = MTLResourceCPUCacheModeDefaultCache | MTLResourceStorageModeManaged;
#endif
txDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
surface->targetColorRT = [surface->device newTextureWithDescriptor: txDesc];
}
surface->targetColorRT.label = @"targetColorRT";
}
if (surface->msaaSamples > 1)
{
MTLTextureDescriptor* txDesc = [MTLTextureDescriptorClass new];
txDesc.textureType = MTLTextureType2DMultisample;
txDesc.width = w;
txDesc.height = h;
txDesc.depth = 1;
txDesc.pixelFormat = colorFormat;
txDesc.arrayLength = 1;
txDesc.mipmapLevelCount = 1;
txDesc.sampleCount = surface->msaaSamples;
#if PLATFORM_OSX
txDesc.resourceOptions = MTLResourceCPUCacheModeDefaultCache | MTLResourceStorageModePrivate;
#endif
txDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
if (![surface->device supportsTextureSampleCount: txDesc.sampleCount])
txDesc.sampleCount = 4;
surface->targetAAColorRT = [surface->device newTextureWithDescriptor: txDesc];
surface->targetAAColorRT.label = @"targetAAColorRT";
}
}
extern "C" void DestroyRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface)
{
surface->targetColorRT = nil;
surface->targetAAColorRT = nil;
if (surface->cvTextureCacheTexture)
CFRelease(surface->cvTextureCacheTexture);
if (surface->cvPixelBuffer)
CFRelease(surface->cvPixelBuffer);
if (surface->cvTextureCache)
CFRelease(surface->cvTextureCache);
surface->cvTextureCache = 0;
}
extern "C" void CreateSharedDepthbufferMTL(UnityDisplaySurfaceMTL* surface)
{
DestroySharedDepthbufferMTL(surface);
if (surface->disableDepthAndStencil)
return;
MTLPixelFormat pixelFormat = MTLPixelFormatDepth32Float_Stencil8;
MTLTextureDescriptor* depthTexDesc = [MTLTextureDescriptorClass texture2DDescriptorWithPixelFormat: pixelFormat width: surface->targetW height: surface->targetH mipmapped: NO];
#if PLATFORM_OSX
depthTexDesc.resourceOptions = MTLResourceCPUCacheModeDefaultCache | MTLResourceStorageModePrivate;
#endif
#if PLATFORM_IOS || PLATFORM_TVOS
if (surface->memorylessDepth)
depthTexDesc.storageMode = MTLStorageModeMemoryless;
#endif
depthTexDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
if (surface->msaaSamples > 1)
{
depthTexDesc.textureType = MTLTextureType2DMultisample;
depthTexDesc.sampleCount = surface->msaaSamples;
if (![surface->device supportsTextureSampleCount: depthTexDesc.sampleCount])
depthTexDesc.sampleCount = 4;
}
surface->depthRB = [surface->device newTextureWithDescriptor: depthTexDesc];
surface->stencilRB = surface->depthRB;
}
extern "C" void DestroySharedDepthbufferMTL(UnityDisplaySurfaceMTL* surface)
{
surface->depthRB = nil;
surface->stencilRB = nil;
}
extern "C" void CreateUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface)
{
UnityRenderBufferDesc sys_desc = { surface->systemW, surface->systemH, 1, 1, 1 };
UnityRenderBufferDesc tgt_desc = { surface->targetW, surface->targetH, 1, (unsigned int)surface->msaaSamples, 1 };
// To avoid race condition with EndFrameRenderingMTL where systemColorRB is nulled we store it here
MTLTextureRef systemColorRB = surface->drawableProxyRT[0];
surface->systemColorRB = systemColorRB;
// we could unify all of it with ugly chain of ternary operators but what if karma exists?
if (surface->targetColorRT)
{
// render to interim RT: we do NOT need to request drawable
MTLTextureRef texRender = surface->targetAAColorRT ? surface->targetAAColorRT : surface->targetColorRT;
MTLTextureRef texResolve = surface->targetAAColorRT ? surface->targetColorRT : nil;
surface->unityColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->unityColorBuffer, texRender, texResolve, &tgt_desc, nil);
}
else
{
// render to backbuffer directly: we will request drawable hence we need to pass surface
MTLTextureRef texRender = surface->targetAAColorRT ? surface->targetAAColorRT : systemColorRB;
MTLTextureRef texResolve = surface->targetAAColorRT ? systemColorRB : nil;
surface->unityColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->unityColorBuffer, texRender, texResolve, &tgt_desc, surface);
}
if (surface->depthRB)
surface->unityDepthBuffer = UnityCreateExternalDepthSurfaceMTL(surface->unityDepthBuffer, surface->depthRB, surface->stencilRB, &tgt_desc);
else
surface->unityDepthBuffer = UnityCreateDummySurface(surface->unityDepthBuffer, false, &tgt_desc);
surface->systemColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->systemColorBuffer, systemColorRB, nil, &sys_desc, surface);
surface->systemDepthBuffer = UnityCreateDummySurface(surface->systemDepthBuffer, false, &sys_desc);
}
extern "C" void DestroySystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface)
{
// before we needed to nil surface->systemColorRB (to release drawable we get from the view)
// but after we switched to proxy rt this is no longer needed
// even more it is harmful when running rendering on another thread (as is default now)
// as on render thread we do StartFrameRenderingMTL/AcquireDrawableMTL/EndFrameRenderingMTL
// and DestroySystemRenderingSurfaceMTL comes on main thread so we might end up with race condition for no reason
}
extern "C" void DestroyUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface)
{
UnityDestroyExternalSurface(surface->unityColorBuffer);
UnityDestroyExternalSurface(surface->systemColorBuffer);
surface->unityColorBuffer = surface->systemColorBuffer = 0;
UnityDestroyExternalSurface(surface->unityDepthBuffer);
UnityDestroyExternalSurface(surface->systemDepthBuffer);
surface->unityDepthBuffer = surface->systemDepthBuffer = 0;
}
extern "C" void PreparePresentMTL(UnityDisplaySurfaceMTL* surface)
{
if (surface->targetColorRT)
UnityBlitToBackbuffer(surface->unityColorBuffer, surface->systemColorBuffer, surface->systemDepthBuffer);
#if UNITY_TRAMPOLINE_IN_USE
APP_CONTROLLER_RENDER_PLUGIN_METHOD(onFrameResolved);
#endif
}
extern "C" void PresentMTL(UnityDisplaySurfaceMTL* surface)
{
if (surface->drawable)
{
#if PLATFORM_IOS || PLATFORM_TVOS
if (@available(iOS 10.3, tvOS 10.2, *))
{
const int targetFPS = UnityGetTargetFPS(); assert(targetFPS > 0);
[UnityCurrentMTLCommandBuffer() presentDrawable: surface->drawable afterMinimumDuration: 1.0 / targetFPS];
return;
}
#endif
// note that we end up here if presentDrawable: afterMinimumDuration: is not supported
[UnityCurrentMTLCommandBuffer() presentDrawable: surface->drawable];
}
}
extern "C" MTLTextureRef AcquireDrawableMTL(UnityDisplaySurfaceMTL* surface)
{
if (!surface)
return nil;
if (!surface->drawable)
surface->drawable = [surface->layer nextDrawable];
// on A7 SoC nextDrawable may be nil before locking the screen
if (!surface->drawable)
return nil;
surface->systemColorRB = [surface->drawable texture];
return surface->systemColorRB;
}
extern "C" int UnityCommandQueueMaxCommandBufferCountMTL()
{
// customizable argument to pass towards [MTLDevice newCommandQueueWithMaxCommandBufferCount:],
// the default value is 64 but with Parallel Render Encoder workloads, it might need to be increased
return 256;
}
extern "C" void StartFrameRenderingMTL(UnityDisplaySurfaceMTL* surface)
{
// we will acquire drawable lazily in AcquireDrawableMTL
surface->drawable = nil;
#if PLATFORM_OSX
bool bufferSwap = OSAtomicCompareAndSwap32Barrier(1, 0, &surface->bufferSwap);
if (bufferSwap || surface->bufferCompleted == 1)
{
MTLTextureRef texture0 = surface->drawableProxyRT[0];
MTLTextureRef texture1 = surface->drawableProxyRT[1];
surface->drawableProxyRT[0] = texture1;
surface->drawableProxyRT[1] = texture0;
}
#endif
surface->systemColorRB = surface->drawableProxyRT[0];
UnityRenderBufferDesc sys_desc = { surface->systemW, surface->systemH, 1, 1, 1};
UnityRenderBufferDesc tgt_desc = { surface->targetW, surface->targetH, 1, (unsigned int)surface->msaaSamples, 1};
surface->systemColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->systemColorBuffer, surface->systemColorRB, nil, &sys_desc, surface);
if (surface->targetColorRT == nil)
{
if (surface->targetAAColorRT)
surface->unityColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->unityColorBuffer, surface->targetAAColorRT, surface->systemColorRB, &tgt_desc, surface);
else
surface->unityColorBuffer = UnityCreateExternalColorSurfaceMTL(surface->unityColorBuffer, surface->systemColorRB, nil, &tgt_desc, surface);
}
}
extern "C" void EndFrameRenderingMTL(UnityDisplaySurfaceMTL* surface)
{
@autoreleasepool
{
if (surface->presentCB)
{
[surface->presentCB enqueue]; [surface->presentCB commit];
surface->presentCB = nil;
}
surface->systemColorRB = nil;
surface->drawable = nil;
}
}
extern "C" void PreparePresentNonMainScreenMTL(UnityDisplaySurfaceMTL* surface)
{
if (surface->drawable)
{
surface->presentCB = [surface->drawableCommandQueue commandBuffer];
[surface->presentCB presentDrawable: surface->drawable];
}
}
extern "C" void SetDrawableSizeMTL(UnityDisplaySurfaceMTL* surface, int width, int height)
{
surface->layer.drawableSize = CGSizeMake(width, height);
}
#else
extern "C" void InitRenderingMTL() {}
extern "C" void CreateSystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void CreateRenderingSurfaceMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void DestroyRenderingSurfaceMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void CreateSharedDepthbufferMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void DestroySharedDepthbufferMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void CreateUnityRenderBuffersMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void DestroySystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void DestroyUnityRenderBuffersMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void StartFrameRenderingMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void EndFrameRenderingMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void PreparePresentMTL(UnityDisplaySurfaceMTL*) {}
extern "C" void PresentMTL(UnityDisplaySurfaceMTL*) {}
extern "C" int UnityCommandQueueMaxCommandBufferCountMTL() { return 0; }
extern "C" void SetDrawableSizeMTL(UnityDisplaySurfaceMTL*, int, int) {}
extern "C" MTLTextureRef AcquireDrawableMTL(UnityDisplaySurfaceMTL*) { return nil; }
#endif