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
5.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. #if JUCE_USE_OPENGL_SHADERS
  18. OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& context_) noexcept
  19. : context (context_)
  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. jassertfalse; // doesn't work in ES
  33. return 0;
  34. #else
  35. return String ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  36. .upToFirstOccurrenceOf (" ", false, false).getDoubleValue();
  37. #endif
  38. }
  39. bool OpenGLShaderProgram::addShader (const char* const code, GLenum type)
  40. {
  41. GLuint shaderID = context.extensions.glCreateShader (type);
  42. context.extensions.glShaderSource (shaderID, 1, (const GLchar**) &code, nullptr);
  43. context.extensions.glCompileShader (shaderID);
  44. GLint status = GL_FALSE;
  45. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  46. if (status == GL_FALSE)
  47. {
  48. GLchar infoLog [16384];
  49. GLsizei infoLogLength = 0;
  50. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
  51. errorLog = String (infoLog, (size_t) infoLogLength);
  52. #if JUCE_DEBUG
  53. DBG (errorLog);
  54. jassertfalse;
  55. #endif
  56. return false;
  57. }
  58. context.extensions.glAttachShader (programID, shaderID);
  59. context.extensions.glDeleteShader (shaderID);
  60. JUCE_CHECK_OPENGL_ERROR
  61. return true;
  62. }
  63. bool OpenGLShaderProgram::link() noexcept
  64. {
  65. context.extensions.glLinkProgram (programID);
  66. GLint status = GL_FALSE;
  67. context.extensions.glGetProgramiv (programID, GL_LINK_STATUS, &status);
  68. if (status == GL_FALSE)
  69. {
  70. GLchar infoLog [16384];
  71. GLsizei infoLogLength = 0;
  72. context.extensions.glGetProgramInfoLog (programID, sizeof (infoLog), &infoLogLength, infoLog);
  73. errorLog = String (infoLog, (size_t) infoLogLength);
  74. #if JUCE_DEBUG
  75. DBG (errorLog);
  76. jassertfalse;
  77. #endif
  78. }
  79. JUCE_CHECK_OPENGL_ERROR
  80. return status != GL_FALSE;
  81. }
  82. void OpenGLShaderProgram::use() const noexcept
  83. {
  84. context.extensions.glUseProgram (programID);
  85. }
  86. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  87. : uniformID (program.context.extensions.glGetUniformLocation (program.programID, name)), context (program.context)
  88. {
  89. jassert (uniformID >= 0);
  90. }
  91. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  92. : attributeID (program.context.extensions.glGetAttribLocation (program.programID, name))
  93. {
  94. jassert (attributeID >= 0);
  95. }
  96. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  97. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  98. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  99. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  100. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  101. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  102. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  103. void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
  104. void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
  105. void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }
  106. #endif