diff --git a/src/scripting/documentation-effect-global.xml b/src/scripting/documentation-effect-global.xml
index 07b774e275..b254ac6e79 100644
--- a/src/scripting/documentation-effect-global.xml
+++ b/src/scripting/documentation-effect-global.xml
@@ -68,13 +68,15 @@ supports the following attributes:
from: FPx2, /* for first animation, optional */
to: FPx2, /* for first animation, optional */
delay: int, /* for first animation, optional */
+ shader: int, /* for first animation, optional */
animations: [ /* additional animations, optional */
{
curve: QEasingCurve.Type, /* overrides global */
type: Effect.Attribute,
from: FPx2,
to: FPx2,
- delay: int
+ delay: int,
+ shader: int
}
]
}
@@ -152,6 +154,22 @@ For convenience you can pass a single quint64 as well.
Registers keySequence as a global shortcut. When the shortcut is invoked the callback will be called. Title and text are used to name the shortcut and make it available to the global shortcut configuration module.
+
+ Q_SCRIPTABLE uint
+ uint KWin::ScriptedEffect::addFragmentShader
+ (ShaderTrait traits, QString fragmentShaderFile)
+ addFragmentShader
+
+ Creates a shader and returns an identifier which can be used in animate or set. The shader sources must be provided in the shaders sub-directory of the contents package directory. The fragment shader needs to have the file extension frag. Each shader should be provided in a GLSL 1.10 and GLSL 1.40 variant. The 1.40 variant needs to have a suffix _core. E.g. there should be a shader myCustomShader.frag and myCustomShader_core.frag. The vertex shader is generated from the ShaderTrait. The ShaderTrait enum can be used as flags in this method.
+
+
+ Q_SCRIPTABLE uint
+ void KWin::ScriptedEffect::setUniform
+ (uint shaderId, QString name, QJSValue value)
+ setUniform
+
+ Updates the uniform value of the uniform identified by @p name for the shader identified by @p shaderId. The @p value can be a floating point numeric value (integer uniform values are not supported), an array with either 2, 3 or 4 numeric values, a string to identify a color or a variant value to identify a color as returned by readConfig. This method can be used to update the state of the shader when the configuration of the effect changed.
+
diff --git a/src/scripting/scriptedeffect.cpp b/src/scripting/scriptedeffect.cpp
index 2291f683bc..a3de410fa0 100644
--- a/src/scripting/scriptedeffect.cpp
+++ b/src/scripting/scriptedeffect.cpp
@@ -21,12 +21,15 @@
#include
#include
#include
+#include
// Qt
#include
#include
#include
#include
+#include
+
Q_DECLARE_METATYPE(KSharedConfigPtr)
namespace KWin
@@ -54,6 +57,7 @@ struct AnimationSettings
uint metaData;
bool fullScreenEffect;
bool keepAlive;
+ std::optional shader;
};
AnimationSettings animationSettingsFromObject(const QJSValue &object)
@@ -121,6 +125,10 @@ AnimationSettings animationSettingsFromObject(const QJSValue &object)
settings.frozenTime = -1;
}
+ if (const auto shader = object.property(QStringLiteral("fragmentShader")); shader.isNumber()) {
+ settings.shader = shader.toUInt();
+ }
+
return settings;
}
@@ -202,6 +210,7 @@ ScriptedEffect::ScriptedEffect()
ScriptedEffect::~ScriptedEffect()
{
+ qDeleteAll(m_shaders);
}
bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript)
@@ -273,6 +282,8 @@ bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript
QStringLiteral("redirect"),
QStringLiteral("complete"),
QStringLiteral("cancel"),
+ QStringLiteral("addShader"),
+ QStringLiteral("setUniform"),
};
for (const QString &propertyName : globalProperties) {
@@ -381,6 +392,9 @@ QJSValue ScriptedEffect::animate_helper(const QJSValue &object, AnimationType an
if (!(s.set & AnimationSettings::KeepAlive)) {
s.keepAlive = settings.at(0).keepAlive;
}
+ if (!s.shader.has_value()) {
+ s.shader = settings.at(0).shader;
+ }
s.metaData = 0;
typedef QMap MetaTypeMap;
@@ -400,6 +414,20 @@ QJSValue ScriptedEffect::animate_helper(const QJSValue &object, AnimationType an
AnimationEffect::setMetaData(it.key(), metaVal.toInt(), s.metaData);
}
}
+ if (s.type == ShaderUniform && s.shader) {
+ auto uniformProperty = value.property(QStringLiteral("uniform")).toString();
+ auto shader = findShader(s.shader.value());
+ if (!shader) {
+ m_engine->throwError(QStringLiteral("Shader for given shaderId not found"));
+ return {};
+ }
+ if (!effects->makeOpenGLContextCurrent()) {
+ m_engine->throwError(QStringLiteral("Failed to make OpenGL context current"));
+ return {};
+ }
+ ShaderBinder binder{shader};
+ s.metaData = shader->uniformLocation(uniformProperty.toUtf8().constData());
+ }
settings << s;
}
@@ -439,7 +467,8 @@ QJSValue ScriptedEffect::animate_helper(const QJSValue &object, AnimationType an
setting.curve,
setting.delay,
setting.fullScreenEffect,
- setting.keepAlive);
+ setting.keepAlive,
+ setting.shader ? setting.shader.value() : 0u);
if (setting.frozenTime >= 0) {
freezeInTime(animationId, setting.frozenTime);
}
@@ -453,7 +482,8 @@ QJSValue ScriptedEffect::animate_helper(const QJSValue &object, AnimationType an
setting.curve,
setting.delay,
setting.fullScreenEffect,
- setting.keepAlive);
+ setting.keepAlive,
+ setting.shader ? setting.shader.value() : 0u);
if (setting.frozenTime >= 0) {
freezeInTime(animationId, setting.frozenTime);
}
@@ -466,7 +496,7 @@ QJSValue ScriptedEffect::animate_helper(const QJSValue &object, AnimationType an
quint64 ScriptedEffect::animate(KWin::EffectWindow *window, KWin::AnimationEffect::Attribute attribute,
int ms, const QJSValue &to, const QJSValue &from, uint metaData, int curve,
- int delay, bool fullScreen, bool keepAlive)
+ int delay, bool fullScreen, bool keepAlive, uint shaderId)
{
QEasingCurve qec;
if (curve < QEasingCurve::Custom) {
@@ -475,7 +505,7 @@ quint64 ScriptedEffect::animate(KWin::EffectWindow *window, KWin::AnimationEffec
qec.setCustomType(qecGaussian);
}
return AnimationEffect::animate(window, attribute, metaData, ms, fpx2FromScriptValue(to), qec,
- delay, fpx2FromScriptValue(from), fullScreen, keepAlive);
+ delay, fpx2FromScriptValue(from), fullScreen, keepAlive, findShader(shaderId));
}
QJSValue ScriptedEffect::animate(const QJSValue &object)
@@ -485,7 +515,7 @@ QJSValue ScriptedEffect::animate(const QJSValue &object)
quint64 ScriptedEffect::set(KWin::EffectWindow *window, KWin::AnimationEffect::Attribute attribute,
int ms, const QJSValue &to, const QJSValue &from, uint metaData, int curve,
- int delay, bool fullScreen, bool keepAlive)
+ int delay, bool fullScreen, bool keepAlive, uint shaderId)
{
QEasingCurve qec;
if (curve < QEasingCurve::Custom) {
@@ -494,7 +524,7 @@ quint64 ScriptedEffect::set(KWin::EffectWindow *window, KWin::AnimationEffect::A
qec.setCustomType(qecGaussian);
}
return AnimationEffect::set(window, attribute, metaData, ms, fpx2FromScriptValue(to), qec,
- delay, fpx2FromScriptValue(from), fullScreen, keepAlive);
+ delay, fpx2FromScriptValue(from), fullScreen, keepAlive, findShader(shaderId));
}
QJSValue ScriptedEffect::set(const QJSValue &object)
@@ -771,4 +801,87 @@ QJSEngine *ScriptedEffect::engine() const
return m_engine;
}
+uint ScriptedEffect::addFragmentShader(ShaderTrait traits, const QString &fragmentShaderFile)
+{
+ if (!effects->makeOpenGLContextCurrent()) {
+ m_engine->throwError(QStringLiteral("Failed to make OpenGL context current"));
+ return 0;
+ }
+ const QString shaderDir{QLatin1String(KWIN_NAME "/effects/") + m_effectName + QLatin1String("/contents/shaders/")};
+ const QString fragment = fragmentShaderFile.isEmpty() ? QString{} : QStandardPaths::locate(QStandardPaths::GenericDataLocation, shaderDir + fragmentShaderFile);
+
+ auto shader = ShaderManager::instance()->generateShaderFromFile(static_cast(int(traits)), {}, fragment);
+ if (!shader->isValid()) {
+ m_engine->throwError(QStringLiteral("Shader failed to load"));
+ delete shader;
+ // 0 is never a valid shader identifier, it's ensured the first shader gets id 1
+ return 0;
+ }
+
+ const uint shaderId{m_nextShaderId};
+ m_nextShaderId++;
+ m_shaders.insert(shaderId, shader);
+ return shaderId;
+}
+
+GLShader *ScriptedEffect::findShader(uint shaderId) const
+{
+ if (auto it = m_shaders.find(shaderId); it != m_shaders.end()) {
+ return it.value();
+ }
+ return nullptr;
+}
+
+void ScriptedEffect::setUniform(uint shaderId, const QString &name, const QJSValue &value)
+{
+ auto shader = findShader(shaderId);
+ if (!shader) {
+ m_engine->throwError(QStringLiteral("Shader for given shaderId not found"));
+ return;
+ }
+ if (!effects->makeOpenGLContextCurrent()) {
+ m_engine->throwError(QStringLiteral("Failed to make OpenGL context current"));
+ return;
+ }
+ auto setColorUniform = [this, shader, name] (const QColor &color)
+ {
+ if (!color.isValid()) {
+ return;
+ }
+ if (!shader->setUniform(name.toUtf8().constData(), color)) {
+ m_engine->throwError(QStringLiteral("Failed to set uniform ") + name);
+ }
+ };
+ ShaderBinder binder{shader};
+ if (value.isString()) {
+ setColorUniform(value.toString());
+ } else if (value.isNumber()) {
+ if (!shader->setUniform(name.toUtf8().constData(), float(value.toNumber()))) {
+ m_engine->throwError(QStringLiteral("Failed to set uniform ") + name);
+ }
+ } else if (value.isArray()) {
+ const auto length = value.property(QStringLiteral("length")).toInt();
+ if (length == 2) {
+ if (!shader->setUniform(name.toUtf8().constData(), QVector2D{float(value.property(0).toNumber()), float(value.property(1).toNumber())})) {
+ m_engine->throwError(QStringLiteral("Failed to set uniform ") + name);
+ }
+ } else if (length == 3) {
+ if (!shader->setUniform(name.toUtf8().constData(), QVector3D{float(value.property(0).toNumber()), float(value.property(1).toNumber()), float(value.property(2).toNumber())})) {
+ m_engine->throwError(QStringLiteral("Failed to set uniform ") + name);
+ }
+ } else if (length == 4) {
+ if (!shader->setUniform(name.toUtf8().constData(), QVector4D{float(value.property(0).toNumber()), float(value.property(1).toNumber()), float(value.property(2).toNumber()), float(value.property(3).toNumber())})) {
+ m_engine->throwError(QStringLiteral("Failed to set uniform ") + name);
+ }
+ } else {
+ m_engine->throwError(QStringLiteral("Invalid number of elements in array"));
+ }
+ } else if (value.isVariant()) {
+ const auto variant = value.toVariant();
+ setColorUniform(variant.value());
+ } else {
+ m_engine->throwError(QStringLiteral("Invalid value provided for uniform"));
+ }
+}
+
} // namespace
diff --git a/src/scripting/scriptedeffect.h b/src/scripting/scriptedeffect.h
index 4bdd05aba0..a73815e393 100644
--- a/src/scripting/scriptedeffect.h
+++ b/src/scripting/scriptedeffect.h
@@ -32,6 +32,7 @@ class KWIN_EXPORT ScriptedEffect : public KWin::AnimationEffect
Q_ENUMS(EasingCurve)
Q_ENUMS(SessionState)
Q_ENUMS(ElectricBorder)
+ Q_ENUMS(ShaderTrait)
/**
* The plugin ID of the effect
*/
@@ -59,6 +60,14 @@ public:
enum EasingCurve {
GaussianCurve = 128
};
+ // copied from kwinglutils.h
+ enum class ShaderTrait {
+ MapTexture = (1 << 0),
+ UniformColor = (1 << 1),
+ Modulate = (1 << 2),
+ AdjustSaturation = (1 << 3),
+ };
+
const QString &scriptFile() const
{
return m_scriptFile;
@@ -127,13 +136,13 @@ public:
Q_SCRIPTABLE quint64 animate(KWin::EffectWindow *window, Attribute attribute, int ms,
const QJSValue &to, const QJSValue &from = QJSValue(),
uint metaData = 0, int curve = QEasingCurve::Linear, int delay = 0,
- bool fullScreen = false, bool keepAlive = true);
+ bool fullScreen = false, bool keepAlive = true, uint shaderId = 0);
Q_SCRIPTABLE QJSValue animate(const QJSValue &object);
Q_SCRIPTABLE quint64 set(KWin::EffectWindow *window, Attribute attribute, int ms,
const QJSValue &to, const QJSValue &from = QJSValue(),
uint metaData = 0, int curve = QEasingCurve::Linear, int delay = 0,
- bool fullScreen = false, bool keepAlive = true);
+ bool fullScreen = false, bool keepAlive = true, uint shaderId = 0);
Q_SCRIPTABLE QJSValue set(const QJSValue &object);
Q_SCRIPTABLE bool retarget(quint64 animationId, const QJSValue &newTarget,
@@ -156,6 +165,10 @@ public:
Q_SCRIPTABLE QList touchEdgesForAction(const QString &action) const;
+ Q_SCRIPTABLE uint addFragmentShader(ShaderTrait traits, const QString &fragmentShaderFile = {});
+
+ Q_SCRIPTABLE void setUniform(uint shaderId, const QString &name, const QJSValue &value);
+
QHash &screenEdgeCallbacks()
{
return m_screenEdgeCallbacks;
@@ -194,6 +207,8 @@ private:
QJSValue animate_helper(const QJSValue &object, AnimationType animationType);
+ GLShader *findShader(uint shaderId) const;
+
QJSEngine *m_engine;
QString m_effectName;
QString m_scriptFile;
@@ -204,6 +219,8 @@ private:
int m_chainPosition;
QHash m_touchScreenEdgeCallbacks;
Effect *m_activeFullScreenEffect = nullptr;
+ QHash m_shaders;
+ uint m_nextShaderId{1u};
};
}