From 85e9b6713f252c4842f5277f3ddd141828b6753a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B6glund?= Date: Thu, 10 Feb 2011 19:16:04 +0100 Subject: [PATCH] kwin: Add additional setUniform() overloads These are specialized overloads for setting the uniforms used in the standard GLSL shaders in kwin. --- lib/kwinglutils.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++- lib/kwinglutils.h | 49 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/lib/kwinglutils.cpp b/lib/kwinglutils.cpp index 5455623bf7..af6a04180a 100644 --- a/lib/kwinglutils.cpp +++ b/lib/kwinglutils.cpp @@ -706,6 +706,7 @@ void GLShader::initStatic() GLShader::GLShader() : mProgram(0) , mValid(false) + , mLocationsResolved(false) , mTextureWidth(-1.0f) , mTextureHeight(-1.0f) { @@ -856,12 +857,60 @@ void GLShader::unbind() glUseProgram(0); } +void GLShader::resolveLocations() +{ + if (mLocationsResolved) + return; + + mMatrixLocation[TextureMatrix] = uniformLocation("textureMatrix"); + mMatrixLocation[ProjectionMatrix] = uniformLocation("projection"); + mMatrixLocation[ModelViewMatrix] = uniformLocation("modelview"); + mMatrixLocation[WindowTransformation] = uniformLocation("windowTransformation"); + mMatrixLocation[ScreenTransformation] = uniformLocation("screenTransformation"); + + mVec2Location[Offset] = uniformLocation("offset"); + + mFloatLocation[Opacity] = uniformLocation("opacity"); + mFloatLocation[Brightness] = uniformLocation("brightness"); + mFloatLocation[Saturation] = uniformLocation("saturation"); + mFloatLocation[TextureWidth] = uniformLocation("textureWidth"); + mFloatLocation[TextureHeight] = uniformLocation("textureHeight"); + + mIntLocation[AlphaToOne] = uniformLocation("u_forceAlpha"); + + mLocationsResolved = true; +} + int GLShader::uniformLocation(const char *name) { - int location = glGetUniformLocation(mProgram, name); + const int location = glGetUniformLocation(mProgram, name); return location; } +bool GLShader::setUniform(GLShader::MatrixUniform uniform, const QMatrix4x4 &matrix) +{ + resolveLocations(); + return setUniform(mMatrixLocation[uniform], matrix); +} + +bool GLShader::setUniform(GLShader::Vec2Uniform uniform, const QVector2D &value) +{ + resolveLocations(); + return setUniform(mVec2Location[uniform], value); +} + +bool GLShader::setUniform(GLShader::FloatUniform uniform, float value) +{ + resolveLocations(); + return setUniform(mFloatLocation[uniform], value); +} + +bool GLShader::setUniform(GLShader::IntUniform uniform, int value) +{ + resolveLocations(); + return setUniform(mIntLocation[uniform], value); +} + bool GLShader::setUniform(const char *name, float value) { const int location = uniformLocation(name); diff --git a/lib/kwinglutils.h b/lib/kwinglutils.h index 37820981ca..5c044e15e4 100644 --- a/lib/kwinglutils.h +++ b/lib/kwinglutils.h @@ -279,9 +279,43 @@ public: void setTextureWidth(float width); void setTextureHeight(float height); + float textureWidth(); float textureHeight(); + enum MatrixUniform { + TextureMatrix = 0, + ProjectionMatrix, + ModelViewMatrix, + WindowTransformation, + ScreenTransformation, + MatrixCount + }; + + enum Vec2Uniform { + Offset, + Vec2UniformCount + }; + + enum FloatUniform { + Opacity, + Brightness, + Saturation, + TextureWidth, + TextureHeight, + FloatUniformCount + }; + + enum IntUniform { + AlphaToOne, + IntUniformCount + }; + + bool setUniform(MatrixUniform uniform, const QMatrix4x4 &matrix); + bool setUniform(Vec2Uniform uniform, const QVector2D &value); + bool setUniform(FloatUniform uniform, float value); + bool setUniform(IntUniform uniform, int value); + static void initStatic(); static bool fragmentShaderSupported() { return sFragmentShaderSupported; @@ -298,15 +332,22 @@ protected: bool compile(GLuint program, GLenum shaderType, const QByteArray &sourceCode) const; void bind(); void unbind(); - + void resolveLocations(); private: unsigned int mProgram; - bool mValid; - static bool sFragmentShaderSupported; - static bool sVertexShaderSupported; + bool mValid:1; + bool mLocationsResolved:1; + int mMatrixLocation[MatrixCount]; + int mVec2Location[Vec2UniformCount]; + int mFloatLocation[FloatUniformCount]; + int mIntLocation[IntUniformCount]; float mTextureWidth; float mTextureHeight; + + static bool sFragmentShaderSupported; + static bool sVertexShaderSupported; + friend class ShaderManager; };