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.

199 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_OPENGLSHADERPROGRAM_H_INCLUDED
  18. #define JUCE_OPENGLSHADERPROGRAM_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Manages an OpenGL shader program.
  22. */
  23. class JUCE_API OpenGLShaderProgram
  24. {
  25. public:
  26. OpenGLShaderProgram (const OpenGLContext&) noexcept;
  27. ~OpenGLShaderProgram() noexcept;
  28. /** Returns the version of GLSL that the current context supports.
  29. E.g.
  30. @code
  31. if (OpenGLShaderProgram::getLanguageVersion() > 1.199)
  32. {
  33. // ..do something that requires GLSL 1.2 or above..
  34. }
  35. @endcode
  36. */
  37. static double getLanguageVersion();
  38. /** Compiles and adds a shader to this program.
  39. After adding all your shaders, remember to call link() to link them into
  40. a usable program.
  41. If your app is built in debug mode, this method will assert if the program
  42. fails to compile correctly.
  43. The shaderType parameter could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc.
  44. @returns true if the shader compiled successfully. If not, you can call
  45. getLastError() to find out what happened.
  46. */
  47. bool addShader (const String& shaderSourceCode, GLenum shaderType);
  48. /** Compiles and adds a fragment shader to this program.
  49. This is equivalent to calling addShader() with a type of GL_VERTEX_SHADER.
  50. */
  51. bool addVertexShader (const String& shaderSourceCode);
  52. /** Compiles and adds a fragment shader to this program.
  53. This is equivalent to calling addShader() with a type of GL_FRAGMENT_SHADER.
  54. */
  55. bool addFragmentShader (const String& shaderSourceCode);
  56. /** Links all the compiled shaders into a usable program.
  57. If your app is built in debug mode, this method will assert if the program
  58. fails to link correctly.
  59. @returns true if the program linked successfully. If not, you can call
  60. getLastError() to find out what happened.
  61. */
  62. bool link() noexcept;
  63. /** Get the output for the last shader compilation or link that failed. */
  64. const String& getLastError() const noexcept { return errorLog; }
  65. /** Selects this program into the current context. */
  66. void use() const noexcept;
  67. /** Deletes the program. */
  68. void release() noexcept;
  69. //==============================================================================
  70. // Methods for setting shader uniforms without using a Uniform object (see below).
  71. // You must make sure this shader is the currently bound one before setting uniforms
  72. // with these functions.
  73. /** Get the uniform ID from the variable name */
  74. GLint getUniformIDFromName (const char* uniformName) const noexcept;
  75. /** Sets a float uniform. */
  76. void setUniform (const char* uniformName, GLfloat value) noexcept;
  77. /** Sets an int uniform. */
  78. void setUniform (const char* uniformName, GLint value) noexcept;
  79. /** Sets a vec2 uniform. */
  80. void setUniform (const char* uniformName, GLfloat x, GLfloat y) noexcept;
  81. /** Sets a vec3 uniform. */
  82. void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z) noexcept;
  83. /** Sets a vec4 uniform. */
  84. void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z, GLfloat w) noexcept;
  85. /** Sets a vec4 uniform. */
  86. void setUniform (const char* uniformName, GLint x, GLint y, GLint z, GLint w) noexcept;
  87. /** Sets a vector float uniform. */
  88. void setUniform (const char* uniformName, const GLfloat* values, GLsizei numValues) noexcept;
  89. /** Sets a 2x2 matrix float uniform. */
  90. void setUniformMat2 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
  91. /** Sets a 3x3 matrix float uniform. */
  92. void setUniformMat3 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
  93. /** Sets a 4x4 matrix float uniform. */
  94. void setUniformMat4 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
  95. //==============================================================================
  96. /** Represents an openGL uniform value.
  97. After a program has been linked, you can create Uniform objects to let you
  98. set the uniforms that your shaders use.
  99. Be careful not to call the set() functions unless the appropriate program
  100. is loaded into the current context.
  101. */
  102. struct Uniform
  103. {
  104. /** Initialises a uniform.
  105. The program must have been successfully linked when this
  106. constructor is called.
  107. */
  108. Uniform (const OpenGLShaderProgram& program, const char* uniformName);
  109. /** Sets a float uniform. */
  110. void set (GLfloat n1) const noexcept;
  111. /** Sets an int uniform. */
  112. void set (GLint n1) const noexcept;
  113. /** Sets a vec2 uniform. */
  114. void set (GLfloat n1, GLfloat n2) const noexcept;
  115. /** Sets a vec3 uniform. */
  116. void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
  117. /** Sets a vec4 uniform. */
  118. void set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept;
  119. /** Sets an ivec4 uniform. */
  120. void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
  121. /** Sets a vector float uniform. */
  122. void set (const GLfloat* values, int numValues) const noexcept;
  123. /** Sets a 2x2 matrix float uniform. */
  124. void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  125. /** Sets a 3x3 matrix float uniform. */
  126. void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  127. /** Sets a 4x4 matrix float uniform. */
  128. void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  129. /** The uniform's ID number.
  130. If the uniform couldn't be found, this value will be < 0.
  131. */
  132. GLint uniformID;
  133. private:
  134. const OpenGLContext& context;
  135. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
  136. };
  137. //==============================================================================
  138. /** Represents an openGL vertex attribute value.
  139. After a program has been linked, you can create Attribute objects to let you
  140. set the attributes that your vertex shaders use.
  141. */
  142. struct Attribute
  143. {
  144. /** Initialises an attribute.
  145. The program must have been successfully linked when this
  146. constructor is called.
  147. */
  148. Attribute (const OpenGLShaderProgram& program, const char* attributeName);
  149. /** The attribute's ID number.
  150. If the uniform couldn't be found, this value will be < 0.
  151. */
  152. GLuint attributeID;
  153. };
  154. /** The ID number of the compiled program. */
  155. GLuint getProgramID() const noexcept;
  156. private:
  157. const OpenGLContext& context;
  158. mutable GLuint programID;
  159. String errorLog;
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
  161. };
  162. #endif // JUCE_OPENGLSHADERPROGRAM_H_INCLUDED