1 #include "timer.h" 2 3 // GLTimer 4 GLTimer()5GLTimer::GLTimer() { 6 query_id = 0; 7 query_value = 0; 8 query_start = 0; 9 query_stop = 0; 10 cpu_time = 0; 11 } 12 GLTimer(const std::string & name)13GLTimer::GLTimer(const std::string& name) : name(name) { 14 query_id = 0; 15 query_value = 0; 16 query_start = 0; 17 query_stop = 0; 18 cpu_time = 0; 19 } 20 ~GLTimer()21GLTimer::~GLTimer() { 22 unload(); 23 } 24 unload()25void GLTimer::unload() { 26 query_value = 0; 27 if(query_id) { 28 glDeleteQueries(1, &query_id); 29 query_id = 0; 30 } 31 query_start = query_stop = 0; 32 } 33 start()34void GLTimer::start() { 35 if(query_start > 0) return; 36 37 query_start = SDL_GetTicks(); 38 39 if(!query_id) glGenQueries( 1, &query_id ); 40 41 glBeginQuery(GL_TIME_ELAPSED, query_id); 42 43 query_stop = 0; 44 } 45 stop()46void GLTimer::stop() { 47 if(!query_start || query_stop > 0) return; 48 glEndQuery(GL_TIME_ELAPSED); 49 query_stop = SDL_GetTicks(); 50 } 51 getName() const52const std::string& GLTimer::getName() const { 53 return name; 54 } 55 getValue() const56GLuint64 GLTimer::getValue() const { 57 return query_value; 58 } 59 getGLMillis() const60Uint32 GLTimer::getGLMillis() const { 61 return query_value / 1000000; 62 } 63 getCPUMillis() const64Uint32 GLTimer::getCPUMillis() const { 65 return cpu_time; 66 } 67 check()68bool GLTimer::check() { 69 if(!query_start) return false; 70 71 GLuint64 elapsed; 72 GLint available = 0; 73 74 glGetQueryObjectiv(query_id, GL_QUERY_RESULT_AVAILABLE, &available); 75 76 if(!available) return false; 77 78 glGetQueryObjectui64v(query_id, GL_QUERY_RESULT, &elapsed); 79 80 query_value = elapsed; 81 cpu_time = query_stop-query_start; 82 query_start = query_stop = 0; 83 84 return true; 85 } 86