glcontext_nsgl.mm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #if BX_PLATFORM_OSX && (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # include <AvailabilityMacros.h>
  9. # include <Cocoa/Cocoa.h>
  10. # include <bx/os.h>
  11. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wdeprecated-declarations")
  12. namespace bgfx { namespace gl
  13. {
  14. # define GL_IMPORT(_optional, _proto, _func, _import) _proto _func
  15. # include "glimports.h"
  16. struct SwapChainGL
  17. {
  18. SwapChainGL(void* _nwh)
  19. {
  20. BX_UNUSED(_nwh);
  21. }
  22. ~SwapChainGL()
  23. {
  24. }
  25. void makeCurrent()
  26. {
  27. }
  28. void swapBuffers()
  29. {
  30. }
  31. };
  32. class AutoreleasePoolHolder
  33. {
  34. public:
  35. AutoreleasePoolHolder() : m_pool([[NSAutoreleasePool alloc] init])
  36. {
  37. }
  38. ~AutoreleasePoolHolder()
  39. {
  40. [m_pool release];
  41. }
  42. private:
  43. AutoreleasePoolHolder(AutoreleasePoolHolder const&);
  44. NSAutoreleasePool* const m_pool;
  45. };
  46. static void* s_opengl = NULL;
  47. void GlContext::create(uint32_t _width, uint32_t _height)
  48. {
  49. BX_UNUSED(_width, _height);
  50. s_opengl = bx::dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL");
  51. BX_ASSERT(NULL != s_opengl, "OpenGL dynamic library is not found!");
  52. const AutoreleasePoolHolder pool;
  53. NSObject* nwh = (NSObject*)g_platformData.nwh;
  54. m_context = g_platformData.context;
  55. NSWindow* nsWindow = nil;
  56. NSView* contentView = nil;
  57. if ([nwh isKindOfClass:[NSView class]])
  58. {
  59. contentView = (NSView*)nwh;
  60. }
  61. else if ([nwh isKindOfClass:[NSWindow class]])
  62. {
  63. nsWindow = (NSWindow*)nwh;
  64. contentView = [nsWindow contentView];
  65. }
  66. if (NULL == g_platformData.context)
  67. {
  68. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  69. NSOpenGLPixelFormatAttribute profile =
  70. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  71. NSOpenGLProfileVersion3_2Core
  72. #else
  73. NSOpenGLProfileVersionLegacy
  74. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  75. ;
  76. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  77. NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
  78. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  79. NSOpenGLPFAOpenGLProfile, profile,
  80. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  81. NSOpenGLPFAColorSize, 24,
  82. NSOpenGLPFAAlphaSize, 8,
  83. NSOpenGLPFADepthSize, 24,
  84. NSOpenGLPFAStencilSize, 8,
  85. NSOpenGLPFADoubleBuffer, true,
  86. NSOpenGLPFAAccelerated, true,
  87. NSOpenGLPFANoRecovery, true,
  88. 0, 0,
  89. };
  90. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
  91. BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format.");
  92. NSRect glViewRect = [contentView bounds];
  93. NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat];
  94. [pixelFormat release];
  95. // GLFW creates a helper contentView that handles things like keyboard and drag and
  96. // drop events. We don't want to clobber that view if it exists. Instead we just
  97. // add ourselves as a subview and make the view resize automatically.
  98. if (nil != contentView)
  99. {
  100. [glView setAutoresizingMask:( NSViewHeightSizable |
  101. NSViewWidthSizable |
  102. NSViewMinXMargin |
  103. NSViewMaxXMargin |
  104. NSViewMinYMargin |
  105. NSViewMaxYMargin )];
  106. [contentView addSubview:glView];
  107. }
  108. else
  109. {
  110. if (nil != nsWindow)
  111. [nsWindow setContentView:glView];
  112. }
  113. NSOpenGLContext* glContext = [glView openGLContext];
  114. BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context.");
  115. [glContext makeCurrentContext];
  116. GLint interval = 0;
  117. [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  118. // When initializing NSOpenGLView programatically (as we are), this sometimes doesn't
  119. // get hooked up properly (especially when there are existing window elements). This ensures
  120. // we are valid. Otherwise, you'll probably get a GL_INVALID_FRAMEBUFFER_OPERATION when
  121. // trying to glClear() for the first time.
  122. [glContext setView:glView];
  123. m_view = glView;
  124. m_context = glContext;
  125. }
  126. else
  127. {
  128. [(NSOpenGLContext*)g_platformData.context makeCurrentContext];
  129. }
  130. import();
  131. g_internalData.context = m_context;
  132. }
  133. void GlContext::destroy()
  134. {
  135. if (NULL == g_platformData.context)
  136. {
  137. NSOpenGLView* glView = (NSOpenGLView*)m_view;
  138. [glView release];
  139. }
  140. m_view = NULL;
  141. m_context = NULL;
  142. bx::dlclose(s_opengl);
  143. }
  144. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  145. {
  146. BX_UNUSED(_width, _height);
  147. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  148. bool hidpi = !!(_flags&BGFX_RESET_HIDPI);
  149. if (m_view)
  150. {
  151. NSOpenGLView* glView = (NSOpenGLView*)m_view;
  152. if ([glView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)])
  153. [glView setWantsBestResolutionOpenGLSurface:hidpi];
  154. }
  155. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  156. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  157. GLint interval = vsync ? 1 : 0;
  158. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  159. [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  160. [glContext update];
  161. }
  162. uint64_t GlContext::getCaps() const
  163. {
  164. uint64_t caps = 0;
  165. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  166. NSObject* nwh = (NSObject*)g_platformData.nwh;
  167. if ([nwh respondsToSelector:@selector(backingScaleFactor)] && (1.0f < [(id)nwh backingScaleFactor]))
  168. caps |= BGFX_CAPS_HIDPI;
  169. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  170. return caps;
  171. }
  172. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  173. {
  174. return BX_NEW(g_allocator, SwapChainGL)(_nwh);
  175. }
  176. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  177. {
  178. BX_DELETE(g_allocator, _swapChain);
  179. }
  180. void GlContext::swap(SwapChainGL* _swapChain)
  181. {
  182. if (NULL == _swapChain)
  183. {
  184. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  185. [glContext makeCurrentContext];
  186. [glContext flushBuffer];
  187. }
  188. else
  189. {
  190. _swapChain->makeCurrent();
  191. _swapChain->swapBuffers();
  192. }
  193. }
  194. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  195. {
  196. if (NULL == _swapChain)
  197. {
  198. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  199. [glContext makeCurrentContext];
  200. }
  201. else
  202. {
  203. _swapChain->makeCurrent();
  204. }
  205. }
  206. void GlContext::import()
  207. {
  208. BX_TRACE("Import:");
  209. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  210. { \
  211. if (_func == NULL) \
  212. { \
  213. _func = (_proto)bx::dlsym(s_opengl, #_import); \
  214. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  215. } \
  216. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. GetProcAddress(\"%s\")", #_import); \
  217. }
  218. # include "glimports.h"
  219. }
  220. } /* namespace gl */ } // namespace bgfx
  221. void* nsglGetProcAddress(const GLubyte* _name)
  222. {
  223. using namespace bgfx::gl;
  224. if (NULL == s_opengl)
  225. {
  226. s_opengl = bx::dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL");
  227. }
  228. return bx::dlsym(s_opengl, (const char*)_name);
  229. }
  230. #endif // BX_PLATFORM_OSX && (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)