-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfmeter.c
268 lines (219 loc) · 6.38 KB
/
perfmeter.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright (c) 2005 Holger Waechtler
Copyright (c) 2005 Jeremy Fitzhardinge
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Performance meter code taken from jsgf's PSPGL. */
#define GL_GLEXT_PROTOTYPES
#include <sys/time.h>
#include <GL/gl.h>
#include "perfmeter.h"
/* enable GLerror logging to "ms0:/log.txt" */
#if 0
extern void __pspgl_log (const char *fmt, ...);
#define EGLCHK(x) \
do { \
EGLint errcode; \
psp_log("%d: %s\n", __LINE__, # x ); \
x; \
errcode = eglGetError(); \
if (errcode != EGL_SUCCESS) { \
__pspgl_log("%s (%d): EGL error 0x%04x\n", \
__FUNCTION__, __LINE__, (unsigned int) errcode);\
} \
} while (0)
#define GLCHK(x) \
do { \
GLint errcode; \
psp_log(#x "\n"); \
x; \
errcode = glGetError(); \
if (errcode != GL_NO_ERROR) { \
__pspgl_log("%s (%d): GL error 0x%04x\n", \
__FUNCTION__, __LINE__, (unsigned int) errcode);\
} \
} while (0)
#else
#define GLCHK(x) x
#define EGLCHK(x) x
#endif
#if !GL_PSP_statistics
static unsigned long long start, end, prev;
static long long now(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000000ll + t.tv_usec;
}
#endif
static void showstats(float drawtime, float frametime, float queuewait)
{
GLCHK(glPushAttrib(GL_ENABLE_BIT |
GL_COLOR_BUFFER_BIT |
GL_CURRENT_BIT |
GL_TRANSFORM_BIT));
GLCHK(glMatrixMode(GL_MODELVIEW));
GLCHK(glPushMatrix());
GLCHK(glLoadIdentity());
GLCHK(glMatrixMode(GL_PROJECTION));
GLCHK(glPushMatrix());
GLCHK(glLoadIdentity());
GLCHK(glOrtho(0, 1, 0, 10, -1, 1));
#if GL_PSP_view_matrix
GLCHK(glMatrixMode(GL_VIEW_PSP));
GLCHK(glPushMatrix());
GLCHK(glLoadIdentity());
#endif
GLCHK(glDisable(GL_DEPTH_TEST));
GLCHK(glDisable(GL_LIGHTING));
GLCHK(glDisable(GL_CULL_FACE));
GLCHK(glDisable(GL_TEXTURE_2D));
GLCHK(glEnable(GL_BLEND));
GLCHK(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
#if GL_PSP_vertex_blend
GLCHK(glDisable(GL_VERTEX_BLEND_PSP));
#endif
/* draw graph background */
glColor4f(.25,.25,.25,.5);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
glVertex2f(1,0);
glVertex2f(1,1);
glVertex2f(0,1);
glEnd();
/* scale so that full scale = 1/10th sec */
GLCHK(glMatrixMode(GL_MODELVIEW));
GLCHK(glScalef(10, 1, 1));
GLCHK(glShadeModel(GL_FLAT));
GLCHK(glColor4f(.5,.5,.5,.5));
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(0,0);
glVertex2f(0,.75);
glVertex2f(drawtime,0);
glVertex2f(drawtime,.75);
glColor4f(.8,.2,0,.5);
glVertex2f(frametime,0);
glVertex2f(frametime,.75);
GLCHK(glEnd());
if (queuewait) {
glColor4f(0,1,1,.5);
glBegin(GL_TRIANGLES);
glVertex2f(queuewait-.0005, 1);
glVertex2f(queuewait, .75);
glVertex2f(queuewait+.0005, 1);
glEnd();
}
GLCHK(glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT));
GLCHK(glEnableClientState(GL_VERTEX_ARRAY));
GLCHK(glDisableClientState(GL_NORMAL_ARRAY));
GLCHK(glDisableClientState(GL_TEXTURE_COORD_ARRAY));
GLCHK(glDisableClientState(GL_COLOR_ARRAY));
#if GL_PSP_vertex_blend
GLCHK(glDisableClientState(GL_WEIGHT_ARRAY_PSP));
#endif
static GLuint bufferid = 0;
#define NTICKS 100
if (bufferid == 0) {
int i;
struct vertex {
float x,y,z;
} ticks[NTICKS * 2];
GLCHK(glGenBuffersARB(1, &bufferid));
for(i = 0; i < NTICKS; i++) {
ticks[i*2+0].x = i / 1000.f;
ticks[i*2+0].y = 0.f;
ticks[i*2+0].z = 0.f;
ticks[i*2+1].x = i / 1000.f;
ticks[i*2+1].y = i % 10 ? ((i % 5) ? .25 : .5) : .75;
ticks[i*2+1].z = 0.f;
}
GLCHK(glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferid));
GLCHK(glBufferDataARB(GL_ARRAY_BUFFER_ARB,
sizeof(ticks), ticks,
GL_STATIC_DRAW_ARB));
} else
GLCHK(glBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferid));
GLCHK(glVertexPointer(3, GL_FLOAT, 0, NULL+0));
GLCHK(glColor4f(.5,.5,0,.5));
GLCHK(glDrawArrays(GL_LINES, 0, NTICKS*2));
GLCHK(glPopClientAttrib());
glBegin(GL_LINES);
glColor3f(0,1,0);
glVertex2f(1./60, 0);
glVertex2f(1./60, 1);
glColor3f(1,1,0);
glVertex2f(1./30, 0);
glVertex2f(1./30, 1);
glColor3f(1,0,0);
glVertex2f(1./15, 0);
glVertex2f(1./15, 1);
GLCHK(glEnd());
GLCHK(glPopAttrib());
#if GL_PSP_view_matrix
GLCHK(glMatrixMode(GL_VIEW_PSP));
GLCHK(glPopMatrix());
#endif
GLCHK(glMatrixMode(GL_PROJECTION));
GLCHK(glPopMatrix());
GLCHK(glMatrixMode(GL_MODELVIEW));
GLCHK(glPopMatrix());
}
void pm_framestart()
{
#if GL_PSP_statistics
GLCHK(glEnableStatsPSP(GL_STATS_TIMING_PSP));
#else
start = now();
#endif
}
void pm_frameend()
{
float drawtime;
float frametime;
float queuewait;
glFinish();
#if GL_PSP_statistics
{
GLuint t;
glGetStatisticsuivPSP(GL_STATS_APPTIME_PSP, &t);
drawtime = t;
glGetStatisticsuivPSP(GL_STATS_FRAMETIME_PSP, &t);
frametime = t;
glGetStatisticsuivPSP(GL_STATS_QUEUEWAITTIME_PSP, &t);
queuewait = t;
glResetStatsPSP(GL_STATS_QUEUEWAITTIME_PSP);
}
#else
end = now();
drawtime = (end - start);
frametime = (end - prev);
queuewait = 0;
#endif
drawtime /= 1000000.;
frametime /= 1000000.;
queuewait /= 1000000.;
showstats(drawtime, frametime, queuewait);
#if !GL_PSP_statistics
prev = now();
#endif
}