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.

136 lines
4.0KB

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