UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LevelStreamingProfilingSubsystem.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
7#include "Containers/Map.h"
10#include "Tasks/Task.h"
11#include "UObject/ObjectKey.h"
13
14#include "LevelStreamingProfilingSubsystem.generated.h"
15
16class AGameStateBase;
17class UWorld;
18class ULevel;
19class ULevelStreaming;
20
21enum class ELevelStreamingState : uint8;
23
25
30UCLASS(MinimalAPI)
32{
34
35public:
36 // WorldSubsystem interface
37 ENGINE_API virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
38 ENGINE_API virtual void PostInitialize() override;
39 ENGINE_API virtual void Deinitialize() override;
40
43
44 // Begin recording timings for level streaming events.
45 ENGINE_API void StartTracking();
46 // Top recording timings for level streaming events and output a .tsv (tab separated values) file to the Profiling directory.
47 ENGINE_API void StopTrackingAndReport();
48
49 inline bool IsTracking() const { return bIsTracking; }
50
51 // Gives child classes an opportunity to clean up after a report is produced.
52 virtual void PostReport() { }
53
54 // Access to tuning values set by cvars for other systems
55 /* Returns the squared distance (e.g. from world partition cell bounds) at which a level is considered to have streamed in too late. */
56 static ENGINE_API double GetLateStreamingDistanceSquared();
57
58#if (CSV_PROFILER_STATS && !UE_BUILD_SHIPPING)
60#endif
61
62protected:
63 // WorldSubsystem interface
64 ENGINE_API virtual bool DoesSupportWorldType(const EWorldType::Type WorldType) const;
65
66 enum class ELevelState;
67 struct FLevelStats;
68
78 virtual void UpdateTrackingData(
81 const ULevelStreaming* StreamingLevel,
82 ELevelState PreviousState,
83 ELevelState NewState) {}
84
91
99
101
102 ENGINE_API TConstArrayView<FLevelStats> GetLevelStats() const;
103
104 enum class ELevelState
105 {
106 None,
107 QueuedForLoading,
108 Loading,
109 Loaded,
110 QueuedForAddToWorld,
111 AddingToWorld,
112 AddedToWorld,
113 QueuedForRemoveFromWorld,
114 RemovingFromWorld,
115 RemovedFromWorld,
116 };
117
119 {
121 : StatsIndex(InStatsIndex)
122 {
123 }
124
125 ~FActiveLevel() = default;
126
127 ELevelState State = ELevelState::QueuedForLoading;
128 double StateStartTime = 0.0; // world time we entered the current state
129 TOptional<double> TimeAddedToWorld; // time we were last added to the world for calculating how long the level was streamed in for
130 int32 StatsIndex = INDEX_NONE; // Index into subsystem's stats array to update
131 };
132
134 {
135 FLevelStats() = default;
136 ~FLevelStats() = default;
137
138 FName PackageNameOnDisk = {}; // Could consider using FPackagePath here?
139 FName PackageNameInMemory = {};
140
141 FBox CellBounds = FBox(ForceInit), ContentBounds = FBox(ForceInit);
142
143 // All time units in seconds
151
152 // Distance at which this level was fully streamed in
155
156 // Location of streaming source (e.g. player) when level was fully streamed in
158
159 // Is this level a hierarchical LOD representation of more detailed content
160 bool bIsHLOD = false;
161 // If a level is tracked but never starts loading before being removed, we don't include it in the results
162 bool bValid = false;
163 };
164
165private:
166 // Callbacks for level streaming status updates
167 void OnLevelStreamingTargetStateChanged(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LevelIfLoaded, ELevelStreamingState CurrentState, ELevelStreamingTargetState PrevTarget, ELevelStreamingTargetState NewTarget);
168 void OnLevelStreamingStateChanged(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LevelIfLoaded, ELevelStreamingState PrevState, ELevelStreamingState NewState);
169
170 void OnLevelQueuedForLoading(UWorld* World, const ULevelStreaming* StreamingLevel);
171 void OnLevelUnqueuedForLoading(UWorld* World, const ULevelStreaming* StreamingLevel);
172 void OnLevelStartedAsyncLoading(UWorld* World, const ULevelStreaming* StreamingLevel);
173 void OnLevelFinishedAsyncLoading(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
174 void OnLevelQueuedForAddToWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
175 void OnLevelUnqueuedForAddToWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
176 void OnLevelStartedAddToWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
177 void OnLevelFinishedAddToWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
178 void OnLevelQueuedForRemoveFromWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
179 void OnLevelUnqueuedForRemoveFromWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
180 void OnLevelStartedRemoveFromWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
181 void OnLevelFinishedRemoveFromWorld(UWorld* World, const ULevelStreaming* StreamingLevel, ULevel* LoadedLevel);
182 void OnStreamingLevelRemoved(UWorld* World, const ULevelStreaming* StreamingLevel);
183
184 TUniquePtr<FActiveLevel> MakeActiveLevel(const ULevelStreaming* StreamingLevel, ELevelState InitialState, ULevel* LoadedLevel=nullptr);
185 void UpdateLevelState(FActiveLevel& Level, const ULevelStreaming* StreamingLevel, ELevelState NewState, double Time);
186
187 // Handles for registered callbacks
188 FDelegateHandle Handle_OnLevelStreamingTargetStateChanged;
189 FDelegateHandle Handle_OnLevelStreamingStateChanged;
190 FDelegateHandle Handle_OnLevelBeginAddToWorld;
191 FDelegateHandle Handle_OnLevelBeginRemoveFromWorld;
192
193 bool bIsTracking = false;
194
195 // A level currently pending or in the process of streaming
196 TMap<TObjectKey<const ULevelStreaming>, TUniquePtr<FActiveLevel>> ActiveLevels; // Can remove indirection when TMap supports forward declared values
197
198 // Stats for levels which have been at least partially loaded.
199 // May contain duplicates for a given level if it streamed out and back in again.
200 TArray<FLevelStats> LevelStats;
201
202 // Possibly-executing task dependend on our recorded stats. Cannot start recording again until this task is complete.
203 UE::Tasks::TTask<void> ReportWritingTask;
204
205#if (CSV_PROFILER_STATS && !UE_BUILD_SHIPPING)
207 {
208 void Remove(const ULevelStreaming* StreamingLevel)
209 {
210 LevelsLoading.Remove(StreamingLevel);
211 LevelsMakingVisible.Remove(StreamingLevel);
212 LevelsMakingInvisible.Remove(StreamingLevel);
213 }
217 };
218
220#endif
221};
222
223
@ INDEX_NONE
Definition CoreMiscDefines.h:150
@ ForceInit
Definition CoreMiscDefines.h:155
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
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 CSV_DECLARE_CATEGORY_MODULE_EXTERN(Module_API, CategoryName)
Definition CsvProfiler.h:80
ENGINE_API const TCHAR * EnumToString(ELevelStreamingState InState)
Definition LevelStreaming.cpp:1246
ELevelStreamingState
Definition LevelStreaming.h:111
ELevelStreamingTargetState
Definition LevelStreaming.h:124
UE::Math::TBox< double > FBox
Definition MathFwd.h:55
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UCLASS(...)
Definition ObjectMacros.h:776
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition GameStateBase.h:33
Definition IDelegateInstance.h:14
Definition NameTypes.h:617
Definition UObjectGlobals.h:1292
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition StringBuilder.h:79
Definition UniquePtr.h:107
Definition Task.h:191
Definition LevelStreamingProfilingSubsystem.h:32
virtual void UpdateTrackingData(int32 TrackingIndex, FLevelStats &BaseStats, const ULevelStreaming *StreamingLevel, ELevelState PreviousState, ELevelState NewState)
Definition LevelStreamingProfilingSubsystem.h:78
bool IsTracking() const
Definition LevelStreamingProfilingSubsystem.h:49
ELevelState
Definition LevelStreamingProfilingSubsystem.h:105
virtual void AugmentReportRow(FUtf8StringBuilderBase &Builder, int32 TrackingIndex)
Definition LevelStreamingProfilingSubsystem.h:98
virtual void AugmentReportHeader(FUtf8StringBuilderBase &Builder)
Definition LevelStreamingProfilingSubsystem.h:90
virtual void PostReport()
Definition LevelStreamingProfilingSubsystem.h:52
Definition LevelStreaming.h:139
Definition Level.h:423
Definition Object.h:95
Definition WorldSubsystem.h:16
Definition World.h:918
Type
Definition EngineTypes.h:1264
Definition AsyncRegisterLevelContext.cpp:16
Definition Optional.h:131
Definition LevelStreamingProfilingSubsystem.h:119
TOptional< double > TimeAddedToWorld
Definition LevelStreamingProfilingSubsystem.h:129
FActiveLevel(int32 InStatsIndex)
Definition LevelStreamingProfilingSubsystem.h:120
Definition LevelStreamingProfilingSubsystem.h:134
TOptional< double > TimeAddingToWorld
Definition LevelStreamingProfilingSubsystem.h:147
TOptional< double > FinalStreamInDistance_Content
Definition LevelStreamingProfilingSubsystem.h:154
TOptional< double > TimeLoading
Definition LevelStreamingProfilingSubsystem.h:145
TOptional< double > TimeQueuedForRemoveFromWorld
Definition LevelStreamingProfilingSubsystem.h:149
TOptional< double > TimeRemovingFromWorld
Definition LevelStreamingProfilingSubsystem.h:150
TOptional< double > TimeInWorld
Definition LevelStreamingProfilingSubsystem.h:148
TOptional< double > FinalStreamInDistance_Cell
Definition LevelStreamingProfilingSubsystem.h:153
TOptional< FVector > FinalStreamInLocation
Definition LevelStreamingProfilingSubsystem.h:157
TOptional< double > TimeQueueudForAddToWorld
Definition LevelStreamingProfilingSubsystem.h:146
TOptional< double > TimeQueuedForLoading
Definition LevelStreamingProfilingSubsystem.h:144