Surprised it took 5 years for someone to figure this out, but here’s how you do it. This will allow you to retrieve used, available, and total VRAM figures, but only on Windows.
- Create a new gameplay module. You can follow the guide in the Unreal Docs here.
- The new module should have the following
*.Build.cs
file contents:
using UnrealBuildTool;
public class LowLevelTelemetry : ModuleRules {
public LowLevelTelemetry(ReadOnlyTargetRules Target) : base(Target) {
PrivateDependencyModuleNames.AddRange(new string[] {
"Core",
"CoreUObject",
"Engine"
});
PublicIncludePathModuleNames.AddRange(new string[] { "RHI" });
if ((Target.IsInPlatformGroup(UnrealPlatformGroup.Windows)))
{
// Uses DXGI to query GPU hardware
// This is what will allow us to get GPU usage statistics at runtime
PublicSystemLibraries.Add("DXGI.lib");
}
}
}
- In the module’s main header file, add the following includes:
#include <CoreMinimal.h>
#include "Windows/WindowsHWrapper.h"
THIRD_PARTY_INCLUDES_START
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
#include "dxgi1_4.h"
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"
THIRD_PARTY_INCLUDES_END
- Now for the implementations:
static size_t GetUsedVideoMemory() {
IDXGIFactory4* pFactory;
CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**)&pFactory);
IDXGIAdapter3* adapter;
pFactory->EnumAdapters(0, reinterpret_cast<IDXGIAdapter**>(&adapter));
DXGI_QUERY_VIDEO_MEMORY_INFO videoMemoryInfo;
adapter->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &videoMemoryInfo);
size_t usedVRAM = videoMemoryInfo.CurrentUsage / 1024 / 1024;
return usedVRAM;
}
You can get
videoMemoryInfo.AvailableForReservation
and
videoMemoryInfo.Budget
to get available and total VRAM as well.
- Add the new module as a dependency to your game module (
YourGame.Build.cs
):
PrivateDependencyModuleNames.AddRange(new string[] {
"YourNewModule"
});
That’s it, tested and working in 5.1. Hope that helps!
References:
- c++ - Directx get VRAM used by game - Stack Overflow
-
ApplicationCore.Build.cs
inEngine/Source/Runtime/ApplicationCore/
-
WindowsPlatformApplicationMisc.h
inEngine/Source/Runtime/ApplicationCore/Public/Windows