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.

165 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. #if JUCE_OPENGL_ES
  44. // GLES doesn't support this version number, but that shouldn't matter since
  45. // on GLES you probably won't need to check it.
  46. jassertfalse;
  47. return 0;
  48. #else
  49. return String ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  50. .upToFirstOccurrenceOf (" ", false, false).getDoubleValue();
  51. #endif
  52. }
  53. bool OpenGLShaderProgram::addShader (const String& code, GLenum type)
  54. {
  55. GLuint shaderID = context.extensions.glCreateShader (type);
  56. const GLchar* c = code.toRawUTF8();
  57. context.extensions.glShaderSource (shaderID, 1, &c, nullptr);
  58. context.extensions.glCompileShader (shaderID);
  59. GLint status = GL_FALSE;
  60. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  61. if (status == GL_FALSE)
  62. {
  63. GLchar infoLog [16384];
  64. GLsizei infoLogLength = 0;
  65. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
  66. errorLog = String (infoLog, (size_t) infoLogLength);
  67. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  68. // Your GLSL code contained compile errors!
  69. // Hopefully this compile log should help to explain what went wrong.
  70. DBG (errorLog);
  71. jassertfalse;
  72. #endif
  73. return false;
  74. }
  75. context.extensions.glAttachShader (getProgramID(), shaderID);
  76. context.extensions.glDeleteShader (shaderID);
  77. JUCE_CHECK_OPENGL_ERROR
  78. return true;
  79. }
  80. bool OpenGLShaderProgram::addVertexShader (const String& code) { return addShader (code, GL_VERTEX_SHADER); }
  81. bool OpenGLShaderProgram::addFragmentShader (const String& code) { return addShader (code, GL_FRAGMENT_SHADER); }
  82. bool OpenGLShaderProgram::link() noexcept
  83. {
  84. // This method can only be used when the current thread has an active OpenGL context.
  85. jassert (OpenGLHelpers::isContextActive());
  86. GLuint progID = getProgramID();
  87. context.extensions.glLinkProgram (progID);
  88. GLint status = GL_FALSE;
  89. context.extensions.glGetProgramiv (progID, GL_LINK_STATUS, &status);
  90. if (status == GL_FALSE)
  91. {
  92. GLchar infoLog [16384];
  93. GLsizei infoLogLength = 0;
  94. context.extensions.glGetProgramInfoLog (progID, sizeof (infoLog), &infoLogLength, infoLog);
  95. errorLog = String (infoLog, (size_t) infoLogLength);
  96. #if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
  97. // Your GLSL code contained link errors!
  98. // Hopefully this compile log should help to explain what went wrong.
  99. DBG (errorLog);
  100. jassertfalse;
  101. #endif
  102. }
  103. JUCE_CHECK_OPENGL_ERROR
  104. return status != GL_FALSE;
  105. }
  106. void OpenGLShaderProgram::use() const noexcept
  107. {
  108. context.extensions.glUseProgram (programID);
  109. }
  110. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  111. : uniformID (program.context.extensions.glGetUniformLocation (program.getProgramID(), name)), context (program.context)
  112. {
  113. jassert (uniformID >= 0);
  114. }
  115. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  116. : attributeID (program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
  117. {
  118. jassert (attributeID >= 0);
  119. }
  120. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  121. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  122. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  123. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  124. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  125. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  126. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  127. void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
  128. void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
  129. void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }