| 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/qshadergraphloader_p.h> |
| 10 | #include <Qt3DRender/private/qshaderlanguage_p.h> |
| 11 | |
| 12 | using namespace Qt3DRender; |
| 13 | |
| 14 | using QBufferPointer = QSharedPointer<QBuffer>; |
| 15 | Q_DECLARE_METATYPE(QBufferPointer); |
| 16 | |
| 17 | using PrototypeHash = QHash<QString, QShaderNode>; |
| 18 | Q_DECLARE_METATYPE(PrototypeHash); |
| 19 | |
| 20 | namespace |
| 21 | { |
| 22 | QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly) |
| 23 | { |
| 24 | auto buffer = QBufferPointer::create(); |
| 25 | buffer->setData(data); |
| 26 | if (openMode != QIODevice::NotOpen) |
| 27 | buffer->open(openMode); |
| 28 | return buffer; |
| 29 | } |
| 30 | |
| 31 | QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion) |
| 32 | { |
| 33 | auto format = QShaderFormat(); |
| 34 | format.setApi(api); |
| 35 | format.setVersion(QVersionNumber(majorVersion, minorVersion)); |
| 36 | return format; |
| 37 | } |
| 38 | |
| 39 | QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) |
| 40 | { |
| 41 | auto port = QShaderNodePort(); |
| 42 | port.direction = portDirection; |
| 43 | port.name = portName; |
| 44 | return port; |
| 45 | } |
| 46 | |
| 47 | QShaderNode createNode(const QList<QShaderNodePort> &ports, const QStringList &layers = QStringList()) |
| 48 | { |
| 49 | auto node = QShaderNode(); |
| 50 | node.setUuid(QUuid::createUuid()); |
| 51 | node.setLayers(layers); |
| 52 | for (const auto &port : ports) |
| 53 | node.addPort(port); |
| 54 | return node; |
| 55 | } |
| 56 | |
| 57 | QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, |
| 58 | const QUuid &targetUuid, const QString &targetName, |
| 59 | const QStringList &layers = QStringList()) |
| 60 | { |
| 61 | auto edge = QShaderGraph::Edge(); |
| 62 | edge.sourceNodeUuid = sourceUuid; |
| 63 | edge.sourcePortName = sourceName; |
| 64 | edge.targetNodeUuid = targetUuid; |
| 65 | edge.targetPortName = targetName; |
| 66 | edge.layers = layers; |
| 67 | return edge; |
| 68 | } |
| 69 | |
| 70 | QShaderGraph createGraph() |
| 71 | { |
| 72 | const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); |
| 73 | const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); |
| 74 | |
| 75 | auto graph = QShaderGraph(); |
| 76 | |
| 77 | auto worldPosition = createNode({ |
| 78 | createPort(QShaderNodePort::Output, "value") |
| 79 | }); |
| 80 | worldPosition.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); |
| 81 | worldPosition.setParameter("name", "worldPosition"); |
| 82 | worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Input)); |
| 83 | worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3)); |
| 84 | worldPosition.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", |
| 85 | QByteArrayList() << "$qualifier highp $type $name;")); |
| 86 | worldPosition.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", |
| 87 | QByteArrayList() << "$qualifier $type $name;")); |
| 88 | |
| 89 | auto texture = createNode({ |
| 90 | createPort(QShaderNodePort::Output, "texture") |
| 91 | }); |
| 92 | texture.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); |
| 93 | texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", |
| 94 | QByteArrayList() << "uniform sampler2D texture;")); |
| 95 | texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", |
| 96 | QByteArrayList() << "uniform sampler2D texture;")); |
| 97 | |
| 98 | auto texCoord = createNode({ |
| 99 | createPort(QShaderNodePort::Output, "texCoord") |
| 100 | }); |
| 101 | texCoord.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); |
| 102 | texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", |
| 103 | QByteArrayList() << "varying highp vec2 texCoord;")); |
| 104 | texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", |
| 105 | QByteArrayList() << "in vec2 texCoord;")); |
| 106 | |
| 107 | auto lightIntensity = createNode({ |
| 108 | createPort(QShaderNodePort::Output, "value") |
| 109 | }); |
| 110 | lightIntensity.setUuid(QUuid("{00000000-0000-0000-0000-000000000004}")); |
| 111 | lightIntensity.setParameter("name", "defaultName"); |
| 112 | lightIntensity.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); |
| 113 | lightIntensity.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); |
| 114 | lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", |
| 115 | QByteArrayList() << "$qualifier highp $type $name;")); |
| 116 | lightIntensity.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", |
| 117 | QByteArrayList() << "$qualifier $type $name;")); |
| 118 | |
| 119 | auto exposure = createNode({ |
| 120 | createPort(QShaderNodePort::Output, "exposure") |
| 121 | }); |
| 122 | exposure.setUuid(QUuid("{00000000-0000-0000-0000-000000000005}")); |
| 123 | exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", |
| 124 | QByteArrayList() << "uniform highp float exposure;")); |
| 125 | exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", |
| 126 | QByteArrayList() << "uniform float exposure;")); |
| 127 | |
| 128 | auto fragColor = createNode({ |
| 129 | createPort(QShaderNodePort::Input, "fragColor") |
| 130 | }); |
| 131 | fragColor.setUuid(QUuid("{00000000-0000-0000-0000-000000000006}")); |
| 132 | fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); |
| 133 | fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", |
| 134 | QByteArrayList() << "out vec4 fragColor;")); |
| 135 | |
| 136 | auto sampleTexture = createNode({ |
| 137 | createPort(QShaderNodePort::Input, "sampler"), |
| 138 | createPort(QShaderNodePort::Input, "coord"), |
| 139 | createPort(QShaderNodePort::Output, "color") |
| 140 | }); |
| 141 | sampleTexture.setUuid(QUuid("{00000000-0000-0000-0000-000000000007}")); |
| 142 | sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); |
| 143 | sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); |
| 144 | |
| 145 | auto lightFunction = createNode({ |
| 146 | createPort(QShaderNodePort::Input, "baseColor"), |
| 147 | createPort(QShaderNodePort::Input, "position"), |
| 148 | createPort(QShaderNodePort::Input, "lightIntensity"), |
| 149 | createPort(QShaderNodePort::Output, "outputColor") |
| 150 | }); |
| 151 | lightFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000008}")); |
| 152 | lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 153 | QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); |
| 154 | lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 155 | QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); |
| 156 | |
| 157 | auto exposureFunction = createNode({ |
| 158 | createPort(QShaderNodePort::Input, "inputColor"), |
| 159 | createPort(QShaderNodePort::Input, "exposure"), |
| 160 | createPort(QShaderNodePort::Output, "outputColor") |
| 161 | }); |
| 162 | exposureFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000009}")); |
| 163 | exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 164 | exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 165 | |
| 166 | graph.addNode(worldPosition); |
| 167 | graph.addNode(texture); |
| 168 | graph.addNode(texCoord); |
| 169 | graph.addNode(lightIntensity); |
| 170 | graph.addNode(exposure); |
| 171 | graph.addNode(fragColor); |
| 172 | graph.addNode(sampleTexture); |
| 173 | graph.addNode(lightFunction); |
| 174 | graph.addNode(exposureFunction); |
| 175 | |
| 176 | graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); |
| 177 | graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); |
| 178 | |
| 179 | graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); |
| 180 | graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); |
| 181 | graph.addEdge(createEdge(lightIntensity.uuid(), "value", lightFunction.uuid(), "lightIntensity")); |
| 182 | |
| 183 | graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); |
| 184 | graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); |
| 185 | |
| 186 | graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); |
| 187 | |
| 188 | return graph; |
| 189 | } |
| 190 | |
| 191 | void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement) |
| 192 | { |
| 193 | qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs; |
| 194 | } |
| 195 | |
| 196 | void dumpStatementsIfNeeded(const QList<QShaderGraph::Statement> &statements, const QList<QShaderGraph::Statement> &expected) |
| 197 | { |
| 198 | if (statements != expected) { |
| 199 | for (int i = 0; i < qMax(statements.size(), expected.size()); i++) { |
| 200 | qDebug() << "----" << i << "----"; |
| 201 | if (i < statements.size()) |
| 202 | debugStatement("A:", statements.at(i)); |
| 203 | if (i < expected.size()) |
| 204 | debugStatement("E:", expected.at(i)); |
| 205 | qDebug() << "-----------"; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | class tst_QShaderGraphLoader : public QObject |
| 212 | { |
| 213 | Q_OBJECT |
| 214 | private slots: |
| 215 | void shouldManipulateLoaderMembers(); |
| 216 | void shouldLoadFromJsonStream_data(); |
| 217 | void shouldLoadFromJsonStream(); |
| 218 | }; |
| 219 | |
| 220 | void tst_QShaderGraphLoader::shouldManipulateLoaderMembers() |
| 221 | { |
| 222 | // GIVEN |
| 223 | auto loader = QShaderGraphLoader(); |
| 224 | |
| 225 | // THEN (default state) |
| 226 | QCOMPARE(loader.status(), QShaderGraphLoader::Null); |
| 227 | QVERIFY(!loader.device()); |
| 228 | QVERIFY(loader.graph().nodes().isEmpty()); |
| 229 | QVERIFY(loader.graph().edges().isEmpty()); |
| 230 | QVERIFY(loader.prototypes().isEmpty()); |
| 231 | |
| 232 | // WHEN |
| 233 | auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen); |
| 234 | loader.setDevice(device1.data()); |
| 235 | |
| 236 | // THEN |
| 237 | QCOMPARE(loader.status(), QShaderGraphLoader::Error); |
| 238 | QCOMPARE(loader.device(), device1.data()); |
| 239 | QVERIFY(loader.graph().nodes().isEmpty()); |
| 240 | QVERIFY(loader.graph().edges().isEmpty()); |
| 241 | |
| 242 | // WHEN |
| 243 | auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly); |
| 244 | loader.setDevice(device2.data()); |
| 245 | |
| 246 | // THEN |
| 247 | QCOMPARE(loader.status(), QShaderGraphLoader::Waiting); |
| 248 | QCOMPARE(loader.device(), device2.data()); |
| 249 | QVERIFY(loader.graph().nodes().isEmpty()); |
| 250 | QVERIFY(loader.graph().edges().isEmpty()); |
| 251 | |
| 252 | |
| 253 | // WHEN |
| 254 | const auto prototypes = []{ |
| 255 | auto res = QHash<QString, QShaderNode>(); |
| 256 | res.insert("foo", createNode({})); |
| 257 | return res; |
| 258 | }(); |
| 259 | loader.setPrototypes(prototypes); |
| 260 | |
| 261 | // THEN |
| 262 | QCOMPARE(loader.prototypes().size(), prototypes.size()); |
| 263 | QVERIFY(loader.prototypes().contains("foo")); |
| 264 | QCOMPARE(loader.prototypes().value("foo").uuid(), prototypes.value("foo").uuid()); |
| 265 | } |
| 266 | |
| 267 | void tst_QShaderGraphLoader::shouldLoadFromJsonStream_data() |
| 268 | { |
| 269 | QTest::addColumn<QBufferPointer>("device"); |
| 270 | QTest::addColumn<PrototypeHash>("prototypes"); |
| 271 | QTest::addColumn<QShaderGraph>("graph"); |
| 272 | QTest::addColumn<QShaderGraphLoader::Status>("status"); |
| 273 | |
| 274 | QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << PrototypeHash() |
| 275 | << QShaderGraph() << QShaderGraphLoader::Error; |
| 276 | |
| 277 | const auto smallJson = "{" |
| 278 | " \"nodes\": [" |
| 279 | " {" |
| 280 | " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," |
| 281 | " \"type\": \"MyInput\"," |
| 282 | " \"layers\": [\"foo\", \"bar\"]" |
| 283 | " }," |
| 284 | " {" |
| 285 | " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," |
| 286 | " \"type\": \"MyOutput\"" |
| 287 | " }," |
| 288 | " {" |
| 289 | " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," |
| 290 | " \"type\": \"MyFunction\"" |
| 291 | " }" |
| 292 | " ]," |
| 293 | " \"edges\": [" |
| 294 | " {" |
| 295 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," |
| 296 | " \"sourcePort\": \"input\"," |
| 297 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000003}\"," |
| 298 | " \"targetPort\": \"functionInput\"," |
| 299 | " \"layers\": [\"bar\", \"baz\"]" |
| 300 | " }," |
| 301 | " {" |
| 302 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," |
| 303 | " \"sourcePort\": \"functionOutput\"," |
| 304 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000002}\"," |
| 305 | " \"targetPort\": \"output\"" |
| 306 | " }" |
| 307 | " ]" |
| 308 | "}"; |
| 309 | |
| 310 | const auto smallProtos = []{ |
| 311 | auto protos = PrototypeHash(); |
| 312 | |
| 313 | auto input = createNode({ |
| 314 | createPort(QShaderNodePort::Output, "input") |
| 315 | }); |
| 316 | protos.insert("MyInput", input); |
| 317 | |
| 318 | auto output = createNode({ |
| 319 | createPort(QShaderNodePort::Input, "output") |
| 320 | }); |
| 321 | protos.insert("MyOutput", output); |
| 322 | |
| 323 | auto function = createNode({ |
| 324 | createPort(QShaderNodePort::Input, "functionInput"), |
| 325 | createPort(QShaderNodePort::Output, "functionOutput") |
| 326 | }); |
| 327 | protos.insert("MyFunction", function); |
| 328 | return protos; |
| 329 | }(); |
| 330 | |
| 331 | const auto smallGraph = []{ |
| 332 | auto graph = QShaderGraph(); |
| 333 | |
| 334 | auto input = createNode({ |
| 335 | createPort(QShaderNodePort::Output, "input") |
| 336 | }, {"foo", "bar"}); |
| 337 | input.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); |
| 338 | auto output = createNode({ |
| 339 | createPort(QShaderNodePort::Input, "output") |
| 340 | }); |
| 341 | output.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); |
| 342 | auto function = createNode({ |
| 343 | createPort(QShaderNodePort::Input, "functionInput"), |
| 344 | createPort(QShaderNodePort::Output, "functionOutput") |
| 345 | }); |
| 346 | function.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); |
| 347 | |
| 348 | graph.addNode(input); |
| 349 | graph.addNode(output); |
| 350 | graph.addNode(function); |
| 351 | graph.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput", {"bar", "baz"})); |
| 352 | graph.addEdge(createEdge(function.uuid(), "functionOutput", output.uuid(), "output")); |
| 353 | |
| 354 | return graph; |
| 355 | }(); |
| 356 | |
| 357 | QTest::newRow("TwoNodesOneEdge") << createBuffer(smallJson) << smallProtos << smallGraph << QShaderGraphLoader::Ready; |
| 358 | QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << smallProtos << QShaderGraph() << QShaderGraphLoader::Error; |
| 359 | QTest::newRow("NoPrototype") << createBuffer(smallJson) << PrototypeHash() << QShaderGraph() << QShaderGraphLoader::Error; |
| 360 | |
| 361 | const auto complexJson = "{" |
| 362 | " \"nodes\": [" |
| 363 | " {" |
| 364 | " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," |
| 365 | " \"type\": \"inputValue\"," |
| 366 | " \"parameters\": {" |
| 367 | " \"name\": \"worldPosition\"," |
| 368 | " \"qualifier\": {" |
| 369 | " \"type\": \"QShaderLanguage::StorageQualifier\"," |
| 370 | " \"value\": \"QShaderLanguage::Input\"" |
| 371 | " }," |
| 372 | " \"type\": {" |
| 373 | " \"type\": \"QShaderLanguage::VariableType\"," |
| 374 | " \"value\": \"QShaderLanguage::Vec3\"" |
| 375 | " }" |
| 376 | " }" |
| 377 | " }," |
| 378 | " {" |
| 379 | " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," |
| 380 | " \"type\": \"texture\"" |
| 381 | " }," |
| 382 | " {" |
| 383 | " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," |
| 384 | " \"type\": \"texCoord\"" |
| 385 | " }," |
| 386 | " {" |
| 387 | " \"uuid\": \"{00000000-0000-0000-0000-000000000004}\"," |
| 388 | " \"type\": \"inputValue\"" |
| 389 | " }," |
| 390 | " {" |
| 391 | " \"uuid\": \"{00000000-0000-0000-0000-000000000005}\"," |
| 392 | " \"type\": \"exposure\"" |
| 393 | " }," |
| 394 | " {" |
| 395 | " \"uuid\": \"{00000000-0000-0000-0000-000000000006}\"," |
| 396 | " \"type\": \"fragColor\"" |
| 397 | " }," |
| 398 | " {" |
| 399 | " \"uuid\": \"{00000000-0000-0000-0000-000000000007}\"," |
| 400 | " \"type\": \"sampleTexture\"" |
| 401 | " }," |
| 402 | " {" |
| 403 | " \"uuid\": \"{00000000-0000-0000-0000-000000000008}\"," |
| 404 | " \"type\": \"lightModel\"" |
| 405 | " }," |
| 406 | " {" |
| 407 | " \"uuid\": \"{00000000-0000-0000-0000-000000000009}\"," |
| 408 | " \"type\": \"exposureFunction\"" |
| 409 | " }" |
| 410 | " ]," |
| 411 | " \"edges\": [" |
| 412 | " {" |
| 413 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000002}\"," |
| 414 | " \"sourcePort\": \"texture\"," |
| 415 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," |
| 416 | " \"targetPort\": \"sampler\"" |
| 417 | " }," |
| 418 | " {" |
| 419 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," |
| 420 | " \"sourcePort\": \"texCoord\"," |
| 421 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," |
| 422 | " \"targetPort\": \"coord\"" |
| 423 | " }," |
| 424 | " {" |
| 425 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," |
| 426 | " \"sourcePort\": \"value\"," |
| 427 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," |
| 428 | " \"targetPort\": \"position\"" |
| 429 | " }," |
| 430 | " {" |
| 431 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000007}\"," |
| 432 | " \"sourcePort\": \"color\"," |
| 433 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," |
| 434 | " \"targetPort\": \"baseColor\"" |
| 435 | " }," |
| 436 | " {" |
| 437 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000004}\"," |
| 438 | " \"sourcePort\": \"value\"," |
| 439 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," |
| 440 | " \"targetPort\": \"lightIntensity\"" |
| 441 | " }," |
| 442 | " {" |
| 443 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000008}\"," |
| 444 | " \"sourcePort\": \"outputColor\"," |
| 445 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," |
| 446 | " \"targetPort\": \"inputColor\"" |
| 447 | " }," |
| 448 | " {" |
| 449 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000005}\"," |
| 450 | " \"sourcePort\": \"exposure\"," |
| 451 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," |
| 452 | " \"targetPort\": \"exposure\"" |
| 453 | " }," |
| 454 | " {" |
| 455 | " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000009}\"," |
| 456 | " \"sourcePort\": \"outputColor\"," |
| 457 | " \"targetUuid\": \"{00000000-0000-0000-0000-000000000006}\"," |
| 458 | " \"targetPort\": \"fragColor\"" |
| 459 | " }" |
| 460 | " ]" |
| 461 | "}"; |
| 462 | |
| 463 | const auto complexProtos = []{ |
| 464 | const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); |
| 465 | const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); |
| 466 | |
| 467 | auto protos = PrototypeHash(); |
| 468 | |
| 469 | auto inputValue = createNode({ |
| 470 | createPort(QShaderNodePort::Output, "value") |
| 471 | }); |
| 472 | inputValue.setParameter("name", "defaultName"); |
| 473 | inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); |
| 474 | inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); |
| 475 | inputValue.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", |
| 476 | QByteArrayList() << "$qualifier highp $type $name;")); |
| 477 | inputValue.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", |
| 478 | QByteArrayList() << "$qualifier $type $name;")); |
| 479 | protos.insert("inputValue", inputValue); |
| 480 | |
| 481 | auto texture = createNode({ |
| 482 | createPort(QShaderNodePort::Output, "texture") |
| 483 | }); |
| 484 | texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", |
| 485 | QByteArrayList() << "uniform sampler2D texture;")); |
| 486 | texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", |
| 487 | QByteArrayList() << "uniform sampler2D texture;")); |
| 488 | protos.insert("texture", texture); |
| 489 | |
| 490 | auto texCoord = createNode({ |
| 491 | createPort(QShaderNodePort::Output, "texCoord") |
| 492 | }); |
| 493 | texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", |
| 494 | QByteArrayList() << "varying highp vec2 texCoord;")); |
| 495 | texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", |
| 496 | QByteArrayList() << "in vec2 texCoord;")); |
| 497 | protos.insert("texCoord", texCoord); |
| 498 | |
| 499 | auto exposure = createNode({ |
| 500 | createPort(QShaderNodePort::Output, "exposure") |
| 501 | }); |
| 502 | exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", |
| 503 | QByteArrayList() << "uniform highp float exposure;")); |
| 504 | exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", |
| 505 | QByteArrayList() << "uniform float exposure;")); |
| 506 | protos.insert("exposure", exposure); |
| 507 | |
| 508 | auto fragColor = createNode({ |
| 509 | createPort(QShaderNodePort::Input, "fragColor") |
| 510 | }); |
| 511 | fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); |
| 512 | fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", |
| 513 | QByteArrayList() << "out vec4 fragColor;")); |
| 514 | protos.insert("fragColor", fragColor); |
| 515 | |
| 516 | auto sampleTexture = createNode({ |
| 517 | createPort(QShaderNodePort::Input, "sampler"), |
| 518 | createPort(QShaderNodePort::Input, "coord"), |
| 519 | createPort(QShaderNodePort::Output, "color") |
| 520 | }); |
| 521 | sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); |
| 522 | sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); |
| 523 | protos.insert("sampleTexture", sampleTexture); |
| 524 | |
| 525 | auto lightModel = createNode({ |
| 526 | createPort(QShaderNodePort::Input, "baseColor"), |
| 527 | createPort(QShaderNodePort::Input, "position"), |
| 528 | createPort(QShaderNodePort::Input, "lightIntensity"), |
| 529 | createPort(QShaderNodePort::Output, "outputColor") |
| 530 | }); |
| 531 | lightModel.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 532 | QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); |
| 533 | lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 534 | QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); |
| 535 | protos.insert("lightModel", lightModel); |
| 536 | |
| 537 | auto exposureFunction = createNode({ |
| 538 | createPort(QShaderNodePort::Input, "inputColor"), |
| 539 | createPort(QShaderNodePort::Input, "exposure"), |
| 540 | createPort(QShaderNodePort::Output, "outputColor") |
| 541 | }); |
| 542 | exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 543 | exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 544 | protos.insert("exposureFunction", exposureFunction); |
| 545 | |
| 546 | return protos; |
| 547 | }(); |
| 548 | |
| 549 | const auto complexGraph = createGraph(); |
| 550 | |
| 551 | QTest::newRow("ComplexGraph") << createBuffer(complexJson) << complexProtos << complexGraph << QShaderGraphLoader::Ready; |
| 552 | } |
| 553 | |
| 554 | void tst_QShaderGraphLoader::shouldLoadFromJsonStream() |
| 555 | { |
| 556 | // GIVEN |
| 557 | QFETCH(QBufferPointer, device); |
| 558 | QFETCH(PrototypeHash, prototypes); |
| 559 | |
| 560 | auto loader = QShaderGraphLoader(); |
| 561 | |
| 562 | // WHEN |
| 563 | loader.setPrototypes(prototypes); |
| 564 | loader.setDevice(device.data()); |
| 565 | loader.load(); |
| 566 | |
| 567 | // THEN |
| 568 | QFETCH(QShaderGraphLoader::Status, status); |
| 569 | QCOMPARE(loader.status(), status); |
| 570 | |
| 571 | QFETCH(QShaderGraph, graph); |
| 572 | const auto statements = loader.graph().createStatements({"foo", "bar", "baz"}); |
| 573 | const auto expected = graph.createStatements({"foo", "bar", "baz"}); |
| 574 | dumpStatementsIfNeeded(statements, expected); |
| 575 | QCOMPARE(statements, expected); |
| 576 | |
| 577 | const auto sortedParameters = [](const QShaderNode &node) { |
| 578 | auto res = node.parameterNames(); |
| 579 | res.sort(); |
| 580 | return res; |
| 581 | }; |
| 582 | |
| 583 | for (int i = 0; i < statements.size(); i++) { |
| 584 | const auto actualNode = statements.at(i).node; |
| 585 | const auto expectedNode = expected.at(i).node; |
| 586 | |
| 587 | QCOMPARE(actualNode.layers(), expectedNode.layers()); |
| 588 | QCOMPARE(actualNode.ports(), expectedNode.ports()); |
| 589 | QCOMPARE(sortedParameters(actualNode), sortedParameters(expectedNode)); |
| 590 | for (const auto &name : expectedNode.parameterNames()) { |
| 591 | QCOMPARE(actualNode.parameter(name), expectedNode.parameter(name)); |
| 592 | } |
| 593 | QCOMPARE(actualNode.availableFormats(), expectedNode.availableFormats()); |
| 594 | for (const auto &format : expectedNode.availableFormats()) { |
| 595 | QCOMPARE(actualNode.rule(format), expectedNode.rule(format)); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | QTEST_MAIN(tst_QShaderGraphLoader) |
| 601 | |
| 602 | #include "tst_qshadergraphloader.moc" |