mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
GPU: support detect gpu devices with OpenCL
This commit is contained in:
@@ -1523,6 +1523,7 @@
|
||||
"auto",
|
||||
"pci",
|
||||
"vulkan",
|
||||
"opencl",
|
||||
"opengl"
|
||||
],
|
||||
"default": "auto"
|
||||
|
||||
+2
-1
@@ -1251,7 +1251,8 @@
|
||||
"enum": {
|
||||
"auto": "Query platform specific graphic APIs. Requires proper GPU drivers installed. Not supported on FreeBSD",
|
||||
"pci": "Search PCI devices, which do not requires GPU driver installed. Not supported on Windows and macOS",
|
||||
"vulkan": "Use Vulkan API. Slow and requires vulkan driver installed. Used for Android",
|
||||
"vulkan": "Use Vulkan API. Slow and requires proper vulkan driver installed. Used for Android",
|
||||
"opencl": "Use OpenCL API. Slow and requires proper OpenCL driver installed",
|
||||
"opengl": "Use OpenGL API. Slow and only detects one GPU"
|
||||
},
|
||||
"default": "auto"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "gpu.h"
|
||||
#include "detection/vulkan/vulkan.h"
|
||||
#include "detection/opencl/opencl.h"
|
||||
#include "detection/opengl/opengl.h"
|
||||
|
||||
const char* FF_GPU_VENDOR_NAME_APPLE = "Apple";
|
||||
@@ -94,6 +95,16 @@ const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_OPENCL)
|
||||
{
|
||||
FFOpenCLResult* opencl = ffDetectOpenCL();
|
||||
if (!opencl->error && opencl->gpus.length > 0)
|
||||
{
|
||||
ffListDestroy(result);
|
||||
ffListInitMove(result, &opencl->gpus);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (options->detectionMethod <= FF_GPU_DETECTION_METHOD_OPENGL)
|
||||
{
|
||||
if (detectByOpenGL(result) == NULL)
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#include "detection/opencl/opencl.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <AvailabilityMacros.h>
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED > 1050) && !defined(__ppc__)
|
||||
#define MACOS_HAS_OPENCL
|
||||
#endif
|
||||
#if !defined(FF_HAVE_OPENCL) && defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_15)
|
||||
#define FF_HAVE_OPENCL 1
|
||||
#endif
|
||||
|
||||
#if defined(FF_HAVE_OPENCL) || defined(MACOS_HAS_OPENCL)
|
||||
#ifdef FF_HAVE_OPENCL
|
||||
|
||||
#include "common/library.h"
|
||||
#include "common/parsing.h"
|
||||
@@ -16,7 +13,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#define CL_TARGET_OPENCL_VERSION 100
|
||||
#ifdef FF_HAVE_OPENCL
|
||||
#ifndef __APPLE__
|
||||
#include <CL/cl.h>
|
||||
#else
|
||||
#include <OpenCL/cl.h>
|
||||
@@ -116,15 +113,13 @@ static const char* openCLHandleData(OpenCLData* data, FFOpenCLResult* result)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // defined(FF_HAVE_OPENCL) || defined(MACOS_HAS_OPENCL)
|
||||
|
||||
const char* ffDetectOpenCL(FFOpenCLResult* result)
|
||||
static const char* detectOpenCL(FFOpenCLResult* result)
|
||||
{
|
||||
#ifdef FF_HAVE_OPENCL
|
||||
|
||||
OpenCLData data;
|
||||
|
||||
FF_LIBRARY_LOAD(opencl, &instance.config.library.libOpenCL, "dlopen libOpenCL"FF_LIBRARY_EXTENSION" failed",
|
||||
#ifndef __APPLE__
|
||||
|
||||
FF_LIBRARY_LOAD(opencl, &instance.config.library.libOpenCL, "dlopen libOpenCL" FF_LIBRARY_EXTENSION" failed",
|
||||
#ifdef _WIN32
|
||||
"OpenCL"FF_LIBRARY_EXTENSION, -1,
|
||||
#endif
|
||||
@@ -136,19 +131,36 @@ const char* ffDetectOpenCL(FFOpenCLResult* result)
|
||||
|
||||
return openCLHandleData(&data, result);
|
||||
|
||||
#elif defined(MACOS_HAS_OPENCL) // FF_HAVE_OPENCL
|
||||
#else
|
||||
|
||||
OpenCLData data;
|
||||
data.ffclGetPlatformInfo = clGetPlatformInfo;
|
||||
data.ffclGetDeviceIDs = clGetDeviceIDs;
|
||||
data.ffclGetDeviceInfo = clGetDeviceInfo;
|
||||
|
||||
return openCLHandleData(&data, result);
|
||||
|
||||
#else
|
||||
|
||||
FF_UNUSED(result);
|
||||
return "Fastfetch was build without OpenCL support";
|
||||
|
||||
#endif // FF_HAVE_OPENCL
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // defined(FF_HAVE_OPENCL)
|
||||
|
||||
FFOpenCLResult* ffDetectOpenCL(void)
|
||||
{
|
||||
static FFOpenCLResult result;
|
||||
|
||||
if (result.gpus.elementSize == 0)
|
||||
{
|
||||
ffStrbufInit(&result.version);
|
||||
ffStrbufInit(&result.name);
|
||||
ffStrbufInit(&result.vendor);
|
||||
ffListInit(&result.gpus, sizeof(FFGPUResult));
|
||||
|
||||
#ifdef FF_HAVE_OPENCL
|
||||
result.error = detectOpenCL(&result);
|
||||
#else
|
||||
result.error = "fastfetch was compiled without OpenCL support";
|
||||
#endif
|
||||
}
|
||||
|
||||
return &result;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ typedef struct FFOpenCLResult
|
||||
FFstrbuf name;
|
||||
FFstrbuf vendor;
|
||||
FFlist gpus; //List of FFGPUResult, see detection/gpu/gpu.h
|
||||
const char* error;
|
||||
} FFOpenCLResult;
|
||||
|
||||
const char* ffDetectOpenCL(FFOpenCLResult* result);
|
||||
FFOpenCLResult* ffDetectOpenCL();
|
||||
|
||||
@@ -150,6 +150,7 @@ bool ffParseGPUCommandOptions(FFGPUOptions* options, const char* key, const char
|
||||
{ "auto", FF_GPU_DETECTION_METHOD_AUTO },
|
||||
{ "pci", FF_GPU_DETECTION_METHOD_PCI },
|
||||
{ "vulkan", FF_GPU_DETECTION_METHOD_VULKAN },
|
||||
{ "opencl", FF_GPU_DETECTION_METHOD_OPENCL },
|
||||
{ "opengl", FF_GPU_DETECTION_METHOD_OPENGL },
|
||||
{},
|
||||
});
|
||||
@@ -204,6 +205,7 @@ void ffParseGPUJsonObject(FFGPUOptions* options, yyjson_val* module)
|
||||
{ "auto", FF_GPU_DETECTION_METHOD_AUTO },
|
||||
{ "pci", FF_GPU_DETECTION_METHOD_PCI },
|
||||
{ "vulkan", FF_GPU_DETECTION_METHOD_VULKAN },
|
||||
{ "opencl", FF_GPU_DETECTION_METHOD_OPENCL },
|
||||
{ "opengl", FF_GPU_DETECTION_METHOD_OPENGL },
|
||||
{},
|
||||
});
|
||||
@@ -260,6 +262,9 @@ void ffGenerateGPUJsonConfig(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_
|
||||
case FF_GPU_DETECTION_METHOD_VULKAN:
|
||||
yyjson_mut_obj_add_str(doc, module, "detectionMethod", "vulkan");
|
||||
break;
|
||||
case FF_GPU_DETECTION_METHOD_OPENCL:
|
||||
yyjson_mut_obj_add_str(doc, module, "detectionMethod", "opencl");
|
||||
break;
|
||||
case FF_GPU_DETECTION_METHOD_OPENGL:
|
||||
yyjson_mut_obj_add_str(doc, module, "detectionMethod", "opengl");
|
||||
break;
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef enum FFGPUDetectionMethod
|
||||
FF_GPU_DETECTION_METHOD_AUTO,
|
||||
FF_GPU_DETECTION_METHOD_PCI,
|
||||
FF_GPU_DETECTION_METHOD_VULKAN,
|
||||
FF_GPU_DETECTION_METHOD_OPENCL,
|
||||
FF_GPU_DETECTION_METHOD_OPENGL,
|
||||
} FFGPUDetectionMethod;
|
||||
|
||||
|
||||
+64
-84
@@ -9,36 +9,27 @@
|
||||
|
||||
void ffPrintOpenCL(FFOpenCLOptions* options)
|
||||
{
|
||||
FFOpenCLResult opencl;
|
||||
ffStrbufInit(&opencl.version);
|
||||
ffStrbufInit(&opencl.name);
|
||||
ffStrbufInit(&opencl.vendor);
|
||||
ffListInit(&opencl.gpus, sizeof(FFGPUResult));
|
||||
FFOpenCLResult* result = ffDetectOpenCL();
|
||||
|
||||
const char* error = ffDetectOpenCL(&opencl);
|
||||
|
||||
if(error != NULL)
|
||||
ffPrintError(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
||||
else
|
||||
if(result->error != NULL)
|
||||
{
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
ffStrbufPutTo(&opencl.version, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINT_FORMAT_CHECKED(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_OPENCL_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.version, "version"},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.name, "name"},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &opencl.vendor, "vendor"},
|
||||
}));
|
||||
}
|
||||
ffPrintError(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", result->error);
|
||||
return;
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&opencl.version);
|
||||
ffStrbufDestroy(&opencl.name);
|
||||
ffStrbufDestroy(&opencl.vendor);
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
ffStrbufPutTo(&result->version, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINT_FORMAT_CHECKED(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_OPENCL_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->version, "version"},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->name, "name"},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->vendor, "vendor"},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
bool ffParseOpenCLCommandOptions(FFOpenCLOptions* options, const char* key, const char* value)
|
||||
@@ -78,72 +69,61 @@ void ffGenerateOpenCLJsonConfig(FFOpenCLOptions* options, yyjson_mut_doc* doc, y
|
||||
|
||||
void ffGenerateOpenCLJsonResult(FF_MAYBE_UNUSED FFOpenCLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
FFOpenCLResult opencl;
|
||||
ffStrbufInit(&opencl.version);
|
||||
ffStrbufInit(&opencl.name);
|
||||
ffStrbufInit(&opencl.vendor);
|
||||
ffListInit(&opencl.gpus, sizeof(FFGPUResult));
|
||||
FFOpenCLResult* result = ffDetectOpenCL();
|
||||
|
||||
const char* error = ffDetectOpenCL(&opencl);
|
||||
|
||||
if(error != NULL)
|
||||
if(result->error != NULL)
|
||||
{
|
||||
yyjson_mut_obj_add_str(doc, module, "error", error);
|
||||
yyjson_mut_obj_add_str(doc, module, "error", result->error);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "version", &opencl.version);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "name", &opencl.name);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &opencl.vendor);
|
||||
|
||||
yyjson_mut_val* gpus = yyjson_mut_obj_add_arr(doc, obj, "gpus");
|
||||
FF_LIST_FOR_EACH(FFGPUResult, gpu, opencl.gpus)
|
||||
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->version);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result->name);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result->vendor);
|
||||
|
||||
yyjson_mut_val* gpus = yyjson_mut_obj_add_arr(doc, obj, "gpus");
|
||||
FF_LIST_FOR_EACH(FFGPUResult, gpu, result->gpus)
|
||||
{
|
||||
yyjson_mut_val* gpuObj = yyjson_mut_arr_add_obj(doc, gpus);
|
||||
yyjson_mut_obj_add_str(doc, gpuObj, "type", gpu->type == FF_GPU_TYPE_UNKNOWN ? "Unknown" : gpu->type == FF_GPU_TYPE_INTEGRATED ? "Integrated" : "Discrete");
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "vendor", &gpu->vendor);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "name", &gpu->name);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "driver", &gpu->driver);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "platformApi", &gpu->platformApi);
|
||||
yyjson_mut_obj_add_int(doc, gpuObj, "coreCount", gpu->coreCount);
|
||||
yyjson_mut_obj_add_real(doc, gpuObj, "frequency", gpu->frequency);
|
||||
|
||||
yyjson_mut_val* memoryObj = yyjson_mut_obj_add_obj(doc, gpuObj, "memory");
|
||||
|
||||
{
|
||||
yyjson_mut_val* gpuObj = yyjson_mut_arr_add_obj(doc, gpus);
|
||||
yyjson_mut_obj_add_str(doc, gpuObj, "type", gpu->type == FF_GPU_TYPE_UNKNOWN ? "Unknown" : gpu->type == FF_GPU_TYPE_INTEGRATED ? "Integrated" : "Discrete");
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "vendor", &gpu->vendor);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "name", &gpu->name);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "driver", &gpu->driver);
|
||||
yyjson_mut_obj_add_strbuf(doc, gpuObj, "platformApi", &gpu->platformApi);
|
||||
yyjson_mut_obj_add_int(doc, gpuObj, "coreCount", gpu->coreCount);
|
||||
yyjson_mut_obj_add_real(doc, gpuObj, "frequency", gpu->frequency);
|
||||
yyjson_mut_val* dedicatedMemory = yyjson_mut_obj_add_obj(doc, memoryObj, "dedicated");
|
||||
if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, dedicatedMemory, "total", gpu->dedicated.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, dedicatedMemory, "total");
|
||||
|
||||
yyjson_mut_val* memoryObj = yyjson_mut_obj_add_obj(doc, gpuObj, "memory");
|
||||
|
||||
{
|
||||
yyjson_mut_val* dedicatedMemory = yyjson_mut_obj_add_obj(doc, memoryObj, "dedicated");
|
||||
if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, dedicatedMemory, "total", gpu->dedicated.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, dedicatedMemory, "total");
|
||||
|
||||
if (gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, dedicatedMemory, "used", gpu->dedicated.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, dedicatedMemory, "used");
|
||||
}
|
||||
|
||||
{
|
||||
yyjson_mut_val* sharedMemory = yyjson_mut_obj_add_obj(doc, memoryObj, "shared");
|
||||
if (gpu->shared.total != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, sharedMemory, "total", gpu->shared.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, sharedMemory, "total");
|
||||
|
||||
if (gpu->shared.used != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, sharedMemory, "used", gpu->shared.used);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, sharedMemory, "used");
|
||||
}
|
||||
|
||||
yyjson_mut_obj_add_uint(doc, gpuObj, "deviceId", gpu->deviceId);
|
||||
if (gpu->dedicated.used != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, dedicatedMemory, "used", gpu->dedicated.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, dedicatedMemory, "used");
|
||||
}
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&opencl.version);
|
||||
ffStrbufDestroy(&opencl.name);
|
||||
ffStrbufDestroy(&opencl.vendor);
|
||||
{
|
||||
yyjson_mut_val* sharedMemory = yyjson_mut_obj_add_obj(doc, memoryObj, "shared");
|
||||
if (gpu->shared.total != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, sharedMemory, "total", gpu->shared.total);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, sharedMemory, "total");
|
||||
|
||||
if (gpu->shared.used != FF_GPU_VMEM_SIZE_UNSET)
|
||||
yyjson_mut_obj_add_uint(doc, sharedMemory, "used", gpu->shared.used);
|
||||
else
|
||||
yyjson_mut_obj_add_null(doc, sharedMemory, "used");
|
||||
}
|
||||
|
||||
yyjson_mut_obj_add_uint(doc, gpuObj, "deviceId", gpu->deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintOpenCLHelpFormat(void)
|
||||
|
||||
Reference in New Issue
Block a user