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.

133 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. JUCE_CHECK_OPENGL_ERROR
  62. return true;
  63. }
  64. bool OpenGLShaderProgram::link() noexcept
  65. {
  66. context.extensions.glLinkProgram (programID);
  67. GLint status = GL_FALSE;
  68. context.extensions.glGetProgramiv (programID, GL_LINK_STATUS, &status);
  69. if (status == GL_FALSE)
  70. {
  71. GLchar infoLog [16384];
  72. GLsizei infologLength = 0;
  73. context.extensions.glGetProgramInfoLog (programID, sizeof (infoLog), &infologLength, infoLog);
  74. errorLog = String (infoLog, infologLength);
  75. #if JUCE_DEBUG
  76. DBG (errorLog);
  77. jassertfalse;
  78. #endif
  79. }
  80. JUCE_CHECK_OPENGL_ERROR
  81. return status != GL_FALSE;
  82. }
  83. void OpenGLShaderProgram::use() const noexcept
  84. {
  85. context.extensions.glUseProgram (programID);
  86. }
  87. OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
  88. : uniformID (program.context.extensions.glGetUniformLocation (program.programID, name)), context (program.context)
  89. {
  90. jassert (uniformID >= 0);
  91. }
  92. OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
  93. : attributeID (program.context.extensions.glGetAttribLocation (program.programID, name))
  94. {
  95. jassert (attributeID >= 0);
  96. }
  97. void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
  98. void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
  99. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
  100. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
  101. void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
  102. void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
  103. void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
  104. #endif