From cab9154723d29c7c8ac53f5d4f0f38fe0e28537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sun, 12 Dec 2010 13:40:39 +0100 Subject: [PATCH] Simple vertex shaders use projection matrix and offset --- lib/kwinglutils.cpp | 25 +++++++++++++------------ scene-color-vertex.glsl | 9 ++++----- scene-vertex.glsl | 9 ++++----- scene_opengl.cpp | 6 +++--- scene_opengl_egl.cpp | 3 ++- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/kwinglutils.cpp b/lib/kwinglutils.cpp index 21fa7aa319..a494822009 100644 --- a/lib/kwinglutils.cpp +++ b/lib/kwinglutils.cpp @@ -545,10 +545,7 @@ void GLTexture::render( QRegion region, const QRect& rect, bool useShader ) m_vbo->setData( 4, 2, verts, texcoords ); } if (useShader) { - GLint currentProgram; - glGetIntegerv( GL_CURRENT_PROGRAM, ¤tProgram ); - GLint location = glGetUniformLocation(currentProgram, "geometry"); - glUniform4f(location, rect.x(), rect.y(), 0.0f, 0.0f); + ShaderManager::instance()->getBoundShader()->setUniform("offset", QVector2D(rect.x(), rect.y())); } else { #ifndef KWIN_HAVE_OPENGLES glTranslatef( rect.x(), rect.y(), 0.0f ); @@ -1260,10 +1257,11 @@ void ShaderManager::resetShader(ShaderType type) // resetShader is either called from init or from push, we know that a built-in shader is bound GLShader *shader = getBoundShader(); switch (type) { - case SimpleShader: - shader->setUniform("displaySize", QVector2D(displayWidth(), displayHeight())); - // TODO: has to become offset - shader->setUniform("geometry", QVector4D(0, 0, 0, 0)); + case SimpleShader: { + QMatrix4x4 projection; + projection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535); + shader->setUniform("projection", projection); + shader->setUniform("offset", QVector2D(0, 0)); shader->setUniform("debug", 0); shader->setUniform("sample", 0); // TODO: has to become textureSize @@ -1274,6 +1272,7 @@ void ShaderManager::resetShader(ShaderType type) shader->setUniform("brightness", 1.0f); shader->setUniform("saturation", 1.0f); break; + } case GenericShader: { shader->setUniform("debug", 0); shader->setUniform("sample", 0); @@ -1305,13 +1304,15 @@ void ShaderManager::resetShader(ShaderType type) shader->setUniform("saturation", 1.0f); break; } - case ColorShader: - shader->setUniform("displaySize", QVector2D(displayWidth(), displayHeight())); - // TODO: has to become offset - shader->setUniform("geometry", QVector4D(0, 0, 0, 0)); + case ColorShader: { + QMatrix4x4 projection; + projection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535); + shader->setUniform("projection", projection); + shader->setUniform("offset", QVector2D(0, 0)); shader->setUniform("geometryColor", QVector4D(0, 0, 0, 1)); break; } + } } /*** GLRenderTarget ***/ diff --git a/scene-color-vertex.glsl b/scene-color-vertex.glsl index 09759c5395..651e22ab3a 100644 --- a/scene-color-vertex.glsl +++ b/scene-color-vertex.glsl @@ -1,11 +1,10 @@ -// size of the complete display in pixels, x==width, y==height -uniform vec2 displaySize; -// geometry of the window/texture to be rendered: x, y, width, height in display geometry -uniform vec4 geometry; +uniform mat4 projection; +// offset of the window/texture to be rendered +uniform vec2 offset; // passed in vertex - only x and y are used attribute vec4 vertex; void main() { - gl_Position.xy = 2.0*vec2(geometry.x + vertex.x, displaySize.y - vertex.y - geometry.y)/displaySize - vertex.ww; + gl_Position = vec4(vertex.xy + offset, vertex.zw) * projection; } diff --git a/scene-vertex.glsl b/scene-vertex.glsl index 766609f286..d3b1c223cd 100644 --- a/scene-vertex.glsl +++ b/scene-vertex.glsl @@ -1,7 +1,6 @@ -// size of the complete display in pixels, x==width, y==height -uniform vec2 displaySize; -// geometry of the window/texture to be rendered: x, y, width, height in display geometry -uniform vec4 geometry; +uniform mat4 projection; +// offset of the window/texture to be rendered +uniform vec2 offset; // passed in vertex - only x and y are used attribute vec4 vertex; @@ -13,5 +12,5 @@ varying vec2 varyingTexCoords; void main() { varyingTexCoords = texCoord; - gl_Position.xy = 2.0*vec2(geometry.x + vertex.x, displaySize.y - vertex.y - geometry.y)/displaySize - vertex.ww; + gl_Position = vec4(vertex.xy + offset, vertex.zw) * projection; } diff --git a/scene_opengl.cpp b/scene_opengl.cpp index adb706eb63..2366bfc1f1 100644 --- a/scene_opengl.cpp +++ b/scene_opengl.cpp @@ -457,7 +457,7 @@ void SceneOpenGL::Window::performPaint( int mask, QRegion region, WindowPaintDat data.shader = ShaderManager::instance()->pushShader(ShaderManager::GenericShader); } else { data.shader = ShaderManager::instance()->pushShader(ShaderManager::SimpleShader); - data.shader->setUniform("geometry", QVector4D(x, y, toplevel->width(), toplevel->height())); + data.shader->setUniform("offset", QVector2D(x, y)); } sceneShader = true; } @@ -1141,7 +1141,7 @@ void SceneOpenGL::EffectFrame::render( QRegion region, double opacity, double fr ShaderManager::instance()->pushShader(shader); } if( sceneShader ) - shader->setUniform("geometry", QVector4D(0, 0, 0, 0)); + shader->setUniform("offset", QVector2D(0, 0)); shader->setUniform("saturation", 1.0f); shader->setUniform("brightness", 1.0f); @@ -1282,7 +1282,7 @@ void SceneOpenGL::EffectFrame::render( QRegion region, double opacity, double fr m_unstyledTexture->bind(); const QPoint pt = m_effectFrame->geometry().topLeft(); if (sceneShader) { - shader->setUniform("geometry", QVector4D(pt.x(), pt.y(), 0.0f, 0.0f)); + shader->setUniform("geometry", QVector2D(pt.x(), pt.y())); } else { #ifndef KWIN_HAVE_OPENGLES glTranslatef( pt.x(), pt.y(), 0.0f ); diff --git a/scene_opengl_egl.cpp b/scene_opengl_egl.cpp index 58a0f3f0d8..4d3a668d45 100644 --- a/scene_opengl_egl.cpp +++ b/scene_opengl_egl.cpp @@ -280,7 +280,8 @@ void SceneOpenGL::paintBackground(QRegion region) vbo->setUseColor(true); vbo->setUseShader(true); vbo->setData(verts.count() / 2, 2, verts.data(), NULL); - ShaderManager::instance()->pushShader(ShaderManager::ColorShader); + GLShader *shader = ShaderManager::instance()->pushShader(ShaderManager::ColorShader); + shader->setUniform("offset", QVector2D(0, 0)); vbo->render(GL_TRIANGLES); ShaderManager::instance()->popShader(); }