-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.h
More file actions
347 lines (309 loc) · 13.5 KB
/
profile.h
File metadata and controls
347 lines (309 loc) · 13.5 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#if defined(USE_DEBUG_PROFILING)
// Windows perf counter header
#if defined(__WIN32__) || defined(_WIN32) || defined(__WIN64__) || defined(_WIN64) || defined(WIN32)
#include <intrin.h>
#define _rdtsc __rdtsc
// Linux perf counter header
#elif defined(__linux__) || defined(LINUX)
#include <x86intrin.h>
#else
#error "compilation error: OS must be either linux or windows"
#endif
#include <stdint.h>
#define ARRAY_COUNT(Array) sizeof((Array)) / sizeof(Array[0])
#define PROFILE_MAX_FRAME_COUNT 500
#define PROFILE_MAX_TIMER_EVENTS_PER_FRAME 500
enum
{
TIMER_NAME_FirstInit,
TIMER_NAME_FilesystemUpdate,
TIMER_NAME_Update,
TIMER_NAME_Editor,
TIMER_NAME_GUI,
TIMER_NAME_SelectionDrawing,
TIMER_NAME_EntityCreation,
TIMER_NAME_Physics,
TIMER_NAME_Render,
TIMER_NAME_PostProcessing,
TIMER_NAME_DebugDrawingSubmission,
TIMER_NAME_LoadTextTexture,
TIMER_NAME_ImportScene,
TIMER_NAME_LoadTexture,
TIMER_NAME_LoadModel,
TIMER_NAME_LoadShader,
TIMER_NAME_LoadMaterial,
TIMER_NAME_PartitionMemory,
TIMER_NAME_LoadInitialResources,
TIMER_NAME_GBufferPass,
TIMER_NAME_SSAOPass,
TIMER_NAME_ShadowmapPass,
TIMER_NAME_VolumetricScatteringPass,
TIMER_NAME_Cubemap,
TIMER_NAME_RenderScene,
TIMER_NAME_RenderSelection,
TIMER_NAME_RenderPreview,
TIMER_NAME_SimulateDynamics,
TIMER_NAME_CopyDataToPhysicsWorld,
TIMER_NAME_CopyDataFromPhysicsWorld,
TIMER_NAME_AnimationSystem,
TIMER_NAME_SAT,
TIMER_NAME_ODE,
TIMER_NAME_LoadSizedFont,
TIMER_NAME_LoadFont,
TIMER_NAME_SearchForDesiredTexture,
TIMER_NAME_FindCacheLineToOccupy,
TIMER_NAME_GetBestMatchingFont,
TIMER_NAME_GetTextSize,
TIMER_NAME_GetTextTextureID,
TIMER_NAME_ResetCache,
TIMER_NAME_SetMaterial,
TIMER_NAME_UpdateAssetPathLists,
TIMER_NAME_DeleteUnused,
TIMER_NAME_ReloadModified,
TIMER_NAME_ReadEntireFile,
TIMER_NAME_WriteEntireFile,
TIMER_NAME_UpdateHardDrivePathList,
TIMER_NAME_HotReloadAssets,
TIMER_NAME_MotionMatch,
TIMER_NAME_BuildMotionSet,
TIMER_NAME_UpdatePlayer,
TIMER_NAME_Offbeat,
TIMER_NAME_Particles,
TIMER_NAME_ImGuiDemo,
TIMER_NAME_RenderImGui,
TIMER_NAME_Count,
};
const char TIMER_NAME_TABLE[][TIMER_NAME_Count] = {
"FirstInit",
"FilesystemUpdate",
"Update",
"Editor",
"GUI",
"SelectionDrawing",
"EntityCreation",
"Physics",
"Render",
"PostProcessing",
"DebugDrawingSubmission",
"LoadTextTexture",
"ImportScene",
"LoadTexture",
"LoadModel",
"LoadShader",
"LoadMaterial",
"PartitionMemory",
"LoadInitialResources",
"GBufferPass",
"SSAOPass",
"ShadowmapPass",
"VolumetricScatteringPass",
"Cubemap",
"RenderScene",
"RenderSelection",
"RenderPreview",
"SimulateDynamics",
"CopyDataToPhysicsWorld",
"CopyDataFromPhysicsWorld",
"AnimationSystem",
"SAT",
"ODE",
"LoadSizedFont",
"LoadFont",
"SearchForDesiredTexture",
"FindCacheLineToOccupy",
"GetBestMatchingFont",
"GetTextSize",
"GetTextTextureID",
"ResetCache",
"SetMaterial",
"UpdateAssetPathLists",
"DeleteUnused",
"ReloadModified",
"ReadEntireFile",
"WriteEntireFile",
"UpdateHardDrivePathList",
"HotReloadAssets",
"MotionMatch",
"BuildMotionSet",
"UpdatePlayer",
"Offbeat",
"Particles",
"ImGuiDemo",
"RenderImGui",
};
const float TIMER_UI_COLOR_TABLE[TIMER_NAME_Count][3] =
{ { 1, 0, 0 }, { 1, 0.4f, 0.4f }, { 0.1f, 0.8f, 0.2f }, { 0.5f, 1, 0.5f },
{ 0.2f, 0.7f, 0.5f }, { 0, 0, 1 }, { 1, 1, 0 }, { 0.2f, 0.2f, 0.2f },
{ 0, 1, 1 }, { 1, 1, 0 }, { 0.7f, 0.7f, 0.7f }, { 1, 0, 1 },
{ 1, 0, 0 }, { 0, 0.5, 1 }, { 0.5f, 0.5f, 0 }, { 1, 0.6f, 0.6f },
{ 1, 0.6f, 0 }, { 0.5f, 1, 0.5f }, { 0.2f, 0.7f, 0.5f }, { 0, 0, 1 },
{ 1, 1, 0 }, { 0.2f, 0.2f, 0.2f }, { 0, 1, 1 }, { 1, 1, 0 },
{ 0.7f, 0.7f, 0.7f }, { 1, 0, 1 }, { 1, 0.4f, 0.4f }, { 0.1f, 0.8f, 0.2f },
{ 0.5f, 1, 0.5f }, { 0.2f, 0.7f, 0.5f }, { 0, 0, 1 }, { 0.6f, 0.2f, 0.2f },
{ 0, 1, 1 }, { 1, 0, 0 }, { 1, 0.5, 1 }, { 0.5f, 0.5f, 0 },
{ 1, 0.6f, 0.6f }, { 1, 0.6f, 0 }, { 0.5f, 1, 0.5f }, { 0.2f, 0.7f, 0.5f },
{ 0, 0, 1 }, { 0.1f, 0.8f, 0.2f }, { 0, 0, 1 }, { 1, 1, 0 },
{ 0.5f, 0.2f, 0.5f }, { 0.6f, 0.5f, 0.3f }, { 1, 0.2f, 0.3f }, { 0.6f, 0.5f, 0.3f },
{ 1, 0.2f, 0.3f }, { 1, 0.2f, 0.2f }, { 0.2f, 0.4f, 0.6f }, { 0, 0.5f, 1 },
{ 0, 0.5f, 1 }, {0.5f, 0.1f, 0.3f}, {0.7f, 0.3f, 0.2f} };
struct frame_endpoints
{
uint64_t FrameStart;
uint64_t FrameEnd;
};
struct timer_frame_summary
{
uint64_t CycleCount;
uint64_t Calls;
};
struct timer_event
{
uint64_t StartCycleCount;
uint64_t EndCycleCount;
int EventDepth;
int NameTableIndex;
};
extern frame_endpoints GLOBAL_FRAME_ENDPOINT_TABLE[PROFILE_MAX_FRAME_COUNT + 1];
extern timer_frame_summary GLOBAL_TIMER_FRAME_SUMMARY_TABLE[PROFILE_MAX_FRAME_COUNT + 1]
[ARRAY_COUNT(TIMER_NAME_TABLE)];
extern timer_event GLOBAL_FRAME_TIMER_EVENT_TABLE[PROFILE_MAX_FRAME_COUNT + 1]
[PROFILE_MAX_TIMER_EVENTS_PER_FRAME];
extern int GLOBAL_TIMER_FRAME_EVENT_COUNT_TABLE[PROFILE_MAX_FRAME_COUNT + 1];
extern int g_CurrentProfilerFrameIndex;
extern int g_SavedCurrentFrameIndex;
extern int g_CurrentTimerEventDepth;
extern int g_CurrentTimerEventCount;
struct timer_event_autoclose_wrapper
{
int32_t NameTableIndex;
int32_t IndexInFrame;
timer_event_autoclose_wrapper(int32_t NameTableIndex);
~timer_event_autoclose_wrapper();
};
#define FOR_ALL_NAMES(DO_FUNC) \
DO_FUNC(GeomPrePass) \
DO_FUNC(Shadowmapping) \
DO_FUNC(VolumetricLighting) DO_FUNC(SSAO) DO_FUNC(RenderScene) DO_FUNC(FXAA) \
DO_FUNC(PostProcessing) DO_FUNC(Bloom) DO_FUNC(MotionBlur) DO_FUNC(DepthOfField) \
DO_FUNC(FlipScreenbuffer)
#define GENERATE_ENUM_NO_COMMA(Name) GPU_TIMER_##Name
#define GENERATE_ENUM(Name) GENERATE_ENUM_NO_COMMA(Name),
#define GENERATE_STRING(Name) #Name,
enum gpu_timer_type
{
FOR_ALL_NAMES(GENERATE_ENUM) GENERATE_ENUM(EnumCount)
};
static const char* GPU_TIMER_NAME_TABLE[GENERATE_ENUM_NO_COMMA(EnumCount)] = { FOR_ALL_NAMES(
GENERATE_STRING) };
#undef FOR_ALL_NAMES
#undef GENERATE_ENUM
#undef GENERATE_STRING
struct gpu_timer_event
{
uint32_t ElapsedTime;
bool WasRunThisFrame;
};
extern gpu_timer_event GPU_TIMER_EVENT_TABLE[PROFILE_MAX_FRAME_COUNT + 1][GPU_TIMER_EnumCount];
extern uint32_t GPU_QUERY_OBJECT_TABLE[GPU_TIMER_EnumCount];
#define TIME_GPU_EXECUTION 0
#if TIME_GPU_EXECUTION
#if 0
struct gpu_timer_event_autoclose_wrapper
{
inline gpu_timer_event_autoclose_wrapper(int32_t TableIndex)
{
glBeginQuery(GL_TIME_ELAPSED, &GPU_QUERY_OBJECT_TABLE[TableIndex]);
}
inline ~gpu_timer_event_autoclose_wrapper()
{
glEndQuery(GL_TIME_ELAPSED);
}
};
#endif
#define BEGIN_GPU_TIMED_BLOCK(ID) \
GPU_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][GPU_TIMER_##ID].WasRunThisFrame = true; \
glBeginQuery(GL_TIME_ELAPSED, GPU_QUERY_OBJECT_TABLE[GPU_TIMER_##ID]);
#define END_GPU_TIMED_BLOCK(ID) glEndQuery(GL_TIME_ELAPSED);
#define INIT_GPU_TIMERS() glGenQueries(GPU_TIMER_EnumCount, GPU_QUERY_OBJECT_TABLE)
#define READ_GPU_QUERY_TIMERS() \
for(int q_ind = 0; q_ind < GPU_TIMER_EnumCount; q_ind++) \
{ \
int PrevInd = \
(g_CurrentProfilerFrameIndex - 1 + PROFILE_MAX_FRAME_COUNT) % PROFILE_MAX_FRAME_COUNT; \
if(GPU_TIMER_EVENT_TABLE[PrevInd][q_ind].WasRunThisFrame) \
glGetQueryObjectuiv(GPU_QUERY_OBJECT_TABLE[q_ind], GL_QUERY_RESULT, \
&GPU_TIMER_EVENT_TABLE[PrevInd][q_ind].ElapsedTime); \
}
#define GPU_TIMED_BLOCK(ID) gpu_timer_event_autoclose_wrapper ID##__LINE__(GPU_TIMER_NAME_##ID)
#else
#define BEGIN_GPU_TIMED_BLOCK(ID)
#define END_GPU_TIMED_BLOCK(ID)
#define INIT_GPU_TIMERS()
#define READ_GPU_QUERY_TIMERS()
#define GPU_TIMED_BLOCK(ID)
#endif
#define TIMED_BLOCK(ID) timer_event_autoclose_wrapper ID##__LINE__(TIMER_NAME_##ID)
#define BEGIN_TIMED_FRAME() \
for(int i = 0; i < ARRAY_COUNT(GPU_TIMER_NAME_TABLE); i++) \
{ \
GPU_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][i] = {}; \
} \
for(int i = 0; i < ARRAY_COUNT(TIMER_NAME_TABLE); i++) \
{ \
GLOBAL_TIMER_FRAME_SUMMARY_TABLE[g_CurrentProfilerFrameIndex][i] = {}; \
} \
GLOBAL_TIMER_FRAME_EVENT_COUNT_TABLE[g_CurrentProfilerFrameIndex] = 0; \
g_CurrentTimerEventCount = 0; \
GLOBAL_FRAME_ENDPOINT_TABLE[g_CurrentProfilerFrameIndex].FrameStart = _rdtsc();
#define END_TIMED_FRAME() \
assert(g_CurrentTimerEventCount <= PROFILE_MAX_TIMER_EVENTS_PER_FRAME); \
assert(g_CurrentTimerEventDepth == 0); \
GLOBAL_FRAME_ENDPOINT_TABLE[g_CurrentProfilerFrameIndex].FrameEnd = _rdtsc(); \
GLOBAL_TIMER_FRAME_EVENT_COUNT_TABLE[g_CurrentProfilerFrameIndex] = g_CurrentTimerEventCount; \
if(g_CurrentProfilerFrameIndex != PROFILE_MAX_FRAME_COUNT) \
{ \
g_CurrentProfilerFrameIndex = (g_CurrentProfilerFrameIndex + 1) % PROFILE_MAX_FRAME_COUNT; \
}
#define BEGIN_TIMED_BLOCK(ID) \
int FrameEventIndex##ID = g_CurrentTimerEventCount; \
GLOBAL_FRAME_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][g_CurrentTimerEventCount] \
.StartCycleCount = _rdtsc(); \
GLOBAL_FRAME_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][g_CurrentTimerEventCount] \
.EventDepth = g_CurrentTimerEventDepth; \
GLOBAL_FRAME_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][g_CurrentTimerEventCount] \
.NameTableIndex = TIMER_NAME_##ID; \
++g_CurrentTimerEventCount; \
++g_CurrentTimerEventDepth; \
if(g_CurrentProfilerFrameIndex == PROFILE_MAX_FRAME_COUNT) \
{ \
g_CurrentTimerEventCount--; \
}
#define END_TIMED_BLOCK(ID) \
uint64_t EndCycleCount##ID = _rdtsc(); \
GLOBAL_FRAME_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][FrameEventIndex##ID].EndCycleCount = \
EndCycleCount##ID; \
GLOBAL_TIMER_FRAME_SUMMARY_TABLE[g_CurrentProfilerFrameIndex][TIMER_NAME_##ID].CycleCount += \
EndCycleCount##ID - \
GLOBAL_FRAME_TIMER_EVENT_TABLE[g_CurrentProfilerFrameIndex][FrameEventIndex##ID] \
.StartCycleCount; \
GLOBAL_TIMER_FRAME_SUMMARY_TABLE[g_CurrentProfilerFrameIndex][TIMER_NAME_##ID].Calls++; \
--g_CurrentTimerEventDepth;
#define BEGIN_GPU_BLOCK(ID) glBeginQuery(GL_TIME_ELAPSED, PROFILER_)
#define PAUSE_PROFILE() \
g_SavedCurrentFrameIndex = g_CurrentProfilerFrameIndex; \
g_CurrentProfilerFrameIndex = PROFILE_MAX_FRAME_COUNT;
#define RESUME_PROFILE() g_CurrentProfilerFrameIndex = g_SavedCurrentFrameIndex;
#else
#define BEGIN_TIMED_FRAME()
#define END_TIMED_FRAME()
#define BEGIN_TIMED_BLOCK(ID)
#define END_TIMED_BLOCK(ID)
#define TIMED_BLOCK(ID)
#define PAUSE_PROFILE()
#define RESUME_PROFILE()
#define INIT_GPU_TIMERS() ;
#define BEGIN_GPU_TIMED_BLOCK(ID)
#define END_GPU_TIMED_BLOCK(ID)
#define READ_GPU_QUERY_TIMERS() ;
#endif // DEGUB_PROFILING