From 315a767fa03211bcb4b7e62a0e07178ec7d50f47 Mon Sep 17 00:00:00 2001 From: Blacktempel <6657796+Blacktempel@users.noreply.github.com> Date: Thu, 19 Feb 2026 16:21:49 +0100 Subject: [PATCH] Fix same memory for different GPUs. (#2232) * Fix same memory for different GPUs. * Status check --- .../Hardware/Gpu/NvidiaGpu.cs | 42 +++++++++++++++++-- LibreHardwareMonitorLib/Interop/NvApi.cs | 23 ++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/LibreHardwareMonitorLib/Hardware/Gpu/NvidiaGpu.cs b/LibreHardwareMonitorLib/Hardware/Gpu/NvidiaGpu.cs index 929494b..cca01b6 100644 --- a/LibreHardwareMonitorLib/Hardware/Gpu/NvidiaGpu.cs +++ b/LibreHardwareMonitorLib/Hardware/Gpu/NvidiaGpu.cs @@ -702,14 +702,31 @@ internal sealed class NvidiaGpu : GenericGpu } } - if (_displayHandle != null) + if (NvApi.NvAPI_GPU_GetMemoryInfoEx != null || _displayHandle != null) { - NvApi.NvMemoryInfo memoryInfo = GetMemoryInfo(out status); + uint free = 0; + uint total = 0; + + //Size in bytes + NvApi.NvMemoryInfoEx memoryInfoEx = GetMemoryInfoEx(out status); if (status == NvApi.NvStatus.OK) { - uint free = memoryInfo.CurrentAvailableDedicatedVideoMemory; - uint total = memoryInfo.DedicatedVideoMemory; + free = (uint)(memoryInfoEx.CurrentAvailableDedicatedVideoMemory / 1024); + total = (uint)(memoryInfoEx.DedicatedVideoMemory / 1024); + } + else + { + //Size in kilobytes, fallback for older drivers + NvApi.NvMemoryInfo memoryInfo = GetMemoryInfo(out status); + if (status == NvApi.NvStatus.OK) + { + free = memoryInfo.CurrentAvailableDedicatedVideoMemory; + total = memoryInfo.DedicatedVideoMemory; + } + } + if (status == NvApi.NvStatus.OK) + { _memoryTotal.Value = total / 1024; ActivateSensor(_memoryTotal); @@ -1142,6 +1159,23 @@ internal sealed class NvidiaGpu : GenericGpu return status == NvApi.NvStatus.OK ? memoryInfo : default; } + private NvApi.NvMemoryInfoEx GetMemoryInfoEx(out NvApi.NvStatus status) + { + if (NvApi.NvAPI_GPU_GetMemoryInfoEx == null) + { + status = NvApi.NvStatus.Error; + return default; + } + + NvApi.NvMemoryInfoEx memoryInfoEx = new() + { + Version = (uint)NvApi.MAKE_NVAPI_VERSION(1) + }; + + status = NvApi.NvAPI_GPU_GetMemoryInfoEx(_handle, ref memoryInfoEx); + return status == NvApi.NvStatus.OK ? memoryInfoEx : default; + } + private NvApi.NvGpuClockFrequencies GetClockFrequencies(out NvApi.NvStatus status) { if (NvApi.NvAPI_GPU_GetAllClockFrequencies == null) diff --git a/LibreHardwareMonitorLib/Interop/NvApi.cs b/LibreHardwareMonitorLib/Interop/NvApi.cs index a0bcb05..8ab5950 100644 --- a/LibreHardwareMonitorLib/Interop/NvApi.cs +++ b/LibreHardwareMonitorLib/Interop/NvApi.cs @@ -42,6 +42,7 @@ internal static class NvApi public static NvAPI_GPU_GetCoolerSettingsDelegate NvAPI_GPU_GetCoolerSettings { get; internal set; } public static NvAPI_GPU_GetDynamicPstatesInfoExDelegate NvAPI_GPU_GetDynamicPstatesInfoEx { get; internal set; } public static NvAPI_GPU_GetMemoryInfoDelegate NvAPI_GPU_GetMemoryInfo { get; internal set; } + public static NvAPI_GPU_GetMemoryInfoExDelegate NvAPI_GPU_GetMemoryInfoEx { get; internal set; } public static NvAPI_GPU_GetPCIIdentifiersDelegate NvAPI_GPU_GetPCIIdentifiers { get; internal set; } public static NvAPI_GPU_GetTachReadingDelegate NvAPI_GPU_GetTachReading { get; internal set; } public static NvAPI_GPU_GetThermalSettingsDelegate NvAPI_GPU_GetThermalSettings { get; internal set; } @@ -84,6 +85,7 @@ internal static class NvApi NvAPI_GPU_GetCoolerSettings = GetDelegate(0xDA141340); NvAPI_GPU_SetCoolerLevels = GetDelegate(0x891FA0AE); NvAPI_GPU_GetMemoryInfo = GetDelegate(0x774AA982); + NvAPI_GPU_GetMemoryInfoEx = GetDelegate(0xC0599498); NvAPI_GetDisplayDriverVersion = GetDelegate(0xF951A4D1); _nvAPI_GetInterfaceVersionString = GetDelegate(0x01053FA5); NvAPI_GPU_GetPCIIdentifiers = GetDelegate(0x2DDFB66E); @@ -146,6 +148,9 @@ internal static class NvApi [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate NvStatus NvAPI_GPU_GetMemoryInfoDelegate(NvDisplayHandle displayHandle, ref NvMemoryInfo nvMemoryInfo); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate NvStatus NvAPI_GPU_GetMemoryInfoExDelegate(NvPhysicalGpuHandle gpuHandle, ref NvMemoryInfoEx nvMemoryInfo); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate NvStatus NvAPI_GPU_GetPCIIdentifiersDelegate(NvPhysicalGpuHandle gpuHandle, out uint deviceId, out uint subSystemId, out uint revisionId, out uint extDeviceId); @@ -576,6 +581,24 @@ internal static class NvApi public uint CurrentAvailableDedicatedVideoMemory; } + [StructLayout(LayoutKind.Sequential)] + internal struct NvMemoryInfoEx + { + public uint Version; + public ulong DedicatedVideoMemory; + public ulong AvailableDedicatedVideoMemory; + public ulong SystemVideoMemory; + public ulong SharedSystemMemory; + + public ulong CurrentAvailableDedicatedVideoMemory; + + public ulong DedicatedVideoMemoryEvictionsSize; + public ulong DedicatedVideoMemoryEvictionCount; + + public ulong DedicatedVideoMemoryPromotionsSize; + public ulong DedicatedVideoMemoryPromotionCount; + } + [StructLayout(LayoutKind.Sequential, Pack = 8)] internal struct NvDisplayDriverVersion {