BsMacOSContext.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "MacOS/BsMacOSContext.h"
  4. #include "MacOS/BsMacOSGLSupport.h"
  5. #define BS_COCOA_INTERNALS
  6. #include "Private/MacOS/BsMacOSWindow.h"
  7. #include "Private/MacOS/BsMacOSPlatform.h"
  8. #import <AppKit/AppKit.h>
  9. namespace bs::ct
  10. {
  11. struct MacOSContext::Pimpl
  12. {
  13. NSOpenGLContext* context = nil;
  14. std::atomic_bool dirty{true};
  15. };
  16. MacOSContext::MacOSContext(bool depthStencil, UINT32 msaaCount)
  17. { @autoreleasepool {
  18. NSOpenGLPixelFormatAttribute attributes[17];
  19. UINT32 attrIdx = 0;
  20. attributes[attrIdx++] = NSOpenGLPFAOpenGLProfile;
  21. attributes[attrIdx++] = NSOpenGLProfileVersion4_1Core;
  22. attributes[attrIdx++] = NSOpenGLPFADoubleBuffer;
  23. attributes[attrIdx++] = NSOpenGLPFAAccelerated;
  24. attributes[attrIdx++] = NSOpenGLPFAColorSize;
  25. attributes[attrIdx++] = 32;
  26. if(depthStencil)
  27. {
  28. attributes[attrIdx++] = NSOpenGLPFADepthSize;
  29. attributes[attrIdx++] = 24;
  30. attributes[attrIdx++] = NSOpenGLPFAStencilSize;
  31. attributes[attrIdx++] = 8;
  32. }
  33. if(msaaCount > 1)
  34. {
  35. attributes[attrIdx++] = NSOpenGLPFAMultisample;
  36. attributes[attrIdx++] = NSOpenGLPFASampleBuffers;
  37. attributes[attrIdx++] = 1;
  38. attributes[attrIdx++] = NSOpenGLPFASamples;
  39. attributes[attrIdx++] = msaaCount;
  40. attributes[attrIdx++] = NSOpenGLPFANoRecovery;
  41. }
  42. attributes[attrIdx] = 0;
  43. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  44. m = bs_new<Pimpl>();
  45. m->context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
  46. markAsDirty();
  47. }}
  48. MacOSContext::~MacOSContext()
  49. {
  50. bs_delete(m);
  51. }
  52. void MacOSContext::setCurrent(const RenderWindow& renderWindow)
  53. {
  54. UINT32 windowId;
  55. renderWindow.getCustomAttribute("WINDOW_ID", &windowId);
  56. MacOSPlatform::lockWindows();
  57. CocoaWindow* window = MacOSPlatform::getWindow(windowId);
  58. if(!window)
  59. {
  60. MacOSPlatform::unlockWindows();
  61. return;
  62. }
  63. NSWindow* nsWindow = window->_getPrivateData()->window;
  64. [m->context setView:[nsWindow contentView]];
  65. [m->context makeCurrentContext];
  66. [m->context update];
  67. updateIfDirty();
  68. MacOSPlatform::unlockWindows();
  69. }
  70. void MacOSContext::endCurrent()
  71. {
  72. [m->context setView:nil];
  73. [m->context clearDrawable];
  74. [NSOpenGLContext clearCurrentContext];
  75. }
  76. void MacOSContext::releaseContext()
  77. {
  78. m->context = nil;
  79. }
  80. void MacOSContext::markAsDirty()
  81. {
  82. m->dirty = true;
  83. }
  84. void MacOSContext::updateIfDirty()
  85. {
  86. if(m->dirty)
  87. {
  88. [m->context update];
  89. m->dirty = false;
  90. }
  91. }
  92. void MacOSContext::setVSync(int interval)
  93. {
  94. if(interval < 0)
  95. interval = 0;
  96. [m->context setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  97. }
  98. void MacOSContext::swapBuffers()
  99. {
  100. [m->context flushBuffer];
  101. updateIfDirty();
  102. }
  103. void MacOSContext::lock()
  104. {
  105. CGLLockContext(m->context.CGLContextObj);
  106. }
  107. void MacOSContext::unlock()
  108. {
  109. CGLUnlockContext(m->context.CGLContextObj);
  110. }
  111. }