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.

109 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #if JUCE_USE_OPENGL_SHADERS
  19. OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& context_) noexcept
  20. : context (context_)
  21. {
  22. // This object can only be created and used when the current thread has an active OpenGL context.
  23. jassert (OpenGLHelpers::isContextActive());
  24. programID = context.extensions.glCreateProgram();
  25. }
  26. OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
  27. {
  28. context.extensions.glDeleteProgram (programID);
  29. }
  30. double OpenGLShaderProgram::getLanguageVersion()
  31. {
  32. return String ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  33. .upToFirstOccurrenceOf (" ", false, false).getDoubleValue();
  34. }
  35. void OpenGLShaderProgram::addShader (const char* const code, GLenum type)
  36. {
  37. GLuint shaderID = context.extensions.glCreateShader (type);
  38. context.extensions.glShaderSource (shaderID, 1, (const GLchar**) &code, nullptr);
  39. context.extensions.glCompileShader (shaderID);
  40. #if JUCE_DEBUG
  41. GLint status = 0;
  42. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  43. if (status == GL_FALSE)
  44. {
  45. GLchar infoLog [16384];
  46. GLsizei infologLength = 0;
  47. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infologLength, infoLog);
  48. DBG (String (infoLog, infologLength));
  49. jassertfalse;
  50. }
  51. #endif
  52. context.extensions.glAttachShader (programID, shaderID);
  53. context.extensions.glDeleteShader (shaderID);
  54. }
  55. void OpenGLShaderProgram::link() noexcept
  56. {
  57. context.extensions.glLinkProgram (programID);
  58. #if JUCE_DEBUG
  59. GLint status = 0;
  60. context.extensions.glGetProgramiv (programID, GL_LINK_STATUS, &status);
  61. jassert (status != GL_FALSE);
  62. #endif
  63. }
  64. void OpenGLShaderProgram::use() const noexcept
  65. {
  66. context.extensions.glUseProgram (programID);
  67. }
  68. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  69. : uniformID (program.context.extensions.glGetUniformLocation (program.programID, name)), context (program.context)
  70. {
  71. jassert (uniformID >= 0);
  72. }
  73. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  74. : attributeID (program.context.extensions.glGetAttribLocation (program.programID, name))
  75. {
  76. jassert (attributeID >= 0);
  77. }
  78. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  79. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  80. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  81. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  82. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  83. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  84. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  85. #endif