| 1 |
// Copyright (C) 2020 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
// This is a compact, minimal demo of deciding the backend at runtime while |
| 5 |
// using the exact same shaders and rendering code without any branching |
| 6 |
// whatsoever once the QWindow is up and the RHI is initialized. |
| 7 |
|
| 8 |
#include <QGuiApplication> |
| 9 |
#include <QCommandLineParser> |
| 10 |
#include "hellowindow.h" |
| 11 |
|
| 12 |
QString graphicsApiName(QRhi::Implementation graphicsApi) |
| 13 |
{ |
| 14 |
switch (graphicsApi) { |
| 15 |
case QRhi::Null: |
| 16 |
return QLatin1String("Null (no output)"); |
| 17 |
case QRhi::OpenGLES2: |
| 18 |
return QLatin1String("OpenGL 2.x"); |
| 19 |
case QRhi::Vulkan: |
| 20 |
return QLatin1String("Vulkan"); |
| 21 |
case QRhi::D3D11: |
| 22 |
return QLatin1String("Direct3D 11"); |
| 23 |
case QRhi::D3D12: |
| 24 |
return QLatin1String("Direct3D 12"); |
| 25 |
case QRhi::Metal: |
| 26 |
return QLatin1String("Metal"); |
| 27 |
default: |
| 28 |
break; |
| 29 |
} |
| 30 |
return QString(); |
| 31 |
} |
| 32 |
|
| 33 |
int main(int argc, char **argv) |
| 34 |
{ |
| 35 |
QGuiApplication app(argc, argv); |
| 36 |
|
| 37 |
QRhi::Implementation graphicsApi; |
| 38 |
#if defined(Q_OS_WIN) |
| 39 |
graphicsApi = QRhi::D3D11; |
| 40 |
#elif QT_CONFIG(metal) |
| 41 |
graphicsApi = QRhi::Metal; |
| 42 |
#elif QT_CONFIG(vulkan) |
| 43 |
graphicsApi = QRhi::Vulkan; |
| 44 |
#else |
| 45 |
graphicsApi = QRhi::OpenGLES2; |
| 46 |
#endif |
| 47 |
|
| 48 |
QCommandLineParser cmdLineParser; |
| 49 |
cmdLineParser.addHelpOption(); |
| 50 |
QCommandLineOption nullOption({ "n", "null" }, QLatin1String("Null")); |
| 51 |
cmdLineParser.addOption(nullOption); |
| 52 |
QCommandLineOption glOption({ "g", "opengl" }, QLatin1String("OpenGL (2.x)")); |
| 53 |
cmdLineParser.addOption(glOption); |
| 54 |
QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan")); |
| 55 |
cmdLineParser.addOption(vkOption); |
| 56 |
QCommandLineOption d3d11Option({ "d", "d3d11" }, QLatin1String("Direct3D 11")); |
| 57 |
cmdLineParser.addOption(d3d11Option); |
| 58 |
QCommandLineOption d3d12Option({ "D", "d3d12" }, QLatin1String("Direct3D 12")); |
| 59 |
cmdLineParser.addOption(d3d12Option); |
| 60 |
QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal")); |
| 61 |
cmdLineParser.addOption(mtlOption); |
| 62 |
|
| 63 |
cmdLineParser.process(app); |
| 64 |
if (cmdLineParser.isSet(nullOption)) |
| 65 |
graphicsApi = QRhi::Null; |
| 66 |
if (cmdLineParser.isSet(glOption)) |
| 67 |
graphicsApi = QRhi::OpenGLES2; |
| 68 |
if (cmdLineParser.isSet(vkOption)) |
| 69 |
graphicsApi = QRhi::Vulkan; |
| 70 |
if (cmdLineParser.isSet(d3d11Option)) |
| 71 |
graphicsApi = QRhi::D3D11; |
| 72 |
if (cmdLineParser.isSet(d3d12Option)) |
| 73 |
graphicsApi = QRhi::D3D12; |
| 74 |
if (cmdLineParser.isSet(mtlOption)) |
| 75 |
graphicsApi = QRhi::Metal; |
| 76 |
|
| 77 |
qDebug("Selected graphics API is %s", qPrintable(graphicsApiName(graphicsApi))); |
| 78 |
qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText())); |
| 79 |
|
| 80 |
QSurfaceFormat fmt; |
| 81 |
fmt.setDepthBufferSize(24); |
| 82 |
fmt.setStencilBufferSize(8); |
| 83 |
QSurfaceFormat::setDefaultFormat(fmt); |
| 84 |
|
| 85 |
#if QT_CONFIG(vulkan) |
| 86 |
QVulkanInstance inst; |
| 87 |
if (graphicsApi == QRhi::Vulkan) { |
| 88 |
inst.setLayers({ "VK_LAYER_KHRONOS_validation" }); |
| 89 |
inst.setExtensions(QRhiVulkanInitParams::preferredInstanceExtensions()); |
| 90 |
if (!inst.create()) { |
| 91 |
qWarning("Failed to create Vulkan instance, switching to OpenGL"); |
| 92 |
graphicsApi = QRhi::OpenGLES2; |
| 93 |
} |
| 94 |
} |
| 95 |
#endif |
| 96 |
|
| 97 |
HelloWindow w(graphicsApi); |
| 98 |
#if QT_CONFIG(vulkan) |
| 99 |
if (graphicsApi == QRhi::Vulkan) |
| 100 |
w.setVulkanInstance(&inst); |
| 101 |
#endif |
| 102 |
w.resize(1280, 720); |
| 103 |
w.setTitle(QCoreApplication::applicationName() + QLatin1String(" - ") + graphicsApiName(graphicsApi)); |
| 104 |
w.show(); |
| 105 |
|
| 106 |
int ret = app.exec(); |
| 107 |
|
| 108 |
// Window::event() will not get invoked when the |
| 109 |
// PlatformSurfaceAboutToBeDestroyed event is sent during the QWindow |
| 110 |
// destruction. That happens only when exiting via app::quit() instead of |
| 111 |
// the more common QWindow::close(). Take care of it: if the QPlatformWindow |
| 112 |
// is still around (there was no close() yet), get rid of the swapchain |
| 113 |
// while it's not too late. |
| 114 |
if (w.handle()) |
| 115 |
w.releaseSwapChain(); |
| 116 |
|
| 117 |
return ret; |
| 118 |
} |