-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtimer.cpp
More file actions
85 lines (63 loc) · 1.57 KB
/
timer.cpp
File metadata and controls
85 lines (63 loc) · 1.57 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
#include "timer.h"
// GLTimer
GLTimer::GLTimer() {
query_id = 0;
query_value = 0;
query_start = 0;
query_stop = 0;
cpu_time = 0;
}
GLTimer::GLTimer(const std::string& name) : name(name) {
query_id = 0;
query_value = 0;
query_start = 0;
query_stop = 0;
cpu_time = 0;
}
GLTimer::~GLTimer() {
unload();
}
void GLTimer::unload() {
query_value = 0;
if(query_id) {
glDeleteQueries(1, &query_id);
query_id = 0;
}
query_start = query_stop = 0;
}
void GLTimer::start() {
if(query_start > 0) return;
query_start = SDL_GetTicks();
if(!query_id) glGenQueries( 1, &query_id );
glBeginQuery(GL_TIME_ELAPSED, query_id);
query_stop = 0;
}
void GLTimer::stop() {
if(!query_start || query_stop > 0) return;
glEndQuery(GL_TIME_ELAPSED);
query_stop = SDL_GetTicks();
}
const std::string& GLTimer::getName() const {
return name;
}
GLuint64 GLTimer::getValue() const {
return query_value;
}
Uint32 GLTimer::getGLMillis() const {
return query_value / 1000000;
}
Uint32 GLTimer::getCPUMillis() const {
return cpu_time;
}
bool GLTimer::check() {
if(!query_start) return false;
GLuint64 elapsed;
GLint available = 0;
glGetQueryObjectiv(query_id, GL_QUERY_RESULT_AVAILABLE, &available);
if(!available) return false;
glGetQueryObjectui64v(query_id, GL_QUERY_RESULT, &elapsed);
query_value = elapsed;
cpu_time = query_stop-query_start;
query_start = query_stop = 0;
return true;
}