summaryrefslogtreecommitdiffstats
path: root/examples/opengl
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2017-02-25 20:31:15 +0100
committerPaolo Angelelli <paolo.angelelli@qt.io>2017-03-08 09:19:39 +0000
commit40ace7a2a0f5cf1e3f097500240b17a2b9cabf2d (patch)
treebd9d115ef967dd5edc32fb53678832d999f665fb /examples/opengl
parent535b142393063b8f9504cb3c2eb716bcfddbbeee (diff)
Fix computegles31 example
This patch fixes the example that contained problems showing up with mesa/nouveau drivers. Change-Id: Ic90f6028e394138781f00bcc00c145c56134f441 Reviewed-by: Gunnar Sletta <gunnar@crimson.no>
Diffstat (limited to 'examples/opengl')
-rw-r--r--examples/opengl/computegles31/glwindow.cpp65
-rw-r--r--examples/opengl/computegles31/glwindow.h1
-rw-r--r--examples/opengl/computegles31/main.cpp64
3 files changed, 82 insertions, 48 deletions
diff --git a/examples/opengl/computegles31/glwindow.cpp b/examples/opengl/computegles31/glwindow.cpp
index 178ad9f1e7..7a14cba66d 100644
--- a/examples/opengl/computegles31/glwindow.cpp
+++ b/examples/opengl/computegles31/glwindow.cpp
@@ -54,7 +54,7 @@
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLExtraFunctions>
-//#include <QtGui/qopenglext.h>
+#include <QOpenGLVertexArrayObject>
#include <QtGui/qopengl.h>
#include <QDebug>
#include <QTimer>
@@ -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;
@@ -116,6 +117,7 @@ GLWindow::~GLWindow()
delete m_animationGroup;
delete m_animationForward;
delete m_animationBackward;
+ delete m_vao;
}
void GLWindow::setBlurRadius(float blurRadius)
@@ -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"
@@ -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"
@@ -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";
@@ -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;
@@ -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;
@@ -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)
@@ -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);
}
diff --git a/examples/opengl/computegles31/glwindow.h b/examples/opengl/computegles31/glwindow.h
index a1a429d7c7..877f7b732e 100644
--- a/examples/opengl/computegles31/glwindow.h
+++ b/examples/opengl/computegles31/glwindow.h
@@ -104,6 +104,7 @@ private:
int m_blurRadius;
bool m_animate;
+ QOpenGLVertexArrayObject *m_vao;
};
#endif
diff --git a/examples/opengl/computegles31/main.cpp b/examples/opengl/computegles31/main.cpp
index e4dde8b3bd..88f0f36115 100644
--- a/examples/opengl/computegles31/main.cpp
+++ b/examples/opengl/computegles31/main.cpp
@@ -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();
@@ -78,6 +80,8 @@ 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;
}
@@ -85,18 +89,34 @@ 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");
@@ -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;