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.

142 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_USE_OPENGL_SHADERS
  18. OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept
  19. : context (c)
  20. {
  21. // This object can only be created and used when the current thread has an active OpenGL context.
  22. jassert (OpenGLHelpers::isContextActive());
  23. programID = context.extensions.glCreateProgram();
  24. }
  25. OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
  26. {
  27. context.extensions.glDeleteProgram (programID);
  28. }
  29. double OpenGLShaderProgram::getLanguageVersion()
  30. {
  31. #if JUCE_OPENGL_ES
  32. // GLES doesn't support this version number, but that shouldn't matter since
  33. // on GLES you probably won't need to check it.
  34. jassertfalse;
  35. return 0;
  36. #else
  37. return String ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  38. .upToFirstOccurrenceOf (" ", false, false).getDoubleValue();
  39. #endif
  40. }
  41. bool OpenGLShaderProgram::addShader (const char* const code, GLenum type)
  42. {
  43. GLuint shaderID = context.extensions.glCreateShader (type);
  44. context.extensions.glShaderSource (shaderID, 1, (const GLchar**) &code, nullptr);
  45. context.extensions.glCompileShader (shaderID);
  46. GLint status = GL_FALSE;
  47. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  48. if (status == GL_FALSE)
  49. {
  50. GLchar infoLog [16384];
  51. GLsizei infoLogLength = 0;
  52. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
  53. errorLog = String (infoLog, (size_t) infoLogLength);
  54. #if JUCE_DEBUG
  55. // Your GLSL code contained compile errors!
  56. // Hopefully this compile log should help to explain what went wrong.
  57. DBG (errorLog);
  58. jassertfalse;
  59. #endif
  60. return false;
  61. }
  62. context.extensions.glAttachShader (programID, shaderID);
  63. context.extensions.glDeleteShader (shaderID);
  64. JUCE_CHECK_OPENGL_ERROR
  65. return true;
  66. }
  67. bool OpenGLShaderProgram::link() noexcept
  68. {
  69. context.extensions.glLinkProgram (programID);
  70. GLint status = GL_FALSE;
  71. context.extensions.glGetProgramiv (programID, GL_LINK_STATUS, &status);
  72. if (status == GL_FALSE)
  73. {
  74. GLchar infoLog [16384];
  75. GLsizei infoLogLength = 0;
  76. context.extensions.glGetProgramInfoLog (programID, sizeof (infoLog), &infoLogLength, infoLog);
  77. errorLog = String (infoLog, (size_t) infoLogLength);
  78. #if JUCE_DEBUG
  79. // Your GLSL code contained link errors!
  80. // Hopefully this compile log should help to explain what went wrong.
  81. DBG (errorLog);
  82. jassertfalse;
  83. #endif
  84. }
  85. JUCE_CHECK_OPENGL_ERROR
  86. return status != GL_FALSE;
  87. }
  88. void OpenGLShaderProgram::use() const noexcept
  89. {
  90. context.extensions.glUseProgram (programID);
  91. }
  92. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  93. : uniformID (program.context.extensions.glGetUniformLocation (program.programID, name)), context (program.context)
  94. {
  95. jassert (uniformID >= 0);
  96. }
  97. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  98. : attributeID (program.context.extensions.glGetAttribLocation (program.programID, name))
  99. {
  100. jassert (attributeID >= 0);
  101. }
  102. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  103. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  104. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  105. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  106. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  107. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  108. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  109. void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
  110. void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
  111. void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }
  112. #endif