1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "widget.h"
52#include "renderwindow.h"
53#include <QVBoxLayout>
54#include <QComboBox>
55#include <QGroupBox>
56#include <QRadioButton>
57#include <QCheckBox>
58#include <QHBoxLayout>
59#include <QLabel>
60#include <QPushButton>
61#include <QTextEdit>
62#include <QSplitter>
63#include <QGuiApplication>
64#include <QSurfaceFormat>
65#include <QOpenGLContext>
66#include <QOpenGLFunctions>
67#include <QDebug>
68#include <QTextStream>
69
70struct Version {
71 const char *str;
72 int major;
73 int minor;
74};
75
76static struct Version versions[] = {
77 { "1.0", 1, 0 },
78 { "1.1", 1, 1 },
79 { "1.2", 1, 2 },
80 { "1.3", 1, 3 },
81 { "1.4", 1, 4 },
82 { "1.5", 1, 5 },
83 { "2.0", 2, 0 },
84 { "2.1", 2, 1 },
85 { "3.0", 3, 0 },
86 { "3.1", 3, 1 },
87 { "3.2", 3, 2 },
88 { "3.3", 3, 3 },
89 { "4.0", 4, 0 },
90 { "4.1", 4, 1 },
91 { "4.2", 4, 2 },
92 { "4.3", 4, 3 },
93 { "4.4", 4, 4 },
94 { "4.5", 4, 5 }
95};
96
97struct Profile {
98 const char *str;
99 QSurfaceFormat::OpenGLContextProfile profile;
100};
101
102static struct Profile profiles[] = {
103 { "none", QSurfaceFormat::NoProfile },
104 { "core", QSurfaceFormat::CoreProfile },
105 { "compatibility", QSurfaceFormat::CompatibilityProfile }
106};
107
108struct Option {
109 const char *str;
110 QSurfaceFormat::FormatOption option;
111};
112
113static struct Option options[] = {
114 { "deprecated functions (not forward compatible)", QSurfaceFormat::DeprecatedFunctions },
115 { "debug context", QSurfaceFormat::DebugContext },
116 { "stereo buffers", QSurfaceFormat::StereoBuffers },
117 // This is not a QSurfaceFormat option but is helpful to determine if the driver
118 // allows compiling old-style shaders with core profile.
119 { "force version 110 shaders", QSurfaceFormat::FormatOption(0) }
120};
121
122struct Renderable {
123 const char *str;
124 QSurfaceFormat::RenderableType renderable;
125};
126
127static struct Renderable renderables[] = {
128 { "default", QSurfaceFormat::DefaultRenderableType },
129 { "OpenGL", QSurfaceFormat::OpenGL },
130 { "OpenGL ES", QSurfaceFormat::OpenGLES }
131};
132
133void Widget::addVersions(QLayout *layout)
134{
135 QHBoxLayout *hbox = new QHBoxLayout;
136 hbox->setSpacing(20);
137 QLabel *label = new QLabel(tr("Context &version: "));
138 hbox->addWidget(label);
139 m_version = new QComboBox;
140 m_version->setMinimumWidth(60);
141 label->setBuddy(m_version);
142 hbox->addWidget(m_version);
143 for (size_t i = 0; i < sizeof(versions) / sizeof(Version); ++i) {
144 m_version->addItem(QString::fromLatin1(versions[i].str));
145 if (versions[i].major == 2 && versions[i].minor == 0)
146 m_version->setCurrentIndex(m_version->count() - 1);
147 }
148
149 QPushButton *btn = new QPushButton(tr("Create context"));
150 connect(btn, &QPushButton::clicked, this, &Widget::start);
151 btn->setMinimumSize(120, 40);
152 hbox->addWidget(btn);
153
154 layout->addItem(hbox);
155}
156
157void Widget::addProfiles(QLayout *layout)
158{
159 QGroupBox *groupBox = new QGroupBox(tr("Profile"));
160 QVBoxLayout *vbox = new QVBoxLayout;
161 for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
162 vbox->addWidget(new QRadioButton(QString::fromLatin1(profiles[i].str)));
163 static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
164 groupBox->setLayout(vbox);
165 layout->addWidget(groupBox);
166 m_profiles = vbox;
167}
168
169void Widget::addOptions(QLayout *layout)
170{
171 QGroupBox *groupBox = new QGroupBox(tr("Options"));
172 QVBoxLayout *vbox = new QVBoxLayout;
173 for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
174 vbox->addWidget(new QCheckBox(QString::fromLatin1(options[i].str)));
175 groupBox->setLayout(vbox);
176 layout->addWidget(groupBox);
177 m_options = vbox;
178}
179
180void Widget::addRenderableTypes(QLayout *layout)
181{
182 QGroupBox *groupBox = new QGroupBox(tr("Renderable type"));
183 QVBoxLayout *vbox = new QVBoxLayout;
184 for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
185 vbox->addWidget(new QRadioButton(QString::fromLatin1(renderables[i].str)));
186 static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true);
187 groupBox->setLayout(vbox);
188 layout->addWidget(groupBox);
189 m_renderables = vbox;
190}
191
192void Widget::addRenderWindow()
193{
194 m_renderWindowLayout->addWidget(m_renderWindowContainer);
195}
196
197static QWidget *widgetWithLayout(QLayout *layout)
198{
199 QWidget *w = new QWidget;
200 w->setLayout(layout);
201 return w;
202}
203
204Widget::Widget(QWidget *parent)
205 : QWidget(parent)
206{
207 QVBoxLayout *layout = new QVBoxLayout;
208 QSplitter *vsplit = new QSplitter(Qt::Vertical);
209 layout->addWidget(vsplit);
210
211 QSplitter *hsplit = new QSplitter;
212
213 QVBoxLayout *settingsLayout = new QVBoxLayout;
214 addVersions(settingsLayout);
215 addProfiles(settingsLayout);
216 addOptions(settingsLayout);
217 addRenderableTypes(settingsLayout);
218 hsplit->addWidget(widgetWithLayout(settingsLayout));
219
220 QVBoxLayout *outputLayout = new QVBoxLayout;
221 m_output = new QTextEdit;
222 m_output->setReadOnly(true);
223 outputLayout->addWidget(m_output);
224 m_extensions = new QTextEdit;
225 m_extensions->setReadOnly(true);
226 outputLayout->addWidget(m_extensions);
227 hsplit->addWidget(widgetWithLayout(outputLayout));
228
229 hsplit->setStretchFactor(0, 4);
230 hsplit->setStretchFactor(1, 6);
231 vsplit->addWidget(hsplit);
232
233 m_renderWindowLayout = new QVBoxLayout;
234 vsplit->addWidget(widgetWithLayout(m_renderWindowLayout));
235 vsplit->setStretchFactor(1, 5);
236
237 m_renderWindowContainer = new QWidget;
238 addRenderWindow();
239
240 QString description;
241 QTextStream str(&description);
242 str << "Qt " << QT_VERSION_STR << ' ' << QGuiApplication::platformName();
243 const char *openGlVariables[] =
244 {"QT_ANGLE_PLATFORM", "QT_OPENGL", "QT_OPENGL_BUGLIST", "QT_OPENGL_DLL"};
245 const size_t variableCount = sizeof(openGlVariables) / sizeof(openGlVariables[0]);
246 for (size_t v = 0; v < variableCount; ++v) {
247 if (qEnvironmentVariableIsSet(openGlVariables[v]))
248 str << ' ' << openGlVariables[v] << '=' << qgetenv(openGlVariables[v]);
249 }
250 if (QCoreApplication::testAttribute(Qt::AA_UseOpenGLES))
251 str << " Qt::AA_UseOpenGLES";
252 if (QCoreApplication::testAttribute(Qt::AA_UseSoftwareOpenGL))
253 str << " Qt::AA_UseSoftwareOpenGL";
254 if (QCoreApplication::testAttribute(Qt::AA_UseDesktopOpenGL))
255 str << " Qt::AA_UseDesktopOpenGL";
256 layout->addWidget(new QLabel(description));
257
258 setLayout(layout);
259}
260
261void Widget::start()
262{
263 QSurfaceFormat fmt;
264
265 int idx = m_version->currentIndex();
266 if (idx < 0)
267 return;
268 fmt.setVersion(versions[idx].major, versions[idx].minor);
269
270 for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
271 if (static_cast<QRadioButton *>(m_profiles->itemAt(int(i))->widget())->isChecked()) {
272 fmt.setProfile(profiles[i].profile);
273 break;
274 }
275
276 bool forceGLSL110 = false;
277 for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
278 if (static_cast<QCheckBox *>(m_options->itemAt(int(i))->widget())->isChecked()) {
279 if (options[i].option)
280 fmt.setOption(options[i].option);
281 else if (i == 3)
282 forceGLSL110 = true;
283 }
284
285 for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
286 if (static_cast<QRadioButton *>(m_renderables->itemAt(int(i))->widget())->isChecked()) {
287 fmt.setRenderableType(renderables[i].renderable);
288 break;
289 }
290
291 // The example rendering will need a depth buffer.
292 fmt.setDepthBufferSize(16);
293
294 m_output->clear();
295 m_extensions->clear();
296 qDebug() << "Requesting surface format" << fmt;
297
298 m_renderWindowLayout->removeWidget(m_renderWindowContainer);
299 delete m_renderWindowContainer;
300
301 RenderWindow *renderWindow = new RenderWindow(fmt);
302 if (!renderWindow->context()) {
303 m_output->append(tr("Failed to create context"));
304 delete renderWindow;
305 m_renderWindowContainer = new QWidget;
306 addRenderWindow();
307 return;
308 }
309 m_surface = renderWindow;
310
311 renderWindow->setForceGLSL110(forceGLSL110);
312 connect(renderWindow, &RenderWindow::ready, this, &Widget::renderWindowReady);
313 connect(renderWindow, &RenderWindow::error, this, &Widget::renderWindowError);
314
315 m_renderWindowContainer = QWidget::createWindowContainer(renderWindow);
316 addRenderWindow();
317}
318
319void Widget::printFormat(const QSurfaceFormat &format)
320{
321 m_output->append(tr("OpenGL version: %1.%2").arg(format.majorVersion()).arg(format.minorVersion()));
322
323 for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i)
324 if (profiles[i].profile == format.profile()) {
325 m_output->append(tr("Profile: %1").arg(QString::fromLatin1(profiles[i].str)));
326 break;
327 }
328
329 QString opts;
330 for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i)
331 if (format.testOption(options[i].option))
332 opts += QString::fromLatin1(options[i].str) + QLatin1Char(' ');
333 m_output->append(tr("Options: %1").arg(opts));
334
335 for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i)
336 if (renderables[i].renderable == format.renderableType()) {
337 m_output->append(tr("Renderable type: %1").arg(QString::fromLatin1(renderables[i].str)));
338 break;
339 }
340
341 m_output->append(tr("Depth buffer size: %1").arg(QString::number(format.depthBufferSize())));
342 m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize())));
343 m_output->append(tr("Samples: %1").arg(QString::number(format.samples())));
344 m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize())));
345 m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize())));
346 m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize())));
347 m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize())));
348 m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval())));
349}
350
351void Widget::renderWindowReady()
352{
353 QOpenGLContext *context = QOpenGLContext::currentContext();
354 Q_ASSERT(context);
355
356 QString vendor, renderer, version, glslVersion;
357 const GLubyte *p;
358 QOpenGLFunctions *f = context->functions();
359 if ((p = f->glGetString(GL_VENDOR)))
360 vendor = QString::fromLatin1(reinterpret_cast<const char *>(p));
361 if ((p = f->glGetString(GL_RENDERER)))
362 renderer = QString::fromLatin1(reinterpret_cast<const char *>(p));
363 if ((p = f->glGetString(GL_VERSION)))
364 version = QString::fromLatin1(reinterpret_cast<const char *>(p));
365 if ((p = f->glGetString(GL_SHADING_LANGUAGE_VERSION)))
366 glslVersion = QString::fromLatin1(reinterpret_cast<const char *>(p));
367
368 m_output->append(tr("*** Context information ***"));
369 m_output->append(tr("Vendor: %1").arg(vendor));
370 m_output->append(tr("Renderer: %1").arg(renderer));
371 m_output->append(tr("OpenGL version: %1").arg(version));
372 m_output->append(tr("GLSL version: %1").arg(glslVersion));
373
374 m_output->append(tr("\n*** QSurfaceFormat from context ***"));
375 printFormat(context->format());
376
377 m_output->append(tr("\n*** QSurfaceFormat from window surface ***"));
378 printFormat(m_surface->format());
379
380 m_output->append(tr("\n*** Qt build information ***"));
381 const char *gltype[] = { "Desktop", "GLES 2", "GLES 1" };
382 m_output->append(tr("Qt OpenGL configuration: %1")
383 .arg(QString::fromLatin1(gltype[QOpenGLContext::openGLModuleType()])));
384#if defined(Q_OS_WIN)
385 using namespace QNativeInterface;
386 m_output->append(tr("Qt OpenGL library handle: %1")
387 .arg(QString::number(qintptr(QWGLContext::openGLModuleHandle()), 16)));
388#endif
389
390 QList<QByteArray> extensionList = context->extensions().values();
391 std::sort(extensionList.begin(), extensionList.end());
392 m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count()));
393 for (const QByteArray &ext : qAsConst(extensionList))
394 m_extensions->append(QString::fromLatin1(ext));
395
396 m_output->moveCursor(QTextCursor::Start);
397 m_extensions->moveCursor(QTextCursor::Start);
398}
399
400void Widget::renderWindowError(const QString &msg)
401{
402 m_output->append(tr("An error has occurred:\n%1").arg(msg));
403}
404