From 57b6b2f5273017f2d1fa8d3e055c5001ecd84e5d Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Mon, 20 Mar 2017 12:10:37 +0100 Subject: [Shader Graph Gen.] Introduce QShaderFormat This is the first building block toward a node based generator for shader programs. QShaderFormat will be used by the other classes to qualify in which format the code snippets and includes used are. Change-Id: I11aefc6bb359832a853ed1b6bec302dc3516cfc4 Reviewed-by: Sean Harmer --- src/gui/util/qshaderformat.cpp | 130 ++++++++++ src/gui/util/qshaderformat_p.h | 109 ++++++++ src/gui/util/util.pri | 6 +- tests/auto/gui/util/qshadernodes/qshadernodes.pro | 5 + .../gui/util/qshadernodes/tst_qshadernodes.cpp | 281 +++++++++++++++++++++ tests/auto/gui/util/util.pro | 1 + 6 files changed, 530 insertions(+), 2 deletions(-) create mode 100644 src/gui/util/qshaderformat.cpp create mode 100644 src/gui/util/qshaderformat_p.h create mode 100644 tests/auto/gui/util/qshadernodes/qshadernodes.pro create mode 100644 tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp diff --git a/src/gui/util/qshaderformat.cpp b/src/gui/util/qshaderformat.cpp new file mode 100644 index 0000000000..373bfb9e7e --- /dev/null +++ b/src/gui/util/qshaderformat.cpp @@ -0,0 +1,130 @@ +/**************************************************************************** +** +** 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 "qshaderformat_p.h" + +QT_BEGIN_NAMESPACE + +QShaderFormat::QShaderFormat() Q_DECL_NOTHROW + : m_api(NoApi) +{ +} + +QShaderFormat::Api QShaderFormat::api() const Q_DECL_NOTHROW +{ + return m_api; +} + +void QShaderFormat::setApi(QShaderFormat::Api api) Q_DECL_NOTHROW +{ + m_api = api; +} + +QVersionNumber QShaderFormat::version() const Q_DECL_NOTHROW +{ + return m_version; +} + +void QShaderFormat::setVersion(const QVersionNumber &version) Q_DECL_NOTHROW +{ + m_version = version; +} + +QStringList QShaderFormat::extensions() const Q_DECL_NOTHROW +{ + return m_extensions; +} + +void QShaderFormat::setExtensions(const QStringList &extensions) Q_DECL_NOTHROW +{ + m_extensions = extensions; + m_extensions.sort(); +} + +QString QShaderFormat::vendor() const Q_DECL_NOTHROW +{ + return m_vendor; +} + +void QShaderFormat::setVendor(const QString &vendor) Q_DECL_NOTHROW +{ + m_vendor = vendor; +} + +bool QShaderFormat::isValid() const Q_DECL_NOTHROW +{ + return m_api != NoApi && m_version.majorVersion() > 0; +} + +bool QShaderFormat::supports(const QShaderFormat &other) const Q_DECL_NOTHROW +{ + if (!isValid() || !other.isValid()) + return false; + + if (m_api == OpenGLES && m_api != other.m_api) + return false; + + if (m_api == OpenGLCoreProfile && m_api != other.m_api) + return false; + + if (m_version < other.m_version) + return false; + + const auto containsAllExtensionsFromOther = std::includes(m_extensions.constBegin(), + m_extensions.constEnd(), + other.m_extensions.constBegin(), + other.m_extensions.constEnd()); + if (!containsAllExtensionsFromOther) + return false; + + if (!other.m_vendor.isEmpty() && m_vendor != other.m_vendor) + return false; + + return true; +} + +bool operator==(const QShaderFormat &lhs, const QShaderFormat &rhs) Q_DECL_NOTHROW +{ + return lhs.api() == rhs.api() + && lhs.version() == rhs.version() + && lhs.extensions() == rhs.extensions() + && lhs.vendor() == rhs.vendor(); +} + +QT_END_NAMESPACE diff --git a/src/gui/util/qshaderformat_p.h b/src/gui/util/qshaderformat_p.h new file mode 100644 index 0000000000..064c2364a7 --- /dev/null +++ b/src/gui/util/qshaderformat_p.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** 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 QSHADERFORMAT_P_H +#define QSHADERFORMAT_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 +#include + +QT_BEGIN_NAMESPACE + +class QShaderFormat +{ +public: + enum Api : int { + NoApi, + OpenGLNoProfile, + OpenGLCoreProfile, + OpenGLCompatibilityProfile, + OpenGLES + }; + + Q_GUI_EXPORT QShaderFormat() Q_DECL_NOTHROW; + + Q_GUI_EXPORT Api api() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void setApi(Api api) Q_DECL_NOTHROW; + + Q_GUI_EXPORT QVersionNumber version() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void setVersion(const QVersionNumber &version) Q_DECL_NOTHROW; + + Q_GUI_EXPORT QStringList extensions() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void setExtensions(const QStringList &extensions) Q_DECL_NOTHROW; + + Q_GUI_EXPORT QString vendor() const Q_DECL_NOTHROW; + Q_GUI_EXPORT void setVendor(const QString &vendor) Q_DECL_NOTHROW; + + Q_GUI_EXPORT bool isValid() const Q_DECL_NOTHROW; + Q_GUI_EXPORT bool supports(const QShaderFormat &other) const Q_DECL_NOTHROW; + +private: + Api m_api; + QVersionNumber m_version; + QStringList m_extensions; + QString m_vendor; +}; + +Q_GUI_EXPORT bool operator==(const QShaderFormat &lhs, const QShaderFormat &rhs) Q_DECL_NOTHROW; + +inline bool operator!=(const QShaderFormat &lhs, const QShaderFormat &rhs) Q_DECL_NOTHROW +{ + return !(lhs == rhs); +} + +Q_DECLARE_TYPEINFO(QShaderFormat, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +Q_DECLARE_METATYPE(QShaderFormat) + +#endif // QSHADERFORMAT_P_H diff --git a/src/gui/util/util.pri b/src/gui/util/util.pri index 79c83599b9..2073a0a825 100644 --- a/src/gui/util/util.pri +++ b/src/gui/util/util.pri @@ -6,11 +6,13 @@ HEADERS += \ util/qvalidator.h \ util/qgridlayoutengine_p.h \ util/qabstractlayoutstyleinfo_p.h \ - util/qlayoutpolicy_p.h + util/qlayoutpolicy_p.h \ + util/qshaderformat_p.h SOURCES += \ util/qdesktopservices.cpp \ util/qvalidator.cpp \ util/qgridlayoutengine.cpp \ util/qabstractlayoutstyleinfo.cpp \ - util/qlayoutpolicy.cpp + util/qlayoutpolicy.cpp \ + util/qshaderformat.cpp diff --git a/tests/auto/gui/util/qshadernodes/qshadernodes.pro b/tests/auto/gui/util/qshadernodes/qshadernodes.pro new file mode 100644 index 0000000000..5ab8b73a51 --- /dev/null +++ b/tests/auto/gui/util/qshadernodes/qshadernodes.pro @@ -0,0 +1,5 @@ +CONFIG += testcase +QT += testlib gui-private + +SOURCES += tst_qshadernodes.cpp +TARGET = tst_qshadernodes diff --git a/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp new file mode 100644 index 0000000000..1d0facfbd4 --- /dev/null +++ b/tests/auto/gui/util/qshadernodes/tst_qshadernodes.cpp @@ -0,0 +1,281 @@ +/**************************************************************************** +** +** 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 +{ + 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; + } +} + +class tst_QShaderNodes : public QObject +{ + Q_OBJECT +private slots: + void shouldManipulateFormatMembers(); + void shouldVerifyFormatsEquality_data(); + void shouldVerifyFormatsEquality(); + void shouldVerifyFormatsCompatibilities_data(); + void shouldVerifyFormatsCompatibilities(); +}; + +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("left"); + QTest::addColumn("right"); + QTest::addColumn("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("reference"); + QTest::addColumn("tested"); + QTest::addColumn("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); +} + +QTEST_MAIN(tst_QShaderNodes) + +#include "tst_qshadernodes.moc" diff --git a/tests/auto/gui/util/util.pro b/tests/auto/gui/util/util.pro index f2c4515dc2..729a342992 100644 --- a/tests/auto/gui/util/util.pro +++ b/tests/auto/gui/util/util.pro @@ -5,4 +5,5 @@ SUBDIRS= \ qintvalidator \ qregexpvalidator \ qregularexpressionvalidator \ + qshadernodes \ -- cgit v1.2.3