1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qglshaderprogram.h"
43 #include "qglextensions_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>
54 \class QGLShaderProgram
55 \brief The QGLShaderProgram class allows OpenGL shader programs to be linked and used.
59 \section1 Introduction
61 This class supports shader programs written in the OpenGL Shading
62 Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).
64 QGLShader and QGLShaderProgram shelter the programmer from the details of
65 compiling and linking vertex and fragment shaders.
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():
72 \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 0
74 \section1 Writing portable shaders
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}.
83 The QGLShaderProgram class makes the process of writing portable shaders
84 easier by prefixing all shader programs with the following lines on
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.
98 \section1 Simple shader example
100 \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 1
102 With the above shader program active, we can draw a green triangle
105 \snippet doc/src/snippets/code/src_opengl_qglshaderprogram.cpp 2
107 \section1 Binary shaders and programs
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().
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.
125 \brief The QGLShader class allows OpenGL shaders to be compiled.
129 This class supports shaders written in the OpenGL Shading Language (GLSL)
130 and in the OpenGL/ES Shading Language (GLSL/ES).
132 QGLShader and QGLShaderProgram shelter the programmer from the details of
133 compiling and linking vertex and fragment shaders.
139 \enum QGLShader::ShaderTypeBit
140 This enum specifies the type of QGLShader that is being created.
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.
148 #ifndef GL_FRAGMENT_SHADER
149 #define GL_FRAGMENT_SHADER 0x8B30
151 #ifndef GL_VERTEX_SHADER
152 #define GL_VERTEX_SHADER 0x8B31
154 #ifndef GL_COMPILE_STATUS
155 #define GL_COMPILE_STATUS 0x8B81
157 #ifndef GL_LINK_STATUS
158 #define GL_LINK_STATUS 0x8B82
160 #ifndef GL_INFO_LOG_LENGTH
161 #define GL_INFO_LOG_LENGTH 0x8B84
163 #ifndef GL_ACTIVE_UNIFORMS
164 #define GL_ACTIVE_UNIFORMS 0x8B86
166 #ifndef GL_ACTIVE_UNIFORM_MAX_LENGTH
167 #define GL_ACTIVE_UNIFORM_MAX_LENGTH 0x8B87
169 #ifndef GL_ACTIVE_ATTRIBUTES
170 #define GL_ACTIVE_ATTRIBUTES 0x8B89
172 #ifndef GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
173 #define GL_ACTIVE_ATTRIBUTE_MAX_LENGTH 0x8B8A
175 #ifndef GL_CURRENT_VERTEX_ATTRIB
176 #define GL_CURRENT_VERTEX_ATTRIB 0x8626
178 #ifndef GL_SHADER_SOURCE_LENGTH
179 #define GL_SHADER_SOURCE_LENGTH 0x8B88
181 #ifndef GL_SHADER_BINARY_FORMATS
182 #define GL_SHADER_BINARY_FORMATS 0x8DF8
184 #ifndef GL_NUM_SHADER_BINARY_FORMATS
185 #define GL_NUM_SHADER_BINARY_FORMATS 0x8DF9
188 class QGLShaderPrivate : public QObjectPrivate
190 Q_DECLARE_PUBLIC(QGLShader)
192 QGLShaderPrivate(const QGLContext *, QGLShader::ShaderType type)
200 QGLSharedResourceGuardBase *shaderGuard;
201 QGLShader::ShaderType shaderType;
206 bool compile(QGLShader *q);
211 void freeShaderFunc(QGLContext *ctx, GLuint id)
218 #define ctx QGLContext::currentContext()
220 QGLShaderPrivate::~QGLShaderPrivate()
226 bool QGLShaderPrivate::create()
228 QGLContext *context = const_cast<QGLContext *>(QGLContext::currentContext());
231 if (qt_resolve_glsl_extensions(context)) {
233 if (shaderType == QGLShader::Vertex)
234 shader = glCreateShader(GL_VERTEX_SHADER);
235 else if (shaderType == QGLShader::Geometry)
236 shader = glCreateShader(GL_GEOMETRY_SHADER_EXT);
238 shader = glCreateShader(GL_FRAGMENT_SHADER);
240 qWarning() << "QGLShader: could not create shader";
243 shaderGuard = createSharedResourceGuard(context, shader, freeShaderFunc);
250 bool QGLShaderPrivate::compile(QGLShader *q)
252 GLuint shader = shaderGuard ? shaderGuard->id() : 0;
255 glCompileShader(shader);
257 glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
258 compiled = (value != 0);
260 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &value);
261 if (!compiled && value > 1) {
262 char *logbuf = new char [value];
264 glGetShaderInfoLog(shader, value, &len, logbuf);
265 log = QString::fromLatin1(logbuf);
266 QString name = q->objectName();
268 const char *types[] = {
275 const char *type = types[3];
276 if (shaderType == QGLShader::Fragment)
278 else if (shaderType == QGLShader::Vertex)
280 else if (shaderType == QGLShader::Geometry)
284 qWarning("QGLShader::compile(%s): %s", type, qPrintable(log));
286 qWarning("QGLShader::compile(%s)[%s]: %s", type, qPrintable(name), qPrintable(log));
293 void QGLShaderPrivate::deleteShader()
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.
306 This constructor is normally followed by a call to compileSourceCode()
307 or compileSourceFile().
309 The shader will be associated with the current QGLContext.
311 \sa compileSourceCode(), compileSourceFile()
313 QGLShader::QGLShader(QGLShader::ShaderType type, QObject *parent)
314 : QObject(*new QGLShaderPrivate(QGLContext::currentContext(), type), parent)
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.
325 This constructor is normally followed by a call to compileSourceCode()
326 or compileSourceFile().
328 The shader will be associated with \a context.
330 \sa compileSourceCode(), compileSourceFile()
332 QGLShader::QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObject *parent)
333 : QObject(*new QGLShaderPrivate(context ? context : QGLContext::currentContext(), type), parent)
337 if (context && !QGLContext::areSharing(context, QGLContext::currentContext())) {
338 qWarning("QGLShader::QGLShader: \'context\' must be the current context or sharing with it.");
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.
350 QGLShader::~QGLShader()
355 Returns the type of this shader.
357 QGLShader::ShaderType QGLShader::shaderType() const
359 Q_D(const QGLShader);
360 return d->shaderType;
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[] =
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"
385 Sets the \a source code for this shader and compiles it.
386 Returns true if the source was successfully compiled, false otherwise.
388 \sa compileSourceFile()
390 bool QGLShader::compileSourceCode(const char *source)
393 if (d->shaderGuard && d->shaderGuard->id()) {
394 QVarLengthArray<const char *, 4> src;
395 QVarLengthArray<GLint, 4> srclen;
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) {
405 while (source[headerLen] != '\0' && source[headerLen] != '\n')
407 if (source[headerLen] == '\n')
412 srclen.append(GLint(headerLen));
414 #ifdef QGL_DEFINE_QUALIFIERS
415 src.append(qualifierDefines);
416 srclen.append(GLint(sizeof(qualifierDefines) - 1));
418 #ifdef QGL_REDEFINE_HIGHP
419 if (d->shaderType == Fragment) {
420 src.append(redefineHighp);
421 srclen.append(GLint(sizeof(redefineHighp) - 1));
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);
436 Sets the \a source code for this shader and compiles it.
437 Returns true if the source was successfully compiled, false otherwise.
439 \sa compileSourceFile()
441 bool QGLShader::compileSourceCode(const QByteArray& source)
443 return compileSourceCode(source.constData());
449 Sets the \a source code for this shader and compiles it.
450 Returns true if the source was successfully compiled, false otherwise.
452 \sa compileSourceFile()
454 bool QGLShader::compileSourceCode(const QString& source)
456 return compileSourceCode(source.toLatin1().constData());
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.
464 \sa compileSourceCode()
466 bool QGLShader::compileSourceFile(const QString& fileName)
468 QFile file(fileName);
469 if (!file.open(QFile::ReadOnly)) {
470 qWarning() << "QGLShader: Unable to open file" << fileName;
474 QByteArray contents = file.readAll();
475 return compileSourceCode(contents.constData());
479 Returns the source code for this shader.
481 \sa compileSourceCode()
483 QByteArray QGLShader::sourceCode() const
485 Q_D(const QGLShader);
486 GLuint shader = d->shaderGuard ? d->shaderGuard->id() : 0;
490 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &size);
494 char *source = new char [size];
495 glGetShaderSource(shader, size, &len, source);
496 QByteArray src(source);
502 Returns true if this shader has been compiled; false otherwise.
504 \sa compileSourceCode(), compileSourceFile()
506 bool QGLShader::isCompiled() const
508 Q_D(const QGLShader);
513 Returns the errors and warnings that occurred during the last compile.
515 \sa compileSourceCode(), compileSourceFile()
517 QString QGLShader::log() const
519 Q_D(const QGLShader);
524 Returns the OpenGL identifier associated with this shader.
526 \sa QGLShaderProgram::programId()
528 GLuint QGLShader::shaderId() const
530 Q_D(const QGLShader);
531 return d->shaderGuard ? d->shaderGuard->id() : 0;
536 class QGLShaderProgramPrivate : public QObjectPrivate
538 Q_DECLARE_PUBLIC(QGLShaderProgram)
540 QGLShaderProgramPrivate(const QGLContext *)
544 , removingShaders(false)
545 , geometryVertexCount(64)
546 , geometryInputType(0)
547 , geometryOutputType(0)
550 ~QGLShaderProgramPrivate();
552 QGLSharedResourceGuardBase *programGuard;
555 bool removingShaders;
557 int geometryVertexCount;
558 GLenum geometryInputType;
559 GLenum geometryOutputType;
562 QList<QGLShader *> shaders;
563 QList<QGLShader *> anonShaders;
565 bool hasShader(QGLShader::ShaderType type) const;
569 void freeProgramFunc(QGLContext *ctx, GLuint id)
577 QGLShaderProgramPrivate::~QGLShaderProgramPrivate()
580 programGuard->free();
583 bool QGLShaderProgramPrivate::hasShader(QGLShader::ShaderType type) const
585 foreach (QGLShader *shader, shaders) {
586 if (shader->shaderType() == type)
592 #define ctx QGLContext::currentContext()
595 Constructs a new shader program and attaches it to \a parent.
596 The program will be invalid until addShader() is called.
598 The shader program will be associated with the current QGLContext.
602 QGLShaderProgram::QGLShaderProgram(QObject *parent)
603 : QObject(*new QGLShaderProgramPrivate(QGLContext::currentContext()), parent)
608 Constructs a new shader program and attaches it to \a parent.
609 The program will be invalid until addShader() is called.
611 The shader program will be associated with \a context.
615 QGLShaderProgram::QGLShaderProgram(const QGLContext *context, QObject *parent)
616 : QObject(*new QGLShaderProgramPrivate(context), parent)
621 Deletes this shader program.
623 QGLShaderProgram::~QGLShaderProgram()
627 bool QGLShaderProgram::init()
629 Q_D(QGLShaderProgram);
630 if ((d->programGuard && d->programGuard->id()) || d->inited)
633 QGLContext *context = const_cast<QGLContext *>(QGLContext::currentContext());
636 if (qt_resolve_glsl_extensions(context)) {
637 GLuint program = glCreateProgram();
639 qWarning() << "QGLShaderProgram: could not create shader program";
643 delete d->programGuard;
644 d->programGuard = createSharedResourceGuard(context, program, freeProgramFunc);
647 qWarning() << "QGLShaderProgram: shader programs are not supported";
653 Adds a compiled \a shader to this shader program. Returns true
654 if the shader could be added, or false otherwise.
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.
661 \sa addShaderFromSourceCode(), addShaderFromSourceFile()
662 \sa removeShader(), link(), removeAllShaders()
664 bool QGLShaderProgram::addShader(QGLShader *shader)
666 Q_D(QGLShaderProgram);
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())
674 if (d->programGuard->group() != shader->d_func()->shaderGuard->group()) {
675 qWarning("QGLShaderProgram::addShader: Program and shader are not associated with same context.");
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()));
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().
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.
698 \sa addShader(), addShaderFromSourceFile()
699 \sa removeShader(), link(), log(), removeAllShaders()
701 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const char *source)
703 Q_D(QGLShaderProgram);
706 QGLShader *shader = new QGLShader(type, this);
707 if (!shader->compileSourceCode(source)) {
708 d->log = shader->log();
712 d->anonShaders.append(shader);
713 return addShader(shader);
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().
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.
728 \sa addShader(), addShaderFromSourceFile()
729 \sa removeShader(), link(), log(), removeAllShaders()
731 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const QByteArray& source)
733 return addShaderFromSourceCode(type, source.constData());
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().
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.
748 \sa addShader(), addShaderFromSourceFile()
749 \sa removeShader(), link(), log(), removeAllShaders()
751 bool QGLShaderProgram::addShaderFromSourceCode(QGLShader::ShaderType type, const QString& source)
753 return addShaderFromSourceCode(type, source.toLatin1().constData());
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().
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.
766 \sa addShader(), addShaderFromSourceCode()
768 bool QGLShaderProgram::addShaderFromSourceFile
769 (QGLShader::ShaderType type, const QString& fileName)
771 Q_D(QGLShaderProgram);
774 QGLShader *shader = new QGLShader(type, this);
775 if (!shader->compileSourceFile(fileName)) {
776 d->log = shader->log();
780 d->anonShaders.append(shader);
781 return addShader(shader);
785 Removes \a shader from this shader program. The object is not deleted.
787 The shader program must be valid in the current QGLContext.
789 \sa addShader(), link(), removeAllShaders()
791 void QGLShaderProgram::removeShader(QGLShader *shader)
793 Q_D(QGLShaderProgram);
794 if (d->programGuard && d->programGuard->id()
795 && shader && shader->d_func()->shaderGuard)
797 glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
799 d->linked = false; // Program needs to be relinked.
801 d->shaders.removeAll(shader);
802 d->anonShaders.removeAll(shader);
803 disconnect(shader, SIGNAL(destroyed()), this, SLOT(shaderDestroyed()));
808 Returns a list of all shaders that have been added to this shader
809 program using addShader().
811 \sa addShader(), removeShader()
813 QList<QGLShader *> QGLShaderProgram::shaders() const
815 Q_D(const QGLShaderProgram);
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.
825 \sa addShader(), removeShader()
827 void QGLShaderProgram::removeAllShaders()
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)
835 glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
838 foreach (QGLShader *shader, d->anonShaders) {
839 // Delete shader objects that were created anonymously.
843 d->anonShaders.clear();
844 d->linked = false; // Program needs to be relinked.
845 d->removingShaders = false;
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().
854 Subclasses can override this function to initialize attributes
855 and uniform variables for use in specific shader programs.
857 If the shader program was already linked, calling this
858 function again will force it to be re-linked.
860 \sa addShader(), log()
862 bool QGLShaderProgram::link()
864 Q_D(QGLShaderProgram);
865 GLuint program = d->programGuard ? d->programGuard->id() : 0;
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.
876 glGetProgramiv(program, GL_LINK_STATUS, &value);
877 d->linked = (value != 0);
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);
897 glLinkProgram(program);
899 glGetProgramiv(program, GL_LINK_STATUS, &value);
900 d->linked = (value != 0);
902 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
905 char *logbuf = new char [value];
907 glGetProgramInfoLog(program, value, &len, logbuf);
908 d->log = QString::fromLatin1(logbuf);
909 QString name = objectName();
911 qWarning() << "QGLShader::link:" << d->log;
913 qWarning() << "QGLShader::link[" << name << "]:" << d->log;
920 Returns true if this shader program has been linked; false otherwise.
924 bool QGLShaderProgram::isLinked() const
926 Q_D(const QGLShaderProgram);
931 Returns the errors and warnings that occurred during the last link()
932 or addShader() with explicitly specified source code.
936 QString QGLShaderProgram::log() const
938 Q_D(const QGLShaderProgram);
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().
950 \sa link(), release()
952 bool QGLShaderProgram::bind()
954 Q_D(QGLShaderProgram);
955 GLuint program = d->programGuard ? d->programGuard->id() : 0;
958 if (!d->linked && !link())
961 if (d->programGuard->group() != QOpenGLContextGroup::currentContextGroup()) {
962 qWarning("QGLShaderProgram::bind: program is not valid in the current context.");
966 glUseProgram(program);
971 #define ctx QGLContext::currentContext()
974 Releases the active shader program from the current QGLContext.
975 This is equivalent to calling \c{glUseProgram(0)}.
979 void QGLShaderProgram::release()
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.");
986 #if defined(QT_OPENGL_ES_2)
995 Returns the OpenGL identifier associated with this shader program.
997 \sa QGLShader::shaderId()
999 GLuint QGLShaderProgram::programId() const
1001 Q_D(const QGLShaderProgram);
1002 GLuint id = d->programGuard ? d->programGuard->id() : 0;
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())
1011 return d->programGuard ? d->programGuard->id() : 0;
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.
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.
1023 \sa attributeLocation()
1025 void QGLShaderProgram::bindAttributeLocation(const char *name, int location)
1027 Q_D(QGLShaderProgram);
1028 if (!init() || !d->programGuard || !d->programGuard->id())
1030 glBindAttribLocation(d->programGuard->id(), location, name);
1031 d->linked = false; // Program needs to be relinked.
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.
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.
1045 \sa attributeLocation()
1047 void QGLShaderProgram::bindAttributeLocation(const QByteArray& name, int location)
1049 bindAttributeLocation(name.constData(), location);
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.
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.
1063 \sa attributeLocation()
1065 void QGLShaderProgram::bindAttributeLocation(const QString& name, int location)
1067 bindAttributeLocation(name.toLatin1().constData(), location);
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.
1075 \sa uniformLocation(), bindAttributeLocation()
1077 int QGLShaderProgram::attributeLocation(const char *name) const
1079 Q_D(const QGLShaderProgram);
1080 if (d->linked && d->programGuard && d->programGuard->id()) {
1081 return glGetAttribLocation(d->programGuard->id(), name);
1083 qWarning() << "QGLShaderProgram::attributeLocation(" << name
1084 << "): shader program is not linked";
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.
1096 \sa uniformLocation(), bindAttributeLocation()
1098 int QGLShaderProgram::attributeLocation(const QByteArray& name) const
1100 return attributeLocation(name.constData());
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.
1110 \sa uniformLocation(), bindAttributeLocation()
1112 int QGLShaderProgram::attributeLocation(const QString& name) const
1114 return attributeLocation(name.toLatin1().constData());
1118 Sets the attribute at \a location in the current context to \a value.
1120 \sa setUniformValue()
1122 void QGLShaderProgram::setAttributeValue(int location, GLfloat value)
1124 Q_D(QGLShaderProgram);
1127 glVertexAttrib1fv(location, &value);
1133 Sets the attribute called \a name in the current context to \a value.
1135 \sa setUniformValue()
1137 void QGLShaderProgram::setAttributeValue(const char *name, GLfloat value)
1139 setAttributeValue(attributeLocation(name), value);
1143 Sets the attribute at \a location in the current context to
1144 the 2D vector (\a x, \a y).
1146 \sa setUniformValue()
1148 void QGLShaderProgram::setAttributeValue(int location, GLfloat x, GLfloat y)
1150 Q_D(QGLShaderProgram);
1152 if (location != -1) {
1153 GLfloat values[2] = {x, y};
1154 glVertexAttrib2fv(location, values);
1161 Sets the attribute called \a name in the current context to
1162 the 2D vector (\a x, \a y).
1164 \sa setUniformValue()
1166 void QGLShaderProgram::setAttributeValue(const char *name, GLfloat x, GLfloat y)
1168 setAttributeValue(attributeLocation(name), x, y);
1172 Sets the attribute at \a location in the current context to
1173 the 3D vector (\a x, \a y, \a z).
1175 \sa setUniformValue()
1177 void QGLShaderProgram::setAttributeValue
1178 (int location, GLfloat x, GLfloat y, GLfloat z)
1180 Q_D(QGLShaderProgram);
1182 if (location != -1) {
1183 GLfloat values[3] = {x, y, z};
1184 glVertexAttrib3fv(location, values);
1191 Sets the attribute called \a name in the current context to
1192 the 3D vector (\a x, \a y, \a z).
1194 \sa setUniformValue()
1196 void QGLShaderProgram::setAttributeValue
1197 (const char *name, GLfloat x, GLfloat y, GLfloat z)
1199 setAttributeValue(attributeLocation(name), x, y, z);
1203 Sets the attribute at \a location in the current context to
1204 the 4D vector (\a x, \a y, \a z, \a w).
1206 \sa setUniformValue()
1208 void QGLShaderProgram::setAttributeValue
1209 (int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1211 Q_D(QGLShaderProgram);
1213 if (location != -1) {
1214 GLfloat values[4] = {x, y, z, w};
1215 glVertexAttrib4fv(location, values);
1222 Sets the attribute called \a name in the current context to
1223 the 4D vector (\a x, \a y, \a z, \a w).
1225 \sa setUniformValue()
1227 void QGLShaderProgram::setAttributeValue
1228 (const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1230 setAttributeValue(attributeLocation(name), x, y, z, w);
1234 Sets the attribute at \a location in the current context to \a value.
1236 \sa setUniformValue()
1238 void QGLShaderProgram::setAttributeValue(int location, const QVector2D& value)
1240 Q_D(QGLShaderProgram);
1243 glVertexAttrib2fv(location, reinterpret_cast<const GLfloat *>(&value));
1249 Sets the attribute called \a name in the current context to \a value.
1251 \sa setUniformValue()
1253 void QGLShaderProgram::setAttributeValue(const char *name, const QVector2D& value)
1255 setAttributeValue(attributeLocation(name), value);
1259 Sets the attribute at \a location in the current context to \a value.
1261 \sa setUniformValue()
1263 void QGLShaderProgram::setAttributeValue(int location, const QVector3D& value)
1265 Q_D(QGLShaderProgram);
1268 glVertexAttrib3fv(location, reinterpret_cast<const GLfloat *>(&value));
1274 Sets the attribute called \a name in the current context to \a value.
1276 \sa setUniformValue()
1278 void QGLShaderProgram::setAttributeValue(const char *name, const QVector3D& value)
1280 setAttributeValue(attributeLocation(name), value);
1284 Sets the attribute at \a location in the current context to \a value.
1286 \sa setUniformValue()
1288 void QGLShaderProgram::setAttributeValue(int location, const QVector4D& value)
1290 Q_D(QGLShaderProgram);
1293 glVertexAttrib4fv(location, reinterpret_cast<const GLfloat *>(&value));
1299 Sets the attribute called \a name in the current context to \a value.
1301 \sa setUniformValue()
1303 void QGLShaderProgram::setAttributeValue(const char *name, const QVector4D& value)
1305 setAttributeValue(attributeLocation(name), value);
1309 Sets the attribute at \a location in the current context to \a value.
1311 \sa setUniformValue()
1313 void QGLShaderProgram::setAttributeValue(int location, const QColor& value)
1315 Q_D(QGLShaderProgram);
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);
1327 Sets the attribute called \a name in the current context to \a value.
1329 \sa setUniformValue()
1331 void QGLShaderProgram::setAttributeValue(const char *name, const QColor& value)
1333 setAttributeValue(attributeLocation(name), value);
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.
1343 \sa setUniformValue()
1345 void QGLShaderProgram::setAttributeValue
1346 (int location, const GLfloat *values, int columns, int rows)
1348 Q_D(QGLShaderProgram);
1350 if (rows < 1 || rows > 4) {
1351 qWarning() << "QGLShaderProgram::setAttributeValue: rows" << rows << "not supported";
1354 if (location != -1) {
1355 while (columns-- > 0) {
1357 glVertexAttrib1fv(location, values);
1359 glVertexAttrib2fv(location, values);
1361 glVertexAttrib3fv(location, values);
1363 glVertexAttrib4fv(location, values);
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.
1379 \sa setUniformValue()
1381 void QGLShaderProgram::setAttributeValue
1382 (const char *name, const GLfloat *values, int columns, int rows)
1384 setAttributeValue(attributeLocation(name), values, columns, rows);
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.
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.
1398 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1399 \sa disableAttributeArray()
1401 void QGLShaderProgram::setAttributeArray
1402 (int location, const GLfloat *values, int tupleSize, int stride)
1404 Q_D(QGLShaderProgram);
1406 if (location != -1) {
1407 glVertexAttribPointer(location, tupleSize, GL_FLOAT, GL_FALSE,
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.
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.
1422 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1423 \sa disableAttributeArray()
1425 void QGLShaderProgram::setAttributeArray
1426 (int location, const QVector2D *values, int stride)
1428 Q_D(QGLShaderProgram);
1430 if (location != -1) {
1431 glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE,
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.
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.
1446 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1447 \sa disableAttributeArray()
1449 void QGLShaderProgram::setAttributeArray
1450 (int location, const QVector3D *values, int stride)
1452 Q_D(QGLShaderProgram);
1454 if (location != -1) {
1455 glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
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.
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.
1470 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1471 \sa disableAttributeArray()
1473 void QGLShaderProgram::setAttributeArray
1474 (int location, const QVector4D *values, int stride)
1476 Q_D(QGLShaderProgram);
1478 if (location != -1) {
1479 glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE,
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.
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.
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.
1498 The setAttributeBuffer() function can be used to set the attribute
1499 array to an offset within a vertex buffer.
1501 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1502 \sa disableAttributeArray(), setAttributeBuffer()
1505 void QGLShaderProgram::setAttributeArray
1506 (int location, GLenum type, const void *values, int tupleSize, int stride)
1508 Q_D(QGLShaderProgram);
1510 if (location != -1) {
1511 glVertexAttribPointer(location, tupleSize, type, GL_TRUE,
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.
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.
1529 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1530 \sa disableAttributeArray()
1532 void QGLShaderProgram::setAttributeArray
1533 (const char *name, const GLfloat *values, int tupleSize, int stride)
1535 setAttributeArray(attributeLocation(name), values, tupleSize, stride);
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.
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.
1550 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1551 \sa disableAttributeArray()
1553 void QGLShaderProgram::setAttributeArray
1554 (const char *name, const QVector2D *values, int stride)
1556 setAttributeArray(attributeLocation(name), values, stride);
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.
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.
1571 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1572 \sa disableAttributeArray()
1574 void QGLShaderProgram::setAttributeArray
1575 (const char *name, const QVector3D *values, int stride)
1577 setAttributeArray(attributeLocation(name), values, stride);
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.
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.
1592 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1593 \sa disableAttributeArray()
1595 void QGLShaderProgram::setAttributeArray
1596 (const char *name, const QVector4D *values, int stride)
1598 setAttributeArray(attributeLocation(name), values, stride);
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.
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.
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.
1617 The setAttributeBuffer() function can be used to set the attribute
1618 array to an offset within a vertex buffer.
1620 \sa setAttributeValue(), setUniformValue(), enableAttributeArray()
1621 \sa disableAttributeArray(), setAttributeBuffer()
1624 void QGLShaderProgram::setAttributeArray
1625 (const char *name, GLenum type, const void *values, int tupleSize, int stride)
1627 setAttributeArray(attributeLocation(name), type, values, tupleSize, stride);
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.
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,
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.
1646 \sa setAttributeArray()
1649 void QGLShaderProgram::setAttributeBuffer
1650 (int location, GLenum type, int offset, int tupleSize, int stride)
1652 Q_D(QGLShaderProgram);
1654 if (location != -1) {
1655 glVertexAttribPointer(location, tupleSize, type, GL_TRUE, stride,
1656 reinterpret_cast<const void *>(offset));
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.
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,
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.
1678 \sa setAttributeArray()
1681 void QGLShaderProgram::setAttributeBuffer
1682 (const char *name, GLenum type, int offset, int tupleSize, int stride)
1684 setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride);
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.
1692 \sa disableAttributeArray(), setAttributeArray(), setAttributeValue()
1693 \sa setUniformValue()
1695 void QGLShaderProgram::enableAttributeArray(int location)
1697 Q_D(QGLShaderProgram);
1700 glEnableVertexAttribArray(location);
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.
1710 \sa disableAttributeArray(), setAttributeArray(), setAttributeValue()
1711 \sa setUniformValue()
1713 void QGLShaderProgram::enableAttributeArray(const char *name)
1715 enableAttributeArray(attributeLocation(name));
1719 Disables the vertex array at \a location in this shader program
1720 that was enabled by a previous call to enableAttributeArray().
1722 \sa enableAttributeArray(), setAttributeArray(), setAttributeValue()
1723 \sa setUniformValue()
1725 void QGLShaderProgram::disableAttributeArray(int location)
1727 Q_D(QGLShaderProgram);
1730 glDisableVertexAttribArray(location);
1736 Disables the vertex array called \a name in this shader program
1737 that was enabled by a previous call to enableAttributeArray().
1739 \sa enableAttributeArray(), setAttributeArray(), setAttributeValue()
1740 \sa setUniformValue()
1742 void QGLShaderProgram::disableAttributeArray(const char *name)
1744 disableAttributeArray(attributeLocation(name));
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.
1752 \sa attributeLocation()
1754 int QGLShaderProgram::uniformLocation(const char *name) const
1756 Q_D(const QGLShaderProgram);
1758 if (d->linked && d->programGuard && d->programGuard->id()) {
1759 return glGetUniformLocation(d->programGuard->id(), name);
1761 qWarning() << "QGLShaderProgram::uniformLocation(" << name
1762 << "): shader program is not linked";
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.
1774 \sa attributeLocation()
1776 int QGLShaderProgram::uniformLocation(const QByteArray& name) const
1778 return uniformLocation(name.constData());
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.
1788 \sa attributeLocation()
1790 int QGLShaderProgram::uniformLocation(const QString& name) const
1792 return uniformLocation(name.toLatin1().constData());
1796 Sets the uniform variable at \a location in the current context to \a value.
1798 \sa setAttributeValue()
1800 void QGLShaderProgram::setUniformValue(int location, GLfloat value)
1802 Q_D(QGLShaderProgram);
1805 glUniform1fv(location, 1, &value);
1811 Sets the uniform variable called \a name in the current context
1814 \sa setAttributeValue()
1816 void QGLShaderProgram::setUniformValue(const char *name, GLfloat value)
1818 setUniformValue(uniformLocation(name), value);
1822 Sets the uniform variable at \a location in the current context to \a value.
1824 \sa setAttributeValue()
1826 void QGLShaderProgram::setUniformValue(int location, GLint value)
1828 Q_D(QGLShaderProgram);
1831 glUniform1i(location, value);
1837 Sets the uniform variable called \a name in the current context
1840 \sa setAttributeValue()
1842 void QGLShaderProgram::setUniformValue(const char *name, GLint value)
1844 setUniformValue(uniformLocation(name), value);
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.
1851 \sa setAttributeValue()
1853 void QGLShaderProgram::setUniformValue(int location, GLuint value)
1855 Q_D(QGLShaderProgram);
1858 glUniform1i(location, value);
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.
1867 \sa setAttributeValue()
1869 void QGLShaderProgram::setUniformValue(const char *name, GLuint value)
1871 setUniformValue(uniformLocation(name), value);
1875 Sets the uniform variable at \a location in the current context to
1876 the 2D vector (\a x, \a y).
1878 \sa setAttributeValue()
1880 void QGLShaderProgram::setUniformValue(int location, GLfloat x, GLfloat y)
1882 Q_D(QGLShaderProgram);
1884 if (location != -1) {
1885 GLfloat values[2] = {x, y};
1886 glUniform2fv(location, 1, values);
1893 Sets the uniform variable called \a name in the current context to
1894 the 2D vector (\a x, \a y).
1896 \sa setAttributeValue()
1898 void QGLShaderProgram::setUniformValue(const char *name, GLfloat x, GLfloat y)
1900 setUniformValue(uniformLocation(name), x, y);
1904 Sets the uniform variable at \a location in the current context to
1905 the 3D vector (\a x, \a y, \a z).
1907 \sa setAttributeValue()
1909 void QGLShaderProgram::setUniformValue
1910 (int location, GLfloat x, GLfloat y, GLfloat z)
1912 Q_D(QGLShaderProgram);
1914 if (location != -1) {
1915 GLfloat values[3] = {x, y, z};
1916 glUniform3fv(location, 1, values);
1923 Sets the uniform variable called \a name in the current context to
1924 the 3D vector (\a x, \a y, \a z).
1926 \sa setAttributeValue()
1928 void QGLShaderProgram::setUniformValue
1929 (const char *name, GLfloat x, GLfloat y, GLfloat z)
1931 setUniformValue(uniformLocation(name), x, y, z);
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).
1938 \sa setAttributeValue()
1940 void QGLShaderProgram::setUniformValue
1941 (int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1943 Q_D(QGLShaderProgram);
1945 if (location != -1) {
1946 GLfloat values[4] = {x, y, z, w};
1947 glUniform4fv(location, 1, values);
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).
1957 \sa setAttributeValue()
1959 void QGLShaderProgram::setUniformValue
1960 (const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1962 setUniformValue(uniformLocation(name), x, y, z, w);
1966 Sets the uniform variable at \a location in the current context to \a value.
1968 \sa setAttributeValue()
1970 void QGLShaderProgram::setUniformValue(int location, const QVector2D& value)
1972 Q_D(QGLShaderProgram);
1975 glUniform2fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
1981 Sets the uniform variable called \a name in the current context
1984 \sa setAttributeValue()
1986 void QGLShaderProgram::setUniformValue(const char *name, const QVector2D& value)
1988 setUniformValue(uniformLocation(name), value);
1992 Sets the uniform variable at \a location in the current context to \a value.
1994 \sa setAttributeValue()
1996 void QGLShaderProgram::setUniformValue(int location, const QVector3D& value)
1998 Q_D(QGLShaderProgram);
2001 glUniform3fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
2007 Sets the uniform variable called \a name in the current context
2010 \sa setAttributeValue()
2012 void QGLShaderProgram::setUniformValue(const char *name, const QVector3D& value)
2014 setUniformValue(uniformLocation(name), value);
2018 Sets the uniform variable at \a location in the current context to \a value.
2020 \sa setAttributeValue()
2022 void QGLShaderProgram::setUniformValue(int location, const QVector4D& value)
2024 Q_D(QGLShaderProgram);
2027 glUniform4fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
2033 Sets the uniform variable called \a name in the current context
2036 \sa setAttributeValue()
2038 void QGLShaderProgram::setUniformValue(const char *name, const QVector4D& value)
2040 setUniformValue(uniformLocation(name), value);
2044 Sets the uniform variable at \a location in the current context to
2045 the red, green, blue, and alpha components of \a color.
2047 \sa setAttributeValue()
2049 void QGLShaderProgram::setUniformValue(int location, const QColor& color)
2051 Q_D(QGLShaderProgram);
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);
2063 Sets the uniform variable called \a name in the current context to
2064 the red, green, blue, and alpha components of \a color.
2066 \sa setAttributeValue()
2068 void QGLShaderProgram::setUniformValue(const char *name, const QColor& color)
2070 setUniformValue(uniformLocation(name), color);
2074 Sets the uniform variable at \a location in the current context to
2075 the x and y coordinates of \a point.
2077 \sa setAttributeValue()
2079 void QGLShaderProgram::setUniformValue(int location, const QPoint& point)
2081 Q_D(QGLShaderProgram);
2083 if (location != -1) {
2084 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2085 glUniform2fv(location, 1, values);
2092 Sets the uniform variable associated with \a name in the current
2093 context to the x and y coordinates of \a point.
2095 \sa setAttributeValue()
2097 void QGLShaderProgram::setUniformValue(const char *name, const QPoint& point)
2099 setUniformValue(uniformLocation(name), point);
2103 Sets the uniform variable at \a location in the current context to
2104 the x and y coordinates of \a point.
2106 \sa setAttributeValue()
2108 void QGLShaderProgram::setUniformValue(int location, const QPointF& point)
2110 Q_D(QGLShaderProgram);
2112 if (location != -1) {
2113 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2114 glUniform2fv(location, 1, values);
2121 Sets the uniform variable associated with \a name in the current
2122 context to the x and y coordinates of \a point.
2124 \sa setAttributeValue()
2126 void QGLShaderProgram::setUniformValue(const char *name, const QPointF& point)
2128 setUniformValue(uniformLocation(name), point);
2132 Sets the uniform variable at \a location in the current context to
2133 the width and height of the given \a size.
2135 \sa setAttributeValue()
2137 void QGLShaderProgram::setUniformValue(int location, const QSize& size)
2139 Q_D(QGLShaderProgram);
2141 if (location != -1) {
2142 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2143 glUniform2fv(location, 1, values);
2150 Sets the uniform variable associated with \a name in the current
2151 context to the width and height of the given \a size.
2153 \sa setAttributeValue()
2155 void QGLShaderProgram::setUniformValue(const char *name, const QSize& size)
2157 setUniformValue(uniformLocation(name), size);
2161 Sets the uniform variable at \a location in the current context to
2162 the width and height of the given \a size.
2164 \sa setAttributeValue()
2166 void QGLShaderProgram::setUniformValue(int location, const QSizeF& size)
2168 Q_D(QGLShaderProgram);
2170 if (location != -1) {
2171 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2172 glUniform2fv(location, 1, values);
2179 Sets the uniform variable associated with \a name in the current
2180 context to the width and height of the given \a size.
2182 \sa setAttributeValue()
2184 void QGLShaderProgram::setUniformValue(const char *name, const QSizeF& size)
2186 setUniformValue(uniformLocation(name), size);
2189 // We have to repack matrices from qreal to GLfloat.
2190 #define setUniformMatrix(func,location,value,cols,rows) \
2191 if (location == -1) \
2193 if (sizeof(qreal) == sizeof(GLfloat)) { \
2194 func(location, 1, GL_FALSE, \
2195 reinterpret_cast<const GLfloat *>(value.constData())); \
2197 GLfloat mat[cols * rows]; \
2198 const qreal *data = value.constData(); \
2199 for (int i = 0; i < cols * rows; ++i) \
2201 func(location, 1, GL_FALSE, mat); \
2203 #if !defined(QT_OPENGL_ES_2)
2204 #define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \
2205 if (location == -1) \
2207 if (sizeof(qreal) == sizeof(GLfloat)) { \
2208 const GLfloat *data = reinterpret_cast<const GLfloat *> \
2209 (value.constData()); \
2211 func(location, 1, GL_FALSE, data); \
2213 colfunc(location, cols, data); \
2215 GLfloat mat[cols * rows]; \
2216 const qreal *data = value.constData(); \
2217 for (int i = 0; i < cols * rows; ++i) \
2220 func(location, 1, GL_FALSE, mat); \
2222 colfunc(location, cols, mat); \
2225 #define setUniformGenericMatrix(func,colfunc,location,value,cols,rows) \
2226 if (location == -1) \
2228 if (sizeof(qreal) == sizeof(GLfloat)) { \
2229 const GLfloat *data = reinterpret_cast<const GLfloat *> \
2230 (value.constData()); \
2231 colfunc(location, cols, data); \
2233 GLfloat mat[cols * rows]; \
2234 const qreal *data = value.constData(); \
2235 for (int i = 0; i < cols * rows; ++i) \
2237 colfunc(location, cols, mat); \
2242 Sets the uniform variable at \a location in the current context
2243 to a 2x2 matrix \a value.
2245 \sa setAttributeValue()
2247 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x2& value)
2249 Q_D(QGLShaderProgram);
2251 setUniformMatrix(glUniformMatrix2fv, location, value, 2, 2);
2257 Sets the uniform variable called \a name in the current context
2258 to a 2x2 matrix \a value.
2260 \sa setAttributeValue()
2262 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x2& value)
2264 setUniformValue(uniformLocation(name), value);
2268 Sets the uniform variable at \a location in the current context
2269 to a 2x3 matrix \a value.
2271 \sa setAttributeValue()
2273 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x3& value)
2275 Q_D(QGLShaderProgram);
2277 setUniformGenericMatrix
2278 (glUniformMatrix2x3fv, glUniform3fv, location, value, 2, 3);
2284 Sets the uniform variable called \a name in the current context
2285 to a 2x3 matrix \a value.
2287 \sa setAttributeValue()
2289 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x3& value)
2291 setUniformValue(uniformLocation(name), value);
2295 Sets the uniform variable at \a location in the current context
2296 to a 2x4 matrix \a value.
2298 \sa setAttributeValue()
2300 void QGLShaderProgram::setUniformValue(int location, const QMatrix2x4& value)
2302 Q_D(QGLShaderProgram);
2304 setUniformGenericMatrix
2305 (glUniformMatrix2x4fv, glUniform4fv, location, value, 2, 4);
2311 Sets the uniform variable called \a name in the current context
2312 to a 2x4 matrix \a value.
2314 \sa setAttributeValue()
2316 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x4& value)
2318 setUniformValue(uniformLocation(name), value);
2322 Sets the uniform variable at \a location in the current context
2323 to a 3x2 matrix \a value.
2325 \sa setAttributeValue()
2327 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x2& value)
2329 Q_D(QGLShaderProgram);
2331 setUniformGenericMatrix
2332 (glUniformMatrix3x2fv, glUniform2fv, location, value, 3, 2);
2338 Sets the uniform variable called \a name in the current context
2339 to a 3x2 matrix \a value.
2341 \sa setAttributeValue()
2343 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x2& value)
2345 setUniformValue(uniformLocation(name), value);
2349 Sets the uniform variable at \a location in the current context
2350 to a 3x3 matrix \a value.
2352 \sa setAttributeValue()
2354 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x3& value)
2356 Q_D(QGLShaderProgram);
2358 setUniformMatrix(glUniformMatrix3fv, location, value, 3, 3);
2364 Sets the uniform variable called \a name in the current context
2365 to a 3x3 matrix \a value.
2367 \sa setAttributeValue()
2369 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x3& value)
2371 setUniformValue(uniformLocation(name), value);
2375 Sets the uniform variable at \a location in the current context
2376 to a 3x4 matrix \a value.
2378 \sa setAttributeValue()
2380 void QGLShaderProgram::setUniformValue(int location, const QMatrix3x4& value)
2382 Q_D(QGLShaderProgram);
2384 setUniformGenericMatrix
2385 (glUniformMatrix3x4fv, glUniform4fv, location, value, 3, 4);
2391 Sets the uniform variable called \a name in the current context
2392 to a 3x4 matrix \a value.
2394 \sa setAttributeValue()
2396 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x4& value)
2398 setUniformValue(uniformLocation(name), value);
2402 Sets the uniform variable at \a location in the current context
2403 to a 4x2 matrix \a value.
2405 \sa setAttributeValue()
2407 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x2& value)
2409 Q_D(QGLShaderProgram);
2411 setUniformGenericMatrix
2412 (glUniformMatrix4x2fv, glUniform2fv, location, value, 4, 2);
2418 Sets the uniform variable called \a name in the current context
2419 to a 4x2 matrix \a value.
2421 \sa setAttributeValue()
2423 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x2& value)
2425 setUniformValue(uniformLocation(name), value);
2429 Sets the uniform variable at \a location in the current context
2430 to a 4x3 matrix \a value.
2432 \sa setAttributeValue()
2434 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x3& value)
2436 Q_D(QGLShaderProgram);
2438 setUniformGenericMatrix
2439 (glUniformMatrix4x3fv, glUniform3fv, location, value, 4, 3);
2445 Sets the uniform variable called \a name in the current context
2446 to a 4x3 matrix \a value.
2448 \sa setAttributeValue()
2450 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x3& value)
2452 setUniformValue(uniformLocation(name), value);
2456 Sets the uniform variable at \a location in the current context
2457 to a 4x4 matrix \a value.
2459 \sa setAttributeValue()
2461 void QGLShaderProgram::setUniformValue(int location, const QMatrix4x4& value)
2463 Q_D(QGLShaderProgram);
2465 setUniformMatrix(glUniformMatrix4fv, location, value, 4, 4);
2471 Sets the uniform variable called \a name in the current context
2472 to a 4x4 matrix \a value.
2474 \sa setAttributeValue()
2476 void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x4& value)
2478 setUniformValue(uniformLocation(name), value);
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.
2488 \sa setAttributeValue()
2491 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[2][2])
2493 Q_D(QGLShaderProgram);
2496 glUniformMatrix2fv(location, 1, GL_FALSE, value[0]);
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.
2506 \sa setAttributeValue()
2509 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[3][3])
2511 Q_D(QGLShaderProgram);
2514 glUniformMatrix3fv(location, 1, GL_FALSE, value[0]);
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.
2524 \sa setAttributeValue()
2526 void QGLShaderProgram::setUniformValue(int location, const GLfloat value[4][4])
2528 Q_D(QGLShaderProgram);
2531 glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
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.
2542 \sa setAttributeValue()
2545 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[2][2])
2547 setUniformValue(uniformLocation(name), value);
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.
2557 \sa setAttributeValue()
2560 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[3][3])
2562 setUniformValue(uniformLocation(name), value);
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.
2572 \sa setAttributeValue()
2574 void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[4][4])
2576 setUniformValue(uniformLocation(name), value);
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.
2583 To set a QTransform value as a 4x4 matrix in a shader, use
2584 \c{setUniformValue(location, QMatrix4x4(value))}.
2586 void QGLShaderProgram::setUniformValue(int location, const QTransform& value)
2588 Q_D(QGLShaderProgram);
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())}
2596 glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
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.
2606 To set a QTransform value as a 4x4 matrix in a shader, use
2607 \c{setUniformValue(name, QMatrix4x4(value))}.
2609 void QGLShaderProgram::setUniformValue
2610 (const char *name, const QTransform& value)
2612 setUniformValue(uniformLocation(name), value);
2616 Sets the uniform variable array at \a location in the current
2617 context to the \a count elements of \a values.
2619 \sa setAttributeValue()
2621 void QGLShaderProgram::setUniformValueArray(int location, const GLint *values, int count)
2623 Q_D(QGLShaderProgram);
2626 glUniform1iv(location, count, values);
2632 Sets the uniform variable array called \a name in the current
2633 context to the \a count elements of \a values.
2635 \sa setAttributeValue()
2637 void QGLShaderProgram::setUniformValueArray
2638 (const char *name, const GLint *values, int count)
2640 setUniformValueArray(uniformLocation(name), values, count);
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.
2648 \sa setAttributeValue()
2650 void QGLShaderProgram::setUniformValueArray(int location, const GLuint *values, int count)
2652 Q_D(QGLShaderProgram);
2655 glUniform1iv(location, count, reinterpret_cast<const GLint *>(values));
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.
2665 \sa setAttributeValue()
2667 void QGLShaderProgram::setUniformValueArray
2668 (const char *name, const GLuint *values, int count)
2670 setUniformValueArray(uniformLocation(name), values, count);
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.
2678 \sa setAttributeValue()
2680 void QGLShaderProgram::setUniformValueArray(int location, const GLfloat *values, int count, int tupleSize)
2682 Q_D(QGLShaderProgram);
2684 if (location != -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);
2694 qWarning() << "QGLShaderProgram::setUniformValue: size" << tupleSize << "not supported";
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.
2705 \sa setAttributeValue()
2707 void QGLShaderProgram::setUniformValueArray
2708 (const char *name, const GLfloat *values, int count, int tupleSize)
2710 setUniformValueArray(uniformLocation(name), values, count, tupleSize);
2714 Sets the uniform variable array at \a location in the current
2715 context to the \a count 2D vector elements of \a values.
2717 \sa setAttributeValue()
2719 void QGLShaderProgram::setUniformValueArray(int location, const QVector2D *values, int count)
2721 Q_D(QGLShaderProgram);
2724 glUniform2fv(location, count, reinterpret_cast<const GLfloat *>(values));
2730 Sets the uniform variable array called \a name in the current
2731 context to the \a count 2D vector elements of \a values.
2733 \sa setAttributeValue()
2735 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector2D *values, int count)
2737 setUniformValueArray(uniformLocation(name), values, count);
2741 Sets the uniform variable array at \a location in the current
2742 context to the \a count 3D vector elements of \a values.
2744 \sa setAttributeValue()
2746 void QGLShaderProgram::setUniformValueArray(int location, const QVector3D *values, int count)
2748 Q_D(QGLShaderProgram);
2751 glUniform3fv(location, count, reinterpret_cast<const GLfloat *>(values));
2757 Sets the uniform variable array called \a name in the current
2758 context to the \a count 3D vector elements of \a values.
2760 \sa setAttributeValue()
2762 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector3D *values, int count)
2764 setUniformValueArray(uniformLocation(name), values, count);
2768 Sets the uniform variable array at \a location in the current
2769 context to the \a count 4D vector elements of \a values.
2771 \sa setAttributeValue()
2773 void QGLShaderProgram::setUniformValueArray(int location, const QVector4D *values, int count)
2775 Q_D(QGLShaderProgram);
2778 glUniform4fv(location, count, reinterpret_cast<const GLfloat *>(values));
2784 Sets the uniform variable array called \a name in the current
2785 context to the \a count 4D vector elements of \a values.
2787 \sa setAttributeValue()
2789 void QGLShaderProgram::setUniformValueArray(const char *name, const QVector4D *values, int count)
2791 setUniformValueArray(uniformLocation(name), values, count);
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) \
2798 if (sizeof(type) == sizeof(GLfloat) * cols * rows) { \
2799 func(location, count, GL_FALSE, \
2800 reinterpret_cast<const GLfloat *>(values[0].constData())); \
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]; \
2809 func(location, count, GL_FALSE, temp.constData()); \
2811 #if !defined(QT_OPENGL_ES_2)
2812 #define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
2813 if (location == -1 || count <= 0) \
2815 if (sizeof(type) == sizeof(GLfloat) * cols * rows) { \
2816 const GLfloat *data = reinterpret_cast<const GLfloat *> \
2817 (values[0].constData()); \
2819 func(location, count, GL_FALSE, data); \
2821 colfunc(location, count * cols, data); \
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]; \
2831 func(location, count, GL_FALSE, temp.constData()); \
2833 colfunc(location, count * cols, temp.constData()); \
2836 #define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
2837 if (location == -1 || count <= 0) \
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); \
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]; \
2851 colfunc(location, count * cols, temp.constData()); \
2856 Sets the uniform variable array at \a location in the current
2857 context to the \a count 2x2 matrix elements of \a values.
2859 \sa setAttributeValue()
2861 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x2 *values, int count)
2863 Q_D(QGLShaderProgram);
2865 setUniformMatrixArray
2866 (glUniformMatrix2fv, location, values, count, QMatrix2x2, 2, 2);
2872 Sets the uniform variable array called \a name in the current
2873 context to the \a count 2x2 matrix elements of \a values.
2875 \sa setAttributeValue()
2877 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x2 *values, int count)
2879 setUniformValueArray(uniformLocation(name), values, count);
2883 Sets the uniform variable array at \a location in the current
2884 context to the \a count 2x3 matrix elements of \a values.
2886 \sa setAttributeValue()
2888 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x3 *values, int count)
2890 Q_D(QGLShaderProgram);
2892 setUniformGenericMatrixArray
2893 (glUniformMatrix2x3fv, glUniform3fv, location, values, count,
2900 Sets the uniform variable array called \a name in the current
2901 context to the \a count 2x3 matrix elements of \a values.
2903 \sa setAttributeValue()
2905 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x3 *values, int count)
2907 setUniformValueArray(uniformLocation(name), values, count);
2911 Sets the uniform variable array at \a location in the current
2912 context to the \a count 2x4 matrix elements of \a values.
2914 \sa setAttributeValue()
2916 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x4 *values, int count)
2918 Q_D(QGLShaderProgram);
2920 setUniformGenericMatrixArray
2921 (glUniformMatrix2x4fv, glUniform4fv, location, values, count,
2928 Sets the uniform variable array called \a name in the current
2929 context to the \a count 2x4 matrix elements of \a values.
2931 \sa setAttributeValue()
2933 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x4 *values, int count)
2935 setUniformValueArray(uniformLocation(name), values, count);
2939 Sets the uniform variable array at \a location in the current
2940 context to the \a count 3x2 matrix elements of \a values.
2942 \sa setAttributeValue()
2944 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x2 *values, int count)
2946 Q_D(QGLShaderProgram);
2948 setUniformGenericMatrixArray
2949 (glUniformMatrix3x2fv, glUniform2fv, location, values, count,
2956 Sets the uniform variable array called \a name in the current
2957 context to the \a count 3x2 matrix elements of \a values.
2959 \sa setAttributeValue()
2961 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x2 *values, int count)
2963 setUniformValueArray(uniformLocation(name), values, count);
2967 Sets the uniform variable array at \a location in the current
2968 context to the \a count 3x3 matrix elements of \a values.
2970 \sa setAttributeValue()
2972 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x3 *values, int count)
2974 Q_D(QGLShaderProgram);
2976 setUniformMatrixArray
2977 (glUniformMatrix3fv, location, values, count, QMatrix3x3, 3, 3);
2983 Sets the uniform variable array called \a name in the current
2984 context to the \a count 3x3 matrix elements of \a values.
2986 \sa setAttributeValue()
2988 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x3 *values, int count)
2990 setUniformValueArray(uniformLocation(name), values, count);
2994 Sets the uniform variable array at \a location in the current
2995 context to the \a count 3x4 matrix elements of \a values.
2997 \sa setAttributeValue()
2999 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x4 *values, int count)
3001 Q_D(QGLShaderProgram);
3003 setUniformGenericMatrixArray
3004 (glUniformMatrix3x4fv, glUniform4fv, location, values, count,
3011 Sets the uniform variable array called \a name in the current
3012 context to the \a count 3x4 matrix elements of \a values.
3014 \sa setAttributeValue()
3016 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x4 *values, int count)
3018 setUniformValueArray(uniformLocation(name), values, count);
3022 Sets the uniform variable array at \a location in the current
3023 context to the \a count 4x2 matrix elements of \a values.
3025 \sa setAttributeValue()
3027 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x2 *values, int count)
3029 Q_D(QGLShaderProgram);
3031 setUniformGenericMatrixArray
3032 (glUniformMatrix4x2fv, glUniform2fv, location, values, count,
3039 Sets the uniform variable array called \a name in the current
3040 context to the \a count 4x2 matrix elements of \a values.
3042 \sa setAttributeValue()
3044 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x2 *values, int count)
3046 setUniformValueArray(uniformLocation(name), values, count);
3050 Sets the uniform variable array at \a location in the current
3051 context to the \a count 4x3 matrix elements of \a values.
3053 \sa setAttributeValue()
3055 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x3 *values, int count)
3057 Q_D(QGLShaderProgram);
3059 setUniformGenericMatrixArray
3060 (glUniformMatrix4x3fv, glUniform3fv, location, values, count,
3067 Sets the uniform variable array called \a name in the current
3068 context to the \a count 4x3 matrix elements of \a values.
3070 \sa setAttributeValue()
3072 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x3 *values, int count)
3074 setUniformValueArray(uniformLocation(name), values, count);
3078 Sets the uniform variable array at \a location in the current
3079 context to the \a count 4x4 matrix elements of \a values.
3081 \sa setAttributeValue()
3083 void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x4 *values, int count)
3085 Q_D(QGLShaderProgram);
3087 setUniformMatrixArray
3088 (glUniformMatrix4fv, location, values, count, QMatrix4x4, 4, 4);
3094 Sets the uniform variable array called \a name in the current
3095 context to the \a count 4x4 matrix elements of \a values.
3097 \sa setAttributeValue()
3099 void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x4 *values, int count)
3101 setUniformValueArray(uniformLocation(name), values, count);
3107 Returns the hardware limit for how many vertices a geometry shader
3112 \sa setGeometryOutputVertexCount()
3114 int QGLShaderProgram::maxGeometryOutputVertices() const
3117 glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &n);
3122 Sets the maximum number of vertices the current geometry shader
3123 program will produce, if active, to \a count.
3127 This parameter takes effect the next time the program is linked.
3129 void QGLShaderProgram::setGeometryOutputVertexCount(int count)
3132 int max = maxGeometryOutputVertices();
3134 qWarning("QGLShaderProgram::setGeometryOutputVertexCount: count: %d higher than maximum: %d",
3138 d_func()->geometryVertexCount = count;
3143 Returns the maximum number of vertices the current geometry shader
3144 program will produce, if active.
3148 This parameter takes effect the ntext time the program is linked.
3150 int QGLShaderProgram::geometryOutputVertexCount() const
3152 return d_func()->geometryVertexCount;
3157 Sets the input type from \a inputType.
3159 This parameter takes effect the next time the program is linked.
3161 void QGLShaderProgram::setGeometryInputType(GLenum inputType)
3163 d_func()->geometryInputType = inputType;
3168 Returns the geometry shader input type, if active.
3170 This parameter takes effect the next time the program is linked.
3175 GLenum QGLShaderProgram::geometryInputType() const
3177 return d_func()->geometryInputType;
3182 Sets the output type from the geometry shader, if active, to
3185 This parameter takes effect the next time the program is linked.
3189 void QGLShaderProgram::setGeometryOutputType(GLenum outputType)
3191 d_func()->geometryOutputType = outputType;
3196 Returns the geometry shader output type, if active.
3198 This parameter takes effect the next time the program is linked.
3202 GLenum QGLShaderProgram::geometryOutputType() const
3204 return d_func()->geometryOutputType;
3209 Returns true if shader programs written in the OpenGL Shading
3210 Language (GLSL) are supported on this system; false otherwise.
3212 The \a context is used to resolve the GLSL extensions.
3213 If \a context is null, then QGLContext::currentContext() is used.
3215 bool QGLShaderProgram::hasOpenGLShaderPrograms(const QGLContext *context)
3217 #if !defined(QT_OPENGL_ES_2)
3219 context = QGLContext::currentContext();
3222 return qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
3232 void QGLShaderProgram::shaderDestroyed()
3234 Q_D(QGLShaderProgram);
3235 QGLShader *shader = qobject_cast<QGLShader *>(sender());
3236 if (shader && !d->removingShaders)
3237 removeShader(shader);
3245 Returns true if shader programs of type \a type are supported on
3246 this system; false otherwise.
3248 The \a context is used to resolve the GLSL extensions.
3249 If \a context is null, then QGLContext::currentContext() is used.
3253 bool QGLShader::hasOpenGLShaders(ShaderType type, const QGLContext *context)
3256 context = QGLContext::currentContext();
3260 if ((type & ~(Geometry | Vertex | Fragment)) || type == 0)
3263 bool resolved = qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
3267 if ((type & Geometry) && !QByteArray((const char *) glGetString(GL_EXTENSIONS)).contains("GL_EXT_geometry_shader4"))