summaryrefslogtreecommitdiffstats
path: root/src/opengl/gl2paintengineex/qglshader.cpp
blob: e72247e4bf2a3ecdc3088c1fe41b920285d72c25 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/****************************************************************************
**
** 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$
**
****************************************************************************/

#include "qglshader_p.h"


// Windows needs to resolve OpenGL 2.0 function pointers for each context. The
// QGL OpenGL 2.0 functions are actually macros, which take a "ctx" parameter.
#define Q_CTX   QGLContext* ctx = d->ctx; \
                if (!ctx) \
                    return false; \
                ctx->makeCurrent(); \




class QGLShaderPrivate
{
public:
    QGLShaderPrivate() : shaderId(0), valid(false), ctx(0) {}

    GLuint                  shaderId;
    QString                 source;
    bool                    valid;
    QGLShader::ShaderType   type;
    QGLContext*             ctx;
};


QGLShader::QGLShader(QGLShader::ShaderType type, const QGLContext* ctx)
    : d_ptr(new QGLShaderPrivate)
{
    Q_D(QGLShader);

    if (!ctx)
        ctx = QGLContext::currentContext();

    if (!ctx) {
        qWarning("QGLShader being created without a context");
        return;
    }

    d->ctx = const_cast<QGLContext*>(ctx);
    d->ctx->makeCurrent();

    if (type == QGLShader::FragmentShader)
        d->shaderId = glCreateShader(GL_FRAGMENT_SHADER);
    else
        d->shaderId = glCreateShader(GL_VERTEX_SHADER);

    if (d->shaderId == 0) {
        qWarning("Error creating shader object");
        return;
    }

    d->type = type;
}

GLuint QGLShader::id()
{
    Q_D(QGLShader);
    return d->shaderId;
}

const QGLContext* QGLShader::context()
{
    Q_D(QGLShader);
    return d->ctx;
}

void QGLShader::clearSource()
{
    Q_D(QGLShader);
    d->source.clear();
    d->valid = false;
}

void QGLShader::addSource(const QLatin1String& newSource)
{
    Q_D(QGLShader);
    d->source += newSource;
    d->valid = false;
}


bool QGLShader::compile()
{
    Q_D(QGLShader);

    d->valid = false;

    if (d->source.size() == 0)
        return false;

    const QByteArray src_ba = d->source.toAscii();
    const char* src = src_ba.constData();

    glShaderSource(d->shaderId, 1, &src, 0);

    glCompileShader(d->shaderId);

    GLint shaderCompiled;
    glGetShaderiv(d->shaderId, GL_COMPILE_STATUS, &shaderCompiled);
    if (!shaderCompiled)
        return false;

    d->valid = true;
    return true;
}

bool QGLShader::isValid()
{
    Q_D(QGLShader);
    return d->valid;
}

QString QGLShader::log()
{
    Q_D(QGLShader);

    char*  logData;
    GLint  logSize;
    GLint  logLength;

    glGetShaderiv(d->shaderId, GL_INFO_LOG_LENGTH, &logSize);

    if (!logSize)
        return QString();

    logData = new char[logSize];
    glGetShaderInfoLog(d->shaderId, logSize, &logLength, logData);
    QString result = QString::fromAscii(logData);
    delete [] logData;

    return result;
}












class QGLShaderProgramPrivate
{
public:
    QGLShaderProgramPrivate() : valid(false), programId(0), ctx(0) {}
    void populateVariableLists();

    QVector<QGLShader*>     shaders;
    QGLUniformList          uniforms;
    QGLVertexAttributeList  attributeArrays;
    bool                    valid;
    GLuint                  programId;
    QGLContext*             ctx;
};



void QGLShaderProgramPrivate::populateVariableLists()
{
    attributeArrays.clear();
    uniforms.clear();

    int count;
    int sizeOfNameBuff;
    char* name;
    GLint nameLength;
    GLenum type;
    GLint  size;
    GLint  location;

    glGetProgramiv(programId, GL_ACTIVE_ATTRIBUTES, &count);
    glGetProgramiv(programId, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &sizeOfNameBuff);
    name = new char[sizeOfNameBuff];

    for (int i = 0; i < count; ++i) {
        nameLength = -1;
        glGetActiveAttrib(programId, i, sizeOfNameBuff, &nameLength, &size, &type, name);
        if (nameLength == -1)
            continue;

        location = glGetAttribLocation(programId, name);
        attributeArrays.insert(QString::fromAscii(name), QGLVertexAttribute(type, location, ctx));
    }

    delete [] name;


    glGetProgramiv(programId, GL_ACTIVE_UNIFORMS, &count);
    glGetProgramiv(programId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &sizeOfNameBuff);

    name = new char[sizeOfNameBuff];

    for (int i = 0; i < count; ++i) {
        nameLength = -1;
        glGetActiveUniform(programId, i, sizeOfNameBuff, &nameLength, &size, &type, name);
        if (nameLength == -1)
            continue;

        location = glGetUniformLocation(programId, name);
        uniforms.insert(QString::fromAscii(name), QGLUniform(type, location, ctx));
    }
}


QGLShaderProgram::QGLShaderProgram(const QGLContext* ctx)
    : d_ptr(new QGLShaderProgramPrivate)
{
    Q_D(QGLShaderProgram);
    if (!ctx)
        ctx = QGLContext::currentContext();

    if (!ctx) {
        qWarning("QGLShaderProgram being created without a context");
        return;
    }

    d->ctx = const_cast<QGLContext*>(ctx);
    d->ctx->makeCurrent();

    d->programId = glCreateProgram();

    d->valid = false;
}


const QGLUniformList & QGLShaderProgram::uniforms()
{
    Q_D(QGLShaderProgram);
    return const_cast<const QGLUniformList&>(d->uniforms);
}


const QGLVertexAttributeList& QGLShaderProgram::vertexAttributes()
{
    Q_D(QGLShaderProgram);
    return const_cast<const QGLVertexAttributeList&>(d->attributeArrays);
}


bool QGLShaderProgram::addShader(QGLShader* newShader)
{
    Q_D(QGLShaderProgram);
    if (!newShader || !d->ctx)
        return false;

    if (newShader->context() != d->ctx) {
        qWarning("Shader object's context does not match program's context");
        return false;
    }

    if (!newShader->isValid())
        return false;

    QGLContext* ctx = d->ctx;
    if (!ctx)
        return false;
    ctx->makeCurrent();

    glAttachShader(d->programId, newShader->id());

    d->shaders.append(newShader);
    return true;
}


bool QGLShaderProgram::removeShader(QGLShader* oldShader)
{
    Q_D(QGLShaderProgram);

    int idx = d->shaders.indexOf(oldShader);

    if (idx == -1)
        return false;

    d->shaders.remove(idx);

    QGLContext* ctx = d->ctx;
    if (!ctx)
        return false;
    ctx->makeCurrent();

    glDetachShader(d->programId, oldShader->id());
    return true;
}


bool QGLShaderProgram::removeAllShaders()
{
    Q_D(QGLShaderProgram);

    QGLContext* ctx = d->ctx;
    if (!ctx)
        return false;
    ctx->makeCurrent();

    foreach (QGLShader* shader, d->shaders)
        glDetachShader(d->programId, shader->id());

    d->shaders.clear();
    return true;
}

#include <stdio.h>

bool QGLShaderProgram::link()
{
    Q_D(QGLShaderProgram);

    QGLContext* ctx = d->ctx;
    if (!ctx)
        return false;
    ctx->makeCurrent();

    glLinkProgram(d->programId);


    GLint linked;
    glGetProgramiv(d->programId, GL_LINK_STATUS, &linked);

    if (!linked)
        return false;

    d->populateVariableLists();

    d->valid = true;
    return true;
}

void QGLShaderProgram::use()
{
    Q_D(QGLShaderProgram);
    if (!d->valid)
        return;

    glUseProgram(d->programId);
}


QString QGLShaderProgram::log()
{
    Q_D(QGLShaderProgram);

    QGLContext* ctx = d->ctx;
    if (!ctx)
        return QString();
    ctx->makeCurrent();

    GLint  logSize = -666;
    glGetProgramiv(d->programId, GL_INFO_LOG_LENGTH, &logSize);

    char* logData = new char[logSize];
    GLint  logLength;

    glGetProgramInfoLog(d->programId, logSize, &logLength, logData);

    QString result = QString::fromAscii(logData);
    delete [] logData;

    return result;
}

GLuint QGLShaderProgram::id()
{
    Q_D(QGLShaderProgram);
    return d->programId;
}

/////////////////////////////////////////////////////////////////////////




QGLUniform::QGLUniform()
    : m_id(0), m_type(QGLInvalidType), ctx(0)
{
    qWarning("Unknown uniform! Either the uniform doesn't exist or it was removed at shader link");
}

const QGLUniform& QGLUniform::operator=(const GLfloat& rhs) const
{
    if (m_type != QGLFloatType)
        return *this;

    glUniform1f(m_id, rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QGLVec2& rhs) const
{
    if (m_type != QGLVec2Type)
        return *this;

    glUniform2fv(m_id, 1, (const GLfloat*)&rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QSizeF& rhs) const
{
    if (m_type != QGLVec2Type)
        return *this;

    glUniform2f(m_id, rhs.width(), rhs.height());

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QPointF& rhs) const
{
    if (m_type != QGLVec2Type)
        return *this;

    glUniform2f(m_id, rhs.x(), rhs.y());

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QGLVec3& rhs) const
{
    if (m_type != QGLVec3Type)
        return *this;

    glUniform3fv(m_id, 1, (const GLfloat*)&rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QGLVec4& rhs) const
{
    if (m_type != QGLVec4Type)
        return *this;

    glUniform4fv(m_id, 1, (const GLfloat*)&rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const QColor& rhs) const
{
    if (m_type != QGLVec4Type)
        return *this;

    glUniform4f(m_id, rhs.redF(), rhs.greenF(), rhs.blueF(), rhs.alphaF());

    return *this;
}

const QGLUniform& QGLUniform::operator=(const GLfloat rhs[2][2]) const
{
    if (m_type != QGLMat2Type)
        return *this;

    glUniformMatrix2fv(m_id, 1, GL_FALSE, (GLfloat*)rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const GLfloat rhs[3][3]) const
{
    if (m_type != QGLMat3Type)
        return *this;

    glUniformMatrix3fv(m_id, 1, GL_FALSE, (GLfloat*)rhs);

    return *this;
}

// Transposes ready for GL
const QGLUniform& QGLUniform::operator=(const QTransform& rhs) const
{
    if (m_type != QGLMat3Type)
        return *this;

    GLfloat mat3[3][3] = {
        {rhs.m11(), rhs.m12(), rhs.m13()},
        {rhs.m21(), rhs.m22(), rhs.m23()},
        {rhs.m31(), rhs.m32(), rhs.m33()}
    };

    glUniformMatrix3fv(m_id, 1, GL_FALSE, (GLfloat*)mat3);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const GLfloat rhs[4][4]) const
{
    if (m_type != QGLMat4Type)
        return *this;

    glUniformMatrix4fv(m_id, 1, GL_FALSE, (GLfloat*)rhs);

    return *this;
}

const QGLUniform& QGLUniform::operator=(const GLuint& rhs) const
{
    if ((m_type != QGLSampler2DType) || (m_type != QGLSamplerCubeType))
        return *this;

    glUniform1i(m_id, rhs);

    return *this;
}




/////////////////////////////////////////////////////////////////////////

QGLVertexAttribute::QGLVertexAttribute()
    : m_id(0), m_type(QGLInvalidType), ctx(0)
{
    qWarning("Unknown vertex attribute!");
}

void QGLVertexAttribute::enable() const
{
    glEnableVertexAttribArray(m_id);
}

void QGLVertexAttribute::disable() const
{
    glDisableVertexAttribArray(m_id);
}

// NOTE: Under PC emulation, QGLVec4Type is _always_ returned as the type, so this
// method isn't very useful. I.e. The datatypes are needed to distinguish the different
// sizes for the function signatures.
const QGLVertexAttribute& QGLVertexAttribute::operator=(const GLfloat* rhs) const
{
    int size = -1;
    if (m_type == QGLFloatType)
        size = 1;
    else if (m_type == QGLVec2Type)
        size = 2;
    else if (m_type == QGLVec3Type)
        size = 3;
    else if (m_type == QGLVec4Type)
        size = 4;
    else if (m_type == QGLMat2Type) //### Not sure if this is right for matrix attributes...
        size = 4;
    else if (m_type == QGLMat3Type) //### Not sure if this is right for matrix attributes...
        size = 9;
    else if (m_type == QGLMat4Type) //### Not sure if this is right for matrix attributes...
        size = 16;
    else
        return *this;

    glVertexAttribPointer(m_id, size, GL_FLOAT, GL_FALSE, 0, rhs);

    return *this;
}

const QGLVertexAttribute& QGLVertexAttribute::operator=(const QGLVec3* rhs) const
{
    glVertexAttribPointer(m_id, 3, GL_FLOAT, GL_FALSE, 0, (GLfloat*)rhs);

    return *this;
}