UnityRendering.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #pragma once
  2. #include <stdint.h>
  3. #ifdef __OBJC__
  4. @class CAMetalLayer;
  5. @protocol CAMetalDrawable;
  6. @protocol MTLDrawable;
  7. @protocol MTLDevice;
  8. @protocol MTLTexture;
  9. @protocol MTLCommandBuffer;
  10. @protocol MTLCommandQueue;
  11. @protocol MTLCommandEncoder;
  12. typedef id<CAMetalDrawable> CAMetalDrawableRef;
  13. typedef id<MTLDevice> MTLDeviceRef;
  14. typedef id<MTLTexture> MTLTextureRef;
  15. typedef id<MTLCommandBuffer> MTLCommandBufferRef;
  16. typedef id<MTLCommandQueue> MTLCommandQueueRef;
  17. typedef id<MTLCommandEncoder> MTLCommandEncoderRef;
  18. #else
  19. typedef struct objc_object CAMetalLayer;
  20. typedef struct objc_object* CAMetalDrawableRef;
  21. typedef struct objc_object* MTLDeviceRef;
  22. typedef struct objc_object* MTLTextureRef;
  23. typedef struct objc_object* MTLCommandBufferRef;
  24. typedef struct objc_object* MTLCommandQueueRef;
  25. typedef struct objc_object* MTLCommandEncoderRef;
  26. #endif
  27. // if this is set, then variables that there were moved from UnityDisplaySurfaceMTL to swapchain
  28. // will be still updated when acquiring drawable.
  29. // NOTE: in this case we assume that all the UnityViewSwapchain pointers passed to unity players are coming from UnityDisplaySurfaceMTL
  30. // if this is set UnityDisplaySurfaceBase still has removed variables, but they are no longer updated
  31. #if !defined(UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY) && UNITY_TRAMPOLINE_IN_USE
  32. #define UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY 1
  33. #endif
  34. // unity internal native render buffer struct (the one you acquire in C# with RenderBuffer.GetNativeRenderBufferPtr())
  35. struct RenderSurfaceBase;
  36. typedef struct RenderSurfaceBase* UnityRenderBufferHandle;
  37. // be aware that this struct is shared with unity implementation so you should absolutely not change it
  38. typedef struct UnityRenderBufferDesc
  39. {
  40. unsigned width, height, depth;
  41. unsigned samples;
  42. int backbuffer;
  43. } UnityRenderBufferDesc;
  44. // trick to make structure inheritance work transparently between c/cpp
  45. // for c we use "anonymous struct"
  46. #ifdef __cplusplus
  47. #define START_STRUCT(T, Base) struct T : Base {
  48. #define END_STRUCT(T) };
  49. #else
  50. #define START_STRUCT(T, Base) typedef struct T { struct Base;
  51. #define END_STRUCT(T) } T;
  52. #endif
  53. // we will keep objc objects in struct, so we need to explicitely mark references as strong to not confuse ARC
  54. // please note that actual object lifetime is managed in objc++ code, so __unsafe_unretained is good enough for objc code
  55. // DO NOT assign objects to UnityDisplaySurface* members in objc code.
  56. // DO NOT store objects from UnityDisplaySurface* members in objc code, as this wont be caught by ARC
  57. #ifdef __OBJC__
  58. #ifdef __cplusplus
  59. #define OBJC_OBJECT_PTR __strong
  60. #else
  61. #define OBJC_OBJECT_PTR __unsafe_unretained
  62. #endif
  63. #else
  64. #define OBJC_OBJECT_PTR
  65. #endif
  66. // unity common rendering (display) surface
  67. typedef struct UnityDisplaySurfaceBase
  68. {
  69. UnityRenderBufferHandle unityColorBuffer;
  70. UnityRenderBufferHandle unityDepthBuffer;
  71. UnityRenderBufferHandle systemColorBuffer;
  72. unsigned targetW, targetH;
  73. unsigned systemW, systemH;
  74. int msaaSamples;
  75. int srgb; // [bool]
  76. int wideColor; // [bool]
  77. int hdr; // [bool]
  78. int disableDepthAndStencil; // [bool]
  79. int allowScreenshot; // [bool] currently we allow screenshots (from script) only on main display
  80. int memorylessDepth; // [bool]
  81. int api; // [UnityRenderingAPI]
  82. // these are no longer supported, we keep them only to avoid breaking compilation
  83. #if UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY
  84. UnityRenderBufferHandle systemDepthBuffer __attribute__((deprecated));
  85. int useCVTextureCache __attribute__((deprecated));
  86. void* cvTextureCache __attribute__((deprecated));
  87. void* cvTextureCacheTexture __attribute__((deprecated));
  88. void* cvPixelBuffer __attribute__((deprecated));
  89. #endif
  90. } UnityDisplaySurfaceBase;
  91. typedef struct UnityViewSwapchain
  92. {
  93. OBJC_OBJECT_PTR CAMetalLayer* layer;
  94. // drawable handling:
  95. // CADisplayLink: we will call [CAMetalLayer nextDrawable] first time we need to use "backbuffer"
  96. // CAMetalDisplayLink: we are getting drawable to render to from the callback
  97. // alas it might happen that we get next callback while still using current drawable (as in - while rendering previous frame)
  98. // thus we will "mimic" the logic for the old display link, by setting "next drawable to use" in callback
  99. // and "acquiring" drawable on first draw to the backbuffer just updates current drawable to the stored one
  100. OBJC_OBJECT_PTR CAMetalDrawableRef drawable;
  101. OBJC_OBJECT_PTR CAMetalDrawableRef nextDrawable;
  102. OBJC_OBJECT_PTR MTLTextureRef drawableTexture;
  103. } UnityViewSwapchain;
  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. // on iOS/tvOS: we render to the drawable directly
  108. // and we need proxy only to delay acquiring drawable until we actually want to render to the "backbuffer"
  109. // thus just one proxy and it will be marked as "empty" (we only need it to query texture params, like extents)
  110. // on macOS: we render to the offscreen RT and blit to the drawable, thus we need several proxy RT
  111. // and all of them will be full blown textures (with GPU backing)
  112. #if PLATFORM_OSX
  113. #define kUnityNumOffscreenSurfaces 2
  114. #else
  115. #define kUnityNumOffscreenSurfaces 1
  116. #endif
  117. // Metal display surface
  118. START_STRUCT(UnityDisplaySurfaceMTL, UnityDisplaySurfaceBase)
  119. UnityViewSwapchain swapchain;
  120. OBJC_OBJECT_PTR MTLDeviceRef device;
  121. UnityRenderBufferHandle targetColorRB;
  122. UnityRenderBufferHandle targetAAColorRB;
  123. OBJC_OBJECT_PTR MTLTextureRef drawableProxyRT[kUnityNumOffscreenSurfaces];
  124. UnityRenderBufferHandle drawableProxyRS[kUnityNumOffscreenSurfaces];
  125. // This is used on a Mac with drawableProxyRT when off-screen rendering is used
  126. int proxySwaps; // Counts times proxy RTs have swapped since surface recreated
  127. int proxyReady; // [bool] Proxy RT has swapped since last present; frame ended
  128. int calledPresentDrawable; // Tracks presenting for editor.
  129. int vsync; // Is vsync enabled or not
  130. unsigned colorFormat; // [MTLPixelFormat]
  131. unsigned depthFormat; // [MTLPixelFormat]
  132. int framebufferOnly;
  133. // these were moved to a separate structure. to simplify the lives of plugin writers we are keeping them here for some time
  134. // if these need to be updated XXX need to be defined: we will try to update these, but please move on from using them
  135. #if UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY
  136. OBJC_OBJECT_PTR CAMetalLayer* layer __attribute__((deprecated));
  137. OBJC_OBJECT_PTR CAMetalDrawableRef nextDrawable __attribute__((deprecated));
  138. OBJC_OBJECT_PTR CAMetalDrawableRef drawable __attribute__((deprecated));
  139. OBJC_OBJECT_PTR MTLTextureRef drawableTex __attribute__((deprecated));
  140. // these are no longer used, and should have never been used before - we still keep them around but they stay zero-inited
  141. OBJC_OBJECT_PTR MTLTextureRef systemColorRB __attribute__((deprecated));
  142. int drawableProxyNeedsClear[kUnityNumOffscreenSurfaces] __attribute__((deprecated));
  143. // these we removed in favor of RTs managed inside player library, we now have renderbuffers instead
  144. OBJC_OBJECT_PTR MTLTextureRef targetColorRT __attribute__((deprecated));
  145. OBJC_OBJECT_PTR MTLTextureRef targetAAColorRT __attribute__((deprecated));
  146. OBJC_OBJECT_PTR MTLTextureRef depthRB __attribute__((deprecated));
  147. OBJC_OBJECT_PTR MTLTextureRef stencilRB __attribute__((deprecated));
  148. #endif
  149. END_STRUCT(UnityDisplaySurfaceMTL)
  150. // START_STRUCT confuse clang c compiler (though it is idiomatic c code that works)
  151. #pragma clang diagnostic pop
  152. // be aware that this enum is shared with unity implementation so you should absolutely not change it
  153. typedef enum UnityRenderingAPI
  154. {
  155. apiMetal = 4,
  156. // command line argument: -nographics
  157. // does not initialize real graphics device and bypass all the rendering
  158. // currently supported only on simulators
  159. apiNoGraphics = -1,
  160. } UnityRenderingAPI;
  161. typedef struct RenderingSurfaceParams
  162. {
  163. // rendering setup
  164. int msaaSampleCount;
  165. int renderW;
  166. int renderH;
  167. int srgb;
  168. int wideColor;
  169. int hdr;
  170. int metalFramebufferOnly;
  171. int metalMemorylessDepth;
  172. // unity setup
  173. int disableDepthAndStencil;
  174. // no longer supported
  175. #if UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY
  176. int useCVTextureCache __attribute__((deprecated));
  177. #endif
  178. } RenderingSurfaceParams;
  179. #ifdef __cplusplus
  180. extern "C" {
  181. #endif
  182. int UnitySelectedRenderingAPI(void);
  183. #ifdef __cplusplus
  184. } // extern "C"
  185. #endif
  186. // metal
  187. #ifdef __cplusplus
  188. extern "C" {
  189. #endif
  190. void InitRenderingMTL(void);
  191. void CreateSystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface);
  192. void CreateUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface);
  193. void DestroyUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface);
  194. void StartFrameRenderingMTL(UnityDisplaySurfaceMTL* surface);
  195. void EndFrameRenderingMTL(UnityDisplaySurfaceMTL* surface);
  196. void PreparePresentMTL(UnityDisplaySurfaceMTL* surface, MTLCommandBufferRef cb);
  197. void PresentMTL(UnityDisplaySurfaceMTL* surface, MTLCommandBufferRef cb);
  198. // Acquires CAMetalDrawable resource for the surface and returns the drawable texture
  199. // AcquireDrawableMTL is deprecated and not used from unity side anymore
  200. // we keep it here only to avoid breaking native plugins that might have used it
  201. MTLTextureRef AcquireDrawableMTL(UnityDisplaySurfaceMTL* surface);
  202. MTLTextureRef AcquireSwapchainDrawable(UnityViewSwapchain* swapchain);
  203. unsigned UnityHDRSurfaceDepth(void);
  204. // starting with ios11 apple insists on having just one presentDrawable per command buffer
  205. // hence we keep normal processing for main screen, but when airplay is used we will create extra command buffers
  206. void PreparePresentNonMainScreenMTL(UnityDisplaySurfaceMTL* surface);
  207. #ifdef __cplusplus
  208. } // extern "C"
  209. #endif
  210. // no graphics
  211. #ifdef __cplusplus
  212. extern "C" {
  213. #endif
  214. void InitRenderingNULL(void);
  215. void CreateSystemRenderingSurfaceNULL(UnityDisplaySurfaceBase* surface);
  216. void CreateUnityRenderBuffersNULL(UnityDisplaySurfaceBase* surface);
  217. void DestroyUnityRenderBuffersNULL(UnityDisplaySurfaceBase* surface);
  218. void StartFrameRenderingNULL(UnityDisplaySurfaceBase* surface);
  219. void EndFrameRenderingNULL(UnityDisplaySurfaceBase* surface);
  220. void PreparePresentNULL(UnityDisplaySurfaceBase* surface);
  221. void PresentNULL(UnityDisplaySurfaceBase* surface);
  222. #ifdef __cplusplus
  223. } // extern "C"
  224. #endif
  225. #ifdef __cplusplus
  226. extern "C" {
  227. #endif
  228. // for Create* functions if surf is null we will actuially create new one, otherwise we update the one provided
  229. // metal: resolveTex should be non-nil only if tex have AA
  230. UnityRenderBufferHandle UnityCreateExternalSurfaceMTL(UnityRenderBufferHandle surf, int isColor, MTLTextureRef tex, const UnityRenderBufferDesc* desc);
  231. // Passing non-nil displaySurface will mark render surface as proxy and will do a delayed drawable acquisition when setting up framebuffer
  232. UnityRenderBufferHandle UnityCreateExternalColorSurfaceMTL(UnityRenderBufferHandle surf, MTLTextureRef tex, MTLTextureRef resolveTex, const UnityRenderBufferDesc* desc, UnityDisplaySurfaceMTL* displaySurface);
  233. UnityRenderBufferHandle UnityCreateExternalDepthSurfaceMTL(UnityRenderBufferHandle surf, MTLTextureRef tex, MTLTextureRef stencilTex, const UnityRenderBufferDesc* desc);
  234. // creates "dummy" surface - will indicate "missing" buffer (e.g. depth-only RT will have color as dummy)
  235. UnityRenderBufferHandle UnityCreateDummySurface(UnityRenderBufferHandle surf, int isColor, const UnityRenderBufferDesc* desc);
  236. // external render surfaces and textures are "out of scope" for memory profiler, hence we add means to register them separately
  237. // the separate mechanism is needed because unity cannot know what manages the lifetime of textures in this case
  238. // specifically since we allow external render surfaces and textures to share metal textures
  239. void UnityRegisterExternalRenderSurfaceTextureForMemoryProfiler(MTLTextureRef tex);
  240. void UnityRegisterExternalTextureForMemoryProfiler(MTLTextureRef tex);
  241. void UnityUnregisterMetalTextureForMemoryProfiler(MTLTextureRef tex);
  242. // handling of unity "backbuffer"
  243. // internally we still pretend that backbuffer has depth, can have MSAA, or extents different from the window/view size
  244. // we had UnityCreateExternal* api before that was slightly too "wordy" to use, and was also using UnityDisplaySurfaceMTL
  245. // which is wrong, and introduced to big a coupling
  246. // now, we have two things happening (with different velocities), triggering the need to update API
  247. // even if this is not (might be not) the final form
  248. // first of all we are working on introducing swift trampoline, where we can simplify rendering logic;
  249. // but also we do not want to drag in the whole UnityDisplaySurfaceMTL (it doesn't make sense nowadays)
  250. // thus we introduce intermediate explicit "swapchain" structure
  251. // this also plays nicely with moving towards having explicit "swapchain" concept internally,
  252. // where (not matching view) resolution, MSAA, etc will be handled separately from "here is the connection to window to give to compositor"
  253. // hence we introduce the api using new UnityViewSwapchain structure (with minimal connection to view internals)
  254. // alas we are not yet ready to fully switch to have only swapchain in the platform layer (trampoline)
  255. // thus we still keep the possibility to create "AA backbuffer that will be resolved to swapchain" or even custom resolution (copied to actual drawable)
  256. // another thing to note is that we are still bound to support "old" CADisplayLink, thus we need to be able to delay acquiring drawable
  257. // so we still need to make sure we have RenderBuffer connecting to a "swapchain" to do the magic when we want to render to it
  258. // and that's why we have such a big api surface instead of simple "here is your swapchain, please render to it"
  259. // but this will hopefully change soon
  260. // things of note:
  261. // * note that we always pass "current" pointer (rbBackbuffer param)
  262. // this api is working similarly to realloc: if you pass null we allocate and create new render buffer, otherwise we update the existing one
  263. // * due to this, UnitySwapchainDestroyBackbuffer is called only if we really do not need this type of surface anymore
  264. UnityRenderBufferHandle UnitySwapchainCreateBackbuffer(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbBackbuffer);
  265. UnityRenderBufferHandle UnitySwapchainCreateBackbufferForExtents(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbBackbuffer, unsigned width, unsigned height);
  266. UnityRenderBufferHandle UnitySwapchainCreateAABackbufferResolveToSwapchain(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbBackbuffer, unsigned sampleCount);
  267. UnityRenderBufferHandle UnitySwapchainCreateAABackbuffer(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbBackbuffer, unsigned sampleCount, UnityRenderBufferHandle rbResolveTo);
  268. UnityRenderBufferHandle UnitySwapchainCreateDepthForBackbuffer(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbColorBackbuffer, UnityRenderBufferHandle rbDepthBackbuffer);
  269. void UnitySwapchainDestroyBackbuffer(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbBackbuffer);
  270. void UnitySwapchainBlitBackbuffer(UnityViewSwapchain* swapchain, UnityRenderBufferHandle rbColorBackbuffer, MTLCommandBufferRef cb);
  271. // disable rendering to render buffers (all Cameras that were rendering to one of buffers would be reset to use backbuffer)
  272. void UnityDisableRenderBuffers(UnityRenderBufferHandle color, UnityRenderBufferHandle depth);
  273. // destroys render buffer
  274. void UnityDestroyExternalSurface(UnityRenderBufferHandle surf);
  275. // sets current render target
  276. void UnitySetRenderTarget(UnityRenderBufferHandle color, UnityRenderBufferHandle depth);
  277. // final blit to backbuffer
  278. void UnityBlitToBackbuffer(UnityRenderBufferHandle srcColor, UnityRenderBufferHandle dstColor, UnityRenderBufferHandle dstDepth);
  279. // get native renderbuffer from handle
  280. // sets vSync on OSX 10.13 and up
  281. #if PLATFORM_OSX
  282. void MetalUpdateDisplaySync(void);
  283. #endif
  284. UnityRenderBufferHandle UnityNativeRenderBufferFromHandle(void *rb);
  285. MTLCommandBufferRef UnityCurrentMTLCommandBuffer(void);
  286. #ifdef __cplusplus
  287. } // extern "C"
  288. #endif
  289. // metal/gles unification
  290. #define GLES_METAL_COMMON_IMPL_SURF(f) \
  291. inline void f(UnityDisplaySurfaceBase* surface) \
  292. { \
  293. switch(surface->api) { \
  294. case apiMetal: f ## MTL((UnityDisplaySurfaceMTL*)surface); break; \
  295. case apiNoGraphics: f ## NULL(surface); break; \
  296. } \
  297. } \
  298. #define GLES_METAL_COMMON_IMPL(f) \
  299. inline void f() \
  300. { \
  301. switch(UnitySelectedRenderingAPI()) { \
  302. case apiMetal: f ## MTL(); break; \
  303. case apiNoGraphics: f ## NULL(); break; \
  304. } \
  305. } \
  306. GLES_METAL_COMMON_IMPL(InitRendering);
  307. GLES_METAL_COMMON_IMPL_SURF(CreateSystemRenderingSurface);
  308. GLES_METAL_COMMON_IMPL_SURF(CreateUnityRenderBuffers);
  309. GLES_METAL_COMMON_IMPL_SURF(DestroyUnityRenderBuffers);
  310. GLES_METAL_COMMON_IMPL_SURF(StartFrameRendering);
  311. GLES_METAL_COMMON_IMPL_SURF(EndFrameRendering);
  312. #undef GLES_METAL_COMMON_IMPL_SURF
  313. #undef GLES_METAL_COMMON_IMPL