2013-12-01 19:28:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
|
2014-01-05 17:28:31 +00:00
|
|
|
* Copyright 2014 Marco Martin <mart@kde.org>
|
2013-12-01 19:28:57 +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; see the file COPYING. if not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contrastshader.h"
|
|
|
|
|
|
|
|
#include <kwineffects.h>
|
|
|
|
#include <kwinglplatform.h>
|
|
|
|
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QMatrix4x4>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QVector2D>
|
|
|
|
#include <KDebug>
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace KWin;
|
|
|
|
|
|
|
|
|
|
|
|
ContrastShader::ContrastShader()
|
2014-01-23 13:33:19 +00:00
|
|
|
: mValid(false), shader(NULL), m_opacity(1)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ContrastShader::~ContrastShader()
|
|
|
|
{
|
2014-01-05 16:20:50 +00:00
|
|
|
reset();
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ContrastShader *ContrastShader::create()
|
|
|
|
{
|
2014-01-05 16:20:50 +00:00
|
|
|
if (ContrastShader::supported()) {
|
2014-01-06 20:43:03 +00:00
|
|
|
return new ContrastShader();
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
return NULL;
|
2013-12-01 19:28:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::reset()
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
delete shader;
|
|
|
|
shader = NULL;
|
|
|
|
|
|
|
|
setIsValid(false);
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
bool ContrastShader::supported()
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
if (!GLPlatform::instance()->supports(GLSL))
|
|
|
|
return false;
|
|
|
|
if (effects->compositingType() == OpenGL1Compositing)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
(void) glGetError(); // Clear the error state
|
|
|
|
|
|
|
|
#ifdef KWIN_HAVE_OPENGL_1
|
|
|
|
// These are the minimum values the implementation is required to support
|
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_VARYING_FLOATS, &value);
|
|
|
|
if (value < 32)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, &value);
|
|
|
|
if (value < 64)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &value);
|
|
|
|
if (value < 512)
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (glGetError() != GL_NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 13:33:19 +00:00
|
|
|
void ContrastShader::setOpacity(float opacity)
|
|
|
|
{
|
|
|
|
m_opacity = opacity;
|
|
|
|
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
|
|
|
shader->setUniform(opacityLocation, opacity);
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
float ContrastShader::opacity() const
|
|
|
|
{
|
|
|
|
return m_opacity;
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::setColorMatrix(const QMatrix4x4 &matrix)
|
2013-12-03 19:20:45 +00:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
|
|
|
shader->setUniform(colorMatrixLocation, matrix);
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::setTextureMatrix(const QMatrix4x4 &matrix)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
shader->setUniform(textureMatrixLocation, matrix);
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::setModelViewProjectionMatrix(const QMatrix4x4 &matrix)
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
shader->setUniform(mvpMatrixLocation, matrix);
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::bind()
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::unbind()
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:20:50 +00:00
|
|
|
void ContrastShader::init()
|
2013-12-01 19:28:57 +00:00
|
|
|
{
|
2014-01-06 20:43:03 +00:00
|
|
|
reset();
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
const bool glsl_140 = false;
|
|
|
|
#else
|
|
|
|
const bool glsl_140 = GLPlatform::instance()->glslVersion() >= kVersionNumber(1, 40);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QByteArray vertexSource;
|
|
|
|
QByteArray fragmentSource;
|
|
|
|
|
|
|
|
const QByteArray attribute = glsl_140 ? "in" : "attribute";
|
|
|
|
const QByteArray varying_in = glsl_140 ? "noperspective in" : "varying";
|
|
|
|
const QByteArray varying_out = glsl_140 ? "noperspective out" : "varying";
|
|
|
|
const QByteArray texture2D = glsl_140 ? "texture" : "texture2D";
|
|
|
|
const QByteArray fragColor = glsl_140 ? "fragColor" : "gl_FragColor";
|
|
|
|
|
|
|
|
// Vertex shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream(&vertexSource);
|
|
|
|
|
|
|
|
if (glsl_140)
|
|
|
|
stream << "#version 140\n\n";
|
|
|
|
|
|
|
|
stream << "uniform mat4 modelViewProjectionMatrix;\n";
|
|
|
|
stream << "uniform mat4 textureMatrix;\n";
|
|
|
|
stream << attribute << " vec4 vertex;\n\n";
|
|
|
|
stream << "out vec4 varyingTexCoords;\n";
|
|
|
|
stream << "\n";
|
|
|
|
stream << "void main(void)\n";
|
|
|
|
stream << "{\n";
|
|
|
|
stream << " varyingTexCoords = vec4(textureMatrix * vertex).stst;\n";
|
|
|
|
stream << " gl_Position = modelViewProjectionMatrix * vertex;\n";
|
|
|
|
stream << "}\n";
|
|
|
|
stream.flush();
|
2014-01-05 17:28:31 +00:00
|
|
|
|
|
|
|
|
2013-12-01 19:28:57 +00:00
|
|
|
// Fragment shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream2(&fragmentSource);
|
|
|
|
|
|
|
|
if (glsl_140)
|
|
|
|
stream2 << "#version 140\n\n";
|
|
|
|
|
2013-12-03 19:20:45 +00:00
|
|
|
stream2 << "uniform mat4 colorMatrix;\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
stream2 << "uniform sampler2D sampler;\n";
|
2014-01-23 13:33:19 +00:00
|
|
|
stream2 << "uniform float opacity;\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
stream2 << "in vec4 varyingTexCoords;\n";
|
|
|
|
|
|
|
|
if (glsl_140)
|
|
|
|
stream2 << "out vec4 fragColor;\n\n";
|
|
|
|
|
|
|
|
stream2 << "void main(void)\n";
|
|
|
|
stream2 << "{\n";
|
|
|
|
stream2 << " vec4 tex = " << texture2D << "(sampler, varyingTexCoords.st);\n";
|
2014-01-23 13:33:19 +00:00
|
|
|
stream2 << " mat4 identity = mat4(1.0, 0.0, 0.0, 0.0,\n";
|
|
|
|
stream2 << " 0.0, 1.0, 0.0, 0.0,\n";
|
|
|
|
stream2 << " 0.0, 0.0, 1.0, 0.0,\n";
|
|
|
|
stream2 << " 0.0, 0.0, 0.0, 1.0);\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
|
2014-01-23 13:33:19 +00:00
|
|
|
stream2 << " if (opacity >= 1.0) {\n";
|
|
|
|
stream2 << " " << fragColor << " = tex * colorMatrix;\n";
|
|
|
|
stream2 << " } else {\n";
|
|
|
|
stream2 << " " << fragColor << " = tex * (opacity * colorMatrix + (1-opacity) * identity);\n";
|
|
|
|
stream2 << " }\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
stream2 << "}\n";
|
|
|
|
stream2.flush();
|
|
|
|
|
|
|
|
shader = ShaderManager::instance()->loadShaderFromCode(vertexSource, fragmentSource);
|
|
|
|
|
|
|
|
if (shader->isValid()) {
|
2013-12-03 19:20:45 +00:00
|
|
|
colorMatrixLocation = shader->uniformLocation("colorMatrix");
|
2013-12-01 19:28:57 +00:00
|
|
|
textureMatrixLocation = shader->uniformLocation("textureMatrix");
|
|
|
|
mvpMatrixLocation = shader->uniformLocation("modelViewProjectionMatrix");
|
2014-01-23 13:33:19 +00:00
|
|
|
opacityLocation = shader->uniformLocation("opacity");
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
QMatrix4x4 modelViewProjection;
|
|
|
|
modelViewProjection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535);
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
2013-12-03 19:20:45 +00:00
|
|
|
shader->setUniform(colorMatrixLocation, QMatrix4x4());
|
2013-12-01 19:28:57 +00:00
|
|
|
shader->setUniform(textureMatrixLocation, QMatrix4x4());
|
|
|
|
shader->setUniform(mvpMatrixLocation, modelViewProjection);
|
2014-01-23 13:33:19 +00:00
|
|
|
shader->setUniform(opacityLocation, (float)1.0);
|
2013-12-01 19:28:57 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsValid(shader->isValid());
|
|
|
|
}
|
|
|
|
|