EffectComposer: Allow 'define' properties to specify control type

Control type is used to determine the control presented for the property
in UI. Currently only int and bool control types are supported.

Also fixed the issue that changing define wouldn't update preview.
This was because changing define requires rebaking shaders, which is
not normally triggered on property change.

Fixes: QDS-11770
Change-Id: I953d827195565f765df1a09550c4a49da9c93c29
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Miikka Heikkinen
2024-02-02 13:08:01 +02:00
parent 7efab2c07e
commit f985b1b091
9 changed files with 108 additions and 34 deletions

View File

@@ -17,24 +17,30 @@ Item {
visible: !uniformUseCustomValue visible: !uniformUseCustomValue
Component.onCompleted: { Component.onCompleted: {
if (uniformType === "int") if (uniformType === "int") {
valueLoader.source = "ValueInt.qml" valueLoader.source = "ValueInt.qml"
else if (uniformType === "vec2") } else if (uniformType === "vec2") {
valueLoader.source = "ValueVec2.qml" valueLoader.source = "ValueVec2.qml"
else if (uniformType === "vec3") } else if (uniformType === "vec3") {
valueLoader.source = "ValueVec3.qml" valueLoader.source = "ValueVec3.qml"
else if (uniformType === "vec4") } else if (uniformType === "vec4") {
valueLoader.source = "ValueVec4.qml" valueLoader.source = "ValueVec4.qml"
else if (uniformType === "bool") } else if (uniformType === "bool") {
valueLoader.source = "ValueBool.qml" valueLoader.source = "ValueBool.qml"
else if (uniformType === "color") } else if (uniformType === "color") {
valueLoader.source = "ValueColor.qml" valueLoader.source = "ValueColor.qml"
else if (uniformType === "sampler2D") } else if (uniformType === "sampler2D") {
valueLoader.source = "ValueImage.qml" valueLoader.source = "ValueImage.qml"
else if (uniformType === "define") } else if (uniformType === "define") {
valueLoader.source = "ValueDefine.qml" if (uniformControlType === "int")
else valueLoader.source = "ValueInt.qml"
else if (uniformControlType === "bool")
valueLoader.source = "ValueBool.qml"
else
valueLoader.source = "ValueDefine.qml"
} else {
valueLoader.source = "ValueFloat.qml" valueLoader.source = "ValueFloat.qml"
}
} }
RowLayout { RowLayout {

View File

@@ -130,6 +130,10 @@ void CompositionNode::parse(const QString &effectName, const QString &qenPath, c
m_unifomrsModel.addUniform(uniform); m_unifomrsModel.addUniform(uniform);
m_uniforms.append(uniform); m_uniforms.append(uniform);
g_propertyData.insert(uniform->name(), uniform->value()); 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 // Seek through code to get tags

View File

@@ -56,6 +56,7 @@ signals:
void uniformsModelChanged(); void uniformsModelChanged();
void isEnabledChanged(); void isEnabledChanged();
void isDepencyChanged(); void isDepencyChanged();
void rebakeRequested();
private: private:
void parse(const QString &effectName, const QString &qenPath, const QJsonObject &json); void parse(const QString &effectName, const QString &qenPath, const QJsonObject &json);

View File

@@ -49,6 +49,8 @@ static bool writeToFile(const QByteArray &buf, const QString &filename, FileType
EffectComposerModel::EffectComposerModel(QObject *parent) EffectComposerModel::EffectComposerModel(QObject *parent)
: QAbstractListModel{parent} : QAbstractListModel{parent}
{ {
m_rebakeTimer.setSingleShot(true);
connect(&m_rebakeTimer, &QTimer::timeout, this, &EffectComposerModel::bakeShaders);
} }
QHash<int, QByteArray> EffectComposerModel::roleNames() const QHash<int, QByteArray> EffectComposerModel::roleNames() const
@@ -107,10 +109,7 @@ void EffectComposerModel::addNode(const QString &nodeQenPath)
{ {
beginResetModel(); beginResetModel();
auto *node = new CompositionNode({}, nodeQenPath); auto *node = new CompositionNode({}, nodeQenPath);
connect(qobject_cast<EffectComposerUniformsModel *>(node->uniformsModel()), connectCompositionNode(node);
&EffectComposerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
const QList<QString> requiredNodes = node->requiredNodes(); const QList<QString> requiredNodes = node->requiredNodes();
if (requiredNodes.size() > 0) { if (requiredNodes.size() > 0) {
@@ -122,10 +121,7 @@ void EffectComposerModel::addNode(const QString &nodeQenPath)
const QString path = EffectUtils::nodesSourcesPath() + "/common/" + requiredId + ".qen"; const QString path = EffectUtils::nodesSourcesPath() + "/common/" + requiredId + ".qen";
auto requiredNode = new CompositionNode({}, path); auto requiredNode = new CompositionNode({}, path);
connect(qobject_cast<EffectComposerUniformsModel *>(requiredNode->uniformsModel()), connectCompositionNode(requiredNode);
&EffectComposerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
requiredNode->setRefCount(1); requiredNode->setRefCount(1);
m_nodes.prepend(requiredNode); m_nodes.prepend(requiredNode);
} }
@@ -167,6 +163,7 @@ void EffectComposerModel::moveNode(int fromIdx, int toIdx)
void EffectComposerModel::removeNode(int idx) void EffectComposerModel::removeNode(int idx)
{ {
beginResetModel(); beginResetModel();
m_rebakeTimer.stop();
CompositionNode *node = m_nodes.takeAt(idx); CompositionNode *node = m_nodes.takeAt(idx);
const QStringList reqNodes = node->requiredNodes(); const QStringList reqNodes = node->requiredNodes();
@@ -193,6 +190,7 @@ void EffectComposerModel::removeNode(int idx)
void EffectComposerModel::clear(bool clearName) void EffectComposerModel::clear(bool clearName)
{ {
beginResetModel(); beginResetModel();
m_rebakeTimer.stop();
qDeleteAll(m_nodes); qDeleteAll(m_nodes);
m_nodes.clear(); m_nodes.clear();
endResetModel(); endResetModel();
@@ -418,7 +416,7 @@ void EffectComposerModel::setEffectError(const QString &errorMessage, int type,
Q_EMIT effectErrorChanged(); Q_EMIT effectErrorChanged();
} }
QString variantAsDataString(const Uniform::Type type, const QVariant &variant) QString variantAsDataString(const Uniform::Type type, const Uniform::Type controlType, const QVariant &variant)
{ {
QString s; QString s;
switch (type) { switch (type) {
@@ -470,7 +468,12 @@ QString variantAsDataString(const Uniform::Type type, const QVariant &variant)
} }
case Uniform::Type::Sampler: case Uniform::Type::Sampler:
case Uniform::Type::Define: { case Uniform::Type::Define: {
s = variant.toString(); if (controlType == Uniform::Type::Int)
s = QString::number(variant.toInt());
else if (controlType == Uniform::Type::Bool)
s = variant.toBool() ? QString("true") : QString("false");
else
s = variant.toString();
break; break;
} }
} }
@@ -495,16 +498,23 @@ QJsonObject nodeToJson(const CompositionNode &node)
uniformObject.insert("name", QString(uniform->name())); uniformObject.insert("name", QString(uniform->name()));
QString type = Uniform::stringFromType(uniform->type()); QString type = Uniform::stringFromType(uniform->type());
uniformObject.insert("type", type); uniformObject.insert("type", type);
if (uniform->type() == Uniform::Type::Define) {
QString controlType = Uniform::stringFromType(uniform->controlType());
if (controlType != type)
uniformObject.insert("controlType", controlType);
}
if (!uniform->displayName().isEmpty()) if (!uniform->displayName().isEmpty())
uniformObject.insert("displayName", QString(uniform->displayName())); uniformObject.insert("displayName", QString(uniform->displayName()));
QString value = variantAsDataString(uniform->type(), uniform->value()); QString value = variantAsDataString(uniform->type(), uniform->controlType(),
uniform->value());
if (uniform->type() == Uniform::Type::Sampler) if (uniform->type() == Uniform::Type::Sampler)
value = QFileInfo(value).fileName(); value = QFileInfo(value).fileName();
uniformObject.insert("value", value); uniformObject.insert("value", value);
QString defaultValue = variantAsDataString(uniform->type(), uniform->defaultValue()); QString defaultValue = variantAsDataString(uniform->type(), uniform->controlType(),
uniform->defaultValue());
if (uniform->type() == Uniform::Type::Sampler) { if (uniform->type() == Uniform::Type::Sampler) {
defaultValue = QFileInfo(value).fileName(); defaultValue = QFileInfo(value).fileName();
if (uniform->enableMipmap()) if (uniform->enableMipmap())
@@ -517,9 +527,14 @@ QJsonObject nodeToJson(const CompositionNode &node)
|| uniform->type() == Uniform::Type::Int || uniform->type() == Uniform::Type::Int
|| uniform->type() == Uniform::Type::Vec2 || uniform->type() == Uniform::Type::Vec2
|| uniform->type() == Uniform::Type::Vec3 || uniform->type() == Uniform::Type::Vec3
|| uniform->type() == Uniform::Type::Vec4) { || uniform->type() == Uniform::Type::Vec4
uniformObject.insert("minValue", variantAsDataString(uniform->type(), uniform->minValue())); || uniform->controlType() == Uniform::Type::Int) {
uniformObject.insert("maxValue", variantAsDataString(uniform->type(), uniform->maxValue())); uniformObject.insert("minValue", variantAsDataString(uniform->type(),
uniform->controlType(),
uniform->minValue()));
uniformObject.insert("maxValue", variantAsDataString(uniform->type(),
uniform->controlType(),
uniform->maxValue()));
} }
if (!uniform->customValue().isEmpty()) if (!uniform->customValue().isEmpty())
uniformObject.insert("customValue", uniform->customValue()); uniformObject.insert("customValue", uniform->customValue());
@@ -754,10 +769,7 @@ void EffectComposerModel::openComposition(const QString &path)
for (const auto &nodeElement : nodesArray) { for (const auto &nodeElement : nodesArray) {
auto *node = new CompositionNode(effectName, {}, nodeElement.toObject()); auto *node = new CompositionNode(effectName, {}, nodeElement.toObject());
connect(qobject_cast<EffectComposerUniformsModel *>(node->uniformsModel()), connectCompositionNode(node);
&EffectComposerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
m_nodes.append(node); m_nodes.append(node);
const QStringList reqIds = node->requiredNodes(); const QStringList reqIds = node->requiredNodes();
for (const QString &reqId : reqIds) for (const QString &reqId : reqIds)
@@ -922,6 +934,10 @@ QString EffectComposerModel::valueAsString(const Uniform &uniform)
} else if (uniform.type() == Uniform::Type::Color) { } else if (uniform.type() == Uniform::Type::Color) {
return QString("\"%1\"").arg(uniform.value().toString()); return QString("\"%1\"").arg(uniform.value().toString());
} else if (uniform.type() == Uniform::Type::Define) { } else if (uniform.type() == Uniform::Type::Define) {
if (uniform.controlType() == Uniform::Type::Int)
return QString::number(uniform.value().toInt());
else if (uniform.controlType() == Uniform::Type::Bool)
return uniform.value().toBool() ? QString("1") : QString("0");
return uniform.value().toString(); return uniform.value().toString();
} else { } else {
qWarning() << QString("Unhandled const variable type: %1").arg(int(uniform.type())).toLatin1(); qWarning() << QString("Unhandled const variable type: %1").arg(int(uniform.type())).toLatin1();
@@ -1020,10 +1036,8 @@ const QString EffectComposerModel::getDefineProperties()
QString s; QString s;
for (Uniform *uniform : uniforms) { for (Uniform *uniform : uniforms) {
// TODO: Check if uniform is already added. // TODO: Check if uniform is already added.
if (uniform->type() == Uniform::Type::Define) { if (uniform->type() == Uniform::Type::Define)
QString defineValue = uniform->value().toString(); s += QString("#define %1 %2\n").arg(uniform->name(), valueAsString(*uniform));
s += QString("#define %1 %2\n").arg(uniform->name(), defineValue);
}
} }
if (!s.isEmpty()) if (!s.isEmpty())
s += '\n'; s += '\n';
@@ -1620,6 +1634,18 @@ QString EffectComposerModel::getQmlComponentString(bool localFiles)
return s; return s;
} }
void EffectComposerModel::connectCompositionNode(CompositionNode *node)
{
connect(qobject_cast<EffectComposerUniformsModel *>(node->uniformsModel()),
&EffectComposerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
connect(node, &CompositionNode::rebakeRequested, this, [this] {
// This can come multiple times in a row in response to property changes, so let's buffer it
m_rebakeTimer.start(200);
});
}
QString EffectComposerModel::currentComposition() const QString EffectComposerModel::currentComposition() const
{ {
return m_currentComposition; return m_currentComposition;

View File

@@ -12,6 +12,7 @@
#include <QMap> #include <QMap>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QTimer>
namespace ProjectExplorer { namespace ProjectExplorer {
class Target; class Target;
@@ -172,6 +173,8 @@ private:
QString getQmlImagesString(bool localFiles); QString getQmlImagesString(bool localFiles);
QString getQmlComponentString(bool localFiles); QString getQmlComponentString(bool localFiles);
void connectCompositionNode(CompositionNode *node);
QList<CompositionNode *> m_nodes; QList<CompositionNode *> m_nodes;
int m_selectedIndex = -1; int m_selectedIndex = -1;
@@ -206,6 +209,7 @@ private:
bool m_loadComponentImages = true; bool m_loadComponentImages = true;
bool m_isEnabled = true; bool m_isEnabled = true;
QString m_currentComposition; QString m_currentComposition;
QTimer m_rebakeTimer;
const QRegularExpression m_spaceReg = QRegularExpression("\\s+"); const QRegularExpression m_spaceReg = QRegularExpression("\\s+");
}; };

View File

@@ -26,6 +26,7 @@ QHash<int, QByteArray> EffectComposerUniformsModel::roleNames() const
roles[MinValueRole] = "uniformMinValue"; roles[MinValueRole] = "uniformMinValue";
roles[MaxValueRole] = "uniformMaxValue"; roles[MaxValueRole] = "uniformMaxValue";
roles[TypeRole] = "uniformType"; roles[TypeRole] = "uniformType";
roles[ControlTypeRole] = "uniformControlType";
roles[UseCustomValueRole] = "uniformUseCustomValue"; roles[UseCustomValueRole] = "uniformUseCustomValue";
return roles; return roles;
} }

View File

@@ -37,6 +37,7 @@ private:
MaxValueRole, MaxValueRole,
MinValueRole, MinValueRole,
TypeRole, TypeRole,
ControlTypeRole,
UseCustomValueRole UseCustomValueRole
}; };

View File

@@ -22,6 +22,7 @@ Uniform::Uniform(const QString &effectName, const QJsonObject &propObj, const QS
m_name = propObj.value("name").toString(); m_name = propObj.value("name").toString();
m_description = propObj.value("description").toString(); m_description = propObj.value("description").toString();
m_type = Uniform::typeFromString(propObj.value("type").toString()); m_type = Uniform::typeFromString(propObj.value("type").toString());
m_controlType = m_type;
defaultValue = propObj.value("defaultValue").toString(); defaultValue = propObj.value("defaultValue").toString();
m_displayName = propObj.value("displayName").toString(); m_displayName = propObj.value("displayName").toString();
@@ -44,6 +45,12 @@ Uniform::Uniform(const QString &effectName, const QJsonObject &propObj, const QS
g_propertyData[mipmapProperty] = m_enableMipmap; g_propertyData[mipmapProperty] = m_enableMipmap;
} }
if (m_type == Type::Define) {
QString controlType = propObj.value("controlType").toString();
if (!controlType.isEmpty())
m_controlType = Uniform::typeFromString(controlType);
}
m_customValue = propObj.value("customValue").toString(); m_customValue = propObj.value("customValue").toString();
m_useCustomValue = getBoolValue(propObj.value("useCustomValue"), false); m_useCustomValue = getBoolValue(propObj.value("useCustomValue"), false);
@@ -61,12 +68,22 @@ Uniform::Type Uniform::type() const
return m_type; return m_type;
} }
Uniform::Type Uniform::controlType() const
{
return m_controlType;
}
// String representation of the type for qml // String representation of the type for qml
QString Uniform::typeName() const QString Uniform::typeName() const
{ {
return Uniform::stringFromType(m_type); return Uniform::stringFromType(m_type);
} }
QString Uniform::controlTypeName() const
{
return Uniform::stringFromType(m_controlType);
}
QVariant Uniform::value() const QVariant Uniform::value() const
{ {
return m_value; return m_value;
@@ -216,6 +233,13 @@ QVariant Uniform::getInitializedVariant(bool maxValue)
return maxValue ? QVector4D(1.0, 1.0, 1.0, 1.0) : QVector4D(0.0, 0.0, 0.0, 0.0); return maxValue ? QVector4D(1.0, 1.0, 1.0, 1.0) : QVector4D(0.0, 0.0, 0.0, 0.0);
case Uniform::Type::Color: case Uniform::Type::Color:
return maxValue ? QColor::fromRgbF(1.0f, 1.0f, 1.0f, 1.0f) : QColor::fromRgbF(0.0f, 0.0f, 0.0f, 0.0f); return maxValue ? QColor::fromRgbF(1.0f, 1.0f, 1.0f, 1.0f) : QColor::fromRgbF(0.0f, 0.0f, 0.0f, 0.0f);
case Uniform::Type::Define:
if (m_controlType == Uniform::Type::Bool)
return maxValue ? true : false;
else if (m_controlType == Uniform::Type::Int)
return maxValue ? 100 : 0;
else
return QVariant();
default: default:
return QVariant(); return QVariant();
} }
@@ -262,7 +286,10 @@ QVariant Uniform::valueStringToVariant(const QString &value)
variant = value; variant = value;
break; break;
case Uniform::Type::Define: case Uniform::Type::Define:
variant = value; if (m_controlType == Uniform::Type::Bool)
variant = (value == "true");
else
variant = value;
break; break;
} }

View File

@@ -20,6 +20,7 @@ class Uniform : public QObject
Q_PROPERTY(QString uniformName MEMBER m_displayName CONSTANT) Q_PROPERTY(QString uniformName MEMBER m_displayName CONSTANT)
Q_PROPERTY(QString uniformType READ typeName CONSTANT) Q_PROPERTY(QString uniformType READ typeName CONSTANT)
Q_PROPERTY(QString uniformControlType READ controlTypeName CONSTANT)
Q_PROPERTY(QString uniformDescription READ description CONSTANT) Q_PROPERTY(QString uniformDescription READ description CONSTANT)
Q_PROPERTY(QVariant uniformValue READ value WRITE setValue NOTIFY uniformValueChanged) Q_PROPERTY(QVariant uniformValue READ value WRITE setValue NOTIFY uniformValueChanged)
Q_PROPERTY(QVariant uniformBackendValue READ backendValue NOTIFY uniformBackendValueChanged) Q_PROPERTY(QVariant uniformBackendValue READ backendValue NOTIFY uniformBackendValueChanged)
@@ -45,7 +46,9 @@ public:
Uniform(const QString &effectName, const QJsonObject &props, const QString &qenPath); Uniform(const QString &effectName, const QJsonObject &props, const QString &qenPath);
Type type() const; Type type() const;
Type controlType() const;
QString typeName() const; QString typeName() const;
QString controlTypeName() const;
QVariant value() const; QVariant value() const;
void setValue(const QVariant &newValue); void setValue(const QVariant &newValue);
@@ -90,6 +93,7 @@ private:
QString m_qenPath; QString m_qenPath;
Type m_type; Type m_type;
Type m_controlType;
QVariant m_value; QVariant m_value;
QVariant m_defaultValue; QVariant m_defaultValue;
QVariant m_minValue; QVariant m_minValue;