EAGLContextHelper.mm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. [(__bridge EAGLContext*)eaglContext renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  14. }
  15. extern "C" EAGLContext* UnityCreateContextEAGL(EAGLContext * parent, int api)
  16. {
  17. const int targetApi = parent ? parent.API : api;
  18. EAGLSharegroup* group = parent ? parent.sharegroup : nil;
  19. return [[EAGLContext alloc] initWithAPI: (EAGLRenderingAPI)targetApi sharegroup: group];
  20. }
  21. extern "C" void UnityMakeCurrentContextEAGL(EAGLContext* context)
  22. {
  23. [EAGLContext setCurrentContext: context];
  24. }
  25. extern "C" EAGLContext* UnityGetCurrentContextEAGL()
  26. {
  27. return [EAGLContext currentContext];
  28. }
  29. EAGLContextSetCurrentAutoRestore::EAGLContextSetCurrentAutoRestore(EAGLContext* cur_) : old([EAGLContext currentContext]), cur(cur_)
  30. {
  31. if (old != cur)
  32. {
  33. [EAGLContext setCurrentContext: cur];
  34. UnityOnSetCurrentGLContext(cur);
  35. }
  36. }
  37. EAGLContextSetCurrentAutoRestore::EAGLContextSetCurrentAutoRestore(UnityDisplaySurfaceBase* surface)
  38. : old(surface->api == apiMetal ? nil : [EAGLContext currentContext]),
  39. cur(surface->api == apiMetal ? nil : ((UnityDisplaySurfaceGLES*)surface)->context)
  40. {
  41. if (old != cur)
  42. {
  43. [EAGLContext setCurrentContext: cur];
  44. UnityOnSetCurrentGLContext(cur);
  45. }
  46. }
  47. EAGLContextSetCurrentAutoRestore::~EAGLContextSetCurrentAutoRestore()
  48. {
  49. if (old != cur)
  50. {
  51. [EAGLContext setCurrentContext: old];
  52. UnityOnSetCurrentGLContext(old);
  53. }
  54. }