2010-03-05 20:42:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
|
|
|
|
*
|
|
|
|
* 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 "blurshader.h"
|
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
#include <kwineffects.h>
|
2011-02-19 10:08:43 +00:00
|
|
|
#include <kwinglplatform.h>
|
2011-01-08 18:56:22 +00:00
|
|
|
|
2010-03-05 20:42:10 +00:00
|
|
|
#include <QByteArray>
|
2011-01-08 18:56:22 +00:00
|
|
|
#include <QMatrix4x4>
|
2010-03-05 20:42:10 +00:00
|
|
|
#include <QTextStream>
|
2011-01-08 18:56:22 +00:00
|
|
|
#include <QVector2D>
|
2010-03-05 20:42:10 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace KWin;
|
|
|
|
|
|
|
|
|
|
|
|
BlurShader::BlurShader()
|
2010-03-12 15:56:19 +00:00
|
|
|
: mRadius(0), mValid(false)
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BlurShader::~BlurShader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
BlurShader *BlurShader::create()
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
2014-06-10 15:09:20 +00:00
|
|
|
return new GLSLBlurShader();
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
void BlurShader::setRadius(int radius)
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
2010-03-08 20:45:58 +00:00
|
|
|
const int r = qMax(radius, 2);
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
if (mRadius != r) {
|
|
|
|
mRadius = r;
|
|
|
|
reset();
|
2010-03-12 15:56:19 +00:00
|
|
|
init();
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
void BlurShader::setDirection(Qt::Orientation direction)
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
2010-03-08 20:45:58 +00:00
|
|
|
mDirection = direction;
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float BlurShader::gaussian(float x, float sigma) const
|
|
|
|
{
|
|
|
|
return (1.0 / std::sqrt(2.0 * M_PI) * sigma)
|
2011-01-30 14:34:42 +00:00
|
|
|
* std::exp(-((x * x) / (2.0 * sigma * sigma)));
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
QList<KernelValue> BlurShader::gaussianKernel() const
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
2010-03-10 18:16:18 +00:00
|
|
|
int size = qMin(mRadius | 1, maxKernelSize());
|
|
|
|
if (!(size & 0x1))
|
|
|
|
size -= 1;
|
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
QList<KernelValue> kernel;
|
2010-03-05 20:42:10 +00:00
|
|
|
const int center = size / 2;
|
2010-03-10 18:16:18 +00:00
|
|
|
const qreal sigma = (size - 1) / 2.5;
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
kernel << KernelValue(0.0, gaussian(0.0, sigma));
|
|
|
|
float total = kernel[0].g;
|
|
|
|
|
|
|
|
for (int x = 1; x <= center; x++) {
|
|
|
|
const float fx = (x - 1) * 2 + 1.5;
|
|
|
|
const float g1 = gaussian(fx - 0.5, sigma);
|
|
|
|
const float g2 = gaussian(fx + 0.5, sigma);
|
|
|
|
|
|
|
|
// Offset taking the contribution of both pixels into account
|
|
|
|
const float offset = .5 - g1 / (g1 + g2);
|
|
|
|
|
|
|
|
kernel << KernelValue(fx + offset, g1 + g2);
|
|
|
|
kernel << KernelValue(-(fx + offset), g1 + g2);
|
|
|
|
|
|
|
|
total += (g1 + g2) * 2;
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
qSort(kernel);
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
// Normalize the kernel
|
|
|
|
for (int i = 0; i < kernel.count(); i++)
|
|
|
|
kernel[i].g /= total;
|
2010-03-05 20:42:10 +00:00
|
|
|
|
|
|
|
return kernel;
|
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLSLBlurShader::GLSLBlurShader()
|
2011-01-08 18:56:22 +00:00
|
|
|
: BlurShader(), shader(NULL)
|
2010-03-08 20:45:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GLSLBlurShader::~GLSLBlurShader()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLSLBlurShader::reset()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
delete shader;
|
|
|
|
shader = NULL;
|
2010-03-12 15:56:19 +00:00
|
|
|
|
|
|
|
setIsValid(false);
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSLBlurShader::setPixelDistance(float val)
|
|
|
|
{
|
2010-03-12 15:56:19 +00:00
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
QVector2D pixelSize(0.0, 0.0);
|
2010-03-08 20:45:58 +00:00
|
|
|
if (direction() == Qt::Horizontal)
|
2011-01-08 18:56:22 +00:00
|
|
|
pixelSize.setX(val);
|
2010-03-08 20:45:58 +00:00
|
|
|
else
|
2011-01-08 18:56:22 +00:00
|
|
|
pixelSize.setY(val);
|
2013-03-25 22:54:24 +00:00
|
|
|
|
|
|
|
shader->setUniform(pixelSizeLocation, pixelSize);
|
2011-01-08 18:56:22 +00:00
|
|
|
}
|
2010-03-08 20:45:58 +00:00
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
void GLSLBlurShader::setTextureMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
2013-03-25 22:54:24 +00:00
|
|
|
if (!isValid())
|
2011-01-08 18:56:22 +00:00
|
|
|
return;
|
2013-03-25 22:54:24 +00:00
|
|
|
|
|
|
|
shader->setUniform(textureMatrixLocation, matrix);
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 15:57:30 +00:00
|
|
|
void GLSLBlurShader::setModelViewProjectionMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
2013-03-25 22:54:24 +00:00
|
|
|
if (!isValid())
|
2011-07-17 15:57:30 +00:00
|
|
|
return;
|
2013-03-25 22:54:24 +00:00
|
|
|
|
|
|
|
shader->setUniform(mvpMatrixLocation, matrix);
|
2011-07-17 15:57:30 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
void GLSLBlurShader::bind()
|
|
|
|
{
|
2010-03-12 15:56:19 +00:00
|
|
|
if (!isValid())
|
|
|
|
return;
|
2010-03-08 20:45:58 +00:00
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
ShaderManager::instance()->pushShader(shader);
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLSLBlurShader::unbind()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 18:16:18 +00:00
|
|
|
int GLSLBlurShader::maxKernelSize() const
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
// GL_MAX_VARYING_FLOATS not available in GLES
|
|
|
|
// querying for GL_MAX_VARYING_VECTORS crashes on nouveau
|
|
|
|
// using the minimum value of 8
|
2011-05-15 19:19:42 +00:00
|
|
|
return 8 * 2;
|
2011-01-08 18:56:22 +00:00
|
|
|
#else
|
2011-08-02 15:26:21 +00:00
|
|
|
int value;
|
2010-03-10 18:16:18 +00:00
|
|
|
glGetIntegerv(GL_MAX_VARYING_FLOATS, &value);
|
2011-05-15 19:19:42 +00:00
|
|
|
// Maximum number of vec4 varyings * 2
|
|
|
|
// The code generator will pack two vec2's into each vec4.
|
|
|
|
return value / 2;
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-03-10 18:16:18 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
void GLSLBlurShader::init()
|
|
|
|
{
|
2013-03-13 17:16:58 +00:00
|
|
|
QList<KernelValue> kernel = gaussianKernel();
|
2010-03-08 20:45:58 +00:00
|
|
|
const int size = kernel.size();
|
|
|
|
const int center = size / 2;
|
|
|
|
|
2013-03-13 17:16:58 +00:00
|
|
|
QList<QVector4D> offsets;
|
|
|
|
for (int i = 0; i < kernel.size(); i += 2) {
|
|
|
|
QVector4D vec4(0, 0, 0, 0);
|
|
|
|
|
|
|
|
vec4.setX(kernel[i].x);
|
|
|
|
vec4.setY(kernel[i].x);
|
|
|
|
|
|
|
|
if (i < kernel.size() - 1) {
|
|
|
|
vec4.setZ(kernel[i + 1].x);
|
|
|
|
vec4.setW(kernel[i + 1].x);
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets << vec4;
|
|
|
|
}
|
|
|
|
|
2013-03-13 17:29:02 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
const bool glsl_140 = false;
|
|
|
|
#else
|
|
|
|
const bool glsl_140 = GLPlatform::instance()->glslVersion() >= kVersionNumber(1, 40);
|
|
|
|
#endif
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
QByteArray vertexSource;
|
|
|
|
QByteArray fragmentSource;
|
|
|
|
|
2013-03-13 17:29:02 +00:00
|
|
|
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";
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
// Vertex shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream(&vertexSource);
|
|
|
|
|
2013-03-13 17:29:02 +00:00
|
|
|
if (glsl_140)
|
|
|
|
stream << "#version 140\n\n";
|
|
|
|
|
2013-03-25 22:54:24 +00:00
|
|
|
stream << "uniform mat4 modelViewProjectionMatrix;\n";
|
|
|
|
stream << "uniform mat4 textureMatrix;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "uniform vec2 pixelSize;\n\n";
|
2013-06-10 16:51:48 +00:00
|
|
|
stream << attribute << " vec4 vertex;\n\n";
|
2013-03-13 17:29:02 +00:00
|
|
|
stream << varying_out << " vec4 samplePos[" << std::ceil(size / 2.0) << "];\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "\n";
|
|
|
|
stream << "void main(void)\n";
|
|
|
|
stream << "{\n";
|
2013-06-10 16:51:48 +00:00
|
|
|
stream << " vec4 center = vec4(textureMatrix * vertex).stst;\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
stream << " vec4 ps = pixelSize.stst;\n\n";
|
2013-03-13 17:16:58 +00:00
|
|
|
for (int i = 0; i < offsets.size(); i++) {
|
|
|
|
stream << " samplePos[" << i << "] = center + ps * vec4("
|
|
|
|
<< offsets[i].x() << ", " << offsets[i].y() << ", "
|
|
|
|
<< offsets[i].z() << ", " << offsets[i].w() << ");\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
}
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "\n";
|
2013-03-25 22:54:24 +00:00
|
|
|
stream << " gl_Position = modelViewProjectionMatrix * vertex;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "}\n";
|
|
|
|
stream.flush();
|
|
|
|
|
|
|
|
// Fragment shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream2(&fragmentSource);
|
|
|
|
|
2013-03-13 17:29:02 +00:00
|
|
|
if (glsl_140)
|
|
|
|
stream2 << "#version 140\n\n";
|
|
|
|
|
2011-05-15 19:19:42 +00:00
|
|
|
stream2 << "uniform sampler2D texUnit;\n";
|
2013-03-13 17:29:02 +00:00
|
|
|
stream2 << varying_in << " vec4 samplePos[" << std::ceil(size / 2.0) << "];\n\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
for (int i = 0; i <= center; i++)
|
2013-03-13 17:16:58 +00:00
|
|
|
stream2 << "const float kernel" << i << " = " << kernel[i].g << ";\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream2 << "\n";
|
2013-03-13 17:29:02 +00:00
|
|
|
|
|
|
|
if (glsl_140)
|
|
|
|
stream2 << "out vec4 fragColor;\n\n";
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
stream2 << "void main(void)\n";
|
|
|
|
stream2 << "{\n";
|
2013-03-13 17:29:02 +00:00
|
|
|
stream2 << " vec4 sum = " << texture2D << "(texUnit, samplePos[0].st) * kernel0;\n";
|
2013-03-13 17:16:58 +00:00
|
|
|
for (int i = 1, j = -center + 1; i < size; i++, j++)
|
2013-03-13 17:29:02 +00:00
|
|
|
stream2 << " sum = sum + " << texture2D << "(texUnit, samplePos[" << i / 2
|
2013-03-13 17:16:58 +00:00
|
|
|
<< ((i % 2) ? "].pq)" : "].st)") << " * kernel" << center - qAbs(j) << ";\n";
|
2013-03-13 17:29:02 +00:00
|
|
|
stream2 << " " << fragColor << " = sum;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream2 << "}\n";
|
|
|
|
stream2.flush();
|
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
shader = ShaderManager::instance()->loadShaderFromCode(vertexSource, fragmentSource);
|
|
|
|
if (shader->isValid()) {
|
2013-03-25 22:54:24 +00:00
|
|
|
pixelSizeLocation = shader->uniformLocation("pixelSize");
|
|
|
|
textureMatrixLocation = shader->uniformLocation("textureMatrix");
|
|
|
|
mvpMatrixLocation = shader->uniformLocation("modelViewProjectionMatrix");
|
|
|
|
|
2011-01-08 18:56:22 +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);
|
2011-01-08 18:56:22 +00:00
|
|
|
ShaderManager::instance()->pushShader(shader);
|
2013-03-25 22:54:24 +00:00
|
|
|
shader->setUniform(textureMatrixLocation, QMatrix4x4());
|
|
|
|
shader->setUniform(mvpMatrixLocation, modelViewProjection);
|
2011-01-08 18:56:22 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
2010-03-12 15:56:19 +00:00
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
setIsValid(shader->isValid());
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|