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