nsgl_context.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //========================================================================
  2. // GLFW 3.4 macOS - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2009-2019 Camilla Löwy <elmindreda@glfw.org>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. // It is fine to use C99 in this file because it will not be built with VS
  27. //========================================================================
  28. #include "internal.h"
  29. static void makeContextCurrentNSGL(_GLFWwindow* window)
  30. {
  31. if (window)
  32. [window->context.nsgl.object makeCurrentContext];
  33. else
  34. [NSOpenGLContext clearCurrentContext];
  35. _glfwPlatformSetTls(&_glfw.contextSlot, window);
  36. }
  37. static void swapBuffersNSGL(_GLFWwindow* window)
  38. {
  39. // ARP appears to be unnecessary, but this is future-proof
  40. [window->context.nsgl.object flushBuffer];
  41. }
  42. static void swapIntervalNSGL(int interval UNUSED)
  43. {
  44. // As of Mojave this does not work so we use CVDisplayLink instead
  45. _glfwInputError(GLFW_API_UNAVAILABLE,
  46. "NSGL: Swap intervals do not work on macOS");
  47. }
  48. static int extensionSupportedNSGL(const char* extension UNUSED)
  49. {
  50. // There are no NSGL extensions
  51. return false;
  52. }
  53. static GLFWglproc getProcAddressNSGL(const char* procname)
  54. {
  55. CFStringRef symbolName = CFStringCreateWithCString(kCFAllocatorDefault,
  56. procname,
  57. kCFStringEncodingASCII);
  58. GLFWglproc symbol;
  59. *(void **) &symbol = CFBundleGetFunctionPointerForName(_glfw.nsgl.framework,
  60. symbolName);
  61. CFRelease(symbolName);
  62. return symbol;
  63. }
  64. static void destroyContextNSGL(_GLFWwindow* window)
  65. {
  66. [window->context.nsgl.pixelFormat release];
  67. window->context.nsgl.pixelFormat = nil;
  68. [window->context.nsgl.object release];
  69. window->context.nsgl.object = nil;
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. ////// GLFW internal API //////
  73. //////////////////////////////////////////////////////////////////////////
  74. // Initialize OpenGL support
  75. //
  76. bool _glfwInitNSGL(void)
  77. {
  78. if (_glfw.nsgl.framework)
  79. return true;
  80. _glfw.nsgl.framework =
  81. CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
  82. if (_glfw.nsgl.framework == NULL)
  83. {
  84. _glfwInputError(GLFW_API_UNAVAILABLE,
  85. "NSGL: Failed to locate OpenGL framework");
  86. return false;
  87. }
  88. return true;
  89. }
  90. // Terminate OpenGL support
  91. //
  92. void _glfwTerminateNSGL(void)
  93. {
  94. }
  95. // Create the OpenGL context
  96. //
  97. bool _glfwCreateContextNSGL(_GLFWwindow* window,
  98. const _GLFWctxconfig* ctxconfig,
  99. const _GLFWfbconfig* fbconfig)
  100. {
  101. if (ctxconfig->client == GLFW_OPENGL_ES_API)
  102. {
  103. _glfwInputError(GLFW_API_UNAVAILABLE,
  104. "NSGL: OpenGL ES is not available on macOS");
  105. return false;
  106. }
  107. if (ctxconfig->major > 2)
  108. {
  109. if (ctxconfig->major == 3 && ctxconfig->minor < 2)
  110. {
  111. _glfwInputError(GLFW_VERSION_UNAVAILABLE,
  112. "NSGL: The targeted version of macOS does not support OpenGL 3.0 or 3.1 but may support 3.2 and above");
  113. return false;
  114. }
  115. }
  116. // Context robustness modes (GL_KHR_robustness) are not yet supported by
  117. // macOS but are not a hard constraint, so ignore and continue
  118. // Context release behaviors (GL_KHR_context_flush_control) are not yet
  119. // supported by macOS but are not a hard constraint, so ignore and continue
  120. // Debug contexts (GL_KHR_debug) are not yet supported by macOS but are not
  121. // a hard constraint, so ignore and continue
  122. // No-error contexts (GL_KHR_no_error) are not yet supported by macOS but
  123. // are not a hard constraint, so ignore and continue
  124. #define addAttrib(a) \
  125. { \
  126. assert((size_t) index < sizeof(attribs) / sizeof(attribs[0])); \
  127. attribs[index++] = a; \
  128. }
  129. #define setAttrib(a, v) { addAttrib(a); addAttrib(v); }
  130. NSOpenGLPixelFormatAttribute attribs[40];
  131. int index = 0;
  132. addAttrib(NSOpenGLPFAAccelerated);
  133. addAttrib(NSOpenGLPFAClosestPolicy);
  134. if (ctxconfig->nsgl.offline)
  135. {
  136. addAttrib(NSOpenGLPFAAllowOfflineRenderers);
  137. // NOTE: This replaces the NSSupportsAutomaticGraphicsSwitching key in
  138. // Info.plist for unbundled applications
  139. // HACK: This assumes that NSOpenGLPixelFormat will remain
  140. // a straightforward wrapper of its CGL counterpart
  141. addAttrib(kCGLPFASupportsAutomaticGraphicsSwitching);
  142. }
  143. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
  144. if (ctxconfig->major >= 4)
  145. {
  146. setAttrib(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core);
  147. }
  148. else
  149. #endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
  150. if (ctxconfig->major >= 3)
  151. {
  152. setAttrib(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  153. }
  154. if (ctxconfig->major <= 2)
  155. {
  156. if (fbconfig->auxBuffers != GLFW_DONT_CARE)
  157. setAttrib(NSOpenGLPFAAuxBuffers, fbconfig->auxBuffers);
  158. if (fbconfig->accumRedBits != GLFW_DONT_CARE &&
  159. fbconfig->accumGreenBits != GLFW_DONT_CARE &&
  160. fbconfig->accumBlueBits != GLFW_DONT_CARE &&
  161. fbconfig->accumAlphaBits != GLFW_DONT_CARE)
  162. {
  163. const int accumBits = fbconfig->accumRedBits +
  164. fbconfig->accumGreenBits +
  165. fbconfig->accumBlueBits +
  166. fbconfig->accumAlphaBits;
  167. setAttrib(NSOpenGLPFAAccumSize, accumBits);
  168. }
  169. }
  170. if (fbconfig->redBits != GLFW_DONT_CARE &&
  171. fbconfig->greenBits != GLFW_DONT_CARE &&
  172. fbconfig->blueBits != GLFW_DONT_CARE)
  173. {
  174. int colorBits = fbconfig->redBits +
  175. fbconfig->greenBits +
  176. fbconfig->blueBits;
  177. // macOS needs non-zero color size, so set reasonable values
  178. if (colorBits == 0)
  179. colorBits = 24;
  180. else if (colorBits < 15)
  181. colorBits = 15;
  182. setAttrib(NSOpenGLPFAColorSize, colorBits);
  183. }
  184. if (fbconfig->alphaBits != GLFW_DONT_CARE)
  185. setAttrib(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
  186. if (fbconfig->depthBits != GLFW_DONT_CARE)
  187. setAttrib(NSOpenGLPFADepthSize, fbconfig->depthBits);
  188. if (fbconfig->stencilBits != GLFW_DONT_CARE)
  189. setAttrib(NSOpenGLPFAStencilSize, fbconfig->stencilBits);
  190. if (fbconfig->stereo)
  191. {
  192. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
  193. _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
  194. "NSGL: Stereo rendering is deprecated");
  195. return false;
  196. #else
  197. addAttrib(NSOpenGLPFAStereo);
  198. #endif
  199. }
  200. if (fbconfig->doublebuffer)
  201. addAttrib(NSOpenGLPFADoubleBuffer);
  202. if (fbconfig->samples != GLFW_DONT_CARE)
  203. {
  204. if (fbconfig->samples == 0)
  205. {
  206. setAttrib(NSOpenGLPFASampleBuffers, 0);
  207. }
  208. else
  209. {
  210. setAttrib(NSOpenGLPFASampleBuffers, 1);
  211. setAttrib(NSOpenGLPFASamples, fbconfig->samples);
  212. }
  213. }
  214. // NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
  215. // framebuffer, so there's no need (and no way) to request it
  216. addAttrib(0);
  217. #undef addAttrib
  218. #undef setAttrib
  219. window->context.nsgl.pixelFormat =
  220. [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
  221. if (window->context.nsgl.pixelFormat == nil)
  222. {
  223. _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
  224. "NSGL: Failed to find a suitable pixel format");
  225. return false;
  226. }
  227. NSOpenGLContext* share = nil;
  228. if (ctxconfig->share)
  229. share = ctxconfig->share->context.nsgl.object;
  230. window->context.nsgl.object =
  231. [[NSOpenGLContext alloc] initWithFormat:window->context.nsgl.pixelFormat
  232. shareContext:share];
  233. if (window->context.nsgl.object == nil)
  234. {
  235. _glfwInputError(GLFW_VERSION_UNAVAILABLE,
  236. "NSGL: Failed to create OpenGL context");
  237. return false;
  238. }
  239. if (fbconfig->transparent)
  240. {
  241. GLint opaque = 0;
  242. [window->context.nsgl.object setValues:&opaque
  243. forParameter:NSOpenGLContextParameterSurfaceOpacity];
  244. }
  245. [window->ns.view setWantsBestResolutionOpenGLSurface:window->ns.retina];
  246. GLint interval = 0;
  247. [window->context.nsgl.object setValues:&interval
  248. forParameter:NSOpenGLContextParameterSwapInterval];
  249. [window->context.nsgl.object setView:window->ns.view];
  250. window->context.makeCurrent = makeContextCurrentNSGL;
  251. window->context.swapBuffers = swapBuffersNSGL;
  252. window->context.swapInterval = swapIntervalNSGL;
  253. window->context.extensionSupported = extensionSupportedNSGL;
  254. window->context.getProcAddress = getProcAddressNSGL;
  255. window->context.destroy = destroyContextNSGL;
  256. return true;
  257. }
  258. //////////////////////////////////////////////////////////////////////////
  259. ////// GLFW native API //////
  260. //////////////////////////////////////////////////////////////////////////
  261. GLFWAPI id glfwGetNSGLContext(GLFWwindow* handle)
  262. {
  263. _GLFWwindow* window = (_GLFWwindow*) handle;
  264. _GLFW_REQUIRE_INIT_OR_RETURN(nil);
  265. if (window->context.client == GLFW_NO_API)
  266. {
  267. _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
  268. return nil;
  269. }
  270. return window->context.nsgl.object;
  271. }