diff options
-rw-r--r-- | src/gui/util/qshadernode.cpp | 142 | ||||
-rw-r--r-- | src/gui/util/qshadernode_p.h | 118 | ||||
-rw-r--r-- | src/gui/util/util.pri | 2 | ||||
-rw-r--r-- | tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp | 159 |
4 files changed, 421 insertions, 0 deletions
diff --git a/src/gui/util/qshadernode.cpp b/src/gui/util/qshadernode.cpp new file mode 100644 index 0000000000..36fddb2235 --- /dev/null +++ b/src/gui/util/qshadernode.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Layouts 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 "qshadernode_p.h" + +QT_BEGIN_NAMESPACE + +QShaderNode::Type QShaderNode::type() const Q_DECL_NOTHROW +{ + int inputCount = 0; + int outputCount = 0; + for (const auto &port : qAsConst(m_ports)) { + switch (port.direction) { + case QShaderNodePort::Input: + inputCount++; + break; + case QShaderNodePort::Output: + outputCount++; + break; + } + } + + return (inputCount == 0 && outputCount == 0) ? Invalid + : (inputCount > 0 && outputCount == 0) ? Output + : (inputCount == 0 && outputCount > 0) ? Input + : Function; +} + +QUuid QShaderNode::uuid() const Q_DECL_NOTHROW +{ + return m_uuid; +} + +void QShaderNode::setUuid(const QUuid &uuid) Q_DECL_NOTHROW +{ + m_uuid = uuid; +} + +QVector<QShaderNodePort> QShaderNode::ports() const Q_DECL_NOTHROW +{ + return m_ports; +} + +void QShaderNode::addPort(const QShaderNodePort &port) +{ + removePort(port); + m_ports.append(port); +} + +void QShaderNode::removePort(const QShaderNodePort &port) +{ + const auto it = std::find_if(m_ports.begin(), m_ports.end(), + [port](const QShaderNodePort &p) { + return p.name == port.name; + }); + if (it != m_ports.end()) + m_ports.erase(it); +} + +void QShaderNode::addRule(const QShaderFormat &format, const QShaderNode::Rule &rule) +{ + removeRule(format); + m_rules << qMakePair(format, rule); +} + +void QShaderNode::removeRule(const QShaderFormat &format) +{ + const auto it = std::find_if(m_rules.begin(), m_rules.end(), + [format](const QPair<QShaderFormat, Rule> &entry) { + return entry.first == format; + }); + if (it != m_rules.end()) + m_rules.erase(it); +} + +QVector<QShaderFormat> QShaderNode::availableFormats() const +{ + auto res = QVector<QShaderFormat>(); + std::transform(m_rules.cbegin(), m_rules.cend(), + std::back_inserter(res), + [](const QPair<QShaderFormat, Rule> &entry) { return entry.first; }); + return res; +} + +QShaderNode::Rule QShaderNode::rule(const QShaderFormat &format) const +{ + const auto it = std::find_if(m_rules.crbegin(), m_rules.crend(), + [format](const QPair<QShaderFormat, Rule> &entry) { + return format.supports(entry.first); + }); + return it != m_rules.crend() ? it->second : Rule(); +} + +QShaderNode::Rule::Rule(const QByteArray &subs, const QByteArrayList &snippets) Q_DECL_NOTHROW + : substitution(subs), + headerSnippets(snippets) +{ +} + +bool operator==(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) Q_DECL_NOTHROW +{ + return lhs.substitution == rhs.substitution + && lhs.headerSnippets == rhs.headerSnippets; +} + +QT_END_NAMESPACE diff --git a/src/gui/util/qshadernode_p.h b/src/gui/util/qshadernode_p.h new file mode 100644 index 0000000000..b3e2d4a841 --- /dev/null +++ b/src/gui/util/qshadernode_p.h @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Layouts 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 QSHADERNODE_P_H +#define QSHADERNODE_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 <QtGui/private/qtguiglobal_p.h> + +#include <QtGui/private/qshaderformat_p.h> +#include <QtGui/private/qshadernodeport_p.h> + +#include <QtCore/quuid.h> + +QT_BEGIN_NAMESPACE + +class QShaderNode +{ +public: + enum Type : char { + Invalid, + Input, + Output, + Function + }; + + class Rule + { + public: + Q_GUI_EXPORT Rule(const QByteArray &substitution = QByteArray(), const QByteArrayList &headerSnippets = QByteArrayList()) Q_DECL_NOTHROW; + + QByteArray substitution; + QByteArrayList headerSnippets; + }; + + Q_GUI_EXPORT Type type() const Q_DECL_NOTHROW; + + Q_GUI_EXPORT QUuid uuid() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void setUuid(const QUuid &uuid) Q_DECL_NOTHROW; + + Q_GUI_EXPORT QVector<QShaderNodePort> ports() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void addPort(const QShaderNodePort &port); + Q_GUI_EXPORT void removePort(const QShaderNodePort &port); + + Q_GUI_EXPORT void addRule(const QShaderFormat &format, const Rule &rule); + Q_GUI_EXPORT void removeRule(const QShaderFormat &format); + + Q_GUI_EXPORT QVector<QShaderFormat> availableFormats() const; + Q_GUI_EXPORT Rule rule(const QShaderFormat &format) const; + +private: + QUuid m_uuid; + QVector<QShaderNodePort> m_ports; + QVector<QPair<QShaderFormat, QShaderNode::Rule>> m_rules; +}; + +Q_GUI_EXPORT bool operator==(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) Q_DECL_NOTHROW; + +inline bool operator!=(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) Q_DECL_NOTHROW +{ + return !(lhs == rhs); +} + +Q_DECLARE_TYPEINFO(QShaderNode, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QShaderNode::Rule, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +Q_DECLARE_METATYPE(QShaderNode) +Q_DECLARE_METATYPE(QShaderNode::Rule) + +#endif // QSHADERNODE_P_H diff --git a/src/gui/util/util.pri b/src/gui/util/util.pri index 6258bcbe86..d467d9dc57 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/qshadernode_p.h \ util/qshadernodeport_p.h SOURCES += \ @@ -17,4 +18,5 @@ SOURCES += \ util/qabstractlayoutstyleinfo.cpp \ util/qlayoutpolicy.cpp \ util/qshaderformat.cpp \ + util/qshadernode.cpp \ util/qshadernodeport.cpp diff --git a/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp index 590f2ececd..9d7139c880 100644 --- a/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp +++ b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp @@ -30,6 +30,7 @@ #include <QtTest/QtTest> #include <QtGui/private/qshaderformat_p.h> +#include <QtGui/private/qshadernode_p.h> #include <QtGui/private/qshadernodeport_p.h> namespace @@ -68,6 +69,9 @@ private slots: void shouldHaveDefaultPortState(); void shouldVerifyPortsEquality_data(); void shouldVerifyPortsEquality(); + + void shouldManipulateNodeUuidPortsAndRules(); + void shouldHandleNodeRulesSupportAndOrder(); }; void tst_QShaderNodes::shouldManipulateFormatMembers() @@ -332,6 +336,161 @@ void tst_QShaderNodes::shouldVerifyPortsEquality() QCOMPARE(notEqual, !expected); } +void tst_QShaderNodes::shouldManipulateNodeUuidPortsAndRules() +{ + // 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.ports().isEmpty()); + QVERIFY(node.availableFormats().isEmpty()); + + // WHEN + const auto uuid = QUuid::createUuid(); + node.setUuid(uuid); + + // THEN + QCOMPARE(node.uuid(), uuid); + + // 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.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" |