-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vulkan_Simulator.cpp
693 lines (562 loc) · 23.1 KB
/
Vulkan_Simulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Vulkan Simulator.cpp : Defines the entry point for the application.
//
#include "Vulkan_Simulator.h"
Q_LOGGING_CATEGORY(lcVk, "qt.vulkan")
VulkanWindow::VulkanWindow()
{
qDebug("VulkanWindow creator");
setSurfaceType(QSurface::VulkanSurface);
}
VulkanWindow::~VulkanWindow()
{
qDebug("VulkanWindow deletor");
delete mTimer;
release();
}
void VulkanWindow::release()
{
qDebug("release");
if (!mDevice) return;
vkDeviceWaitIdle(mDevice);
if (mCommandPool)
{
vkDestroyCommandPool(mDevice, mCommandPool, nullptr);
mCommandPool = VK_NULL_HANDLE;
}
if (mDevice)
{
vkDestroyDevice(mDevice, nullptr);
// Play nice and notify QVulkanInstance that the QVulkanDeviceFunctions
// for mDevice needs to be invalidated.
/// vulkanInstance()->resetDeviceFunctions(mDevice); // TODO: destroy everything before using this!
mDevice = VK_NULL_HANDLE;
}
mSurface = VK_NULL_HANDLE;
}
/// <summary>
///Returns a QMatrix4x4 that can be used to correct for coordinate
///system differences between OpenGL and Vulkan.
///
///By pre - multiplying the projection matrix with this matrix, applications can
///continue to assume that Y is pointing upwards, and can set minDepthand
///maxDepth in the viewport to 0 and 1, respectively, without having to do any
///further corrections to the vertex Z positions.Geometry from OpenGL
///applications can then be used as - is, assuming a rasterization state matching
///the OpenGL cullingand front face settings.
/// </summary>
/// <returns></returns>
QMatrix4x4 VulkanWindow::clipCorrectionMatrix()
{
if (mClipCorrect.isIdentity()) {
// The QMatrix creator takes row-major.
mClipCorrect = QMatrix4x4(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.5f,
0.0f, 0.0f, 0.0f, 1.0f);
}
return mClipCorrect;
}
void VulkanWindow::setupVulkanInstance(QVulkanInstance& instance) {
mQInstance = &instance;
mInstance = instance.vkInstance();
setVulkanInstance(&instance);
QByteArrayList extensions = { "VK_EXT_debug_utils" }; // This doesn't work. Function vkSetDebugUtilsObjectNameEXT remains a null pointer.
instance.setExtensions(extensions);
// Get window and Vulkan instance functions.
mVulkanPointers.pVulkanWindow = this;
mVulkanPointers.pVulkanFunctions = vulkanInstance()->functions();
mVulkanPointers.pInstance = vulkanInstance();
// Let user select a glTF file to be shown.
QString filename = QFileDialog::getOpenFileName(
nullptr,
QObject::tr("Open glTF file"),
#ifdef __linux__
"/home",
#elif _WIN32
"C:\\Users\\Public",
#else
QDir::currentPath(),
#endif
QObject::tr("glTF files (*.gltf)"));
mVulkanPointers.path = filename.toStdString();
// Setup Vulkan device, physical device, queues and command pool.
init();
// Load glTF file and setup other needed Vulkan stuff.
initResources();
// Start VulkanWindow refresh timer. There seems not to be a possibility to use std::unique_ptr
// with QTimer, so let's use the good old wintage methods new and delete.
mTimer = new QTimer();
connect(mTimer, &QTimer::timeout, this, &VulkanWindow::refresh);
mTimer->start(50);
}
/// <summary>
/// This helper function initializes necessary components inherently coupled with VkInstace:
/// device, physical device, queues and command pool.
/// </summary>
void VulkanWindow::init()
{
qDebug("init");
QVulkanInstance* inst = vulkanInstance();
mSurface = QVulkanInstance::surfaceForWindow(this);
if (!mSurface) qFatal("Failed to get surface for window.");
// Enumerate Vulkan physical devices.
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(mInstance, &deviceCount, nullptr);
if (deviceCount == 0) qFatal("No physical Vulkan devices.");
QVector<VkPhysicalDevice> physDevices(deviceCount);
VkResult err = vkEnumeratePhysicalDevices(mInstance, &deviceCount, physDevices.data());
if (err != VK_SUCCESS && err != VK_INCOMPLETE)
qFatal("Failed to enumerate Vulkan physical devices: %d", err);
// Select suitable physical device.
int integrated = -1;
int discrete = -1;
for (int i = 0; i < deviceCount; i++) {
VkPhysicalDeviceProperties physDeviceProps;
vkGetPhysicalDeviceProperties(physDevices[i], &physDeviceProps);
if (physDeviceProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && discrete == -1) {
discrete = i;
qDebug("Discrete device name: %s Driver version: %d.%d.%d",
physDeviceProps.deviceName,
VK_API_VERSION_MAJOR(physDeviceProps.driverVersion),
VK_API_VERSION_MINOR(physDeviceProps.driverVersion),
VK_API_VERSION_PATCH(physDeviceProps.driverVersion));
}
if (physDeviceProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU && integrated == -1) {
integrated = i;
qDebug("Integrated device name: %s Driver version: %d.%d.%d",
physDeviceProps.deviceName,
VK_API_VERSION_MAJOR(physDeviceProps.driverVersion),
VK_API_VERSION_MINOR(physDeviceProps.driverVersion),
VK_API_VERSION_PATCH(physDeviceProps.driverVersion));
}
}
mPhysDevice = physDevices[0];
if (integrated != -1) mPhysDevice = physDevices[integrated];
if (discrete != -1) mPhysDevice = physDevices[discrete];
// Setup function pointers regarding to the physical device.
initPhysDeviceFunctions();
// Graphics drivers executes commands asychronously along queues, which themselves are grouped
// into queue families. We need to select graphics queue and presentation queue.
uint32_t queueCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(mPhysDevice, &queueCount, nullptr);
QVector<VkQueueFamilyProperties> queueFamilyProps(queueCount);
vkGetPhysicalDeviceQueueFamilyProperties(mPhysDevice, &queueCount,
queueFamilyProps.data());
int graphicsQueueFamilyIndex = -1;
int presentQueueFamilyIndex = -1;
// First look for a queue family that supports both.
for (int i = 0; i < queueFamilyProps.count(); ++i)
{
if (graphicsQueueFamilyIndex == -1 &&
(queueFamilyProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
inst->supportsPresent(mPhysDevice, i, this))
graphicsQueueFamilyIndex = i;
}
if (graphicsQueueFamilyIndex != -1) {
presentQueueFamilyIndex = graphicsQueueFamilyIndex;
}
else {
// Separate queues then.
qDebug("No queue with graphics+present; Trying separate queues.");
for (int i = 0; i < queueFamilyProps.count(); ++i) {
if (graphicsQueueFamilyIndex == -1 &&
(queueFamilyProps[i].queueFlags & VK_QUEUE_GRAPHICS_BIT))
graphicsQueueFamilyIndex = i;
if (presentQueueFamilyIndex == -1 &&
inst->supportsPresent(mPhysDevice, i, this))
presentQueueFamilyIndex = i;
}
}
if (graphicsQueueFamilyIndex == -1)
qFatal("No graphics queue family found");
if (presentQueueFamilyIndex == -1)
qFatal("No present queue family found");
// Create logical device.
VkDeviceQueueCreateInfo queueInfo[2];
const float prio[] = { 0 };
memset(queueInfo, 0, sizeof(queueInfo));
queueInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueInfo[0].queueFamilyIndex = graphicsQueueFamilyIndex;
queueInfo[0].queueCount = 1;
queueInfo[0].pQueuePriorities = prio;
if (graphicsQueueFamilyIndex != presentQueueFamilyIndex) {
queueInfo[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueInfo[1].queueFamilyIndex = presentQueueFamilyIndex;
queueInfo[1].queueCount = 1;
queueInfo[1].pQueuePriorities = prio;
}
QVector<const char*> deviceExtensions;
deviceExtensions.append("VK_KHR_swapchain");
VkDeviceCreateInfo devInfo{};
devInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
devInfo.queueCreateInfoCount = graphicsQueueFamilyIndex == presentQueueFamilyIndex ? 1 : 2;
devInfo.pQueueCreateInfos = queueInfo;
devInfo.enabledLayerCount = ValidationLayers.size();
devInfo.ppEnabledLayerNames = ValidationLayers.data();
devInfo.enabledExtensionCount = deviceExtensions.count();
devInfo.ppEnabledExtensionNames = deviceExtensions.constData();
err = vkCreateDevice(mPhysDevice, &devInfo, nullptr, &mDevice);
if (err != VK_SUCCESS) qFatal("Failed to create logical device: %d", err);
// Setup function pointers regarding to the logical device.
initDeviceFunctions();
// Create command pool.
vkGetDeviceQueue(mDevice, graphicsQueueFamilyIndex, 0, &mGraphicsQueue);
if (graphicsQueueFamilyIndex == presentQueueFamilyIndex) {
mPresentQueue = mGraphicsQueue;
}
else {
vkGetDeviceQueue(mDevice, presentQueueFamilyIndex, 0, &mPresentQueue);
}
}
void VulkanWindow::initDeviceFunctions() {
vkCreateSwapchainKHR = reinterpret_cast<PFN_vkCreateSwapchainKHR>(
vkGetDeviceProcAddr(mDevice, "vkCreateSwapchainKHR"));
vkDestroySwapchainKHR = reinterpret_cast<PFN_vkDestroySwapchainKHR>(
vkGetDeviceProcAddr(mDevice, "vkDestroySwapchainKHR"));
vkGetSwapchainImagesKHR = reinterpret_cast<PFN_vkGetSwapchainImagesKHR>(
vkGetDeviceProcAddr(mDevice, "vkGetSwapchainImagesKHR"));
vkAcquireNextImageKHR = reinterpret_cast<PFN_vkAcquireNextImageKHR>(
vkGetDeviceProcAddr(mDevice, "vkAcquireNextImageKHR"));
vkQueuePresentKHR = reinterpret_cast<PFN_vkQueuePresentKHR>(
vkGetDeviceProcAddr(mDevice, "vkQueuePresentKHR"));
}
void VulkanWindow::initPhysDeviceFunctions() {
vkGetPhysicalDeviceSurfaceCapabilitiesKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR>(
vkGetInstanceProcAddr(mInstance,
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR"));
vkGetPhysicalDeviceSurfaceFormatsKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR>(
vkGetInstanceProcAddr(mInstance, "vkGetPhysicalDeviceSurfaceFormatsKHR"));
vkGetPhysicalDeviceSurfacePresentModesKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR>(
vkGetInstanceProcAddr(mInstance, "vkGetPhysicalDeviceSurfacePresentModesKHR"));
vkGetPhysicalDeviceQueueFamilyProperties =
reinterpret_cast<PFN_vkGetPhysicalDeviceQueueFamilyProperties>(
vkGetInstanceProcAddr(mInstance, "vkGetPhysicalDeviceQueueFamilyProperties"));
vkGetPhysicalDeviceSurfaceSupportKHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceSupportKHR>(
vkGetInstanceProcAddr(mInstance, "vkGetPhysicalDeviceSurfaceSupportKHR"));
vkGetPhysicalDeviceFeatures =
reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures>(
vkGetInstanceProcAddr(mInstance, "vkGetPhysicalDeviceFeatures"));
vkEnumerateDeviceExtensionProperties =
reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
vkGetInstanceProcAddr(mInstance, "vkEnumerateDeviceExtensionProperties"));
}
void VulkanWindow::refresh() {
if (!mStart && mInitialized)
mRenderer->render();
}
void VulkanWindow::resizeEvent(QResizeEvent*) {
// TODO: Implement this.
}
void VulkanWindow::exposeEvent(QExposeEvent*) {
if (isExposed() && !mInitialized)
{
mInitialized = true;
initSwapChainResources();
mRenderer->render();
}
// Release everything when unexposed - the meaning of which is platform
// specific. Can be essential on mobile, to release resources while in
// background.
if (!isExposed() && mInitialized) {
mInitialized = false;
releaseSwapChainResources();
releaseResources();
}
}
bool VulkanWindow::event(QEvent* e) {
if (mRenderer != nullptr && mStart == true) {
mStart = false;
initSwapChainResources();
mRenderer->render();
}
switch (e->type())
{
case QEvent::UpdateRequest:
mRenderer->render();
break;
// The swapchain must be destroyed before the surface as per spec.
// This is not ideal for us because the surface is managed by the
// QPlatformWindow which may be gone already when the unexpose comes, making
// the validation layer scream. The solution is to listen to the
// PlatformSurface events.
case QEvent::PlatformSurface: {
auto* ev = static_cast<QPlatformSurfaceEvent*>(e);
if (ev->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
releaseSwapChainResources();
releaseResources();
}
break;
}
default:
break;
}
return QWindow::event(e);
}
/// <summary>
/// This helper function loads glTF file and creates Renderer class instance, which sets the most
/// of Vulkan stuff. Before calling this function call init() first! Also pVulkanWindow and
/// pInstance as well as glTF file path need to be set before.
/// </summary>
void VulkanWindow::initResources()
{
qDebug("initResources");
// Get device and Vulkan device functions.
mVulkanPointers.device = mVulkanPointers.pVulkanWindow->device();
mVulkanPointers.physicalDevice = mVulkanPointers.pVulkanWindow->physicalDevice();
mVulkanPointers.pDeviceFunctions = mVulkanPointers.pVulkanWindow->vulkanInstance()->
deviceFunctions(mVulkanPointers.pVulkanWindow->device());
mVulkanPointers.surface = mVulkanPointers.pInstance->surfaceForWindow(
mVulkanPointers.pVulkanWindow);
// Load 3D model into memory.
if (mFileReader == nullptr) {
mFileReader = std::make_unique<FileReader>();
}
if (!mFileReader->loadFile(mVulkanPointers.path))
qFatal("Couldn't load a file!");
// Create renderer and init all the permanent parts of the renderer.
mRenderer = std::make_unique<Renderer>(Renderer(mVulkanPointers));
mRenderer->createVertexBuffer(*mFileReader->getModel());
mRenderer->setViewMatrix();
mRenderer->setModelMatrix();
mRenderer->createUniformBuffers();
mRenderer->createGraphicsPipeline();
mRenderer->createSyncObjects();
}
/// <summary>
/// This helper function creates the parts of Vulkan which may be destroyed and recreated during
/// the application lifetime: swapchain, imageviews and renderpass.
/// </summary>
void VulkanWindow::initSwapChainResources()
{
qDebug("initSwapChainResources");
// Every window recreate need to set projection matrix in a case user resizes window.
// Use this...
QMatrix4x4 proj1 = clipCorrectionMatrix();
const QSize size = swapChainImageSize();
proj1.perspective(45.0f, size.width() / (float)size.height(), 0.1f, 1000.0f);
// Perspective projection parameters.
// ...or this.
float fov = 45.0f;
float aspectRatio = size.width() / (float)size.height();
float nearZ = 0.1f;
float farZ = 1000.0f;
float top = nearZ * std::tan(M_PI / 180 * fov / 2.0);
float bottom = -top;
float right = top * aspectRatio;
float left = -right;
QMatrix4x4 proj2 = QMatrix4x4(2 * nearZ / (right - left), 0, (right + left) / (right - left), 0,
0, 2 * nearZ / (top - bottom), (top + bottom) / (top - bottom), 0,
0, 0, -(farZ + nearZ) / (farZ - nearZ), -(2 * farZ * nearZ) / (farZ - nearZ),
0, 0, -1, 0);
proj2 = proj2 * clipCorrectionMatrix();
mRenderer->setProjectionMatrix(proj2.data());
// Then create swap chain.
mRenderer->createSwapChain(nullptr, nullptr, mRenderer->getSwapChain());
}
void VulkanWindow::releaseSwapChainResources()
{
qDebug("releaseSwapChainResources");
// It is important to finish the pending frame right here since this is the
// last opportunity to act with all resources intact.
vkDeviceWaitIdle(mDevice);
mRenderer->deleteSwapChain();
}
void VulkanWindow::releaseResources()
{
qDebug("releaseResources");
// Before releasing resources it is important there aren't any pending work going.
mVulkanPointers.pDeviceFunctions->vkDeviceWaitIdle(mVulkanPointers.device);
mRenderer->deleteVertexBuffer();
mRenderer->deleteUniformBuffers();
mRenderer->deleteGraphicsPipeline();
mRenderer->deleteSwapChain();
mRenderer->deleteSyncObjects();
}
VkInstance VulkanWindow::createInstance() {
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Vulkan Simulator";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = nullptr;//"No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo instInfo{};
instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instInfo.pApplicationInfo = &appInfo;
QVector<const char*> reqExtensions = getRequiredExtensions();
instInfo.enabledExtensionCount = reqExtensions.size();
instInfo.ppEnabledExtensionNames = reqExtensions.data();
if (enableValidationLayers)
{
instInfo.enabledLayerCount = ValidationLayers.size();
instInfo.ppEnabledLayerNames = ValidationLayers.data();
}
else {
instInfo.enabledLayerCount = 0;
}
VkInstance instance;
if (vkCreateInstance(&instInfo, nullptr, &instance) == VK_SUCCESS)
return instance;
qFatal("Failed to create Vulkan instance!");
return VK_NULL_HANDLE;
}
QVector<const char*> VulkanWindow::getRequiredExtensions() {
QVector<const char*> extensions;
extensions.append(VK_KHR_SURFACE_EXTENSION_NAME);// "VK_KHR_surface"
// Vulkan surface types:
//VK_KHR_win32_surface
//VK_KHR_wayland_surface
//VK_KHR_xcb_surface
//VK_KHR_xlib_surface
//VK_KHR_android_surface
//VK_MVK_macos_surface
//VK_MVK_ios_surface
#ifdef _WIN32
extensions.append("VK_KHR_win32_surface");
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
mtl_line();
#elif TARGET_OS_IPHONE
// iOS device
extensions.append("VK_MVK_ios_surface");
#elif TARGET_OS_MAC
// Other kinds of Mac OS
extensions.append("VK_MVK_macos_surface");
#else
qWarning("Unknown Apple platform");
#endif
#else
extensions.append(pickLinuxSurfaceExtension());
#endif
if (enableValidationLayers)
extensions.append(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
return extensions;
}
LinuxDisplayType VulkanWindow::getLinuxDisplayType() {
// Inspired from:
// stackoverflow.com/questions/45536141/how-i-can-find-out-if-a-linux-system-uses-wayland-or-x11
auto env = QProcessEnvironment::systemEnvironment();
QString value = env.value(QLatin1String("WAYLAND_DISPLAY"));
if (!value.isEmpty())
return LinuxDisplayType::Wayland;
value = env.value(QLatin1String("DISPLAY"));
if (!value.isEmpty())
return LinuxDisplayType::X11;
qWarning("Unknown Linux display type");
return LinuxDisplayType::None;
}
const char* VulkanWindow::pickLinuxSurfaceExtension() {
#ifdef __ANDROID__
return "VK_KHR_android_surface";
#endif
const auto display = getLinuxDisplayType();
if (display == LinuxDisplayType::Wayland)
return "VK_KHR_wayland_surface";
if (display == LinuxDisplayType::X11)
return "VK_KHR_xcb_surface";
qWarning("No Linux surface extension");
return nullptr;
}
/// <summary>
/// For Vulkan debugging we need some layers installed. Global vector ValidationLayers
/// tells what layers we want to install. CheckValidationLayerSupport checks are those
/// layers available.
/// </summary>
/// <param name="inst">Optional QVulkanInstance parameter.</param>
/// <returns></returns>
bool CheckValidationLayerSupport(QVulkanInstance* inst = nullptr) {
if (inst != nullptr) {
// First enumerate available layers.
QVulkanInfoVector<QVulkanLayer> layers = inst->supportedLayers();
// Check if there are the layers we want among availabale layers.
for (const char* layerName : ValidationLayers) {
bool layerFound = false;
for (const auto& layer : layers) {
if (strcmp(layerName, layer.name) == 0) {
layerFound = true;
break;
}
}
if (!layerFound) {
return false;
}
}
}
else {
// First enumerate available layers.
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
std::vector<VkLayerProperties> availableLayers(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
// Check if there are the layers we want among available layers.
for (const char* layerName : ValidationLayers) {
bool layerFound = false;
for (const auto& layerProperties : availableLayers) {
if (strcmp(layerName, layerProperties.layerName) == 0) {
layerFound = true;
break;
}
}
if (!layerFound) {
return false;
}
}
}
return true;
}
int main(int argc, char* argv[])
{
// Let's get additional Qt diagnosis data to the Linux Console Window.
// This affects only for a Linux build.
qputenv("QT_DEBUG_PLUGINS", QByteArray("1"));
// QApplication is the child class of QGuiApplication which has base class QCoreApplication.
// We need QApplication because we want to use both QWidgets and QVulkanInstance.
QApplication app(argc, argv);
QLoggingCategory::setFilterRules(QStringLiteral("qt.vulkan=true"));
// Create a window to render into.
VulkanWindow w;
w.resize(1024, 768);
w.show();
// If we are running on debug mode, create Vulkan validation layers.
QVulkanInstance inst;
if (enableValidationLayers && !CheckValidationLayerSupport(&inst)) {
qWarning("Validation layers requested, but not available!");
}
else {
qInfo("Validation layers requested.");
QList<QByteArray> temp;
temp.reserve(ValidationLayers.size());
for (int i = 0; i < ValidationLayers.size(); i++) {
temp.append(ValidationLayers[i]);
}
inst.setLayers(temp);
}
// Install needed Vulkan extensions.
auto extra = w.getRequiredExtensions();
QByteArrayList list;
for (const auto& next : extra) list.append(next);
inst.setExtensions(list);
// Assing our own self created Vulkan instance for QVulkanInstance so that it doesn't
// make its' default instance.
VkInstance instance = w.createInstance();
if (instance == VK_NULL_HANDLE)
{
qFatal("Failed to create VkInstance!");
}
inst.setVkInstance(instance);
// Every Vulkan-based QVulkanWindow must be associated with a QVulkanInstance. Our custom
// VulkanWindow does not make a difference with default QVulkanWindow, so let's create one.
if (!inst.create())
qFatal("Failed to create QVulkanInstance: %d", inst.errorCode());
// Connect QVulkanInstance with VulkanWindow. This also loads other Vulkan stuff and let
// user choose gltf file to show on window.
w.setupVulkanInstance(inst);
// Finally start application.
return app.exec();
}