Debian Codegraph @

tests/auto/render/shadergraph/qshadergraph/tst_qshadergraph.cpp

raw symbols SCIP
1 // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4
5 #include <QtTest/QTest>
6
7 #include <Qt3DRender/private/qshadergraph_p.h>
8
9 using namespace Qt3DRender;
10 namespace
11 {
12 QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName)
13 {
14 auto port = QShaderNodePort();
15 port.direction = portDirection;
16 port.name = portName;
17 return port;
18 }
19
20 QShaderNode createNode(const QList<QShaderNodePort> &ports, const QStringList &layers = QStringList())
21 {
22 auto node = QShaderNode();
23 node.setUuid(QUuid::createUuid());
24 node.setLayers(layers);
25 for (const auto &port : ports)
26 node.addPort(port);
27 return node;
28 }
29
30 QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName,
31 const QUuid &targetUuid, const QString &targetName,
32 const QStringList &layers = QStringList())
33 {
34 auto edge = QShaderGraph::Edge();
35 edge.sourceNodeUuid = sourceUuid;
36 edge.sourcePortName = sourceName;
37 edge.targetNodeUuid = targetUuid;
38 edge.targetPortName = targetName;
39 edge.layers = layers;
40 return edge;
41 }
42
43 QShaderGraph::Statement createStatement(const QShaderNode &node,
44 const QList<int> &inputs = QList<int>(),
45 const QList<int> &outputs = QList<int>())
46 {
47 auto statement = QShaderGraph::Statement();
48 statement.node = node;
49 statement.inputs = inputs;
50 statement.outputs = outputs;
51 return statement;
52 }
53
54 void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement)
55 {
56 qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs;
57 }
58
59 void dumpStatementsIfNeeded(const QList<QShaderGraph::Statement> &statements, const QList<QShaderGraph::Statement> &expected)
60 {
61 if (statements != expected) {
62 for (int i = 0; i < qMax(statements.size(), expected.size()); i++) {
63 qDebug() << "----" << i << "----";
64 if (i < statements.size())
65 debugStatement("A:", statements.at(i));
66 if (i < expected.size())
67 debugStatement("E:", expected.at(i));
68 qDebug() << "-----------";
69 }
70 }
71 }
72 }
73
74 class tst_QShaderGraph : public QObject
75 {
76 Q_OBJECT
77 private slots:
78 void shouldHaveEdgeDefaultState();
79 void shouldTestEdgesEquality_data();
80 void shouldTestEdgesEquality();
81 void shouldManipulateStatementMembers();
82 void shouldTestStatementsEquality_data();
83 void shouldTestStatementsEquality();
84 void shouldFindIndexFromPortNameInStatements_data();
85 void shouldFindIndexFromPortNameInStatements();
86 void shouldManageNodeList();
87 void shouldManageEdgeList();
88 void shouldSerializeGraphForCodeGeneration();
89 void shouldHandleUnboundPortsDuringGraphSerialization();
90 void shouldSurviveCyclesDuringGraphSerialization();
91 void shouldDealWithEdgesJumpingOverLayers();
92 void shouldGenerateDifferentStatementsDependingOnActiveLayers();
93 void shouldDealWithBranchesWithoutOutput();
94 void shouldDiscardEdgesConnectedToDiscardedNodes();
95 };
96
97 void tst_QShaderGraph::shouldHaveEdgeDefaultState()
98 {
99 // GIVEN
100 auto edge = QShaderGraph::Edge();
101
102 // THEN
103 QVERIFY(edge.sourceNodeUuid.isNull());
104 QVERIFY(edge.sourcePortName.isEmpty());
105 QVERIFY(edge.targetNodeUuid.isNull());
106 QVERIFY(edge.targetPortName.isEmpty());
107 }
108
109 void tst_QShaderGraph::shouldTestEdgesEquality_data()
110 {
111 QTest::addColumn<QShaderGraph::Edge>("left");
112 QTest::addColumn<QShaderGraph::Edge>("right");
113 QTest::addColumn<bool>("expected");
114
115 const auto sourceUuid1 = QUuid::createUuid();
116 const auto sourceUuid2 = QUuid::createUuid();
117 const auto targetUuid1 = QUuid::createUuid();
118 const auto targetUuid2 = QUuid::createUuid();
119
120 QTest::newRow("Equals") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
121 << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
122 << true;
123 QTest::newRow("SourceUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
124 << createEdge(sourceUuid2, "foo", targetUuid1, "bar")
125 << false;
126 QTest::newRow("SourceName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
127 << createEdge(sourceUuid1, "bleh", targetUuid1, "bar")
128 << false;
129 QTest::newRow("TargetUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
130 << createEdge(sourceUuid1, "foo", targetUuid2, "bar")
131 << false;
132 QTest::newRow("TargetName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar")
133 << createEdge(sourceUuid1, "foo", targetUuid1, "bleh")
134 << false;
135 }
136
137 void tst_QShaderGraph::shouldTestEdgesEquality()
138 {
139 // GIVEN
140 QFETCH(QShaderGraph::Edge, left);
141 QFETCH(QShaderGraph::Edge, right);
142
143 // WHEN
144 const auto equal = (left == right);
145 const auto notEqual = (left != right);
146
147 // THEN
148 QFETCH(bool, expected);
149 QCOMPARE(equal, expected);
150 QCOMPARE(notEqual, !expected);
151 }
152
153 void tst_QShaderGraph::shouldManipulateStatementMembers()
154 {
155 // GIVEN
156 auto statement = QShaderGraph::Statement();
157
158 // THEN (default state)
159 QVERIFY(statement.inputs.isEmpty());
160 QVERIFY(statement.outputs.isEmpty());
161 QVERIFY(statement.node.uuid().isNull());
162 QVERIFY(statement.uuid().isNull());
163
164 // WHEN
165 const auto node = createNode({});
166 statement.node = node;
167
168 // THEN
169 QCOMPARE(statement.uuid(), node.uuid());
170
171 // WHEN
172 statement.node = QShaderNode();
173
174 // THEN
175 QVERIFY(statement.uuid().isNull());
176 }
177
178 void tst_QShaderGraph::shouldTestStatementsEquality_data()
179 {
180 QTest::addColumn<QShaderGraph::Statement>("left");
181 QTest::addColumn<QShaderGraph::Statement>("right");
182 QTest::addColumn<bool>("expected");
183
184 const auto node1 = createNode({});
185 const auto node2 = createNode({});
186
187 QTest::newRow("EqualNodes") << createStatement(node1, {1, 2}, {3, 4})
188 << createStatement(node1, {1, 2}, {3, 4})
189 << true;
190 QTest::newRow("EqualInvalids") << createStatement(QShaderNode(), {1, 2}, {3, 4})
191 << createStatement(QShaderNode(), {1, 2}, {3, 4})
192 << true;
193 QTest::newRow("Nodes") << createStatement(node1, {1, 2}, {3, 4})
194 << createStatement(node2, {1, 2}, {3, 4})
195 << false;
196 QTest::newRow("Inputs") << createStatement(node1, {1, 2}, {3, 4})
197 << createStatement(node1, {1, 2, 0}, {3, 4})
198 << false;
199 QTest::newRow("Outputs") << createStatement(node1, {1, 2}, {3, 4})
200 << createStatement(node1, {1, 2}, {3, 0, 4})
201 << false;
202 }
203
204 void tst_QShaderGraph::shouldTestStatementsEquality()
205 {
206 // GIVEN
207 QFETCH(QShaderGraph::Statement, left);
208 QFETCH(QShaderGraph::Statement, right);
209
210 // WHEN
211 const auto equal = (left == right);
212 const auto notEqual = (left != right);
213
214 // THEN
215 QFETCH(bool, expected);
216 QCOMPARE(equal, expected);
217 QCOMPARE(notEqual, !expected);
218 }
219
220 void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements_data()
221 {
222 QTest::addColumn<QShaderGraph::Statement>("statement");
223 QTest::addColumn<QString>("portName");
224 QTest::addColumn<int>("expectedInputIndex");
225 QTest::addColumn<int>("expectedOutputIndex");
226
227 const auto inputNodeStatement = createStatement(createNode({
228 createPort(QShaderNodePort::Output, "input")
229 }));
230 const auto outputNodeStatement = createStatement(createNode({
231 createPort(QShaderNodePort::Input, "output")
232 }));
233 const auto functionNodeStatement = createStatement(createNode({
234 createPort(QShaderNodePort::Input, "input1"),
235 createPort(QShaderNodePort::Output, "output1"),
236 createPort(QShaderNodePort::Input, "input2"),
237 createPort(QShaderNodePort::Output, "output2"),
238 createPort(QShaderNodePort::Output, "output3"),
239 createPort(QShaderNodePort::Input, "input3")
240 }));
241
242 QTest::newRow("Invalid") << QShaderGraph::Statement() << "foo" << -1 << -1;
243 QTest::newRow("InputNodeWrongName") << inputNodeStatement << "foo" << -1 << -1;
244 QTest::newRow("InputNodeExistingName") << inputNodeStatement << "input" << -1 << 0;
245 QTest::newRow("OutputNodeWrongName") << outputNodeStatement << "foo" << -1 << -1;
246 QTest::newRow("OutputNodeExistingName") << outputNodeStatement << "output" << 0 << -1;
247 QTest::newRow("FunctionNodeWrongName") << functionNodeStatement << "foo" << -1 << -1;
248 QTest::newRow("FunctionNodeInput1") << functionNodeStatement << "input1" << 0 << -1;
249 QTest::newRow("FunctionNodeOutput1") << functionNodeStatement << "output1" << -1 << 0;
250 QTest::newRow("FunctionNodeInput2") << functionNodeStatement << "input2" << 1 << -1;
251 QTest::newRow("FunctionNodeOutput2") << functionNodeStatement << "output2" << -1 << 1;
252 QTest::newRow("FunctionNodeInput3") << functionNodeStatement << "input3" << 2 << -1;
253 QTest::newRow("FunctionNodeOutput3") << functionNodeStatement << "output3" << -1 << 2;
254 }
255
256 void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements()
257 {
258 // GIVEN
259 QFETCH(QShaderGraph::Statement, statement);
260 QFETCH(QString, portName);
261 QFETCH(int, expectedInputIndex);
262 QFETCH(int, expectedOutputIndex);
263
264 // WHEN
265 const auto inputIndex = statement.portIndex(QShaderNodePort::Input, portName);
266 const auto outputIndex = statement.portIndex(QShaderNodePort::Output, portName);
267
268 // THEN
269 QCOMPARE(inputIndex, expectedInputIndex);
270 QCOMPARE(outputIndex, expectedOutputIndex);
271 }
272
273 void tst_QShaderGraph::shouldManageNodeList()
274 {
275 // GIVEN
276 const auto node1 = createNode({createPort(QShaderNodePort::Output, "node1")});
277 const auto node2 = createNode({createPort(QShaderNodePort::Output, "node2")});
278
279 auto graph = QShaderGraph();
280
281 // THEN (default state)
282 QVERIFY(graph.nodes().isEmpty());
283
284 // WHEN
285 graph.addNode(node1);
286
287 // THEN
288 QCOMPARE(graph.nodes().size(), 1);
289 QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
290 QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
291
292 // WHEN
293 graph.addNode(node2);
294
295 // THEN
296 QCOMPARE(graph.nodes().size(), 2);
297 QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
298 QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
299 QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid());
300 QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name);
301
302
303 // WHEN
304 graph.removeNode(node2);
305
306 // THEN
307 QCOMPARE(graph.nodes().size(), 1);
308 QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
309 QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
310
311 // WHEN
312 graph.addNode(node2);
313
314 // THEN
315 QCOMPARE(graph.nodes().size(), 2);
316 QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid());
317 QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name);
318 QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid());
319 QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name);
320
321 // WHEN
322 const auto node1bis = [node1] {
323 auto res = node1;
324 auto port = res.ports().at(0);
325 port.name = QStringLiteral("node1bis");
326 res.addPort(port);
327 return res;
328 }();
329 graph.addNode(node1bis);
330
331 // THEN
332 QCOMPARE(graph.nodes().size(), 2);
333 QCOMPARE(graph.nodes().at(0).uuid(), node2.uuid());
334 QCOMPARE(graph.nodes().at(0).ports().at(0).name, node2.ports().at(0).name);
335 QCOMPARE(graph.nodes().at(1).uuid(), node1bis.uuid());
336 QCOMPARE(graph.nodes().at(1).ports().at(0).name, node1bis.ports().at(0).name);
337 }
338
339 void tst_QShaderGraph::shouldManageEdgeList()
340 {
341 // GIVEN
342 const auto edge1 = createEdge(QUuid::createUuid(), "foo", QUuid::createUuid(), "bar");
343 const auto edge2 = createEdge(QUuid::createUuid(), "baz", QUuid::createUuid(), "boo");
344
345 auto graph = QShaderGraph();
346
347 // THEN (default state)
348 QVERIFY(graph.edges().isEmpty());
349
350 // WHEN
351 graph.addEdge(edge1);
352
353 // THEN
354 QCOMPARE(graph.edges().size(), 1);
355 QCOMPARE(graph.edges().at(0), edge1);
356
357 // WHEN
358 graph.addEdge(edge2);
359
360 // THEN
361 QCOMPARE(graph.edges().size(), 2);
362 QCOMPARE(graph.edges().at(0), edge1);
363 QCOMPARE(graph.edges().at(1), edge2);
364
365
366 // WHEN
367 graph.removeEdge(edge2);
368
369 // THEN
370 QCOMPARE(graph.edges().size(), 1);
371 QCOMPARE(graph.edges().at(0), edge1);
372
373 // WHEN
374 graph.addEdge(edge2);
375
376 // THEN
377 QCOMPARE(graph.edges().size(), 2);
378 QCOMPARE(graph.edges().at(0), edge1);
379 QCOMPARE(graph.edges().at(1), edge2);
380
381 // WHEN
382 graph.addEdge(edge1);
383
384 // THEN
385 QCOMPARE(graph.edges().size(), 2);
386 QCOMPARE(graph.edges().at(0), edge1);
387 QCOMPARE(graph.edges().at(1), edge2);
388 }
389
390 void tst_QShaderGraph::shouldSerializeGraphForCodeGeneration()
391 {
392 // GIVEN
393 const auto input1 = createNode({
394 createPort(QShaderNodePort::Output, "input1Value")
395 });
396 const auto input2 = createNode({
397 createPort(QShaderNodePort::Output, "input2Value")
398 });
399 const auto output1 = createNode({
400 createPort(QShaderNodePort::Input, "output1Value")
401 });
402 const auto output2 = createNode({
403 createPort(QShaderNodePort::Input, "output2Value")
404 });
405 const auto function1 = createNode({
406 createPort(QShaderNodePort::Input, "function1Input"),
407 createPort(QShaderNodePort::Output, "function1Output")
408 });
409 const auto function2 = createNode({
410 createPort(QShaderNodePort::Input, "function2Input1"),
411 createPort(QShaderNodePort::Input, "function2Input2"),
412 createPort(QShaderNodePort::Output, "function2Output")
413 });
414 const auto function3 = createNode({
415 createPort(QShaderNodePort::Input, "function3Input1"),
416 createPort(QShaderNodePort::Input, "function3Input2"),
417 createPort(QShaderNodePort::Output, "function3Output1"),
418 createPort(QShaderNodePort::Output, "function3Output2")
419 });
420
421 const auto graph = [=] {
422 auto res = QShaderGraph();
423 res.addNode(input1);
424 res.addNode(input2);
425 res.addNode(output1);
426 res.addNode(output2);
427 res.addNode(function1);
428 res.addNode(function2);
429 res.addNode(function3);
430 res.addEdge(createEdge(input1.uuid(), "input1Value", function1.uuid(), "function1Input"));
431 res.addEdge(createEdge(input1.uuid(), "input1Value", function2.uuid(), "function2Input1"));
432 res.addEdge(createEdge(input2.uuid(), "input2Value", function2.uuid(), "function2Input2"));
433 res.addEdge(createEdge(function1.uuid(), "function1Output", function3.uuid(), "function3Input1"));
434 res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input2"));
435 res.addEdge(createEdge(function3.uuid(), "function3Output1", output1.uuid(), "output1Value"));
436 res.addEdge(createEdge(function3.uuid(), "function3Output2", output2.uuid(), "output2Value"));
437 return res;
438 }();
439
440 // WHEN
441 const auto statements = graph.createStatements();
442
443 // THEN
444 const QList<QShaderGraph::Statement> expected
445 = { createStatement(input2, {}, {1}),
446 createStatement(input1, {}, {0}),
447 createStatement(function2, {0, 1}, {3}),
448 createStatement(function1, {0}, {2}),
449 createStatement(function3, {2, 3}, {4, 5}),
450 createStatement(output2, {5}, {}),
451 createStatement(output1, {4}, {}) };
452 dumpStatementsIfNeeded(statements, expected);
453 QCOMPARE(statements, expected);
454 }
455
456 void tst_QShaderGraph::shouldHandleUnboundPortsDuringGraphSerialization()
457 {
458 // GIVEN
459 const auto input = createNode({
460 createPort(QShaderNodePort::Output, "input")
461 });
462 const auto unboundInput = createNode({
463 createPort(QShaderNodePort::Output, "unbound")
464 });
465 const auto output = createNode({
466 createPort(QShaderNodePort::Input, "output")
467 });
468 const auto unboundOutput = createNode({
469 createPort(QShaderNodePort::Input, "unbound")
470 });
471 const auto function = createNode({
472 createPort(QShaderNodePort::Input, "functionInput1"),
473 createPort(QShaderNodePort::Input, "functionInput2"),
474 createPort(QShaderNodePort::Input, "functionInput3"),
475 createPort(QShaderNodePort::Output, "functionOutput1"),
476 createPort(QShaderNodePort::Output, "functionOutput2"),
477 createPort(QShaderNodePort::Output, "functionOutput3")
478 });
479
480 const auto graph = [=] {
481 auto res = QShaderGraph();
482 res.addNode(input);
483 res.addNode(unboundInput);
484 res.addNode(output);
485 res.addNode(unboundOutput);
486 res.addNode(function);
487 res.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput2"));
488 res.addEdge(createEdge(function.uuid(), "functionOutput2", output.uuid(), "output"));
489 return res;
490 }();
491
492 // WHEN
493 const auto statements = graph.createStatements();
494
495 // THEN
496 // Note that no statement has any unbound input
497 const QList<QShaderGraph::Statement> expected = { createStatement(input, {}, {0}) };
498 dumpStatementsIfNeeded(statements, expected);
499 QCOMPARE(statements, expected);
500 }
501
502 void tst_QShaderGraph::shouldSurviveCyclesDuringGraphSerialization()
503 {
504 // GIVEN
505 const auto input = createNode({
506 createPort(QShaderNodePort::Output, "input")
507 });
508 const auto output = createNode({
509 createPort(QShaderNodePort::Input, "output")
510 });
511 const auto function1 = createNode({
512 createPort(QShaderNodePort::Input, "function1Input1"),
513 createPort(QShaderNodePort::Input, "function1Input2"),
514 createPort(QShaderNodePort::Output, "function1Output")
515 });
516 const auto function2 = createNode({
517 createPort(QShaderNodePort::Input, "function2Input"),
518 createPort(QShaderNodePort::Output, "function2Output")
519 });
520 const auto function3 = createNode({
521 createPort(QShaderNodePort::Input, "function3Input"),
522 createPort(QShaderNodePort::Output, "function3Output")
523 });
524
525 const auto graph = [=] {
526 auto res = QShaderGraph();
527 res.addNode(input);
528 res.addNode(output);
529 res.addNode(function1);
530 res.addNode(function2);
531 res.addNode(function3);
532 res.addEdge(createEdge(input.uuid(), "input", function1.uuid(), "function1Input1"));
533 res.addEdge(createEdge(function1.uuid(), "function1Output", function2.uuid(), "function2Input"));
534 res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input"));
535 res.addEdge(createEdge(function3.uuid(), "function3Output", function1.uuid(), "function1Input2"));
536 res.addEdge(createEdge(function2.uuid(), "function2Output", output.uuid(), "output"));
537 return res;
538 }();
539
540 // WHEN
541 const auto statements = graph.createStatements();
542
543 // THEN
544 // The cycle is ignored
545 const QList<QShaderGraph::Statement> expected;
546 dumpStatementsIfNeeded(statements, expected);
547 QCOMPARE(statements, expected);
548 }
549
550 void tst_QShaderGraph::shouldDealWithEdgesJumpingOverLayers()
551 {
552 // GIVEN
553 const auto worldPosition = createNode({
554 createPort(QShaderNodePort::Output, "worldPosition")
555 });
556 const auto texture = createNode({
557 createPort(QShaderNodePort::Output, "texture")
558 });
559 const auto texCoord = createNode({
560 createPort(QShaderNodePort::Output, "texCoord")
561 });
562 const auto lightIntensity = createNode({
563 createPort(QShaderNodePort::Output, "lightIntensity")
564 });
565 const auto exposure = createNode({
566 createPort(QShaderNodePort::Output, "exposure")
567 });
568 const auto fragColor = createNode({
569 createPort(QShaderNodePort::Input, "fragColor")
570 });
571 const auto sampleTexture = createNode({
572 createPort(QShaderNodePort::Input, "sampler"),
573 createPort(QShaderNodePort::Input, "coord"),
574 createPort(QShaderNodePort::Output, "color")
575 });
576 const auto lightFunction = createNode({
577 createPort(QShaderNodePort::Input, "baseColor"),
578 createPort(QShaderNodePort::Input, "position"),
579 createPort(QShaderNodePort::Input, "lightIntensity"),
580 createPort(QShaderNodePort::Output, "outputColor")
581 });
582 const auto exposureFunction = createNode({
583 createPort(QShaderNodePort::Input, "inputColor"),
584 createPort(QShaderNodePort::Input, "exposure"),
585 createPort(QShaderNodePort::Output, "outputColor")
586 });
587
588 const auto graph = [=] {
589 auto res = QShaderGraph();
590
591 res.addNode(worldPosition);
592 res.addNode(texture);
593 res.addNode(texCoord);
594 res.addNode(lightIntensity);
595 res.addNode(exposure);
596 res.addNode(fragColor);
597 res.addNode(sampleTexture);
598 res.addNode(lightFunction);
599 res.addNode(exposureFunction);
600
601 res.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler"));
602 res.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord"));
603
604 res.addEdge(createEdge(worldPosition.uuid(), "worldPosition", lightFunction.uuid(), "position"));
605 res.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor"));
606 res.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity"));
607
608 res.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor"));
609 res.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure"));
610
611 res.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor"));
612
613 return res;
614 }();
615
616 // WHEN
617 const auto statements = graph.createStatements();
618
619 // THEN
620 const QList<QShaderGraph::Statement> expected
621 = { createStatement(texCoord, {}, {2}),
622 createStatement(texture, {}, {1}),
623 createStatement(lightIntensity, {}, {3}),
624 createStatement(sampleTexture, {1, 2}, {5}),
625 createStatement(worldPosition, {}, {0}),
626 createStatement(exposure, {}, {4}),
627 createStatement(lightFunction, {5, 0, 3}, {6}),
628 createStatement(exposureFunction, {6, 4}, {7}),
629 createStatement(fragColor, {7}, {}) };
630 dumpStatementsIfNeeded(statements, expected);
631 QCOMPARE(statements, expected);
632 }
633
634 void tst_QShaderGraph::shouldGenerateDifferentStatementsDependingOnActiveLayers()
635 {
636 // GIVEN
637 const auto texCoord = createNode({
638 createPort(QShaderNodePort::Output, "texCoord")
639 }, {
640 "diffuseTexture",
641 "normalTexture"
642 });
643 const auto diffuseUniform = createNode({
644 createPort(QShaderNodePort::Output, "color")
645 }, {"diffuseUniform"});
646 const auto diffuseTexture = createNode({
647 createPort(QShaderNodePort::Input, "coord"),
648 createPort(QShaderNodePort::Output, "color")
649 }, {"diffuseTexture"});
650 const auto normalUniform = createNode({
651 createPort(QShaderNodePort::Output, "normal")
652 }, {"normalUniform"});
653 const auto normalTexture = createNode({
654 createPort(QShaderNodePort::Input, "coord"),
655 createPort(QShaderNodePort::Output, "normal")
656 }, {"normalTexture"});
657 const auto lightFunction = createNode({
658 createPort(QShaderNodePort::Input, "color"),
659 createPort(QShaderNodePort::Input, "normal"),
660 createPort(QShaderNodePort::Output, "output")
661 });
662 const auto fragColor = createNode({
663 createPort(QShaderNodePort::Input, "fragColor")
664 });
665
666 const auto graph = [=] {
667 auto res = QShaderGraph();
668
669 res.addNode(texCoord);
670 res.addNode(diffuseUniform);
671 res.addNode(diffuseTexture);
672 res.addNode(normalUniform);
673 res.addNode(normalTexture);
674 res.addNode(lightFunction);
675 res.addNode(fragColor);
676
677 res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"}));
678 res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"}));
679 res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"}));
680
681 res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"}));
682 res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"}));
683 res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"}));
684
685 res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor"));
686
687 return res;
688 }();
689
690 {
691 // WHEN
692 const auto statements = graph.createStatements({"diffuseUniform", "normalUniform"});
693
694 // THEN
695 const QList<QShaderGraph::Statement> expected
696 = { createStatement(normalUniform, {}, {1}),
697 createStatement(diffuseUniform, {}, {0}),
698 createStatement(lightFunction, {0, 1}, {2}),
699 createStatement(fragColor, {2}, {}) };
700 dumpStatementsIfNeeded(statements, expected);
701 QCOMPARE(statements, expected);
702 }
703
704 {
705 // WHEN
706 const auto statements = graph.createStatements({"diffuseUniform", "normalTexture"});
707
708 // THEN
709 const QList<QShaderGraph::Statement> expected
710 = { createStatement(texCoord, {}, {0}),
711 createStatement(normalTexture, {0}, {2}),
712 createStatement(diffuseUniform, {}, {1}),
713 createStatement(lightFunction, {1, 2}, {3}),
714 createStatement(fragColor, {3}, {}) };
715 dumpStatementsIfNeeded(statements, expected);
716 QCOMPARE(statements, expected);
717 }
718
719 {
720 // WHEN
721 const auto statements = graph.createStatements({"diffuseTexture", "normalUniform"});
722
723 // THEN
724 const QList<QShaderGraph::Statement> expected
725 = { createStatement(texCoord, {}, {0}),
726 createStatement(normalUniform, {}, {2}),
727 createStatement(diffuseTexture, {0}, {1}),
728 createStatement(lightFunction, {1, 2}, {3}),
729 createStatement(fragColor, {3}, {}) };
730 dumpStatementsIfNeeded(statements, expected);
731 QCOMPARE(statements, expected);
732 }
733
734 {
735 // WHEN
736 const auto statements = graph.createStatements({"diffuseTexture", "normalTexture"});
737
738 // THEN
739 const QList<QShaderGraph::Statement> expected
740 = { createStatement(texCoord, {}, {0}),
741 createStatement(normalTexture, {0}, {2}),
742 createStatement(diffuseTexture, {0}, {1}),
743 createStatement(lightFunction, {1, 2}, {3}),
744 createStatement(fragColor, {3}, {}) };
745 dumpStatementsIfNeeded(statements, expected);
746 QCOMPARE(statements, expected);
747 }
748 }
749
750 void tst_QShaderGraph::shouldDealWithBranchesWithoutOutput()
751 {
752 // GIVEN
753 const auto input = createNode({
754 createPort(QShaderNodePort::Output, "input")
755 });
756 const auto output = createNode({
757 createPort(QShaderNodePort::Input, "output")
758 });
759 const auto danglingFunction = createNode({
760 createPort(QShaderNodePort::Input, "functionInput"),
761 createPort(QShaderNodePort::Output, "unbound")
762 });
763 const auto function = createNode({
764 createPort(QShaderNodePort::Input, "functionInput"),
765 createPort(QShaderNodePort::Output, "functionOutput")
766 });
767
768 const auto graph = [=] {
769 auto res = QShaderGraph();
770 res.addNode(input);
771 res.addNode(function);
772 res.addNode(danglingFunction);
773 res.addNode(output);
774 res.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput"));
775 res.addEdge(createEdge(input.uuid(), "input", danglingFunction.uuid(), "functionInput"));
776 res.addEdge(createEdge(function.uuid(), "functionOutput", output.uuid(), "output"));
777 return res;
778 }();
779
780 // WHEN
781 const auto statements = graph.createStatements();
782
783 // THEN
784 // Note that no edge leads to the unbound input
785 const QList<QShaderGraph::Statement> expected
786 = { createStatement(input, {}, {0}),
787 createStatement(function, {0}, {1}),
788 createStatement(output, {1}, {}),
789 createStatement(danglingFunction, {0}, {2}) };
790 dumpStatementsIfNeeded(statements, expected);
791 QCOMPARE(statements, expected);
792 }
793
794 void tst_QShaderGraph::shouldDiscardEdgesConnectedToDiscardedNodes()
795 {
796 // GIVEN
797 const auto input = createNode({
798 createPort(QShaderNodePort::Output, "input")
799 });
800 const auto output = createNode({
801 createPort(QShaderNodePort::Input, "output")
802 });
803 const auto function0 = createNode({
804 createPort(QShaderNodePort::Input, "function0Input"),
805 createPort(QShaderNodePort::Output, "function0Output")
806 }, {"0"});
807 const auto function1 = createNode({
808 createPort(QShaderNodePort::Input, "function1Input"),
809 createPort(QShaderNodePort::Output, "function1Output")
810 }, {"1"});
811
812 const auto graph = [=] {
813 auto res = QShaderGraph();
814 res.addNode(input);
815 res.addNode(function0);
816 res.addNode(function1);
817 res.addNode(output);
818 res.addEdge(createEdge(input.uuid(), "input", function0.uuid(), "function0Input", {"0"}));
819 res.addEdge(createEdge(input.uuid(), "input", function1.uuid(), "function1Input"));
820 res.addEdge(createEdge(function0.uuid(), "function0Output", output.uuid(), "output"));
821 res.addEdge(createEdge(function1.uuid(), "function1Output", output.uuid(), "output"));
822 return res;
823 }();
824
825 // WHEN
826 const auto statements = graph.createStatements({"0"});
827
828 // THEN
829 const QList<QShaderGraph::Statement> expected
830 = { createStatement(input, {}, {0}),
831 createStatement(function0, {0}, {1}),
832 createStatement(output, {1}, {}) };
833 dumpStatementsIfNeeded(statements, expected);
834 QCOMPARE(statements, expected);
835 }
836
837 QTEST_MAIN(tst_QShaderGraph)
838
839 #include "tst_qshadergraph.moc"