From 29a748511a8bd9dac373d1390d38999b9fd7c49d Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Thu, 30 Mar 2017 09:28:17 +0200 Subject: [Shader Graph Gen.] Introduce QShaderGraph This allows to connect our nodes together via ports. This way user code can assemble a shader from simpler blocks. Change-Id: I168dcf4af6aa11ad47b68d91e5a55e96ca922678 Reviewed-by: Sean Harmer --- src/gui/util/qshadergraph.cpp | 88 ++++++++ src/gui/util/qshadergraph_p.h | 100 ++++++++ src/gui/util/util.pri | 2 + tests/auto/gui/util/qshadergraph/qshadergraph.pro | 5 + .../gui/util/qshadergraph/tst_qshadergraph.cpp | 251 +++++++++++++++++++++ tests/auto/gui/util/util.pro | 1 + 6 files changed, 447 insertions(+) create mode 100644 src/gui/util/qshadergraph.cpp create mode 100644 src/gui/util/qshadergraph_p.h create mode 100644 tests/auto/gui/util/qshadergraph/qshadergraph.pro create mode 100644 tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp diff --git a/src/gui/util/qshadergraph.cpp b/src/gui/util/qshadergraph.cpp new file mode 100644 index 0000000000..0735c3cfe3 --- /dev/null +++ b/src/gui/util/qshadergraph.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qshadergraph_p.h" + +QT_BEGIN_NAMESPACE + +void QShaderGraph::addNode(const QShaderNode &node) +{ + removeNode(node); + m_nodes.append(node); +} + +void QShaderGraph::removeNode(const QShaderNode &node) +{ + const auto it = std::find_if(m_nodes.begin(), m_nodes.end(), + [node] (const QShaderNode &n) { return n.uuid() == node.uuid(); }); + if (it != m_nodes.end()) + m_nodes.erase(it); +} + +QVector QShaderGraph::nodes() const Q_DECL_NOTHROW +{ + return m_nodes; +} + +void QShaderGraph::addEdge(const QShaderGraph::Edge &edge) +{ + if (m_edges.contains(edge)) + return; + m_edges.append(edge); +} + +void QShaderGraph::removeEdge(const QShaderGraph::Edge &edge) +{ + m_edges.removeAll(edge); +} + +QVector QShaderGraph::edges() const Q_DECL_NOTHROW +{ + return m_edges; +} + +bool operator==(const QShaderGraph::Edge &lhs, const QShaderGraph::Edge &rhs) Q_DECL_NOTHROW +{ + return lhs.sourceNodeUuid == rhs.sourceNodeUuid + && lhs.sourcePortName == rhs.sourcePortName + && lhs.targetNodeUuid == rhs.targetNodeUuid + && lhs.targetPortName == rhs.targetPortName; +} + +QT_END_NAMESPACE diff --git a/src/gui/util/qshadergraph_p.h b/src/gui/util/qshadergraph_p.h new file mode 100644 index 0000000000..c582dcd18b --- /dev/null +++ b/src/gui/util/qshadergraph_p.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSHADERGRAPH_P_H +#define QSHADERGRAPH_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +#include + +QT_BEGIN_NAMESPACE + +class QShaderGraph +{ +public: + class Edge + { + public: + QUuid sourceNodeUuid; + QString sourcePortName; + QUuid targetNodeUuid; + QString targetPortName; + }; + + Q_GUI_EXPORT void addNode(const QShaderNode &node); + Q_GUI_EXPORT void removeNode(const QShaderNode &node); + Q_GUI_EXPORT QVector nodes() const Q_DECL_NOTHROW; + + Q_GUI_EXPORT void addEdge(const Edge &edge); + Q_GUI_EXPORT void removeEdge(const Edge &edge); + Q_GUI_EXPORT QVector edges() const Q_DECL_NOTHROW; + +private: + QVector m_nodes; + QVector m_edges; +}; + +Q_GUI_EXPORT bool operator==(const QShaderGraph::Edge &lhs, const QShaderGraph::Edge &rhs) Q_DECL_NOTHROW; + +inline bool operator!=(const QShaderGraph::Edge &lhs, const QShaderGraph::Edge &rhs) Q_DECL_NOTHROW +{ + return !(lhs == rhs); +} + +Q_DECLARE_TYPEINFO(QShaderGraph, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QShaderGraph::Edge, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +Q_DECLARE_METATYPE(QShaderGraph) +Q_DECLARE_METATYPE(QShaderGraph::Edge) + +#endif // QSHADERGRAPH_P_H diff --git a/src/gui/util/util.pri b/src/gui/util/util.pri index d467d9dc57..96b25df4f9 100644 --- a/src/gui/util/util.pri +++ b/src/gui/util/util.pri @@ -8,6 +8,7 @@ HEADERS += \ util/qabstractlayoutstyleinfo_p.h \ util/qlayoutpolicy_p.h \ util/qshaderformat_p.h \ + util/qshadergraph_p.h \ util/qshadernode_p.h \ util/qshadernodeport_p.h @@ -18,5 +19,6 @@ SOURCES += \ util/qabstractlayoutstyleinfo.cpp \ util/qlayoutpolicy.cpp \ util/qshaderformat.cpp \ + util/qshadergraph.cpp \ util/qshadernode.cpp \ util/qshadernodeport.cpp diff --git a/tests/auto/gui/util/qshadergraph/qshadergraph.pro b/tests/auto/gui/util/qshadergraph/qshadergraph.pro new file mode 100644 index 0000000000..ec54941c77 --- /dev/null +++ b/tests/auto/gui/util/qshadergraph/qshadergraph.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadergraph.cpp +TARGET = tst_qshadergraph diff --git a/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp b/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp new file mode 100644 index 0000000000..5ffdc58b51 --- /dev/null +++ b/tests/auto/gui/util/qshadergraph/tst_qshadergraph.cpp @@ -0,0 +1,251 @@ +/**************************************************************************** +** +** 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 + +#include + +namespace +{ + QShaderNodePort createPort(QShaderNodePort::Direction portDirection, const QString &portName) + { + auto port = QShaderNodePort(); + port.direction = portDirection; + port.name = portName; + return port; + } + + QShaderNode createNode(const QVector &ports) + { + auto node = QShaderNode(); + node.setUuid(QUuid::createUuid()); + 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) + { + auto edge = QShaderGraph::Edge(); + edge.sourceNodeUuid = sourceUuid; + edge.sourcePortName = sourceName; + edge.targetNodeUuid = targetUuid; + edge.targetPortName = targetName; + return edge; + } +} + +class tst_QShaderGraph : public QObject +{ + Q_OBJECT +private slots: + void shouldHaveEdgeDefaultState(); + void shouldTestEdgesEquality_data(); + void shouldTestEdgesEquality(); + void shouldManageNodeList(); + void shouldManageEdgeList(); +}; + +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("left"); + QTest::addColumn("right"); + QTest::addColumn("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::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); +} + +QTEST_MAIN(tst_QShaderGraph) + +#include "tst_qshadergraph.moc" diff --git a/tests/auto/gui/util/util.pro b/tests/auto/gui/util/util.pro index 729a342992..94404cd9f6 100644 --- a/tests/auto/gui/util/util.pro +++ b/tests/auto/gui/util/util.pro @@ -5,5 +5,6 @@ SUBDIRS= \ qintvalidator \ qregexpvalidator \ qregularexpressionvalidator \ + qshadergraph \ qshadernodes \ -- cgit v1.2.3