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.

153 lines
6.3KB

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