Hi, is it possible to get the current VRAM usage in C++? Through RHI or something?
I attached an example screenshot of what I want to achieve. Thanks in advance.
Hi, is it possible to get the current VRAM usage in C++? Through RHI or something?
I attached an example screenshot of what I want to achieve. Thanks in advance.
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.
*.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");
}
}
}
#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
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.
YourGame.Build.cs
):PrivateDependencyModuleNames.AddRange(new string[] {
"YourNewModule"
});
Thatās it, tested and working in 5.1. Hope that helps!
References:
ApplicationCore.Build.cs
in Engine/Source/Runtime/ApplicationCore/
WindowsPlatformApplicationMisc.h
in Engine/Source/Runtime/ApplicationCore/Public/Windows
Hi!
Thanks for the sharing. Iām having some issues and would like to consult you about this error.
Error : Unable to find āclassā, ādelegateā, āenumā, or āstructā with name āsize_tā
I tried to āincludeā but none works.
Any idea?
You canāt have UPROPERTY of just any type, only those that are supported explicitly by the engine (int, bool, float, etc) and those that use the various engine macros (USTRUCT , UCLASS, etc).
size_t is neither. If you want a UPROPERTY, youāll have to choose from something more explicit like int, int32, int64, uint32, etc.