Qt for Windows - 圖形加速

For Qt Quick to work, a graphics driver that supports Direct 3D 11, Direct3D 12, 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.

要強製使用 Direct3D 的軟件光柵化器 (WARP), 設置環境變量 QSG_RHI_PREFER_SOFTWARE_RENDERER to 1 . In some cases Qt Quick will perform such a fallback automatically, in order to allow running Qt applications without additional configuration. This happens when the drivers do not provide a sufficient level of D3D11 functionality, and occurs typically in virtual machines that do not have proper GPU acceleration and passthrough implemented. When in doubt what graphics device is being used, and when troubleshooting or reporting problems to Qt, run the application with the environment variable QSG_INFO=1 set and inspect the debug output. Running the qtdiag tool can also provide useful information since it enumerates all available 3D APIs.

To request using Vulkan, OpenGL, or Direct 3D 12, set the environment variable QSG_RHI_BACKEND to vulkan or opengl or d3d12 ,或使用等效 C++ API in main() . Note that some of these 3D APIs may require the appropriate drivers to be installed.

雖然不是默認對於 Qt Quick ,OpenGL 仍常用於很多 Qt 應用程序中,例如在 QWidget 基的應用程序構建於 QOpenGLWindow or QOpenGLWidget 。下列章節涵蓋瞭 Qt 構建的一些 OpenGL 細節。

動態加載 OpenGL

Qt 支持在運行時選擇並加載 OpenGL 這是默認模式,且可以明確請求通過傳遞 -opengl dynamic 給 configure 腳本。

configure -opengl dynamic
					

這種配置最靈活,因為構建期間未硬編碼 OpenGL 實現的依賴 (或假定)。它允許健壯的應用程序部署。當給定環境無法提供適當的 OpenGL 2.0 實現時,它會自動迴退到加載替代 opengl32.dll ,默認名稱為 opengl32sw.dll 。預構建 Qt 包隨附 Mesa llvmpipe 構建,OpenGL 的軟件光柵化器實現,使用該名稱。

當配置采用 -opengl dynamic ,不管 Qt 還是應用程序構建使用 qmake or CMake 將鏈接到 opengl32.lib。取而代之,將在運行時選擇並加載庫。默認情況下,Qt 將確定係統的 opengl32.dll 是否提供 OpenGL 2 功能。若存在,則使用 opengl32.dll,否則試圖加載 opengl32sw.dll 。詳見下文。

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

  • Qt::AA_UseDesktopOpenGL 相當於設置 QT_OPENGL to desktop .
  • Qt::AA_UseOpenGLES 在 Qt 6 沒有效果。
  • Qt::AA_UseSoftwareOpenGL 相當於設置 QT_OPENGL to software .

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

動態加載對包含本機 OpenGL 調用的應用程序有顯著影響:可能無法鏈接它們,由於不會將 opengl32.lib 自動指定給鏈接器。相反,期望應用程序使用 OpenGL 函數憑藉 QOpenGLFunctions 類。因此,會移除 OpenGL 庫的直接依賴,且運行期間的所有調用會被路由到由 Qt 選擇的實現。另外,應用程序可以自由地直接調用 OpenGL 函數,若它們將 opengl32.lib 添加到其 .pro 工程文件: LIBS += opengl32.lib (Visual Studio) 或 LIBS += -lopengl32 (Mingw-w64). 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 實現相關:

注意: 在 Qt 6,傳統 ANGLE 相關關鍵字 ( disable_angle , disable_d3d11 , disable_d3d9 ) 會被接受,但沒有效果。

  • disable_desktopgl - 禁用 OpenGL。這確保 Qt 不會試圖使用常規 OpenGL (opengl32.dll),且它會采用 ANGLE 立即開始。這對阻止壞的 OpenGL 驅動程序崩潰應用程序,很有用。
  • disable_rotation - 強製應用程序始終按橫嚮方式運行。它沒有效果,當使用軟件 OpenGL 實現時。這旨在為有鏇轉問題的驅動程序準備的。
  • disable_program_cache - 禁止在磁盤存儲著色器程序二進製文件。

樣本文件看起來像:

{
"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 設置,防止 Qt 使用其不穩定的桌麵 OpenGL 實現,而不是試圖迴退到立即加載基於軟件的替代庫。

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

  • disable_desktopgl - 若係統提供 OpenGL 2.0 或更新版本,但已知驅動程序不穩定且易於崩潰。
  • disable_desktopgl , disable_angle - 當不期望加速路徑時。這確保 Qt 嘗試的唯一選項是軟件光柵化 opengl32sw.dll。對於虛擬機和部署在各種舊係統內的應用程序,很有用。

匹配給定卡 (或驅動程序) 的支持鍵,如下。注意:其中一些是 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 在某些虛擬環境中可以變得相關,其中存在多個 (可能虛擬的) 圖形適配器。若若日誌按類彆像 qt.qpa.gl 指示由於驅動程序和顯示適配器的檢測導緻錯誤禁用 OpenGL,那麼,推薦設置此環境變量以使應用程序能夠正常運行。該環境變量在 Qt 5.15 引入。

直接依賴 opengl32.dll

替代默認 dynamic OpenGL 構建直接依賴 opengl32.dll。此模式是傳遞命令行選項 -opengl desktop 給 configure 腳本。

configure -opengl desktop
					

注意: 在 Windows,不支持使用 EGL 和 OpenGL ES。在 Qt 6,OpenGL on Windows 始終隱含將 WGL 用作窗口係統接口。

在這種 Qt 構建中,很多 Qt 共享庫和 Qt 應用程序也將依賴 opengl32.dll。因此,使用替代庫不可能。