hank
2019-01-22 9bb554260c63842b23919b1f128b9cc8488b7f50
commit | author | age
9bb554 1 #pragma once
H 2
3 #ifdef __OBJC__
4 @class CAEAGLLayer;
5 @class EAGLContext;
6 #else
7 typedef struct objc_object CAEAGLLayer;
8 typedef struct objc_object EAGLContext;
9 #endif
10
11 #ifdef __OBJC__
12 @class CAMetalLayer;
13 @protocol CAMetalDrawable;
14 @protocol MTLDrawable;
15 @protocol MTLDevice;
16 @protocol MTLTexture;
17 @protocol MTLCommandBuffer;
18 @protocol MTLCommandQueue;
19 @protocol MTLCommandEncoder;
20
21 typedef id<CAMetalDrawable>     CAMetalDrawableRef;
22 typedef id<MTLDevice>           MTLDeviceRef;
23 typedef id<MTLTexture>          MTLTextureRef;
24 typedef id<MTLCommandBuffer>    MTLCommandBufferRef;
25 typedef id<MTLCommandQueue>     MTLCommandQueueRef;
26 typedef id<MTLCommandEncoder>   MTLCommandEncoderRef;
27 #else
28 typedef struct objc_object      CAMetalLayer;
29 typedef struct objc_object*     CAMetalDrawableRef;
30 typedef struct objc_object*     MTLDeviceRef;
31 typedef struct objc_object*     MTLTextureRef;
32 typedef struct objc_object*     MTLCommandBufferRef;
33 typedef struct objc_object*     MTLCommandQueueRef;
34 typedef struct objc_object*     MTLCommandEncoderRef;
35 #endif
36
37 // unity internal native render buffer struct (the one you acquire in C# with RenderBuffer.GetNativeRenderBufferPtr())
38 struct RenderSurfaceBase;
39 typedef struct RenderSurfaceBase* UnityRenderBufferHandle;
40
41 // be aware that this struct is shared with unity implementation so you should absolutely not change it
42 typedef struct
43     UnityRenderBufferDesc
44 {
45     unsigned    width, height, depth;
46     unsigned    samples;
47
48     int         backbuffer;
49 }
50 UnityRenderBufferDesc;
51
52 // trick to make structure inheritance work transparently between c/cpp
53 // for c we use "anonymous struct"
54 #ifdef __cplusplus
55     #define START_STRUCT(T, Base)   struct T : Base {
56     #define END_STRUCT(T)           };
57 #else
58     #define START_STRUCT(T, Base)   typedef struct T { struct Base;
59     #define END_STRUCT(T)           } T;
60 #endif
61
62 // we will keep objc objects in struct, so we need to explicitely mark references as strong to not confuse ARC
63 // please note that actual object lifetime is managed in objc++ code, so __unsafe_unretained is good enough for objc code
64 // DO NOT assign objects to UnityDisplaySurface* members in objc code.
65 // DO NOT store objects from UnityDisplaySurface* members in objc code, as this wont be caught by ARC
66 #ifdef __OBJC__
67     #ifdef __cplusplus
68         #define OBJC_OBJECT_PTR __strong
69     #else
70         #define OBJC_OBJECT_PTR __unsafe_unretained
71     #endif
72 #else
73     #define OBJC_OBJECT_PTR
74 #endif
75
76 // unity common rendering (display) surface
77 typedef struct
78     UnityDisplaySurfaceBase
79 {
80     UnityRenderBufferHandle unityColorBuffer;
81     UnityRenderBufferHandle unityDepthBuffer;
82
83     UnityRenderBufferHandle systemColorBuffer;
84     UnityRenderBufferHandle systemDepthBuffer;
85
86     void*               cvTextureCache;         // CVOpenGLESTextureCacheRef
87     void*               cvTextureCacheTexture;  // CVOpenGLESTextureRef
88     void*               cvPixelBuffer;          // CVPixelBufferRef
89
90     unsigned            targetW, targetH;
91     unsigned            systemW, systemH;
92
93     int                 msaaSamples;
94     int                 useCVTextureCache;      // [bool]
95     int                 srgb;                   // [bool]
96     int                 disableDepthAndStencil; // [bool]
97     int                 allowScreenshot;        // [bool] currently we allow screenshots (from script) only on main display
98
99     int                 api;                    // [UnityRenderingAPI]
100 }
101 UnityDisplaySurfaceBase;
102
103
104 // START_STRUCT confuse clang c compiler (though it is idiomatic c code that works)
105 #pragma clang diagnostic push
106 #pragma clang diagnostic ignored "-Wmissing-declarations"
107
108 // GLES display surface
109 START_STRUCT(UnityDisplaySurfaceGLES, UnityDisplaySurfaceBase)
110 OBJC_OBJECT_PTR CAEAGLLayer *    layer;
111 OBJC_OBJECT_PTR EAGLContext*    context;
112
113 // system FB
114 unsigned    systemFB;
115 unsigned    systemColorRB;
116
117 // target resolution FB/target RT to blit from
118 unsigned    targetFB;
119 unsigned    targetColorRT;
120
121 // MSAA FB
122 unsigned    msaaFB;
123 unsigned    msaaColorRB;
124
125 // when we enable AA for non-native resolution we need interim RT to resolve AA to (and then we will blit it to screen)
126 UnityRenderBufferHandle resolvedColorBuffer;
127
128 // will be "shared", only one depth buffer is needed
129 unsigned    depthRB;
130
131 // render surface gl setup: formats and AA
132 unsigned    colorFormat;
133 unsigned    depthFormat;
134 END_STRUCT(UnityDisplaySurfaceGLES)
135
136 // Metal display surface
137 START_STRUCT(UnityDisplaySurfaceMTL, UnityDisplaySurfaceBase)
138 OBJC_OBJECT_PTR CAMetalLayer *       layer;
139 OBJC_OBJECT_PTR MTLDeviceRef        device;
140
141 OBJC_OBJECT_PTR MTLCommandQueueRef  commandQueue;
142 OBJC_OBJECT_PTR CAMetalDrawableRef  drawable;
143
144 OBJC_OBJECT_PTR MTLTextureRef       systemColorRB;
145 OBJC_OBJECT_PTR MTLTextureRef       targetColorRT;
146 OBJC_OBJECT_PTR MTLTextureRef       targetAAColorRT;
147
148 OBJC_OBJECT_PTR MTLTextureRef       depthRB;
149 OBJC_OBJECT_PTR MTLTextureRef       stencilRB;
150
151 unsigned                            colorFormat;        // [MTLPixelFormat]
152 unsigned                            depthFormat;        // [MTLPixelFormat]
153 END_STRUCT(UnityDisplaySurfaceMTL)
154
155 // START_STRUCT confuse clang c compiler (though it is idiomatic c code that works)
156 #pragma clang diagnostic pop
157
158 // be aware that this enum is shared with unity implementation so you should absolutely not change it
159 typedef enum
160     UnityRenderingAPI
161 {
162     apiOpenGLES2    = 2,
163     apiOpenGLES3    = 3,
164     apiMetal        = 4,
165 }
166 UnityRenderingAPI;
167
168 #ifdef __cplusplus
169 extern "C" {
170 #endif
171 int UnitySelectedRenderingAPI();
172 #ifdef __cplusplus
173 } // extern "C"
174 #endif
175
176 // gles
177 #ifdef __cplusplus
178 extern "C" {
179 #endif
180
181 void InitRenderingGLES();
182
183 void CreateSystemRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface);
184 void DestroySystemRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface);
185 void CreateRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface);
186 void DestroyRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface);
187 void CreateSharedDepthbufferGLES(UnityDisplaySurfaceGLES* surface);
188 void DestroySharedDepthbufferGLES(UnityDisplaySurfaceGLES* surface);
189 void CreateUnityRenderBuffersGLES(UnityDisplaySurfaceGLES* surface);
190 void DestroyUnityRenderBuffersGLES(UnityDisplaySurfaceGLES* surface);
191 void StartFrameRenderingGLES(UnityDisplaySurfaceGLES* surface);
192 void EndFrameRenderingGLES(UnityDisplaySurfaceGLES* surface);
193 void PreparePresentGLES(UnityDisplaySurfaceGLES* surface);
194 void PresentGLES(UnityDisplaySurfaceGLES* surface);
195
196 #ifdef __cplusplus
197 } // extern "C"
198 #endif
199
200 // metal
201 #ifdef __cplusplus
202 extern "C" {
203 #endif
204
205 void InitRenderingMTL();
206
207 void CreateSystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface);
208 void DestroySystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface);
209 void CreateRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface);
210 void DestroyRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface);
211 void CreateSharedDepthbufferMTL(UnityDisplaySurfaceMTL* surface);
212 void DestroySharedDepthbufferMTL(UnityDisplaySurfaceMTL* surface);
213 void CreateUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface);
214 void DestroyUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface);
215 void StartFrameRenderingMTL(UnityDisplaySurfaceMTL* surface);
216 void EndFrameRenderingMTL(UnityDisplaySurfaceMTL* surface);
217 void PreparePresentMTL(UnityDisplaySurfaceMTL* surface);
218 void PresentMTL(UnityDisplaySurfaceMTL* surface);
219
220 #ifdef __cplusplus
221 } // extern "C"
222 #endif
223
224 #ifdef __cplusplus
225 extern "C" {
226 #endif
227
228 // for Create* functions if surf is null we will actuially create new one, otherwise we update the one provided
229 // gles: one and only one of texid/rbid should be non-zero
230 // metal: resolveTex should be non-nil only if tex have AA
231 UnityRenderBufferHandle UnityCreateExternalSurfaceGLES(UnityRenderBufferHandle surf, int isColor, unsigned texid, unsigned rbid, unsigned glesFormat, const UnityRenderBufferDesc* desc);
232 UnityRenderBufferHandle UnityCreateExternalSurfaceMTL(UnityRenderBufferHandle surf, int isColor, MTLTextureRef tex, const UnityRenderBufferDesc* desc);
233 UnityRenderBufferHandle UnityCreateExternalColorSurfaceMTL(UnityRenderBufferHandle surf, MTLTextureRef tex, MTLTextureRef resolveTex, const UnityRenderBufferDesc* desc);
234 UnityRenderBufferHandle UnityCreateExternalDepthSurfaceMTL(UnityRenderBufferHandle surf, MTLTextureRef tex, MTLTextureRef stencilTex, const UnityRenderBufferDesc* desc);
235 // creates "dummy" surface - will indicate "missing" buffer (e.g. depth-only RT will have color as dummy)
236 UnityRenderBufferHandle UnityCreateDummySurface(UnityRenderBufferHandle surf, int isColor, const UnityRenderBufferDesc* desc);
237
238 // disable rendering to render buffers (all Cameras that were rendering to one of buffers would be reset to use backbuffer)
239 void    UnityDisableRenderBuffers(UnityRenderBufferHandle color, UnityRenderBufferHandle depth);
240 // destroys render buffer
241 void    UnityDestroyExternalSurface(UnityRenderBufferHandle surf);
242 // sets current render target
243 void    UnitySetRenderTarget(UnityRenderBufferHandle color, UnityRenderBufferHandle depth);
244 // final blit to backbuffer
245 void    UnityBlitToBackbuffer(UnityRenderBufferHandle srcColor, UnityRenderBufferHandle dstColor, UnityRenderBufferHandle dstDepth);
246 // get native renderbuffer from handle
247 UnityRenderBufferHandle UnityNativeRenderBufferFromHandle(void *rb);
248
249 MTLCommandBufferRef UnityCurrentMTLCommandBuffer();
250
251 // sets vSync on OSX 10.13 and up
252 #if PLATFORM_OSX
253 void MetalUpdateDisplaySync();
254 #endif
255
256 #ifdef __cplusplus
257 } // extern "C"
258 #endif
259
260 // metal/gles unification
261
262 #define GLES_METAL_COMMON_IMPL_SURF(f)                                              \
263 inline void f(UnityDisplaySurfaceBase* surface)                                     \
264 {                                                                                   \
265     if(surface->api == apiMetal)    f ## MTL((UnityDisplaySurfaceMTL*)surface); \
266     else                            f ## GLES((UnityDisplaySurfaceGLES*)surface);\
267 }                                                                                   \
268
269 #define GLES_METAL_COMMON_IMPL(f)                               \
270 inline void f()                                                 \
271 {                                                               \
272     if(UnitySelectedRenderingAPI() == apiMetal) f ## MTL();     \
273     else                                        f ## GLES();\
274 }                                                               \
275
276
277 GLES_METAL_COMMON_IMPL(InitRendering);
278
279 GLES_METAL_COMMON_IMPL_SURF(CreateSystemRenderingSurface);
280 GLES_METAL_COMMON_IMPL_SURF(DestroySystemRenderingSurface);
281 GLES_METAL_COMMON_IMPL_SURF(CreateRenderingSurface);
282 GLES_METAL_COMMON_IMPL_SURF(DestroyRenderingSurface);
283 GLES_METAL_COMMON_IMPL_SURF(CreateSharedDepthbuffer);
284 GLES_METAL_COMMON_IMPL_SURF(DestroySharedDepthbuffer);
285 GLES_METAL_COMMON_IMPL_SURF(CreateUnityRenderBuffers);
286 GLES_METAL_COMMON_IMPL_SURF(DestroyUnityRenderBuffers);
287 GLES_METAL_COMMON_IMPL_SURF(StartFrameRendering);
288 GLES_METAL_COMMON_IMPL_SURF(EndFrameRendering);
289 GLES_METAL_COMMON_IMPL_SURF(PreparePresent);
290 GLES_METAL_COMMON_IMPL_SURF(Present);
291
292 #undef GLES_METAL_COMMON_IMPL_SURF
293 #undef GLES_METAL_COMMON_IMPL