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.

135 lines
4.0KB

  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
  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 JUCE_OPENGL3
  66. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  67. {
  68. String output;
  69. #if JUCE_ANDROID
  70. {
  71. int numAttributes = 0;
  72. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  73. numAttributes++;
  74. int last = 0;
  75. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  76. {
  77. output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
  78. last = p + 10;
  79. }
  80. output += code.substring (last);
  81. }
  82. #else
  83. output = code.replace ("attribute", "in");
  84. #endif
  85. return JUCE_GLSL_VERSION "\n" + output.replace ("varying", "out");
  86. }
  87. #endif
  88. return code;
  89. }
  90. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  91. {
  92. #if JUCE_OPENGL3
  93. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  94. return JUCE_GLSL_VERSION "\n"
  95. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  96. + code.replace ("varying", "in")
  97. .replace ("texture2D", "texture")
  98. .replace ("gl_FragColor", "fragColor");
  99. #endif
  100. return code;
  101. }
  102. } // namespace juce