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.

227 lines
6.1KB

  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. namespace juce
  19. {
  20. class Version
  21. {
  22. public:
  23. constexpr Version() = default;
  24. constexpr explicit Version (int majorIn)
  25. : Version (majorIn, 0) {}
  26. constexpr Version (int majorIn, int minorIn)
  27. : major (majorIn), minor (minorIn) {}
  28. int major = 0, minor = 0;
  29. constexpr bool operator== (const Version& other) const noexcept
  30. {
  31. return toTuple() == other.toTuple();
  32. }
  33. constexpr bool operator!= (const Version& other) const noexcept
  34. {
  35. return toTuple() != other.toTuple();
  36. }
  37. constexpr bool operator< (const Version& other) const noexcept
  38. {
  39. return toTuple() < other.toTuple();
  40. }
  41. constexpr bool operator<= (const Version& other) const noexcept
  42. {
  43. return toTuple() <= other.toTuple();
  44. }
  45. constexpr bool operator> (const Version& other) const noexcept
  46. {
  47. return toTuple() > other.toTuple();
  48. }
  49. constexpr bool operator>= (const Version& other) const noexcept
  50. {
  51. return toTuple() >= other.toTuple();
  52. }
  53. private:
  54. constexpr std::tuple<int, int> toTuple() const noexcept
  55. {
  56. return std::make_tuple (major, minor);
  57. }
  58. };
  59. template <typename Char>
  60. static auto* findNullTerminator (const Char* ptr)
  61. {
  62. while (*ptr != 0)
  63. ++ptr;
  64. return ptr;
  65. }
  66. static Version getOpenGLVersion()
  67. {
  68. const auto* versionBegin = glGetString (GL_VERSION);
  69. if (versionBegin == nullptr)
  70. return {};
  71. const auto* versionEnd = findNullTerminator (versionBegin);
  72. const std::string versionString (versionBegin, versionEnd);
  73. const auto spaceSeparated = StringArray::fromTokens (versionString.c_str(), false);
  74. if (spaceSeparated.isEmpty())
  75. return {};
  76. const auto pointSeparated = StringArray::fromTokens (spaceSeparated[0], ".", "");
  77. const auto major = pointSeparated[0].getIntValue();
  78. const auto minor = pointSeparated[1].getIntValue();
  79. return { major, minor };
  80. }
  81. void OpenGLHelpers::resetErrorState()
  82. {
  83. while (glGetError() != GL_NO_ERROR) {}
  84. }
  85. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  86. {
  87. #if JUCE_WINDOWS
  88. return (void*) wglGetProcAddress (functionName);
  89. #elif JUCE_LINUX || JUCE_BSD
  90. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  91. #else
  92. static void* handle = dlopen (nullptr, RTLD_LAZY);
  93. return dlsym (handle, functionName);
  94. #endif
  95. }
  96. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  97. {
  98. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  99. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  100. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  101. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  102. for (;;)
  103. {
  104. const char* found = strstr (extensions, extensionName);
  105. if (found == nullptr)
  106. break;
  107. extensions = found + strlen (extensionName);
  108. if (extensions[0] == ' ' || extensions[0] == 0)
  109. return true;
  110. }
  111. return false;
  112. }
  113. void OpenGLHelpers::clear (Colour colour)
  114. {
  115. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  116. colour.getFloatBlue(), colour.getFloatAlpha());
  117. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  118. }
  119. void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
  120. {
  121. glEnable (GL_SCISSOR_TEST);
  122. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  123. }
  124. String OpenGLHelpers::getGLSLVersionString()
  125. {
  126. if (getOpenGLVersion() >= Version (3, 2))
  127. {
  128. #if JUCE_OPENGL_ES
  129. return "#version 300 es";
  130. #else
  131. return "#version 150";
  132. #endif
  133. }
  134. return "#version 110";
  135. }
  136. String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
  137. {
  138. if (getOpenGLVersion() >= Version (3, 2))
  139. {
  140. String output;
  141. #if JUCE_ANDROID
  142. {
  143. int numAttributes = 0;
  144. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  145. numAttributes++;
  146. int last = 0;
  147. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  148. {
  149. output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
  150. last = p + 10;
  151. }
  152. output += code.substring (last);
  153. }
  154. #else
  155. output = code.replace ("attribute", "in");
  156. #endif
  157. return getGLSLVersionString() + "\n" + output.replace ("varying", "out");
  158. }
  159. return code;
  160. }
  161. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  162. {
  163. if (getOpenGLVersion() >= Version (3, 2))
  164. return getGLSLVersionString() + "\n"
  165. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  166. + code.replace ("varying", "in")
  167. .replace ("texture2D", "texture")
  168. .replace ("gl_FragColor", "fragColor");
  169. return code;
  170. }
  171. } // namespace juce