Qt for Windows - Graphics Acceleration

For Qt Quick to work, a graphics driver that provides Direct 3D 11.1, Vulkan 1.0, or OpenGL 2.1 or higher is required. As of Qt 6, the default for Qt Quick on Windows is Direct3D 11. This is different from Qt 5, where the default was OpenGL, either directly, or through ANGLE, an OpenGL to Direct3D translator. ANGLE is no longer shipped with Qt in Qt 6.

To force using Direct3D's software rasterizer (WARP), set the environment variable QSG_RHI_PREFER_SOFTWARE_RENDERER to 1 .

To request using Vulkan or OpenGL, both of which require a Vulkan or OpenGL driver to be installed, set the environment variable QSG_RHI_BACKEND to vulkan or opengl , or use the equivalent C++ API in main() .

While not the default for Qt Quick , OpenGL is still commonly used in many Qt applications, for example in QWidget -based applications building on QOpenGLWindow or QOpenGLWidget . The following sections cover some OpenGL specifics of a Qt build.

Dynamically Loading OpenGL

Qt supports choosing and loading the OpenGL implementation at runtime. This mode is the default, and can be explicitly requested by passing -opengl dynamic 給 configure 腳本。

configure -opengl dynamic
					

This configuration is the most flexible because no dependencies or assumptions are hardcoded about the OpenGL implementation during build time. It allows robust application deployment. When a given environment fails to provide a proper OpenGL 2.0 implementation, it will fall back automatically to load an alternative to opengl32.dll , the default name of which is opengl32sw.dll . The pre-built Qt packages ship a build of Mesa llvmpipe, a software rasterizer implementation of OpenGL, under that name.

當配置采用 -opengl dynamic ,不管 Qt 還是應用程序構建使用 qmake or CMake will link to opengl32.lib. Instead, the library is chosen and loaded at runtime. By default, Qt will determine whether the system's opengl32.dll provides OpenGL 2 functions. If these are present, opengl32.dll is used, otherwise it attempts to load opengl32sw.dll 。詳見下文。

可以配置加載機製透過 QT_OPENGL 環境變量和以下應用程序屬性:

  • Qt::AA_UseDesktopOpenGL 相當於設置 QT_OPENGL to desktop .
  • Qt::AA_UseOpenGLES Has no effect in Qt 6.
  • Qt::AA_UseSoftwareOpenGL 相當於設置 QT_OPENGL to software .

當明確請求某個配置時,不會進行任何校驗在應用程序啓動時,也就是說,將不審查係統提供的 opengl32.dll。

The dynamic loading has a significant impact on applications that contain native OpenGL calls: they may fail to link since opengl32.lib is not automatically specified to the linker. Instead, applications are expected to use the OpenGL functions via the QOpenGLFunctions class. Thus the direct dependency on the OpenGL library is removed and all calls will be routed during runtime to the implementation chosen by Qt. Alternatively, applications are free to make direct OpenGL function calls if they add opengl32.lib to their .pro project files: LIBS += opengl32.lib (Visual Studio) 或 LIBS += -lopengl32 (MinGW). The result is, from the application's perspective, equivalent to the -opengl desktop Qt 構建配置。

Qt::AA_UseSoftwareOpenGL 的特殊之處是它會試著采用非標準名稱來加載 OpenGL 實現。默認名稱為 opengl32sw.dll 。這允許隨附僅軟件 OpenGL 實現,例如構建 Mesa 采用 llvmpipe ,在此名稱下。若有必要,可以覆蓋文件名通過設置 QT_OPENGL_DLL 環境變量。

根據圖形卡和驅動程序版本,提供 JSON 格式配置文件以指定要使用哪種 OpenGL 實現是可能的。位置的給定通過環境變量 QT_OPENGL_BUGLIST 。相對路徑的解析使用 QLibraryInfo::SettingsPath or QStandardPaths::ConfigLocation 。文件利用驅動程序 Bug 列錶所使用的格式,在 Chromium 工程 。它由條目的列錶組成,各條目指定一組條件和特徵關鍵詞的列錶。通常,使用設備 ID 和供應商 ID 相匹配的特定圖形卡。可以找到它們,在輸齣的 qtdiag6 or dxdiag 工具。

以下特徵關鍵詞與選取 OpenGL 實現相關:

注意: In Qt 6, the legacy ANGLE related keywords ( disable_angle , disable_d3d11 , disable_d3d9 ) are accepted, but have no effect.

  • disable_desktopgl - 禁用 OpenGL。這確保 Qt 不會試圖使用常規 OpenGL (opengl32.dll),且它會采用 ANGLE 立即開始。這對阻止壞的 OpenGL 驅動程序崩潰應用程序,很有用。
  • disable_rotation - Forces the application to run in landscape orientation always. It has no effect when using a software OpenGL implementation. This is intended for drivers that have issues with rotation.
  • disable_program_cache - Disable storing shader program binaries on disk.

樣本文件看起來像:

{
"entries": [
{
  "id": 1,
  "description": "Disable D3D11 on older nVidia drivers",
  "os": {
    "type": "win"
  },
  "vendor_id": "0x10de",
  "device_id": ["0x0DE9"],
  "driver_version": {
    "op": "<=",
    "value": "8.17.12.6973"
  },
  "features": [
    "disable_d3d11"
  ]
},
...
					

QT_OPENGL_BUGLIST 未指定,將使用內置列錶。這通常包括一些較舊的、功能較差圖形卡采用 disable_desktopgl set, in order to prevent Qt from using their unstable desktop OpenGL implementations and instead fall back to attempting to load the software-based alternative library right away.

在實踐中,最常見組閤期望如下:

  • disable_desktopgl - 若係統提供 OpenGL 2.0 或更新版本,但已知驅動程序不穩定且易於崩潰。
  • disable_desktopgl , disable_angle - When no accelerated path is desired. This ensures that the only option Qt tries is the software rasterizer (opengl32sw.dll). Can be useful in virtual machines and applications that are deployed on a wide range of old systems.

匹配給定卡 (或驅動程序) 的支持鍵,如下。注意:其中一些是 Qt 特有的。

  • os.type - 操作係統: win , linux , macosx , android
  • os.version - 內核版本
  • os.release - 指定 Windows 操作係統發行列錶: xp , vista , 7 , 8 , 8.1 , 10 .
  • vendor_id - 來自適配器標識符的供應商
  • device_id - PCI 設備 ID 列錶。
  • driver_version - 來自適配器標識符的驅動程序版本
  • driver_description - 匹配,當值是來自適配器標識符的驅動程序所描述的子字符串時
  • gl_vendor - 匹配,當值是子字符串對於 GL_VENDOR string

要禁用所有黑名單,設置環境變量 QT_NO_OPENGL_BUGLIST 為任意值。這會跳過讀取任何配置文件,且假定未禁用任何內容,不管是驅動程序還是 OS。

注意: 雖然通常不需要, QT_NO_OPENGL_BUGLIST can become relevant in certain virtual environments, with multiple, possibly virtual, graphics adapters present. If the logs from categories like qt.qpa.gl indicate that the detection of the driver and display adapter leads to incorrectly disabling OpenGL, it is then recommended to set this environment variable in order to enable the application to run normally. This environment variable was introduced in Qt 5.15.

Direct dependency to opengl32.dll

An alternative to the default dynamic OpenGL builds is to depend directly on opengl32.dll. For this mode, pass the command line options -opengl desktop 給 configure 腳本。

configure -opengl desktop
					

注意: Using EGL and OpenGL ES is not supported on Windows. In Qt 6, OpenGL on Windows always implies using WGL as the windowing system interface.

In such Qt builds, many Qt shared libraries, and also Qt applications will have a dependency to opengl32.dll, and therefore using an alternative library is not possible.