context_gl_osx.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* context_gl_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "context_gl_osx.h"
  31. #if defined(OPENGL_ENABLED) || defined(GLES_ENABLED)
  32. void ContextGL_OSX::release_current() {
  33. [NSOpenGLContext clearCurrentContext];
  34. }
  35. void ContextGL_OSX::make_current() {
  36. [context makeCurrentContext];
  37. }
  38. void ContextGL_OSX::update() {
  39. [context update];
  40. }
  41. void ContextGL_OSX::set_opacity(GLint p_opacity) {
  42. [context setValues:&p_opacity forParameter:NSOpenGLCPSurfaceOpacity];
  43. }
  44. int ContextGL_OSX::get_window_width() {
  45. return OS::get_singleton()->get_video_mode().width;
  46. }
  47. int ContextGL_OSX::get_window_height() {
  48. return OS::get_singleton()->get_video_mode().height;
  49. }
  50. void ContextGL_OSX::swap_buffers() {
  51. [context flushBuffer];
  52. }
  53. void ContextGL_OSX::set_use_vsync(bool p_use) {
  54. CGLContextObj ctx = CGLGetCurrentContext();
  55. if (ctx) {
  56. GLint swapInterval = p_use ? 1 : 0;
  57. CGLSetParameter(ctx, kCGLCPSwapInterval, &swapInterval);
  58. use_vsync = p_use;
  59. }
  60. }
  61. bool ContextGL_OSX::is_using_vsync() const {
  62. return use_vsync;
  63. }
  64. Error ContextGL_OSX::initialize() {
  65. framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
  66. ERR_FAIL_COND_V(!framework, ERR_CANT_CREATE);
  67. unsigned int attributeCount = 0;
  68. // OS X needs non-zero color size, so set reasonable values
  69. int colorBits = 32;
  70. // Fail if a robustness strategy was requested
  71. #define ADD_ATTR(x) \
  72. { attributes[attributeCount++] = x; }
  73. #define ADD_ATTR2(x, y) \
  74. { \
  75. ADD_ATTR(x); \
  76. ADD_ATTR(y); \
  77. }
  78. // Arbitrary array size here
  79. NSOpenGLPixelFormatAttribute attributes[40];
  80. ADD_ATTR(NSOpenGLPFADoubleBuffer);
  81. ADD_ATTR(NSOpenGLPFAClosestPolicy);
  82. if (!opengl_3_context) {
  83. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy);
  84. } else {
  85. //we now need OpenGL 3 or better, maybe even change this to 3_3Core ?
  86. ADD_ATTR2(NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core);
  87. }
  88. ADD_ATTR2(NSOpenGLPFAColorSize, colorBits);
  89. /*
  90. if (fbconfig->alphaBits > 0)
  91. ADD_ATTR2(NSOpenGLPFAAlphaSize, fbconfig->alphaBits);
  92. */
  93. ADD_ATTR2(NSOpenGLPFADepthSize, 24);
  94. ADD_ATTR2(NSOpenGLPFAStencilSize, 8);
  95. /*
  96. if (fbconfig->stereo)
  97. ADD_ATTR(NSOpenGLPFAStereo);
  98. */
  99. /*
  100. if (fbconfig->samples > 0) {
  101. ADD_ATTR2(NSOpenGLPFASampleBuffers, 1);
  102. ADD_ATTR2(NSOpenGLPFASamples, fbconfig->samples);
  103. }
  104. */
  105. // NOTE: All NSOpenGLPixelFormats on the relevant cards support sRGB
  106. // framebuffer, so there's no need (and no way) to request it
  107. ADD_ATTR(0);
  108. #undef ADD_ATTR
  109. #undef ADD_ATTR2
  110. pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  111. ERR_FAIL_COND_V(pixelFormat == nil, ERR_CANT_CREATE);
  112. context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
  113. ERR_FAIL_COND_V(context == nil, ERR_CANT_CREATE);
  114. [context setView:window_view];
  115. [context makeCurrentContext];
  116. return OK;
  117. }
  118. ContextGL_OSX::ContextGL_OSX(id p_view, bool p_opengl_3_context) {
  119. opengl_3_context = p_opengl_3_context;
  120. window_view = p_view;
  121. use_vsync = false;
  122. }
  123. ContextGL_OSX::~ContextGL_OSX() {}
  124. #endif