UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StatsData.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
5#include "Chaos/Box.h"
7
8// Wrap use of FStatData in CHAOS_COLLISION_STAT to remove impact in shipping builds
9#if UE_BUILD_SHIPPING
10#define CHAOS_COLLISION_STAT(X)
11#define CHAOS_COLLISION_STAT_DISABLED(X) X
12#else
13#define CHAOS_COLLISION_STAT(X) X;
14#define CHAOS_COLLISION_STAT_DISABLED(X)
15#endif
16
17namespace Chaos
18{
19 namespace CollisionStats
20 {
21
22 static constexpr int32 BucketSizes2[] = { 0,1,4,8,16,32,64,128,512,MAX_int32 };
23
24 template <bool bGatherStats>
26 {
28
31 FString ToString() const { return TEXT(""); }
32 };
33
34 template <>
36 {
37 int32 BucketCount[UE_ARRAY_COUNT(BucketSizes2)];
39
41 {
42 FMemory::Memset(BucketCount, 0, sizeof(BucketCount));
43 MaxCount = 0;
44 }
45
47 {
48 for (int32 BucketIdx = 1; BucketIdx < UE_ARRAY_COUNT(BucketSizes2); ++BucketIdx)
49 {
50 if (Count >= BucketSizes2[BucketIdx - 1] && Count < BucketSizes2[BucketIdx])
51 {
52 ++BucketCount[BucketIdx];
53 }
54 }
55
56 if (Count > MaxCount)
57 {
59 }
60 }
61
62 FString ToString() const
63 {
64 FString OutLog;
66 for (int32 Count : BucketCount)
67 {
69 {
71 }
72 }
73
74 const float CountPerChar = static_cast<float>(MaxBucketCount) / 20.f;
75 for (int32 Idx = 1; Idx < UE_ARRAY_COUNT(BucketSizes2); ++Idx)
76 {
77 int32 NumChars = static_cast<int32>(static_cast<float>(BucketCount[Idx]) / CountPerChar);
78 if (Idx < UE_ARRAY_COUNT(BucketSizes2) - 1)
79 {
80 OutLog += FString::Printf(TEXT("\t[%4d - %4d) (%4d) |"), BucketSizes2[Idx - 1], BucketSizes2[Idx], BucketCount[Idx]);
81 }
82 else
83 {
84 OutLog += FString::Printf(TEXT("\t[%4d - inf) (%4d) |"), BucketSizes2[Idx - 1], BucketCount[Idx]);
85 }
86 for (int32 Count = 0; Count < NumChars; ++Count)
87 {
88 OutLog += TEXT("-");
89 }
90 OutLog += TEXT("\n");
91 }
92
93 return OutLog;
94 }
95 };
96
98 {
100
101
106
108 {
109 BoundsDistribution.Record(static_cast<int32>(Box1.Extents().GetMax()));
110 }
111
117
119 {
120 CountNP += Count;
121 }
122
124 {
125 ++RejectedNP;
126 }
127
135
136 void Print()
137 {
138 FString OutLog;
139#if CHAOS_PARTICLEHANDLE_TODO
140 const float NumParticles = InParticles.Size();
141 OutLog = FString::Printf(TEXT("ComputeConstraints stats:\n"
142 "Total Particles:%d\nSimulated Particles:%d (%.2f%%)\n"
143 "Max candidates per instance:%d (%.2f%%)\n"
144 "Max candidates skipped per instance (NP skipped):%d (%.2f%%)\n"
145 "Max narrow phase tests per instance:%d (%.2f%%)\n"
146 "Max narrow phase rejected per instance (NP rejected):%d (%.2f%%)\n"
147 "Constraints generated:%d\n"
148 ),
149 InParticles.Size(),
150 SimulatedParticles, SimulatedParticles / NumParticles * 100.f,
155 Constraints.Num()
156 );
157
158 OutLog += FString::Printf(TEXT("Potentials per instance distribution:\n"));
160
161
162 OutLog += FString::Printf(TEXT("\nCandidates skipped per instance (NP skipped) distribution:\n"));
164
165 OutLog += FString::Printf(TEXT("\nNarrow phase performed per instance distribution:\n"));
167
168 OutLog += FString::Printf(TEXT("\nNarrow phase candidates rejected per instance distribution:\n"));
170
171 OutLog += FString::Printf(TEXT("\nBounds distribution:\n"));
173
174 UE_LOG(LogChaos, Warning, TEXT("%s"), *OutLog);
175
177 bPendingHierarchyDump = false;
178#endif
179 }
180
190 };
191
193 {
195 : StatImp(nullptr)
196 {
197 CHAOS_COLLISION_STAT({ if (bGatherStats) { StatImp = new FStatDataImp(); } });
198 }
199
201 {
202 CHAOS_COLLISION_STAT({ if (IsEnabled()) { delete StatImp; } });
203 }
204
206 {
207 CHAOS_COLLISION_STAT({ return StatImp != nullptr; })
208 CHAOS_COLLISION_STAT_DISABLED({ return false; })
209 }
210
215
217 {
218 CHAOS_COLLISION_STAT({ if (IsEnabled()) { StatImp->RecordBoundsData(Box1); } });
219 }
220
225
227 {
228 CHAOS_COLLISION_STAT({ if (IsEnabled()) { StatImp->IncrementCountNP(Count); } })
229 }
230
232 {
233 CHAOS_COLLISION_STAT({ if (IsEnabled()) { StatImp->IncrementRejectedNP(); } });
234 }
235
237 {
238 CHAOS_COLLISION_STAT({ if (IsEnabled()) { StatImp->FinalizeData(); } });
239 }
240
242 {
243 CHAOS_COLLISION_STAT({ if (IsEnabled()) { StatImp->Print(); } });
244 }
245
246 private:
247 FStatDataImp* StatImp;
248 };
249
250
251 }
252
253}
254
255
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define CHAOS_COLLISION_STAT_DISABLED(X)
Definition StatsData.h:14
#define CHAOS_COLLISION_STAT(X)
Definition StatsData.h:13
return true
Definition ExternalRpcRegistry.cpp:601
#define UE_LOG(CategoryName, Verbosity, Format,...)
Definition LogMacros.h:270
@ Num
Definition MetalRHIPrivate.h:234
#define MAX_int32
Definition NumericLimits.h:25
#define UE_ARRAY_COUNT(array)
Definition UnrealTemplate.h:212
Definition Constraints.Build.cs:6
Definition SkeletalMeshComponent.h:307
CHAOS_API bool bPendingHierarchyDump
Definition PBDRigidsEvolutionGBF.cpp:38
Definition StatsData.h:98
void IncrementCountNP(int32 Count=1)
Definition StatsData.h:118
FStatHelper< true > NarrowPhaseSkipped
Definition StatsData.h:186
void IncrementSimulatedParticles()
Definition StatsData.h:102
FStatDataImp()
Definition StatsData.h:99
int32 NumPotentials
Definition StatsData.h:184
int32 CountNP
Definition StatsData.h:181
FStatHelper< true > NarrowPhasePerformed
Definition StatsData.h:187
void RecordBoundsData(const FAABB3 &Box1)
Definition StatsData.h:107
int32 RejectedNP
Definition StatsData.h:182
void Print()
Definition StatsData.h:136
int32 SimulatedParticles
Definition StatsData.h:183
FStatHelper< true > NarrowPhaseRejected
Definition StatsData.h:188
FStatHelper< true > BoundsDistribution
Definition StatsData.h:189
void FinalizeData()
Definition StatsData.h:128
FStatHelper< true > BroadphasePotentials
Definition StatsData.h:185
void IncrementRejectedNP()
Definition StatsData.h:123
void RecordBroadphasePotentials(int32 Num)
Definition StatsData.h:112
Definition StatsData.h:193
~FStatData()
Definition StatsData.h:200
FORCEINLINE void RecordBoundsData(const FAABB3 &Box1)
Definition StatsData.h:216
FORCEINLINE void IncrementSimulatedParticles()
Definition StatsData.h:211
FORCEINLINE void Print()
Definition StatsData.h:241
FORCEINLINE void RecordBroadphasePotentials(int32 Num)
Definition StatsData.h:221
FORCEINLINE void FinalizeData()
Definition StatsData.h:236
FORCEINLINE void IncrementRejectedNP()
Definition StatsData.h:231
FORCEINLINE void IncrementCountNP(int32 Count=1)
Definition StatsData.h:226
FORCEINLINE bool IsEnabled() const
Definition StatsData.h:205
FStatData(bool bGatherStats)
Definition StatsData.h:194
void Record(int32 Count)
Definition StatsData.h:46
FStatHelper()
Definition StatsData.h:40
int32 MaxCount
Definition StatsData.h:38
FString ToString() const
Definition StatsData.h:62
Definition StatsData.h:26
int32 MaxCount
Definition StatsData.h:27
FStatHelper()
Definition StatsData.h:29
void Record(int32 Count)
Definition StatsData.h:30
FString ToString() const
Definition StatsData.h:31
static UE_FORCEINLINE_HINT void * Memset(void *Dest, uint8 Char, SIZE_T Count)
Definition UnrealMemory.h:119