KWin
Loading...
Searching...
No Matches
glshadermanager.h
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2006-2007 Rivo Laks <rivolaks@hot.ee>
6 SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
7 SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@kde.org>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11#pragma once
12#include "kwin_export.h"
13
14#include <QByteArray>
15#include <QFlags>
16#include <QStack>
17#include <map>
18#include <memory>
19
20namespace KWin
21{
22
23class GLShader;
24
25enum class ShaderTrait {
26 MapTexture = (1 << 0),
27 UniformColor = (1 << 1),
28 Modulate = (1 << 2),
29 AdjustSaturation = (1 << 3),
30 TransformColorspace = (1 << 4),
31 MapExternalTexture = (1 << 5),
32};
33
34Q_DECLARE_FLAGS(ShaderTraits, ShaderTrait)
35
36
47class KWIN_EXPORT ShaderManager
48{
49public:
50 explicit ShaderManager();
52
56 GLShader *shader(ShaderTraits traits);
57
61 GLShader *getBoundShader() const;
62
66 bool isShaderBound() const;
67
72 GLShader *pushShader(ShaderTraits traits);
73
81 void pushShader(GLShader *shader);
82
90 void popShader();
91
99 std::unique_ptr<GLShader> loadShaderFromCode(const QByteArray &vertexSource, const QByteArray &fragmentSource);
100
117 std::unique_ptr<GLShader> generateCustomShader(ShaderTraits traits, const QByteArray &vertexSource = QByteArray(), const QByteArray &fragmentSource = QByteArray());
118
139 std::unique_ptr<GLShader> generateShaderFromFile(ShaderTraits traits, const QString &vertexFile = QString(), const QString &fragmentFile = QString());
140
144 static ShaderManager *instance();
145
149 static void cleanup();
150
151private:
152 void bindFragDataLocations(GLShader *shader);
153 void bindAttributeLocations(GLShader *shader) const;
154
155 std::optional<QByteArray> preprocess(const QByteArray &src, int recursionDepth = 0) const;
156 QByteArray generateVertexSource(ShaderTraits traits) const;
157 QByteArray generateFragmentSource(ShaderTraits traits) const;
158 std::unique_ptr<GLShader> generateShader(ShaderTraits traits);
159
160 QStack<GLShader *> m_boundShaders;
161 std::map<ShaderTraits, std::unique_ptr<GLShader>> m_shaderHash;
162 static std::unique_ptr<ShaderManager> s_shaderManager;
163};
164
181class KWIN_EXPORT ShaderBinder
182{
183public:
190 explicit ShaderBinder(GLShader *shader);
198 explicit ShaderBinder(ShaderTraits traits);
200
204 GLShader *shader();
205
206private:
207 GLShader *m_shader;
208};
209
211 : m_shader(shader)
212{
214}
215
216inline ShaderBinder::ShaderBinder(ShaderTraits traits)
217 : m_shader(nullptr)
218{
219 m_shader = ShaderManager::instance()->pushShader(traits);
220}
221
226
228{
229 return m_shader;
230}
231
232}
233
234Q_DECLARE_OPERATORS_FOR_FLAGS(KWin::ShaderTraits)
ShaderBinder(GLShader *shader)
Pushes the given shader to the ShaderManager's stack.
Manager for Shaders.
static ShaderManager * instance()
GLShader * pushShader(ShaderTraits traits)