]> git.sesse.net Git - nageru/blob - gpu_timers.h
Move GPUTimers into its own file.
[nageru] / gpu_timers.h
1 #ifndef _GPU_TIMERS_H
2 #define _GPU_TIMERS_H 1
3
4 #include <string>
5 #include <utility>
6 #include <vector>
7
8 #include <epoxy/gl.h>
9
10 extern bool enable_timing;
11
12 class GPUTimers {
13 public:
14         void print();
15         std::pair<GLuint, GLuint> begin_timer(const std::string &name, int level);
16
17 private:
18         struct Timer {
19                 std::string name;
20                 int level;
21                 std::pair<GLuint, GLuint> query;
22         };
23         std::vector<Timer> timers;
24 };
25
26 // A simple RAII class for timing until the end of the scope.
27 class ScopedTimer {
28 public:
29         ScopedTimer(const std::string &name, GPUTimers *timers)
30                 : timers(timers), level(0)
31         {
32                 query = timers->begin_timer(name, level);
33         }
34
35         ScopedTimer(const std::string &name, ScopedTimer *parent_timer)
36                 : timers(parent_timer->timers),
37                   level(parent_timer->level + 1)
38         {
39                 query = timers->begin_timer(name, level);
40         }
41
42         ~ScopedTimer()
43         {
44                 end();
45         }
46
47         void end()
48         {
49                 if (enable_timing && !ended) {
50                         glQueryCounter(query.second, GL_TIMESTAMP);
51                         ended = true;
52                 }
53         }
54
55 private:
56         GPUTimers *timers;
57         int level;
58         std::pair<GLuint, GLuint> query;
59         bool ended = false;
60 };
61
62 #endif  // !defined(_GPU_TIMERS_H)