Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_gui_vulkan_qvulkaninstance.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QGuiApplication>
5#include <QVulkanFunctions>
6#include <QVulkanInstance>
7#include <QWindow>
8
10
11struct Window {
12 void setVulkanInstance(QVulkanInstance *instance) { Q_UNUSED(instance); }
13 void show();
14};
15Window *window = nullptr;
16
17
18//! [0]
19 int main(int argc, char **argv)
20 {
21 QGuiApplication app(argc, argv);
22
23 QVulkanInstance inst;
24 if (!inst.create())
25 return 1;
26
27 // ...
28 window->setVulkanInstance(&inst);
30
31 return app.exec();
32 }
33//! [0]
34
35
36void wrapper0() {
37//! [1]
38 QVulkanInstance inst;
39
40 // Enable validation layer, if supported. Messages go to qDebug by default.
41 inst.setLayers({ "VK_LAYER_KHRONOS_validation" });
42
43 bool ok = inst.create();
44 if (!ok) {
45 // ... Vulkan not available
46 }
47
48 if (!inst.layers().contains("VK_LAYER_KHRONOS_validation")) {
49 // ... validation layer not available
50 }
51//! [1]
52}
53
54
55void wrapper1() {
56//! [2]
57 QVulkanInstance inst;
58
59 if (inst.supportedLayers().contains("VK_LAYER_KHRONOS_validation")) {
60 // ...
61 }
62 bool ok = inst.create();
63 // ...
64//! [2]
65
66Q_UNUSED(ok);
67} // wrapper1
68} // src_gui_vulkan_qvulkaninstance
69
70
72
73//! [3]
74class VulkanWindow : public QWindow
75{
76public:
78 setSurfaceType(VulkanSurface);
79 }
80
81 void exposeEvent(QExposeEvent *) {
82 if (isExposed()) {
83 if (!m_initialized) {
84 m_initialized = true;
85 // initialize device, swapchain, etc.
86 QVulkanInstance *inst = vulkanInstance();
87 QVulkanFunctions *f = inst->functions();
88 uint32_t devCount = 0;
89 f->vkEnumeratePhysicalDevices(inst->vkInstance(), &devCount, nullptr);
90 // ...
91 // build the first frame
93 }
94 }
95 }
96
97 bool event(QEvent *e) {
98 if (e->type() == QEvent::UpdateRequest)
100 return QWindow::event(e);
101 }
102
103 void render() {
104 // ...
105 requestUpdate(); // render continuously
106 }
107
108private:
109 bool m_initialized = false;
110};
111
112int main(int argc, char **argv)
113{
114 QGuiApplication app(argc, argv);
115
116 QVulkanInstance inst;
117 if (!inst.create()) {
118 qWarning("Vulkan not available");
119 return 1;
120 }
121
122 VulkanWindow window;
123 window.showMaximized();
124
125 return app.exec();
126
127}
128//! [3]
129
130
131} // src_gui_vulkan_qvulkaninstance2
bool event(QEvent *e)
Override this to handle any event (ev) sent to the window.
void exposeEvent(QExposeEvent *)
The expose event (ev) is sent by the window system when a window moves between the un-exposed and exp...