UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RHIStats.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "MultiGPU.h"
7#include "RHIDefinitions.h"
8#include "RHIGlobals.h"
9#include "Stats/Stats.h"
10
12{
13 // Hardware state (never change after device creation):
14
15 // -1 if unknown, in bytes
17
18 // -1 if unknown, in bytes
20
21 // -1 if unknown, in bytes
23
24 // Total amount of "graphics memory" that we think we can use for all our graphics resources, in bytes. -1 if unknown.
26
27 // Size of memory allocated to streaming textures, in bytes
29
30 // Size of memory allocated to non-streaming textures, in bytes
32
33 // Size of the largest memory fragment, in bytes
35
36 // 0 if streaming pool size limitation is disabled, in bytes
38
40 {
42 {
43 // Max in case the device failed to report the available working memory
44 return FMath::Max(TotalGraphicsMemory, DedicatedVideoMemory);
45 }
46
48 }
49
51 {
52 // pardon the redundancy, have a broken compiler (__EMSCRIPTEN__) that needs these types spelled out...
54 }
55
57 {
58 return TexturePoolSize > 0;
59 }
60
62 {
63 return FMath::Max<int64>(TexturePoolSize - StreamingMemorySize, 0);
64 }
65};
66
67
68// GPU stats
69
72
73#if HAS_GPU_STATS
74
76{
79
80 bool ShouldCountDraws() const { return Index != -1; }
81
82 FName const Name;
83 uint32 const Index;
84
85 static constexpr int32 MAX_DRAWCALL_CATEGORY = 31;
86
87 struct FManager
88 {
90
91 // A backup of the counts that can be used to display on screen to avoid flickering.
93
95
96 RHI_API FManager();
97 };
98
99 RHI_API static FManager& GetManager();
100};
101
102// RHI counter stats.
106
107#else
108
110{
111 static constexpr uint32 Index = 0;
112};
113
114#endif
115
116// Macros for use inside RHI context Draw/Dispatch functions.
117// Updates the Stats structure on the executing RHI command list
118#if RHI_NEW_GPU_PROFILER
119 #define RHI_DRAW_CALL_STATS(Type,Verts,Prims,Instances) \
120 do \
121 { \
122 StatEvent.NumDraws++; \
123 StatEvent.NumPrimitives += Prims * FMath::Max(1u, Instances); \
124 StatEvent.NumVertices += Verts * FMath::Max(1u, Instances); \
125 GetExecutingCommandList().Stats_AddDrawAndPrimitives(Type, Prims); \
126 } while (false)
127
128 #define RHI_DRAW_CALL_INC() do { StatEvent.NumDraws++; GetExecutingCommandList().Stats_AddDraw(); } while (false)
129 #define RHI_DISPATCH_CALL_INC() do { StatEvent.NumDispatches++; } while (false)
130#else
131 #define RHI_DRAW_CALL_INC() do { GetExecutingCommandList().Stats_AddDraw(); } while (false)
132 #define RHI_DRAW_CALL_STATS(Type,Verts,Prims,Instances) do { GetExecutingCommandList().Stats_AddDrawAndPrimitives(Type, Prims); } while (false)
133 #define RHI_DISPATCH_CALL_INC() do { } while (false)
134#endif
135
137{
138#if HAS_GPU_STATS
139 // The +1 is for "uncategorised"
140 static constexpr int32 NumCategories = FRHIDrawStatsCategory::MAX_DRAWCALL_CATEGORY + 1;
141#else
142 static constexpr int32 NumCategories = 1;
143#endif
144
145 static constexpr int32 NoCategory = NumCategories - 1;
146
148 {
155
157 {
158 return Triangles
159 + Lines
160 + Quads
161 + Points
162 + Rectangles;
163 }
164
166 {
167 Draws += RHS.Draws;
168 Triangles += RHS.Triangles;
169 Lines += RHS.Lines;
170 Quads += RHS.Quads;
171 Points += RHS.Points;
172 Rectangles += RHS.Rectangles;
173 return *this;
174 }
175 };
176
182
184 {
185 checkSlow(GPUIndex < UE_ARRAY_COUNT(GPUs));
186 return GPUs[GPUIndex];
187 }
188
190 {
191 Reset();
192 }
193
194 void Reset()
195 {
196 FMemory::Memzero(*this);
197 }
198
199 void AddDraw(FRHIGPUMask GPUMask, FRHIDrawStatsCategory const* Category)
200 {
201 uint32 CategoryIndex = Category ? Category->Index : NoCategory;
202 for (uint32 GPUIndex : GPUMask)
203 {
204 FPerCategory& Stats = GPUs[GPUIndex].Categories[CategoryIndex];
205 Stats.Draws++;
206 }
207 }
208
209 void AddDrawAndPrimitives(FRHIGPUMask GPUMask, FRHIDrawStatsCategory const* Category, EPrimitiveType PrimitiveType, uint32 NumPrimitives)
210 {
211 uint32 CategoryIndex = Category ? Category->Index : NoCategory;
212 for (uint32 GPUIndex : GPUMask)
213 {
214 FPerCategory& Stats = GPUs[GPUIndex].Categories[CategoryIndex];
215 Stats.Draws++;
216
217 switch (PrimitiveType)
218 {
219 case PT_TriangleList : Stats.Triangles += NumPrimitives; break;
220 case PT_TriangleStrip: Stats.Triangles += NumPrimitives; break;
221 case PT_LineList : Stats.Lines += NumPrimitives; break;
222 case PT_QuadList : Stats.Quads += NumPrimitives; break;
223 case PT_PointList : Stats.Points += NumPrimitives; break;
224 case PT_RectList : Stats.Rectangles += NumPrimitives; break;
225 }
226 }
227 }
228
231
232private:
233 FPerGPU GPUs[MAX_NUM_GPUS];
234};
235
236// RHI memory stats.
240
242
246
255
260
263
266
269
270#if PLATFORM_MICROSOFT
271
272// D3D memory stats.
273struct FD3DMemoryStats
274{
275 // Budget assigned by the OS. This can be considered the total memory
276 // the application should use, but an application can also go over-budget.
279
280 // Used memory.
281 uint64 UsedLocal = 0;
282 uint64 UsedSystem = 0;
283
284 // Over-budget memory. This is Budget - Used if Used > Budget.
287
288 // Available memory within budget. This is Budget - Used clamped to 0 if over-budget.
291
292 bool IsOverBudget() const
293 {
294 return DemotedLocal > 0 || DemotedSystem > 0;
295 }
296};
297
308
309// Update D3D memory stat counters and CSV profiler stats, if enabled.
310RHI_API void UpdateD3DMemoryStatsAndCSV(const FD3DMemoryStats& MemoryStats, bool bUpdateCSV);
311
312#endif // PLATFORM_MICROSOFT
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
#define DECLARE_MEMORY_STAT_POOL_EXTERN(CounterName, StatId, GroupId, Pool, API)
Definition Stats.h:688
#define DECLARE_CYCLE_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:679
#define DECLARE_DWORD_COUNTER_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:682
#define DECLARE_STATS_GROUP(GroupDesc, GroupId, GroupCat)
Definition Stats.h:689
#define DECLARE_DWORD_ACCUMULATOR_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:684
#define DECLARE_MEMORY_STAT_EXTERN(CounterName, StatId, GroupId, API)
Definition Stats.h:687
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define MAX_NUM_GPUS
Definition MultiGPU.h:25
EPrimitiveType
Definition RHIDefinitions.h:822
@ PT_RectList
Definition RHIDefinitions.h:844
@ PT_PointList
Definition RHIDefinitions.h:837
@ PT_LineList
Definition RHIDefinitions.h:830
@ PT_TriangleList
Definition RHIDefinitions.h:824
@ PT_QuadList
Definition RHIDefinitions.h:834
@ PT_TriangleStrip
Definition RHIDefinitions.h:827
#define GRHIDeviceIsIntegrated
Definition RHIGlobals.h:867
RHI_API int32 GNumPrimitivesDrawnRHI[MAX_NUM_GPUS]
Definition RHIStats.cpp:10
RHI_API int32 GNumDrawCallsRHI[MAX_NUM_GPUS]
Definition RHIStats.cpp:9
#define UE_ARRAY_COUNT(array)
Definition UnrealTemplate.h:212
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition NameTypes.h:617
Definition StaticArray.h:26
U16 Index
Definition radfft.cpp:71
@ MCR_GPU
Definition GenericPlatformMemory.h:252
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131
Definition RHIStats.h:110
static constexpr uint32 Index
Definition RHIStats.h:111
Definition RHIStats.h:148
uint32 Rectangles
Definition RHIStats.h:154
FPerCategory & operator+=(FPerCategory const &RHS)
Definition RHIStats.h:165
uint32 GetTotalPrimitives() const
Definition RHIStats.h:156
uint32 Points
Definition RHIStats.h:153
uint32 Quads
Definition RHIStats.h:152
uint32 Draws
Definition RHIStats.h:149
uint32 Lines
Definition RHIStats.h:151
uint32 Triangles
Definition RHIStats.h:150
Definition RHIStats.h:178
FPerCategory Categories[NumCategories]
Definition RHIStats.h:180
Definition RHIStats.h:137
FRHIDrawStats()
Definition RHIStats.h:189
void AddDraw(FRHIGPUMask GPUMask, FRHIDrawStatsCategory const *Category)
Definition RHIStats.h:199
RHI_API void ProcessAsFrameStats()
Definition RHI.cpp:1264
FPerGPU & GetGPU(uint32 GPUIndex)
Definition RHIStats.h:183
void AddDrawAndPrimitives(FRHIGPUMask GPUMask, FRHIDrawStatsCategory const *Category, EPrimitiveType PrimitiveType, uint32 NumPrimitives)
Definition RHIStats.h:209
static constexpr int32 NumCategories
Definition RHIStats.h:142
static constexpr int32 NoCategory
Definition RHIStats.h:145
void Reset()
Definition RHIStats.h:194
Definition MultiGPU.h:33
Definition RHIStats.h:12
uint64 StreamingMemorySize
Definition RHIStats.h:28
int64 SharedSystemMemory
Definition RHIStats.h:22
int64 TexturePoolSize
Definition RHIStats.h:37
int64 DedicatedSystemMemory
Definition RHIStats.h:19
int64 DedicatedVideoMemory
Definition RHIStats.h:16
bool AreHardwareStatsValid() const
Definition RHIStats.h:50
uint64 NonStreamingMemorySize
Definition RHIStats.h:31
int64 TotalGraphicsMemory
Definition RHIStats.h:25
bool IsUsingLimitedPoolSize() const
Definition RHIStats.h:56
int64 LargestContiguousAllocation
Definition RHIStats.h:34
int64 ComputeAvailableMemorySize() const
Definition RHIStats.h:61
int64 GetTotalDeviceWorkingMemory() const
Definition RHIStats.h:39