Polyphase Game Engine
Loading...
Searching...
No Matches
Profiler.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <vector>
5
6#include <string.h>
7
8#define PROFILING_ENABLED 1
9
10#define STAT_NAME_LENGTH 31
11#define STAT_NAME_BUFFER_LENGTH (STAT_NAME_LENGTH + 1)
12
13struct CpuStat
14{
16 uint64_t mStartTime = 0;
17 uint64_t mEndTime = 0;
18 float mTime = 0.0f;
19 float mSmoothedTime = 0.0f;
20};
21
22struct GpuStat
23{
25 float mTime = 0.0f;
26 float mSmoothedTime = 0.0f;
27};
28
30{
31public:
32
33 void BeginFrame();
34 void EndFrame();
35
36 void BeginCpuStat(const char* name, bool persistent);
37 void EndCpuStat(const char* name, bool persistent);
38
39 void BeginGpuStat(const char* name);
40 void EndGpuStat(const char* name);
41 void SetGpuStatTime(const char* name, float time);
42
43 CpuStat* FindCpuStat(const char* name, bool persistent);
44 const std::vector<CpuStat>& GetCpuFrameStats() const;
45
46 const std::vector<CpuStat>& GetCpuPersistentStats() const;
47 const std::vector<GpuStat>& GetGpuStats() const;
48
49 void LogPersistentStats();
51
52protected:
53
54 std::vector<CpuStat> mCpuFrameStats;
55 std::vector<CpuStat> mCpuPersistentStats;
56 std::vector<GpuStat> mGpuStats;
57};
58
59void CreateProfiler();
60void DestroyProfiler();
62
64{
65 ScopedCpuStat(const char* name, bool persistent)
66 {
67 strncpy(mName, name, STAT_NAME_LENGTH);
68 mPersistent = persistent;
70 }
71
76
78 bool mPersistent = false;
79};
80
82{
83 ScopedGpuStat(const char* name)
84 {
85 strncpy(mName, name, STAT_NAME_LENGTH);
87 }
88
93
95};
96
97#if PROFILING_ENABLED
98#define SCOPED_FRAME_STAT(name) ScopedCpuStat scopedStat##__LINE__(name, false);
99#define BEGIN_FRAME_STAT(name) GetProfiler()->BeginCpuStat(name, false);
100#define END_FRAME_STAT(name) GetProfiler()->EndCpuStat(name, false);
101
102#define SCOPED_STAT(name) ScopedCpuStat scopedStat##__LINE__(name, true);
103#define BEGIN_STAT(name) GetProfiler()->BeginCpuStat(name, true);
104#define END_STAT(name) GetProfiler()->EndCpuStat(name, true);
105
106#define SCOPED_GPU_STAT(name) ScopedGpuStat scopedStat##__LINE__(name);
107#define BEGIN_GPU_STAT(name) GetProfiler()->BeginGpuStat(name);
108#define END_GPU_STAT(name) GetProfiler()->EndGpuStat(name);
109#else
110#define SCOPED_FRAME_STAT(name)
111#define BEGIN_FRAME_STAT(name)
112#define END_FRAME_STAT(name)
113
114#define SCOPED_STAT(name)
115#define BEGIN_STAT(name)
116#define END_STAT(name)
117
118#define SCOPED_GPU_STAT(name)
119#define BEGIN_GPU_STAT(name)
120#define END_GPU_STAT(name)
121#endif
Profiler * GetProfiler()
Definition Profiler.cpp:206
#define STAT_NAME_LENGTH
Definition Profiler.h:10
#define STAT_NAME_BUFFER_LENGTH
Definition Profiler.h:11
void CreateProfiler()
Definition Profiler.cpp:185
void DestroyProfiler()
Definition Profiler.cpp:195
Definition Profiler.h:30
CpuStat * FindCpuStat(const char *name, bool persistent)
Definition Profiler.cpp:123
void DumpPersistentStats()
Definition Profiler.cpp:169
const std::vector< CpuStat > & GetCpuPersistentStats() const
Definition Profiler.cpp:147
void EndCpuStat(const char *name, bool persistent)
Definition Profiler.cpp:70
void BeginCpuStat(const char *name, bool persistent)
Definition Profiler.cpp:44
std::vector< CpuStat > mCpuPersistentStats
Definition Profiler.h:55
const std::vector< GpuStat > & GetGpuStats() const
Definition Profiler.cpp:152
void SetGpuStatTime(const char *name, float time)
Definition Profiler.cpp:98
void BeginFrame()
Definition Profiler.cpp:13
void LogPersistentStats()
Definition Profiler.cpp:157
std::vector< GpuStat > mGpuStats
Definition Profiler.h:56
void EndFrame()
Definition Profiler.cpp:26
std::vector< CpuStat > mCpuFrameStats
Definition Profiler.h:54
const std::vector< CpuStat > & GetCpuFrameStats() const
Definition Profiler.cpp:142
void BeginGpuStat(const char *name)
Definition Profiler.cpp:84
void EndGpuStat(const char *name)
Definition Profiler.cpp:91
Definition Profiler.h:14
float mTime
Definition Profiler.h:18
uint64_t mStartTime
Definition Profiler.h:16
float mSmoothedTime
Definition Profiler.h:19
uint64_t mEndTime
Definition Profiler.h:17
char mName[STAT_NAME_BUFFER_LENGTH]
Definition Profiler.h:15
Definition Profiler.h:23
char mName[STAT_NAME_BUFFER_LENGTH]
Definition Profiler.h:24
float mTime
Definition Profiler.h:25
float mSmoothedTime
Definition Profiler.h:26
Definition Profiler.h:64
ScopedCpuStat(const char *name, bool persistent)
Definition Profiler.h:65
~ScopedCpuStat()
Definition Profiler.h:72
bool mPersistent
Definition Profiler.h:78
char mName[STAT_NAME_BUFFER_LENGTH]
Definition Profiler.h:77
Definition Profiler.h:82
ScopedGpuStat(const char *name)
Definition Profiler.h:83
~ScopedGpuStat()
Definition Profiler.h:89
char mName[STAT_NAME_BUFFER_LENGTH]
Definition Profiler.h:94