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.

162 lines
6.5KB

  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. OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept
  18. : context (c), programID (0)
  19. {
  20. }
  21. OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
  22. {
  23. release();
  24. }
  25. GLuint OpenGLShaderProgram::getProgramID() const noexcept
  26. {
  27. // This method can only be used when the current thread has an active OpenGL context.
  28. jassert (OpenGLHelpers::isContextActive());
  29. if (programID == 0)
  30. programID = context.extensions.glCreateProgram();
  31. return programID;
  32. }
  33. void OpenGLShaderProgram::release() noexcept
  34. {
  35. if (programID != 0)
  36. {
  37. context.extensions.glDeleteProgram (programID);
  38. programID = 0;
  39. }
  40. }
  41. double OpenGLShaderProgram::getLanguageVersion()
  42. {
  43. return String::fromUTF8 ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  44. .retainCharacters("1234567890.").getDoubleValue();
  45. }
  46. bool OpenGLShaderProgram::addShader (const String& code, GLenum type)
  47. {
  48. GLuint shaderID = context.extensions.glCreateShader (type);
  49. const GLchar* c = code.toRawUTF8();
  50. context.extensions.glShaderSource (shaderID, 1, &c, nullptr);
  51. context.extensions.glCompileShader (shaderID);
  52. GLint status = GL_FALSE;
  53. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  54. if (status == GL_FALSE)
  55. {
  56. GLchar infoLog [16384];
  57. GLsizei infoLogLength = 0;
  58. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
  59. errorLog = String (infoLog, (size_t) infoLogLength);
  60. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  61. // Your GLSL code contained compile errors!
  62. // Hopefully this compile log should help to explain what went wrong.
  63. DBG (errorLog);
  64. jassertfalse;
  65. #endif
  66. return false;
  67. }
  68. context.extensions.glAttachShader (getProgramID(), shaderID);
  69. context.extensions.glDeleteShader (shaderID);
  70. JUCE_CHECK_OPENGL_ERROR
  71. return true;
  72. }
  73. bool OpenGLShaderProgram::addVertexShader (const String& code) { return addShader (code, GL_VERTEX_SHADER); }
  74. bool OpenGLShaderProgram::addFragmentShader (const String& code) { return addShader (code, GL_FRAGMENT_SHADER); }
  75. bool OpenGLShaderProgram::link() noexcept
  76. {
  77. // This method can only be used when the current thread has an active OpenGL context.
  78. jassert (OpenGLHelpers::isContextActive());
  79. GLuint progID = getProgramID();
  80. context.extensions.glLinkProgram (progID);
  81. GLint status = GL_FALSE;
  82. context.extensions.glGetProgramiv (progID, GL_LINK_STATUS, &status);
  83. if (status == GL_FALSE)
  84. {
  85. GLchar infoLog [16384];
  86. GLsizei infoLogLength = 0;
  87. context.extensions.glGetProgramInfoLog (progID, sizeof (infoLog), &infoLogLength, infoLog);
  88. errorLog = String (infoLog, (size_t) infoLogLength);
  89. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  90. // Your GLSL code contained link errors!
  91. // Hopefully this compile log should help to explain what went wrong.
  92. DBG (errorLog);
  93. jassertfalse;
  94. #endif
  95. }
  96. JUCE_CHECK_OPENGL_ERROR
  97. return status != GL_FALSE;
  98. }
  99. void OpenGLShaderProgram::use() const noexcept
  100. {
  101. context.extensions.glUseProgram (programID);
  102. }
  103. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  104. : uniformID (program.context.extensions.glGetUniformLocation (program.getProgramID(), name)), context (program.context)
  105. {
  106. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  107. jassert (uniformID >= 0);
  108. #endif
  109. }
  110. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  111. : attributeID ((GLuint) program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
  112. {
  113. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  114. jassert ((GLint) attributeID >= 0);
  115. #endif
  116. }
  117. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  118. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  119. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  120. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  121. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  122. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  123. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  124. void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
  125. void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
  126. void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }