Skip to content

Commit

Permalink
Fix computegles31 example
Browse files Browse the repository at this point in the history
This patch fixes the example that contained problems showing up
with mesa/nouveau drivers.

Change-Id: Ic90f6028e394138781f00bcc00c145c56134f441
Reviewed-by: Gunnar Sletta <[email protected]>
  • Loading branch information
paoletto committed Mar 8, 2017
1 parent 535b142 commit 40ace7a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 48 deletions.
65 changes: 39 additions & 26 deletions examples/opengl/computegles31/glwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLExtraFunctions>
//#include <QtGui/qopenglext.h>
#include <QOpenGLVertexArrayObject>
#include <QtGui/qopengl.h>
#include <QDebug>
#include <QTimer>
Expand All @@ -80,7 +80,8 @@ GLWindow::GLWindow()
m_shaderComputeV(0),
m_shaderComputeH(0),
m_blurRadius(0.0f),
m_animate(true)
m_animate(true),
m_vao(0)
{
const float animationStart = 0.0;
const float animationEnd = 10.0;
Expand Down Expand Up @@ -116,6 +117,7 @@ GLWindow::~GLWindow()
delete m_animationGroup;
delete m_animationForward;
delete m_animationBackward;
delete m_vao;
}

void GLWindow::setBlurRadius(float blurRadius)
Expand Down Expand Up @@ -178,19 +180,21 @@ static const char *fsDisplaySource =
"}\n";

static const char *csComputeSourceV =
//"#extension GL_EXT_gpu_shader5 : require \n"
"#define COMPUTEPATCHSIZE 32 \n"
"#define IMGFMT rgba8 \n"
"layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n"
"layout(binding=0, IMGFMT) uniform highp image2D inputImage; // Use a sampler to improve performance \n"
"layout(binding=1, IMGFMT) uniform highp image2D resultImage;\n"
"layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n"
"layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n"
"uniform int radius;\n"
"const float cutoff = 2.2;\n"
"float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n" // Const initialization with dynamically uniform expressions doesn't work in GLES
"float expFactor = 1.0 / (2.0 * sigma * sigma);\n" // Same here

"float gaussian(float distance) {\n"
" return exp( -(distance * distance) * expFactor);\n"
"float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n"
" float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n"
" return 1.0 / (2.0 * sigma * sigma);\n"
"}\n"

"float gaussian(float distance, float expfactor) {\n"
" return exp( -(distance * distance) * expfactor);\n"
"}\n"

"void main() {\n"
Expand All @@ -204,31 +208,34 @@ static const char *csComputeSourceV =
" int right = clamp(x + radius, 0, imgSize.x - 1);\n"
" int top = clamp(y - radius, 0, imgSize.y - 1);\n"
" int bottom = clamp(y + radius, 0, imgSize.y - 1);\n"
" float expfactor = expFactor();\n"
" for (int iY = top; iY <= bottom; iY++) {\n"
" float dy = float(abs(iY - y));\n"
" vec4 imgValue = imageLoad(inputImage, ivec2(x,iY));\n"
" float weight = gaussian(dy);\n"
" float weight = gaussian(dy, expfactor);\n"
" sumWeights += weight;\n"
" sumPixels += (imgValue * weight);\n"
" }\n"
" sumPixels /= sumWeights;\n"
" imageStore(resultImage, ivec2(x,y), sumPixels);"
" imageStore(resultImage, ivec2(x,y), sumPixels);\n"
"}\n";

static const char *csComputeSourceH =
//"#extension GL_EXT_gpu_shader5 : require \n"
"#define COMPUTEPATCHSIZE 32 \n"
"#define IMGFMT rgba8 \n"
"layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n"
"layout(binding=0, IMGFMT) uniform highp image2D inputImage; // Use a sampler to improve performance \n"
"layout(binding=1, IMGFMT) uniform highp image2D resultImage;\n"
"layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n"
"layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n"
"uniform int radius;\n"
"const float cutoff = 2.2;\n"
"float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n"
"float expFactor = 1.0 / (2.0 * sigma * sigma);\n"

"float gaussian(float distance) {\n"
" return exp( -(distance * distance) * expFactor);\n"
"float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n"
" float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n"
" return 1.0 / (2.0 * sigma * sigma);\n"
"}\n"

"float gaussian(float distance, float expfactor) {\n"
" return exp( -(distance * distance) * expfactor);\n"
"}\n"

"void main() {\n"
Expand All @@ -242,15 +249,16 @@ static const char *csComputeSourceH =
" int right = clamp(x + radius, 0, imgSize.x - 1);\n"
" int top = clamp(y - radius, 0, imgSize.y - 1);\n"
" int bottom = clamp(y + radius, 0, imgSize.y - 1);\n"
" for (int iX = left; iX <= right; iX++) {\n"
" float expfactor = expFactor();\n"
" for (int iX = left; iX <= right; iX++) {\n"
" float dx = float(abs(iX - x));\n"
" vec4 imgValue = imageLoad(inputImage, ivec2(iX,y));\n"
" float weight = gaussian(dx);\n"
" float weight = gaussian(dx, expfactor);\n"
" sumWeights += weight;\n"
" sumPixels += (imgValue * weight);\n"
" }\n"
" }\n"
" sumPixels /= sumWeights;\n"
" imageStore(resultImage, ivec2(x,y), sumPixels);"
" imageStore(resultImage, ivec2(x,y), sumPixels);\n"
"}\n";


Expand All @@ -262,7 +270,7 @@ QByteArray versionedShaderCode(const char *src)
if (QOpenGLContext::currentContext()->isOpenGLES())
versionedSrc.append(QByteArrayLiteral("#version 310 es\n"));
else
versionedSrc.append(QByteArrayLiteral("#version 430\n"));
versionedSrc.append(QByteArrayLiteral("#version 430 core\n"));

versionedSrc.append(src);
return versionedSrc;
Expand Down Expand Up @@ -315,7 +323,6 @@ void GLWindow::initializeGL()
<< ctx->format().minorVersion()
<< ((ctx->format().renderableType() == QSurfaceFormat::OpenGLES) ? (" GLES") : (" GL"))
<< " context";
//QOpenGLFunctions *f = ctx->functions();

if (m_texImageInput) {
delete m_texImageInput;
Expand Down Expand Up @@ -373,6 +380,10 @@ void GLWindow::initializeGL()
m_shaderComputeH = new QOpenGLShaderProgram;
m_shaderComputeH->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceH));
m_shaderComputeH->link();

// Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL core context.
m_vao = new QOpenGLVertexArrayObject;
m_vao->create();
}

void GLWindow::resizeGL(int w, int h)
Expand Down Expand Up @@ -421,13 +432,15 @@ void GLWindow::paintGL()
// Display processed image
f->glClearColor(0, 0, 0, 1);
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_texImageProcessed->bind(GL_TEXTURE0);
m_texImageProcessed->bind(0);
m_shaderDisplay->bind();
m_shaderDisplay->setUniformValue("matProjection",m_proj);
m_shaderDisplay->setUniformValue("imageRatio",m_quadSize);
m_shaderDisplay->setUniformValue("samImage",0);
m_vao->bind();
f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_vao->release();
m_shaderDisplay->release();
m_texImageProcessed->release(GL_TEXTURE0);
m_texImageProcessed->release(0);
}

1 change: 1 addition & 0 deletions examples/opengl/computegles31/glwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class GLWindow : public QOpenGLWindow

int m_blurRadius;
bool m_animate;
QOpenGLVertexArrayObject *m_vao;
};

#endif
64 changes: 42 additions & 22 deletions examples/opengl/computegles31/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@
#include <QPair>
#include "glwindow.h"

bool OGLSupports(int major, int minor, bool gles = false)
bool OGLSupports(int major, int minor, bool gles = false, QSurfaceFormat::OpenGLContextProfile profile = QSurfaceFormat::NoProfile)
{
QOpenGLContext ctx;
QSurfaceFormat fmt;
fmt.setVersion(major, minor);
if (gles)
if (gles) {
fmt.setRenderableType(QSurfaceFormat::OpenGLES);
else
} else {
fmt.setRenderableType(QSurfaceFormat::OpenGL);
fmt.setProfile(profile);
}

ctx.setFormat(fmt);
ctx.create();
Expand All @@ -78,25 +80,43 @@ bool OGLSupports(int major, int minor, bool gles = false)
if (ctxMajor < major) return false;
if (ctxMajor == major && ctxMinor < minor)
return false;
if (!gles && ctx.format().profile() != profile)
return false;
return true;
}

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

qDebug() << "Support for GL 2.0 "<<( OGLSupports(2,0) ? "yes" : "no");
qDebug() << "Support for GL 2.1 "<<( OGLSupports(2,1) ? "yes" : "no");
qDebug() << "Support for GL 3.0 "<<( OGLSupports(3,0) ? "yes" : "no");
qDebug() << "Support for GL 3.1 "<<( OGLSupports(3,1) ? "yes" : "no");
qDebug() << "Support for GL 3.2 "<<( OGLSupports(3,2) ? "yes" : "no");
qDebug() << "Support for GL 3.3 "<<( OGLSupports(3,3) ? "yes" : "no");
qDebug() << "Support for GL 4.0 "<<( OGLSupports(4,0) ? "yes" : "no");
qDebug() << "Support for GL 4.1 "<<( OGLSupports(4,1) ? "yes" : "no");
qDebug() << "Support for GL 4.2 "<<( OGLSupports(4,2) ? "yes" : "no");
qDebug() << "Support for GL 4.3 "<<( OGLSupports(4,3) ? "yes" : "no");
qDebug() << "Support for GL 4.4 "<<( OGLSupports(4,4) ? "yes" : "no");
qDebug() << "Support for GL 4.5 "<<( OGLSupports(4,5) ? "yes" : "no");
qDebug() << "Support for GL 2.0 noprof "<<( OGLSupports(2,0,false) ? "yes" : "no");
qDebug() << "Support for GL 2.0 core "<<( OGLSupports(2,0,false, QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 2.0 compat "<<( OGLSupports(2,0,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 2.1 noprof "<<( OGLSupports(2,1,false) ? "yes" : "no");
qDebug() << "Support for GL 2.1 core "<<( OGLSupports(2,1,false, QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 2.1 compat "<<( OGLSupports(2,1,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.0 noprof "<<( OGLSupports(3,0,false) ? "yes" : "no");
qDebug() << "Support for GL 3.0 core "<<( OGLSupports(3,0,false, QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.0 compat "<<( OGLSupports(3,0,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.1 noprof "<<( OGLSupports(3,1,false) ? "yes" : "no");
qDebug() << "Support for GL 3.1 core "<<( OGLSupports(3,1,false, QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.1 compat "<<( OGLSupports(3,1,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.2 core "<<( OGLSupports(3,2,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.2 compat "<<( OGLSupports(3,2,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.3 core "<<( OGLSupports(3,3,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 3.3 compat "<<( OGLSupports(3,3,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.0 core "<<( OGLSupports(4,0,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.0 compat "<<( OGLSupports(4,0,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.1 core "<<( OGLSupports(4,1,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.1 compat "<<( OGLSupports(4,1,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.2 core "<<( OGLSupports(4,2,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.2 compat "<<( OGLSupports(4,2,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.3 core "<<( OGLSupports(4,3,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.3 compat "<<( OGLSupports(4,3,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.4 core "<<( OGLSupports(4,4,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.4 compat "<<( OGLSupports(4,4,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.5 core "<<( OGLSupports(4,5,false,QSurfaceFormat::CoreProfile) ? "yes" : "no");
qDebug() << "Support for GL 4.5 compat "<<( OGLSupports(4,5,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no");
qDebug() << "Support for GLES 2.0 "<<( OGLSupports(2,0,true) ? "yes" : "no");
qDebug() << "Support for GLES 3.0 "<<( OGLSupports(3,0,true) ? "yes" : "no");
qDebug() << "Support for GLES 3.1 "<<( OGLSupports(3,1,true) ? "yes" : "no");
Expand All @@ -105,16 +125,16 @@ int main(int argc, char *argv[])
QSurfaceFormat fmt;
fmt.setDepthBufferSize(24);

// Request OpenGL 4.3 compatibility or OpenGL ES 3.1.
if (OGLSupports(4,3)) {
qDebug("Requesting 4.3 compatibility context");
fmt.setVersion(4, 3);
fmt.setRenderableType(QSurfaceFormat::OpenGL);
fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
} else if (OGLSupports(3,1,true)) {
// Request OpenGL ES 3.1 context, as this is a GLES example. If not available, go for OpenGL 4.3 core.
if (OGLSupports(3,1,true)) {
qDebug("Requesting 3.1 GLES context");
fmt.setVersion(3, 1);
fmt.setRenderableType(QSurfaceFormat::OpenGLES);
} else if (OGLSupports(4,3,false,QSurfaceFormat::CoreProfile)) {
qDebug("Requesting 4.3 core context");
fmt.setVersion(4, 3);
fmt.setRenderableType(QSurfaceFormat::OpenGL);
fmt.setProfile(QSurfaceFormat::CoreProfile);
} else {
qWarning("Error: This system does not support OpenGL Compute Shaders! Exiting.");
return -1;
Expand Down

0 comments on commit 40ace7a

Please sign in to comment.