summaryrefslogtreecommitdiff
path: root/src/glean/tchgperf.cpp
blob: 9ba9e9d64f35eb846c4fd1c39231a6b4232f5488 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 1999  Allen Akin   All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// 
// END_COPYRIGHT

// tchgperf.cpp:  Some basic tests of attribute-change performance.

#include "tchgperf.h"
#include <algorithm>
#include "rand.h"
#include "image.h"
#include "timer.h"
#include "geomutil.h"

#if 0
#ifdef __UNIX__
#include <unistd.h>
#endif

#include <iostream>
#include <fstream>
#include "dsconfig.h"
#include "dsfilt.h"
#include "dsurf.h"
#include "winsys.h"
#include "environ.h"
#include "rc.h"
#include "glutils.h"
#include "timer.h"
#include "tchgperf.h"
#include "misc.h"
#endif

namespace {

GLEAN::Image redImage(64, 64, GL_RGB, GL_UNSIGNED_BYTE, 1.0, 0.0, 0.0, 0.0);
GLuint redTex;
GLEAN::Image greenImage(64, 64, GL_RGB, GL_UNSIGNED_BYTE, 0.0, 1.0, 0.0, 0.0);
GLuint greenTex;

int nPoints;
float* vertices;
float* texCoords;

void
noBindDraw() {
	int rowSize = 2 * nPoints;
	for (int y = 0; y < nPoints - 1; ++y) {
		float* t0 = texCoords + y * rowSize;
		float* v0 = vertices + y * rowSize;
		for (int x = 0; x < nPoints - 1; ++x) {
			float* t1 = t0 + rowSize;
			float* t2 = t1 + 2;
			float* t3 = t0 + 2;
			float* v1 = v0 + rowSize;
			float* v2 = v1 + 2;
			float* v3 = v0 + 2;

			glBegin(GL_TRIANGLES);
				glTexCoord2fv(t0);
				glVertex2fv(v0);
				glTexCoord2fv(t1);
				glVertex2fv(v1);
				glTexCoord2fv(t2);
				glVertex2fv(v2);
			glEnd();
			glBegin(GL_TRIANGLES);
				glTexCoord2fv(t2);
				glVertex2fv(v2);
				glTexCoord2fv(t3);
				glVertex2fv(v3);
				glTexCoord2fv(t0);
				glVertex2fv(v0);
			glEnd();

			t0 += 2;
			v0 += 2;
		}
	}
} // noBindDraw

void
bindDraw() {
	int rowSize = 2 * nPoints;
	for (int y = 0; y < nPoints - 1; ++y) {
		float* v0 = vertices + y * rowSize;
		float* t0 = texCoords + y * rowSize;
		for (int x = 0; x < nPoints - 1; ++x) {
			float* t1 = t0 + rowSize;
			float* t2 = t1 + 2;
			float* t3 = t0 + 2;
			float* v1 = v0 + rowSize;
			float* v2 = v1 + 2;
			float* v3 = v0 + 2;

			glBindTexture(GL_TEXTURE_2D, redTex);
			glBegin(GL_TRIANGLES);
				glTexCoord2fv(t0);
				glVertex2fv(v0);
				glTexCoord2fv(t1);
				glVertex2fv(v1);
				glTexCoord2fv(t2);
				glVertex2fv(v2);
			glEnd();

			glBindTexture(GL_TEXTURE_2D, greenTex);
			glBegin(GL_TRIANGLES);
				glTexCoord2fv(t2);
				glVertex2fv(v2);
				glTexCoord2fv(t3);
				glVertex2fv(v3);
				glTexCoord2fv(t0);
				glVertex2fv(v0);
			glEnd();

			t0 += 2;
			v0 += 2;
		}
	}
} // BindDraw

class BindDrawTimer: public GLEAN::Timer {
	virtual void op()     { bindDraw(); }
	virtual void preop()  { glFinish(); }
	virtual void postop() { glFinish(); }
};

class NoBindDrawTimer: public GLEAN::Timer {
	virtual void op()     { noBindDraw(); }
	virtual void preop()  { glFinish();   }
	virtual void postop() { glFinish();   }
};

void
logStats(GLEAN::TexBindPerfResult& r, GLEAN::Environment* env) {
	env->log << "\tApproximate texture binding time = " << r.bindTime
		<< " microseconds.\n\tRange of valid measurements = ["
		<< r.lowerBound << ", " << r.upperBound << "]\n";
} // logStats

} // anonymous namespace

namespace GLEAN {

///////////////////////////////////////////////////////////////////////////////
// runOne:  Run a single test case
///////////////////////////////////////////////////////////////////////////////

void
TexBindPerf::runOne(TexBindPerfResult& r, Window& w) {
	glGenTextures(1, &redTex);
	glBindTexture(GL_TEXTURE_2D, redTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	redImage.makeMipmaps(GL_RGB);

	glGenTextures(1, &greenTex);
	glBindTexture(GL_TEXTURE_2D, greenTex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	greenImage.makeMipmaps(GL_RGB);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	GLUtils::useScreenCoords(drawingSize + 2, drawingSize + 2);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_TEXTURE_2D);
	glColor4f(1.0, 1.0, 1.0, 1.0);

	nPoints = drawingSize / 2;	// Yields 1-pixel triangles.

	RandomDouble vRand(142857);
	RandomMesh2D v(1.0, drawingSize, nPoints, 1.0, drawingSize, nPoints,
		vRand);
	vertices = v(0, 0);

	RandomDouble tRand(314159);
	RandomMesh2D t(0.0, 1.0, nPoints, 0.0, 1.0, nPoints, tRand);
	texCoords = t(0, 0);

	int nTris = (nPoints - 1) * (nPoints - 1) / 2;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	BindDrawTimer   bindDrawTimer;
	NoBindDrawTimer noBindDrawTimer;

	bindDrawTimer.calibrate();
	noBindDrawTimer.calibrate();

	vector<float> measurements;
	for (int i = 0; i < 5; ++i) {
		env->quiesce();
		double tBind = bindDrawTimer.time();
		w.swap();	// So the user can see something happening.

		env->quiesce();
		double tNoBind = noBindDrawTimer.time();
		w.swap();

		double bindTime = 1E6 * (tBind - tNoBind) / nTris;
		if (bindTime < 0.0) {
			// This can happen if the system isn't quiescent;
			// some process sneaks in and takes wall-clock time
			// when ``noBindDraw'' is running.  Just flush it
			// and try again.  (Note:  You really shouldn't be
			// running timing tests on a system where other
			// processes are active!)
			--i;
			continue;
		}

		measurements.push_back(bindTime);
	}

	sort(measurements.begin(), measurements.end());
	r.bindTime = (measurements[1]+measurements[2]+measurements[3]) / 3.0;
	r.lowerBound = measurements[1];
	r.upperBound = measurements[3];
	r.pass = true;
} // TexBindPerf::runOne

///////////////////////////////////////////////////////////////////////////////
// logOne:  Log a single test case
///////////////////////////////////////////////////////////////////////////////
void
TexBindPerf::logOne(TexBindPerfResult& r) {
	logPassFail(r);
	logConcise(r);
	logStats(r, env);
} // TexBindPerf::logOne

///////////////////////////////////////////////////////////////////////////////
// compareOne:  Compare results for a single test case
///////////////////////////////////////////////////////////////////////////////
void
TexBindPerf::compareOne(TexBindPerfResult& oldR, TexBindPerfResult& newR) {
	if (env->options.verbosity) {
	if (newR.bindTime < oldR.lowerBound) {
		int percent = static_cast<int>(
			100.0 * (oldR.bindTime - newR.bindTime) / newR.bindTime
			+ 0.5);
		env->log << name << ":  DIFF "
			<< newR.config->conciseDescription() << '\n'
			<< '\t' << env->options.db2Name << " may be "
			<< percent << "% faster.\n";
	} else if (newR.bindTime > oldR.upperBound) {
		int percent = static_cast<int>(
			100.0 * (newR.bindTime - oldR.bindTime) / oldR.bindTime
			+ 0.5);
		env->log << name << ":  DIFF "
			<< oldR.config->conciseDescription() << '\n'
			<< '\t' << env->options.db1Name << " may be "
			<< percent << "% faster.\n";
	} else {
			env->log << name << ":  SAME "
				<< newR.config->conciseDescription()
				<< "\n\t"
				<< env->options.db2Name
				<< " test time falls within the "
				<< "valid measurement range of "
				<< env->options.db1Name
				<< " test time.\n";
	}
	}
	if (env->options.verbosity) {
		env->log << env->options.db1Name << ':';
		logStats(oldR, env);
		env->log << env->options.db2Name << ':';
		logStats(newR, env);
	}
} // TexBindPerf::compareOne

///////////////////////////////////////////////////////////////////////////////
// The test object itself:
///////////////////////////////////////////////////////////////////////////////
TexBindPerf texBindPerfTest("texBindPerf", "window, rgb, z",

	"This test makes a rough estimate of the cost of a glBindTexture()\n"
	"operation, expressed in microseconds.\n"
	"\n"
	"Since the apparent cost of a texture bind is dependent on many\n"
	"factors (including the fraction of the texture map that's actually\n"
	"used for drawing, on machines that cache textures; texture map\n"
	"size; texel format; etc.), a general-purpose test can only estimate\n"
	"it.  In this test we do so by drawing random triangles of very\n"
	"small size, and reporting simple statistics concerning the cost.\n");


} // namespace GLEAN