Skip to content

Commit c250e87

Browse files
committed
Fix same memory for different GPUs
1 parent 69198ca commit c250e87

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

OpenHardwareMonitorLib/Hardware/Gpu/NvidiaGpu.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,14 +696,31 @@ public override void Update()
696696
}
697697
}
698698

699-
if (_displayHandle != null)
699+
if (NvApi.NvAPI_GPU_GetMemoryInfoEx != null || _displayHandle != null)
700700
{
701-
NvApi.NvMemoryInfo memoryInfo = GetMemoryInfo(out status);
701+
uint free = 0;
702+
uint total = 0;
703+
704+
//Size in bytes
705+
NvApi.NvMemoryInfoEx memoryInfoEx = GetMemoryInfoEx(out status);
702706
if (status == NvApi.NvStatus.OK)
703707
{
704-
uint free = memoryInfo.CurrentAvailableDedicatedVideoMemory;
705-
uint total = memoryInfo.DedicatedVideoMemory;
708+
free = (uint)(memoryInfoEx.CurrentAvailableDedicatedVideoMemory / 1024);
709+
total = (uint)(memoryInfoEx.DedicatedVideoMemory / 1024);
710+
}
711+
else
712+
{
713+
//Size in kilobytes, fallback for older drivers
714+
NvApi.NvMemoryInfo memoryInfo = GetMemoryInfo(out status);
715+
if (status == NvApi.NvStatus.OK)
716+
{
717+
free = memoryInfo.CurrentAvailableDedicatedVideoMemory;
718+
total = memoryInfo.DedicatedVideoMemory;
719+
}
720+
}
706721

722+
if (status == NvApi.NvStatus.OK)
723+
{
707724
_memoryTotal.Value = total / 1024;
708725
ActivateSensor(_memoryTotal);
709726

@@ -1136,6 +1153,23 @@ private NvApi.NvMemoryInfo GetMemoryInfo(out NvApi.NvStatus status)
11361153
return status == NvApi.NvStatus.OK ? memoryInfo : default;
11371154
}
11381155

1156+
private NvApi.NvMemoryInfoEx GetMemoryInfoEx(out NvApi.NvStatus status)
1157+
{
1158+
if (NvApi.NvAPI_GPU_GetMemoryInfoEx == null)
1159+
{
1160+
status = NvApi.NvStatus.Error;
1161+
return default;
1162+
}
1163+
1164+
NvApi.NvMemoryInfoEx memoryInfoEx = new()
1165+
{
1166+
Version = (uint)NvApi.MAKE_NVAPI_VERSION<NvApi.NvMemoryInfoEx>(1)
1167+
};
1168+
1169+
status = NvApi.NvAPI_GPU_GetMemoryInfoEx(_handle, ref memoryInfoEx);
1170+
return status == NvApi.NvStatus.OK ? memoryInfoEx : default;
1171+
}
1172+
11391173
private NvApi.NvGpuClockFrequencies GetClockFrequencies(out NvApi.NvStatus status)
11401174
{
11411175
if (NvApi.NvAPI_GPU_GetAllClockFrequencies == null)

OpenHardwareMonitorLib/Interop/NvApi.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ internal static class NvApi
4141
public static NvAPI_GPU_GetCoolerSettingsDelegate NvAPI_GPU_GetCoolerSettings { get; internal set; }
4242
public static NvAPI_GPU_GetDynamicPstatesInfoExDelegate NvAPI_GPU_GetDynamicPstatesInfoEx { get; internal set; }
4343
public static NvAPI_GPU_GetMemoryInfoDelegate NvAPI_GPU_GetMemoryInfo { get; internal set; }
44+
public static NvAPI_GPU_GetMemoryInfoExDelegate NvAPI_GPU_GetMemoryInfoEx { get; internal set; }
4445
public static NvAPI_GPU_GetPCIIdentifiersDelegate NvAPI_GPU_GetPCIIdentifiers { get; internal set; }
4546
public static NvAPI_GPU_GetTachReadingDelegate NvAPI_GPU_GetTachReading { get; internal set; }
4647
public static NvAPI_GPU_GetThermalSettingsDelegate NvAPI_GPU_GetThermalSettings { get; internal set; }
@@ -83,6 +84,7 @@ public static void Initialize()
8384
NvAPI_GPU_GetCoolerSettings = GetDelegate<NvAPI_GPU_GetCoolerSettingsDelegate>(0xDA141340);
8485
NvAPI_GPU_SetCoolerLevels = GetDelegate<NvAPI_GPU_SetCoolerLevelsDelegate>(0x891FA0AE);
8586
NvAPI_GPU_GetMemoryInfo = GetDelegate<NvAPI_GPU_GetMemoryInfoDelegate>(0x774AA982);
87+
NvAPI_GPU_GetMemoryInfoEx = GetDelegate<NvAPI_GPU_GetMemoryInfoExDelegate>(0xC0599498);
8688
NvAPI_GetDisplayDriverVersion = GetDelegate<NvAPI_GetDisplayDriverVersionDelegate>(0xF951A4D1);
8789
_nvAPI_GetInterfaceVersionString = GetDelegate<NvAPI_GetInterfaceVersionStringDelegate>(0x01053FA5);
8890
NvAPI_GPU_GetPCIIdentifiers = GetDelegate<NvAPI_GPU_GetPCIIdentifiersDelegate>(0x2DDFB66E);
@@ -145,6 +147,9 @@ public static void Initialize()
145147
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
146148
public delegate NvStatus NvAPI_GPU_GetMemoryInfoDelegate(NvDisplayHandle displayHandle, ref NvMemoryInfo nvMemoryInfo);
147149

150+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
151+
public delegate NvStatus NvAPI_GPU_GetMemoryInfoExDelegate(NvPhysicalGpuHandle gpuHandle, ref NvMemoryInfoEx nvMemoryInfo);
152+
148153
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
149154
public delegate NvStatus NvAPI_GPU_GetPCIIdentifiersDelegate(NvPhysicalGpuHandle gpuHandle, out uint deviceId, out uint subSystemId, out uint revisionId, out uint extDeviceId);
150155

@@ -575,6 +580,24 @@ internal struct NvMemoryInfo
575580
public uint CurrentAvailableDedicatedVideoMemory;
576581
}
577582

583+
[StructLayout(LayoutKind.Sequential)]
584+
internal struct NvMemoryInfoEx
585+
{
586+
public uint Version;
587+
public ulong DedicatedVideoMemory;
588+
public ulong AvailableDedicatedVideoMemory;
589+
public ulong SystemVideoMemory;
590+
public ulong SharedSystemMemory;
591+
592+
public ulong CurrentAvailableDedicatedVideoMemory;
593+
594+
public ulong DedicatedVideoMemoryEvictionsSize;
595+
public ulong DedicatedVideoMemoryEvictionCount;
596+
597+
public ulong DedicatedVideoMemoryPromotionsSize;
598+
public ulong DedicatedVideoMemoryPromotionCount;
599+
}
600+
578601
[StructLayout(LayoutKind.Sequential, Pack = 8)]
579602
internal struct NvDisplayDriverVersion
580603
{

0 commit comments

Comments
 (0)