summaryrefslogtreecommitdiffstats
path: root/src/render/shadergraph/qshadergraphloader.cpp
blob: 8194c8594165a2f9c86301798d0df4007ab5d80b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qshadergraphloader_p.h"

#include "qshadernodesloader_p.h"

#include <QtCore/qdebug.h>
#include <QtCore/qiodevice.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qmetaobject.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender
{
void qt_register_ShaderLanguage_enums();

QShaderGraphLoader::QShaderGraphLoader() noexcept
    : m_status(Null),
      m_device(nullptr)
{
    qt_register_ShaderLanguage_enums();
}

QShaderGraphLoader::Status QShaderGraphLoader::status() const noexcept
{
    return m_status;
}

QShaderGraph QShaderGraphLoader::graph() const noexcept
{
    return m_graph;
}

QIODevice *QShaderGraphLoader::device() const noexcept
{
    return m_device;
}

void QShaderGraphLoader::setDevice(QIODevice *device) noexcept
{
    m_device = device;
    m_graph = QShaderGraph();
    m_status = !m_device ? Null
             : (m_device->openMode() & QIODevice::ReadOnly) ? Waiting
             : Error;
}

QHash<QString, QShaderNode> QShaderGraphLoader::prototypes() const noexcept
{
    return m_prototypes;
}

void QShaderGraphLoader::setPrototypes(const QHash<QString, QShaderNode> &prototypes) noexcept
{
    m_prototypes = prototypes;
}

void QShaderGraphLoader::load()
{
    if (m_status == Error)
        return;

    auto error = QJsonParseError();
    const QJsonDocument document = QJsonDocument::fromJson(m_device->readAll(), &error);

    if (error.error != QJsonParseError::NoError) {
        qWarning() << "Invalid JSON document:" << error.errorString();
        m_status = Error;
        return;
    }

    if (document.isEmpty() || !document.isObject()) {
        qWarning() << "Invalid JSON document, root should be an object";
        m_status = Error;
        return;
    }

    const QJsonObject root = document.object();

    const QJsonValue nodesValue = root.value(QStringLiteral("nodes"));
    if (!nodesValue.isArray()) {
        qWarning() << "Invalid nodes property, should be an array";
        m_status = Error;
        return;
    }

    const QJsonValue edgesValue = root.value(QStringLiteral("edges"));
    if (!edgesValue.isArray()) {
        qWarning() << "Invalid edges property, should be an array";
        m_status = Error;
        return;
    }

    bool hasError = false;

    const QJsonValue prototypesValue = root.value(QStringLiteral("prototypes"));
    if (!prototypesValue.isUndefined()) {
        if (prototypesValue.isObject()) {
            QShaderNodesLoader loader;
            loader.load(prototypesValue.toObject());
            m_prototypes.insert(loader.nodes());
        } else {
            qWarning() << "Invalid prototypes property, should be an object";
            m_status = Error;
            return;
        }
    }

    const QJsonArray nodes = nodesValue.toArray();
    for (const QJsonValue nodeValue : nodes) {
        if (!nodeValue.isObject()) {
            qWarning() << "Invalid node found";
            hasError = true;
            continue;
        }

        const QJsonObject nodeObject = nodeValue.toObject();

        const QString uuidString = nodeObject.value(QStringLiteral("uuid")).toString();
        const QUuid uuid = QUuid(uuidString);
        if (uuid.isNull()) {
            qWarning() << "Invalid UUID found in node:" << uuidString;
            hasError = true;
            continue;
        }

        const QString type = nodeObject.value(QStringLiteral("type")).toString();
        if (!m_prototypes.contains(type)) {
            qWarning() << "Unsupported node type found:" << type;
            hasError = true;
            continue;
        }

        const QJsonArray layersArray = nodeObject.value(QStringLiteral("layers")).toArray();
        auto layers = QStringList();
        for (const QJsonValue layerValue : layersArray)
            layers.append(layerValue.toString());

        QShaderNode node = m_prototypes.value(type);
        node.setUuid(uuid);
        node.setLayers(layers);

        const QJsonValue parametersValue = nodeObject.value(QStringLiteral("parameters"));
        if (parametersValue.isObject()) {
            const QJsonObject parametersObject = parametersValue.toObject();
            for (const QString &parameterName : parametersObject.keys()) {
                const QJsonValue parameterValue = parametersObject.value(parameterName);
                if (parameterValue.isObject()) {
                    const QJsonObject parameterObject = parameterValue.toObject();
                    const QString type = parameterObject.value(QStringLiteral("type")).toString();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                    const QMetaType typeId = QMetaType::fromName(type.toUtf8());
#else
                    const QMetaType typeId(QMetaType::type(type.toUtf8()));
#endif

                    const QString value = parameterObject.value(QStringLiteral("value")).toString();
                    auto variant = QVariant(value);

                    if (typeId.flags() & QMetaType::IsEnumeration) {
                        const QMetaObject *metaObject = typeId.metaObject();
                        const char *className = metaObject->className();
                        const QByteArray enumName = type.mid(static_cast<int>(qstrlen(className)) + 2).toUtf8();
                        const QMetaEnum metaEnum = metaObject->enumerator(metaObject->indexOfEnumerator(enumName));
                        const int enumValue = metaEnum.keyToValue(value.toUtf8());
                        variant = QVariant(enumValue);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                        variant.convert(typeId);
#else
                        variant.convert(typeId.id());
#endif
                    } else {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
                        variant.convert(typeId);
#else
                        variant.convert(typeId.id());
#endif
                    }
                    node.setParameter(parameterName, variant);
                } else {
                    node.setParameter(parameterName, parameterValue.toVariant());
                }
            }
        }

        m_graph.addNode(node);
    }

    const QJsonArray edges = edgesValue.toArray();
    for (const QJsonValue edgeValue : edges) {
        if (!edgeValue.isObject()) {
            qWarning() << "Invalid edge found";
            hasError = true;
            continue;
        }

        const QJsonObject edgeObject = edgeValue.toObject();

        const QString sourceUuidString = edgeObject.value(QStringLiteral("sourceUuid")).toString();
        const QUuid sourceUuid = QUuid(sourceUuidString);
        if (sourceUuid.isNull()) {
            qWarning() << "Invalid source UUID found in edge:" << sourceUuidString;
            hasError = true;
            continue;
        }

        const QString sourcePort = edgeObject.value(QStringLiteral("sourcePort")).toString();

        const QString targetUuidString = edgeObject.value(QStringLiteral("targetUuid")).toString();
        const QUuid targetUuid = QUuid(targetUuidString);
        if (targetUuid.isNull()) {
            qWarning() << "Invalid target UUID found in edge:" << targetUuidString;
            hasError = true;
            continue;
        }

        const QString targetPort = edgeObject.value(QStringLiteral("targetPort")).toString();

        const QJsonArray layersArray = edgeObject.value(QStringLiteral("layers")).toArray();
        auto layers = QStringList();
        for (const QJsonValue layerValue : layersArray)
            layers.append(layerValue.toString());

        auto edge = QShaderGraph::Edge();
        edge.sourceNodeUuid = sourceUuid;
        edge.sourcePortName = sourcePort;
        edge.targetNodeUuid = targetUuid;
        edge.targetPortName = targetPort;
        edge.layers = layers;
        m_graph.addEdge(edge);
    }

    if (hasError) {
        m_status = Error;
        m_graph = QShaderGraph();
    } else {
        m_status = Ready;
    }
}
}
QT_END_NAMESPACE