1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qshadernode_p.h"
41
42QT_BEGIN_NAMESPACE
43
44namespace Qt3DRender
45{
46QShaderNode::Type QShaderNode::type() const noexcept
47{
48 int inputCount = 0;
49 int outputCount = 0;
50 for (const auto &port : qAsConst(t: m_ports)) {
51 switch (port.direction) {
52 case QShaderNodePort::Input:
53 inputCount++;
54 break;
55 case QShaderNodePort::Output:
56 outputCount++;
57 break;
58 }
59 }
60
61 return (inputCount == 0 && outputCount == 0) ? Invalid
62 : (inputCount > 0 && outputCount == 0) ? Output
63 : (inputCount == 0 && outputCount > 0) ? Input
64 : Function;
65}
66
67QUuid QShaderNode::uuid() const noexcept
68{
69 return m_uuid;
70}
71
72void QShaderNode::setUuid(const QUuid &uuid) noexcept
73{
74 m_uuid = uuid;
75}
76
77QStringList QShaderNode::layers() const noexcept
78{
79 return m_layers;
80}
81
82void QShaderNode::setLayers(const QStringList &layers) noexcept
83{
84 m_layers = layers;
85}
86
87QVector<QShaderNodePort> QShaderNode::ports() const noexcept
88{
89 return m_ports;
90}
91
92void QShaderNode::addPort(const QShaderNodePort &port)
93{
94 removePort(port);
95 m_ports.append(t: port);
96}
97
98void QShaderNode::removePort(const QShaderNodePort &port)
99{
100 const auto it = std::find_if(first: m_ports.begin(), last: m_ports.end(),
101 pred: [port](const QShaderNodePort &p) {
102 return p.name == port.name;
103 });
104 if (it != m_ports.end())
105 m_ports.erase(pos: it);
106}
107
108QStringList QShaderNode::parameterNames() const
109{
110 return m_parameters.keys();
111}
112
113QVariant QShaderNode::parameter(const QString &name) const
114{
115 return m_parameters.value(akey: name);
116}
117
118void QShaderNode::setParameter(const QString &name, const QVariant &value)
119{
120 m_parameters.insert(akey: name, avalue: value);
121}
122
123void QShaderNode::clearParameter(const QString &name)
124{
125 m_parameters.remove(akey: name);
126}
127
128void QShaderNode::addRule(const QShaderFormat &format, const QShaderNode::Rule &rule)
129{
130 removeRule(format);
131 m_rules << qMakePair(x: format, y: rule);
132}
133
134void QShaderNode::removeRule(const QShaderFormat &format)
135{
136 const auto it = std::find_if(first: m_rules.begin(), last: m_rules.end(),
137 pred: [format](const QPair<QShaderFormat, Rule> &entry) {
138 return entry.first == format;
139 });
140 if (it != m_rules.end())
141 m_rules.erase(pos: it);
142}
143
144QVector<QShaderFormat> QShaderNode::availableFormats() const
145{
146 auto res = QVector<QShaderFormat>();
147 std::transform(first: m_rules.cbegin(), last: m_rules.cend(),
148 result: std::back_inserter(x&: res),
149 unary_op: [](const QPair<QShaderFormat, Rule> &entry) { return entry.first; });
150 return res;
151}
152
153QShaderNode::Rule QShaderNode::rule(const QShaderFormat &format) const
154{
155 const QPair<QShaderFormat, Rule> *selected = nullptr;
156 for (auto it = m_rules.crbegin(); it != m_rules.crend(); ++it) {
157 const auto &entry = *it;
158 if (format.supports(other: entry.first)) {
159 if (!selected || entry.first.version() > selected->first.version())
160 selected = &entry;
161 }
162 }
163 return selected ? selected->second : Rule();
164}
165
166QShaderNode::Rule::Rule(const QByteArray &subs, const QByteArrayList &snippets) noexcept
167 : substitution(subs),
168 headerSnippets(snippets)
169{
170}
171
172bool operator==(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) noexcept
173{
174 return lhs.substitution == rhs.substitution
175 && lhs.headerSnippets == rhs.headerSnippets;
176}
177}
178QT_END_NAMESPACE
179

source code of qt3d/src/render/shadergraph/qshadernode.cpp