Fix QNetworkReply ioGetFromHttpWithCache test case
[profile/ivi/qtbase.git] / src / opengl / qglshaderprogram.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qglshaderprogram.h"
43 #include "qglextensions_p.h"
44 #include "qgl_p.h"
45 #include <QtCore/private/qobject_p.h>
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qfile.h>
48 #include <QtCore/qvarlengtharray.h>
49 #include <QtCore/qvector.h>
50
51 QT_BEGIN_NAMESPACE
52
53 /*!
54     \class QGLShaderProgram
55     \brief The QGLShaderProgram class allows OpenGL shader programs to be linked and used.
56     \since 4.6
57     \ingroup painting-3D
58
59     \section1 Introduction
60
61     This class supports shader programs written in the OpenGL Shading
62     Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).
63
64     QGLShader and QGLShaderProgram shelter the programmer from the details of
65     compiling and linking vertex and fragment shaders.
66
67     The following example creates a vertex shader program using the
68     supplied source \c{code}.  Once compiled and linked, the shader
69     program is activated in the current QGLContext by calling
70     QGLShaderProgram::bind():
71
72     \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 0
73
74     \section1 Writing portable shaders
75
76     Shader programs can be difficult to reuse across OpenGL implementations
77     because of varying levels of support for standard vertex attributes and
78     uniform variables.  In particular, GLSL/ES lacks all of the
79     standard variables that are present on desktop OpenGL systems:
80     \c{gl_Vertex}, \c{gl_Normal}, \c{gl_Color}, and so on.  Desktop OpenGL
81     lacks the variable qualifiers \c{highp}, \c{mediump}, and \c{lowp}.
82
83     The QGLShaderProgram class makes the process of writing portable shaders
84     easier by prefixing all shader programs with the following lines on
85     desktop OpenGL:
86
87     \code
88     #define highp
89     #define mediump
90     #define lowp
91     \endcode
92
93     This makes it possible to run most GLSL/ES shader programs
94     on desktop systems.  The programmer should restrict themselves
95     to just features that are present in GLSL/ES, and avoid
96     standard variable names that only work on the desktop.
97
98     \section1 Simple shader example
99
100     \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 1
101
102     With the above shader program active, we can draw a green triangle
103     as follows:
104
105     \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 2
106
107     \section1 Binary shaders and programs
108
109     Binary shaders may be specified using \c{glShaderBinary()} on
110     the return value from QGLShader::shaderId().  The QGLShader instance
111     containing the binary can then be added to the shader program with
112     addShader() and linked in the usual fashion with link().
113
114     Binary programs may be specified using \c{glProgramBinaryOES()}
115     on the return value from programId().  Then the application should
116     call link(), which will notice that the program has already been
117     specified and linked, allowing other operations to be performed
118     on the shader program.
119
120     \sa QGLShader
121 */
122
123 /*!
124     \class QGLShader
125     \brief The QGLShader class allows OpenGL shaders to be compiled.
126     \since 4.6
127     \ingroup painting-3D
128
129     This class supports shaders written in the OpenGL Shading Language (GLSL)
130     and in the OpenGL/ES Shading Language (GLSL/ES).
131
132     QGLShader and QGLShaderProgram shelter the programmer from the details of
133     compiling and linking vertex and fragment shaders.
134
135     \sa QGLShaderProgram
136 */
137
138 /*!
139     \enum QGLShader::ShaderTypeBit
140     This enum specifies the type of QGLShader that is being created.
141
142     \value Vertex Vertex shader written in the OpenGL Shading Language (GLSL).
143     \value Fragment Fragment shader written in the OpenGL Shading Language (GLSL).
144     \value Geometry Geometry shaders written in the OpenGL Shading
145            Language (GLSL), based on the GL_EXT_geometry_shader4 extension.
146 */
147
148 #ifndef GL_FRAGMENT_SHADER
149 #define GL_FRAGMENT_SHADER 0x8B30
150 #endif
151 #ifndef GL_VERTEX_SHADER
152 #define GL_VERTEX_SHADER 0x8B31
153 #endif
154 #ifndef GL_COMPILE_STATUS
155 #define GL_COMPILE_STATUS 0x8B81
156 #endif
157 #ifndef GL_LINK_STATUS
158 #define GL_LINK_STATUS 0x8B82
159 #endif
160 #ifndef GL_INFO_LOG_LENGTH
161 #define GL_INFO_LOG_LENGTH 0x8B84
162 #endif
163 #ifndef GL_ACTIVE_UNIFORMS
164 #define GL_ACTIVE_UNIFORMS 0x8B86
165 #endif
166 #ifndef GL_ACTIVE_UNIFORM_MAX_LENGTH
167 #define GL_ACTIVE_UNIFORM_MAX_LENGTH 0x8B87
168 #endif
169 #ifndef GL_ACTIVE_ATTRIBUTES
170 #define GL_ACTIVE_ATTRIBUTES 0x8B89
171 #endif
172 #ifndef GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
173 #define GL_ACTIVE_ATTRIBUTE_MAX_LENGTH 0x8B8A
174 #endif
175 #ifndef GL_CURRENT_VERTEX_ATTRIB
176 #define GL_CURRENT_VERTEX_ATTRIB 0x8626
177 #endif
178 #ifndef GL_SHADER_SOURCE_LENGTH
179 #define GL_SHADER_SOURCE_LENGTH 0x8B88
180 #endif
181 #ifndef GL_SHADER_BINARY_FORMATS
182 #define GL_SHADER_BINARY_FORMATS          0x8DF8
183 #endif
184 #ifndef GL_NUM_SHADER_BINARY_FORMATS
185 #define GL_NUM_SHADER_BINARY_FORMATS      0x8DF9
186 #endif
187
188 class QGLShaderPrivate : public QObjectPrivate
189 {
190     Q_DECLARE_PUBLIC(QGLShader)
191 public:
192     QGLShaderPrivate(const QGLContext *, QGLShader::ShaderType type)
193         : shaderGuard(0)
194         , shaderType(type)
195         , compiled(false)
196     {
197     }
198     ~QGLShaderPrivate();
199
200     QGLSharedResourceGuardBase *shaderGuard;
201     QGLShader::ShaderType shaderType;
202     bool compiled;
203     QString log;
204
205     bool create();
206     bool compile(QGLShader *q);
207     void deleteShader();
208 };
209
210 namespace {
211     void freeShaderFunc(QGLContext *ctx, GLuint id)
212     {
213         Q_UNUSED(ctx);
214         glDeleteShader(id);
215     }
216 }
217
218 #define ctx QGLContext::currentContext()
219
220 QGLShaderPrivate::~QGLShaderPrivate()
221 {
222     if (shaderGuard)
223         shaderGuard->free();
224 }
225
226 bool QGLShaderPrivate::create()
227 {
228     QGLContext *context = const_cast<QGLContext *>(QGLContext::currentContext());
229     if (!context)
230         return false;
231     if (qt_resolve_glsl_extensions(context)) {
232         GLuint shader;
233         if (shaderType == QGLShader::Vertex)
234             shader = glCreateShader(GL_VERTEX_SHADER);
235         else if (shaderType == QGLShader::Geometry)
236             shader = glCreateShader(GL_GEOMETRY_SHADER_EXT);
237         else
238             shader = glCreateShader(GL_FRAGMENT_SHADER);
239         if (!shader) {
240             qWarning() << "QGLShader: could not create shader";
241             return false;
242         }
243         shaderGuard = createSharedResourceGuard(context, shader, freeShaderFunc);
244         return true;
245     } else {
246         return false;
247     }
248 }
249
250 bool QGLShaderPrivate::compile(QGLShader *q)
251 {
252     GLuint shader = shaderGuard ? shaderGuard->id() : 0;
253     if (!shader)
254         return false;
255     glCompileShader(shader);
256     GLint value = 0;
257     glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
258     compiled = (value != 0);
259     value = 0;
260     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &value);
261     if (!compiled && value > 1) {
262         char *logbuf = new char [value];
263         GLint len;
264         glGetShaderInfoLog(shader, value, &len, logbuf);
265         log = QString::fromLatin1(logbuf);
266         QString name = q->objectName();
267
268         const char *types[] = {
269             "Fragment",
270             "Vertex",
271             "Geometry",
272             ""
273         };
274
275         const char *type = types[3];
276         if (shaderType == QGLShader::Fragment)
277             type = types[0];
278         else if (shaderType == QGLShader::Vertex)
279             type = types[1];
280         else if (shaderType == QGLShader::Geometry)
281             type = types[2];
282
283         if (name.isEmpty())
284             qWarning("QGLShader::compile(%s): %s", type, qPrintable(log));
285         else
286             qWarning("QGLShader::compile(%s)[%s]: %s", type, qPrintable(name), qPrintable(log));
287
288         delete [] logbuf;
289     }
290     return compiled;
291 }
292
293 void QGLShaderPrivate::deleteShader()
294 {
295     if (shaderGuard) {
296         shaderGuard->free();
297         shaderGuard = 0;
298     }
299 }
300
301 /*!
302     Constructs a new QGLShader object of the specified \a type
303     and attaches it to \a parent.  If shader programs are not supported,
304     QGLShaderProgram::hasOpenGLShaderPrograms() will return false.
305
306     This constructor is normally followed by a call to compileSourceCode()
307     or compileSourceFile().
308
309     The shader will be associated with the current QGLContext.
310
311     \sa compileSourceCode(), compileSourceFile()
312 */
313 QGLShader::QGLShader(QGLShader::ShaderType type, QObject *parent)
314     : QObject(*new QGLShaderPrivate(QGLContext::currentContext(), type), parent)
315 {
316     Q_D(QGLShader);
317     d->create();
318 }
319
320 /*!
321     Constructs a new QGLShader object of the specified \a type
322     and attaches it to \a parent.  If shader programs are not supported,
323     then QGLShaderProgram::hasOpenGLShaderPrograms() will return false.
324
325     This constructor is normally followed by a call to compileSourceCode()
326     or compileSourceFile().
327
328     The shader will be associated with \a context.
329
330     \sa compileSourceCode(), compileSourceFile()
331 */
332 QGLShader::QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObject *parent)
333     : QObject(*new QGLShaderPrivate(context ? context : QGLContext::currentContext(), type), parent)
334 {
335     Q_D(QGLShader);
336 #ifndef QT_NO_DEBUG
337     if (context && !QGLContext::areSharing(context, QGLContext::currentContext())) {
338         qWarning("QGLShader::QGLShader: \'context\' must be the current context or sharing with it.");
339         return;
340     }
341 #endif
342     d->create();
343 }
344
345 /*!
346     Deletes this shader.  If the shader has been attached to a
347     QGLShaderProgram object, then the actual shader will stay around
348     until the QGLShaderProgram is destroyed.
349 */
350 QGLShader::~QGLShader()
351 {
352 }
353
354 /*!
355     Returns the type of this shader.
356 */
357 QGLShader::ShaderType QGLShader::shaderType() const
358 {
359     Q_D(const QGLShader);
360     return d->shaderType;
361 }
362
363 // The precision qualifiers are useful on OpenGL/ES systems,
364 // but usually not present on desktop systems.  Define the
365 // keywords to empty strings on desktop systems.
366 #if !defined(QT_OPENGL_ES) || defined(QT_OPENGL_FORCE_SHADER_DEFINES)
367 #define QGL_DEFINE_QUALIFIERS 1
368 static const char qualifierDefines[] =
369     "#define lowp\n"
370     "#define mediump\n"
371     "#define highp\n";
372
373 #else
374
375 // The "highp" qualifier doesn't exist in fragment shaders
376 // on all ES platforms.  When it doesn't exist, use "mediump".
377 #define QGL_REDEFINE_HIGHP 1
378 static const char redefineHighp[] =
379     "#ifndef GL_FRAGMENT_PRECISION_HIGH\n"
380     "#define highp mediump\n"
381     "#endif\n";
382 #endif
383
384 /*!
385     Sets the \a source code for this shader and compiles it.
386     Returns true if the source was successfully compiled, false otherwise.
387
388     \sa compileSourceFile()
389 */
390 bool QGLShader::compileSourceCode(const char *source)
391 {
392     Q_D(QGLShader);
393     if (d->shaderGuard && d->shaderGuard->id()) {
394         QVarLengthArray<const char *, 4> src;
395         QVarLengthArray<GLint, 4> srclen;
396         int headerLen = 0;
397         while (source && source[headerLen] == '#') {
398             // Skip #version and #extension directives at the start of
399             // the shader code.  We need to insert the qualifierDefines
400             // and redefineHighp just after them.
401             if (qstrncmp(source + headerLen, "#version", 8) != 0 &&
402                     qstrncmp(source + headerLen, "#extension", 10) != 0) {
403                 break;
404             }
405             while (source[headerLen] != '\0' && source[headerLen] != '\n')
406                 ++headerLen;
407             if (source[headerLen] == '\n')
408                 ++headerLen;
409         }
410         if (headerLen > 0) {
411             src.append(source);
412             srclen.append(GLint(headerLen));
413         }
414 #ifdef QGL_DEFINE_QUALIFIERS
415         src.append(qualifierDefines);
416         srclen.append(GLint(sizeof(qualifierDefines) - 1));
417 #endif
418 #ifdef QGL_REDEFINE_HIGHP
419         if (d->shaderType == Fragment) {
420             src.append(redefineHighp);
421             srclen.append(GLint(sizeof(redefineHighp) - 1));
422         }
423 #endif
424         src.append(source + headerLen);
425         srclen.append(GLint(qstrlen(source + headerLen)));
426         glShaderSource(d->shaderGuard->id(), src.size(), src.data(), srclen.data());
427         return d->compile(this);
428     } else {
429         return false;
430     }
431 }
432
433 /*!
434     \overload
435
436     Sets the \a source code for this shader and compiles it.
437     Returns true if the source was successfully compiled, false otherwise.
438
439     \sa compileSourceFile()
440 */
441 bool QGLShader::compileSourceCode(const QByteArray& source)
442 {
443     return compileSourceCode(source.constData());
444 }
445
446 /*!
447     \overload
448
449     Sets the \a source code for this shader and compiles it.
450     Returns true if the source was successfully compiled, false otherwise.
451
452     \sa compileSourceFile()
453 */
454 bool QGLShader::compileSourceCode(const QString& source)
455 {
456     return compileSourceCode(source.toLatin1().constData());
457 }
458
459 /*!
460     Sets the source code for this shader to the contents of \a fileName
461     and compiles it.  Returns true if the file could be opened and the
462     source compiled, false otherwise.
463
464     \sa compileSourceCode()
465 */
466 bool QGLShader::compileSourceFile(const QString& fileName)
467 {
468     QFile file(fileName);
469     if (!file.open(QFile::ReadOnly)) {
470         qWarning() << "QGLShader: Unable to open file" << fileName;
471         return false;
472     }
473
474     QByteArray contents = file.readAll();
475     return compileSourceCode(contents.constData());
476 }
477
478 /*!
479     Returns the source code for this shader.
480
481     \sa compileSourceCode()
482 */
483 QByteArray QGLShader::sourceCode() const
484 {
485     Q_D(const QGLShader);
486     GLuint shader = d->shaderGuard ? d->shaderGuard->id() : 0;
487     if (!shader)
488         return QByteArray();
489     GLint size = 0;
490     glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &size);
491     if (size <= 0)
492         return QByteArray();
493     GLint len = 0;
494     char *source = new char [size];
495     glGetShaderSource(shader, size, &len, source);
496     QByteArray src(source);
497     delete [] source;
498     return src;
499 }
500
501 /*!
502     Returns true if this shader has been compiled; false otherwise.
503
504     \sa compileSourceCode(), compileSourceFile()
505 */
506 bool QGLShader::isCompiled() const
507 {
508     Q_D(const QGLShader);
509     return d->compiled;
510 }
511
512 /*!
513     Returns the errors and warnings that occurred during the last compile.
514
515     \sa compileSourceCode(), compileSourceFile()
516 */
517 QString QGLShader::log() const
518 {
519     Q_D(const QGLShader);
520     return d->log;
521 }
522
523 /*!
524     Returns the OpenGL identifier associated with this shader.
525
526     \sa QGLShaderProgram::programId()
527 */
528 GLuint QGLShader::shaderId() const
529 {
530     Q_D(const QGLShader);
531     return d->shaderGuard ? d->shaderGuard->id() : 0;
532 }
533
534 #undef ctx
535
536 class QGLShaderProgramPrivate : public QObjectPrivate
537 {
538     Q_DECLARE_PUBLIC(QGLShaderProgram)
539 public:
540     QGLShaderProgramPrivate(const QGLContext *)
541         : programGuard(0)
542         , linked(false)
543         , inited(false)
544         , removingShaders(false)
545         , geometryVertexCount(64)
546         , geometryInputType(0)
547         , geometryOutputType(0)
548     {
549     }
550     ~QGLShaderProgramPrivate();
551
552     QGLSharedResourceGuardBase *programGuard;
553     bool linked;
554     bool inited;
555     bool removingShaders;
556
557     int geometryVertexCount;
558     GLenum geometryInputType;
559     GLenum geometryOutputType;
560
561     QString log;
562     QList<QGLShader *> shaders;
563     QList<QGLShader *> anonShaders;
564
565     bool hasShader(QGLShader::ShaderType type) const;
566 };
567
568 namespace {
569     void freeProgramFunc(QGLContext *ctx, GLuint id)
570     {
571         Q_UNUSED(ctx);
572         glDeleteProgram(id);
573     }
574 }
575
576
577 QGLShaderProgramPrivate::~QGLShaderProgramPrivate()
578 {
579     if (programGuard)
580         programGuard->free();
581 }
582
583 bool QGLShaderProgramPrivate::hasShader(QGLShader::ShaderType type) const
584 {
585     foreach (QGLShader *shader, shaders) {
586         if (shader->shaderType() == type)
587             return true;
588     }
589     return false;
590 }
591
592 #define ctx QGLContext::currentContext()
593
594 /*!
595     Constructs a new shader program and attaches it to \a parent.
596     The program will be invalid until addShader() is called.
597
598     The shader program will be associated with the current QGLContext.
599
600     \sa addShader()
601 */
602 QGLShaderProgram::QGLShaderProgram(QObject *parent)
603     : QObject(*new QGLShaderProgramPrivate(QGLContext::currentContext()), parent)
604 {
605 }
606
607 /*!
608     Constructs a new shader program and attaches it to \a parent.
609     The program will be invalid until addShader() is called.
610
611     The shader program will be associated with \a context.
612
613     \sa addShader()
614 */
615 QGLShaderProgram::QGLShaderProgram(const QGLContext *context, QObject *parent)
616     : QObject(*new QGLShaderProgramPrivate(context), parent)
617 {
618 }
619
620 /*!
621     Deletes this shader program.
622 */
623 QGLShaderProgram::~QGLShaderProgram()
624 {
625 }
626
627 bool QGLShaderProgram::init()
628 {
629     Q_D(QGLShaderProgram);
630     if ((d->programGuard && d->programGuard->id()) || d->inited)
631         return true;
632     d->inited = true;
633     QGLContext *context = const_cast<QGLContext *>(QGLContext::currentContext());
634     if (!context)
635         return false;
636     if (qt_resolve_glsl_extensions(context)) {
637         GLuint program = glCreateProgram();
638         if (!program) {
639             qWarning() << "QGLShaderProgram: could not create shader program";
640             return false;
641         }
642         if (d->programGuard)
643             delete d->programGuard;
644         d->programGuard = createSharedResourceGuard(context, program, freeProgramFunc);
645         return true;
646     } else {
647         qWarning() << "QGLShaderProgram: shader programs are not supported";
648         return false;
649     }
650 }
651
652 /*!
653     Adds a compiled \a shader to this shader program.  Returns true
654     if the shader could be added, or false otherwise.
655
656     Ownership of the \a shader object remains with the caller.
657     It will not be deleted when this QGLShaderProgram instance
658     is deleted.  This allows the caller to add the same shader
659     to multiple shader programs.
660
661     \sa addShaderFromSourceCode(), addShaderFromSourceFile()
662     \sa removeShader(), link(), removeAllShaders()
663 */
664 bool QGLShaderProgram::addShader(QGLShader *shader)
665 {
666     Q_D(QGLShaderProgram);
667     if (!init())
668         return false;
669     if (d->shaders.contains(shader))
670         return true;    // Already added to this shader program.
671     if (d->programGuard && d->programGuard->id() && shader) {
672         if (!shader->d_func()->shaderGuard || !shader->d_func()->shaderGuard->id())
673             return false;
674         if (d->programGuard->group() != shader->d_func()->shaderGuard->group()) {
675             qWarning("QGLShaderProgram::addShader: Program and shader are not associated with same context.");
676             return false;
677         }
678         glAttachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
679         d->linked = false;  // Program needs to be relinked.
680         d->shaders.append(shader);
681         connect(shader, SIGNAL(destroyed()), this, SLOT(shaderDestroyed()));
682         return true;
683     } else {
684         return false;
685     }
686 }
687
688 /*!
689     Compiles \a source as a shader of the specified \a type and
690     adds it to this shader program.  Returns true if compilation
691     was successful, false otherwise.  The compilation errors
692     and warnings will be made available via log().
693
694     This function is intended to be a short-cut for quickly
695     adding vertex and fragment shaders to a shader program without
696     creating an instance of QGLShader first.
697
698     \sa addShader(), addShaderFromSourceFile()
699     \sa removeShader(), link(), log(), removeAllShaders()
700 */
701 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const char *source)
702 {
703     Q_D(QGLShaderProgram);
704     if (!init())
705         return false;
706     QGLShader *shader = new QGLShader(type, this);
707     if (!shader->compileSourceCode(source)) {
708         d->log = shader->log();
709         delete shader;
710         return false;
711     }
712     d->anonShaders.append(shader);
713     return addShader(shader);
714 }
715
716 /*!
717     \overload
718
719     Compiles \a source as a shader of the specified \a type and
720     adds it to this shader program.  Returns true if compilation
721     was successful, false otherwise.  The compilation errors
722     and warnings will be made available via log().
723
724     This function is intended to be a short-cut for quickly
725     adding vertex and fragment shaders to a shader program without
726     creating an instance of QGLShader first.
727
728     \sa addShader(), addShaderFromSourceFile()
729     \sa removeShader(), link(), log(), removeAllShaders()
730 */
731 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const QByteArray& source)
732 {
733     return addShaderFromSourceCode(type, source.constData());
734 }
735
736 /*!
737     \overload
738
739     Compiles \a source as a shader of the specified \a type and
740     adds it to this shader program.  Returns true if compilation
741     was successful, false otherwise.  The compilation errors
742     and warnings will be made available via log().
743
744     This function is intended to be a short-cut for quickly
745     adding vertex and fragment shaders to a shader program without
746     creating an instance of QGLShader first.
747
748     \sa addShader(), addShaderFromSourceFile()
749     \sa removeShader(), link(), log(), removeAllShaders()
750 */
751 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const QString& source)
752 {
753     return addShaderFromSourceCode(type, source.toLatin1().constData());
754 }
755
756 /*!
757     Compiles the contents of \a fileName as a shader of the specified
758     \a type and adds it to this shader program.  Returns true if
759     compilation was successful, false otherwise.  The compilation errors
760     and warnings will be made available via log().
761
762     This function is intended to be a short-cut for quickly
763     adding vertex and fragment shaders to a shader program without
764     creating an instance of QGLShader first.
765
766     \sa addShader(), addShaderFromSourceCode()
767 */
768 bool QGLShaderProgram::addShaderFromSourceFile
769     (QGLShader::ShaderType type, const QString& fileName)
770 {
771     Q_D(QGLShaderProgram);
772     if (!init())
773         return false;
774     QGLShader *shader = new QGLShader(type, this);
775     if (!shader->compileSourceFile(fileName)) {
776         d->log = shader->log();
777         delete shader;
778         return false;
779     }
780     d->anonShaders.append(shader);
781     return addShader(shader);
782 }
783
784 /*!
785     Removes \a shader from this shader program.  The object is not deleted.
786
787     The shader program must be valid in the current QGLContext.
788
789     \sa addShader(), link(), removeAllShaders()
790 */
791 void QGLShaderProgram::removeShader(QGLShader *shader)
792 {
793     Q_D(QGLShaderProgram);
794     if (d->programGuard && d->programGuard->id()
795         && shader && shader->d_func()->shaderGuard)
796     {
797         glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
798     }
799     d->linked = false;  // Program needs to be relinked.
800     if (shader) {
801         d->shaders.removeAll(shader);
802         d->anonShaders.removeAll(shader);
803         disconnect(shader, SIGNAL(destroyed()), this, SLOT(shaderDestroyed()));
804     }
805 }
806
807 /*!
808     Returns a list of all shaders that have been added to this shader
809     program using addShader().
810
811     \sa addShader(), removeShader()
812 */
813 QList<QGLShader *> QGLShaderProgram::shaders() const
814 {
815     Q_D(const QGLShaderProgram);
816     return d->shaders;
817 }
818
819 /*!
820     Removes all of the shaders that were added to this program previously.
821     The QGLShader objects for the shaders will not be deleted if they
822     were constructed externally.  QGLShader objects that are constructed
823     internally by QGLShaderProgram will be deleted.
824
825     \sa addShader(), removeShader()
826 */
827 void QGLShaderProgram::removeAllShaders()
828 {
829     Q_D(QGLShaderProgram);
830     d->removingShaders = true;
831     foreach (QGLShader *shader, d->shaders) {
832         if (d->programGuard && d->programGuard->id()
833             && shader && shader->d_func()->shaderGuard)
834         {
835             glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
836         }
837     }
838     foreach (QGLShader *shader, d->anonShaders) {
839         // Delete shader objects that were created anonymously.
840         delete shader;
841     }
842     d->shaders.clear();
843     d->anonShaders.clear();
844     d->linked = false;  // Program needs to be relinked.
845     d->removingShaders = false;
846 }
847
848 /*!
849     Links together the shaders that were added to this program with
850     addShader().  Returns true if the link was successful or
851     false otherwise.  If the link failed, the error messages can
852     be retrieved with log().
853
854     Subclasses can override this function to initialize attributes
855     and uniform variables for use in specific shader programs.
856
857     If the shader program was already linked, calling this
858     function again will force it to be re-linked.
859
860     \sa addShader(), log()
861 */
862 bool QGLShaderProgram::link()
863 {
864     Q_D(QGLShaderProgram);
865     GLuint program = d->programGuard ? d->programGuard->id() : 0;
866     if (!program)
867         return false;
868
869     GLint value;
870     if (d->shaders.isEmpty()) {
871         // If there are no explicit shaders, then it is possible that the
872         // application added a program binary with glProgramBinaryOES(),
873         // or otherwise populated the shaders itself.  Check to see if the
874         // program is already linked and bail out if so.
875         value = 0;
876         glGetProgramiv(program, GL_LINK_STATUS, &value);
877         d->linked = (value != 0);
878         if (d->linked)
879             return true;
880     }
881
882     // Set up the geometry shader parameters
883     if (glProgramParameteriEXT) {
884         foreach (QGLShader *shader, d->shaders) {
885             if (shader->shaderType() & QGLShader::Geometry) {
886                 glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT,
887                                        d->geometryInputType);
888                 glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT,
889                                        d->geometryOutputType);
890                 glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT,
891                                        d->geometryVertexCount);
892                 break;
893             }
894         }
895     }
896
897     glLinkProgram(program);
898     value = 0;
899     glGetProgramiv(program, GL_LINK_STATUS, &value);
900     d->linked = (value != 0);
901     value = 0;
902     glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
903     d->log = QString();
904     if (value > 1) {
905         char *logbuf = new char [value];
906         GLint len;
907         glGetProgramInfoLog(program, value, &len, logbuf);
908         d->log = QString::fromLatin1(logbuf);
909         QString name = objectName();
910         if (name.isEmpty())
911             qWarning() << "QGLShader::link:" << d->log;
912         else
913             qWarning() << "QGLShader::link[" << name << "]:" << d->log;
914         delete [] logbuf;
915     }
916     return d->linked;
917 }
918
919 /*!
920     Returns true if this shader program has been linked; false otherwise.
921
922     \sa link()
923 */
924 bool QGLShaderProgram::isLinked() const
925 {
926     Q_D(const QGLShaderProgram);
927     return d->linked;
928 }
929
930 /*!
931     Returns the errors and warnings that occurred during the last link()
932     or addShader() with explicitly specified source code.
933
934     \sa link()
935 */
936 QString QGLShaderProgram::log() const
937 {
938     Q_D(const QGLShaderProgram);
939     return d->log;
940 }
941
942 /*!
943     Binds this shader program to the active QGLContext and makes
944     it the current shader program.  Any previously bound shader program
945     is released.  This is equivalent to calling \c{glUseProgram()} on
946     programId().  Returns true if the program was successfully bound;
947     false otherwise.  If the shader program has not yet been linked,
948     or it needs to be re-linked, this function will call link().
949
950     \sa link(), release()
951 */
952 bool QGLShaderProgram::bind()
953 {
954     Q_D(QGLShaderProgram);
955     GLuint program = d->programGuard ? d->programGuard->id() : 0;
956     if (!program)
957         return false;
958     if (!d->linked && !link())
959         return false;
960 #ifndef QT_NO_DEBUG
961     if (d->programGuard->group() != QOpenGLContextGroup::currentContextGroup()) {
962         qWarning("QGLShaderProgram::bind: program is not valid in the current context.");
963         return false;
964     }
965 #endif
966     glUseProgram(program);
967     return true;
968 }
969
970 #undef ctx
971 #define ctx QGLContext::currentContext()
972
973 /*!
974     Releases the active shader program from the current QGLContext.
975     This is equivalent to calling \c{glUseProgram(0)}.
976
977     \sa bind()
978 */
979 void QGLShaderProgram::release()
980 {
981 #ifndef QT_NO_DEBUG
982     Q_D(QGLShaderProgram);
983     if (d->programGuard && d->programGuard->group() != QOpenGLContextGroup::currentContextGroup())
984         qWarning("QGLShaderProgram::release: program is not valid in the current context.");
985 #endif
986 #if defined(QT_OPENGL_ES_2)
987     glUseProgram(0);
988 #else
989     if (glUseProgram)
990         glUseProgram(0);
991 #endif
992 }
993
994 /*!
995     Returns the OpenGL identifier associated with this shader program.
996
997     \sa QGLShader::shaderId()
998 */
999 GLuint QGLShaderProgram::programId() const
1000 {
1001     Q_D(const QGLShaderProgram);
1002     GLuint id = d->programGuard ? d->programGuard->id() : 0;
1003     if (id)
1004         return id;
1005
1006     // Create the identifier if we don't have one yet.  This is for
1007     // applications that want to create the attached shader configuration
1008     // themselves, particularly those using program binaries.
1009     if (!const_cast<QGLShaderProgram *>(this)->init())
1010         return 0;
1011     return d->programGuard ? d->programGuard->id() : 0;
1012 }
1013
1014 /*!
1015     Binds the attribute \a name to the specified \a location.  This
1016     function can be called before or after the program has been linked.
1017     Any attributes that have not been explicitly bound when the program
1018     is linked will be assigned locations automatically.
1019
1020     When this function is called after the program has been linked,
1021     the program will need to be relinked for the change to take effect.
1022
1023     \sa attributeLocation()
1024 */
1025 void QGLShaderProgram::bindAttributeLocation(const char *name, int location)
1026 {
1027     Q_D(QGLShaderProgram);
1028     if (!init() || !d->programGuard || !d->programGuard->id())
1029         return;
1030     glBindAttribLocation(d->programGuard->id(), location, name);
1031     d->linked = false;  // Program needs to be relinked.
1032 }
1033
1034 /*!
1035     \overload
1036
1037     Binds the attribute \a name to the specified \a location.  This
1038     function can be called before or after the program has been linked.
1039     Any attributes that have not been explicitly bound when the program
1040     is linked will be assigned locations automatically.
1041
1042     When this function is called after the program has been linked,
1043     the program will need to be relinked for the change to take effect.
1044
1045     \sa attributeLocation()
1046 */
1047 void QGLShaderProgram::bindAttributeLocation(const QByteArray& name, int location)
1048 {
1049     bindAttributeLocation(name.constData(), location);
1050 }
1051
1052 /*!
1053     \overload
1054
1055     Binds the attribute \a name to the specified \a location.  This
1056     function can be called before or after the program has been linked.
1057     Any attributes that have not been explicitly bound when the program
1058     is linked will be assigned locations automatically.
1059
1060     When this function is called after the program has been linked,
1061     the program will need to be relinked for the change to take effect.
1062
1063     \sa attributeLocation()
1064 */
1065 void QGLShaderProgram::bindAttributeLocation(const QString& name, int location)
1066 {
1067     bindAttributeLocation(name.toLatin1().constData(), location);
1068 }
1069
1070 /*!
1071     Returns the location of the attribute \a name within this shader
1072     program's parameter list.  Returns -1 if \a name is not a valid
1073     attribute for this shader program.
1074
1075     \sa uniformLocation(), bindAttributeLocation()
1076 */
1077 int QGLShaderProgram::attributeLocation(const char *name) const
1078 {
1079     Q_D(const QGLShaderProgram);
1080     if (d->linked && d->programGuard && d->programGuard->id()) {
1081         return glGetAttribLocation(d->programGuard->id(), name);
1082     } else {
1083         qWarning() << "QGLShaderProgram::attributeLocation(" << name
1084                    << "): shader program is not linked";
1085         return -1;
1086     }
1087 }
1088
1089 /*!
1090     \overload
1091
1092     Returns the location of the attribute \a name within this shader
1093     program's parameter list.  Returns -1 if \a name is not a valid
1094     attribute for this shader program.
1095
1096     \sa uniformLocation(), bindAttributeLocation()
1097 */
1098 int QGLShaderProgram::attributeLocation(const QByteArray& name) const
1099 {
1100     return attributeLocation(name.constData());
1101 }
1102
1103 /*!
1104     \overload
1105
1106     Returns the location of the attribute \a name within this shader
1107     program's parameter list.  Returns -1 if \a name is not a valid
1108     attribute for this shader program.
1109
1110     \sa uniformLocation(), bindAttributeLocation()
1111 */
1112 int QGLShaderProgram::attributeLocation(const QString& name) const
1113 {
1114     return attributeLocation(name.toLatin1().constData());
1115 }
1116
1117 /*!
1118     Sets the attribute at \a location in the current context to \a value.
1119
1120     \sa setUniformValue()
1121 */
1122 void QGLShaderProgram::setAttributeValue(int location, GLfloat value)
1123 {
1124     Q_D(QGLShaderProgram);
1125     Q_UNUSED(d);
1126     if (location != -1)
1127         glVertexAttrib1fv(location, &value);
1128 }
1129
1130 /*!
1131     \overload
1132
1133     Sets the attribute called \a name in the current context to \a value.
1134
1135     \sa setUniformValue()
1136 */
1137 void QGLShaderProgram::setAttributeValue(const char *name, GLfloat value)
1138 {
1139     setAttributeValue(attributeLocation(name), value);
1140 }
1141
1142 /*!
1143     Sets the attribute at \a location in the current context to
1144     the 2D vector (\a x, \a y).
1145
1146     \sa setUniformValue()
1147 */
1148 void QGLShaderProgram::setAttributeValue(int location, GLfloat x, GLfloat y)
1149 {
1150     Q_D(QGLShaderProgram);
1151     Q_UNUSED(d);
1152     if (location != -1) {
1153         GLfloat values[2] = {x, y};
1154         glVertexAttrib2fv(location, values);
1155     }
1156 }
1157
1158 /*!
1159     \overload
1160
1161     Sets the attribute called \a name in the current context to
1162     the 2D vector (\a x, \a y).
1163
1164     \sa setUniformValue()
1165 */
1166 void QGLShaderProgram::setAttributeValue(const char *name, GLfloat x, GLfloat y)
1167 {
1168     setAttributeValue(attributeLocation(name), x, y);
1169 }
1170
1171 /*!
1172     Sets the attribute at \a location in the current context to
1173     the 3D vector (\a x, \a y, \a z).
1174
1175     \sa setUniformValue()
1176 */
1177 void QGLShaderProgram::setAttributeValue
1178         (int location, GLfloat x, GLfloat y, GLfloat z)
1179 {
1180     Q_D(QGLShaderProgram);
1181     Q_UNUSED(d);
1182     if (location != -1) {
1183         GLfloat values[3] = {x, y, z};
1184         glVertexAttrib3fv(location, values);
1185     }
1186 }
1187
1188 /*!
1189     \overload
1190
1191     Sets the attribute called \a name in the current context to
1192     the 3D vector (\a x, \a y, \a z).
1193
1194     \sa setUniformValue()
1195 */
1196 void QGLShaderProgram::setAttributeValue
1197         (const char *name, GLfloat x, GLfloat y, GLfloat z)
1198 {
1199     setAttributeValue(attributeLocation(name), x, y, z);
1200 }
1201
1202 /*!
1203     Sets the attribute at \a location in the current context to
1204     the 4D vector (\a x, \a y, \a z, \a w).
1205
1206     \sa setUniformValue()
1207 */
1208 void QGLShaderProgram::setAttributeValue
1209         (int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1210 {
1211     Q_D(QGLShaderProgram);
1212     Q_UNUSED(d);
1213     if (location != -1) {
1214         GLfloat values[4] = {x, y, z, w};
1215         glVertexAttrib4fv(location, values);
1216     }
1217 }
1218
1219 /*!
1220     \overload
1221
1222     Sets the attribute called \a name in the current context to
1223     the 4D vector (\a x, \a y, \a z, \a w).
1224
1225     \sa setUniformValue()
1226 */
1227 void QGLShaderProgram::setAttributeValue
1228         (const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1229 {
1230     setAttributeValue(attributeLocation(name), x, y, z, w);
1231 }
1232
1233 /*!
1234     Sets the attribute at \a location in the current context to \a value.
1235
1236     \sa setUniformValue()
1237 */
1238 void QGLShaderProgram::setAttributeValue(int location, const QVector2D& value)
1239 {
1240     Q_D(QGLShaderProgram);
1241     Q_UNUSED(d);
1242     if (location != -1)
1243         glVertexAttrib2fv(location, reinterpret_cast<const GLfloat *>(&value));
1244 }
1245
1246 /*!
1247     \overload
1248
1249     Sets the attribute called \a name in the current context to \a value.
1250
1251     \sa setUniformValue()
1252 */
1253 void QGLShaderProgram::setAttributeValue(const char *name, const QVector2D& value)
1254 {
1255     setAttributeValue(attributeLocation(name), value);
1256 }
1257
1258 /*!
1259     Sets the attribute at \a location in the current context to \a value.
1260
1261     \sa setUniformValue()
1262 */
1263 void QGLShaderProgram::setAttributeValue(int location, const QVector3D& value)
1264 {
1265     Q_D(QGLShaderProgram);
1266     Q_UNUSED(d);
1267     if (location != -1)
1268         glVertexAttrib3fv(location, reinterpret_cast<const GLfloat *>(&value));
1269 }
1270
1271 /*!
1272     \overload
1273
1274     Sets the attribute called \a name in the current context to \a value.
1275
1276     \sa setUniformValue()
1277 */
1278 void QGLShaderProgram::setAttributeValue(const char *name, const QVector3D& value)
1279 {
1280     setAttributeValue(attributeLocation(name), value);
1281 }
1282
1283 /*!
1284     Sets the attribute at \a location in the current context to \a value.
1285
1286     \sa setUniformValue()
1287 */
1288 void QGLShaderProgram::setAttributeValue(int location, const QVector4D& value)
1289 {
1290     Q_D(QGLShaderProgram);
1291     Q_UNUSED(d);
1292     if (location != -1)
1293         glVertexAttrib4fv(location, reinterpret_cast<const GLfloat *>(&value));
1294 }
1295
1296 /*!
1297     \overload
1298
1299     Sets the attribute called \a name in the current context to \a value.
1300
1301     \sa setUniformValue()
1302 */
1303 void QGLShaderProgram::setAttributeValue(const char *name, const QVector4D& value)
1304 {
1305     setAttributeValue(attributeLocation(name), value);
1306 }
1307
1308 /*!
1309     Sets the attribute at \a location in the current context to \a value.
1310
1311     \sa setUniformValue()
1312 */
1313 void QGLShaderProgram::setAttributeValue(int location, const QColor& value)
1314 {
1315     Q_D(QGLShaderProgram);
1316     Q_UNUSED(d);
1317     if (location != -1) {
1318         GLfloat values[4] = {GLfloat(value.redF()), GLfloat(value.greenF()),
1319                              GLfloat(value.blueF()), GLfloat(value.alphaF())};
1320         glVertexAttrib4fv(location, values);
1321     }
1322 }
1323
1324 /*!
1325     \overload
1326
1327     Sets the attribute called \a name in the current context to \a value.
1328
1329     \sa setUniformValue()
1330 */
1331 void QGLShaderProgram::setAttributeValue(const char *name, const QColor& value)
1332 {
1333     setAttributeValue(attributeLocation(name), value);
1334 }
1335
1336 /*!
1337     Sets the attribute at \a location in the current context to the
1338     contents of \a values, which contains \a columns elements, each
1339     consisting of \a rows elements.  The \a rows value should be
1340     1, 2, 3, or 4.  This function is typically used to set matrix
1341     values and column vectors.
1342
1343     \sa setUniformValue()
1344 */
1345 void QGLShaderProgram::setAttributeValue
1346     (int location, const GLfloat *values, int columns, int rows)
1347 {
1348     Q_D(QGLShaderProgram);
1349     Q_UNUSED(d);
1350     if (rows < 1 || rows > 4) {
1351         qWarning() << "QGLShaderProgram::setAttributeValue: rows" << rows << "not supported";
1352         return;
1353     }
1354     if (location != -1) {
1355         while (columns-- > 0) {
1356             if (rows == 1)
1357                 glVertexAttrib1fv(location, values);
1358             else if (rows == 2)
1359                 glVertexAttrib2fv(location, values);
1360             else if (rows == 3)
1361                 glVertexAttrib3fv(location, values);
1362             else
1363                 glVertexAttrib4fv(location, values);
1364             values += rows;
1365             ++location;
1366         }
1367     }
1368 }
1369
1370 /*!
1371     \overload
1372
1373     Sets the attribute called \a name in the current context to the
1374     contents of \a values, which contains \a columns elements, each
1375     consisting of \a rows elements.  The \a rows value should be
1376     1, 2, 3, or 4.  This function is typically used to set matrix
1377     values and column vectors.
1378
1379     \sa setUniformValue()
1380 */
1381 void QGLShaderProgram::setAttributeValue
1382     (const char *name, const GLfloat *values, int columns, int rows)
1383 {
1384     setAttributeValue(attributeLocation(name), values, columns, rows);
1385 }
1386
1387 /*!
1388     Sets an array of vertex \a values on the attribute at \a location
1389     in this shader program.  The \a tupleSize indicates the number of
1390     components per vertex (1, 2, 3, or 4), and the \a stride indicates
1391     the number of bytes between vertices.  A default \a stride value
1392     of zero indicates that the vertices are densely packed in \a values.
1393
1394     The array will become active when enableAttributeArray() is called
1395     on the \a location.  Otherwise the value specified with
1396     setAttributeValue() for \a location will be used.
1397
1398     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1399     \sa disableAttributeArray()
1400 */
1401 void QGLShaderProgram::setAttributeArray
1402     (int location, const GLfloat *values, int tupleSize, int stride)
1403 {
1404     Q_D(QGLShaderProgram);
1405     Q_UNUSED(d);
1406     if (location != -1) {
1407         glVertexAttribPointer(location, tupleSize, GL_FLOAT, GL_FALSE,
1408                               stride, values);
1409     }
1410 }
1411
1412 /*!
1413     Sets an array of 2D vertex \a values on the attribute at \a location
1414     in this shader program.  The \a stride indicates the number of bytes
1415     between vertices.  A default \a stride value of zero indicates that
1416     the vertices are densely packed in \a values.
1417
1418     The array will become active when enableAttributeArray() is called
1419     on the \a location.  Otherwise the value specified with
1420     setAttributeValue() for \a location will be used.
1421
1422     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1423     \sa disableAttributeArray()
1424 */
1425 void QGLShaderProgram::setAttributeArray
1426         (int location, const QVector2D *values, int stride)
1427 {
1428     Q_D(QGLShaderProgram);
1429     Q_UNUSED(d);
1430     if (location != -1) {
1431         glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE,
1432                               stride, values);
1433     }
1434 }
1435
1436 /*!
1437     Sets an array of 3D vertex \a values on the attribute at \a location
1438     in this shader program.  The \a stride indicates the number of bytes
1439     between vertices.  A default \a stride value of zero indicates that
1440     the vertices are densely packed in \a values.
1441
1442     The array will become active when enableAttributeArray() is called
1443     on the \a location.  Otherwise the value specified with
1444     setAttributeValue() for \a location will be used.
1445
1446     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1447     \sa disableAttributeArray()
1448 */
1449 void QGLShaderProgram::setAttributeArray
1450         (int location, const QVector3D *values, int stride)
1451 {
1452     Q_D(QGLShaderProgram);
1453     Q_UNUSED(d);
1454     if (location != -1) {
1455         glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
1456                               stride, values);
1457     }
1458 }
1459
1460 /*!
1461     Sets an array of 4D vertex \a values on the attribute at \a location
1462     in this shader program.  The \a stride indicates the number of bytes
1463     between vertices.  A default \a stride value of zero indicates that
1464     the vertices are densely packed in \a values.
1465
1466     The array will become active when enableAttributeArray() is called
1467     on the \a location.  Otherwise the value specified with
1468     setAttributeValue() for \a location will be used.
1469
1470     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1471     \sa disableAttributeArray()
1472 */
1473 void QGLShaderProgram::setAttributeArray
1474         (int location, const QVector4D *values, int stride)
1475 {
1476     Q_D(QGLShaderProgram);
1477     Q_UNUSED(d);
1478     if (location != -1) {
1479         glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE,
1480                               stride, values);
1481     }
1482 }
1483
1484 /*!
1485     Sets an array of vertex \a values on the attribute at \a location
1486     in this shader program.  The \a stride indicates the number of bytes
1487     between vertices.  A default \a stride value of zero indicates that
1488     the vertices are densely packed in \a values.
1489
1490     The \a type indicates the type of elements in the \a values array,
1491     usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a tupleSize
1492     indicates the number of components per vertex: 1, 2, 3, or 4.
1493
1494     The array will become active when enableAttributeArray() is called
1495     on the \a location.  Otherwise the value specified with
1496     setAttributeValue() for \a location will be used.
1497
1498     The setAttributeBuffer() function can be used to set the attribute
1499     array to an offset within a vertex buffer.
1500
1501     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1502     \sa disableAttributeArray(), setAttributeBuffer()
1503     \since 4.7
1504 */
1505 void QGLShaderProgram::setAttributeArray
1506     (int location, GLenum type, const void *values, int tupleSize, int stride)
1507 {
1508     Q_D(QGLShaderProgram);
1509     Q_UNUSED(d);
1510     if (location != -1) {
1511         glVertexAttribPointer(location, tupleSize, type, GL_TRUE,
1512                               stride, values);
1513     }
1514 }
1515
1516 /*!
1517     \overload
1518
1519     Sets an array of vertex \a values on the attribute called \a name
1520     in this shader program.  The \a tupleSize indicates the number of
1521     components per vertex (1, 2, 3, or 4), and the \a stride indicates
1522     the number of bytes between vertices.  A default \a stride value
1523     of zero indicates that the vertices are densely packed in \a values.
1524
1525     The array will become active when enableAttributeArray() is called
1526     on \a name.  Otherwise the value specified with setAttributeValue()
1527     for \a name will be used.
1528
1529     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1530     \sa disableAttributeArray()
1531 */
1532 void QGLShaderProgram::setAttributeArray
1533     (const char *name, const GLfloat *values, int tupleSize, int stride)
1534 {
1535     setAttributeArray(attributeLocation(name), values, tupleSize, stride);
1536 }
1537
1538 /*!
1539     \overload
1540
1541     Sets an array of 2D vertex \a values on the attribute called \a name
1542     in this shader program.  The \a stride indicates the number of bytes
1543     between vertices.  A default \a stride value of zero indicates that
1544     the vertices are densely packed in \a values.
1545
1546     The array will become active when enableAttributeArray() is called
1547     on \a name.  Otherwise the value specified with setAttributeValue()
1548     for \a name will be used.
1549
1550     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1551     \sa disableAttributeArray()
1552 */
1553 void QGLShaderProgram::setAttributeArray
1554         (const char *name, const QVector2D *values, int stride)
1555 {
1556     setAttributeArray(attributeLocation(name), values, stride);
1557 }
1558
1559 /*!
1560     \overload
1561
1562     Sets an array of 3D vertex \a values on the attribute called \a name
1563     in this shader program.  The \a stride indicates the number of bytes
1564     between vertices.  A default \a stride value of zero indicates that
1565     the vertices are densely packed in \a values.
1566
1567     The array will become active when enableAttributeArray() is called
1568     on \a name.  Otherwise the value specified with setAttributeValue()
1569     for \a name will be used.
1570
1571     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1572     \sa disableAttributeArray()
1573 */
1574 void QGLShaderProgram::setAttributeArray
1575         (const char *name, const QVector3D *values, int stride)
1576 {
1577     setAttributeArray(attributeLocation(name), values, stride);
1578 }
1579
1580 /*!
1581     \overload
1582
1583     Sets an array of 4D vertex \a values on the attribute called \a name
1584     in this shader program.  The \a stride indicates the number of bytes
1585     between vertices.  A default \a stride value of zero indicates that
1586     the vertices are densely packed in \a values.
1587
1588     The array will become active when enableAttributeArray() is called
1589     on \a name.  Otherwise the value specified with setAttributeValue()
1590     for \a name will be used.
1591
1592     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1593     \sa disableAttributeArray()
1594 */
1595 void QGLShaderProgram::setAttributeArray
1596         (const char *name, const QVector4D *values, int stride)
1597 {
1598     setAttributeArray(attributeLocation(name), values, stride);
1599 }
1600
1601 /*!
1602     \overload
1603
1604     Sets an array of vertex \a values on the attribute called \a name
1605     in this shader program.  The \a stride indicates the number of bytes
1606     between vertices.  A default \a stride value of zero indicates that
1607     the vertices are densely packed in \a values.
1608
1609     The \a type indicates the type of elements in the \a values array,
1610     usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a tupleSize
1611     indicates the number of components per vertex: 1, 2, 3, or 4.
1612
1613     The array will become active when enableAttributeArray() is called
1614     on the \a name.  Otherwise the value specified with
1615     setAttributeValue() for \a name will be used.
1616
1617     The setAttributeBuffer() function can be used to set the attribute
1618     array to an offset within a vertex buffer.
1619
1620     \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1621     \sa disableAttributeArray(), setAttributeBuffer()
1622     \since 4.7
1623 */
1624 void QGLShaderProgram::setAttributeArray
1625     (const char *name, GLenum type, const void *values, int tupleSize, int stride)
1626 {
1627     setAttributeArray(attributeLocation(name), type, values, tupleSize, stride);
1628 }
1629
1630 /*!
1631     Sets an array of vertex values on the attribute at \a location in
1632     this shader program, starting at a specific \a offset in the
1633     currently bound vertex buffer.  The \a stride indicates the number
1634     of bytes between vertices.  A default \a stride value of zero
1635     indicates that the vertices are densely packed in the value array.
1636
1637     The \a type indicates the type of elements in the vertex value
1638     array, usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a
1639     tupleSize indicates the number of components per vertex: 1, 2, 3,
1640     or 4.
1641
1642     The array will become active when enableAttributeArray() is called
1643     on the \a location.  Otherwise the value specified with
1644     setAttributeValue() for \a location will be used.
1645
1646     \sa setAttributeArray()
1647     \since 4.7
1648 */
1649 void QGLShaderProgram::setAttributeBuffer
1650     (int location, GLenum type, int offset, int tupleSize, int stride)
1651 {
1652     Q_D(QGLShaderProgram);
1653     Q_UNUSED(d);
1654     if (location != -1) {
1655         glVertexAttribPointer(location, tupleSize, type, GL_TRUE, stride,
1656                               reinterpret_cast<const void *>(offset));
1657     }
1658 }
1659
1660 /*!
1661     \overload
1662
1663     Sets an array of vertex values on the attribute called \a name
1664     in this shader program, starting at a specific \a offset in the
1665     currently bound vertex buffer.  The \a stride indicates the number
1666     of bytes between vertices.  A default \a stride value of zero
1667     indicates that the vertices are densely packed in the value array.
1668
1669     The \a type indicates the type of elements in the vertex value
1670     array, usually \c{GL_FLOAT}, \c{GL_UNSIGNED_BYTE}, etc.  The \a
1671     tupleSize indicates the number of components per vertex: 1, 2, 3,
1672     or 4.
1673
1674     The array will become active when enableAttributeArray() is called
1675     on the \a name.  Otherwise the value specified with
1676     setAttributeValue() for \a name will be used.
1677
1678     \sa setAttributeArray()
1679     \since 4.7
1680 */
1681 void QGLShaderProgram::setAttributeBuffer
1682     (const char *name, GLenum type, int offset, int tupleSize, int stride)
1683 {
1684     setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride);
1685 }
1686
1687 /*!
1688     Enables the vertex array at \a location in this shader program
1689     so that the value set by setAttributeArray() on \a location
1690     will be used by the shader program.
1691
1692     \sa disableAttributeArray(), setAttributeArray(), setAttributeValue()
1693     \sa setUniformValue()
1694 */
1695 void QGLShaderProgram::enableAttributeArray(int location)
1696 {
1697     Q_D(QGLShaderProgram);
1698     Q_UNUSED(d);
1699     if (location != -1)
1700         glEnableVertexAttribArray(location);
1701 }
1702
1703 /*!
1704     \overload
1705
1706     Enables the vertex array called \a name in this shader program
1707     so that the value set by setAttributeArray() on \a name
1708     will be used by the shader program.
1709
1710     \sa disableAttributeArray(), setAttributeArray(), setAttributeValue()
1711     \sa setUniformValue()
1712 */
1713 void QGLShaderProgram::enableAttributeArray(const char *name)
1714 {
1715     enableAttributeArray(attributeLocation(name));
1716 }
1717
1718 /*!
1719     Disables the vertex array at \a location in this shader program
1720     that was enabled by a previous call to enableAttributeArray().
1721
1722     \sa enableAttributeArray(), setAttributeArray(), setAttributeValue()
1723     \sa setUniformValue()
1724 */
1725 void QGLShaderProgram::disableAttributeArray(int location)
1726 {
1727     Q_D(QGLShaderProgram);
1728     Q_UNUSED(d);
1729     if (location != -1)
1730         glDisableVertexAttribArray(location);
1731 }
1732
1733 /*!
1734     \overload
1735
1736     Disables the vertex array called \a name in this shader program
1737     that was enabled by a previous call to enableAttributeArray().
1738
1739     \sa enableAttributeArray(), setAttributeArray(), setAttributeValue()
1740     \sa setUniformValue()
1741 */
1742 void QGLShaderProgram::disableAttributeArray(const char *name)
1743 {
1744     disableAttributeArray(attributeLocation(name));
1745 }
1746
1747 /*!
1748     Returns the location of the uniform variable \a name within this shader
1749     program's parameter list.  Returns -1 if \a name is not a valid
1750     uniform variable for this shader program.
1751
1752     \sa attributeLocation()
1753 */
1754 int QGLShaderProgram::uniformLocation(const char *name) const
1755 {
1756     Q_D(const QGLShaderProgram);
1757     Q_UNUSED(d);
1758     if (d->linked && d->programGuard && d->programGuard->id()) {
1759         return glGetUniformLocation(d->programGuard->id(), name);
1760     } else {
1761         qWarning() << "QGLShaderProgram::uniformLocation(" << name
1762                    << "): shader program is not linked";
1763         return -1;
1764     }
1765 }
1766
1767 /*!
1768     \overload
1769
1770     Returns the location of the uniform variable \a name within this shader
1771     program's parameter list.  Returns -1 if \a name is not a valid
1772     uniform variable for this shader program.
1773
1774     \sa attributeLocation()
1775 */
1776 int QGLShaderProgram::uniformLocation(const QByteArray& name) const
1777 {
1778     return uniformLocation(name.constData());
1779 }
1780
1781 /*!
1782     \overload
1783
1784     Returns the location of the uniform variable \a name within this shader
1785     program's parameter list.  Returns -1 if \a name is not a valid
1786     uniform variable for this shader program.
1787
1788     \sa attributeLocation()
1789 */
1790 int QGLShaderProgram::uniformLocation(const QString& name) const
1791 {
1792     return uniformLocation(name.toLatin1().constData());
1793 }
1794
1795 /*!
1796     Sets the uniform variable at \a location in the current context to \a value.
1797
1798     \sa setAttributeValue()
1799 */
1800 void QGLShaderProgram::setUniformValue(int location, GLfloat value)
1801 {
1802     Q_D(QGLShaderProgram);
1803     Q_UNUSED(d);
1804     if (location != -1)
1805         glUniform1fv(location, 1, &value);
1806 }
1807
1808 /*!
1809     \overload
1810
1811     Sets the uniform variable called \a name in the current context
1812     to \a value.
1813
1814     \sa setAttributeValue()
1815 */
1816 void QGLShaderProgram::setUniformValue(const char *name, GLfloat value)
1817 {
1818     setUniformValue(uniformLocation(name), value);
1819 }
1820
1821 /*!
1822     Sets the uniform variable at \a location in the current context to \a value.
1823
1824     \sa setAttributeValue()
1825 */
1826 void QGLShaderProgram::setUniformValue(int location, GLint value)
1827 {
1828     Q_D(QGLShaderProgram);
1829     Q_UNUSED(d);
1830     if (location != -1)
1831         glUniform1i(location, value);
1832 }
1833
1834 /*!
1835     \overload
1836
1837     Sets the uniform variable called \a name in the current context
1838     to \a value.
1839
1840     \sa setAttributeValue()
1841 */
1842 void QGLShaderProgram::setUniformValue(const char *name, GLint value)
1843 {
1844     setUniformValue(uniformLocation(name), value);
1845 }
1846
1847 /*!
1848     Sets the uniform variable at \a location in the current context to \a value.
1849     This function should be used when setting sampler values.
1850
1851     \sa setAttributeValue()
1852 */
1853 void QGLShaderProgram::setUniformValue(int location, GLuint value)
1854 {
1855     Q_D(QGLShaderProgram);
1856     Q_UNUSED(d);
1857     if (location != -1)
1858         glUniform1i(location, value);
1859 }
1860
1861 /*!
1862     \overload
1863
1864     Sets the uniform variable called \a name in the current context
1865     to \a value.  This function should be used when setting sampler values.
1866
1867     \sa setAttributeValue()
1868 */
1869 void QGLShaderProgram::setUniformValue(const char *name, GLuint value)
1870 {
1871     setUniformValue(uniformLocation(name), value);
1872 }
1873
1874 /*!
1875     Sets the uniform variable at \a location in the current context to
1876     the 2D vector (\a x, \a y).
1877
1878     \sa setAttributeValue()
1879 */
1880 void QGLShaderProgram::setUniformValue(int location, GLfloat x, GLfloat y)
1881 {
1882     Q_D(QGLShaderProgram);
1883     Q_UNUSED(d);
1884     if (location != -1) {
1885         GLfloat values[2] = {x, y};
1886         glUniform2fv(location, 1, values);
1887     }
1888 }
1889
1890 /*!
1891     \overload
1892
1893     Sets the uniform variable called \a name in the current context to
1894     the 2D vector (\a x, \a y).
1895
1896     \sa setAttributeValue()
1897 */
1898 void QGLShaderProgram::setUniformValue(const char *name, GLfloat x, GLfloat y)
1899 {
1900     setUniformValue(uniformLocation(name), x, y);
1901 }
1902
1903 /*!
1904     Sets the uniform variable at \a location in the current context to
1905     the 3D vector (\a x, \a y, \a z).
1906
1907     \sa setAttributeValue()
1908 */
1909 void QGLShaderProgram::setUniformValue
1910         (int location, GLfloat x, GLfloat y, GLfloat z)
1911 {
1912     Q_D(QGLShaderProgram);
1913     Q_UNUSED(d);
1914     if (location != -1) {
1915         GLfloat values[3] = {x, y, z};
1916         glUniform3fv(location, 1, values);
1917     }
1918 }
1919
1920 /*!
1921     \overload
1922
1923     Sets the uniform variable called \a name in the current context to
1924     the 3D vector (\a x, \a y, \a z).
1925
1926     \sa setAttributeValue()
1927 */
1928 void QGLShaderProgram::setUniformValue
1929         (const char *name, GLfloat x, GLfloat y, GLfloat z)
1930 {
1931     setUniformValue(uniformLocation(name), x, y, z);
1932 }
1933
1934 /*!
1935     Sets the uniform variable at \a location in the current context to
1936     the 4D vector (\a x, \a y, \a z, \a w).
1937
1938     \sa setAttributeValue()
1939 */
1940 void QGLShaderProgram::setUniformValue
1941         (int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1942 {
1943     Q_D(QGLShaderProgram);
1944     Q_UNUSED(d);
1945     if (location != -1) {
1946         GLfloat values[4] = {x, y, z, w};
1947         glUniform4fv(location, 1, values);
1948     }
1949 }
1950
1951 /*!
1952     \overload
1953
1954     Sets the uniform variable called \a name in the current context to
1955     the 4D vector (\a x, \a y, \a z, \a w).
1956
1957     \sa setAttributeValue()
1958 */
1959 void QGLShaderProgram::setUniformValue
1960         (const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1961 {
1962     setUniformValue(uniformLocation(name), x, y, z, w);
1963 }
1964
1965 /*!
1966     Sets the uniform variable at \a location in the current context to \a value.
1967
1968     \sa setAttributeValue()
1969 */
1970 void QGLShaderProgram::setUniformValue(int location, const QVector2D& value)
1971 {
1972     Q_D(QGLShaderProgram);
1973     Q_UNUSED(d);
1974     if (location != -1)
1975         glUniform2fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
1976 }
1977
1978 /*!
1979     \overload
1980
1981     Sets the uniform variable called \a name in the current context
1982     to \a value.
1983
1984     \sa setAttributeValue()
1985 */
1986 void QGLShaderProgram::setUniformValue(const char *name, const QVector2D& value)
1987 {
1988     setUniformValue(uniformLocation(name), value);
1989 }
1990
1991 /*!
1992     Sets the uniform variable at \a location in the current context to \a value.
1993
1994     \sa setAttributeValue()
1995 */
1996 void QGLShaderProgram::setUniformValue(int location, const QVector3D& value)
1997 {
1998     Q_D(QGLShaderProgram);
1999     Q_UNUSED(d);
2000     if (location != -1)
2001         glUniform3fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
2002 }
2003
2004 /*!
2005     \overload
2006
2007     Sets the uniform variable called \a name in the current context
2008     to \a value.
2009
2010     \sa setAttributeValue()
2011 */
2012 void QGLShaderProgram::setUniformValue(const char *name, const QVector3D& value)
2013 {
2014     setUniformValue(uniformLocation(name), value);
2015 }
2016
2017 /*!
2018     Sets the uniform variable at \a location in the current context to \a value.
2019
2020     \sa setAttributeValue()
2021 */
2022 void QGLShaderProgram::setUniformValue(int location, const QVector4D& value)
2023 {
2024     Q_D(QGLShaderProgram);
2025     Q_UNUSED(d);
2026     if (location != -1)
2027         glUniform4fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
2028 }
2029
2030 /*!
2031     \overload
2032
2033     Sets the uniform variable called \a name in the current context
2034     to \a value.
2035
2036     \sa setAttributeValue()
2037 */
2038 void QGLShaderProgram::setUniformValue(const char *name, const QVector4D& value)
2039 {
2040     setUniformValue(uniformLocation(name), value);
2041 }
2042
2043 /*!
2044     Sets the uniform variable at \a location in the current context to
2045     the red, green, blue, and alpha components of \a color.
2046
2047     \sa setAttributeValue()
2048 */
2049 void QGLShaderProgram::setUniformValue(int location, const QColor& color)
2050 {
2051     Q_D(QGLShaderProgram);
2052     Q_UNUSED(d);
2053     if (location != -1) {
2054         GLfloat values[4] = {GLfloat(color.redF()), GLfloat(color.greenF()),
2055                              GLfloat(color.blueF()), GLfloat(color.alphaF())};
2056         glUniform4fv(location, 1, values);
2057     }
2058 }
2059
2060 /*!
2061     \overload
2062
2063     Sets the uniform variable called \a name in the current context to
2064     the red, green, blue, and alpha components of \a color.
2065
2066     \sa setAttributeValue()
2067 */
2068 void QGLShaderProgram::setUniformValue(const char *name, const QColor& color)
2069 {
2070     setUniformValue(uniformLocation(name), color);
2071 }
2072
2073 /*!
2074     Sets the uniform variable at \a location in the current context to
2075     the x and y coordinates of \a point.
2076
2077     \sa setAttributeValue()
2078 */
2079 void QGLShaderProgram::setUniformValue(int location, const QPoint& point)
2080 {
2081     Q_D(QGLShaderProgram);
2082     Q_UNUSED(d);
2083     if (location != -1) {
2084         GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2085         glUniform2fv(location, 1, values);
2086     }
2087 }
2088
2089 /*!
2090     \overload
2091
2092     Sets the uniform variable associated with \a name in the current
2093     context to the x and y coordinates of \a point.
2094
2095     \sa setAttributeValue()
2096 */
2097 void QGLShaderProgram::setUniformValue(const char *name, const QPoint& point)
2098 {
2099     setUniformValue(uniformLocation(name), point);
2100 }
2101
2102 /*!
2103     Sets the uniform variable at \a location in the current context to
2104     the x and y coordinates of \a point.
2105
2106     \sa setAttributeValue()
2107 */
2108 void QGLShaderProgram::setUniformValue(int location, const QPointF& point)
2109 {
2110     Q_D(QGLShaderProgram);
2111     Q_UNUSED(d);
2112     if (location != -1) {
2113         GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2114         glUniform2fv(location, 1, values);
2115     }
2116 }
2117
2118 /*!
2119     \overload
2120
2121     Sets the uniform variable associated with \a name in the current
2122     context to the x and y coordinates of \a point.
2123
2124     \sa setAttributeValue()
2125 */
2126 void QGLShaderProgram::setUniformValue(const char *name, const QPointF& point)
2127 {
2128     setUniformValue(uniformLocation(name), point);
2129 }
2130
2131 /*!
2132     Sets the uniform variable at \a location in the current context to
2133     the width and height of the given \a size.
2134
2135     \sa setAttributeValue()
2136 */
2137 void QGLShaderProgram::setUniformValue(int location, const QSize& size)
2138 {
2139     Q_D(QGLShaderProgram);
2140     Q_UNUSED(d);
2141     if (location != -1) {
2142         GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2143         glUniform2fv(location, 1, values);
2144     }
2145 }
2146
2147 /*!
2148     \overload
2149
2150     Sets the uniform variable associated with \a name in the current
2151     context to the width and height of the given \a size.
2152
2153     \sa setAttributeValue()
2154 */
2155 void QGLShaderProgram::setUniformValue(const char *name, const QSize& size)
2156 {
2157     setUniformValue(uniformLocation(name), size);
2158 }
2159
2160 /*!
2161     Sets the uniform variable at \a location in the current context to
2162     the width and height of the given \a size.
2163
2164     \sa setAttributeValue()
2165 */
2166 void QGLShaderProgram::setUniformValue(int location, const QSizeF& size)
2167 {
2168     Q_D(QGLShaderProgram);
2169     Q_UNUSED(d);
2170     if (location != -1) {
2171         GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2172         glUniform2fv(location, 1, values);
2173     }
2174 }
2175
2176 /*!
2177     \overload
2178
2179     Sets the uniform variable associated with \a name in the current
2180     context to the width and height of the given \a size.
2181
2182     \sa setAttributeValue()
2183 */
2184 void QGLShaderProgram::setUniformValue(const char *name, const QSizeF& size)
2185 {
2186     setUniformValue(uniformLocation(name), size);
2187 }
2188
2189 // We have to repack matrices from qreal to GLfloat.
2190 #define setUniformMatrix(func,location,value,cols,rows) \
2191     if (location == -1) \
2192         return; \
2193     if (sizeof(qreal) == sizeof(GLfloat)) { \
2194         func(location, 1, GL_FALSE, \
2195              reinterpret_cast<const GLfloat *>(value.constData())); \
2196     } else { \
2197         GLfloat mat[cols * rows]; \
2198         const qreal *data = value.constData(); \
2199         for (int i = 0; i < cols * rows; ++i) \
2200             mat[i] = data[i]; \
2201         func(location, 1, GL_FALSE, mat); \
2202     }
2203 #if !defined(QT_OPENGL_ES_2)
2204 #define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \
2205     if (location == -1) \
2206         return; \
2207     if (sizeof(qreal) == sizeof(GLfloat)) { \
2208         const GLfloat *data = reinterpret_cast<const GLfloat *> \
2209             (value.constData());  \
2210         if (func) \
2211             func(location, 1, GL_FALSE, data); \
2212         else \
2213             colfunc(location, cols, data); \
2214     } else { \
2215         GLfloat mat[cols * rows]; \
2216         const qreal *data = value.constData(); \
2217         for (int i = 0; i < cols * rows; ++i) \
2218             mat[i] = data[i]; \
2219         if (func) \
2220             func(location, 1, GL_FALSE, mat); \
2221         else \
2222             colfunc(location, cols, mat); \
2223     }
2224 #else
2225 #define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \
2226     if (location == -1) \
2227         return; \
2228     if (sizeof(qreal) == sizeof(GLfloat)) { \
2229         const GLfloat *data = reinterpret_cast<const GLfloat *> \
2230             (value.constData());  \
2231         colfunc(location, cols, data); \
2232     } else { \
2233         GLfloat mat[cols * rows]; \
2234         const qreal *data = value.constData(); \
2235         for (int i = 0; i < cols * rows; ++i) \
2236             mat[i] = data[i]; \
2237         colfunc(location, cols, mat); \
2238     }
2239 #endif
2240
2241 /*!
2242     Sets the uniform variable at \a location in the current context
2243     to a 2x2 matrix \a value.
2244
2245     \sa setAttributeValue()
2246 */
2247 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x2& value)
2248 {
2249     Q_D(QGLShaderProgram);
2250     Q_UNUSED(d);
2251     setUniformMatrix(glUniformMatrix2fv, location, value, 2, 2);
2252 }
2253
2254 /*!
2255     \overload
2256
2257     Sets the uniform variable called \a name in the current context
2258     to a 2x2 matrix \a value.
2259
2260     \sa setAttributeValue()
2261 */
2262 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x2& value)
2263 {
2264     setUniformValue(uniformLocation(name), value);
2265 }
2266
2267 /*!
2268     Sets the uniform variable at \a location in the current context
2269     to a 2x3 matrix \a value.
2270
2271     \sa setAttributeValue()
2272 */
2273 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x3& value)
2274 {
2275     Q_D(QGLShaderProgram);
2276     Q_UNUSED(d);
2277     setUniformGenericMatrix
2278         (glUniformMatrix2x3fv, glUniform3fv, location, value, 2, 3);
2279 }
2280
2281 /*!
2282     \overload
2283
2284     Sets the uniform variable called \a name in the current context
2285     to a 2x3 matrix \a value.
2286
2287     \sa setAttributeValue()
2288 */
2289 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x3& value)
2290 {
2291     setUniformValue(uniformLocation(name), value);
2292 }
2293
2294 /*!
2295     Sets the uniform variable at \a location in the current context
2296     to a 2x4 matrix \a value.
2297
2298     \sa setAttributeValue()
2299 */
2300 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x4& value)
2301 {
2302     Q_D(QGLShaderProgram);
2303     Q_UNUSED(d);
2304     setUniformGenericMatrix
2305         (glUniformMatrix2x4fv, glUniform4fv, location, value, 2, 4);
2306 }
2307
2308 /*!
2309     \overload
2310
2311     Sets the uniform variable called \a name in the current context
2312     to a 2x4 matrix \a value.
2313
2314     \sa setAttributeValue()
2315 */
2316 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x4& value)
2317 {
2318     setUniformValue(uniformLocation(name), value);
2319 }
2320
2321 /*!
2322     Sets the uniform variable at \a location in the current context
2323     to a 3x2 matrix \a value.
2324
2325     \sa setAttributeValue()
2326 */
2327 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x2& value)
2328 {
2329     Q_D(QGLShaderProgram);
2330     Q_UNUSED(d);
2331     setUniformGenericMatrix
2332         (glUniformMatrix3x2fv, glUniform2fv, location, value, 3, 2);
2333 }
2334
2335 /*!
2336     \overload
2337
2338     Sets the uniform variable called \a name in the current context
2339     to a 3x2 matrix \a value.
2340
2341     \sa setAttributeValue()
2342 */
2343 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x2& value)
2344 {
2345     setUniformValue(uniformLocation(name), value);
2346 }
2347
2348 /*!
2349     Sets the uniform variable at \a location in the current context
2350     to a 3x3 matrix \a value.
2351
2352     \sa setAttributeValue()
2353 */
2354 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x3& value)
2355 {
2356     Q_D(QGLShaderProgram);
2357     Q_UNUSED(d);
2358     setUniformMatrix(glUniformMatrix3fv, location, value, 3, 3);
2359 }
2360
2361 /*!
2362     \overload
2363
2364     Sets the uniform variable called \a name in the current context
2365     to a 3x3 matrix \a value.
2366
2367     \sa setAttributeValue()
2368 */
2369 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x3& value)
2370 {
2371     setUniformValue(uniformLocation(name), value);
2372 }
2373
2374 /*!
2375     Sets the uniform variable at \a location in the current context
2376     to a 3x4 matrix \a value.
2377
2378     \sa setAttributeValue()
2379 */
2380 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x4& value)
2381 {
2382     Q_D(QGLShaderProgram);
2383     Q_UNUSED(d);
2384     setUniformGenericMatrix
2385         (glUniformMatrix3x4fv, glUniform4fv, location, value, 3, 4);
2386 }
2387
2388 /*!
2389     \overload
2390
2391     Sets the uniform variable called \a name in the current context
2392     to a 3x4 matrix \a value.
2393
2394     \sa setAttributeValue()
2395 */
2396 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x4& value)
2397 {
2398     setUniformValue(uniformLocation(name), value);
2399 }
2400
2401 /*!
2402     Sets the uniform variable at \a location in the current context
2403     to a 4x2 matrix \a value.
2404
2405     \sa setAttributeValue()
2406 */
2407 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x2& value)
2408 {
2409     Q_D(QGLShaderProgram);
2410     Q_UNUSED(d);
2411     setUniformGenericMatrix
2412         (glUniformMatrix4x2fv, glUniform2fv, location, value, 4, 2);
2413 }
2414
2415 /*!
2416     \overload
2417
2418     Sets the uniform variable called \a name in the current context
2419     to a 4x2 matrix \a value.
2420
2421     \sa setAttributeValue()
2422 */
2423 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x2& value)
2424 {
2425     setUniformValue(uniformLocation(name), value);
2426 }
2427
2428 /*!
2429     Sets the uniform variable at \a location in the current context
2430     to a 4x3 matrix \a value.
2431
2432     \sa setAttributeValue()
2433 */
2434 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x3& value)
2435 {
2436     Q_D(QGLShaderProgram);
2437     Q_UNUSED(d);
2438     setUniformGenericMatrix
2439         (glUniformMatrix4x3fv, glUniform3fv, location, value, 4, 3);
2440 }
2441
2442 /*!
2443     \overload
2444
2445     Sets the uniform variable called \a name in the current context
2446     to a 4x3 matrix \a value.
2447
2448     \sa setAttributeValue()
2449 */
2450 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x3& value)
2451 {
2452     setUniformValue(uniformLocation(name), value);
2453 }
2454
2455 /*!
2456     Sets the uniform variable at \a location in the current context
2457     to a 4x4 matrix \a value.
2458
2459     \sa setAttributeValue()
2460 */
2461 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x4& value)
2462 {
2463     Q_D(QGLShaderProgram);
2464     Q_UNUSED(d);
2465     setUniformMatrix(glUniformMatrix4fv, location, value, 4, 4);
2466 }
2467
2468 /*!
2469     \overload
2470
2471     Sets the uniform variable called \a name in the current context
2472     to a 4x4 matrix \a value.
2473
2474     \sa setAttributeValue()
2475 */
2476 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x4& value)
2477 {
2478     setUniformValue(uniformLocation(name), value);
2479 }
2480
2481 /*!
2482     \overload
2483
2484     Sets the uniform variable at \a location in the current context
2485     to a 2x2 matrix \a value.  The matrix elements must be specified
2486     in column-major order.
2487
2488     \sa setAttributeValue()
2489     \since 4.7
2490 */
2491 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[2][2])
2492 {
2493     Q_D(QGLShaderProgram);
2494     Q_UNUSED(d);
2495     if (location != -1)
2496         glUniformMatrix2fv(location, 1, GL_FALSE, value[0]);
2497 }
2498
2499 /*!
2500     \overload
2501
2502     Sets the uniform variable at \a location in the current context
2503     to a 3x3 matrix \a value.  The matrix elements must be specified
2504     in column-major order.
2505
2506     \sa setAttributeValue()
2507     \since 4.7
2508 */
2509 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[3][3])
2510 {
2511     Q_D(QGLShaderProgram);
2512     Q_UNUSED(d);
2513     if (location != -1)
2514         glUniformMatrix3fv(location, 1, GL_FALSE, value[0]);
2515 }
2516
2517 /*!
2518     \overload
2519
2520     Sets the uniform variable at \a location in the current context
2521     to a 4x4 matrix \a value.  The matrix elements must be specified
2522     in column-major order.
2523
2524     \sa setAttributeValue()
2525 */
2526 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[4][4])
2527 {
2528     Q_D(QGLShaderProgram);
2529     Q_UNUSED(d);
2530     if (location != -1)
2531         glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
2532 }
2533
2534
2535 /*!
2536     \overload
2537
2538     Sets the uniform variable called \a name in the current context
2539     to a 2x2 matrix \a value.  The matrix elements must be specified
2540     in column-major order.
2541
2542     \sa setAttributeValue()
2543     \since 4.7
2544 */
2545 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[2][2])
2546 {
2547     setUniformValue(uniformLocation(name), value);
2548 }
2549
2550 /*!
2551     \overload
2552
2553     Sets the uniform variable called \a name in the current context
2554     to a 3x3 matrix \a value.  The matrix elements must be specified
2555     in column-major order.
2556
2557     \sa setAttributeValue()
2558     \since 4.7
2559 */
2560 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[3][3])
2561 {
2562     setUniformValue(uniformLocation(name), value);
2563 }
2564
2565 /*!
2566     \overload
2567
2568     Sets the uniform variable called \a name in the current context
2569     to a 4x4 matrix \a value.  The matrix elements must be specified
2570     in column-major order.
2571
2572     \sa setAttributeValue()
2573 */
2574 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[4][4])
2575 {
2576     setUniformValue(uniformLocation(name), value);
2577 }
2578
2579 /*!
2580     Sets the uniform variable at \a location in the current context to a
2581     3x3 transformation matrix \a value that is specified as a QTransform value.
2582
2583     To set a QTransform value as a 4x4 matrix in a shader, use
2584     \c{setUniformValue(location, QMatrix4x4(value))}.
2585 */
2586 void QGLShaderProgram::setUniformValue(int location, const QTransform& value)
2587 {
2588     Q_D(QGLShaderProgram);
2589     Q_UNUSED(d);
2590     if (location != -1) {
2591         GLfloat mat[3][3] = {
2592             {GLfloat(value.m11()), GLfloat(value.m12()), GLfloat(value.m13())},
2593             {GLfloat(value.m21()), GLfloat(value.m22()), GLfloat(value.m23())},
2594             {GLfloat(value.m31()), GLfloat(value.m32()), GLfloat(value.m33())}
2595         };
2596         glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
2597     }
2598 }
2599
2600 /*!
2601     \overload
2602
2603     Sets the uniform variable called \a name in the current context to a
2604     3x3 transformation matrix \a value that is specified as a QTransform value.
2605
2606     To set a QTransform value as a 4x4 matrix in a shader, use
2607     \c{setUniformValue(name, QMatrix4x4(value))}.
2608 */
2609 void QGLShaderProgram::setUniformValue
2610         (const char *name, const QTransform& value)
2611 {
2612     setUniformValue(uniformLocation(name), value);
2613 }
2614
2615 /*!
2616     Sets the uniform variable array at \a location in the current
2617     context to the \a count elements of \a values.
2618
2619     \sa setAttributeValue()
2620 */
2621 void QGLShaderProgram::setUniformValueArray(int location, const GLint *values, int count)
2622 {
2623     Q_D(QGLShaderProgram);
2624     Q_UNUSED(d);
2625     if (location != -1)
2626         glUniform1iv(location, count, values);
2627 }
2628
2629 /*!
2630     \overload
2631
2632     Sets the uniform variable array called \a name in the current
2633     context to the \a count elements of \a values.
2634
2635     \sa setAttributeValue()
2636 */
2637 void QGLShaderProgram::setUniformValueArray
2638         (const char *name, const GLint *values, int count)
2639 {
2640     setUniformValueArray(uniformLocation(name), values, count);
2641 }
2642
2643 /*!
2644     Sets the uniform variable array at \a location in the current
2645     context to the \a count elements of \a values.  This overload
2646     should be used when setting an array of sampler values.
2647
2648     \sa setAttributeValue()
2649 */
2650 void QGLShaderProgram::setUniformValueArray(int location, const GLuint *values, int count)
2651 {
2652     Q_D(QGLShaderProgram);
2653     Q_UNUSED(d);
2654     if (location != -1)
2655         glUniform1iv(location, count, reinterpret_cast<const GLint *>(values));
2656 }
2657
2658 /*!
2659     \overload
2660
2661     Sets the uniform variable array called \a name in the current
2662     context to the \a count elements of \a values.  This overload
2663     should be used when setting an array of sampler values.
2664
2665     \sa setAttributeValue()
2666 */
2667 void QGLShaderProgram::setUniformValueArray
2668         (const char *name, const GLuint *values, int count)
2669 {
2670     setUniformValueArray(uniformLocation(name), values, count);
2671 }
2672
2673 /*!
2674     Sets the uniform variable array at \a location in the current
2675     context to the \a count elements of \a values.  Each element
2676     has \a tupleSize components.  The \a tupleSize must be 1, 2, 3, or 4.
2677
2678     \sa setAttributeValue()
2679 */
2680 void QGLShaderProgram::setUniformValueArray(int location, const GLfloat *values, int count, int tupleSize)
2681 {
2682     Q_D(QGLShaderProgram);
2683     Q_UNUSED(d);
2684     if (location != -1) {
2685         if (tupleSize == 1)
2686             glUniform1fv(location, count, values);
2687         else if (tupleSize == 2)
2688             glUniform2fv(location, count, values);
2689         else if (tupleSize == 3)
2690             glUniform3fv(location, count, values);
2691         else if (tupleSize == 4)
2692             glUniform4fv(location, count, values);
2693         else
2694             qWarning() << "QGLShaderProgram::setUniformValue: size" << tupleSize << "not supported";
2695     }
2696 }
2697
2698 /*!
2699     \overload
2700
2701     Sets the uniform variable array called \a name in the current
2702     context to the \a count elements of \a values.  Each element
2703     has \a tupleSize components.  The \a tupleSize must be 1, 2, 3, or 4.
2704
2705     \sa setAttributeValue()
2706 */
2707 void QGLShaderProgram::setUniformValueArray
2708         (const char *name, const GLfloat *values, int count, int tupleSize)
2709 {
2710     setUniformValueArray(uniformLocation(name), values, count, tupleSize);
2711 }
2712
2713 /*!
2714     Sets the uniform variable array at \a location in the current
2715     context to the \a count 2D vector elements of \a values.
2716
2717     \sa setAttributeValue()
2718 */
2719 void QGLShaderProgram::setUniformValueArray(int location, const QVector2D *values, int count)
2720 {
2721     Q_D(QGLShaderProgram);
2722     Q_UNUSED(d);
2723     if (location != -1)
2724         glUniform2fv(location, count, reinterpret_cast<const GLfloat *>(values));
2725 }
2726
2727 /*!
2728     \overload
2729
2730     Sets the uniform variable array called \a name in the current
2731     context to the \a count 2D vector elements of \a values.
2732
2733     \sa setAttributeValue()
2734 */
2735 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector2D *values, int count)
2736 {
2737     setUniformValueArray(uniformLocation(name), values, count);
2738 }
2739
2740 /*!
2741     Sets the uniform variable array at \a location in the current
2742     context to the \a count 3D vector elements of \a values.
2743
2744     \sa setAttributeValue()
2745 */
2746 void QGLShaderProgram::setUniformValueArray(int location, const QVector3D *values, int count)
2747 {
2748     Q_D(QGLShaderProgram);
2749     Q_UNUSED(d);
2750     if (location != -1)
2751         glUniform3fv(location, count, reinterpret_cast<const GLfloat *>(values));
2752 }
2753
2754 /*!
2755     \overload
2756
2757     Sets the uniform variable array called \a name in the current
2758     context to the \a count 3D vector elements of \a values.
2759
2760     \sa setAttributeValue()
2761 */
2762 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector3D *values, int count)
2763 {
2764     setUniformValueArray(uniformLocation(name), values, count);
2765 }
2766
2767 /*!
2768     Sets the uniform variable array at \a location in the current
2769     context to the \a count 4D vector elements of \a values.
2770
2771     \sa setAttributeValue()
2772 */
2773 void QGLShaderProgram::setUniformValueArray(int location, const QVector4D *values, int count)
2774 {
2775     Q_D(QGLShaderProgram);
2776     Q_UNUSED(d);
2777     if (location != -1)
2778         glUniform4fv(location, count, reinterpret_cast<const GLfloat *>(values));
2779 }
2780
2781 /*!
2782     \overload
2783
2784     Sets the uniform variable array called \a name in the current
2785     context to the \a count 4D vector elements of \a values.
2786
2787     \sa setAttributeValue()
2788 */
2789 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector4D *values, int count)
2790 {
2791     setUniformValueArray(uniformLocation(name), values, count);
2792 }
2793
2794 // We have to repack matrix arrays from qreal to GLfloat.
2795 #define setUniformMatrixArray(func,location,values,count,type,cols,rows) \
2796     if (location == -1 || count <= 0) \
2797         return; \
2798     if (sizeof(type) == sizeof(GLfloat) * cols * rows) { \
2799         func(location, count, GL_FALSE, \
2800              reinterpret_cast<const GLfloat *>(values[0].constData())); \
2801     } else { \
2802         QVarLengthArray<GLfloat> temp(cols * rows * count); \
2803         for (int index = 0; index < count; ++index) { \
2804             for (int index2 = 0; index2 < (cols * rows); ++index2) { \
2805                 temp.data()[cols * rows * index + index2] = \
2806                     values[index].constData()[index2]; \
2807             } \
2808         } \
2809         func(location, count, GL_FALSE, temp.constData()); \
2810     }
2811 #if !defined(QT_OPENGL_ES_2)
2812 #define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
2813     if (location == -1 || count <= 0) \
2814         return; \
2815     if (sizeof(type) == sizeof(GLfloat) * cols * rows) { \
2816         const GLfloat *data = reinterpret_cast<const GLfloat *> \
2817             (values[0].constData());  \
2818         if (func) \
2819             func(location, count, GL_FALSE, data); \
2820         else \
2821             colfunc(location, count * cols, data); \
2822     } else { \
2823         QVarLengthArray<GLfloat> temp(cols * rows * count); \
2824         for (int index = 0; index < count; ++index) { \
2825             for (int index2 = 0; index2 < (cols * rows); ++index2) { \
2826                 temp.data()[cols * rows * index + index2] = \
2827                     values[index].constData()[index2]; \
2828             } \
2829         } \
2830         if (func) \
2831             func(location, count, GL_FALSE, temp.constData()); \
2832         else \
2833             colfunc(location, count * cols, temp.constData()); \
2834     }
2835 #else
2836 #define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
2837     if (location == -1 || count <= 0) \
2838         return; \
2839     if (sizeof(type) == sizeof(GLfloat) * cols * rows) { \
2840         const GLfloat *data = reinterpret_cast<const GLfloat *> \
2841             (values[0].constData());  \
2842         colfunc(location, count * cols, data); \
2843     } else { \
2844         QVarLengthArray<GLfloat> temp(cols * rows * count); \
2845         for (int index = 0; index < count; ++index) { \
2846             for (int index2 = 0; index2 < (cols * rows); ++index2) { \
2847                 temp.data()[cols * rows * index + index2] = \
2848                     values[index].constData()[index2]; \
2849             } \
2850         } \
2851         colfunc(location, count * cols, temp.constData()); \
2852     }
2853 #endif
2854
2855 /*!
2856     Sets the uniform variable array at \a location in the current
2857     context to the \a count 2x2 matrix elements of \a values.
2858
2859     \sa setAttributeValue()
2860 */
2861 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x2 *values, int count)
2862 {
2863     Q_D(QGLShaderProgram);
2864     Q_UNUSED(d);
2865     setUniformMatrixArray
2866         (glUniformMatrix2fv, location, values, count, QMatrix2x2, 2, 2);
2867 }
2868
2869 /*!
2870     \overload
2871
2872     Sets the uniform variable array called \a name in the current
2873     context to the \a count 2x2 matrix elements of \a values.
2874
2875     \sa setAttributeValue()
2876 */
2877 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x2 *values, int count)
2878 {
2879     setUniformValueArray(uniformLocation(name), values, count);
2880 }
2881
2882 /*!
2883     Sets the uniform variable array at \a location in the current
2884     context to the \a count 2x3 matrix elements of \a values.
2885
2886     \sa setAttributeValue()
2887 */
2888 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x3 *values, int count)
2889 {
2890     Q_D(QGLShaderProgram);
2891     Q_UNUSED(d);
2892     setUniformGenericMatrixArray
2893         (glUniformMatrix2x3fv, glUniform3fv, location, values, count,
2894          QMatrix2x3, 2, 3);
2895 }
2896
2897 /*!
2898     \overload
2899
2900     Sets the uniform variable array called \a name in the current
2901     context to the \a count 2x3 matrix elements of \a values.
2902
2903     \sa setAttributeValue()
2904 */
2905 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x3 *values, int count)
2906 {
2907     setUniformValueArray(uniformLocation(name), values, count);
2908 }
2909
2910 /*!
2911     Sets the uniform variable array at \a location in the current
2912     context to the \a count 2x4 matrix elements of \a values.
2913
2914     \sa setAttributeValue()
2915 */
2916 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x4 *values, int count)
2917 {
2918     Q_D(QGLShaderProgram);
2919     Q_UNUSED(d);
2920     setUniformGenericMatrixArray
2921         (glUniformMatrix2x4fv, glUniform4fv, location, values, count,
2922          QMatrix2x4, 2, 4);
2923 }
2924
2925 /*!
2926     \overload
2927
2928     Sets the uniform variable array called \a name in the current
2929     context to the \a count 2x4 matrix elements of \a values.
2930
2931     \sa setAttributeValue()
2932 */
2933 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x4 *values, int count)
2934 {
2935     setUniformValueArray(uniformLocation(name), values, count);
2936 }
2937
2938 /*!
2939     Sets the uniform variable array at \a location in the current
2940     context to the \a count 3x2 matrix elements of \a values.
2941
2942     \sa setAttributeValue()
2943 */
2944 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x2 *values, int count)
2945 {
2946     Q_D(QGLShaderProgram);
2947     Q_UNUSED(d);
2948     setUniformGenericMatrixArray
2949         (glUniformMatrix3x2fv, glUniform2fv, location, values, count,
2950          QMatrix3x2, 3, 2);
2951 }
2952
2953 /*!
2954     \overload
2955
2956     Sets the uniform variable array called \a name in the current
2957     context to the \a count 3x2 matrix elements of \a values.
2958
2959     \sa setAttributeValue()
2960 */
2961 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x2 *values, int count)
2962 {
2963     setUniformValueArray(uniformLocation(name), values, count);
2964 }
2965
2966 /*!
2967     Sets the uniform variable array at \a location in the current
2968     context to the \a count 3x3 matrix elements of \a values.
2969
2970     \sa setAttributeValue()
2971 */
2972 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x3 *values, int count)
2973 {
2974     Q_D(QGLShaderProgram);
2975     Q_UNUSED(d);
2976     setUniformMatrixArray
2977         (glUniformMatrix3fv, location, values, count, QMatrix3x3, 3, 3);
2978 }
2979
2980 /*!
2981     \overload
2982
2983     Sets the uniform variable array called \a name in the current
2984     context to the \a count 3x3 matrix elements of \a values.
2985
2986     \sa setAttributeValue()
2987 */
2988 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x3 *values, int count)
2989 {
2990     setUniformValueArray(uniformLocation(name), values, count);
2991 }
2992
2993 /*!
2994     Sets the uniform variable array at \a location in the current
2995     context to the \a count 3x4 matrix elements of \a values.
2996
2997     \sa setAttributeValue()
2998 */
2999 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x4 *values, int count)
3000 {
3001     Q_D(QGLShaderProgram);
3002     Q_UNUSED(d);
3003     setUniformGenericMatrixArray
3004         (glUniformMatrix3x4fv, glUniform4fv, location, values, count,
3005          QMatrix3x4, 3, 4);
3006 }
3007
3008 /*!
3009     \overload
3010
3011     Sets the uniform variable array called \a name in the current
3012     context to the \a count 3x4 matrix elements of \a values.
3013
3014     \sa setAttributeValue()
3015 */
3016 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x4 *values, int count)
3017 {
3018     setUniformValueArray(uniformLocation(name), values, count);
3019 }
3020
3021 /*!
3022     Sets the uniform variable array at \a location in the current
3023     context to the \a count 4x2 matrix elements of \a values.
3024
3025     \sa setAttributeValue()
3026 */
3027 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x2 *values, int count)
3028 {
3029     Q_D(QGLShaderProgram);
3030     Q_UNUSED(d);
3031     setUniformGenericMatrixArray
3032         (glUniformMatrix4x2fv, glUniform2fv, location, values, count,
3033          QMatrix4x2, 4, 2);
3034 }
3035
3036 /*!
3037     \overload
3038
3039     Sets the uniform variable array called \a name in the current
3040     context to the \a count 4x2 matrix elements of \a values.
3041
3042     \sa setAttributeValue()
3043 */
3044 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x2 *values, int count)
3045 {
3046     setUniformValueArray(uniformLocation(name), values, count);
3047 }
3048
3049 /*!
3050     Sets the uniform variable array at \a location in the current
3051     context to the \a count 4x3 matrix elements of \a values.
3052
3053     \sa setAttributeValue()
3054 */
3055 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x3 *values, int count)
3056 {
3057     Q_D(QGLShaderProgram);
3058     Q_UNUSED(d);
3059     setUniformGenericMatrixArray
3060         (glUniformMatrix4x3fv, glUniform3fv, location, values, count,
3061          QMatrix4x3, 4, 3);
3062 }
3063
3064 /*!
3065     \overload
3066
3067     Sets the uniform variable array called \a name in the current
3068     context to the \a count 4x3 matrix elements of \a values.
3069
3070     \sa setAttributeValue()
3071 */
3072 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x3 *values, int count)
3073 {
3074     setUniformValueArray(uniformLocation(name), values, count);
3075 }
3076
3077 /*!
3078     Sets the uniform variable array at \a location in the current
3079     context to the \a count 4x4 matrix elements of \a values.
3080
3081     \sa setAttributeValue()
3082 */
3083 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x4 *values, int count)
3084 {
3085     Q_D(QGLShaderProgram);
3086     Q_UNUSED(d);
3087     setUniformMatrixArray
3088         (glUniformMatrix4fv, location, values, count, QMatrix4x4, 4, 4);
3089 }
3090
3091 /*!
3092     \overload
3093
3094     Sets the uniform variable array called \a name in the current
3095     context to the \a count 4x4 matrix elements of \a values.
3096
3097     \sa setAttributeValue()
3098 */
3099 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x4 *values, int count)
3100 {
3101     setUniformValueArray(uniformLocation(name), values, count);
3102 }
3103
3104 #undef ctx
3105
3106 /*!
3107     Returns the hardware limit for how many vertices a geometry shader
3108     can output.
3109
3110     \since 4.7
3111
3112     \sa setGeometryOutputVertexCount()
3113 */
3114 int QGLShaderProgram::maxGeometryOutputVertices() const
3115 {
3116     GLint n;
3117     glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &n);
3118     return n;
3119 }
3120
3121 /*!
3122     Sets the maximum number of vertices the current geometry shader
3123     program will produce, if active, to \a count.
3124
3125     \since 4.7
3126
3127     This parameter takes effect the next time the program is linked.
3128 */
3129 void QGLShaderProgram::setGeometryOutputVertexCount(int count)
3130 {
3131 #ifndef QT_NO_DEBUG
3132     int max = maxGeometryOutputVertices();
3133     if (count > max) {
3134         qWarning("QGLShaderProgram::setGeometryOutputVertexCount: count: %d higher than maximum: %d",
3135                  count, max);
3136     }
3137 #endif
3138     d_func()->geometryVertexCount = count;
3139 }
3140
3141
3142 /*!
3143     Returns the maximum number of vertices the current geometry shader
3144     program will produce, if active.
3145
3146     \since 4.7
3147
3148     This parameter takes effect the ntext time the program is linked.
3149 */
3150 int QGLShaderProgram::geometryOutputVertexCount() const
3151 {
3152     return d_func()->geometryVertexCount;
3153 }
3154
3155
3156 /*!
3157     Sets the input type from \a inputType.
3158
3159     This parameter takes effect the next time the program is linked.
3160 */
3161 void QGLShaderProgram::setGeometryInputType(GLenum inputType)
3162 {
3163     d_func()->geometryInputType = inputType;
3164 }
3165
3166
3167 /*!
3168     Returns the geometry shader input type, if active.
3169
3170     This parameter takes effect the next time the program is linked.
3171
3172     \since 4.7
3173  */
3174
3175 GLenum QGLShaderProgram::geometryInputType() const
3176 {
3177     return d_func()->geometryInputType;
3178 }
3179
3180
3181 /*!
3182     Sets the output type from the geometry shader, if active, to
3183     \a outputType.
3184
3185     This parameter takes effect the next time the program is linked.
3186
3187     \since 4.7
3188 */
3189 void QGLShaderProgram::setGeometryOutputType(GLenum outputType)
3190 {
3191     d_func()->geometryOutputType = outputType;
3192 }
3193
3194
3195 /*!
3196     Returns the geometry shader output type, if active.
3197
3198     This parameter takes effect the next time the program is linked.
3199
3200     \since 4.7
3201  */
3202 GLenum QGLShaderProgram::geometryOutputType() const
3203 {
3204     return d_func()->geometryOutputType;
3205 }
3206
3207
3208 /*!
3209     Returns true if shader programs written in the OpenGL Shading
3210     Language (GLSL) are supported on this system; false otherwise.
3211
3212     The \a context is used to resolve the GLSL extensions.
3213     If \a context is null, then QGLContext::currentContext() is used.
3214 */
3215 bool QGLShaderProgram::hasOpenGLShaderPrograms(const QGLContext *context)
3216 {
3217 #if !defined(QT_OPENGL_ES_2)
3218     if (!context)
3219         context = QGLContext::currentContext();
3220     if (!context)
3221         return false;
3222     return qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
3223 #else
3224     Q_UNUSED(context);
3225     return true;
3226 #endif
3227 }
3228
3229 /*!
3230     \internal
3231 */
3232 void QGLShaderProgram::shaderDestroyed()
3233 {
3234     Q_D(QGLShaderProgram);
3235     QGLShader *shader = qobject_cast<QGLShader *>(sender());
3236     if (shader && !d->removingShaders)
3237         removeShader(shader);
3238 }
3239
3240
3241 #undef ctx
3242 #undef context
3243
3244 /*!
3245     Returns true if shader programs of type \a type are supported on
3246     this system; false otherwise.
3247
3248     The \a context is used to resolve the GLSL extensions.
3249     If \a context is null, then QGLContext::currentContext() is used.
3250
3251     \since 4.7
3252 */
3253 bool QGLShader::hasOpenGLShaders(ShaderType type, const QGLContext *context)
3254 {
3255     if (!context)
3256         context = QGLContext::currentContext();
3257     if (!context)
3258         return false;
3259
3260     if ((type & ~(Geometry | Vertex | Fragment)) || type == 0)
3261         return false;
3262
3263     bool resolved = qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
3264     if (!resolved)
3265         return false;
3266
3267     if ((type & Geometry) && !QByteArray((const char *) glGetString(GL_EXTENSIONS)).contains("GL_EXT_geometry_shader4"))
3268         return false;
3269
3270     return true;
3271 }
3272
3273 QT_END_NAMESPACE