1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the libgltf project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 */ 9 10 #ifndef TIMER_H 11 #define TIMER_H 12 #include "time.h" 13 #include "Common.h" 14 15 #define ENABLE_TIMER 0 16 17 #if ENABLE_TIMER 18 #define WRITETIME2FILE 1 19 #define TIMER_FILE "./Time.txt" 20 #define TRACE_TIME TraceTime tt 21 #else 22 #define WRITETIME2FILE 0 23 #define TRACE_TIME 24 #endif 25 26 #if WRITETIME2FILE 27 #include <iostream> 28 #include <iomanip> 29 #include <fstream> 30 #endif 31 32 #ifdef _WIN32 33 #include <windows.h> 34 #endif 35 36 #ifdef _linux_ 37 #include <sys/time.h> 38 #endif 39 40 namespace libgltf 41 { 42 43 #ifdef _WIN32 44 static double dqFreq; 45 static LARGE_INTEGER temp; 46 #endif 47 48 #if WRITETIME2FILE 49 static ofstream timerFile; 50 #endif 51 52 static unsigned int queryID[2]; 53 static double gpuTotalTime = 0; 54 55 class Timer 56 { 57 public : Timer()58 Timer() 59 { 60 #if WRITETIME2FILE 61 timerFile = ofstream(TIMER_FILE); 62 #endif 63 } 64 ~Timer()65 ~Timer() 66 { 67 #if WRITETIME2FILE 68 timerFile << "Total Time Taken by GPU " << std::fixed << gpuTotalTime << " ms"; 69 #endif 70 } 71 }; 72 73 class TraceTime 74 { 75 public: TraceTime()76 TraceTime() 77 : startTime(0) 78 , stopTime(0) 79 { 80 glGenQueries(2, queryID); 81 glQueryCounter(queryID[0], GL_TIMESTAMP); 82 glGetQueryObjectui64v(queryID[0], GL_QUERY_RESULT, &startTime); 83 } 84 ~TraceTime()85 ~TraceTime() 86 { 87 glQueryCounter(queryID[1], GL_TIMESTAMP); 88 GLint stopTimerAvailable = 0; 89 while (!stopTimerAvailable) 90 { 91 glGetQueryObjectiv(queryID[1], GL_QUERY_RESULT_AVAILABLE, 92 &stopTimerAvailable); 93 } 94 glGetQueryObjectui64v(queryID[1], GL_QUERY_RESULT, &stopTime); 95 gpuTotalTime += (double)((stopTime - startTime) / 1000000); 96 } 97 private: 98 GLuint64 startTime, stopTime; 99 }; 100 101 } // namespace libgltf 102 103 #endif 104 105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 106