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.

209 lines
6.7KB

  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. #define JUCE_INIT_GL_FUNCTION_EXT(name, returnType, params, callparams) \
  64. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name); \
  65. if (name == nullptr) \
  66. name = (type_ ## name) OpenGLHelpers::getExtensionFunction (JUCE_STRINGIFY (name ## EXT));
  67. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_INIT_GL_FUNCTION, JUCE_INIT_GL_FUNCTION_EXT)
  68. #undef JUCE_INIT_GL_FUNCTION
  69. #undef JUCE_INIT_GL_FUNCTION_EXT
  70. #endif
  71. }
  72. #if JUCE_OPENGL_ES
  73. #define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) \
  74. returnType OpenGLExtensionFunctions::name params { return ::name callparams; }
  75. JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
  76. #undef JUCE_DECLARE_GL_FUNCTION
  77. #endif
  78. #undef JUCE_GL_EXTENSION_FUNCTIONS
  79. #if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
  80. static const char* getGLErrorMessage (const GLenum e)
  81. {
  82. switch (e)
  83. {
  84. case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
  85. case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
  86. case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
  87. case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
  88. #ifdef GL_STACK_OVERFLOW
  89. case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
  90. #endif
  91. #ifdef GL_STACK_UNDERFLOW
  92. case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
  93. #endif
  94. #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
  95. case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
  96. #endif
  97. default: break;
  98. }
  99. return "Unknown error";
  100. }
  101. static void checkGLError (const char* file, const int line)
  102. {
  103. for (;;)
  104. {
  105. const GLenum e = glGetError();
  106. if (e == GL_NO_ERROR)
  107. break;
  108. DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
  109. jassertfalse;
  110. }
  111. }
  112. #define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
  113. #else
  114. #define JUCE_CHECK_OPENGL_ERROR ;
  115. #endif
  116. static void clearGLError() noexcept
  117. {
  118. while (glGetError() != GL_NO_ERROR) {}
  119. }
  120. struct OpenGLTargetSaver
  121. {
  122. OpenGLTargetSaver (const OpenGLContext& c) noexcept
  123. : context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
  124. {
  125. glGetIntegerv (GL_VIEWPORT, oldViewport);
  126. }
  127. ~OpenGLTargetSaver() noexcept
  128. {
  129. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
  130. glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  131. }
  132. private:
  133. const OpenGLContext& context;
  134. GLuint oldFramebuffer;
  135. GLint oldViewport[4];
  136. OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
  137. };
  138. //==============================================================================
  139. #include "opengl/juce_OpenGLFrameBuffer.cpp"
  140. #include "opengl/juce_OpenGLGraphicsContext.cpp"
  141. #include "opengl/juce_OpenGLHelpers.cpp"
  142. #include "opengl/juce_OpenGLImage.cpp"
  143. #include "opengl/juce_OpenGLPixelFormat.cpp"
  144. #include "opengl/juce_OpenGLShaderProgram.cpp"
  145. #include "opengl/juce_OpenGLTexture.cpp"
  146. //==============================================================================
  147. #if JUCE_MAC || JUCE_IOS
  148. #include "../juce_core/native/juce_osx_ObjCHelpers.h"
  149. #include "../juce_graphics/native/juce_mac_CoreGraphicsHelpers.h"
  150. #if JUCE_MAC
  151. #include "native/juce_OpenGL_osx.h"
  152. #else
  153. #include "native/juce_OpenGL_ios.h"
  154. #endif
  155. #elif JUCE_WINDOWS
  156. #include "native/juce_OpenGL_win32.h"
  157. #elif JUCE_LINUX
  158. #include "native/juce_OpenGL_linux.h"
  159. #elif JUCE_ANDROID
  160. #include "../juce_core/native/juce_android_JNIHelpers.h"
  161. #include "native/juce_OpenGL_android.h"
  162. #endif
  163. #include "opengl/juce_OpenGLContext.cpp"
  164. #include "utils/juce_OpenGLAppComponent.cpp"
  165. }