QVulkanWindow Class

  • Header: QVulkanWindow

  • Since: Qt 5.10

  • qmake: QT += gui

  • Inherits: QWindow

Detailed Description

QVulkanWindow is a Vulkan-capable QWindow that manages a Vulkan device, a graphics queue, a command pool and buffer, a depth-stencil image and a double-buffered FIFO swapchain, while taking care of correct behavior when it comes to events like resize, special situations like not having a device queue supporting both graphics and presentation, device lost scenarios, and additional functionality like reading the rendered content back. Conceptually it is the counterpart of QOpenGLWindow in the Vulkan world.

QVulkanWindow does not always eliminate the need to implement a fully custom QWindow subclass as it will not necessarily be sufficient in advanced use cases.

QVulkanWindow can be embedded into QWidget-based user interfaces via QWidget::createWindowContainer(). This approach has a number of limitations, however. Make sure to study the documentation first.

A typical application using QVulkanWindow may look like the following:

 
Sélectionnez
  class VulkanRenderer : public QVulkanWindowRenderer
  {
  public:
      VulkanRenderer(QVulkanWindow *w) : m_window(w) { }

      void initResources() override
      {
          m_devFuncs = m_window->vulkanInstance()->deviceFunctions(m_window->device());
          ...
      }
      void initSwapChainResources() override { ... }
      void releaseSwapChainResources() override { ... }
      void releaseResources() override { ... }

      void startNextFrame() override
      {
          VkCommandBuffer cmdBuf = m_window->currentCommandBuffer();
          ...
          m_devFuncs->vkCmdBeginRenderPass(...);
          ...
          m_window->frameReady();
      }

  private:
      QVulkanWindow *m_window;
      QVulkanDeviceFunctions *m_devFuncs;
  };

  class VulkanWindow : public QVulkanWindow
  {
  public:
      QVulkanWindowRenderer *createRenderer() override {
          return new VulkanRenderer(this);
      }
  };

  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);

      QVulkanInstance inst;
      // enable the standard validation layers, when available
      inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation");
      if (!inst.create())
          qFatal("Failed to create Vulkan instance: %d", inst.errorCode());

      VulkanWindow w;
      w.setVulkanInstance(&inst);
      w.showMaximized();

      return app.exec();
  }

As it can be seen in the example, the main patterns in QVulkanWindow usage are:

  • The QVulkanInstance is associated via QWindow::setVulkanInstance(). It is then retrievable via QWindow::vulkanInstance() from everywhere, on any thread.

  • Similarly to QVulkanInstance, device extensions can be queried via supportedDeviceExtensions() before the actual initialization. Requesting an extension to be enabled is done via setDeviceExtensions(). Such calls must be made before the window becomes visible, that is, before calling show() or similar functions. Unsupported extension requests are gracefully ignored.

  • The renderer is implemented in a QVulkanWindowRenderer subclass, an instance of which is created in the createRenderer() factory function.

  • The core Vulkan commands are exposed via the QVulkanFunctions object, retrievable by calling QVulkanInstance::functions(). Device level functions are available after creating a VkDevice by calling QVulkanInstance::deviceFunctions().

  • The building of the draw calls for the next frame happens in QVulkanWindowRenderer::startNextFrame(). The implementation is expected to add commands to the command buffer returned from currentCommandBuffer(). Returning from the function does not indicate that the commands are ready for submission. Rather, an explicit call to frameReady() is required. This allows asynchronous generation of commands, possibly on multiple threads. Simple implementations will simply call frameReady() at the end of their QVulkanWindowRenderer::startNextFrame().

  • The basic Vulkan resources (physical device, graphics queue, a command pool, the window's main command buffer, image formats, etc.) are exposed on the QVulkanWindow via lightweight getter functions. Some of these are for convenience only, and applications are always free to query, create and manage additional resources directly via the Vulkan API.

  • The renderer lives in the gui/main thread, like the window itself. This thread is then throttled to the presentation rate, similarly to how OpenGL with a swap interval of 1 would behave. However, the renderer implementation is free to utilize multiple threads in any way it sees fit. The accessors like vulkanInstance(), currentCommandBuffer(), etc. can be called from any thread. The submission of the main command buffer, the queueing of present, and the building of the next frame do not start until frameReady() is invoked on the gui/main thread.

  • When the window is made visible, the content is updated automatically. Further updates can be requested by calling QWindow::requestUpdate(). To render continuously, call requestUpdate() after frameReady().

For troubleshooting, enable the logging category qt.vulkan. Critical errors are printed via qWarning() automatically.

Coordinate system differences between OpenGL and Vulkan

There are two notable differences to be aware of: First, with Vulkan Y points down the screen in clip space, while OpenGL uses an upwards pointing Y axis. Second, the standard OpenGL projection matrix assume a near and far plane values of -1 and 1, while Vulkan prefers 0 and 1.

In order to help applications migrate from OpenGL-based code without having to flip Y coordinates in the vertex data, and to allow using QMatrix4x4 functions like QMatrix4x4::perspective() while keeping the Vulkan viewport's minDepth and maxDepth set to 0 and 1, QVulkanWindow provides a correction matrix retrievable by calling clipCorrectionMatrix().

Multisampling

While disabled by default, multisample antialiasing is fully supported by QVulkanWindow. Additional color buffers and resolving into the swapchain's non-multisample buffers are all managed automatically.

To query the supported sample counts, call supportedSampleCounts(). When the returned set contains 4, 8, ..., passing one of those values to setSampleCount() requests multisample rendering.

unlike QSurfaceFormat::setSamples(), the list of supported sample counts are exposed to the applications in advance and there is no automatic falling back to lower sample counts in setSampleCount(). If the requested value is not supported, a warning is shown and a no multisampling will be used.

Reading images back

When supportsGrab() returns true, QVulkanWindow can perform readbacks from the color buffer into a QImage. grab() is a slow and inefficient operation, so frequent usage should be avoided. It is nonetheless valuable since it allows applications to take screenshots, or tools and tests to process and verify the output of the GPU rendering.

sRGB support

While many applications will be fine with the default behavior of QVulkanWindow when it comes to swapchain image formats, setPreferredColorFormats() allows requesting a pre-defined format. This is useful most notably when working in the sRGB color space. Passing a format like VK_FORMAT_B8G8R8A8_SRGB results in choosing an sRGB format, when available.

Validation layers

During application development it can be extremely valuable to have the Vulkan validation layers enabled. As shown in the example code above, calling QVulkanInstance::setLayers() on the QVulkanInstance before QVulkanInstance::create() enables validation, assuming the Vulkan driver stack in the system contains the necessary layers.

Be aware of platform-specific differences. On desktop platforms installing the Vulkan SDK is typically sufficient. However, Android for example requires deploying additional shared libraries together with the application, and also mandates a different list of validation layer names. See the Android Vulkan development pages for more information.

QVulkanWindow does not expose device layers since this functionality has been deprecated since version 1.0.13 of the Vulkan API.

See Also

Member Type Documentation

 

enum QVulkanWindow::Flag

flags QVulkanWindow::Flags

This enum describes the flags that can be passed to setFlags().

Constant

Value

Description

QVulkanWindow::PersistentResources

0x01

Ensures no graphics resources are released when the window becomes unexposed. The default behavior is to release everything, and reinitialize later when becoming visible again.

The Flags type is a typedef for QFlags<Flag>. It stores an OR combination of Flag values.

Member Function Documentation

 

[explicit] QVulkanWindow::QVulkanWindow(QWindow *parent = nullptr)

Constructs a new QVulkanWindow with the given parent.

The surface type is set to QSurface::VulkanSurface.

[virtual] QVulkanWindow::~QVulkanWindow()

Destructor.

int QVulkanWindow::availablePhysicalDevices()

Returns the list of properties for the supported physical devices in the system.

This function can be called before making the window visible.

QMatrix4x4 QVulkanWindow::clipCorrectionMatrix()

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 minDepth and 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 culling and front face settings.

int QVulkanWindow::colorFormat() const

Returns the color buffer format used by the swapchain.

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initResources() up until QVulkanWindowRenderer::releaseResources().

See Also

int QVulkanWindow::concurrentFrameCount() const

Returns the number of frames that can be potentially active at the same time.

The value is constant for the entire lifetime of the QVulkanWindow.

 
Sélectionnez
    class Renderer {
        ...
        VkDescriptorBufferInfo m_uniformBufInfo[QVulkanWindow::MAX_CONCURRENT_FRAME_COUNT];
    };

    void Renderer::startNextFrame()
    {
        const int count = m_window-&gt;concurrentFrameCount();
        for (int i = 0; i &lt; count; ++i)
            m_uniformBufInfo[i] = ...
        ...
    }
See Also

See also currentFrame()

[virtual] QVulkanWindowRenderer *QVulkanWindow::createRenderer()

Returns a new instance of QVulkanWindowRenderer.

This virtual function is called once during the lifetime of the window, at some point after making it visible for the first time.

The default implementation returns null and so no rendering will be performed apart from clearing the buffers.

The window takes ownership of the returned renderer object.

int QVulkanWindow::currentCommandBuffer() const

Returns The active command buffer for the current swap chain image. Implementations of QVulkanWindowRenderer::startNextFrame() are expected to add commands to this command buffer.

This function must only be called from within startNextFrame() and, in case of asynchronous command generation, up until the call to frameReady().

int QVulkanWindow::currentFrame() const

Returns the current frame index in the range [0, concurrentFrameCount() - 1].

Renderer implementations will have to ensure that uniform data and other dynamic resources exist in multiple copies, in order to prevent frame N altering the data used by the still-active frames N - 1, N - 2, ... N - concurrentFrameCount() + 1.

To avoid relying on dynamic array sizes, applications can use MAX_CONCURRENT_FRAME_COUNT when declaring arrays. This is guaranteed to be always equal to or greater than the value returned from concurrentFrameCount(). Such arrays can then be indexed by the value returned from this function.

 
Sélectionnez
    class Renderer {
        ...
        VkDescriptorBufferInfo m_uniformBufInfo[QVulkanWindow::MAX_CONCURRENT_FRAME_COUNT];
    };

    void Renderer::startNextFrame()
    {
        VkDescriptorBufferInfo &amp;uniformBufInfo(m_uniformBufInfo[m_window-&gt;currentFrame()]);
        ...
    }

This function must only be called from within startNextFrame() and, in case of asynchronous command generation, up until the call to frameReady().

See Also

int QVulkanWindow::currentFramebuffer() const

Returns a VkFramebuffer for the current swapchain image using the default render pass.

The framebuffer has two attachments (color, depth-stencil) when multisampling is not in use, and three (color resolve, depth-stencil, multisample color) when sampleCountFlagBits() is greater than VK_SAMPLE_COUNT_1_BIT. Renderers must take this into account, for example when providing clear values.

Applications are not required to use this framebuffer in case they provide their own render pass instead of using the one returned from defaultRenderPass().

This function must only be called from within startNextFrame() and, in case of asynchronous command generation, up until the call to frameReady().

See Also

See also defaultRenderPass()

int QVulkanWindow::currentSwapChainImageIndex() const

Returns the current swap chain image index in the range [0, swapChainImageCount() - 1].

This function must only be called from within startNextFrame() and, in case of asynchronous command generation, up until the call to frameReady().

int QVulkanWindow::defaultRenderPass() const

Returns a typical render pass with one sub-pass.

Applications are not required to use this render pass. However, they are then responsible for ensuring the current swap chain and depth-stencil images get transitioned from VK_IMAGE_LAYOUT_UNDEFINED to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR and VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL either via the application's custom render pass or by other means.

Stencil read/write is not enabled in this render pass.

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initResources() up until QVulkanWindowRenderer::releaseResources().

See Also

See also currentFramebuffer()

int QVulkanWindow::depthStencilFormat() const

Returns the format used by the depth-stencil buffer(s).

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initResources() up until QVulkanWindowRenderer::releaseResources().

VkImage QVulkanWindow::depthStencilImage() const

Returns the depth-stencil image.

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initSwapChainResources() up until QVulkanWindowRenderer::releaseSwapChainResources().

VkImageView QVulkanWindow::depthStencilImageView() const

Returns the depth-stencil image view.

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initSwapChainResources() up until QVulkanWindowRenderer::releaseSwapChainResources().

int QVulkanWindow::device() const

Returns the active logical device.

Calling this function is only valid from the invocation of QVulkanWindowRenderer::initResources() up until QVulkanWindowRenderer::releaseResources().

uint32_t QVulkanWindow::deviceLocalMemoryIndex() const