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.

221 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifdef JUCE_OPENGL_H_INCLUDED
  18. /* When you add this cpp file to your project, you mustn't include it in a file where you've
  19. already included any other headers - just put it inside a file on its own, possibly with your config
  20. flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
  21. header files that the compiler may be using.
  22. */
  23. #error "Incorrect use of JUCE cpp file"
  24. #endif
  25. #include "../juce_core/native/juce_BasicNativeHeaders.h"
  26. #include "juce_opengl.h"
  27. //==============================================================================
  28. #if JUCE_IOS
  29. #import <QuartzCore/QuartzCore.h>
  30. //==============================================================================
  31. #elif JUCE_WINDOWS
  32. #include <windowsx.h>
  33. #if JUCE_MSVC && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
  34. #pragma comment(lib, "OpenGL32.Lib")
  35. #endif
  36. //==============================================================================
  37. #elif JUCE_LINUX
  38. /* Got an include error here?
  39. If you want to install OpenGL support, the packages to get are "mesa-common-dev"
  40. and "freeglut3-dev".
  41. */
  42. #include <GL/glx.h>
  43. //==============================================================================
  44. #elif JUCE_MAC
  45. #include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
  46. #include <OpenGL/OpenGL.h>
  47. //==============================================================================
  48. #elif JUCE_ANDROID
  49. #ifndef GL_GLEXT_PROTOTYPES
  50. #define GL_GLEXT_PROTOTYPES 1
  51. #endif
  52. #include <GLES2/gl2.h>
  53. #endif
  54. namespace juce
  55. {
  56. //==============================================================================
  57. #include "native/juce_OpenGLExtensions.h"
  58. void OpenGLExtensionFunctions::initialise()
  59. {
  60. #if JUCE_WINDOWS || JUCE_LINUX
  61. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  62. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  63. JUCE_GL_BASE_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  64. #undef JUCE_INIT_GL_FUNCTION
  65. #define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
  66. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name); \
  67. if (name == nullptr) \
  68. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (JUCE_STRINGIFY (name ## EXT));
  69. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  70. #if JUCE_OPENGL3
  71. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_INIT_GL_FUNCTION)
  72. #endif
  73. #undef JUCE_INIT_GL_FUNCTION
  74. #endif
  75. }
  76. #if JUCE_OPENGL_ES
  77. #define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) \
  78. returnType OpenGLExtensionFunctions::name params noexcept { return ::name callparams; }
  79. JUCE_GL_BASE_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  80. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  81. #if JUCE_OPENGL3
  82. JUCE_GL_VERTEXBUFFER_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION)
  83. #endif
  84. #undef JUCE_DECLARE_GL_FUNCTION
  85. #endif
  86. #undef JUCE_GL_EXTENSION_FUNCTIONS
  87. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  88. static const char* getGLErrorMessage (const GLenum e) noexcept
  89. {
  90. switch (e)
  91. {
  92. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  93. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  94. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  95. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  96. #ifdef GL_STACK_OVERFLOW
  97. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  98. #endif
  99. #ifdef GL_STACK_UNDERFLOW
  100. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  101. #endif
  102. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  103. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  104. #endif
  105. default: break;
  106. }
  107. return "Unknown error";
  108. }
  109. static void checkGLError (const char* file, const int line)
  110. {
  111. for (;;)
  112. {
  113. const GLenum e = glGetError();
  114. if (e == GL_NO_ERROR)
  115. break;
  116. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  117. jassertfalse;
  118. }
  119. }
  120. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  121. #else
  122. #define JUCE_CHECK_OPENGL_ERROR ;
  123. #endif
  124. static void clearGLError() noexcept
  125. {
  126. while (glGetError() != GL_NO_ERROR) {}
  127. }
  128. struct OpenGLTargetSaver
  129. {
  130. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  131. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  132. {
  133. glGetIntegerv (GL_VIEWPORT, oldViewport);
  134. }
  135. ~OpenGLTargetSaver() noexcept
  136. {
  137. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  138. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  139. }
  140. private:
  141. const OpenGLContext& context;
  142. GLuint oldFramebuffer;
  143. GLint oldViewport[4];
  144. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  145. };
  146. //==============================================================================
  147. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  148. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  149. #include "opengl/juce_OpenGLHelpers.cpp"
  150. #include "opengl/juce_OpenGLImage.cpp"
  151. #include "opengl/juce_OpenGLPixelFormat.cpp"
  152. #include "opengl/juce_OpenGLShaderProgram.cpp"
  153. #include "opengl/juce_OpenGLTexture.cpp"
  154. //==============================================================================
  155. #if JUCE_MAC || JUCE_IOS
  156. #include "../juce_core/native/juce_osx_ObjCHelpers.h"
  157. #include "../juce_graphics/native/juce_mac_CoreGraphicsHelpers.h"
  158. #if JUCE_MAC
  159. #include "native/juce_OpenGL_osx.h"
  160. #else
  161. #include "native/juce_OpenGL_ios.h"
  162. #endif
  163. #elif JUCE_WINDOWS
  164. #include "native/juce_OpenGL_win32.h"
  165. #elif JUCE_LINUX
  166. #include "native/juce_OpenGL_linux.h"
  167. #elif JUCE_ANDROID
  168. #include "../juce_core/native/juce_android_JNIHelpers.h"
  169. #include "native/juce_OpenGL_android.h"
  170. #endif
  171. #include "opengl/juce_OpenGLContext.cpp"
  172. #include "utils/juce_OpenGLAppComponent.cpp"
  173. }