From 1a66bc4ec0dee43a30f26c0db11cc65a0fc9bec7 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 2 Aug 2024 16:29:23 +0800 Subject: [PATCH] GPU: code cleanup; port MT specific code to Windows --- CMakeLists.txt | 5 +-- src/detection/gpu/gpu_driver_specific.h | 50 +++++++++++++++++++++++++ src/detection/gpu/gpu_linux.c | 50 ++++++++++--------------- src/detection/gpu/gpu_windows.c | 37 +----------------- 4 files changed, 71 insertions(+), 71 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9af2b044..eb74f018 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -838,15 +838,12 @@ endif() # Proprietary GPU driver APIs if(LINUX OR BSD OR WIN32) list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_nvidia.c) + list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_mthreads.c) endif() if(WIN32) list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_intel.c) list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_amd.c) endif() -if(LINUX) - list(APPEND LIBFASTFETCH_SRC src/detection/gpu/gpu_mthreads.c) -endif() - include(CheckFunctionExists) check_function_exists(wcwidth HAVE_WCWIDTH) if(NOT HAVE_WCWIDTH) diff --git a/src/detection/gpu/gpu_driver_specific.h b/src/detection/gpu/gpu_driver_specific.h index 7b1ec742..945b92cf 100644 --- a/src/detection/gpu/gpu_driver_specific.h +++ b/src/detection/gpu/gpu_driver_specific.h @@ -50,3 +50,53 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName); const char* ffDetectAmdGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName); const char* ffDetectMthreadsGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName); + +FF_MAYBE_UNUSED static inline bool getDriverSpecificDetectionFn(const char* vendor, __typeof__(&ffDetectNvidiaGpuInfo)* pDetectFn, const char** pDllName) +{ + if (vendor == FF_GPU_VENDOR_NAME_NVIDIA) + { + *pDetectFn = ffDetectNvidiaGpuInfo; + #ifdef _WIN32 + *pDllName = "nvml.dll"; + #else + *pDllName = "libnvidia-ml.so"; + #endif + } + else if (vendor == FF_GPU_VENDOR_NAME_MTHREADS) + { + *pDetectFn = ffDetectMthreadsGpuInfo; + #ifdef _WIN32 + *pDllName = "mtml.dll"; + #else + *pDllName = "libmtml.so"; + #endif + } + #ifdef _WIN32 + else if (vendor == FF_GPU_VENDOR_NAME_INTEL) + { + *pDetectFn = ffDetectIntelGpuInfo; + #ifdef _WIN64 + *pDllName = "ControlLib.dll"; + #else + *pDllName = "ControlLib32.dll"; + #endif + } + else if (vendor == FF_GPU_VENDOR_NAME_AMD) + { + *pDetectFn = ffDetectAmdGpuInfo; + #ifdef _WIN64 + *pDllName = "amd_ags_x64.dll"; + #else + *pDllName = "amd_ags_x86.dll"; + #endif + } + #endif + else + { + *pDetectFn = NULL; + *pDllName = NULL; + return false; + } + + return true; +} diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 5c564c0d..0dd04bc1 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -343,11 +343,13 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf pciDetectIntelSpecific(gpu, deviceDir, buffer); ffStrbufSubstrBefore(deviceDir, drmDirPathLength); } - else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA) + else { - if (options->temp || options->driverSpecific) + __typeof__(&ffDetectNvidiaGpuInfo) detectFn; + const char* soName; + if (getDriverSpecificDetectionFn(gpu->vendor.chars, &detectFn, &soName) && (options->temp || options->driverSpecific)) { - ffDetectNvidiaGpuInfo(&(FFGpuDriverCondition) { + detectFn(&(FFGpuDriverCondition) { .type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID, .pciBusId = { .domain = pciDomain, @@ -359,41 +361,27 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf .temp = options->temp ? &gpu->temperature : NULL, .memory = options->driverSpecific ? &gpu->dedicated : NULL, .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, + .coreUsage = options->driverSpecific ? &gpu->coreUsage : NULL, .type = &gpu->type, .frequency = options->driverSpecific ? &gpu->frequency : NULL, .name = options->driverSpecific ? &gpu->name : NULL, - }, "libnvidia-ml.so"); + }, soName); } if (gpu->type == FF_GPU_TYPE_UNKNOWN) { - if (ffStrbufStartsWithIgnCaseS(&gpu->name, "GeForce") || - ffStrbufStartsWithIgnCaseS(&gpu->name, "Quadro") || - ffStrbufStartsWithIgnCaseS(&gpu->name, "Tesla")) - gpu->type = FF_GPU_TYPE_DISCRETE; - } - } - else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_MTHREADS) - { - if (options->temp || options->driverSpecific) - { - ffDetectMthreadsGpuInfo(&(FFGpuDriverCondition){ - .type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID, - .pciBusId = { - .domain = pciDomain, - .bus = pciBus, - .device = pciDevice, - .func = pciFunc, - }, - }, - (FFGpuDriverResult){ - .temp = options->temp ? &gpu->temperature : NULL, - .memory = options->driverSpecific ? &gpu->dedicated : NULL, - .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, - .type = &gpu->type, - .frequency = options->driverSpecific ? &gpu->frequency : NULL, - }, - "libmtml.so"); + if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA) + { + if (ffStrbufStartsWithIgnCaseS(&gpu->name, "GeForce") || + ffStrbufStartsWithIgnCaseS(&gpu->name, "Quadro") || + ffStrbufStartsWithIgnCaseS(&gpu->name, "Tesla")) + gpu->type = FF_GPU_TYPE_DISCRETE; + } + else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_MTHREADS) + { + if (ffStrbufStartsWithIgnCaseS(&gpu->name, "MTT ")) + gpu->type = FF_GPU_TYPE_DISCRETE; + } } } diff --git a/src/detection/gpu/gpu_windows.c b/src/detection/gpu/gpu_windows.c index 111aced8..c0399ecd 100644 --- a/src/detection/gpu/gpu_windows.c +++ b/src/detection/gpu/gpu_windows.c @@ -10,41 +10,6 @@ static int isGpuNameEqual(const FFGPUResult* gpu, const FFstrbuf* name) return ffStrbufEqual(&gpu->name, name); } -static inline bool getDriverSpecificDetectionFn(const char* vendor, __typeof__(&ffDetectNvidiaGpuInfo)* pDetectFn, const char** pDllName) -{ - if (vendor == FF_GPU_VENDOR_NAME_NVIDIA) - { - *pDetectFn = ffDetectNvidiaGpuInfo; - *pDllName = "nvml.dll"; - } - else if (vendor == FF_GPU_VENDOR_NAME_INTEL) - { - *pDetectFn = ffDetectIntelGpuInfo; - #ifdef _WIN64 - *pDllName = "ControlLib.dll"; - #else - *pDllName = "ControlLib32.dll"; - #endif - } - else if (vendor == FF_GPU_VENDOR_NAME_AMD) - { - *pDetectFn = ffDetectAmdGpuInfo; - #ifdef _WIN64 - *pDllName = "amd_ags_x64.dll"; - #else - *pDllName = "amd_ags_x86.dll"; - #endif - } - else - { - *pDetectFn = NULL; - *pDllName = NULL; - return false; - } - - return true; -} - const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* gpus) { DISPLAY_DEVICEW displayDevice = { .cb = sizeof(displayDevice) }; @@ -150,7 +115,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* if (vendorId && deviceId) { int bus = -1, dev = -1, func = -1; - if (detectFn == ffDetectNvidiaGpuInfo) + if (detectFn == ffDetectNvidiaGpuInfo || detectFn == ffDetectMthreadsGpuInfo) { // Find PCI id from video id // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\PCI\DEVICE_ID\INSTANCE_ID\Device Parameters\VideoID