[Win32] Use nvapi on windows for gpu usage

This commit is contained in:
FlightlessMango 2020-06-08 01:59:05 +02:00
parent 1756d53126
commit 74e096773e
4 changed files with 73 additions and 0 deletions

View File

@ -6,6 +6,7 @@ struct gpuInfo gpu_info;
amdgpu_files amdgpu {}; amdgpu_files amdgpu {};
void getNvidiaGpuInfo(){ void getNvidiaGpuInfo(){
#ifdef __gnu_linux__
if (nvmlSuccess){ if (nvmlSuccess){
getNVMLInfo(); getNVMLInfo();
gpu_info.load = nvidiaUtilization.gpu; gpu_info.load = nvidiaUtilization.gpu;
@ -26,6 +27,10 @@ void getNvidiaGpuInfo(){
gpu_info.powerUsage = 0; gpu_info.powerUsage = 0;
} }
#endif #endif
#endif
#ifdef __WIN32
nvapi_util();
#endif
} }
void getAmdGpuInfo(){ void getAmdGpuInfo(){

View File

@ -48,6 +48,7 @@ if ['windows', 'mingw'].contains(host_machine.system())
'file_utils_win32.cpp', 'file_utils_win32.cpp',
'loaders/loader_nvml_win32.cpp', 'loaders/loader_nvml_win32.cpp',
'cpu_win32.cpp', 'cpu_win32.cpp',
'nvapi.cpp',
# 'MangoHud.def', # 'MangoHud.def',
) )
endif endif

65
src/nvapi.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <windows.h>
#include <iostream>
#include "nvidia_info.h"
#include "gpu.h"
// magic numbers, do not change them
#define NVAPI_MAX_PHYSICAL_GPUS 64
#define NVAPI_MAX_USAGES_PER_GPU 34
// function pointer types
typedef int *(*NvAPI_QueryInterface_t)(unsigned int offset);
typedef int (*NvAPI_Initialize_t)();
typedef int (*NvAPI_EnumPhysicalGPUs_t)(int **handles, int *count);
typedef int (*NvAPI_GPU_GetUsages_t)(int *handle, unsigned int *usages);
NvAPI_QueryInterface_t NvAPI_QueryInterface = NULL;
NvAPI_Initialize_t NvAPI_Initialize = NULL;
NvAPI_EnumPhysicalGPUs_t NvAPI_EnumPhysicalGPUs = NULL;
NvAPI_GPU_GetUsages_t NvAPI_GPU_GetUsages = NULL;
HMODULE hmod;
bool init_nvapi_bool;
int *gpuHandles[NVAPI_MAX_PHYSICAL_GPUS] = { NULL };
int gpuCount = 0;
unsigned int gpuUsages[NVAPI_MAX_USAGES_PER_GPU] = { 0 };
bool checkNVAPI(){
if (MANGOHUD_ARCH == "64bit")
hmod = LoadLibraryA("nvapi64.dll");
else
hmod = LoadLibraryA("nvapi.dll");
if (hmod == NULL)
{
printf("Failed to load nvapi.dll");
return false;
}
NvAPI_QueryInterface = (NvAPI_QueryInterface_t) GetProcAddress(hmod, "nvapi_QueryInterface");
NvAPI_Initialize = (NvAPI_Initialize_t) (*NvAPI_QueryInterface)(0x0150E828);
NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) (*NvAPI_QueryInterface)(0xE5AC921F);
NvAPI_GPU_GetUsages = (NvAPI_GPU_GetUsages_t) (*NvAPI_QueryInterface)(0x189A1FDF);
if (NvAPI_Initialize == NULL || NvAPI_EnumPhysicalGPUs == NULL ||
NvAPI_EnumPhysicalGPUs == NULL || NvAPI_GPU_GetUsages == NULL)
{
std::cerr << "Couldn't get functions in nvapi.dll" << std::endl;
return 2;
}
(*NvAPI_Initialize)();
int *gpuHandles[NVAPI_MAX_PHYSICAL_GPUS] = { NULL };
return true;
}
void nvapi_util()
{
if (!init_nvapi_bool){
init_nvapi_bool = checkNVAPI();
}
gpuUsages[0] = (NVAPI_MAX_USAGES_PER_GPU * 4) | 0x10000;
(*NvAPI_EnumPhysicalGPUs)(gpuHandles, &gpuCount);
(*NvAPI_GPU_GetUsages)(gpuHandles[0], gpuUsages);
gpu_info.load = gpuUsages[3];
}

View File

@ -8,4 +8,6 @@ extern struct nvmlMemory_st nvidiaMemory;
extern bool nvmlSuccess; extern bool nvmlSuccess;
bool checkNVML(const char* pciBusId); bool checkNVML(const char* pciBusId);
bool checkNVAPI(void);
bool getNVMLInfo(void); bool getNVMLInfo(void);
void nvapi_util(void);