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.

294 lines
8.7KB

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