Skip to content

Commit

Permalink
Merge pull request #168 from taj-ny/blur-texture-size
Browse files Browse the repository at this point in the history
blur: calculate texture sizes properly when the display is scaled
  • Loading branch information
taj-ny authored Jan 16, 2025
2 parents 4bfabf8 + 5fa19c7 commit 668452e
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,13 +839,13 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg

if (!staticBlurTexture
&& (renderInfo.framebuffers.size() != (m_iterationCount + 1)
|| renderInfo.textures[0]->size() != backgroundRect.size()
|| renderInfo.textures[0]->size() != deviceBackgroundRect.size()
|| renderInfo.textures[0]->internalFormat() != textureFormat)) {
renderInfo.framebuffers.clear();
renderInfo.textures.clear();

for (size_t i = 0; i <= m_iterationCount; ++i) {
auto texture = GLTexture::allocate(textureFormat, backgroundRect.size() / (1 << i));
auto texture = GLTexture::allocate(textureFormat, deviceBackgroundRect.size() / (1 << i));
if (!texture) {
qCWarning(KWIN_BLUR) << "Failed to allocate an offscreen texture";
return;
Expand All @@ -866,8 +866,9 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg
// Fetch the pixels behind the shape that is going to be blurred.
if (!staticBlurTexture) {
const QRegion dirtyRegion = region & backgroundRect;
for (const QRect &dirtyRect: dirtyRegion) {
renderInfo.framebuffers[0]->blitFromRenderTarget(renderTarget, viewport, dirtyRect, dirtyRect.translated(-backgroundRect.topLeft()));
for (const QRect &dirtyRect : dirtyRegion) {
const auto destination = snapToPixelGrid(scaledRect(dirtyRect, viewport.scale())).translated(-deviceBackgroundRect.topLeft());
renderInfo.framebuffers[0]->blitFromRenderTarget(renderTarget, viewport, dirtyRect, destination);
}
}

Expand All @@ -885,17 +886,17 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg

// The geometry that will be blurred offscreen, in logical pixels.
{
const QRectF localRect = QRectF(0, 0, backgroundRect.width(), backgroundRect.height());
const QRectF localRect = QRectF(0, 0, deviceBackgroundRect.width(), deviceBackgroundRect.height());

const float x0 = localRect.left();
const float y0 = localRect.top();
const float x1 = localRect.right();
const float y1 = localRect.bottom();

const float u0 = x0 / backgroundRect.width();
const float v0 = 1.0f - y0 / backgroundRect.height();
const float u1 = x1 / backgroundRect.width();
const float v1 = 1.0f - y1 / backgroundRect.height();
const float u0 = x0 / deviceBackgroundRect.width();
const float v0 = 1.0f - y0 / deviceBackgroundRect.height();
const float u1 = x1 / deviceBackgroundRect.width();
const float v1 = 1.0f - y1 / deviceBackgroundRect.height();

// first triangle
map[vboIndex++] = GLVertex2D{
Expand Down Expand Up @@ -1012,7 +1013,7 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg
ShaderManager::instance()->pushShader(m_downsamplePass.shader.get());

QMatrix4x4 projectionMatrix;
projectionMatrix.ortho(QRectF(0.0, 0.0, backgroundRect.width(), backgroundRect.height()));
projectionMatrix.ortho(QRectF(0.0, 0.0, deviceBackgroundRect.width(), deviceBackgroundRect.height()));

m_downsamplePass.shader->setUniform(m_downsamplePass.mvpMatrixLocation, projectionMatrix);
m_downsamplePass.shader->setUniform(m_downsamplePass.offsetLocation, float(m_offset));
Expand Down Expand Up @@ -1044,7 +1045,7 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg
ShaderManager::instance()->pushShader(m_upsamplePass.shader.get());

QMatrix4x4 projectionMatrix;
projectionMatrix.ortho(QRectF(0.0, 0.0, backgroundRect.width(), backgroundRect.height()));
projectionMatrix.ortho(QRectF(0.0, 0.0, deviceBackgroundRect.width(), deviceBackgroundRect.height()));

m_upsamplePass.shader->setUniform(m_upsamplePass.topCornerRadiusLocation, static_cast<float>(0));
m_upsamplePass.shader->setUniform(m_upsamplePass.bottomCornerRadiusLocation, static_cast<float>(0));
Expand Down Expand Up @@ -1087,7 +1088,7 @@ void BlurEffect::blur(BlurRenderData &renderInfo, const RenderTarget &renderTarg
m_upsamplePass.shader->setUniform(m_upsamplePass.topCornerRadiusLocation, topCornerRadius);
m_upsamplePass.shader->setUniform(m_upsamplePass.bottomCornerRadiusLocation, bottomCornerRadius);
m_upsamplePass.shader->setUniform(m_upsamplePass.antialiasingLocation, m_settings.roundedCorners.antialiasing);
m_upsamplePass.shader->setUniform(m_upsamplePass.blurSizeLocation, QVector2D(backgroundRect.width(), backgroundRect.height()));
m_upsamplePass.shader->setUniform(m_upsamplePass.blurSizeLocation, QVector2D(deviceBackgroundRect.width(), deviceBackgroundRect.height()));
m_upsamplePass.shader->setUniform(m_upsamplePass.opacityLocation, static_cast<float>(opacity));

projectionMatrix = viewport.projectionMatrix();
Expand Down

0 comments on commit 668452e

Please sign in to comment.