Debian Codegraph @

tests/manual/rhi/hellominimalcrossgfxtriangle/main.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 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 // This is a compact, minimal demo of deciding the backend at runtime while
52 // using the exact same shaders and rendering code without any branching
53 // whatsoever once the QWindow is up and the RHI is initialized.
54
55 #include <QGuiApplication>
56 #include <QCommandLineParser>
57 #include "hellowindow.h"
58
59 QString graphicsApiName(QRhi::Implementation graphicsApi)
60 {
61 switch (graphicsApi) {
62 case QRhi::Null:
63 return QLatin1String("Null (no output)");
64 case QRhi::OpenGLES2:
65 return QLatin1String("OpenGL 2.x");
66 case QRhi::Vulkan:
67 return QLatin1String("Vulkan");
68 case QRhi::D3D11:
69 return QLatin1String("Direct3D 11");
70 case QRhi::Metal:
71 return QLatin1String("Metal");
72 default:
73 break;
74 }
75 return QString();
76 }
77
78 int main(int argc, char **argv)
79 {
80 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
81 QGuiApplication app(argc, argv);
82
83 QRhi::Implementation graphicsApi;
84 #if defined(Q_OS_WIN)
85 graphicsApi = QRhi::D3D11;
86 #elif defined(Q_OS_MACOS) || defined(Q_OS_IOS)
87 graphicsApi = QRhi::Metal;
88 #elif QT_CONFIG(vulkan)
89 graphicsApi = QRhi::Vulkan;
90 #else
91 graphicsApi = QRhi::OpenGLES2;
92 #endif
93
94 QCommandLineParser cmdLineParser;
95 cmdLineParser.addHelpOption();
96 QCommandLineOption nullOption({ "n", "null" }, QLatin1String("Null"));
97 cmdLineParser.addOption(nullOption);
98 QCommandLineOption glOption({ "g", "opengl" }, QLatin1String("OpenGL (2.x)"));
99 cmdLineParser.addOption(glOption);
100 QCommandLineOption vkOption({ "v", "vulkan" }, QLatin1String("Vulkan"));
101 cmdLineParser.addOption(vkOption);
102 QCommandLineOption d3dOption({ "d", "d3d11" }, QLatin1String("Direct3D 11"));
103 cmdLineParser.addOption(d3dOption);
104 QCommandLineOption mtlOption({ "m", "metal" }, QLatin1String("Metal"));
105 cmdLineParser.addOption(mtlOption);
106
107 cmdLineParser.process(app);
108 if (cmdLineParser.isSet(nullOption))
109 graphicsApi = QRhi::Null;
110 if (cmdLineParser.isSet(glOption))
111 graphicsApi = QRhi::OpenGLES2;
112 if (cmdLineParser.isSet(vkOption))
113 graphicsApi = QRhi::Vulkan;
114 if (cmdLineParser.isSet(d3dOption))
115 graphicsApi = QRhi::D3D11;
116 if (cmdLineParser.isSet(mtlOption))
117 graphicsApi = QRhi::Metal;
118
119 qDebug("Selected graphics API is %s", qPrintable(graphicsApiName(graphicsApi)));
120 qDebug("This is a multi-api example, use command line arguments to override:\n%s", qPrintable(cmdLineParser.helpText()));
121
122 QSurfaceFormat fmt;
123 fmt.setDepthBufferSize(24);
124 fmt.setStencilBufferSize(8);
125 QSurfaceFormat::setDefaultFormat(fmt);
126
127 #if QT_CONFIG(vulkan)
128 QVulkanInstance inst;
129 if (graphicsApi == QRhi::Vulkan) {
130 #ifndef Q_OS_ANDROID
131 inst.setLayers({ "VK_LAYER_LUNARG_standard_validation" });
132 #else
133 inst.setLayers({
134 "VK_LAYER_GOOGLE_threading",
135 "VK_LAYER_LUNARG_parameter_validation",
136 "VK_LAYER_LUNARG_object_tracker",
137 "VK_LAYER_LUNARG_core_validation",
138 "VK_LAYER_LUNARG_image",
139 "VK_LAYER_LUNARG_swapchain",
140 "VK_LAYER_GOOGLE_unique_objects"});
141 #endif
142 inst.setExtensions({ "VK_KHR_get_physical_device_properties2" });
143 if (!inst.create()) {
144 qWarning("Failed to create Vulkan instance, switching to OpenGL");
145 graphicsApi = QRhi::OpenGLES2;
146 }
147 }
148 #endif
149
150 HelloWindow w(graphicsApi);
151 #if QT_CONFIG(vulkan)
152 if (graphicsApi == QRhi::Vulkan)
153 w.setVulkanInstance(&inst);
154 #endif
155 w.resize(1280, 720);
156 w.setTitle(QCoreApplication::applicationName() + QLatin1String(" - ") + graphicsApiName(graphicsApi));
157 w.show();
158
159 int ret = app.exec();
160
161 // Window::event() will not get invoked when the
162 // PlatformSurfaceAboutToBeDestroyed event is sent during the QWindow
163 // destruction. That happens only when exiting via app::quit() instead of
164 // the more common QWindow::close(). Take care of it: if the QPlatformWindow
165 // is still around (there was no close() yet), get rid of the swapchain
166 // while it's not too late.
167 if (w.handle())
168 w.releaseSwapChain();
169
170 return ret;
171 }