Qt Shader Tools
Introduction
The Qt Shader Tools module builds on the SPIR-V Open Source Ecosystem as described at the Khronos SPIR-V web site. For compiling into SPIR-V glslang is used, while translating and reflecting is done via SPIRV-Cross.
In order to allow shader code to be written once in Qt applications and libraries, all shaders are expected to be written in a single language which is then compiled into SPIR-V. This shanding language is Vulkan-compatible GLSL at the moment. This is different from the OpenGL-style GLSL Qt 5.x expects. See the GL_KHR_vulkan_glsl specification for an overview of the differences.
Source code for other shading languages, such as, GLSL, HLSL and the Metal Shading Language, are then generated from translating the SPIR-V bytecode, together with reflection information (inputs, outputs, shader resources). For GLSL in particular, this also involves generating multiple variants, meaning source code suitable for different GLSL versions, such as, GLSL ES 100, GLSL ES 300, GLSL 120, 150, etc. This is then all packed into serializable QShader container objects, typically stored in disk in form of files an extension of .qsb
. The Qt Rendering Hardware Interface consumes QShader instances directly, picking the shader source or bytecode that is best suited for the graphics API used at run time. A QShader object is typically deserialized from .qsb
files shipped with the application or Qt itself in the Qt Resource System.
Some platforms provide the option of compiling shader source code to an intermediate format similar to SPIR-V. This involves running platform-specific tools. With Direct 3D, the qsb
tool provides the option to invoke the fxc tool from the Windows SDK once the HLSL source code has been generated. It then replaces the HLSL source code with the DXBC
binary generated by fxc
. This can have a positive effect on applications' run time performance since they no longer need to do the first phase of compilation (HLSL source to DXBC) themselves. For macOS and iOS the XCode SDK provides similar tools. The downside of this approach that running these tools is only possible on their respective platforms. Therefore, this is best suited for use in combination with qsb's CMake integration since doing shader conditioning at application build time implicitly comes with the knowledge needed about the target platform and what platform-specific tools can be invoked.
The following diagram describes the steps that happen during an invocation of the qsb
tool:
The main components of the Qt shader conditioning system are:
- the
qsb
command-line tool - CMake integration for the
qsb
tool - QShader (part of the QtGui module)
- QShaderBaker (part of this module, the library equivalent of the
qsb
tool)
Note: As of Qt 6.0, the C++ APIs are considered private. They are not recommended for direct usage from applications. Rather, use the qsb
command-line tool either directly, or indirectly from CMake projects.
Typical Usage with Qt Quick
Application developers typically encounter the need to work with custom shader code in Qt Quick scenes that have:
- ShaderEffect items, or
- QQuickItem subclasses that use scene graph nodes in combination with custom materials built by subclassing QSGMaterial.
For example, consider the following QML snippet:
ShaderEffect { width: 100; height: 100 fragmentShader: "myeffect.frag.qsb" }
Here the fragmentShader URL refers to a .qsb
file relative to the .qml
file's location. The application is expected to ship myeffect.frag.qsb
next to the .qml
file. To generate that file from the Vulkan-style GLSL source (which itself does not need to be shipped with the application), the application must use the qsb
tool either directly or via CMake.
Typical Usage with Qt Quick 3D
Qt Quick 3D uses the Shader Tools module directly to perform shader conditioning at run time. In addition, it provides solutions for pre-generating shaders for materials at build time, in case a run time dependency to shader compilers and other tools is not seen ideal. In either case developers do not need to work directly with the qsb
tool since it is encapsulated by the Qt Quick 3D's own framework and tools.
Table of Contents
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.0/qtshadertools-index.html