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 | |
70 | struct Version { |
71 | const char *str; |
72 | int major; |
73 | int minor; |
74 | }; |
75 | |
76 | static struct Version versions[] = { |
77 | { .str: "1.0" , .major: 1, .minor: 0 }, |
78 | { .str: "1.1" , .major: 1, .minor: 1 }, |
79 | { .str: "1.2" , .major: 1, .minor: 2 }, |
80 | { .str: "1.3" , .major: 1, .minor: 3 }, |
81 | { .str: "1.4" , .major: 1, .minor: 4 }, |
82 | { .str: "1.5" , .major: 1, .minor: 5 }, |
83 | { .str: "2.0" , .major: 2, .minor: 0 }, |
84 | { .str: "2.1" , .major: 2, .minor: 1 }, |
85 | { .str: "3.0" , .major: 3, .minor: 0 }, |
86 | { .str: "3.1" , .major: 3, .minor: 1 }, |
87 | { .str: "3.2" , .major: 3, .minor: 2 }, |
88 | { .str: "3.3" , .major: 3, .minor: 3 }, |
89 | { .str: "4.0" , .major: 4, .minor: 0 }, |
90 | { .str: "4.1" , .major: 4, .minor: 1 }, |
91 | { .str: "4.2" , .major: 4, .minor: 2 }, |
92 | { .str: "4.3" , .major: 4, .minor: 3 }, |
93 | { .str: "4.4" , .major: 4, .minor: 4 }, |
94 | { .str: "4.5" , .major: 4, .minor: 5 } |
95 | }; |
96 | |
97 | struct Profile { |
98 | const char *str; |
99 | QSurfaceFormat::OpenGLContextProfile profile; |
100 | }; |
101 | |
102 | static struct Profile profiles[] = { |
103 | { .str: "none" , .profile: QSurfaceFormat::NoProfile }, |
104 | { .str: "core" , .profile: QSurfaceFormat::CoreProfile }, |
105 | { .str: "compatibility" , .profile: QSurfaceFormat::CompatibilityProfile } |
106 | }; |
107 | |
108 | struct Option { |
109 | const char *str; |
110 | QSurfaceFormat::FormatOption option; |
111 | }; |
112 | |
113 | static struct Option options[] = { |
114 | { .str: "deprecated functions (not forward compatible)" , .option: QSurfaceFormat::DeprecatedFunctions }, |
115 | { .str: "debug context" , .option: QSurfaceFormat::DebugContext }, |
116 | { .str: "stereo buffers" , .option: 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 | { .str: "force version 110 shaders" , .option: QSurfaceFormat::FormatOption(0) } |
120 | }; |
121 | |
122 | struct Renderable { |
123 | const char *str; |
124 | QSurfaceFormat::RenderableType renderable; |
125 | }; |
126 | |
127 | static struct Renderable renderables[] = { |
128 | { .str: "default" , .renderable: QSurfaceFormat::DefaultRenderableType }, |
129 | { .str: "OpenGL" , .renderable: QSurfaceFormat::OpenGL }, |
130 | { .str: "OpenGL ES" , .renderable: QSurfaceFormat::OpenGLES } |
131 | }; |
132 | |
133 | void Widget::addVersions(QLayout *layout) |
134 | { |
135 | QHBoxLayout *hbox = new QHBoxLayout; |
136 | hbox->setSpacing(20); |
137 | QLabel *label = new QLabel(tr(s: "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(atext: QString::fromLatin1(str: 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(s: "Create context" )); |
150 | connect(sender: btn, signal: &QPushButton::clicked, receiver: this, slot: &Widget::start); |
151 | btn->setMinimumSize(minw: 120, minh: 40); |
152 | hbox->addWidget(btn); |
153 | |
154 | layout->addItem(hbox); |
155 | } |
156 | |
157 | void Widget::addProfiles(QLayout *layout) |
158 | { |
159 | QGroupBox *groupBox = new QGroupBox(tr(s: "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(str: profiles[i].str))); |
163 | static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true); |
164 | groupBox->setLayout(vbox); |
165 | layout->addWidget(w: groupBox); |
166 | m_profiles = vbox; |
167 | } |
168 | |
169 | void Widget::addOptions(QLayout *layout) |
170 | { |
171 | QGroupBox *groupBox = new QGroupBox(tr(s: "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(str: options[i].str))); |
175 | groupBox->setLayout(vbox); |
176 | layout->addWidget(w: groupBox); |
177 | m_options = vbox; |
178 | } |
179 | |
180 | void Widget::addRenderableTypes(QLayout *layout) |
181 | { |
182 | QGroupBox *groupBox = new QGroupBox(tr(s: "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(str: renderables[i].str))); |
186 | static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true); |
187 | groupBox->setLayout(vbox); |
188 | layout->addWidget(w: groupBox); |
189 | m_renderables = vbox; |
190 | } |
191 | |
192 | void Widget::addRenderWindow() |
193 | { |
194 | m_renderWindowLayout->addWidget(m_renderWindowContainer); |
195 | } |
196 | |
197 | static QWidget *widgetWithLayout(QLayout *layout) |
198 | { |
199 | QWidget *w = new QWidget; |
200 | w->setLayout(layout); |
201 | return w; |
202 | } |
203 | |
204 | Widget::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(layout: settingsLayout); |
215 | addProfiles(layout: settingsLayout); |
216 | addOptions(layout: settingsLayout); |
217 | addRenderableTypes(layout: settingsLayout); |
218 | hsplit->addWidget(widget: widgetWithLayout(layout: 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(widget: widgetWithLayout(layout: outputLayout)); |
228 | |
229 | hsplit->setStretchFactor(index: 0, stretch: 4); |
230 | hsplit->setStretchFactor(index: 1, stretch: 6); |
231 | vsplit->addWidget(widget: hsplit); |
232 | |
233 | m_renderWindowLayout = new QVBoxLayout; |
234 | vsplit->addWidget(widget: widgetWithLayout(layout: m_renderWindowLayout)); |
235 | vsplit->setStretchFactor(index: 1, stretch: 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(varName: openGlVariables[v])) |
248 | str << ' ' << openGlVariables[v] << '=' << qgetenv(varName: openGlVariables[v]); |
249 | } |
250 | if (QCoreApplication::testAttribute(attribute: Qt::AA_UseOpenGLES)) |
251 | str << " Qt::AA_UseOpenGLES" ; |
252 | if (QCoreApplication::testAttribute(attribute: Qt::AA_UseSoftwareOpenGL)) |
253 | str << " Qt::AA_UseSoftwareOpenGL" ; |
254 | if (QCoreApplication::testAttribute(attribute: Qt::AA_UseDesktopOpenGL)) |
255 | str << " Qt::AA_UseDesktopOpenGL" ; |
256 | layout->addWidget(new QLabel(description)); |
257 | |
258 | setLayout(layout); |
259 | } |
260 | |
261 | void Widget::start() |
262 | { |
263 | QSurfaceFormat fmt; |
264 | |
265 | int idx = m_version->currentIndex(); |
266 | if (idx < 0) |
267 | return; |
268 | fmt.setVersion(major: versions[idx].major, minor: versions[idx].minor); |
269 | |
270 | for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i) |
271 | if (static_cast<QRadioButton *>(m_profiles->itemAt(index: 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(index: int(i))->widget())->isChecked()) { |
279 | if (options[i].option) |
280 | fmt.setOption(option: 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(index: 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(w: m_renderWindowContainer); |
299 | delete m_renderWindowContainer; |
300 | |
301 | RenderWindow *renderWindow = new RenderWindow(fmt); |
302 | if (!renderWindow->context()) { |
303 | m_output->append(text: tr(s: "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(sender: renderWindow, signal: &RenderWindow::ready, receiver: this, slot: &Widget::renderWindowReady); |
313 | connect(sender: renderWindow, signal: &RenderWindow::error, receiver: this, slot: &Widget::renderWindowError); |
314 | |
315 | m_renderWindowContainer = QWidget::createWindowContainer(window: renderWindow); |
316 | addRenderWindow(); |
317 | } |
318 | |
319 | void Widget::printFormat(const QSurfaceFormat &format) |
320 | { |
321 | m_output->append(text: tr(s: "OpenGL version: %1.%2" ).arg(a: format.majorVersion()).arg(a: 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(text: tr(s: "Profile: %1" ).arg(a: QString::fromLatin1(str: 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(option: options[i].option)) |
332 | opts += QString::fromLatin1(str: options[i].str) + QLatin1Char(' '); |
333 | m_output->append(text: tr(s: "Options: %1" ).arg(a: 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(text: tr(s: "Renderable type: %1" ).arg(a: QString::fromLatin1(str: renderables[i].str))); |
338 | break; |
339 | } |
340 | |
341 | m_output->append(text: tr(s: "Depth buffer size: %1" ).arg(a: QString::number(format.depthBufferSize()))); |
342 | m_output->append(text: tr(s: "Stencil buffer size: %1" ).arg(a: QString::number(format.stencilBufferSize()))); |
343 | m_output->append(text: tr(s: "Samples: %1" ).arg(a: QString::number(format.samples()))); |
344 | m_output->append(text: tr(s: "Red buffer size: %1" ).arg(a: QString::number(format.redBufferSize()))); |
345 | m_output->append(text: tr(s: "Green buffer size: %1" ).arg(a: QString::number(format.greenBufferSize()))); |
346 | m_output->append(text: tr(s: "Blue buffer size: %1" ).arg(a: QString::number(format.blueBufferSize()))); |
347 | m_output->append(text: tr(s: "Alpha buffer size: %1" ).arg(a: QString::number(format.alphaBufferSize()))); |
348 | m_output->append(text: tr(s: "Swap interval: %1" ).arg(a: QString::number(format.swapInterval()))); |
349 | } |
350 | |
351 | void 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(str: reinterpret_cast<const char *>(p)); |
361 | if ((p = f->glGetString(GL_RENDERER))) |
362 | renderer = QString::fromLatin1(str: reinterpret_cast<const char *>(p)); |
363 | if ((p = f->glGetString(GL_VERSION))) |
364 | version = QString::fromLatin1(str: reinterpret_cast<const char *>(p)); |
365 | if ((p = f->glGetString(GL_SHADING_LANGUAGE_VERSION))) |
366 | glslVersion = QString::fromLatin1(str: reinterpret_cast<const char *>(p)); |
367 | |
368 | m_output->append(text: tr(s: "*** Context information ***" )); |
369 | m_output->append(text: tr(s: "Vendor: %1" ).arg(a: vendor)); |
370 | m_output->append(text: tr(s: "Renderer: %1" ).arg(a: renderer)); |
371 | m_output->append(text: tr(s: "OpenGL version: %1" ).arg(a: version)); |
372 | m_output->append(text: tr(s: "GLSL version: %1" ).arg(a: glslVersion)); |
373 | |
374 | m_output->append(text: tr(s: "\n*** QSurfaceFormat from context ***" )); |
375 | printFormat(format: context->format()); |
376 | |
377 | m_output->append(text: tr(s: "\n*** QSurfaceFormat from window surface ***" )); |
378 | printFormat(format: m_surface->format()); |
379 | |
380 | m_output->append(text: tr(s: "\n*** Qt build information ***" )); |
381 | const char *gltype[] = { "Desktop" , "GLES 2" , "GLES 1" }; |
382 | m_output->append(text: tr(s: "Qt OpenGL configuration: %1" ) |
383 | .arg(a: QString::fromLatin1(str: gltype[QOpenGLContext::openGLModuleType()]))); |
384 | m_output->append(text: tr(s: "Qt OpenGL library handle: %1" ) |
385 | .arg(a: QString::number(qintptr(QOpenGLContext::openGLModuleHandle()), base: 16))); |
386 | |
387 | QList<QByteArray> extensionList = context->extensions().values(); |
388 | std::sort(first: extensionList.begin(), last: extensionList.end()); |
389 | m_extensions->append(text: tr(s: "Found %1 extensions:" ).arg(a: extensionList.count())); |
390 | for (const QByteArray &ext : qAsConst(t&: extensionList)) |
391 | m_extensions->append(text: QString::fromLatin1(str: ext)); |
392 | |
393 | m_output->moveCursor(operation: QTextCursor::Start); |
394 | m_extensions->moveCursor(operation: QTextCursor::Start); |
395 | } |
396 | |
397 | void Widget::renderWindowError(const QString &msg) |
398 | { |
399 | m_output->append(text: tr(s: "An error has occurred:\n%1" ).arg(a: msg)); |
400 | } |
401 | |