Commit 0179f741 authored by Alex Nemeth's avatar Alex Nemeth Committed by Eike Hein
Browse files

Updated the blur method to use the more efficient dual kawase blur algorithm.

Summary:
Updated the old and outdated blur method to use the much more efficient dual kawase blur method.
Now with this we can do virtually infinite blur with very very little performance cost.
The dual kawase blur method is basically downscaling and upscaling an image, but combined with the kawase blur shader.
Comparison: https://i.imgur.com/mh6Cw61.png
Left is old, right is new.
Comparison was done with the strongest blur setting in a VM running on an Intel i7-4790 and a GTX980
We can see here that the performance is even better with this new method.

Reviewers: #plasma, #kwin, graesslin, fredrik

Reviewed By: fredrik

Subscribers: hein, dos, luebking, broulik, romangg, zzag, anthonyfieroni, mart, davidedmundson, fredrik, ngraham, plasma-devel, kwin, #kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D9848
parent 87b688da
......@@ -12,7 +12,6 @@ target_link_libraries(kwin_blur_config
KF5::ConfigWidgets
KF5::I18n
KF5::Service
KF5::WindowSystem
)
kcoreaddons_desktop_to_json(kwin_blur_config blur_config.desktop SERVICE_TYPES kcmodule.desktop)
......
This diff is collapsed.
/*
* Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
* Copyright © 2018 Alex Nemeth <alex.nemeth329@gmail.com>
*
* 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
......@@ -26,6 +27,7 @@
#include <QVector>
#include <QVector2D>
#include <QStack>
namespace KWayland
{
......@@ -38,13 +40,14 @@ class BlurManagerInterface;
namespace KWin
{
static const int borderSize = 5;
class BlurShader;
class BlurEffect : public KWin::Effect
{
Q_OBJECT
Q_PROPERTY(int blurRadius READ blurRadius)
Q_PROPERTY(bool cacheTexture READ isCacheTexture)
public:
BlurEffect();
~BlurEffect();
......@@ -58,11 +61,6 @@ public:
void drawWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data);
void paintEffectFrame(EffectFrame *frame, QRegion region, double opacity, double frameOpacity);
// for dynamic setting extraction
int blurRadius() const;
bool isCacheTexture() const {
return m_shouldCache;
}
virtual bool provides(Feature feature);
int requestedEffectChainPosition() const override {
......@@ -76,39 +74,59 @@ public Q_SLOTS:
void slotScreenGeometryChanged();
private:
void updateTexture();
QRect expand(const QRect &rect) const;
QRegion expand(const QRegion &region) const;
bool renderTargetsValid() const;
void deleteFBOs();
void initBlurStrengthValues();
void updateTexture();
QRegion blurRegion(const EffectWindow *w) const;
bool shouldBlur(const EffectWindow *w, int mask, const WindowPaintData &data) const;
void updateBlurRegion(EffectWindow *w) const;
void doSimpleBlur(EffectWindow *w, const float opacity, const QMatrix4x4 &screenProjection);
void doBlur(const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection);
void doCachedBlur(EffectWindow *w, const QRegion& region, const float opacity, const QMatrix4x4 &screenProjection);
void uploadRegion(QVector2D *&map, const QRegion &region);
void uploadGeometry(GLVertexBuffer *vbo, const QRegion &horizontal, const QRegion &vertical);
void doBlur(const QRegion &shape, const QRect &screen, const float opacity, const QMatrix4x4 &screenProjection, bool isDock);
void uploadRegion(QVector2D *&map, const QRegion &region, const int downSampleIterations);
void uploadGeometry(GLVertexBuffer *vbo, const QRegion &blurRegion, const QRegion &windowRegion);
void downSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
void upSampleTexture(GLVertexBuffer *vbo, int blurRectCount);
void copyScreenSampleTexture(GLVertexBuffer *vbo, int blurRectCount, QRegion blurShape, QSize screenSize, QMatrix4x4 screenProjection);
private:
BlurShader *shader;
GLShader *m_simpleShader;
GLRenderTarget *target = nullptr;
GLTexture tex;
GLRenderTarget *m_simpleTarget;
BlurShader *m_shader;
QVector <GLRenderTarget*> m_renderTargets;
QVector <GLTexture> m_renderTextures;
QStack <GLRenderTarget*> m_renderTargetStack;
bool m_renderTargetsValid;
long net_wm_blur_region;
QRegion m_damagedArea; // keeps track of the area which has been damaged (from bottom to top)
QRegion m_paintedArea; // actually painted area which is greater than m_damagedArea
QRegion m_currentBlur; // keeps track of the currently blured area of non-caching windows(from bottom to top)
bool m_shouldCache;
struct BlurWindowInfo {
GLTexture blurredBackground; // keeps the horizontally blurred background
QRegion damagedRegion;
QPoint windowPos;
bool dropCache;
QMetaObject::Connection blurChangedConnection;
QRegion m_currentBlur; // keeps track of the currently blured area of the windows(from bottom to top)
bool m_useSimpleBlur;
int m_downSampleIterations; // number of times the texture will be downsized to half size
int m_offset;
int m_expandSize;
struct OffsetStruct {
float minOffset;
float maxOffset;
int expandSize;
};
QVector <OffsetStruct> blurOffsets;
struct BlurValuesStruct {
int iteration;
float offset;
};
QHash< const EffectWindow*, BlurWindowInfo > windows;
typedef QHash<const EffectWindow*, BlurWindowInfo>::iterator CacheEntry;
QVector <BlurValuesStruct> blurStrengthValues;
QMap <EffectWindow*, QMetaObject::Connection> windowBlurChangedConnections;
KWayland::Server::BlurManagerInterface *m_blurManager = nullptr;
};
......
......@@ -5,11 +5,11 @@
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile arg="true"/>
<group name="Effect-Blur">
<entry name="BlurRadius" type="Int">
<default>12</default>
<entry name="BlurStrength" type="Int">
<default>5</default>
</entry>
<entry name="CacheTexture" type="Bool">
<default>true</default>
<entry name="UseSimpleBlur" type="Bool">
<default>false</default>
</entry>
</group>
</kcfg>
......@@ -25,7 +25,6 @@
#include <kwineffects_interface.h>
#include <KAboutData>
#include <KPluginFactory>
#include <KWindowSystem>
K_PLUGIN_FACTORY_WITH_JSON(BlurEffectConfigFactory,
"blur_config.json",
......@@ -38,9 +37,6 @@ BlurEffectConfig::BlurEffectConfig(QWidget *parent, const QVariantList &args)
: KCModule(KAboutData::pluginData(QStringLiteral("blur")), parent, args)
{
ui.setupUi(this);
if (KWindowSystem::isPlatformWayland()) {
ui.kcfg_CacheTexture->setVisible(false);
}
BlurConfig::instance(KWIN_CONFIG);
addConfig(BlurConfig::self(), this);
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -6,13 +6,13 @@
<rect>
<x>0</x>
<y>0</y>
<width>396</width>
<height>103</height>
<width>480</width>
<height>95</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<widget class="QLabel" name="labelConstantExplanation">
<property name="text">
<string>Strength of the effect:</string>
</property>
......@@ -37,28 +37,28 @@
</spacer>
</item>
<item>
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="labelConstantLight">
<property name="text">
<string>Light</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="kcfg_BlurRadius">
<widget class="QSlider" name="kcfg_BlurStrength">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>14</number>
<number>15</number>
</property>
<property name="singleStep">
<number>2</number>
<number>1</number>
</property>
<property name="pageStep">
<number>2</number>
<number>1</number>
</property>
<property name="value">
<number>12</number>
<number>5</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
......@@ -69,7 +69,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<widget class="QLabel" name="labelConstantStrong">
<property name="text">
<string>Strong</string>
</property>
......@@ -78,12 +78,12 @@
</layout>
</item>
<item>
<widget class="QCheckBox" name="kcfg_CacheTexture">
<property name="toolTip">
<string extracomment="Enables an internal texture cache which saves the background of the blurred area. This results in less repaints and in a slightly higher memory usage."/>
</property>
<widget class="QCheckBox" name="kcfg_UseSimpleBlur">
<property name="text">
<string>Save intermediate rendering results.</string>
<string>Use simple fullscreen blur</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
......
File mode changed from 100644 to 100755
/*
* Copyright © 2010 Fredrik Höglund <fredrik@kde.org>
* Copyright © 2018 Alex Nemeth <alex.nemeth329@gmail.com>
*
* 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
......@@ -20,6 +21,7 @@
#include "blurshader.h"
#include <kwineffects.h>
#include "kwinglutils.h"
#include <kwinglplatform.h>
#include <QByteArray>
......@@ -33,7 +35,7 @@ using namespace KWin;
BlurShader::BlurShader()
: mRadius(0), mValid(false)
: mValid(false)
{
}
......@@ -46,124 +48,163 @@ BlurShader *BlurShader::create()
return new GLSLBlurShader();
}
void BlurShader::setRadius(int radius)
{
const int r = qMax(radius, 2);
// ----------------------------------------------------------------------------
if (mRadius != r) {
mRadius = r;
reset();
init();
}
}
void BlurShader::setDirection(Qt::Orientation direction)
GLSLBlurShader::GLSLBlurShader()
{
mDirection = direction;
init();
}
float BlurShader::gaussian(float x, float sigma) const
GLSLBlurShader::~GLSLBlurShader()
{
return (1.0 / std::sqrt(2.0 * M_PI) * sigma)
* std::exp(-((x * x) / (2.0 * sigma * sigma)));
reset();
}
QList<KernelValue> BlurShader::gaussianKernel() const
void GLSLBlurShader::reset()
{
int size = qMin(mRadius | 1, maxKernelSize());
if (!(size & 0x1))
size -= 1;
delete m_shaderDownsample;
m_shaderDownsample = nullptr;
QList<KernelValue> kernel;
const int center = size / 2;
const qreal sigma = (size - 1) / 2.5;
delete m_shaderUpsample;
m_shaderUpsample = nullptr;
kernel << KernelValue(0.0, gaussian(0.0, sigma));
float total = kernel[0].g;
delete m_shaderCopysample;
m_shaderCopysample = nullptr;
setIsValid(false);
}
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);
void GLSLBlurShader::setModelViewProjectionMatrix(const QMatrix4x4 &matrix)
{
if (!isValid())
return;
// Offset taking the contribution of both pixels into account
const float offset = .5 - g1 / (g1 + g2);
switch (m_activeSampleType) {
case CopySampleType:
if (matrix == m_matrixCopysample)
return;
kernel << KernelValue(fx + offset, g1 + g2);
kernel << KernelValue(-(fx + offset), g1 + g2);
m_matrixCopysample = matrix;
m_shaderCopysample->setUniform(m_mvpMatrixLocationCopysample, matrix);
break;
total += (g1 + g2) * 2;
}
case UpSampleType:
if (matrix == m_matrixUpsample)
return;
qSort(kernel);
m_matrixUpsample = matrix;
m_shaderUpsample->setUniform(m_mvpMatrixLocationUpsample, matrix);
break;
// Normalize the kernel
for (int i = 0; i < kernel.count(); i++)
kernel[i].g /= total;
case DownSampleType:
if (matrix == m_matrixDownsample)
return;
return kernel;
m_matrixDownsample = matrix;
m_shaderDownsample->setUniform(m_mvpMatrixLocationDownsample, matrix);
break;
}
}
void GLSLBlurShader::setOffset(float offset)
{
if (!isValid())
return;
switch (m_activeSampleType) {
case UpSampleType:
if (offset == m_offsetUpsample)
return;
// ----------------------------------------------------------------------------
m_offsetUpsample = offset;
m_shaderUpsample->setUniform(m_offsetLocationUpsample, offset);
break;
case DownSampleType:
if (offset == m_offsetDownsample)
return;
GLSLBlurShader::GLSLBlurShader()
: BlurShader(), shader(NULL)
{
m_offsetDownsample = offset;
m_shaderDownsample->setUniform(m_offsetLocationDownsample, offset);
break;
}
}
GLSLBlurShader::~GLSLBlurShader()
void GLSLBlurShader::setTargetSize(QSize renderTextureSize)
{
reset();
}
if (!isValid())
return;
void GLSLBlurShader::reset()
{
delete shader;
shader = NULL;
QVector2D texSize = QVector2D(renderTextureSize.width(), renderTextureSize.height());
setIsValid(false);
}
switch (m_activeSampleType) {
case CopySampleType:
if (renderTextureSize == m_renderTextureSizeCopysample)
return;
void GLSLBlurShader::setPixelDistance(float val)
{
if (!isValid())
return;
m_renderTextureSizeCopysample = renderTextureSize;
m_shaderCopysample->setUniform(m_renderTextureSizeLocationCopysample, texSize);
break;
QVector2D pixelSize(0.0, 0.0);
if (direction() == Qt::Horizontal)
pixelSize.setX(val);
else
pixelSize.setY(val);
case UpSampleType:
if (renderTextureSize == m_renderTextureSizeUpsample)
return;
shader->setUniform(pixelSizeLocation, pixelSize);
}
m_renderTextureSizeUpsample = renderTextureSize;
m_shaderUpsample->setUniform(m_renderTextureSizeLocationUpsample, texSize);
m_shaderUpsample->setUniform(m_halfpixelLocationUpsample, QVector2D(0.5 / texSize.x(), 0.5 / texSize.y()));
break;
void GLSLBlurShader::setTextureMatrix(const QMatrix4x4 &matrix)
{
if (!isValid())
return;
case DownSampleType:
if (renderTextureSize == m_renderTextureSizeDownsample)
return;
shader->setUniform(textureMatrixLocation, matrix);
m_renderTextureSizeDownsample = renderTextureSize;
m_shaderDownsample->setUniform(m_renderTextureSizeLocationDownsample, texSize);
m_shaderDownsample->setUniform(m_halfpixelLocationDownsample, QVector2D(0.5 / texSize.x(), 0.5 / texSize.y()));
break;
}
}
void GLSLBlurShader::setModelViewProjectionMatrix(const QMatrix4x4 &matrix)
void GLSLBlurShader::setBlurRect(QRect blurRect, QSize screenSize)
{
if (!isValid())
if (!isValid() || blurRect == m_blurRectCopysample)
return;
shader->setUniform(mvpMatrixLocation, matrix);
m_blurRectCopysample = blurRect;
QVector4D rect = QVector4D(
blurRect.bottomLeft().x() / float(screenSize.width()),
1.0 - blurRect.bottomLeft().y() / float(screenSize.height()),
blurRect.topRight().x() / float(screenSize.width()),
1.0 - blurRect.topRight().y() / float(screenSize.height())
);
m_shaderCopysample->setUniform(m_blurRectLocationCopysample, rect);
}
void GLSLBlurShader::bind()
void GLSLBlurShader::bind(SampleType sampleType)
{
if (!isValid())
return;
ShaderManager::instance()->pushShader(shader);
switch (sampleType) {
case CopySampleType:
ShaderManager::instance()->pushShader(m_shaderCopysample);
break;
case UpSampleType:
ShaderManager::instance()->pushShader(m_shaderUpsample);
break;
case DownSampleType:
ShaderManager::instance()->pushShader(m_shaderDownsample);
break;
}
m_activeSampleType = sampleType;
}
void GLSLBlurShader::unbind()
......@@ -171,136 +212,173 @@ void GLSLBlurShader::unbind()
ShaderManager::instance()->popShader();
}
int GLSLBlurShader::maxKernelSize() const
{
if (GLPlatform::instance()->isGLES()) {
// GL_MAX_VARYING_FLOATS not available in GLES
// querying for GL_MAX_VARYING_VECTORS crashes on nouveau
// using the minimum value of 8
return 8 * 2;
} else {
int value;
glGetIntegerv(GL_MAX_VARYING_FLOATS, &value);
// Maximum number of vec4 varyings * 2
// The code generator will pack two vec2's into each vec4.
return value / 2;
}
}
void GLSLBlurShader::init()
{
QList<KernelValue> kernel = gaussianKernel();
const int size = kernel.size();
const int center = size / 2;
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;
}
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));
QByteArray vertexSource;
QByteArray fragmentSource;
QByteArray fragmentDownSource;
QByteArray fragmentUpSource;
QByteArray fragmentCopySource;
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";
const QByteArray attribute = core ? "in" : "attribute";
const QByteArray texture2D = core ? "texture" : "texture2D";
const QByteArray fragColor = core ? "fragColor" : "gl_FragColor";
// Vertex shader
// ===================================================================
QTextStream stream(&vertexSource);
QString glHeaderString;
if (gles) {
if (core) {
stream << "#version 300 es\n\n";
glHeaderString += "#version 300 es\n\n";
}
stream << "precision highp float;\n";
glHeaderString += "precision highp float;\n";
} else if (glsl_140) {
stream << "#version 140\n\n";
glHeaderString += "#version 140\n\n";
}
stream << "uniform mat4 modelViewProjectionMatrix;\n";
stream << "uniform mat4 textureMatrix;\n";
stream << "uniform vec2 pixelSize;\n\n";
stream << attribute << " vec4 vertex;\n\n";
stream << varying_out << " vec4 samplePos[" << std::ceil(size / 2.0) << "];\n";
stream << "\n";
stream << "void main(void)\n";
stream << "{\n";
stream << " vec4 center = vec4(textureMatrix * vertex).stst;\n";
stream << " vec4 ps = pixelSize.stst;\n\n";
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";
QString glUniformString = "uniform sampler2D texUnit;\n"
"uniform float offset;\n"
"uniform vec2 renderTextureSize;\n"
"uniform vec2 halfpixel;\n";
if (core) {
glUniformString += "out vec4 fragColor;\n\n";
}
stream << "\n";
<