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 "qshadergraphloader_p.h"
41
42#include "qshadernodesloader_p.h"
43
44#include <QtCore/qdebug.h>
45#include <QtCore/qiodevice.h>
46#include <QtCore/qjsonarray.h>
47#include <QtCore/qjsondocument.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qmetaobject.h>
50
51QT_BEGIN_NAMESPACE
52
53namespace Qt3DRender
54{
55void qt_register_ShaderLanguage_enums();
56
57QShaderGraphLoader::QShaderGraphLoader() noexcept
58 : m_status(Null),
59 m_device(nullptr)
60{
61 qt_register_ShaderLanguage_enums();
62}
63
64QShaderGraphLoader::Status QShaderGraphLoader::status() const noexcept
65{
66 return m_status;
67}
68
69QShaderGraph QShaderGraphLoader::graph() const noexcept
70{
71 return m_graph;
72}
73
74QIODevice *QShaderGraphLoader::device() const noexcept
75{
76 return m_device;
77}
78
79void QShaderGraphLoader::setDevice(QIODevice *device) noexcept
80{
81 m_device = device;
82 m_graph = QShaderGraph();
83 m_status = !m_device ? Null
84 : (m_device->openMode() & QIODevice::ReadOnly) ? Waiting
85 : Error;
86}
87
88QHash<QString, QShaderNode> QShaderGraphLoader::prototypes() const noexcept
89{
90 return m_prototypes;
91}
92
93void QShaderGraphLoader::setPrototypes(const QHash<QString, QShaderNode> &prototypes) noexcept
94{
95 m_prototypes = prototypes;
96}
97
98void QShaderGraphLoader::load()
99{
100 if (m_status == Error)
101 return;
102
103 auto error = QJsonParseError();
104 const QJsonDocument document = QJsonDocument::fromJson(json: m_device->readAll(), error: &error);
105
106 if (error.error != QJsonParseError::NoError) {
107 qWarning() << "Invalid JSON document:" << error.errorString();
108 m_status = Error;
109 return;
110 }
111
112 if (document.isEmpty() || !document.isObject()) {
113 qWarning() << "Invalid JSON document, root should be an object";
114 m_status = Error;
115 return;
116 }
117
118 const QJsonObject root = document.object();
119
120 const QJsonValue nodesValue = root.value(QStringLiteral("nodes"));
121 if (!nodesValue.isArray()) {
122 qWarning() << "Invalid nodes property, should be an array";
123 m_status = Error;
124 return;
125 }
126
127 const QJsonValue edgesValue = root.value(QStringLiteral("edges"));
128 if (!edgesValue.isArray()) {
129 qWarning() << "Invalid edges property, should be an array";
130 m_status = Error;
131 return;
132 }
133
134 bool hasError = false;
135
136 const QJsonValue prototypesValue = root.value(QStringLiteral("prototypes"));
137 if (!prototypesValue.isUndefined()) {
138 if (prototypesValue.isObject()) {
139 QShaderNodesLoader loader;
140 loader.load(prototypesObject: prototypesValue.toObject());
141 m_prototypes.insert(hash: loader.nodes());
142 } else {
143 qWarning() << "Invalid prototypes property, should be an object";
144 m_status = Error;
145 return;
146 }
147 }
148
149 const QJsonArray nodes = nodesValue.toArray();
150 for (const QJsonValue &nodeValue : nodes) {
151 if (!nodeValue.isObject()) {
152 qWarning() << "Invalid node found";
153 hasError = true;
154 continue;
155 }
156
157 const QJsonObject nodeObject = nodeValue.toObject();
158
159 const QString uuidString = nodeObject.value(QStringLiteral("uuid")).toString();
160 const QUuid uuid = QUuid(uuidString);
161 if (uuid.isNull()) {
162 qWarning() << "Invalid UUID found in node:" << uuidString;
163 hasError = true;
164 continue;
165 }
166
167 const QString type = nodeObject.value(QStringLiteral("type")).toString();
168 if (!m_prototypes.contains(akey: type)) {
169 qWarning() << "Unsupported node type found:" << type;
170 hasError = true;
171 continue;
172 }
173
174 const QJsonArray layersArray = nodeObject.value(QStringLiteral("layers")).toArray();
175 auto layers = QStringList();
176 for (const QJsonValue &layerValue : layersArray) {
177 layers.append(t: layerValue.toString());
178 }
179
180 QShaderNode node = m_prototypes.value(akey: type);
181 node.setUuid(uuid);
182 node.setLayers(layers);
183
184 const QJsonValue parametersValue = nodeObject.value(QStringLiteral("parameters"));
185 if (parametersValue.isObject()) {
186 const QJsonObject parametersObject = parametersValue.toObject();
187 for (const QString &parameterName : parametersObject.keys()) {
188 const QJsonValue parameterValue = parametersObject.value(key: parameterName);
189 if (parameterValue.isObject()) {
190 const QJsonObject parameterObject = parameterValue.toObject();
191 const QString type = parameterObject.value(QStringLiteral("type")).toString();
192 const int typeId = QMetaType::type(typeName: type.toUtf8());
193
194 const QString value = parameterObject.value(QStringLiteral("value")).toString();
195 auto variant = QVariant(value);
196
197 if (QMetaType::typeFlags(type: typeId) & QMetaType::IsEnumeration) {
198 const QMetaObject *metaObject = QMetaType::metaObjectForType(type: typeId);
199 const char *className = metaObject->className();
200 const QByteArray enumName = type.mid(position: static_cast<int>(qstrlen(str: className)) + 2).toUtf8();
201 const QMetaEnum metaEnum = metaObject->enumerator(index: metaObject->indexOfEnumerator(name: enumName));
202 const int enumValue = metaEnum.keyToValue(key: value.toUtf8());
203 variant = QVariant(enumValue);
204 variant.convert(targetTypeId: typeId);
205 } else {
206 variant.convert(targetTypeId: typeId);
207 }
208 node.setParameter(name: parameterName, value: variant);
209 } else {
210 node.setParameter(name: parameterName, value: parameterValue.toVariant());
211 }
212 }
213 }
214
215 m_graph.addNode(node);
216 }
217
218 const QJsonArray edges = edgesValue.toArray();
219 for (const QJsonValue &edgeValue : edges) {
220 if (!edgeValue.isObject()) {
221 qWarning() << "Invalid edge found";
222 hasError = true;
223 continue;
224 }
225
226 const QJsonObject edgeObject = edgeValue.toObject();
227
228 const QString sourceUuidString = edgeObject.value(QStringLiteral("sourceUuid")).toString();
229 const QUuid sourceUuid = QUuid(sourceUuidString);
230 if (sourceUuid.isNull()) {
231 qWarning() << "Invalid source UUID found in edge:" << sourceUuidString;
232 hasError = true;
233 continue;
234 }
235
236 const QString sourcePort = edgeObject.value(QStringLiteral("sourcePort")).toString();
237
238 const QString targetUuidString = edgeObject.value(QStringLiteral("targetUuid")).toString();
239 const QUuid targetUuid = QUuid(targetUuidString);
240 if (targetUuid.isNull()) {
241 qWarning() << "Invalid target UUID found in edge:" << targetUuidString;
242 hasError = true;
243 continue;
244 }
245
246 const QString targetPort = edgeObject.value(QStringLiteral("targetPort")).toString();
247
248 const QJsonArray layersArray = edgeObject.value(QStringLiteral("layers")).toArray();
249 auto layers = QStringList();
250 for (const QJsonValue &layerValue : layersArray) {
251 layers.append(t: layerValue.toString());
252 }
253
254 auto edge = QShaderGraph::Edge();
255 edge.sourceNodeUuid = sourceUuid;
256 edge.sourcePortName = sourcePort;
257 edge.targetNodeUuid = targetUuid;
258 edge.targetPortName = targetPort;
259 edge.layers = layers;
260 m_graph.addEdge(edge);
261 }
262
263 if (hasError) {
264 m_status = Error;
265 m_graph = QShaderGraph();
266 } else {
267 m_status = Ready;
268 }
269}
270}
271QT_END_NAMESPACE
272

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