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 <KDebug>
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2010-06-14 00:18:04 +00:00
|
|
|
if (GLSLBlurShader::supported())
|
2010-03-08 20:45:58 +00:00
|
|
|
return new GLSLBlurShader();
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
return new ARBBlurShader();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QVector<float> BlurShader::gaussianKernel() const
|
|
|
|
{
|
2010-03-10 18:16:18 +00:00
|
|
|
int size = qMin(mRadius | 1, maxKernelSize());
|
|
|
|
if (!(size & 0x1))
|
|
|
|
size -= 1;
|
|
|
|
|
2010-03-05 20:42:10 +00:00
|
|
|
QVector<float> kernel(size);
|
|
|
|
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
|
|
|
|
|
|
|
// Generate the gaussian kernel
|
|
|
|
kernel[center] = gaussian(0, sigma) * .5;
|
|
|
|
for (int i = 1; i <= center; i++) {
|
|
|
|
const float val = gaussian(1.5 + (i - 1) * 2.0, sigma);
|
|
|
|
kernel[center + i] = val;
|
|
|
|
kernel[center - i] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the kernel
|
|
|
|
qreal total = 0;
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
total += kernel[i];
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
kernel[i] /= total;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-06-14 00:18:04 +00:00
|
|
|
bool GLSLBlurShader::supported()
|
|
|
|
{
|
2011-02-19 10:08:43 +00:00
|
|
|
if (!GLPlatform::instance()->supports(GLSL))
|
2010-06-14 00:18:04 +00:00
|
|
|
return false;
|
2011-04-29 14:33:23 +00:00
|
|
|
if (!ShaderManager::instance()->isValid())
|
|
|
|
return false;
|
2010-06-14 00:18:04 +00:00
|
|
|
|
|
|
|
(void) glGetError(); // Clear the error state
|
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2010-06-14 00:18:04 +00:00
|
|
|
// 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;
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-06-14 00:18:04 +00:00
|
|
|
|
|
|
|
if (glGetError() != GL_NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
shader->setUniform("pixelSize", pixelSize);
|
|
|
|
}
|
2010-03-08 20:45:58 +00:00
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
void GLSLBlurShader::setTextureMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
if (!isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shader->setUniform("u_textureMatrix", matrix);
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 15:57:30 +00:00
|
|
|
void GLSLBlurShader::setModelViewProjectionMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
if (!isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shader->setUniform("u_modelViewProjectionMatrix", matrix);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
QVector<float> kernel = gaussianKernel();
|
|
|
|
const int size = kernel.size();
|
|
|
|
const int center = size / 2;
|
|
|
|
|
|
|
|
QByteArray vertexSource;
|
|
|
|
QByteArray fragmentSource;
|
|
|
|
|
|
|
|
// Vertex shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream(&vertexSource);
|
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
stream << "uniform mat4 u_modelViewProjectionMatrix;\n";
|
|
|
|
stream << "uniform mat4 u_textureMatrix;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "uniform vec2 pixelSize;\n\n";
|
2011-01-08 18:56:22 +00:00
|
|
|
stream << "attribute vec4 vertex;\n";
|
|
|
|
stream << "attribute vec4 texCoord;\n\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
stream << "varying 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";
|
2011-05-15 19:19:42 +00:00
|
|
|
stream << " vec4 center = vec4(u_textureMatrix * texCoord).stst;\n";
|
|
|
|
stream << " vec4 ps = pixelSize.stst;\n\n";
|
|
|
|
for (int i = 0; i < size; i += 2) {
|
|
|
|
float offset1, offset2;
|
|
|
|
if (i < center) {
|
|
|
|
offset1 = -(1.5 + (center - i - 1) * 2.0);
|
|
|
|
offset2 = (i + 1) == center ? 0 : offset1 + 2;
|
|
|
|
} else if (i > center) {
|
|
|
|
offset1 = 1.5 + (i - center - 1) * 2.0;
|
|
|
|
offset2 = (i + 1) == size ? 0 : offset1 + 2;
|
|
|
|
} else {
|
|
|
|
offset1 = 0;
|
|
|
|
offset2 = 1.5;
|
|
|
|
}
|
|
|
|
stream << " samplePos[" << i / 2 << "] = center + ps * vec4("
|
|
|
|
<< offset1 << ", " << offset1 << ", " << offset2 << ", " << offset2 << ");\n";
|
|
|
|
}
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
stream << " gl_Position = u_modelViewProjectionMatrix * vertex;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
stream << "}\n";
|
|
|
|
stream.flush();
|
|
|
|
|
|
|
|
// Fragment shader
|
|
|
|
// ===================================================================
|
|
|
|
QTextStream stream2(&fragmentSource);
|
|
|
|
|
2011-05-15 19:19:42 +00:00
|
|
|
stream2 << "uniform sampler2D texUnit;\n";
|
|
|
|
stream2 << "varying vec4 samplePos[" << std::ceil(size / 2.0) << "];\n\n";
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
for (int i = 0; i <= center; i++)
|
|
|
|
stream2 << "const vec4 kernel" << i << " = vec4(" << kernel[i] << ");\n";
|
|
|
|
stream2 << "\n";
|
|
|
|
stream2 << "void main(void)\n";
|
|
|
|
stream2 << "{\n";
|
2011-05-15 19:19:42 +00:00
|
|
|
stream2 << " vec4 sum = texture2D(texUnit, samplePos[0].st) * kernel0;\n";
|
2010-03-08 20:45:58 +00:00
|
|
|
for (int i = 1; i < size; i++)
|
2011-05-15 19:19:42 +00:00
|
|
|
stream2 << " sum = sum + texture2D(texUnit, samplePos[" << i / 2 << ((i % 2) ? "].pq)" : "].st)")
|
|
|
|
<< " * kernel" << (i > center ? size - i - 1 : i) << ";\n";
|
2010-03-12 16:10:57 +00:00
|
|
|
stream2 << " gl_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()) {
|
|
|
|
QMatrix4x4 modelViewProjection;
|
|
|
|
modelViewProjection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535);
|
|
|
|
ShaderManager::instance()->pushShader(shader);
|
|
|
|
shader->setUniform("texUnit", 0);
|
|
|
|
shader->setUniform("u_textureMatrix", QMatrix4x4());
|
|
|
|
shader->setUniform("u_modelViewProjectionMatrix", modelViewProjection);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ARBBlurShader::ARBBlurShader()
|
|
|
|
: BlurShader(), program(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ARBBlurShader::~ARBBlurShader()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARBBlurShader::reset()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2010-03-08 20:45:58 +00:00
|
|
|
if (program) {
|
|
|
|
glDeleteProgramsARB(1, &program);
|
|
|
|
program = 0;
|
|
|
|
}
|
2010-03-12 15:56:19 +00:00
|
|
|
|
|
|
|
setIsValid(false);
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
2010-06-14 00:18:04 +00:00
|
|
|
bool ARBBlurShader::supported()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
return false;
|
|
|
|
#else
|
2010-06-14 00:18:04 +00:00
|
|
|
if (!hasGLExtension("GL_ARB_fragment_program"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
(void) glGetError(); // Clear the error state
|
|
|
|
|
|
|
|
// These are the minimum values the implementation is required to support
|
|
|
|
int value = 0;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_PARAMETERS_ARB, &value);
|
|
|
|
if (value < 24)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_TEMPORARIES_ARB, &value);
|
|
|
|
if (value < 16)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_INSTRUCTIONS_ARB, &value);
|
|
|
|
if (value < 72)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, &value);
|
|
|
|
if (value < 24)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB, &value);
|
|
|
|
if (value < 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (glGetError() != GL_NO_ERROR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-06-14 00:18:04 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:45:58 +00:00
|
|
|
void ARBBlurShader::setPixelDistance(float val)
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(val)
|
|
|
|
#else
|
2010-03-08 20:45:58 +00:00
|
|
|
float firstStep = val * 1.5;
|
|
|
|
float nextStep = val * 2.0;
|
|
|
|
|
|
|
|
if (direction() == Qt::Horizontal) {
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, firstStep, 0, 0, 0);
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, nextStep, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 0, firstStep, 0, 0);
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0, nextStep, 0, 0);
|
|
|
|
}
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARBBlurShader::bind()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2010-03-12 15:56:19 +00:00
|
|
|
if (!isValid())
|
|
|
|
return;
|
2010-03-08 20:45:58 +00:00
|
|
|
|
|
|
|
glEnable(GL_FRAGMENT_PROGRAM_ARB);
|
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program);
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARBBlurShader::unbind()
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2010-11-10 18:25:40 +00:00
|
|
|
int boundObject;
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_BINDING_ARB, &boundObject);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (boundObject == program) {
|
2010-11-10 18:25:40 +00:00
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
|
|
|
|
glDisable(GL_FRAGMENT_PROGRAM_ARB);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-01-08 18:56:22 +00:00
|
|
|
#endif
|
2010-03-08 20:45:58 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 18:16:18 +00:00
|
|
|
int ARBBlurShader::maxKernelSize() const
|
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
return 0;
|
|
|
|
#else
|
2010-03-10 18:16:18 +00:00
|
|
|
int value;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_PARAMETERS_ARB, &value);
|
|
|
|
result = (value - 1) * 2; // We only need to store half the kernel, since it's symmetrical
|
|
|
|
|
|
|
|
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_INSTRUCTIONS_ARB, &value);
|
|
|
|
result = qMin(result, value / 3); // We need 3 instructions / sample
|
|
|
|
|
|
|
|
return result;
|
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 ARBBlurShader::init()
|
2010-03-05 20:42:10 +00:00
|
|
|
{
|
2011-01-08 18:56:22 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2010-03-05 20:42:10 +00:00
|
|
|
QVector<float> kernel = gaussianKernel();
|
|
|
|
const int size = kernel.size();
|
|
|
|
const int center = size / 2;
|
|
|
|
|
|
|
|
QByteArray text;
|
|
|
|
QTextStream stream(&text);
|
|
|
|
|
|
|
|
stream << "!!ARBfp1.0\n";
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
// The kernel values are hardcoded into the program
|
2010-03-05 20:42:10 +00:00
|
|
|
for (int i = 0; i <= center; i++)
|
2010-06-14 22:13:48 +00:00
|
|
|
stream << "PARAM kernel" << i << " = " << kernel[center + i] << ";\n";
|
2010-03-05 20:42:10 +00:00
|
|
|
|
|
|
|
stream << "PARAM firstSample = program.local[0];\n"; // Distance from gl_TexCoord[0] to the next sample
|
|
|
|
stream << "PARAM nextSample = program.local[1];\n"; // Distance to the subsequent sample
|
|
|
|
|
2010-06-14 22:13:48 +00:00
|
|
|
// Temporary variables to hold coordinates and texture samples
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
stream << "TEMP temp" << i << ";\n";
|
|
|
|
|
|
|
|
// Compute the texture coordinates
|
|
|
|
stream << "ADD temp1, fragment.texcoord[0], firstSample;\n"; // temp1 = gl_TexCoord[0] + firstSample
|
|
|
|
stream << "SUB temp2, fragment.texcoord[0], firstSample;\n"; // temp2 = gl_TexCoord[0] - firstSample
|
|
|
|
for (int i = 1, j = 3; i < center; i++, j += 2) {
|
|
|
|
stream << "ADD temp" << j + 0 << ", temp" << j - 2 << ", nextSample;\n";
|
|
|
|
stream << "SUB temp" << j + 1 << ", temp" << j - 1 << ", nextSample;\n";
|
|
|
|
}
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2010-06-14 22:13:48 +00:00
|
|
|
// Sample the texture coordinates
|
|
|
|
stream << "TEX temp0, fragment.texcoord[0], texture[0], 2D;\n";
|
|
|
|
for (int i = 1; i < size; i++)
|
|
|
|
stream << "TEX temp" << i << ", temp" << i << ", texture[0], 2D;\n";
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
// Multiply the samples with the kernel values and compute the sum
|
2010-06-14 22:13:48 +00:00
|
|
|
stream << "MUL temp0, temp0, kernel0;\n";
|
|
|
|
for (int i = 0, j = 1; i < center; i++) {
|
|
|
|
stream << "MAD temp0, temp" << j++ << ", kernel" << i + 1 << ", temp0;\n";
|
|
|
|
stream << "MAD temp0, temp" << j++ << ", kernel" << i + 1 << ", temp0;\n";
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
stream << "MOV result.color, temp0;\n"; // gl_FragColor = temp0
|
2010-03-05 20:42:10 +00:00
|
|
|
stream << "END\n";
|
|
|
|
stream.flush();
|
|
|
|
|
|
|
|
glGenProgramsARB(1, &program);
|
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program);
|
|
|
|
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, text.length(), text.constData());
|
|
|
|
|
|
|
|
if (glGetError()) {
|
|
|
|
const char *error = (const char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
2010-03-08 20:45:58 +00:00
|
|
|
kError() << "Failed to compile fragment program:" << error;
|
2010-03-12 15:56:19 +00:00
|
|
|
setIsValid(false);
|
|
|
|
} else
|
|
|
|
setIsValid(true);
|
2010-03-05 20:42:10 +00:00
|
|
|
|
2011-01-08 18:56:22 +00:00
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
|
|
|
|
#endif
|
2010-03-05 20:42:10 +00:00
|
|
|
}
|
|
|
|
|