KWin
Loading...
Searching...
No Matches
glshader.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 "core/colorspace.h"
13
14#include <QColor>
15#include <QMatrix3x3>
16#include <QMatrix4x4>
17#include <QString>
18#include <QVector2D>
19#include <QVector3D>
20#include <epoxy/gl.h>
21
22namespace KWin
23{
24
25class KWIN_EXPORT GLShader
26{
27public:
28 enum Flags {
29 NoFlags = 0,
30 ExplicitLinking = (1 << 0)
31 };
32
33 GLShader(const QString &vertexfile, const QString &fragmentfile, unsigned int flags = NoFlags);
34 ~GLShader();
35
36 bool isValid() const
37 {
38 return m_valid;
39 }
40
41 void bindAttributeLocation(const char *name, int index);
42 void bindFragDataLocation(const char *name, int index);
43
44 bool link();
45
46 int uniformLocation(const char *name);
47
48 bool setUniform(const char *name, float value);
49 bool setUniform(const char *name, int value);
50 bool setUniform(const char *name, const QVector2D &value);
51 bool setUniform(const char *name, const QVector3D &value);
52 bool setUniform(const char *name, const QVector4D &value);
53 bool setUniform(const char *name, const QMatrix4x4 &value);
54 bool setUniform(const char *name, const QColor &color);
55
56 bool setUniform(int location, float value);
57 bool setUniform(int location, int value);
58 bool setUniform(int location, int xValue, int yValue, int zValue);
59 bool setUniform(int location, const QVector2D &value);
60 bool setUniform(int location, const QVector3D &value);
61 bool setUniform(int location, const QVector4D &value);
62 bool setUniform(int location, const QMatrix3x3 &value);
63 bool setUniform(int location, const QMatrix4x4 &value);
64 bool setUniform(int location, const QColor &value);
65
66 int attributeLocation(const char *name);
67 bool setAttribute(const char *name, float value);
72 QMatrix4x4 getUniformMatrix4x4(const char *name);
73
74 enum class Mat3Uniform {
75 };
76
77 enum class Mat4Uniform {
78 TextureMatrix = 0,
79 ProjectionMatrix,
80 ModelViewMatrix,
81 ModelViewProjectionMatrix,
82 WindowTransformation,
83 ScreenTransformation,
84 ColorimetryTransformation,
85 MatrixCount
86 };
87
88 enum class Vec2Uniform {
89 Offset,
90 Vec2UniformCount
91 };
92
93 enum class Vec3Uniform {
94 PrimaryBrightness = 0
95 };
96
97 enum class Vec4Uniform {
98 ModulationConstant,
99 Vec4UniformCount
100 };
101
102 enum class FloatUniform {
103 Saturation,
104 MaxHdrBrightness,
105 SdrBrightness,
106 FloatUniformCount
107 };
108
109 enum class IntUniform {
110 AlphaToOne,
111 TextureWidth,
112 TextureHeight,
113 SourceNamedTransferFunction,
114 DestinationNamedTransferFunction,
115 Sampler,
116 Sampler1,
117 IntUniformCount
118 };
119
120 enum class ColorUniform {
121 Color,
122 ColorUniformCount
123 };
124
125 bool setUniform(Mat3Uniform uniform, const QMatrix3x3 &value);
126 bool setUniform(Mat4Uniform uniform, const QMatrix4x4 &matrix);
127 bool setUniform(Vec2Uniform uniform, const QVector2D &value);
128 bool setUniform(Vec3Uniform uniform, const QVector3D &value);
129 bool setUniform(Vec4Uniform uniform, const QVector4D &value);
130 bool setUniform(FloatUniform uniform, float value);
131 bool setUniform(IntUniform uniform, int value);
132 bool setUniform(ColorUniform uniform, const QVector4D &value);
133 bool setUniform(ColorUniform uniform, const QColor &value);
134
135 bool setColorspaceUniforms(const ColorDescription &src, const ColorDescription &dst);
136 bool setColorspaceUniformsFromSRGB(const ColorDescription &dst);
137 bool setColorspaceUniformsToSRGB(const ColorDescription &src);
138
139protected:
140 GLShader(unsigned int flags = NoFlags);
141 bool loadFromFiles(const QString &vertexfile, const QString &fragmentfile);
142 bool load(const QByteArray &vertexSource, const QByteArray &fragmentSource);
143 const QByteArray prepareSource(GLenum shaderType, const QByteArray &sourceCode) const;
144 bool compile(GLuint program, GLenum shaderType, const QByteArray &sourceCode) const;
145 void bind();
146 void unbind();
147 void resolveLocations();
148
149private:
150 unsigned int m_program;
151 bool m_valid : 1;
152 bool m_locationsResolved : 1;
153 bool m_explicitLinking : 1;
154 QHash<Mat3Uniform, int> m_matrix3Locations;
155 QHash<Mat4Uniform, int> m_matrix4Locations;
156 QHash<Vec2Uniform, int> m_vec2Locations;
157 QHash<Vec3Uniform, int> m_vec3Locations;
158 QHash<Vec4Uniform, int> m_vec4Locations;
159 QHash<FloatUniform, int> m_floatLocations;
160 QHash<IntUniform, int> m_intLocations;
161 QHash<ColorUniform, int> m_colorLocations;
162
163 friend class ShaderManager;
164};
165
166}
bool isValid() const
Definition glshader.h:36
Manager for Shaders.