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.

149 lines
6.2KB

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