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 <cmath>
|
|
|
|
|
|
|
|
using namespace KWin;
|
|
|
|
|
|
|
|
|
|
|
|
ContrastShader::ContrastShader()
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
: mValid(false), shader(nullptr), 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-06-10 15:09:20 +00:00
|
|
|
return new ContrastShader();
|
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;
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
shader = nullptr;
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
setIsValid(false);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-11-09 09:38:49 +00:00
|
|
|
const bool gles = GLPlatform::instance()->isGLES();
|
|
|
|
const bool glsl_140 = !gles && GLPlatform::instance()->glslVersion() >= kVersionNumber(1, 40);
|
|
|
|
const bool core = glsl_140 || (gles && GLPlatform::instance()->glslVersion() >= kVersionNumber(3, 0));
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
QByteArray vertexSource;
|
|
|
|
QByteArray fragmentSource;
|
|
|
|
|
2015-11-09 09:38:49 +00:00
|
|
|
const QByteArray attribute = core ? "in" : "attribute";
|
|
|
|
const QByteArray varying_in = core ? (gles ? "in" : "noperspective in") : "varying";
|
|
|
|
const QByteArray varying_out = core ? (gles ? "out" : "noperspective out") : "varying";
|
|
|
|
const QByteArray texture2D = core ? "texture" : "texture2D";
|
|
|
|
const QByteArray fragColor = core ? "fragColor" : "gl_FragColor";
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
// Vertex shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream(&vertexSource);
|
|
|
|
|
2015-11-09 09:38:49 +00:00
|
|
|
if (gles) {
|
|
|
|
if (core) {
|
|
|
|
stream << "#version 300 es\n\n";
|
|
|
|
}
|
|
|
|
stream << "precision highp float;\n";
|
|
|
|
} else if (glsl_140) {
|
2013-12-01 19:28:57 +00:00
|
|
|
stream << "#version 140\n\n";
|
2015-11-09 09:38:49 +00:00
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
|
|
|
|
stream << "uniform mat4 modelViewProjectionMatrix;\n";
|
|
|
|
stream << "uniform mat4 textureMatrix;\n";
|
|
|
|
stream << attribute << " vec4 vertex;\n\n";
|
2014-01-23 14:44:12 +00:00
|
|
|
stream << varying_out << " vec4 varyingTexCoords;\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
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);
|
|
|
|
|
2015-11-09 09:38:49 +00:00
|
|
|
if (gles) {
|
|
|
|
if (core) {
|
|
|
|
stream2 << "#version 300 es\n\n";
|
|
|
|
}
|
|
|
|
stream2 << "precision highp float;\n";
|
|
|
|
} else if (glsl_140) {
|
2013-12-01 19:28:57 +00:00
|
|
|
stream2 << "#version 140\n\n";
|
2015-11-09 09:38:49 +00:00
|
|
|
}
|
2013-12-01 19:28:57 +00:00
|
|
|
|
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";
|
2014-01-23 14:44:12 +00:00
|
|
|
stream2 << varying_in << " vec4 varyingTexCoords;\n";
|
2013-12-01 19:28:57 +00:00
|
|
|
|
2015-11-09 09:38:49 +00:00
|
|
|
if (core)
|
2013-12-01 19:28:57 +00:00
|
|
|
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 << " if (opacity >= 1.0) {\n";
|
|
|
|
stream2 << " " << fragColor << " = tex * colorMatrix;\n";
|
|
|
|
stream2 << " } else {\n";
|
2014-01-23 14:44:12 +00:00
|
|
|
stream2 << " " << fragColor << " = tex * (opacity * colorMatrix + (1.0 - opacity) * mat4(1.0));\n";
|
2014-01-23 13:33:19 +00:00
|
|
|
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;
|
2014-02-24 15:13:30 +00:00
|
|
|
const QSize screenSize = effects->virtualScreenSize();
|
|
|
|
modelViewProjection.ortho(0, screenSize.width(), screenSize.height(), 0, 0, 65535);
|
2013-12-01 19:28:57 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|