From a728823fbe6f06ba863dff4b05ba77e6de8b736d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sun, 6 Mar 2011 09:10:21 +0100 Subject: [PATCH] Fix passing matrixes to the shaders QMatrix4x4 accepts data in row-major order, but returns them in column-major order, which is not documented and because of that I expected them to be in row-major order. This commit fixes it and rewrites the shaders to apply the matrix multiplications in the right order. REVIEW: 100759 --- effects/blur/blurshader.cpp | 4 ++-- effects/cube/cube.cpp | 2 +- effects/cube/data/cylinder.vert | 2 +- effects/cube/data/sphere.vert | 2 +- libkwineffects/kwinglutils.cpp | 14 ++++++-------- scene-color-vertex.glsl | 2 +- scene-generic-vertex.glsl | 2 +- scene-vertex.glsl | 2 +- 8 files changed, 14 insertions(+), 16 deletions(-) diff --git a/effects/blur/blurshader.cpp b/effects/blur/blurshader.cpp index 1542578e15..5e0da02e19 100644 --- a/effects/blur/blurshader.cpp +++ b/effects/blur/blurshader.cpp @@ -229,7 +229,7 @@ void GLSLBlurShader::init() stream << "\n"; stream << "void main(void)\n"; stream << "{\n"; - stream << " vec2 center = vec4(texCoord * u_textureMatrix).st;\n\n"; + stream << " vec2 center = vec4(u_textureMatrix*texCoord).st;\n\n"; for (int i = 0; i < center; i++) stream << " samplePos" << i << " = center + pixelSize * vec2(" @@ -239,7 +239,7 @@ void GLSLBlurShader::init() stream << " samplePos" << i << " = center + pixelSize * vec2(" << 1.5 + (i - center - 1) * 2.0 << ");\n"; stream << "\n"; - stream << " gl_Position = vertex * u_modelViewProjectionMatrix;\n"; + stream << " gl_Position = u_modelViewProjectionMatrix*vertex;\n"; stream << "}\n"; stream.flush(); diff --git a/effects/cube/cube.cpp b/effects/cube/cube.cpp index 89edf32b41..8710d16e92 100644 --- a/effects/cube/cube.cpp +++ b/effects/cube/cube.cpp @@ -1438,7 +1438,7 @@ void CubeEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPa if (reflectionPainting) { shader->setUniform("screenTransformation", m_reflectionMatrix * m_rotationMatrix * origMatrix); } else { - shader->setUniform("screenTransformation", m_rotationMatrix * origMatrix); + shader->setUniform("screenTransformation", origMatrix*m_rotationMatrix); } } } diff --git a/effects/cube/data/cylinder.vert b/effects/cube/data/cylinder.vert index 5d685d368a..9a520bb2b0 100644 --- a/effects/cube/data/cylinder.vert +++ b/effects/cube/data/cylinder.vert @@ -47,5 +47,5 @@ void main() vec3 diff = (vertex.xyz - transformedVertex.xyz)*timeLine; transformedVertex.xyz += diff; - gl_Position = transformedVertex*(windowTransformation*screenTransformation*modelview)*projection; + gl_Position = projection*(modelview*screenTransformation*windowTransformation)*transformedVertex; } diff --git a/effects/cube/data/sphere.vert b/effects/cube/data/sphere.vert index d634d25bcc..8cf3a07b03 100644 --- a/effects/cube/data/sphere.vert +++ b/effects/cube/data/sphere.vert @@ -51,5 +51,5 @@ void main() vec3 diff = (vertex.xyz - transformedVertex.xyz)*timeLine; transformedVertex.xyz += diff; - gl_Position = transformedVertex*(windowTransformation*screenTransformation*modelview)*projection; + gl_Position = projection*(modelview*screenTransformation*windowTransformation)*transformedVertex; } diff --git a/libkwineffects/kwinglutils.cpp b/libkwineffects/kwinglutils.cpp index f8ddb693ff..bcf1713d63 100644 --- a/libkwineffects/kwinglutils.cpp +++ b/libkwineffects/kwinglutils.cpp @@ -989,10 +989,8 @@ bool GLShader::setUniform(int location, const QMatrix4x4 &value) GLfloat m[16]; const qreal *data = value.constData(); // i is column, j is row for m - for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 4; ++j) { - m[i*4+j] = data[j*4+i]; - } + for (int i = 0; i < 16; ++i) { + m[i] = data[i]; } glUniformMatrix4fv(location, 1, GL_FALSE, m); } @@ -1048,10 +1046,10 @@ QMatrix4x4 GLShader::getUniformMatrix4x4(const char* name) if (location >= 0) { GLfloat m[16]; glGetUniformfv(mProgram, location, m); - QMatrix4x4 matrix(m[0], m[1], m[2], m[3], - m[4], m[5], m[6], m[7], - m[8], m[9], m[10], m[11], - m[12], m[13], m[14], m[15]); + QMatrix4x4 matrix(m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15]); matrix.optimize(); return matrix; } else { diff --git a/scene-color-vertex.glsl b/scene-color-vertex.glsl index 651e22ab3a..d06dff09ff 100644 --- a/scene-color-vertex.glsl +++ b/scene-color-vertex.glsl @@ -6,5 +6,5 @@ uniform vec2 offset; attribute vec4 vertex; void main() { - gl_Position = vec4(vertex.xy + offset, vertex.zw) * projection; + gl_Position = projection*vec4(vertex.xy + offset, vertex.zw); } diff --git a/scene-generic-vertex.glsl b/scene-generic-vertex.glsl index 6672dec5a6..1d93b76fb9 100644 --- a/scene-generic-vertex.glsl +++ b/scene-generic-vertex.glsl @@ -16,5 +16,5 @@ varying vec2 varyingTexCoords; void main() { varyingTexCoords = texCoord / vec2(textureWidth, textureHeight); - gl_Position = vertex*(windowTransformation*screenTransformation*modelview)*projection; + gl_Position = projection*(modelview*screenTransformation*windowTransformation)*vertex; } diff --git a/scene-vertex.glsl b/scene-vertex.glsl index 5869dcdac6..43bf297adc 100644 --- a/scene-vertex.glsl +++ b/scene-vertex.glsl @@ -15,5 +15,5 @@ varying vec2 varyingTexCoords; void main() { varyingTexCoords = texCoord / vec2(textureWidth, textureHeight); - gl_Position = vec4(vertex.xy + offset, vertex.zw) * projection; + gl_Position = projection*vec4(vertex.xy + offset, vertex.zw); }