| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "glwindow.h" |
| 5 |
#include <QImage> |
| 6 |
#include <QOpenGLShaderProgram> |
| 7 |
#include <QOpenGLContext> |
| 8 |
#include <QOpenGLFunctions> |
| 9 |
#include <QOpenGLExtraFunctions> |
| 10 |
#include <QOpenGLVertexArrayObject> |
| 11 |
#include <QtGui/qopengl.h> |
| 12 |
#include <QDebug> |
| 13 |
#include <QTimer> |
| 14 |
#include <math.h> |
| 15 |
|
| 16 |
#ifndef GL_READ_WRITE |
| 17 |
#define GL_READ_WRITE 0x88BA |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifndef GL_RGBA8 |
| 21 |
#define GL_RGBA8 0x8058 |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifndef GL_SHADER_IMAGE_ACCESS_BARRIER_BIT |
| 25 |
#define GL_SHADER_IMAGE_ACCESS_BARRIER_BIT 0x00000020 |
| 26 |
#endif |
| 27 |
|
| 28 |
GLWindow::GLWindow() |
| 29 |
{ |
| 30 |
const float animationStart = 0.0; |
| 31 |
const float animationEnd = 10.0; |
| 32 |
const float animationLength = 1000; |
| 33 |
|
| 34 |
m_animationGroup = new QSequentialAnimationGroup(this); |
| 35 |
m_animationGroup->setLoopCount(-1); |
| 36 |
|
| 37 |
m_animationForward = new QPropertyAnimation(this, QByteArrayLiteral("blurRadius")); |
| 38 |
m_animationForward->setStartValue(animationStart); |
| 39 |
m_animationForward->setEndValue(animationEnd); |
| 40 |
m_animationForward->setDuration(animationLength); |
| 41 |
m_animationGroup->addAnimation(m_animationForward); |
| 42 |
|
| 43 |
m_animationBackward = new QPropertyAnimation(this, QByteArrayLiteral("blurRadius")); |
| 44 |
m_animationBackward->setStartValue(animationEnd); |
| 45 |
m_animationBackward->setEndValue(animationStart); |
| 46 |
m_animationBackward->setDuration(animationLength); |
| 47 |
m_animationGroup->addAnimation(m_animationBackward); |
| 48 |
|
| 49 |
m_animationGroup->start(); |
| 50 |
} |
| 51 |
|
| 52 |
GLWindow::~GLWindow() |
| 53 |
{ |
| 54 |
makeCurrent(); |
| 55 |
delete m_texImageInput; |
| 56 |
delete m_texImageProcessed; |
| 57 |
delete m_texImageTmp; |
| 58 |
delete m_shaderDisplay; |
| 59 |
delete m_shaderComputeH; |
| 60 |
delete m_shaderComputeV; |
| 61 |
delete m_animationGroup; |
| 62 |
delete m_animationForward; |
| 63 |
delete m_animationBackward; |
| 64 |
delete m_vao; |
| 65 |
} |
| 66 |
|
| 67 |
void GLWindow::setBlurRadius(float blurRadius) |
| 68 |
{ |
| 69 |
int radius = int(blurRadius); |
| 70 |
if (radius != m_blurRadius) { |
| 71 |
m_blurRadius = radius; |
| 72 |
update(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void GLWindow::setAnimating(bool animate) |
| 77 |
{ |
| 78 |
m_animate = animate; |
| 79 |
if (animate) |
| 80 |
m_animationGroup->start(); |
| 81 |
else |
| 82 |
m_animationGroup->stop(); |
| 83 |
} |
| 84 |
|
| 85 |
void GLWindow::keyPressEvent(QKeyEvent *e) |
| 86 |
{ |
| 87 |
if (e->key() == Qt::Key_Space) { // pause |
| 88 |
setAnimating(!m_animate); |
| 89 |
} |
| 90 |
update(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
static const char *vsDisplaySource = |
| 97 |
"const vec4 vertices[4] = vec4[4] (\n" |
| 98 |
" vec4( -1.0, 1.0, 0.0, 1.0),\n" |
| 99 |
" vec4( -1.0, -1.0, 0.0, 1.0),\n" |
| 100 |
" vec4( 1.0, 1.0, 0.0, 1.0),\n" |
| 101 |
" vec4( 1.0, -1.0, 0.0, 1.0)\n" |
| 102 |
");\n" |
| 103 |
"const vec2 texCoords[4] = vec2[4] (\n" |
| 104 |
" vec2( 0.0, 1.0),\n" |
| 105 |
" vec2( 0.0, 0.0),\n" |
| 106 |
" vec2( 1.0, 1.0),\n" |
| 107 |
" vec2( 1.0, 0.0)\n" |
| 108 |
");\n" |
| 109 |
"out vec2 texCoord;\n" |
| 110 |
"uniform mat4 matProjection;\n" |
| 111 |
"uniform vec2 imageRatio;\n" |
| 112 |
"void main() {\n" |
| 113 |
" gl_Position = matProjection * ( vertices[gl_VertexID] * vec4(imageRatio,0,1) );\n" |
| 114 |
" texCoord = texCoords[gl_VertexID];\n" |
| 115 |
"}\n"; |
| 116 |
|
| 117 |
static const char *fsDisplaySource = |
| 118 |
"in lowp vec2 texCoord; \n" |
| 119 |
"uniform sampler2D samImage; \n" |
| 120 |
"layout(location = 0) out lowp vec4 color;\n" |
| 121 |
"void main() {\n" |
| 122 |
" lowp vec4 texColor = texture(samImage,texCoord);\n" |
| 123 |
" color = vec4(texColor.rgb, 1.0);\n" |
| 124 |
"}\n"; |
| 125 |
|
| 126 |
static const char *csComputeSourceV = |
| 127 |
"#define COMPUTEPATCHSIZE 10 // Setting this to 10 to comply with MAX_COMPUTE_WORK_GROUP_INVOCATIONS for both OpenGL and OpenGLES - see QTBUG-79374 \n" |
| 128 |
"#define IMGFMT rgba8 \n" |
| 129 |
"layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n" |
| 130 |
"layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n" |
| 131 |
"layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n" |
| 132 |
"uniform int radius;\n" |
| 133 |
"const float cutoff = 2.2;\n" |
| 134 |
|
| 135 |
"float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n" |
| 136 |
" float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n" |
| 137 |
" return 1.0 / (2.0 * sigma * sigma);\n" |
| 138 |
"}\n" |
| 139 |
|
| 140 |
"float gaussian(float distance, float expfactor) {\n" |
| 141 |
" return exp( -(distance * distance) * expfactor);\n" |
| 142 |
"}\n" |
| 143 |
|
| 144 |
"void main() {\n" |
| 145 |
" ivec2 imgSize = imageSize(resultImage);\n" |
| 146 |
" int x = int(gl_GlobalInvocationID.x);\n" |
| 147 |
" int y = int(gl_GlobalInvocationID.y);\n" |
| 148 |
" if ( (x >= imgSize.x) || (y >= imgSize.y) ) return;\n" |
| 149 |
" vec4 sumPixels = vec4(0.0);\n" |
| 150 |
" float sumWeights = 0.0;\n" |
| 151 |
" int left = clamp(x - radius, 0, imgSize.x - 1);\n" |
| 152 |
" int right = clamp(x + radius, 0, imgSize.x - 1);\n" |
| 153 |
" int top = clamp(y - radius, 0, imgSize.y - 1);\n" |
| 154 |
" int bottom = clamp(y + radius, 0, imgSize.y - 1);\n" |
| 155 |
" float expfactor = expFactor();\n" |
| 156 |
" for (int iY = top; iY <= bottom; iY++) {\n" |
| 157 |
" float dy = float(abs(iY - y));\n" |
| 158 |
" vec4 imgValue = imageLoad(inputImage, ivec2(x,iY));\n" |
| 159 |
" float weight = gaussian(dy, expfactor);\n" |
| 160 |
" sumWeights += weight;\n" |
| 161 |
" sumPixels += (imgValue * weight);\n" |
| 162 |
" }\n" |
| 163 |
" sumPixels /= sumWeights;\n" |
| 164 |
" imageStore(resultImage, ivec2(x,y), sumPixels);\n" |
| 165 |
"}\n"; |
| 166 |
|
| 167 |
static const char *csComputeSourceH = |
| 168 |
"#define COMPUTEPATCHSIZE 10 \n" |
| 169 |
"#define IMGFMT rgba8 \n" |
| 170 |
"layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n" |
| 171 |
"layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n" |
| 172 |
"layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n" |
| 173 |
"uniform int radius;\n" |
| 174 |
"const float cutoff = 2.2;\n" |
| 175 |
|
| 176 |
"float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n" |
| 177 |
" float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n" |
| 178 |
" return 1.0 / (2.0 * sigma * sigma);\n" |
| 179 |
"}\n" |
| 180 |
|
| 181 |
"float gaussian(float distance, float expfactor) {\n" |
| 182 |
" return exp( -(distance * distance) * expfactor);\n" |
| 183 |
"}\n" |
| 184 |
|
| 185 |
"void main() {\n" |
| 186 |
" ivec2 imgSize = imageSize(resultImage);\n" |
| 187 |
" int x = int(gl_GlobalInvocationID.x);\n" |
| 188 |
" int y = int(gl_GlobalInvocationID.y);\n" |
| 189 |
" if ( (x >= imgSize.x) || (y >= imgSize.y) ) return;\n" |
| 190 |
" vec4 sumPixels = vec4(0.0);\n" |
| 191 |
" float sumWeights = 0.0;\n" |
| 192 |
" int left = clamp(x - radius, 0, imgSize.x - 1);\n" |
| 193 |
" int right = clamp(x + radius, 0, imgSize.x - 1);\n" |
| 194 |
" int top = clamp(y - radius, 0, imgSize.y - 1);\n" |
| 195 |
" int bottom = clamp(y + radius, 0, imgSize.y - 1);\n" |
| 196 |
" float expfactor = expFactor();\n" |
| 197 |
" for (int iX = left; iX <= right; iX++) {\n" |
| 198 |
" float dx = float(abs(iX - x));\n" |
| 199 |
" vec4 imgValue = imageLoad(inputImage, ivec2(iX,y));\n" |
| 200 |
" float weight = gaussian(dx, expfactor);\n" |
| 201 |
" sumWeights += weight;\n" |
| 202 |
" sumPixels += (imgValue * weight);\n" |
| 203 |
" }\n" |
| 204 |
" sumPixels /= sumWeights;\n" |
| 205 |
" imageStore(resultImage, ivec2(x,y), sumPixels);\n" |
| 206 |
"}\n"; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
QByteArray versionedShaderCode(const char *src) |
| 211 |
{ |
| 212 |
QByteArray versionedSrc; |
| 213 |
|
| 214 |
if (QOpenGLContext::currentContext()->isOpenGLES()) |
| 215 |
versionedSrc.append(QByteArrayLiteral("#version 310 es\n")); |
| 216 |
else |
| 217 |
versionedSrc.append(QByteArrayLiteral("#version 430 core\n")); |
| 218 |
|
| 219 |
versionedSrc.append(src); |
| 220 |
return versionedSrc; |
| 221 |
} |
| 222 |
|
| 223 |
void computeProjection(int winWidth, int winHeight, int imgWidth, int imgHeight, QMatrix4x4 &outProjection, QSizeF &outQuadSize) |
| 224 |
{ |
| 225 |
float ratioImg = float(imgWidth) / float(imgHeight); |
| 226 |
float ratioCanvas = float(winWidth) / float(winHeight); |
| 227 |
|
| 228 |
float correction = ratioImg / ratioCanvas; |
| 229 |
float rescaleFactor = 1.0f; |
| 230 |
float quadWidth = 1.0f; |
| 231 |
float quadHeight = 1.0f; |
| 232 |
|
| 233 |
if (correction < 1.0f) // canvas larger than image -- height = 1.0, vertical black bands |
| 234 |
{ |
| 235 |
quadHeight = 1.0f; |
| 236 |
quadWidth = 1.0f * ratioImg; |
| 237 |
rescaleFactor = ratioCanvas; |
| 238 |
correction = 1.0f / rescaleFactor; |
| 239 |
} |
| 240 |
else // image larger than canvas -- width = 1.0, horizontal black bands |
| 241 |
{ |
| 242 |
quadWidth = 1.0f; |
| 243 |
quadHeight = 1.0f / ratioImg; |
| 244 |
correction = 1.0f / ratioCanvas; |
| 245 |
} |
| 246 |
|
| 247 |
const float frustumWidth = 1.0f * rescaleFactor; |
| 248 |
const float frustumHeight = 1.0f * rescaleFactor * correction; |
| 249 |
|
| 250 |
outProjection = QMatrix4x4(); |
| 251 |
outProjection.ortho( |
| 252 |
-frustumWidth, |
| 253 |
frustumWidth, |
| 254 |
-frustumHeight, |
| 255 |
frustumHeight, |
| 256 |
-1.0f, |
| 257 |
1.0f); |
| 258 |
outQuadSize = QSizeF(quadWidth,quadHeight); |
| 259 |
} |
| 260 |
|
| 261 |
void GLWindow::initializeGL() |
| 262 |
{ |
| 263 |
QOpenGLContext *ctx = QOpenGLContext::currentContext(); |
| 264 |
qDebug() << "Got a " |
| 265 |
<< ctx->format().majorVersion() |
| 266 |
<< "." |
| 267 |
<< ctx->format().minorVersion() |
| 268 |
<< ((ctx->format().renderableType() == QSurfaceFormat::OpenGLES) ? (" GLES") : (" GL")) |
| 269 |
<< " context"; |
| 270 |
|
| 271 |
QImage img(":/Qt-logo-medium.png"); |
| 272 |
Q_ASSERT(!img.isNull()); |
| 273 |
delete m_texImageInput; |
| 274 |
m_texImageInput = new QOpenGLTexture(img.convertToFormat(QImage::Format_RGBA8888).flipped()); |
| 275 |
|
| 276 |
delete m_texImageTmp; |
| 277 |
m_texImageTmp = new QOpenGLTexture(QOpenGLTexture::Target2D); |
| 278 |
m_texImageTmp->setFormat(m_texImageInput->format()); |
| 279 |
m_texImageTmp->setSize(m_texImageInput->width(),m_texImageInput->height()); |
| 280 |
m_texImageTmp->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); // WTF? |
| 281 |
|
| 282 |
delete m_texImageProcessed; |
| 283 |
m_texImageProcessed = new QOpenGLTexture(QOpenGLTexture::Target2D); |
| 284 |
m_texImageProcessed->setFormat(m_texImageInput->format()); |
| 285 |
m_texImageProcessed->setSize(m_texImageInput->width(),m_texImageInput->height()); |
| 286 |
m_texImageProcessed->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); |
| 287 |
|
| 288 |
m_texImageProcessed->setMagnificationFilter(QOpenGLTexture::Linear); |
| 289 |
m_texImageProcessed->setMinificationFilter(QOpenGLTexture::Linear); |
| 290 |
m_texImageProcessed->setWrapMode(QOpenGLTexture::ClampToEdge); |
| 291 |
|
| 292 |
delete m_shaderDisplay; |
| 293 |
m_shaderDisplay = new QOpenGLShaderProgram; |
| 294 |
// Prepend the correct version directive to the sources. The rest is the |
| 295 |
// same, thanks to the common GLSL syntax. |
| 296 |
m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Vertex, versionedShaderCode(vsDisplaySource)); |
| 297 |
m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Fragment, versionedShaderCode(fsDisplaySource)); |
| 298 |
m_shaderDisplay->link(); |
| 299 |
|
| 300 |
delete m_shaderComputeV; |
| 301 |
m_shaderComputeV = new QOpenGLShaderProgram; |
| 302 |
m_shaderComputeV->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceV)); |
| 303 |
m_shaderComputeV->link(); |
| 304 |
|
| 305 |
delete m_shaderComputeH; |
| 306 |
m_shaderComputeH = new QOpenGLShaderProgram; |
| 307 |
m_shaderComputeH->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceH)); |
| 308 |
m_shaderComputeH->link(); |
| 309 |
|
| 310 |
// Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL core context. |
| 311 |
m_vao = new QOpenGLVertexArrayObject; |
| 312 |
m_vao->create(); |
| 313 |
} |
| 314 |
|
| 315 |
void GLWindow::resizeGL(int w, int h) |
| 316 |
{ |
| 317 |
computeProjection(w,h,m_texImageInput->width(),m_texImageInput->height(),m_proj,m_quadSize); |
| 318 |
} |
| 319 |
|
| 320 |
QSize getWorkGroups(int workGroupSize, const QSize &imageSize) |
| 321 |
{ |
| 322 |
int x = imageSize.width(); |
| 323 |
x = (x % workGroupSize) ? (x / workGroupSize) + 1 : (x / workGroupSize); |
| 324 |
int y = imageSize.height(); |
| 325 |
y = (y % workGroupSize) ? (y / workGroupSize) + 1 : (y / workGroupSize); |
| 326 |
return QSize(x,y); |
| 327 |
} |
| 328 |
|
| 329 |
void GLWindow::paintGL() |
| 330 |
{ |
| 331 |
// Now use QOpenGLExtraFunctions instead of QOpenGLFunctions as we want to |
| 332 |
// do more than what GL(ES) 2.0 offers. |
| 333 |
QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); |
| 334 |
|
| 335 |
|
| 336 |
// Process input image |
| 337 |
QSize workGroups = getWorkGroups(10, QSize(m_texImageInput->width(), m_texImageInput->height())); |
| 338 |
// Pass 1 |
| 339 |
f->glBindImageTexture(0, m_texImageInput->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 340 |
f->glBindImageTexture(1, m_texImageTmp->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 341 |
m_shaderComputeV->bind(); |
| 342 |
m_shaderComputeV->setUniformValue("radius",m_blurRadius); |
| 343 |
f->glDispatchCompute(workGroups.width(),workGroups.height(),1); |
| 344 |
f->glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); |
| 345 |
m_shaderComputeV->release(); |
| 346 |
// Pass 2 |
| 347 |
f->glBindImageTexture(0, m_texImageTmp->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 348 |
f->glBindImageTexture(1, m_texImageProcessed->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 349 |
m_shaderComputeH->bind(); |
| 350 |
m_shaderComputeH->setUniformValue("radius",m_blurRadius); |
| 351 |
f->glDispatchCompute(workGroups.width(),workGroups.height(),1); |
| 352 |
f->glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); |
| 353 |
m_shaderComputeH->release(); |
| 354 |
// Compute cleanup |
| 355 |
f->glBindImageTexture(0, 0, 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 356 |
f->glBindImageTexture(1, 0, 0, 0, 0, GL_READ_WRITE, GL_RGBA8); |
| 357 |
|
| 358 |
// Display processed image |
| 359 |
f->glClearColor(0, 0, 0, 1); |
| 360 |
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 361 |
m_texImageProcessed->bind(0); |
| 362 |
m_shaderDisplay->bind(); |
| 363 |
m_shaderDisplay->setUniformValue("matProjection",m_proj); |
| 364 |
m_shaderDisplay->setUniformValue("imageRatio",m_quadSize); |
| 365 |
m_shaderDisplay->setUniformValue("samImage",0); |
| 366 |
m_vao->bind(); |
| 367 |
f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 368 |
m_vao->release(); |
| 369 |
m_shaderDisplay->release(); |
| 370 |
m_texImageProcessed->release(0); |
| 371 |
} |
| 372 |
|