Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
ShaderRegistry.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorShaders/phosphorshaders_export.h>
7
10
11#include <QHash>
12#include <QImage>
13#include <QList>
14#include <QMap>
15#include <QMutex>
16#include <QRect>
17#include <QSize>
18#include <QString>
19#include <QUrl>
20#include <QVariant>
21#include <array>
22#include <memory>
23
24namespace PhosphorShaders {
25
26class IWallpaperProvider;
27
56{
57 Q_OBJECT
58
59public:
61 {
62 QString id;
63 QString name;
64 QString group;
65 QString type;
66 int slot = -1;
67 QVariant defaultValue;
68 QVariant minValue;
69 QVariant maxValue;
70 bool useZoneColor = false;
71 QString wrap;
72
74 QString uniformName() const;
75 };
76
78 {
79 QString id;
80 QString name;
81 QString description;
82 QString author;
83 QString version;
85 QString sourcePath;
87 QStringList bufferShaderPaths;
88 QString previewPath;
89 QString category;
90 QList<ParameterInfo> parameters;
91 QMap<QString, QVariantMap> presets;
92 bool isUserShader = false;
93 bool isMultipass = false;
94 bool useWallpaper = false;
95 bool bufferFeedback = false;
96 qreal bufferScale = 1.0;
97 QString bufferWrap = QStringLiteral("clamp");
98 QStringList bufferWraps;
99 QString bufferFilter = QStringLiteral("linear");
100 QStringList bufferFilters;
101 bool useDepthBuffer = false;
102
103 bool isValid() const
104 {
105 return !id.isEmpty() && (isNoneShader(id) || shaderUrl.isValid());
106 }
107 };
108
109 explicit ShaderRegistry(QObject* parent = nullptr);
110 ~ShaderRegistry() override;
111
112 // ── Shader discovery ──────────────────────────────────────────────
113
114 static QString noneShaderUuid();
115 static bool isNoneShader(const QString& id);
116
117 QList<ShaderInfo> availableShaders() const;
118 Q_INVOKABLE QVariantList availableShadersVariant() const;
119
120 ShaderInfo shader(const QString& id) const;
121 Q_INVOKABLE QVariantMap shaderInfo(const QString& id) const;
122 Q_INVOKABLE QUrl shaderUrl(const QString& id) const;
123
124 // ── Parameters & presets ──────────────────────────────────────────
125
126 Q_INVOKABLE QVariantMap defaultParams(const QString& id) const;
127 bool validateParams(const QString& id, const QVariantMap& params) const;
128 QVariantMap validateAndCoerceParams(const QString& id, const QVariantMap& params) const;
129 Q_INVOKABLE QVariantMap translateParamsToUniforms(const QString& shaderId, const QVariantMap& storedParams) const;
130 Q_INVOKABLE QVariantMap presetParams(const QString& shaderId, const QString& presetName) const;
131 Q_INVOKABLE QStringList shaderPresetNames(const QString& shaderId) const;
132 Q_INVOKABLE QVariantList shaderPresetsVariant(const QString& shaderId) const;
133
142 Q_INVOKABLE bool shadersEnabled() const
143 {
144 return true;
145 }
146
147 // ── Lifecycle ─────────────────────────────────────────────────────
148
149 void reportShaderBakeStarted(const QString& shaderId);
150 void reportShaderBakeFinished(const QString& shaderId, bool success, const QString& error);
151
152 // ── Wallpaper ─────────────────────────────────────────────────────
153
154 static QString wallpaperPath();
155 static QImage loadWallpaperImage();
156
172 static QImage loadWallpaperImage(const QRect& subGeom, const QRect& physGeom);
173
180 static QRect computeWallpaperCropRect(QSize wpSize, const QRect& physGeom, const QRect& subGeom);
181
183
184Q_SIGNALS:
186 void shaderCompilationStarted(const QString& shaderId);
187 void shaderCompilationFinished(const QString& shaderId, bool success, const QString& error);
188
189protected:
190 void onUserPathChanged(const QString& path) override;
191
192private:
193 bool validateParameterValue(const ParameterInfo& param, const QVariant& value) const;
194 QVariantMap shaderInfoToVariantMap(const ShaderInfo& info) const;
195 QVariantMap parameterInfoToVariantMap(const ParameterInfo& param) const;
196
198
204 static std::unique_ptr<PhosphorFsLoader::IScanStrategy> buildScanStrategy(ShaderRegistry* self);
205
206 // Non-owning typed alias for the strategy the base owns. Populated
207 // in the ctor's member-init list via `static_cast<ScanStrategy*>(strategy())`
208 // — the cast is safe because we passed in the same instance. Named
209 // distinctly from the base's private `m_strategy` so a future reader
210 // can't accidentally read this as the same field.
211 ScanStrategy* m_typedStrategy;
212
213 static std::unique_ptr<IWallpaperProvider> s_wallpaperProvider;
214 static QString s_cachedWallpaperPath;
215 static QImage s_cachedWallpaperImage;
216 static qint64 s_cachedWallpaperMtime;
217 static QMutex s_wallpaperCacheMutex;
218
219 // Per-VS crop cache: keeps the same QImage (and cacheKey) for repeated
220 // loadWallpaperImage(sub, phys) calls so downstream cacheKey()-based
221 // short-circuits in ShaderEffect/ShaderNodeRhi keep working and the GPU
222 // doesn't re-upload the wallpaper on every overlay update.
223 struct WallpaperCropEntry
224 {
225 QRect sub;
226 QRect phys;
227 qint64 mtime = 0;
228 QImage img;
229 };
230 static constexpr int CropCacheCapacity = 8;
231 static std::array<WallpaperCropEntry, CropCacheCapacity> s_cachedWallpaperCrops;
232 static int s_cachedWallpaperCropNextSlot;
233};
234
235} // namespace PhosphorShaders
QObject base for registries hosting a MetadataPackScanStrategy<Payload>.
Definition MetadataPackRegistryBase.h:61
Reusable scan strategy for metadata.json-driven subdirectory pack registries.
Definition MetadataPackScanStrategy.h:187
Registry of available shader effects.
Definition ShaderRegistry.h:56
ShaderRegistry(QObject *parent=nullptr)
QVariantMap shaderInfo(const QString &id) const
void reportShaderBakeFinished(const QString &shaderId, bool success, const QString &error)
void shaderCompilationStarted(const QString &shaderId)
QStringList shaderPresetNames(const QString &shaderId) const
static QImage loadWallpaperImage(const QRect &subGeom, const QRect &physGeom)
Return the wallpaper image cropped to the portion that a sub-region (subGeom) occupies on a physical ...
QVariantMap translateParamsToUniforms(const QString &shaderId, const QVariantMap &storedParams) const
QList< ShaderInfo > availableShaders() const
ShaderInfo shader(const QString &id) const
static QImage loadWallpaperImage()
QVariantList availableShadersVariant() const
void onUserPathChanged(const QString &path) override
Hook invoked from setUserPath when the path actually changes.
static void invalidateWallpaperCache()
void shaderCompilationFinished(const QString &shaderId, bool success, const QString &error)
QVariantList shaderPresetsVariant(const QString &shaderId) const
bool shadersEnabled() const
Always true — once a ShaderRegistry is constructed, shader discovery and metadata are functional (the...
Definition ShaderRegistry.h:142
QVariantMap validateAndCoerceParams(const QString &id, const QVariantMap &params) const
QVariantMap defaultParams(const QString &id) const
bool validateParams(const QString &id, const QVariantMap &params) const
static bool isNoneShader(const QString &id)
void reportShaderBakeStarted(const QString &shaderId)
QUrl shaderUrl(const QString &id) const
static QRect computeWallpaperCropRect(QSize wpSize, const QRect &physGeom, const QRect &subGeom)
Pure geometry helper: compute the pixel rect inside a wallpaper of size wpSize that corresponds to su...
QVariantMap presetParams(const QString &shaderId, const QString &presetName) const
Definition ShaderEffect.h:28
QString group
Definition ShaderRegistry.h:64
QVariant maxValue
Definition ShaderRegistry.h:69
QString uniformName() const
Convert slot to uniform name (e.g., slot 0 → "customParams1_x")
QVariant defaultValue
Definition ShaderRegistry.h:67
QString wrap
Definition ShaderRegistry.h:71
QVariant minValue
Definition ShaderRegistry.h:68
QString id
Definition ShaderRegistry.h:62
QString name
Definition ShaderRegistry.h:63
QString type
"float", "color", "int", "bool", "image"
Definition ShaderRegistry.h:65
Definition ShaderRegistry.h:78
QString vertexShaderPath
Definition ShaderRegistry.h:86
QString id
Definition ShaderRegistry.h:79
QString sourcePath
Definition ShaderRegistry.h:85
bool isValid() const
Definition ShaderRegistry.h:103
QString author
Definition ShaderRegistry.h:82
QMap< QString, QVariantMap > presets
Definition ShaderRegistry.h:91
QString category
Definition ShaderRegistry.h:89
QList< ParameterInfo > parameters
Definition ShaderRegistry.h:90
QUrl shaderUrl
Definition ShaderRegistry.h:84
QString name
Definition ShaderRegistry.h:80
QString previewPath
Definition ShaderRegistry.h:88
QStringList bufferFilters
Definition ShaderRegistry.h:100
QString description
Definition ShaderRegistry.h:81
QString version
Definition ShaderRegistry.h:83
QStringList bufferShaderPaths
Definition ShaderRegistry.h:87
QStringList bufferWraps
Definition ShaderRegistry.h:98