The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #ifdef JUCE_OPENGL_H_INCLUDED
  14. /* When you add this cpp file to your project, you mustn't include it in a file where you've
  15. already included any other headers - just put it inside a file on its own, possibly with your config
  16. flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
  17. header files that the compiler may be using.
  18. */
  19. #error "Incorrect use of JUCE cpp file"
  20. #endif
  21. #define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
  22. #define JUCE_CORE_INCLUDE_JNI_HELPERS 1
  23. #define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
  24. #define JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS 1
  25. #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
  26. #define JUCE_GUI_BASICS_INCLUDE_SCOPED_THREAD_DPI_AWARENESS_SETTER 1
  27. #include "juce_opengl.h"
  28. #define JUCE_STATIC_LINK_GL_VERSION_1_0 1
  29. #define JUCE_STATIC_LINK_GL_VERSION_1_1 1
  30. #if JUCE_MAC
  31. #define JUCE_STATIC_LINK_GL_VERSION_1_2 1
  32. #define JUCE_STATIC_LINK_GL_VERSION_1_3 1
  33. #define JUCE_STATIC_LINK_GL_VERSION_1_4 1
  34. #define JUCE_STATIC_LINK_GL_VERSION_1_5 1
  35. #define JUCE_STATIC_LINK_GL_VERSION_2_0 1
  36. #define JUCE_STATIC_LINK_GL_VERSION_2_1 1
  37. #define JUCE_STATIC_LINK_GL_VERSION_3_0 1
  38. #define JUCE_STATIC_LINK_GL_VERSION_3_1 1
  39. #define JUCE_STATIC_LINK_GL_VERSION_3_2 1
  40. #endif
  41. #define JUCE_STATIC_LINK_GL_ES_VERSION_2_0 1
  42. #if !JUCE_ANDROID || JUCE_ANDROID_GL_ES_VERSION_3_0
  43. #define JUCE_STATIC_LINK_GL_ES_VERSION_3_0 1
  44. #endif
  45. #if JUCE_OPENGL_ES
  46. #include "opengl/juce_gles2.cpp"
  47. #else
  48. #include "opengl/juce_gl.cpp"
  49. #endif
  50. //==============================================================================
  51. #if JUCE_IOS
  52. #import <QuartzCore/QuartzCore.h>
  53. //==============================================================================
  54. #elif JUCE_WINDOWS
  55. #include <windowsx.h>
  56. #if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  57. #pragma comment(lib, "OpenGL32.Lib")
  58. #endif
  59. //==============================================================================
  60. #elif JUCE_LINUX || JUCE_BSD
  61. /* Got an include error here?
  62. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  63. and "freeglut3-dev".
  64. */
  65. #include <GL/glx.h>
  66. //==============================================================================
  67. #elif JUCE_MAC
  68. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  69. #include <OpenGL/OpenGL.h>
  70. //==============================================================================
  71. #elif JUCE_ANDROID
  72. #include <android/native_window.h>
  73. #include <android/native_window_jni.h>
  74. #include <EGL/egl.h>
  75. #endif
  76. //==============================================================================
  77. namespace juce
  78. {
  79. using namespace ::juce::gl;
  80. void OpenGLExtensionFunctions::initialise()
  81. {
  82. gl::loadFunctions();
  83. }
  84. #define X(name) decltype (::juce::gl::name)& OpenGLExtensionFunctions::name = ::juce::gl::name;
  85. JUCE_GL_BASE_FUNCTIONS
  86. JUCE_GL_EXTENSION_FUNCTIONS
  87. JUCE_GL_VERTEXBUFFER_FUNCTIONS
  88. #undef X
  89. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  90. static const char* getGLErrorMessage (const GLenum e) noexcept
  91. {
  92. switch (e)
  93. {
  94. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  95. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  96. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  97. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  98. #ifdef GL_STACK_OVERFLOW
  99. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  100. #endif
  101. #ifdef GL_STACK_UNDERFLOW
  102. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  103. #endif
  104. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  105. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  106. #endif
  107. default: break;
  108. }
  109. return "Unknown error";
  110. }
  111. #if JUCE_MAC || JUCE_IOS
  112. #ifndef JUCE_IOS_MAC_VIEW
  113. #if JUCE_IOS
  114. #define JUCE_IOS_MAC_VIEW UIView
  115. #define JUCE_IOS_MAC_WINDOW UIWindow
  116. #else
  117. #define JUCE_IOS_MAC_VIEW NSView
  118. #define JUCE_IOS_MAC_WINDOW NSWindow
  119. #endif
  120. #endif
  121. #endif
  122. static bool checkPeerIsValid (OpenGLContext* context)
  123. {
  124. jassert (context != nullptr);
  125. if (context != nullptr)
  126. {
  127. if (auto* comp = context->getTargetComponent())
  128. {
  129. if (auto* peer = comp->getPeer())
  130. {
  131. #if JUCE_MAC || JUCE_IOS
  132. if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
  133. {
  134. if (auto nsWindow = [nsView window])
  135. {
  136. #if JUCE_MAC
  137. return ([nsWindow isVisible]
  138. && (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
  139. #else
  140. ignoreUnused (nsWindow);
  141. return true;
  142. #endif
  143. }
  144. }
  145. #else
  146. ignoreUnused (peer);
  147. return true;
  148. #endif
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. static void checkGLError (const char* file, const int line)
  155. {
  156. for (;;)
  157. {
  158. const GLenum e = glGetError();
  159. if (e == GL_NO_ERROR)
  160. break;
  161. // if the peer is not valid then ignore errors
  162. if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
  163. continue;
  164. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  165. jassertfalse;
  166. }
  167. }
  168. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  169. #else
  170. #define JUCE_CHECK_OPENGL_ERROR ;
  171. #endif
  172. static void clearGLError() noexcept
  173. {
  174. while (glGetError() != GL_NO_ERROR) {}
  175. }
  176. struct OpenGLTargetSaver
  177. {
  178. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  179. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  180. {
  181. glGetIntegerv (GL_VIEWPORT, oldViewport);
  182. }
  183. ~OpenGLTargetSaver() noexcept
  184. {
  185. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  186. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  187. }
  188. private:
  189. const OpenGLContext& context;
  190. GLuint oldFramebuffer;
  191. GLint oldViewport[4];
  192. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  193. };
  194. } // namespace juce
  195. //==============================================================================
  196. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  197. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  198. #include "opengl/juce_OpenGLHelpers.cpp"
  199. #include "opengl/juce_OpenGLImage.cpp"
  200. #include "opengl/juce_OpenGLPixelFormat.cpp"
  201. #include "opengl/juce_OpenGLShaderProgram.cpp"
  202. #include "opengl/juce_OpenGLTexture.cpp"
  203. //==============================================================================
  204. #if JUCE_MAC || JUCE_IOS
  205. #if JUCE_MAC
  206. #include "native/juce_OpenGL_osx.h"
  207. #else
  208. #include "native/juce_OpenGL_ios.h"
  209. #endif
  210. #elif JUCE_WINDOWS
  211. #include "opengl/juce_wgl.h"
  212. #include "native/juce_OpenGL_win32.h"
  213. #define JUCE_IMPL_WGL_EXTENSION_FUNCTION(name) \
  214. decltype (juce::OpenGLContext::NativeContext::name) juce::OpenGLContext::NativeContext::name = nullptr;
  215. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB)
  216. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT)
  217. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT)
  218. JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB)
  219. #undef JUCE_IMPL_WGL_EXTENSION_FUNCTION
  220. #elif JUCE_LINUX || JUCE_BSD
  221. #include "native/juce_OpenGL_linux_X11.h"
  222. #elif JUCE_ANDROID
  223. #include "native/juce_OpenGL_android.h"
  224. #endif
  225. #include "opengl/juce_OpenGLContext.cpp"
  226. #include "utils/juce_OpenGLAppComponent.cpp"