2010-06-02 20:04:54 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2010 by Fredrik Höglund <fredrik@kde.org>
|
2010-11-10 18:25:40 +00:00
|
|
|
Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
|
2010-06-02 20:04:54 +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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "lanczosfilter.h"
|
|
|
|
#include "effects.h"
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
#include <kwinglutils.h>
|
|
|
|
#include <kwinglplatform.h>
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
#include <kwineffects.h>
|
|
|
|
#include <KDE/KGlobalSettings>
|
|
|
|
|
|
|
|
#include <qmath.h>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
LanczosFilter::LanczosFilter(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_offscreenTex(0)
|
|
|
|
, m_offscreenTarget(0)
|
|
|
|
, m_shader(0)
|
|
|
|
, m_inited(false)
|
|
|
|
{
|
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
LanczosFilter::~LanczosFilter()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-06-02 20:04:54 +00:00
|
|
|
delete m_offscreenTarget;
|
|
|
|
delete m_offscreenTex;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
void LanczosFilter::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-06-02 20:04:54 +00:00
|
|
|
if (m_inited)
|
|
|
|
return;
|
|
|
|
m_inited = true;
|
2011-07-20 20:41:58 +00:00
|
|
|
const bool force = (qstrcmp(qgetenv("KWIN_FORCE_LANCZOS"), "1") == 0);
|
|
|
|
if (force) {
|
|
|
|
kWarning(1212) << "Lanczos Filter forced on by environment variable";
|
|
|
|
}
|
2010-11-11 19:53:19 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("kwinrc");
|
2010-11-11 19:53:19 +00:00
|
|
|
|
2011-07-20 20:41:58 +00:00
|
|
|
if (!force && config->group("Compositing").readEntry("GLTextureFilter", 2) != 2)
|
2010-11-11 19:53:19 +00:00
|
|
|
return; // disabled by config
|
|
|
|
|
2011-01-18 22:36:58 +00:00
|
|
|
// The lanczos filter is reported to be broken with the Intel driver and Mesa 7.10
|
|
|
|
GLPlatform *gl = GLPlatform::instance();
|
2011-07-20 20:41:58 +00:00
|
|
|
if (!force && gl->driver() == Driver_Intel && gl->mesaVersion() >= kVersionNumber(7, 10))
|
2011-01-18 22:36:58 +00:00
|
|
|
return;
|
2011-12-31 07:54:15 +00:00
|
|
|
// With fglrx the ARB Shader crashes KWin (see Bug #270818 and #286795) and GLSL Shaders are not functional
|
|
|
|
if (!force && gl->driver() == Driver_Catalyst) {
|
|
|
|
return;
|
|
|
|
}
|
2011-01-18 22:36:58 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
m_shader = new LanczosShader(this);
|
|
|
|
if (!m_shader->init()) {
|
2010-11-10 18:25:40 +00:00
|
|
|
delete m_shader;
|
|
|
|
m_shader = 0;
|
2010-06-02 20:04:54 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
void LanczosFilter::updateOffscreenSurfaces()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-06-02 20:04:54 +00:00
|
|
|
int w = displayWidth();
|
|
|
|
int h = displayHeight();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!GLTexture::NPOTTextureSupported()) {
|
|
|
|
w = nearestPowerOfTwo(w);
|
|
|
|
h = nearestPowerOfTwo(h);
|
|
|
|
}
|
|
|
|
if (!m_offscreenTex || m_offscreenTex->width() != w || m_offscreenTex->height() != h) {
|
|
|
|
if (m_offscreenTex) {
|
2010-06-02 20:04:54 +00:00
|
|
|
delete m_offscreenTex;
|
|
|
|
delete m_offscreenTarget;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
m_offscreenTex = new GLTexture(w, h);
|
|
|
|
m_offscreenTex->setFilter(GL_LINEAR);
|
|
|
|
m_offscreenTex->setWrapMode(GL_CLAMP_TO_EDGE);
|
2011-07-17 15:57:30 +00:00
|
|
|
m_offscreenTarget = new GLRenderTarget(*m_offscreenTex);
|
2010-06-02 20:04:54 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static float sinc(float x)
|
|
|
|
{
|
|
|
|
return std::sin(x * M_PI) / (x * M_PI);
|
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static float lanczos(float x, float a)
|
|
|
|
{
|
|
|
|
if (qFuzzyCompare(x + 1.0, 1.0))
|
2010-06-02 20:04:54 +00:00
|
|
|
return 1.0;
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (qAbs(x) >= a)
|
2010-06-02 20:04:54 +00:00
|
|
|
return 0.0;
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
return sinc(x) * sinc(x / a);
|
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosShader::createKernel(float delta, int *size)
|
|
|
|
{
|
2010-06-02 20:04:54 +00:00
|
|
|
const float a = 2.0;
|
|
|
|
|
|
|
|
// The two outermost samples always fall at points where the lanczos
|
|
|
|
// function returns 0, so we'll skip them.
|
2011-01-30 14:34:42 +00:00
|
|
|
const int sampleCount = qBound(3, qCeil(delta * a) * 2 + 1 - 2, 29);
|
2010-06-02 20:04:54 +00:00
|
|
|
const int center = sampleCount / 2;
|
|
|
|
const int kernelSize = center + 1;
|
|
|
|
const float factor = 1.0 / delta;
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
QVector<float> values(kernelSize);
|
2010-06-02 20:04:54 +00:00
|
|
|
float sum = 0;
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < kernelSize; i++) {
|
|
|
|
const float val = lanczos(i * factor, a);
|
2010-06-02 20:04:54 +00:00
|
|
|
sum += i > 0 ? val * 2 : val;
|
|
|
|
values[i] = val;
|
|
|
|
}
|
|
|
|
|
2011-01-18 22:43:08 +00:00
|
|
|
memset(m_kernel, 0, 16 * sizeof(QVector4D));
|
2010-09-13 22:03:21 +00:00
|
|
|
|
2010-06-02 20:04:54 +00:00
|
|
|
// Normalize the kernel
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < kernelSize; i++) {
|
2010-06-02 20:04:54 +00:00
|
|
|
const float val = values[i] / sum;
|
2011-01-30 14:34:42 +00:00
|
|
|
m_kernel[i] = QVector4D(val, val, val, val);
|
2010-06-02 20:04:54 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 22:03:21 +00:00
|
|
|
*size = kernelSize;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosShader::createOffsets(int count, float width, Qt::Orientation direction)
|
|
|
|
{
|
2011-01-18 22:43:08 +00:00
|
|
|
memset(m_offsets, 0, 16 * sizeof(QVector2D));
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
m_offsets[i] = (direction == Qt::Horizontal) ?
|
|
|
|
QVector2D(i / width, 0) : QVector2D(0, i / width);
|
2010-06-02 20:04:54 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosFilter::performPaint(EffectWindowImpl* w, int mask, QRegion region, WindowPaintData& data)
|
|
|
|
{
|
2011-04-25 14:30:10 +00:00
|
|
|
if (effects->compositingType() == KWin::OpenGLCompositing && (data.xScale < 0.9 || data.yScale < 0.9) &&
|
2011-01-30 14:34:42 +00:00
|
|
|
KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects) {
|
2010-06-02 20:04:54 +00:00
|
|
|
if (!m_inited)
|
|
|
|
init();
|
2011-01-30 14:34:42 +00:00
|
|
|
const QRect screenRect = Workspace::self()->clientArea(ScreenArea, w->screen(), w->desktop());
|
2010-08-29 11:27:57 +00:00
|
|
|
// window geometry may not be bigger than screen geometry to fit into the FBO
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_shader && w->width() <= screenRect.width() && w->height() <= screenRect.height()) {
|
2010-08-08 09:59:03 +00:00
|
|
|
double left = 0;
|
|
|
|
double top = 0;
|
|
|
|
double right = w->width();
|
|
|
|
double bottom = w->height();
|
2011-01-30 14:34:42 +00:00
|
|
|
foreach (const WindowQuad & quad, data.quads) {
|
2010-08-08 09:59:03 +00:00
|
|
|
// we need this loop to include the decoration padding
|
|
|
|
left = qMin(left, quad.left());
|
|
|
|
top = qMin(top, quad.top());
|
|
|
|
right = qMax(right, quad.right());
|
|
|
|
bottom = qMax(bottom, quad.bottom());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-08-08 09:59:03 +00:00
|
|
|
double width = right - left;
|
|
|
|
double height = bottom - top;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (width > screenRect.width() || height > screenRect.height()) {
|
2010-08-29 11:27:57 +00:00
|
|
|
// window with padding does not fit into the framebuffer
|
|
|
|
// so cut of the shadow
|
|
|
|
left = 0;
|
|
|
|
top = 0;
|
|
|
|
width = w->width();
|
|
|
|
height = w->height();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
int tx = data.xTranslate + w->x() + left * data.xScale;
|
|
|
|
int ty = data.yTranslate + w->y() + top * data.yScale;
|
|
|
|
int tw = width * data.xScale;
|
|
|
|
int th = height * data.yScale;
|
2010-11-01 10:46:11 +00:00
|
|
|
const QRect textureRect(tx, ty, tw, th);
|
2010-08-08 09:59:03 +00:00
|
|
|
|
|
|
|
int sw = width;
|
|
|
|
int sh = height;
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
GLTexture *cachedTexture = static_cast< GLTexture*>(w->data(LanczosCacheRole).value<void*>());
|
|
|
|
if (cachedTexture) {
|
|
|
|
if (cachedTexture->width() == tw && cachedTexture->height() == th) {
|
2010-11-01 10:46:11 +00:00
|
|
|
cachedTexture->bind();
|
2011-01-02 20:28:07 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
|
|
|
const float rgb = data.brightness * data.opacity;
|
|
|
|
const float a = data.opacity;
|
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
GLShader *shader = ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
|
2011-02-10 18:37:51 +00:00
|
|
|
shader->setUniform(GLShader::Offset, QVector2D(0, 0));
|
2011-02-12 00:36:21 +00:00
|
|
|
shader->setUniform(GLShader::ModulationConstant, QVector4D(rgb, rgb, rgb, a));
|
2011-02-10 18:37:51 +00:00
|
|
|
shader->setUniform(GLShader::Saturation, data.saturation);
|
|
|
|
shader->setUniform(GLShader::AlphaToOne, 0);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
cachedTexture->render(textureRect, textureRect);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
} else {
|
2011-01-30 14:34:42 +00:00
|
|
|
prepareRenderStates(cachedTexture, data.opacity, data.brightness, data.saturation);
|
|
|
|
cachedTexture->render(textureRect, textureRect);
|
|
|
|
restoreRenderStates(cachedTexture, data.opacity, data.brightness, data.saturation);
|
2011-01-02 20:28:07 +00:00
|
|
|
}
|
2010-11-01 10:46:11 +00:00
|
|
|
cachedTexture->unbind();
|
2011-01-30 14:34:42 +00:00
|
|
|
m_timer.start(5000, this);
|
2010-11-01 10:46:11 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-11-01 10:46:11 +00:00
|
|
|
// offscreen texture not matching - delete
|
|
|
|
delete cachedTexture;
|
|
|
|
cachedTexture = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
w->setData(LanczosCacheRole, QVariant());
|
2010-11-01 10:46:11 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
WindowPaintData thumbData = data;
|
|
|
|
thumbData.xScale = 1.0;
|
|
|
|
thumbData.yScale = 1.0;
|
2010-08-08 09:59:03 +00:00
|
|
|
thumbData.xTranslate = -w->x() - left;
|
|
|
|
thumbData.yTranslate = -w->y() - top;
|
2010-11-01 10:46:11 +00:00
|
|
|
thumbData.brightness = 1.0;
|
|
|
|
thumbData.opacity = 1.0;
|
|
|
|
thumbData.saturation = 1.0;
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Bind the offscreen FBO and draw the window on it unscaled
|
|
|
|
updateOffscreenSurfaces();
|
2011-03-13 13:34:30 +00:00
|
|
|
GLRenderTarget::pushRenderTarget(m_offscreenTarget);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
2011-01-30 14:34:42 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
w->sceneWindow()->performPaint(mask, infiniteRegion(), thumbData);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Create a scratch texture and copy the rendered window into it
|
2011-01-30 14:34:42 +00:00
|
|
|
GLTexture tex(sw, sh);
|
|
|
|
tex.setFilter(GL_LINEAR);
|
|
|
|
tex.setWrapMode(GL_CLAMP_TO_EDGE);
|
2010-06-02 20:04:54 +00:00
|
|
|
tex.bind();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - sh, sw, sh);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Set up the shader for horizontal scaling
|
|
|
|
float dx = sw / float(tw);
|
2010-09-13 22:03:21 +00:00
|
|
|
int kernelSize;
|
2011-01-30 14:34:42 +00:00
|
|
|
m_shader->createKernel(dx, &kernelSize);
|
|
|
|
m_shader->createOffsets(kernelSize, sw, Qt::Horizontal);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
m_shader->bind();
|
2010-11-10 18:25:40 +00:00
|
|
|
m_shader->setUniforms();
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Draw the window back into the FBO, this time scaled horizontally
|
2011-01-30 14:34:42 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2011-01-02 20:28:07 +00:00
|
|
|
QVector<float> verts;
|
|
|
|
QVector<float> texCoords;
|
|
|
|
verts.reserve(12);
|
|
|
|
texCoords.reserve(12);
|
|
|
|
|
|
|
|
texCoords << 1.0 << 0.0; verts << tw << 0.0; // Top right
|
|
|
|
texCoords << 0.0 << 0.0; verts << 0.0 << 0.0; // Top left
|
|
|
|
texCoords << 0.0 << 1.0; verts << 0.0 << sh; // Bottom left
|
|
|
|
texCoords << 0.0 << 1.0; verts << 0.0 << sh; // Bottom left
|
|
|
|
texCoords << 1.0 << 1.0; verts << tw << sh; // Bottom right
|
|
|
|
texCoords << 1.0 << 0.0; verts << tw << 0.0; // Top right
|
|
|
|
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
|
|
|
|
vbo->reset();
|
|
|
|
vbo->setData(6, 2, verts.constData(), texCoords.constData());
|
|
|
|
vbo->render(GL_TRIANGLES);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// At this point we don't need the scratch texture anymore
|
|
|
|
tex.unbind();
|
|
|
|
tex.discard();
|
|
|
|
|
2010-11-01 10:46:11 +00:00
|
|
|
// create scratch texture for second rendering pass
|
2011-01-30 14:34:42 +00:00
|
|
|
GLTexture tex2(tw, sh);
|
|
|
|
tex2.setFilter(GL_LINEAR);
|
|
|
|
tex2.setWrapMode(GL_CLAMP_TO_EDGE);
|
2010-11-01 10:46:11 +00:00
|
|
|
tex2.bind();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - sh, tw, sh);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Set up the shader for vertical scaling
|
|
|
|
float dy = sh / float(th);
|
2011-01-30 14:34:42 +00:00
|
|
|
m_shader->createKernel(dy, &kernelSize);
|
|
|
|
m_shader->createOffsets(kernelSize, m_offscreenTex->height(), Qt::Vertical);
|
2010-11-10 18:25:40 +00:00
|
|
|
m_shader->setUniforms();
|
2010-06-02 20:04:54 +00:00
|
|
|
|
|
|
|
// Now draw the horizontally scaled window in the FBO at the right
|
|
|
|
// coordinates on the screen, while scaling it vertically and blending it.
|
2011-01-30 14:34:42 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
verts.clear();
|
|
|
|
|
|
|
|
verts << tw << 0.0; // Top right
|
|
|
|
verts << 0.0 << 0.0; // Top left
|
|
|
|
verts << 0.0 << th; // Bottom left
|
|
|
|
verts << 0.0 << th; // Bottom left
|
|
|
|
verts << tw << th; // Bottom right
|
|
|
|
verts << tw << 0.0; // Top right
|
|
|
|
vbo->setData(6, 2, verts.constData(), texCoords.constData());
|
|
|
|
vbo->render(GL_TRIANGLES);
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2010-11-01 10:46:11 +00:00
|
|
|
tex2.unbind();
|
|
|
|
tex2.discard();
|
2010-06-02 20:04:54 +00:00
|
|
|
m_shader->unbind();
|
|
|
|
|
2010-11-01 10:46:11 +00:00
|
|
|
// create cache texture
|
2011-01-30 14:34:42 +00:00
|
|
|
GLTexture *cache = new GLTexture(tw, th);
|
2010-11-01 10:46:11 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
cache->setFilter(GL_LINEAR);
|
|
|
|
cache->setWrapMode(GL_CLAMP_TO_EDGE);
|
2010-11-01 10:46:11 +00:00
|
|
|
cache->bind();
|
2011-01-30 14:34:42 +00:00
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - th, tw, th);
|
2011-03-13 13:34:30 +00:00
|
|
|
GLRenderTarget::popRenderTarget();
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
if (ShaderManager::instance()->isValid()) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
|
|
|
const float rgb = data.brightness * data.opacity;
|
|
|
|
const float a = data.opacity;
|
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
GLShader *shader = ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
|
2011-02-10 18:37:51 +00:00
|
|
|
shader->setUniform(GLShader::Offset, QVector2D(0, 0));
|
2011-02-12 00:36:21 +00:00
|
|
|
shader->setUniform(GLShader::ModulationConstant, QVector4D(rgb, rgb, rgb, a));
|
2011-02-10 18:37:51 +00:00
|
|
|
shader->setUniform(GLShader::Saturation, data.saturation);
|
|
|
|
shader->setUniform(GLShader::AlphaToOne, 0);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
cache->render(textureRect, textureRect);
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
} else {
|
2011-01-30 14:34:42 +00:00
|
|
|
prepareRenderStates(cache, data.opacity, data.brightness, data.saturation);
|
|
|
|
cache->render(textureRect, textureRect);
|
|
|
|
restoreRenderStates(cache, data.opacity, data.brightness, data.saturation);
|
2011-01-02 20:28:07 +00:00
|
|
|
}
|
2011-02-12 00:36:21 +00:00
|
|
|
|
2010-11-01 10:46:11 +00:00
|
|
|
cache->unbind();
|
2011-01-30 14:34:42 +00:00
|
|
|
w->setData(LanczosCacheRole, QVariant::fromValue(static_cast<void*>(cache)));
|
2010-11-01 10:46:11 +00:00
|
|
|
|
2010-06-02 20:04:54 +00:00
|
|
|
// Delete the offscreen surface after 5 seconds
|
2011-01-30 14:34:42 +00:00
|
|
|
m_timer.start(5000, this);
|
2010-06-02 20:04:54 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
} // if ( effects->compositingType() == KWin::OpenGLCompositing )
|
|
|
|
w->sceneWindow()->performPaint(mask, region, data);
|
|
|
|
} // End of function
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosFilter::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
|
|
|
if (event->timerId() == m_timer.timerId()) {
|
2010-06-02 20:04:54 +00:00
|
|
|
m_timer.stop();
|
|
|
|
|
|
|
|
delete m_offscreenTarget;
|
|
|
|
delete m_offscreenTex;
|
|
|
|
m_offscreenTarget = 0;
|
|
|
|
m_offscreenTex = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
foreach (EffectWindow * w, effects->stackingOrder()) {
|
|
|
|
QVariant cachedTextureVariant = w->data(LanczosCacheRole);
|
|
|
|
if (cachedTextureVariant.isValid()) {
|
2010-11-01 10:46:11 +00:00
|
|
|
GLTexture *cachedTexture = static_cast< GLTexture*>(cachedTextureVariant.value<void*>());
|
|
|
|
delete cachedTexture;
|
|
|
|
cachedTexture = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
w->setData(LanczosCacheRole, QVariant());
|
2010-11-01 10:46:11 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-02 20:04:54 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosFilter::prepareRenderStates(GLTexture* tex, double opacity, double brightness, double saturation)
|
|
|
|
{
|
2011-12-31 14:04:14 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(tex)
|
|
|
|
Q_UNUSED(opacity)
|
|
|
|
Q_UNUSED(brightness)
|
|
|
|
Q_UNUSED(saturation)
|
|
|
|
#else
|
2010-12-19 21:32:02 +00:00
|
|
|
const bool alpha = true;
|
2010-11-01 10:46:11 +00:00
|
|
|
// setup blending of transparent windows
|
2011-01-30 14:34:42 +00:00
|
|
|
glPushAttrib(GL_ENABLE_BIT);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
if (saturation != 1.0 && tex->saturationSupported()) {
|
2010-11-01 10:46:11 +00:00
|
|
|
// First we need to get the color from [0; 1] range to [0.5; 1] range
|
2011-01-30 14:34:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
|
2010-11-01 10:46:11 +00:00
|
|
|
const float scale_constant[] = { 1.0, 1.0, 1.0, 0.5};
|
2011-01-30 14:34:42 +00:00
|
|
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, scale_constant);
|
2010-11-01 10:46:11 +00:00
|
|
|
tex->bind();
|
|
|
|
|
|
|
|
// Then we take dot product of the result of previous pass and
|
|
|
|
// saturation_constant. This gives us completely unsaturated
|
|
|
|
// (greyscale) image
|
|
|
|
// Note that both operands have to be in range [0.5; 1] since opengl
|
|
|
|
// automatically substracts 0.5 from them
|
2011-01-30 14:34:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
|
|
float saturation_constant[] = { 0.5 + 0.5 * 0.30, 0.5 + 0.5 * 0.59, 0.5 + 0.5 * 0.11, saturation };
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, saturation_constant);
|
2010-11-01 10:46:11 +00:00
|
|
|
tex->bind();
|
|
|
|
|
|
|
|
// Finally we need to interpolate between the original image and the
|
|
|
|
// greyscale image to get wanted level of saturation
|
2011-01-30 14:34:42 +00:00
|
|
|
glActiveTexture(GL_TEXTURE2);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE0);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
|
|
|
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, saturation_constant);
|
2010-11-01 10:46:11 +00:00
|
|
|
// Also replace alpha by primary color's alpha here
|
2011-01-30 14:34:42 +00:00
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
|
2010-11-01 10:46:11 +00:00
|
|
|
// And make primary color contain the wanted opacity
|
2011-01-30 14:34:42 +00:00
|
|
|
glColor4f(opacity, opacity, opacity, opacity);
|
2010-11-01 10:46:11 +00:00
|
|
|
tex->bind();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (alpha || brightness != 1.0f) {
|
|
|
|
glActiveTexture(GL_TEXTURE3);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
2010-11-01 10:46:11 +00:00
|
|
|
// The color has to be multiplied by both opacity and brightness
|
|
|
|
float opacityByBrightness = opacity * brightness;
|
2011-01-30 14:34:42 +00:00
|
|
|
glColor4f(opacityByBrightness, opacityByBrightness, opacityByBrightness, opacity);
|
|
|
|
if (alpha) {
|
2010-11-01 10:46:11 +00:00
|
|
|
// Multiply original texture's alpha by our opacity
|
2011-01-30 14:34:42 +00:00
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE0);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
|
|
|
|
} else {
|
2010-11-01 10:46:11 +00:00
|
|
|
// Alpha will be taken from previous stage
|
2011-01-30 14:34:42 +00:00
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
|
2010-11-01 10:46:11 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
tex->bind();
|
2010-11-01 10:46:11 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
} else if (opacity != 1.0 || brightness != 1.0) {
|
2010-11-01 10:46:11 +00:00
|
|
|
// the window is additionally configured to have its opacity adjusted,
|
|
|
|
// do it
|
|
|
|
float opacityByBrightness = opacity * brightness;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (alpha) {
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
glColor4f(opacityByBrightness, opacityByBrightness, opacityByBrightness,
|
|
|
|
opacity);
|
|
|
|
} else {
|
2010-11-01 10:46:11 +00:00
|
|
|
// Multiply color by brightness and replace alpha by opacity
|
|
|
|
float constant[] = { opacityByBrightness, opacityByBrightness, opacityByBrightness, opacity };
|
2011-01-30 14:34:42 +00:00
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_CONSTANT);
|
|
|
|
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constant);
|
2010-11-01 10:46:11 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-20 12:30:55 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-01 10:46:11 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LanczosFilter::restoreRenderStates(GLTexture* tex, double opacity, double brightness, double saturation)
|
|
|
|
{
|
2011-12-31 14:04:14 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(tex)
|
|
|
|
Q_UNUSED(opacity)
|
|
|
|
Q_UNUSED(brightness)
|
|
|
|
Q_UNUSED(saturation)
|
|
|
|
#else
|
2011-01-30 14:34:42 +00:00
|
|
|
if (opacity != 1.0 || saturation != 1.0 || brightness != 1.0f) {
|
|
|
|
if (saturation != 1.0 && tex->saturationSupported()) {
|
2010-11-01 10:46:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE3);
|
2011-01-30 14:34:42 +00:00
|
|
|
glDisable(tex->target());
|
2010-11-01 10:46:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE2);
|
2011-01-30 14:34:42 +00:00
|
|
|
glDisable(tex->target());
|
2010-11-01 10:46:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE1);
|
2011-01-30 14:34:42 +00:00
|
|
|
glDisable(tex->target());
|
2010-11-01 10:46:11 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
|
|
|
glColor4f(0, 0, 0, 0);
|
2010-11-01 10:46:11 +00:00
|
|
|
|
|
|
|
glPopAttrib(); // ENABLE_BIT
|
2010-11-20 12:30:55 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-01 10:46:11 +00:00
|
|
|
|
2010-11-10 18:25:40 +00:00
|
|
|
/************************************************
|
|
|
|
* LanczosShader
|
|
|
|
************************************************/
|
2011-01-30 14:34:42 +00:00
|
|
|
LanczosShader::LanczosShader(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_shader(0)
|
|
|
|
, m_arbProgram(0)
|
|
|
|
{
|
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
|
|
|
LanczosShader::~LanczosShader()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-11-10 18:25:40 +00:00
|
|
|
delete m_shader;
|
2011-01-02 20:28:07 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_arbProgram) {
|
2010-11-10 18:25:40 +00:00
|
|
|
glDeleteProgramsARB(1, &m_arbProgram);
|
|
|
|
m_arbProgram = 0;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
|
|
|
void LanczosShader::bind()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (m_shader)
|
2011-01-02 20:28:07 +00:00
|
|
|
ShaderManager::instance()->pushShader(m_shader);
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
else {
|
2010-11-10 18:25:40 +00:00
|
|
|
glEnable(GL_FRAGMENT_PROGRAM_ARB);
|
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, m_arbProgram);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
|
|
|
void LanczosShader::unbind()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (m_shader)
|
2011-01-02 20:28:07 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
else {
|
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 == m_arbProgram) {
|
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
|
|
|
#endif
|
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
|
|
|
void LanczosShader::setUniforms()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (m_shader) {
|
|
|
|
glUniform1i(m_uTexUnit, 0);
|
|
|
|
glUniform2fv(m_uOffsets, 16, (const GLfloat*)m_offsets);
|
|
|
|
glUniform4fv(m_uKernel, 16, (const GLfloat*)m_kernel);
|
|
|
|
}
|
2011-01-02 20:28:07 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
else {
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, i, m_offsets[i].x(), m_offsets[i].y(), 0, 0);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, i + 16, m_kernel[i].x(), m_kernel[i].y(), m_kernel[i].z(), m_kernel[i].w());
|
2010-11-10 18:25:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
|
|
|
bool LanczosShader::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-01-18 22:43:08 +00:00
|
|
|
GLPlatform *gl = GLPlatform::instance();
|
2011-02-19 10:08:43 +00:00
|
|
|
if (gl->supports(GLSL) &&
|
2011-04-29 14:39:15 +00:00
|
|
|
ShaderManager::instance()->isValid() &&
|
2011-01-30 14:34:42 +00:00
|
|
|
GLRenderTarget::supported() &&
|
|
|
|
!(gl->isRadeon() && gl->chipClass() < R600)) {
|
2011-01-02 20:28:07 +00:00
|
|
|
m_shader = ShaderManager::instance()->loadFragmentShader(ShaderManager::SimpleShader, ":/resources/lanczos-fragment.glsl");
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_shader->isValid()) {
|
2011-01-30 12:59:07 +00:00
|
|
|
ShaderManager::instance()->pushShader(m_shader);
|
2010-11-10 18:25:40 +00:00
|
|
|
m_uTexUnit = m_shader->uniformLocation("texUnit");
|
|
|
|
m_uKernel = m_shader->uniformLocation("kernel");
|
|
|
|
m_uOffsets = m_shader->uniformLocation("offsets");
|
2011-01-30 12:59:07 +00:00
|
|
|
ShaderManager::instance()->popShader();
|
2010-11-10 18:25:40 +00:00
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-11-10 18:25:40 +00:00
|
|
|
kDebug(1212) << "Shader is not valid";
|
|
|
|
m_shader = 0;
|
|
|
|
// try ARB shader
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
2011-01-02 20:28:07 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
// no ARB shader in GLES
|
|
|
|
return false;
|
|
|
|
#else
|
2010-11-10 18:25:40 +00:00
|
|
|
// try to create an ARB Shader
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!hasGLExtension("GL_ARB_fragment_program"))
|
2010-11-10 18:25:40 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QByteArray text;
|
|
|
|
QTextStream stream(&text);
|
|
|
|
|
2011-01-18 22:43:08 +00:00
|
|
|
// Note: This program uses 31 temporaries, 61 ALU instructions, 31 texture
|
|
|
|
// fetches, 3 texture indirections and 93 instructions.
|
|
|
|
// The R300 limitations are 32, 64, 32, 4 and 96 respectively.
|
2010-11-10 18:25:40 +00:00
|
|
|
stream << "!!ARBfp1.0\n";
|
2011-01-18 22:43:08 +00:00
|
|
|
stream << "TEMP sum;\n";
|
|
|
|
|
|
|
|
// Declare 30 temporaries for holding texcoords and TEX results
|
|
|
|
for (int i = 0; i < 30; i++)
|
|
|
|
stream << "TEMP temp" << i << ";\n";
|
|
|
|
|
|
|
|
// Compute the texture coordinates
|
|
|
|
for (int i = 0, j = 0; i < 30 / 2; i++) {
|
2011-01-30 14:34:42 +00:00
|
|
|
stream << "ADD temp" << j++ << ", fragment.texcoord, program.local[" << i + 1 << "];\n";
|
|
|
|
stream << "SUB temp" << j++ << ", fragment.texcoord, program.local[" << i + 1 << "];\n";
|
2011-01-18 22:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sample the texture coordinates
|
|
|
|
stream << "TEX sum, fragment.texcoord, texture[0], 2D;\n";
|
|
|
|
for (int i = 0; i < 30; i++)
|
|
|
|
stream << "TEX temp" << i << ", temp" << i << ", texture[0], 2D;\n";
|
|
|
|
|
|
|
|
// Process the results
|
|
|
|
stream << "MUL sum, sum, program.local[16];\n"; // sum = sum * kernel[0]
|
|
|
|
for (int i = 0, j = 0; i < 30 / 2; i++) {
|
2011-01-30 14:34:42 +00:00
|
|
|
stream << "MAD sum, temp" << j++ << ", program.local[" << (17 + i) << "], sum;\n";
|
|
|
|
stream << "MAD sum, temp" << j++ << ", program.local[" << (17 + i) << "], sum;\n";
|
2011-01-18 22:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stream << "MOV result.color, sum;\n";
|
2010-11-10 18:25:40 +00:00
|
|
|
stream << "END\n";
|
|
|
|
stream.flush();
|
|
|
|
|
|
|
|
glEnable(GL_FRAGMENT_PROGRAM_ARB);
|
|
|
|
glGenProgramsARB(1, &m_arbProgram);
|
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, m_arbProgram);
|
|
|
|
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, text.length(), text.constData());
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (glGetError()) {
|
2010-11-10 18:25:40 +00:00
|
|
|
const char *error = (const char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
|
|
|
|
kError() << "Failed to compile fragment program:" << error;
|
2011-01-19 16:40:37 +00:00
|
|
|
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
|
|
|
|
glDeleteProgramsARB(1, &m_arbProgram);
|
|
|
|
glDisable(GL_FRAGMENT_PROGRAM_ARB);
|
|
|
|
m_arbProgram = 0;
|
2010-11-10 18:25:40 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
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
|
|
|
kDebug(1212) << "ARB Shader compiled, id: " << m_arbProgram;
|
2010-11-10 18:25:40 +00:00
|
|
|
return true;
|
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-10 18:25:40 +00:00
|
|
|
|
2010-06-02 20:04:54 +00:00
|
|
|
} // namespace
|
|
|
|
|