6#include "private/qopenglcontext_p.h"
7#include <QtOpenGL/QOpenGLVersionFunctionsFactory>
8#include <QtCore/private/qobject_p.h>
9#include <QtCore/qdebug.h>
10#include <QtCore/qfile.h>
11#include <QtCore/qlist.h>
12#include <QtCore/qloggingcategory.h>
13#include <QtCore/qvarlengtharray.h>
14#include <QtGui/private/qopenglprogrambinarycache_p.h>
15#include <QtGui/qtransform.h>
16#include <QtGui/QColor>
17#include <QtGui/QSurfaceFormat>
19#if !QT_CONFIG(opengles2)
20#include <QtOpenGL/qopenglfunctions_4_0_core.h>
28using namespace Qt::StringLiterals;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
117
118
119
120
121
122
123
124
125
126
127
128
129
130
133
134
135
136
137
138
139
140
141
142
143
144
145
146
149#ifndef GL_GEOMETRY_SHADER
150#define GL_GEOMETRY_SHADER 0x8DD9
152#ifndef GL_TESS_CONTROL_SHADER
153#define GL_TESS_CONTROL_SHADER 0x8E88
155#ifndef GL_TESS_EVALUATION_SHADER
156#define GL_TESS_EVALUATION_SHADER 0x8E87
158#ifndef GL_COMPUTE_SHADER
159#define GL_COMPUTE_SHADER 0x91B9
161#ifndef GL_MAX_GEOMETRY_OUTPUT_VERTICES
162#define GL_MAX_GEOMETRY_OUTPUT_VERTICES 0x8DE0
164#ifndef GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS
165#define GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS 0x8DE1
167#ifndef GL_PATCH_VERTICES
168#define GL_PATCH_VERTICES 0x8E72
170#ifndef GL_PATCH_DEFAULT_OUTER_LEVEL
171#define GL_PATCH_DEFAULT_OUTER_LEVEL 0x8E74
173#ifndef GL_PATCH_DEFAULT_INNER_LEVEL
174#define GL_PATCH_DEFAULT_INNER_LEVEL 0x8E73
177#if !QT_CONFIG(opengles2)
178static inline bool isFormatGLES(
const QSurfaceFormat &f)
180 return (f.renderableType() == QSurfaceFormat::OpenGLES);
186 return f.version() >= std::pair(3, 2);
191#if !QT_CONFIG(opengles2)
192 if (!isFormatGLES(f))
193 return f.version() >= std::pair(4, 3);
195 return f.version() >= std::pair(3, 1);
197 return f.version() >= std::pair(3, 1);
203#if !QT_CONFIG(opengles2)
204 if (!isFormatGLES(f))
205 return f.version() >= std::pair(4, 0);
207 return f.version() >= std::pair(3, 2);
209 return f.version() >= std::pair(3, 2);
215 Q_DECLARE_PUBLIC(QOpenGLShader)
256 void freeShaderFunc(QOpenGLFunctions *funcs, GLuint id)
258 funcs->glDeleteShader(id);
271 QOpenGLContext *context =
const_cast<QOpenGLContext *>(QOpenGLContext::currentContext());
275 if (shaderType == QOpenGLShader::Vertex) {
276 shader = glfuncs->glCreateShader(GL_VERTEX_SHADER);
277 }
else if (shaderType == QOpenGLShader::Geometry && supportsGeometryShaders) {
279 }
else if (shaderType == QOpenGLShader::TessellationControl && supportsTessellationShaders) {
281 }
else if (shaderType == QOpenGLShader::TessellationEvaluation && supportsTessellationShaders) {
283 }
else if (shaderType == QOpenGLShader::Compute && supportsComputeShaders) {
285 }
else if (shaderType == QOpenGLShader::Fragment) {
286 shader = glfuncs->glCreateShader(GL_FRAGMENT_SHADER);
289 qWarning(
"QOpenGLShader: could not create shader");
292 shaderGuard =
new QOpenGLSharedResourceGuard(context, shader, freeShaderFunc);
298 GLuint shader = shaderGuard ? shaderGuard->id() : 0;
303 glfuncs->glCompileShader(shader);
307 glfuncs->glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
308 compiled = (value != 0);
312 QString name = q->objectName();
314 const char *types[] = {
318 "Tessellation Control",
319 "Tessellation Evaluation",
324 const char *type = types[6];
325 switch (shaderType) {
326 case QOpenGLShader::Fragment:
327 type = types[0];
break;
328 case QOpenGLShader::Vertex:
329 type = types[1];
break;
330 case QOpenGLShader::Geometry:
331 type = types[2];
break;
332 case QOpenGLShader::TessellationControl:
333 type = types[3];
break;
334 case QOpenGLShader::TessellationEvaluation:
335 type = types[4];
break;
336 case QOpenGLShader::Compute:
337 type = types[5];
break;
341 GLint infoLogLength = 0;
342 GLint sourceCodeLength = 0;
343 char *logBuffer =
nullptr;
344 char *sourceCodeBuffer =
nullptr;
347 glfuncs->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
349 if (infoLogLength > 1) {
351 logBuffer =
new char [infoLogLength];
352 glfuncs->glGetShaderInfoLog(shader, infoLogLength, &temp, logBuffer);
356 glfuncs->glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &sourceCodeLength);
358 if (sourceCodeLength > 1) {
360 sourceCodeBuffer =
new char [sourceCodeLength];
361 glfuncs->glGetShaderSource(shader, sourceCodeLength, &temp, sourceCodeBuffer);
365 log = QString::fromLatin1(logBuffer);
370 qWarning(
"QOpenGLShader::compile(%s): %s", type, qPrintable(log));
372 qWarning(
"QOpenGLShader::compile(%s)[%s]: %s", type, qPrintable(name), qPrintable(log));
375 if (sourceCodeBuffer) {
376 qWarning(
"*** Problematic %s shader source code ***\n"
379 type, qUtf16Printable(QString::fromLatin1(sourceCodeBuffer)));
384 delete [] sourceCodeBuffer;
394 shaderGuard =
nullptr;
399
400
401
402
403
404
405
406
407
408
409
410QOpenGLShader::QOpenGLShader(QOpenGLShader::ShaderType type, QObject *parent)
411 : QObject(*
new QOpenGLShaderPrivate(QOpenGLContext::currentContext(), type), parent)
418
419
420
421
422QOpenGLShader::~QOpenGLShader()
427
428
429QOpenGLShader::ShaderType QOpenGLShader::shaderType()
const
431 Q_D(
const QOpenGLShader);
432 return d->shaderType;
440#if QT_CONFIG(opengles2) && !defined(QT_OPENGL_FORCE_SHADER_DEFINES)
443#define QOpenGL_REDEFINE_HIGHP 1
444static const char redefineHighp[] =
445 "#ifndef GL_FRAGMENT_PRECISION_HIGH\n"
446 "#define highp mediump\n"
452 "#ifdef GL_KHR_blend_equation_advanced\n"
453 "#extension GL_ARB_fragment_coord_conventions : enable\n"
454 "#extension GL_KHR_blend_equation_advanced : enable\n"
486 PreprocessorDirective,
491 } state = StartOfLine;
493 const char *c = source;
496 case PreprocessorDirective:
497 if (*c ==
' ' || *c ==
'\t')
499 if (!strncmp(c,
"version", strlen(
"version"))) {
501 c += strlen(
"version");
502 while (*c && *c !=
'\n')
504 int splitPosition = c - source + 1;
505 int linePosition =
int(
std::count(source, c,
'\n')) + 1;
507 }
else if (*c ==
'/')
508 state = CommentStarting;
515 if (*c ==
' ' || *c ==
'\t')
517 else if (*c ==
'#') {
518 state = PreprocessorDirective;
525 state = CommentStarting;
529 case CommentStarting:
531 state = MultiLineComment;
533 state = SingleLineComment;
537 case MultiLineComment:
539 state = CommentEnding;
541 case SingleLineComment:
549 state = MultiLineComment;
559
560
561
562
563
564bool QOpenGLShader::compileSourceCode(
const char *source)
575 if (d->shaderGuard && d->shaderGuard->id() && source) {
576 const QVersionDirectivePosition versionDirectivePosition = findVersionDirectivePosition(source);
578 QVarLengthArray<
const char *, 5> sourceChunks;
579 QVarLengthArray<GLint, 5> sourceChunkLengths;
580 QOpenGLContext *ctx = QOpenGLContext::currentContext();
582 if (versionDirectivePosition.hasPosition()) {
584 sourceChunks.append(source);
585 sourceChunkLengths.append(GLint(versionDirectivePosition.position));
588 if (ctx->format().profile() == QSurfaceFormat::CompatibilityProfile) {
589 const char *vendor =
reinterpret_cast<
const char *>(ctx->functions()->glGetString(GL_VENDOR));
590 if (vendor && !strcmp(vendor,
"Intel")) {
591 static const char version110[] =
"#version 110\n";
592 sourceChunks.append(version110);
593 sourceChunkLengths.append(GLint(
sizeof(version110)) - 1);
597 if (d->shaderType == Fragment) {
598 sourceChunks.append(blendEquationAdvancedHeader);
599 sourceChunkLengths.append(GLint(
sizeof(blendEquationAdvancedHeader) - 1));
604 const QSurfaceFormat currentSurfaceFormat = ctx->format();
605 QOpenGLContextPrivate *ctx_d = QOpenGLContextPrivate::get(QOpenGLContext::currentContext());
606 if (currentSurfaceFormat.renderableType() == QSurfaceFormat::OpenGL
607 || ctx_d->workaround_missingPrecisionQualifiers
608#ifdef QT_OPENGL_FORCE_SHADER_DEFINES
612 sourceChunks.append(qualifierDefines);
613 sourceChunkLengths.append(GLint(
sizeof(qualifierDefines) - 1));
616#ifdef QOpenGL_REDEFINE_HIGHP
617 if (d->shaderType == Fragment && !ctx_d->workaround_missingPrecisionQualifiers
618 && QOpenGLContext::currentContext()->isOpenGLES()) {
619 sourceChunks.append(redefineHighp);
620 sourceChunkLengths.append(GLint(
sizeof(redefineHighp) - 1));
624 QByteArray lineDirective;
627 const char *version =
reinterpret_cast<
const char *>(ctx->functions()->glGetString(GL_VERSION));
628 if (!version || !strstr(version,
"2.1 Mesa 8")) {
630 lineDirective = QStringLiteral(
"#line %1\n").arg(versionDirectivePosition.line).toUtf8();
631 sourceChunks.append(lineDirective.constData());
632 sourceChunkLengths.append(GLint(lineDirective.size()));
636 sourceChunks.append(source + versionDirectivePosition.position);
637 sourceChunkLengths.append(GLint(qstrlen(source + versionDirectivePosition.position)));
639 d->glfuncs->glShaderSource(d->shaderGuard->id(), sourceChunks.size(), sourceChunks.data(), sourceChunkLengths.data());
640 return d->compile(
this);
647
648
649
650
651
652
653
654bool QOpenGLShader::compileSourceCode(
const QByteArray& source)
656 return compileSourceCode(source.constData());
660
661
662
663
664
665
666
667bool QOpenGLShader::compileSourceCode(
const QString& source)
669 return compileSourceCode(source.toLatin1().constData());
673
674
675
676
677
678
679bool QOpenGLShader::compileSourceFile(
const QString& fileName)
681 QFile file(fileName);
682 if (!file.open(QFile::ReadOnly)) {
683 qWarning() <<
"QOpenGLShader: Unable to open file" << fileName;
687 QByteArray contents = file.readAll();
688 return compileSourceCode(contents.constData());
692
693
694
695
696QByteArray QOpenGLShader::sourceCode()
const
698 Q_D(
const QOpenGLShader);
699 GLuint shader = d->shaderGuard ? d->shaderGuard->id() : 0;
703 d->glfuncs->glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &size);
707 char *source =
new char [size];
708 d->glfuncs->glGetShaderSource(shader, size, &len, source);
709 QByteArray src(source);
715
716
717
718
719bool QOpenGLShader::isCompiled()
const
721 Q_D(
const QOpenGLShader);
726
727
728
729
730QString QOpenGLShader::log()
const
732 Q_D(
const QOpenGLShader);
737
738
739
740
741GLuint QOpenGLShader::shaderId()
const
743 Q_D(
const QOpenGLShader);
744 return d->shaderGuard ? d->shaderGuard->id() : 0;
749 Q_DECLARE_PUBLIC(QOpenGLShaderProgram)
757#if !QT_CONFIG(opengles2)
775#if !QT_CONFIG(opengles2)
791 void freeProgramFunc(QOpenGLFunctions *funcs, GLuint id)
793 funcs->glDeleteProgram(id);
802 programGuard->free();
807 for (QOpenGLShader *shader : shaders) {
808 if (shader->shaderType() == type)
815
816
817
818
819
820
821
822QOpenGLShaderProgram::QOpenGLShaderProgram(QObject *parent)
823 : QObject(*
new QOpenGLShaderProgramPrivate, parent)
828
829
830QOpenGLShaderProgram::~QOpenGLShaderProgram()
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849bool QOpenGLShaderProgram::create()
854bool QOpenGLShaderProgram::init()
856 Q_D(QOpenGLShaderProgram);
857 if ((d->programGuard && d->programGuard->id()) || d->inited)
860 QOpenGLContext *context =
const_cast<QOpenGLContext *>(QOpenGLContext::currentContext());
863 d->glfuncs->initializeOpenGLFunctions();
865#if !QT_CONFIG(opengles2)
866 if (!context->isOpenGLES() && context->format().version() >= std::pair(4, 0)) {
867 d->tessellationFuncs = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_4_0_Core>(context);
868 d->tessellationFuncs->initializeOpenGLFunctions();
872 GLuint program = d->glfuncs->glCreateProgram();
874 qWarning(
"QOpenGLShaderProgram: could not create shader program");
878 delete d->programGuard;
879 d->programGuard =
new QOpenGLSharedResourceGuard(context, program, freeProgramFunc);
884
885
886
887
888
889
890
891
892
893
894
895bool QOpenGLShaderProgram::addShader(QOpenGLShader *shader)
897 Q_D(QOpenGLShaderProgram);
900 if (d->shaders.contains(shader))
902 if (d->programGuard && d->programGuard->id() && shader) {
903 if (!shader->d_func()->shaderGuard || !shader->d_func()->shaderGuard->id())
905 if (d->programGuard->group() != shader->d_func()->shaderGuard->group()) {
906 qWarning(
"QOpenGLShaderProgram::addShader: Program and shader are not associated with same context.");
909 d->glfuncs->glAttachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
911 d->shaders.append(shader);
912 connect(shader, SIGNAL(destroyed()),
this, SLOT(shaderDestroyed()));
920
921
922
923
924
925
926
927
928
929
930
931
932bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const char *source)
934 Q_D(QOpenGLShaderProgram);
937 QOpenGLShader *shader =
new QOpenGLShader(type,
this);
938 if (!shader->compileSourceCode(source)) {
939 d->log = shader->log();
943 d->anonShaders.append(shader);
944 return addShader(shader);
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QByteArray& source)
964 return addShaderFromSourceCode(type, source.constData());
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QString& source)
984 return addShaderFromSourceCode(type, source.toLatin1().constData());
988
989
990
991
992
993
994
995
996
997
998
999bool QOpenGLShaderProgram::addShaderFromSourceFile
1000 (QOpenGLShader::ShaderType type,
const QString& fileName)
1002 Q_D(QOpenGLShaderProgram);
1005 QOpenGLShader *shader =
new QOpenGLShader(type,
this);
1006 if (!shader->compileSourceFile(fileName)) {
1007 d->log = shader->log();
1011 d->anonShaders.append(shader);
1012 return addShader(shader);
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const char *source)
1035 Q_D(QOpenGLShaderProgram);
1038 if (d->isCacheDisabled())
1039 return addShaderFromSourceCode(type, source);
1041 return addCacheableShaderFromSourceCode(type, QByteArray(source));
1047 case QOpenGLShader::Vertex:
1048 return QShader::VertexStage;
1049 case QOpenGLShader::Fragment:
1050 return QShader::FragmentStage;
1051 case QOpenGLShader::Geometry:
1052 return QShader::GeometryStage;
1053 case QOpenGLShader::TessellationControl:
1054 return QShader::TessellationControlStage;
1055 case QOpenGLShader::TessellationEvaluation:
1056 return QShader::TessellationEvaluationStage;
1057 case QOpenGLShader::Compute:
1058 return QShader::ComputeStage;
1060 return QShader::VertexStage;
1066 case QShader::VertexStage:
1067 return QOpenGLShader::Vertex;
1068 case QShader::TessellationControlStage:
1069 return QOpenGLShader::TessellationControl;
1070 case QShader::TessellationEvaluationStage:
1071 return QOpenGLShader::TessellationEvaluation;
1072 case QShader::GeometryStage:
1073 return QOpenGLShader::Geometry;
1074 case QShader::FragmentStage:
1075 return QOpenGLShader::Fragment;
1076 case QShader::ComputeStage:
1077 return QOpenGLShader::Compute;
1079 return QOpenGLShader::Vertex;
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QByteArray &source)
1104 Q_D(QOpenGLShaderProgram);
1107 if (d->isCacheDisabled())
1108 return addShaderFromSourceCode(type, source);
1110 d->binaryProgram.shaders.append(QOpenGLProgramBinaryCache::ShaderDesc(qt_shaderTypeToStage(type), source));
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QString &source)
1132 Q_D(QOpenGLShaderProgram);
1135 if (d->isCacheDisabled())
1136 return addShaderFromSourceCode(type, source);
1138 return addCacheableShaderFromSourceCode(type, source.toUtf8().constData());
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159bool QOpenGLShaderProgram::addCacheableShaderFromSourceFile(QOpenGLShader::ShaderType type,
const QString &fileName)
1161 Q_D(QOpenGLShaderProgram);
1164 if (d->isCacheDisabled())
1165 return addShaderFromSourceFile(type, fileName);
1167 QOpenGLProgramBinaryCache::ShaderDesc shader(qt_shaderTypeToStage(type));
1175 if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
1176 shader.source = f.readAll();
1179 qWarning(
"QOpenGLShaderProgram: Unable to open file %s", qPrintable(fileName));
1182 d->binaryProgram.shaders.append(shader);
1187
1188
1189
1190
1191
1192
1193void QOpenGLShaderProgram::removeShader(QOpenGLShader *shader)
1195 Q_D(QOpenGLShaderProgram);
1196 if (d->programGuard && d->programGuard->id()
1197 && shader && shader->d_func()->shaderGuard)
1199 d->glfuncs->glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
1203 d->shaders.removeAll(shader);
1204 d->anonShaders.removeAll(shader);
1205 disconnect(shader, SIGNAL(destroyed()),
this, SLOT(shaderDestroyed()));
1210
1211
1212
1213
1214
1215QList<QOpenGLShader *> QOpenGLShaderProgram::shaders()
const
1217 Q_D(
const QOpenGLShaderProgram);
1222
1223
1224
1225
1226
1227
1228
1229void QOpenGLShaderProgram::removeAllShaders()
1231 Q_D(QOpenGLShaderProgram);
1232 d->removingShaders =
true;
1233 for (QOpenGLShader *shader : std::as_const(d->shaders)) {
1234 if (d->programGuard && d->programGuard->id()
1235 && shader && shader->d_func()->shaderGuard)
1237 d->glfuncs->glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
1241 qDeleteAll(d->anonShaders);
1243 d->anonShaders.clear();
1244 d->binaryProgram = QOpenGLProgramBinaryCache::ProgramDesc();
1246 d->removingShaders =
false;
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273bool QOpenGLShaderProgram::link()
1275 Q_D(QOpenGLShaderProgram);
1276 GLuint program = d->programGuard ? d->programGuard->id() : 0;
1280 if (!d->linkBinaryRecursion && d->shaders.isEmpty() && !d->binaryProgram.shaders.isEmpty())
1281 return d->linkBinary();
1284 if (d->shaders.isEmpty()) {
1292 d->glfuncs->glGetProgramiv(program, GL_LINK_STATUS, &value);
1293 d->linked = (value != 0);
1298 d->glfuncs->glLinkProgram(program);
1300 d->glfuncs->glGetProgramiv(program, GL_LINK_STATUS, &value);
1301 d->linked = (value != 0);
1303 d->glfuncs->glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
1306 char *logbuf =
new char [value];
1308 d->glfuncs->glGetProgramInfoLog(program, value, &len, logbuf);
1309 d->log = QString::fromLatin1(logbuf);
1310 if (!d->linked && !d->linkBinaryRecursion) {
1311 QString name = objectName();
1313 qWarning(
"QOpenGLShader::link: %ls", qUtf16Printable(d->log));
1315 qWarning(
"QOpenGLShader::link[%ls]: %ls", qUtf16Printable(name), qUtf16Printable(d->log));
1323
1324
1325
1326
1327bool QOpenGLShaderProgram::isLinked()
const
1329 Q_D(
const QOpenGLShaderProgram);
1334
1335
1336
1337
1338
1339QString QOpenGLShaderProgram::log()
const
1341 Q_D(
const QOpenGLShaderProgram);
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355bool QOpenGLShaderProgram::bind()
1357 Q_D(QOpenGLShaderProgram);
1358 GLuint program = d->programGuard ? d->programGuard->id() : 0;
1361 if (!d->linked && !link())
1364 if (d->programGuard->group() != QOpenGLContextGroup::currentContextGroup()) {
1365 qWarning(
"QOpenGLShaderProgram::bind: program is not valid in the current context.");
1369 d->glfuncs->glUseProgram(program);
1374
1375
1376
1377
1378
1379void QOpenGLShaderProgram::release()
1381 Q_D(QOpenGLShaderProgram);
1383 if (d->programGuard && d->programGuard->group() != QOpenGLContextGroup::currentContextGroup())
1384 qWarning(
"QOpenGLShaderProgram::release: program is not valid in the current context.");
1386 d->glfuncs->glUseProgram(0);
1390
1391
1392
1393
1394GLuint QOpenGLShaderProgram::programId()
const
1396 Q_D(
const QOpenGLShaderProgram);
1397 GLuint id = d->programGuard ? d->programGuard->id() : 0;
1404 if (!
const_cast<QOpenGLShaderProgram *>(
this)->init())
1406 return d->programGuard ? d->programGuard->id() : 0;
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420void QOpenGLShaderProgram::bindAttributeLocation(
const char *name,
int location)
1422 Q_D(QOpenGLShaderProgram);
1423 if (!init() || !d->programGuard || !d->programGuard->id())
1425 d->glfuncs->glBindAttribLocation(d->programGuard->id(), location, name);
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442void QOpenGLShaderProgram::bindAttributeLocation(
const QByteArray& name,
int location)
1444 bindAttributeLocation(name.constData(), location);
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460void QOpenGLShaderProgram::bindAttributeLocation(
const QString& name,
int location)
1462 bindAttributeLocation(name.toLatin1().constData(), location);
1466
1467
1468
1469
1470
1471
1472int QOpenGLShaderProgram::attributeLocation(
const char *name)
const
1474 Q_D(
const QOpenGLShaderProgram);
1475 if (d->linked && d->programGuard && d->programGuard->id()) {
1476 return d->glfuncs->glGetAttribLocation(d->programGuard->id(), name);
1478 qWarning(
"QOpenGLShaderProgram::attributeLocation(%s): shader program is not linked", name);
1484
1485
1486
1487
1488
1489
1490
1491
1492int QOpenGLShaderProgram::attributeLocation(
const QByteArray& name)
const
1494 return attributeLocation(name.constData());
1498
1499
1500
1501
1502
1503
1504
1505
1506int QOpenGLShaderProgram::attributeLocation(
const QString& name)
const
1508 return attributeLocation(name.toLatin1().constData());
1512
1513
1514
1515
1516void QOpenGLShaderProgram::setAttributeValue(
int location, GLfloat value)
1518 Q_D(QOpenGLShaderProgram);
1520 d->glfuncs->glVertexAttrib1fv(location, &value);
1524
1525
1526
1527
1528
1529
1530void QOpenGLShaderProgram::setAttributeValue(
const char *name, GLfloat value)
1532 setAttributeValue(attributeLocation(name), value);
1536
1537
1538
1539
1540
1541void QOpenGLShaderProgram::setAttributeValue(
int location, GLfloat x, GLfloat y)
1543 Q_D(QOpenGLShaderProgram);
1544 if (location != -1) {
1545 GLfloat values[2] = {x, y};
1546 d->glfuncs->glVertexAttrib2fv(location, values);
1551
1552
1553
1554
1555
1556
1557
1558void QOpenGLShaderProgram::setAttributeValue(
const char *name, GLfloat x, GLfloat y)
1560 setAttributeValue(attributeLocation(name), x, y);
1564
1565
1566
1567
1568
1569void QOpenGLShaderProgram::setAttributeValue
1570 (
int location, GLfloat x, GLfloat y, GLfloat z)
1572 Q_D(QOpenGLShaderProgram);
1574 if (location != -1) {
1575 GLfloat values[3] = {x, y, z};
1576 d->glfuncs->glVertexAttrib3fv(location, values);
1581
1582
1583
1584
1585
1586
1587
1588void QOpenGLShaderProgram::setAttributeValue
1589 (
const char *name, GLfloat x, GLfloat y, GLfloat z)
1591 setAttributeValue(attributeLocation(name), x, y, z);
1595
1596
1597
1598
1599
1600void QOpenGLShaderProgram::setAttributeValue
1601 (
int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1603 Q_D(QOpenGLShaderProgram);
1604 if (location != -1) {
1605 GLfloat values[4] = {x, y, z, w};
1606 d->glfuncs->glVertexAttrib4fv(location, values);
1611
1612
1613
1614
1615
1616
1617
1618void QOpenGLShaderProgram::setAttributeValue
1619 (
const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1621 setAttributeValue(attributeLocation(name), x, y, z, w);
1625
1626
1627
1628
1629void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector2D& value)
1631 Q_D(QOpenGLShaderProgram);
1633 d->glfuncs->glVertexAttrib2fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1637
1638
1639
1640
1641
1642
1643void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector2D& value)
1645 setAttributeValue(attributeLocation(name), value);
1649
1650
1651
1652
1653void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector3D& value)
1655 Q_D(QOpenGLShaderProgram);
1658 d->glfuncs->glVertexAttrib3fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1662
1663
1664
1665
1666
1667
1668void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector3D& value)
1670 setAttributeValue(attributeLocation(name), value);
1674
1675
1676
1677
1678void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector4D& value)
1680 Q_D(QOpenGLShaderProgram);
1683 d->glfuncs->glVertexAttrib4fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1687
1688
1689
1690
1691
1692
1693void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector4D& value)
1695 setAttributeValue(attributeLocation(name), value);
1699
1700
1701
1702
1703void QOpenGLShaderProgram::setAttributeValue(
int location,
const QColor& value)
1705 Q_D(QOpenGLShaderProgram);
1707 if (location != -1) {
1708 GLfloat values[4] = {GLfloat(value.redF()), GLfloat(value.greenF()),
1709 GLfloat(value.blueF()), GLfloat(value.alphaF())};
1710 d->glfuncs->glVertexAttrib4fv(location, values);
1715
1716
1717
1718
1719
1720
1721void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QColor& value)
1723 setAttributeValue(attributeLocation(name), value);
1727
1728
1729
1730
1731
1732
1733
1734
1735void QOpenGLShaderProgram::setAttributeValue
1736 (
int location,
const GLfloat *values,
int columns,
int rows)
1738 Q_D(QOpenGLShaderProgram);
1740 if (rows < 1 || rows > 4) {
1741 qWarning(
"QOpenGLShaderProgram::setAttributeValue: rows %d not supported", rows);
1744 if (location != -1) {
1745 while (columns-- > 0) {
1747 d->glfuncs->glVertexAttrib1fv(location, values);
1749 d->glfuncs->glVertexAttrib2fv(location, values);
1751 d->glfuncs->glVertexAttrib3fv(location, values);
1753 d->glfuncs->glVertexAttrib4fv(location, values);
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771void QOpenGLShaderProgram::setAttributeValue
1772 (
const char *name,
const GLfloat *values,
int columns,
int rows)
1774 setAttributeValue(attributeLocation(name), values, columns, rows);
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791void QOpenGLShaderProgram::setAttributeArray
1792 (
int location,
const GLfloat *values,
int tupleSize,
int stride)
1794 Q_D(QOpenGLShaderProgram);
1796 if (location != -1) {
1797 d->glfuncs->glVertexAttribPointer(location, tupleSize, GL_FLOAT, GL_FALSE,
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815void QOpenGLShaderProgram::setAttributeArray
1816 (
int location,
const QVector2D *values,
int stride)
1818 Q_D(QOpenGLShaderProgram);
1820 if (location != -1) {
1821 d->glfuncs->glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE,
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839void QOpenGLShaderProgram::setAttributeArray
1840 (
int location,
const QVector3D *values,
int stride)
1842 Q_D(QOpenGLShaderProgram);
1844 if (location != -1) {
1845 d->glfuncs->glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863void QOpenGLShaderProgram::setAttributeArray
1864 (
int location,
const QVector4D *values,
int stride)
1866 Q_D(QOpenGLShaderProgram);
1868 if (location != -1) {
1869 d->glfuncs->glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE,
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897void QOpenGLShaderProgram::setAttributeArray
1898 (
int location, GLenum type,
const void *values,
int tupleSize,
int stride)
1900 Q_D(QOpenGLShaderProgram);
1902 if (location != -1) {
1903 d->glfuncs->glVertexAttribPointer(location, tupleSize, type, GL_TRUE,
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924void QOpenGLShaderProgram::setAttributeArray
1925 (
const char *name,
const GLfloat *values,
int tupleSize,
int stride)
1927 setAttributeArray(attributeLocation(name), values, tupleSize, stride);
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945void QOpenGLShaderProgram::setAttributeArray
1946 (
const char *name,
const QVector2D *values,
int stride)
1948 setAttributeArray(attributeLocation(name), values, stride);
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966void QOpenGLShaderProgram::setAttributeArray
1967 (
const char *name,
const QVector3D *values,
int stride)
1969 setAttributeArray(attributeLocation(name), values, stride);
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987void QOpenGLShaderProgram::setAttributeArray
1988 (
const char *name,
const QVector4D *values,
int stride)
1990 setAttributeArray(attributeLocation(name), values, stride);
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015void QOpenGLShaderProgram::setAttributeArray
2016 (
const char *name, GLenum type,
const void *values,
int tupleSize,
int stride)
2018 setAttributeArray(attributeLocation(name), type, values, tupleSize, stride);
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042void QOpenGLShaderProgram::setAttributeBuffer
2043 (
int location, GLenum type,
int offset,
int tupleSize,
int stride)
2045 Q_D(QOpenGLShaderProgram);
2047 if (location != -1) {
2048 d->glfuncs->glVertexAttribPointer(location, tupleSize, type, GL_TRUE, stride,
2049 reinterpret_cast<
const void *>(qintptr(offset)));
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073void QOpenGLShaderProgram::setAttributeBuffer
2074 (
const char *name, GLenum type,
int offset,
int tupleSize,
int stride)
2076 setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride);
2080
2081
2082
2083
2084
2085
2086
2087void QOpenGLShaderProgram::enableAttributeArray(
int location)
2089 Q_D(QOpenGLShaderProgram);
2092 d->glfuncs->glEnableVertexAttribArray(location);
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105void QOpenGLShaderProgram::enableAttributeArray(
const char *name)
2107 enableAttributeArray(attributeLocation(name));
2111
2112
2113
2114
2115
2116
2117void QOpenGLShaderProgram::disableAttributeArray(
int location)
2119 Q_D(QOpenGLShaderProgram);
2122 d->glfuncs->glDisableVertexAttribArray(location);
2126
2127
2128
2129
2130
2131
2132
2133
2134void QOpenGLShaderProgram::disableAttributeArray(
const char *name)
2136 disableAttributeArray(attributeLocation(name));
2140
2141
2142
2143
2144
2145
2146int QOpenGLShaderProgram::uniformLocation(
const char *name)
const
2148 Q_D(
const QOpenGLShaderProgram);
2150 if (d->linked && d->programGuard && d->programGuard->id()) {
2151 return d->glfuncs->glGetUniformLocation(d->programGuard->id(), name);
2153 qWarning(
"QOpenGLShaderProgram::uniformLocation(%s): shader program is not linked", name);
2159
2160
2161
2162
2163
2164
2165
2166
2167int QOpenGLShaderProgram::uniformLocation(
const QByteArray& name)
const
2169 return uniformLocation(name.constData());
2173
2174
2175
2176
2177
2178
2179
2180
2181int QOpenGLShaderProgram::uniformLocation(
const QString& name)
const
2183 return uniformLocation(name.toLatin1().constData());
2187
2188
2189
2190
2191void QOpenGLShaderProgram::setUniformValue(
int location, GLfloat value)
2193 Q_D(QOpenGLShaderProgram);
2196 d->glfuncs->glUniform1fv(location, 1, &value);
2200
2201
2202
2203
2204
2205
2206
2207void QOpenGLShaderProgram::setUniformValue(
const char *name, GLfloat value)
2209 setUniformValue(uniformLocation(name), value);
2213
2214
2215
2216
2217void QOpenGLShaderProgram::setUniformValue(
int location, GLint value)
2219 Q_D(QOpenGLShaderProgram);
2222 d->glfuncs->glUniform1i(location, value);
2226
2227
2228
2229
2230
2231
2232
2233void QOpenGLShaderProgram::setUniformValue(
const char *name, GLint value)
2235 setUniformValue(uniformLocation(name), value);
2239
2240
2241
2242
2243
2244
2245
2246
2247void QOpenGLShaderProgram::setUniformValue(
int location, GLuint value)
2249 Q_D(QOpenGLShaderProgram);
2252 d->glfuncs->glUniform1i(location, value);
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266void QOpenGLShaderProgram::setUniformValue(
const char *name, GLuint value)
2268 setUniformValue(uniformLocation(name), value);
2272
2273
2274
2275
2276
2277void QOpenGLShaderProgram::setUniformValue(
int location, GLfloat x, GLfloat y)
2279 Q_D(QOpenGLShaderProgram);
2281 if (location != -1) {
2282 GLfloat values[2] = {x, y};
2283 d->glfuncs->glUniform2fv(location, 1, values);
2288
2289
2290
2291
2292
2293
2294
2295void QOpenGLShaderProgram::setUniformValue(
const char *name, GLfloat x, GLfloat y)
2297 setUniformValue(uniformLocation(name), x, y);
2301
2302
2303
2304
2305
2306void QOpenGLShaderProgram::setUniformValue
2307 (
int location, GLfloat x, GLfloat y, GLfloat z)
2309 Q_D(QOpenGLShaderProgram);
2311 if (location != -1) {
2312 GLfloat values[3] = {x, y, z};
2313 d->glfuncs->glUniform3fv(location, 1, values);
2318
2319
2320
2321
2322
2323
2324
2325void QOpenGLShaderProgram::setUniformValue
2326 (
const char *name, GLfloat x, GLfloat y, GLfloat z)
2328 setUniformValue(uniformLocation(name), x, y, z);
2332
2333
2334
2335
2336
2337void QOpenGLShaderProgram::setUniformValue
2338 (
int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2340 Q_D(QOpenGLShaderProgram);
2342 if (location != -1) {
2343 GLfloat values[4] = {x, y, z, w};
2344 d->glfuncs->glUniform4fv(location, 1, values);
2349
2350
2351
2352
2353
2354
2355
2356void QOpenGLShaderProgram::setUniformValue
2357 (
const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2359 setUniformValue(uniformLocation(name), x, y, z, w);
2363
2364
2365
2366
2367void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector2D& value)
2369 Q_D(QOpenGLShaderProgram);
2372 d->glfuncs->glUniform2fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2376
2377
2378
2379
2380
2381
2382
2383void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector2D& value)
2385 setUniformValue(uniformLocation(name), value);
2389
2390
2391
2392
2393void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector3D& value)
2395 Q_D(QOpenGLShaderProgram);
2398 d->glfuncs->glUniform3fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2402
2403
2404
2405
2406
2407
2408
2409void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector3D& value)
2411 setUniformValue(uniformLocation(name), value);
2415
2416
2417
2418
2419void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector4D& value)
2421 Q_D(QOpenGLShaderProgram);
2424 d->glfuncs->glUniform4fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2428
2429
2430
2431
2432
2433
2434
2435void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector4D& value)
2437 setUniformValue(uniformLocation(name), value);
2441
2442
2443
2444
2445
2446void QOpenGLShaderProgram::setUniformValue(
int location,
const QColor& color)
2448 Q_D(QOpenGLShaderProgram);
2450 if (location != -1) {
2451 GLfloat values[4] = {GLfloat(color.redF()), GLfloat(color.greenF()),
2452 GLfloat(color.blueF()), GLfloat(color.alphaF())};
2453 d->glfuncs->glUniform4fv(location, 1, values);
2458
2459
2460
2461
2462
2463
2464
2465void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QColor& color)
2467 setUniformValue(uniformLocation(name), color);
2471
2472
2473
2474
2475
2476void QOpenGLShaderProgram::setUniformValue(
int location,
const QPoint& point)
2478 Q_D(QOpenGLShaderProgram);
2480 if (location != -1) {
2481 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2482 d->glfuncs->glUniform2fv(location, 1, values);
2487
2488
2489
2490
2491
2492
2493
2494void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QPoint& point)
2496 setUniformValue(uniformLocation(name), point);
2500
2501
2502
2503
2504
2505void QOpenGLShaderProgram::setUniformValue(
int location,
const QPointF& point)
2507 Q_D(QOpenGLShaderProgram);
2509 if (location != -1) {
2510 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2511 d->glfuncs->glUniform2fv(location, 1, values);
2516
2517
2518
2519
2520
2521
2522
2523void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QPointF& point)
2525 setUniformValue(uniformLocation(name), point);
2529
2530
2531
2532
2533
2534void QOpenGLShaderProgram::setUniformValue(
int location,
const QSize& size)
2536 Q_D(QOpenGLShaderProgram);
2538 if (location != -1) {
2539 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2540 d->glfuncs->glUniform2fv(location, 1, values);
2545
2546
2547
2548
2549
2550
2551
2552void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QSize& size)
2554 setUniformValue(uniformLocation(name), size);
2558
2559
2560
2561
2562
2563void QOpenGLShaderProgram::setUniformValue(
int location,
const QSizeF& size)
2565 Q_D(QOpenGLShaderProgram);
2567 if (location != -1) {
2568 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2569 d->glfuncs->glUniform2fv(location, 1, values);
2574
2575
2576
2577
2578
2579
2580
2581void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QSizeF& size)
2583 setUniformValue(uniformLocation(name), size);
2587
2588
2589
2590
2591
2592void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x2& value)
2594 Q_D(QOpenGLShaderProgram);
2596 d->glfuncs->glUniformMatrix2fv(location, 1, GL_FALSE, value.constData());
2600
2601
2602
2603
2604
2605
2606
2607void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x2& value)
2609 setUniformValue(uniformLocation(name), value);
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x3& value)
2624 Q_D(QOpenGLShaderProgram);
2626 d->glfuncs->glUniform3fv(location, 2, value.constData());
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x3& value)
2643 setUniformValue(uniformLocation(name), value);
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x4& value)
2658 Q_D(QOpenGLShaderProgram);
2660 d->glfuncs->glUniform4fv(location, 2, value.constData());
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x4& value)
2677 setUniformValue(uniformLocation(name), value);
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x2& value)
2692 Q_D(QOpenGLShaderProgram);
2694 d->glfuncs->glUniform2fv(location, 3, value.constData());
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x2& value)
2711 setUniformValue(uniformLocation(name), value);
2715
2716
2717
2718
2719
2720void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x3& value)
2722 Q_D(QOpenGLShaderProgram);
2724 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, value.constData());
2728
2729
2730
2731
2732
2733
2734
2735void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x3& value)
2737 setUniformValue(uniformLocation(name), value);
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x4& value)
2752 Q_D(QOpenGLShaderProgram);
2754 d->glfuncs->glUniform4fv(location, 3, value.constData());
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x4& value)
2771 setUniformValue(uniformLocation(name), value);
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x2& value)
2786 Q_D(QOpenGLShaderProgram);
2788 d->glfuncs->glUniform2fv(location, 4, value.constData());
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x2& value)
2805 setUniformValue(uniformLocation(name), value);
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x3& value)
2820 Q_D(QOpenGLShaderProgram);
2822 d->glfuncs->glUniform3fv(location, 4, value.constData());
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x3& value)
2839 setUniformValue(uniformLocation(name), value);
2843
2844
2845
2846
2847
2848void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x4& value)
2850 Q_D(QOpenGLShaderProgram);
2852 d->glfuncs->glUniformMatrix4fv(location, 1, GL_FALSE, value.constData());
2856
2857
2858
2859
2860
2861
2862
2863void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x4& value)
2865 setUniformValue(uniformLocation(name), value);
2869
2870
2871
2872
2873
2874
2875
2876
2877void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[2][2])
2879 Q_D(QOpenGLShaderProgram);
2882 d->glfuncs->glUniformMatrix2fv(location, 1, GL_FALSE, value[0]);
2886
2887
2888
2889
2890
2891
2892
2893
2894void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[3][3])
2896 Q_D(QOpenGLShaderProgram);
2899 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, value[0]);
2903
2904
2905
2906
2907
2908
2909
2910
2911void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[4][4])
2913 Q_D(QOpenGLShaderProgram);
2916 d->glfuncs->glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
2921
2922
2923
2924
2925
2926
2927
2928
2929void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[2][2])
2931 setUniformValue(uniformLocation(name), value);
2935
2936
2937
2938
2939
2940
2941
2942
2943void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[3][3])
2945 setUniformValue(uniformLocation(name), value);
2949
2950
2951
2952
2953
2954
2955
2956
2957void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[4][4])
2959 setUniformValue(uniformLocation(name), value);
2963
2964
2965
2966
2967
2968
2969void QOpenGLShaderProgram::setUniformValue(
int location,
const QTransform& value)
2971 Q_D(QOpenGLShaderProgram);
2973 if (location != -1) {
2974 GLfloat mat[3][3] = {
2975 {GLfloat(value.m11()), GLfloat(value.m12()), GLfloat(value.m13())},
2976 {GLfloat(value.m21()), GLfloat(value.m22()), GLfloat(value.m23())},
2977 {GLfloat(value.m31()), GLfloat(value.m32()), GLfloat(value.m33())}
2979 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
2984
2985
2986
2987
2988
2989
2990
2991
2992void QOpenGLShaderProgram::setUniformValue
2993 (
const char *name,
const QTransform& value)
2995 setUniformValue(uniformLocation(name), value);
2999
3000
3001
3002
3003
3004void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLint *values,
int count)
3006 Q_D(QOpenGLShaderProgram);
3009 d->glfuncs->glUniform1iv(location, count, values);
3013
3014
3015
3016
3017
3018
3019
3020void QOpenGLShaderProgram::setUniformValueArray
3021 (
const char *name,
const GLint *values,
int count)
3023 setUniformValueArray(uniformLocation(name), values, count);
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLuint *values,
int count)
3038 Q_D(QOpenGLShaderProgram);
3041 d->glfuncs->glUniform1iv(location, count,
reinterpret_cast<
const GLint *>(values));
3045
3046
3047
3048
3049
3050
3051
3052
3053void QOpenGLShaderProgram::setUniformValueArray
3054 (
const char *name,
const GLuint *values,
int count)
3056 setUniformValueArray(uniformLocation(name), values, count);
3060
3061
3062
3063
3064
3065
3066void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLfloat *values,
int count,
int tupleSize)
3068 Q_D(QOpenGLShaderProgram);
3070 if (location != -1) {
3072 d->glfuncs->glUniform1fv(location, count, values);
3073 else if (tupleSize == 2)
3074 d->glfuncs->glUniform2fv(location, count, values);
3075 else if (tupleSize == 3)
3076 d->glfuncs->glUniform3fv(location, count, values);
3077 else if (tupleSize == 4)
3078 d->glfuncs->glUniform4fv(location, count, values);
3080 qWarning(
"QOpenGLShaderProgram::setUniformValue: size %d not supported", tupleSize);
3085
3086
3087
3088
3089
3090
3091
3092
3093void QOpenGLShaderProgram::setUniformValueArray
3094 (
const char *name,
const GLfloat *values,
int count,
int tupleSize)
3096 setUniformValueArray(uniformLocation(name), values, count, tupleSize);
3100
3101
3102
3103
3104
3105void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector2D *values,
int count)
3107 Q_D(QOpenGLShaderProgram);
3110 d->glfuncs->glUniform2fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3114
3115
3116
3117
3118
3119
3120
3121void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector2D *values,
int count)
3123 setUniformValueArray(uniformLocation(name), values, count);
3127
3128
3129
3130
3131
3132void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector3D *values,
int count)
3134 Q_D(QOpenGLShaderProgram);
3137 d->glfuncs->glUniform3fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3141
3142
3143
3144
3145
3146
3147
3148void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector3D *values,
int count)
3150 setUniformValueArray(uniformLocation(name), values, count);
3154
3155
3156
3157
3158
3159void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector4D *values,
int count)
3161 Q_D(QOpenGLShaderProgram);
3164 d->glfuncs->glUniform4fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3168
3169
3170
3171
3172
3173
3174
3175void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector4D *values,
int count)
3177 setUniformValueArray(uniformLocation(name), values, count);
3181#define setUniformMatrixArray(func,location,values,count,type,cols,rows)
3182 if (location == -1
|| count <= 0
)
3184 if (sizeof(type) == sizeof(GLfloat) * cols * rows) {
3185 func(location, count, GL_FALSE,
3186 reinterpret_cast<const GLfloat *>(values[0
].constData()));
3188 QVarLengthArray<GLfloat> temp(cols * rows * count);
3189 for (int index = 0
; index < count; ++index) {
3190 for (int index2 = 0
; index2 < (cols * rows); ++index2) {
3191 temp.data()[cols * rows * index + index2] =
3192 values[index].constData()[index2];
3195 func(location, count, GL_FALSE, temp.constData());
3197#define setUniformGenericMatrixArray(colfunc,location,values,count,type,cols,rows)
3198 if (location == -1
|| count <= 0
)
3200 if (sizeof(type) == sizeof(GLfloat) * cols * rows) {
3201 const GLfloat *data = reinterpret_cast<const GLfloat *>
3202 (values[0
].constData());
3203 colfunc(location, count * cols, data);
3205 QVarLengthArray<GLfloat> temp(cols * rows * count);
3206 for (int index = 0
; index < count; ++index) {
3207 for (int index2 = 0
; index2 < (cols * rows); ++index2) {
3208 temp.data()[cols * rows * index + index2] =
3209 values[index].constData()[index2];
3212 colfunc(location, count * cols, temp.constData());
3216
3217
3218
3219
3220
3221void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x2 *values,
int count)
3223 Q_D(QOpenGLShaderProgram);
3226 (d->glfuncs->glUniformMatrix2fv, location, values, count, QMatrix2x2, 2, 2);
3230
3231
3232
3233
3234
3235
3236
3237void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x2 *values,
int count)
3239 setUniformValueArray(uniformLocation(name), values, count);
3243
3244
3245
3246
3247
3248void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x3 *values,
int count)
3250 Q_D(QOpenGLShaderProgram);
3253 (d->glfuncs->glUniform3fv, location, values, count,
3258
3259
3260
3261
3262
3263
3264
3265void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x3 *values,
int count)
3267 setUniformValueArray(uniformLocation(name), values, count);
3271
3272
3273
3274
3275
3276void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x4 *values,
int count)
3278 Q_D(QOpenGLShaderProgram);
3281 (d->glfuncs->glUniform4fv, location, values, count,
3286
3287
3288
3289
3290
3291
3292
3293void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x4 *values,
int count)
3295 setUniformValueArray(uniformLocation(name), values, count);
3299
3300
3301
3302
3303
3304void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x2 *values,
int count)
3306 Q_D(QOpenGLShaderProgram);
3309 (d->glfuncs->glUniform2fv, location, values, count,
3314
3315
3316
3317
3318
3319
3320
3321void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x2 *values,
int count)
3323 setUniformValueArray(uniformLocation(name), values, count);
3327
3328
3329
3330
3331
3332void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x3 *values,
int count)
3334 Q_D(QOpenGLShaderProgram);
3337 (d->glfuncs->glUniformMatrix3fv, location, values, count, QMatrix3x3, 3, 3);
3341
3342
3343
3344
3345
3346
3347
3348void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x3 *values,
int count)
3350 setUniformValueArray(uniformLocation(name), values, count);
3354
3355
3356
3357
3358
3359void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x4 *values,
int count)
3361 Q_D(QOpenGLShaderProgram);
3364 (d->glfuncs->glUniform4fv, location, values, count,
3369
3370
3371
3372
3373
3374
3375
3376void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x4 *values,
int count)
3378 setUniformValueArray(uniformLocation(name), values, count);
3382
3383
3384
3385
3386
3387void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x2 *values,
int count)
3389 Q_D(QOpenGLShaderProgram);
3392 (d->glfuncs->glUniform2fv, location, values, count,
3397
3398
3399
3400
3401
3402
3403
3404void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x2 *values,
int count)
3406 setUniformValueArray(uniformLocation(name), values, count);
3410
3411
3412
3413
3414
3415void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x3 *values,
int count)
3417 Q_D(QOpenGLShaderProgram);
3420 (d->glfuncs->glUniform3fv, location, values, count,
3425
3426
3427
3428
3429
3430
3431
3432void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x3 *values,
int count)
3434 setUniformValueArray(uniformLocation(name), values, count);
3438
3439
3440
3441
3442
3443void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x4 *values,
int count)
3445 Q_D(QOpenGLShaderProgram);
3448 (d->glfuncs->glUniformMatrix4fv, location, values, count, QMatrix4x4, 4, 4);
3452
3453
3454
3455
3456
3457
3458
3459void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x4 *values,
int count)
3461 setUniformValueArray(uniformLocation(name), values, count);
3465
3466
3467
3468int QOpenGLShaderProgram::maxGeometryOutputVertices()
const
3471 Q_D(
const QOpenGLShaderProgram);
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494void QOpenGLShaderProgram::setPatchVertexCount(
int count)
3496 Q_D(QOpenGLShaderProgram);
3501
3502
3503
3504
3505
3506
3507
3508int QOpenGLShaderProgram::patchVertexCount()
const
3510 int patchVertices = 0;
3511 Q_D(
const QOpenGLShaderProgram);
3513 return patchVertices;
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536void QOpenGLShaderProgram::setDefaultOuterTessellationLevels(
const QList<
float> &levels)
3538#if !QT_CONFIG(opengles2)
3539 Q_D(QOpenGLShaderProgram);
3540 if (d->tessellationFuncs) {
3541 QList<
float> tessLevels = levels;
3545 const int argCount = 4;
3546 if (tessLevels.size() < argCount) {
3547 tessLevels.reserve(argCount);
3548 for (
int i = tessLevels.size(); i < argCount; ++i)
3549 tessLevels.append(1.0f);
3551 d->tessellationFuncs->glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels.data());
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576QList<
float> QOpenGLShaderProgram::defaultOuterTessellationLevels()
const
3578#if !QT_CONFIG(opengles2)
3579 QList<
float> tessLevels(4, 1.0f);
3580 Q_D(
const QOpenGLShaderProgram);
3581 if (d->tessellationFuncs)
3582 d->tessellationFuncs->glGetFloatv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels.data());
3585 return QList<
float>();
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609void QOpenGLShaderProgram::setDefaultInnerTessellationLevels(
const QList<
float> &levels)
3611#if !QT_CONFIG(opengles2)
3612 Q_D(QOpenGLShaderProgram);
3613 if (d->tessellationFuncs) {
3614 QList<
float> tessLevels = levels;
3618 const int argCount = 2;
3619 if (tessLevels.size() < argCount) {
3620 tessLevels.reserve(argCount);
3621 for (
int i = tessLevels.size(); i < argCount; ++i)
3622 tessLevels.append(1.0f);
3624 d->tessellationFuncs->glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels.data());
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649QList<
float> QOpenGLShaderProgram::defaultInnerTessellationLevels()
const
3651#if !QT_CONFIG(opengles2)
3652 QList<
float> tessLevels(2, 1.0f);
3653 Q_D(
const QOpenGLShaderProgram);
3654 if (d->tessellationFuncs)
3655 d->tessellationFuncs->glGetFloatv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels.data());
3658 return QList<
float>();
3664
3665
3666
3667
3668
3669
3670
3671bool QOpenGLShaderProgram::hasOpenGLShaderPrograms(QOpenGLContext *context)
3674 context = QOpenGLContext::currentContext();
3677 return QOpenGLFunctions(context).hasOpenGLFeature(QOpenGLFunctions::Shaders);
3681
3682
3683void QOpenGLShaderProgram::shaderDestroyed()
3685 Q_D(QOpenGLShaderProgram);
3686 QOpenGLShader *shader = qobject_cast<QOpenGLShader *>(sender());
3687 if (shader && !d->removingShaders)
3688 removeShader(shader);
3692
3693
3694
3695
3696
3697
3698
3699bool QOpenGLShader::hasOpenGLShaders(ShaderType type, QOpenGLContext *context)
3702 context = QOpenGLContext::currentContext();
3706 if ((type & ~(Geometry | Vertex | Fragment | TessellationControl | TessellationEvaluation | Compute)) || type == 0)
3709 if (type & QOpenGLShader::Geometry)
3710 return supportsGeometry(context->format());
3711 else if (type & (QOpenGLShader::TessellationControl | QOpenGLShader::TessellationEvaluation))
3712 return supportsTessellation(context->format());
3713 else if (type & QOpenGLShader::Compute)
3714 return supportsCompute(context->format());
3723 static QOpenGLProgramBinarySupportCheckWrapper binSupportCheck;
3724 return !binSupportCheck.get(QOpenGLContext::currentContext())->isSupported();
3729 Q_Q(QOpenGLShaderProgram);
3730 for (
const QOpenGLProgramBinaryCache::ShaderDesc &shader : std::as_const(binaryProgram.shaders)) {
3731 auto s = std::make_unique<QOpenGLShader>(qt_shaderStageToType(shader.stage), q);
3732 if (!s->compileSourceCode(shader.source)) {
3736 anonShaders.append(s.release());
3737 if (!q->addShader(anonShaders.last()))
3745 static QOpenGLProgramBinaryCache binCache;
3747 Q_Q(QOpenGLShaderProgram);
3749 const QByteArray cacheKey = binaryProgram.cacheKey();
3750 if (lcOpenGLProgramDiskCache().isEnabled(QtDebugMsg))
3751 qCDebug(lcOpenGLProgramDiskCache,
"program with %d shaders, cache key %s",
3752 int(binaryProgram.shaders.size()), cacheKey.constData());
3754 bool needsCompile =
true;
3755 if (binCache.load(cacheKey, q->programId())) {
3756 qCDebug(lcOpenGLProgramDiskCache,
"Program binary received from cache");
3757 needsCompile =
false;
3760 bool needsSave =
false;
3762 qCDebug(lcOpenGLProgramDiskCache,
"Program binary not in cache, compiling");
3770 bool ok = q->link();
3772 if (ok && needsSave)
3773 binCache.save(cacheKey, q->programId());
3780#include "moc_qopenglshaderprogram.cpp"
QOpenGLSharedResourceGuard * shaderGuard
QOpenGLExtraFunctions * glfuncs
bool supportsComputeShaders
bool supportsGeometryShaders
bool supportsTessellationShaders
bool compile(QOpenGLShader *q)
QOpenGLExtraFunctions * glfuncs
QOpenGLSharedResourceGuard * programGuard
bool hasShader(QOpenGLShader::ShaderType type) const
bool isCacheDisabled() const
QList< QOpenGLShader * > shaders
QOpenGLProgramBinaryCache::ProgramDesc binaryProgram
QList< QOpenGLShader * > anonShaders
#define GL_GEOMETRY_SHADER
#define GL_TESS_CONTROL_SHADER
#define GL_COMPUTE_SHADER
#define GL_TESS_EVALUATION_SHADER
#define GL_MAX_GEOMETRY_OUTPUT_VERTICES
#define GL_PATCH_VERTICES
#define setUniformGenericMatrixArray(colfunc, location, values, count, type, cols, rows)
static const char qualifierDefines[]
static bool supportsTessellation(const QSurfaceFormat &f)
static QVersionDirectivePosition findVersionDirectivePosition(const char *source)
static QShader::Stage qt_shaderTypeToStage(QOpenGLShader::ShaderType type)
static bool supportsGeometry(const QSurfaceFormat &f)
#define setUniformMatrixArray(func, location, values, count, type, cols, rows)
static QOpenGLShader::ShaderType qt_shaderStageToType(QShader::Stage stage)
static bool supportsCompute(const QSurfaceFormat &f)
static const char blendEquationAdvancedHeader[]
constexpr QVersionDirectivePosition(int position=0, int line=-1)
constexpr bool hasPosition() const