Debian Codegraph @

tests/auto/render/shadergraph/qshadernodesloader/tst_qshadernodesloader.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 <QtCore/qbuffer.h>
8
9 #include <Qt3DRender/private/qshadernodesloader_p.h>
10 #include <Qt3DRender/private/qshadergraphloader_p.h>
11 #include <Qt3DRender/private/qshaderlanguage_p.h>
12
13 using namespace Qt3DRender;
14
15 using QBufferPointer = QSharedPointer<QBuffer>;
16 Q_DECLARE_METATYPE(QBufferPointer);
17
18 using NodeHash = QHash<QString, QShaderNode>;
19 Q_DECLARE_METATYPE(NodeHash);
20
21 namespace
22 {
23 QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly)
24 {
25 auto buffer = QBufferPointer::create();
26 buffer->setData(data);
27 if (openMode != QIODevice::NotOpen)
28 buffer->open(openMode);
29 return buffer;
30 }
31
32 QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion,
33 const QStringList &extensions = QStringList(),
34 const QString &vendor = QString())
35 {
36 auto format = QShaderFormat();
37 format.setApi(api);
38 format.setVersion(QVersionNumber(majorVersion, minorVersion));
39 format.setExtensions(extensions);
40 format.setVendor(vendor);
41 return format;
42 }
43
44 QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName)
45 {
46 auto port = QShaderNodePort();
47 port.direction = portDirection;
48 port.name = portName;
49 return port;
50 }
51
52 QShaderNode createNode(const QList<QShaderNodePort> &ports)
53 {
54 auto node = QShaderNode();
55 for (const auto &port : ports)
56 node.addPort(port);
57 return node;
58 }
59 }
60
61 class tst_QShaderNodesLoader : public QObject
62 {
63 Q_OBJECT
64 private slots:
65 void initTestCase();
66 void shouldManipulateLoaderMembers();
67 void shouldLoadFromJsonStream_data();
68 void shouldLoadFromJsonStream();
69 };
70
71 void tst_QShaderNodesLoader::initTestCase()
72 {
73 // Make sure we register the meta types. This is done by the
74 // QShaderGraphLoader ctor usually but in this test, no QShaderGraphLoader
75 // is used so we need to to it ourselves
76 QShaderGraphLoader l;
77 Q_UNUSED(l);
78 }
79
80 void tst_QShaderNodesLoader::shouldManipulateLoaderMembers()
81 {
82 // GIVEN
83 auto loader = QShaderNodesLoader();
84
85 // THEN (default state)
86 QCOMPARE(loader.status(), QShaderNodesLoader::Null);
87 QVERIFY(!loader.device());
88 QVERIFY(loader.nodes().isEmpty());
89
90 // WHEN
91 auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen);
92 loader.setDevice(device1.data());
93
94 // THEN
95 QCOMPARE(loader.status(), QShaderNodesLoader::Error);
96 QCOMPARE(loader.device(), device1.data());
97 QVERIFY(loader.nodes().isEmpty());
98
99 // WHEN
100 auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly);
101 loader.setDevice(device2.data());
102
103 // THEN
104 QCOMPARE(loader.status(), QShaderNodesLoader::Waiting);
105 QCOMPARE(loader.device(), device2.data());
106 QVERIFY(loader.nodes().isEmpty());
107 }
108
109 void tst_QShaderNodesLoader::shouldLoadFromJsonStream_data()
110 {
111 QTest::addColumn<QBufferPointer>("device");
112 QTest::addColumn<NodeHash>("nodes");
113 QTest::addColumn<QShaderNodesLoader::Status>("status");
114
115 QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << NodeHash() << QShaderNodesLoader::Error;
116
117 const auto smallJson = "{"
118 " \"inputValue\": {"
119 " \"outputs\": ["
120 " \"value\""
121 " ],"
122 " \"parameters\": {"
123 " \"name\": \"defaultName\","
124 " \"qualifier\": {"
125 " \"type\": \"QShaderLanguage::StorageQualifier\","
126 " \"value\": \"QShaderLanguage::Uniform\""
127 " },"
128 " \"type\": {"
129 " \"type\": \"QShaderLanguage::VariableType\","
130 " \"value\": \"QShaderLanguage::Vec3\""
131 " },"
132 " \"defaultValue\": {"
133 " \"type\": \"float\","
134 " \"value\": \"1.25\""
135 " }"
136 " },"
137 " \"rules\": ["
138 " {"
139 " \"format\": {"
140 " \"api\": \"OpenGLES\","
141 " \"major\": 2,"
142 " \"minor\": 0"
143 " },"
144 " \"substitution\": \"highp vec3 $value = $name;\","
145 " \"headerSnippets\": [ \"varying highp vec3 $name;\" ]"
146 " },"
147 " {"
148 " \"format\": {"
149 " \"api\": \"OpenGLCompatibilityProfile\","
150 " \"major\": 2,"
151 " \"minor\": 1"
152 " },"
153 " \"substitution\": \"vec3 $value = $name;\","
154 " \"headerSnippets\": [ \"in vec3 $name;\" ]"
155 " }"
156 " ]"
157 " },"
158 " \"fragColor\": {"
159 " \"inputs\": ["
160 " \"fragColor\""
161 " ],"
162 " \"rules\": ["
163 " {"
164 " \"format\": {"
165 " \"api\": \"OpenGLES\","
166 " \"major\": 2,"
167 " \"minor\": 0"
168 " },"
169 " \"substitution\": \"gl_fragColor = $fragColor;\""
170 " },"
171 " {"
172 " \"format\": {"
173 " \"api\": \"OpenGLNoProfile\","
174 " \"major\": 4,"
175 " \"minor\": 0"
176 " },"
177 " \"substitution\": \"fragColor = $fragColor;\","
178 " \"headerSnippets\": [ \"out vec4 fragColor;\" ]"
179 " }"
180 " ]"
181 " },"
182 " \"lightModel\": {"
183 " \"inputs\": ["
184 " \"baseColor\","
185 " \"position\","
186 " \"lightIntensity\""
187 " ],"
188 " \"outputs\": ["
189 " \"outputColor\""
190 " ],"
191 " \"rules\": ["
192 " {"
193 " \"format\": {"
194 " \"api\": \"OpenGLES\","
195 " \"major\": 2,"
196 " \"minor\": 0,"
197 " \"extensions\": [ \"ext1\", \"ext2\" ],"
198 " \"vendor\": \"kdab\""
199 " },"
200 " \"substitution\": \"highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\","
201 " \"headerSnippets\": [ \"#pragma include es2/lightmodel.frag.inc\" ]"
202 " },"
203 " {"
204 " \"format\": {"
205 " \"api\": \"OpenGLCoreProfile\","
206 " \"major\": 3,"
207 " \"minor\": 3"
208 " },"
209 " \"substitution\": \"vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\","
210 " \"headerSnippets\": [ \"#pragma include gl3/lightmodel.frag.inc\" ]"
211 " }"
212 " ]"
213 " }"
214 "}";
215
216 const auto smallProtos = []{
217 const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0);
218 const auto openGLES2Extended = createFormat(QShaderFormat::OpenGLES, 2, 0, {"ext1", "ext2"}, "kdab");
219 const auto openGL2 = createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1);
220 const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3);
221 const auto openGL4 = createFormat(QShaderFormat::OpenGLNoProfile, 4, 0);
222
223 auto protos = NodeHash();
224
225 auto inputValue = createNode({
226 createPort(QShaderNodePort::Output, "value")
227 });
228 inputValue.setParameter("name", "defaultName");
229 inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform));
230 inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3));
231 inputValue.setParameter("defaultValue", QVariant(1.25f));
232 inputValue.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;",
233 QByteArrayList() << "varying highp vec3 $name;"));
234 inputValue.addRule(openGL2, QShaderNode::Rule("vec3 $value = $name;",
235 QByteArrayList() << "in vec3 $name;"));
236 protos.insert("inputValue", inputValue);
237
238 auto fragColor = createNode({
239 createPort(QShaderNodePort::Input, "fragColor")
240 });
241 fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;"));
242 fragColor.addRule(openGL4, QShaderNode::Rule("fragColor = $fragColor;",
243 QByteArrayList() << "out vec4 fragColor;"));
244 protos.insert(QStringLiteral("fragColor"), fragColor);
245
246 auto lightModel = createNode({
247 createPort(QShaderNodePort::Input, "baseColor"),
248 createPort(QShaderNodePort::Input, "position"),
249 createPort(QShaderNodePort::Input, "lightIntensity"),
250 createPort(QShaderNodePort::Output, "outputColor")
251 });
252 lightModel.addRule(openGLES2Extended, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);",
253 QByteArrayList() << "#pragma include es2/lightmodel.frag.inc"));
254 lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);",
255 QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc"));
256 protos.insert("lightModel", lightModel);
257
258 return protos;
259 }();
260
261 QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << NodeHash() << QShaderNodesLoader::Error;
262 QTest::newRow("CorrectJSON") << createBuffer(smallJson) << smallProtos << QShaderNodesLoader::Ready;
263 }
264
265 void tst_QShaderNodesLoader::shouldLoadFromJsonStream()
266 {
267 // GIVEN
268 QFETCH(QBufferPointer, device);
269
270 auto loader = QShaderNodesLoader();
271
272 // WHEN
273 loader.setDevice(device.data());
274 loader.load();
275
276 // THEN
277 QFETCH(QShaderNodesLoader::Status, status);
278 QCOMPARE(loader.status(), status);
279
280 QFETCH(NodeHash, nodes);
281 const auto sortedKeys = [](const NodeHash &nodes) {
282 auto res = nodes.keys();
283 res.sort();
284 return res;
285 };
286 const auto sortedParameters = [](const QShaderNode &node) {
287 auto res = node.parameterNames();
288 res.sort();
289 return res;
290 };
291 QCOMPARE(sortedKeys(loader.nodes()), sortedKeys(nodes));
292 for (const auto &key : nodes.keys()) {
293 const auto actual = loader.nodes().value(key);
294 const auto expected = nodes.value(key);
295
296 QVERIFY(actual.uuid().isNull());
297 QCOMPARE(actual.ports(), expected.ports());
298 QCOMPARE(sortedParameters(actual), sortedParameters(expected));
299 for (const auto &name : expected.parameterNames()) {
300 QCOMPARE(actual.parameter(name), expected.parameter(name));
301 }
302 QCOMPARE(actual.availableFormats(), expected.availableFormats());
303 for (const auto &format : expected.availableFormats()) {
304 QCOMPARE(actual.rule(format), expected.rule(format));
305 }
306 }
307 }
308
309 QTEST_MAIN(tst_QShaderNodesLoader)
310
311 #include "tst_qshadernodesloader.moc"