File indexing completed on 2024-05-12 05:31:43

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2006-2007 Rivo Laks <rivolaks@hot.ee>
0006     SPDX-FileCopyrightText: 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
0007     SPDX-FileCopyrightText: 2023 Xaver Hugl <xaver.hugl@kde.org>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 #pragma once
0012 #include "core/colorspace.h"
0013 
0014 #include <QColor>
0015 #include <QMatrix3x3>
0016 #include <QMatrix4x4>
0017 #include <QString>
0018 #include <QVector2D>
0019 #include <QVector3D>
0020 #include <epoxy/gl.h>
0021 
0022 namespace KWin
0023 {
0024 
0025 class KWIN_EXPORT GLShader
0026 {
0027 public:
0028     enum Flags {
0029         NoFlags = 0,
0030         ExplicitLinking = (1 << 0)
0031     };
0032 
0033     GLShader(const QString &vertexfile, const QString &fragmentfile, unsigned int flags = NoFlags);
0034     ~GLShader();
0035 
0036     bool isValid() const
0037     {
0038         return m_valid;
0039     }
0040 
0041     void bindAttributeLocation(const char *name, int index);
0042     void bindFragDataLocation(const char *name, int index);
0043 
0044     bool link();
0045 
0046     int uniformLocation(const char *name);
0047 
0048     bool setUniform(const char *name, float value);
0049     bool setUniform(const char *name, int value);
0050     bool setUniform(const char *name, const QVector2D &value);
0051     bool setUniform(const char *name, const QVector3D &value);
0052     bool setUniform(const char *name, const QVector4D &value);
0053     bool setUniform(const char *name, const QMatrix4x4 &value);
0054     bool setUniform(const char *name, const QColor &color);
0055 
0056     bool setUniform(int location, float value);
0057     bool setUniform(int location, int value);
0058     bool setUniform(int location, int xValue, int yValue, int zValue);
0059     bool setUniform(int location, const QVector2D &value);
0060     bool setUniform(int location, const QVector3D &value);
0061     bool setUniform(int location, const QVector4D &value);
0062     bool setUniform(int location, const QMatrix3x3 &value);
0063     bool setUniform(int location, const QMatrix4x4 &value);
0064     bool setUniform(int location, const QColor &value);
0065 
0066     int attributeLocation(const char *name);
0067     bool setAttribute(const char *name, float value);
0068     /**
0069      * @return The value of the uniform as a matrix
0070      * @since 4.7
0071      */
0072     QMatrix4x4 getUniformMatrix4x4(const char *name);
0073 
0074     enum class Mat3Uniform {
0075     };
0076 
0077     enum class Mat4Uniform {
0078         TextureMatrix = 0,
0079         ProjectionMatrix,
0080         ModelViewMatrix,
0081         ModelViewProjectionMatrix,
0082         WindowTransformation,
0083         ScreenTransformation,
0084         ColorimetryTransformation,
0085         MatrixCount
0086     };
0087 
0088     enum class Vec2Uniform {
0089         Offset,
0090         Vec2UniformCount
0091     };
0092 
0093     enum class Vec3Uniform {
0094         PrimaryBrightness = 0
0095     };
0096 
0097     enum class Vec4Uniform {
0098         ModulationConstant,
0099         Vec4UniformCount
0100     };
0101 
0102     enum class FloatUniform {
0103         Saturation,
0104         MaxHdrBrightness,
0105         SdrBrightness,
0106         FloatUniformCount
0107     };
0108 
0109     enum class IntUniform {
0110         AlphaToOne, ///< @deprecated no longer used
0111         TextureWidth,
0112         TextureHeight,
0113         SourceNamedTransferFunction,
0114         DestinationNamedTransferFunction,
0115         Sampler,
0116         Sampler1,
0117         IntUniformCount
0118     };
0119 
0120     enum class ColorUniform {
0121         Color,
0122         ColorUniformCount
0123     };
0124 
0125     bool setUniform(Mat3Uniform uniform, const QMatrix3x3 &value);
0126     bool setUniform(Mat4Uniform uniform, const QMatrix4x4 &matrix);
0127     bool setUniform(Vec2Uniform uniform, const QVector2D &value);
0128     bool setUniform(Vec3Uniform uniform, const QVector3D &value);
0129     bool setUniform(Vec4Uniform uniform, const QVector4D &value);
0130     bool setUniform(FloatUniform uniform, float value);
0131     bool setUniform(IntUniform uniform, int value);
0132     bool setUniform(ColorUniform uniform, const QVector4D &value);
0133     bool setUniform(ColorUniform uniform, const QColor &value);
0134 
0135     bool setColorspaceUniforms(const ColorDescription &src, const ColorDescription &dst);
0136     bool setColorspaceUniformsFromSRGB(const ColorDescription &dst);
0137     bool setColorspaceUniformsToSRGB(const ColorDescription &src);
0138 
0139 protected:
0140     GLShader(unsigned int flags = NoFlags);
0141     bool loadFromFiles(const QString &vertexfile, const QString &fragmentfile);
0142     bool load(const QByteArray &vertexSource, const QByteArray &fragmentSource);
0143     const QByteArray prepareSource(GLenum shaderType, const QByteArray &sourceCode) const;
0144     bool compile(GLuint program, GLenum shaderType, const QByteArray &sourceCode) const;
0145     void bind();
0146     void unbind();
0147     void resolveLocations();
0148 
0149 private:
0150     unsigned int m_program;
0151     bool m_valid : 1;
0152     bool m_locationsResolved : 1;
0153     bool m_explicitLinking : 1;
0154     QHash<Mat3Uniform, int> m_matrix3Locations;
0155     QHash<Mat4Uniform, int> m_matrix4Locations;
0156     QHash<Vec2Uniform, int> m_vec2Locations;
0157     QHash<Vec3Uniform, int> m_vec3Locations;
0158     QHash<Vec4Uniform, int> m_vec4Locations;
0159     QHash<FloatUniform, int> m_floatLocations;
0160     QHash<IntUniform, int> m_intLocations;
0161     QHash<ColorUniform, int> m_colorLocations;
0162 
0163     friend class ShaderManager;
0164 };
0165 
0166 }