Debian Codegraph @

tests/auto/render/qshaderprogram/tst_qshaderprogram.cpp

raw symbols SCIP
1 // Copyright (C) 2016 Paul Lemire <[email protected]>
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <Qt3DRender/qshaderprogram.h>
6 #include <Qt3DRender/private/qshaderprogram_p.h>
7 #include <QObject>
8 #include <QSignalSpy>
9 #include "testarbiter.h"
10
11 // We need to call QShaderProgram::sceneChangeEvent
12 class tst_QShaderProgram : public Qt3DRender::QShaderProgram
13 {
14 Q_OBJECT
15
16 public:
17 tst_QShaderProgram()
18 : Qt3DRender::QShaderProgram()
19 {
20 qRegisterMetaType<Qt3DRender::QShaderProgram::Format>("Format");
21 }
22
23 private Q_SLOTS:
24
25 void checkDefaultConstruction()
26 {
27 // GIVEN
28 Qt3DRender::QShaderProgram shaderProgram;
29
30 // THEN
31 QCOMPARE(shaderProgram.vertexShaderCode(), QByteArray());
32 QCOMPARE(shaderProgram.tessellationControlShaderCode(), QByteArray());
33 QCOMPARE(shaderProgram.tessellationEvaluationShaderCode(), QByteArray());
34 QCOMPARE(shaderProgram.geometryShaderCode(), QByteArray());
35 QCOMPARE(shaderProgram.fragmentShaderCode(), QByteArray());
36 QCOMPARE(shaderProgram.computeShaderCode(), QByteArray());
37 QCOMPARE(shaderProgram.log(), QString());
38 QCOMPARE(shaderProgram.status(), Qt3DRender::QShaderProgram::NotReady);
39 QCOMPARE(shaderProgram.format(), Qt3DRender::QShaderProgram::GLSL);
40 }
41
42 void checkPropertyChanges()
43 {
44 // GIVEN
45 Qt3DRender::QShaderProgram shaderProgram;
46
47 {
48 // WHEN
49 QSignalSpy spy(&shaderProgram, SIGNAL(vertexShaderCodeChanged(QByteArray)));
50 const QByteArray newValue = "VERTEX";
51 shaderProgram.setVertexShaderCode(newValue);
52
53 // THEN
54 QVERIFY(spy.isValid());
55 QCOMPARE(shaderProgram.vertexShaderCode(), newValue);
56 QCOMPARE(spy.size(), 1);
57
58 // WHEN
59 spy.clear();
60 shaderProgram.setVertexShaderCode(newValue);
61
62 // THEN
63 QCOMPARE(shaderProgram.vertexShaderCode(), newValue);
64 QCOMPARE(spy.size(), 0);
65 }
66 {
67 // WHEN
68 QSignalSpy spy(&shaderProgram, SIGNAL(tessellationControlShaderCodeChanged(QByteArray)));
69 const QByteArray newValue = "TESSELATION_CONTROL";
70 shaderProgram.setTessellationControlShaderCode(newValue);
71
72 // THEN
73 QVERIFY(spy.isValid());
74 QCOMPARE(shaderProgram.tessellationControlShaderCode(), newValue);
75 QCOMPARE(spy.size(), 1);
76
77 // WHEN
78 spy.clear();
79 shaderProgram.setTessellationControlShaderCode(newValue);
80
81 // THEN
82 QCOMPARE(shaderProgram.tessellationControlShaderCode(), newValue);
83 QCOMPARE(spy.size(), 0);
84 }
85 {
86 // WHEN
87 QSignalSpy spy(&shaderProgram, SIGNAL(tessellationEvaluationShaderCodeChanged(QByteArray)));
88 const QByteArray newValue = "TESSELATION_EVALUTATION";
89 shaderProgram.setTessellationEvaluationShaderCode(newValue);
90
91 // THEN
92 QVERIFY(spy.isValid());
93 QCOMPARE(shaderProgram.tessellationEvaluationShaderCode(), newValue);
94 QCOMPARE(spy.size(), 1);
95
96 // WHEN
97 spy.clear();
98 shaderProgram.setTessellationEvaluationShaderCode(newValue);
99
100 // THEN
101 QCOMPARE(shaderProgram.tessellationEvaluationShaderCode(), newValue);
102 QCOMPARE(spy.size(), 0);
103 }
104 {
105 // WHEN
106 QSignalSpy spy(&shaderProgram, SIGNAL(geometryShaderCodeChanged(QByteArray)));
107 const QByteArray newValue = "GEOMETRY";
108 shaderProgram.setGeometryShaderCode(newValue);
109
110 // THEN
111 QVERIFY(spy.isValid());
112 QCOMPARE(shaderProgram.geometryShaderCode(), newValue);
113 QCOMPARE(spy.size(), 1);
114
115 // WHEN
116 spy.clear();
117 shaderProgram.setGeometryShaderCode(newValue);
118
119 // THEN
120 QCOMPARE(shaderProgram.geometryShaderCode(), newValue);
121 QCOMPARE(spy.size(), 0);
122 }
123 {
124 // WHEN
125 QSignalSpy spy(&shaderProgram, SIGNAL(fragmentShaderCodeChanged(QByteArray)));
126 const QByteArray newValue = "FRAGMENT";
127 shaderProgram.setFragmentShaderCode(newValue);
128
129 // THEN
130 QVERIFY(spy.isValid());
131 QCOMPARE(shaderProgram.fragmentShaderCode(), newValue);
132 QCOMPARE(spy.size(), 1);
133
134 // WHEN
135 spy.clear();
136 shaderProgram.setFragmentShaderCode(newValue);
137
138 // THEN
139 QCOMPARE(shaderProgram.fragmentShaderCode(), newValue);
140 QCOMPARE(spy.size(), 0);
141 }
142 {
143 // WHEN
144 QSignalSpy spy(&shaderProgram, SIGNAL(computeShaderCodeChanged(QByteArray)));
145 const QByteArray newValue = "COMPUTE";
146 shaderProgram.setComputeShaderCode(newValue);
147
148 // THEN
149 QVERIFY(spy.isValid());
150 QCOMPARE(shaderProgram.computeShaderCode(), newValue);
151 QCOMPARE(spy.size(), 1);
152
153 // WHEN
154 spy.clear();
155 shaderProgram.setComputeShaderCode(newValue);
156
157 // THEN
158 QCOMPARE(shaderProgram.computeShaderCode(), newValue);
159 QCOMPARE(spy.size(), 0);
160 }
161 {
162 // WHEN
163 QSignalSpy spy(&shaderProgram, SIGNAL(formatChanged(Format)));
164 const QShaderProgram::Format newValue = QShaderProgram::SPIRV;
165 shaderProgram.setFormat(newValue);
166
167 // THEN
168 QVERIFY(spy.isValid());
169 QCOMPARE(shaderProgram.format(), newValue);
170 QCOMPARE(spy.size(), 1);
171
172 // WHEN
173 spy.clear();
174 shaderProgram.setFormat(newValue);
175
176 // THEN
177 QCOMPARE(shaderProgram.format(), newValue);
178 QCOMPARE(spy.size(), 0);
179 }
180 }
181
182 void checkVertexShaderCodeUpdate()
183 {
184 // GIVEN
185 TestArbiter arbiter;
186 Qt3DRender::QShaderProgram shaderProgram;
187 arbiter.setArbiterOnNode(&shaderProgram);
188
189 {
190 // WHEN
191 shaderProgram.setVertexShaderCode(QByteArrayLiteral("in vec3 toto;"));
192 QCoreApplication::processEvents();
193
194 // THEN
195 QCOMPARE(arbiter.dirtyNodes().size(), 1);
196 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
197
198 arbiter.clear();
199 }
200
201 {
202 // WHEN
203 shaderProgram.setVertexShaderCode(QByteArrayLiteral("in vec3 toto;"));
204 QCoreApplication::processEvents();
205
206 // THEN
207 QCOMPARE(arbiter.dirtyNodes().size(), 0);
208 }
209
210 }
211
212 void checkTessellationControlShaderCodeUpdate()
213 {
214 // GIVEN
215 TestArbiter arbiter;
216 Qt3DRender::QShaderProgram shaderProgram;
217 arbiter.setArbiterOnNode(&shaderProgram);
218
219 {
220 // WHEN
221 shaderProgram.setTessellationControlShaderCode(QByteArrayLiteral("in vec3 toto2;"));
222 QCoreApplication::processEvents();
223
224 // THEN
225 QCOMPARE(arbiter.dirtyNodes().size(), 1);
226 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
227
228 arbiter.clear();
229 }
230
231 {
232 // WHEN
233 shaderProgram.setTessellationControlShaderCode(QByteArrayLiteral("in vec3 toto2;"));
234 QCoreApplication::processEvents();
235
236 // THEN
237 QCOMPARE(arbiter.dirtyNodes().size(), 0);
238 }
239
240 }
241
242 void checkTessellationEvaluationShaderCodeUpdate()
243 {
244 // GIVEN
245 TestArbiter arbiter;
246 Qt3DRender::QShaderProgram shaderProgram;
247 arbiter.setArbiterOnNode(&shaderProgram);
248
249 {
250 // WHEN
251 shaderProgram.setTessellationEvaluationShaderCode(QByteArrayLiteral("in vec3 toto3;"));
252 QCoreApplication::processEvents();
253
254 // THEN
255 QCOMPARE(arbiter.dirtyNodes().size(), 1);
256 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
257
258 arbiter.clear();
259 }
260
261 {
262 // WHEN
263 shaderProgram.setTessellationEvaluationShaderCode(QByteArrayLiteral("in vec3 toto3;"));
264 QCoreApplication::processEvents();
265
266 // THEN
267 QCOMPARE(arbiter.dirtyNodes().size(), 0);
268 }
269
270 }
271
272 void checkGeometryShaderCodeUpdate()
273 {
274 // GIVEN
275 TestArbiter arbiter;
276 Qt3DRender::QShaderProgram shaderProgram;
277 arbiter.setArbiterOnNode(&shaderProgram);
278
279 {
280 // WHEN
281 shaderProgram.setGeometryShaderCode(QByteArrayLiteral("in vec3 toto4;"));
282 QCoreApplication::processEvents();
283
284 // THEN
285 QCOMPARE(arbiter.dirtyNodes().size(), 1);
286 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
287
288 arbiter.clear();
289 }
290
291 {
292 // WHEN
293 shaderProgram.setGeometryShaderCode(QByteArrayLiteral("in vec3 toto4;"));
294 QCoreApplication::processEvents();
295
296 // THEN
297 QCOMPARE(arbiter.dirtyNodes().size(), 0);
298 }
299
300 }
301
302 void checkFragmentShaderCodeUpdate()
303 {
304 // GIVEN
305 TestArbiter arbiter;
306 Qt3DRender::QShaderProgram shaderProgram;
307 arbiter.setArbiterOnNode(&shaderProgram);
308
309 {
310 // WHEN
311 shaderProgram.setFragmentShaderCode(QByteArrayLiteral("out vec4 fragColor;"));
312 QCoreApplication::processEvents();
313
314 // THEN
315 QCOMPARE(arbiter.dirtyNodes().size(), 1);
316 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
317
318 arbiter.clear();
319 }
320
321 {
322 // WHEN
323 shaderProgram.setFragmentShaderCode(QByteArrayLiteral("out vec4 fragColor;"));
324 QCoreApplication::processEvents();
325
326 // THEN
327 QCOMPARE(arbiter.dirtyNodes().size(), 0);
328 }
329
330 }
331
332 void checkComputeShaderCodeUpdate()
333 {
334 // GIVEN
335 TestArbiter arbiter;
336 Qt3DRender::QShaderProgram shaderProgram;
337 arbiter.setArbiterOnNode(&shaderProgram);
338
339 {
340 // WHEN
341 shaderProgram.setComputeShaderCode(QByteArrayLiteral("uniform vec3 temp;"));
342 QCoreApplication::processEvents();
343
344 // THEN
345 QCOMPARE(arbiter.dirtyNodes().size(), 1);
346 QCOMPARE(arbiter.dirtyNodes().front(), &shaderProgram);
347
348 arbiter.clear();
349 }
350
351 {
352 // WHEN
353 shaderProgram.setComputeShaderCode(QByteArrayLiteral("uniform vec3 temp;"));
354 QCoreApplication::processEvents();
355
356 // THEN
357 QCOMPARE(arbiter.dirtyNodes().size(), 0);
358 }
359
360 }
361
362 void checkLogPropertyUpdate()
363 {
364 // GIVEN
365 TestArbiter arbiter;
366 arbiter.setArbiterOnNode(this);
367 QSignalSpy spy(this, SIGNAL(logChanged(QString)));
368 const QString logValue = QStringLiteral("Some log...");
369
370 // THEN
371 QVERIFY(spy.isValid());
372
373 // WHEN
374 auto *dNode = static_cast<Qt3DRender::QShaderProgramPrivate *>(Qt3DCore::QNodePrivate::get(this));
375 dNode->setLog(logValue);
376
377 // THEN
378 QCOMPARE(spy.size(), 1);
379 QCOMPARE(arbiter.dirtyNodes().size(), 0);
380 QCOMPARE(log(), logValue);
381
382 // WHEN
383 spy.clear();
384 dNode->setLog(logValue);
385
386 // THEN
387 QCOMPARE(spy.size(), 0);
388 QCOMPARE(arbiter.dirtyNodes().size(), 0);
389 QCOMPARE(log(), logValue);
390
391 // Cleanup
392 Qt3DCore::QNodePrivate::get(this)->setArbiter(nullptr);
393 }
394
395 void checkStatusPropertyUpdate()
396 {
397 // GIVEN
398 qRegisterMetaType<Qt3DRender::QShaderProgram::Status>("Status");
399 TestArbiter arbiter;
400 arbiter.setArbiterOnNode(this);
401 QSignalSpy spy(this, SIGNAL(statusChanged(Status)));
402 const Qt3DRender::QShaderProgram::Status newStatus = Qt3DRender::QShaderProgram::Error;
403
404 // THEN
405 QVERIFY(spy.isValid());
406
407 // WHEN
408 auto *dNode = static_cast<Qt3DRender::QShaderProgramPrivate *>(Qt3DCore::QNodePrivate::get(this));
409 dNode->setStatus(newStatus);
410
411 // THEN
412 QCOMPARE(spy.size(), 1);
413 QCOMPARE(arbiter.dirtyNodes().size(), 0);
414 QCOMPARE(status(), newStatus);
415
416 // WHEN
417 spy.clear();
418 dNode->setStatus(newStatus);
419
420 // THEN
421 QCOMPARE(spy.size(), 0);
422 QCOMPARE(arbiter.dirtyNodes().size(), 0);
423 QCOMPARE(status(), newStatus);
424
425 // Cleanup
426 Qt3DCore::QNodePrivate::get(this)->setArbiter(nullptr);
427 }
428
429 void checkIncludes()
430 {
431 // GIVEN
432 Qt3DRender::QShaderProgram shaderProgram;
433 QByteArray includedContent = shaderProgram.loadSource(QUrl(QStringLiteral("qrc:/included.frag")));
434
435 // WHEN (test relative include)
436 QByteArray mainContent = shaderProgram.loadSource(QUrl(QStringLiteral("qrc:/main.frag")));
437
438 // THEN
439 QVERIFY(mainContent.indexOf(includedContent) == 0);
440
441 // WHEN (test absolute include)
442 mainContent = shaderProgram.loadSource(QUrl(QStringLiteral("qrc:/mainabsolute.frag")));
443
444 // THEN
445 QVERIFY(mainContent.indexOf(includedContent) == 0);
446 }
447
448 void checkFormatPropertyUpdate()
449 {
450 // GIVEN
451 TestArbiter arbiter;
452 Qt3DRender::QShaderProgram shaderProgram;
453 arbiter.setArbiterOnNode(&shaderProgram);
454
455 QSignalSpy spy(&shaderProgram, SIGNAL(formatChanged(Format)));
456
457 // THEN
458 QVERIFY(spy.isValid());
459
460 {
461 // WHEN
462 shaderProgram.setFormat(QShaderProgram::SPIRV);
463 QCoreApplication::processEvents();
464
465 // THEN
466 QCOMPARE(spy.size(), 1);
467 QCOMPARE(arbiter.dirtyNodes().size(), 1);
468
469 spy.clear();
470 }
471
472 {
473 // WHEN
474 shaderProgram.setFormat(QShaderProgram::SPIRV);
475 QCoreApplication::processEvents();
476
477 // THEN
478 QCOMPARE(spy.size(), 0);
479 QCOMPARE(arbiter.dirtyNodes().size(), 1);
480 }
481 }
482
483 };
484
485 QTEST_MAIN(tst_QShaderProgram)
486
487 #include "tst_qshaderprogram.moc"