From b300cbf7a973ba8436b88902b328bf174148a03a Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Sat, 22 Aug 2026 16:16:38 +1000 Subject: [PATCH] ANSLS: enumerate GPUs over DXGI so detection survives a missing OpenCL ICD hwinfo::getAllGPUs() is an OpenCL device list -- the prebuilt HWiNFO.lib has no Win32_VideoController path, only clGetPlatformIDs/clGetDeviceIDs/clGetDeviceInfo. A machine whose driver never registered an OpenCL ICD therefore reported no GPU at all, even with a healthy WDDM driver driving the display. Two consequences: the system-info JSON emitted "GPU":"", and CheckHardwareInformation() silently fell back to the CPU engine on a box holding a perfectly good CUDA card. DXGI needs only a WDDM display driver, so it sees the adapter either way. CheckHardwareInformation() now classifies adapters by PCI vendor id. Exact ids also retire the substring matching and its "ATI" inside "CORPORATION" workaround. It falls back to the OpenCL list when DXGI returns nothing. GetSystemHardwareInformation() keeps its OpenCL loop untouched and merely appends DXGI adapters that OpenCL did not already report, so the JSON stays byte-identical -- same keys, order, indices and values -- on any machine that already worked. Appended entries reuse OpenCL's vendor spelling (OpenClStyleVendor) and read the driver version from the display-adapter class key, since DXGI exposes neither. Verified on a dual-adapter box: the GPU node matches the pre-change output exactly, and forcing the append path yields the same shape with values sourced from DXGI plus the registry. Co-Authored-By: Claude Opus 5 --- core/ANSLicensingSystem/ANSLicense.cpp | 211 +++++++++++++++++++++---- core/ANSLicensingSystem/CMakeLists.txt | 2 +- 2 files changed, 184 insertions(+), 29 deletions(-) diff --git a/core/ANSLicensingSystem/ANSLicense.cpp b/core/ANSLicensingSystem/ANSLicense.cpp index f4ec8d4..960d7d0 100644 --- a/core/ANSLicensingSystem/ANSLicense.cpp +++ b/core/ANSLicensingSystem/ANSLicense.cpp @@ -700,6 +700,7 @@ struct GpuAdapter { DWORD vendorId{0}; std::string name; // UTF-8 int64_t dedicatedVramMB{0}; + int64_t sharedSysMemMB{0}; // iGPUs report ~0 dedicated; this is their pool bool isDiscrete{false}; // heuristic: dedicated VRAM ≥ 1 GiB // running totals — reset every Sample() @@ -707,6 +708,19 @@ struct GpuAdapter { int64_t processMemMB{0}; }; +// Vendor name in the spelling OpenCL uses, for adapters OpenCL did not +// report. Callers that string-match the JSON "vendor" field see the same +// text whichever source filled it in. +const char* OpenClStyleVendor(DWORD vendorId) { + switch (vendorId) { + case 0x10DE: return "NVIDIA"; + case 0x8086: return "Intel Corporation"; + case 0x1002: // fallthrough — AMD and some AMD APUs + case 0x1022: return "Advanced Micro Devices, Inc."; + default: return ""; + } +} + const char* VendorString(DWORD vendorId) { switch (vendorId) { case 0x10DE: return "NVIDIA"; @@ -747,6 +761,7 @@ std::vector EnumerateGpuAdapters() { a.vendorId = desc.VendorId; a.name = WideToUtf8(desc.Description); a.dedicatedVramMB = static_cast(desc.DedicatedVideoMemory / (1024ULL * 1024ULL)); + a.sharedSysMemMB = static_cast(desc.SharedSystemMemory / (1024ULL * 1024ULL)); a.isDiscrete = a.dedicatedVramMB >= 1024; // ≥ 1 GiB ⇒ probably dGPU out.push_back(std::move(a)); } @@ -757,6 +772,66 @@ std::vector EnumerateGpuAdapters() { return out; } +// Driver version for a DXGI adapter. +// +// DXGI does not expose it, and WMI (Win32_VideoController) costs a COM/WMI +// round trip. The display-adapter class key has it: every installed adapter +// gets a numbered subkey holding DriverDesc — the Device Manager name, which +// is the same text DXGI returns as Description — plus DriverVersion. +// Returns "" when the adapter has no match (rare: a driver installed after +// the key was enumerated, or a name mismatch), which is what the JSON then +// reports rather than a fabricated value. +std::string GpuDriverVersion(const std::string& description) { + if (description.empty()) return {}; + + static const wchar_t* kDisplayClassKey = + L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}"; + + HKEY classKey = nullptr; + if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, kDisplayClassKey, 0, + KEY_READ, &classKey) != ERROR_SUCCESS) { + ANS_DBG("HWInfo", "GpuDriverVersion: cannot open display class key"); + return {}; + } + + std::string version; + constexpr DWORD kSubNameChars = 256; + wchar_t subName[kSubNameChars]; + for (DWORD i = 0; version.empty(); ++i) { + DWORD subLen = kSubNameChars; + if (RegEnumKeyExW(classKey, i, subName, &subLen, + nullptr, nullptr, nullptr, nullptr) != ERROR_SUCCESS) + break; // ERROR_NO_MORE_ITEMS — done + + HKEY adapterKey = nullptr; + if (RegOpenKeyExW(classKey, subName, 0, KEY_READ, &adapterKey) != ERROR_SUCCESS) + continue; + + auto readString = [&adapterKey](const wchar_t* value) -> std::string { + constexpr DWORD kBufChars = 512; + wchar_t buf[kBufChars]; + DWORD len = sizeof(buf); + DWORD type = 0; + if (RegQueryValueExW(adapterKey, value, nullptr, &type, + reinterpret_cast(buf), &len) != ERROR_SUCCESS || + type != REG_SZ) + return {}; + // RegQueryValueExW does not guarantee NUL termination. + const DWORD chars = len / sizeof(wchar_t); + buf[chars < kBufChars ? chars : kBufChars - 1] = L'\0'; + return WideToUtf8(buf); + }; + + if (readString(L"DriverDesc") == description) + version = readString(L"DriverVersion"); + + RegCloseKey(adapterKey); + } + + RegCloseKey(classKey); + return version; +} + bool ParseLuidFromInstance(const wchar_t* inst, LUID& out) { if (!inst) return false; const wchar_t* p = wcsstr(inst, L"luid_0x"); @@ -1371,32 +1446,67 @@ namespace ANSCENTER } // ---------------------------------------------------------------- - // GPU — classify all adapters by vendor string + // GPU — classify all adapters by PCI vendor ID. + // + // Enumerated over DXGI, not OpenCL. hwinfo::getAllGPUs() is an + // OpenCL device list (the prebuilt HWiNFO.lib has no + // Win32_VideoController path), so it reports nothing on a machine + // whose driver did not register an OpenCL ICD — and we would then + // silently drop a perfectly good CUDA card down to the CPU engine. + // DXGI only needs a WDDM display driver, which any usable GPU has. + // + // Vendor IDs are exact, so no substring matching and none of its + // traps (e.g. "ATI" matching inside "CORPOR-ATI-ON"). // ---------------------------------------------------------------- - auto gpus = hwinfo::getAllGPUs(); - ANS_DBG("HWInfo", "Found %zu GPU adapter(s)", gpus.size()); - for (auto& gpu : gpus) { - std::string vendor = gpu.vendor(); - ANS_DBG("HWInfo", "GPU: name=%s vendor=%s", gpu.name().c_str(), vendor.c_str()); - std::cout << "[HWInfo] Detected GPU: " << gpu.name() << " (Vendor: " << vendor << ")" << std::endl; - // Normalize to uppercase for reliable substring matching - std::transform(vendor.begin(), vendor.end(), - vendor.begin(), ::toupper); + auto adapters = EnumerateGpuAdapters(); + ANS_DBG("HWInfo", "DXGI: found %zu GPU adapter(s)", adapters.size()); + for (const auto& a : adapters) { + ANS_DBG("HWInfo", "GPU: name=%s vendorId=0x%04X vram=%lldMiB discrete=%d", + a.name.c_str(), a.vendorId, + static_cast(a.dedicatedVramMB), a.isDiscrete ? 1 : 0); + std::cout << "[HWInfo] Detected GPU: " << a.name + << " (Vendor: " << VendorString(a.vendorId) << ")" << std::endl; - if (vendor.find("NVIDIA") != std::string::npos) - gpuNvidiaAvailable = true; - if (vendor.find("INTEL") != std::string::npos) - gpuOpenVINOAvailable = true; - // AMD cards report as "AMD", "Advanced Micro Devices", or - // legacy "ATI Technologies" branding. - // NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains - // "ATI" inside "CORPOR-ATI-ON", causing a false positive. - // Use "ATI " (with trailing space) to match "ATI Technologies" - // or "ATI Radeon" without matching "CORPORATION". - if (vendor.find("AMD") != std::string::npos || - vendor.find("ADVANCED MICRO") != std::string::npos || - vendor.find("ATI ") != std::string::npos) - gpuAMDAvailable = true; + switch (a.vendorId) { + case 0x10DE: gpuNvidiaAvailable = true; break; // NVIDIA + case 0x1002: gpuAMDAvailable = true; break; // AMD / ATI + case 0x1022: gpuAMDAvailable = true; break; // AMD (some APUs) + case 0x8086: gpuOpenVINOAvailable = true; break; // Intel + default: break; + } + } + + // DXGI came back empty — no WDDM driver at all, or a session with no + // graphics stack. Fall back to the OpenCL list rather than assuming + // there is no GPU. + if (adapters.empty()) { + auto gpus = hwinfo::getAllGPUs(); + ANS_DBG("HWInfo", "DXGI empty; OpenCL fallback found %zu device(s)", gpus.size()); + for (auto& gpu : gpus) { + std::string vendor = gpu.vendor(); + ANS_DBG("HWInfo", "OpenCL GPU: name=%s vendor=%s", + gpu.name().c_str(), vendor.c_str()); + std::cout << "[HWInfo] Detected GPU (OpenCL): " << gpu.name() + << " (Vendor: " << vendor << ")" << std::endl; + // Normalize to uppercase for reliable substring matching + std::transform(vendor.begin(), vendor.end(), + vendor.begin(), ::toupper); + + if (vendor.find("NVIDIA") != std::string::npos) + gpuNvidiaAvailable = true; + if (vendor.find("INTEL") != std::string::npos) + gpuOpenVINOAvailable = true; + // AMD cards report as "AMD", "Advanced Micro Devices", or + // legacy "ATI Technologies" branding. + // NOTE: "ATI" alone is too broad — "INTEL CORPORATION" contains + // "ATI" inside "CORPOR-ATI-ON", causing a false positive. + // Use "ATI " (with trailing space) to match "ATI Technologies" + // or "ATI Radeon" without matching "CORPORATION". + if (vendor.find("AMD") != std::string::npos || + vendor.find("ADVANCED MICRO") != std::string::npos || + vendor.find("ATI ") != std::string::npos) + gpuAMDAvailable = true; + } } ANS_DBG("HWInfo", "Detection: cpuIntel=%d gpuNvidia=%d gpuAMD=%d gpuOpenVINO=%d", cpuIntelAvailable, gpuNvidiaAvailable, gpuAMDAvailable, gpuOpenVINOAvailable); @@ -3887,9 +3997,25 @@ namespace ANSCENTER osNode.put("endianess", os.isLittleEndian() ? "little endian" : "big endian"); root.add_child("OS", osNode); - // GPU information - auto gpus = hwinfo::getAllGPUs(); - for (auto& gpu : gpus) { + // GPU information. + // + // The OpenCL loop below is unchanged — same keys, same order, + // same values — so on a machine that already reported its GPUs + // the JSON is byte-identical to before. + // + // But hwinfo::getAllGPUs() *is* an OpenCL device list, and it + // reports nothing when no vendor registered an OpenCL ICD. That + // is how this field ended up as "" on machines that plainly have + // a GPU. So afterwards we append any DXGI adapter OpenCL did not + // already cover: DXGI needs only a WDDM display driver, so it + // sees the adapter either way. Nothing is appended when OpenCL + // works, which is why the identical case stays identical. + auto clDevices = hwinfo::getAllGPUs(); + auto adapters = EnumerateGpuAdapters(); + ANS_DBG("HWInfo", "GetSystemHardwareInformation: OpenCL %zu device(s), DXGI %zu adapter(s)", + clDevices.size(), adapters.size()); + + for (auto& gpu : clDevices) { boost::property_tree::ptree gpuNodeChild; gpuNodeChild.put("GPU", gpu.id()); gpuNodeChild.put("vendor", gpu.vendor()); @@ -3900,8 +4026,37 @@ namespace ANSCENTER gpuNode.push_back(std::make_pair("", gpuNodeChild)); } + for (const auto& a : adapters) { + bool covered = false; + for (const auto& d : clDevices) { + if (d.name() == a.name) { covered = true; break; } + } + if (covered) continue; + + ANS_DBG("HWInfo", "GPU not seen by OpenCL, adding from DXGI: name=%s vendorId=0x%04X", + a.name.c_str(), a.vendorId); + + // Dedicated VRAM, or the shared pool for an iGPU. An iGPU + // carves out a token amount of dedicated memory (128 MiB is + // typical) and works out of system RAM, so reporting the + // carve-out would badly understate it. + const double memMiB = static_cast( + a.dedicatedVramMB >= 512 ? a.dedicatedVramMB : a.sharedSysMemMB); + const char* vendor = OpenClStyleVendor(a.vendorId); + + boost::property_tree::ptree gpuNodeChild; + gpuNodeChild.put("GPU", 0); // hwinfo never set a per-device id either + gpuNodeChild.put("vendor", std::string(*vendor ? vendor : VendorString(a.vendorId))); + gpuNodeChild.put("model", a.name); + gpuNodeChild.put("diver_version", GpuDriverVersion(a.name)); + gpuNodeChild.put("memory[MiB]", memMiB); + // DXGI exposes no clock; 0 rather than a fabricated number. + gpuNodeChild.put("frequency", static_cast(0)); + gpuNode.push_back(std::make_pair("", gpuNodeChild)); + } + root.add_child("GPU", gpuNode); - + // RAM information hwinfo::RAM ram; diff --git a/core/ANSLicensingSystem/CMakeLists.txt b/core/ANSLicensingSystem/CMakeLists.txt index a4165cf..9ea5812 100644 --- a/core/ANSLicensingSystem/CMakeLists.txt +++ b/core/ANSLicensingSystem/CMakeLists.txt @@ -54,7 +54,7 @@ target_link_libraries(ANSLicensingSystem PRIVATE shlwapi.lib winhttp.lib pdh.lib psapi.lib wbemuuid.lib oleaut32.lib ole32.lib - dxgi.lib + dxgi.lib advapi32.lib dnsapi.lib ${WIN_COMMON_LIBS} )