1// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qshadernode_p.h"
5
6QT_BEGIN_NAMESPACE
7
8namespace Qt3DRender
9{
10QShaderNode::Type QShaderNode::type() const noexcept
11{
12 int inputCount = 0;
13 int outputCount = 0;
14 for (const auto &port : std::as_const(t: m_ports)) {
15 switch (port.direction) {
16 case QShaderNodePort::Input:
17 inputCount++;
18 break;
19 case QShaderNodePort::Output:
20 outputCount++;
21 break;
22 }
23 }
24
25 return (inputCount == 0 && outputCount == 0) ? Invalid
26 : (inputCount > 0 && outputCount == 0) ? Output
27 : (inputCount == 0 && outputCount > 0) ? Input
28 : Function;
29}
30
31QUuid QShaderNode::uuid() const noexcept
32{
33 return m_uuid;
34}
35
36void QShaderNode::setUuid(const QUuid &uuid) noexcept
37{
38 m_uuid = uuid;
39}
40
41QStringList QShaderNode::layers() const noexcept
42{
43 return m_layers;
44}
45
46void QShaderNode::setLayers(const QStringList &layers) noexcept
47{
48 m_layers = layers;
49}
50
51QList<QShaderNodePort> QShaderNode::ports() const noexcept
52{
53 return m_ports;
54}
55
56void QShaderNode::addPort(const QShaderNodePort &port)
57{
58 removePort(port);
59 m_ports.append(t: port);
60}
61
62void QShaderNode::removePort(const QShaderNodePort &port)
63{
64 const auto it = std::find_if(first: m_ports.begin(), last: m_ports.end(),
65 pred: [port](const QShaderNodePort &p) {
66 return p.name == port.name;
67 });
68 if (it != m_ports.end())
69 m_ports.erase(pos: it);
70}
71
72QStringList QShaderNode::parameterNames() const
73{
74 return m_parameters.keys();
75}
76
77QVariant QShaderNode::parameter(const QString &name) const
78{
79 return m_parameters.value(key: name);
80}
81
82void QShaderNode::setParameter(const QString &name, const QVariant &value)
83{
84 m_parameters.insert(key: name, value);
85}
86
87void QShaderNode::clearParameter(const QString &name)
88{
89 m_parameters.remove(key: name);
90}
91
92void QShaderNode::addRule(const QShaderFormat &format, const QShaderNode::Rule &rule)
93{
94 removeRule(format);
95 m_rules << qMakePair(value1: format, value2: rule);
96}
97
98void QShaderNode::removeRule(const QShaderFormat &format)
99{
100 const auto it = std::find_if(first: m_rules.begin(), last: m_rules.end(),
101 pred: [format](const QPair<QShaderFormat, Rule> &entry) {
102 return entry.first == format;
103 });
104 if (it != m_rules.end())
105 m_rules.erase(pos: it);
106}
107
108QList<QShaderFormat> QShaderNode::availableFormats() const
109{
110 auto res = QList<QShaderFormat>();
111 std::transform(first: m_rules.cbegin(), last: m_rules.cend(),
112 result: std::back_inserter(x&: res),
113 unary_op: [](const QPair<QShaderFormat, Rule> &entry) { return entry.first; });
114 return res;
115}
116
117QShaderNode::Rule QShaderNode::rule(const QShaderFormat &format) const
118{
119 const QPair<QShaderFormat, Rule> *selected = nullptr;
120 for (auto it = m_rules.crbegin(); it != m_rules.crend(); ++it) {
121 const auto &entry = *it;
122 if (format.supports(other: entry.first)) {
123 if (!selected || entry.first.version() > selected->first.version())
124 selected = &entry;
125 }
126 }
127 return selected ? selected->second : Rule();
128}
129
130QShaderNode::Rule::Rule(const QByteArray &subs, const QByteArrayList &snippets) noexcept
131 : substitution(subs),
132 headerSnippets(snippets)
133{
134}
135
136bool operator==(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) noexcept
137{
138 return lhs.substitution == rhs.substitution
139 && lhs.headerSnippets == rhs.headerSnippets;
140}
141}
142QT_END_NAMESPACE
143

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