EAGLContextHelper.mm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "EAGLContextHelper.h"
  2. #include "UnityRendering.h"
  3. #import <QuartzCore/QuartzCore.h>
  4. #import <OpenGLES/EAGL.h>
  5. #import <OpenGLES/ES2/gl.h>
  6. #import <OpenGLES/ES2/glext.h>
  7. extern "C" bool AllocateRenderBufferStorageFromEAGLLayer(void* eaglContext, void* eaglLayer)
  8. {
  9. return [(__bridge EAGLContext*)eaglContext renderbufferStorage: GL_RENDERBUFFER fromDrawable: (__bridge CAEAGLLayer*)eaglLayer];
  10. }
  11. extern "C" void DeallocateRenderBufferStorageFromEAGLLayer(void* eaglContext)
  12. {
  13. // After deprecating OpenGL ES, Apple implement gles driver in metal.
  14. // Alas, it seem that on older iOS versions (< 13.0) there seems to be a bug in
  15. // [EAGLContext renderbufferStorage: fromDrawable:nil]
  16. // resulting in metal validation failure.
  17. // Thankfully this code path is taken only to delete the backbuffer, so this is not that bad:
  18. // we go there only on exit/going-to-background or unloading unity library
  19. // If we look at the metal - all this would mean drawable recreation (and this happens inside gles driver)
  20. // so memory-wise we should be still fine ignoring this completely
  21. if (UnityiOS130orNewer())
  22. [(__bridge EAGLContext*)eaglContext renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  23. }
  24. extern "C" EAGLContext* UnityCreateContextEAGL(EAGLContext * parent, int api)
  25. {
  26. const int targetApi = parent ? parent.API : api;
  27. EAGLSharegroup* group = parent ? parent.sharegroup : nil;
  28. return [[EAGLContext alloc] initWithAPI: (EAGLRenderingAPI)targetApi sharegroup: group];
  29. }
  30. extern "C" void UnityMakeCurrentContextEAGL(EAGLContext* context)
  31. {
  32. [EAGLContext setCurrentContext: context];
  33. }
  34. extern "C" EAGLContext* UnityGetCurrentContextEAGL()
  35. {
  36. return [EAGLContext currentContext];
  37. }
  38. EAGLContextSetCurrentAutoRestore::EAGLContextSetCurrentAutoRestore(EAGLContext* cur_) : old([EAGLContext currentContext]), cur(cur_)
  39. {
  40. if (old != cur)
  41. {
  42. [EAGLContext setCurrentContext: cur];
  43. UnityOnSetCurrentGLContext(cur);
  44. }
  45. }
  46. EAGLContextSetCurrentAutoRestore::EAGLContextSetCurrentAutoRestore(UnityDisplaySurfaceBase* surface)
  47. : old(surface->api == apiMetal ? nil : [EAGLContext currentContext]),
  48. cur(surface->api == apiMetal ? nil : ((UnityDisplaySurfaceGLES*)surface)->context)
  49. {
  50. if (old != cur)
  51. {
  52. [EAGLContext setCurrentContext: cur];
  53. UnityOnSetCurrentGLContext(cur);
  54. }
  55. }
  56. EAGLContextSetCurrentAutoRestore::~EAGLContextSetCurrentAutoRestore()
  57. {
  58. if (old != cur)
  59. {
  60. [EAGLContext setCurrentContext: old];
  61. if (old)
  62. UnityOnSetCurrentGLContext(old);
  63. }
  64. }