]> git.sesse.net Git - nageru/blob - gpu_timers.h
Split out the flow code from the example driver.
[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 extern bool detailed_timing;
12 extern bool in_warmup;
13
14 class GPUTimers {
15 public:
16         void print();
17         std::pair<GLuint, GLuint> begin_timer(const std::string &name, int level);
18
19 private:
20         struct Timer {
21                 std::string name;
22                 int level;
23                 std::pair<GLuint, GLuint> query;
24         };
25         std::vector<Timer> timers;
26 };
27
28 // A simple RAII class for timing until the end of the scope.
29 class ScopedTimer {
30 public:
31         ScopedTimer(const std::string &name, GPUTimers *timers)
32                 : timers(timers), level(0)
33         {
34                 query = timers->begin_timer(name, level);
35         }
36
37         ScopedTimer(const std::string &name, ScopedTimer *parent_timer)
38                 : timers(parent_timer->timers),
39                   level(parent_timer->level + 1)
40         {
41                 query = timers->begin_timer(name, level);
42         }
43
44         ~ScopedTimer()
45         {
46                 end();
47         }
48
49         void end()
50         {
51                 if (enable_timing && !ended) {
52                         glQueryCounter(query.second, GL_TIMESTAMP);
53                         ended = true;
54                 }
55         }
56
57 private:
58         GPUTimers *timers;
59         int level;
60         std::pair<GLuint, GLuint> query;
61         bool ended = false;
62 };
63
64 #endif  // !defined(_GPU_TIMERS_H)