Add an example for creating OpenGL contexts

Besides serving as an example for performing OpenGL rendering inside
a window container in a way that it works across all GL versions,
this is an extremely useful tool for developers and users alike
since it allows quick and easy checking of what sort of context a
particular driver returns for a particular QSurfaceFormat.

NB! Depending on the OpenGL driver, some surprises can be expected.
The handling of core/compatibility profiles, the fwdcompat bit,
the supported GLSL versions, etc. tend to be somewhat different
across the different OpenGL implementations.

Task-number: QTBUG-37071
Change-Id: Iae4328e66cd0bb19f74a77fefef93ea5a3221e31
Reviewed-by: Jørgen Lind <jorgen.lind@digia.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Laszlo Agocs 2014-02-26 15:21:18 +01:00 committed by The Qt Project
parent 4051914641
commit 1219dbe543
7 changed files with 800 additions and 1 deletions

View File

@ -0,0 +1,9 @@
TEMPLATE = app
QT += widgets
SOURCES += main.cpp \
widget.cpp \
renderwindow.cpp
HEADERS += widget.h \
renderwindow.h

View File

@ -0,0 +1,53 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QApplication>
#include "widget.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Widget w;
w.resize(700, 800);
w.show();
return app.exec();
}

View File

@ -0,0 +1,218 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "renderwindow.h"
#include <QTimer>
#include <QMatrix4x4>
#include <QOpenGLContext>
#include <QOpenGLShaderProgram>
RenderWindow::RenderWindow(const QSurfaceFormat &format)
: m_context(0),
m_initialized(false),
m_forceGLSL110(false),
m_angle(0.0f)
{
setSurfaceType(QWindow::OpenGLSurface);
setFormat(format);
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
if (!m_context->create()) {
delete m_context;
m_context = 0;
}
}
void RenderWindow::exposeEvent(QExposeEvent *)
{
if (isExposed())
render();
}
// ES needs the precision qualifiers.
// On desktop GL QOpenGLShaderProgram inserts dummy defines for highp/mediump/lowp.
static const char *vertexShaderSource110 =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";
static const char *fragmentShaderSource110 =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
static const char *vertexShaderSource =
"#version 150\n"
"in vec4 posAttr;\n"
"in vec4 colAttr;\n"
"out vec4 col;\n"
"uniform mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"#version 150\n"
"in vec4 col;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = col;\n"
"}\n";
static GLfloat vertices[] = {
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f
};
static GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
void RenderWindow::init()
{
m_program = new QOpenGLShaderProgram(this);
QSurfaceFormat format = m_context->format();
bool useNewStyleShader = format.profile() == QSurfaceFormat::CoreProfile;
// Try to handle 3.0 & 3.1 that do not have the core/compatibility profile concept 3.2+ has.
// This may still fail since version 150 (3.2) is specified in the sources but it's worth a try.
if (format.renderableType() == QSurfaceFormat::OpenGL && format.majorVersion() == 3 && format.minorVersion() <= 1)
useNewStyleShader = !format.testOption(QSurfaceFormat::DeprecatedFunctions);
if (m_forceGLSL110)
useNewStyleShader = false;
const char *vsrc = useNewStyleShader ? vertexShaderSource : vertexShaderSource110;
const char *fsrc = useNewStyleShader ? fragmentShaderSource : fragmentShaderSource110;
qDebug("Using version %s shader", useNewStyleShader ? "150" : "110");
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vsrc)) {
emit error(m_program->log());
return;
}
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fsrc)) {
emit error(m_program->log());
return;
}
if (!m_program->link()) {
emit error(m_program->log());
return;
}
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->uniformLocation("matrix");
m_vbo.create();
m_vbo.bind();
m_vbo.allocate(vertices, sizeof(vertices) + sizeof(colors));
m_vbo.write(sizeof(vertices), colors, sizeof(colors));
m_vbo.release();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
if (m_vao.isCreated()) // have VAO support, use it
setupVertexAttribs();
}
void RenderWindow::setupVertexAttribs()
{
m_vbo.bind();
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT, 0, 2);
m_program->setAttributeBuffer(m_colAttr, GL_FLOAT, sizeof(vertices), 3);
m_program->enableAttributeArray(m_posAttr);
m_program->enableAttributeArray(m_colAttr);
m_vbo.release();
}
void RenderWindow::render()
{
if (!m_context->makeCurrent(this)) {
qWarning("makeCurrent() failed");
return;
}
if (!m_initialized) {
m_initialized = true;
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
init();
emit ready();
}
if (!m_vbo.isCreated()) // init() failed, don't bother with trying to render
return;
const qreal retinaScale = devicePixelRatio();
glViewport(0, 0, width() * retinaScale, height() * retinaScale);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
matrix.translate(0.0f, 0.0f, -2.0f);
matrix.rotate(m_angle, 0.0f, 1.0f, 0.0f);
m_program->setUniformValue(m_matrixUniform, matrix);
if (m_vao.isCreated())
m_vao.bind();
else // no VAO support, set the vertex attribute arrays now
setupVertexAttribs();
glDrawArrays(GL_TRIANGLES, 0, 3);
m_vao.release();
m_program->release();
// swapInterval is 1 by default which means that swapBuffers() will (hopefully) block
// and wait for vsync.
m_context->swapBuffers(this);
m_angle += 1.0f;
QTimer::singleShot(0, this, SLOT(render()));
}

View File

@ -0,0 +1,82 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef RENDERWINDOW_H
#define RENDERWINDOW_H
#include <QWindow>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
QT_FORWARD_DECLARE_CLASS(QOpenGLContext)
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
class RenderWindow : public QWindow
{
Q_OBJECT
public:
RenderWindow(const QSurfaceFormat &format);
QOpenGLContext *context() { return m_context; }
void exposeEvent(QExposeEvent *) Q_DECL_OVERRIDE;
void setForceGLSL110(bool enable) { m_forceGLSL110 = enable; }
signals:
void ready();
void error(const QString &msg);
private slots:
void render();
private:
void init();
void setupVertexAttribs();
QOpenGLContext *m_context;
bool m_initialized;
bool m_forceGLSL110;
QOpenGLShaderProgram *m_program;
int m_posAttr, m_colAttr, m_matrixUniform;
QOpenGLVertexArrayObject m_vao;
QOpenGLBuffer m_vbo;
float m_angle;
};
#endif // RENDERWINDOW_H

View File

@ -0,0 +1,357 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "widget.h"
#include "renderwindow.h"
#include <QVBoxLayout>
#include <QComboBox>
#include <QGroupBox>
#include <QRadioButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include <QSplitter>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QDebug>
struct Version {
const char *str;
int major;
int minor;
};
static struct Version versions[] = {
{ "1.0", 1, 0 },
{ "1.1", 1, 1 },
{ "1.2", 1, 2 },
{ "1.3", 1, 3 },
{ "1.4", 1, 4 },
{ "1.5", 1, 5 },
{ "2.0", 2, 0 },
{ "2.1", 2, 1 },
{ "3.0", 3, 0 },
{ "3.1", 3, 1 },
{ "3.2", 3, 2 },
{ "3.3", 3, 3 },
{ "4.0", 4, 0 },
{ "4.1", 4, 1 },
{ "4.2", 4, 2 },
{ "4.3", 4, 3 },
{ "4.4", 4, 4 }
};
struct Profile {
const char *str;
QSurfaceFormat::OpenGLContextProfile profile;
};
static struct Profile profiles[] = {
{ "none", QSurfaceFormat::NoProfile },
{ "core", QSurfaceFormat::CoreProfile },
{ "compatibility", QSurfaceFormat::CompatibilityProfile }
};
struct Option {
const char *str;
QSurfaceFormat::FormatOption option;
};
static struct Option options[] = {
{ "deprecated functions (not forward compatible)", QSurfaceFormat::DeprecatedFunctions },
{ "debug context", QSurfaceFormat::DebugContext },
{ "stereo buffers", QSurfaceFormat::StereoBuffers },
// This is not a QSurfaceFormat option but is helpful to determine if the driver
// allows compiling old-style shaders with core profile.
{ "force version 110 shaders", QSurfaceFormat::FormatOption(0) }
};
struct Renderable {
const char *str;
QSurfaceFormat::RenderableType renderable;
};
static struct Renderable renderables[] = {
{ "default", QSurfaceFormat::DefaultRenderableType },
{ "OpenGL", QSurfaceFormat::OpenGL },
{ "OpenGL ES", QSurfaceFormat::OpenGLES }
};
void Widget::addVersions(QLayout *layout)
{
QHBoxLayout *hbox = new QHBoxLayout;
hbox->setSpacing(20);
QLabel *label = new QLabel(tr("Context &version: "));
hbox->addWidget(label);
m_version = new QComboBox;
m_version->setMinimumWidth(60);
label->setBuddy(m_version);
hbox->addWidget(m_version);
for (size_t i = 0; i < sizeof(versions) / sizeof(Version); ++i) {
m_version->addItem(QString::fromLatin1(versions[i].str));
if (versions[i].major == 2 && versions[i].minor == 0)
m_version->setCurrentIndex(m_version->count() - 1);
}
QPushButton *btn = new QPushButton(tr("Create context"));
connect(btn, &QPushButton::clicked, this, &Widget::start);
btn->setMinimumSize(120, 40);
hbox->addWidget(btn);
layout->addItem(hbox);
}
void Widget::addProfiles(QLayout *layout)
{
QGroupBox *groupBox = new QGroupBox(tr("Profile"));
QVBoxLayout *vbox = new QVBoxLayout;
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
vbox->addWidget(new QRadioButton(QString::fromLatin1(profiles[i].str)));
static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
groupBox->setLayout(vbox);
layout->addWidget(groupBox);
m_profiles = vbox;
}
void Widget::addOptions(QLayout *layout)
{
QGroupBox *groupBox = new QGroupBox(tr("Options"));
QVBoxLayout *vbox = new QVBoxLayout;
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
vbox->addWidget(new QCheckBox(QString::fromLatin1(options[i].str)));
groupBox->setLayout(vbox);
layout->addWidget(groupBox);
m_options = vbox;
}
void Widget::addRenderableTypes(QLayout *layout)
{
QGroupBox *groupBox = new QGroupBox(tr("Renderable type"));
QVBoxLayout *vbox = new QVBoxLayout;
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
vbox->addWidget(new QRadioButton(QString::fromLatin1(renderables[i].str)));
static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
groupBox->setLayout(vbox);
layout->addWidget(groupBox);
m_renderables = vbox;
}
void Widget::addRenderWindow()
{
m_renderWindowLayout->addWidget(m_renderWindowContainer);
}
static QWidget *widgetWithLayout(QLayout *layout)
{
QWidget *w = new QWidget;
w->setLayout(layout);
return w;
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
QSplitter *vsplit = new QSplitter(Qt::Vertical);
layout->addWidget(vsplit);
QSplitter *hsplit = new QSplitter;
QVBoxLayout *settingsLayout = new QVBoxLayout;
addVersions(settingsLayout);
addProfiles(settingsLayout);
addOptions(settingsLayout);
addRenderableTypes(settingsLayout);
hsplit->addWidget(widgetWithLayout(settingsLayout));
QVBoxLayout *outputLayout = new QVBoxLayout;
m_output = new QTextEdit;
m_output->setReadOnly(true);
outputLayout->addWidget(m_output);
m_extensions = new QTextEdit;
m_extensions->setReadOnly(true);
outputLayout->addWidget(m_extensions);
hsplit->addWidget(widgetWithLayout(outputLayout));
hsplit->setStretchFactor(0, 4);
hsplit->setStretchFactor(1, 6);
vsplit->addWidget(hsplit);
m_renderWindowLayout = new QVBoxLayout;
vsplit->addWidget(widgetWithLayout(m_renderWindowLayout));
vsplit->setStretchFactor(1, 5);
m_renderWindowContainer = new QWidget;
addRenderWindow();
setLayout(layout);
}
void Widget::start()
{
QSurfaceFormat fmt;
int idx = m_version->currentIndex();
if (idx < 0)
return;
fmt.setVersion(versions[idx].major, versions[idx].minor);
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
if (static_cast<QRadioButton *>(m_profiles->itemAt(int(i))->widget())->isChecked()) {
fmt.setProfile(profiles[i].profile);
break;
}
bool forceGLSL110 = false;
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
if (static_cast<QCheckBox *>(m_options->itemAt(int(i))->widget())->isChecked()) {
if (options[i].option)
fmt.setOption(options[i].option);
else if (i == 3)
forceGLSL110 = true;
}
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
if (static_cast<QRadioButton *>(m_renderables->itemAt(int(i))->widget())->isChecked()) {
fmt.setRenderableType(renderables[i].renderable);
break;
}
// The example rendering will need a depth buffer.
fmt.setDepthBufferSize(16);
m_output->clear();
m_extensions->clear();
qDebug() << "Requesting surface format" << fmt;
m_renderWindowLayout->removeWidget(m_renderWindowContainer);
delete m_renderWindowContainer;
RenderWindow *renderWindow = new RenderWindow(fmt);
if (!renderWindow->context()) {
m_output->append(tr("Failed to create context"));
delete renderWindow;
m_renderWindowContainer = new QWidget;
addRenderWindow();
return;
}
renderWindow->setForceGLSL110(forceGLSL110);
connect(renderWindow, &RenderWindow::ready, this, &Widget::renderWindowReady);
connect(renderWindow, &RenderWindow::error, this, &Widget::renderWindowError);
m_renderWindowContainer = QWidget::createWindowContainer(renderWindow);
addRenderWindow();
}
void Widget::renderWindowReady()
{
QOpenGLContext *context = QOpenGLContext::currentContext();
Q_ASSERT(context);
const QSurfaceFormat format = context->format();
m_output->append(tr("OpenGL version: %1.%2").arg(format.majorVersion()).arg(format.minorVersion()));
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
if (profiles[i].profile == format.profile()) {
m_output->append(tr("Profile: %1").arg(QString::fromLatin1(profiles[i].str)));
break;
}
QString opts;
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
if (format.testOption(options[i].option))
opts += QString::fromLatin1(options[i].str) + QStringLiteral(" ");
m_output->append(tr("Options: %1").arg(opts));
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
if (renderables[i].renderable == format.renderableType()) {
m_output->append(tr("Renderable type: %1").arg(QString::fromLatin1(renderables[i].str)));
break;
}
QString vendor, renderer, version, glslVersion;
const GLubyte *p;
if ((p = glGetString(GL_VENDOR)))
vendor = QString::fromLatin1(reinterpret_cast<const char *>(p));
if ((p = glGetString(GL_RENDERER)))
renderer = QString::fromLatin1(reinterpret_cast<const char *>(p));
if ((p = glGetString(GL_VERSION)))
version = QString::fromLatin1(reinterpret_cast<const char *>(p));
if ((p = glGetString(GL_SHADING_LANGUAGE_VERSION)))
glslVersion = QString::fromLatin1(reinterpret_cast<const char *>(p));
m_output->append(tr("\nVendor: %1").arg(vendor));
m_output->append(tr("Renderer: %1").arg(renderer));
m_output->append(tr("OpenGL version: %1").arg(version));
m_output->append(tr("GLSL version: %1").arg(glslVersion));
m_output->append(tr("\nDepth buffer size: %1").arg(QString::number(format.depthBufferSize())));
m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize())));
m_output->append(tr("Samples: %1").arg(QString::number(format.samples())));
m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize())));
m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize())));
m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize())));
m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize())));
m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval())));
const char *gltype[] = { "Desktop", "GLES 2", "GLES 1" };
m_output->append(tr("\nQt OpenGL configuration: %1")
.arg(QString::fromLatin1(gltype[QOpenGLFunctions::platformGLType()])));
m_output->append(tr("Qt OpenGL library handle: %1")
.arg(QString::number(qintptr(QOpenGLFunctions::platformGLHandle()), 16)));
QList<QByteArray> extensionList = context->extensions().toList();
std::sort(extensionList.begin(), extensionList.end());
m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count()));
Q_FOREACH (const QByteArray &ext, extensionList)
m_extensions->append(QString::fromLatin1(ext));
m_output->moveCursor(QTextCursor::Start);
m_extensions->moveCursor(QTextCursor::Start);
}
void Widget::renderWindowError(const QString &msg)
{
m_output->append(tr("An error has occurred:\n%1").arg(msg));
}

View File

@ -0,0 +1,79 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_FORWARD_DECLARE_CLASS(QComboBox)
QT_FORWARD_DECLARE_CLASS(QTextEdit)
QT_FORWARD_DECLARE_CLASS(QVBoxLayout)
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
private slots:
void start();
void renderWindowReady();
void renderWindowError(const QString &msg);
private:
void addVersions(QLayout *layout);
void addProfiles(QLayout *layout);
void addOptions(QLayout *layout);
void addRenderableTypes(QLayout *layout);
void addRenderWindow();
QComboBox *m_version;
QLayout *m_profiles;
QLayout *m_options;
QLayout *m_renderables;
QTextEdit *m_output;
QTextEdit *m_extensions;
QVBoxLayout *m_renderWindowLayout;
QWidget *m_renderWindowContainer;
};
#endif // WIDGET_H

View File

@ -25,6 +25,7 @@ contains(QT_CONFIG, opengles1)|contains(QT_CONFIG, opengles2){
}
SUBDIRS += hellowindow \
paintedwindow
paintedwindow \
contextinfo
EXAMPLE_FILES = shared