Skip to content

Commit 9f8626c

Browse files
committed
[Windows] Try reading GPU driver information directly from registry.
1 parent c81fd6c commit 9f8626c

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

platform/windows/os_windows.cpp

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,78 @@ String OS_Windows::get_version_alias() const {
648648
return "";
649649
}
650650

651-
Vector<String> OS_Windows::get_video_adapter_driver_info() const {
652-
if (RenderingServer::get_singleton() == nullptr) {
651+
Vector<String> OS_Windows::_get_video_adapter_driver_info_reg(const String &p_name) const {
652+
Vector<String> info;
653+
654+
String subkey = "SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}";
655+
HKEY hkey = nullptr;
656+
LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)subkey.utf16().get_data(), 0, KEY_READ, &hkey);
657+
if (result != ERROR_SUCCESS) {
653658
return Vector<String>();
654659
}
655660

656-
static Vector<String> info;
657-
if (!info.is_empty()) {
658-
return info;
661+
DWORD subkeys = 0;
662+
result = RegQueryInfoKeyW(hkey, nullptr, nullptr, nullptr, &subkeys, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
663+
if (result != ERROR_SUCCESS) {
664+
RegCloseKey(hkey);
665+
return Vector<String>();
666+
}
667+
for (DWORD i = 0; i < subkeys; i++) {
668+
WCHAR key_name[MAX_PATH] = L"";
669+
DWORD key_name_size = MAX_PATH;
670+
result = RegEnumKeyExW(hkey, i, key_name, &key_name_size, nullptr, nullptr, nullptr, nullptr);
671+
if (result != ERROR_SUCCESS) {
672+
continue;
673+
}
674+
String id = String::utf16((const char16_t *)key_name, key_name_size);
675+
if (!id.is_empty()) {
676+
HKEY sub_hkey = nullptr;
677+
result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)(subkey + "\\" + id).utf16().get_data(), 0, KEY_QUERY_VALUE, &sub_hkey);
678+
if (result != ERROR_SUCCESS) {
679+
continue;
680+
}
681+
682+
WCHAR buffer[4096];
683+
DWORD buffer_len = 4096;
684+
DWORD vtype = REG_SZ;
685+
if (RegQueryValueExW(sub_hkey, L"DriverDesc", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) != ERROR_SUCCESS || buffer_len == 0) {
686+
buffer_len = 4096;
687+
if (RegQueryValueExW(sub_hkey, L"HardwareInformation.AdapterString", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) != ERROR_SUCCESS || buffer_len == 0) {
688+
RegCloseKey(sub_hkey);
689+
continue;
690+
}
691+
}
692+
693+
String driver_name = String::utf16((const char16_t *)buffer, buffer_len).strip_edges();
694+
if (driver_name == p_name) {
695+
String driver_provider = driver_name;
696+
String driver_version;
697+
698+
buffer_len = 4096;
699+
if (RegQueryValueExW(sub_hkey, L"ProviderName", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS && buffer_len != 0) {
700+
driver_provider = String::utf16((const char16_t *)buffer, buffer_len).strip_edges();
701+
}
702+
buffer_len = 4096;
703+
if (RegQueryValueExW(sub_hkey, L"DriverVersion", nullptr, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS && buffer_len != 0) {
704+
driver_version = String::utf16((const char16_t *)buffer, buffer_len).strip_edges();
705+
}
706+
if (!driver_version.is_empty()) {
707+
info.push_back(driver_provider);
708+
info.push_back(driver_version);
709+
710+
RegCloseKey(sub_hkey);
711+
break;
712+
}
713+
}
714+
RegCloseKey(sub_hkey);
715+
}
659716
}
717+
RegCloseKey(hkey);
718+
return info;
719+
}
720+
721+
Vector<String> OS_Windows::_get_video_adapter_driver_info_wmi(const String &p_name) const {
722+
Vector<String> info;
660723

661724
REFCLSID clsid = CLSID_WbemLocator; // Unmarshaler CLSID
662725
REFIID uuid = IID_IWbemLocator; // Interface UUID
@@ -667,11 +730,6 @@ Vector<String> OS_Windows::get_video_adapter_driver_info() const {
667730
String driver_name;
668731
String driver_version;
669732

670-
const String device_name = RenderingServer::get_singleton()->get_video_adapter_name();
671-
if (device_name.is_empty()) {
672-
return Vector<String>();
673-
}
674-
675733
HRESULT hr = CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, uuid, (LPVOID *)&wbemLocator);
676734
if (hr != S_OK) {
677735
return Vector<String>();
@@ -686,7 +744,7 @@ Vector<String> OS_Windows::get_video_adapter_driver_info() const {
686744
return Vector<String>();
687745
}
688746

689-
const String gpu_device_class_query = vformat("SELECT * FROM Win32_PnPSignedDriver WHERE DeviceName = \"%s\"", device_name);
747+
const String gpu_device_class_query = vformat("SELECT * FROM Win32_PnPSignedDriver WHERE DeviceName = \"%s\"", p_name);
690748
BSTR query = SysAllocString((const WCHAR *)gpu_device_class_query.utf16().get_data());
691749
BSTR query_lang = SysAllocString(L"WQL");
692750
hr = wbemServices->ExecQuery(query_lang, query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, nullptr, &iter);
@@ -751,6 +809,28 @@ Vector<String> OS_Windows::get_video_adapter_driver_info() const {
751809
return info;
752810
}
753811

812+
Vector<String> OS_Windows::get_video_adapter_driver_info() const {
813+
if (RenderingServer::get_singleton() == nullptr) {
814+
return Vector<String>();
815+
}
816+
817+
static Vector<String> info;
818+
if (!info.is_empty()) {
819+
return info;
820+
}
821+
822+
const String device_name = RenderingServer::get_singleton()->get_video_adapter_name();
823+
if (device_name.is_empty()) {
824+
return Vector<String>();
825+
}
826+
827+
info = _get_video_adapter_driver_info_reg(device_name);
828+
if (info.is_empty()) {
829+
info = _get_video_adapter_driver_info_wmi(device_name);
830+
}
831+
return info;
832+
}
833+
754834
bool OS_Windows::get_user_prefers_integrated_gpu() const {
755835
// On Windows 10, the preferred GPU configured in Windows Settings is
756836
// stored in the registry under the key

platform/windows/os_windows.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class OS_Windows : public OS {
145145
HashMap<String, int> encodings;
146146
void _init_encodings();
147147

148+
Vector<String> _get_video_adapter_driver_info_reg(const String &p_name) const;
149+
Vector<String> _get_video_adapter_driver_info_wmi(const String &p_name) const;
150+
148151
// functions used by main to initialize/deinitialize the OS
149152
protected:
150153
virtual void initialize() override;

0 commit comments

Comments
 (0)