summaryrefslogtreecommitdiffstats
path: root/src/opengl/gl2paintengineex/qglshader_p.h
blob: 6602d9be4d6d57e3b1c7c64e65df548027714df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

/*
Uniform Types in OpenGL ES 2.0
==============================
GL_FLOAT        GLfloat         GLfloat
GL_FLOAT_VEC2   QGLVec2         GLfloat[2]
GL_FLOAT_VEC3   QGLVec3         GLfloat[3]
GL_FLOAT_VEC4   QGLVec4         GLfloat[4]
GL_INT          GLint           GLint
GL_INT_VEC2     QGLIVec2        GLint[2]
GL_INT_VEC3     QGLIVec3        GLint[3]
GL_INT_VEC4     QGLIVec4        GLint[4]
GL_BOOL         GLbool          GLbool
GL_BOOL_VEC2    QGLBVec2        GLbool[2]
GL_BOOL_VEC3    QGLBVec3        GLbool[3]
GL_BOOL_VEC4    QGLBVec4        GLbool[4]
GL_FLOAT_MAT2   QGLMat2         GLfloat[2][2]
GL_FLOAT_MAT3   QGLMat3         GLfloat[3][3]
GL_FLOAT_MAT4   QGLMat4         GLfloat[4][4]
GL_SAMPLER_2D   QGLSampler2D    GLuint
GL_SAMPLER_CUBE QGLSamplerCube  GLuint

Additional Types in Desktop OpenGL 2.0
======================================
SAMPLER_1D,
SAMPLER_3D,
SAMPLER_1D_SHADOW,
SAMPLER_2D_SHADOW.
*/

#include <QtOpenGL>


typedef struct {
    GLfloat a;
    GLfloat b;
} QGLVec2;

typedef struct {
    GLfloat a;
    GLfloat b;
    GLfloat c;
} QGLVec3;

typedef struct {
    GLfloat a;
    GLfloat b;
    GLfloat c;
    GLfloat d;
} QGLVec4;


class QGLShaderProgram;

class QGLShaderPrivate;

class QGLShader : QObject
{
    Q_OBJECT
public:
    enum ShaderType {VertexShader, FragmentShader};

    QGLShader(ShaderType type, const QGLContext* ctx = 0);

    GLuint id();
    void clearSource();
    void addSource(const QLatin1String& newSource);
    bool compile();
    bool isValid();
    QString log();
    const QGLContext* context(); //maybe make private with prog a friend?

private:
    QGLShaderPrivate* d_ptr;
    Q_DECLARE_PRIVATE(QGLShader);

/*
public slots:
    void cleanupGLContextRefs(const QGLContext *context);
*/
};


enum QGLType {
    QGLInvalidType      = 0,
    QGLFloatType        = GL_FLOAT,
    QGLVec2Type         = GL_FLOAT_VEC2,
    QGLVec3Type         = GL_FLOAT_VEC3,
    QGLVec4Type         = GL_FLOAT_VEC4,
    QGLIntType          = GL_INT,
    QGLIVec2Type        = GL_INT_VEC2,
    QGLIVec3Type        = GL_INT_VEC3,
    QGLIVec4Type        = GL_INT_VEC4,
    QGLBoolType         = GL_BOOL,
    QGLBVec2Type        = GL_BOOL_VEC2,
    QGLBVec3Type        = GL_BOOL_VEC3,
    QGLBVec4Type        = GL_BOOL_VEC4,
    QGLMat2Type         = GL_FLOAT_MAT2,
    QGLMat3Type         = GL_FLOAT_MAT3,
    QGLMat4Type         = GL_FLOAT_MAT4,
    QGLSampler2DType    = GL_SAMPLER_2D,
    QGLSamplerCubeType  = GL_SAMPLER_CUBE
};

class QGLUniform
{
public:

    QGLUniform(GLenum glType, GLint location, QGLContext* context)
        : m_id(location), m_type(QGLType(glType)), ctx(context) {}
    
    QGLUniform(); // Called by QMap when there's no match on the name

    QGLType type() const {return m_type;}
    GLuint  id()   const {return m_id;}

    // Seems odd to be const, but it doesn't actually modify any of the
    // class members, only the GL state!
    const QGLUniform& operator=(const GLfloat&) const;

    const QGLUniform& operator=(const QGLVec2&) const;
    const QGLUniform& operator=(const QSizeF&) const;
    const QGLUniform& operator=(const QPointF&) const;

    const QGLUniform& operator=(const QGLVec3&) const;

    const QGLUniform& operator=(const QGLVec4&) const;
    const QGLUniform& operator=(const QColor&) const;

    const QGLUniform& operator=(const GLfloat[2][2]) const;

    const QGLUniform& operator=(const GLfloat[3][3]) const;
    const QGLUniform& operator=(const QTransform&) const;

    const QGLUniform& operator=(const GLfloat[4][4]) const;

    const QGLUniform& operator=(const GLuint&) const;   // sampler2d, specifying a texture unit


protected:
    GLuint      m_id;
    QGLType     m_type;
    QGLContext* ctx;
};

typedef QMap<QString, QGLUniform> QGLUniformList;
typedef QMapIterator<QString, QGLUniform> QGLUniformListIterator;


class QGLVertexAttribute
{
public:
    QGLVertexAttribute(GLenum glType, GLuint location, QGLContext* context)
        : m_id(location), m_type(QGLType(glType)), ctx(context) {}

    QGLVertexAttribute(); // Called by QMap when there's no match on the name

    QGLType type() const {return m_type;}
    GLuint  id()   const {return m_id;}
    void    enable() const;
    void    disable() const;

    const QGLVertexAttribute& operator=(const GLfloat* rhs) const;
    const QGLVertexAttribute& operator=(const QGLVec3* rhs) const;

protected:
    GLuint        m_id;
    QGLType       m_type;
    QGLContext*   ctx;
};

//TODO: Convert into setter overloads on QGLShaderProgram
typedef QMap<QString, QGLVertexAttribute> QGLVertexAttributeList;
typedef QMapIterator<QString, QGLVertexAttribute> QGLVertexAttributeListIterator;



class QGLShaderProgramPrivate;

class QGLShaderProgram : QObject
{
    Q_OBJECT
public:
    QGLShaderProgram(const QGLContext* ctx = 0);

    const QGLUniformList &          uniforms();
    const QGLVertexAttributeList&   vertexAttributes();

    bool addShader(QGLShader* newShader);
    bool removeShader(QGLShader* oldShader);
    bool removeAllShaders();

    bool link();
    QString log();
    bool isValid();
    void use();

    GLuint id();

private:
    QGLShaderProgramPrivate* d_ptr;
    Q_DECLARE_PRIVATE(QGLShaderProgram);

/*
public slots:
    void cleanupGLContextRefs(const QGLContext *context);
*/
};