2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-04-29 17:35:43 +00:00
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
|
2011-03-16 18:39:10 +00:00
|
|
|
Copyright (C) 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#ifndef KWIN_GLUTILS_H
|
|
|
|
#define KWIN_GLUTILS_H
|
|
|
|
|
2013-02-26 07:02:27 +00:00
|
|
|
// kwin
|
2013-12-03 09:43:57 +00:00
|
|
|
#include <kwinglutils_export.h>
|
2013-02-26 07:02:27 +00:00
|
|
|
#include "kwinglutils_funcs.h"
|
|
|
|
#include "kwingltexture.h"
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-02-26 07:02:27 +00:00
|
|
|
// Qt
|
2013-02-26 08:00:51 +00:00
|
|
|
#include <QSize>
|
|
|
|
#include <QStack>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2008-01-16 18:13:24 +00:00
|
|
|
/** @addtogroup kwineffects */
|
|
|
|
/** @{ */
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-11-14 19:49:00 +00:00
|
|
|
class QVector2D;
|
|
|
|
class QVector3D;
|
|
|
|
class QVector4D;
|
2010-12-04 11:25:49 +00:00
|
|
|
class QMatrix4x4;
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
template< class K, class V > class QHash;
|
|
|
|
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2010-07-19 20:53:32 +00:00
|
|
|
class GLVertexBuffer;
|
|
|
|
class GLVertexBufferPrivate;
|
2007-07-04 20:33:35 +00:00
|
|
|
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
// Initializes GLX function pointers
|
2013-12-03 09:43:57 +00:00
|
|
|
void KWINGLUTILS_EXPORT initGLX();
|
2007-04-29 17:35:43 +00:00
|
|
|
// Initializes OpenGL stuff. This includes resolving function pointers as
|
|
|
|
// well as checking for GL version and extensions
|
|
|
|
// Note that GL context has to be created by the time this function is called
|
2013-12-03 09:43:57 +00:00
|
|
|
void KWINGLUTILS_EXPORT initGL(OpenGLPlatformInterface platformInterface);
|
2010-12-05 10:55:19 +00:00
|
|
|
// Initializes EGL function pointers
|
2013-12-03 09:43:57 +00:00
|
|
|
void KWINGLUTILS_EXPORT initEGL();
|
2011-04-27 12:51:36 +00:00
|
|
|
// Cleans up all resources hold by the GL Context
|
2013-12-03 09:43:57 +00:00
|
|
|
void KWINGLUTILS_EXPORT cleanupGL();
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
// Number of supported texture units
|
2013-12-03 09:43:57 +00:00
|
|
|
extern KWINGLUTILS_EXPORT int glTextureUnitsCount;
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
|
2013-12-03 09:43:57 +00:00
|
|
|
bool KWINGLUTILS_EXPORT hasGLVersion(int major, int minor, int release = 0);
|
|
|
|
bool KWINGLUTILS_EXPORT hasGLXVersion(int major, int minor, int release = 0);
|
|
|
|
bool KWINGLUTILS_EXPORT hasEGLVersion(int major, int minor, int release = 0);
|
2007-04-29 17:35:43 +00:00
|
|
|
// use for both OpenGL and GLX extensions
|
2013-12-03 09:43:57 +00:00
|
|
|
bool KWINGLUTILS_EXPORT hasGLExtension(const QString& extension);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
// detect OpenGL error (add to various places in code to pinpoint the place)
|
2013-12-03 09:43:57 +00:00
|
|
|
bool KWINGLUTILS_EXPORT checkGLError(const char* txt);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-12-03 09:43:57 +00:00
|
|
|
inline bool KWINGLUTILS_EXPORT isPowerOfTwo(int x)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
return ((x & (x - 1)) == 0);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
/**
|
|
|
|
* @return power of two integer _greater or equal to_ x.
|
|
|
|
* E.g. nearestPowerOfTwo(513) = nearestPowerOfTwo(800) = 1024
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
int KWINGLUTILS_EXPORT nearestPowerOfTwo(int x);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-01 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* Push a new matrix on the GL matrix stack.
|
|
|
|
* In GLES this method is a noop. This method should be preferred over glPushMatrix
|
|
|
|
* as it also handles GLES.
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
KWINGLUTILS_EXPORT void pushMatrix();
|
2011-01-01 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* Multiplies current matrix on GL stack with @p matrix and pushes the result on the matrix stack.
|
|
|
|
* This method is the same as pushMatrix followed by multiplyMatrix.
|
|
|
|
* In GLES this method is a noop. This method should be preferred over glPushMatrix
|
|
|
|
* as it also handles GLES.
|
|
|
|
* @param matrix The matrix the current matrix on the stack should be multiplied with.
|
|
|
|
* @see pushMatrix
|
|
|
|
* @see multiplyMatrix
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
KWINGLUTILS_EXPORT void pushMatrix(const QMatrix4x4 &matrix);
|
2011-01-01 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* Multiplies the current matrix on GL stack with @p matrix.
|
|
|
|
* In GLES this method is a noop. This method should be preferred over glMultMatrix
|
|
|
|
* as it also handles GLES.
|
|
|
|
* @param matrix The matrix the current matrix on the stack should be multiplied with.
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
KWINGLUTILS_EXPORT void multiplyMatrix(const QMatrix4x4 &matrix);
|
2011-01-01 17:55:51 +00:00
|
|
|
/**
|
|
|
|
* Replaces the current matrix on GL stack with @p matrix.
|
|
|
|
* In GLES this method is a no-op. This method should be preferred over glLoadMatrix
|
|
|
|
* as it also handles GLES.
|
|
|
|
* @param matrix The new matrix to replace the existing one on the GL stack.
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
KWINGLUTILS_EXPORT void loadMatrix(const QMatrix4x4 &matrix);
|
2011-01-01 17:36:40 +00:00
|
|
|
/**
|
|
|
|
* Pops the current matrix from the GL matrix stack.
|
|
|
|
* In GLES this method is a noop. This method should be preferred over glPopMatrix
|
|
|
|
* as it also handles GLES.
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
KWINGLUTILS_EXPORT void popMatrix();
|
2011-01-01 17:36:40 +00:00
|
|
|
|
2013-12-03 09:43:57 +00:00
|
|
|
class KWINGLUTILS_EXPORT GLShader
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-07-22 13:44:47 +00:00
|
|
|
enum Flags {
|
|
|
|
NoFlags = 0,
|
|
|
|
ExplicitLinking = (1 << 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
GLShader(const QString &vertexfile, const QString &fragmentfile, unsigned int flags = NoFlags);
|
2011-01-30 14:34:42 +00:00
|
|
|
~GLShader();
|
|
|
|
|
|
|
|
bool isValid() const {
|
|
|
|
return mValid;
|
|
|
|
}
|
|
|
|
|
2012-07-22 13:44:47 +00:00
|
|
|
void bindAttributeLocation(const char *name, int index);
|
|
|
|
void bindFragDataLocation(const char *name, int index);
|
|
|
|
|
|
|
|
bool link();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
int uniformLocation(const char* name);
|
2011-02-04 18:57:19 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool setUniform(const char* name, float value);
|
|
|
|
bool setUniform(const char* name, int value);
|
|
|
|
bool setUniform(const char* name, const QVector2D& value);
|
|
|
|
bool setUniform(const char* name, const QVector3D& value);
|
|
|
|
bool setUniform(const char* name, const QVector4D& value);
|
|
|
|
bool setUniform(const char* name, const QMatrix4x4& value);
|
|
|
|
bool setUniform(const char* name, const QColor& color);
|
2011-02-04 18:57:19 +00:00
|
|
|
|
|
|
|
bool setUniform(int location, float value);
|
|
|
|
bool setUniform(int location, int value);
|
|
|
|
bool setUniform(int location, const QVector2D &value);
|
|
|
|
bool setUniform(int location, const QVector3D &value);
|
|
|
|
bool setUniform(int location, const QVector4D &value);
|
|
|
|
bool setUniform(int location, const QMatrix4x4 &value);
|
|
|
|
bool setUniform(int location, const QColor &value);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
int attributeLocation(const char* name);
|
|
|
|
bool setAttribute(const char* name, float value);
|
|
|
|
/**
|
|
|
|
* @return The value of the uniform as a matrix
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
QMatrix4x4 getUniformMatrix4x4(const char* name);
|
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
enum MatrixUniform {
|
|
|
|
TextureMatrix = 0,
|
|
|
|
ProjectionMatrix,
|
|
|
|
ModelViewMatrix,
|
|
|
|
WindowTransformation,
|
|
|
|
ScreenTransformation,
|
|
|
|
MatrixCount
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Vec2Uniform {
|
|
|
|
Offset,
|
|
|
|
Vec2UniformCount
|
|
|
|
};
|
|
|
|
|
2011-02-12 00:36:21 +00:00
|
|
|
enum Vec4Uniform {
|
|
|
|
ModulationConstant,
|
|
|
|
Vec4UniformCount
|
|
|
|
};
|
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
enum FloatUniform {
|
|
|
|
Saturation,
|
|
|
|
FloatUniformCount
|
|
|
|
};
|
|
|
|
|
|
|
|
enum IntUniform {
|
2012-10-28 10:34:02 +00:00
|
|
|
AlphaToOne, ///< @deprecated no longer used
|
2013-06-02 13:02:58 +00:00
|
|
|
ColorCorrectionLookupTextureUnit,
|
2011-02-10 18:16:04 +00:00
|
|
|
IntUniformCount
|
|
|
|
};
|
|
|
|
|
2013-03-20 19:06:18 +00:00
|
|
|
enum ColorUniform {
|
|
|
|
Color,
|
|
|
|
ColorUniformCount
|
|
|
|
};
|
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
bool setUniform(MatrixUniform uniform, const QMatrix4x4 &matrix);
|
|
|
|
bool setUniform(Vec2Uniform uniform, const QVector2D &value);
|
2011-02-12 00:36:21 +00:00
|
|
|
bool setUniform(Vec4Uniform uniform, const QVector4D &value);
|
2011-02-10 18:16:04 +00:00
|
|
|
bool setUniform(FloatUniform uniform, float value);
|
|
|
|
bool setUniform(IntUniform uniform, int value);
|
2013-03-20 19:06:18 +00:00
|
|
|
bool setUniform(ColorUniform uniform, const QVector4D &value);
|
|
|
|
bool setUniform(ColorUniform uniform, const QColor &value);
|
2011-02-10 18:16:04 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
protected:
|
2012-07-22 13:44:47 +00:00
|
|
|
GLShader(unsigned int flags = NoFlags);
|
2011-01-30 14:34:42 +00:00
|
|
|
bool loadFromFiles(const QString& vertexfile, const QString& fragmentfile);
|
2011-02-04 15:41:55 +00:00
|
|
|
bool load(const QByteArray &vertexSource, const QByteArray &fragmentSource);
|
2012-11-13 20:41:02 +00:00
|
|
|
const QByteArray prepareSource(GLenum shaderType, const QByteArray &sourceCode) const;
|
2011-02-04 16:03:56 +00:00
|
|
|
bool compile(GLuint program, GLenum shaderType, const QByteArray &sourceCode) const;
|
2011-01-30 14:34:42 +00:00
|
|
|
void bind();
|
|
|
|
void unbind();
|
2011-02-10 18:16:04 +00:00
|
|
|
void resolveLocations();
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int mProgram;
|
2011-02-10 18:16:04 +00:00
|
|
|
bool mValid:1;
|
|
|
|
bool mLocationsResolved:1;
|
2012-07-22 13:44:47 +00:00
|
|
|
bool mExplicitLinking:1;
|
2011-02-10 18:16:04 +00:00
|
|
|
int mMatrixLocation[MatrixCount];
|
|
|
|
int mVec2Location[Vec2UniformCount];
|
2011-02-12 00:36:21 +00:00
|
|
|
int mVec4Location[Vec4UniformCount];
|
2011-02-10 18:16:04 +00:00
|
|
|
int mFloatLocation[FloatUniformCount];
|
|
|
|
int mIntLocation[IntUniformCount];
|
2013-03-20 19:06:18 +00:00
|
|
|
int mColorLocation[ColorUniformCount];
|
2011-02-10 18:16:04 +00:00
|
|
|
|
2012-11-13 20:41:02 +00:00
|
|
|
static bool sColorCorrect;
|
|
|
|
|
|
|
|
friend class ColorCorrection;
|
2013-06-17 16:19:32 +00:00
|
|
|
friend class ColorCorrectionPrivate;
|
2011-01-30 14:34:42 +00:00
|
|
|
friend class ShaderManager;
|
|
|
|
};
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
/**
|
|
|
|
* @short Manager for Shaders.
|
|
|
|
*
|
|
|
|
* This class provides some built-in shaders to be used by both compositing scene and effects.
|
|
|
|
* The ShaderManager provides methods to bind a built-in or a custom shader and keeps track of
|
|
|
|
* the shaders which have been bound. When a shader is unbound the previously bound shader
|
|
|
|
* will be rebound.
|
|
|
|
*
|
2013-03-12 12:17:53 +00:00
|
|
|
* @author Martin Gräßlin <mgraesslin@kde.org>
|
2010-12-11 09:25:46 +00:00
|
|
|
* @since 4.7
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
class KWINGLUTILS_EXPORT ShaderManager
|
2010-12-11 09:25:46 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Identifiers for built-in shaders available for effects and scene
|
|
|
|
**/
|
|
|
|
enum ShaderType {
|
2010-12-11 09:25:46 +00:00
|
|
|
/**
|
2011-01-30 14:34:42 +00:00
|
|
|
* An orthographic projection shader able to render textured geometries.
|
|
|
|
* Expects a @c vec2 uniform @c offset describing the offset from top-left corner
|
|
|
|
* and defaults to @c (0/0). Expects a @c vec2 uniform @c textureSize to calculate
|
|
|
|
* normalized texture coordinates. Defaults to @c (1.0/1.0). And expects a @c vec3
|
|
|
|
* uniform @c colorManiuplation, with @c x being opacity, @c y being brightness and
|
|
|
|
* @c z being saturation. All three values default to @c 1.0.
|
|
|
|
* The sampler uniform is @c sample and defaults to @c 0.
|
|
|
|
* The shader uses two vertex attributes @c vertex and @c texCoord.
|
2010-12-11 09:25:46 +00:00
|
|
|
**/
|
2012-09-21 17:00:09 +00:00
|
|
|
SimpleShader = 0,
|
2010-12-11 09:25:46 +00:00
|
|
|
/**
|
2011-01-30 14:34:42 +00:00
|
|
|
* A generic shader able to render transformed, textured geometries.
|
|
|
|
* This shader is mostly needed by the scene and not of much interest for effects.
|
|
|
|
* Effects can influence this shader through @link ScreenPaintData and @link WindowPaintData.
|
|
|
|
* The shader expects four @c mat4 uniforms @c projection, @c modelview,
|
|
|
|
* @c screenTransformation and @c windowTransformation. The fragment shader expect the
|
|
|
|
* same uniforms as the SimpleShader and the same vertex attributes are used.
|
2010-12-11 09:25:46 +00:00
|
|
|
**/
|
2011-01-30 14:34:42 +00:00
|
|
|
GenericShader,
|
2010-12-19 13:38:01 +00:00
|
|
|
/**
|
2011-01-30 14:34:42 +00:00
|
|
|
* An orthographic shader to render simple colored geometries without texturing.
|
|
|
|
* Expects a @c vec2 uniform @c offset describing the offset from top-left corner
|
|
|
|
* and defaults to @c (0/0). The fragment shader expects a single @c vec4 uniform
|
|
|
|
* @c geometryColor, which defaults to fully opaque black.
|
|
|
|
* The Shader uses one vertex attribute @c vertex.
|
2010-12-19 13:38:01 +00:00
|
|
|
**/
|
2011-01-30 14:34:42 +00:00
|
|
|
ColorShader
|
|
|
|
};
|
2010-12-11 09:25:46 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* @return The currently bound shader or @c null if no shader is bound.
|
|
|
|
**/
|
|
|
|
GLShader *getBoundShader() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return @c true if a shader is bound, @c false otherwise
|
|
|
|
**/
|
|
|
|
bool isShaderBound() const;
|
|
|
|
/**
|
2012-09-21 07:09:52 +00:00
|
|
|
* Allows to query whether Shaders are supported by the compositor, that is
|
|
|
|
* whether the Shaders compiled successfully.
|
|
|
|
*
|
|
|
|
* With OpenGL 1 compositing this method will always return @c false.
|
|
|
|
*
|
|
|
|
* Do not use this method to check whether the compositor uses OpenGL 1 or 2,
|
|
|
|
* use @link EffectsHandler::compositingType instead.
|
2011-01-30 14:34:42 +00:00
|
|
|
* @return @c true if the built-in shaders are valid, @c false otherwise
|
|
|
|
**/
|
|
|
|
bool isValid() const;
|
2011-07-23 16:57:50 +00:00
|
|
|
/**
|
|
|
|
* Is @c true if the environment variable KWIN_GL_DEBUG is set to 1.
|
|
|
|
* In that case shaders are compiled with KWIN_SHADER_DEBUG defined.
|
|
|
|
* @returns @c true if shaders are compiled with debug information
|
|
|
|
* @since 4.8
|
|
|
|
**/
|
|
|
|
bool isShaderDebug() const;
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the shader of specified @p type.
|
|
|
|
* To unbind the shader use @link popShader. A previous bound shader will be rebound.
|
|
|
|
* @param type The built-in shader to bind
|
|
|
|
* @param reset Whether all uniforms should be reset to their default values
|
|
|
|
* @return The bound shader or @c NULL if shaders are not valid
|
|
|
|
* @see popShader
|
|
|
|
**/
|
|
|
|
GLShader *pushShader(ShaderType type, bool reset = false);
|
|
|
|
/**
|
|
|
|
* Binds the @p shader.
|
|
|
|
* To unbind the shader use @link popShader. A previous bound shader will be rebound.
|
|
|
|
* To bind a built-in shader use the more specific method.
|
|
|
|
* @param shader The shader to be bound
|
|
|
|
* @see popShader
|
|
|
|
**/
|
|
|
|
void pushShader(GLShader *shader);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbinds the currently bound shader and rebinds a previous stored shader.
|
|
|
|
* If there is no previous shader, no shader will be rebound.
|
|
|
|
* It is not safe to call this method if there is no bound shader.
|
|
|
|
* @see pushShader
|
|
|
|
* @see getBoundShader
|
|
|
|
**/
|
|
|
|
void popShader();
|
|
|
|
|
2011-11-26 15:15:46 +00:00
|
|
|
/**
|
|
|
|
* Resets all shaders to the default uniform values.
|
|
|
|
* Only built in shaders are changed.
|
|
|
|
* @since 4.8
|
|
|
|
**/
|
|
|
|
void resetAllShaders();
|
|
|
|
|
2013-08-17 14:46:46 +00:00
|
|
|
/**
|
|
|
|
* Resets ShaderType @p type uniforms of a custom shader
|
|
|
|
* @since 4.11.1
|
|
|
|
*/
|
|
|
|
void resetShader(GLShader *shader, ShaderType type);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* Creates a GLShader with a built-in vertex shader and a custom fragment shader.
|
|
|
|
* @param vertex The generic vertex shader
|
|
|
|
* @param fragmentFile The path to the source code of the fragment shader
|
|
|
|
* @return The created shader
|
|
|
|
**/
|
|
|
|
GLShader *loadFragmentShader(ShaderType vertex, const QString &fragmentFile);
|
|
|
|
/**
|
|
|
|
* Creates a GLShader with a built-in fragment shader and a custom vertex shader.
|
|
|
|
* @param fragment The generic fragment shader
|
|
|
|
* @param vertexFile The path to the source code of the vertex shader
|
|
|
|
* @return The created shader
|
|
|
|
**/
|
|
|
|
GLShader *loadVertexShader(ShaderType fragment, const QString &vertexFile);
|
|
|
|
/**
|
|
|
|
* Creates a GLShader with the specified sources.
|
|
|
|
* The difference to GLShader is that it does not need to be loaded from files.
|
|
|
|
* @param vertexSource The source code of the vertex shader
|
|
|
|
* @param fragmentSource The source code of the fragment shader.
|
|
|
|
* @return The created shader
|
|
|
|
**/
|
2011-02-04 15:41:55 +00:00
|
|
|
GLShader *loadShaderFromCode(const QByteArray &vertexSource, const QByteArray &fragmentSource);
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a pointer to the ShaderManager instance
|
|
|
|
**/
|
|
|
|
static ShaderManager *instance();
|
2012-09-06 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* @brief Ensures that the ShaderManager is disabled.
|
|
|
|
*
|
|
|
|
* Used only by an OpenGL 1 Scene which does not use OpenGL 2 Shaders.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @since 4.10
|
|
|
|
**/
|
|
|
|
static void disable();
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
**/
|
|
|
|
static void cleanup();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ShaderManager();
|
|
|
|
~ShaderManager();
|
|
|
|
|
|
|
|
void initShaders();
|
|
|
|
void resetShader(ShaderType type);
|
2013-03-13 16:38:35 +00:00
|
|
|
void bindFragDataLocations(GLShader *shader);
|
2012-09-21 17:04:26 +00:00
|
|
|
void bindAttributeLocations(GLShader *shader) const;
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
QStack<GLShader*> m_boundShaders;
|
2012-09-21 17:00:09 +00:00
|
|
|
GLShader *m_shader[3];
|
2011-01-30 14:34:42 +00:00
|
|
|
bool m_inited;
|
|
|
|
bool m_valid;
|
2011-07-23 16:57:50 +00:00
|
|
|
bool m_debug;
|
2013-03-13 16:29:41 +00:00
|
|
|
QByteArray m_shaderDir;
|
2011-01-30 14:34:42 +00:00
|
|
|
static ShaderManager *s_shaderManager;
|
2010-12-11 09:25:46 +00:00
|
|
|
};
|
|
|
|
|
2012-09-21 09:25:08 +00:00
|
|
|
/**
|
|
|
|
* An helper class to push a Shader on to ShaderManager's stack and ensuring that the Shader
|
|
|
|
* gets popped again from the stack automatically once the object goes out of life.
|
|
|
|
*
|
|
|
|
* How to use:
|
|
|
|
* @code
|
|
|
|
* {
|
|
|
|
* GLShader *myCustomShaderIWantToPush;
|
|
|
|
* ShaderBinder binder(myCustomShaderIWantToPush);
|
|
|
|
* // do stuff with the shader being pushed on the stack
|
|
|
|
* }
|
|
|
|
* // here the Shader is automatically popped as helper does no longer exist.
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* This class takes care for the case that the Compositor uses OpenGL 1 and the ShaderManager is
|
|
|
|
* not valid. In that case the helper does not do anything. So this helper can be used to simplify
|
|
|
|
* the code to remove checks for OpenGL 1/2.
|
|
|
|
* @since 4.10
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
class KWINGLUTILS_EXPORT ShaderBinder
|
2012-09-21 09:25:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Pushes the Shader of the given @p type to the ShaderManager's stack.
|
|
|
|
*
|
|
|
|
* @param type The built-in Shader type
|
|
|
|
* @param reset Whether all uniforms should be reset to their default values. Defaults to false.
|
|
|
|
* @see ShaderManager::pushShader
|
|
|
|
**/
|
2012-12-29 06:34:38 +00:00
|
|
|
explicit ShaderBinder(ShaderManager::ShaderType type, bool reset = false);
|
2012-09-21 09:25:08 +00:00
|
|
|
/**
|
|
|
|
* @brief Pushes the given @p shader to the ShaderManager's stack.
|
|
|
|
*
|
|
|
|
* @param shader The Shader to push on the stack
|
|
|
|
* @see ShaderManager::pushShader
|
|
|
|
**/
|
2012-12-29 06:34:38 +00:00
|
|
|
explicit ShaderBinder(GLShader *shader);
|
2012-09-21 09:25:08 +00:00
|
|
|
~ShaderBinder();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The Shader pushed to the Stack. On OpenGL 1 this returns a @c null pointer.
|
|
|
|
**/
|
|
|
|
GLShader *shader();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLShader *m_shader;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
ShaderBinder::ShaderBinder(ShaderManager::ShaderType type, bool reset)
|
|
|
|
: m_shader(NULL)
|
|
|
|
{
|
2012-10-05 08:45:10 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
2012-09-21 09:25:08 +00:00
|
|
|
if (!ShaderManager::instance()->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
m_shader = ShaderManager::instance()->pushShader(type, reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ShaderBinder::ShaderBinder(GLShader *shader)
|
|
|
|
: m_shader(shader)
|
|
|
|
{
|
2012-10-05 08:45:10 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
2012-09-21 09:25:08 +00:00
|
|
|
if (!ShaderManager::instance()->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ShaderBinder::~ShaderBinder()
|
|
|
|
{
|
2012-10-05 08:45:10 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
2012-09-21 09:25:08 +00:00
|
|
|
if (!ShaderManager::instance()->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
GLShader* ShaderBinder::shader()
|
|
|
|
{
|
|
|
|
return m_shader;
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
/**
|
|
|
|
* @short Render target object
|
|
|
|
*
|
|
|
|
* Render target object enables you to render onto a texture. This texture can
|
|
|
|
* later be used to e.g. do post-processing of the scene.
|
|
|
|
*
|
|
|
|
* @author Rivo Laks <rivolaks@hot.ee>
|
|
|
|
**/
|
2013-12-03 09:43:57 +00:00
|
|
|
class KWINGLUTILS_EXPORT GLRenderTarget
|
2007-04-29 17:35:43 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructs a GLRenderTarget
|
|
|
|
* @param color texture where the scene will be rendered onto
|
|
|
|
**/
|
2012-12-29 06:34:38 +00:00
|
|
|
explicit GLRenderTarget(const GLTexture& color);
|
2011-01-30 14:34:42 +00:00
|
|
|
~GLRenderTarget();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables this render target.
|
|
|
|
* All OpenGL commands from now on affect this render target until the
|
|
|
|
* @ref disable method is called
|
|
|
|
**/
|
|
|
|
bool enable();
|
|
|
|
/**
|
|
|
|
* Disables this render target, activating whichever target was active
|
|
|
|
* when @ref enable was called.
|
|
|
|
**/
|
|
|
|
bool disable();
|
|
|
|
|
2011-07-17 15:57:30 +00:00
|
|
|
/**
|
|
|
|
* Sets the target texture
|
|
|
|
* @param target texture where the scene will be rendered on
|
|
|
|
* @since 4.8
|
|
|
|
**/
|
|
|
|
void attachTexture(const GLTexture& target);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool valid() const {
|
|
|
|
return mValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initStatic();
|
|
|
|
static bool supported() {
|
2011-02-04 16:30:36 +00:00
|
|
|
return sSupported;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-03-13 13:34:30 +00:00
|
|
|
static void pushRenderTarget(GLRenderTarget *target);
|
|
|
|
static GLRenderTarget *popRenderTarget();
|
|
|
|
static bool isRenderTargetBound();
|
2011-08-17 17:32:36 +00:00
|
|
|
/**
|
|
|
|
* Whether the GL_EXT_framebuffer_blit extension is supported.
|
|
|
|
* This functionality is not available in OpenGL ES 2.0.
|
|
|
|
*
|
|
|
|
* @returns whether framebuffer blitting is supported.
|
|
|
|
* @since 4.8
|
|
|
|
**/
|
|
|
|
static bool blitSupported();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blits the content of the current draw framebuffer into the texture attached to this FBO.
|
|
|
|
*
|
|
|
|
* Be aware that framebuffer blitting may not be supported on all hardware. Use @link blitSupported to check whether
|
|
|
|
* it is supported.
|
|
|
|
* @param source Geometry in screen coordinates which should be blitted, if not specified complete framebuffer is used
|
|
|
|
* @param destination Geometry in attached texture, if not specified complete texture is used as destination
|
|
|
|
* @param filter The filter to use if blitted content needs to be scaled.
|
|
|
|
* @see blitSupported
|
|
|
|
* @since 4.8
|
|
|
|
**/
|
|
|
|
void blitFromFramebuffer(const QRect &source = QRect(), const QRect &destination = QRect(), GLenum filter = GL_LINEAR);
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void initFBO();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2011-02-04 16:30:36 +00:00
|
|
|
static bool sSupported;
|
2011-08-17 17:32:36 +00:00
|
|
|
static bool s_blitSupported;
|
2011-03-13 13:34:30 +00:00
|
|
|
static QStack<GLRenderTarget*> s_renderTargets;
|
2011-01-30 14:34:42 +00:00
|
|
|
|
2011-07-17 15:57:30 +00:00
|
|
|
GLTexture mTexture;
|
2011-01-30 14:34:42 +00:00
|
|
|
bool mValid;
|
|
|
|
|
|
|
|
GLuint mFramebuffer;
|
2007-04-29 17:35:43 +00:00
|
|
|
};
|
|
|
|
|
2012-09-27 15:26:19 +00:00
|
|
|
enum VertexAttributeType {
|
|
|
|
VA_Position = 0,
|
|
|
|
VA_TexCoord = 1,
|
|
|
|
VertexAttributeCount = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describes the format of a vertex attribute stored in a buffer object.
|
|
|
|
*
|
|
|
|
* The attribute format consists of the attribute index, the number of
|
|
|
|
* vector components, the data type, and the offset of the first element
|
|
|
|
* relative to the start of the vertex data.
|
|
|
|
*/
|
|
|
|
struct GLVertexAttrib
|
|
|
|
{
|
|
|
|
int index; /** The attribute index */
|
|
|
|
int size; /** The number of components [1..4] */
|
|
|
|
GLenum type; /** The type (e.g. GL_FLOAT) */
|
|
|
|
int relativeOffset; /** The relative offset of the attribute */
|
|
|
|
};
|
|
|
|
|
2010-07-19 20:53:32 +00:00
|
|
|
/**
|
|
|
|
* @short Vertex Buffer Object
|
|
|
|
*
|
|
|
|
* This is a short helper class to use vertex buffer objects (VBO). A VBO can be used to buffer
|
|
|
|
* vertex data and to store them on graphics memory. It is the only allowed way to pass vertex
|
|
|
|
* data to the GPU in OpenGL ES 2 and OpenGL 3 with forward compatible mode.
|
|
|
|
*
|
2010-07-24 07:32:42 +00:00
|
|
|
* If VBOs are not supported on the used OpenGL profile this class falls back to legacy
|
|
|
|
* rendering using client arrays. Therefore this class should always be used for rendering geometries.
|
|
|
|
*
|
2013-03-12 12:17:53 +00:00
|
|
|
* @author Martin Gräßlin <mgraesslin@kde.org>
|
2010-07-19 20:53:32 +00:00
|
|
|
* @since 4.6
|
|
|
|
*/
|
2013-12-03 09:43:57 +00:00
|
|
|
class KWINGLUTILS_EXPORT GLVertexBuffer
|
2010-07-19 20:53:32 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Enum to define how often the vertex data in the buffer object changes.
|
|
|
|
*/
|
|
|
|
enum UsageHint {
|
|
|
|
Dynamic, ///< frequent changes, but used several times for rendering
|
|
|
|
Static, ///< No changes to data
|
|
|
|
Stream ///< Data only used once for rendering, updated very frequently
|
|
|
|
};
|
2010-12-11 14:52:21 +00:00
|
|
|
|
2012-12-29 06:34:38 +00:00
|
|
|
explicit GLVertexBuffer(UsageHint hint);
|
2011-01-30 14:34:42 +00:00
|
|
|
~GLVertexBuffer();
|
|
|
|
|
2012-09-27 15:26:19 +00:00
|
|
|
/**
|
|
|
|
* Specifies how interleaved vertex attributes are laid out in
|
|
|
|
* the buffer object.
|
|
|
|
*
|
|
|
|
* Note that the attributes and the stride should be 32 bit aligned
|
|
|
|
* or a performance penalty may be incurred.
|
|
|
|
*
|
|
|
|
* For some hardware the optimal stride is a multiple of 32 bytes.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* struct Vertex {
|
|
|
|
* QVector3D position;
|
|
|
|
* QVector2D texcoord;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* const GLVertexAttrib attribs[] = {
|
|
|
|
* { VA_Position, 3, GL_FLOAT, offsetof(Vertex, position) },
|
|
|
|
* { VA_TexCoord, 2, GL_FLOAT, offsetof(Vertex, texcoord) }
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* Vertex vertices[6];
|
|
|
|
* vbo->setAttribLayout(attribs, 2, sizeof(Vertex));
|
|
|
|
* vbo->setData(vertices, sizeof(vertices));
|
|
|
|
*/
|
|
|
|
void setAttribLayout(const GLVertexAttrib *attribs, int count, int stride);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads data into the buffer object's data store.
|
|
|
|
*/
|
|
|
|
void setData(const void *data, size_t sizeInBytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of vertices that will be drawn by the render() method.
|
|
|
|
*/
|
|
|
|
void setVertexCount(int count);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* Sets the vertex data.
|
|
|
|
* @param numberVertices The number of vertices in the arrays
|
|
|
|
* @param dim The dimension of the vertices: 2 for x/y, 3 for x/y/z
|
|
|
|
* @param vertices The vertices, size must equal @a numberVertices * @a dim
|
|
|
|
* @param texcoords The texture coordinates for each vertex.
|
|
|
|
* Size must equal 2 * @a numberVertices.
|
|
|
|
*/
|
|
|
|
void setData(int numberVertices, int dim, const float* vertices, const float* texcoords);
|
2012-09-26 15:17:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps an unused range of the data store into the client's address space.
|
|
|
|
*
|
|
|
|
* The data store will be reallocated if it is smaller than the given size.
|
|
|
|
*
|
|
|
|
* The buffer object is mapped for writing, not reading. Attempts to read from
|
|
|
|
* the mapped buffer range may result in system errors, including program
|
|
|
|
* termination. The data in the mapped region is undefined until it has been
|
|
|
|
* written to. If subsequent GL calls access unwritten memory, the results are
|
|
|
|
* undefined and system errors, including program termination, may occur.
|
|
|
|
*
|
|
|
|
* No GL calls that access the buffer object must be made while the buffer
|
|
|
|
* object is mapped. The returned pointer must not be passed as a parameter
|
|
|
|
* value to any GL function.
|
|
|
|
*
|
|
|
|
* It is assumed that the GL_ARRAY_BUFFER_BINDING will not be changed while
|
|
|
|
* the buffer object is mapped.
|
|
|
|
*/
|
|
|
|
GLvoid *map(size_t size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes the mapped buffer range and unmaps the buffer.
|
|
|
|
*/
|
|
|
|
void unmap();
|
|
|
|
|
2013-03-18 15:43:08 +00:00
|
|
|
/**
|
|
|
|
* Binds the vertex arrays to the context.
|
|
|
|
*/
|
|
|
|
void bindArrays();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables the vertex arrays.
|
|
|
|
*/
|
|
|
|
void unbindArrays();
|
|
|
|
|
2013-06-10 20:21:16 +00:00
|
|
|
/**
|
|
|
|
* Draws count vertices beginning with first.
|
|
|
|
*/
|
|
|
|
void draw(GLenum primitiveMode, int first, int count);
|
|
|
|
|
2013-03-18 15:43:08 +00:00
|
|
|
/**
|
|
|
|
* Draws count vertices beginning with first.
|
|
|
|
*/
|
|
|
|
void draw(const QRegion ®ion, GLenum primitiveMode, int first, int count, bool hardwareClipping = false);
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* Renders the vertex data in given @a primitiveMode.
|
|
|
|
* Please refer to OpenGL documentation of glDrawArrays or glDrawElements for allowed
|
|
|
|
* values for @a primitiveMode. Best is to use GL_TRIANGLES or similar to be future
|
|
|
|
* compatible.
|
|
|
|
*/
|
|
|
|
void render(GLenum primitiveMode);
|
|
|
|
/**
|
2012-02-12 15:42:09 +00:00
|
|
|
* Same as above restricting painting to @a region if @a hardwareClipping is true.
|
|
|
|
* It's within the caller's responsibility to enable GL_SCISSOR_TEST.
|
2011-01-30 14:34:42 +00:00
|
|
|
*/
|
2012-02-12 15:42:09 +00:00
|
|
|
void render(const QRegion& region, GLenum primitiveMode, bool hardwareClipping = false);
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* Sets the color the geometry will be rendered with.
|
|
|
|
* For legacy rendering glColor is used before rendering the geometry.
|
|
|
|
* For core shader a uniform "geometryColor" is expected and is set.
|
|
|
|
* @param color The color to render the geometry
|
|
|
|
* @param enableColor Whether the geometry should be rendered with a color or not
|
|
|
|
* @see setUseColor
|
|
|
|
* @see isUseColor
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
void setColor(const QColor& color, bool enableColor = true);
|
|
|
|
/**
|
|
|
|
* @return @c true if geometry will be painted with a color, @c false otherwise
|
|
|
|
* @see setUseColor
|
|
|
|
* @see setColor
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
bool isUseColor() const;
|
|
|
|
/**
|
|
|
|
* Enables/Disables rendering the geometry with a color.
|
|
|
|
* If no color is set an opaque, black color is used.
|
|
|
|
* @param enable Enable/Disable rendering with color
|
|
|
|
* @see isUseColor
|
|
|
|
* @see setColor
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
void setUseColor(bool enable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the instance to default values.
|
|
|
|
* Useful for shared buffers.
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
static void initStatic();
|
2013-03-21 22:02:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
static void cleanup();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* Returns true if VBOs are supported, it is save to use this class even if VBOs are not
|
|
|
|
* supported.
|
|
|
|
* @returns true if vertex buffer objects are supported
|
|
|
|
*/
|
|
|
|
static bool isSupported();
|
|
|
|
|
2013-03-21 22:02:07 +00:00
|
|
|
/**
|
|
|
|
* Returns true if indexed quad mode is supported, and false otherwise.
|
|
|
|
*/
|
|
|
|
static bool supportsIndexedQuads();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
/**
|
|
|
|
* @return A shared VBO for streaming data
|
|
|
|
* @since 4.7
|
|
|
|
**/
|
|
|
|
static GLVertexBuffer *streamingBuffer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLVertexBufferPrivate* const d;
|
2010-07-19 20:53:32 +00:00
|
|
|
};
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
} // namespace
|
|
|
|
|
2008-01-16 18:13:24 +00:00
|
|
|
/** @} */
|
|
|
|
|
2007-05-07 11:46:01 +00:00
|
|
|
#endif
|