Scene Graph - Vulkan Under QML#

Shows how to render directly with Vulkan under a Qt Quick scene.

../_images/vulkanunderqml-example.jpg

Note

Compiling this example requires an SDK. See Vulkan Integration for information on what to install.

Overview#

This example makes use of the beforeRendering() and beforeRenderPassRecording() signals to draw custom Vulkan content under a Qt Quick scene. QML is used to render a text label on top.

The example is equivalent in most ways to the OpenGL Under QML , Direct3D 11 Under QML , and Metal Under QML examples, they all render the same custom content, just via different native APIs.

The particulars of utilizing QML will be covered in this documentation without delving into the detail of custom Vulkan rendering.

Affecting Vulkan rendering from QML#

The example shows how to use values that are exposed to QML to control Vulkan rendering.

To expose the threshold value t to QML, in the definition of VulkanSquircle, we use the Q_OBJECT, Q_PROPERTY, and QML_ELEMENT macros like so:

We then go on to declare public and private items:

Then in main.qml we animate the threshold value using a NumberAnimation .

The t variable is ultimately used by the SPIR-V shader program that draws the squircles.

Using signals to render custom Vulkan content#

The beforeRendering() and beforeRenderPassRecording() signals are what are used.

The beforeRendering() signal is emitted at the start of every frame, before the scene graph starts its rendering. This means any Vulkan draw calls that are made as a response to this signal, will stack under the Qt Quick items. There are two signals because the custom Vulkan commands are recorded onto the same command buffer used by the scene graph.

The beforeRendering() function on its own is not sufficient for this, because it gets emitted at the start of the frame, before recording the start of a renderpass instance by using vkCmdBeginRenderPass .

The solution: by connecting to beforeRenderPassRecording(), the application’s own commands and the scene graph’s scaffolding will end up in the right order.

Connecting the signals is done by the sync() function:

Another way you can render Vulkan content on top of the Qt Quick scene is by connecting to the afterRendering() and afterRenderPassRecording() signals.

Example project @ code.qt.io