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.

129 lines
3.9KB

  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. void OpenGLHelpers::resetErrorState()
  18. {
  19. while (glGetError() != GL_NO_ERROR) {}
  20. }
  21. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  22. {
  23. #if JUCE_WINDOWS
  24. return (void*) wglGetProcAddress (functionName);
  25. #elif JUCE_LINUX
  26. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  27. #else
  28. static void* handle = dlopen (nullptr, RTLD_LAZY);
  29. return dlsym (handle, functionName);
  30. #endif
  31. }
  32. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  33. {
  34. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  35. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  36. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  37. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  38. for (;;)
  39. {
  40. const char* found = strstr (extensions, extensionName);
  41. if (found == nullptr)
  42. break;
  43. extensions = found + strlen (extensionName);
  44. if (extensions[0] == ' ' || extensions[0] == 0)
  45. return true;
  46. }
  47. return false;
  48. }
  49. void OpenGLHelpers::clear (Colour colour)
  50. {
  51. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  52. colour.getFloatBlue(), colour.getFloatAlpha());
  53. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  54. }
  55. void OpenGLHelpers::enableScissorTest (const Rectangle<int>& clip)
  56. {
  57. glEnable (GL_SCISSOR_TEST);
  58. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  59. }
  60. String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
  61. {
  62. #if JUCE_OPENGL3
  63. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  64. {
  65. String output;
  66. #if JUCE_ANDROID
  67. {
  68. int numAttributes = 0;
  69. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  70. numAttributes++;
  71. int last = 0;
  72. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  73. {
  74. output += code.substring (last, p) + String ("layout(location=") + String (--numAttributes) + ") in ";
  75. last = p + 10;
  76. }
  77. output += code.substring (last);
  78. }
  79. #else
  80. output = code.replace ("attribute", "in");
  81. #endif
  82. return JUCE_GLSL_VERSION "\n" + output.replace ("varying", "out");
  83. }
  84. #endif
  85. return code;
  86. }
  87. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  88. {
  89. #if JUCE_OPENGL3
  90. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  91. return JUCE_GLSL_VERSION "\n"
  92. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  93. + code.replace ("varying", "in")
  94. .replace ("texture2D", "texture")
  95. .replace ("gl_FragColor", "fragColor");
  96. #endif
  97. return code;
  98. }