File size: 4,450 Bytes
7fd553e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | // Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "ChartCreation.h"
#include "LyraPerformanceStatTypes.h"
#include "Algo/MaxElement.h"
#include "Algo/MinElement.h"
#include "Stats/StatsData.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "LyraPerformanceStatSubsystem.generated.h"
enum class ELyraDisplayablePerformanceStat : uint8;
class FSubsystemCollectionBase;
class ULyraPerformanceStatSubsystem;
class UObject;
struct FFrame;
/**
* Stores a buffer of the given sample size and provides an interface to get data
* like the min, max, and average of that group.
*/
class FSampledStatCache
{
public:
FSampledStatCache(const int32 InSampleSize = 125)
: SampleSize(InSampleSize)
{
check(InSampleSize > 0);
Samples.Empty();
Samples.AddZeroed(SampleSize);
}
void RecordSample(const double Sample)
{
// A simple little ring buffer for storing the samples over time
Samples[CurrentSampleIndex] = Sample;
CurrentSampleIndex++;
if (CurrentSampleIndex >= Samples.Num())
{
CurrentSampleIndex = 0u;
}
}
double GetCurrentCachedStat() const
{
return Samples[CurrentSampleIndex];
}
double GetLastCachedStat() const
{
int32 LastIndex = CurrentSampleIndex - 1;
if (LastIndex < 0)
{
LastIndex = Samples.Num() - 1;
}
return Samples[LastIndex];
}
/**
* Iterates all the samples, starting at the most recently sampled index
*/
void ForEachCurrentSample(const TFunctionRef<void(const double Stat)> Func) const
{
int32 Index = CurrentSampleIndex;
for (int32 i = 0; i < SampleSize; i++)
{
Func(Samples[Index]);
Index++;
if (Index == SampleSize)
{
Index = 0;
}
}
}
inline int32 GetSampleSize() const
{
return SampleSize;
}
inline double GetAverage() const
{
double Sum = 0.0;
ForEachCurrentSample([&Sum](const double Stat)
{
Sum += Stat;
});
// Return the average stat value
return Sum / static_cast<double>(SampleSize);
}
inline double GetMin() const
{
return *Algo::MinElement(Samples);
}
inline double GetMax() const
{
return *Algo::MaxElement(Samples);
}
private:
const int32 SampleSize = 125;
int32 CurrentSampleIndex = 0;
TArray<double> Samples;
};
//////////////////////////////////////////////////////////////////////
// Observer which caches the stats for the previous frame
struct FLyraPerformanceStatCache : public IPerformanceDataConsumer
{
public:
FLyraPerformanceStatCache(ULyraPerformanceStatSubsystem* InSubsystem)
: MySubsystem(InSubsystem)
{
}
//~IPerformanceDataConsumer interface
virtual void StartCharting() override;
virtual void ProcessFrame(const FFrameData& FrameData) override;
virtual void StopCharting() override;
//~End of IPerformanceDataConsumer interface
/**
* Returns the latest cached value for the given stat type
*/
double GetCachedStat(ELyraDisplayablePerformanceStat Stat) const;
/**
* Returns a pointer to the cache for the given stat type. This can be used it
* get the min/max/average of this stat, the latest stat, and iterate all of the samples.
* This is useful for generating some UI, like an FPS chart over time.
*/
const FSampledStatCache* GetCachedStatData(const ELyraDisplayablePerformanceStat Stat) const;
protected:
void RecordStat(const ELyraDisplayablePerformanceStat Stat, const double Value);
ULyraPerformanceStatSubsystem* MySubsystem;
/**
* Caches the sampled data for each of the performance stats currently available
*/
TMap<ELyraDisplayablePerformanceStat, FSampledStatCache> PerfStateCache;
};
//////////////////////////////////////////////////////////////////////
// Subsystem to allow access to performance stats for display purposes
UCLASS(BlueprintType)
class ULyraPerformanceStatSubsystem : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
double GetCachedStat(ELyraDisplayablePerformanceStat Stat) const;
const FSampledStatCache* GetCachedStatData(const ELyraDisplayablePerformanceStat Stat) const;
//~USubsystem interface
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
virtual void Deinitialize() override;
//~End of USubsystem interface
protected:
TSharedPtr<FLyraPerformanceStatCache> Tracker;
};
|