diff options
Diffstat (limited to 'tests')
38 files changed, 5220 insertions, 87 deletions
diff --git a/tests/auto/animation/animationutils/tst_animationutils.cpp b/tests/auto/animation/animationutils/tst_animationutils.cpp index 8c340f56e..9692fb339 100644 --- a/tests/auto/animation/animationutils/tst_animationutils.cpp +++ b/tests/auto/animation/animationutils/tst_animationutils.cpp @@ -2059,6 +2059,114 @@ private Q_SLOTS: QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 1") << handler << clip << animatorData << clipData; } + + { + handler = new Handler(); + clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); + const qint64 globalStartTimeNS = clip->duration(); + const int loops = 1; + auto animator = createClipAnimator(handler, globalStartTimeNS, loops); + animator->setCurrentLoop(1); + clipData.currentLoop = animator->currentLoop(); + const qint64 elapsedTimeNS = toNsecs(clip->duration() * 0.5); // +1 to ensure beyond end of clip + + Clock clock; + clock.setPlaybackRate(-1.0); + + animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere + + clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime, + animatorData.elapsedTime, + animatorData.playbackRate, + clip->duration(), + animatorData.loopCount, + clipData.currentLoop); // Tested elsewhere + clipData.isFinalFrame = false; + + QTest::newRow("clip1.json, elapsedTime = duration / 2, loops = 1, current_loop = 1, playback_rate = -1") + << handler << clip << animatorData << clipData; + } + + { + handler = new Handler(); + clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); + const qint64 globalStartTimeNS = clip->duration(); + const int loops = 1; + auto animator = createClipAnimator(handler, globalStartTimeNS, loops); + animator->setCurrentLoop(1); + clipData.currentLoop = animator->currentLoop(); + const qint64 elapsedTimeNS = toNsecs(clip->duration() + 1); // +1 to ensure beyond end of clip + + Clock clock; + clock.setPlaybackRate(-1.0); + + animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere + + clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime, + animatorData.elapsedTime, + animatorData.playbackRate, + clip->duration(), + animatorData.loopCount, + clipData.currentLoop); // Tested elsewhere + clipData.isFinalFrame = true; + + QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 1, current_loop = 1, playback_rate = -1") + << handler << clip << animatorData << clipData; + } + + { + handler = new Handler(); + clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); + const qint64 globalStartTimeNS = clip->duration(); + const int loops = 2; + auto animator = createClipAnimator(handler, globalStartTimeNS, loops); + animator->setCurrentLoop(0); + clipData.currentLoop = animator->currentLoop(); + const qint64 elapsedTimeNS = toNsecs(clip->duration() + 1); // +1 to ensure beyond end of clip + + Clock clock; + clock.setPlaybackRate(-1.0); + + animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere + + clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime, + animatorData.elapsedTime, + animatorData.playbackRate, + clip->duration(), + animatorData.loopCount, + clipData.currentLoop); // Tested elsewhere + clipData.isFinalFrame = true; + + QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 0, playback_rate = -1") + << handler << clip << animatorData << clipData; + } + + { + handler = new Handler(); + clip = createAnimationClipLoader(handler, QUrl("qrc:/clip1.json")); + const qint64 globalStartTimeNS = clip->duration(); + const int loops = 2; + auto animator = createClipAnimator(handler, globalStartTimeNS, loops); + animator->setCurrentLoop(1); + clipData.currentLoop = animator->currentLoop(); + const qint64 elapsedTimeNS = toNsecs(clip->duration() * 2.0 + 1); // +1 to ensure beyond end of clip + + Clock clock; + clock.setPlaybackRate(-1.0); + + animatorData = evaluationDataForAnimator(animator, &clock, elapsedTimeNS); // Tested elsewhere + + clipData.localTime = localTimeFromElapsedTime(animatorData.currentTime, + animatorData.elapsedTime, + animatorData.playbackRate, + clip->duration(), + animatorData.loopCount, + clipData.currentLoop); // Tested elsewhere + clipData.isFinalFrame = true; + + QTest::newRow("clip1.json, elapsedTime = duration + 1, loops = 2, current_loop = 1, playback_rate = -1") + << handler << clip << animatorData << clipData; + } } void checkEvaluationDataForClip() diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 5c1060a7c..6f46eff4b 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -14,5 +14,9 @@ qtConfig(qt3d-animation): SUBDIRS += animation qtConfig(qt3d-extras): SUBDIRS += extras qtConfig(qt3d-render) { SUBDIRS += geometryloaders - qtConfig(qt3d-input): SUBDIRS += quick3d + qtConfig(qt3d-input) { + qtHaveModule(quick) { + SUBDIRS += quick3d + } + } } diff --git a/tests/auto/extras/extras.pro b/tests/auto/extras/extras.pro index e5ae28e97..a9fd78369 100644 --- a/tests/auto/extras/extras.pro +++ b/tests/auto/extras/extras.pro @@ -6,6 +6,10 @@ contains(QT_CONFIG, private_tests) { qtorusgeometry \ qforwardrenderer \ qfirstpersoncameracontroller \ - qorbitcameracontroller \ + qorbitcameracontroller +} + +qtHaveModule(quick) { + SUBDIRS += \ qtext2dentity } diff --git a/tests/auto/render/commons/testrenderer.h b/tests/auto/render/commons/testrenderer.h index 8d5d87a98..87c452ad8 100644 --- a/tests/auto/render/commons/testrenderer.h +++ b/tests/auto/render/commons/testrenderer.h @@ -41,7 +41,7 @@ public: ~TestRenderer(); void dumpInfo() const override {} - API api() const override { return AbstractRenderer::OpenGL; } + Qt3DRender::API api() const override { return Qt3DRender::API::OpenGL; } qint64 time() const override { return 0; } void setTime(qint64 time) override { Q_UNUSED(time) } void setAspect(Qt3DRender::QRenderAspect *aspect) override { m_aspect = aspect; } @@ -85,6 +85,7 @@ public: void resetDirty(); QVariant executeCommand(const QStringList &args) override; QOpenGLContext *shareContext() const override; + const Qt3DRender::GraphicsApiFilterData *contextInfo() const override { return nullptr; } void setOffscreenSurfaceHelper(Qt3DRender::Render::OffscreenSurfaceHelper *helper) override; QSurfaceFormat format() override; diff --git a/tests/auto/render/opengl/opengl.pro b/tests/auto/render/opengl/opengl.pro index 5299ebd36..fead9b5ff 100644 --- a/tests/auto/render/opengl/opengl.pro +++ b/tests/auto/render/opengl/opengl.pro @@ -6,7 +6,6 @@ SUBDIRS += \ graphicshelpergl3_2 \ graphicshelpergl2 \ glshadermanager \ - materialparametergathererjob \ textures \ renderer \ renderviewutils \ @@ -16,4 +15,8 @@ SUBDIRS += \ qgraphicsutils \ computecommand +qtHaveModule(quick) { + SUBDIRS += \ + materialparametergathererjob +} !macos: SUBDIRS += graphicshelpergl4 diff --git a/tests/auto/render/opengl/renderer/tst_renderer.cpp b/tests/auto/render/opengl/renderer/tst_renderer.cpp index 85582d178..93a788921 100644 --- a/tests/auto/render/opengl/renderer/tst_renderer.cpp +++ b/tests/auto/render/opengl/renderer/tst_renderer.cpp @@ -37,6 +37,7 @@ #include <Qt3DRender/private/viewportnode_p.h> #include <Qt3DRender/private/offscreensurfacehelper_p.h> #include <Qt3DRender/private/qrenderaspect_p.h> +#include <Qt3DRender/qmaterial.h> #include "testaspect.h" @@ -114,6 +115,10 @@ private Q_SLOTS: Qt3DRender::Render::ViewportNode *fgRoot = new Qt3DRender::Render::ViewportNode(); const Qt3DCore::QNodeId fgRootId = Qt3DCore::QNodeId::createId(); + // Create fake material so that we crean materialGathererJobs + const Qt3DCore::QNodeId materialId = Qt3DCore::QNodeId::createId(); + nodeManagers.materialManager()->getOrCreateResource(materialId); + nodeManagers.frameGraphManager()->appendNode(fgRootId, fgRoot); settings.setActiveFrameGraphId(fgRootId); @@ -128,16 +133,16 @@ private Q_SLOTS: // NOTE: FilterCompatibleTechniqueJob and ShaderGathererJob cannot run because the context // is not initialized in this test - const int renderViewBuilderMaterialCacheJobCount = 1 + Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount(); + const int renderViewBuilderMaterialCacheJobCount = 1 + 1; // syncMaterialGathererJob - // n * materialGathererJob + // n * materialGathererJob (where n depends on the numbers of available threads and the number of materials) const int layerCacheJobCount = 2; // filterEntityByLayerJob, // syncFilterEntityByLayerJob - const int singleRenderViewCommandRebuildJobCount = 1 + Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount(); + const int singleRenderViewCommandRebuildJobCount = 1 + Qt3DRender::Render::OpenGL::RenderViewBuilder::defaultJobCount(); - const int singleRenderViewJobCount = 8 + 1 * Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount(); + const int singleRenderViewJobCount = 8 + 1 * Qt3DRender::Render::OpenGL::RenderViewBuilder::defaultJobCount(); // RenderViewBuilder renderViewJob, // syncRenderViewInitializationJob, // syncFrustumCullingJob, diff --git a/tests/auto/render/opengl/renderviewbuilder/tst_renderviewbuilder.cpp b/tests/auto/render/opengl/renderviewbuilder/tst_renderviewbuilder.cpp index 19ab51c8c..f092795e3 100644 --- a/tests/auto/render/opengl/renderviewbuilder/tst_renderviewbuilder.cpp +++ b/tests/auto/render/opengl/renderviewbuilder/tst_renderviewbuilder.cpp @@ -248,14 +248,15 @@ private Q_SLOTS: QVERIFY(renderViewBuilder.filterEntityByLayerJob().isNull()); QVERIFY(renderViewBuilder.syncFilterEntityByLayerJob().isNull()); - QCOMPARE(renderViewBuilder.renderViewCommandUpdaterJobs().size(), Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount()); + QCOMPARE(renderViewBuilder.renderViewCommandUpdaterJobs().size(), Qt3DRender::Render::OpenGL::RenderViewBuilder::defaultJobCount()); QCOMPARE(renderViewBuilder.materialGathererJobs().size(), 0); - QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 8 + 1 * Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount()); + QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 8 + 1 * Qt3DRender::Render::OpenGL::RenderViewBuilder::defaultJobCount()); } { // WHEN Qt3DRender::Render::OpenGL::RenderViewBuilder renderViewBuilder(leafNode, 0, testAspect.renderer()); + renderViewBuilder.setOptimalJobCount(2); renderViewBuilder.setLayerCacheNeedsToBeRebuilt(true); renderViewBuilder.prepareJobs(); @@ -265,22 +266,25 @@ private Q_SLOTS: QVERIFY(!renderViewBuilder.syncFilterEntityByLayerJob().isNull()); // mark jobs dirty and recheck - QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 10 + 1 * Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount()); + QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 10 + renderViewBuilder.optimalJobCount()); } { // WHEN Qt3DRender::Render::OpenGL::RenderViewBuilder renderViewBuilder(leafNode, 0, testAspect.renderer()); + renderViewBuilder.setOptimalJobCount(2); renderViewBuilder.setMaterialGathererCacheNeedsToBeRebuilt(true); renderViewBuilder.prepareJobs(); // THEN QCOMPARE(renderViewBuilder.materialGathererCacheNeedsToBeRebuilt(), true); - QCOMPARE(renderViewBuilder.materialGathererJobs().size(), Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount()); + // We have elementsPerJob = qMax(materialHandles.size() / m_optimalParallelJobCount, 1) + // Given we have 2 materials -> 1 element per job -> so we need 2 jobs + QCOMPARE(renderViewBuilder.materialGathererJobs().size(), 2); QVERIFY(!renderViewBuilder.syncMaterialGathererJob().isNull()); // mark jobs dirty and recheck - QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 9 + 2 * Qt3DRender::Render::OpenGL::RenderViewBuilder::optimalJobCount()); + QCOMPARE(renderViewBuilder.buildJobHierachy().size(), 13); } } diff --git a/tests/auto/render/render.pro b/tests/auto/render/render.pro index c03f93d6c..e772e3ddd 100644 --- a/tests/auto/render/render.pro +++ b/tests/auto/render/render.pro @@ -97,7 +97,6 @@ qtConfig(private_tests) { qraycaster \ raycaster \ qscreenraycaster \ - raycastingjob \ qcamera \ qsetfence \ qwaitfence \ @@ -105,7 +104,8 @@ qtConfig(private_tests) { waitfence \ qtexturedataupdate \ qshaderimage \ - shaderimage + shaderimage \ + shadergraph QT_FOR_CONFIG = 3dcore-private # TO DO: These could be restored to be executed in all cases @@ -116,6 +116,11 @@ qtConfig(private_tests) { raycasting \ triangleboundingvolume \ } + + qtHaveModule(quick) { + SUBDIRS += \ + raycastingjob + } } # Tests related to the OpenGL renderer @@ -124,24 +129,31 @@ QT_FOR_CONFIG += 3drender-private qtConfig(qt3d-opengl-renderer):qtConfig(private_tests) { SUBDIRS += \ - opengl \ - scene2d + opengl qtConfig(qt3d-extras) { + qtHaveModule(quick) { + SUBDIRS += \ + boundingsphere \ + pickboundingvolumejob \ + updateshaderdatatransformjob + } + SUBDIRS += \ qmaterial \ geometryloaders \ picking \ - boundingsphere \ pickboundingvolumejob \ gltfplugins \ updateshaderdatatransformjob } qtConfig(qt3d-input) { - SUBDIRS += \ - qscene2d \ - scene2d + qtHaveModule(quick) { + SUBDIRS += \ + qscene2d \ + scene2d + } } qtConfig(qt3d-simd-avx2): SUBDIRS += alignedresourcesmanagers-avx diff --git a/tests/auto/render/shadergraph/qshadergenerator/qshadergenerator.pro b/tests/auto/render/shadergraph/qshadergenerator/qshadergenerator.pro new file mode 100644 index 000000000..edd0be9eb --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergenerator/qshadergenerator.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib 3drender-private + +SOURCES += tst_qshadergenerator.cpp +TARGET = tst_qshadergenerator diff --git a/tests/auto/render/shadergraph/qshadergenerator/tst_qshadergenerator.cpp b/tests/auto/render/shadergraph/qshadergenerator/tst_qshadergenerator.cpp new file mode 100644 index 000000000..90906f4e9 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergenerator/tst_qshadergenerator.cpp @@ -0,0 +1,1428 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qmetaobject.h> +#include <Qt3DRender/private/qshadergenerator_p.h> +#include <Qt3DRender/private/qshaderlanguage_p.h> + +using namespace Qt3DRender; +namespace +{ + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, + QShaderFormat::ShaderType shaderType= QShaderFormat::Fragment) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + format.setShaderType(shaderType); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph createFragmentShaderGraph() + { + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", + QByteArrayList() << "varying highp vec3 $name;")); + worldPosition.addRule(openGL3, QShaderNode::Rule("vec3 $value = $name;", + QByteArrayList() << "in vec3 $name;")); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + + auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "lightIntensity") + }); + lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp float $lightIntensity = lightIntensity;", + QByteArrayList() << "uniform highp float lightIntensity;")); + lightIntensity.addRule(openGL3, QShaderNode::Rule("float $lightIntensity = lightIntensity;", + QByteArrayList() << "uniform float lightIntensity;")); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture($sampler, $coord);")); + + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + + graph.addNode(worldPosition); + graph.addNode(texture); + graph.addNode(texCoord); + graph.addNode(lightIntensity); + graph.addNode(exposure); + graph.addNode(fragColor); + graph.addNode(sampleTexture); + graph.addNode(lightFunction); + graph.addNode(exposureFunction); + + graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); + graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + graph.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity")); + + graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return graph; + } +} + +class tst_QShaderGenerator : public QObject +{ + Q_OBJECT +private slots: + void shouldHaveDefaultState(); + void shouldGenerateShaderCode_data(); + void shouldGenerateShaderCode(); + void shouldGenerateVersionCommands_data(); + void shouldGenerateVersionCommands(); + void shouldProcessLanguageQualifierAndTypeEnums_data(); + void shouldProcessLanguageQualifierAndTypeEnums(); + void shouldGenerateDifferentCodeDependingOnActiveLayers(); + void shouldUseGlobalVariableRatherThanTemporaries(); + void shouldGenerateTemporariesWisely(); + void shouldHandlePortNamesPrefixingOneAnother(); + void shouldHandleNodesWithMultipleOutputPorts(); + void shouldHandleExpressionsInInputNodes(); +}; + +void tst_QShaderGenerator::shouldHaveDefaultState() +{ + // GIVEN + auto generator = QShaderGenerator(); + + // THEN + QVERIFY(generator.graph.nodes().isEmpty()); + QVERIFY(generator.graph.edges().isEmpty()); + QVERIFY(!generator.format.isValid()); +} + +void tst_QShaderGenerator::shouldGenerateShaderCode_data() +{ + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("expectedCode"); + + const auto graph = createFragmentShaderGraph(); + + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto openGL32 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2); + const auto openGL4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto versionGLES2 = QByteArrayList() << "#version 100" << ""; + const auto versionGL3 = QByteArrayList() << "#version 130" << ""; + const auto versionGL32 = QByteArrayList() << "#version 150 core" << ""; + const auto versionGL4 = QByteArrayList() << "#version 400 core" << ""; + + const auto es2Code = QByteArrayList() << "varying highp vec3 worldPosition;" + << "uniform sampler2D texture;" + << "varying highp vec2 texCoord;" + << "uniform highp float lightIntensity;" + << "uniform highp float exposure;" + << "#pragma include es2/lightmodel.frag.inc" + << "" + << "void main()" + << "{" + << " gl_fragColor = (((((lightModel(((texture2D(texture, texCoord))), worldPosition, lightIntensity)))) * pow(2.0, exposure)));" + << "}" + << ""; + + const auto gl3Code = QByteArrayList() << "in vec3 worldPosition;" + << "uniform sampler2D texture;" + << "in vec2 texCoord;" + << "uniform float lightIntensity;" + << "uniform float exposure;" + << "out vec4 fragColor;" + << "#pragma include gl3/lightmodel.frag.inc" + << "" + << "void main()" + << "{" + << " fragColor = (((((lightModel(((texture(texture, texCoord))), worldPosition, lightIntensity)))) * pow(2.0, exposure)));" + << "}" + << ""; + + QTest::newRow("EmptyGraphAndFormat") << QShaderGraph() << QShaderFormat() << QByteArrayLiteral("\n\n\nvoid main()\n{\n}\n"); + QTest::newRow("LightExposureGraphAndES2") << graph << openGLES2 << (versionGLES2 + es2Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL3") << graph << openGL3 << (versionGL3 + gl3Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL32") << graph << openGL32 << (versionGL32 + gl3Code).join('\n'); + QTest::newRow("LightExposureGraphAndGL4") << graph << openGL4 << (versionGL4 + gl3Code).join('\n'); +} + +void tst_QShaderGenerator::shouldGenerateShaderCode() +{ + // GIVEN + QFETCH(QShaderGraph, graph); + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, expectedCode); + QCOMPARE(code, expectedCode); +} + +void tst_QShaderGenerator::shouldGenerateVersionCommands_data() +{ + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("version"); + + QTest::newRow("GLES2") << createFormat(QShaderFormat::OpenGLES, 2, 0) << QByteArrayLiteral("#version 100"); + QTest::newRow("GLES3") << createFormat(QShaderFormat::OpenGLES, 3, 0) << QByteArrayLiteral("#version 300 es"); + + QTest::newRow("GL20") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 2) << QByteArrayLiteral("#version 150"); + QTest::newRow("GL33") << createFormat(QShaderFormat::OpenGLNoProfile, 3, 3) << QByteArrayLiteral("#version 330"); + QTest::newRow("GL40") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 0) << QByteArrayLiteral("#version 400"); + QTest::newRow("GL41") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 1) << QByteArrayLiteral("#version 410"); + QTest::newRow("GL42") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 2) << QByteArrayLiteral("#version 420"); + QTest::newRow("GL43") << createFormat(QShaderFormat::OpenGLNoProfile, 4, 3) << QByteArrayLiteral("#version 430"); + + QTest::newRow("GL20core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21core") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2) << QByteArrayLiteral("#version 150 core"); + QTest::newRow("GL33core") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3) << QByteArrayLiteral("#version 330 core"); + QTest::newRow("GL40core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0) << QByteArrayLiteral("#version 400 core"); + QTest::newRow("GL41core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 1) << QByteArrayLiteral("#version 410 core"); + QTest::newRow("GL42core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 2) << QByteArrayLiteral("#version 420 core"); + QTest::newRow("GL43core") << createFormat(QShaderFormat::OpenGLCoreProfile, 4, 3) << QByteArrayLiteral("#version 430 core"); + + QTest::newRow("GL20compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) << QByteArrayLiteral("#version 110"); + QTest::newRow("GL21compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1) << QByteArrayLiteral("#version 120"); + QTest::newRow("GL30compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 0) << QByteArrayLiteral("#version 130"); + QTest::newRow("GL31compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 1) << QByteArrayLiteral("#version 140"); + QTest::newRow("GL32compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 2) << QByteArrayLiteral("#version 150 compatibility"); + QTest::newRow("GL33compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 3, 3) << QByteArrayLiteral("#version 330 compatibility"); + QTest::newRow("GL40compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 0) << QByteArrayLiteral("#version 400 compatibility"); + QTest::newRow("GL41compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 1) << QByteArrayLiteral("#version 410 compatibility"); + QTest::newRow("GL42compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 2) << QByteArrayLiteral("#version 420 compatibility"); + QTest::newRow("GL43compatibility") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 4, 3) << QByteArrayLiteral("#version 430 compatibility"); +} + +void tst_QShaderGenerator::shouldGenerateVersionCommands() +{ + // GIVEN + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, version); + const auto expectedCode = (QByteArrayList() << version + << "" + << "" + << "void main()" + << "{" + << "}" + << "").join('\n'); + QCOMPARE(code, expectedCode); +} + + +namespace { + QString toGlsl(QShaderLanguage::StorageQualifier qualifier, const QShaderFormat &format) + { + if (format.version().majorVersion() <= 2) { + // Note we're assuming fragment shader only here, it'd be different + // values for vertex shader, will need to be fixed properly at some + // point but isn't necessary yet (this problem already exists in past + // commits anyway) + switch (qualifier) { + case QShaderLanguage::Const: + return "const"; + case QShaderLanguage::Input: + return "varying"; + case QShaderLanguage::BuiltIn: + return "//"; + case QShaderLanguage::Output: + return ""; // Although fragment shaders for <=2 only have fixed outputs + case QShaderLanguage::Uniform: + return "uniform"; + } + } else { + switch (qualifier) { + case QShaderLanguage::Const: + return "const"; + case QShaderLanguage::Input: + return "in"; + case QShaderLanguage::BuiltIn: + return "//"; + case QShaderLanguage::Output: + return "out"; + case QShaderLanguage::Uniform: + return "uniform"; + } + } + + Q_UNREACHABLE(); + } + + QString toGlsl(QShaderLanguage::VariableType type) + { + switch (type) { + case QShaderLanguage::Bool: + return "bool"; + case QShaderLanguage::Int: + return "int"; + case QShaderLanguage::Uint: + return "uint"; + case QShaderLanguage::Float: + return "float"; + case QShaderLanguage::Double: + return "double"; + case QShaderLanguage::Vec2: + return "vec2"; + case QShaderLanguage::Vec3: + return "vec3"; + case QShaderLanguage::Vec4: + return "vec4"; + case QShaderLanguage::DVec2: + return "dvec2"; + case QShaderLanguage::DVec3: + return "dvec3"; + case QShaderLanguage::DVec4: + return "dvec4"; + case QShaderLanguage::BVec2: + return "bvec2"; + case QShaderLanguage::BVec3: + return "bvec3"; + case QShaderLanguage::BVec4: + return "bvec4"; + case QShaderLanguage::IVec2: + return "ivec2"; + case QShaderLanguage::IVec3: + return "ivec3"; + case QShaderLanguage::IVec4: + return "ivec4"; + case QShaderLanguage::UVec2: + return "uvec2"; + case QShaderLanguage::UVec3: + return "uvec3"; + case QShaderLanguage::UVec4: + return "uvec4"; + case QShaderLanguage::Mat2: + return "mat2"; + case QShaderLanguage::Mat3: + return "mat3"; + case QShaderLanguage::Mat4: + return "mat4"; + case QShaderLanguage::Mat2x2: + return "mat2x2"; + case QShaderLanguage::Mat2x3: + return "mat2x3"; + case QShaderLanguage::Mat2x4: + return "mat2x4"; + case QShaderLanguage::Mat3x2: + return "mat3x2"; + case QShaderLanguage::Mat3x3: + return "mat3x3"; + case QShaderLanguage::Mat3x4: + return "mat3x4"; + case QShaderLanguage::Mat4x2: + return "mat4x2"; + case QShaderLanguage::Mat4x3: + return "mat4x3"; + case QShaderLanguage::Mat4x4: + return "mat4x4"; + case QShaderLanguage::DMat2: + return "dmat2"; + case QShaderLanguage::DMat3: + return "dmat3"; + case QShaderLanguage::DMat4: + return "dmat4"; + case QShaderLanguage::DMat2x2: + return "dmat2x2"; + case QShaderLanguage::DMat2x3: + return "dmat2x3"; + case QShaderLanguage::DMat2x4: + return "dmat2x4"; + case QShaderLanguage::DMat3x2: + return "dmat3x2"; + case QShaderLanguage::DMat3x3: + return "dmat3x3"; + case QShaderLanguage::DMat3x4: + return "dmat3x4"; + case QShaderLanguage::DMat4x2: + return "dmat4x2"; + case QShaderLanguage::DMat4x3: + return "dmat4x3"; + case QShaderLanguage::DMat4x4: + return "dmat4x4"; + case QShaderLanguage::Sampler1D: + return "sampler1D"; + case QShaderLanguage::Sampler2D: + return "sampler2D"; + case QShaderLanguage::Sampler3D: + return "sampler3D"; + case QShaderLanguage::SamplerCube: + return "samplerCube"; + case QShaderLanguage::Sampler2DRect: + return "sampler2DRect"; + case QShaderLanguage::Sampler2DMs: + return "sampler2DMS"; + case QShaderLanguage::SamplerBuffer: + return "samplerBuffer"; + case QShaderLanguage::Sampler1DArray: + return "sampler1DArray"; + case QShaderLanguage::Sampler2DArray: + return "sampler2DArray"; + case QShaderLanguage::Sampler2DMsArray: + return "sampler2DMSArray"; + case QShaderLanguage::SamplerCubeArray: + return "samplerCubeArray"; + case QShaderLanguage::Sampler1DShadow: + return "sampler1DShadow"; + case QShaderLanguage::Sampler2DShadow: + return "sampler2DShadow"; + case QShaderLanguage::Sampler2DRectShadow: + return "sampler2DRectShadow"; + case QShaderLanguage::Sampler1DArrayShadow: + return "sampler1DArrayShadow"; + case QShaderLanguage::Sampler2DArrayShadow: + return "sample2DArrayShadow"; + case QShaderLanguage::SamplerCubeShadow: + return "samplerCubeShadow"; + case QShaderLanguage::SamplerCubeArrayShadow: + return "samplerCubeArrayShadow"; + case QShaderLanguage::ISampler1D: + return "isampler1D"; + case QShaderLanguage::ISampler2D: + return "isampler2D"; + case QShaderLanguage::ISampler3D: + return "isampler3D"; + case QShaderLanguage::ISamplerCube: + return "isamplerCube"; + case QShaderLanguage::ISampler2DRect: + return "isampler2DRect"; + case QShaderLanguage::ISampler2DMs: + return "isampler2DMS"; + case QShaderLanguage::ISamplerBuffer: + return "isamplerBuffer"; + case QShaderLanguage::ISampler1DArray: + return "isampler1DArray"; + case QShaderLanguage::ISampler2DArray: + return "isampler2DArray"; + case QShaderLanguage::ISampler2DMsArray: + return "isampler2DMSArray"; + case QShaderLanguage::ISamplerCubeArray: + return "isamplerCubeArray"; + case QShaderLanguage::USampler1D: + return "usampler1D"; + case QShaderLanguage::USampler2D: + return "usampler2D"; + case QShaderLanguage::USampler3D: + return "usampler3D"; + case QShaderLanguage::USamplerCube: + return "usamplerCube"; + case QShaderLanguage::USampler2DRect: + return "usampler2DRect"; + case QShaderLanguage::USampler2DMs: + return "usampler2DMS"; + case QShaderLanguage::USamplerBuffer: + return "usamplerBuffer"; + case QShaderLanguage::USampler1DArray: + return "usampler1DArray"; + case QShaderLanguage::USampler2DArray: + return "usampler2DArray"; + case QShaderLanguage::USampler2DMsArray: + return "usampler2DMSArray"; + case QShaderLanguage::USamplerCubeArray: + return "usamplerCubeArray"; + } + + Q_UNREACHABLE(); + } +} + +void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums_data() +{ + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderFormat>("format"); + QTest::addColumn<QByteArray>("expectedCode"); + + { + const auto es2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto es3 = createFormat(QShaderFormat::OpenGLES, 3, 0); + const auto gl2 = createFormat(QShaderFormat::OpenGLNoProfile, 2, 0); + const auto gl3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto qualifierEnum = QMetaEnum::fromType<QShaderLanguage::StorageQualifier>(); + const auto typeEnum = QMetaEnum::fromType<QShaderLanguage::VariableType>(); + + for (int qualifierIndex = 0; qualifierIndex < qualifierEnum.keyCount(); qualifierIndex++) { + const auto qualifierName = qualifierEnum.key(qualifierIndex); + const auto qualifierValue = static_cast<QShaderLanguage::StorageQualifier>(qualifierEnum.value(qualifierIndex)); + + for (int typeIndex = 0; typeIndex < typeEnum.keyCount(); typeIndex++) { + const auto typeName = typeEnum.key(typeIndex); + const auto typeValue = static_cast<QShaderLanguage::VariableType>(typeEnum.value(typeIndex)); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(qualifierValue)); + worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(typeValue)); + worldPosition.addRule(es2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + worldPosition.addRule(gl2, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + worldPosition.addRule(gl3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(es2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(gl2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(gl3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + graph.addNode(worldPosition); + graph.addNode(fragColor); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", fragColor.uuid(), "fragColor")); + + const auto gl2Code = (QByteArrayList() << "#version 110" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl2)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << " gl_fragColor = worldPosition;" + << "}" + << "").join("\n"); + const auto gl3Code = (QByteArrayList() << "#version 130" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl3)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = worldPosition;" + << "}" + << "").join("\n"); + const auto gl4Code = (QByteArrayList() << "#version 400 core" + << "" + << QStringLiteral("%1 %2 worldPosition;").arg(toGlsl(qualifierValue, gl4)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = worldPosition;" + << "}" + << "").join("\n"); + const auto es2Code = (QByteArrayList() << "#version 100" + << "" + << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es2)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << " gl_fragColor = worldPosition;" + << "}" + << "").join("\n"); + const auto es3Code = (QByteArrayList() << "#version 300 es" + << "" + << QStringLiteral("%1 highp %2 worldPosition;").arg(toGlsl(qualifierValue, es3)) + .arg(toGlsl(typeValue)) + .toUtf8() + << "" + << "void main()" + << "{" + << " gl_fragColor = worldPosition;" + << "}" + << "").join("\n"); + + QTest::addRow("%s %s ES2", qualifierName, typeName) << graph << es2 << es2Code; + QTest::addRow("%s %s ES3", qualifierName, typeName) << graph << es3 << es3Code; + QTest::addRow("%s %s GL2", qualifierName, typeName) << graph << gl2 << gl2Code; + QTest::addRow("%s %s GL3", qualifierName, typeName) << graph << gl3 << gl3Code; + QTest::addRow("%s %s GL4", qualifierName, typeName) << graph << gl4 << gl4Code; + } + } + } + + { + const auto es2 = createFormat(QShaderFormat::OpenGLES, 2, 0, QShaderFormat::Vertex); + const auto es3 = createFormat(QShaderFormat::OpenGLES, 3, 0, QShaderFormat::Vertex); + const auto gl2 = createFormat(QShaderFormat::OpenGLNoProfile, 2, 0, QShaderFormat::Vertex); + const auto gl3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, QShaderFormat::Vertex); + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0, QShaderFormat::Vertex); + + auto graph = QShaderGraph(); + + auto vertexPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + vertexPosition.setParameter("name", "vertexPosition"); + vertexPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Input)); + vertexPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec4)); + + vertexPosition.addRule(es2, QShaderNode::Rule("", + QByteArrayList() << "$qualifier highp $type $name;")); + vertexPosition.addRule(gl2, QShaderNode::Rule("", + QByteArrayList() << "$qualifier $type $name;")); + vertexPosition.addRule(gl3, QShaderNode::Rule("", + QByteArrayList() << "$qualifier $type $name;")); + + graph.addNode(vertexPosition); + + const auto gl2Code = (QByteArrayList() << "#version 110" + << "" + << "attribute vec4 vertexPosition;" + << "" + << "void main()" + << "{" + << "}" + << "").join("\n"); + const auto gl3Code = (QByteArrayList() << "#version 130" + << "" + << "in vec4 vertexPosition;" + << "" + << "void main()" + << "{" + << "}" + << "").join("\n"); + const auto gl4Code = (QByteArrayList() << "#version 400 core" + << "" + << "in vec4 vertexPosition;" + << "" + << "void main()" + << "{" + << "}" + << "").join("\n"); + const auto es2Code = (QByteArrayList() << "#version 100" + << "" + << "attribute highp vec4 vertexPosition;" + << "" + << "void main()" + << "{" + << "}" + << "").join("\n"); + const auto es3Code = (QByteArrayList() << "#version 300 es" + << "" + << "in highp vec4 vertexPosition;" + << "" + << "void main()" + << "{" + << "}" + << "").join("\n"); + + QTest::addRow("Attribute header substitution ES2") << graph << es2 << es2Code; + QTest::addRow("Attribute header substitution ES3") << graph << es3 << es3Code; + QTest::addRow("Attribute header substitution GL2") << graph << gl2 << gl2Code; + QTest::addRow("Attribute header substitution GL3") << graph << gl3 << gl3Code; + QTest::addRow("Attribute header substitution GL4") << graph << gl4 << gl4Code; + } +} + +void tst_QShaderGenerator::shouldProcessLanguageQualifierAndTypeEnums() +{ + // GIVEN + QFETCH(QShaderGraph, graph); + QFETCH(QShaderFormat, format); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = format; + + // WHEN + const auto code = generator.createShaderCode(); + + // THEN + QFETCH(QByteArray, expectedCode); + QCOMPARE(code, expectedCode); +} + +void tst_QShaderGenerator::shouldGenerateDifferentCodeDependingOnActiveLayers() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }, { + "diffuseTexture", + "normalTexture" + }); + texCoord.addRule(gl4, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + auto diffuseUniform = createNode({ + createPort(QShaderNodePort::Output, "color") + }, {"diffuseUniform"}); + diffuseUniform.addRule(gl4, QShaderNode::Rule("vec4 $color = diffuseUniform;", + QByteArrayList() << "uniform vec4 diffuseUniform;")); + auto diffuseTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }, {"diffuseTexture"}); + diffuseTexture.addRule(gl4, QShaderNode::Rule("vec4 $color = texture2D(diffuseTexture, $coord);", + QByteArrayList() << "uniform sampler2D diffuseTexture;")); + auto normalUniform = createNode({ + createPort(QShaderNodePort::Output, "normal") + }, {"normalUniform"}); + normalUniform.addRule(gl4, QShaderNode::Rule("vec3 $normal = normalUniform;", + QByteArrayList() << "uniform vec3 normalUniform;")); + auto normalTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "normal") + }, {"normalTexture"}); + normalTexture.addRule(gl4, QShaderNode::Rule("vec3 $normal = texture2D(normalTexture, $coord).rgb;", + QByteArrayList() << "uniform sampler2D normalTexture;")); + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "color"), + createPort(QShaderNodePort::Input, "normal"), + createPort(QShaderNodePort::Output, "output") + }); + lightFunction.addRule(gl4, QShaderNode::Rule("vec4 $output = lightModel($color, $normal);", + QByteArrayList() << "#pragma include gl4/lightmodel.frag.inc")); + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(gl4, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(texCoord); + res.addNode(diffuseUniform); + res.addNode(diffuseTexture); + res.addNode(normalUniform); + res.addNode(normalTexture); + res.addNode(lightFunction); + res.addNode(fragColor); + + res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"})); + res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"})); + + res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"})); + res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"})); + + res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseUniform", "normalUniform"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "uniform vec4 diffuseUniform;" + << "uniform vec3 normalUniform;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = ((lightModel(diffuseUniform, normalUniform)));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseUniform", "normalTexture"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform vec4 diffuseUniform;" + << "uniform sampler2D normalTexture;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = ((lightModel(diffuseUniform, texture2D(normalTexture, texCoord).rgb)));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseTexture", "normalUniform"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform sampler2D diffuseTexture;" + << "uniform vec3 normalUniform;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = ((lightModel(texture2D(diffuseTexture, texCoord), normalUniform)));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto code = generator.createShaderCode({"diffuseTexture", "normalTexture"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec2 texCoord;" + << "uniform sampler2D diffuseTexture;" + << "uniform sampler2D normalTexture;" + << "#pragma include gl4/lightmodel.frag.inc" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = ((lightModel(texture2D(diffuseTexture, texCoord), texture2D(normalTexture, texCoord).rgb)));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } +} + +void tst_QShaderGenerator::shouldUseGlobalVariableRatherThanTemporaries() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + { + // WHEN + auto vertexPosition = createNode({ + createPort(QShaderNodePort::Output, "vertexPosition") + }); + vertexPosition.addRule(gl4, QShaderNode::Rule("vec4 $vertexPosition = vertexPosition;", + QByteArrayList() << "in vec4 vertexPosition;")); + + auto fakeMultiPlyNoSpace = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeMultiPlyNoSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName*speed;")); + + auto fakeMultiPlySpace = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeMultiPlySpace.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName * speed;")); + + auto fakeJoinNoSpace = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeJoinNoSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = vec4($varName.xyz,$varName.w);")); + + auto fakeJoinSpace = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeJoinSpace.addRule(gl4, QShaderNode::Rule("vec4 $out = vec4($varName.xyz, $varName.w);")); + + auto fakeAdd = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeAdd.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName.xyzw + $varName;")); + + auto fakeSub = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeSub.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName.xyzw - $varName;")); + + auto fakeDiv = createNode({ + createPort(QShaderNodePort::Input, "varName"), + createPort(QShaderNodePort::Output, "out") + }); + fakeDiv.addRule(gl4, QShaderNode::Rule("vec4 $out = $varName / v0;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "input1"), + createPort(QShaderNodePort::Input, "input2"), + createPort(QShaderNodePort::Input, "input3"), + createPort(QShaderNodePort::Input, "input4"), + createPort(QShaderNodePort::Input, "input5"), + createPort(QShaderNodePort::Input, "input6"), + createPort(QShaderNodePort::Input, "input7") + }); + fragColor.addRule(gl4, QShaderNode::Rule("fragColor = $input1 + $input2 + $input3 + $input4 + $input5 + $input6 + $input7;", + QByteArrayList() << "out vec4 fragColor;")); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(vertexPosition); + res.addNode(fakeMultiPlyNoSpace); + res.addNode(fakeMultiPlySpace); + res.addNode(fakeJoinNoSpace); + res.addNode(fakeJoinSpace); + res.addNode(fakeAdd); + res.addNode(fakeSub); + res.addNode(fakeDiv); + res.addNode(fragColor); + + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeMultiPlyNoSpace.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeMultiPlySpace.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeJoinNoSpace.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeJoinSpace.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeAdd.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeSub.uuid(), "varName")); + res.addEdge(createEdge(vertexPosition.uuid(), "vertexPosition", fakeDiv.uuid(), "varName")); + res.addEdge(createEdge(fakeMultiPlyNoSpace.uuid(), "out", fragColor.uuid(), "input1")); + res.addEdge(createEdge(fakeMultiPlySpace.uuid(), "out", fragColor.uuid(), "input2")); + res.addEdge(createEdge(fakeJoinNoSpace.uuid(), "out", fragColor.uuid(), "input3")); + res.addEdge(createEdge(fakeJoinSpace.uuid(), "out", fragColor.uuid(), "input4")); + res.addEdge(createEdge(fakeAdd.uuid(), "out", fragColor.uuid(), "input5")); + res.addEdge(createEdge(fakeSub.uuid(), "out", fragColor.uuid(), "input6")); + res.addEdge(createEdge(fakeDiv.uuid(), "out", fragColor.uuid(), "input7")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode({"diffuseUniform", "normalUniform"}); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 vertexPosition;" + << "out vec4 fragColor;" + << "" + << "void main()" + << "{" + << " fragColor = (((((((vertexPosition*speed + vertexPosition * speed + ((vec4(vertexPosition.xyz,vertexPosition.w))) + ((vec4(vertexPosition.xyz, vertexPosition.w))) + ((vertexPosition.xyzw + vertexPosition)) + ((vertexPosition.xyzw - vertexPosition)) + ((vertexPosition / vertexPosition)))))))));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } +} + +void tst_QShaderGenerator::shouldGenerateTemporariesWisely() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + { + auto attribute = createNode({ + createPort(QShaderNodePort::Output, "vertexPosition") + }); + attribute.addRule(gl4, QShaderNode::Rule("vec4 $vertexPosition = vertexPosition;", + QByteArrayList() << "in vec4 vertexPosition;")); + + auto complexFunction = createNode({ + createPort(QShaderNodePort::Input, "inputVarName"), + createPort(QShaderNodePort::Output, "out") + }); + complexFunction.addRule(gl4, QShaderNode::Rule("vec4 $out = $inputVarName * 2.0;")); + + auto complexFunction2 = createNode({ + createPort(QShaderNodePort::Input, "inputVarName"), + createPort(QShaderNodePort::Output, "out") + }); + complexFunction2.addRule(gl4, QShaderNode::Rule("vec4 $out = $inputVarName * 4.0;")); + + auto complexFunction3 = createNode({ + createPort(QShaderNodePort::Input, "a"), + createPort(QShaderNodePort::Input, "b"), + createPort(QShaderNodePort::Output, "out") + }); + complexFunction3.addRule(gl4, QShaderNode::Rule("vec4 $out = $a + $b;")); + + auto shaderOutput1 = createNode({ + createPort(QShaderNodePort::Input, "input") + }); + + shaderOutput1.addRule(gl4, QShaderNode::Rule("shaderOutput1 = $input;", + QByteArrayList() << "out vec4 shaderOutput1;")); + + auto shaderOutput2 = createNode({ + createPort(QShaderNodePort::Input, "input") + }); + + shaderOutput2.addRule(gl4, QShaderNode::Rule("shaderOutput2 = $input;", + QByteArrayList() << "out vec4 shaderOutput2;")); + + { + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(attribute); + res.addNode(complexFunction); + res.addNode(shaderOutput1); + + res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); + res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput1.uuid(), "input")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 vertexPosition;" + << "out vec4 shaderOutput1;" + << "" + << "void main()" + << "{" + << " shaderOutput1 = vertexPosition * 2.0;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(attribute); + res.addNode(complexFunction); + res.addNode(shaderOutput1); + res.addNode(shaderOutput2); + + res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); + res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput1.uuid(), "input")); + res.addEdge(createEdge(complexFunction.uuid(), "out", shaderOutput2.uuid(), "input")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 vertexPosition;" + << "out vec4 shaderOutput1;" + << "out vec4 shaderOutput2;" + << "" + << "void main()" + << "{" + << " vec4 v1 = vertexPosition * 2.0;" + << " shaderOutput2 = v1;" + << " shaderOutput1 = v1;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + + { + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(attribute); + res.addNode(complexFunction); + res.addNode(complexFunction2); + res.addNode(complexFunction3); + res.addNode(shaderOutput1); + res.addNode(shaderOutput2); + + res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction.uuid(), "inputVarName")); + res.addEdge(createEdge(attribute.uuid(), "vertexPosition", complexFunction2.uuid(), "inputVarName")); + + res.addEdge(createEdge(complexFunction.uuid(), "out", complexFunction3.uuid(), "a")); + res.addEdge(createEdge(complexFunction2.uuid(), "out", complexFunction3.uuid(), "b")); + + res.addEdge(createEdge(complexFunction3.uuid(), "out", shaderOutput1.uuid(), "input")); + res.addEdge(createEdge(complexFunction2.uuid(), "out", shaderOutput2.uuid(), "input")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 vertexPosition;" + << "out vec4 shaderOutput1;" + << "out vec4 shaderOutput2;" + << "" + << "void main()" + << "{" + << " vec4 v2 = vertexPosition * 4.0;" + << " shaderOutput2 = v2;" + << " shaderOutput1 = (vertexPosition * 2.0 + v2);" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); + } + } +} + +void tst_QShaderGenerator::shouldHandlePortNamesPrefixingOneAnother() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + auto color1 = createNode({ + createPort(QShaderNodePort::Output, "output") + }); + color1.addRule(gl4, QShaderNode::Rule("vec4 $output = color1;", + QByteArrayList() << "in vec4 color1;")); + + auto color2 = createNode({ + createPort(QShaderNodePort::Output, "output") + }); + color2.addRule(gl4, QShaderNode::Rule("vec4 $output = color2;", + QByteArrayList() << "in vec4 color2;")); + + auto addColor = createNode({ + createPort(QShaderNodePort::Output, "color"), + createPort(QShaderNodePort::Input, "color1"), + createPort(QShaderNodePort::Input, "color2"), + }); + addColor.addRule(gl4, QShaderNode::Rule("vec4 $color = $color1 + $color2;")); + + auto shaderOutput = createNode({ + createPort(QShaderNodePort::Input, "input") + }); + + shaderOutput.addRule(gl4, QShaderNode::Rule("shaderOutput = $input;", + QByteArrayList() << "out vec4 shaderOutput;")); + + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(color1); + res.addNode(color2); + res.addNode(addColor); + res.addNode(shaderOutput); + + res.addEdge(createEdge(color1.uuid(), "output", addColor.uuid(), "color1")); + res.addEdge(createEdge(color2.uuid(), "output", addColor.uuid(), "color2")); + res.addEdge(createEdge(addColor.uuid(), "color", shaderOutput.uuid(), "input")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 color1;" + << "in vec4 color2;" + << "out vec4 shaderOutput;" + << "" + << "void main()" + << "{" + << " shaderOutput = ((color1 + color2));" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); +} + +void tst_QShaderGenerator::shouldHandleNodesWithMultipleOutputPorts() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "output0"), + createPort(QShaderNodePort::Output, "output1") + }); + input.addRule(gl4, QShaderNode::Rule("vec4 $output0 = globalIn0;" + "float $output1 = globalIn1;", + QByteArrayList() << "in vec4 globalIn0;" << "in float globalIn1;")); + + auto function = createNode({ + createPort(QShaderNodePort::Input, "input0"), + createPort(QShaderNodePort::Input, "input1"), + createPort(QShaderNodePort::Output, "output0"), + createPort(QShaderNodePort::Output, "output1") + }); + function.addRule(gl4, QShaderNode::Rule("vec4 $output0 = $input0;" + "float $output1 = $input1;")); + + auto output = createNode({ + createPort(QShaderNodePort::Input, "input0"), + createPort(QShaderNodePort::Input, "input1") + }); + + output.addRule(gl4, QShaderNode::Rule("globalOut0 = $input0;" + "globalOut1 = $input1;", + QByteArrayList() << "out vec4 globalOut0;" << "out float globalOut1;")); + + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(input); + res.addNode(function); + res.addNode(output); + + res.addEdge(createEdge(input.uuid(), "output0", function.uuid(), "input0")); + res.addEdge(createEdge(input.uuid(), "output1", function.uuid(), "input1")); + + res.addEdge(createEdge(function.uuid(), "output0", output.uuid(), "input0")); + res.addEdge(createEdge(function.uuid(), "output1", output.uuid(), "input1")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "in vec4 globalIn0;" + << "in float globalIn1;" + << "out vec4 globalOut0;" + << "out float globalOut1;" + << "" + << "void main()" + << "{" + << " globalOut0 = globalIn0;" + << " globalOut1 = globalIn1;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); +} + +void tst_QShaderGenerator::shouldHandleExpressionsInInputNodes() +{ + // GIVEN + const auto gl4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "output") + }); + input.addRule(gl4, QShaderNode::Rule("float $output = 3 + 4;")); + + auto output = createNode({ + createPort(QShaderNodePort::Input, "input") + }); + + output.addRule(gl4, QShaderNode::Rule("globalOut = $input;", + QByteArrayList() << "out float globalOut;")); + + // WHEN + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(input); + res.addNode(output); + + res.addEdge(createEdge(input.uuid(), "output", output.uuid(), "input")); + + return res; + }(); + + auto generator = QShaderGenerator(); + generator.graph = graph; + generator.format = gl4; + + const auto code = generator.createShaderCode(); + + // THEN + const auto expected = QByteArrayList() + << "#version 400 core" + << "" + << "out float globalOut;" + << "" + << "void main()" + << "{" + << " globalOut = 3 + 4;" + << "}" + << ""; + QCOMPARE(code, expected.join("\n")); +} + +QTEST_MAIN(tst_QShaderGenerator) + +#include "tst_qshadergenerator.moc" diff --git a/tests/auto/render/shadergraph/qshadergraph/qshadergraph.pro b/tests/auto/render/shadergraph/qshadergraph/qshadergraph.pro new file mode 100644 index 000000000..2348ccb24 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergraph/qshadergraph.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib 3drender-private + +SOURCES += tst_qshadergraph.cpp +TARGET = tst_qshadergraph diff --git a/tests/auto/render/shadergraph/qshadergraph/tst_qshadergraph.cpp b/tests/auto/render/shadergraph/qshadergraph/tst_qshadergraph.cpp new file mode 100644 index 000000000..6336763f5 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergraph/tst_qshadergraph.cpp @@ -0,0 +1,821 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <Qt3DRender/private/qshadergraph_p.h> + +using namespace Qt3DRender; +namespace +{ + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph::Statement createStatement(const QShaderNode &node, + const QVector<int> &inputs = QVector<int>(), + const QVector<int> &outputs = QVector<int>()) + { + auto statement = QShaderGraph::Statement(); + statement.node = node; + statement.inputs = inputs; + statement.outputs = outputs; + return statement; + } + + void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement) + { + qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs; + } + + void dumpStatementsIfNeeded(const QVector<QShaderGraph::Statement> &statements, const QVector<QShaderGraph::Statement> &expected) + { + if (statements != expected) { + for (int i = 0; i < qMax(statements.size(), expected.size()); i++) { + qDebug() << "----" << i << "----"; + if (i < statements.size()) + debugStatement("A:", statements.at(i)); + if (i < expected.size()) + debugStatement("E:", expected.at(i)); + qDebug() << "-----------"; + } + } + } +} + +class tst_QShaderGraph : public QObject +{ + Q_OBJECT +private slots: + void shouldHaveEdgeDefaultState(); + void shouldTestEdgesEquality_data(); + void shouldTestEdgesEquality(); + void shouldManipulateStatementMembers(); + void shouldTestStatementsEquality_data(); + void shouldTestStatementsEquality(); + void shouldFindIndexFromPortNameInStatements_data(); + void shouldFindIndexFromPortNameInStatements(); + void shouldManageNodeList(); + void shouldManageEdgeList(); + void shouldSerializeGraphForCodeGeneration(); + void shouldHandleUnboundPortsDuringGraphSerialization(); + void shouldSurviveCyclesDuringGraphSerialization(); + void shouldDealWithEdgesJumpingOverLayers(); + void shouldGenerateDifferentStatementsDependingOnActiveLayers(); + void shouldDealWithBranchesWithoutOutput(); +}; + +void tst_QShaderGraph::shouldHaveEdgeDefaultState() +{ + // GIVEN + auto edge = QShaderGraph::Edge(); + + // THEN + QVERIFY(edge.sourceNodeUuid.isNull()); + QVERIFY(edge.sourcePortName.isEmpty()); + QVERIFY(edge.targetNodeUuid.isNull()); + QVERIFY(edge.targetPortName.isEmpty()); +} + +void tst_QShaderGraph::shouldTestEdgesEquality_data() +{ + QTest::addColumn<QShaderGraph::Edge>("left"); + QTest::addColumn<QShaderGraph::Edge>("right"); + QTest::addColumn<bool>("expected"); + + const auto sourceUuid1 = QUuid::createUuid(); + const auto sourceUuid2 = QUuid::createUuid(); + const auto targetUuid1 = QUuid::createUuid(); + const auto targetUuid2 = QUuid::createUuid(); + + QTest::newRow("Equals") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << true; + QTest::newRow("SourceUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid2, "foo", targetUuid1, "bar") + << false; + QTest::newRow("SourceName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "bleh", targetUuid1, "bar") + << false; + QTest::newRow("TargetUuid") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid2, "bar") + << false; + QTest::newRow("TargetName") << createEdge(sourceUuid1, "foo", targetUuid1, "bar") + << createEdge(sourceUuid1, "foo", targetUuid1, "bleh") + << false; +} + +void tst_QShaderGraph::shouldTestEdgesEquality() +{ + // GIVEN + QFETCH(QShaderGraph::Edge, left); + QFETCH(QShaderGraph::Edge, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderGraph::shouldManipulateStatementMembers() +{ + // GIVEN + auto statement = QShaderGraph::Statement(); + + // THEN (default state) + QVERIFY(statement.inputs.isEmpty()); + QVERIFY(statement.outputs.isEmpty()); + QVERIFY(statement.node.uuid().isNull()); + QVERIFY(statement.uuid().isNull()); + + // WHEN + const auto node = createNode({}); + statement.node = node; + + // THEN + QCOMPARE(statement.uuid(), node.uuid()); + + // WHEN + statement.node = QShaderNode(); + + // THEN + QVERIFY(statement.uuid().isNull()); +} + +void tst_QShaderGraph::shouldTestStatementsEquality_data() +{ + QTest::addColumn<QShaderGraph::Statement>("left"); + QTest::addColumn<QShaderGraph::Statement>("right"); + QTest::addColumn<bool>("expected"); + + const auto node1 = createNode({}); + const auto node2 = createNode({}); + + QTest::newRow("EqualNodes") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2}, {3, 4}) + << true; + QTest::newRow("EqualInvalids") << createStatement(QShaderNode(), {1, 2}, {3, 4}) + << createStatement(QShaderNode(), {1, 2}, {3, 4}) + << true; + QTest::newRow("Nodes") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node2, {1, 2}, {3, 4}) + << false; + QTest::newRow("Inputs") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2, 0}, {3, 4}) + << false; + QTest::newRow("Outputs") << createStatement(node1, {1, 2}, {3, 4}) + << createStatement(node1, {1, 2}, {3, 0, 4}) + << false; +} + +void tst_QShaderGraph::shouldTestStatementsEquality() +{ + // GIVEN + QFETCH(QShaderGraph::Statement, left); + QFETCH(QShaderGraph::Statement, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements_data() +{ + QTest::addColumn<QShaderGraph::Statement>("statement"); + QTest::addColumn<QString>("portName"); + QTest::addColumn<int>("expectedInputIndex"); + QTest::addColumn<int>("expectedOutputIndex"); + + const auto inputNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Output, "input") + })); + const auto outputNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Input, "output") + })); + const auto functionNodeStatement = createStatement(createNode({ + createPort(QShaderNodePort::Input, "input1"), + createPort(QShaderNodePort::Output, "output1"), + createPort(QShaderNodePort::Input, "input2"), + createPort(QShaderNodePort::Output, "output2"), + createPort(QShaderNodePort::Output, "output3"), + createPort(QShaderNodePort::Input, "input3") + })); + + QTest::newRow("Invalid") << QShaderGraph::Statement() << "foo" << -1 << -1; + QTest::newRow("InputNodeWrongName") << inputNodeStatement << "foo" << -1 << -1; + QTest::newRow("InputNodeExistingName") << inputNodeStatement << "input" << -1 << 0; + QTest::newRow("OutputNodeWrongName") << outputNodeStatement << "foo" << -1 << -1; + QTest::newRow("OutputNodeExistingName") << outputNodeStatement << "output" << 0 << -1; + QTest::newRow("FunctionNodeWrongName") << functionNodeStatement << "foo" << -1 << -1; + QTest::newRow("FunctionNodeInput1") << functionNodeStatement << "input1" << 0 << -1; + QTest::newRow("FunctionNodeOutput1") << functionNodeStatement << "output1" << -1 << 0; + QTest::newRow("FunctionNodeInput2") << functionNodeStatement << "input2" << 1 << -1; + QTest::newRow("FunctionNodeOutput2") << functionNodeStatement << "output2" << -1 << 1; + QTest::newRow("FunctionNodeInput3") << functionNodeStatement << "input3" << 2 << -1; + QTest::newRow("FunctionNodeOutput3") << functionNodeStatement << "output3" << -1 << 2; +} + +void tst_QShaderGraph::shouldFindIndexFromPortNameInStatements() +{ + // GIVEN + QFETCH(QShaderGraph::Statement, statement); + QFETCH(QString, portName); + QFETCH(int, expectedInputIndex); + QFETCH(int, expectedOutputIndex); + + // WHEN + const auto inputIndex = statement.portIndex(QShaderNodePort::Input, portName); + const auto outputIndex = statement.portIndex(QShaderNodePort::Output, portName); + + // THEN + QCOMPARE(inputIndex, expectedInputIndex); + QCOMPARE(outputIndex, expectedOutputIndex); +} + +void tst_QShaderGraph::shouldManageNodeList() +{ + // GIVEN + const auto node1 = createNode({createPort(QShaderNodePort::Output, "node1")}); + const auto node2 = createNode({createPort(QShaderNodePort::Output, "node2")}); + + auto graph = QShaderGraph(); + + // THEN (default state) + QVERIFY(graph.nodes().isEmpty()); + + // WHEN + graph.addNode(node1); + + // THEN + QCOMPARE(graph.nodes().size(), 1); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + + // WHEN + graph.addNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name); + + + // WHEN + graph.removeNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 1); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + + // WHEN + graph.addNode(node2); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node1.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node1.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node2.ports().at(0).name); + + // WHEN + const auto node1bis = [node1] { + auto res = node1; + auto port = res.ports().at(0); + port.name = QStringLiteral("node1bis"); + res.addPort(port); + return res; + }(); + graph.addNode(node1bis); + + // THEN + QCOMPARE(graph.nodes().size(), 2); + QCOMPARE(graph.nodes().at(0).uuid(), node2.uuid()); + QCOMPARE(graph.nodes().at(0).ports().at(0).name, node2.ports().at(0).name); + QCOMPARE(graph.nodes().at(1).uuid(), node1bis.uuid()); + QCOMPARE(graph.nodes().at(1).ports().at(0).name, node1bis.ports().at(0).name); +} + +void tst_QShaderGraph::shouldManageEdgeList() +{ + // GIVEN + const auto edge1 = createEdge(QUuid::createUuid(), "foo", QUuid::createUuid(), "bar"); + const auto edge2 = createEdge(QUuid::createUuid(), "baz", QUuid::createUuid(), "boo"); + + auto graph = QShaderGraph(); + + // THEN (default state) + QVERIFY(graph.edges().isEmpty()); + + // WHEN + graph.addEdge(edge1); + + // THEN + QCOMPARE(graph.edges().size(), 1); + QCOMPARE(graph.edges().at(0), edge1); + + // WHEN + graph.addEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); + + + // WHEN + graph.removeEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 1); + QCOMPARE(graph.edges().at(0), edge1); + + // WHEN + graph.addEdge(edge2); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); + + // WHEN + graph.addEdge(edge1); + + // THEN + QCOMPARE(graph.edges().size(), 2); + QCOMPARE(graph.edges().at(0), edge1); + QCOMPARE(graph.edges().at(1), edge2); +} + +void tst_QShaderGraph::shouldSerializeGraphForCodeGeneration() +{ + // GIVEN + const auto input1 = createNode({ + createPort(QShaderNodePort::Output, "input1Value") + }); + const auto input2 = createNode({ + createPort(QShaderNodePort::Output, "input2Value") + }); + const auto output1 = createNode({ + createPort(QShaderNodePort::Input, "output1Value") + }); + const auto output2 = createNode({ + createPort(QShaderNodePort::Input, "output2Value") + }); + const auto function1 = createNode({ + createPort(QShaderNodePort::Input, "function1Input"), + createPort(QShaderNodePort::Output, "function1Output") + }); + const auto function2 = createNode({ + createPort(QShaderNodePort::Input, "function2Input1"), + createPort(QShaderNodePort::Input, "function2Input2"), + createPort(QShaderNodePort::Output, "function2Output") + }); + const auto function3 = createNode({ + createPort(QShaderNodePort::Input, "function3Input1"), + createPort(QShaderNodePort::Input, "function3Input2"), + createPort(QShaderNodePort::Output, "function3Output1"), + createPort(QShaderNodePort::Output, "function3Output2") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input1); + res.addNode(input2); + res.addNode(output1); + res.addNode(output2); + res.addNode(function1); + res.addNode(function2); + res.addNode(function3); + res.addEdge(createEdge(input1.uuid(), "input1Value", function1.uuid(), "function1Input")); + res.addEdge(createEdge(input1.uuid(), "input1Value", function2.uuid(), "function2Input1")); + res.addEdge(createEdge(input2.uuid(), "input2Value", function2.uuid(), "function2Input2")); + res.addEdge(createEdge(function1.uuid(), "function1Output", function3.uuid(), "function3Input1")); + res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input2")); + res.addEdge(createEdge(function3.uuid(), "function3Output1", output1.uuid(), "output1Value")); + res.addEdge(createEdge(function3.uuid(), "function3Output2", output2.uuid(), "output2Value")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(input2, {}, {1}) + << createStatement(input1, {}, {0}) + << createStatement(function2, {0, 1}, {3}) + << createStatement(function1, {0}, {2}) + << createStatement(function3, {2, 3}, {4, 5}) + << createStatement(output2, {5}, {}) + << createStatement(output1, {4}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldHandleUnboundPortsDuringGraphSerialization() +{ + // GIVEN + const auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + const auto unboundInput = createNode({ + createPort(QShaderNodePort::Output, "unbound") + }); + const auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + const auto unboundOutput = createNode({ + createPort(QShaderNodePort::Input, "unbound") + }); + const auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput1"), + createPort(QShaderNodePort::Input, "functionInput2"), + createPort(QShaderNodePort::Input, "functionInput3"), + createPort(QShaderNodePort::Output, "functionOutput1"), + createPort(QShaderNodePort::Output, "functionOutput2"), + createPort(QShaderNodePort::Output, "functionOutput3") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input); + res.addNode(unboundInput); + res.addNode(output); + res.addNode(unboundOutput); + res.addNode(function); + res.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput2")); + res.addEdge(createEdge(function.uuid(), "functionOutput2", output.uuid(), "output")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + // Note that no statement has any unbound input + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(input, {}, {0}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldSurviveCyclesDuringGraphSerialization() +{ + // GIVEN + const auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + const auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + const auto function1 = createNode({ + createPort(QShaderNodePort::Input, "function1Input1"), + createPort(QShaderNodePort::Input, "function1Input2"), + createPort(QShaderNodePort::Output, "function1Output") + }); + const auto function2 = createNode({ + createPort(QShaderNodePort::Input, "function2Input"), + createPort(QShaderNodePort::Output, "function2Output") + }); + const auto function3 = createNode({ + createPort(QShaderNodePort::Input, "function3Input"), + createPort(QShaderNodePort::Output, "function3Output") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input); + res.addNode(output); + res.addNode(function1); + res.addNode(function2); + res.addNode(function3); + res.addEdge(createEdge(input.uuid(), "input", function1.uuid(), "function1Input1")); + res.addEdge(createEdge(function1.uuid(), "function1Output", function2.uuid(), "function2Input")); + res.addEdge(createEdge(function2.uuid(), "function2Output", function3.uuid(), "function3Input")); + res.addEdge(createEdge(function3.uuid(), "function3Output", function1.uuid(), "function1Input2")); + res.addEdge(createEdge(function2.uuid(), "function2Output", output.uuid(), "output")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + // The cycle is ignored + const auto expected = QVector<QShaderGraph::Statement>(); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldDealWithEdgesJumpingOverLayers() +{ + // GIVEN + const auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "worldPosition") + }); + const auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + const auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + const auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "lightIntensity") + }); + const auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + const auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + const auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + const auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + const auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(worldPosition); + res.addNode(texture); + res.addNode(texCoord); + res.addNode(lightIntensity); + res.addNode(exposure); + res.addNode(fragColor); + res.addNode(sampleTexture); + res.addNode(lightFunction); + res.addNode(exposureFunction); + + res.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + res.addEdge(createEdge(worldPosition.uuid(), "worldPosition", lightFunction.uuid(), "position")); + res.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + res.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity")); + + res.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + res.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + res.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {2}) + << createStatement(texture, {}, {1}) + << createStatement(lightIntensity, {}, {3}) + << createStatement(sampleTexture, {1, 2}, {5}) + << createStatement(worldPosition, {}, {0}) + << createStatement(exposure, {}, {4}) + << createStatement(lightFunction, {5, 0, 3}, {6}) + << createStatement(exposureFunction, {6, 4}, {7}) + << createStatement(fragColor, {7}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +void tst_QShaderGraph::shouldGenerateDifferentStatementsDependingOnActiveLayers() +{ + // GIVEN + const auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }, { + "diffuseTexture", + "normalTexture" + }); + const auto diffuseUniform = createNode({ + createPort(QShaderNodePort::Output, "color") + }, {"diffuseUniform"}); + const auto diffuseTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }, {"diffuseTexture"}); + const auto normalUniform = createNode({ + createPort(QShaderNodePort::Output, "normal") + }, {"normalUniform"}); + const auto normalTexture = createNode({ + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "normal") + }, {"normalTexture"}); + const auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "color"), + createPort(QShaderNodePort::Input, "normal"), + createPort(QShaderNodePort::Output, "output") + }); + const auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + + res.addNode(texCoord); + res.addNode(diffuseUniform); + res.addNode(diffuseTexture); + res.addNode(normalUniform); + res.addNode(normalTexture); + res.addNode(lightFunction); + res.addNode(fragColor); + + res.addEdge(createEdge(diffuseUniform.uuid(), "color", lightFunction.uuid(), "color", {"diffuseUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", diffuseTexture.uuid(), "coord", {"diffuseTexture"})); + res.addEdge(createEdge(diffuseTexture.uuid(), "color", lightFunction.uuid(), "color", {"diffuseTexture"})); + + res.addEdge(createEdge(normalUniform.uuid(), "normal", lightFunction.uuid(), "normal", {"normalUniform"})); + res.addEdge(createEdge(texCoord.uuid(), "texCoord", normalTexture.uuid(), "coord", {"normalTexture"})); + res.addEdge(createEdge(normalTexture.uuid(), "normal", lightFunction.uuid(), "normal", {"normalTexture"})); + + res.addEdge(createEdge(lightFunction.uuid(), "output", fragColor.uuid(), "fragColor")); + + return res; + }(); + + { + // WHEN + const auto statements = graph.createStatements({"diffuseUniform", "normalUniform"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(normalUniform, {}, {1}) + << createStatement(diffuseUniform, {}, {0}) + << createStatement(lightFunction, {0, 1}, {2}) + << createStatement(fragColor, {2}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseUniform", "normalTexture"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalTexture, {0}, {2}) + << createStatement(diffuseUniform, {}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseTexture", "normalUniform"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalUniform, {}, {2}) + << createStatement(diffuseTexture, {0}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } + + { + // WHEN + const auto statements = graph.createStatements({"diffuseTexture", "normalTexture"}); + + // THEN + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(texCoord, {}, {0}) + << createStatement(normalTexture, {0}, {2}) + << createStatement(diffuseTexture, {0}, {1}) + << createStatement(lightFunction, {1, 2}, {3}) + << createStatement(fragColor, {3}, {}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + } +} + +void tst_QShaderGraph::shouldDealWithBranchesWithoutOutput() +{ + // GIVEN + const auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + const auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + const auto danglingFunction = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "unbound") + }); + const auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "functionOutput") + }); + + const auto graph = [=] { + auto res = QShaderGraph(); + res.addNode(input); + res.addNode(function); + res.addNode(danglingFunction); + res.addNode(output); + res.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput")); + res.addEdge(createEdge(input.uuid(), "input", danglingFunction.uuid(), "functionInput")); + res.addEdge(createEdge(function.uuid(), "functionOutput", output.uuid(), "output")); + return res; + }(); + + // WHEN + const auto statements = graph.createStatements(); + + // THEN + // Note that no edge leads to the unbound input + const auto expected = QVector<QShaderGraph::Statement>() + << createStatement(input, {}, {0}) + << createStatement(function, {0}, {1}) + << createStatement(output, {1}, {}) + << createStatement(danglingFunction, {0}, {2}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); +} + +QTEST_MAIN(tst_QShaderGraph) + +#include "tst_qshadergraph.moc" diff --git a/tests/auto/render/shadergraph/qshadergraphloader/qshadergraphloader.pro b/tests/auto/render/shadergraph/qshadergraphloader/qshadergraphloader.pro new file mode 100644 index 000000000..754f801e0 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergraphloader/qshadergraphloader.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib 3drender-private + +SOURCES += tst_qshadergraphloader.cpp +TARGET = tst_qshadergraphloader diff --git a/tests/auto/render/shadergraph/qshadergraphloader/tst_qshadergraphloader.cpp b/tests/auto/render/shadergraph/qshadergraphloader/tst_qshadergraphloader.cpp new file mode 100644 index 000000000..25e71d50b --- /dev/null +++ b/tests/auto/render/shadergraph/qshadergraphloader/tst_qshadergraphloader.cpp @@ -0,0 +1,627 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qbuffer.h> + +#include <Qt3DRender/private/qshadergraphloader_p.h> +#include <Qt3DRender/private/qshaderlanguage_p.h> + +using namespace Qt3DRender; + +using QBufferPointer = QSharedPointer<QBuffer>; +Q_DECLARE_METATYPE(QBufferPointer); + +using PrototypeHash = QHash<QString, QShaderNode>; +Q_DECLARE_METATYPE(PrototypeHash); + +namespace +{ + QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly) + { + auto buffer = QBufferPointer::create(); + buffer->setData(data); + if (openMode != QIODevice::NotOpen) + buffer->open(openMode); + return buffer; + } + + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports, const QStringList &layers = QStringList()) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + node.setLayers(layers); + for (const auto &port : ports) + node.addPort(port); + return node; + } + + QShaderGraph::Edge createEdge(const QUuid &sourceUuid, const QString &sourceName, + const QUuid &targetUuid, const QString &targetName, + const QStringList &layers = QStringList()) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + edge.layers = layers; + return edge; + } + + QShaderGraph createGraph() + { + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto graph = QShaderGraph(); + + auto worldPosition = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + worldPosition.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); + worldPosition.setParameter("name", "worldPosition"); + worldPosition.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Input)); + worldPosition.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3)); + worldPosition.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + worldPosition.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + + auto lightIntensity = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + lightIntensity.setUuid(QUuid("{00000000-0000-0000-0000-000000000004}")); + lightIntensity.setParameter("name", "defaultName"); + lightIntensity.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + lightIntensity.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); + lightIntensity.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + lightIntensity.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.setUuid(QUuid("{00000000-0000-0000-0000-000000000005}")); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.setUuid(QUuid("{00000000-0000-0000-0000-000000000006}")); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.setUuid(QUuid("{00000000-0000-0000-0000-000000000007}")); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); + + auto lightFunction = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000008}")); + lightFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.setUuid(QUuid("{00000000-0000-0000-0000-000000000009}")); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + + graph.addNode(worldPosition); + graph.addNode(texture); + graph.addNode(texCoord); + graph.addNode(lightIntensity); + graph.addNode(exposure); + graph.addNode(fragColor); + graph.addNode(sampleTexture); + graph.addNode(lightFunction); + graph.addNode(exposureFunction); + + graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler")); + graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord")); + + graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position")); + graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor")); + graph.addEdge(createEdge(lightIntensity.uuid(), "value", lightFunction.uuid(), "lightIntensity")); + + graph.addEdge(createEdge(lightFunction.uuid(), "outputColor", exposureFunction.uuid(), "inputColor")); + graph.addEdge(createEdge(exposure.uuid(), "exposure", exposureFunction.uuid(), "exposure")); + + graph.addEdge(createEdge(exposureFunction.uuid(), "outputColor", fragColor.uuid(), "fragColor")); + + return graph; + } + + void debugStatement(const QString &prefix, const QShaderGraph::Statement &statement) + { + qDebug() << prefix << statement.inputs << statement.uuid().toString() << statement.outputs; + } + + void dumpStatementsIfNeeded(const QVector<QShaderGraph::Statement> &statements, const QVector<QShaderGraph::Statement> &expected) + { + if (statements != expected) { + for (int i = 0; i < qMax(statements.size(), expected.size()); i++) { + qDebug() << "----" << i << "----"; + if (i < statements.size()) + debugStatement("A:", statements.at(i)); + if (i < expected.size()) + debugStatement("E:", expected.at(i)); + qDebug() << "-----------"; + } + } + } +} + +class tst_QShaderGraphLoader : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateLoaderMembers(); + void shouldLoadFromJsonStream_data(); + void shouldLoadFromJsonStream(); +}; + +void tst_QShaderGraphLoader::shouldManipulateLoaderMembers() +{ + // GIVEN + auto loader = QShaderGraphLoader(); + + // THEN (default state) + QCOMPARE(loader.status(), QShaderGraphLoader::Null); + QVERIFY(!loader.device()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + QVERIFY(loader.prototypes().isEmpty()); + + // WHEN + auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen); + loader.setDevice(device1.data()); + + // THEN + QCOMPARE(loader.status(), QShaderGraphLoader::Error); + QCOMPARE(loader.device(), device1.data()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + + // WHEN + auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly); + loader.setDevice(device2.data()); + + // THEN + QCOMPARE(loader.status(), QShaderGraphLoader::Waiting); + QCOMPARE(loader.device(), device2.data()); + QVERIFY(loader.graph().nodes().isEmpty()); + QVERIFY(loader.graph().edges().isEmpty()); + + + // WHEN + const auto prototypes = [this]{ + auto res = QHash<QString, QShaderNode>(); + res.insert("foo", createNode({})); + return res; + }(); + loader.setPrototypes(prototypes); + + // THEN + QCOMPARE(loader.prototypes().size(), prototypes.size()); + QVERIFY(loader.prototypes().contains("foo")); + QCOMPARE(loader.prototypes().value("foo").uuid(), prototypes.value("foo").uuid()); +} + +void tst_QShaderGraphLoader::shouldLoadFromJsonStream_data() +{ + QTest::addColumn<QBufferPointer>("device"); + QTest::addColumn<PrototypeHash>("prototypes"); + QTest::addColumn<QShaderGraph>("graph"); + QTest::addColumn<QShaderGraphLoader::Status>("status"); + + QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << PrototypeHash() + << QShaderGraph() << QShaderGraphLoader::Error; + + const auto smallJson = "{" + " \"nodes\": [" + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"type\": \"MyInput\"," + " \"layers\": [\"foo\", \"bar\"]" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"type\": \"MyOutput\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"type\": \"MyFunction\"" + " }" + " ]," + " \"edges\": [" + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"sourcePort\": \"input\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"targetPort\": \"functionInput\"," + " \"layers\": [\"bar\", \"baz\"]" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"sourcePort\": \"functionOutput\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"targetPort\": \"output\"" + " }" + " ]" + "}"; + + const auto smallProtos = [this]{ + auto protos = PrototypeHash(); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }); + protos.insert("MyInput", input); + + auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + protos.insert("MyOutput", output); + + auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "functionOutput") + }); + protos.insert("MyFunction", function); + return protos; + }(); + + const auto smallGraph = [this]{ + auto graph = QShaderGraph(); + + auto input = createNode({ + createPort(QShaderNodePort::Output, "input") + }, {"foo", "bar"}); + input.setUuid(QUuid("{00000000-0000-0000-0000-000000000001}")); + auto output = createNode({ + createPort(QShaderNodePort::Input, "output") + }); + output.setUuid(QUuid("{00000000-0000-0000-0000-000000000002}")); + auto function = createNode({ + createPort(QShaderNodePort::Input, "functionInput"), + createPort(QShaderNodePort::Output, "functionOutput") + }); + function.setUuid(QUuid("{00000000-0000-0000-0000-000000000003}")); + + graph.addNode(input); + graph.addNode(output); + graph.addNode(function); + graph.addEdge(createEdge(input.uuid(), "input", function.uuid(), "functionInput", {"bar", "baz"})); + graph.addEdge(createEdge(function.uuid(), "functionOutput", output.uuid(), "output")); + + return graph; + }(); + + QTest::newRow("TwoNodesOneEdge") << createBuffer(smallJson) << smallProtos << smallGraph << QShaderGraphLoader::Ready; + QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << smallProtos << QShaderGraph() << QShaderGraphLoader::Error; + QTest::newRow("NoPrototype") << createBuffer(smallJson) << PrototypeHash() << QShaderGraph() << QShaderGraphLoader::Error; + + const auto complexJson = "{" + " \"nodes\": [" + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"type\": \"inputValue\"," + " \"parameters\": {" + " \"name\": \"worldPosition\"," + " \"qualifier\": {" + " \"type\": \"QShaderLanguage::StorageQualifier\"," + " \"value\": \"QShaderLanguage::Input\"" + " }," + " \"type\": {" + " \"type\": \"QShaderLanguage::VariableType\"," + " \"value\": \"QShaderLanguage::Vec3\"" + " }" + " }" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"type\": \"texture\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"type\": \"texCoord\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000004}\"," + " \"type\": \"inputValue\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000005}\"," + " \"type\": \"exposure\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000006}\"," + " \"type\": \"fragColor\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"type\": \"sampleTexture\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"type\": \"lightModel\"" + " }," + " {" + " \"uuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"type\": \"exposureFunction\"" + " }" + " ]," + " \"edges\": [" + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000002}\"," + " \"sourcePort\": \"texture\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"targetPort\": \"sampler\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000003}\"," + " \"sourcePort\": \"texCoord\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"targetPort\": \"coord\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000001}\"," + " \"sourcePort\": \"value\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"position\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000007}\"," + " \"sourcePort\": \"color\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"baseColor\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000004}\"," + " \"sourcePort\": \"value\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"targetPort\": \"lightIntensity\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000008}\"," + " \"sourcePort\": \"outputColor\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"targetPort\": \"inputColor\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000005}\"," + " \"sourcePort\": \"exposure\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"targetPort\": \"exposure\"" + " }," + " {" + " \"sourceUuid\": \"{00000000-0000-0000-0000-000000000009}\"," + " \"sourcePort\": \"outputColor\"," + " \"targetUuid\": \"{00000000-0000-0000-0000-000000000006}\"," + " \"targetPort\": \"fragColor\"" + " }" + " ]" + "}"; + + const auto complexProtos = [this]{ + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + auto protos = PrototypeHash(); + + auto inputValue = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + inputValue.setParameter("name", "defaultName"); + inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Float)); + inputValue.addRule(openGLES2, QShaderNode::Rule("highp $type $value = $name;", + QByteArrayList() << "$qualifier highp $type $name;")); + inputValue.addRule(openGL3, QShaderNode::Rule("$type $value = $name;", + QByteArrayList() << "$qualifier $type $name;")); + protos.insert("inputValue", inputValue); + + auto texture = createNode({ + createPort(QShaderNodePort::Output, "texture") + }); + texture.addRule(openGLES2, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + texture.addRule(openGL3, QShaderNode::Rule("sampler2D $texture = texture;", + QByteArrayList() << "uniform sampler2D texture;")); + protos.insert("texture", texture); + + auto texCoord = createNode({ + createPort(QShaderNodePort::Output, "texCoord") + }); + texCoord.addRule(openGLES2, QShaderNode::Rule("highp vec2 $texCoord = texCoord;", + QByteArrayList() << "varying highp vec2 texCoord;")); + texCoord.addRule(openGL3, QShaderNode::Rule("vec2 $texCoord = texCoord;", + QByteArrayList() << "in vec2 texCoord;")); + protos.insert("texCoord", texCoord); + + auto exposure = createNode({ + createPort(QShaderNodePort::Output, "exposure") + }); + exposure.addRule(openGLES2, QShaderNode::Rule("highp float $exposure = exposure;", + QByteArrayList() << "uniform highp float exposure;")); + exposure.addRule(openGL3, QShaderNode::Rule("float $exposure = exposure;", + QByteArrayList() << "uniform float exposure;")); + protos.insert("exposure", exposure); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL3, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + protos.insert("fragColor", fragColor); + + auto sampleTexture = createNode({ + createPort(QShaderNodePort::Input, "sampler"), + createPort(QShaderNodePort::Input, "coord"), + createPort(QShaderNodePort::Output, "color") + }); + sampleTexture.addRule(openGLES2, QShaderNode::Rule("highp vec4 $color = texture2D($sampler, $coord);")); + sampleTexture.addRule(openGL3, QShaderNode::Rule("vec4 $color = texture2D($sampler, $coord);")); + protos.insert("sampleTexture", sampleTexture); + + auto lightModel = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightModel.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + protos.insert("lightModel", lightModel); + + auto exposureFunction = createNode({ + createPort(QShaderNodePort::Input, "inputColor"), + createPort(QShaderNodePort::Input, "exposure"), + createPort(QShaderNodePort::Output, "outputColor") + }); + exposureFunction.addRule(openGLES2, QShaderNode::Rule("highp vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + exposureFunction.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = $inputColor * pow(2.0, $exposure);")); + protos.insert("exposureFunction", exposureFunction); + + return protos; + }(); + + const auto complexGraph = createGraph(); + + QTest::newRow("ComplexGraph") << createBuffer(complexJson) << complexProtos << complexGraph << QShaderGraphLoader::Ready; +} + +void tst_QShaderGraphLoader::shouldLoadFromJsonStream() +{ + // GIVEN + QFETCH(QBufferPointer, device); + QFETCH(PrototypeHash, prototypes); + + auto loader = QShaderGraphLoader(); + + // WHEN + loader.setPrototypes(prototypes); + loader.setDevice(device.data()); + loader.load(); + + // THEN + QFETCH(QShaderGraphLoader::Status, status); + QCOMPARE(loader.status(), status); + + QFETCH(QShaderGraph, graph); + const auto statements = loader.graph().createStatements({"foo", "bar", "baz"}); + const auto expected = graph.createStatements({"foo", "bar", "baz"}); + dumpStatementsIfNeeded(statements, expected); + QCOMPARE(statements, expected); + + const auto sortedParameters = [](const QShaderNode &node) { + auto res = node.parameterNames(); + res.sort(); + return res; + }; + + for (int i = 0; i < statements.size(); i++) { + const auto actualNode = statements.at(i).node; + const auto expectedNode = expected.at(i).node; + + QCOMPARE(actualNode.layers(), expectedNode.layers()); + QCOMPARE(actualNode.ports(), expectedNode.ports()); + QCOMPARE(sortedParameters(actualNode), sortedParameters(expectedNode)); + for (const auto &name : expectedNode.parameterNames()) { + QCOMPARE(actualNode.parameter(name), expectedNode.parameter(name)); + } + QCOMPARE(actualNode.availableFormats(), expectedNode.availableFormats()); + for (const auto &format : expectedNode.availableFormats()) { + QCOMPARE(actualNode.rule(format), expectedNode.rule(format)); + } + } +} + +QTEST_MAIN(tst_QShaderGraphLoader) + +#include "tst_qshadergraphloader.moc" diff --git a/tests/auto/render/shadergraph/qshadernodes/qshadernodes.pro b/tests/auto/render/shadergraph/qshadernodes/qshadernodes.pro new file mode 100644 index 000000000..754cde9e1 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadernodes/qshadernodes.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib 3drender-private + +SOURCES += tst_qshadernodes.cpp +TARGET = tst_qshadernodes diff --git a/tests/auto/render/shadergraph/qshadernodes/tst_qshadernodes.cpp b/tests/auto/render/shadergraph/qshadernodes/tst_qshadernodes.cpp new file mode 100644 index 000000000..2cd2ff90d --- /dev/null +++ b/tests/auto/render/shadergraph/qshadernodes/tst_qshadernodes.cpp @@ -0,0 +1,549 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <Qt3DRender/private/qshaderformat_p.h> +#include <Qt3DRender/private/qshadernode_p.h> +#include <Qt3DRender/private/qshadernodeport_p.h> + +using namespace Qt3DRender; +namespace +{ + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, + const QStringList &extensions = QStringList(), + const QString &vendor = QString()) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + format.setExtensions(extensions); + format.setVendor(vendor); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction direction, const QString &name) + { + auto port = QShaderNodePort(); + port.direction = direction; + port.name = name; + return port; + } +} + +class tst_QShaderNodes : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateFormatMembers(); + void shouldVerifyFormatsEquality_data(); + void shouldVerifyFormatsEquality(); + void shouldVerifyFormatsCompatibilities_data(); + void shouldVerifyFormatsCompatibilities(); + + void shouldHaveDefaultPortState(); + void shouldVerifyPortsEquality_data(); + void shouldVerifyPortsEquality(); + + void shouldManipulateNodeMembers(); + void shouldHandleNodeRulesSupportAndOrder(); +}; + +void tst_QShaderNodes::shouldManipulateFormatMembers() +{ + // GIVEN + auto format = QShaderFormat(); + + // THEN (default state) + QCOMPARE(format.api(), QShaderFormat::NoApi); + QCOMPARE(format.version().majorVersion(), 0); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(!format.isValid()); + + // WHEN + format.setApi(QShaderFormat::OpenGLES); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 0); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(!format.isValid()); + + // WHEN + format.setVersion(QVersionNumber(3)); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 0); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setVersion(QVersionNumber(3, 2)); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList()); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setExtensions({"foo", "bar"}); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList({"bar", "foo"})); + QCOMPARE(format.vendor(), QString()); + QVERIFY(format.isValid()); + + // WHEN + format.setVendor(QStringLiteral("KDAB")); + + // THEN + QCOMPARE(format.api(), QShaderFormat::OpenGLES); + QCOMPARE(format.version().majorVersion(), 3); + QCOMPARE(format.version().minorVersion(), 2); + QCOMPARE(format.extensions(), QStringList({"bar", "foo"})); + QCOMPARE(format.vendor(), QStringLiteral("KDAB")); + QVERIFY(format.isValid()); +} + +void tst_QShaderNodes::shouldVerifyFormatsEquality_data() +{ + QTest::addColumn<QShaderFormat>("left"); + QTest::addColumn<QShaderFormat>("right"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Equals") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << true; + QTest::newRow("Apis") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLNoProfile, 3, 0, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Major") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Minor") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1, {"foo", "bar"}, "KDAB") + << false; + QTest::newRow("Extensions") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}, "KDAB") + << false; + QTest::newRow("Vendor") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << false; +} + +void tst_QShaderNodes::shouldVerifyFormatsEquality() +{ + // GIVEN + QFETCH(QShaderFormat, left); + QFETCH(QShaderFormat, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderNodes::shouldVerifyFormatsCompatibilities_data() +{ + QTest::addColumn<QShaderFormat>("reference"); + QTest::addColumn<QShaderFormat>("tested"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("NoProfileVsES") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << true; + QTest::newRow("CoreProfileVsES") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << false; + QTest::newRow("CompatProfileVsES") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLES, 2, 0) + << true; + + QTest::newRow("ESVsNoProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << false; + QTest::newRow("ESVsCoreProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << false; + QTest::newRow("ESVsCompatProfile") << createFormat(QShaderFormat::OpenGLES, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << false; + + QTest::newRow("CoreVsNoProfile") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << false; + QTest::newRow("CoreVsCompat") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << false; + QTest::newRow("CoreVsCore") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + + QTest::newRow("NoProfileVsCore") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("NoProvileVsCompat") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << true; + QTest::newRow("NoProfileVsNoProfile") << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << true; + + QTest::newRow("CompatVsCore") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("CompatVsCompat") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << true; + QTest::newRow("CompatVsNoProfile") << createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLNoProfile, 2, 0) + << true; + + QTest::newRow("MajorForwardCompat_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << true; + QTest::newRow("MajorForwardCompat_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 4) + << true; + QTest::newRow("MajorForwardCompat_3") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << false; + QTest::newRow("MajorForwardCompat_4") << createFormat(QShaderFormat::OpenGLCoreProfile, 2, 4) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << false; + + QTest::newRow("MinorForwardCompat_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << true; + QTest::newRow("MinorForwardCompat_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 1) + << false; + + QTest::newRow("Extensions_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}) + << true; + QTest::newRow("Extensions_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo"}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {"foo", "bar"}) + << false; + + QTest::newRow("Vendor_1") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}) + << true; + QTest::newRow("Vendor_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}) + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << false; + QTest::newRow("Vendor_2") << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0, {}, "KDAB") + << true; +} + +void tst_QShaderNodes::shouldVerifyFormatsCompatibilities() +{ + // GIVEN + QFETCH(QShaderFormat, reference); + QFETCH(QShaderFormat, tested); + + // WHEN + const auto supported = reference.supports(tested); + + // THEN + QFETCH(bool, expected); + QCOMPARE(supported, expected); +} + +void tst_QShaderNodes::shouldHaveDefaultPortState() +{ + // GIVEN + auto port = QShaderNodePort(); + + // THEN + QCOMPARE(port.direction, QShaderNodePort::Output); + QVERIFY(port.name.isEmpty()); +} + +void tst_QShaderNodes::shouldVerifyPortsEquality_data() +{ + QTest::addColumn<QShaderNodePort>("left"); + QTest::addColumn<QShaderNodePort>("right"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("Equals") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Input, "foo") + << true; + QTest::newRow("Direction") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Output, "foo") + << false; + QTest::newRow("Name") << createPort(QShaderNodePort::Input, "foo") + << createPort(QShaderNodePort::Input, "bar") + << false; +} + +void tst_QShaderNodes::shouldVerifyPortsEquality() +{ + // GIVEN + QFETCH(QShaderNodePort, left); + QFETCH(QShaderNodePort, right); + + // WHEN + const auto equal = (left == right); + const auto notEqual = (left != right); + + // THEN + QFETCH(bool, expected); + QCOMPARE(equal, expected); + QCOMPARE(notEqual, !expected); +} + +void tst_QShaderNodes::shouldManipulateNodeMembers() +{ + // GIVEN + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + + const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"}); + const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + + auto node = QShaderNode(); + + // THEN (default state) + QCOMPARE(node.type(), QShaderNode::Invalid); + QVERIFY(node.uuid().isNull()); + QVERIFY(node.layers().isEmpty()); + QVERIFY(node.ports().isEmpty()); + QVERIFY(node.parameterNames().isEmpty()); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + const auto uuid = QUuid::createUuid(); + node.setUuid(uuid); + + // THEN + QCOMPARE(node.uuid(), uuid); + + // WHEN + node.setLayers({"foo", "bar"}); + + // THEN + QCOMPARE(node.layers(), QStringList({"foo", "bar"})); + + // WHEN + auto firstPort = QShaderNodePort(); + firstPort.direction = QShaderNodePort::Input; + firstPort.name = QStringLiteral("foo"); + node.addPort(firstPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Output); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), firstPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + auto secondPort = QShaderNodePort(); + secondPort.direction = QShaderNodePort::Output; + secondPort.name = QStringLiteral("bar"); + node.addPort(secondPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Function); + QCOMPARE(node.ports().size(), 2); + QCOMPARE(node.ports().at(0), firstPort); + QCOMPARE(node.ports().at(1), secondPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.removePort(firstPort); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.setParameter(QStringLiteral("baz"), 42); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + auto parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 1); + QCOMPARE(parameterNames.at(0), QStringLiteral("baz")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42)); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.setParameter(QStringLiteral("bleh"), QStringLiteral("value")); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 2); + QCOMPARE(parameterNames.at(0), QStringLiteral("baz")); + QCOMPARE(parameterNames.at(1), QStringLiteral("bleh")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant(42)); + QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value"))); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.clearParameter(QStringLiteral("baz")); + + // THEN + QCOMPARE(node.type(), QShaderNode::Input); + QCOMPARE(node.ports().size(), 1); + QCOMPARE(node.ports().at(0), secondPort); + parameterNames = node.parameterNames(); + parameterNames.sort(); + QCOMPARE(parameterNames.size(), 1); + QCOMPARE(parameterNames.at(0), QStringLiteral("bleh")); + QCOMPARE(node.parameter(QStringLiteral("baz")), QVariant()); + QCOMPARE(node.parameter(QStringLiteral("bleh")), QVariant(QStringLiteral("value"))); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + node.addRule(openGLES2, es2Rule); + node.addRule(openGL3, gl3Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.removeRule(openGLES2); + + // THEN + QCOMPARE(node.availableFormats().size(), 1); + QCOMPARE(node.availableFormats().at(0), openGL3); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.addRule(openGLES2, es2Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGL3); + QCOMPARE(node.availableFormats().at(1), openGLES2); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + + // WHEN + node.addRule(openGL3, gl3bisRule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3bisRule); +} + +void tst_QShaderNodes::shouldHandleNodeRulesSupportAndOrder() +{ + // GIVEN + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 0); + const auto openGL32 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 2); + const auto openGL4 = createFormat(QShaderFormat::OpenGLCoreProfile, 4, 0); + + const auto es2Rule = QShaderNode::Rule(QByteArrayLiteral("gles2"), {"#pragma include es2/foo.inc", "#pragma include es2/bar.inc"}); + const auto gl3Rule = QShaderNode::Rule(QByteArrayLiteral("gl3"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + const auto gl32Rule = QShaderNode::Rule(QByteArrayLiteral("gl32"), {"#pragma include gl32/foo.inc", "#pragma include gl32/bar.inc"}); + const auto gl3bisRule = QShaderNode::Rule(QByteArrayLiteral("gl3bis"), {"#pragma include gl3/foo.inc", "#pragma include gl3/bar.inc"}); + + auto node = QShaderNode(); + + // WHEN + node.addRule(openGLES2, es2Rule); + node.addRule(openGL3, gl3Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 2); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + QCOMPARE(node.rule(openGL32), gl3Rule); + QCOMPARE(node.rule(openGL4), gl3Rule); + + // WHEN + node.addRule(openGL32, gl32Rule); + + // THEN + QCOMPARE(node.availableFormats().size(), 3); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL3); + QCOMPARE(node.availableFormats().at(2), openGL32); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3Rule); + QCOMPARE(node.rule(openGL32), gl32Rule); + QCOMPARE(node.rule(openGL4), gl32Rule); + + // WHEN + node.addRule(openGL3, gl3bisRule); + + // THEN + QCOMPARE(node.availableFormats().size(), 3); + QCOMPARE(node.availableFormats().at(0), openGLES2); + QCOMPARE(node.availableFormats().at(1), openGL32); + QCOMPARE(node.availableFormats().at(2), openGL3); + QCOMPARE(node.rule(openGLES2), es2Rule); + QCOMPARE(node.rule(openGL3), gl3bisRule); + QCOMPARE(node.rule(openGL32), gl3bisRule); + QCOMPARE(node.rule(openGL4), gl3bisRule); +} + +QTEST_MAIN(tst_QShaderNodes) + +#include "tst_qshadernodes.moc" diff --git a/tests/auto/render/shadergraph/qshadernodesloader/qshadernodesloader.pro b/tests/auto/render/shadergraph/qshadernodesloader/qshadernodesloader.pro new file mode 100644 index 000000000..7da336b5e --- /dev/null +++ b/tests/auto/render/shadergraph/qshadernodesloader/qshadernodesloader.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib 3drender-private + +SOURCES += tst_qshadernodesloader.cpp +TARGET = tst_qshadernodesloader diff --git a/tests/auto/render/shadergraph/qshadernodesloader/tst_qshadernodesloader.cpp b/tests/auto/render/shadergraph/qshadernodesloader/tst_qshadernodesloader.cpp new file mode 100644 index 000000000..27cf7f425 --- /dev/null +++ b/tests/auto/render/shadergraph/qshadernodesloader/tst_qshadernodesloader.cpp @@ -0,0 +1,325 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <QtCore/qbuffer.h> + +#include <Qt3DRender/private/qshadernodesloader_p.h> +#include <Qt3DRender/private/qshaderlanguage_p.h> + +using namespace Qt3DRender; + +using QBufferPointer = QSharedPointer<QBuffer>; +Q_DECLARE_METATYPE(QBufferPointer); + +using NodeHash = QHash<QString, QShaderNode>; +Q_DECLARE_METATYPE(NodeHash); + +namespace +{ + QBufferPointer createBuffer(const QByteArray &data, QIODevice::OpenMode openMode = QIODevice::ReadOnly) + { + auto buffer = QBufferPointer::create(); + buffer->setData(data); + if (openMode != QIODevice::NotOpen) + buffer->open(openMode); + return buffer; + } + + QShaderFormat createFormat(QShaderFormat::Api api, int majorVersion, int minorVersion, + const QStringList &extensions = QStringList(), + const QString &vendor = QString()) + { + auto format = QShaderFormat(); + format.setApi(api); + format.setVersion(QVersionNumber(majorVersion, minorVersion)); + format.setExtensions(extensions); + format.setVendor(vendor); + return format; + } + + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector<QShaderNodePort> &ports) + { + auto node = QShaderNode(); + for (const auto &port : ports) + node.addPort(port); + return node; + } +} + +class tst_QShaderNodesLoader : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateLoaderMembers(); + void shouldLoadFromJsonStream_data(); + void shouldLoadFromJsonStream(); +}; + +void tst_QShaderNodesLoader::shouldManipulateLoaderMembers() +{ + // GIVEN + auto loader = QShaderNodesLoader(); + + // THEN (default state) + QCOMPARE(loader.status(), QShaderNodesLoader::Null); + QVERIFY(!loader.device()); + QVERIFY(loader.nodes().isEmpty()); + + // WHEN + auto device1 = createBuffer(QByteArray("..........."), QIODevice::NotOpen); + loader.setDevice(device1.data()); + + // THEN + QCOMPARE(loader.status(), QShaderNodesLoader::Error); + QCOMPARE(loader.device(), device1.data()); + QVERIFY(loader.nodes().isEmpty()); + + // WHEN + auto device2 = createBuffer(QByteArray("..........."), QIODevice::ReadOnly); + loader.setDevice(device2.data()); + + // THEN + QCOMPARE(loader.status(), QShaderNodesLoader::Waiting); + QCOMPARE(loader.device(), device2.data()); + QVERIFY(loader.nodes().isEmpty()); +} + +void tst_QShaderNodesLoader::shouldLoadFromJsonStream_data() +{ + QTest::addColumn<QBufferPointer>("device"); + QTest::addColumn<NodeHash>("nodes"); + QTest::addColumn<QShaderNodesLoader::Status>("status"); + + QTest::newRow("empty") << createBuffer("", QIODevice::ReadOnly) << NodeHash() << QShaderNodesLoader::Error; + + const auto smallJson = "{" + " \"inputValue\": {" + " \"outputs\": [" + " \"value\"" + " ]," + " \"parameters\": {" + " \"name\": \"defaultName\"," + " \"qualifier\": {" + " \"type\": \"QShaderLanguage::StorageQualifier\"," + " \"value\": \"QShaderLanguage::Uniform\"" + " }," + " \"type\": {" + " \"type\": \"QShaderLanguage::VariableType\"," + " \"value\": \"QShaderLanguage::Vec3\"" + " }," + " \"defaultValue\": {" + " \"type\": \"float\"," + " \"value\": \"1.25\"" + " }" + " }," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0" + " }," + " \"substitution\": \"highp vec3 $value = $name;\"," + " \"headerSnippets\": [ \"varying highp vec3 $name;\" ]" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLCompatibilityProfile\"," + " \"major\": 2," + " \"minor\": 1" + " }," + " \"substitution\": \"vec3 $value = $name;\"," + " \"headerSnippets\": [ \"in vec3 $name;\" ]" + " }" + " ]" + " }," + " \"fragColor\": {" + " \"inputs\": [" + " \"fragColor\"" + " ]," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0" + " }," + " \"substitution\": \"gl_fragColor = $fragColor;\"" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLNoProfile\"," + " \"major\": 4," + " \"minor\": 0" + " }," + " \"substitution\": \"fragColor = $fragColor;\"," + " \"headerSnippets\": [ \"out vec4 fragColor;\" ]" + " }" + " ]" + " }," + " \"lightModel\": {" + " \"inputs\": [" + " \"baseColor\"," + " \"position\"," + " \"lightIntensity\"" + " ]," + " \"outputs\": [" + " \"outputColor\"" + " ]," + " \"rules\": [" + " {" + " \"format\": {" + " \"api\": \"OpenGLES\"," + " \"major\": 2," + " \"minor\": 0," + " \"extensions\": [ \"ext1\", \"ext2\" ]," + " \"vendor\": \"kdab\"" + " }," + " \"substitution\": \"highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\"," + " \"headerSnippets\": [ \"#pragma include es2/lightmodel.frag.inc\" ]" + " }," + " {" + " \"format\": {" + " \"api\": \"OpenGLCoreProfile\"," + " \"major\": 3," + " \"minor\": 3" + " }," + " \"substitution\": \"vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);\"," + " \"headerSnippets\": [ \"#pragma include gl3/lightmodel.frag.inc\" ]" + " }" + " ]" + " }" + "}"; + + const auto smallProtos = [this]{ + const auto openGLES2 = createFormat(QShaderFormat::OpenGLES, 2, 0); + const auto openGLES2Extended = createFormat(QShaderFormat::OpenGLES, 2, 0, {"ext1", "ext2"}, "kdab"); + const auto openGL2 = createFormat(QShaderFormat::OpenGLCompatibilityProfile, 2, 1); + const auto openGL3 = createFormat(QShaderFormat::OpenGLCoreProfile, 3, 3); + const auto openGL4 = createFormat(QShaderFormat::OpenGLNoProfile, 4, 0); + + auto protos = NodeHash(); + + auto inputValue = createNode({ + createPort(QShaderNodePort::Output, "value") + }); + inputValue.setParameter("name", "defaultName"); + inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform)); + inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3)); + inputValue.setParameter("defaultValue", QVariant(1.25f)); + inputValue.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", + QByteArrayList() << "varying highp vec3 $name;")); + inputValue.addRule(openGL2, QShaderNode::Rule("vec3 $value = $name;", + QByteArrayList() << "in vec3 $name;")); + protos.insert("inputValue", inputValue); + + auto fragColor = createNode({ + createPort(QShaderNodePort::Input, "fragColor") + }); + fragColor.addRule(openGLES2, QShaderNode::Rule("gl_fragColor = $fragColor;")); + fragColor.addRule(openGL4, QShaderNode::Rule("fragColor = $fragColor;", + QByteArrayList() << "out vec4 fragColor;")); + protos.insert(QStringLiteral("fragColor"), fragColor); + + auto lightModel = createNode({ + createPort(QShaderNodePort::Input, "baseColor"), + createPort(QShaderNodePort::Input, "position"), + createPort(QShaderNodePort::Input, "lightIntensity"), + createPort(QShaderNodePort::Output, "outputColor") + }); + lightModel.addRule(openGLES2Extended, QShaderNode::Rule("highp vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include es2/lightmodel.frag.inc")); + lightModel.addRule(openGL3, QShaderNode::Rule("vec4 $outputColor = lightModel($baseColor, $position, $lightIntensity);", + QByteArrayList() << "#pragma include gl3/lightmodel.frag.inc")); + protos.insert("lightModel", lightModel); + + return protos; + }(); + + QTest::newRow("NotOpen") << createBuffer(smallJson, QIODevice::NotOpen) << NodeHash() << QShaderNodesLoader::Error; + QTest::newRow("CorrectJSON") << createBuffer(smallJson) << smallProtos << QShaderNodesLoader::Ready; +} + +void tst_QShaderNodesLoader::shouldLoadFromJsonStream() +{ + // GIVEN + QFETCH(QBufferPointer, device); + + auto loader = QShaderNodesLoader(); + + // WHEN + loader.setDevice(device.data()); + loader.load(); + + // THEN + QFETCH(QShaderNodesLoader::Status, status); + QCOMPARE(loader.status(), status); + + QFETCH(NodeHash, nodes); + const auto sortedKeys = [](const NodeHash &nodes) { + auto res = nodes.keys(); + res.sort(); + return res; + }; + const auto sortedParameters = [](const QShaderNode &node) { + auto res = node.parameterNames(); + res.sort(); + return res; + }; + QCOMPARE(sortedKeys(loader.nodes()), sortedKeys(nodes)); + for (const auto &key : nodes.keys()) { + const auto actual = loader.nodes().value(key); + const auto expected = nodes.value(key); + + QVERIFY(actual.uuid().isNull()); + QCOMPARE(actual.ports(), expected.ports()); + QCOMPARE(sortedParameters(actual), sortedParameters(expected)); + for (const auto &name : expected.parameterNames()) { + QCOMPARE(actual.parameter(name), expected.parameter(name)); + } + QCOMPARE(actual.availableFormats(), expected.availableFormats()); + for (const auto &format : expected.availableFormats()) { + QCOMPARE(actual.rule(format), expected.rule(format)); + } + } +} + +QTEST_MAIN(tst_QShaderNodesLoader) + +#include "tst_qshadernodesloader.moc" diff --git a/tests/auto/render/shadergraph/shadergraph.pro b/tests/auto/render/shadergraph/shadergraph.pro new file mode 100644 index 000000000..653a9a694 --- /dev/null +++ b/tests/auto/render/shadergraph/shadergraph.pro @@ -0,0 +1,10 @@ +TEMPLATE = subdirs + +qtConfig(private_tests) { + SUBDIRS += \ + qshadergenerator \ + qshadergraph \ + qshadergraphloader \ + qshadernodes \ + qshadernodesloader +} diff --git a/tests/benchmarks/render/opengl/opengl.pro b/tests/benchmarks/render/opengl/opengl.pro new file mode 100644 index 000000000..4fdf80256 --- /dev/null +++ b/tests/benchmarks/render/opengl/opengl.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + shaderparameterpack diff --git a/tests/benchmarks/render/opengl/opengl_render_plugin.pri b/tests/benchmarks/render/opengl/opengl_render_plugin.pri new file mode 100644 index 000000000..50fade878 --- /dev/null +++ b/tests/benchmarks/render/opengl/opengl_render_plugin.pri @@ -0,0 +1,18 @@ +# Found no way of having the test runner include the correct +# library path as runtime + +#PLUGIN_SRC_PATH = $$PWD/../../../../src/plugins/renderers/opengl + +#INCLUDEPATH += \ +# $$PLUGIN_SRC_PATH/graphicshelpers \ +# $$PLUGIN_SRC_PATH/io \ +# $$PLUGIN_SRC_PATH/jobs \ +# $$PLUGIN_SRC_PATH/managers \ +# $$PLUGIN_SRC_PATH/renderer \ +# $$PLUGIN_SRC_PATH/renderstates \ +# $$PLUGIN_SRC_PATH/textures + +#LIBS += -L$$[QT_INSTALL_PLUGINS]/renderers/ -lopenglrenderer + +# Instead we link against the sources directly +include(../../../../src/plugins/renderers/opengl/opengl.pri) diff --git a/tests/benchmarks/render/opengl/shaderparameterpack/shaderparameterpack.pro b/tests/benchmarks/render/opengl/shaderparameterpack/shaderparameterpack.pro new file mode 100644 index 000000000..4943a5e91 --- /dev/null +++ b/tests/benchmarks/render/opengl/shaderparameterpack/shaderparameterpack.pro @@ -0,0 +1,17 @@ +TEMPLATE = app + +TARGET = tst_bench_shaderparameterpack + +QT += core-private 3dcore 3dcore-private 3drender 3drender-private testlib + +CONFIG += testcase + +SOURCES += tst_bench_shaderparameterpack.cpp + +include(../../../../auto/render/commons/commons.pri) + +# Needed to use the TestAspect +DEFINES += QT_BUILD_INTERNAL + +# Link Against OpenGL Renderer Plugin +include(../opengl_render_plugin.pri) diff --git a/tests/benchmarks/render/opengl/shaderparameterpack/tst_bench_shaderparameterpack.cpp b/tests/benchmarks/render/opengl/shaderparameterpack/tst_bench_shaderparameterpack.cpp new file mode 100644 index 000000000..d992e03af --- /dev/null +++ b/tests/benchmarks/render/opengl/shaderparameterpack/tst_bench_shaderparameterpack.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QTest> +#include <Qt3DRender/private/uniform_p.h> +#include <shaderparameterpack_p.h> +#include <glshader_p.h> +#include <shadervariables_p.h> +#include <QRandomGenerator> + +using namespace Qt3DRender::Render; +using namespace Qt3DRender::Render::OpenGL; + + +class tst_BenchShaderParameterPack : public QObject +{ + Q_OBJECT +private Q_SLOTS: + + void checkPackUniformInsert() + { + // GIVEN + PackUniformHash pack; + + QVector<int> randKeys(64); + QRandomGenerator gen; + + for (int i = 0; i < 64; ++i) + randKeys[i] = gen.generate(); + + QBENCHMARK { + for (const int key : qAsConst(randKeys)) + pack.insert(key, UniformValue(key)); + } + } + + void prepareUniforms() + { + // GIVEN + GLShader shader; + QVector<ShaderUniform> uniformDescriptions; + for (int i = 0; i < 30; i++) { + ShaderUniform u; + u.m_name = QString::number(i); + uniformDescriptions << u; + } + shader.initializeUniforms(uniformDescriptions); + + // THEN + QCOMPARE(shader.uniforms().size(), 30); + + // WHEN + QVector<int> testNames; + for (int i = 0; i < 10; ++i) + testNames << shader.uniforms()[i * 3].m_nameId; + + // WHEN + ShaderParameterPack pack; + for (const int nameId : qAsConst(testNames)) + pack.setUniform(nameId, UniformValue(nameId)); + + QBENCHMARK { + shader.prepareUniforms(pack); + } + } +}; + +QTEST_MAIN(tst_BenchShaderParameterPack) + +#include "tst_bench_shaderparameterpack.moc" diff --git a/tests/benchmarks/render/render.pro b/tests/benchmarks/render/render.pro index 7720a2d4a..98d2ba1e7 100644 --- a/tests/benchmarks/render/render.pro +++ b/tests/benchmarks/render/render.pro @@ -3,5 +3,6 @@ TEMPLATE=subdirs qtConfig(private_tests) { SUBDIRS += jobs \ layerfiltering \ - materialparametergathering + materialparametergathering \ + opengl } diff --git a/tests/manual/bigscene-cpp/main.cpp b/tests/manual/bigscene-cpp/main.cpp index 5e61758db..5a66cb213 100644 --- a/tests/manual/bigscene-cpp/main.cpp +++ b/tests/manual/bigscene-cpp/main.cpp @@ -70,6 +70,11 @@ #include <qmath.h> #include <Qt3DExtras/qt3dwindow.h> #include <Qt3DExtras/qfirstpersoncameracontroller.h> +#include <Qt3DRender/QRenderSurfaceSelector> +#include <Qt3DRender/QCameraSelector> +#include <Qt3DRender/QViewport> +#include <Qt3DRender/QNoDraw> +#include <Qt3DRender/QDebugOverlay> using namespace Qt3DCore; using namespace Qt3DRender; @@ -78,7 +83,46 @@ int main(int ac, char **av) { QGuiApplication app(ac, av); Qt3DExtras::Qt3DWindow view; - view.defaultFrameGraph()->setClearColor(Qt::black); + + // FrameGraph + { + QRenderSurfaceSelector *surfaceSelector = new QRenderSurfaceSelector(); + QCameraSelector *cameraSelector = new Qt3DRender::QCameraSelector(surfaceSelector); + cameraSelector->setCamera(view.camera()); + QClearBuffers *clearBuffers = new Qt3DRender::QClearBuffers(cameraSelector); + clearBuffers->setClearColor(QColor(Qt::gray)); + clearBuffers->setBuffers(QClearBuffers::ColorDepthBuffer); + new QNoDraw(clearBuffers); + + const QRectF viewports[] = { + {0.0f, 0.0f, 0.25f, 0.25f}, + {0.25f, 0.0f, 0.25f, 0.25f}, + {0.5f, 0.0f, 0.25f, 0.25f}, + {0.75f, 0.0f, 0.25f, 0.25f}, + {0.0f, 0.25f, 0.25f, 0.25f}, + {0.25f, 0.25f, 0.25f, 0.25f}, + {0.5f, 0.25f, 0.25f, 0.25f}, + {0.75f, 0.25f, 0.25f, 0.25f}, + {0.0f, 0.5f, 0.25f, 0.25f}, + {0.25f, 0.5f, 0.25f, 0.25f}, + {0.5f, 0.5f, 0.25f, 0.25f}, + {0.75f, 0.5f, 0.25f, 0.25f}, + {0.0f, 0.75f, 0.25f, 0.25f}, + {0.25f, 0.75f, 0.25f, 0.25f}, + {0.5f, 0.75f, 0.25f, 0.25f}, + {0.75f, 0.75f, 0.25f, 0.25f}, + }; + + for (const QRectF &vp : viewports) { + QViewport *viewport = new Qt3DRender::QViewport(cameraSelector); + viewport->setNormalizedRect(vp); + } + + new QDebugOverlay(qobject_cast<Qt3DCore::QNode *>(cameraSelector->children().last())); + + view.setActiveFrameGraph(surfaceSelector); + } + QEntity *root = new QEntity(); diff --git a/tests/manual/manual.pro b/tests/manual/manual.pro index 2a639afdb..c64044464 100644 --- a/tests/manual/manual.pro +++ b/tests/manual/manual.pro @@ -1,85 +1,29 @@ TEMPLATE = subdirs SUBDIRS += \ - assimp \ bigscene-cpp \ - bigmodel-qml \ - bigscene-instanced-qml \ - clip-planes-qml \ component-changes \ custom-mesh-cpp \ custom-mesh-cpp-indirect \ - custom-mesh-qml \ custom-mesh-update-data-cpp \ - custom-mesh-update-data-qml \ cylinder-cpp \ cylinder-parent-test \ - cylinder-qml \ deferred-renderer-cpp \ - deferred-renderer-qml \ - downloading \ - dragging \ - dynamicscene-cpp \ - enabled-qml \ - gltf \ - gooch-qml \ - keyboardinput-qml \ - loader-qml \ - lod \ - mouseinput-qml \ - multiplewindows-qml \ - picking-qml \ - plasma \ - pointlinesize \ - scene3d-loader \ - simple-shaders-qml \ - skybox \ - tessellation-modes \ - transforms-qml \ - spritegrid \ - transparency-qml \ - transparency-qml-scene3d \ - rendercapture-qml \ - additional-attributes-qml \ - dynamic-model-loader-qml \ - buffercapture-qml \ - render-qml-to-texture \ - render-qml-to-texture-qml \ - video-texture-qml \ - animation-keyframe-simple \ - animation-keyframe-blendtree \ - distancefieldtext \ - mesh-morphing \ - anim-viewer \ - animation-keyframe-programmatic \ - layerfilter-qml \ - skinned-mesh \ - rigged-simple \ - proximityfilter \ - rendercapture-qml-fbo \ - blitframebuffer-qml \ - raycasting-qml \ - shared_texture_image \ - texture_property_updates \ raster-cpp \ - raster-qml \ qtbug-72236 \ - qtbug-76766 \ - shader-image-qml \ - scene3d-in-sync \ - compressed_textures \ - subtree-enabler-qml \ - scene3d-visibility \ manual-renderloop \ + rhi \ boundingvolumes -!macos:!uikit: SUBDIRS += compute-manual - qtHaveModule(multimedia): { SUBDIRS += \ - sharedtexture \ - sharedtextureqml + sharedtexture + + qtHaveModule(quick) { + SUBDIRS += \ + sharedtextureqml + } } qtHaveModule(widgets): { @@ -89,3 +33,70 @@ qtHaveModule(widgets): { rendercapture-cpp \ texture-updates-cpp } + +qtHaveModule(quick) { + !macos:!uikit: SUBDIRS += compute-manual + + SUBDIRS += \ + assimp \ + animation-keyframe-simple \ + animation-keyframe-blendtree \ + animation-keyframe-programmatic \ + bigmodel-qml \ + bigscene-instanced-qml \ + clip-planes-qml \ + custom-mesh-qml \ + custom-mesh-update-data-qml \ + cylinder-qml \ + deferred-renderer-qml \ + downloading \ + dynamicscene-cpp \ + dragging \ + enabled-qml \ + gltf \ + gooch-qml \ + keyboardinput-qml \ + lod \ + loader-qml \ + mouseinput-qml \ + multiplewindows-qml \ + plasma \ + pointlinesize \ + scene3d-loader \ + picking-qml \ + skybox \ + simple-shaders-qml \ + transparency-qml \ + transparency-qml-scene3d \ + rendercapture-qml \ + additional-attributes-qml \ + dynamic-model-loader-qml \ + buffercapture-qml \ + render-qml-to-texture \ + render-qml-to-texture-qml \ + video-texture-qml \ + transforms-qml \ + layerfilter-qml \ + tessellation-modes \ + rendercapture-qml-fbo \ + blitframebuffer-qml \ + raycasting-qml \ + raster-qml \ + shader-image-qml \ + spritegrid \ + subtree-enabler-qml \ + distancefieldtext \ + mesh-morphing \ + anim-viewer \ + skinned-mesh \ + rigged-simple \ + proximityfilter \ + scene3d-visibility \ + shared_texture_image \ + texture_property_updates \ + qtbug-76766 \ + scene3d-in-sync \ + compressed_textures \ +} + +qtHaveModule(quickwidgets): SUBDIRS += quickwidget-switch diff --git a/tests/manual/quickwidget-switch/main.cpp b/tests/manual/quickwidget-switch/main.cpp new file mode 100644 index 000000000..ebadc458d --- /dev/null +++ b/tests/manual/quickwidget-switch/main.cpp @@ -0,0 +1,125 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDesktopWidget> +#include <QScreen> +#include <QApplication> +#include <QMainWindow> +#include <QMdiArea> +#include <QMdiSubWindow> +#include <QQuickWidget> +#include <QVBoxLayout> +#include <QPushButton> + +void configureMainWindow(QMainWindow *w, QMdiArea *mdiArea, QPushButton *button) +{ + auto widget = new QWidget; + auto layout = new QVBoxLayout; + layout->addWidget(mdiArea); + layout->addWidget(button); + widget->setLayout(layout); + w->setCentralWidget(widget); +} + +int main(int argc, char* argv[]) +{ + QApplication app(argc, argv); + + QMainWindow w1; + w1.winId(); + w1.windowHandle()->setScreen(QGuiApplication::screens().at(0)); + auto mdiArea1 = new QMdiArea; + auto button1 = new QPushButton("Switch to this window"); + configureMainWindow(&w1, mdiArea1, button1); + w1.setGeometry(0, 0, 800, 800); + w1.show(); + + QMainWindow w2; + w2.winId(); + w2.windowHandle()->setScreen(QGuiApplication::screens().at(1)); + auto mdiArea2 = new QMdiArea; + auto button2 = new QPushButton("Switch to this window"); + configureMainWindow(&w2, mdiArea2, button2); + w2.setGeometry(0, 0, 800, 800); + w2.show(); + + QMdiSubWindow* subWindow = new QMdiSubWindow(); + + QQuickWidget *quickWidget = new QQuickWidget(); + quickWidget->resize(QSize(400, 400)); + quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView); + quickWidget->setSource(QUrl("qrc:/main.qml")); + + subWindow->setWidget(quickWidget); + + QObject::connect(button1, &QPushButton::clicked, + [mdiArea1, mdiArea2, subWindow, button1, button2]() { + mdiArea2->removeSubWindow(subWindow); + mdiArea1->addSubWindow(subWindow); + subWindow->show(); + button1->setEnabled(false); + button2->setEnabled(true); + }); + + QObject::connect(button2, &QPushButton::clicked, + [mdiArea1, mdiArea2, subWindow, button1, button2]() { + mdiArea1->removeSubWindow(subWindow); + mdiArea2->addSubWindow(subWindow); + subWindow->show(); + button1->setEnabled(true); + button2->setEnabled(false); + }); + + mdiArea2->addSubWindow(subWindow); + button2->setEnabled(false); + subWindow->show(); + + return app.exec(); +} diff --git a/tests/manual/quickwidget-switch/main.qml b/tests/manual/quickwidget-switch/main.qml new file mode 100644 index 000000000..d4bce020c --- /dev/null +++ b/tests/manual/quickwidget-switch/main.qml @@ -0,0 +1,152 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.2 as QQ2 +import Qt3D.Core 2.0 +import Qt3D.Render 2.0 +import Qt3D.Input 2.0 +import Qt3D.Extras 2.0 + +import QtQuick.Scene3D 2.0 + +QQ2.Item { + id: mioitem + width: 300 + height: 300 + Scene3D { + id: scene3d + anchors.fill: parent + Entity { + id: sceneRoot + + Camera { + id: camera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 45 + aspectRatio: 16/9 + nearPlane : 0.1 + farPlane : 1000.0 + position: Qt.vector3d( 0.0, 0.0, -40.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + } + + OrbitCameraController { + camera: camera + } + + components: [ + RenderSettings { + activeFrameGraph: ForwardRenderer { + clearColor: Qt.rgba(0, 0.5, 1, 1) + camera: camera + } + }, + // Event Source will be set by the Qt3DQuickWindow + InputSettings { } + ] + + PhongMaterial { + id: material + } + + TorusMesh { + id: torusMesh + radius: 5 + minorRadius: 1 + rings: 100 + slices: 20 + } + + Transform { + id: torusTransform + scale3D: Qt.vector3d(1.5, 1, 0.5) + rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45) + } + + Entity { + id: torusEntity + components: [ torusMesh, material, torusTransform ] + } + + SphereMesh { + id: sphereMesh + radius: 3 + } + + Transform { + id: sphereTransform + property real userAngle: 0.0 + matrix: { + var m = Qt.matrix4x4(); + m.rotate(userAngle, Qt.vector3d(0, 1, 0)); + m.translate(Qt.vector3d(20, 0, 0)); + return m; + } + } + + QQ2.NumberAnimation { + target: sphereTransform + property: "userAngle" + duration: 10000 + from: 0 + to: 360 + + loops: QQ2.Animation.Infinite + running: true + } + + Entity { + id: sphereEntity + components: [ sphereMesh, material, sphereTransform ] + } + } + } +} diff --git a/tests/manual/quickwidget-switch/quickwidget-switch.pro b/tests/manual/quickwidget-switch/quickwidget-switch.pro new file mode 100644 index 000000000..2f1cb98f5 --- /dev/null +++ b/tests/manual/quickwidget-switch/quickwidget-switch.pro @@ -0,0 +1,13 @@ +TEMPLATE = app + +QT += 3dextras +CONFIG += resources_big + +QT += 3dcore 3drender 3dinput 3dquick 3dlogic qml quick 3dquickextras widgets quickwidgets + +SOURCES += \ + main.cpp + +RESOURCES += \ + quickwidget-switch.qrc + diff --git a/tests/manual/quickwidget-switch/quickwidget-switch.qrc b/tests/manual/quickwidget-switch/quickwidget-switch.qrc new file mode 100644 index 000000000..5f6483ac3 --- /dev/null +++ b/tests/manual/quickwidget-switch/quickwidget-switch.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + </qresource> +</RCC> diff --git a/tests/manual/quickwindow-switch/main.cpp b/tests/manual/quickwindow-switch/main.cpp new file mode 100644 index 000000000..7bebe0c75 --- /dev/null +++ b/tests/manual/quickwindow-switch/main.cpp @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> +#include <QQmlApplicationEngine> + +int main(int argc, char* argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, + &app, [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/tests/manual/quickwindow-switch/main.qml b/tests/manual/quickwindow-switch/main.qml new file mode 100644 index 000000000..4b474bd11 --- /dev/null +++ b/tests/manual/quickwindow-switch/main.qml @@ -0,0 +1,181 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.2 +import QtQuick.Window 2.12 as Win +import Qt3D.Core 2.0 +import Qt3D.Render 2.0 +import Qt3D.Input 2.0 +import Qt3D.Extras 2.0 + +import QtQuick.Scene3D 2.0 + +Win.Window { + id: win + width: 300 + height: 350 + visible: true + x: Win.Screen.width / 2 - width / 2 + y: Win.Screen.height / 2 - height / 2 + Item { + id: mioitem + width: 300 + height: 300 + Scene3D { + id: scene3d + anchors.fill: parent + Entity { + id: sceneRoot + + Camera { + id: camera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 45 + aspectRatio: 16/9 + nearPlane : 0.1 + farPlane : 1000.0 + position: Qt.vector3d( 0.0, 0.0, -40.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + } + + OrbitCameraController { + camera: camera + } + + components: [ + RenderSettings { + activeFrameGraph: ForwardRenderer { + clearColor: Qt.rgba(0, 0.5, 1, 1) + camera: camera + } + }, + // Event Source will be set by the Qt3DQuickWindow + InputSettings { } + ] + + PhongMaterial { + id: material + } + + TorusMesh { + id: torusMesh + radius: 5 + minorRadius: 1 + rings: 100 + slices: 20 + } + + Transform { + id: torusTransform + scale3D: Qt.vector3d(1.5, 1, 0.5) + rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45) + } + + Entity { + id: torusEntity + components: [ torusMesh, material, torusTransform ] + } + + SphereMesh { + id: sphereMesh + radius: 3 + } + + Transform { + id: sphereTransform + property real userAngle: 0.0 + matrix: { + var m = Qt.matrix4x4(); + m.rotate(userAngle, Qt.vector3d(0, 1, 0)); + m.translate(Qt.vector3d(20, 0, 0)); + return m; + } + } + + NumberAnimation { + target: sphereTransform + property: "userAngle" + duration: 10000 + from: 0 + to: 360 + + loops: Animation.Infinite + running: true + } + + Entity { + id: sphereEntity + components: [ sphereMesh, material, sphereTransform ] + } + } + } + } + Rectangle { + height: 50 + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + color: "yellow" + MouseArea { + anchors.fill: parent + z: 5 + onClicked: { + win.screen = (win.screen === Qt.application.screens[0] ? Qt.application.screens[1] : Qt.application.screens[0]) + } + } + Text { + anchors.centerIn: parent + minimumPointSize: 12 + fontSizeMode: Text.Fit + text: "Move to the other screen" + } + } +} diff --git a/tests/manual/quickwindow-switch/quickwindow-switch.pro b/tests/manual/quickwindow-switch/quickwindow-switch.pro new file mode 100644 index 000000000..2f338d36d --- /dev/null +++ b/tests/manual/quickwindow-switch/quickwindow-switch.pro @@ -0,0 +1,13 @@ +TEMPLATE = app + +QT += 3dextras +CONFIG += resources_big + +QT += 3dcore 3drender 3dinput 3dquick 3dlogic qml quick 3dquickextras + +SOURCES += \ + main.cpp + +RESOURCES += \ + quickwindow-switch.qrc + diff --git a/tests/manual/quickwindow-switch/quickwindow-switch.qrc b/tests/manual/quickwindow-switch/quickwindow-switch.qrc new file mode 100644 index 000000000..5f6483ac3 --- /dev/null +++ b/tests/manual/quickwindow-switch/quickwindow-switch.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + </qresource> +</RCC> diff --git a/tests/manual/rendercapture-cpp/mycapture.h b/tests/manual/rendercapture-cpp/mycapture.h index fea1abe46..f7893c8ad 100644 --- a/tests/manual/rendercapture-cpp/mycapture.h +++ b/tests/manual/rendercapture-cpp/mycapture.h @@ -75,7 +75,7 @@ public slots: m_reply->saveImage("capture.bmp"); - delete m_reply; + m_reply->deleteLater(); m_reply = nullptr; if (m_continuous) diff --git a/tests/manual/rhi/main.cpp b/tests/manual/rhi/main.cpp new file mode 100644 index 000000000..62ea0ab93 --- /dev/null +++ b/tests/manual/rhi/main.cpp @@ -0,0 +1,445 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt3D module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QGuiApplication> + +#include <Qt3DCore/QEntity> +#include <Qt3DRender/QCamera> +#include <Qt3DRender/QCameraLens> +#include <Qt3DCore/QTransform> +#include <Qt3DCore/QAspectEngine> +#include <Qt3DCore/QGeometry> +#include <Qt3DCore/QAttribute> +#include <Qt3DCore/QBuffer> + +#include <Qt3DInput/QInputAspect> + +#include <Qt3DRender/QRenderStateSet> +#include <Qt3DRender/QRenderAspect> +#include <Qt3DExtras/QForwardRenderer> +#include <Qt3DExtras/QPerVertexColorMaterial> + +#include <Qt3DRender/QGeometryRenderer> + +#include <QPropertyAnimation> +#include <Qt3DExtras/qt3dwindow.h> +#include <Qt3DExtras/qorbitcameracontroller.h> +#include <Qt3DRender/QParameter> +#include <Qt3DRender/QEffect> +#include <Qt3DRender/QTechnique> +#include <Qt3DRender/QAbstractTexture> +#include <Qt3DRender/QShaderProgram> +#include <Qt3DRender/QRenderPass> +#include <Qt3DRender/QBlendEquation> +#include <Qt3DRender/QBlendEquationArguments> +#include <Qt3DRender/QFilterKey> +#include <Qt3DRender/QGraphicsApiFilter> +#include <Qt3DRender/QRenderSurfaceSelector> +#include <Qt3DRender/QViewport> +#include <Qt3DRender/QCameraSelector> +#include <Qt3DRender/QNoDraw> +#include <QColor> +#include <QVector2D> +#include <QUrl> +#include <QTimer> +#include <Qt3DRender/QMaterial> +#include <Qt3DRender/QFilterKey> +#include <Qt3DRender/QTechnique> +#include <Qt3DRender/QMaterial> +#include <Qt3DRender/QTexture> +#include <qmath.h> + +static const constexpr auto vertex_shader = R"_(#version 450 + +layout(location = 0) in vec3 vertexPosition; +layout(location = 1) in vec3 vertexColor; +layout(location = 0) out vec3 color; + +layout(std140, binding = 0) uniform qt3d_render_view_uniforms { + mat4 viewMatrix; + mat4 projectionMatrix; + mat4 viewProjectionMatrix; + mat4 inverseViewMatrix; + mat4 inverseProjectionMatrix; + mat4 inverseViewProjectionMatrix; + mat4 viewportMatrix; + mat4 inverseViewportMatrix; + vec4 textureTransformMatrix; + vec3 eyePosition; + float aspectRatio; + float gamma; + float exposure; + float time; +}; +layout(std140, binding = 1) uniform qt3d_command_uniforms { + mat4 modelMatrix; + mat4 inverseModelMatrix; + mat4 modelViewMatrix; + mat3 modelNormalMatrix; + mat4 inverseModelViewMatrix; + mat4 mvp; + mat4 inverseModelViewProjectionMatrix; +}; +void main() +{ + color = vertexColor; + gl_Position = mvp * vec4(vertexPosition, 1.0); +} +)_"; + +static const constexpr auto fragment_shader = R"_(#version 450 + +layout(location = 0) out vec4 fragColor; +layout(location = 0) in vec3 color; + +layout(std140, binding = 0) uniform qt3d_render_view_uniforms { + mat4 viewMatrix; + mat4 projectionMatrix; + mat4 viewProjectionMatrix; + mat4 inverseViewMatrix; + mat4 inverseProjectionMatrix; + mat4 inverseViewProjectionMatrix; + mat4 viewportMatrix; + mat4 inverseViewportMatrix; + vec4 textureTransformMatrix; + vec3 eyePosition; + float aspectRatio; + float gamma; + float exposure; + float time; +}; +layout(std140, binding = 1) uniform qt3d_command_uniforms { + mat4 modelMatrix; + mat4 inverseModelMatrix; + mat4 modelViewMatrix; + mat3 modelNormalMatrix; + mat4 inverseModelViewMatrix; + mat4 mvp; + mat4 inverseModelViewProjectionMatrix; +}; +layout(std140, binding = 2) uniform custom_ubo { + vec3 colorFactor; +}; + +layout(binding = 3) uniform sampler2D myTexture; +void main() +{ + vec2 texCoord = color.xz; + vec2 rhiTexCoord = textureTransformMatrix.xy * texCoord+ textureTransformMatrix.zw; + fragColor = vec4(color * colorFactor, 1.0); + + fragColor *= texture(myTexture, rhiTexCoord); +} + +)_"; + +class Material : public Qt3DRender::QMaterial +{ +public: + explicit Material(Qt3DCore::QNode *parent = nullptr) + : QMaterial(parent) + , m_effect(new Qt3DRender::QEffect(this)) +{ + setEffect(m_effect); + + m_testParam = new Qt3DRender::QParameter(QStringLiteral("example"), float(0.5)); + + m_effect->addParameter(m_testParam); + + m_filter = new Qt3DRender::QFilterKey(this); + m_filter->setName(QStringLiteral("renderingStyle")); + m_filter->setValue(QStringLiteral("forward")); + + m_technique = new Qt3DRender::QTechnique(m_effect); + m_technique->addFilterKey(m_filter); + + m_effect->addTechnique(m_technique); + + m_program = new Qt3DRender::QShaderProgram(m_effect); + m_program->setVertexShaderCode(vertex_shader); + m_program->setFragmentShaderCode(fragment_shader); + + m_renderPass = new Qt3DRender::QRenderPass(m_effect); + + m_renderPass->setShaderProgram(m_program); + + m_technique->addRenderPass(m_renderPass); + + m_technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::RHI); +} +private: + Qt3DRender::QEffect *m_effect{}; + Qt3DRender::QParameter *m_testParam{}; + Qt3DRender::QFilterKey *m_filter{}; + Qt3DRender::QTechnique *m_technique{}; + Qt3DRender::QShaderProgram *m_program{}; + Qt3DRender::QRenderPass *m_renderPass{}; +}; + +int main(int argc, char* argv[]) +{ + qputenv("QT3D_RENDERER", "rhi"); + QGuiApplication app(argc, argv); + + auto api = Qt3DRender::API::OpenGL; + if (argc >= 2) { + +#ifdef Q_OS_WIN + if (argv[1] == QByteArrayLiteral("--d3d11")) api = Qt3DRender::API::DirectX; +#endif + +#if QT_CONFIG(vulkan) + if (argv[1] == QByteArrayLiteral("--vulkan")) api = Qt3DRender::API::Vulkan; +#endif + +#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) + if (argv[1] == QByteArrayLiteral("--metal")) api = Qt3DRender::API::Metal; +#endif + } + + Qt3DExtras::Qt3DWindow view{nullptr, api}; + + // Root entity + Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); + + // Camera + Qt3DRender::QCamera *topViewCamera = new Qt3DRender::QCamera(rootEntity); + topViewCamera->setPosition(QVector3D(0, 40, 0)); + topViewCamera->setViewCenter(QVector3D(0, 0, 0)); + topViewCamera->setUpVector(QVector3D(0, 0, 1)); + topViewCamera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); + + Qt3DRender::QCamera *cameraEntity = view.camera(); + cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); + cameraEntity->setPosition(QVector3D(0, 0, 40.0f)); + cameraEntity->setUpVector(QVector3D(0, 1, 0)); + cameraEntity->setViewCenter(QVector3D(0, 0, 0)); + + { + // Custom FG + auto *surfaceSelector = new Qt3DRender::QRenderSurfaceSelector(); + surfaceSelector->setSurface(&view); + + // RV 1 + auto *clearBuffer = new Qt3DRender::QClearBuffers(surfaceSelector); + clearBuffer->setBuffers(Qt3DRender::QClearBuffers::ColorDepthBuffer); + clearBuffer->setClearColor(QColor::fromRgbF(0.1, 0.5, 0.0, 1.0)); + auto *noDraw = new Qt3DRender::QNoDraw(clearBuffer); + + // RV 2 + auto *cameraSelector1 = new Qt3DRender::QCameraSelector(surfaceSelector); + cameraSelector1->setCamera(view.camera()); + auto *viewport1 = new Qt3DRender::QViewport(cameraSelector1); + viewport1->setNormalizedRect(QRectF(0.0f, 0.0f, 0.5f, 0.5f)); + + // RV3 + auto *cameraSelector2 = new Qt3DRender::QCameraSelector(surfaceSelector); + cameraSelector2->setCamera(topViewCamera); + auto *viewport2 = new Qt3DRender::QViewport(cameraSelector2); + viewport2->setNormalizedRect(QRectF(0.5f, 0.5f, 0.5f, 0.5f)); + + view.setActiveFrameGraph(surfaceSelector); + } + + QTimer *cameraAnimationTimer = new QTimer(&view); + QObject::connect(cameraAnimationTimer, &QTimer::timeout, + [cameraEntity] { + static int angle = 0; + const float radius = 40.0f; + const float anglef = qDegreesToRadians(float(angle)); + cameraEntity->setPosition(QVector3D(qSin(anglef), 0.0f, qCos(anglef)) * radius); + angle += 1; + }); + cameraAnimationTimer->start(16); + + // For camera controls + Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); + camController->setCamera(cameraEntity); + + // Material + Qt3DRender::QMaterial *material = new Material(rootEntity); + Qt3DRender::QParameter *parameter = new Qt3DRender::QParameter(QStringLiteral("colorFactor"), QColor(Qt::white)); + material->addParameter(parameter); + + Qt3DRender::QTextureLoader *textureLoader = new Qt3DRender::QTextureLoader{}; + textureLoader->setSource(QUrl{QStringLiteral("qrc:///qtlogo.png")}); + Qt3DRender::QParameter *texture = new Qt3DRender::QParameter(QStringLiteral("myTexture"), textureLoader); + material->addParameter(texture); + + QTimer *parameterAnimationTimer = new QTimer(&view); + QObject::connect(parameterAnimationTimer, &QTimer::timeout, + [parameter] { + static int angle = 0; + const float anglef = qDegreesToRadians(float(angle)); + parameter->setValue(QColor::fromRgbF(fabs(qCos(anglef)), fabs(qSin(anglef)), 1.0f)); + angle += 10; + }); + parameterAnimationTimer->start(16); + + // Torus + Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity); + + // Transform + Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; + transform->setScale(8.0f); + + // Custom Mesh (TetraHedron) + Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; + Qt3DCore::QGeometryView *customMeshView = new Qt3DCore::QGeometryView; + Qt3DCore::QGeometry *customGeometry = new Qt3DCore::QGeometry; + + Qt3DCore::QBuffer *vertexDataBuffer = new Qt3DCore::QBuffer(customGeometry); + Qt3DCore::QBuffer *indexDataBuffer = new Qt3DCore::QBuffer(customGeometry); + + // 5 vertices of 3 vertices + 3 colors + QByteArray vertexBufferData; + vertexBufferData.resize(5 * (3 + 3) * sizeof(float)); + + // Vertices + QVector3D v0(-1.0f, 0.0f, -1.0f); + QVector3D v1(1.0f, 0.0f, -1.0f); + QVector3D v2(-1.0f, 0.0f, 1.0f); + QVector3D v3(1.0f, 0.0f, 1.0f); + QVector3D v4(0.0f, 2.0f, 0.0f); + + QVector3D red(1.0f, 0.0f, 0.0f); + QVector3D green(0.0f, 1.0f, 0.0f); + QVector3D blue(0.0f, 0.0f, 1.0f); + QVector3D white(1.0f, 1.0f, 1.0f); + QVector3D grey(0.5f, 0.5f, 0.5f); + + const QVector<QVector3D> vertices = QVector<QVector3D>() + << v0 << red + << v1 << green + << v2 << blue + << v3 << grey + << v4 << white; + + memcpy(vertexBufferData.data(), vertices.constData(), vertices.size() * sizeof(QVector3D)); + vertexDataBuffer->setData(vertexBufferData); + + QByteArray indexBufferData; + // 6 triangle faces + indexBufferData.resize(6 * 3 * sizeof(ushort)); + ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data()); + + rawIndexArray[0] = 0; + rawIndexArray[1] = 1; + rawIndexArray[2] = 2; + + rawIndexArray[3] = 2; + rawIndexArray[4] = 1; + rawIndexArray[5] = 3; + + rawIndexArray[6] = 2; + rawIndexArray[7] = 3; + rawIndexArray[8] = 4; + + rawIndexArray[9] = 3; + rawIndexArray[10] = 1; + rawIndexArray[11] = 4; + + rawIndexArray[12] = 1; + rawIndexArray[13] = 0; + rawIndexArray[14] = 4; + + rawIndexArray[15] = 0; + rawIndexArray[16] = 2; + rawIndexArray[17] = 4; + + indexDataBuffer->setData(indexBufferData); + + // Attributes + Qt3DCore::QAttribute *positionAttribute = new Qt3DCore::QAttribute(); + positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); + positionAttribute->setBuffer(vertexDataBuffer); + positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); + positionAttribute->setVertexSize(3); + positionAttribute->setByteOffset(0); + positionAttribute->setByteStride(6 * sizeof(float)); + positionAttribute->setCount(5); + positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); + + Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute(); + colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); + colorAttribute->setBuffer(vertexDataBuffer); + colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); + colorAttribute->setVertexSize(3); + colorAttribute->setByteOffset(3 * sizeof(float)); + colorAttribute->setByteStride(6 * sizeof(float)); + colorAttribute->setCount(5); + colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName()); + + Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute(); + indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); + indexAttribute->setBuffer(indexDataBuffer); + indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort); + indexAttribute->setVertexSize(1); + indexAttribute->setByteOffset(0); + indexAttribute->setByteStride(0); + indexAttribute->setCount(18); + + customGeometry->addAttribute(positionAttribute); + customGeometry->addAttribute(colorAttribute); + customGeometry->addAttribute(indexAttribute); + + customMeshView->setPrimitiveType(Qt3DCore::QGeometryView::Triangles); + customMeshView->setGeometry(customGeometry); + customMeshRenderer->setView(customMeshView); + + customMeshEntity->addComponent(customMeshRenderer); + customMeshEntity->addComponent(transform); + customMeshEntity->addComponent(material); + + view.setRootEntity(rootEntity); + view.show(); + + return app.exec(); +} diff --git a/tests/manual/rhi/qtlogo.png b/tests/manual/rhi/qtlogo.png Binary files differnew file mode 100644 index 000000000..19eecfabf --- /dev/null +++ b/tests/manual/rhi/qtlogo.png diff --git a/tests/manual/rhi/rhi.pro b/tests/manual/rhi/rhi.pro new file mode 100644 index 000000000..94e560391 --- /dev/null +++ b/tests/manual/rhi/rhi.pro @@ -0,0 +1,10 @@ +!include( ../manual.pri ) { + error( "Couldn't find the manual.pri file!" ) +} + +QT += 3dcore 3drender 3dinput 3dextras + +SOURCES += \ + main.cpp + +RESOURCES += qtlogo.png |