kwin: Add additional setUniform() overloads

These are specialized overloads for setting the uniforms used
in the standard GLSL shaders in kwin.
This commit is contained in:
Fredrik Höglund 2011-02-10 19:16:04 +01:00
parent 6a72efea96
commit 85e9b6713f
2 changed files with 95 additions and 5 deletions

View file

@ -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);

View file

@ -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;
};