2023-08-16 11:35:10 +03:00
|
|
|
// 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"
|
|
|
|
|
|
2023-08-18 15:39:40 +03:00
|
|
|
#include "effectutils.h"
|
2024-01-26 14:55:50 +02:00
|
|
|
#include "effectcomposeruniformsmodel.h"
|
2023-10-18 18:33:42 +03:00
|
|
|
#include "propertyhandler.h"
|
2023-08-17 18:38:40 +03:00
|
|
|
#include "uniform.h"
|
|
|
|
|
|
2023-08-29 16:24:43 +03:00
|
|
|
#include <QFile>
|
2023-08-17 15:59:46 +03:00
|
|
|
#include <QJsonArray>
|
2023-08-16 11:35:10 +03:00
|
|
|
#include <QJsonDocument>
|
|
|
|
|
|
2024-01-26 14:55:50 +02:00
|
|
|
namespace EffectComposer {
|
2023-08-16 11:35:10 +03:00
|
|
|
|
2023-12-07 18:32:55 +02:00
|
|
|
CompositionNode::CompositionNode(const QString &effectName, const QString &qenPath,
|
|
|
|
|
const QJsonObject &jsonObject)
|
2023-08-16 11:35:10 +03:00
|
|
|
{
|
2023-11-14 14:49:42 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2023-08-16 11:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::fragmentCode() const
|
|
|
|
|
{
|
|
|
|
|
return m_fragmentCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::vertexCode() const
|
|
|
|
|
{
|
|
|
|
|
return m_vertexCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::description() const
|
|
|
|
|
{
|
|
|
|
|
return m_description;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 18:32:55 +02:00
|
|
|
QString CompositionNode::id() const
|
|
|
|
|
{
|
|
|
|
|
return m_id;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 11:52:11 +03:00
|
|
|
QObject *CompositionNode::uniformsModel()
|
|
|
|
|
{
|
|
|
|
|
return &m_unifomrsModel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 13:30:36 +03:00
|
|
|
QStringList CompositionNode::requiredNodes() const
|
|
|
|
|
{
|
|
|
|
|
return m_requiredNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 16:43:56 +03:00
|
|
|
bool CompositionNode::isEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_isEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CompositionNode::setIsEnabled(bool newIsEnabled)
|
|
|
|
|
{
|
2023-09-06 15:18:02 +03:00
|
|
|
if (newIsEnabled != m_isEnabled) {
|
|
|
|
|
m_isEnabled = newIsEnabled;
|
|
|
|
|
emit isEnabledChanged();
|
|
|
|
|
}
|
2023-09-01 16:43:56 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-07 18:32:55 +02:00
|
|
|
bool CompositionNode::isDependency() const
|
|
|
|
|
{
|
|
|
|
|
return m_refCount > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 16:43:56 +03:00
|
|
|
CompositionNode::NodeType CompositionNode::type() const
|
|
|
|
|
{
|
|
|
|
|
return m_type;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 14:49:42 +02:00
|
|
|
void CompositionNode::parse(const QString &effectName, const QString &qenPath, const QJsonObject &json)
|
2023-08-16 11:35:10 +03:00
|
|
|
{
|
2023-08-17 18:38:40 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 15:39:40 +03:00
|
|
|
m_name = json.value("name").toString();
|
2023-08-17 18:38:40 +03:00
|
|
|
m_description = json.value("description").toString();
|
2023-08-18 15:39:40 +03:00
|
|
|
m_fragmentCode = EffectUtils::codeFromJsonArray(json.value("fragmentCode").toArray());
|
|
|
|
|
m_vertexCode = EffectUtils::codeFromJsonArray(json.value("vertexCode").toArray());
|
2023-08-17 15:59:46 +03:00
|
|
|
|
2024-01-17 12:04:00 +02:00
|
|
|
if (json.contains("enabled"))
|
|
|
|
|
m_isEnabled = json["enabled"].toBool();
|
|
|
|
|
|
2023-12-07 18:32:55 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 15:59:46 +03:00
|
|
|
// parse properties
|
2023-08-28 13:30:36 +03:00
|
|
|
QJsonArray jsonProps = json.value("properties").toArray();
|
2023-10-18 18:33:42 +03:00
|
|
|
for (const auto /*QJsonValueRef*/ &prop : jsonProps) {
|
2023-11-14 14:49:42 +02:00
|
|
|
const auto uniform = new Uniform(effectName, prop.toObject(), qenPath);
|
2023-10-18 18:33:42 +03:00
|
|
|
m_unifomrsModel.addUniform(uniform);
|
2023-11-08 16:12:35 +02:00
|
|
|
m_uniforms.append(uniform);
|
2023-10-18 18:33:42 +03:00
|
|
|
g_propertyData.insert(uniform->name(), uniform->value());
|
2024-02-02 13:08:01 +02:00
|
|
|
if (uniform->type() == Uniform::Type::Define) {
|
|
|
|
|
// Changing defines requires rebaking the shaders
|
|
|
|
|
connect(uniform, &Uniform::uniformValueChanged, this, &CompositionNode::rebakeRequested);
|
|
|
|
|
}
|
2023-10-18 18:33:42 +03:00
|
|
|
}
|
2023-08-30 13:43:24 +03:00
|
|
|
|
|
|
|
|
// 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")) {
|
2023-12-07 18:32:55 +02:00
|
|
|
// Get the required node, remove "@requires "
|
2023-12-15 12:56:17 +02:00
|
|
|
QString nodeId = trimmedLine.sliced(10).toLower();
|
|
|
|
|
if (!nodeId.isEmpty() && !m_requiredNodes.contains(nodeId))
|
|
|
|
|
m_requiredNodes << nodeId;
|
2023-08-30 13:43:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-17 18:38:40 +03:00
|
|
|
}
|
2023-08-16 11:35:10 +03:00
|
|
|
|
2023-11-08 16:12:35 +02:00
|
|
|
QList<Uniform *> CompositionNode::uniforms() const
|
|
|
|
|
{
|
|
|
|
|
return m_uniforms;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 18:32:55 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 16:12:35 +02:00
|
|
|
QString CompositionNode::name() const
|
|
|
|
|
{
|
|
|
|
|
return m_name;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 14:55:50 +02:00
|
|
|
} // namespace EffectComposer
|
2023-09-15 15:36:15 +03:00
|
|
|
|