aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/effectcomposer/compositionnode.cpp
blob: ba3dd38cfcdffcf3f83b45e241fc9ed68a56b3a9 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "compositionnode.h"

#include "effectcomposeruniformsmodel.h"
#include "effectshaderscodeeditor.h"
#include "effectutils.h"
#include "propertyhandler.h"
#include "uniform.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QRegularExpression>
#include <QRegularExpressionMatch>

namespace {

struct CodeRename
{
    CodeRename(const QString &oldName, const QString &newName)
        : m_newName(newName)
        , m_regex{QString(R"(\b%1\b)").arg(oldName)}
    {}

    QString operator()(QString code) const { return code.replace(m_regex, m_newName); }

    void operator()(QTextDocument *document) const
    {
        QTextCursor docCursor(document);
        bool documentChanged = false;
        QTextBlock block = document->lastBlock();
        while (block.isValid()) {
            QString blockText = block.text();
            QRegularExpressionMatch match = m_regex.match(blockText);
            if (match.hasMatch()) {
                if (!documentChanged) {
                    docCursor.beginEditBlock();
                    documentChanged = true;
                }
                blockText.replace(m_regex, m_newName);
                QTextCursor blockCursor(block);
                blockCursor.movePosition(QTextCursor::StartOfBlock);
                blockCursor.insertText(blockText);
                blockCursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
                blockCursor.removeSelectedText();
            }
            block = block.previous();
        }
        if (documentChanged)
            docCursor.endEditBlock();
    }

    const QString m_newName;
    const QRegularExpression m_regex;
};

} // namespace

namespace EffectComposer {

CompositionNode::CompositionNode(const QString &effectName, const QString &qenPath,
                                 const QJsonObject &jsonObject)
{
    QJsonObject json;
    if (jsonObject.isEmpty()) {
        QFile qenFile(qenPath);
        if (!qenFile.open(QIODevice::ReadOnly)) {
            qWarning("Couldn't open effect file.");
            return;
        }

        QByteArray loadData = qenFile.readAll();
        QJsonParseError parseError;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(loadData, &parseError));

        if (parseError.error != QJsonParseError::NoError) {
            QString error = QString("Error parsing effect node");
            QString errorDetails = QString("%1: %2").arg(parseError.offset).arg(parseError.errorString());
            qWarning() << error;
            qWarning() << errorDetails;
            return;
        }
        json = jsonDoc.object().value("QEN").toObject();
        parse(effectName, qenPath, json);
    }
    else {
        parse(effectName, "", jsonObject);
    }

    connect(
        &m_uniformsModel,
        &QAbstractItemModel::rowsRemoved,
        this,
        &CompositionNode::rebakeRequested);

    connect(
        &m_uniformsModel,
        &EffectComposerUniformsModel::uniformRenamed,
        this,
        &CompositionNode::onUniformRenamed);
}

CompositionNode::~CompositionNode()
{
    EffectShadersCodeEditor::instance()->cleanFromData(m_shaderEditorData.get());
};

QString CompositionNode::fragmentCode() const
{
    return m_fragmentCode;
}

QString CompositionNode::vertexCode() const
{
    return m_vertexCode;
}

QString CompositionNode::description() const
{
    return m_description;
}

QString CompositionNode::id() const
{
    return m_id;
}

QObject *CompositionNode::uniformsModel()
{
    return &m_uniformsModel;
}

QStringList CompositionNode::requiredNodes() const
{
    return m_requiredNodes;
}

bool CompositionNode::isEnabled() const
{
    return m_isEnabled;
}

void CompositionNode::setIsEnabled(bool newIsEnabled)
{
    if (newIsEnabled != m_isEnabled) {
        m_isEnabled = newIsEnabled;
        emit isEnabledChanged();
    }
}

bool CompositionNode::isDependency() const
{
    return m_refCount > 0;
}

bool CompositionNode::isCustom() const
{
    return m_isCustom;
}

void CompositionNode::setCustom(bool enable)
{
    if (enable != m_isCustom) {
        m_isCustom = enable;
        emit isCustomChanged();
    }
}

CompositionNode::NodeType CompositionNode::type() const
{
    return m_type;
}

void CompositionNode::parse(const QString &effectName, const QString &qenPath, const QJsonObject &json)
{
    int version = -1;
    if (json.contains("version"))
        version = json["version"].toInt(-1);
    if (version != 1) {
        QString error = QString("Error: Unknown effect version (%1)").arg(version);
        qWarning() << qPrintable(error);
        return;
    }

    m_name = json.value("name").toString();
    m_description = json.value("description").toString();
    setFragmentCode(EffectUtils::codeFromJsonArray(json.value("fragmentCode").toArray()));
    setVertexCode(EffectUtils::codeFromJsonArray(json.value("vertexCode").toArray()));

    if (json.contains("extraMargin"))
        m_extraMargin = json.value("extraMargin").toInt();

    if (json.contains("enabled"))
        m_isEnabled = json["enabled"].toBool();

    if (json.contains("custom"))
        m_isCustom = json["custom"].toBool();

    m_id = json.value("id").toString();
    if (m_id.isEmpty() && !qenPath.isEmpty()) {
        QString fileName = qenPath.split('/').last();
        fileName.chop(4); // remove ".qen"
        m_id = fileName;
    }

    // parse properties
    const QJsonArray &jsonProps = json.value("properties").toArray();
    for (const QJsonValueConstRef &prop : jsonProps) {
        const auto uniform = new Uniform(effectName, prop.toObject(), qenPath);
        m_uniformsModel.addUniform(uniform);
        g_propertyData()->insert(uniform->name(), uniform->value());
        if (uniform->type() == Uniform::Type::Define) {
            // Changing defines requires rebaking the shaders
            connect(uniform, &Uniform::uniformValueChanged, this, &CompositionNode::rebakeRequested);
        }
    }

    // Seek through code to get tags
    QStringList shaderCodeLines;
    shaderCodeLines += m_vertexCode.split('\n');
    shaderCodeLines += m_fragmentCode.split('\n');
    for (const QString &codeLine : std::as_const(shaderCodeLines)) {
        QString trimmedLine = codeLine.trimmed();
        if (trimmedLine.startsWith("@requires")) {
            // Get the required node, remove "@requires "
            QString nodeId = trimmedLine.sliced(10).toLower();
            if (!nodeId.isEmpty() && !m_requiredNodes.contains(nodeId))
                m_requiredNodes << nodeId;
        }
    }
}

void CompositionNode::ensureCodeEditorData()
{
    using TextEditor::TextDocument;
    if (m_shaderEditorData)
        return;

    m_shaderEditorData.reset(EffectShadersCodeEditor::instance()
                                 ->createEditorData(fragmentCode(), vertexCode(), &m_uniformsModel));

    connect(m_shaderEditorData->fragmentDocument.get(), &TextDocument::contentsChanged, this, [this] {
        setFragmentCode(m_shaderEditorData->fragmentDocument->plainText());
    });

    connect(m_shaderEditorData->vertexDocument.get(), &TextDocument::contentsChanged, this, [this] {
        setVertexCode(m_shaderEditorData->vertexDocument->plainText());
    });
}

void CompositionNode::requestRebakeIfLiveUpdateMode()
{
    if (EffectShadersCodeEditor::instance()->liveUpdate())
        emit rebakeRequested();
}

QList<Uniform *> CompositionNode::uniforms() const
{
    return m_uniformsModel.uniforms();
}

int CompositionNode::incRefCount()
{
    ++m_refCount;

    if (m_refCount == 1)
        emit isDepencyChanged();

    return m_refCount;
}

int CompositionNode::decRefCount()
{
    --m_refCount;

    if (m_refCount == 0)
        emit isDepencyChanged();

    return m_refCount;
}

void CompositionNode::setRefCount(int count)
{
    bool notifyChange = (m_refCount > 0 && count == 0) || (m_refCount <= 0 && count > 0);

    m_refCount = count;

    if (notifyChange)
        emit isDepencyChanged();
}

void CompositionNode::setFragmentCode(const QString &fragmentCode)
{
    if (m_fragmentCode == fragmentCode)
        return;

    m_fragmentCode = fragmentCode;
    m_InUseCheckNeeded = true;
    emit fragmentCodeChanged();

    requestRebakeIfLiveUpdateMode();
}

void CompositionNode::setVertexCode(const QString &vertexCode)
{
    if (m_vertexCode == vertexCode)
        return;

    m_vertexCode = vertexCode;
    m_InUseCheckNeeded = true;
    emit vertexCodeChanged();

    requestRebakeIfLiveUpdateMode();
}

void CompositionNode::markAsSaved()
{
    if (!m_shaderEditorData)
        return;

    m_shaderEditorData->fragmentDocument->document()->setModified(false);
    m_shaderEditorData->vertexDocument->document()->setModified(false);
}

void CompositionNode::openCodeEditor()
{
    auto editor = EffectShadersCodeEditor::instance();
    ensureCodeEditorData();
    editor->setupShader(m_shaderEditorData.get());
    editor->showWidget();
}

void CompositionNode::addUniform(const QVariantMap &data)
{
    const auto uniform = new Uniform({}, QJsonObject::fromVariantMap(data), {});
    g_propertyData()->insert(uniform->name(), uniform->value());
    m_uniformsModel.addUniform(uniform);
    updateAreUniformsInUse(true);
}

void CompositionNode::updateUniform(int index, const QVariantMap &data)
{
    QTC_ASSERT(index < uniforms().size() && index >= 0, return);

    const auto uniform = new Uniform({}, QJsonObject::fromVariantMap(data), {});

    g_propertyData()->insert(uniform->name(), uniform->value());
    m_uniformsModel.updateUniform(index, uniform);
    updateAreUniformsInUse(true);
}

void CompositionNode::updateAreUniformsInUse(bool force)
{
    if (force || m_InUseCheckNeeded) {
        const QString matchTemplate("\\b%1\\b");
        const QList<Uniform *> uniList = uniforms();

        // Some of the uniforms may only be used by customValue properties
        QString customValues;
        for (const Uniform *u : uniList) {
            if (!u->customValue().isEmpty()) {
                customValues.append(u->customValue());
                customValues.append(' ');
            }
        }

        for (int i = 0; i < uniList.size(); ++i) {
            Uniform *u = uniList[i];
            QString pattern = matchTemplate.arg(QRegularExpression::escape(u->name()));
            QRegularExpression regex(pattern);
            bool found = false;
            found = regex.match(m_fragmentCode).hasMatch();
            if (!found)
                found = regex.match(m_vertexCode).hasMatch();
            if (!found && !customValues.isEmpty())
                found = regex.match(customValues).hasMatch();
            m_uniformsModel.setData(m_uniformsModel.index(i), found,
                                    EffectComposerUniformsModel::IsInUse);
        }
        m_InUseCheckNeeded = false;
    }
}

void CompositionNode::onUniformRenamed(const QString &oldName, const QString &newName)
{
    CodeRename codeRename{oldName, newName};
    if (m_shaderEditorData) {
        codeRename(m_shaderEditorData->vertexDocument->document());
        codeRename(m_shaderEditorData->fragmentDocument->document());
    } else {
        setVertexCode(codeRename(vertexCode()));
        setFragmentCode(codeRename(fragmentCode()));
    }
}

QString CompositionNode::name() const
{
    return m_name;
}

void CompositionNode::setName(const QString &name)
{
    if (m_name == name)
        return;

    m_name = name;
    emit nameChanged();
}

} // namespace EffectComposer