summaryrefslogtreecommitdiff
path: root/tests/glean/tdepthstencil.cpp
blob: 54e97df0f01411cb03737e1de244307fc91c5735 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// 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

// tdepthstencil.h:  Test GL_EXT_packed_depth_stencil extension.
// Brian Paul  1 October 2005


#include <cassert>
#include <cmath>
#include <cstring>
#include "tdepthstencil.h"
#include "rand.h"
#include "timer.h"
#include "image.h"

#ifdef GL_EXT_packed_depth_stencil

namespace GLEAN {

static PFNGLWINDOWPOS2IARBPROC WindowPos2i = NULL;


DepthStencilResult::DepthStencilResult()
{
	pass = false;
	readDepthStencilRate = 0;
	readDepthUintRate = 0;
	readDepthUshortRate = 0;
}


bool
DepthStencilTest::checkError(const char *where)
{
	GLenum err = glGetError();
	if (err) {
		errorCode = err;
		errorPos = where;
		return true;
	}
	return false;
}

void
DepthStencilTest::setup(void)
{
	glGetIntegerv(GL_DEPTH_BITS, &depthBits);
	glGetIntegerv(GL_STENCIL_BITS, &stencilBits);

        WindowPos2i = (PFNGLWINDOWPOS2IARBPROC)
		GLUtils::getProcAddress("glWindowPos2iARB");
        assert(WindowPos2i);
}


// If we're lacking a depth and/or stencil buffer we'll just run this test.
// Return true if pass, false if fail.
bool
DepthStencilTest::testInsufficientVisual(void)
{
	GLuint p[1];

	glDrawPixels(1, 1, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, p);
	if (glGetError() != GL_INVALID_OPERATION) {
		sprintf(errorMsg,
			"glDrawPixels failed to raise GL_INVALID_OPERATION"
			" when there's no depth or stencil buffer.");
		return false;
	}

	glCopyPixels(0, 0, 5, 5, GL_DEPTH_STENCIL_EXT);
	if (glGetError() != GL_INVALID_OPERATION) {
		sprintf(errorMsg,
			"glCopyPixels failed to raise GL_INVALID_OPERATION"
			" when there's no depth or stencil buffer.");
		return false;
	}

	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT,
			 0, 0, 1, 1, 0);
	if (glGetError() != GL_INVALID_OPERATION) {
		sprintf(errorMsg,
			"glCopyTexImage2D failed to raise GL_INVALID_OPERATION"
			" when there's no depth or stencil buffer.");
		return false;
	}

	return true;
}


// Each of these OpenGL calls in this function should generate an error!
// Note to GL implementors: if you find any errors here, you better check
// your glTexImage functions too!
bool
DepthStencilTest::testErrorDetection(void)
{
	GLuint p[1];

	glDrawPixels(1, 1, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT, p);
	if (glGetError() != GL_INVALID_ENUM) {
		sprintf(errorMsg,
			"glDrawPixels(GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT)"
			" failed to generate GL_INVALID_ENUM.");
		return false;
	}

	glDrawPixels(1, 1, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_EXT, p);
	if (glGetError() != GL_INVALID_OPERATION) {
		sprintf(errorMsg,
			"glDrawPixels(GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8_EXT)"
			" failed to generate GL_INVALID_OPERATION.");
		return false;
	}

	glReadPixels(0, 0, 1, 1, GL_DEPTH_STENCIL_EXT, GL_FLOAT, p);
	if (glGetError() != GL_INVALID_ENUM) {
		sprintf(errorMsg,
			"glReadPixels(GL_DEPTH_STENCIL_EXT, GL_FLOAT)"
			" failed to generate GL_INVALID_ENUM.");
		return false;
	}

	glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT_24_8_EXT, p);
	if (glGetError() != GL_INVALID_OPERATION) {
		sprintf(errorMsg,
			"glReadPixels(GL_STENCIL_INDEX, GL_UNSIGNED_INT_24_8_EXT)"
			" failed to generate GL_INVALID_OPERATION.");
		return false;
	}

	return true;
}


bool
DepthStencilTest::testDrawAndRead(void)
{
	// the reference image
	static const GLuint image[4] = {
		0x00000000,
		0x000000ff,
		0xffffff00,
		0xffffffff
	};
	GLuint readback[4];

	WindowPos2i(0, 0);
	glDrawPixels(2, 2, GL_DEPTH_STENCIL_EXT,
		     GL_UNSIGNED_INT_24_8_EXT, image);
	if (checkError("glDrawPixels in testDrawAndRead"))
		return false;

	glReadPixels(0, 0, 2, 2, GL_DEPTH_STENCIL_EXT,
		     GL_UNSIGNED_INT_24_8_EXT, readback);
	if (checkError("glReadPixels in testDrawAndRead"))
		return false;

	for (int i = 0; i < 4; i++) {
		if (image[i] != readback[i]) {
			sprintf(errorMsg,
				"Image returned by glReadPixels didn't match"
				" the expected result (0x%x != 0x%x)",
				readback[i], image[i]);
			return false;
		}
	}

	// test depth scale/bias and stencil mapping (in a trivial way)
	glPixelTransferf(GL_DEPTH_SCALE, 0.0);  // map all depths to 1.0
	glPixelTransferf(GL_DEPTH_BIAS, 1.0);
	GLuint stencilMap[2] = { 2, 2 };  // map all stencil values to 2
	glPixelMapuiv(GL_PIXEL_MAP_S_TO_S, 2, stencilMap);
	glPixelTransferi(GL_MAP_STENCIL, 1);
	glReadPixels(0, 0, 2, 2, GL_DEPTH_STENCIL_EXT,
		     GL_UNSIGNED_INT_24_8_EXT, readback);
	if (checkError("glReadPixels in testDrawAndRead"))
		return false;
	for (int i = 0; i < 4; i++) {
		if (readback[i] != 0xffffff02) {
			sprintf(errorMsg,
				"Image returned by glReadPixels didn't match"
				" the expected result (0x%x != 0xffffff02)",
				readback[i]);
			return false;
		}
	}
	glPixelTransferf(GL_DEPTH_SCALE, 1.0);
	glPixelTransferf(GL_DEPTH_BIAS, 0.0);
	glPixelTransferi(GL_MAP_STENCIL, 0);

	return true;
}


bool
DepthStencilTest::testTextureOperations(void)
{
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT,
			 0, 0, 1, 1, 0);
	if (checkError("glCopyTexImage2D in testTextureOperations."))
		return false;

	glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
	if (checkError("glCopyTexSubImage2D in testTextureOperations."))
		return false;

	return true;
}


double
DepthStencilTest::readPixelsRate(GLenum format, GLenum type)
{
	const int width = drawingSize, height = drawingSize;
	GLuint *img = new GLuint [width * height];

	WindowPos2i(0, 0);
	glDrawPixels(width, height, GL_DEPTH_STENCIL_EXT,
		     GL_UNSIGNED_INT_24_8_EXT, img);

	const double minInterval = 2.0; // two seconds
	Timer tTimer;
	double start = tTimer.getClock();
	double elapsedTime = 0.0;
	int iterations = 0;
	do {
		for (int i = 0; i < 50; i++) {
			glReadPixels(0, 0, width, height, format, type, img);
			iterations++;
		}

		double finish = tTimer.getClock();
		elapsedTime = finish - start;
	} while (elapsedTime < minInterval);

	delete [] img;

	double rate = width * height * iterations / elapsedTime;
	return rate;  // pixels/second
}


void
DepthStencilTest::testPerformance(DepthStencilResult &r)
{
	r.readDepthStencilRate = readPixelsRate(GL_DEPTH_STENCIL_EXT,
						GL_UNSIGNED_INT_24_8_EXT);
	r.readDepthUintRate = readPixelsRate(GL_DEPTH_COMPONENT,
					     GL_UNSIGNED_INT);
	r.readDepthUshortRate = readPixelsRate(GL_DEPTH_COMPONENT,
					       GL_UNSIGNED_SHORT);

	// XXX maybe also test glCopyTexImage, etc.
}


void
DepthStencilTest::runOne(DepthStencilResult &r, Window &w)
{
	(void) w;  // silence warning
	r.pass = true;
	errorCode = 0;
	errorPos = NULL;
	errorMsg[0] = 0;

	setup();

	if (depthBits == 0 || stencilBits == 0) {
		r.pass = testInsufficientVisual();
		return;
	}

	if (r.pass)
		r.pass = testErrorDetection();
	if (r.pass)
		r.pass = testDrawAndRead();
	if (r.pass)
		r.pass = testTextureOperations();
	if (r.pass)
		testPerformance(r);
}


void
DepthStencilTest::logOne(DepthStencilResult &r)
{
	if (r.pass) {
		logPassFail(r);
		logConcise(r);

		char str[1000];
		double mbps;

		env->log << "\tglReadPixels GL_DEPTH_STENCIL rate: ";
		mbps = r.readDepthStencilRate * sizeof(GLuint) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		env->log << str << " MBytes per second.\n";

		env->log << "\tglReadPixels GL_DEPTH/GLuint rate: ";
		mbps = r.readDepthUintRate * sizeof(GLuint) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		env->log << str << " MBytes per second.\n";

		env->log << "\tglReadPixels GL_DEPTH/GLushort rate: ";
		mbps = r.readDepthUshortRate * sizeof(GLshort) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		env->log << str << " MBytes per second.\n";
	}
	else {
		env->log << name << "FAIL\n";
		if (errorCode) {
			env->log << "\tOpenGL Error " << gluErrorString(errorCode)
				 << " at " << errorPos << "\n";
		}
		else if (errorMsg[0]) {
			env->log << "\t" << errorMsg << "\n";
		}
	}
}


void
DepthStencilTest::compareOne(DepthStencilResult &oldR,
			     DepthStencilResult &newR)
{
	comparePassFail(oldR, newR);

	if (newR.pass && oldR.pass == newR.pass) {
		if (env->options.verbosity) {
			env->log << "\tReadPixels rate:\n";
			env->log << "\t\tGL_DEPTH_STENCIL:\n";
			env->log << "\t\t\told: " << oldR.readDepthStencilRate;
			env->log << "\t\t\tnew: " << newR.readDepthStencilRate;
			env->log << "\t\tGL_DEPTH/GL_UNSIGNED_INT:\n";
			env->log << "\t\t\told: " << oldR.readDepthUintRate;
			env->log << "\t\t\tnew: " << newR.readDepthUintRate;
			env->log << "\t\tGL_DEPTH/GL_UNSIGNED_SHORT:\n";
			env->log << "\t\t\told: " << oldR.readDepthUshortRate;
			env->log << "\t\t\tnew: " << newR.readDepthUshortRate;
		}
	}
	else {
		env->log << "\tNew: ";
		env->log << (newR.pass ? "PASS" : "FAIL");
		env->log << "\tOld: ";
		env->log << (oldR.pass ? "PASS" : "FAIL");
	}
}


void
DepthStencilResult::putresults(ostream &s) const
{
	if (pass) {
		s << "PASS\n";

		char str[1000];
		double mbps;

		mbps = readDepthStencilRate * sizeof(GLuint) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		s << str << "\n";

		mbps = readDepthUintRate * sizeof(GLuint) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		s << str << "\n";

		mbps = readDepthUshortRate * sizeof(GLushort) / (1024*1024);
		sprintf(str, "%.2f", mbps);
		s << str << "\n";
	}
	else {
		s << "FAIL\n";
	}
}


bool
DepthStencilResult::getresults(istream &s)
{
	char result[1000];
	s >> result;

	if (strcmp(result, "FAIL") == 0) {
		pass = false;
	}
	else {
		pass = true;
		s >> readDepthStencilRate;
		s >> readDepthUintRate;
		s >> readDepthUshortRate;
	}
	return s.good();
}


// The test object itself:
DepthStencilTest depthstencilTest("depthStencil", "window, rgb",
			"GL_EXT_packed_depth_stencil GL_ARB_window_pos",
			"Test the GL_EXT_packed_depth_stencil extension.\n");



} // namespace GLEAN

#endif // GL_EXT_packed_depth_stencil