BsMacOSContext.mm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #import <AppKit/AppKit.h>
  8. namespace bs::ct
  9. {
  10. struct MacOSContext::Pimpl
  11. {
  12. NSOpenGLContext* context = nil;
  13. bool dirty = true;
  14. };
  15. MacOSContext::MacOSContext(bool depthStencil, UINT32 msaaCount)
  16. { @autoreleasepool {
  17. NSOpenGLPixelFormatAttribute attributes[] =
  18. {
  19. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core,
  20. NSOpenGLPFADoubleBuffer,
  21. NSOpenGLPFAAccelerated,
  22. NSOpenGLPFADepthSize, depthStencil ? 24U : 0U,
  23. NSOpenGLPFAStencilSize, depthStencil ? 8U : 0U,
  24. NSOpenGLPFASampleBuffers, msaaCount > 1 ? 1U : 0U,
  25. };
  26. UINT32 attrIdx = 0;
  27. attributes[attrIdx++] = NSOpenGLPFAOpenGLProfile;
  28. attributes[attrIdx++] = NSOpenGLProfileVersion4_1Core;
  29. attributes[attrIdx++] = NSOpenGLPFADoubleBuffer;
  30. attributes[attrIdx++] = NSOpenGLPFAAccelerated;
  31. attributes[attrIdx++] = NSOpenGLPFAColorSize;
  32. attributes[attrIdx++] = 32;
  33. if(depthStencil)
  34. {
  35. attributes[attrIdx++] = NSOpenGLPFADepthSize;
  36. attributes[attrIdx++] = 24;
  37. attributes[attrIdx++] = NSOpenGLPFAStencilSize;
  38. attributes[attrIdx++] = 8;
  39. }
  40. if(msaaCount > 1)
  41. {
  42. attributes[attrIdx++] = NSOpenGLPFAMultisample;
  43. attributes[attrIdx++] = NSOpenGLPFASampleBuffers;
  44. attributes[attrIdx++] = 1;
  45. attributes[attrIdx++] = NSOpenGLPFASamples;
  46. attributes[attrIdx++] = msaaCount;
  47. attributes[attrIdx++] = NSOpenGLPFANoRecovery;
  48. }
  49. attributes[attrIdx] = 0;
  50. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  51. m = bs_new<Pimpl>();
  52. m->context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
  53. markAsDirty();
  54. }}
  55. MacOSContext::~MacOSContext()
  56. {
  57. bs_delete(m);
  58. }
  59. void MacOSContext::setCurrent(const RenderWindow& renderWindow)
  60. {
  61. CocoaWindow* window;
  62. renderWindow.getCustomAttribute("COCOA_WINDOW", &window);
  63. NSWindow* nsWindow = window->_getPrivateData()->window;
  64. [m->context setView:[nsWindow contentView]];
  65. [m->context makeCurrentContext];
  66. [m->context update];
  67. m->dirty = false;
  68. }
  69. void MacOSContext::endCurrent()
  70. {
  71. [m->context setView:nil];
  72. [m->context clearDrawable];
  73. [NSOpenGLContext clearCurrentContext];
  74. }
  75. void MacOSContext::releaseContext()
  76. {
  77. m->context = nil;
  78. }
  79. void MacOSContext::markAsDirty()
  80. {
  81. m->dirty = true;
  82. }
  83. void MacOSContext::updateIfDirty()
  84. {
  85. if(m->dirty)
  86. {
  87. [m->context update];
  88. m->dirty = false;
  89. }
  90. }
  91. void MacOSContext::setVSync(int interval)
  92. {
  93. if(interval < 0)
  94. interval = 0;
  95. [m->context setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  96. }
  97. void MacOSContext::swapBuffers()
  98. {
  99. [m->context flushBuffer];
  100. updateIfDirty();
  101. }
  102. }