GlesHelper.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include <QuartzCore/QuartzCore.h>
  2. #include <stdio.h>
  3. #include "GlesHelper.h"
  4. #include "UnityAppController.h"
  5. #include "DisplayManager.h"
  6. #include "EAGLContextHelper.h"
  7. #include "CVTextureCache.h"
  8. #include "InternalProfiler.h"
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // OpenGLES2 and OpenGLES3 on iOS and tvOS are deprecated as of Unity 2019.3 ///
  11. ////////////////////////////////////////////////////////////////////////////////
  12. // here goes some gles magic
  13. // we include gles3 header so we will use gles3 constants.
  14. // sure all the actual gles3 is guarded (and constants are staying same)
  15. #include <OpenGLES/ES3/gl.h>
  16. #include <OpenGLES/ES3/glext.h>
  17. // here are the prototypes for gles2 ext functions that moved to core in gles3
  18. extern "C" void glDiscardFramebufferEXT(GLenum target, GLsizei numAttachments, const GLenum* attachments);
  19. extern "C" void glRenderbufferStorageMultisampleAPPLE(GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height);
  20. extern "C" void glResolveMultisampleFramebufferAPPLE(void);
  21. #define SAFE_GL_DELETE(func, obj) do { if(obj) { func(1,&obj); obj = 0; } } while(0)
  22. #define DISCARD_FBO(ctx, fbo, cnt, att) \
  23. do{ \
  24. if(surface->context.API >= 3) glInvalidateFramebuffer(fbo, cnt, att);\
  25. else if(_supportsDiscard) glDiscardFramebufferEXT(fbo, cnt, att);\
  26. } while(0)
  27. #define CREATE_RB_AA(ctx, aa, fmt, w, h) \
  28. do{ \
  29. if(surface->context.API >= 3) glRenderbufferStorageMultisample(GL_RENDERBUFFER, aa, fmt, w, h); \
  30. else if(_supportsDiscard) glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, aa, fmt, w, h);\
  31. } while(0)
  32. static bool _supportsDiscard = false;
  33. static bool _supportsPackedStencil = false;
  34. extern "C" void InitRenderingGLES()
  35. {
  36. int api = UnitySelectedRenderingAPI();
  37. assert(api == apiOpenGLES2 || api == apiOpenGLES3);
  38. EAGLContextSetCurrentAutoRestore autorestore(GetMainDisplaySurface());
  39. _supportsDiscard = api == apiOpenGLES2 ? UnityHasRenderingAPIExtension("GL_EXT_discard_framebuffer") : true;
  40. _supportsMSAA = api == apiOpenGLES2 ? UnityHasRenderingAPIExtension("GL_APPLE_framebuffer_multisample") : true;
  41. _supportsPackedStencil = api == apiOpenGLES2 ? UnityHasRenderingAPIExtension("GL_OES_packed_depth_stencil") : true;
  42. }
  43. extern "C" void CreateSystemRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface)
  44. {
  45. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  46. DestroySystemRenderingSurfaceGLES(surface);
  47. if (UnityPreserveFramebufferAlpha())
  48. {
  49. const CGFloat components[] = {1.0f, 1.0f, 1.0f, 0.0f};
  50. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  51. CGColorRef color = CGColorCreate(colorSpace, components);
  52. surface->layer.opaque = NO;
  53. surface->layer.backgroundColor = color;
  54. CGColorRelease(color);
  55. CGColorSpaceRelease(colorSpace);
  56. }
  57. else
  58. surface->layer.opaque = YES;
  59. surface->layer.drawableProperties = @{ kEAGLDrawablePropertyRetainedBacking: @(FALSE), kEAGLDrawablePropertyColorFormat: kEAGLColorFormatRGBA8 };
  60. surface->colorFormat = GL_RGBA8;
  61. glGenRenderbuffers(1, &surface->systemColorRB);
  62. glBindRenderbuffer(GL_RENDERBUFFER, surface->systemColorRB);
  63. AllocateRenderBufferStorageFromEAGLLayer((__bridge void*)surface->context, (__bridge void*)surface->layer);
  64. glGenFramebuffers(1, &surface->systemFB);
  65. UnityBindFramebuffer(kDrawFramebuffer, surface->systemFB);
  66. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, surface->systemColorRB);
  67. }
  68. extern "C" void CreateRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface)
  69. {
  70. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  71. DestroyRenderingSurfaceGLES(surface);
  72. bool needRenderingSurface = surface->targetW != surface->systemW || surface->targetH != surface->systemH || surface->useCVTextureCache;
  73. if (needRenderingSurface)
  74. {
  75. GLint oldTexBinding = 0;
  76. if (surface->useCVTextureCache)
  77. surface->cvTextureCache = CreateCVTextureCache();
  78. if (surface->cvTextureCache)
  79. {
  80. surface->cvTextureCacheTexture = CreateReadableRTFromCVTextureCache(surface->cvTextureCache, surface->targetW, surface->targetH, &surface->cvPixelBuffer);
  81. surface->targetColorRT = GetGLTextureFromCVTextureCache(surface->cvTextureCacheTexture);
  82. }
  83. else
  84. {
  85. glGenTextures(1, &surface->targetColorRT);
  86. }
  87. glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTexBinding);
  88. glBindTexture(GL_TEXTURE_2D, surface->targetColorRT);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLES_UPSCALE_FILTER);
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLES_UPSCALE_FILTER);
  91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  92. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  93. if (!surface->cvTextureCache)
  94. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->targetW, surface->targetH, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  95. glGenFramebuffers(1, &surface->targetFB);
  96. UnityBindFramebuffer(kDrawFramebuffer, surface->targetFB);
  97. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, surface->targetColorRT, 0);
  98. glBindTexture(GL_TEXTURE_2D, oldTexBinding);
  99. }
  100. if (_supportsMSAA && surface->msaaSamples > 1)
  101. {
  102. glGenRenderbuffers(1, &surface->msaaColorRB);
  103. glBindRenderbuffer(GL_RENDERBUFFER, surface->msaaColorRB);
  104. glGenFramebuffers(1, &surface->msaaFB);
  105. UnityBindFramebuffer(kDrawFramebuffer, surface->msaaFB);
  106. CREATE_RB_AA(surface->context, surface->msaaSamples, surface->colorFormat, surface->targetW, surface->targetH);
  107. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, surface->msaaColorRB);
  108. }
  109. }
  110. extern "C" void CreateSharedDepthbufferGLES(UnityDisplaySurfaceGLES* surface)
  111. {
  112. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  113. DestroySharedDepthbufferGLES(surface);
  114. if (surface->disableDepthAndStencil)
  115. return;
  116. surface->depthFormat = GL_DEPTH_COMPONENT24;
  117. if (_supportsPackedStencil)
  118. surface->depthFormat = GL_DEPTH24_STENCIL8;
  119. glGenRenderbuffers(1, &surface->depthRB);
  120. glBindRenderbuffer(GL_RENDERBUFFER, surface->depthRB);
  121. bool needMSAA = _supportsMSAA && surface->msaaSamples > 1;
  122. if (needMSAA)
  123. CREATE_RB_AA(surface->context, surface->msaaSamples, surface->depthFormat, surface->targetW, surface->targetH);
  124. if (!needMSAA)
  125. glRenderbufferStorage(GL_RENDERBUFFER, surface->depthFormat, surface->targetW, surface->targetH);
  126. if (surface->msaaFB)
  127. UnityBindFramebuffer(kDrawFramebuffer, surface->msaaFB);
  128. else if (surface->targetFB)
  129. UnityBindFramebuffer(kDrawFramebuffer, surface->targetFB);
  130. else
  131. UnityBindFramebuffer(kDrawFramebuffer, surface->systemFB);
  132. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, surface->depthRB);
  133. if (_supportsPackedStencil)
  134. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, surface->depthRB);
  135. }
  136. extern "C" void CreateUnityRenderBuffersGLES(UnityDisplaySurfaceGLES* surface)
  137. {
  138. UnityRenderBufferDesc target_desc = {surface->targetW, surface->targetH, 1, (unsigned int)surface->msaaSamples, 1};
  139. UnityRenderBufferDesc system_desc = {surface->systemW, surface->systemH, 1, 1, 1};
  140. {
  141. unsigned texid = 0, rbid = 0, fbo = 0;
  142. if (surface->msaaFB)
  143. {
  144. rbid = surface->msaaColorRB;
  145. fbo = surface->msaaFB;
  146. }
  147. else if (surface->targetFB)
  148. {
  149. texid = surface->targetColorRT;
  150. fbo = surface->targetFB;
  151. }
  152. else
  153. {
  154. rbid = surface->systemColorRB;
  155. fbo = surface->systemFB;
  156. }
  157. surface->unityColorBuffer = UnityCreateExternalSurfaceGLES(surface->unityColorBuffer, true, texid, rbid, surface->colorFormat, &target_desc);
  158. if (surface->depthRB)
  159. surface->unityDepthBuffer = UnityCreateExternalSurfaceGLES(surface->unityDepthBuffer, false, 0, surface->depthRB, surface->depthFormat, &target_desc);
  160. else
  161. surface->unityDepthBuffer = UnityCreateDummySurface(surface->unityDepthBuffer, false, &target_desc);
  162. UnityRegisterFBO(surface->unityColorBuffer, surface->unityDepthBuffer, fbo);
  163. }
  164. surface->systemColorBuffer = surface->systemDepthBuffer = 0;
  165. if (surface->msaaFB || surface->targetFB)
  166. {
  167. unsigned rbid = surface->systemColorRB;
  168. surface->systemColorBuffer = UnityCreateExternalSurfaceGLES(surface->systemColorBuffer, true, 0, rbid, surface->colorFormat, &system_desc);
  169. surface->systemDepthBuffer = UnityCreateDummySurface(surface->systemDepthBuffer, false, &system_desc);
  170. UnityRegisterFBO(surface->systemColorBuffer, surface->systemDepthBuffer, surface->systemFB);
  171. }
  172. surface->resolvedColorBuffer = 0;
  173. if (surface->msaaFB && surface->targetFB)
  174. surface->resolvedColorBuffer = UnityCreateExternalSurfaceGLES(surface->resolvedColorBuffer, true, surface->targetColorRT, 0, surface->colorFormat, &target_desc);
  175. }
  176. extern "C" void DestroySystemRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface)
  177. {
  178. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  179. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  180. UnityBindFramebuffer(kDrawFramebuffer, 0);
  181. if (surface->systemColorRB)
  182. {
  183. glBindRenderbuffer(GL_RENDERBUFFER, surface->systemColorRB);
  184. DeallocateRenderBufferStorageFromEAGLLayer((__bridge void*)surface->context);
  185. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  186. glDeleteRenderbuffers(1, &surface->systemColorRB);
  187. surface->systemColorRB = 0;
  188. }
  189. if (surface->targetFB == 0 && surface->msaaFB == 0)
  190. SAFE_GL_DELETE(glDeleteRenderbuffers, surface->depthRB);
  191. SAFE_GL_DELETE(glDeleteFramebuffers, surface->systemFB);
  192. }
  193. extern "C" void DestroyRenderingSurfaceGLES(UnityDisplaySurfaceGLES* surface)
  194. {
  195. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  196. if (surface->targetColorRT && !surface->cvTextureCache)
  197. {
  198. glDeleteTextures(1, &surface->targetColorRT); UnityOnDeleteGLTexture(surface->targetColorRT);
  199. surface->targetColorRT = 0;
  200. }
  201. UnityBindFramebuffer(kDrawFramebuffer, 0);
  202. UnityBindFramebuffer(kReadFramebuffer, 0);
  203. if (surface->cvTextureCacheTexture)
  204. CFRelease(surface->cvTextureCacheTexture);
  205. if (surface->cvPixelBuffer)
  206. CFRelease(surface->cvPixelBuffer);
  207. if (surface->cvTextureCache)
  208. CFRelease(surface->cvTextureCache);
  209. surface->cvTextureCache = 0;
  210. SAFE_GL_DELETE(glDeleteFramebuffers, surface->targetFB);
  211. SAFE_GL_DELETE(glDeleteRenderbuffers, surface->msaaColorRB);
  212. SAFE_GL_DELETE(glDeleteFramebuffers, surface->msaaFB);
  213. }
  214. extern "C" void DestroySharedDepthbufferGLES(UnityDisplaySurfaceGLES* surface)
  215. {
  216. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  217. SAFE_GL_DELETE(glDeleteRenderbuffers, surface->depthRB);
  218. }
  219. extern "C" void DestroyUnityRenderBuffersGLES(UnityDisplaySurfaceGLES* surface)
  220. {
  221. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  222. if (surface->unityColorBuffer)
  223. UnityDestroyExternalSurface(surface->unityColorBuffer);
  224. if (surface->systemColorBuffer)
  225. UnityDestroyExternalSurface(surface->systemColorBuffer);
  226. surface->unityColorBuffer = surface->systemColorBuffer = 0;
  227. if (surface->unityDepthBuffer)
  228. UnityDestroyExternalSurface(surface->unityDepthBuffer);
  229. if (surface->systemDepthBuffer)
  230. UnityDestroyExternalSurface(surface->systemDepthBuffer);
  231. surface->unityDepthBuffer = surface->systemDepthBuffer = 0;
  232. if (surface->resolvedColorBuffer)
  233. UnityDestroyExternalSurface(surface->resolvedColorBuffer);
  234. surface->resolvedColorBuffer = 0;
  235. }
  236. extern "C" void PreparePresentGLES(UnityDisplaySurfaceGLES* surface)
  237. {
  238. {
  239. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  240. if (_supportsMSAA && surface->msaaSamples > 1)
  241. {
  242. Profiler_StartMSAAResolve();
  243. GLuint targetFB = surface->targetFB ? surface->targetFB : surface->systemFB;
  244. UnityBindFramebuffer(kReadFramebuffer, surface->msaaFB);
  245. UnityBindFramebuffer(kDrawFramebuffer, targetFB);
  246. GLenum discardAttach[] = {GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
  247. DISCARD_FBO(surface->context, GL_READ_FRAMEBUFFER, 2, discardAttach);
  248. if (surface->context.API < 3)
  249. {
  250. glResolveMultisampleFramebufferAPPLE();
  251. }
  252. else
  253. {
  254. const GLint w = surface->targetW, h = surface->targetH;
  255. glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  256. }
  257. Profiler_EndMSAAResolve();
  258. }
  259. if (surface->allowScreenshot && UnityIsCaptureScreenshotRequested())
  260. {
  261. GLint targetFB = surface->targetFB ? surface->targetFB : surface->systemFB;
  262. UnityBindFramebuffer(kReadFramebuffer, targetFB);
  263. UnityCaptureScreenshot();
  264. }
  265. }
  266. APP_CONTROLLER_RENDER_PLUGIN_METHOD(onFrameResolved);
  267. if (surface->targetColorRT)
  268. {
  269. // shaders are bound to context
  270. EAGLContextSetCurrentAutoRestore autorestore(UnityGetMainScreenContextGLES());
  271. assert(surface->systemColorBuffer != 0 && surface->systemDepthBuffer != 0);
  272. UnityRenderBufferHandle src = surface->resolvedColorBuffer ? surface->resolvedColorBuffer : surface->unityColorBuffer;
  273. UnityBlitToBackbuffer(src, surface->systemColorBuffer, surface->systemDepthBuffer);
  274. }
  275. if (_supportsDiscard)
  276. {
  277. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  278. GLenum discardAttach[] = {GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT};
  279. if (surface->msaaFB)
  280. DISCARD_FBO(surface->context, GL_READ_FRAMEBUFFER, 3, discardAttach);
  281. if (surface->targetFB)
  282. {
  283. UnityBindFramebuffer(kDrawFramebuffer, surface->targetFB);
  284. DISCARD_FBO(surface->context, GL_FRAMEBUFFER, 3, discardAttach);
  285. }
  286. UnityBindFramebuffer(kDrawFramebuffer, surface->systemFB);
  287. DISCARD_FBO(surface->context, GL_FRAMEBUFFER, 2, &discardAttach[1]);
  288. }
  289. }
  290. extern "C" void PresentGLES(UnityDisplaySurfaceGLES* surface)
  291. {
  292. if (surface->context && surface->systemColorRB)
  293. {
  294. EAGLContextSetCurrentAutoRestore autorestore(surface->context);
  295. glBindRenderbuffer(GL_RENDERBUFFER, surface->systemColorRB);
  296. [surface->context presentRenderbuffer: GL_RENDERBUFFER];
  297. }
  298. }
  299. extern "C" void StartFrameRenderingGLES(UnityDisplaySurfaceGLES* /*surface*/)
  300. {
  301. }
  302. extern "C" void EndFrameRenderingGLES(UnityDisplaySurfaceGLES* /*surface*/)
  303. {
  304. }
  305. extern "C" void CheckGLESError(const char* file, int line)
  306. {
  307. GLenum e = glGetError();
  308. if (e)
  309. ::printf("OpenGLES error 0x%04X in %s:%i\n", e, file, line);
  310. }