| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "widget.h" |
| 5 |
#include "renderwindow.h" |
| 6 |
#include <QVBoxLayout> |
| 7 |
#include <QComboBox> |
| 8 |
#include <QGroupBox> |
| 9 |
#include <QRadioButton> |
| 10 |
#include <QCheckBox> |
| 11 |
#include <QHBoxLayout> |
| 12 |
#include <QLabel> |
| 13 |
#include <QList> |
| 14 |
#include <QByteArray> |
| 15 |
#include <QPushButton> |
| 16 |
#include <QTextEdit> |
| 17 |
#include <QSplitter> |
| 18 |
#include <QGuiApplication> |
| 19 |
#include <QSurfaceFormat> |
| 20 |
#include <QOpenGLContext> |
| 21 |
#include <QOpenGLFunctions> |
| 22 |
#include <QDebug> |
| 23 |
#include <QTextStream> |
| 24 |
|
| 25 |
struct Version { |
| 26 |
const char *str; |
| 27 |
int major; |
| 28 |
int minor; |
| 29 |
}; |
| 30 |
|
| 31 |
static struct Version versions[] = { |
| 32 |
{ "1.0", 1, 0 }, |
| 33 |
{ "1.1", 1, 1 }, |
| 34 |
{ "1.2", 1, 2 }, |
| 35 |
{ "1.3", 1, 3 }, |
| 36 |
{ "1.4", 1, 4 }, |
| 37 |
{ "1.5", 1, 5 }, |
| 38 |
{ "2.0", 2, 0 }, |
| 39 |
{ "2.1", 2, 1 }, |
| 40 |
{ "3.0", 3, 0 }, |
| 41 |
{ "3.1", 3, 1 }, |
| 42 |
{ "3.2", 3, 2 }, |
| 43 |
{ "3.3", 3, 3 }, |
| 44 |
{ "4.0", 4, 0 }, |
| 45 |
{ "4.1", 4, 1 }, |
| 46 |
{ "4.2", 4, 2 }, |
| 47 |
{ "4.3", 4, 3 }, |
| 48 |
{ "4.4", 4, 4 }, |
| 49 |
{ "4.5", 4, 5 } |
| 50 |
}; |
| 51 |
|
| 52 |
struct Profile { |
| 53 |
const char *str; |
| 54 |
QSurfaceFormat::OpenGLContextProfile profile; |
| 55 |
}; |
| 56 |
|
| 57 |
static struct Profile profiles[] = { |
| 58 |
{ "none", QSurfaceFormat::NoProfile }, |
| 59 |
{ "core", QSurfaceFormat::CoreProfile }, |
| 60 |
{ "compatibility", QSurfaceFormat::CompatibilityProfile } |
| 61 |
}; |
| 62 |
|
| 63 |
struct Option { |
| 64 |
const char *str; |
| 65 |
QSurfaceFormat::FormatOption option; |
| 66 |
}; |
| 67 |
|
| 68 |
static struct Option options[] = { |
| 69 |
{ "deprecated functions (not forward compatible)", QSurfaceFormat::DeprecatedFunctions }, |
| 70 |
{ "debug context", QSurfaceFormat::DebugContext }, |
| 71 |
{ "stereo buffers", QSurfaceFormat::StereoBuffers }, |
| 72 |
// This is not a QSurfaceFormat option but is helpful to determine if the driver |
| 73 |
// allows compiling old-style shaders with core profile. |
| 74 |
{ "force version 110 shaders", QSurfaceFormat::FormatOption(0) } |
| 75 |
}; |
| 76 |
|
| 77 |
struct Renderable { |
| 78 |
const char *str; |
| 79 |
QSurfaceFormat::RenderableType renderable; |
| 80 |
}; |
| 81 |
|
| 82 |
static struct Renderable renderables[] = { |
| 83 |
{ "default", QSurfaceFormat::DefaultRenderableType }, |
| 84 |
#ifndef Q_OS_ANDROID |
| 85 |
{ "OpenGL", QSurfaceFormat::OpenGL }, |
| 86 |
#endif |
| 87 |
{ "OpenGL ES", QSurfaceFormat::OpenGLES } |
| 88 |
}; |
| 89 |
|
| 90 |
void Widget::addVersions(QLayout *layout) |
| 91 |
{ |
| 92 |
QHBoxLayout *hbox = new QHBoxLayout; |
| 93 |
hbox->setSpacing(20); |
| 94 |
QLabel *label = new QLabel(tr("Context &version: ")); |
| 95 |
hbox->addWidget(label); |
| 96 |
m_version = new QComboBox; |
| 97 |
m_version->setMinimumWidth(60); |
| 98 |
label->setBuddy(m_version); |
| 99 |
hbox->addWidget(m_version); |
| 100 |
for (size_t i = 0; i < sizeof(versions) / sizeof(Version); ++i) { |
| 101 |
m_version->addItem(QString::fromLatin1(versions[i].str)); |
| 102 |
if (versions[i].major == 2 && versions[i].minor == 0) |
| 103 |
m_version->setCurrentIndex(m_version->count() - 1); |
| 104 |
} |
| 105 |
|
| 106 |
QPushButton *btn = new QPushButton(tr("Create context")); |
| 107 |
connect(btn, &QPushButton::clicked, this, &Widget::start); |
| 108 |
btn->setMinimumSize(120, 40); |
| 109 |
hbox->addWidget(btn); |
| 110 |
|
| 111 |
layout->addItem(hbox); |
| 112 |
} |
| 113 |
|
| 114 |
void Widget::addProfiles(QLayout *layout) |
| 115 |
{ |
| 116 |
QGroupBox *groupBox = new QGroupBox(tr("Profile")); |
| 117 |
QVBoxLayout *vbox = new QVBoxLayout; |
| 118 |
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i) |
| 119 |
vbox->addWidget(new QRadioButton(QString::fromLatin1(profiles[i].str))); |
| 120 |
static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true); |
| 121 |
groupBox->setLayout(vbox); |
| 122 |
layout->addWidget(groupBox); |
| 123 |
m_profiles = vbox; |
| 124 |
} |
| 125 |
|
| 126 |
void Widget::addOptions(QLayout *layout) |
| 127 |
{ |
| 128 |
QGroupBox *groupBox = new QGroupBox(tr("Options")); |
| 129 |
QVBoxLayout *vbox = new QVBoxLayout; |
| 130 |
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i) |
| 131 |
vbox->addWidget(new QCheckBox(QString::fromLatin1(options[i].str))); |
| 132 |
groupBox->setLayout(vbox); |
| 133 |
layout->addWidget(groupBox); |
| 134 |
m_options = vbox; |
| 135 |
} |
| 136 |
|
| 137 |
void Widget::addRenderableTypes(QLayout *layout) |
| 138 |
{ |
| 139 |
QGroupBox *groupBox = new QGroupBox(tr("Renderable type")); |
| 140 |
QVBoxLayout *vbox = new QVBoxLayout; |
| 141 |
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i) |
| 142 |
vbox->addWidget(new QRadioButton(QString::fromLatin1(renderables[i].str))); |
| 143 |
static_cast<QRadioButton *>(vbox->itemAt(0)->widget())->setChecked(true); |
| 144 |
groupBox->setLayout(vbox); |
| 145 |
layout->addWidget(groupBox); |
| 146 |
m_renderables = vbox; |
| 147 |
} |
| 148 |
|
| 149 |
void Widget::addRenderWindow() |
| 150 |
{ |
| 151 |
m_renderWindowLayout->addWidget(m_renderWindowContainer); |
| 152 |
} |
| 153 |
|
| 154 |
static QWidget *widgetWithLayout(QLayout *layout) |
| 155 |
{ |
| 156 |
QWidget *w = new QWidget; |
| 157 |
w->setLayout(layout); |
| 158 |
return w; |
| 159 |
} |
| 160 |
|
| 161 |
Widget::Widget(QWidget *parent) |
| 162 |
: QWidget(parent) |
| 163 |
{ |
| 164 |
QVBoxLayout *layout = new QVBoxLayout; |
| 165 |
QSplitter *vsplit = new QSplitter(Qt::Vertical); |
| 166 |
layout->addWidget(vsplit); |
| 167 |
|
| 168 |
QSplitter *hsplit = new QSplitter; |
| 169 |
|
| 170 |
QVBoxLayout *settingsLayout = new QVBoxLayout; |
| 171 |
addVersions(settingsLayout); |
| 172 |
addProfiles(settingsLayout); |
| 173 |
addOptions(settingsLayout); |
| 174 |
addRenderableTypes(settingsLayout); |
| 175 |
hsplit->addWidget(widgetWithLayout(settingsLayout)); |
| 176 |
|
| 177 |
QVBoxLayout *outputLayout = new QVBoxLayout; |
| 178 |
m_output = new QTextEdit; |
| 179 |
m_output->setReadOnly(true); |
| 180 |
outputLayout->addWidget(m_output); |
| 181 |
m_extensions = new QTextEdit; |
| 182 |
m_extensions->setReadOnly(true); |
| 183 |
outputLayout->addWidget(m_extensions); |
| 184 |
hsplit->addWidget(widgetWithLayout(outputLayout)); |
| 185 |
|
| 186 |
hsplit->setStretchFactor(0, 4); |
| 187 |
hsplit->setStretchFactor(1, 6); |
| 188 |
vsplit->addWidget(hsplit); |
| 189 |
|
| 190 |
m_renderWindowLayout = new QVBoxLayout; |
| 191 |
vsplit->addWidget(widgetWithLayout(m_renderWindowLayout)); |
| 192 |
vsplit->setStretchFactor(1, 5); |
| 193 |
|
| 194 |
m_renderWindowContainer = new QWidget; |
| 195 |
addRenderWindow(); |
| 196 |
|
| 197 |
QString description; |
| 198 |
QTextStream str(&description); |
| 199 |
str << "Qt " << QT_VERSION_STR << ' ' << QGuiApplication::platformName(); |
| 200 |
const char *openGlVariables[] = |
| 201 |
{"QT_ANGLE_PLATFORM", "QT_OPENGL", "QT_OPENGL_BUGLIST", "QT_OPENGL_DLL"}; |
| 202 |
const size_t variableCount = sizeof(openGlVariables) / sizeof(openGlVariables[0]); |
| 203 |
for (size_t v = 0; v < variableCount; ++v) { |
| 204 |
if (qEnvironmentVariableIsSet(openGlVariables[v])) |
| 205 |
str << ' ' << openGlVariables[v] << '=' << qgetenv(openGlVariables[v]); |
| 206 |
} |
| 207 |
if (QCoreApplication::testAttribute(Qt::AA_UseOpenGLES)) |
| 208 |
str << " Qt::AA_UseOpenGLES"; |
| 209 |
if (QCoreApplication::testAttribute(Qt::AA_UseSoftwareOpenGL)) |
| 210 |
str << " Qt::AA_UseSoftwareOpenGL"; |
| 211 |
if (QCoreApplication::testAttribute(Qt::AA_UseDesktopOpenGL)) |
| 212 |
str << " Qt::AA_UseDesktopOpenGL"; |
| 213 |
layout->addWidget(new QLabel(description)); |
| 214 |
|
| 215 |
setLayout(layout); |
| 216 |
} |
| 217 |
|
| 218 |
void Widget::start() |
| 219 |
{ |
| 220 |
QSurfaceFormat fmt; |
| 221 |
|
| 222 |
int idx = m_version->currentIndex(); |
| 223 |
if (idx < 0) |
| 224 |
return; |
| 225 |
fmt.setVersion(versions[idx].major, versions[idx].minor); |
| 226 |
|
| 227 |
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i) |
| 228 |
if (static_cast<QRadioButton *>(m_profiles->itemAt(int(i))->widget())->isChecked()) { |
| 229 |
fmt.setProfile(profiles[i].profile); |
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
bool forceGLSL110 = false; |
| 234 |
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i) |
| 235 |
if (static_cast<QCheckBox *>(m_options->itemAt(int(i))->widget())->isChecked()) { |
| 236 |
if (options[i].option) |
| 237 |
fmt.setOption(options[i].option); |
| 238 |
else if (i == 3) |
| 239 |
forceGLSL110 = true; |
| 240 |
} |
| 241 |
|
| 242 |
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i) |
| 243 |
if (static_cast<QRadioButton *>(m_renderables->itemAt(int(i))->widget())->isChecked()) { |
| 244 |
fmt.setRenderableType(renderables[i].renderable); |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
// The example rendering will need a depth buffer. |
| 249 |
fmt.setDepthBufferSize(16); |
| 250 |
|
| 251 |
m_output->clear(); |
| 252 |
m_extensions->clear(); |
| 253 |
qDebug() << "Requesting surface format" << fmt; |
| 254 |
|
| 255 |
m_renderWindowLayout->removeWidget(m_renderWindowContainer); |
| 256 |
delete m_renderWindowContainer; |
| 257 |
|
| 258 |
RenderWindow *renderWindow = new RenderWindow(fmt); |
| 259 |
if (!renderWindow->context()) { |
| 260 |
m_output->append(tr("Failed to create context")); |
| 261 |
delete renderWindow; |
| 262 |
m_renderWindowContainer = new QWidget; |
| 263 |
addRenderWindow(); |
| 264 |
return; |
| 265 |
} |
| 266 |
m_surface = renderWindow; |
| 267 |
|
| 268 |
renderWindow->setForceGLSL110(forceGLSL110); |
| 269 |
connect(renderWindow, &RenderWindow::ready, this, &Widget::renderWindowReady); |
| 270 |
connect(renderWindow, &RenderWindow::error, this, &Widget::renderWindowError); |
| 271 |
|
| 272 |
m_renderWindowContainer = QWidget::createWindowContainer(renderWindow); |
| 273 |
addRenderWindow(); |
| 274 |
} |
| 275 |
|
| 276 |
void Widget::printFormat(const QSurfaceFormat &format) |
| 277 |
{ |
| 278 |
m_output->append(tr("OpenGL version: %1.%2").arg(format.majorVersion()).arg(format.minorVersion())); |
| 279 |
|
| 280 |
for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i) |
| 281 |
if (profiles[i].profile == format.profile()) { |
| 282 |
m_output->append(tr("Profile: %1").arg(QString::fromLatin1(profiles[i].str))); |
| 283 |
break; |
| 284 |
} |
| 285 |
|
| 286 |
QString opts; |
| 287 |
for (size_t i = 0; i < sizeof(options) / sizeof(Option); ++i) |
| 288 |
if (format.testOption(options[i].option)) |
| 289 |
opts += QString::fromLatin1(options[i].str) + QLatin1Char(' '); |
| 290 |
m_output->append(tr("Options: %1").arg(opts)); |
| 291 |
|
| 292 |
for (size_t i = 0; i < sizeof(renderables) / sizeof(Renderable); ++i) |
| 293 |
if (renderables[i].renderable == format.renderableType()) { |
| 294 |
m_output->append(tr("Renderable type: %1").arg(QString::fromLatin1(renderables[i].str))); |
| 295 |
break; |
| 296 |
} |
| 297 |
|
| 298 |
m_output->append(tr("Depth buffer size: %1").arg(QString::number(format.depthBufferSize()))); |
| 299 |
m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize()))); |
| 300 |
m_output->append(tr("Samples: %1").arg(QString::number(format.samples()))); |
| 301 |
m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize()))); |
| 302 |
m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize()))); |
| 303 |
m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize()))); |
| 304 |
m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize()))); |
| 305 |
m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval()))); |
| 306 |
} |
| 307 |
|
| 308 |
void Widget::renderWindowReady() |
| 309 |
{ |
| 310 |
QOpenGLContext *context = QOpenGLContext::currentContext(); |
| 311 |
Q_ASSERT(context); |
| 312 |
|
| 313 |
QString vendor, renderer, version, glslVersion; |
| 314 |
const GLubyte *p; |
| 315 |
QOpenGLFunctions *f = context->functions(); |
| 316 |
if ((p = f->glGetString(GL_VENDOR))) |
| 317 |
vendor = QString::fromLatin1(reinterpret_cast<const char *>(p)); |
| 318 |
if ((p = f->glGetString(GL_RENDERER))) |
| 319 |
renderer = QString::fromLatin1(reinterpret_cast<const char *>(p)); |
| 320 |
if ((p = f->glGetString(GL_VERSION))) |
| 321 |
version = QString::fromLatin1(reinterpret_cast<const char *>(p)); |
| 322 |
if ((p = f->glGetString(GL_SHADING_LANGUAGE_VERSION))) |
| 323 |
glslVersion = QString::fromLatin1(reinterpret_cast<const char *>(p)); |
| 324 |
|
| 325 |
m_output->append(tr("*** Context information ***")); |
| 326 |
m_output->append(tr("Vendor: %1").arg(vendor)); |
| 327 |
m_output->append(tr("Renderer: %1").arg(renderer)); |
| 328 |
m_output->append(tr("OpenGL version: %1").arg(version)); |
| 329 |
m_output->append(tr("GLSL version: %1").arg(glslVersion)); |
| 330 |
|
| 331 |
m_output->append(tr("\n*** QSurfaceFormat from context ***")); |
| 332 |
printFormat(context->format()); |
| 333 |
|
| 334 |
m_output->append(tr("\n*** QSurfaceFormat from window surface ***")); |
| 335 |
printFormat(m_surface->format()); |
| 336 |
|
| 337 |
m_output->append(tr("\n*** Qt build information ***")); |
| 338 |
const char *gltype[] = { "Desktop", "GLES 2", "GLES 1" }; |
| 339 |
m_output->append(tr("Qt OpenGL configuration: %1") |
| 340 |
.arg(QString::fromLatin1(gltype[QOpenGLContext::openGLModuleType()]))); |
| 341 |
#if defined(Q_OS_WIN) |
| 342 |
using namespace QNativeInterface; |
| 343 |
m_output->append(tr("Qt OpenGL library handle: %1") |
| 344 |
.arg(QString::number(qintptr(QWGLContext::openGLModuleHandle()), 16))); |
| 345 |
#endif |
| 346 |
|
| 347 |
QList<QByteArray> extensionList = context->extensions().values(); |
| 348 |
std::sort(extensionList.begin(), extensionList.end()); |
| 349 |
m_extensions->append(tr("Found %1 extensions:").arg(extensionList.count())); |
| 350 |
for (const QByteArray &ext : std::as_const(extensionList)) |
| 351 |
m_extensions->append(QString::fromLatin1(ext)); |
| 352 |
|
| 353 |
m_output->moveCursor(QTextCursor::Start); |
| 354 |
m_extensions->moveCursor(QTextCursor::Start); |
| 355 |
} |
| 356 |
|
| 357 |
void Widget::renderWindowError(const QString &msg) |
| 358 |
{ |
| 359 |
m_output->append(tr("An error has occurred:\n%1").arg(msg)); |
| 360 |
} |