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.

240 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class Version
  16. {
  17. public:
  18. constexpr Version() = default;
  19. constexpr explicit Version (int majorIn)
  20. : Version (majorIn, 0) {}
  21. constexpr Version (int majorIn, int minorIn)
  22. : major (majorIn), minor (minorIn) {}
  23. int major = 0, minor = 0;
  24. constexpr bool operator== (const Version& other) const noexcept
  25. {
  26. return toTuple() == other.toTuple();
  27. }
  28. constexpr bool operator!= (const Version& other) const noexcept
  29. {
  30. return toTuple() != other.toTuple();
  31. }
  32. constexpr bool operator< (const Version& other) const noexcept
  33. {
  34. return toTuple() < other.toTuple();
  35. }
  36. constexpr bool operator<= (const Version& other) const noexcept
  37. {
  38. return toTuple() <= other.toTuple();
  39. }
  40. constexpr bool operator> (const Version& other) const noexcept
  41. {
  42. return toTuple() > other.toTuple();
  43. }
  44. constexpr bool operator>= (const Version& other) const noexcept
  45. {
  46. return toTuple() >= other.toTuple();
  47. }
  48. private:
  49. constexpr std::tuple<int, int> toTuple() const noexcept
  50. {
  51. return std::make_tuple (major, minor);
  52. }
  53. };
  54. template <typename Char>
  55. static auto* findNullTerminator (const Char* ptr)
  56. {
  57. while (*ptr != 0)
  58. ++ptr;
  59. return ptr;
  60. }
  61. static Version getOpenGLVersion()
  62. {
  63. const auto* versionBegin = glGetString (GL_VERSION);
  64. if (versionBegin == nullptr)
  65. return {};
  66. const auto* versionEnd = findNullTerminator (versionBegin);
  67. const std::string versionString (versionBegin, versionEnd);
  68. const auto spaceSeparated = StringArray::fromTokens (versionString.c_str(), false);
  69. for (const auto& token : spaceSeparated)
  70. {
  71. const auto pointSeparated = StringArray::fromTokens (token, ".", "");
  72. const auto major = pointSeparated[0].getIntValue();
  73. const auto minor = pointSeparated[1].getIntValue();
  74. if (major != 0)
  75. return { major, minor };
  76. }
  77. return {};
  78. }
  79. void OpenGLHelpers::resetErrorState()
  80. {
  81. while (glGetError() != GL_NO_ERROR) {}
  82. }
  83. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  84. {
  85. #if JUCE_WINDOWS
  86. return (void*) wglGetProcAddress (functionName);
  87. #elif JUCE_LINUX || JUCE_BSD
  88. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  89. #else
  90. static void* handle = dlopen (nullptr, RTLD_LAZY);
  91. return dlsym (handle, functionName);
  92. #endif
  93. }
  94. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  95. {
  96. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  97. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  98. if (getOpenGLVersion().major >= 3)
  99. {
  100. using GetStringi = const GLubyte* (*) (GLenum, GLuint);
  101. if (auto* thisGlGetStringi = reinterpret_cast<GetStringi> (getExtensionFunction ("glGetStringi")))
  102. {
  103. GLint n = 0;
  104. glGetIntegerv (GL_NUM_EXTENSIONS, &n);
  105. for (auto i = (decltype (n)) 0; i < n; ++i)
  106. if (StringRef (extensionName) == StringRef ((const char*) thisGlGetStringi (GL_EXTENSIONS, (GLuint) i)))
  107. return true;
  108. return false;
  109. }
  110. }
  111. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  112. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  113. for (;;)
  114. {
  115. const char* found = strstr (extensions, extensionName);
  116. if (found == nullptr)
  117. break;
  118. extensions = found + strlen (extensionName);
  119. if (extensions[0] == ' ' || extensions[0] == 0)
  120. return true;
  121. }
  122. return false;
  123. }
  124. void OpenGLHelpers::clear (Colour colour)
  125. {
  126. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  127. colour.getFloatBlue(), colour.getFloatAlpha());
  128. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  129. }
  130. void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
  131. {
  132. glEnable (GL_SCISSOR_TEST);
  133. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  134. }
  135. String OpenGLHelpers::getGLSLVersionString()
  136. {
  137. if (getOpenGLVersion() >= Version (3, 2))
  138. {
  139. #if JUCE_OPENGL_ES
  140. return "#version 300 es";
  141. #else
  142. return "#version 150";
  143. #endif
  144. }
  145. return "#version 110";
  146. }
  147. String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
  148. {
  149. if (getOpenGLVersion() >= Version (3, 2))
  150. {
  151. String output;
  152. #if JUCE_ANDROID
  153. {
  154. int numAttributes = 0;
  155. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  156. numAttributes++;
  157. int last = 0;
  158. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  159. {
  160. output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
  161. last = p + 10;
  162. }
  163. output += code.substring (last);
  164. }
  165. #else
  166. output = code.replace ("attribute", "in");
  167. #endif
  168. return getGLSLVersionString() + "\n" + output.replace ("varying", "out");
  169. }
  170. return code;
  171. }
  172. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  173. {
  174. if (getOpenGLVersion() >= Version (3, 2))
  175. return getGLSLVersionString() + "\n"
  176. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  177. + code.replace ("varying", "in")
  178. .replace ("texture2D", "texture")
  179. .replace ("gl_FragColor", "fragColor");
  180. return code;
  181. }
  182. } // namespace juce