From 2f632086e9a0bf5b3ae00102c9914e5c156a1e5c Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Thu, 9 Jan 2025 17:53:09 +0100 Subject: [PATCH] rhi: vulkan: Support initializing without a Vulkan instance given Problem is, this has potential issues down the line. For example, if one pops up a QQuickWindow afterwards, also with Vulkan, and wants to do things like enabling the debug layer (QSG_RHI_DEBUG_LAYER and similar), that will not work since the instance is already up. In absence of better solutions, drop a warning as well and keep this undocumented for now. On the plus side, this allows applications to implement things like enumerating adapters with Vulkan and then launching a QQuickWindow with the selected adapter. With other APIs (D3D) this is a breeze, but is impossible here due to the very unfortunate concept of Vulkan instances, and handles such as a VkPhysicalDevice being tied to the instance. An application has no way of knowing what VkInstance Qt Quick will create/use, unless the developer does the extra boilerplate to provide their own, which is an overkill in this case. So offer some way to make this work, even if there are some pitfalls. Change-Id: I1fc11f90cd1bf3fdac945ed5a7221f596368d30e Reviewed-by: Kristoffer Skau Reviewed-by: Andy Nichols (cherry picked from commit 3c338def89a4e03e3b47e624d83d83785c52037b) Reviewed-by: Qt Cherry-pick Bot --- src/gui/rhi/qrhivulkan.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/gui/rhi/qrhivulkan.cpp b/src/gui/rhi/qrhivulkan.cpp index 283b475ad10..4f6af6e41bf 100644 --- a/src/gui/rhi/qrhivulkan.cpp +++ b/src/gui/rhi/qrhivulkan.cpp @@ -23,6 +23,7 @@ QT_WARNING_POP #include #include #include +#include #include QT_BEGIN_NAMESPACE @@ -407,6 +408,15 @@ QRhiVulkan::QRhiVulkan(QRhiVulkanInitParams *params, QRhiVulkanNativeHandles *im : ofr(this) { inst = params->inst; + if (!inst) { + // This builds on the fact that Qt Quick also uses QVulkanDefaultInstance. While + // this way we can support a null inst, it has consequences, so only do it with a + // warning. (e.g. if Qt Quick initializes afterwards, its attempt to set flags on + // QVulkanDefaultInstance will be futile) + qWarning("QRhi for Vulkan attempted to be initialized without a QVulkanInstance; using QVulkanDefaultInstance."); + inst = QVulkanDefaultInstance::instance(); + } + maybeWindow = params->window; // may be null requestedDeviceExtensions = params->deviceExtensions;