/**************************************************************************** ** ** Copyright (C) 2016 Jolla Ltd, author: ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Graphical Effects module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include "qgfxshaderbuilder_p.h" #include #include #include #include #include #include #ifndef GL_MAX_VARYING_COMPONENTS #define GL_MAX_VARYING_COMPONENTS 0x8B4B #endif #ifndef GL_MAX_VARYING_FLOATS #define GL_MAX_VARYING_FLOATS 0x8B4B #endif #ifndef GL_MAX_VARYING_VECTORS #define GL_MAX_VARYING_VECTORS 0x8DFC #endif QGfxShaderBuilder::QGfxShaderBuilder() : m_coreProfile(false) { // The following code makes the assumption that an OpenGL context the GUI // thread will get the same capabilities as the render thread's OpenGL // context. Not 100% accurate, but it works... QOpenGLContext context; if (!context.create()) { qDebug() << "failed to acquire GL context to resolve capabilities, using defaults.."; m_maxBlurSamples = 8; // minimum number of varyings in the ES 2.0 spec. return; } QOffscreenSurface surface; // In very odd cases, we can get incompatible configs here unless we pass the // GL context's format on to the offscreen format. surface.setFormat(context.format()); surface.create(); QOpenGLContext *oldContext = QOpenGLContext::currentContext(); QSurface *oldSurface = oldContext ? oldContext->surface() : 0; if (context.makeCurrent(&surface)) { QOpenGLFunctions *gl = context.functions(); if (context.isOpenGLES()) { gl->glGetIntegerv(GL_MAX_VARYING_VECTORS, &m_maxBlurSamples); } else if (context.format().majorVersion() >= 3) { int components; gl->glGetIntegerv(GL_MAX_VARYING_COMPONENTS, &components); m_maxBlurSamples = components / 2.0; m_coreProfile = context.format().profile() == QSurfaceFormat::CoreProfile; } else { int floats; gl->glGetIntegerv(GL_MAX_VARYING_FLOATS, &floats); m_maxBlurSamples = floats / 2.0; } if (oldContext && oldSurface) oldContext->makeCurrent(oldSurface); else context.doneCurrent(); } else { qDebug() << "failed to acquire GL context to resolve capabilities, using defaults.."; m_maxBlurSamples = 8; // minimum number of varyings in the ES 2.0 spec. } } /* The algorithm works like this.. For every two pixels we want to sample we take one sample between those two pixels and rely on linear interpoliation to get both values at the cost of one texture sample. The sample point is calculated based on the gaussian weights at the two texels. I've included the table here for future reference: Requested Effective Actual Actual Samples Radius/Kernel Samples Radius(*) ------------------------------------------------- 0 0 / 1x1 1 0 1 0 / 1x1 1 0 2 1 / 3x3 2 0 3 1 / 3x3 2 0 4 2 / 5x5 3 1 5 2 / 5x5 3 1 6 3 / 7x7 4 1 7 3 / 7x7 4 1 8 4 / 9x9 5 2 9 4 / 9x9 5 2 10 5 / 11x11 6 2 11 5 / 11x11 6 2 12 6 / 13x13 7 3 13 6 / 13x13 7 3 ... ... ... ... When ActualSamples is an 'odd' nunber, sample center pixel separately: EffectiveRadius: 4 EffectiveKernel: 9x9 ActualSamples: 5 -4 -3 -2 -1 0 +1 +2 +3 +4 | | | | | | | | | | \ / \ / | \ / \ / tL2 tL1 tC tR1 tR2 When ActualSamples is an 'even' number, sample 3 center pixels with two samples: EffectiveRadius: 3 EffectiveKernel: 7x7 ActualSamples: 4 -3 -2 -1 0 +1 +2 +3 | | | | | | | | \ / \ / | \ / tL1 tL0 tR0 tR2 From this table we have the following formulas: EffectiveRadius = RequestedSamples / 2; EffectiveKernel = EffectiveRadius * 2 + 1 ActualSamples = 1 + RequstedSamples / 2; ActualRadius = RequestedSamples / 4; (*) ActualRadius excludes the pixel pair sampled in the center for even 'actual sample' counts */ static qreal qgfx_gaussian(qreal x, qreal d) { return qExp(- x * x / (2 * d * d)); } struct QGfxGaussSample { QByteArray name; qreal pos; qreal weight; inline void set(const QByteArray &n, qreal p, qreal w) { name = n; pos = p; weight = w; } }; static void qgfx_declareBlurVaryings(QByteArray &shader, QGfxGaussSample *s, int samples) { for (int i=0; i m_maxBlurSamples || masked || fallback) { if (m_coreProfile) { result[QStringLiteral("fragmentShader")] = qgfx_fallbackCoreFragmentShader(requestedRadius, deviation, masked, alphaOnly); result[QStringLiteral("vertexShader")] = qgfx_fallbackCoreVertexShader(); } else { result[QStringLiteral("fragmentShader")] = qgfx_fallbackFragmentShader(requestedRadius, deviation, masked, alphaOnly); result[QStringLiteral("vertexShader")] = qgfx_fallbackVertexShader(); } return result; } QVarLengthArray p(samples); qgfx_buildGaussSamplePoints(p.data(), samples, radius, deviation); if (m_coreProfile) { result[QStringLiteral("fragmentShader")] = qgfx_gaussianFragmentCoreShader(p.data(), samples, alphaOnly); result[QStringLiteral("vertexShader")] = qgfx_gaussianVertexCoreShader(p.data(), samples); } else { result[QStringLiteral("fragmentShader")] = qgfx_gaussianFragmentShader(p.data(), samples, alphaOnly); result[QStringLiteral("vertexShader")] = qgfx_gaussianVertexShader(p.data(), samples); } return result; }