GlesHelper.mm 15 KB

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