7#include "private/qopenglcontext_p.h"
8#include <QtOpenGL/QOpenGLVersionFunctionsFactory>
9#include <QtCore/private/qobject_p.h>
10#include <QtCore/qdebug.h>
11#include <QtCore/qfile.h>
12#include <QtCore/qlist.h>
13#include <QtCore/qloggingcategory.h>
14#include <QtCore/qvarlengtharray.h>
15#include <QtGui/private/qopenglprogrambinarycache_p.h>
16#include <QtGui/qtransform.h>
17#include <QtGui/QColor>
18#include <QtGui/QSurfaceFormat>
20#if !QT_CONFIG(opengles2)
21#include <QtOpenGL/qopenglfunctions_4_0_core.h>
29using namespace Qt::StringLiterals;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
154
155
156
157
158
159
160
161
162
163
164
165
166
167
170#ifndef GL_GEOMETRY_SHADER
171#define GL_GEOMETRY_SHADER 0x8DD9
173#ifndef GL_TESS_CONTROL_SHADER
174#define GL_TESS_CONTROL_SHADER 0x8E88
176#ifndef GL_TESS_EVALUATION_SHADER
177#define GL_TESS_EVALUATION_SHADER 0x8E87
179#ifndef GL_COMPUTE_SHADER
180#define GL_COMPUTE_SHADER 0x91B9
182#ifndef GL_MAX_GEOMETRY_OUTPUT_VERTICES
183#define GL_MAX_GEOMETRY_OUTPUT_VERTICES 0x8DE0
185#ifndef GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS
186#define GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS 0x8DE1
188#ifndef GL_PATCH_VERTICES
189#define GL_PATCH_VERTICES 0x8E72
191#ifndef GL_PATCH_DEFAULT_OUTER_LEVEL
192#define GL_PATCH_DEFAULT_OUTER_LEVEL 0x8E74
194#ifndef GL_PATCH_DEFAULT_INNER_LEVEL
195#define GL_PATCH_DEFAULT_INNER_LEVEL 0x8E73
198#if !QT_CONFIG(opengles2)
199static inline bool isFormatGLES(
const QSurfaceFormat &f)
201 return (f.renderableType() == QSurfaceFormat::OpenGLES);
207 return f.version() >= std::pair(3, 2);
212#if !QT_CONFIG(opengles2)
213 if (!isFormatGLES(f))
214 return f.version() >= std::pair(4, 3);
216 return f.version() >= std::pair(3, 1);
218 return f.version() >= std::pair(3, 1);
224#if !QT_CONFIG(opengles2)
225 if (!isFormatGLES(f))
226 return f.version() >= std::pair(4, 0);
228 return f.version() >= std::pair(3, 2);
230 return f.version() >= std::pair(3, 2);
236 Q_DECLARE_PUBLIC(QOpenGLShader)
277 void freeShaderFunc(QOpenGLFunctions *funcs, GLuint id)
279 funcs->glDeleteShader(id);
292 QOpenGLContext *context =
const_cast<QOpenGLContext *>(QOpenGLContext::currentContext());
296 if (shaderType == QOpenGLShader::Vertex) {
297 shader = glfuncs->glCreateShader(GL_VERTEX_SHADER);
298 }
else if (shaderType == QOpenGLShader::Geometry && supportsGeometryShaders) {
300 }
else if (shaderType == QOpenGLShader::TessellationControl && supportsTessellationShaders) {
302 }
else if (shaderType == QOpenGLShader::TessellationEvaluation && supportsTessellationShaders) {
304 }
else if (shaderType == QOpenGLShader::Compute && supportsComputeShaders) {
306 }
else if (shaderType == QOpenGLShader::Fragment) {
307 shader = glfuncs->glCreateShader(GL_FRAGMENT_SHADER);
310 qWarning(
"QOpenGLShader: could not create shader");
313 shaderGuard =
new QOpenGLSharedResourceGuard(context, shader, freeShaderFunc);
319 GLuint shader = shaderGuard ? shaderGuard->id() : 0;
324 glfuncs->glCompileShader(shader);
328 glfuncs->glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
329 compiled = (value != 0);
333 QString name = q->objectName();
335 const char *types[] = {
339 "Tessellation Control",
340 "Tessellation Evaluation",
345 const char *type = types[6];
346 switch (shaderType) {
347 case QOpenGLShader::Fragment:
348 type = types[0];
break;
349 case QOpenGLShader::Vertex:
350 type = types[1];
break;
351 case QOpenGLShader::Geometry:
352 type = types[2];
break;
353 case QOpenGLShader::TessellationControl:
354 type = types[3];
break;
355 case QOpenGLShader::TessellationEvaluation:
356 type = types[4];
break;
357 case QOpenGLShader::Compute:
358 type = types[5];
break;
362 GLint infoLogLength = 0;
363 GLint sourceCodeLength = 0;
364 char *logBuffer =
nullptr;
365 char *sourceCodeBuffer =
nullptr;
368 glfuncs->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
370 if (infoLogLength > 1) {
372 logBuffer =
new char [infoLogLength];
373 glfuncs->glGetShaderInfoLog(shader, infoLogLength, &temp, logBuffer);
377 glfuncs->glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &sourceCodeLength);
379 if (sourceCodeLength > 1) {
381 sourceCodeBuffer =
new char [sourceCodeLength];
382 glfuncs->glGetShaderSource(shader, sourceCodeLength, &temp, sourceCodeBuffer);
386 log = QString::fromLatin1(logBuffer);
391 qWarning(
"QOpenGLShader::compile(%s): %s", type, qPrintable(log));
393 qWarning(
"QOpenGLShader::compile(%s)[%s]: %s", type, qPrintable(name), qPrintable(log));
396 if (sourceCodeBuffer) {
397 qWarning(
"*** Problematic %s shader source code ***\n"
400 type, qUtf16Printable(QString::fromLatin1(sourceCodeBuffer)));
405 delete [] sourceCodeBuffer;
415 shaderGuard =
nullptr;
420
421
422
423
424
425
426
427
428
429
430
431QOpenGLShader::QOpenGLShader(QOpenGLShader::ShaderType type, QObject *parent)
432 : QObject(*
new QOpenGLShaderPrivate(QOpenGLContext::currentContext(), type), parent)
439
440
441
442
443QOpenGLShader::~QOpenGLShader()
448
449
450QOpenGLShader::ShaderType QOpenGLShader::shaderType()
const
452 Q_D(
const QOpenGLShader);
453 return d->shaderType;
461#if QT_CONFIG(opengles2) && !defined(QT_OPENGL_FORCE_SHADER_DEFINES)
464#define QOpenGL_REDEFINE_HIGHP 1
465static const char redefineHighp[] =
466 "#ifndef GL_FRAGMENT_PRECISION_HIGH\n"
467 "#define highp mediump\n"
473 "#ifdef GL_KHR_blend_equation_advanced\n"
474 "#extension GL_ARB_fragment_coord_conventions : enable\n"
475 "#extension GL_KHR_blend_equation_advanced : enable\n"
507 PreprocessorDirective,
512 } state = StartOfLine;
514 const char *c = source;
517 case PreprocessorDirective:
518 if (*c ==
' ' || *c ==
'\t')
520 if (!strncmp(c,
"version", strlen(
"version"))) {
522 c += strlen(
"version");
523 while (*c && *c !=
'\n')
525 int splitPosition = c - source + 1;
526 int linePosition =
int(
std::count(source, c,
'\n')) + 1;
528 }
else if (*c ==
'/')
529 state = CommentStarting;
536 if (*c ==
' ' || *c ==
'\t')
538 else if (*c ==
'#') {
539 state = PreprocessorDirective;
546 state = CommentStarting;
550 case CommentStarting:
552 state = MultiLineComment;
554 state = SingleLineComment;
558 case MultiLineComment:
560 state = CommentEnding;
562 case SingleLineComment:
570 state = MultiLineComment;
580
581
582
583
584
585bool QOpenGLShader::compileSourceCode(
const char *source)
596 if (d->shaderGuard && d->shaderGuard->id() && source) {
597 const QVersionDirectivePosition versionDirectivePosition = findVersionDirectivePosition(source);
599 QVarLengthArray<
const char *, 5> sourceChunks;
600 QVarLengthArray<GLint, 5> sourceChunkLengths;
601 QOpenGLContext *ctx = QOpenGLContext::currentContext();
603 if (versionDirectivePosition.hasPosition()) {
605 sourceChunks.append(source);
606 sourceChunkLengths.append(GLint(versionDirectivePosition.position));
609 if (ctx->format().profile() == QSurfaceFormat::CompatibilityProfile) {
610 const char *vendor =
reinterpret_cast<
const char *>(ctx->functions()->glGetString(GL_VENDOR));
611 if (vendor && !strcmp(vendor,
"Intel")) {
612 static const char version110[] =
"#version 110\n";
613 sourceChunks.append(version110);
614 sourceChunkLengths.append(GLint(
sizeof(version110)) - 1);
618 if (d->shaderType == Fragment) {
619 sourceChunks.append(blendEquationAdvancedHeader);
620 sourceChunkLengths.append(GLint(
sizeof(blendEquationAdvancedHeader) - 1));
625 const QSurfaceFormat currentSurfaceFormat = ctx->format();
626 QOpenGLContextPrivate *ctx_d = QOpenGLContextPrivate::get(QOpenGLContext::currentContext());
627 if (currentSurfaceFormat.renderableType() == QSurfaceFormat::OpenGL
628 || ctx_d->workaround_missingPrecisionQualifiers
629#ifdef QT_OPENGL_FORCE_SHADER_DEFINES
633 sourceChunks.append(qualifierDefines);
634 sourceChunkLengths.append(GLint(
sizeof(qualifierDefines) - 1));
637#ifdef QOpenGL_REDEFINE_HIGHP
638 if (d->shaderType == Fragment && !ctx_d->workaround_missingPrecisionQualifiers
639 && QOpenGLContext::currentContext()->isOpenGLES()) {
640 sourceChunks.append(redefineHighp);
641 sourceChunkLengths.append(GLint(
sizeof(redefineHighp) - 1));
645 QByteArray lineDirective;
648 const char *version =
reinterpret_cast<
const char *>(ctx->functions()->glGetString(GL_VERSION));
649 if (!version || !strstr(version,
"2.1 Mesa 8")) {
651 lineDirective = QStringLiteral(
"#line %1\n").arg(versionDirectivePosition.line).toUtf8();
652 sourceChunks.append(lineDirective.constData());
653 sourceChunkLengths.append(GLint(lineDirective.size()));
657 sourceChunks.append(source + versionDirectivePosition.position);
658 sourceChunkLengths.append(GLint(qstrlen(source + versionDirectivePosition.position)));
660 d->glfuncs->glShaderSource(d->shaderGuard->id(), sourceChunks.size(), sourceChunks.data(), sourceChunkLengths.data());
661 return d->compile(
this);
668
669
670
671
672
673
674
675bool QOpenGLShader::compileSourceCode(
const QByteArray& source)
677 return compileSourceCode(source.constData());
681
682
683
684
685
686
687
688bool QOpenGLShader::compileSourceCode(
const QString& source)
690 return compileSourceCode(source.toLatin1().constData());
694
695
696
697
698
699
700bool QOpenGLShader::compileSourceFile(
const QString& fileName)
702 QFile file(fileName);
703 if (!file.open(QFile::ReadOnly)) {
704 qWarning() <<
"QOpenGLShader: Unable to open file" << fileName;
708 QByteArray contents = file.readAll();
709 return compileSourceCode(contents.constData());
713
714
715
716
717QByteArray QOpenGLShader::sourceCode()
const
719 Q_D(
const QOpenGLShader);
720 GLuint shader = d->shaderGuard ? d->shaderGuard->id() : 0;
724 d->glfuncs->glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &size);
728 char *source =
new char [size];
729 d->glfuncs->glGetShaderSource(shader, size, &len, source);
730 QByteArray src(source);
736
737
738
739
740bool QOpenGLShader::isCompiled()
const
742 Q_D(
const QOpenGLShader);
747
748
749
750
751QString QOpenGLShader::log()
const
753 Q_D(
const QOpenGLShader);
758
759
760
761
762GLuint QOpenGLShader::shaderId()
const
764 Q_D(
const QOpenGLShader);
765 return d->shaderGuard ? d->shaderGuard->id() : 0;
770 Q_DECLARE_PUBLIC(QOpenGLShaderProgram)
778#if !QT_CONFIG(opengles2)
796#if !QT_CONFIG(opengles2)
812 void freeProgramFunc(QOpenGLFunctions *funcs, GLuint id)
814 funcs->glDeleteProgram(id);
823 programGuard->free();
828 for (QOpenGLShader *shader : shaders) {
829 if (shader->shaderType() == type)
836
837
838
839
840
841
842
843QOpenGLShaderProgram::QOpenGLShaderProgram(QObject *parent)
844 : QObject(*
new QOpenGLShaderProgramPrivate, parent)
849
850
851QOpenGLShaderProgram::~QOpenGLShaderProgram()
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870bool QOpenGLShaderProgram::create()
875bool QOpenGLShaderProgram::init()
877 Q_D(QOpenGLShaderProgram);
878 if ((d->programGuard && d->programGuard->id()) || d->inited)
881 QOpenGLContext *context =
const_cast<QOpenGLContext *>(QOpenGLContext::currentContext());
884 d->glfuncs->initializeOpenGLFunctions();
886#if !QT_CONFIG(opengles2)
887 if (!context->isOpenGLES() && context->format().version() >= std::pair(4, 0)) {
888 d->tessellationFuncs = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_4_0_Core>(context);
889 d->tessellationFuncs->initializeOpenGLFunctions();
893 GLuint program = d->glfuncs->glCreateProgram();
895 qWarning(
"QOpenGLShaderProgram: could not create shader program");
899 delete d->programGuard;
900 d->programGuard =
new QOpenGLSharedResourceGuard(context, program, freeProgramFunc);
905
906
907
908
909
910
911
912
913
914
915
916bool QOpenGLShaderProgram::addShader(QOpenGLShader *shader)
918 Q_D(QOpenGLShaderProgram);
921 if (d->shaders.contains(shader))
923 if (d->programGuard && d->programGuard->id() && shader) {
924 if (!shader->d_func()->shaderGuard || !shader->d_func()->shaderGuard->id())
926 if (d->programGuard->group() != shader->d_func()->shaderGuard->group()) {
927 qWarning(
"QOpenGLShaderProgram::addShader: Program and shader are not associated with same context.");
930 d->glfuncs->glAttachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
932 d->shaders.append(shader);
933 connect(shader, SIGNAL(destroyed()),
this, SLOT(shaderDestroyed()));
941
942
943
944
945
946
947
948
949
950
951
952
953bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const char *source)
955 Q_D(QOpenGLShaderProgram);
958 QOpenGLShader *shader =
new QOpenGLShader(type,
this);
959 if (!shader->compileSourceCode(source)) {
960 d->log = shader->log();
964 d->anonShaders.append(shader);
965 return addShader(shader);
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QByteArray& source)
985 return addShaderFromSourceCode(type, source.constData());
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003bool QOpenGLShaderProgram::addShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QString& source)
1005 return addShaderFromSourceCode(type, source.toLatin1().constData());
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020bool QOpenGLShaderProgram::addShaderFromSourceFile
1021 (QOpenGLShader::ShaderType type,
const QString& fileName)
1023 Q_D(QOpenGLShaderProgram);
1026 QOpenGLShader *shader =
new QOpenGLShader(type,
this);
1027 if (!shader->compileSourceFile(fileName)) {
1028 d->log = shader->log();
1032 d->anonShaders.append(shader);
1033 return addShader(shader);
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const char *source)
1056 Q_D(QOpenGLShaderProgram);
1059 if (d->isCacheDisabled())
1060 return addShaderFromSourceCode(type, source);
1062 return addCacheableShaderFromSourceCode(type, QByteArray(source));
1068 case QOpenGLShader::Vertex:
1069 return QShader::VertexStage;
1070 case QOpenGLShader::Fragment:
1071 return QShader::FragmentStage;
1072 case QOpenGLShader::Geometry:
1073 return QShader::GeometryStage;
1074 case QOpenGLShader::TessellationControl:
1075 return QShader::TessellationControlStage;
1076 case QOpenGLShader::TessellationEvaluation:
1077 return QShader::TessellationEvaluationStage;
1078 case QOpenGLShader::Compute:
1079 return QShader::ComputeStage;
1081 return QShader::VertexStage;
1087 case QShader::VertexStage:
1088 return QOpenGLShader::Vertex;
1089 case QShader::TessellationControlStage:
1090 return QOpenGLShader::TessellationControl;
1091 case QShader::TessellationEvaluationStage:
1092 return QOpenGLShader::TessellationEvaluation;
1093 case QShader::GeometryStage:
1094 return QOpenGLShader::Geometry;
1095 case QShader::FragmentStage:
1096 return QOpenGLShader::Fragment;
1097 case QShader::ComputeStage:
1098 return QOpenGLShader::Compute;
1100 return QOpenGLShader::Vertex;
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QByteArray &source)
1125 Q_D(QOpenGLShaderProgram);
1128 if (d->isCacheDisabled())
1129 return addShaderFromSourceCode(type, source);
1131 d->binaryProgram.shaders.append(QOpenGLProgramBinaryCache::ShaderDesc(qt_shaderTypeToStage(type), source));
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151bool QOpenGLShaderProgram::addCacheableShaderFromSourceCode(QOpenGLShader::ShaderType type,
const QString &source)
1153 Q_D(QOpenGLShaderProgram);
1156 if (d->isCacheDisabled())
1157 return addShaderFromSourceCode(type, source);
1159 return addCacheableShaderFromSourceCode(type, source.toUtf8().constData());
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180bool QOpenGLShaderProgram::addCacheableShaderFromSourceFile(QOpenGLShader::ShaderType type,
const QString &fileName)
1182 Q_D(QOpenGLShaderProgram);
1185 if (d->isCacheDisabled())
1186 return addShaderFromSourceFile(type, fileName);
1188 QOpenGLProgramBinaryCache::ShaderDesc shader(qt_shaderTypeToStage(type));
1196 if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
1197 shader.source = f.readAll();
1200 qWarning(
"QOpenGLShaderProgram: Unable to open file %s", qPrintable(fileName));
1203 d->binaryProgram.shaders.append(shader);
1208
1209
1210
1211
1212
1213
1214void QOpenGLShaderProgram::removeShader(QOpenGLShader *shader)
1216 Q_D(QOpenGLShaderProgram);
1217 if (d->programGuard && d->programGuard->id()
1218 && shader && shader->d_func()->shaderGuard)
1220 d->glfuncs->glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
1224 d->shaders.removeAll(shader);
1225 d->anonShaders.removeAll(shader);
1226 disconnect(shader, SIGNAL(destroyed()),
this, SLOT(shaderDestroyed()));
1231
1232
1233
1234
1235
1236QList<QOpenGLShader *> QOpenGLShaderProgram::shaders()
const
1238 Q_D(
const QOpenGLShaderProgram);
1243
1244
1245
1246
1247
1248
1249
1250void QOpenGLShaderProgram::removeAllShaders()
1252 Q_D(QOpenGLShaderProgram);
1253 d->removingShaders =
true;
1254 for (QOpenGLShader *shader : std::as_const(d->shaders)) {
1255 if (d->programGuard && d->programGuard->id()
1256 && shader && shader->d_func()->shaderGuard)
1258 d->glfuncs->glDetachShader(d->programGuard->id(), shader->d_func()->shaderGuard->id());
1262 qDeleteAll(d->anonShaders);
1264 d->anonShaders.clear();
1265 d->binaryProgram = QOpenGLProgramBinaryCache::ProgramDesc();
1267 d->removingShaders =
false;
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294bool QOpenGLShaderProgram::link()
1296 Q_D(QOpenGLShaderProgram);
1297 GLuint program = d->programGuard ? d->programGuard->id() : 0;
1301 if (!d->linkBinaryRecursion && d->shaders.isEmpty() && !d->binaryProgram.shaders.isEmpty())
1302 return d->linkBinary();
1305 if (d->shaders.isEmpty()) {
1313 d->glfuncs->glGetProgramiv(program, GL_LINK_STATUS, &value);
1314 d->linked = (value != 0);
1319 d->glfuncs->glLinkProgram(program);
1321 d->glfuncs->glGetProgramiv(program, GL_LINK_STATUS, &value);
1322 d->linked = (value != 0);
1324 d->glfuncs->glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
1327 char *logbuf =
new char [value];
1329 d->glfuncs->glGetProgramInfoLog(program, value, &len, logbuf);
1330 d->log = QString::fromLatin1(logbuf);
1331 if (!d->linked && !d->linkBinaryRecursion) {
1332 QString name = objectName();
1334 qWarning(
"QOpenGLShader::link: %ls", qUtf16Printable(d->log));
1336 qWarning(
"QOpenGLShader::link[%ls]: %ls", qUtf16Printable(name), qUtf16Printable(d->log));
1344
1345
1346
1347
1348bool QOpenGLShaderProgram::isLinked()
const
1350 Q_D(
const QOpenGLShaderProgram);
1355
1356
1357
1358
1359
1360QString QOpenGLShaderProgram::log()
const
1362 Q_D(
const QOpenGLShaderProgram);
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376bool QOpenGLShaderProgram::bind()
1378 Q_D(QOpenGLShaderProgram);
1379 GLuint program = d->programGuard ? d->programGuard->id() : 0;
1382 if (!d->linked && !link())
1385 if (d->programGuard->group() != QOpenGLContextGroup::currentContextGroup()) {
1386 qWarning(
"QOpenGLShaderProgram::bind: program is not valid in the current context.");
1390 d->glfuncs->glUseProgram(program);
1395
1396
1397
1398
1399
1400void QOpenGLShaderProgram::release()
1402 Q_D(QOpenGLShaderProgram);
1404 if (d->programGuard && d->programGuard->group() != QOpenGLContextGroup::currentContextGroup())
1405 qWarning(
"QOpenGLShaderProgram::release: program is not valid in the current context.");
1407 d->glfuncs->glUseProgram(0);
1411
1412
1413
1414
1415GLuint QOpenGLShaderProgram::programId()
const
1417 Q_D(
const QOpenGLShaderProgram);
1418 GLuint id = d->programGuard ? d->programGuard->id() : 0;
1425 if (!
const_cast<QOpenGLShaderProgram *>(
this)->init())
1427 return d->programGuard ? d->programGuard->id() : 0;
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441void QOpenGLShaderProgram::bindAttributeLocation(
const char *name,
int location)
1443 Q_D(QOpenGLShaderProgram);
1444 if (!init() || !d->programGuard || !d->programGuard->id())
1446 d->glfuncs->glBindAttribLocation(d->programGuard->id(), location, name);
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463void QOpenGLShaderProgram::bindAttributeLocation(
const QByteArray& name,
int location)
1465 bindAttributeLocation(name.constData(), location);
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481void QOpenGLShaderProgram::bindAttributeLocation(
const QString& name,
int location)
1483 bindAttributeLocation(name.toLatin1().constData(), location);
1487
1488
1489
1490
1491
1492
1493int QOpenGLShaderProgram::attributeLocation(
const char *name)
const
1495 Q_D(
const QOpenGLShaderProgram);
1496 if (d->linked && d->programGuard && d->programGuard->id()) {
1497 return d->glfuncs->glGetAttribLocation(d->programGuard->id(), name);
1499 qWarning(
"QOpenGLShaderProgram::attributeLocation(%s): shader program is not linked", name);
1505
1506
1507
1508
1509
1510
1511
1512
1513int QOpenGLShaderProgram::attributeLocation(
const QByteArray& name)
const
1515 return attributeLocation(name.constData());
1519
1520
1521
1522
1523
1524
1525
1526
1527int QOpenGLShaderProgram::attributeLocation(
const QString& name)
const
1529 return attributeLocation(name.toLatin1().constData());
1533
1534
1535
1536
1537void QOpenGLShaderProgram::setAttributeValue(
int location, GLfloat value)
1539 Q_D(QOpenGLShaderProgram);
1541 d->glfuncs->glVertexAttrib1fv(location, &value);
1545
1546
1547
1548
1549
1550
1551void QOpenGLShaderProgram::setAttributeValue(
const char *name, GLfloat value)
1553 setAttributeValue(attributeLocation(name), value);
1557
1558
1559
1560
1561
1562void QOpenGLShaderProgram::setAttributeValue(
int location, GLfloat x, GLfloat y)
1564 Q_D(QOpenGLShaderProgram);
1565 if (location != -1) {
1566 GLfloat values[2] = {x, y};
1567 d->glfuncs->glVertexAttrib2fv(location, values);
1572
1573
1574
1575
1576
1577
1578
1579void QOpenGLShaderProgram::setAttributeValue(
const char *name, GLfloat x, GLfloat y)
1581 setAttributeValue(attributeLocation(name), x, y);
1585
1586
1587
1588
1589
1590void QOpenGLShaderProgram::setAttributeValue
1591 (
int location, GLfloat x, GLfloat y, GLfloat z)
1593 Q_D(QOpenGLShaderProgram);
1595 if (location != -1) {
1596 GLfloat values[3] = {x, y, z};
1597 d->glfuncs->glVertexAttrib3fv(location, values);
1602
1603
1604
1605
1606
1607
1608
1609void QOpenGLShaderProgram::setAttributeValue
1610 (
const char *name, GLfloat x, GLfloat y, GLfloat z)
1612 setAttributeValue(attributeLocation(name), x, y, z);
1616
1617
1618
1619
1620
1621void QOpenGLShaderProgram::setAttributeValue
1622 (
int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1624 Q_D(QOpenGLShaderProgram);
1625 if (location != -1) {
1626 GLfloat values[4] = {x, y, z, w};
1627 d->glfuncs->glVertexAttrib4fv(location, values);
1632
1633
1634
1635
1636
1637
1638
1639void QOpenGLShaderProgram::setAttributeValue
1640 (
const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
1642 setAttributeValue(attributeLocation(name), x, y, z, w);
1646
1647
1648
1649
1650void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector2D& value)
1652 Q_D(QOpenGLShaderProgram);
1654 d->glfuncs->glVertexAttrib2fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1658
1659
1660
1661
1662
1663
1664void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector2D& value)
1666 setAttributeValue(attributeLocation(name), value);
1670
1671
1672
1673
1674void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector3D& value)
1676 Q_D(QOpenGLShaderProgram);
1679 d->glfuncs->glVertexAttrib3fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1683
1684
1685
1686
1687
1688
1689void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector3D& value)
1691 setAttributeValue(attributeLocation(name), value);
1695
1696
1697
1698
1699void QOpenGLShaderProgram::setAttributeValue(
int location,
const QVector4D& value)
1701 Q_D(QOpenGLShaderProgram);
1704 d->glfuncs->glVertexAttrib4fv(location,
reinterpret_cast<
const GLfloat *>(&value));
1708
1709
1710
1711
1712
1713
1714void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QVector4D& value)
1716 setAttributeValue(attributeLocation(name), value);
1720
1721
1722
1723
1724void QOpenGLShaderProgram::setAttributeValue(
int location,
const QColor& value)
1726 Q_D(QOpenGLShaderProgram);
1728 if (location != -1) {
1729 GLfloat values[4] = {GLfloat(value.redF()), GLfloat(value.greenF()),
1730 GLfloat(value.blueF()), GLfloat(value.alphaF())};
1731 d->glfuncs->glVertexAttrib4fv(location, values);
1736
1737
1738
1739
1740
1741
1742void QOpenGLShaderProgram::setAttributeValue(
const char *name,
const QColor& value)
1744 setAttributeValue(attributeLocation(name), value);
1748
1749
1750
1751
1752
1753
1754
1755
1756void QOpenGLShaderProgram::setAttributeValue
1757 (
int location,
const GLfloat *values,
int columns,
int rows)
1759 Q_D(QOpenGLShaderProgram);
1761 if (rows < 1 || rows > 4) {
1762 qWarning(
"QOpenGLShaderProgram::setAttributeValue: rows %d not supported", rows);
1765 if (location != -1) {
1766 while (columns-- > 0) {
1768 d->glfuncs->glVertexAttrib1fv(location, values);
1770 d->glfuncs->glVertexAttrib2fv(location, values);
1772 d->glfuncs->glVertexAttrib3fv(location, values);
1774 d->glfuncs->glVertexAttrib4fv(location, values);
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792void QOpenGLShaderProgram::setAttributeValue
1793 (
const char *name,
const GLfloat *values,
int columns,
int rows)
1795 setAttributeValue(attributeLocation(name), values, columns, rows);
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812void QOpenGLShaderProgram::setAttributeArray
1813 (
int location,
const GLfloat *values,
int tupleSize,
int stride)
1815 Q_D(QOpenGLShaderProgram);
1817 if (location != -1) {
1818 d->glfuncs->glVertexAttribPointer(location, tupleSize, GL_FLOAT, GL_FALSE,
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836void QOpenGLShaderProgram::setAttributeArray
1837 (
int location,
const QVector2D *values,
int stride)
1839 Q_D(QOpenGLShaderProgram);
1841 if (location != -1) {
1842 d->glfuncs->glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE,
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860void QOpenGLShaderProgram::setAttributeArray
1861 (
int location,
const QVector3D *values,
int stride)
1863 Q_D(QOpenGLShaderProgram);
1865 if (location != -1) {
1866 d->glfuncs->glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884void QOpenGLShaderProgram::setAttributeArray
1885 (
int location,
const QVector4D *values,
int stride)
1887 Q_D(QOpenGLShaderProgram);
1889 if (location != -1) {
1890 d->glfuncs->glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE,
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918void QOpenGLShaderProgram::setAttributeArray
1919 (
int location, GLenum type,
const void *values,
int tupleSize,
int stride)
1921 Q_D(QOpenGLShaderProgram);
1923 if (location != -1) {
1924 d->glfuncs->glVertexAttribPointer(location, tupleSize, type, GL_TRUE,
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945void QOpenGLShaderProgram::setAttributeArray
1946 (
const char *name,
const GLfloat *values,
int tupleSize,
int stride)
1948 setAttributeArray(attributeLocation(name), values, tupleSize, stride);
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966void QOpenGLShaderProgram::setAttributeArray
1967 (
const char *name,
const QVector2D *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 QVector3D *values,
int stride)
1990 setAttributeArray(attributeLocation(name), values, stride);
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008void QOpenGLShaderProgram::setAttributeArray
2009 (
const char *name,
const QVector4D *values,
int stride)
2011 setAttributeArray(attributeLocation(name), values, stride);
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036void QOpenGLShaderProgram::setAttributeArray
2037 (
const char *name, GLenum type,
const void *values,
int tupleSize,
int stride)
2039 setAttributeArray(attributeLocation(name), type, values, tupleSize, stride);
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063void QOpenGLShaderProgram::setAttributeBuffer
2064 (
int location, GLenum type,
int offset,
int tupleSize,
int stride)
2066 Q_D(QOpenGLShaderProgram);
2068 if (location != -1) {
2069 d->glfuncs->glVertexAttribPointer(location, tupleSize, type, GL_TRUE, stride,
2070 reinterpret_cast<
const void *>(qintptr(offset)));
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094void QOpenGLShaderProgram::setAttributeBuffer
2095 (
const char *name, GLenum type,
int offset,
int tupleSize,
int stride)
2097 setAttributeBuffer(attributeLocation(name), type, offset, tupleSize, stride);
2101
2102
2103
2104
2105
2106
2107
2108void QOpenGLShaderProgram::enableAttributeArray(
int location)
2110 Q_D(QOpenGLShaderProgram);
2113 d->glfuncs->glEnableVertexAttribArray(location);
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126void QOpenGLShaderProgram::enableAttributeArray(
const char *name)
2128 enableAttributeArray(attributeLocation(name));
2132
2133
2134
2135
2136
2137
2138void QOpenGLShaderProgram::disableAttributeArray(
int location)
2140 Q_D(QOpenGLShaderProgram);
2143 d->glfuncs->glDisableVertexAttribArray(location);
2147
2148
2149
2150
2151
2152
2153
2154
2155void QOpenGLShaderProgram::disableAttributeArray(
const char *name)
2157 disableAttributeArray(attributeLocation(name));
2161
2162
2163
2164
2165
2166
2167int QOpenGLShaderProgram::uniformLocation(
const char *name)
const
2169 Q_D(
const QOpenGLShaderProgram);
2171 if (d->linked && d->programGuard && d->programGuard->id()) {
2172 return d->glfuncs->glGetUniformLocation(d->programGuard->id(), name);
2174 qWarning(
"QOpenGLShaderProgram::uniformLocation(%s): shader program is not linked", name);
2180
2181
2182
2183
2184
2185
2186
2187
2188int QOpenGLShaderProgram::uniformLocation(
const QByteArray& name)
const
2190 return uniformLocation(name.constData());
2194
2195
2196
2197
2198
2199
2200
2201
2202int QOpenGLShaderProgram::uniformLocation(
const QString& name)
const
2204 return uniformLocation(name.toLatin1().constData());
2208
2209
2210
2211
2212void QOpenGLShaderProgram::setUniformValue(
int location, GLfloat value)
2214 Q_D(QOpenGLShaderProgram);
2217 d->glfuncs->glUniform1fv(location, 1, &value);
2221
2222
2223
2224
2225
2226
2227
2228void QOpenGLShaderProgram::setUniformValue(
const char *name, GLfloat value)
2230 setUniformValue(uniformLocation(name), value);
2234
2235
2236
2237
2238void QOpenGLShaderProgram::setUniformValue(
int location, GLint value)
2240 Q_D(QOpenGLShaderProgram);
2243 d->glfuncs->glUniform1i(location, value);
2247
2248
2249
2250
2251
2252
2253
2254void QOpenGLShaderProgram::setUniformValue(
const char *name, GLint value)
2256 setUniformValue(uniformLocation(name), value);
2260
2261
2262
2263
2264
2265
2266
2267
2268void QOpenGLShaderProgram::setUniformValue(
int location, GLuint value)
2270 Q_D(QOpenGLShaderProgram);
2273 d->glfuncs->glUniform1i(location, value);
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287void QOpenGLShaderProgram::setUniformValue(
const char *name, GLuint value)
2289 setUniformValue(uniformLocation(name), value);
2293
2294
2295
2296
2297
2298void QOpenGLShaderProgram::setUniformValue(
int location, GLfloat x, GLfloat y)
2300 Q_D(QOpenGLShaderProgram);
2302 if (location != -1) {
2303 GLfloat values[2] = {x, y};
2304 d->glfuncs->glUniform2fv(location, 1, values);
2309
2310
2311
2312
2313
2314
2315
2316void QOpenGLShaderProgram::setUniformValue(
const char *name, GLfloat x, GLfloat y)
2318 setUniformValue(uniformLocation(name), x, y);
2322
2323
2324
2325
2326
2327void QOpenGLShaderProgram::setUniformValue
2328 (
int location, GLfloat x, GLfloat y, GLfloat z)
2330 Q_D(QOpenGLShaderProgram);
2332 if (location != -1) {
2333 GLfloat values[3] = {x, y, z};
2334 d->glfuncs->glUniform3fv(location, 1, values);
2339
2340
2341
2342
2343
2344
2345
2346void QOpenGLShaderProgram::setUniformValue
2347 (
const char *name, GLfloat x, GLfloat y, GLfloat z)
2349 setUniformValue(uniformLocation(name), x, y, z);
2353
2354
2355
2356
2357
2358void QOpenGLShaderProgram::setUniformValue
2359 (
int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2361 Q_D(QOpenGLShaderProgram);
2363 if (location != -1) {
2364 GLfloat values[4] = {x, y, z, w};
2365 d->glfuncs->glUniform4fv(location, 1, values);
2370
2371
2372
2373
2374
2375
2376
2377void QOpenGLShaderProgram::setUniformValue
2378 (
const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2380 setUniformValue(uniformLocation(name), x, y, z, w);
2384
2385
2386
2387
2388void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector2D& value)
2390 Q_D(QOpenGLShaderProgram);
2393 d->glfuncs->glUniform2fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2397
2398
2399
2400
2401
2402
2403
2404void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector2D& value)
2406 setUniformValue(uniformLocation(name), value);
2410
2411
2412
2413
2414void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector3D& value)
2416 Q_D(QOpenGLShaderProgram);
2419 d->glfuncs->glUniform3fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2423
2424
2425
2426
2427
2428
2429
2430void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector3D& value)
2432 setUniformValue(uniformLocation(name), value);
2436
2437
2438
2439
2440void QOpenGLShaderProgram::setUniformValue(
int location,
const QVector4D& value)
2442 Q_D(QOpenGLShaderProgram);
2445 d->glfuncs->glUniform4fv(location, 1,
reinterpret_cast<
const GLfloat *>(&value));
2449
2450
2451
2452
2453
2454
2455
2456void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QVector4D& value)
2458 setUniformValue(uniformLocation(name), value);
2462
2463
2464
2465
2466
2467void QOpenGLShaderProgram::setUniformValue(
int location,
const QColor& color)
2469 Q_D(QOpenGLShaderProgram);
2471 if (location != -1) {
2472 GLfloat values[4] = {GLfloat(color.redF()), GLfloat(color.greenF()),
2473 GLfloat(color.blueF()), GLfloat(color.alphaF())};
2474 d->glfuncs->glUniform4fv(location, 1, values);
2479
2480
2481
2482
2483
2484
2485
2486void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QColor& color)
2488 setUniformValue(uniformLocation(name), color);
2492
2493
2494
2495
2496
2497void QOpenGLShaderProgram::setUniformValue(
int location,
const QPoint& point)
2499 Q_D(QOpenGLShaderProgram);
2501 if (location != -1) {
2502 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2503 d->glfuncs->glUniform2fv(location, 1, values);
2508
2509
2510
2511
2512
2513
2514
2515void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QPoint& point)
2517 setUniformValue(uniformLocation(name), point);
2521
2522
2523
2524
2525
2526void QOpenGLShaderProgram::setUniformValue(
int location,
const QPointF& point)
2528 Q_D(QOpenGLShaderProgram);
2530 if (location != -1) {
2531 GLfloat values[4] = {GLfloat(point.x()), GLfloat(point.y())};
2532 d->glfuncs->glUniform2fv(location, 1, values);
2537
2538
2539
2540
2541
2542
2543
2544void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QPointF& point)
2546 setUniformValue(uniformLocation(name), point);
2550
2551
2552
2553
2554
2555void QOpenGLShaderProgram::setUniformValue(
int location,
const QSize& size)
2557 Q_D(QOpenGLShaderProgram);
2559 if (location != -1) {
2560 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2561 d->glfuncs->glUniform2fv(location, 1, values);
2566
2567
2568
2569
2570
2571
2572
2573void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QSize& size)
2575 setUniformValue(uniformLocation(name), size);
2579
2580
2581
2582
2583
2584void QOpenGLShaderProgram::setUniformValue(
int location,
const QSizeF& size)
2586 Q_D(QOpenGLShaderProgram);
2588 if (location != -1) {
2589 GLfloat values[4] = {GLfloat(size.width()), GLfloat(size.height())};
2590 d->glfuncs->glUniform2fv(location, 1, values);
2595
2596
2597
2598
2599
2600
2601
2602void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QSizeF& size)
2604 setUniformValue(uniformLocation(name), size);
2608
2609
2610
2611
2612
2613void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x2& value)
2615 Q_D(QOpenGLShaderProgram);
2617 d->glfuncs->glUniformMatrix2fv(location, 1, GL_FALSE, value.constData());
2621
2622
2623
2624
2625
2626
2627
2628void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x2& value)
2630 setUniformValue(uniformLocation(name), value);
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x3& value)
2645 Q_D(QOpenGLShaderProgram);
2647 d->glfuncs->glUniform3fv(location, 2, value.constData());
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x3& value)
2664 setUniformValue(uniformLocation(name), value);
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix2x4& value)
2679 Q_D(QOpenGLShaderProgram);
2681 d->glfuncs->glUniform4fv(location, 2, value.constData());
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix2x4& value)
2698 setUniformValue(uniformLocation(name), value);
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x2& value)
2713 Q_D(QOpenGLShaderProgram);
2715 d->glfuncs->glUniform2fv(location, 3, value.constData());
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x2& value)
2732 setUniformValue(uniformLocation(name), value);
2736
2737
2738
2739
2740
2741void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x3& value)
2743 Q_D(QOpenGLShaderProgram);
2745 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, value.constData());
2749
2750
2751
2752
2753
2754
2755
2756void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x3& value)
2758 setUniformValue(uniformLocation(name), value);
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix3x4& value)
2773 Q_D(QOpenGLShaderProgram);
2775 d->glfuncs->glUniform4fv(location, 3, value.constData());
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix3x4& value)
2792 setUniformValue(uniformLocation(name), value);
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x2& value)
2807 Q_D(QOpenGLShaderProgram);
2809 d->glfuncs->glUniform2fv(location, 4, value.constData());
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x2& value)
2826 setUniformValue(uniformLocation(name), value);
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x3& value)
2841 Q_D(QOpenGLShaderProgram);
2843 d->glfuncs->glUniform3fv(location, 4, value.constData());
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x3& value)
2860 setUniformValue(uniformLocation(name), value);
2864
2865
2866
2867
2868
2869void QOpenGLShaderProgram::setUniformValue(
int location,
const QMatrix4x4& value)
2871 Q_D(QOpenGLShaderProgram);
2873 d->glfuncs->glUniformMatrix4fv(location, 1, GL_FALSE, value.constData());
2877
2878
2879
2880
2881
2882
2883
2884void QOpenGLShaderProgram::setUniformValue(
const char *name,
const QMatrix4x4& value)
2886 setUniformValue(uniformLocation(name), value);
2890
2891
2892
2893
2894
2895
2896
2897
2898void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[2][2])
2900 Q_D(QOpenGLShaderProgram);
2903 d->glfuncs->glUniformMatrix2fv(location, 1, GL_FALSE, value[0]);
2907
2908
2909
2910
2911
2912
2913
2914
2915void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[3][3])
2917 Q_D(QOpenGLShaderProgram);
2920 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, value[0]);
2924
2925
2926
2927
2928
2929
2930
2931
2932void QOpenGLShaderProgram::setUniformValue(
int location,
const GLfloat value[4][4])
2934 Q_D(QOpenGLShaderProgram);
2937 d->glfuncs->glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
2942
2943
2944
2945
2946
2947
2948
2949
2950void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[2][2])
2952 setUniformValue(uniformLocation(name), value);
2956
2957
2958
2959
2960
2961
2962
2963
2964void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[3][3])
2966 setUniformValue(uniformLocation(name), value);
2970
2971
2972
2973
2974
2975
2976
2977
2978void QOpenGLShaderProgram::setUniformValue(
const char *name,
const GLfloat value[4][4])
2980 setUniformValue(uniformLocation(name), value);
2984
2985
2986
2987
2988
2989
2990void QOpenGLShaderProgram::setUniformValue(
int location,
const QTransform& value)
2992 Q_D(QOpenGLShaderProgram);
2994 if (location != -1) {
2995 GLfloat mat[3][3] = {
2996 {GLfloat(value.m11()), GLfloat(value.m12()), GLfloat(value.m13())},
2997 {GLfloat(value.m21()), GLfloat(value.m22()), GLfloat(value.m23())},
2998 {GLfloat(value.m31()), GLfloat(value.m32()), GLfloat(value.m33())}
3000 d->glfuncs->glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
3005
3006
3007
3008
3009
3010
3011
3012
3013void QOpenGLShaderProgram::setUniformValue
3014 (
const char *name,
const QTransform& value)
3016 setUniformValue(uniformLocation(name), value);
3020
3021
3022
3023
3024
3025void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLint *values,
int count)
3027 Q_D(QOpenGLShaderProgram);
3030 d->glfuncs->glUniform1iv(location, count, values);
3034
3035
3036
3037
3038
3039
3040
3041void QOpenGLShaderProgram::setUniformValueArray
3042 (
const char *name,
const GLint *values,
int count)
3044 setUniformValueArray(uniformLocation(name), values, count);
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLuint *values,
int count)
3059 Q_D(QOpenGLShaderProgram);
3062 d->glfuncs->glUniform1iv(location, count,
reinterpret_cast<
const GLint *>(values));
3066
3067
3068
3069
3070
3071
3072
3073
3074void QOpenGLShaderProgram::setUniformValueArray
3075 (
const char *name,
const GLuint *values,
int count)
3077 setUniformValueArray(uniformLocation(name), values, count);
3081
3082
3083
3084
3085
3086
3087void QOpenGLShaderProgram::setUniformValueArray(
int location,
const GLfloat *values,
int count,
int tupleSize)
3089 Q_D(QOpenGLShaderProgram);
3091 if (location != -1) {
3093 d->glfuncs->glUniform1fv(location, count, values);
3094 else if (tupleSize == 2)
3095 d->glfuncs->glUniform2fv(location, count, values);
3096 else if (tupleSize == 3)
3097 d->glfuncs->glUniform3fv(location, count, values);
3098 else if (tupleSize == 4)
3099 d->glfuncs->glUniform4fv(location, count, values);
3101 qWarning(
"QOpenGLShaderProgram::setUniformValue: size %d not supported", tupleSize);
3106
3107
3108
3109
3110
3111
3112
3113
3114void QOpenGLShaderProgram::setUniformValueArray
3115 (
const char *name,
const GLfloat *values,
int count,
int tupleSize)
3117 setUniformValueArray(uniformLocation(name), values, count, tupleSize);
3121
3122
3123
3124
3125
3126void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector2D *values,
int count)
3128 Q_D(QOpenGLShaderProgram);
3131 d->glfuncs->glUniform2fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3135
3136
3137
3138
3139
3140
3141
3142void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector2D *values,
int count)
3144 setUniformValueArray(uniformLocation(name), values, count);
3148
3149
3150
3151
3152
3153void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector3D *values,
int count)
3155 Q_D(QOpenGLShaderProgram);
3158 d->glfuncs->glUniform3fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3162
3163
3164
3165
3166
3167
3168
3169void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector3D *values,
int count)
3171 setUniformValueArray(uniformLocation(name), values, count);
3175
3176
3177
3178
3179
3180void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QVector4D *values,
int count)
3182 Q_D(QOpenGLShaderProgram);
3185 d->glfuncs->glUniform4fv(location, count,
reinterpret_cast<
const GLfloat *>(values));
3189
3190
3191
3192
3193
3194
3195
3196void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QVector4D *values,
int count)
3198 setUniformValueArray(uniformLocation(name), values, count);
3202#define setUniformMatrixArray(func,location,values,count,type,cols,rows)
3203 if (location == -1
|| count <= 0
)
3205 if (sizeof(type) == sizeof(GLfloat) * cols * rows) {
3206 func(location, count, GL_FALSE,
3207 reinterpret_cast<const GLfloat *>(values[0
].constData()));
3209 QVarLengthArray<GLfloat> temp(cols * rows * count);
3210 for (int index = 0
; index < count; ++index) {
3211 for (int index2 = 0
; index2 < (cols * rows); ++index2) {
3212 temp.data()[cols * rows * index + index2] =
3213 values[index].constData()[index2];
3216 func(location, count, GL_FALSE, temp.constData());
3218#define setUniformGenericMatrixArray(colfunc,location,values,count,type,cols,rows)
3219 if (location == -1
|| count <= 0
)
3221 if (sizeof(type) == sizeof(GLfloat) * cols * rows) {
3222 const GLfloat *data = reinterpret_cast<const GLfloat *>
3223 (values[0
].constData());
3224 colfunc(location, count * cols, data);
3226 QVarLengthArray<GLfloat> temp(cols * rows * count);
3227 for (int index = 0
; index < count; ++index) {
3228 for (int index2 = 0
; index2 < (cols * rows); ++index2) {
3229 temp.data()[cols * rows * index + index2] =
3230 values[index].constData()[index2];
3233 colfunc(location, count * cols, temp.constData());
3237
3238
3239
3240
3241
3242void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x2 *values,
int count)
3244 Q_D(QOpenGLShaderProgram);
3247 (d->glfuncs->glUniformMatrix2fv, location, values, count, QMatrix2x2, 2, 2);
3251
3252
3253
3254
3255
3256
3257
3258void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x2 *values,
int count)
3260 setUniformValueArray(uniformLocation(name), values, count);
3264
3265
3266
3267
3268
3269void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x3 *values,
int count)
3271 Q_D(QOpenGLShaderProgram);
3274 (d->glfuncs->glUniform3fv, location, values, count,
3279
3280
3281
3282
3283
3284
3285
3286void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x3 *values,
int count)
3288 setUniformValueArray(uniformLocation(name), values, count);
3292
3293
3294
3295
3296
3297void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix2x4 *values,
int count)
3299 Q_D(QOpenGLShaderProgram);
3302 (d->glfuncs->glUniform4fv, location, values, count,
3307
3308
3309
3310
3311
3312
3313
3314void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix2x4 *values,
int count)
3316 setUniformValueArray(uniformLocation(name), values, count);
3320
3321
3322
3323
3324
3325void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x2 *values,
int count)
3327 Q_D(QOpenGLShaderProgram);
3330 (d->glfuncs->glUniform2fv, location, values, count,
3335
3336
3337
3338
3339
3340
3341
3342void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x2 *values,
int count)
3344 setUniformValueArray(uniformLocation(name), values, count);
3348
3349
3350
3351
3352
3353void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x3 *values,
int count)
3355 Q_D(QOpenGLShaderProgram);
3358 (d->glfuncs->glUniformMatrix3fv, location, values, count, QMatrix3x3, 3, 3);
3362
3363
3364
3365
3366
3367
3368
3369void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x3 *values,
int count)
3371 setUniformValueArray(uniformLocation(name), values, count);
3375
3376
3377
3378
3379
3380void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix3x4 *values,
int count)
3382 Q_D(QOpenGLShaderProgram);
3385 (d->glfuncs->glUniform4fv, location, values, count,
3390
3391
3392
3393
3394
3395
3396
3397void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix3x4 *values,
int count)
3399 setUniformValueArray(uniformLocation(name), values, count);
3403
3404
3405
3406
3407
3408void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x2 *values,
int count)
3410 Q_D(QOpenGLShaderProgram);
3413 (d->glfuncs->glUniform2fv, location, values, count,
3418
3419
3420
3421
3422
3423
3424
3425void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x2 *values,
int count)
3427 setUniformValueArray(uniformLocation(name), values, count);
3431
3432
3433
3434
3435
3436void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x3 *values,
int count)
3438 Q_D(QOpenGLShaderProgram);
3441 (d->glfuncs->glUniform3fv, location, values, count,
3446
3447
3448
3449
3450
3451
3452
3453void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x3 *values,
int count)
3455 setUniformValueArray(uniformLocation(name), values, count);
3459
3460
3461
3462
3463
3464void QOpenGLShaderProgram::setUniformValueArray(
int location,
const QMatrix4x4 *values,
int count)
3466 Q_D(QOpenGLShaderProgram);
3469 (d->glfuncs->glUniformMatrix4fv, location, values, count, QMatrix4x4, 4, 4);
3473
3474
3475
3476
3477
3478
3479
3480void QOpenGLShaderProgram::setUniformValueArray(
const char *name,
const QMatrix4x4 *values,
int count)
3482 setUniformValueArray(uniformLocation(name), values, count);
3486
3487
3488
3489int QOpenGLShaderProgram::maxGeometryOutputVertices()
const
3492 Q_D(
const QOpenGLShaderProgram);
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515void QOpenGLShaderProgram::setPatchVertexCount(
int count)
3517 Q_D(QOpenGLShaderProgram);
3522
3523
3524
3525
3526
3527
3528
3529int QOpenGLShaderProgram::patchVertexCount()
const
3531 int patchVertices = 0;
3532 Q_D(
const QOpenGLShaderProgram);
3534 return patchVertices;
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557void QOpenGLShaderProgram::setDefaultOuterTessellationLevels(
const QList<
float> &levels)
3559#if !QT_CONFIG(opengles2)
3560 Q_D(QOpenGLShaderProgram);
3561 if (d->tessellationFuncs) {
3562 QList<
float> tessLevels = levels;
3566 const int argCount = 4;
3567 if (tessLevels.size() < argCount) {
3568 tessLevels.reserve(argCount);
3569 for (
int i = tessLevels.size(); i < argCount; ++i)
3570 tessLevels.append(1.0f);
3572 d->tessellationFuncs->glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels.data());
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597QList<
float> QOpenGLShaderProgram::defaultOuterTessellationLevels()
const
3599#if !QT_CONFIG(opengles2)
3600 QList<
float> tessLevels(4, 1.0f);
3601 Q_D(
const QOpenGLShaderProgram);
3602 if (d->tessellationFuncs)
3603 d->tessellationFuncs->glGetFloatv(GL_PATCH_DEFAULT_OUTER_LEVEL, tessLevels.data());
3606 return QList<
float>();
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630void QOpenGLShaderProgram::setDefaultInnerTessellationLevels(
const QList<
float> &levels)
3632#if !QT_CONFIG(opengles2)
3633 Q_D(QOpenGLShaderProgram);
3634 if (d->tessellationFuncs) {
3635 QList<
float> tessLevels = levels;
3639 const int argCount = 2;
3640 if (tessLevels.size() < argCount) {
3641 tessLevels.reserve(argCount);
3642 for (
int i = tessLevels.size(); i < argCount; ++i)
3643 tessLevels.append(1.0f);
3645 d->tessellationFuncs->glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels.data());
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670QList<
float> QOpenGLShaderProgram::defaultInnerTessellationLevels()
const
3672#if !QT_CONFIG(opengles2)
3673 QList<
float> tessLevels(2, 1.0f);
3674 Q_D(
const QOpenGLShaderProgram);
3675 if (d->tessellationFuncs)
3676 d->tessellationFuncs->glGetFloatv(GL_PATCH_DEFAULT_INNER_LEVEL, tessLevels.data());
3679 return QList<
float>();
3685
3686
3687
3688
3689
3690
3691
3692bool QOpenGLShaderProgram::hasOpenGLShaderPrograms(QOpenGLContext *context)
3695 context = QOpenGLContext::currentContext();
3698 return QOpenGLFunctions(context).hasOpenGLFeature(QOpenGLFunctions::Shaders);
3702
3703
3704void QOpenGLShaderProgram::shaderDestroyed()
3706 Q_D(QOpenGLShaderProgram);
3707 QOpenGLShader *shader = qobject_cast<QOpenGLShader *>(sender());
3708 if (shader && !d->removingShaders)
3709 removeShader(shader);
3713
3714
3715
3716
3717
3718
3719
3720bool QOpenGLShader::hasOpenGLShaders(ShaderType type, QOpenGLContext *context)
3723 context = QOpenGLContext::currentContext();
3727 if ((type & ~(Geometry | Vertex | Fragment | TessellationControl | TessellationEvaluation | Compute)) || type == 0)
3730 if (type & QOpenGLShader::Geometry)
3731 return supportsGeometry(context->format());
3732 else if (type & (QOpenGLShader::TessellationControl | QOpenGLShader::TessellationEvaluation))
3733 return supportsTessellation(context->format());
3734 else if (type & QOpenGLShader::Compute)
3735 return supportsCompute(context->format());
3744 static QOpenGLProgramBinarySupportCheckWrapper binSupportCheck;
3745 return !binSupportCheck.get(QOpenGLContext::currentContext())->isSupported();
3750 Q_Q(QOpenGLShaderProgram);
3751 for (
const QOpenGLProgramBinaryCache::ShaderDesc &shader : std::as_const(binaryProgram.shaders)) {
3752 auto s = std::make_unique<QOpenGLShader>(qt_shaderStageToType(shader.stage), q);
3753 if (!s->compileSourceCode(shader.source)) {
3757 anonShaders.append(s.release());
3758 if (!q->addShader(anonShaders.last()))
3766 static QOpenGLProgramBinaryCache binCache;
3768 Q_Q(QOpenGLShaderProgram);
3770 const QByteArray cacheKey = binaryProgram.cacheKey();
3771 if (lcOpenGLProgramDiskCache().isEnabled(QtDebugMsg))
3772 qCDebug(lcOpenGLProgramDiskCache,
"program with %d shaders, cache key %s",
3773 int(binaryProgram.shaders.size()), cacheKey.constData());
3775 bool needsCompile =
true;
3776 if (binCache.load(cacheKey, q->programId())) {
3777 qCDebug(lcOpenGLProgramDiskCache,
"Program binary received from cache");
3778 needsCompile =
false;
3781 bool needsSave =
false;
3783 qCDebug(lcOpenGLProgramDiskCache,
"Program binary not in cache, compiling");
3791 bool ok = q->link();
3793 if (ok && needsSave)
3794 binCache.save(cacheKey, q->programId());
3801#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