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