QVulkanInstance Class
The QVulkanInstance class represents a native Vulkan instance, enabling Vulkan rendering onto a QSurface. More...
Header: | #include <QVulkanInstance> |
qmake: | QT += gui |
Since: | Qt 5.10 |
This class was introduced in Qt 5.10.
Public Types
typedef | DebugFilter |
enum | Flag { NoDebugOutputRedirect } |
flags | Flags |
Public Functions
QVulkanInstance() | |
~QVulkanInstance() | |
QVersionNumber | apiVersion() const |
bool | create() |
void | destroy() |
int | errorCode() const |
QByteArrayList | extensions() const |
QVulkanInstance::Flags | flags() const |
QVulkanFunctions * | functions() const |
PFN_vkVoidFunction | getInstanceProcAddr(const char *name) |
void | installDebugOutputFilter(QVulkanInstance::DebugFilter filter) |
bool | isValid() const |
QByteArrayList | layers() const |
void | presentQueued(QWindow *window) |
void | removeDebugOutputFilter(QVulkanInstance::DebugFilter filter) |
void | setApiVersion(const QVersionNumber &vulkanVersion) |
void | setExtensions(const QByteArrayList &extensions) |
void | setFlags(QVulkanInstance::Flags flags) |
void | setLayers(const QByteArrayList &layers) |
QVulkanInfoVector<QVulkanExtension> | supportedExtensions() |
QVulkanInfoVector<QVulkanLayer> | supportedLayers() |
int | vkInstance() const |
Static Public Members
VkSurfaceKHR | surfaceForWindow(QWindow *window) |
Detailed Description
Vulkan is a cross-platform, explicit graphics and compute API. This class provides support for loading a Vulkan library and creating an instance
in a cross-platform manner. For an introduction on Vulkan instances, refer to section 3.2 of the specification.
Note: Platform-specific support for Vulkan instances and windows with Vulkan-capable surfaces is provided by the various platform plugins. Not all of them will support Vulkan, however. When running on such a platform, create() will fail and always return false
.
Note: Vulkan support may get automatically disabled for a given Qt build due to not having the necessary Vulkan headers available at build time. When this is the case, and the output of configure
indicates Vulkan support is disabled, the QVulkan* classes will be unavailable.
Note: Some functions changed their signature between the various Vulkan header revisions. When building Qt and only headers with the old, conflicting signatures are present in a system, Vulkan support will get disabled. It is recommended to use headers from Vulkan 1.0.39 or newer.
Initialization
Similarly to QOpenGLContext, any actual Vulkan instance creation happens only when calling create(). This allows using QVulkanInstance as a plain member variable while retaining control over when to perform initialization.
Querying the supported instance-level layers and extensions is possible by calling supportedLayers() and supportedExtensions(). These ensure the Vulkan library is loaded, and can therefore be called safely before create() as well.
Instances store per-application Vulkan state and creating a VkInstance
object initializes the Vulkan library. In practice there will typically be a single instance constructed early on in main(). The object then stays alive until exiting the application.
Every Vulkan-based QWindow must be associated with a QVulkanInstance by calling QWindow::setVulkanInstance(). Thus a typical application pattern is the following:
int main(int argc, char **argv) { QGuiApplication app(argc, argv); QVulkanInstance inst; if (!inst.create()) return 1; ... window->setVulkanInstance(&inst); window->show(); return app.exec(); }
Configuration
QVulkanInstance automatically enables the minimum set of extensions it needs on the newly created instance. In practice this means the VK_KHR_*_surface
family of extensions.
By default Vulkan debug output, for example messages from the validation layers, is routed to qDebug(). This can be disabled by passing the flag NoDebugOutputRedirect
to setFlags() before invoking create().
To enable additional layers and extensions, provide the list via setLayers() and setExtensions() before invoking create(). When a given layer or extension is not reported as available from the instance, the request is ignored. After a successful call to create(), the values returned from functions like layers() and extensions() reflect the actual enabled layers and extensions. When necessary, for example to avoid requesting extensions that conflict and thus would fail the Vulkan instance creation, the list of actually supported layers and extensions can be examined via supportedLayers() and supportedExtensions() before calling create().
For example, to enable the standard validation layers, one could do the following:
QVulkanInstance inst; // Enable validation layer, if supported. Messages go to qDebug by default. inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation"); bool ok = inst.create(); if (!ok) ... // Vulkan not available if (!inst.layers().contains("VK_LAYER_LUNARG_standard_validation")) ... // validation layer not available
Or, alternatively, to make decisions before attempting to create a Vulkan instance:
QVulkanInstance inst; if (inst.supportedLayers().contains("VK_LAYER_LUNARG_standard_validation")) ... bool ok = inst.create(); ...
Adopting an Existing Instance
By default QVulkanInstance creates a new Vulkan instance. When working with external engines and renderers, this may sometimes not be desirable. When there is a VkInstance
handle already available, call setVkInstance() before invoking create(). This way no additional instances will get created, and QVulkanInstance will not own the handle.
Note: It is up to the component creating the external instance to ensure the necessary extensions are enabled on it. These are: VK_KHR_surface
, the WSI-specific VK_KHR_*_surface
that is appropriate for the platform in question, and VK_EXT_debug_report
in case QVulkanInstance's debug output redirection is desired.
Accessing Core Vulkan Commands
To access the VkInstance
handle the QVulkanInstance wraps, call vkInstance(). To resolve Vulkan functions, call getInstanceProcAddr(). For core Vulkan commands manual resolving is not necessary as they are provided via the QVulkanFunctions and QVulkanDeviceFunctions objects accessible via functions() and deviceFunctions().
Note: QVulkanFunctions and QVulkanDeviceFunctions are generated from the Vulkan API XML specifications when building the Qt libraries. Therefore no documentation is provided for them. They contain the Vulkan 1.0 functions with the same signatures as described in the Vulkan API documentation.
Getting a Native Vulkan Surface for a Window
The two common windowing system specific operations are getting a surface (a VkSurfaceKHR
handle) for a window, and querying if a given queue family supports presenting to a given surface. To avoid WSI-specific bits in the applications, these are abstracted by QVulkanInstance and the underlying QPA layers.
To create a Vulkan surface for a window, or retrieve an existing one, call surfaceForWindow(). Most platforms will only create the surface via VK_KHR_*_surface
when first calling surfaceForWindow(), but there may be platform-specific variations in the internal behavior. Once created, subsequent calls to surfaceForWindow() just return the same handle. This fits the structure of typical Vulkan-enabled QWindow subclasses well.
To query if a given queue family within a physical device can be used to perform presentation to a given surface, call supportsPresent(). This encapsulates both the generic vkGetPhysicalDeviceSurfaceSupportKHR
and the WSI-specific vkGetPhysicalDevice*PresentationSupportKHR
checks.
Troubleshooting
Besides returning false
from create() or 0
from surfaceForWindow(), critical errors will also get printed to the debug output via qWarning(). Additional logging can be requested by enabling debug output for the logging category qt.vulkan
. The actual Vulkan error code from instance creation can be retrieved by calling errorCode() after a failing create().
In some special cases it may be necessary to override the Vulkan library name. This can be achieved by setting the QT_VULKAN_LIB
environment variable.
Example
The following is the basic outline of creating a Vulkan-capable QWindow:
class VulkanWindow : public QWindow { public: VulkanWindow() { setSurfaceType(VulkanSurface); } void exposeEvent(QExposeEvent *) { if (isExposed()) { if (!m_initialized) { m_initialized = true; // initialize device, swapchain, etc. QVulkanInstance *inst = vulkanInstance(); QVulkanFunctions *f = inst->functions(); uint32_t devCount = 0; f->vkEnumeratePhysicalDevices(inst->vkInstance(), &devCount, nullptr); ... // build the first frame render(); } } } bool event(QEvent *e) { if (e->type() == QEvent::UpdateRequest) render(); return QWindow::event(e); } void render() { ... requestUpdate(); // render continuously } private: bool m_initialized = false; }; int main(int argc, char **argv) { QGuiApplication app(argc, argv); QVulkanInstance inst; if (!inst.create()) { qWarning("Vulkan not available"); return 1; } VulkanWindow window; window.showMaximized(); return app.exec(); }
Note: In addition to expose, a well-behaving window implementation will also have to take care of additional events like resize and QPlatformSurfaceEvent in order to ensure proper management of the swap chain. Additionally, some platforms may require releasing resources when not being exposed anymore.
Using C++ Bindings for Vulkan
Combining Qt's Vulkan enablers with a C++ Vulkan wrapper, for example Vulkan-Hpp, is possible as well. The pre-requisite here is that the C++ layer must be able to adopt native handles (VkInstance, VkSurfaceKHR) in its classes without taking ownership (since the ownership stays with QVulkanInstance and QWindow). Consider also the following:
- Some wrappers require exception support to be enabled. Qt does not use exceptions. To enable exceptions for the application, add
CONFIG += exceptions
to the.pro
file. - Some wrappers call Vulkan functions directly, assuming
vulkan.h
provides prototypes and the application links to a Vulkan library exporting all necessary symbols. Qt may not directly link to a Vulkan library. Therefore, on some platforms it may be necessary to addLIBS += -lvulkan
or similar in the application's.pro
file. - The headers for the QVulkan classes may include
vulkan.h
withVK_NO_PROTOTYPES
enabled. This can cause issues in C++ wrapper headers that rely on the prototypes. Hence in application code it may be necessary to includevulkan.hpp
or similar before any of the QVulkan headers.
See also QVulkanFunctions and QSurface::SurfaceType.
Member Type Documentation
typedef QVulkanInstance::DebugFilter
Typedef for debug filtering callback functions.
See also installDebugOutputFilter() and removeDebugOutputFilter().
enum QVulkanInstance::Flag
flags QVulkanInstance::Flags
This enum describes the flags that can be passed to setFlags(). These control the behavior of create().
Constant | Value | Description |
---|---|---|
QVulkanInstance::NoDebugOutputRedirect | 0x01 | Disables Vulkan debug output (VK_EXT_debug_report ) redirection to qDebug. |
This enum was introduced or modified in Qt 5.10.
The Flags type is a typedef for QFlags<Flag>. It stores an OR combination of Flag values.
Member Function Documentation
QVulkanInstance::QVulkanInstance()
Constructs a new instance.
Note: No Vulkan initialization is performed in the constructor.
QVulkanInstance::~QVulkanInstance()
Destructor.
Note: current() will return nullptr
once the instance is destroyed.
QVersionNumber QVulkanInstance::apiVersion() const
Returns the requested Vulkan API version against which the application expects to run, or a null version number if setApiVersion() was not called before create().
See also setApiVersion().
bool QVulkanInstance::create()
Initializes the Vulkan library and creates a new or adopts and existing Vulkan instance.
Returns true if successful, false on error or when Vulkan is not supported.
When successful, the pointer to this QVulkanInstance is retrievable via the static function current().
The Vulkan instance and library is available as long as this QVulkanInstance exists, or until destroy() is called.
void QVulkanInstance::destroy()
Destroys the underlying platform instance, thus destroying the VkInstance (when owned). The QVulkanInstance object is still reusable by calling create() again.
int QVulkanInstance::errorCode() const
Returns the Vulkan error code after an unsuccessful create(), VK_SUCCESS
otherwise.
The value is typically the return value from vkCreateInstance() (when creating a new Vulkan instance instead of adopting an existing one), but may also be VK_NOT_READY
if the platform plugin does not support Vulkan.
QByteArrayList QVulkanInstance::extensions() const
Returns the enabled instance extensions, if create() was called and was successful. The requested extensions otherwise.
See also setExtensions().
QVulkanInstance::Flags QVulkanInstance::flags() const
Returns the requested flags.
See also setFlags().
QVulkanFunctions *QVulkanInstance::functions() const
Returns the corresponding QVulkanFunctions object that exposes the core Vulkan command set, excluding device level functions, and is guaranteed to be functional cross-platform.
Note: The returned object is owned and managed by the QVulkanInstance. Do not destroy or alter it.
See also deviceFunctions().
PFN_vkVoidFunction QVulkanInstance::getInstanceProcAddr(const char *name)
Resolves the Vulkan function with the given name.
For core Vulkan commands prefer using the function wrappers retrievable from functions() and deviceFunctions() instead.
void QVulkanInstance::installDebugOutputFilter(QVulkanInstance::DebugFilter filter)
Installs a filter function that is called for every Vulkan debug message. When the callback returns true
, the message is stopped (filtered out) and will not appear on the debug output.
Note: Filtering is only effective when NoDebugOutputRedirect is not set. Installing filters has no effect otherwise.
Note: This function can be called before create().
See also removeDebugOutputFilter().
bool QVulkanInstance::isValid() const
Returns true if create() was successful and the instance is valid.
QByteArrayList QVulkanInstance::layers() const
Returns the enabled instance layers, if create() was called and was successful. The requested layers otherwise.
See also setLayers().
void QVulkanInstance::presentQueued(QWindow *window)
This function should be called by the application's renderer after queuing a present operation for window.
While on some platforms this will be a no-op, some may perform windowing system dependent synchronization. For example, on X11 this will update _NET_WM_SYNC_REQUEST_COUNTER
.
void QVulkanInstance::removeDebugOutputFilter(QVulkanInstance::DebugFilter filter)
Removes a filter function previously installed by installDebugOutputFilter().
Note: This function can be called before create().
See also installDebugOutputFilter().
void QVulkanInstance::setApiVersion(const QVersionNumber &vulkanVersion)
Specifies the Vulkan API against which the application expects to run.
By default no vulkanVersion is specified, and so no version check is performed during Vulkan instance creation.
Note: This function can only be called before create() and has no effect if called afterwards.
See also apiVersion().
void QVulkanInstance::setExtensions(const QByteArrayList &extensions)
Specifies the list of additional instance extensions to enable. It is safe to specify unsupported extensions as well because these get ignored when not supported at run time. The surface-related extensions required by Qt will always be added automatically, no need to include them in this list.
Note: This function can only be called before create() and has no effect if called afterwards.
See also extensions().
void QVulkanInstance::setFlags(QVulkanInstance::Flags flags)
Configures the behavior of create() based on the provided flags.
Note: This function can only be called before create() and has no effect if called afterwards.
See also flags().
void QVulkanInstance::setLayers(const QByteArrayList &layers)
Specifies the list of instance layers to enable. It is safe to specify unsupported layers as well because these get ignored when not supported at run time.
Note: This function can only be called before create() and has no effect if called afterwards.
See also layers().
QVulkanInfoVector<QVulkanExtension> QVulkanInstance::supportedExtensions()
Returns the list of supported instance-level extensions.
Note: This function can be called before create().
QVulkanInfoVector<QVulkanLayer> QVulkanInstance::supportedLayers()
Returns the list of supported instance-level layers.
Note: This function can be called before create().
[static]
VkSurfaceKHR QVulkanInstance::surfaceForWindow(QWindow *window)
Creates or retrieves the already existing VkSurfaceKHR
handle for the given window.
Returns the Vulkan surface handle or 0 when failed.
int QVulkanInstance::vkInstance() const
Returns the VkInstance handle this QVulkanInstance wraps, or nullptr
if create() has not yet been successfully called and no existing instance has been provided via setVkInstance().
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.