| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 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 |
// Adapted from hellominimalcrossgfxtriangle with the frame rendering stripped out. |
| 52 |
// Include this file and implement Window::customInit, release and render. |
| 53 |
// Debug/validation layer is enabled for D3D and Vulkan. |
| 54 |
|
| 55 |
#include <QGuiApplication> |
| 56 |
#include <QCommandLineParser> |
| 57 |
#include <QWindow> |
| 58 |
#include <QPlatformSurfaceEvent> |
| 59 |
#include <QElapsedTimer> |
| 60 |
#include <QTimer> |
| 61 |
#include <QLoggingCategory> |
| 62 |
|
| 63 |
#include <QtGui/private/qshader_p.h> |
| 64 |
#include <QFile> |
| 65 |
#include <QtGui/private/qrhiprofiler_p.h> |
| 66 |
#include <QtGui/private/qrhinull_p.h> |
| 67 |
|
| 68 |
#ifndef QT_NO_OPENGL |
| 69 |
#include <QtGui/private/qrhigles2_p.h> |
| 70 |
#include <QOffscreenSurface> |
| 71 |
#endif |
| 72 |
|
| 73 |
#if QT_CONFIG(vulkan) |
| 74 |
#include <QtGui/private/qrhivulkan_p.h> |
| 75 |
#endif |
| 76 |
|
| 77 |
#ifdef Q_OS_WIN |
| 78 |
#include <QtGui/private/qrhid3d11_p.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 82 |
#include <QtGui/private/qrhimetal_p.h> |
| 83 |
#endif |
| 84 |
|
| 85 |
QShader getShader(const QString &name) |
| 86 |
{ |
| 87 |
QFile f(name); |
| 88 |
if (f.open(QIODevice::ReadOnly)) |
| 89 |
return QShader::fromSerialized(f.readAll()); |
| 90 |
|
| 91 |
return QShader(); |
| 92 |
} |
| 93 |
|
| 94 |
enum GraphicsApi |
| 95 |
{ |
| 96 |
Null, |
| 97 |
OpenGL, |
| 98 |
Vulkan, |
| 99 |
D3D11, |
| 100 |
Metal |
| 101 |
}; |
| 102 |
|
| 103 |
GraphicsApi graphicsApi; |
| 104 |
|
| 105 |
QString graphicsApiName() |
| 106 |
{ |
| 107 |
switch (graphicsApi) { |
| 108 |
case Null: |
| 109 |
return QLatin1String("Null (no output)"); |
| 110 |
case OpenGL: |
| 111 |
return QLatin1String("OpenGL 2.x"); |
| 112 |
case Vulkan: |
| 113 |
return QLatin1String("Vulkan"); |
| 114 |
case D3D11: |
| 115 |
return QLatin1String("Direct3D 11"); |
| 116 |
case Metal: |
| 117 |
return QLatin1String("Metal"); |
| 118 |
default: |
| 119 |
break; |
| 120 |
} |
| 121 |
return QString(); |
| 122 |
} |
| 123 |
|
| 124 |
QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers; |
| 125 |
int sampleCount = 1; |
| 126 |
QRhiSwapChain::Flags scFlags; |
| 127 |
QRhi::BeginFrameFlags beginFrameFlags; |
| 128 |
QRhi::EndFrameFlags endFrameFlags; |
| 129 |
int framesUntilTdr = -1; |
| 130 |
bool transparentBackground = false; |
| 131 |
|
| 132 |
class Window : public QWindow |
| 133 |
{ |
| 134 |
public: |
| 135 |
Window(); |
| 136 |
~Window(); |
| 137 |
|
| 138 |
protected: |
| 139 |
void init(); |
| 140 |
void releaseResources(); |
| 141 |
void resizeSwapChain(); |
| 142 |
void releaseSwapChain(); |
| 143 |
void render(); |
| 144 |
|
| 145 |
void customInit(); |
| 146 |
void customRelease(); |
| 147 |
void customRender(); |
| 148 |
|
| 149 |
void exposeEvent(QExposeEvent *) override; |
| 150 |
bool event(QEvent *) override; |
| 151 |
#ifdef EXAMPLEFW_KEYPRESS_EVENTS |
| 152 |
void keyPressEvent(QKeyEvent *e) override; |
| 153 |
#endif |
| 154 |
|
| 155 |
bool m_running = false; |
| 156 |
bool m_notExposed = false; |
| 157 |
bool m_newlyExposed = false; |
| 158 |
|
| 159 |
QRhi *m_r = nullptr; |
| 160 |
bool m_hasSwapChain = false; |
| 161 |
QRhiSwapChain *m_sc = nullptr; |
| 162 |
QRhiRenderBuffer *m_ds = nullptr; |
| 163 |
QRhiRenderPassDescriptor *m_rp = nullptr; |
| 164 |
|
| 165 |
QMatrix4x4 m_proj; |
| 166 |
|
| 167 |
QElapsedTimer m_timer; |
| 168 |
int m_frameCount; |
| 169 |
|
| 170 |
#ifndef QT_NO_OPENGL |
| 171 |
QOffscreenSurface *m_fallbackSurface = nullptr; |
| 172 |
#endif |
| 173 |
|
| 174 |
QColor m_clearColor; |
| 175 |
|
| 176 |
friend int main(int, char**); |
| 177 |
}; |
| 178 |
|
| 179 |
Window::Window() |
| 180 |
{ |
| 181 |
// Tell the platform plugin what we want. |
| 182 |
switch (graphicsApi) { |
| 183 |
case OpenGL: |
| 184 |
#if QT_CONFIG(opengl) |
| 185 |
setSurfaceType(OpenGLSurface); |
| 186 |
setFormat(QRhiGles2InitParams::adjustedFormat()); |
| 187 |
#endif |
| 188 |
break; |
| 189 |
case Vulkan: |
| 190 |
setSurfaceType(VulkanSurface); |
| 191 |
break; |
| 192 |
case D3D11: |
| 193 |
setSurfaceType(OpenGLSurface); // not a typo |
| 194 |
break; |
| 195 |
case Metal: |
| 196 |
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) |
| 197 |
setSurfaceType(MetalSurface); |
| 198 |
#endif |
| 199 |
break; |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
m_clearColor = transparentBackground ? Qt::transparent : QColor::fromRgbF(0.4f, 0.7f, 0.0f, 1.0f); |
| 205 |
} |
| 206 |
|
| 207 |
Window::~Window() |
| 208 |
{ |
| 209 |
releaseResources(); |
| 210 |
} |
| 211 |
|
| 212 |
void Window::exposeEvent(QExposeEvent *) |
| 213 |
{ |
| 214 |
// initialize and start rendering when the window becomes usable for graphics purposes |
| 215 |
if (isExposed() && !m_running) { |
| 216 |
m_running = true; |
| 217 |
init(); |
| 218 |
resizeSwapChain(); |
| 219 |
} |
| 220 |
|
| 221 |
const QSize surfaceSize = m_hasSwapChain ? m_sc->surfacePixelSize() : QSize(); |
| 222 |
|
| 223 |
// stop pushing frames when not exposed (or size is 0) |
| 224 |
if ((!isExposed() || (m_hasSwapChain && surfaceSize.isEmpty())) && m_running) |
| 225 |
m_notExposed = true; |
| 226 |
|
| 227 |
// continue when exposed again and the surface has a valid size. |
| 228 |
// note that the surface size can be (0, 0) even though size() reports a valid one... |
| 229 |
if (isExposed() && m_running && m_notExposed && !surfaceSize.isEmpty()) { |
| 230 |
m_notExposed = false; |
| 231 |
m_newlyExposed = true; |
| 232 |
} |
| 233 |
|
| 234 |
// always render a frame on exposeEvent() (when exposed) in order to update |
| 235 |
// immediately on window resize. |
| 236 |
if (isExposed() && !surfaceSize.isEmpty()) |
| 237 |
render(); |
| 238 |
} |
| 239 |
|
| 240 |
bool Window::event(QEvent *e) |
| 241 |
{ |
| 242 |
switch (e->type()) { |
| 243 |
case QEvent::UpdateRequest: |
| 244 |
render(); |
| 245 |
break; |
| 246 |
|
| 247 |
case QEvent::PlatformSurface: |
| 248 |
// this is the proper time to tear down the swapchain (while the native window and surface are still around) |
| 249 |
if (static_cast<QPlatformSurfaceEvent *>(e)->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) |
| 250 |
releaseSwapChain(); |
| 251 |
break; |
| 252 |
|
| 253 |
default: |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
return QWindow::event(e); |
| 258 |
} |
| 259 |
|
| 260 |
void Window::init() |
| 261 |
{ |
| 262 |
if (graphicsApi == Null) { |
| 263 |
QRhiNullInitParams params; |
| 264 |
m_r = QRhi::create(QRhi::Null, ¶ms, rhiFlags); |
| 265 |
} |
| 266 |
|
| 267 |
#ifndef QT_NO_OPENGL |
| 268 |
if (graphicsApi == OpenGL) { |
| 269 |
m_fallbackSurface = QRhiGles2InitParams::newFallbackSurface(); |
| 270 |
QRhiGles2InitParams params; |
| 271 |
params.fallbackSurface = m_fallbackSurface; |
| 272 |
params.window = this; |
| 273 |
m_r = QRhi::create(QRhi::OpenGLES2, ¶ms, rhiFlags); |
| 274 |
} |
| 275 |
#endif |
| 276 |
|
| 277 |
#if QT_CONFIG(vulkan) |
| 278 |
if (graphicsApi == Vulkan) { |
| 279 |
QRhiVulkanInitParams params; |
| 280 |
params.inst = vulkanInstance(); |
| 281 |
params.window = this; |
| 282 |
m_r = QRhi::create(QRhi::Vulkan, ¶ms, rhiFlags); |
| 283 |
} |
| 284 |
#endif |
| 285 |
|
| 286 |
#ifdef Q_OS_WIN |
| 287 |
if (graphicsApi == D3D11) { |
| 288 |
QRhiD3D11InitParams params; |
| 289 |
params.enableDebugLayer = true; |
| 290 |
if (framesUntilTdr > 0) { |
| 291 |
params.framesUntilKillingDeviceViaTdr = framesUntilTdr; |
| 292 |
params.repeatDeviceKill = true; |
| 293 |
} |
| 294 |
m_r = QRhi::create(QRhi::D3D11, ¶ms, rhiFlags); |
| 295 |
} |
| 296 |
#endif |
| 297 |
|
| 298 |
#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 299 |
if (graphicsApi == Metal) { |
| 300 |
QRhiMetalInitParams params; |
| 301 |
m_r = QRhi::create(QRhi::Metal, ¶ms, rhiFlags); |
| 302 |
} |
| 303 |
#endif |
| 304 |
|
| 305 |
if (!m_r) |
| 306 |
qFatal("Failed to create RHI backend"); |
| 307 |
|
| 308 |
// now onto the backend-independent init |
| 309 |
|
| 310 |
m_sc = m_r->newSwapChain(); |
| 311 |
// allow depth-stencil, although we do not actually enable depth test/write for the triangle |
| 312 |
m_ds = m_r->newRenderBuffer(QRhiRenderBuffer::DepthStencil, |
| 313 |
QSize(), // no need to set the size here, due to UsedWithSwapChainOnly |
| 314 |
sampleCount, |
| 315 |
QRhiRenderBuffer::UsedWithSwapChainOnly); |
| 316 |
m_sc->setWindow(this); |
| 317 |
m_sc->setDepthStencil(m_ds); |
| 318 |
m_sc->setSampleCount(sampleCount); |
| 319 |
m_sc->setFlags(scFlags); |
| 320 |
m_rp = m_sc->newCompatibleRenderPassDescriptor(); |
| 321 |
m_sc->setRenderPassDescriptor(m_rp); |
| 322 |
|
| 323 |
customInit(); |
| 324 |
} |
| 325 |
|
| 326 |
void Window::releaseResources() |
| 327 |
{ |
| 328 |
customRelease(); |
| 329 |
|
| 330 |
delete m_rp; |
| 331 |
m_rp = nullptr; |
| 332 |
|
| 333 |
delete m_ds; |
| 334 |
m_ds = nullptr; |
| 335 |
|
| 336 |
delete m_sc; |
| 337 |
m_sc = nullptr; |
| 338 |
|
| 339 |
delete m_r; |
| 340 |
m_r = nullptr; |
| 341 |
|
| 342 |
#ifndef QT_NO_OPENGL |
| 343 |
delete m_fallbackSurface; |
| 344 |
m_fallbackSurface = nullptr; |
| 345 |
#endif |
| 346 |
} |
| 347 |
|
| 348 |
void Window::resizeSwapChain() |
| 349 |
{ |
| 350 |
m_hasSwapChain = m_sc->buildOrResize(); // also handles m_ds |
| 351 |
|
| 352 |
m_frameCount = 0; |
| 353 |
m_timer.restart(); |
| 354 |
|
| 355 |
const QSize outputSize = m_sc->currentPixelSize(); |
| 356 |
m_proj = m_r->clipSpaceCorrMatrix(); |
| 357 |
m_proj.perspective(45.0f, outputSize.width() / (float) outputSize.height(), 0.01f, 1000.0f); |
| 358 |
m_proj.translate(0, 0, -4); |
| 359 |
} |
| 360 |
|
| 361 |
void Window::releaseSwapChain() |
| 362 |
{ |
| 363 |
if (m_hasSwapChain) { |
| 364 |
m_hasSwapChain = false; |
| 365 |
m_sc->release(); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
void Window::render() |
| 370 |
{ |
| 371 |
if (!m_hasSwapChain || m_notExposed) |
| 372 |
return; |
| 373 |
|
| 374 |
// If the window got resized or got newly exposed, resize the swapchain. |
| 375 |
// (the newly-exposed case is not actually required by some |
| 376 |
// platforms/backends, but f.ex. Vulkan on Windows seems to need it) |
| 377 |
if (m_sc->currentPixelSize() != m_sc->surfacePixelSize() || m_newlyExposed) { |
| 378 |
resizeSwapChain(); |
| 379 |
if (!m_hasSwapChain) |
| 380 |
return; |
| 381 |
m_newlyExposed = false; |
| 382 |
} |
| 383 |
|
| 384 |
// Start a new frame. This is where we block when too far ahead of |
| 385 |
// GPU/present, and that's what throttles the thread to the refresh rate. |
| 386 |
// (except for OpenGL where it happens either in endFrame or somewhere else |
| 387 |
// depending on the GL implementation) |
| 388 |
QRhi::FrameOpResult r = m_r->beginFrame(m_sc, beginFrameFlags); |
| 389 |
if (r == QRhi::FrameOpSwapChainOutOfDate) { |
| 390 |
resizeSwapChain(); |
| 391 |
if (!m_hasSwapChain) |
| 392 |
return; |
| 393 |
r = m_r->beginFrame(m_sc); |
| 394 |
} |
| 395 |
if (r != QRhi::FrameOpSuccess) { |
| 396 |
requestUpdate(); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
m_frameCount += 1; |
| 401 |
if (m_timer.elapsed() > 1000) { |
| 402 |
if (rhiFlags.testFlag(QRhi::EnableProfiling)) { |
| 403 |
const QRhiProfiler::CpuTime ff = m_r->profiler()->frameToFrameTimes(m_sc); |
| 404 |
const QRhiProfiler::CpuTime be = m_r->profiler()->frameBuildTimes(m_sc); |
| 405 |
const QRhiProfiler::GpuTime gp = m_r->profiler()->gpuFrameTimes(m_sc); |
| 406 |
if (m_r->isFeatureSupported(QRhi::Timestamps)) { |
| 407 |
qDebug("ca. %d fps. " |
| 408 |
"frame-to-frame: min %lld max %lld avg %f. " |
| 409 |
"frame build: min %lld max %lld avg %f. " |
| 410 |
"gpu frame time: min %f max %f avg %f", |
| 411 |
m_frameCount, |
| 412 |
ff.minTime, ff.maxTime, ff.avgTime, |
| 413 |
be.minTime, be.maxTime, be.avgTime, |
| 414 |
gp.minTime, gp.maxTime, gp.avgTime); |
| 415 |
} else { |
| 416 |
qDebug("ca. %d fps. " |
| 417 |
"frame-to-frame: min %lld max %lld avg %f. " |
| 418 |
"frame build: min %lld max %lld avg %f. ", |
| 419 |
m_frameCount, |
| 420 |
ff.minTime, ff.maxTime, ff.avgTime, |
| 421 |
be.minTime, be.maxTime, be.avgTime); |
| 422 |
} |
| 423 |
} else { |
| 424 |
qDebug("ca. %d fps", m_frameCount); |
| 425 |
} |
| 426 |
|
| 427 |
m_timer.restart(); |
| 428 |
m_frameCount = 0; |
| 429 |
} |
| 430 |
|
| 431 |
customRender(); |
| 432 |
|
| 433 |
m_r->endFrame(m_sc, endFrameFlags); |
| 434 |
|
| 435 |
if (!scFlags.testFlag(QRhiSwapChain::NoVSync)) |
| 436 |
requestUpdate(); |
| 437 |
else // try prevent all delays when NoVSync |
| 438 |
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest)); |
| 439 |
} |
| 440 |
|
| 441 |
int main(int argc, char **argv) |
| 442 |
{ |
| 443 |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); |
| 444 |
QGuiApplication app(argc, argv); |
| 445 |
|
| 446 |
QLoggingCategory::setFilterRules(QLatin1String("qt.rhi.*=true")); |
| 447 |
|
| 448 |
// Defaults. |
| 449 |
#if defined(Q_OS_WIN) |
| 450 |
graphicsApi = D3D11; |
| 451 |
#elif defined(Q_OS_MACOS) || defined(Q_OS_IOS) |
| 452 |
graphicsApi = Metal; |
| 453 |
#elif QT_CONFIG(vulkan) |
| 454 |
graphicsApi = Vulkan; |
| 455 |
#else |
| 456 |
graphicsApi = OpenGL; |
| 457 |
#endif |
| 458 |
|
| 459 |
// Allow overriding via the command line. |
| 460 |
QCommandLineParser cmdLineParser; |
| 461 |
cmdLineParser.addHelpOption(); |
| 462 |
QCommandLineOption nullOption({ "n", "null" }, QLatin1String("Null")); |
| 463 |
cmdLineParser.addOption(nullOption); |
| 464 |
QCommandLineOption glOption({ "g", "opengl" }, QLatin1String("OpenGL (2.x)")); |
| 465 |
cmdLineParser.addOption(glOption); |
| 466 |
QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan")); |
| 467 |
cmdLineParser.addOption(vkOption); |
| 468 |
QCommandLineOption d3dOption({ "d", "d3d11" }, QLatin1String("Direct3D 11")); |
| 469 |
cmdLineParser.addOption(d3dOption); |
| 470 |
QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal")); |
| 471 |
cmdLineParser.addOption(mtlOption); |
| 472 |
// Testing cleanup both with QWindow::close() (hitting X or Alt-F4) and |
| 473 |
// QCoreApplication::quit() (e.g. what a menu widget would do) is important. |
| 474 |
// Use this parameter for the latter. |
| 475 |
QCommandLineOption sdOption({ "s", "self-destruct" }, QLatin1String("Self-destruct after 5 seconds.")); |
| 476 |
cmdLineParser.addOption(sdOption); |
| 477 |
// Attempt testing device lost situations on D3D at least. |
| 478 |
QCommandLineOption tdrOption(QLatin1String("curse"), QLatin1String("Curse the graphics device. " |
| 479 |
"(generate a device reset every <count> frames when on D3D11)"), |
| 480 |
QLatin1String("count")); |
| 481 |
cmdLineParser.addOption(tdrOption); |
| 482 |
// Allow testing preferring the software adapter (D3D). |
| 483 |
QCommandLineOption swOption(QLatin1String("software"), QLatin1String("Prefer a software renderer when choosing the adapter. " |
| 484 |
"Only applicable with some APIs and platforms.")); |
| 485 |
cmdLineParser.addOption(swOption); |
| 486 |
// Allow testing having a semi-transparent window. |
| 487 |
QCommandLineOption transparentOption(QLatin1String("transparent"), QLatin1String("Make background transparent")); |
| 488 |
cmdLineParser.addOption(transparentOption); |
| 489 |
|
| 490 |
cmdLineParser.process(app); |
| 491 |
if (cmdLineParser.isSet(nullOption)) |
| 492 |
graphicsApi = Null; |
| 493 |
if (cmdLineParser.isSet(glOption)) |
| 494 |
graphicsApi = OpenGL; |
| 495 |
if (cmdLineParser.isSet(vkOption)) |
| 496 |
graphicsApi = Vulkan; |
| 497 |
if (cmdLineParser.isSet(d3dOption)) |
| 498 |
graphicsApi = D3D11; |
| 499 |
if (cmdLineParser.isSet(mtlOption)) |
| 500 |
graphicsApi = Metal; |
| 501 |
|
| 502 |
qDebug("Selected graphics API is %s", qPrintable(graphicsApiName())); |
| 503 |
qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText())); |
| 504 |
|
| 505 |
if (cmdLineParser.isSet(transparentOption)) { |
| 506 |
transparentBackground = true; |
| 507 |
scFlags |= QRhiSwapChain::SurfaceHasPreMulAlpha; |
| 508 |
} |
| 509 |
|
| 510 |
#ifdef EXAMPLEFW_PREINIT |
| 511 |
void preInit(); |
| 512 |
preInit(); |
| 513 |
#endif |
| 514 |
|
| 515 |
// OpenGL specifics. |
| 516 |
QSurfaceFormat fmt; |
| 517 |
fmt.setDepthBufferSize(24); |
| 518 |
fmt.setStencilBufferSize(8); |
| 519 |
if (sampleCount > 1) |
| 520 |
fmt.setSamples(sampleCount); |
| 521 |
if (scFlags.testFlag(QRhiSwapChain::NoVSync)) |
| 522 |
fmt.setSwapInterval(0); |
| 523 |
if (scFlags.testFlag(QRhiSwapChain::sRGB)) |
| 524 |
fmt.setColorSpace(QSurfaceFormat::sRGBColorSpace); |
| 525 |
// Exception: The alpha size is not necessarily OpenGL specific. |
| 526 |
if (transparentBackground) |
| 527 |
fmt.setAlphaBufferSize(8); |
| 528 |
QSurfaceFormat::setDefaultFormat(fmt); |
| 529 |
|
| 530 |
// Vulkan setup. |
| 531 |
#if QT_CONFIG(vulkan) |
| 532 |
QVulkanInstance inst; |
| 533 |
if (graphicsApi == Vulkan) { |
| 534 |
#ifndef Q_OS_ANDROID |
| 535 |
inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation"); |
| 536 |
#else |
| 537 |
inst.setLayers(QByteArrayList() |
| 538 |
<< "VK_LAYER_GOOGLE_threading" |
| 539 |
<< "VK_LAYER_LUNARG_parameter_validation" |
| 540 |
<< "VK_LAYER_LUNARG_object_tracker" |
| 541 |
<< "VK_LAYER_LUNARG_core_validation" |
| 542 |
<< "VK_LAYER_LUNARG_image" |
| 543 |
<< "VK_LAYER_LUNARG_swapchain" |
| 544 |
<< "VK_LAYER_GOOGLE_unique_objects"); |
| 545 |
#endif |
| 546 |
inst.setExtensions(QByteArrayList() |
| 547 |
<< "VK_KHR_get_physical_device_properties2"); |
| 548 |
if (!inst.create()) { |
| 549 |
qWarning("Failed to create Vulkan instance, switching to OpenGL"); |
| 550 |
graphicsApi = OpenGL; |
| 551 |
} |
| 552 |
} |
| 553 |
#endif |
| 554 |
|
| 555 |
if (cmdLineParser.isSet(tdrOption)) |
| 556 |
framesUntilTdr = cmdLineParser.value(tdrOption).toInt(); |
| 557 |
|
| 558 |
if (cmdLineParser.isSet(swOption)) |
| 559 |
rhiFlags |= QRhi::PreferSoftwareRenderer; |
| 560 |
|
| 561 |
// Create and show the window. |
| 562 |
Window w; |
| 563 |
#if QT_CONFIG(vulkan) |
| 564 |
if (graphicsApi == Vulkan) |
| 565 |
w.setVulkanInstance(&inst); |
| 566 |
#endif |
| 567 |
w.resize(1280, 720); |
| 568 |
w.setTitle(QCoreApplication::applicationName() + QLatin1String(" - ") + graphicsApiName()); |
| 569 |
w.show(); |
| 570 |
|
| 571 |
if (cmdLineParser.isSet(sdOption)) |
| 572 |
QTimer::singleShot(5000, qGuiApp, SLOT(quit())); |
| 573 |
|
| 574 |
int ret = app.exec(); |
| 575 |
|
| 576 |
// Window::event() will not get invoked when the |
| 577 |
// PlatformSurfaceAboutToBeDestroyed event is sent during the QWindow |
| 578 |
// destruction. That happens only when exiting via app::quit() instead of |
| 579 |
// the more common QWindow::close(). Take care of it: if the |
| 580 |
// QPlatformWindow is still around (there was no close() yet), get rid of |
| 581 |
// the swapchain while it's not too late. |
| 582 |
if (w.handle()) |
| 583 |
w.releaseSwapChain(); |
| 584 |
|
| 585 |
return ret; |
| 586 |
} |