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.

128 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. void OpenGLHelpers::resetErrorState()
  16. {
  17. while (glGetError() != GL_NO_ERROR) {}
  18. }
  19. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  20. {
  21. #if JUCE_WINDOWS
  22. return (void*) wglGetProcAddress (functionName);
  23. #elif JUCE_LINUX
  24. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  25. #else
  26. static void* handle = dlopen (nullptr, RTLD_LAZY);
  27. return dlsym (handle, functionName);
  28. #endif
  29. }
  30. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  31. {
  32. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  33. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  34. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  35. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  36. for (;;)
  37. {
  38. const char* found = strstr (extensions, extensionName);
  39. if (found == nullptr)
  40. break;
  41. extensions = found + strlen (extensionName);
  42. if (extensions[0] == ' ' || extensions[0] == 0)
  43. return true;
  44. }
  45. return false;
  46. }
  47. void OpenGLHelpers::clear (Colour colour)
  48. {
  49. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  50. colour.getFloatBlue(), colour.getFloatAlpha());
  51. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  52. }
  53. void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
  54. {
  55. glEnable (GL_SCISSOR_TEST);
  56. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  57. }
  58. String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
  59. {
  60. #if JUCE_OPENGL3
  61. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  62. {
  63. String output;
  64. #if JUCE_ANDROID
  65. {
  66. int numAttributes = 0;
  67. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  68. numAttributes++;
  69. int last = 0;
  70. for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
  71. {
  72. output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
  73. last = p + 10;
  74. }
  75. output += code.substring (last);
  76. }
  77. #else
  78. output = code.replace ("attribute", "in");
  79. #endif
  80. return JUCE_GLSL_VERSION "\n" + output.replace ("varying", "out");
  81. }
  82. #endif
  83. return code;
  84. }
  85. String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
  86. {
  87. #if JUCE_OPENGL3
  88. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  89. return JUCE_GLSL_VERSION "\n"
  90. "out " JUCE_MEDIUMP " vec4 fragColor;\n"
  91. + code.replace ("varying", "in")
  92. .replace ("texture2D", "texture")
  93. .replace ("gl_FragColor", "fragColor");
  94. #endif
  95. return code;
  96. }
  97. } // namespace juce