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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "UnityPrefix.h"
#if ENABLE_PROFILER && GFX_SUPPORTS_OPENGLES20
#include "TimerQueryGLES20.h"
#include "AssertGLES20.h"
#include "UnityGLES20Ext.h"
#include "Runtime/Shaders/GraphicsCaps.h"
bool TimerQueriesGLES::Init()
{
bool hasHWSupport = false;
if( QueryExtension("GL_NV_timer_query") )
{
hasHWSupport = gGlesExtFunc.glGetQueryObjectuivEXT && gGlesExtFunc.glGenQueriesEXT;
#if UNITY_ANDROID
hasHWSupport = hasHWSupport && gGlesExtFunc.glQueryCounterNV && gGlesExtFunc.glGetQueryObjectui64vNV;
#endif
}
gGraphicsCaps.hasTimerQuery = hasHWSupport;
if(hasHWSupport)
GLES_CHK(gGlesExtFunc.glGenQueriesEXT(kQueriesCount, timestamp_gl));
nextIndex = 0;
return true;
}
void TimerQueriesGLES::BeginTimerQueries()
{
GLES_CHK(glFlush());
SetTimestamp();
}
void TimerQueriesGLES::EndTimerQueries()
{
GLES_CHK(glFlush());
}
unsigned TimerQueriesGLES::SetTimestamp()
{
#if UNITY_ANDROID
GLES_CHK(gGlesExtFunc.glQueryCounterNV(timestamp_gl[nextIndex], GL_TIMESTAMP_NV));
#endif
unsigned ret = nextIndex;
++nextIndex;
if(nextIndex == kQueriesCount)
nextIndex = 0;
return ret;
}
UInt64 TimerQueriesGLES::GetElapsedTime(unsigned idx, bool wait)
{
#if UNITY_ANDROID
GLuint available = 0;
GLES_CHK(gGlesExtFunc.glGetQueryObjectuivEXT(timestamp_gl[idx], GL_QUERY_RESULT_AVAILABLE_EXT, &available));
// sometimes timestamp might be not ready (we still dont know why)
// the only workaround would be to add glFlush into SetTimestamp
// but then some timings will be a bit off
if(wait)
{
for(unsigned i = 0 ; i < 100 && !available ; ++i)
{
GLES_CHK(gGlesExtFunc.glGetQueryObjectuivEXT(timestamp_gl[idx], GL_QUERY_RESULT_AVAILABLE_EXT, &available));
}
}
if(available)
{
unsigned prev_idx = idx > 0 ? idx-1 : kQueriesCount-1;
EGLuint64NV time1, time2;
GLES_CHK(gGlesExtFunc.glGetQueryObjectui64vNV(timestamp_gl[prev_idx], GL_QUERY_RESULT_EXT, &time1));
GLES_CHK(gGlesExtFunc.glGetQueryObjectui64vNV(timestamp_gl[idx], GL_QUERY_RESULT_EXT, &time2));
return time2-time1;
}
#endif
return kInvalidProfileTime;
}
TimerQueryGLES::TimerQueryGLES()
: m_Index(0),
m_Time(kInvalidProfileTime)
{
}
void TimerQueryGLES::Measure()
{
m_Index = g_TimerQueriesGLES.SetTimestamp();
m_Time = kInvalidProfileTime;
}
ProfileTimeFormat TimerQueryGLES::GetElapsed(UInt32 flags)
{
if(m_Time == kInvalidProfileTime)
m_Time = g_TimerQueriesGLES.GetElapsedTime(m_Index, (flags & kWaitRenderThread) != 0);
return m_Time;
}
TimerQueriesGLES g_TimerQueriesGLES;
#endif
|