MetalHelper.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "UnityRendering.h"
  2. #import <Metal/Metal.h>
  3. #import <QuartzCore/QuartzCore.h>
  4. #include "UnityAppController.h"
  5. #include "CVTextureCache.h"
  6. #include "ObjCRuntime.h"
  7. #include <libkern/OSAtomic.h>
  8. #include <utility>
  9. extern "C" void InitRenderingMTL()
  10. {
  11. }
  12. static MTLPixelFormat GetColorFormatForSurface(const UnityDisplaySurfaceMTL* surface)
  13. {
  14. MTLPixelFormat colorFormat = MTLPixelFormatInvalid;
  15. #if PLATFORM_IOS || PLATFORM_VISIONOS
  16. if (surface->hdr)
  17. {
  18. // 0 = 10 bit, 1 = 16bit
  19. if (@available(iOS 16.0, *))
  20. colorFormat = UnityHDRSurfaceDepth() == 0 ? MTLPixelFormatRGB10A2Unorm : MTLPixelFormatRGBA16Float;
  21. }
  22. #endif
  23. if(colorFormat == MTLPixelFormatInvalid && surface->wideColor)
  24. {
  25. // at some point we tried using MTLPixelFormatBGR10_XR formats, but it seems that apple CoreImage have issues with that
  26. // and we are not alone here, see for example https://forums.developer.apple.com/forums/thread/66166
  27. // when application goes to background the colors are changed (more white-ish?)
  28. // no matter what we tried, the issue persists
  29. // NOTE: the most funny thing is when we set color space to be P3 we get same whitish colors always
  30. // NOTE: but this time they become normal when going to background
  31. // in all, it seems that using rgba f16 is the most robust option here, so we are back to it again
  32. colorFormat = MTLPixelFormatRGBA16Float;
  33. }
  34. if(colorFormat == MTLPixelFormatInvalid)
  35. colorFormat = surface->srgb ? MTLPixelFormatBGRA8Unorm_sRGB : MTLPixelFormatBGRA8Unorm;
  36. return colorFormat;
  37. }
  38. extern "C" void CreateSystemRenderingSurfaceMTL(UnityDisplaySurfaceMTL* surface)
  39. {
  40. MTLPixelFormat colorFormat = GetColorFormatForSurface(surface);
  41. surface->swapchain.layer.presentsWithTransaction = NO;
  42. surface->swapchain.layer.drawsAsynchronously = YES;
  43. if (UnityPreserveFramebufferAlpha())
  44. {
  45. const CGFloat components[] = {1.0f, 1.0f, 1.0f, 0.0f};
  46. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  47. CGColorRef color = CGColorCreate(colorSpace, components);
  48. surface->swapchain.layer.opaque = NO;
  49. surface->swapchain.layer.backgroundColor = color;
  50. CGColorRelease(color);
  51. CGColorSpaceRelease(colorSpace);
  52. }
  53. CGColorSpaceRef colorSpaceRef = nil;
  54. if (surface->hdr)
  55. {
  56. if (@available(iOS 16.0, *))
  57. colorSpaceRef = UnityHDRSurfaceDepth() == 0 ? CGColorSpaceCreateWithName(CFSTR("kCGColorSpaceITUR_2100_PQ")) : CGColorSpaceCreateWithName(CFSTR("kCGColorSpaceExtendedLinearITUR_2020"));
  58. }
  59. if(colorSpaceRef == nil)
  60. {
  61. if (surface->wideColor)
  62. colorSpaceRef = CGColorSpaceCreateWithName(surface->srgb ? kCGColorSpaceExtendedLinearSRGB : kCGColorSpaceExtendedSRGB);
  63. else
  64. colorSpaceRef = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
  65. }
  66. surface->swapchain.layer.colorspace = colorSpaceRef;
  67. CGColorSpaceRelease(colorSpaceRef);
  68. surface->swapchain.layer.device = surface->device;
  69. surface->swapchain.layer.pixelFormat = colorFormat;
  70. surface->swapchain.layer.framebufferOnly = (surface->framebufferOnly != 0);
  71. surface->colorFormat = (unsigned)colorFormat;
  72. }
  73. extern "C" void CreateUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface)
  74. {
  75. const int w = surface->targetW, h = surface->targetH;
  76. const bool needInterimColor = w != surface->systemW || h != surface->systemH;
  77. if (needInterimColor)
  78. {
  79. surface->targetColorRB = UnitySwapchainCreateBackbufferForExtents(&surface->swapchain, surface->targetColorRB, w, h);
  80. }
  81. else
  82. {
  83. UnityDestroyExternalSurface(surface->targetColorRB);
  84. surface->targetColorRB = 0;
  85. }
  86. if (surface->msaaSamples > 1)
  87. {
  88. if (needInterimColor)
  89. surface->targetAAColorRB = UnitySwapchainCreateAABackbuffer(&surface->swapchain, surface->targetAAColorRB, surface->msaaSamples, surface->targetColorRB);
  90. else
  91. surface->targetAAColorRB = UnitySwapchainCreateAABackbufferResolveToSwapchain(&surface->swapchain, surface->targetAAColorRB, surface->msaaSamples);
  92. }
  93. else
  94. {
  95. UnityDestroyExternalSurface(surface->targetAAColorRB);
  96. surface->targetAAColorRB = 0;
  97. }
  98. surface->systemColorBuffer = UnitySwapchainCreateBackbuffer(&surface->swapchain, surface->systemColorBuffer);
  99. if (surface->targetAAColorRB)
  100. surface->unityColorBuffer = surface->targetAAColorRB;
  101. else if (surface->targetColorRB)
  102. surface->unityColorBuffer = surface->targetColorRB;
  103. else
  104. surface->unityColorBuffer = surface->systemColorBuffer;
  105. surface->unityDepthBuffer = UnitySwapchainCreateDepthForBackbuffer(&surface->swapchain, surface->unityColorBuffer, surface->unityDepthBuffer);
  106. }
  107. extern "C" void DestroyUnityRenderBuffersMTL(UnityDisplaySurfaceMTL* surface)
  108. {
  109. UnitySwapchainDestroyBackbuffer(&surface->swapchain, surface->systemColorBuffer);
  110. UnitySwapchainDestroyBackbuffer(&surface->swapchain, surface->unityDepthBuffer);
  111. UnitySwapchainDestroyBackbuffer(&surface->swapchain, surface->targetColorRB);
  112. UnitySwapchainDestroyBackbuffer(&surface->swapchain, surface->targetAAColorRB);
  113. surface->targetColorRB = surface->targetAAColorRB = surface->systemColorBuffer = surface->unityDepthBuffer = 0;
  114. surface->unityColorBuffer = 0;
  115. }
  116. extern "C" void PreparePresentMTL(UnityDisplaySurfaceMTL* surface, MTLCommandBufferRef cb)
  117. {
  118. if (surface->targetColorRB)
  119. UnitySwapchainBlitBackbuffer(&surface->swapchain, surface->targetColorRB, cb);
  120. APP_CONTROLLER_RENDER_PLUGIN_METHOD(onFrameResolved);
  121. }
  122. extern "C" void PresentMTL(UnityDisplaySurfaceMTL* surface, MTLCommandBufferRef cb)
  123. {
  124. UnityViewSwapchain* swapchain = &surface->swapchain;
  125. @autoreleasepool
  126. {
  127. if (swapchain->drawable)
  128. [cb presentDrawable:swapchain->drawable];
  129. if (swapchain->drawableTexture)
  130. UnityUnregisterMetalTextureForMemoryProfiler(swapchain->drawableTexture);
  131. swapchain->nextDrawable = nil;
  132. swapchain->drawable = nil;
  133. swapchain->drawableTexture = nil;
  134. }
  135. surface->calledPresentDrawable = 1;
  136. }
  137. UNITY_EXPORT extern "C" MTLTextureRef AcquireSwapchainDrawable(UnityViewSwapchain* swapchain)
  138. {
  139. // check if have acquired the backbuffer texture already
  140. if (swapchain->drawableTexture)
  141. return swapchain->drawableTexture;
  142. // this is coming from CAMetalDisplayLinkUpdate
  143. if (swapchain->nextDrawable)
  144. swapchain->drawable = swapchain->nextDrawable;
  145. // this is coming from CADisplayLink: query next drawable
  146. if (!swapchain->drawable)
  147. swapchain->drawable = [swapchain->layer nextDrawable];
  148. id<MTLTexture> drawableTex = [swapchain->drawable texture];
  149. if (drawableTex)
  150. {
  151. UnityUnregisterMetalTextureForMemoryProfiler(swapchain->drawableTexture);
  152. swapchain->drawableTexture = drawableTex;
  153. UnityRegisterExternalRenderSurfaceTextureForMemoryProfiler(drawableTex);
  154. }
  155. #if UNITY_DISPLAY_SURFACE_MTL_BACKWARD_COMPATIBILITY
  156. #pragma clang diagnostic push
  157. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  158. #pragma clang diagnostic ignored "-Winvalid-offsetof"
  159. const uintptr_t surfacePtr = (uintptr_t)swapchain - offsetof(UnityDisplaySurfaceMTL, swapchain);
  160. UnityDisplaySurfaceMTL* surface = (UnityDisplaySurfaceMTL*)surfacePtr;
  161. surface->layer = swapchain->layer;
  162. surface->nextDrawable = swapchain->nextDrawable;
  163. surface->drawable = swapchain->drawable;
  164. surface->drawableTex = swapchain->drawableTexture;
  165. #pragma clang diagnostic pop
  166. #endif
  167. return drawableTex;
  168. }
  169. UNITY_EXPORT extern "C" MTLTextureRef AcquireDrawableMTL(UnityDisplaySurfaceMTL* surface)
  170. {
  171. if (!surface)
  172. return nil;
  173. return AcquireSwapchainDrawable(&surface->swapchain);
  174. }
  175. UNITY_EXPORT extern "C" int UnityCommandQueueMaxCommandBufferCountMTL()
  176. {
  177. // customizable argument to pass towards [MTLDevice newCommandQueueWithMaxCommandBufferCount:],
  178. // the default value is 64 but with Parallel Render Encoder workloads, it might need to be increased
  179. return 256;
  180. }
  181. UNITY_EXPORT extern "C" void StartFrameRenderingMTL(UnityDisplaySurfaceMTL* surface)
  182. {
  183. }
  184. UNITY_EXPORT extern "C" void EndFrameRenderingMTL(UnityDisplaySurfaceMTL* surface)
  185. {
  186. }