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) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. void OpenGLHelpers::resetErrorState()
  20. {
  21. while (glGetError() != GL_NO_ERROR) {}
  22. }
  23. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  24. {
  25. #if JUCE_WINDOWS
  26. return (void*) wglGetProcAddress (functionName);
  27. #elif JUCE_LINUX
  28. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  29. #else
  30. static void* handle = dlopen (nullptr, RTLD_LAZY);
  31. return dlsym (handle, functionName);
  32. #endif
  33. }
  34. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  35. {
  36. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  37. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  38. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  39. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  40. for (;;)
  41. {
  42. const char* found = strstr (extensions, extensionName);
  43. if (found == nullptr)
  44. break;
  45. extensions = found + strlen (extensionName);
  46. if (extensions[0] == ' ' || extensions[0] == 0)
  47. return true;
  48. }
  49. return false;
  50. }
  51. void OpenGLHelpers::clear (Colour colour)
  52. {
  53. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  54. colour.getFloatBlue(), colour.getFloatAlpha());
  55. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  56. }
  57. void OpenGLHelpers::enableScissorTest (const Rectangle<int>& clip)
  58. {
  59. glEnable (GL_SCISSOR_TEST);
  60. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  61. }
  62. String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
  63. {
  64. #if JUCE_OPENGL3
  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) + String ("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. #endif
  87. return code;
  88. }
  89. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  90. {
  91. #if JUCE_OPENGL3
  92. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  93. return JUCE_GLSL_VERSION "\n"
  94. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  95. + code.replace ("varying", "in")
  96. .replace ("texture2D", "texture")
  97. .replace ("gl_FragColor", "fragColor");
  98. #endif
  99. return code;
  100. }