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 QtGui 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 
40 #ifndef QOPENGLSHADERPROGRAM_H
41 #define QOPENGLSHADERPROGRAM_H
42 
43 #include <QtGui/qtguiglobal.h>
44 
45 #ifndef QT_NO_OPENGL
46 
47 #include <QtGui/qopengl.h>
48 #include <QtGui/qvector2d.h>
49 #include <QtGui/qvector3d.h>
50 #include <QtGui/qvector4d.h>
51 #include <QtGui/qmatrix4x4.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 
56 class QOpenGLContext;
57 class QOpenGLShaderProgram;
58 class QOpenGLShaderPrivate;
59 
60 class Q_GUI_EXPORT QOpenGLShader : public QObject
61 {
62     Q_OBJECT
63 public:
64     enum ShaderTypeBit
65     {
66         Vertex                 = 0x0001,
67         Fragment               = 0x0002,
68         Geometry               = 0x0004,
69         TessellationControl    = 0x0008,
70         TessellationEvaluation = 0x0010,
71         Compute                = 0x0020
72     };
73     Q_DECLARE_FLAGS(ShaderType, ShaderTypeBit)
74 
75     explicit QOpenGLShader(QOpenGLShader::ShaderType type, QObject *parent = nullptr);
76     ~QOpenGLShader();
77 
78     QOpenGLShader::ShaderType shaderType() const;
79 
80     bool compileSourceCode(const char *source);
81     bool compileSourceCode(const QByteArray& source);
82     bool compileSourceCode(const QString& source);
83     bool compileSourceFile(const QString& fileName);
84 
85     QByteArray sourceCode() const;
86 
87     bool isCompiled() const;
88     QString log() const;
89 
90     GLuint shaderId() const;
91 
92     static bool hasOpenGLShaders(ShaderType type, QOpenGLContext *context = nullptr);
93 
94 private:
95     friend class QOpenGLShaderProgram;
96 
97     Q_DISABLE_COPY(QOpenGLShader)
98     Q_DECLARE_PRIVATE(QOpenGLShader)
99 };
100 
101 Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLShader::ShaderType)
102 
103 
104 class QOpenGLShaderProgramPrivate;
105 
106 class Q_GUI_EXPORT QOpenGLShaderProgram : public QObject
107 {
108     Q_OBJECT
109 public:
110     explicit QOpenGLShaderProgram(QObject *parent = nullptr);
111     ~QOpenGLShaderProgram();
112 
113     bool addShader(QOpenGLShader *shader);
114     void removeShader(QOpenGLShader *shader);
115     QList<QOpenGLShader *> shaders() const;
116 
117     bool addShaderFromSourceCode(QOpenGLShader::ShaderType type, const char *source);
118     bool addShaderFromSourceCode(QOpenGLShader::ShaderType type, const QByteArray& source);
119     bool addShaderFromSourceCode(QOpenGLShader::ShaderType type, const QString& source);
120     bool addShaderFromSourceFile(QOpenGLShader::ShaderType type, const QString& fileName);
121 
122     bool addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type, const char *source);
123     bool addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type, const QByteArray &source);
124     bool addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type, const QString &source);
125     bool addCacheableShaderFromSourceFile(QOpenGLShader::ShaderType type, const QString &fileName);
126 
127     void removeAllShaders();
128 
129     virtual bool link();
130     bool isLinked() const;
131     QString log() const;
132 
133     bool bind();
134     void release();
135 
136     bool create();
137 
138     GLuint programId() const;
139 
140     int maxGeometryOutputVertices() const;
141 
142     void setPatchVertexCount(int count);
143     int patchVertexCount() const;
144 
145     void setDefaultOuterTessellationLevels(const QVector<float> &levels);
146     QVector<float> defaultOuterTessellationLevels() const;
147 
148     void setDefaultInnerTessellationLevels(const QVector<float> &levels);
149     QVector<float> defaultInnerTessellationLevels() const;
150 
151     void bindAttributeLocation(const char *name, int location);
152     void bindAttributeLocation(const QByteArray& name, int location);
153     void bindAttributeLocation(const QString& name, int location);
154 
155     int attributeLocation(const char *name) const;
156     int attributeLocation(const QByteArray& name) const;
157     int attributeLocation(const QString& name) const;
158 
159     void setAttributeValue(int location, GLfloat value);
160     void setAttributeValue(int location, GLfloat x, GLfloat y);
161     void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z);
162     void setAttributeValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
163     void setAttributeValue(int location, const QVector2D& value);
164     void setAttributeValue(int location, const QVector3D& value);
165     void setAttributeValue(int location, const QVector4D& value);
166     void setAttributeValue(int location, const QColor& value);
167     void setAttributeValue(int location, const GLfloat *values, int columns, int rows);
168 
169     void setAttributeValue(const char *name, GLfloat value);
170     void setAttributeValue(const char *name, GLfloat x, GLfloat y);
171     void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
172     void setAttributeValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
173     void setAttributeValue(const char *name, const QVector2D& value);
174     void setAttributeValue(const char *name, const QVector3D& value);
175     void setAttributeValue(const char *name, const QVector4D& value);
176     void setAttributeValue(const char *name, const QColor& value);
177     void setAttributeValue(const char *name, const GLfloat *values, int columns, int rows);
178 
179     void setAttributeArray
180         (int location, const GLfloat *values, int tupleSize, int stride = 0);
181     void setAttributeArray
182         (int location, const QVector2D *values, int stride = 0);
183     void setAttributeArray
184         (int location, const QVector3D *values, int stride = 0);
185     void setAttributeArray
186         (int location, const QVector4D *values, int stride = 0);
187     void setAttributeArray
188         (int location, GLenum type, const void *values, int tupleSize, int stride = 0);
189     void setAttributeArray
190         (const char *name, const GLfloat *values, int tupleSize, int stride = 0);
191     void setAttributeArray
192         (const char *name, const QVector2D *values, int stride = 0);
193     void setAttributeArray
194         (const char *name, const QVector3D *values, int stride = 0);
195     void setAttributeArray
196         (const char *name, const QVector4D *values, int stride = 0);
197     void setAttributeArray
198         (const char *name, GLenum type, const void *values, int tupleSize, int stride = 0);
199 
200     void setAttributeBuffer
201         (int location, GLenum type, int offset, int tupleSize, int stride = 0);
202     void setAttributeBuffer
203         (const char *name, GLenum type, int offset, int tupleSize, int stride = 0);
204 
205     void enableAttributeArray(int location);
206     void enableAttributeArray(const char *name);
207     void disableAttributeArray(int location);
208     void disableAttributeArray(const char *name);
209 
210     int uniformLocation(const char *name) const;
211     int uniformLocation(const QByteArray& name) const;
212     int uniformLocation(const QString& name) const;
213 
214     void setUniformValue(int location, GLfloat value);
215     void setUniformValue(int location, GLint value);
216     void setUniformValue(int location, GLuint value);
217     void setUniformValue(int location, GLfloat x, GLfloat y);
218     void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z);
219     void setUniformValue(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
220     void setUniformValue(int location, const QVector2D& value);
221     void setUniformValue(int location, const QVector3D& value);
222     void setUniformValue(int location, const QVector4D& value);
223     void setUniformValue(int location, const QColor& color);
224     void setUniformValue(int location, const QPoint& point);
225     void setUniformValue(int location, const QPointF& point);
226     void setUniformValue(int location, const QSize& size);
227     void setUniformValue(int location, const QSizeF& size);
228     void setUniformValue(int location, const QMatrix2x2& value);
229     void setUniformValue(int location, const QMatrix2x3& value);
230     void setUniformValue(int location, const QMatrix2x4& value);
231     void setUniformValue(int location, const QMatrix3x2& value);
232     void setUniformValue(int location, const QMatrix3x3& value);
233     void setUniformValue(int location, const QMatrix3x4& value);
234     void setUniformValue(int location, const QMatrix4x2& value);
235     void setUniformValue(int location, const QMatrix4x3& value);
236     void setUniformValue(int location, const QMatrix4x4& value);
237     void setUniformValue(int location, const GLfloat value[2][2]);
238     void setUniformValue(int location, const GLfloat value[3][3]);
239     void setUniformValue(int location, const GLfloat value[4][4]);
240     void setUniformValue(int location, const QTransform& value);
241 
242     void setUniformValue(const char *name, GLfloat value);
243     void setUniformValue(const char *name, GLint value);
244     void setUniformValue(const char *name, GLuint value);
245     void setUniformValue(const char *name, GLfloat x, GLfloat y);
246     void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z);
247     void setUniformValue(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
248     void setUniformValue(const char *name, const QVector2D& value);
249     void setUniformValue(const char *name, const QVector3D& value);
250     void setUniformValue(const char *name, const QVector4D& value);
251     void setUniformValue(const char *name, const QColor& color);
252     void setUniformValue(const char *name, const QPoint& point);
253     void setUniformValue(const char *name, const QPointF& point);
254     void setUniformValue(const char *name, const QSize& size);
255     void setUniformValue(const char *name, const QSizeF& size);
256     void setUniformValue(const char *name, const QMatrix2x2& value);
257     void setUniformValue(const char *name, const QMatrix2x3& value);
258     void setUniformValue(const char *name, const QMatrix2x4& value);
259     void setUniformValue(const char *name, const QMatrix3x2& value);
260     void setUniformValue(const char *name, const QMatrix3x3& value);
261     void setUniformValue(const char *name, const QMatrix3x4& value);
262     void setUniformValue(const char *name, const QMatrix4x2& value);
263     void setUniformValue(const char *name, const QMatrix4x3& value);
264     void setUniformValue(const char *name, const QMatrix4x4& value);
265     void setUniformValue(const char *name, const GLfloat value[2][2]);
266     void setUniformValue(const char *name, const GLfloat value[3][3]);
267     void setUniformValue(const char *name, const GLfloat value[4][4]);
268     void setUniformValue(const char *name, const QTransform& value);
269 
270     void setUniformValueArray(int location, const GLfloat *values, int count, int tupleSize);
271     void setUniformValueArray(int location, const GLint *values, int count);
272     void setUniformValueArray(int location, const GLuint *values, int count);
273     void setUniformValueArray(int location, const QVector2D *values, int count);
274     void setUniformValueArray(int location, const QVector3D *values, int count);
275     void setUniformValueArray(int location, const QVector4D *values, int count);
276     void setUniformValueArray(int location, const QMatrix2x2 *values, int count);
277     void setUniformValueArray(int location, const QMatrix2x3 *values, int count);
278     void setUniformValueArray(int location, const QMatrix2x4 *values, int count);
279     void setUniformValueArray(int location, const QMatrix3x2 *values, int count);
280     void setUniformValueArray(int location, const QMatrix3x3 *values, int count);
281     void setUniformValueArray(int location, const QMatrix3x4 *values, int count);
282     void setUniformValueArray(int location, const QMatrix4x2 *values, int count);
283     void setUniformValueArray(int location, const QMatrix4x3 *values, int count);
284     void setUniformValueArray(int location, const QMatrix4x4 *values, int count);
285 
286     void setUniformValueArray(const char *name, const GLfloat *values, int count, int tupleSize);
287     void setUniformValueArray(const char *name, const GLint *values, int count);
288     void setUniformValueArray(const char *name, const GLuint *values, int count);
289     void setUniformValueArray(const char *name, const QVector2D *values, int count);
290     void setUniformValueArray(const char *name, const QVector3D *values, int count);
291     void setUniformValueArray(const char *name, const QVector4D *values, int count);
292     void setUniformValueArray(const char *name, const QMatrix2x2 *values, int count);
293     void setUniformValueArray(const char *name, const QMatrix2x3 *values, int count);
294     void setUniformValueArray(const char *name, const QMatrix2x4 *values, int count);
295     void setUniformValueArray(const char *name, const QMatrix3x2 *values, int count);
296     void setUniformValueArray(const char *name, const QMatrix3x3 *values, int count);
297     void setUniformValueArray(const char *name, const QMatrix3x4 *values, int count);
298     void setUniformValueArray(const char *name, const QMatrix4x2 *values, int count);
299     void setUniformValueArray(const char *name, const QMatrix4x3 *values, int count);
300     void setUniformValueArray(const char *name, const QMatrix4x4 *values, int count);
301 
302     static bool hasOpenGLShaderPrograms(QOpenGLContext *context = nullptr);
303 
304 private Q_SLOTS:
305     void shaderDestroyed();
306 
307 private:
308     Q_DISABLE_COPY(QOpenGLShaderProgram)
309     Q_DECLARE_PRIVATE(QOpenGLShaderProgram)
310 
311     bool init();
312 };
313 
314 QT_END_NAMESPACE
315 
316 #endif // QT_NO_OPENGL
317 
318 #endif
319