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.

155 lines
5.9KB

  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. #ifndef __JUCE_OPENGLSHADERPROGRAM_JUCEHEADER__
  19. #define __JUCE_OPENGLSHADERPROGRAM_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Manages an OpenGL shader program.
  23. */
  24. class JUCE_API OpenGLShaderProgram
  25. {
  26. public:
  27. OpenGLShaderProgram (const OpenGLContext& context) noexcept;
  28. ~OpenGLShaderProgram() noexcept;
  29. /** Returns the version of GLSL that the current context supports.
  30. E.g.
  31. @code
  32. if (OpenGLShaderProgram::getLanguageVersion() > 1.199)
  33. {
  34. // ..do something that requires GLSL 1.2 or above..
  35. }
  36. @endcode
  37. */
  38. static double getLanguageVersion();
  39. /** Compiles and adds a shader to this program.
  40. After adding all your shaders, remember to call link() to link them into
  41. a usable program.
  42. If your app is built in debug mode, this method will assert if the program
  43. fails to compile correctly.
  44. The shaderType parameter could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc.
  45. @returns true if the shader compiled successfully. If not, you can call
  46. getLastError() to find out what happened.
  47. */
  48. bool addShader (const char* const shaderSourceCode, GLenum shaderType);
  49. /** Links all the compiled shaders into a usable program.
  50. If your app is built in debug mode, this method will assert if the program
  51. fails to link correctly.
  52. @returns true if the program linked successfully. If not, you can call
  53. getLastError() to find out what happened.
  54. */
  55. bool link() noexcept;
  56. /** Get the output for the last shader compilation or link that failed. */
  57. const String& getLastError() const noexcept { return errorLog; }
  58. /** Selects this program into the current context. */
  59. void use() const noexcept;
  60. /** Represents an openGL uniform value.
  61. After a program has been linked, you can create Uniform objects to let you
  62. set the uniforms that your shaders use.
  63. Be careful not to call the set() functions unless the appropriate program
  64. is loaded into the current context.
  65. */
  66. struct Uniform
  67. {
  68. /** Initialises a uniform.
  69. The program must have been successfully linked when this
  70. constructor is called.
  71. */
  72. Uniform (const OpenGLShaderProgram& program, const char* uniformName);
  73. /** Sets a float uniform. */
  74. void set (GLfloat n1) const noexcept;
  75. /** Sets an int uniform. */
  76. void set (GLint n1) const noexcept;
  77. /** Sets a vec2 uniform. */
  78. void set (GLfloat n1, GLfloat n2) const noexcept;
  79. /** Sets a vec3 uniform. */
  80. void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
  81. /** Sets a vec4 uniform. */
  82. void set (GLfloat n1, GLfloat n2, GLfloat n3, float n4) const noexcept;
  83. /** Sets an ivec4 uniform. */
  84. void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
  85. /** Sets a vector float uniform. */
  86. void set (const GLfloat* values, int numValues) const noexcept;
  87. /** Sets a 2x2 matrix float uniform. */
  88. void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  89. /** Sets a 3x3 matrix float uniform. */
  90. void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  91. /** Sets a 4x4 matrix float uniform. */
  92. void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
  93. /** The uniform's ID number.
  94. If the uniform couldn't be found, this value will be < 0.
  95. */
  96. GLint uniformID;
  97. private:
  98. const OpenGLContext& context;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
  100. };
  101. /** Represents an openGL vertex attribute value.
  102. After a program has been linked, you can create Attribute objects to let you
  103. set the attributes that your vertex shaders use.
  104. */
  105. struct Attribute
  106. {
  107. /** Initialises an attribute.
  108. The program must have been successfully linked when this
  109. constructor is called.
  110. */
  111. Attribute (const OpenGLShaderProgram& program, const char* attributeName);
  112. /** The attribute's ID number.
  113. If the uniform couldn't be found, this value will be < 0.
  114. */
  115. GLint attributeID;
  116. };
  117. /** The ID number of the compiled program. */
  118. GLuint programID;
  119. private:
  120. const OpenGLContext& context;
  121. String errorLog;
  122. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
  123. };
  124. #endif // __JUCE_OPENGLSHADERPROGRAM_JUCEHEADER__