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.

154 lines
5.7KB

  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. #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& context) 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 char* const shaderSourceCode, GLenum shaderType);
  48. /** Links all the compiled shaders into a usable program.
  49. If your app is built in debug mode, this method will assert if the program
  50. fails to link correctly.
  51. @returns true if the program linked successfully. If not, you can call
  52. getLastError() to find out what happened.
  53. */
  54. bool link() noexcept;
  55. /** Get the output for the last shader compilation or link that failed. */
  56. const String& getLastError() const noexcept { return errorLog; }
  57. /** Selects this program into the current context. */
  58. void use() const noexcept;
  59. /** Represents an openGL uniform value.
  60. After a program has been linked, you can create Uniform objects to let you
  61. set the uniforms that your shaders use.
  62. Be careful not to call the set() functions unless the appropriate program
  63. is loaded into the current context.
  64. */
  65. struct Uniform
  66. {
  67. /** Initialises a uniform.
  68. The program must have been successfully linked when this
  69. constructor is called.
  70. */
  71. Uniform (const OpenGLShaderProgram& program, const char* uniformName);
  72. /** Sets a float uniform. */
  73. void set (GLfloat n1) const noexcept;
  74. /** Sets an int uniform. */
  75. void set (GLint n1) const noexcept;
  76. /** Sets a vec2 uniform. */
  77. void set (GLfloat n1, GLfloat n2) const noexcept;
  78. /** Sets a vec3 uniform. */
  79. void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
  80. /** Sets a vec4 uniform. */
  81. void set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept;
  82. /** Sets an ivec4 uniform. */
  83. void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
  84. /** Sets a vector float uniform. */
  85. void set (const GLfloat* values, int numValues) const noexcept;
  86. /** Sets a 2x2 matrix float uniform. */
  87. void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  88. /** Sets a 3x3 matrix float uniform. */
  89. void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  90. /** Sets a 4x4 matrix float uniform. */
  91. void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  92. /** The uniform's ID number.
  93. If the uniform couldn't be found, this value will be < 0.
  94. */
  95. GLint uniformID;
  96. private:
  97. const OpenGLContext& context;
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
  99. };
  100. /** Represents an openGL vertex attribute value.
  101. After a program has been linked, you can create Attribute objects to let you
  102. set the attributes that your vertex shaders use.
  103. */
  104. struct Attribute
  105. {
  106. /** Initialises an attribute.
  107. The program must have been successfully linked when this
  108. constructor is called.
  109. */
  110. Attribute (const OpenGLShaderProgram& program, const char* attributeName);
  111. /** The attribute's ID number.
  112. If the uniform couldn't be found, this value will be < 0.
  113. */
  114. GLint attributeID;
  115. };
  116. /** The ID number of the compiled program. */
  117. GLuint programID;
  118. private:
  119. const OpenGLContext& context;
  120. String errorLog;
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
  122. };
  123. #endif // JUCE_OPENGLSHADERPROGRAM_H_INCLUDED