qglshaderprogram.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtOpenGL module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QGLSHADERPROGRAM_H
  40. #define QGLSHADERPROGRAM_H
  41. #include <QtOpenGL/qgl.h>
  42. #include <QtGui/qvector2d.h>
  43. #include <QtGui/qvector3d.h>
  44. #include <QtGui/qvector4d.h>
  45. #include <QtGui/qmatrix4x4.h>
  46. QT_BEGIN_NAMESPACE
  47. class QGLShaderProgram;
  48. class QGLShaderPrivate;
  49. class Q_OPENGL_EXPORT QGLShader : public QObject
  50. {
  51. Q_OBJECT
  52. public:
  53. enum ShaderTypeBit
  54. {
  55. Vertex = 0x0001,
  56. Fragment = 0x0002,
  57. Geometry = 0x0004
  58. };
  59. Q_DECLARE_FLAGS(ShaderType, ShaderTypeBit)
  60. explicit QGLShader(QGLShader::ShaderType type, QObject *parent = Q_NULLPTR);
  61. QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObject *parent = Q_NULLPTR);
  62. virtual ~QGLShader();
  63. QGLShader::ShaderType shaderType() const;
  64. bool compileSourceCode(const char *source);
  65. bool compileSourceCode(const QByteArray& source);
  66. bool compileSourceCode(const QString& source);
  67. bool compileSourceFile(const QString& fileName);
  68. QByteArray sourceCode() const;
  69. bool isCompiled() const;
  70. QString log() const;
  71. GLuint shaderId() const;
  72. static bool hasOpenGLShaders(ShaderType type, const QGLContext *context = Q_NULLPTR);
  73. private:
  74. friend class QGLShaderProgram;
  75. Q_DISABLE_COPY(QGLShader)
  76. Q_DECLARE_PRIVATE(QGLShader)
  77. };
  78. Q_DECLARE_OPERATORS_FOR_FLAGS(QGLShader::ShaderType)
  79. class QGLShaderProgramPrivate;
  80. class Q_OPENGL_EXPORT QGLShaderProgram : public QObject
  81. {
  82. Q_OBJECT
  83. public:
  84. explicit QGLShaderProgram(QObject *parent = Q_NULLPTR);
  85. explicit QGLShaderProgram(const QGLContext *context, QObject *parent = Q_NULLPTR);
  86. virtual ~QGLShaderProgram();
  87. bool addShader(QGLShader *shader);
  88. void removeShader(QGLShader *shader);
  89. QList<QGLShader *> shaders() const;
  90. bool addShaderFromSourceCode(QGLShader::ShaderType type, const char *source);
  91. bool addShaderFromSourceCode(QGLShader::ShaderType type, const QByteArray& source);
  92. bool addShaderFromSourceCode(QGLShader::ShaderType type, const QString& source);
  93. bool addShaderFromSourceFile(QGLShader::ShaderType type, const QString& fileName);
  94. void removeAllShaders();
  95. virtual bool link();
  96. bool isLinked() const;
  97. QString log() const;
  98. bool bind();
  99. void release();
  100. GLuint programId() const;
  101. int maxGeometryOutputVertices() const;
  102. void setGeometryOutputVertexCount(int count);
  103. int geometryOutputVertexCount() const;
  104. void setGeometryInputType(GLenum inputType);
  105. GLenum geometryInputType() const;
  106. void setGeometryOutputType(GLenum outputType);
  107. GLenum geometryOutputType() const;
  108. void bindAttributeLocation(const char *name, int location);
  109. void bindAttributeLocation(const QByteArray& name, int location);
  110. void bindAttributeLocation(const QString& name, int location);
  111. int attributeLocation(const char *name) const;
  112. int attributeLocation(const QByteArray& name) const;
  113. int attributeLocation(const QString& name) const;
  114. void setAttributeValue(int location, GLfloat value);
  115. void setAttributeValue(int location, GLfloat x, GLfloat y);
  116. void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z);
  117. void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  118. void setAttributeValue(int location, const QVector2D& value);
  119. void setAttributeValue(int location, const QVector3D& value);
  120. void setAttributeValue(int location, const QVector4D& value);
  121. void setAttributeValue(int location, const QColor& value);
  122. void setAttributeValue(int location, const GLfloat *values, int columns, int rows);
  123. void setAttributeValue(const char *name, GLfloat value);
  124. void setAttributeValue(const char *name, GLfloat x, GLfloat y);
  125. void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
  126. void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  127. void setAttributeValue(const char *name, const QVector2D& value);
  128. void setAttributeValue(const char *name, const QVector3D& value);
  129. void setAttributeValue(const char *name, const QVector4D& value);
  130. void setAttributeValue(const char *name, const QColor& value);
  131. void setAttributeValue(const char *name, const GLfloat *values, int columns, int rows);
  132. void setAttributeArray
  133. (int location, const GLfloat *values, int tupleSize, int stride = 0);
  134. void setAttributeArray
  135. (int location, const QVector2D *values, int stride = 0);
  136. void setAttributeArray
  137. (int location, const QVector3D *values, int stride = 0);
  138. void setAttributeArray
  139. (int location, const QVector4D *values, int stride = 0);
  140. void setAttributeArray
  141. (int location, GLenum type, const void *values, int tupleSize, int stride = 0);
  142. void setAttributeArray
  143. (const char *name, const GLfloat *values, int tupleSize, int stride = 0);
  144. void setAttributeArray
  145. (const char *name, const QVector2D *values, int stride = 0);
  146. void setAttributeArray
  147. (const char *name, const QVector3D *values, int stride = 0);
  148. void setAttributeArray
  149. (const char *name, const QVector4D *values, int stride = 0);
  150. void setAttributeArray
  151. (const char *name, GLenum type, const void *values, int tupleSize, int stride = 0);
  152. void setAttributeBuffer
  153. (int location, GLenum type, int offset, int tupleSize, int stride = 0);
  154. void setAttributeBuffer
  155. (const char *name, GLenum type, int offset, int tupleSize, int stride = 0);
  156. void enableAttributeArray(int location);
  157. void enableAttributeArray(const char *name);
  158. void disableAttributeArray(int location);
  159. void disableAttributeArray(const char *name);
  160. int uniformLocation(const char *name) const;
  161. int uniformLocation(const QByteArray& name) const;
  162. int uniformLocation(const QString& name) const;
  163. void setUniformValue(int location, GLfloat value);
  164. void setUniformValue(int location, GLint value);
  165. void setUniformValue(int location, GLuint value);
  166. void setUniformValue(int location, GLfloat x, GLfloat y);
  167. void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z);
  168. void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  169. void setUniformValue(int location, const QVector2D& value);
  170. void setUniformValue(int location, const QVector3D& value);
  171. void setUniformValue(int location, const QVector4D& value);
  172. void setUniformValue(int location, const QColor& color);
  173. void setUniformValue(int location, const QPoint& point);
  174. void setUniformValue(int location, const QPointF& point);
  175. void setUniformValue(int location, const QSize& size);
  176. void setUniformValue(int location, const QSizeF& size);
  177. void setUniformValue(int location, const QMatrix2x2& value);
  178. void setUniformValue(int location, const QMatrix2x3& value);
  179. void setUniformValue(int location, const QMatrix2x4& value);
  180. void setUniformValue(int location, const QMatrix3x2& value);
  181. void setUniformValue(int location, const QMatrix3x3& value);
  182. void setUniformValue(int location, const QMatrix3x4& value);
  183. void setUniformValue(int location, const QMatrix4x2& value);
  184. void setUniformValue(int location, const QMatrix4x3& value);
  185. void setUniformValue(int location, const QMatrix4x4& value);
  186. void setUniformValue(int location, const GLfloat value[2][2]);
  187. void setUniformValue(int location, const GLfloat value[3][3]);
  188. void setUniformValue(int location, const GLfloat value[4][4]);
  189. void setUniformValue(int location, const QTransform& value);
  190. void setUniformValue(const char *name, GLfloat value);
  191. void setUniformValue(const char *name, GLint value);
  192. void setUniformValue(const char *name, GLuint value);
  193. void setUniformValue(const char *name, GLfloat x, GLfloat y);
  194. void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
  195. void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  196. void setUniformValue(const char *name, const QVector2D& value);
  197. void setUniformValue(const char *name, const QVector3D& value);
  198. void setUniformValue(const char *name, const QVector4D& value);
  199. void setUniformValue(const char *name, const QColor& color);
  200. void setUniformValue(const char *name, const QPoint& point);
  201. void setUniformValue(const char *name, const QPointF& point);
  202. void setUniformValue(const char *name, const QSize& size);
  203. void setUniformValue(const char *name, const QSizeF& size);
  204. void setUniformValue(const char *name, const QMatrix2x2& value);
  205. void setUniformValue(const char *name, const QMatrix2x3& value);
  206. void setUniformValue(const char *name, const QMatrix2x4& value);
  207. void setUniformValue(const char *name, const QMatrix3x2& value);
  208. void setUniformValue(const char *name, const QMatrix3x3& value);
  209. void setUniformValue(const char *name, const QMatrix3x4& value);
  210. void setUniformValue(const char *name, const QMatrix4x2& value);
  211. void setUniformValue(const char *name, const QMatrix4x3& value);
  212. void setUniformValue(const char *name, const QMatrix4x4& value);
  213. void setUniformValue(const char *name, const GLfloat value[2][2]);
  214. void setUniformValue(const char *name, const GLfloat value[3][3]);
  215. void setUniformValue(const char *name, const GLfloat value[4][4]);
  216. void setUniformValue(const char *name, const QTransform& value);
  217. void setUniformValueArray(int location, const GLfloat *values, int count, int tupleSize);
  218. void setUniformValueArray(int location, const GLint *values, int count);
  219. void setUniformValueArray(int location, const GLuint *values, int count);
  220. void setUniformValueArray(int location, const QVector2D *values, int count);
  221. void setUniformValueArray(int location, const QVector3D *values, int count);
  222. void setUniformValueArray(int location, const QVector4D *values, int count);
  223. void setUniformValueArray(int location, const QMatrix2x2 *values, int count);
  224. void setUniformValueArray(int location, const QMatrix2x3 *values, int count);
  225. void setUniformValueArray(int location, const QMatrix2x4 *values, int count);
  226. void setUniformValueArray(int location, const QMatrix3x2 *values, int count);
  227. void setUniformValueArray(int location, const QMatrix3x3 *values, int count);
  228. void setUniformValueArray(int location, const QMatrix3x4 *values, int count);
  229. void setUniformValueArray(int location, const QMatrix4x2 *values, int count);
  230. void setUniformValueArray(int location, const QMatrix4x3 *values, int count);
  231. void setUniformValueArray(int location, const QMatrix4x4 *values, int count);
  232. void setUniformValueArray(const char *name, const GLfloat *values, int count, int tupleSize);
  233. void setUniformValueArray(const char *name, const GLint *values, int count);
  234. void setUniformValueArray(const char *name, const GLuint *values, int count);
  235. void setUniformValueArray(const char *name, const QVector2D *values, int count);
  236. void setUniformValueArray(const char *name, const QVector3D *values, int count);
  237. void setUniformValueArray(const char *name, const QVector4D *values, int count);
  238. void setUniformValueArray(const char *name, const QMatrix2x2 *values, int count);
  239. void setUniformValueArray(const char *name, const QMatrix2x3 *values, int count);
  240. void setUniformValueArray(const char *name, const QMatrix2x4 *values, int count);
  241. void setUniformValueArray(const char *name, const QMatrix3x2 *values, int count);
  242. void setUniformValueArray(const char *name, const QMatrix3x3 *values, int count);
  243. void setUniformValueArray(const char *name, const QMatrix3x4 *values, int count);
  244. void setUniformValueArray(const char *name, const QMatrix4x2 *values, int count);
  245. void setUniformValueArray(const char *name, const QMatrix4x3 *values, int count);
  246. void setUniformValueArray(const char *name, const QMatrix4x4 *values, int count);
  247. static bool hasOpenGLShaderPrograms(const QGLContext *context = Q_NULLPTR);
  248. private Q_SLOTS:
  249. void shaderDestroyed();
  250. private:
  251. Q_DISABLE_COPY(QGLShaderProgram)
  252. Q_DECLARE_PRIVATE(QGLShaderProgram)
  253. bool init();
  254. };
  255. QT_END_NAMESPACE
  256. #endif