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.

131 lines
5.1KB

  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. #if JUCE_OPENGL_ES
  33. jassertfalse; // doesn't work in ES
  34. return 0;
  35. #else
  36. return String ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
  37. .upToFirstOccurrenceOf (" ", false, false).getDoubleValue();
  38. #endif
  39. }
  40. bool OpenGLShaderProgram::addShader (const char* const code, GLenum type)
  41. {
  42. GLuint shaderID = context.extensions.glCreateShader (type);
  43. context.extensions.glShaderSource (shaderID, 1, (const GLchar**) &code, nullptr);
  44. context.extensions.glCompileShader (shaderID);
  45. GLint status = GL_FALSE;
  46. context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
  47. if (status == GL_FALSE)
  48. {
  49. GLchar infoLog [16384];
  50. GLsizei infologLength = 0;
  51. context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infologLength, infoLog);
  52. errorLog = String (infoLog, infologLength);
  53. #if JUCE_DEBUG
  54. DBG (errorLog);
  55. jassertfalse;
  56. #endif
  57. return false;
  58. }
  59. context.extensions.glAttachShader (programID, shaderID);
  60. context.extensions.glDeleteShader (shaderID);
  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, infologLength);
  74. #if JUCE_DEBUG
  75. DBG (errorLog);
  76. jassertfalse;
  77. #endif
  78. }
  79. return status != GL_FALSE;
  80. }
  81. void OpenGLShaderProgram::use() const noexcept
  82. {
  83. context.extensions.glUseProgram (programID);
  84. }
  85. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  86. : uniformID (program.context.extensions.glGetUniformLocation (program.programID, name)), context (program.context)
  87. {
  88. jassert (uniformID >= 0);
  89. }
  90. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  91. : attributeID (program.context.extensions.glGetAttribLocation (program.programID, name))
  92. {
  93. jassert (attributeID >= 0);
  94. }
  95. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  96. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  97. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  98. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  99. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  100. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  101. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  102. #endif