-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.jai
141 lines (116 loc) · 4.49 KB
/
profiler.jai
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
Profiler_Task :: struct {
start: float64;
end: float64;
name: string;
color: u32;
}
Profiler_Frame :: struct {
start: float64;
end: float64;
tasks: [..] Profiler_Task;
}
Profiler :: struct {
frames: [MAX_PROFILER_FRAMES] Profiler_Frame;
index: int;
pool: Pool;
}
#if Enable_Profiler {
profiler_init :: (using profiler: *Profiler) {
frames[index].start = FLOAT64_MAX;
set_allocators(*profiler.pool);
allocator := .{pool_allocator_proc, *profiler.pool};
for *frame: frames frame.tasks.allocator = allocator;
}
profiler_deinit :: (using profiler: *Profiler) {
release(profiler.pool);
}
profiler_scoped_task :: (using profiler: *Profiler, name: string, color := Colors8.White) #expand {
id := profiler_begin_task(name, color);
defer profiler_end_task(id);
}
profiler_begin_frame :: (using profiler: *Profiler) {
frames[index].start = get_time();
frames[index].tasks.count = 0;
}
profiler_end_frame :: (using profiler: *Profiler) {
frames[index].end = get_time();
index = (index + 1) % MAX_PROFILER_FRAMES;
}
profiler_frame_elapsed :: (using profiler: *Profiler) -> float64 {
return get_time() - frames[index].start;
}
profiler_begin_task :: (using profiler: *Profiler, name: string, color := Colors8.White) -> int {
task: Profiler_Task = ---;
task.start = profiler_frame_elapsed(profiler);
task.name = name;
task.color = color.value;
array_add(*frames[index].tasks, task);
return frames[index].tasks.count-1;
}
profiler_end_task :: (using profiler: *Profiler, id: int) -> Profiler_Task {
frames[index].tasks[id].end = profiler_frame_elapsed(profiler);
return frames[index].tasks[id];
}
cpu_scoped_task :: (name: string, color := Colors8.White) #expand {
id := profiler_begin_task(*_cpu_profiler, name, color);
`defer profiler_end_task(*_cpu_profiler, id);
}
cpu_begin_frame :: () #expand {
profiler_begin_frame(*_cpu_profiler);
}
cpu_end_frame :: () #expand {
profiler_end_frame(*_cpu_profiler);
}
cpu_elapsed :: () -> float64 #expand {
profiler_frame_elapsed(*_cpu_profiler);
}
cpu_begin_task :: (name: string, color := Colors8.White) -> int #expand {
return profiler_begin_task(*_cpu_profiler, name, color);
}
cpu_end_task :: (id: int) -> Profiler_Task #expand {
profiler_end_task(*_cpu_profiler, id);
}
gpu_scoped_task :: (name: string, color := Colors8.White) #expand {
id := profiler_begin_task(*_gpu_profiler, name, color);
`defer profiler_end_task(*_gpu_profiler, id);
}
gpu_begin_frame :: () #expand {
profiler_begin_frame(*_gpu_profiler);
}
gpu_end_frame :: () #expand {
profiler_end_frame(*_gpu_profiler);
}
gpu_elapsed :: () -> float64 #expand {
profiler_frame_elapsed(*_gpu_profiler);
}
gpu_begin_task :: (name: string, color := Colors8.White) -> int #expand {
return profiler_begin_task(*_gpu_profiler, name, color);
}
gpu_end_task :: (id: int) -> Profiler_Task #expand {
profiler_end_task(*_gpu_profiler, id);
}
} else {
profiler_init :: (using profiler: *Profiler) #expand {}
profiler_deinit :: (using profiler: *Profiler) #expand {}
profiler_begin_frame :: (profiler: *Profiler) #expand {}
profiler_end_frame :: (profiler: *Profiler) #expand {}
profiler_elapsed :: (profiler: *Profiler) -> float64 #expand {}
profiler_begin_task :: (profiler: *Profiler, name: string, color := Colors8.White) -> int #expand {}
profiler_end_task :: (profiler: *Profiler, id: int) -> Profiler_Task #expand {}
cpu_scoped_task :: (name: string, color := Colors8.White) #expand {}
cpu_begin_frame :: () #expand {}
cpu_end_frame :: () #expand {}
cpu_elapsed :: () -> float64 #expand {}
cpu_begin_task :: (name: string, color := Colors8.White) -> int #expand {}
cpu_end_task :: (id: int) -> Profiler_Task #expand {}
gpu_scoped_task :: (name: string, color := Colors8.White) #expand {}
gpu_begin_frame :: () #expand {}
gpu_end_frame :: () #expand {}
gpu_elapsed :: () -> float64 #expand {}
gpu_begin_task :: (name: string, color := Colors8.White) -> int #expand {}
gpu_end_task :: (id: int) -> Profiler_Task #expand {}
}
#scope_module
MAX_PROFILER_FRAMES :: 300;
_cpu_profiler: Profiler;
_gpu_profiler: Profiler;