| 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/qmetaobject.h> |
| 8 | #include <Qt3DRender/private/qshadergenerator_p.h> |
| 9 | #include <Qt3DRender/private/qshaderlanguage_p.h> |
| 10 | |
| 11 | using namespace Qt3DRender; |
| 12 | namespace |
| 13 | { |
| 14 | QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, |
| 15 | QShaderFormat::ShaderType shaderType= QShaderFormat::Fragment) |
| 16 | { |
| 17 | auto format = QShaderFormat(); |
| 18 | format.setApi(api); |
| 19 | format.setVersion(QVersionNumber(majorVersion, minorVersion)); |
| 20 | format.setShaderType(shaderType); |
| 21 | return format; |
| 22 | } |
| 23 | |
| 24 | QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) |
| 25 | { |
| 26 | auto port = QShaderNodePort(); |
| 27 | port.direction = portDirection; |
| 28 | port.name = portName; |
| 29 | return port; |
| 30 | } |
| 31 | |
| 32 | QShaderNode createNode(const QList<QShaderNodePort> &ports, const QStringList &layers = QStringList()) |
| 33 | { |
| 34 | auto node = QShaderNode(); |
| 35 | node.setUuid(QUuid::createUuid()); |
| 36 | node.setLayers(layers); |
| 37 | for (const auto &port : ports) |
| 38 | node.addPort(port); |
| 39 | return node; |
| 40 | } |
| 41 | |
| 42 | QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, |
| 43 | const QUuid &targetUuid, const QString &targetName, |
| 44 | const QStringList &layers = QStringList()) |
| 45 | { |
| 46 | auto edge = QShaderGraph::Edge(); |
| 47 | edge.sourceNodeUuid = sourceUuid; |
| 48 | edge.sourcePortName = sourceName; |
| 49 | edge.targetNodeUuid = targetUuid; |
| 50 | edge.targetPortName = targetName; |
| 51 | edge.layers = layers; |
| 52 | return edge; |
| 53 | } |
| 54 | |
| 55 | QShaderGraph createFragmentShaderGraph() |
| 56 | { |
| 57 | const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); |
| 58 | const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); |
| 59 | |
| 60 | auto graph = QShaderGraph(); |
| 61 | |
| 62 | auto worldPosition = createNode({ |
| 63 | createPort(QShaderNodePort::Output, "value") |
| 64 | }); |
| 65 | worldPosition.setParameter("name", "worldPosition"); |
| 66 | worldPosition.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", |
| 67 | QByteArrayList() << "varying highp vec3 $name;")); |
| 68 | worldPosition.addRule(openGL3, QShaderNode::Rule("vec3 $value = $name;", |
| 69 | QByteArrayList() << "in vec3 $name;")); |
| 70 | |
| 71 | auto texture = createNode({ |
| 72 | createPort(QShaderNodePort::Output, "texture") |
| 73 | }); |
| 74 | texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", |
| 75 | QByteArrayList() << "uniform sampler2D texture;")); |
| 76 | texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", |
| 77 | QByteArrayList() << "uniform sampler2D texture;")); |
| 78 | |
| 79 | auto texCoord = createNode({ |
| 80 | createPort(QShaderNodePort::Output, "texCoord") |
| 81 | }); |
| 82 | texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", |
| 83 | QByteArrayList() << "varying highp vec2 texCoord;")); |
| 84 | texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", |
| 85 | QByteArrayList() << "in vec2 texCoord;")); |
| 86 | |
| 87 | auto lightIntensity = createNode({ |
| 88 | createPort(QShaderNodePort::Output, "lightIntensity") |
| 89 | }); |
| 90 | lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp float $lightIntensity = lightIntensity;", |
| 91 | QByteArrayList() << "uniform highp float lightIntensity;")); |
| 92 | lightIntensity.addRule(openGL3, QShaderNode::Rule("float $lightIntensity = lightIntensity;", |
| 93 | QByteArrayList() << "uniform float lightIntensity;")); |
| 94 | |
| 95 | auto exposure = createNode({ |
| 96 | createPort(QShaderNodePort::Output, "exposure") |
| 97 | }); |
| 98 | exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", |
| 99 | QByteArrayList() << "uniform highp float exposure;")); |
| 100 | exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", |
| 101 | QByteArrayList() << "uniform float exposure;")); |
| 102 | |
| 103 | auto fragColor = createNode({ |
| 104 | createPort(QShaderNodePort::Input, "fragColor") |
| 105 | }); |
| 106 | fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); |
| 107 | fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", |
| 108 | QByteArrayList() << "out vec4 fragColor;")); |
| 109 | |
| 110 | auto sampleTexture = createNode({ |
| 111 | createPort(QShaderNodePort::Input, "sampler"), |
| 112 | createPort(QShaderNodePort::Input, "coord"), |
| 113 | createPort(QShaderNodePort::Output, "color") |
| 114 | }); |
| 115 | sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); |
| 116 | sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture($sampler, $coord);")); |
| 117 | |
| 118 | auto lightFunction = createNode({ |
| 119 | createPort(QShaderNodePort::Input, "baseColor"), |
| 120 | createPort(QShaderNodePort::Input, "position"), |
| 121 | createPort(QShaderNodePort::Input, "lightIntensity"), |
| 122 | createPort(QShaderNodePort::Output, "outputColor") |
| 123 | }); |
| 124 | lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 125 | QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); |
| 126 | lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", |
| 127 | QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); |
| 128 | |
| 129 | auto exposureFunction = createNode({ |
| 130 | createPort(QShaderNodePort::Input, "inputColor"), |
| 131 | createPort(QShaderNodePort::Input, "exposure"), |
| 132 | createPort(QShaderNodePort::Output, "outputColor") |
| 133 | }); |
| 134 | exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 135 | exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); |
| 136 | |
| 137 | graph.addNode(worldPosition); |
| 138 | graph.addNode(texture); |
| 139 | graph.addNode(texCoord); |
| 140 | graph.addNode(lightIntensity); |
| 141 | graph.addNode(exposure); |
| 142 | graph.addNode(fragColor); |
| 143 | graph.addNode(sampleTexture); |
| 144 | graph.addNode(lightFunction); |
| 145 | graph.addNode(exposureFunction); |
| 146 | |
| 147 | graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); |
| 148 | graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); |
| 149 | |
| 150 | graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); |
| 151 | graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); |
| 152 | graph.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity")); |
| 153 | |
| 154 | graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); |
| 155 | graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); |
| 156 | |
| 157 | graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); |
| 158 | |
| 159 | return graph; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | class tst_QShaderGenerator : public QObject |
| 164 | { |
| 165 | Q_OBJECT |
| 166 | private slots: |
| 167 | void shouldHaveDefaultState(); |
| 168 | void shouldGenerateShaderCode_data(); |
| 169 | void shouldGenerateShaderCode(); |
| 170 | void shouldGenerateVersionCommands_data(); |
| 171 | void shouldGenerateVersionCommands(); |
| 172 | void shouldProcessLanguageQualifierAndTypeEnums_data(); |
| 173 | void shouldProcessLanguageQualifierAndTypeEnums(); |
| 174 | void shouldGenerateDifferentCodeDependingOnActiveLayers(); |
| 175 | void shouldUseGlobalVariableRatherThanTemporaries(); |
| 176 | void shouldGenerateTemporariesWisely(); |
| 177 | void shouldHandlePortNamesPrefixingOneAnother(); |
| 178 | void shouldHandleNodesWithMultipleOutputPorts(); |
| 179 | void shouldHandleExpressionsInInputNodes(); |
| 180 | void shouldGenerateLayerDefines(); |
| 181 | }; |
| 182 | |
| 183 | void tst_QShaderGenerator::shouldHaveDefaultState() |
| 184 | { |
| 185 | // GIVEN |
| 186 | auto generator = QShaderGenerator(); |
| 187 | |
| 188 | // THEN |
| 189 | QVERIFY(generator.graph.nodes().isEmpty()); |
| 190 | QVERIFY(generator.graph.edges().isEmpty()); |
| 191 | QVERIFY(!generator.format.isValid()); |
| 192 | } |
| 193 | |
| 194 | void tst_QShaderGenerator::shouldGenerateShaderCode_data() |
| 195 | { |
| 196 | QTest::addColumn<QShaderGraph>("graph"); |
| 197 | QTest::addColumn<QShaderFormat>("format"); |
| 198 | QTest::addColumn<QByteArray>("expectedCode"); |
| 199 | |
| 200 | const auto graph = createFragmentShaderGraph(); |
| 201 | |
| 202 | const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); |
| 203 | const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); |
| 204 | const auto openGL32 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2); |
| 205 | const auto openGL4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 206 | |
| 207 | const auto versionGLES2 = QByteArrayList() << "#version 100" << ""; |
| 208 | const auto versionGL3 = QByteArrayList() << "#version 130" << ""; |
| 209 | const auto versionGL32 = QByteArrayList() << "#version 150 core" << ""; |
| 210 | const auto versionGL4 = QByteArrayList() << "#version 400 core" << ""; |
| 211 | |
| 212 | const auto es2Code = QByteArrayList() |
| 213 | << "varying highp vec2 texCoord;" |
| 214 | << "uniform sampler2D texture;" |
| 215 | << "uniform highp float lightIntensity;" |
| 216 | << "varying highp vec3 worldPosition;" |
| 217 | << "uniform highp float exposure;" |
| 218 | << "#pragma include es2/lightmodel.frag.inc" |
| 219 | << "" |
| 220 | << "void main()" |
| 221 | << "{" |
| 222 | << " gl_fragColor = (((((lightModel(((texture2D(texture, texCoord))), " |
| 223 | "worldPosition, lightIntensity)))) * pow(2.0, exposure)));" |
| 224 | << "}" |
| 225 | << ""; |
| 226 | |
| 227 | const auto gl3Code = QByteArrayList() |
| 228 | << "in vec2 texCoord;" |
| 229 | << "uniform sampler2D texture;" |
| 230 | << "uniform float lightIntensity;" |
| 231 | << "in vec3 worldPosition;" |
| 232 | << "uniform float exposure;" |
| 233 | << "#pragma include gl3/lightmodel.frag.inc" |
| 234 | << "out vec4 fragColor;" |
| 235 | << "" |
| 236 | << "void main()" |
| 237 | << "{" |
| 238 | << " fragColor = (((((lightModel(((texture(texture, texCoord))), worldPosition, " |
| 239 | "lightIntensity)))) * pow(2.0, exposure)));" |
| 240 | << "}" |
| 241 | << ""; |
| 242 | |
| 243 | QTest::newRow("EmptyGraphAndFormat") << QShaderGraph() << QShaderFormat() << QByteArrayLiteral("\n\n\nvoid main()\n{\n}\n"); |
| 244 | QTest::newRow("LightExposureGraphAndES2") << graph << openGLES2 << (versionGLES2 + es2Code).join('\n'); |
| 245 | QTest::newRow("LightExposureGraphAndGL3") << graph << openGL3 << (versionGL3 + gl3Code).join('\n'); |
| 246 | QTest::newRow("LightExposureGraphAndGL32") << graph << openGL32 << (versionGL32 + gl3Code).join('\n'); |
| 247 | QTest::newRow("LightExposureGraphAndGL4") << graph << openGL4 << (versionGL4 + gl3Code).join('\n'); |
| 248 | } |
| 249 | |
| 250 | void tst_QShaderGenerator::shouldGenerateShaderCode() |
| 251 | { |
| 252 | // GIVEN |
| 253 | QFETCH(QShaderGraph, graph); |
| 254 | QFETCH(QShaderFormat, format); |
| 255 | |
| 256 | auto generator = QShaderGenerator(); |
| 257 | generator.graph = graph; |
| 258 | generator.format = format; |
| 259 | |
| 260 | // WHEN |
| 261 | const auto code = generator.createShaderCode(); |
| 262 | |
| 263 | // THEN |
| 264 | QFETCH(QByteArray, expectedCode); |
| 265 | QCOMPARE(code, expectedCode); |
| 266 | } |
| 267 | |
| 268 | void tst_QShaderGenerator::shouldGenerateVersionCommands_data() |
| 269 | { |
| 270 | QTest::addColumn<QShaderFormat>("format"); |
| 271 | QTest::addColumn<QByteArray>("version"); |
| 272 | |
| 273 | QTest::newRow("GLES2") << createFormat(QShaderFormat::OpenGLES, 2, 0) << QByteArrayLiteral("#version 100"); |
| 274 | QTest::newRow("GLES3") << createFormat(QShaderFormat::OpenGLES, 3, 0) << QByteArrayLiteral("#version 300 es"); |
| 275 | |
| 276 | QTest::newRow("GL20") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) << QByteArrayLiteral("#version 110"); |
| 277 | QTest::newRow("GL21") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 1) << QByteArrayLiteral("#version 120"); |
| 278 | QTest::newRow("GL30") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 0) << QByteArrayLiteral("#version 130"); |
| 279 | QTest::newRow("GL31") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 1) << QByteArrayLiteral("#version 140"); |
| 280 | QTest::newRow("GL32") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 2) << QByteArrayLiteral("#version 150"); |
| 281 | QTest::newRow("GL33") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 3) << QByteArrayLiteral("#version 330"); |
| 282 | QTest::newRow("GL40") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 0) << QByteArrayLiteral("#version 400"); |
| 283 | QTest::newRow("GL41") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 1) << QByteArrayLiteral("#version 410"); |
| 284 | QTest::newRow("GL42") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 2) << QByteArrayLiteral("#version 420"); |
| 285 | QTest::newRow("GL43") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 3) << QByteArrayLiteral("#version 430"); |
| 286 | |
| 287 | QTest::newRow("GL20core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) << QByteArrayLiteral("#version 110"); |
| 288 | QTest::newRow("GL21core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 1) << QByteArrayLiteral("#version 120"); |
| 289 | QTest::newRow("GL30core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) << QByteArrayLiteral("#version 130"); |
| 290 | QTest::newRow("GL31core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) << QByteArrayLiteral("#version 140"); |
| 291 | QTest::newRow("GL32core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2) << QByteArrayLiteral("#version 150 core"); |
| 292 | QTest::newRow("GL33core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3) << QByteArrayLiteral("#version 330 core"); |
| 293 | QTest::newRow("GL40core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0) << QByteArrayLiteral("#version 400 core"); |
| 294 | QTest::newRow("GL41core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 1) << QByteArrayLiteral("#version 410 core"); |
| 295 | QTest::newRow("GL42core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 2) << QByteArrayLiteral("#version 420 core"); |
| 296 | QTest::newRow("GL43core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 3) << QByteArrayLiteral("#version 430 core"); |
| 297 | |
| 298 | QTest::newRow("GL20compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) << QByteArrayLiteral("#version 110"); |
| 299 | QTest::newRow("GL21compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1) << QByteArrayLiteral("#version 120"); |
| 300 | QTest::newRow("GL30compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 0) << QByteArrayLiteral("#version 130"); |
| 301 | QTest::newRow("GL31compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 1) << QByteArrayLiteral("#version 140"); |
| 302 | QTest::newRow("GL32compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 2) << QByteArrayLiteral("#version 150 compatibility"); |
| 303 | QTest::newRow("GL33compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 3) << QByteArrayLiteral("#version 330 compatibility"); |
| 304 | QTest::newRow("GL40compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 0) << QByteArrayLiteral("#version 400 compatibility"); |
| 305 | QTest::newRow("GL41compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 1) << QByteArrayLiteral("#version 410 compatibility"); |
| 306 | QTest::newRow("GL42compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 2) << QByteArrayLiteral("#version 420 compatibility"); |
| 307 | QTest::newRow("GL43compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 3) << QByteArrayLiteral("#version 430 compatibility"); |
| 308 | } |
| 309 | |
| 310 | void tst_QShaderGenerator::shouldGenerateVersionCommands() |
| 311 | { |
| 312 | // GIVEN |
| 313 | QFETCH(QShaderFormat, format); |
| 314 | |
| 315 | auto generator = QShaderGenerator(); |
| 316 | generator.format = format; |
| 317 | |
| 318 | // WHEN |
| 319 | const auto code = generator.createShaderCode(); |
| 320 | |
| 321 | // THEN |
| 322 | QFETCH(QByteArray, version); |
| 323 | const auto expectedCode = (QByteArrayList() << version |
| 324 | << "" |
| 325 | << "" |
| 326 | << "void main()" |
| 327 | << "{" |
| 328 | << "}" |
| 329 | << "").join('\n'); |
| 330 | QCOMPARE(code, expectedCode); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | namespace { |
| 335 | QString toGlsl(QShaderLanguage::StorageQualifier qualifier, const QShaderFormat &format) |
| 336 | { |
| 337 | if (format.version().majorVersion() <= 2) { |
| 338 | // Note we're assuming fragment shader only here, it'd be different |
| 339 | // values for vertex shader, will need to be fixed properly at some |
| 340 | // point but isn't necessary yet (this problem already exists in past |
| 341 | // commits anyway) |
| 342 | switch (qualifier) { |
| 343 | case QShaderLanguage::Const: |
| 344 | return "const"; |
| 345 | case QShaderLanguage::Input: |
| 346 | return "varying"; |
| 347 | case QShaderLanguage::BuiltIn: |
| 348 | return "//"; |
| 349 | case QShaderLanguage::Output: |
| 350 | return ""; // Although fragment shaders for <=2 only have fixed outputs |
| 351 | case QShaderLanguage::Uniform: |
| 352 | return "uniform"; |
| 353 | } |
| 354 | } else { |
| 355 | switch (qualifier) { |
| 356 | case QShaderLanguage::Const: |
| 357 | return "const"; |
| 358 | case QShaderLanguage::Input: |
| 359 | return "in"; |
| 360 | case QShaderLanguage::BuiltIn: |
| 361 | return "//"; |
| 362 | case QShaderLanguage::Output: |
| 363 | return "out"; |
| 364 | case QShaderLanguage::Uniform: |
| 365 | return "uniform"; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | Q_UNREACHABLE(); |
| 370 | } |
| 371 | |
| 372 | QString toGlsl(QShaderLanguage::VariableType type) |
| 373 | { |
| 374 | switch (type) { |
| 375 | case QShaderLanguage::Bool: |
| 376 | return "bool"; |
| 377 | case QShaderLanguage::Int: |
| 378 | return "int"; |
| 379 | case QShaderLanguage::Uint: |
| 380 | return "uint"; |
| 381 | case QShaderLanguage::Float: |
| 382 | return "float"; |
| 383 | case QShaderLanguage::Double: |
| 384 | return "double"; |
| 385 | case QShaderLanguage::Vec2: |
| 386 | return "vec2"; |
| 387 | case QShaderLanguage::Vec3: |
| 388 | return "vec3"; |
| 389 | case QShaderLanguage::Vec4: |
| 390 | return "vec4"; |
| 391 | case QShaderLanguage::DVec2: |
| 392 | return "dvec2"; |
| 393 | case QShaderLanguage::DVec3: |
| 394 | return "dvec3"; |
| 395 | case QShaderLanguage::DVec4: |
| 396 | return "dvec4"; |
| 397 | case QShaderLanguage::BVec2: |
| 398 | return "bvec2"; |
| 399 | case QShaderLanguage::BVec3: |
| 400 | return "bvec3"; |
| 401 | case QShaderLanguage::BVec4: |
| 402 | return "bvec4"; |
| 403 | case QShaderLanguage::IVec2: |
| 404 | return "ivec2"; |
| 405 | case QShaderLanguage::IVec3: |
| 406 | return "ivec3"; |
| 407 | case QShaderLanguage::IVec4: |
| 408 | return "ivec4"; |
| 409 | case QShaderLanguage::UVec2: |
| 410 | return "uvec2"; |
| 411 | case QShaderLanguage::UVec3: |
| 412 | return "uvec3"; |
| 413 | case QShaderLanguage::UVec4: |
| 414 | return "uvec4"; |
| 415 | case QShaderLanguage::Mat2: |
| 416 | return "mat2"; |
| 417 | case QShaderLanguage::Mat3: |
| 418 | return "mat3"; |
| 419 | case QShaderLanguage::Mat4: |
| 420 | return "mat4"; |
| 421 | case QShaderLanguage::Mat2x2: |
| 422 | return "mat2x2"; |
| 423 | case QShaderLanguage::Mat2x3: |
| 424 | return "mat2x3"; |
| 425 | case QShaderLanguage::Mat2x4: |
| 426 | return "mat2x4"; |
| 427 | case QShaderLanguage::Mat3x2: |
| 428 | return "mat3x2"; |
| 429 | case QShaderLanguage::Mat3x3: |
| 430 | return "mat3x3"; |
| 431 | case QShaderLanguage::Mat3x4: |
| 432 | return "mat3x4"; |
| 433 | case QShaderLanguage::Mat4x2: |
| 434 | return "mat4x2"; |
| 435 | case QShaderLanguage::Mat4x3: |
| 436 | return "mat4x3"; |
| 437 | case QShaderLanguage::Mat4x4: |
| 438 | return "mat4x4"; |
| 439 | case QShaderLanguage::DMat2: |
| 440 | return "dmat2"; |
| 441 | case QShaderLanguage::DMat3: |
| 442 | return "dmat3"; |
| 443 | case QShaderLanguage::DMat4: |
| 444 | return "dmat4"; |
| 445 | case QShaderLanguage::DMat2x2: |
| 446 | return "dmat2x2"; |
| 447 | case QShaderLanguage::DMat2x3: |
| 448 | return "dmat2x3"; |
| 449 | case QShaderLanguage::DMat2x4: |
| 450 | return "dmat2x4"; |
| 451 | case QShaderLanguage::DMat3x2: |
| 452 | return "dmat3x2"; |
| 453 | case QShaderLanguage::DMat3x3: |
| 454 | return "dmat3x3"; |
| 455 | case QShaderLanguage::DMat3x4: |
| 456 | return "dmat3x4"; |
| 457 | case QShaderLanguage::DMat4x2: |
| 458 | return "dmat4x2"; |
| 459 | case QShaderLanguage::DMat4x3: |
| 460 | return "dmat4x3"; |
| 461 | case QShaderLanguage::DMat4x4: |
| 462 | return "dmat4x4"; |
| 463 | case QShaderLanguage::Sampler1D: |
| 464 | return "sampler1D"; |
| 465 | case QShaderLanguage::Sampler2D: |
| 466 | return "sampler2D"; |
| 467 | case QShaderLanguage::Sampler3D: |
| 468 | return "sampler3D"; |
| 469 | case QShaderLanguage::SamplerCube: |
| 470 | return "samplerCube"; |
| 471 | case QShaderLanguage::Sampler2DRect: |
| 472 | return "sampler2DRect"; |
| 473 | case QShaderLanguage::Sampler2DMs: |
| 474 | return "sampler2DMS"; |
| 475 | case QShaderLanguage::SamplerBuffer: |
| 476 | return "samplerBuffer"; |
| 477 | case QShaderLanguage::Sampler1DArray: |
| 478 | return "sampler1DArray"; |
| 479 | case QShaderLanguage::Sampler2DArray: |
| 480 | return "sampler2DArray"; |
| 481 | case QShaderLanguage::Sampler2DMsArray: |
| 482 | return "sampler2DMSArray"; |
| 483 | case QShaderLanguage::SamplerCubeArray: |
| 484 | return "samplerCubeArray"; |
| 485 | case QShaderLanguage::Sampler1DShadow: |
| 486 | return "sampler1DShadow"; |
| 487 | case QShaderLanguage::Sampler2DShadow: |
| 488 | return "sampler2DShadow"; |
| 489 | case QShaderLanguage::Sampler2DRectShadow: |
| 490 | return "sampler2DRectShadow"; |
| 491 | case QShaderLanguage::Sampler1DArrayShadow: |
| 492 | return "sampler1DArrayShadow"; |
| 493 | case QShaderLanguage::Sampler2DArrayShadow: |
| 494 | return "sampler2DArrayShadow"; |
| 495 | case QShaderLanguage::SamplerCubeShadow: |
| 496 | return "samplerCubeShadow"; |
| 497 | case QShaderLanguage::SamplerCubeArrayShadow: |
| 498 | return "samplerCubeArrayShadow"; |
| 499 | case QShaderLanguage::ISampler1D: |
| 500 | return "isampler1D"; |
| 501 | case QShaderLanguage::ISampler2D: |
| 502 | return "isampler2D"; |
| 503 | case QShaderLanguage::ISampler3D: |
| 504 | return "isampler3D"; |
| 505 | case QShaderLanguage::ISamplerCube: |
| 506 | return "isamplerCube"; |
| 507 | case QShaderLanguage::ISampler2DRect: |
| 508 | return "isampler2DRect"; |
| 509 | case QShaderLanguage::ISampler2DMs: |
| 510 | return "isampler2DMS"; |
| 511 | case QShaderLanguage::ISamplerBuffer: |
| 512 | return "isamplerBuffer"; |
| 513 | case QShaderLanguage::ISampler1DArray: |
| 514 | return "isampler1DArray"; |
| 515 | case QShaderLanguage::ISampler2DArray: |
| 516 | return "isampler2DArray"; |
| 517 | case QShaderLanguage::ISampler2DMsArray: |
| 518 | return "isampler2DMSArray"; |
| 519 | case QShaderLanguage::ISamplerCubeArray: |
| 520 | return "isamplerCubeArray"; |
| 521 | case QShaderLanguage::USampler1D: |
| 522 | return "usampler1D"; |
| 523 | case QShaderLanguage::USampler2D: |
| 524 | return "usampler2D"; |
| 525 | case QShaderLanguage::USampler3D: |
| 526 | return "usampler3D"; |
| 527 | case QShaderLanguage::USamplerCube: |
| 528 | return "usamplerCube"; |
| 529 | case QShaderLanguage::USampler2DRect: |
| 530 | return "usampler2DRect"; |
| 531 | case QShaderLanguage::USampler2DMs: |
| 532 | return "usampler2DMS"; |
| 533 | case QShaderLanguage::USamplerBuffer: |
| 534 | return "usamplerBuffer"; |
| 535 | case QShaderLanguage::USampler1DArray: |
| 536 | return "usampler1DArray"; |
| 537 | case QShaderLanguage::USampler2DArray: |
| 538 | return "usampler2DArray"; |
| 539 | case QShaderLanguage::USampler2DMsArray: |
| 540 | return "usampler2DMSArray"; |
| 541 | case QShaderLanguage::USamplerCubeArray: |
| 542 | return "usamplerCubeArray"; |
| 543 | } |
| 544 | |
| 545 | Q_UNREACHABLE(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums_data() |
| 550 | { |
| 551 | QTest::addColumn<QShaderGraph>("graph"); |
| 552 | QTest::addColumn<QShaderFormat>("format"); |
| 553 | QTest::addColumn<QByteArray>("expectedCode"); |
| 554 | |
| 555 | { |
| 556 | const auto es2 = createFormat(QShaderFormat::OpenGLES, 2, 0); |
| 557 | const auto es3 = createFormat(QShaderFormat::OpenGLES, 3, 0); |
| 558 | const auto gl2 = createFormat(QShaderFormat::OpenGLNoProfile, 2, 0); |
| 559 | const auto gl3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); |
| 560 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 561 | |
| 562 | const auto qualifierEnum = QMetaEnum::fromType<QShaderLanguage::StorageQualifier>(); |
| 563 | const auto typeEnum = QMetaEnum::fromType<QShaderLanguage::VariableType>(); |
| 564 | |
| 565 | for (int qualifierIndex = 0; qualifierIndex < qualifierEnum.keyCount(); qualifierIndex++) { |
| 566 | const auto qualifierName = qualifierEnum.key(qualifierIndex); |
| 567 | const auto qualifierValue = static_cast<QShaderLanguage::StorageQualifier>(qualifierEnum.value(qualifierIndex)); |
| 568 | |
| 569 | for (int typeIndex = 0; typeIndex < typeEnum.keyCount(); typeIndex++) { |
| 570 | const auto typeName = typeEnum.key(typeIndex); |
| 571 | const auto typeValue = static_cast<QShaderLanguage::VariableType>(typeEnum.value(typeIndex)); |
| 572 | |
| 573 | auto graph = QShaderGraph(); |
| 574 | |
| 575 | auto worldPosition = createNode({ |
| 576 | createPort(QShaderNodePort::Output, "value") |
| 577 | }); |
| 578 | worldPosition.setParameter("name", "worldPosition"); |
| 579 | worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(qualifierValue)); |
| 580 | worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(typeValue)); |
| 581 | worldPosition.addRule(es2, QShaderNode::Rule("highp $type $value = $name;", |
| 582 | QByteArrayList() << "$qualifier highp $type $name;")); |
| 583 | worldPosition.addRule(gl2, QShaderNode::Rule("$type $value = $name;", |
| 584 | QByteArrayList() << "$qualifier $type $name;")); |
| 585 | worldPosition.addRule(gl3, QShaderNode::Rule("$type $value = $name;", |
| 586 | QByteArrayList() << "$qualifier $type $name;")); |
| 587 | |
| 588 | auto fragColor = createNode({ |
| 589 | createPort(QShaderNodePort::Input, "fragColor") |
| 590 | }); |
| 591 | fragColor.addRule(es2, QShaderNode::Rule("gl_fragColor = $fragColor;")); |
| 592 | fragColor.addRule(gl2, QShaderNode::Rule("gl_fragColor = $fragColor;")); |
| 593 | fragColor.addRule(gl3, QShaderNode::Rule("fragColor = $fragColor;", |
| 594 | QByteArrayList() << "out vec4 fragColor;")); |
| 595 | |
| 596 | graph.addNode(worldPosition); |
| 597 | graph.addNode(fragColor); |
| 598 | |
| 599 | graph.addEdge(createEdge(worldPosition.uuid(), "value", fragColor.uuid(), "fragColor")); |
| 600 | |
| 601 | const auto gl2Code = (QByteArrayList() << "#version 110" |
| 602 | << "" |
| 603 | << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl2)) |
| 604 | .arg(toGlsl(typeValue)) |
| 605 | .toUtf8() |
| 606 | << "" |
| 607 | << "void main()" |
| 608 | << "{" |
| 609 | << " gl_fragColor = worldPosition;" |
| 610 | << "}" |
| 611 | << "").join("\n"); |
| 612 | const auto gl3Code = (QByteArrayList() << "#version 130" |
| 613 | << "" |
| 614 | << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl3)) |
| 615 | .arg(toGlsl(typeValue)) |
| 616 | .toUtf8() |
| 617 | << "out vec4 fragColor;" |
| 618 | << "" |
| 619 | << "void main()" |
| 620 | << "{" |
| 621 | << " fragColor = worldPosition;" |
| 622 | << "}" |
| 623 | << "").join("\n"); |
| 624 | const auto gl4Code = (QByteArrayList() << "#version 400 core" |
| 625 | << "" |
| 626 | << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl4)) |
| 627 | .arg(toGlsl(typeValue)) |
| 628 | .toUtf8() |
| 629 | << "out vec4 fragColor;" |
| 630 | << "" |
| 631 | << "void main()" |
| 632 | << "{" |
| 633 | << " fragColor = worldPosition;" |
| 634 | << "}" |
| 635 | << "").join("\n"); |
| 636 | const auto es2Code = (QByteArrayList() << "#version 100" |
| 637 | << "" |
| 638 | << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es2)) |
| 639 | .arg(toGlsl(typeValue)) |
| 640 | .toUtf8() |
| 641 | << "" |
| 642 | << "void main()" |
| 643 | << "{" |
| 644 | << " gl_fragColor = worldPosition;" |
| 645 | << "}" |
| 646 | << "").join("\n"); |
| 647 | const auto es3Code = (QByteArrayList() << "#version 300 es" |
| 648 | << "" |
| 649 | << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es3)) |
| 650 | .arg(toGlsl(typeValue)) |
| 651 | .toUtf8() |
| 652 | << "" |
| 653 | << "void main()" |
| 654 | << "{" |
| 655 | << " gl_fragColor = worldPosition;" |
| 656 | << "}" |
| 657 | << "").join("\n"); |
| 658 | |
| 659 | QTest::addRow("%s %s ES2", qualifierName, typeName) << graph << es2 << es2Code; |
| 660 | QTest::addRow("%s %s ES3", qualifierName, typeName) << graph << es3 << es3Code; |
| 661 | QTest::addRow("%s %s GL2", qualifierName, typeName) << graph << gl2 << gl2Code; |
| 662 | QTest::addRow("%s %s GL3", qualifierName, typeName) << graph << gl3 << gl3Code; |
| 663 | QTest::addRow("%s %s GL4", qualifierName, typeName) << graph << gl4 << gl4Code; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | { |
| 669 | const auto es2 = createFormat(QShaderFormat::OpenGLES, 2, 0, QShaderFormat::Vertex); |
| 670 | const auto es3 = createFormat(QShaderFormat::OpenGLES, 3, 0, QShaderFormat::Vertex); |
| 671 | const auto gl2 = createFormat(QShaderFormat::OpenGLNoProfile, 2, 0, QShaderFormat::Vertex); |
| 672 | const auto gl3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, QShaderFormat::Vertex); |
| 673 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0, QShaderFormat::Vertex); |
| 674 | |
| 675 | auto graph = QShaderGraph(); |
| 676 | |
| 677 | auto vertexPosition = createNode({ |
| 678 | createPort(QShaderNodePort::Output, "value") |
| 679 | }); |
| 680 | vertexPosition.setParameter("name", "vertexPosition"); |
| 681 | vertexPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Input)); |
| 682 | vertexPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec4)); |
| 683 | |
| 684 | vertexPosition.addRule(es2, QShaderNode::Rule("", |
| 685 | QByteArrayList() << "$qualifier highp $type $name;")); |
| 686 | vertexPosition.addRule(gl2, QShaderNode::Rule("", |
| 687 | QByteArrayList() << "$qualifier $type $name;")); |
| 688 | vertexPosition.addRule(gl3, QShaderNode::Rule("", |
| 689 | QByteArrayList() << "$qualifier $type $name;")); |
| 690 | |
| 691 | graph.addNode(vertexPosition); |
| 692 | |
| 693 | const auto gl2Code = (QByteArrayList() << "#version 110" |
| 694 | << "" |
| 695 | << "" |
| 696 | << "void main()" |
| 697 | << "{" |
| 698 | << "}" |
| 699 | << "").join("\n"); |
| 700 | const auto gl3Code = (QByteArrayList() << "#version 130" |
| 701 | << "" |
| 702 | << "" |
| 703 | << "void main()" |
| 704 | << "{" |
| 705 | << "}" |
| 706 | << "").join("\n"); |
| 707 | const auto gl4Code = (QByteArrayList() << "#version 400 core" |
| 708 | << "" |
| 709 | << "" |
| 710 | << "void main()" |
| 711 | << "{" |
| 712 | << "}" |
| 713 | << "").join("\n"); |
| 714 | const auto es2Code = (QByteArrayList() << "#version 100" |
| 715 | << "" |
| 716 | << "" |
| 717 | << "void main()" |
| 718 | << "{" |
| 719 | << "}" |
| 720 | << "").join("\n"); |
| 721 | const auto es3Code = (QByteArrayList() << "#version 300 es" |
| 722 | << "" |
| 723 | << "" |
| 724 | << "void main()" |
| 725 | << "{" |
| 726 | << "}" |
| 727 | << "").join("\n"); |
| 728 | |
| 729 | QTest::addRow("Attribute header substitution ES2") << graph << es2 << es2Code; |
| 730 | QTest::addRow("Attribute header substitution ES3") << graph << es3 << es3Code; |
| 731 | QTest::addRow("Attribute header substitution GL2") << graph << gl2 << gl2Code; |
| 732 | QTest::addRow("Attribute header substitution GL3") << graph << gl3 << gl3Code; |
| 733 | QTest::addRow("Attribute header substitution GL4") << graph << gl4 << gl4Code; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums() |
| 738 | { |
| 739 | // GIVEN |
| 740 | QFETCH(QShaderGraph, graph); |
| 741 | QFETCH(QShaderFormat, format); |
| 742 | |
| 743 | auto generator = QShaderGenerator(); |
| 744 | generator.graph = graph; |
| 745 | generator.format = format; |
| 746 | |
| 747 | // WHEN |
| 748 | const auto code = generator.createShaderCode(); |
| 749 | |
| 750 | // THEN |
| 751 | QFETCH(QByteArray, expectedCode); |
| 752 | QCOMPARE(code, expectedCode); |
| 753 | } |
| 754 | |
| 755 | void tst_QShaderGenerator::shouldGenerateDifferentCodeDependingOnActiveLayers() |
| 756 | { |
| 757 | // GIVEN |
| 758 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 759 | |
| 760 | auto texCoord = createNode({ |
| 761 | createPort(QShaderNodePort::Output, "texCoord") |
| 762 | }, { |
| 763 | "diffuseTexture", |
| 764 | "normalTexture" |
| 765 | }); |
| 766 | texCoord.addRule(gl4, QShaderNode::Rule("vec2 $texCoord = texCoord;", |
| 767 | QByteArrayList() << "in vec2 texCoord;")); |
| 768 | auto diffuseUniform = createNode({ |
| 769 | createPort(QShaderNodePort::Output, "color") |
| 770 | }, {"diffuseUniform"}); |
| 771 | diffuseUniform.addRule(gl4, QShaderNode::Rule("vec4 $color = diffuseUniform;", |
| 772 | QByteArrayList() << "uniform vec4 diffuseUniform;")); |
| 773 | auto diffuseTexture = createNode({ |
| 774 | createPort(QShaderNodePort::Input, "coord"), |
| 775 | createPort(QShaderNodePort::Output, "color") |
| 776 | }, {"diffuseTexture"}); |
| 777 | diffuseTexture.addRule(gl4, QShaderNode::Rule("vec4 $color = texture2D(diffuseTexture, $coord);", |
| 778 | QByteArrayList() << "uniform sampler2D diffuseTexture;")); |
| 779 | auto normalUniform = createNode({ |
| 780 | createPort(QShaderNodePort::Output, "normal") |
| 781 | }, {"normalUniform"}); |
| 782 | normalUniform.addRule(gl4, QShaderNode::Rule("vec3 $normal = normalUniform;", |
| 783 | QByteArrayList() << "uniform vec3 normalUniform;")); |
| 784 | auto normalTexture = createNode({ |
| 785 | createPort(QShaderNodePort::Input, "coord"), |
| 786 | createPort(QShaderNodePort::Output, "normal") |
| 787 | }, {"normalTexture"}); |
| 788 | normalTexture.addRule(gl4, QShaderNode::Rule("vec3 $normal = texture2D(normalTexture, $coord).rgb;", |
| 789 | QByteArrayList() << "uniform sampler2D normalTexture;")); |
| 790 | auto lightFunction = createNode({ |
| 791 | createPort(QShaderNodePort::Input, "color"), |
| 792 | createPort(QShaderNodePort::Input, "normal"), |
| 793 | createPort(QShaderNodePort::Output, "output") |
| 794 | }); |
| 795 | lightFunction.addRule(gl4, QShaderNode::Rule("vec4 $output = lightModel($color, $normal);", |
| 796 | QByteArrayList() << "#pragma include gl4/lightmodel.frag.inc")); |
| 797 | auto fragColor = createNode({ |
| 798 | createPort(QShaderNodePort::Input, "fragColor") |
| 799 | }); |
| 800 | fragColor.addRule(gl4, QShaderNode::Rule("fragColor = $fragColor;", |
| 801 | QByteArrayList() << "out vec4 fragColor;")); |
| 802 | |
| 803 | const auto graph = [=] { |
| 804 | auto res = QShaderGraph(); |
| 805 | |
| 806 | res.addNode(texCoord); |
| 807 | res.addNode(diffuseUniform); |
| 808 | res.addNode(diffuseTexture); |
| 809 | res.addNode(normalUniform); |
| 810 | res.addNode(normalTexture); |
| 811 | res.addNode(lightFunction); |
| 812 | res.addNode(fragColor); |
| 813 | |
| 814 | res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"})); |
| 815 | res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"})); |
| 816 | res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"})); |
| 817 | |
| 818 | res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"})); |
| 819 | res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"})); |
| 820 | res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"})); |
| 821 | |
| 822 | res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor")); |
| 823 | |
| 824 | return res; |
| 825 | }(); |
| 826 | |
| 827 | auto generator = QShaderGenerator(); |
| 828 | generator.graph = graph; |
| 829 | generator.format = gl4; |
| 830 | |
| 831 | { |
| 832 | // WHEN |
| 833 | const auto code = generator.createShaderCode({"diffuseUniform", "normalUniform"}); |
| 834 | |
| 835 | // THEN |
| 836 | const auto expected = QByteArrayList() |
| 837 | << "#version 400 core" |
| 838 | << "" |
| 839 | << "#define LAYER_diffuseUniform" |
| 840 | << "#define LAYER_normalUniform" |
| 841 | << "uniform vec3 normalUniform;" |
| 842 | << "uniform vec4 diffuseUniform;" |
| 843 | << "#pragma include gl4/lightmodel.frag.inc" |
| 844 | << "out vec4 fragColor;" |
| 845 | << "" |
| 846 | << "void main()" |
| 847 | << "{" |
| 848 | << " fragColor = ((lightModel(diffuseUniform, normalUniform)));" |
| 849 | << "}" |
| 850 | << ""; |
| 851 | QCOMPARE(code, expected.join("\n")); |
| 852 | } |
| 853 | |
| 854 | { |
| 855 | // WHEN |
| 856 | const auto code = generator.createShaderCode({"diffuseUniform", "normalTexture"}); |
| 857 | |
| 858 | // THEN |
| 859 | const auto expected = QByteArrayList() << "#version 400 core" |
| 860 | << "" |
| 861 | << "#define LAYER_diffuseUniform" |
| 862 | << "#define LAYER_normalTexture" |
| 863 | << "in vec2 texCoord;" |
| 864 | << "uniform sampler2D normalTexture;" |
| 865 | << "uniform vec4 diffuseUniform;" |
| 866 | << "#pragma include gl4/lightmodel.frag.inc" |
| 867 | << "out vec4 fragColor;" |
| 868 | << "" |
| 869 | << "void main()" |
| 870 | << "{" |
| 871 | << " fragColor = ((lightModel(diffuseUniform, " |
| 872 | "texture2D(normalTexture, texCoord).rgb)));" |
| 873 | << "}" |
| 874 | << ""; |
| 875 | QCOMPARE(code, expected.join("\n")); |
| 876 | } |
| 877 | |
| 878 | { |
| 879 | // WHEN |
| 880 | const auto code = generator.createShaderCode({"diffuseTexture", "normalUniform"}); |
| 881 | |
| 882 | // THEN |
| 883 | const auto expected = QByteArrayList() |
| 884 | << "#version 400 core" |
| 885 | << "" |
| 886 | << "#define LAYER_diffuseTexture" |
| 887 | << "#define LAYER_normalUniform" |
| 888 | << "in vec2 texCoord;" |
| 889 | << "uniform vec3 normalUniform;" |
| 890 | << "uniform sampler2D diffuseTexture;" |
| 891 | << "#pragma include gl4/lightmodel.frag.inc" |
| 892 | << "out vec4 fragColor;" |
| 893 | << "" |
| 894 | << "void main()" |
| 895 | << "{" |
| 896 | << " fragColor = ((lightModel(texture2D(diffuseTexture, texCoord), " |
| 897 | "normalUniform)));" |
| 898 | << "}" |
| 899 | << ""; |
| 900 | QCOMPARE(code, expected.join("\n")); |
| 901 | } |
| 902 | |
| 903 | { |
| 904 | // WHEN |
| 905 | const auto code = generator.createShaderCode({"diffuseTexture", "normalTexture"}); |
| 906 | |
| 907 | // THEN |
| 908 | const auto expected = QByteArrayList() |
| 909 | << "#version 400 core" |
| 910 | << "" |
| 911 | << "#define LAYER_diffuseTexture" |
| 912 | << "#define LAYER_normalTexture" |
| 913 | << "in vec2 texCoord;" |
| 914 | << "uniform sampler2D normalTexture;" |
| 915 | << "uniform sampler2D diffuseTexture;" |
| 916 | << "#pragma include gl4/lightmodel.frag.inc" |
| 917 | << "out vec4 fragColor;" |
| 918 | << "" |
| 919 | << "void main()" |
| 920 | << "{" |
| 921 | << " fragColor = ((lightModel(texture2D(diffuseTexture, texCoord), " |
| 922 | "texture2D(normalTexture, texCoord).rgb)));" |
| 923 | << "}" |
| 924 | << ""; |
| 925 | QCOMPARE(code, expected.join("\n")); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | void tst_QShaderGenerator::shouldUseGlobalVariableRatherThanTemporaries() |
| 930 | { |
| 931 | // GIVEN |
| 932 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 933 | |
| 934 | { |
| 935 | // WHEN |
| 936 | auto vertexPosition = createNode({ |
| 937 | createPort(QShaderNodePort::Output, "vertexPosition") |
| 938 | }); |
| 939 | vertexPosition.addRule(gl4, QShaderNode::Rule("vec4 $vertexPosition = vertexPosition;", |
| 940 | QByteArrayList() << "in vec4 vertexPosition;")); |
| 941 | |
| 942 | auto fakeMultiPlyNoSpace = createNode({ |
| 943 | createPort(QShaderNodePort::Input, "varName"), |
| 944 | createPort(QShaderNodePort::Output, "out") |
| 945 | }); |
| 946 | fakeMultiPlyNoSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName*speed;")); |
| 947 | |
| 948 | auto fakeMultiPlySpace = createNode({ |
| 949 | createPort(QShaderNodePort::Input, "varName"), |
| 950 | createPort(QShaderNodePort::Output, "out") |
| 951 | }); |
| 952 | fakeMultiPlySpace.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName * speed;")); |
| 953 | |
| 954 | auto fakeJoinNoSpace = createNode({ |
| 955 | createPort(QShaderNodePort::Input, "varName"), |
| 956 | createPort(QShaderNodePort::Output, "out") |
| 957 | }); |
| 958 | fakeJoinNoSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = vec4($varName.xyz,$varName.w);")); |
| 959 | |
| 960 | auto fakeJoinSpace = createNode({ |
| 961 | createPort(QShaderNodePort::Input, "varName"), |
| 962 | createPort(QShaderNodePort::Output, "out") |
| 963 | }); |
| 964 | fakeJoinSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = vec4($varName.xyz, $varName.w);")); |
| 965 | |
| 966 | auto fakeAdd = createNode({ |
| 967 | createPort(QShaderNodePort::Input, "varName"), |
| 968 | createPort(QShaderNodePort::Output, "out") |
| 969 | }); |
| 970 | fakeAdd.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName.xyzw + $varName;")); |
| 971 | |
| 972 | auto fakeSub = createNode({ |
| 973 | createPort(QShaderNodePort::Input, "varName"), |
| 974 | createPort(QShaderNodePort::Output, "out") |
| 975 | }); |
| 976 | fakeSub.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName.xyzw - $varName;")); |
| 977 | |
| 978 | auto fakeDiv = createNode({ |
| 979 | createPort(QShaderNodePort::Input, "varName"), |
| 980 | createPort(QShaderNodePort::Output, "out") |
| 981 | }); |
| 982 | fakeDiv.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName / v0;")); |
| 983 | |
| 984 | auto fragColor = createNode({ |
| 985 | createPort(QShaderNodePort::Input, "input1"), |
| 986 | createPort(QShaderNodePort::Input, "input2"), |
| 987 | createPort(QShaderNodePort::Input, "input3"), |
| 988 | createPort(QShaderNodePort::Input, "input4"), |
| 989 | createPort(QShaderNodePort::Input, "input5"), |
| 990 | createPort(QShaderNodePort::Input, "input6"), |
| 991 | createPort(QShaderNodePort::Input, "input7") |
| 992 | }); |
| 993 | fragColor.addRule(gl4, QShaderNode::Rule("fragColor = $input1 + $input2 + $input3 + $input4 + $input5 + $input6 + $input7;", |
| 994 | QByteArrayList() << "out vec4 fragColor;")); |
| 995 | |
| 996 | const auto graph = [=] { |
| 997 | auto res = QShaderGraph(); |
| 998 | |
| 999 | res.addNode(vertexPosition); |
| 1000 | res.addNode(fakeMultiPlyNoSpace); |
| 1001 | res.addNode(fakeMultiPlySpace); |
| 1002 | res.addNode(fakeJoinNoSpace); |
| 1003 | res.addNode(fakeJoinSpace); |
| 1004 | res.addNode(fakeAdd); |
| 1005 | res.addNode(fakeSub); |
| 1006 | res.addNode(fakeDiv); |
| 1007 | res.addNode(fragColor); |
| 1008 | |
| 1009 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeMultiPlyNoSpace.uuid(), "varName")); |
| 1010 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeMultiPlySpace.uuid(), "varName")); |
| 1011 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeJoinNoSpace.uuid(), "varName")); |
| 1012 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeJoinSpace.uuid(), "varName")); |
| 1013 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeAdd.uuid(), "varName")); |
| 1014 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeSub.uuid(), "varName")); |
| 1015 | res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeDiv.uuid(), "varName")); |
| 1016 | res.addEdge(createEdge(fakeMultiPlyNoSpace.uuid(), "out", fragColor.uuid(), "input1")); |
| 1017 | res.addEdge(createEdge(fakeMultiPlySpace.uuid(), "out", fragColor.uuid(), "input2")); |
| 1018 | res.addEdge(createEdge(fakeJoinNoSpace.uuid(), "out", fragColor.uuid(), "input3")); |
| 1019 | res.addEdge(createEdge(fakeJoinSpace.uuid(), "out", fragColor.uuid(), "input4")); |
| 1020 | res.addEdge(createEdge(fakeAdd.uuid(), "out", fragColor.uuid(), "input5")); |
| 1021 | res.addEdge(createEdge(fakeSub.uuid(), "out", fragColor.uuid(), "input6")); |
| 1022 | res.addEdge(createEdge(fakeDiv.uuid(), "out", fragColor.uuid(), "input7")); |
| 1023 | |
| 1024 | return res; |
| 1025 | }(); |
| 1026 | |
| 1027 | auto generator = QShaderGenerator(); |
| 1028 | generator.graph = graph; |
| 1029 | generator.format = gl4; |
| 1030 | |
| 1031 | const auto code = generator.createShaderCode({"diffuseUniform", "normalUniform"}); |
| 1032 | |
| 1033 | // THEN |
| 1034 | const auto expected = QByteArrayList() |
| 1035 | << "#version 400 core" |
| 1036 | << "" |
| 1037 | << "#define LAYER_diffuseUniform" |
| 1038 | << "#define LAYER_normalUniform" |
| 1039 | << "in vec4 vertexPosition;" |
| 1040 | << "out vec4 fragColor;" |
| 1041 | << "" |
| 1042 | << "void main()" |
| 1043 | << "{" |
| 1044 | << " fragColor = (((((((vertexPosition*speed + vertexPosition * speed + ((vec4(vertexPosition.xyz,vertexPosition.w))) + ((vec4(vertexPosition.xyz, vertexPosition.w))) + ((vertexPosition.xyzw + vertexPosition)) + ((vertexPosition.xyzw - vertexPosition)) + ((vertexPosition / vertexPosition)))))))));" |
| 1045 | << "}" |
| 1046 | << ""; |
| 1047 | QCOMPARE(code, expected.join("\n")); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | void tst_QShaderGenerator::shouldGenerateTemporariesWisely() |
| 1052 | { |
| 1053 | // GIVEN |
| 1054 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 1055 | |
| 1056 | { |
| 1057 | auto attribute = createNode({ |
| 1058 | createPort(QShaderNodePort::Output, "vertexPosition") |
| 1059 | }); |
| 1060 | attribute.addRule(gl4, QShaderNode::Rule("vec4 $vertexPosition = vertexPosition;", |
| 1061 | QByteArrayList() << "in vec4 vertexPosition;")); |
| 1062 | |
| 1063 | auto complexFunction = createNode({ |
| 1064 | createPort(QShaderNodePort::Input, "inputVarName"), |
| 1065 | createPort(QShaderNodePort::Output, "out") |
| 1066 | }); |
| 1067 | complexFunction.addRule(gl4, QShaderNode::Rule("vec4 $out = $inputVarName * 2.0;")); |
| 1068 | |
| 1069 | auto complexFunction2 = createNode({ |
| 1070 | createPort(QShaderNodePort::Input, "inputVarName"), |
| 1071 | createPort(QShaderNodePort::Output, "out") |
| 1072 | }); |
| 1073 | complexFunction2.addRule(gl4, QShaderNode::Rule("vec4 $out = $inputVarName * 4.0;")); |
| 1074 | |
| 1075 | auto complexFunction3 = createNode({ |
| 1076 | createPort(QShaderNodePort::Input, "a"), |
| 1077 | createPort(QShaderNodePort::Input, "b"), |
| 1078 | createPort(QShaderNodePort::Output, "out") |
| 1079 | }); |
| 1080 | complexFunction3.addRule(gl4, QShaderNode::Rule("vec4 $out = $a + $b;")); |
| 1081 | |
| 1082 | auto shaderOutput1 = createNode({ |
| 1083 | createPort(QShaderNodePort::Input, "input") |
| 1084 | }); |
| 1085 | |
| 1086 | shaderOutput1.addRule(gl4, QShaderNode::Rule("shaderOutput1 = $input;", |
| 1087 | QByteArrayList() << "out vec4 shaderOutput1;")); |
| 1088 | |
| 1089 | auto shaderOutput2 = createNode({ |
| 1090 | createPort(QShaderNodePort::Input, "input") |
| 1091 | }); |
| 1092 | |
| 1093 | shaderOutput2.addRule(gl4, QShaderNode::Rule("shaderOutput2 = $input;", |
| 1094 | QByteArrayList() << "out vec4 shaderOutput2;")); |
| 1095 | |
| 1096 | { |
| 1097 | // WHEN |
| 1098 | const auto graph = [=] { |
| 1099 | auto res = QShaderGraph(); |
| 1100 | |
| 1101 | res.addNode(attribute); |
| 1102 | res.addNode(complexFunction); |
| 1103 | res.addNode(shaderOutput1); |
| 1104 | |
| 1105 | res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); |
| 1106 | res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput1.uuid(), "input")); |
| 1107 | |
| 1108 | return res; |
| 1109 | }(); |
| 1110 | |
| 1111 | auto generator = QShaderGenerator(); |
| 1112 | generator.graph = graph; |
| 1113 | generator.format = gl4; |
| 1114 | |
| 1115 | const auto code = generator.createShaderCode(); |
| 1116 | |
| 1117 | // THEN |
| 1118 | const auto expected = QByteArrayList() |
| 1119 | << "#version 400 core" |
| 1120 | << "" |
| 1121 | << "in vec4 vertexPosition;" |
| 1122 | << "out vec4 shaderOutput1;" |
| 1123 | << "" |
| 1124 | << "void main()" |
| 1125 | << "{" |
| 1126 | << " shaderOutput1 = vertexPosition * 2.0;" |
| 1127 | << "}" |
| 1128 | << ""; |
| 1129 | QCOMPARE(code, expected.join("\n")); |
| 1130 | } |
| 1131 | |
| 1132 | { |
| 1133 | // WHEN |
| 1134 | const auto graph = [=] { |
| 1135 | auto res = QShaderGraph(); |
| 1136 | |
| 1137 | res.addNode(attribute); |
| 1138 | res.addNode(complexFunction); |
| 1139 | res.addNode(shaderOutput1); |
| 1140 | res.addNode(shaderOutput2); |
| 1141 | |
| 1142 | res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); |
| 1143 | res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput1.uuid(), "input")); |
| 1144 | res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput2.uuid(), "input")); |
| 1145 | |
| 1146 | return res; |
| 1147 | }(); |
| 1148 | |
| 1149 | auto generator = QShaderGenerator(); |
| 1150 | generator.graph = graph; |
| 1151 | generator.format = gl4; |
| 1152 | |
| 1153 | const auto code = generator.createShaderCode(); |
| 1154 | |
| 1155 | // THEN |
| 1156 | const auto expected = QByteArrayList() << "#version 400 core" |
| 1157 | << "" |
| 1158 | << "in vec4 vertexPosition;" |
| 1159 | << "out vec4 shaderOutput2;" |
| 1160 | << "out vec4 shaderOutput1;" |
| 1161 | << "" |
| 1162 | << "void main()" |
| 1163 | << "{" |
| 1164 | << " vec4 v1 = vertexPosition * 2.0;" |
| 1165 | << " shaderOutput2 = v1;" |
| 1166 | << " shaderOutput1 = v1;" |
| 1167 | << "}" |
| 1168 | << ""; |
| 1169 | QCOMPARE(code, expected.join("\n")); |
| 1170 | } |
| 1171 | |
| 1172 | { |
| 1173 | // WHEN |
| 1174 | const auto graph = [=] { |
| 1175 | auto res = QShaderGraph(); |
| 1176 | |
| 1177 | res.addNode(attribute); |
| 1178 | res.addNode(complexFunction); |
| 1179 | res.addNode(complexFunction2); |
| 1180 | res.addNode(complexFunction3); |
| 1181 | res.addNode(shaderOutput1); |
| 1182 | res.addNode(shaderOutput2); |
| 1183 | |
| 1184 | res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); |
| 1185 | res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction2.uuid(), "inputVarName")); |
| 1186 | |
| 1187 | res.addEdge(createEdge(complexFunction.uuid(), "out", complexFunction3.uuid(), "a")); |
| 1188 | res.addEdge(createEdge(complexFunction2.uuid(), "out", complexFunction3.uuid(), "b")); |
| 1189 | |
| 1190 | res.addEdge(createEdge(complexFunction3.uuid(), "out", shaderOutput1.uuid(), "input")); |
| 1191 | res.addEdge(createEdge(complexFunction2.uuid(), "out", shaderOutput2.uuid(), "input")); |
| 1192 | |
| 1193 | return res; |
| 1194 | }(); |
| 1195 | |
| 1196 | auto generator = QShaderGenerator(); |
| 1197 | generator.graph = graph; |
| 1198 | generator.format = gl4; |
| 1199 | |
| 1200 | const auto code = generator.createShaderCode(); |
| 1201 | |
| 1202 | // THEN |
| 1203 | const auto expected = QByteArrayList() |
| 1204 | << "#version 400 core" |
| 1205 | << "" |
| 1206 | << "in vec4 vertexPosition;" |
| 1207 | << "out vec4 shaderOutput2;" |
| 1208 | << "out vec4 shaderOutput1;" |
| 1209 | << "" |
| 1210 | << "void main()" |
| 1211 | << "{" |
| 1212 | << " vec4 v2 = vertexPosition * 4.0;" |
| 1213 | << " shaderOutput2 = v2;" |
| 1214 | << " shaderOutput1 = (vertexPosition * 2.0 + v2);" |
| 1215 | << "}" |
| 1216 | << ""; |
| 1217 | QCOMPARE(code, expected.join("\n")); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | void tst_QShaderGenerator::shouldHandlePortNamesPrefixingOneAnother() |
| 1223 | { |
| 1224 | // GIVEN |
| 1225 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 1226 | |
| 1227 | auto color1 = createNode({ |
| 1228 | createPort(QShaderNodePort::Output, "output") |
| 1229 | }); |
| 1230 | color1.addRule(gl4, QShaderNode::Rule("vec4 $output = color1;", |
| 1231 | QByteArrayList() << "in vec4 color1;")); |
| 1232 | |
| 1233 | auto color2 = createNode({ |
| 1234 | createPort(QShaderNodePort::Output, "output") |
| 1235 | }); |
| 1236 | color2.addRule(gl4, QShaderNode::Rule("vec4 $output = color2;", |
| 1237 | QByteArrayList() << "in vec4 color2;")); |
| 1238 | |
| 1239 | auto addColor = createNode({ |
| 1240 | createPort(QShaderNodePort::Output, "color"), |
| 1241 | createPort(QShaderNodePort::Input, "color1"), |
| 1242 | createPort(QShaderNodePort::Input, "color2"), |
| 1243 | }); |
| 1244 | addColor.addRule(gl4, QShaderNode::Rule("vec4 $color = $color1 + $color2;")); |
| 1245 | |
| 1246 | auto shaderOutput = createNode({ |
| 1247 | createPort(QShaderNodePort::Input, "input") |
| 1248 | }); |
| 1249 | |
| 1250 | shaderOutput.addRule(gl4, QShaderNode::Rule("shaderOutput = $input;", |
| 1251 | QByteArrayList() << "out vec4 shaderOutput;")); |
| 1252 | |
| 1253 | // WHEN |
| 1254 | const auto graph = [=] { |
| 1255 | auto res = QShaderGraph(); |
| 1256 | |
| 1257 | res.addNode(color1); |
| 1258 | res.addNode(color2); |
| 1259 | res.addNode(addColor); |
| 1260 | res.addNode(shaderOutput); |
| 1261 | |
| 1262 | res.addEdge(createEdge(color1.uuid(), "output", addColor.uuid(), "color1")); |
| 1263 | res.addEdge(createEdge(color2.uuid(), "output", addColor.uuid(), "color2")); |
| 1264 | res.addEdge(createEdge(addColor.uuid(), "color", shaderOutput.uuid(), "input")); |
| 1265 | |
| 1266 | return res; |
| 1267 | }(); |
| 1268 | |
| 1269 | auto generator = QShaderGenerator(); |
| 1270 | generator.graph = graph; |
| 1271 | generator.format = gl4; |
| 1272 | |
| 1273 | const auto code = generator.createShaderCode(); |
| 1274 | |
| 1275 | // THEN |
| 1276 | const auto expected = QByteArrayList() << "#version 400 core" |
| 1277 | << "" |
| 1278 | << "in vec4 color2;" |
| 1279 | << "in vec4 color1;" |
| 1280 | << "out vec4 shaderOutput;" |
| 1281 | << "" |
| 1282 | << "void main()" |
| 1283 | << "{" |
| 1284 | << " shaderOutput = ((color1 + color2));" |
| 1285 | << "}" |
| 1286 | << ""; |
| 1287 | QCOMPARE(code, expected.join("\n")); |
| 1288 | } |
| 1289 | |
| 1290 | void tst_QShaderGenerator::shouldHandleNodesWithMultipleOutputPorts() |
| 1291 | { |
| 1292 | // GIVEN |
| 1293 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 1294 | |
| 1295 | auto input = createNode({ |
| 1296 | createPort(QShaderNodePort::Output, "output0"), |
| 1297 | createPort(QShaderNodePort::Output, "output1") |
| 1298 | }); |
| 1299 | input.addRule(gl4, QShaderNode::Rule("vec4 $output0 = globalIn0;" |
| 1300 | "float $output1 = globalIn1;", |
| 1301 | QByteArrayList() << "in vec4 globalIn0;" << "in float globalIn1;")); |
| 1302 | |
| 1303 | auto function = createNode({ |
| 1304 | createPort(QShaderNodePort::Input, "input0"), |
| 1305 | createPort(QShaderNodePort::Input, "input1"), |
| 1306 | createPort(QShaderNodePort::Output, "output0"), |
| 1307 | createPort(QShaderNodePort::Output, "output1") |
| 1308 | }); |
| 1309 | function.addRule(gl4, QShaderNode::Rule("vec4 $output0 = $input0;" |
| 1310 | "float $output1 = $input1;")); |
| 1311 | |
| 1312 | auto output = createNode({ |
| 1313 | createPort(QShaderNodePort::Input, "input0"), |
| 1314 | createPort(QShaderNodePort::Input, "input1") |
| 1315 | }); |
| 1316 | |
| 1317 | output.addRule(gl4, QShaderNode::Rule("globalOut0 = $input0;" |
| 1318 | "globalOut1 = $input1;", |
| 1319 | QByteArrayList() << "out vec4 globalOut0;" << "out float globalOut1;")); |
| 1320 | |
| 1321 | // WHEN |
| 1322 | const auto graph = [=] { |
| 1323 | auto res = QShaderGraph(); |
| 1324 | |
| 1325 | res.addNode(input); |
| 1326 | res.addNode(function); |
| 1327 | res.addNode(output); |
| 1328 | |
| 1329 | res.addEdge(createEdge(input.uuid(), "output0", function.uuid(), "input0")); |
| 1330 | res.addEdge(createEdge(input.uuid(), "output1", function.uuid(), "input1")); |
| 1331 | |
| 1332 | res.addEdge(createEdge(function.uuid(), "output0", output.uuid(), "input0")); |
| 1333 | res.addEdge(createEdge(function.uuid(), "output1", output.uuid(), "input1")); |
| 1334 | |
| 1335 | return res; |
| 1336 | }(); |
| 1337 | |
| 1338 | auto generator = QShaderGenerator(); |
| 1339 | generator.graph = graph; |
| 1340 | generator.format = gl4; |
| 1341 | |
| 1342 | const auto code = generator.createShaderCode(); |
| 1343 | |
| 1344 | // THEN |
| 1345 | const auto expected = QByteArrayList() |
| 1346 | << "#version 400 core" |
| 1347 | << "" |
| 1348 | << "in vec4 globalIn0;" |
| 1349 | << "in float globalIn1;" |
| 1350 | << "out vec4 globalOut0;" |
| 1351 | << "out float globalOut1;" |
| 1352 | << "" |
| 1353 | << "void main()" |
| 1354 | << "{" |
| 1355 | << " globalOut0 = globalIn0;" |
| 1356 | << " globalOut1 = globalIn1;" |
| 1357 | << "}" |
| 1358 | << ""; |
| 1359 | QCOMPARE(code, expected.join("\n")); |
| 1360 | } |
| 1361 | |
| 1362 | void tst_QShaderGenerator::shouldHandleExpressionsInInputNodes() |
| 1363 | { |
| 1364 | // GIVEN |
| 1365 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 1366 | |
| 1367 | auto input = createNode({ |
| 1368 | createPort(QShaderNodePort::Output, "output") |
| 1369 | }); |
| 1370 | input.addRule(gl4, QShaderNode::Rule("float $output = 3 + 4;")); |
| 1371 | |
| 1372 | auto output = createNode({ |
| 1373 | createPort(QShaderNodePort::Input, "input") |
| 1374 | }); |
| 1375 | |
| 1376 | output.addRule(gl4, QShaderNode::Rule("globalOut = $input;", |
| 1377 | QByteArrayList() << "out float globalOut;")); |
| 1378 | |
| 1379 | // WHEN |
| 1380 | const auto graph = [=] { |
| 1381 | auto res = QShaderGraph(); |
| 1382 | |
| 1383 | res.addNode(input); |
| 1384 | res.addNode(output); |
| 1385 | |
| 1386 | res.addEdge(createEdge(input.uuid(), "output", output.uuid(), "input")); |
| 1387 | |
| 1388 | return res; |
| 1389 | }(); |
| 1390 | |
| 1391 | auto generator = QShaderGenerator(); |
| 1392 | generator.graph = graph; |
| 1393 | generator.format = gl4; |
| 1394 | |
| 1395 | const auto code = generator.createShaderCode(); |
| 1396 | |
| 1397 | // THEN |
| 1398 | const auto expected = QByteArrayList() |
| 1399 | << "#version 400 core" |
| 1400 | << "" |
| 1401 | << "out float globalOut;" |
| 1402 | << "" |
| 1403 | << "void main()" |
| 1404 | << "{" |
| 1405 | << " globalOut = 3 + 4;" |
| 1406 | << "}" |
| 1407 | << ""; |
| 1408 | QCOMPARE(code, expected.join("\n")); |
| 1409 | } |
| 1410 | |
| 1411 | void tst_QShaderGenerator::shouldGenerateLayerDefines() |
| 1412 | { |
| 1413 | // GIVEN |
| 1414 | const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); |
| 1415 | |
| 1416 | auto generator = QShaderGenerator(); |
| 1417 | generator.format = gl4; |
| 1418 | |
| 1419 | // WHEN |
| 1420 | const auto code = generator.createShaderCode({"layer1", "layer2"}); |
| 1421 | |
| 1422 | // THEN |
| 1423 | const auto expected = QByteArrayList() |
| 1424 | << "#version 400 core" |
| 1425 | << "" |
| 1426 | << "#define LAYER_layer1" |
| 1427 | << "#define LAYER_layer2" |
| 1428 | << "" |
| 1429 | << "void main()" |
| 1430 | << "{" |
| 1431 | << "}" |
| 1432 | << ""; |
| 1433 | QCOMPARE(code, expected.join("\n")); |
| 1434 | } |
| 1435 | |
| 1436 | QTEST_MAIN(tst_QShaderGenerator) |
| 1437 | |
| 1438 | #include "tst_qshadergenerator.moc" |