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