1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <Qt3DRender/private/qshaderformat_p.h>
33#include <Qt3DRender/private/qshadernode_p.h>
34#include <Qt3DRender/private/qshadernodeport_p.h>
35
36using namespace Qt3DRender;
37namespace
38{
39 QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion,
40 const QStringList &extensions = QStringList(),
41 const QString &vendor = QString())
42 {
43 auto format = QShaderFormat();
44 format.setApi(api);
45 format.setVersion(QVersionNumber(majorVersion, minorVersion));
46 format.setExtensions(extensions);
47 format.setVendor(vendor);
48 return format;
49 }
50
51 QShaderNodePort createPort(QShaderNodePort::Direction direction, const QString &name)
52 {
53 auto port = QShaderNodePort();
54 port.direction = direction;
55 port.name = name;
56 return port;
57 }
58}
59
60class tst_QShaderNodes : public QObject
61{
62 Q_OBJECT
63private slots:
64 void shouldManipulateFormatMembers();
65 void shouldVerifyFormatsEquality_data();
66 void shouldVerifyFormatsEquality();
67 void shouldVerifyFormatsCompatibilities_data();
68 void shouldVerifyFormatsCompatibilities();
69
70 void shouldHaveDefaultPortState();
71 void shouldVerifyPortsEquality_data();
72 void shouldVerifyPortsEquality();
73
74 void shouldManipulateNodeMembers();
75 void shouldHandleNodeRulesSupportAndOrder();
76};
77
78void tst_QShaderNodes::shouldManipulateFormatMembers()
79{
80 // GIVEN
81 auto format = QShaderFormat();
82
83 // THEN (default state)
84 QCOMPARE(format.api(), QShaderFormat::NoApi);
85 QCOMPARE(format.version().majorVersion(), 0);
86 QCOMPARE(format.version().minorVersion(), 0);
87 QCOMPARE(format.extensions(), QStringList());
88 QCOMPARE(format.vendor(), QString());
89 QVERIFY(!format.isValid());
90
91 // WHEN
92 format.setApi(QShaderFormat::OpenGLES);
93
94 // THEN
95 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
96 QCOMPARE(format.version().majorVersion(), 0);
97 QCOMPARE(format.version().minorVersion(), 0);
98 QCOMPARE(format.extensions(), QStringList());
99 QCOMPARE(format.vendor(), QString());
100 QVERIFY(!format.isValid());
101
102 // WHEN
103 format.setVersion(QVersionNumber(3));
104
105 // THEN
106 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
107 QCOMPARE(format.version().majorVersion(), 3);
108 QCOMPARE(format.version().minorVersion(), 0);
109 QCOMPARE(format.extensions(), QStringList());
110 QCOMPARE(format.vendor(), QString());
111 QVERIFY(format.isValid());
112
113 // WHEN
114 format.setVersion(QVersionNumber(3, 2));
115
116 // THEN
117 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
118 QCOMPARE(format.version().majorVersion(), 3);
119 QCOMPARE(format.version().minorVersion(), 2);
120 QCOMPARE(format.extensions(), QStringList());
121 QCOMPARE(format.vendor(), QString());
122 QVERIFY(format.isValid());
123
124 // WHEN
125 format.setExtensions({"foo", "bar"});
126
127 // THEN
128 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
129 QCOMPARE(format.version().majorVersion(), 3);
130 QCOMPARE(format.version().minorVersion(), 2);
131 QCOMPARE(format.extensions(), QStringList({"bar", "foo"}));
132 QCOMPARE(format.vendor(), QString());
133 QVERIFY(format.isValid());
134
135 // WHEN
136 format.setVendor(QStringLiteral("KDAB"));
137
138 // THEN
139 QCOMPARE(format.api(), QShaderFormat::OpenGLES);
140 QCOMPARE(format.version().majorVersion(), 3);
141 QCOMPARE(format.version().minorVersion(), 2);
142 QCOMPARE(format.extensions(), QStringList({"bar", "foo"}));
143 QCOMPARE(format.vendor(), QStringLiteral("KDAB"));
144 QVERIFY(format.isValid());
145}
146
147void tst_QShaderNodes::shouldVerifyFormatsEquality_data()
148{
149 QTest::addColumn<QShaderFormat>(name: "left");
150 QTest::addColumn<QShaderFormat>(name: "right");
151 QTest::addColumn<bool>(name: "expected");
152
153 QTest::newRow(dataTag: "Equals") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
154 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
155 << true;
156 QTest::newRow(dataTag: "Apis") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
157 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
158 << false;
159 QTest::newRow(dataTag: "Major") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
160 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
161 << false;
162 QTest::newRow(dataTag: "Minor") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
163 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1, extensions: {"foo", "bar"}, vendor: "KDAB")
164 << false;
165 QTest::newRow(dataTag: "Extensions") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
166 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"}, vendor: "KDAB")
167 << false;
168 QTest::newRow(dataTag: "Vendor") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"}, vendor: "KDAB")
169 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
170 << false;
171}
172
173void tst_QShaderNodes::shouldVerifyFormatsEquality()
174{
175 // GIVEN
176 QFETCH(QShaderFormat, left);
177 QFETCH(QShaderFormat, right);
178
179 // WHEN
180 const auto equal = (left == right);
181 const auto notEqual = (left != right);
182
183 // THEN
184 QFETCH(bool, expected);
185 QCOMPARE(equal, expected);
186 QCOMPARE(notEqual, !expected);
187}
188
189void tst_QShaderNodes::shouldVerifyFormatsCompatibilities_data()
190{
191 QTest::addColumn<QShaderFormat>(name: "reference");
192 QTest::addColumn<QShaderFormat>(name: "tested");
193 QTest::addColumn<bool>(name: "expected");
194
195 QTest::newRow(dataTag: "NoProfileVsES") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
196 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
197 << true;
198 QTest::newRow(dataTag: "CoreProfileVsES") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
199 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
200 << false;
201 QTest::newRow(dataTag: "CompatProfileVsES") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
202 << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
203 << true;
204
205 QTest::newRow(dataTag: "ESVsNoProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
206 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
207 << false;
208 QTest::newRow(dataTag: "ESVsCoreProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
209 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
210 << false;
211 QTest::newRow(dataTag: "ESVsCompatProfile") << createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0)
212 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
213 << false;
214
215 QTest::newRow(dataTag: "CoreVsNoProfile") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
216 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
217 << false;
218 QTest::newRow(dataTag: "CoreVsCompat") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
219 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
220 << false;
221 QTest::newRow(dataTag: "CoreVsCore") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
222 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
223 << true;
224
225 QTest::newRow(dataTag: "NoProfileVsCore") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
226 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
227 << true;
228 QTest::newRow(dataTag: "NoProvileVsCompat") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
229 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
230 << true;
231 QTest::newRow(dataTag: "NoProfileVsNoProfile") << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
232 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
233 << true;
234
235 QTest::newRow(dataTag: "CompatVsCore") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
236 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
237 << true;
238 QTest::newRow(dataTag: "CompatVsCompat") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
239 << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
240 << true;
241 QTest::newRow(dataTag: "CompatVsNoProfile") << createFormat(api: QShaderFormat::OpenGLCompatibilityProfile, majorVersion: 2, minorVersion: 0)
242 << createFormat(api: QShaderFormat::OpenGLNoProfile, majorVersion: 2, minorVersion: 0)
243 << true;
244
245 QTest::newRow(dataTag: "MajorForwardCompat_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
246 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
247 << true;
248 QTest::newRow(dataTag: "MajorForwardCompat_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
249 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 4)
250 << true;
251 QTest::newRow(dataTag: "MajorForwardCompat_3") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 0)
252 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
253 << false;
254 QTest::newRow(dataTag: "MajorForwardCompat_4") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 2, minorVersion: 4)
255 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
256 << false;
257
258 QTest::newRow(dataTag: "MinorForwardCompat_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1)
259 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
260 << true;
261 QTest::newRow(dataTag: "MinorForwardCompat_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0)
262 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 1)
263 << false;
264
265 QTest::newRow(dataTag: "Extensions_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
266 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"})
267 << true;
268 QTest::newRow(dataTag: "Extensions_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo"})
269 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {"foo", "bar"})
270 << false;
271
272 QTest::newRow(dataTag: "Vendor_1") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
273 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {})
274 << true;
275 QTest::newRow(dataTag: "Vendor_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {})
276 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
277 << false;
278 QTest::newRow(dataTag: "Vendor_2") << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
279 << createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0, extensions: {}, vendor: "KDAB")
280 << true;
281}
282
283void tst_QShaderNodes::shouldVerifyFormatsCompatibilities()
284{
285 // GIVEN
286 QFETCH(QShaderFormat, reference);
287 QFETCH(QShaderFormat, tested);
288
289 // WHEN
290 const auto supported = reference.supports(other: tested);
291
292 // THEN
293 QFETCH(bool, expected);
294 QCOMPARE(supported, expected);
295}
296
297void tst_QShaderNodes::shouldHaveDefaultPortState()
298{
299 // GIVEN
300 auto port = QShaderNodePort();
301
302 // THEN
303 QCOMPARE(port.direction, QShaderNodePort::Output);
304 QVERIFY(port.name.isEmpty());
305}
306
307void tst_QShaderNodes::shouldVerifyPortsEquality_data()
308{
309 QTest::addColumn<QShaderNodePort>(name: "left");
310 QTest::addColumn<QShaderNodePort>(name: "right");
311 QTest::addColumn<bool>(name: "expected");
312
313 QTest::newRow(dataTag: "Equals") << createPort(direction: QShaderNodePort::Input, name: "foo")
314 << createPort(direction: QShaderNodePort::Input, name: "foo")
315 << true;
316 QTest::newRow(dataTag: "Direction") << createPort(direction: QShaderNodePort::Input, name: "foo")
317 << createPort(direction: QShaderNodePort::Output, name: "foo")
318 << false;
319 QTest::newRow(dataTag: "Name") << createPort(direction: QShaderNodePort::Input, name: "foo")
320 << createPort(direction: QShaderNodePort::Input, name: "bar")
321 << false;
322}
323
324void tst_QShaderNodes::shouldVerifyPortsEquality()
325{
326 // GIVEN
327 QFETCH(QShaderNodePort, left);
328 QFETCH(QShaderNodePort, right);
329
330 // WHEN
331 const auto equal = (left == right);
332 const auto notEqual = (left != right);
333
334 // THEN
335 QFETCH(bool, expected);
336 QCOMPARE(equal, expected);
337 QCOMPARE(notEqual, !expected);
338}
339
340void tst_QShaderNodes::shouldManipulateNodeMembers()
341{
342 // GIVEN
343 const auto openGLES2 = createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0);
344 const auto openGL3 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0);
345
346 const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"});
347 const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
348 const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
349
350 auto node = QShaderNode();
351
352 // THEN (default state)
353 QCOMPARE(node.type(), QShaderNode::Invalid);
354 QVERIFY(node.uuid().isNull());
355 QVERIFY(node.layers().isEmpty());
356 QVERIFY(node.ports().isEmpty());
357 QVERIFY(node.parameterNames().isEmpty());
358 QVERIFY(node.availableFormats().isEmpty());
359
360 // WHEN
361 const auto uuid = QUuid::createUuid();
362 node.setUuid(uuid);
363
364 // THEN
365 QCOMPARE(node.uuid(), uuid);
366
367 // WHEN
368 node.setLayers({"foo", "bar"});
369
370 // THEN
371 QCOMPARE(node.layers(), QStringList({"foo", "bar"}));
372
373 // WHEN
374 auto firstPort = QShaderNodePort();
375 firstPort.direction = QShaderNodePort::Input;
376 firstPort.name = QStringLiteral("foo");
377 node.addPort(port: firstPort);
378
379 // THEN
380 QCOMPARE(node.type(), QShaderNode::Output);
381 QCOMPARE(node.ports().size(), 1);
382 QCOMPARE(node.ports().at(0), firstPort);
383 QVERIFY(node.availableFormats().isEmpty());
384
385 // WHEN
386 auto secondPort = QShaderNodePort();
387 secondPort.direction = QShaderNodePort::Output;
388 secondPort.name = QStringLiteral("bar");
389 node.addPort(port: secondPort);
390
391 // THEN
392 QCOMPARE(node.type(), QShaderNode::Function);
393 QCOMPARE(node.ports().size(), 2);
394 QCOMPARE(node.ports().at(0), firstPort);
395 QCOMPARE(node.ports().at(1), secondPort);
396 QVERIFY(node.availableFormats().isEmpty());
397
398 // WHEN
399 node.removePort(port: firstPort);
400
401 // THEN
402 QCOMPARE(node.type(), QShaderNode::Input);
403 QCOMPARE(node.ports().size(), 1);
404 QCOMPARE(node.ports().at(0), secondPort);
405 QVERIFY(node.availableFormats().isEmpty());
406
407 // WHEN
408 node.setParameter(QStringLiteral("baz"), value: 42);
409
410 // THEN
411 QCOMPARE(node.type(), QShaderNode::Input);
412 QCOMPARE(node.ports().size(), 1);
413 QCOMPARE(node.ports().at(0), secondPort);
414 auto parameterNames = node.parameterNames();
415 parameterNames.sort();
416 QCOMPARE(parameterNames.size(), 1);
417 QCOMPARE(parameterNames.at(0), QStringLiteral("baz"));
418 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42));
419 QVERIFY(node.availableFormats().isEmpty());
420
421 // WHEN
422 node.setParameter(QStringLiteral("bleh"), QStringLiteral("value"));
423
424 // THEN
425 QCOMPARE(node.type(), QShaderNode::Input);
426 QCOMPARE(node.ports().size(), 1);
427 QCOMPARE(node.ports().at(0), secondPort);
428 parameterNames = node.parameterNames();
429 parameterNames.sort();
430 QCOMPARE(parameterNames.size(), 2);
431 QCOMPARE(parameterNames.at(0), QStringLiteral("baz"));
432 QCOMPARE(parameterNames.at(1), QStringLiteral("bleh"));
433 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42));
434 QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value")));
435 QVERIFY(node.availableFormats().isEmpty());
436
437 // WHEN
438 node.clearParameter(QStringLiteral("baz"));
439
440 // THEN
441 QCOMPARE(node.type(), QShaderNode::Input);
442 QCOMPARE(node.ports().size(), 1);
443 QCOMPARE(node.ports().at(0), secondPort);
444 parameterNames = node.parameterNames();
445 parameterNames.sort();
446 QCOMPARE(parameterNames.size(), 1);
447 QCOMPARE(parameterNames.at(0), QStringLiteral("bleh"));
448 QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant());
449 QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value")));
450 QVERIFY(node.availableFormats().isEmpty());
451
452 // WHEN
453 node.addRule(format: openGLES2, rule: es2Rule);
454 node.addRule(format: openGL3, rule: gl3Rule);
455
456 // THEN
457 QCOMPARE(node.availableFormats().size(), 2);
458 QCOMPARE(node.availableFormats().at(0), openGLES2);
459 QCOMPARE(node.availableFormats().at(1), openGL3);
460 QCOMPARE(node.rule(openGLES2), es2Rule);
461 QCOMPARE(node.rule(openGL3), gl3Rule);
462
463 // WHEN
464 node.removeRule(format: openGLES2);
465
466 // THEN
467 QCOMPARE(node.availableFormats().size(), 1);
468 QCOMPARE(node.availableFormats().at(0), openGL3);
469 QCOMPARE(node.rule(openGL3), gl3Rule);
470
471 // WHEN
472 node.addRule(format: openGLES2, rule: es2Rule);
473
474 // THEN
475 QCOMPARE(node.availableFormats().size(), 2);
476 QCOMPARE(node.availableFormats().at(0), openGL3);
477 QCOMPARE(node.availableFormats().at(1), openGLES2);
478 QCOMPARE(node.rule(openGLES2), es2Rule);
479 QCOMPARE(node.rule(openGL3), gl3Rule);
480
481 // WHEN
482 node.addRule(format: openGL3, rule: gl3bisRule);
483
484 // THEN
485 QCOMPARE(node.availableFormats().size(), 2);
486 QCOMPARE(node.availableFormats().at(0), openGLES2);
487 QCOMPARE(node.availableFormats().at(1), openGL3);
488 QCOMPARE(node.rule(openGLES2), es2Rule);
489 QCOMPARE(node.rule(openGL3), gl3bisRule);
490}
491
492void tst_QShaderNodes::shouldHandleNodeRulesSupportAndOrder()
493{
494 // GIVEN
495 const auto openGLES2 = createFormat(api: QShaderFormat::OpenGLES, majorVersion: 2, minorVersion: 0);
496 const auto openGL3 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 0);
497 const auto openGL32 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 3, minorVersion: 2);
498 const auto openGL4 = createFormat(api: QShaderFormat::OpenGLCoreProfile, majorVersion: 4, minorVersion: 0);
499
500 const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"});
501 const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
502 const auto gl32Rule = QShaderNode::Rule(QByteArrayLiteral("gl32"), {"#pragma include gl32/foo.inc", "#pragma include gl32/bar.inc"});
503 const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"});
504
505 auto node = QShaderNode();
506
507 // WHEN
508 node.addRule(format: openGLES2, rule: es2Rule);
509 node.addRule(format: openGL3, rule: gl3Rule);
510
511 // THEN
512 QCOMPARE(node.availableFormats().size(), 2);
513 QCOMPARE(node.availableFormats().at(0), openGLES2);
514 QCOMPARE(node.availableFormats().at(1), openGL3);
515 QCOMPARE(node.rule(openGLES2), es2Rule);
516 QCOMPARE(node.rule(openGL3), gl3Rule);
517 QCOMPARE(node.rule(openGL32), gl3Rule);
518 QCOMPARE(node.rule(openGL4), gl3Rule);
519
520 // WHEN
521 node.addRule(format: openGL32, rule: gl32Rule);
522
523 // THEN
524 QCOMPARE(node.availableFormats().size(), 3);
525 QCOMPARE(node.availableFormats().at(0), openGLES2);
526 QCOMPARE(node.availableFormats().at(1), openGL3);
527 QCOMPARE(node.availableFormats().at(2), openGL32);
528 QCOMPARE(node.rule(openGLES2), es2Rule);
529 QCOMPARE(node.rule(openGL3), gl3Rule);
530 QCOMPARE(node.rule(openGL32), gl32Rule);
531 QCOMPARE(node.rule(openGL4), gl32Rule);
532
533 // WHEN
534 node.addRule(format: openGL3, rule: gl3bisRule);
535
536 // THEN
537 QCOMPARE(node.availableFormats().size(), 3);
538 QCOMPARE(node.availableFormats().at(0), openGLES2);
539 QCOMPARE(node.availableFormats().at(1), openGL32);
540 QCOMPARE(node.availableFormats().at(2), openGL3);
541 QCOMPARE(node.rule(openGLES2), es2Rule);
542 QCOMPARE(node.rule(openGL3), gl3bisRule);
543 QCOMPARE(node.rule(openGL32), gl32Rule);
544 QCOMPARE(node.rule(openGL4), gl32Rule);
545}
546
547QTEST_MAIN(tst_QShaderNodes)
548
549#include "tst_qshadernodes.moc"
550

source code of qt3d/tests/auto/render/shadergraph/qshadernodes/tst_qshadernodes.cpp