glcontext_nsgl.mm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2011-2019 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_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!");
  52. const AutoreleasePoolHolder pool;
  53. NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
  54. m_context = g_platformData.context;
  55. if (NULL == g_platformData.context)
  56. {
  57. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  58. NSOpenGLPixelFormatAttribute profile =
  59. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  60. NSOpenGLProfileVersion3_2Core
  61. #else
  62. NSOpenGLProfileVersionLegacy
  63. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  64. ;
  65. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  66. NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
  67. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  68. NSOpenGLPFAOpenGLProfile, profile,
  69. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  70. NSOpenGLPFAColorSize, 24,
  71. NSOpenGLPFAAlphaSize, 8,
  72. NSOpenGLPFADepthSize, 24,
  73. NSOpenGLPFAStencilSize, 8,
  74. NSOpenGLPFADoubleBuffer, true,
  75. NSOpenGLPFAAccelerated, true,
  76. NSOpenGLPFANoRecovery, true,
  77. 0, 0,
  78. };
  79. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
  80. BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format.");
  81. NSRect glViewRect = [[nsWindow contentView] bounds];
  82. NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat];
  83. [pixelFormat release];
  84. // GLFW creates a helper contentView that handles things like keyboard and drag and
  85. // drop events. We don't want to clobber that view if it exists. Instead we just
  86. // add ourselves as a subview and make the view resize automatically.
  87. NSView *contentView = [nsWindow contentView];
  88. if (nil != contentView)
  89. {
  90. [glView setAutoresizingMask:( NSViewHeightSizable |
  91. NSViewWidthSizable |
  92. NSViewMinXMargin |
  93. NSViewMaxXMargin |
  94. NSViewMinYMargin |
  95. NSViewMaxYMargin )];
  96. [contentView addSubview:glView];
  97. }
  98. else
  99. {
  100. [nsWindow setContentView:glView];
  101. }
  102. NSOpenGLContext* glContext = [glView openGLContext];
  103. BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context.");
  104. [glContext makeCurrentContext];
  105. GLint interval = 0;
  106. [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  107. // When initializing NSOpenGLView programatically (as we are), this sometimes doesn't
  108. // get hooked up properly (especially when there are existing window elements). This ensures
  109. // we are valid. Otherwise, you'll probably get a GL_INVALID_FRAMEBUFFER_OPERATION when
  110. // trying to glClear() for the first time.
  111. [glContext setView:glView];
  112. m_view = glView;
  113. m_context = glContext;
  114. }
  115. import();
  116. g_internalData.context = m_context;
  117. }
  118. void GlContext::destroy()
  119. {
  120. if (NULL == g_platformData.context)
  121. {
  122. NSOpenGLView* glView = (NSOpenGLView*)m_view;
  123. [glView release];
  124. }
  125. m_view = NULL;
  126. m_context = NULL;
  127. bx::dlclose(s_opengl);
  128. }
  129. void GlContext::resize(uint32_t _width, uint32_t _height, uint32_t _flags)
  130. {
  131. BX_UNUSED(_width, _height);
  132. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  133. bool hidpi = !!(_flags&BGFX_RESET_HIDPI);
  134. NSOpenGLView* glView = (NSOpenGLView*)m_view;
  135. if ([glView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)])
  136. [glView setWantsBestResolutionOpenGLSurface:hidpi];
  137. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  138. bool vsync = !!(_flags&BGFX_RESET_VSYNC);
  139. GLint interval = vsync ? 1 : 0;
  140. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  141. [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  142. [glContext update];
  143. }
  144. uint64_t GlContext::getCaps() const
  145. {
  146. uint64_t caps = 0;
  147. #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  148. NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
  149. if ([nsWindow respondsToSelector:@selector(backingScaleFactor)] && (1.0f < [nsWindow backingScaleFactor]))
  150. caps |= BGFX_CAPS_HIDPI;
  151. #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
  152. return caps;
  153. }
  154. SwapChainGL* GlContext::createSwapChain(void* _nwh)
  155. {
  156. return BX_NEW(g_allocator, SwapChainGL)(_nwh);
  157. }
  158. void GlContext::destroySwapChain(SwapChainGL* _swapChain)
  159. {
  160. BX_DELETE(g_allocator, _swapChain);
  161. }
  162. void GlContext::swap(SwapChainGL* _swapChain)
  163. {
  164. if (NULL == _swapChain)
  165. {
  166. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  167. [glContext makeCurrentContext];
  168. [glContext flushBuffer];
  169. }
  170. else
  171. {
  172. _swapChain->makeCurrent();
  173. _swapChain->swapBuffers();
  174. }
  175. }
  176. void GlContext::makeCurrent(SwapChainGL* _swapChain)
  177. {
  178. if (NULL == _swapChain)
  179. {
  180. NSOpenGLContext* glContext = (NSOpenGLContext*)m_context;
  181. [glContext makeCurrentContext];
  182. }
  183. else
  184. {
  185. _swapChain->makeCurrent();
  186. }
  187. }
  188. void GlContext::import()
  189. {
  190. BX_TRACE("Import:");
  191. # define GL_EXTENSION(_optional, _proto, _func, _import) \
  192. { \
  193. if (_func == NULL) \
  194. { \
  195. _func = (_proto)bx::dlsym(s_opengl, #_import); \
  196. BX_TRACE("%p " #_func " (" #_import ")", _func); \
  197. } \
  198. BGFX_FATAL(_optional || NULL != _func, Fatal::UnableToInitialize, "Failed to create OpenGL context. GetProcAddress(\"%s\")", #_import); \
  199. }
  200. # include "glimports.h"
  201. }
  202. } /* namespace gl */ } // namespace bgfx
  203. void* nsglGetProcAddress(const GLubyte* _name)
  204. {
  205. using namespace bgfx::gl;
  206. if (NULL == s_opengl)
  207. {
  208. s_opengl = bx::dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL");
  209. }
  210. return bx::dlsym(s_opengl, (const char*)_name);
  211. }
  212. #endif // BX_PLATFORM_OSX && (BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3|BGFX_CONFIG_RENDERER_OPENGL)