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()
QVulkanDeviceFunctions *deviceFunctions(VkDevice device)
VkResult errorCode() const
QByteArrayList extensions() const
Flags flags() const
QVulkanFunctions *functions() const
PFN_vkVoidFunction getInstanceProcAddr(const char *name)
void installDebugOutputFilter(DebugFilter filter)
bool isValid() const
QByteArrayList layers() const
void presentAboutToBeQueued(QWindow *window)
void presentQueued(QWindow *window)
void removeDebugOutputFilter(DebugFilter filter)
void resetDeviceFunctions(VkDevice device)
void setApiVersion(const QVersionNumber &vulkanVersion)
void setExtensions(const QByteArrayList &extensions)
void setFlags(Flags flags)
void setLayers(const QByteArrayList &layers)
void setVkInstance(VkInstance existingVkInstance)
QVulkanInfoVector<QVulkanExtension> supportedExtensions()
QVulkanInfoVector<QVulkanLayer> supportedLayers()
bool supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window)
VkInstance 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 add LIBS += -lvulkan or similar in the application's .pro file.
  • The headers for the QVulkan classes may include vulkan.h with VK_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 include vulkan.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().

ConstantValueDescription
QVulkanInstance::NoDebugOutputRedirect0x01Disables 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.

QVulkanDeviceFunctions *QVulkanInstance::deviceFunctions(VkDevice device)

Returns the QVulkanDeviceFunctions object that exposes the device level core Vulkan command set and is guaranteed to be functional cross-platform.

Note: The Vulkan functions in the returned object must only be called with device or a child object (VkQueue, VkCommandBuffer) of device as their first parameter. This is because these functions are resolved via vkGetDeviceProcAddr in order to avoid the potential overhead of internal dispatching.

Note: The returned object is owned and managed by the QVulkanInstance. Do not destroy or alter it.

Note: The object is cached so calling this function with the same device again is a cheap operation. However, when the device gets destroyed, it is up to the application to notify the QVulkanInstance by calling resetDeviceFunctions().

See also functions() and resetDeviceFunctions().

VkResult 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().

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(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::presentAboutToBeQueued(QWindow *window)

This function should be called by the application's renderer before 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 Wayland this will add send a wl_surface.frame request in order to prevent the driver from blocking for minimized windows.

This function was introduced in Qt 5.15.

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(DebugFilter filter)

Removes a filter function previously installed by installDebugOutputFilter().

Note: This function can be called before create().

See also installDebugOutputFilter().

void QVulkanInstance::resetDeviceFunctions(VkDevice device)

Invalidates and destroys the QVulkanDeviceFunctions object for the given device.

This function must be called when a VkDevice, for which deviceFunctions() was called, gets destroyed while the application intends to continue running, possibly creating a new logical Vulkan device later on.

There is no need to call this before destroying the QVulkanInstance since clean up is then performed automatically.

See also deviceFunctions().

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.

Note: Be aware that Vulkan 1.1 changes the behavior with regards to the Vulkan API version field. In Vulkan 1.0 specifying an unsupported vulkanVersion led to failing create() with VK_ERROR_INCOMPATIBLE_DRIVER, as was mandated by the specification. Starting with Vulkan 1.1, the specification disallows this, the driver must accept any version without failing the instance creation.

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(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().

void QVulkanInstance::setVkInstance(VkInstance existingVkInstance)

Makes QVulkanInstance adopt an existing VkInstance handle instead of creating a new one.

Note: existingVkInstance must have at least VK_KHR_surface and the appropriate WSI-specific VK_KHR_*_surface extensions enabled. To ensure debug output redirection is functional, VK_EXT_debug_report is needed as well.

Note: This function can only be called before create() and has no effect if called afterwards.

See also vkInstance().

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().

bool QVulkanInstance::supportsPresent(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, QWindow *window)

Returns true if the queue family with queueFamilyIndex within the physicalDevice supports presenting to window.

Call this function when examining the queues of a given Vulkan device, in order to decide which queue can be used for performing presentation.

[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.

VkInstance 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().

See also setVkInstance().