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.

286 lines
8.7KB

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