UnityRendering.h 12 KB

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