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

source code of qtbase/src/gui/util/qshadergraphloader.cpp