| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include "Common/GL/GLExtensions/GLExtensions.h" |
| 7 |
|
| 8 |
#ifndef GL_TIME_ELAPSED |
| 9 |
#define GL_TIME_ELAPSED 0x88BF |
| 10 |
#endif |
| 11 |
|
| 12 |
namespace OGL |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class GPUTimer final |
| 36 |
{ |
| 37 |
public: |
| 38 |
GPUTimer() |
| 39 |
{ |
| 40 |
glGenQueries(1, &m_query_id); |
| 41 |
Begin(); |
| 42 |
} |
| 43 |
|
| 44 |
~GPUTimer() |
| 45 |
{ |
| 46 |
End(); |
| 47 |
glDeleteQueries(1, &m_query_id); |
| 48 |
} |
| 49 |
|
| 50 |
void Begin() |
| 51 |
{ |
| 52 |
if (m_started) |
| 53 |
glEndQuery(GL_TIME_ELAPSED); |
| 54 |
|
| 55 |
glBeginQuery(GL_TIME_ELAPSED, m_query_id); |
| 56 |
m_started = true; |
| 57 |
} |
| 58 |
|
| 59 |
void End() |
| 60 |
{ |
| 61 |
if (!m_started) |
| 62 |
return; |
| 63 |
|
| 64 |
glEndQuery(GL_TIME_ELAPSED); |
| 65 |
m_started = false; |
| 66 |
} |
| 67 |
|
| 68 |
double GetTimeSeconds() |
| 69 |
{ |
| 70 |
GetResult(); |
| 71 |
return static_cast<double>(m_result) / 1000000000.0; |
| 72 |
} |
| 73 |
|
| 74 |
double GetTimeMilliseconds() |
| 75 |
{ |
| 76 |
GetResult(); |
| 77 |
return static_cast<double>(m_result) / 1000000.0; |
| 78 |
} |
| 79 |
|
| 80 |
u32 GetTimeNanoseconds() |
| 81 |
{ |
| 82 |
GetResult(); |
| 83 |
return m_result; |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
void GetResult() |
| 88 |
{ |
| 89 |
if (m_has_result) |
| 90 |
return; |
| 91 |
|
| 92 |
if (m_started) |
| 93 |
End(); |
| 94 |
|
| 95 |
glGetQueryObjectuiv(m_query_id, GL_QUERY_RESULT, &m_result); |
| 96 |
m_has_result = true; |
| 97 |
} |
| 98 |
|
| 99 |
GLuint m_query_id; |
| 100 |
GLuint m_result = 0; |
| 101 |
bool m_started = false; |
| 102 |
bool m_has_result = false; |
| 103 |
}; |
| 104 |
} |