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.

131 lines
3.9KB

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