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

276 lines
11 KiB
C
Raw Normal View History

2022-06-01 00:04:58 +02:00
#include "fastfetch.h"
#include "detection/gpu/gpu.h"
2023-02-14 17:50:53 +08:00
#include "detection/vulkan/vulkan.h"
2022-06-01 00:04:58 +02:00
#ifdef FF_HAVE_VULKAN
#include "common/library.h"
#include "common/io/io.h"
#include "common/parsing.h"
2023-02-01 15:56:25 +01:00
#include "util/stringUtils.h"
2022-06-18 14:25:31 +02:00
#include <stdlib.h>
2022-06-01 00:04:58 +02:00
#include <vulkan/vulkan.h>
static inline void applyVulkanVersion(uint32_t vulkanVersion, FFVersion* ffVersion)
{
ffVersion->major = VK_VERSION_MAJOR(vulkanVersion);
ffVersion->minor = VK_VERSION_MINOR(vulkanVersion);
ffVersion->patch = VK_VERSION_PATCH(vulkanVersion);
}
static void applyDriverName(VkPhysicalDeviceDriverPropertiesKHR* properties, FFstrbuf* result)
2022-06-01 00:04:58 +02:00
{
if(!ffStrSet(properties->driverName))
return;
ffStrbufAppendS(result, properties->driverName);
/*
* Some drivers (android for example) expose a multiline string as driver info.
* It contains too much info anyways, so we just don't append it.
*/
if(!ffStrSet(properties->driverInfo) || strchr(properties->driverInfo, '\n') != NULL)
2022-06-01 00:04:58 +02:00
return;
ffStrbufAppendS(result, " [");
ffStrbufAppendS(result, properties->driverInfo);
ffStrbufAppendC(result, ']');
}
static const char* detectVulkan(FFVulkanResult* result)
2022-06-01 00:04:58 +02:00
{
2023-10-25 16:24:24 +08:00
FF_LIBRARY_LOAD(vulkan, &instance.config.library.libVulkan, "dlopen libvulkan"FF_LIBRARY_EXTENSION " failed",
2022-12-13 11:10:50 +08:00
#ifdef __APPLE__
"libMoltenVK"FF_LIBRARY_EXTENSION, -1
2023-02-11 12:34:57 +08:00
#elif defined(_WIN32)
"vulkan-1"FF_LIBRARY_EXTENSION, -1
2022-12-13 11:10:50 +08:00
#else
2023-02-11 12:34:57 +08:00
"libvulkan"FF_LIBRARY_EXTENSION, 2
2022-12-13 11:10:50 +08:00
#endif
)
2023-01-15 16:54:31 +08:00
FF_LIBRARY_LOAD_SYMBOL_MESSAGE2(vulkan, vkGetInstanceProcAddr, vkGetInstanceProcAddr@8)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE2(vulkan, vkCreateInstance, vkCreateInstance@12)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE2(vulkan, vkDestroyInstance, vkDestroyInstance@8)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE2(vulkan, vkEnumeratePhysicalDevices, vkEnumeratePhysicalDevices@12)
2022-06-01 00:04:58 +02:00
//Some drivers (nvdc) print messages to stdout
//and that is the best way I found to disable that
2023-02-11 12:34:57 +08:00
FF_SUPPRESS_IO();
2022-06-01 00:04:58 +02:00
FFVersion instanceVersion = FF_VERSION_INIT;
//We need to get the function pointer this way, because it is only provided by vulkan 1.1 and higher.
//a dlsym would fail on 1.0 implementations
PFN_vkEnumerateInstanceVersion ffvkEnumerateInstanceVersion = (PFN_vkEnumerateInstanceVersion) ffvkGetInstanceProcAddr(NULL, "vkEnumerateInstanceVersion");
if(ffvkEnumerateInstanceVersion != NULL)
{
uint32_t version;
if(ffvkEnumerateInstanceVersion(&version) == VK_SUCCESS)
applyVulkanVersion(version, &instanceVersion);
}
const uint32_t projectVersion = VK_MAKE_VERSION(
FASTFETCH_PROJECT_VERSION_MAJOR,
FASTFETCH_PROJECT_VERSION_MINOR,
FASTFETCH_PROJECT_VERSION_PATCH
);
2023-02-11 12:34:57 +08:00
VkInstance vkInstance;
2024-05-30 10:09:37 +08:00
VkResult res = ffvkCreateInstance(&(VkInstanceCreateInfo) {
2022-06-01 00:04:58 +02:00
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = NULL,
2023-02-11 12:34:57 +08:00
.pApplicationInfo = &(VkApplicationInfo) {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = NULL,
.pApplicationName = FASTFETCH_PROJECT_NAME,
.applicationVersion = projectVersion,
.pEngineName = "vulkanPrintGPUs",
.engineVersion = projectVersion,
// We need to request 1.1 to get physicalDeviceDriverProperties
.apiVersion = instanceVersion.minor >= 1 ? VK_API_VERSION_1_1 : VK_API_VERSION_1_0
},
2022-06-01 00:04:58 +02:00
.enabledLayerCount = 0,
.ppEnabledLayerNames = NULL,
2022-12-13 11:10:50 +08:00
.enabledExtensionCount = 0,
.ppEnabledExtensionNames = NULL,
.flags = 0
2024-05-30 10:09:37 +08:00
}, NULL, &vkInstance);
if(res != VK_SUCCESS)
{
switch (res)
{
case VK_ERROR_OUT_OF_HOST_MEMORY:
return "ffvkCreateInstance() failed: VK_ERROR_OUT_OF_HOST_MEMORY";
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
return "ffvkCreateInstance() failed: VK_ERROR_OUT_OF_DEVICE_MEMORY";
case VK_ERROR_INITIALIZATION_FAILED:
return "ffvkCreateInstance() failed: VK_ERROR_INITIALIZATION_FAILED";
case VK_ERROR_LAYER_NOT_PRESENT:
return "ffvkCreateInstance() failed: VK_ERROR_LAYER_NOT_PRESENT";
case VK_ERROR_EXTENSION_NOT_PRESENT:
return "ffvkCreateInstance() failed: VK_ERROR_EXTENSION_NOT_PRESENT";
case VK_ERROR_INCOMPATIBLE_DRIVER:
return "ffvkCreateInstance() failed: VK_ERROR_INCOMPATIBLE_DRIVER";
default:
return "ffvkCreateInstance() failed: unknown error";
}
}
2022-06-01 00:04:58 +02:00
//if instance creation succeeded, but vkEnumerateInstanceVersion didn't, this means we are running against a vulkan 1.0 implementation
//explicitly set this version, if no device is found, so we still have at least this info
if(instanceVersion.major == 0 && instanceVersion.minor == 0 && instanceVersion.patch == 0)
instanceVersion.major = 1;
2023-02-11 12:34:57 +08:00
VkPhysicalDevice physicalDevices[128];
uint32_t physicalDeviceCount = (uint32_t) (sizeof(physicalDevices) / sizeof(*physicalDevices));
2024-05-30 10:09:37 +08:00
res = ffvkEnumeratePhysicalDevices(vkInstance, &physicalDeviceCount, physicalDevices);
if(res != VK_SUCCESS)
2022-06-01 00:04:58 +02:00
{
ffvkDestroyInstance(vkInstance, NULL);
2024-05-30 10:09:37 +08:00
switch (res)
{
case VK_ERROR_OUT_OF_HOST_MEMORY:
return "ffvkEnumeratePhysicalDevices() failed: VK_ERROR_OUT_OF_HOST_MEMORY";
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
return "ffvkEnumeratePhysicalDevices() failed: VK_ERROR_OUT_OF_DEVICE_MEMORY";
case VK_ERROR_INITIALIZATION_FAILED:
return "ffvkEnumeratePhysicalDevices() failed: VK_ERROR_INITIALIZATION_FAILED";
case VK_INCOMPLETE:
return "ffvkEnumeratePhysicalDevices() failed: VK_INCOMPLETE";
default:
return "ffvkEnumeratePhysicalDevices() failed";
}
2022-06-01 00:04:58 +02:00
}
2023-02-11 12:34:57 +08:00
PFN_vkGetPhysicalDeviceProperties ffvkGetPhysicalDeviceProperties = NULL;
PFN_vkGetPhysicalDeviceProperties2 ffvkGetPhysicalDeviceProperties2 = (PFN_vkGetPhysicalDeviceProperties2) ffvkGetInstanceProcAddr(vkInstance, "vkGetPhysicalDeviceProperties2"); // 1.1
if(!ffvkGetPhysicalDeviceProperties2)
ffvkGetPhysicalDeviceProperties = (PFN_vkGetPhysicalDeviceProperties) ffvkGetInstanceProcAddr(vkInstance, "vkGetPhysicalDeviceProperties");
PFN_vkGetPhysicalDeviceMemoryProperties ffvkGetPhysicalDeviceMemoryProperties = (PFN_vkGetPhysicalDeviceMemoryProperties) ffvkGetInstanceProcAddr(vkInstance, "vkGetPhysicalDeviceMemoryProperties");
2022-06-01 00:04:58 +02:00
FFVersion maxDeviceApiVersion = FF_VERSION_INIT;
FFVersion maxDeviceConformanceVersion = FF_VERSION_INIT;
for(uint32_t i = 0; i < physicalDeviceCount; i++)
{
//Get device properties.
2023-02-11 12:34:57 +08:00
//On VK 1.1 and up, we use vkGetPhysicalDeviceProperties2, so we can put VkPhysicalDeviceDriverProperties in the pNext chain.
2022-06-01 00:04:58 +02:00
//This is required to get the driver name and conformance version.
VkPhysicalDeviceDriverPropertiesKHR driverProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR,
2023-02-11 12:34:57 +08:00
};
VkPhysicalDeviceProperties2 physicalDeviceProperties = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
.pNext = &driverProperties,
};
2022-06-01 00:04:58 +02:00
if(ffvkGetPhysicalDeviceProperties2 != NULL)
ffvkGetPhysicalDeviceProperties2(physicalDevices[i], &physicalDeviceProperties);
else
ffvkGetPhysicalDeviceProperties(physicalDevices[i], &physicalDeviceProperties.properties);
2023-12-30 18:12:21 +08:00
//We don't want software rasterizers to show up as physical gpu
if(physicalDeviceProperties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU)
continue;
2022-06-01 00:04:58 +02:00
//If the device api version is higher than the current highest device api version, overwrite it
//In this case, also use the current device driver name as the shown driver name
FFVersion deviceAPIVersion = FF_VERSION_INIT;
applyVulkanVersion(physicalDeviceProperties.properties.apiVersion, &deviceAPIVersion);
if(ffVersionCompare(&deviceAPIVersion, &maxDeviceApiVersion) > 0)
{
maxDeviceApiVersion = deviceAPIVersion;
applyDriverName(&driverProperties, &result->driver);
}
//If the device conformance version is higher than the current highest device conformance version, overwrite it
2023-02-11 12:34:57 +08:00
if(ffvkGetPhysicalDeviceProperties2)
{
FFVersion deviceConformanceVersion = {
.major = driverProperties.conformanceVersion.major,
.minor = driverProperties.conformanceVersion.minor,
.patch = driverProperties.conformanceVersion.patch,
};
if(ffVersionCompare(&deviceConformanceVersion, &maxDeviceConformanceVersion) > 0)
maxDeviceConformanceVersion = deviceConformanceVersion;
}
2022-06-01 00:04:58 +02:00
//Add the device to the list of devices shown by the GPU module
2023-05-16 20:52:14 +08:00
// #456
FF_LIST_FOR_EACH(FFGPUResult, gpu, result->gpus)
{
if (gpu->deviceId == physicalDeviceProperties.properties.deviceID)
2023-05-16 20:52:14 +08:00
goto next;
}
2022-09-05 17:42:42 +02:00
FFGPUResult* gpu = ffListAdd(&result->gpus);
ffStrbufInitF(&gpu->platformApi, "Vulkan %u.%u.%u", deviceAPIVersion.major, deviceAPIVersion.minor, deviceAPIVersion.patch);
gpu->deviceId = physicalDeviceProperties.properties.deviceID;
2023-05-16 20:52:14 +08:00
ffStrbufInitS(&gpu->name, physicalDeviceProperties.properties.deviceName);
2022-09-05 17:42:42 +02:00
gpu->type = physicalDeviceProperties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ? FF_GPU_TYPE_DISCRETE : FF_GPU_TYPE_INTEGRATED;
ffStrbufInitS(&gpu->vendor, ffGetGPUVendorString(physicalDeviceProperties.properties.vendorID));
2023-02-11 12:34:57 +08:00
ffStrbufInitS(&gpu->driver, driverProperties.driverInfo);
VkPhysicalDeviceMemoryProperties memoryProperties = {};
ffvkGetPhysicalDeviceMemoryProperties(physicalDevices[i], &memoryProperties);
2023-02-11 12:34:57 +08:00
gpu->dedicated.total = gpu->shared.total = 0;
gpu->dedicated.used = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
for(uint32_t index = 0; index < memoryProperties.memoryHeapCount; ++index)
2023-02-11 12:34:57 +08:00
{
const VkMemoryHeap* heap = &memoryProperties.memoryHeaps[index];
2023-02-11 12:34:57 +08:00
FFGPUMemory* vmem = gpu->type == FF_GPU_TYPE_DISCRETE && (heap->flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) ? &gpu->dedicated : &gpu->shared;
vmem->total += heap->size;
}
2023-01-12 11:41:07 +01:00
2022-09-05 17:42:42 +02:00
//No way to detect those using vulkan
2022-09-17 01:47:56 +08:00
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
2022-09-05 17:42:42 +02:00
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
2023-05-16 20:52:14 +08:00
next:
continue;
2022-06-01 00:04:58 +02:00
}
ffVersionToPretty(&instanceVersion, &result->instanceVersion);
ffVersionToPretty(&maxDeviceApiVersion, &result->apiVersion);
2022-06-01 00:04:58 +02:00
ffVersionToPretty(&maxDeviceConformanceVersion, &result->conformanceVersion);
ffvkDestroyInstance(vkInstance, NULL);
return NULL;
2022-06-01 00:04:58 +02:00
}
#endif
FFVulkanResult* ffDetectVulkan(void)
2022-06-01 00:04:58 +02:00
{
static FFVulkanResult result;
2023-12-30 18:12:21 +08:00
if (result.gpus.elementSize == 0)
{
ffStrbufInit(&result.driver);
ffStrbufInit(&result.apiVersion);
ffStrbufInit(&result.conformanceVersion);
ffStrbufInit(&result.instanceVersion);
2023-12-30 18:12:21 +08:00
ffListInit(&result.gpus, sizeof(FFGPUResult));
#ifdef FF_HAVE_VULKAN
result.error = detectVulkan(&result);
#else
result.error = "fastfetch was compiled without vulkan support";
#endif
}
2022-06-01 00:04:58 +02:00
return &result;
}