Files
fastfetch/src/detection/gpu/gpu.c
T

206 lines
7.7 KiB
C
Raw Normal View History

2022-09-05 17:42:42 +02:00
#include "gpu.h"
2026-02-12 18:30:02 +08:00
#include "common/debug.h"
2023-02-14 17:50:53 +08:00
#include "detection/vulkan/vulkan.h"
2024-06-20 14:05:38 +08:00
#include "detection/opencl/opencl.h"
2023-12-26 10:53:43 +08:00
#include "detection/opengl/opengl.h"
#include "modules/opengl/opengl.h"
2022-09-05 17:42:42 +02:00
2023-02-06 11:07:49 +08:00
const char* FF_GPU_VENDOR_NAME_APPLE = "Apple";
const char* FF_GPU_VENDOR_NAME_AMD = "AMD";
const char* FF_GPU_VENDOR_NAME_INTEL = "Intel";
const char* FF_GPU_VENDOR_NAME_NVIDIA = "NVIDIA";
const char* FF_GPU_VENDOR_NAME_MTHREADS = "Moore Threads";
2024-01-29 21:48:33 +08:00
const char* FF_GPU_VENDOR_NAME_QUALCOMM = "Qualcomm";
const char* FF_GPU_VENDOR_NAME_MTK = "MTK";
2023-10-21 17:36:13 +08:00
const char* FF_GPU_VENDOR_NAME_VMWARE = "VMware";
2026-04-07 13:53:29 +08:00
const char* FF_GPU_VENDOR_NAME_PARALLELS = "Parallels";
2023-10-21 17:36:13 +08:00
const char* FF_GPU_VENDOR_NAME_MICROSOFT = "Microsoft";
2023-10-31 15:11:22 +08:00
const char* FF_GPU_VENDOR_NAME_REDHAT = "RedHat";
const char* FF_GPU_VENDOR_NAME_ORACLE = "Oracle";
2025-05-30 10:33:26 +08:00
const char* FF_GPU_VENDOR_NAME_BROADCOM = "Broadcom";
const char* FF_GPU_VENDOR_NAME_LOONGSON = "Loongson";
const char* FF_GPU_VENDOR_NAME_JINGJIA_MICRO = "Jingjia Micro";
const char* FF_GPU_VENDOR_NAME_HUAWEI = "Huawei";
const char* FF_GPU_VENDOR_NAME_ZHAOXIN = "Zhaoxin";
2023-02-06 11:07:49 +08:00
2026-03-29 09:28:49 +08:00
const char* ffGPUGetVendorString(unsigned vendorId) {
2023-10-31 15:11:22 +08:00
// https://devicehunt.com/all-pci-vendors
2026-03-29 09:28:49 +08:00
switch (vendorId) {
case 0x106b:
return FF_GPU_VENDOR_NAME_APPLE;
case 0x1002:
case 0x1022:
case 0x1dd8:
return FF_GPU_VENDOR_NAME_AMD;
case 0x8086:
case 0x8087:
case 0x03e7:
return FF_GPU_VENDOR_NAME_INTEL;
case 0x0955:
case 0x10de:
case 0x12d2:
return FF_GPU_VENDOR_NAME_NVIDIA;
case 0x1ed5:
return FF_GPU_VENDOR_NAME_MTHREADS;
case 0x17cb:
case 0x5143:
return FF_GPU_VENDOR_NAME_QUALCOMM;
case 0x14c3:
return FF_GPU_VENDOR_NAME_MTK;
case 0x15ad:
return FF_GPU_VENDOR_NAME_VMWARE;
case 0x1af4:
return FF_GPU_VENDOR_NAME_REDHAT;
case 0x1ab8:
case 0x05404c42: // PD
2026-04-07 13:53:29 +08:00
return FF_GPU_VENDOR_NAME_PARALLELS;
2026-03-29 09:28:49 +08:00
case 0x1414:
return FF_GPU_VENDOR_NAME_MICROSOFT;
case 0x108e:
return FF_GPU_VENDOR_NAME_ORACLE;
case 0x182f:
case 0x14e4:
return FF_GPU_VENDOR_NAME_BROADCOM;
case 0x0014:
return FF_GPU_VENDOR_NAME_LOONGSON;
case 0x0731:
return FF_GPU_VENDOR_NAME_JINGJIA_MICRO;
case 0x19e5:
return FF_GPU_VENDOR_NAME_HUAWEI;
case 0x1d17:
return FF_GPU_VENDOR_NAME_ZHAOXIN;
default:
return NULL;
2024-05-13 19:33:31 +08:00
}
2023-02-06 11:07:49 +08:00
}
2022-09-05 17:42:42 +02:00
2026-03-29 09:28:49 +08:00
const char* detectByOpenGL(FFlist* gpus) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Starting OpenGL GPU detection fallback");
2023-12-26 10:53:43 +08:00
FFOpenGLResult result;
ffStrbufInit(&result.version);
ffStrbufInit(&result.renderer);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.slv);
2024-07-09 20:39:15 +08:00
ffStrbufInit(&result.library);
2026-04-09 16:20:59 +08:00
FF_A_CLEANUP(ffDestroyOpenGLOptions) FFOpenGLOptions options;
ffInitOpenGLOptions(&options);
const char* error = ffDetectOpenGL(&options, &result);
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenGL detection returns: %s", error ?: "success");
2026-03-29 09:28:49 +08:00
if (!error) {
2026-04-14 10:41:18 +08:00
FFGPUResult* gpu = FF_LIST_ADD(FFGPUResult, *gpus);
2023-12-26 10:53:43 +08:00
gpu->type = FF_GPU_TYPE_UNKNOWN;
ffStrbufInitMove(&gpu->vendor, &result.vendor);
2023-12-26 10:53:43 +08:00
ffStrbufInitMove(&gpu->name, &result.renderer);
ffStrbufInit(&gpu->driver);
ffStrbufInitF(&gpu->platformApi, "OpenGL %s", result.version.chars);
ffStrbufInit(&gpu->memoryType);
2024-09-19 15:22:09 +08:00
gpu->index = FF_GPU_INDEX_UNSET;
2023-12-26 10:53:43 +08:00
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
2024-08-01 13:54:03 +08:00
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
2026-03-30 10:27:23 +08:00
gpu->dedicated = gpu->shared = (FFGPUMemory) { 0, 0 };
2023-12-26 10:53:43 +08:00
gpu->deviceId = 0;
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenGL reported renderer='%s', vendor='%s', version='%s'",
gpu->name.chars,
gpu->vendor.chars,
result.version.chars);
2026-03-29 09:28:49 +08:00
if (ffStrbufContainS(&gpu->name, "Apple")) {
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_APPLE);
2024-01-04 10:29:02 +08:00
gpu->type = FF_GPU_TYPE_INTEGRATED;
2026-03-29 09:28:49 +08:00
} else if (ffStrbufContainS(&gpu->name, "Intel")) {
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_INTEL);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufContainS(&gpu->name, "AMD") || ffStrbufContainS(&gpu->name, "ATI")) {
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_AMD);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufContainS(&gpu->name, "NVIDIA")) {
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_NVIDIA);
2026-03-29 09:28:49 +08:00
} else if (ffStrbufContainS(&gpu->name, "MTT")) {
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_MTHREADS);
2026-03-29 09:28:49 +08:00
}
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenGL fallback produced GPU: name='%s', vendor='%s', type=%u",
gpu->name.chars,
gpu->vendor.chars,
gpu->type);
2023-12-26 10:53:43 +08:00
}
ffStrbufDestroy(&result.version);
ffStrbufDestroy(&result.renderer);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.slv);
2024-07-09 20:39:15 +08:00
ffStrbufDestroy(&result.library);
2023-12-26 10:53:43 +08:00
return error;
}
2026-03-29 09:28:49 +08:00
const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Starting GPU detection with method=%d", (int) options->detectionMethod);
2026-03-29 09:28:49 +08:00
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_PCI) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Trying PCI/native GPU detection");
const char* error = ffDetectGPUImpl(options, result);
2026-03-29 09:28:49 +08:00
if (!error && result->length > 0) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("PCI/native GPU detection succeeded with %u GPU(s)", result->length);
return NULL;
}
FF_DEBUG("PCI/native GPU detection did not produce results (error=%s, gpuCount=%u)",
error ?: "none",
result->length);
2023-06-12 11:30:18 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_VULKAN) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Trying Vulkan GPU detection fallback");
FFVulkanResult* vulkan = ffDetectVulkan();
2026-03-29 09:28:49 +08:00
if (!vulkan->error && vulkan->gpus.length > 0) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Vulkan detection succeeded with %u GPU(s)", vulkan->gpus.length);
ffListDestroy(result);
ffListInitMove(result, &vulkan->gpus);
2026-03-29 09:28:49 +08:00
#ifdef __ANDROID__
double ffGPUDetectTempFromTZ(void);
2026-03-29 09:28:49 +08:00
if (options->temp && result->length == 1) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Applying Android thermal-zone temperature to single Vulkan GPU");
FF_LIST_GET(FFGPUResult, *result, 0)->temperature = ffGPUDetectTempFromTZ();
2026-02-12 18:30:02 +08:00
}
2026-03-29 09:28:49 +08:00
#endif
return NULL;
}
2026-02-12 18:30:02 +08:00
FF_DEBUG("Vulkan detection did not produce results (error=%s, gpuCount=%u)",
vulkan->error ?: "none",
vulkan->gpus.length);
}
2026-03-29 09:28:49 +08:00
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_OPENCL) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Trying OpenCL GPU detection fallback");
2024-06-20 14:05:38 +08:00
FFOpenCLResult* opencl = ffDetectOpenCL();
2026-03-29 09:28:49 +08:00
if (!opencl->error && opencl->gpus.length > 0) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenCL detection succeeded with %u GPU(s)", opencl->gpus.length);
2024-06-20 14:05:38 +08:00
ffListDestroy(result);
ffListInitMove(result, &opencl->gpus);
return NULL;
}
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenCL detection did not produce results (error=%s, gpuCount=%u)",
opencl->error ?: "none",
opencl->gpus.length);
2024-06-20 14:05:38 +08:00
}
2026-03-29 09:28:49 +08:00
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_OPENGL) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("Trying OpenGL GPU detection fallback");
const char* error = detectByOpenGL(result);
2026-03-29 09:28:49 +08:00
if (error == NULL) {
2026-02-12 18:30:02 +08:00
FF_DEBUG("OpenGL fallback succeeded with %u GPU(s)", result->length);
return NULL;
2026-02-12 18:30:02 +08:00
}
FF_DEBUG("OpenGL fallback failed: %s", error);
2023-12-26 10:53:43 +08:00
}
2023-06-12 11:30:18 +08:00
2026-02-12 18:30:02 +08:00
FF_DEBUG("GPU detection failed in all enabled backends");
2023-12-26 10:53:43 +08:00
return "GPU detection failed";
2022-09-05 17:42:42 +02:00
}