summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-depthstencil.c
blob: 094e5ea41a2a510b79bec20d045aede441f0fa76 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
 *
 * 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 (including the next
 * paragraph) 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
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 */

/** @file fbo-depthstencil.c
 *
 * Tests glClear, glReadPixels, glDrawPixels, glCopyPixels, glBlitFramebuffer
 * with depth-stencil buffers.
 */

#include "piglit-util.h"

#define BUF_SIZE 123
int piglit_width = BUF_SIZE;
int piglit_height = BUF_SIZE;
int piglit_window_mode = GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL;
enum {
	CLEAR,
	READPIXELS,
	DRAWPIXELS,
	COPYPIXELS,
	BLIT
};
int test = CLEAR;

#define F(name) #name, name

struct format {
	const char *name;
	GLenum iformat;
	const char *extension;
} formats[] = {
	{"default_fb", 0, NULL},
	{F(GL_DEPTH24_STENCIL8),  "GL_EXT_packed_depth_stencil"},
	{F(GL_DEPTH32F_STENCIL8), "GL_ARB_depth_buffer_float"}
};

struct packformat {
	const char *name;
	GLenum type;
	const char *extension;
} packformats[] = {
	{"FLOAT-and-USHORT", 0, NULL},
	{"24_8", GL_UNSIGNED_INT_24_8, "GL_EXT_packed_depth_stencil"},
	{"32F_24_8_REV", GL_FLOAT_32_UNSIGNED_INT_24_8_REV, "GL_ARB_depth_buffer_float"}
};

struct format f;
struct packformat pf;
GLuint stencilmask;

static enum piglit_result test_clear(void)
{
	GLuint cb;
	GLenum status;
	float green[3] = {0, 1, 0};
	enum piglit_result res;

	/* Add a colorbuffer. */
	if (f.iformat) {
		glGenRenderbuffersEXT(1, &cb);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, cb);
		glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, BUF_SIZE, BUF_SIZE);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0,
					     GL_RENDERBUFFER_EXT, cb);

		glDrawBuffer(GL_COLOR_ATTACHMENT0);
		glReadBuffer(GL_COLOR_ATTACHMENT0);
		status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
		if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
			printf("FBO incomplete status 0x%X\n", status);
			piglit_report_result(PIGLIT_FAIL); /* RGBA8 must succeed. */
		}
	}

	glClearDepth(0.75);
	glClearStencil(0x3456);
	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_EQUAL);
	glEnable(GL_STENCIL_TEST);
	glStencilFunc(GL_EQUAL, 0x3456 & stencilmask, ~0);

	glColor3fv(green);
	piglit_draw_rect_z(0.5, -1, -1, 2, 2); /* 0.75 converted to clip space is 0.5. */
	glColor3f(1, 1, 1);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_STENCIL_TEST);

	res = piglit_probe_rect_rgb(0, 0, BUF_SIZE, BUF_SIZE, green) ? PIGLIT_PASS : PIGLIT_FAIL;

	/* Display the colorbuffer. */
	if (!piglit_automatic && f.iformat) {
		glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
		glBlitFramebufferEXT(0, 0, BUF_SIZE, BUF_SIZE, 0, 0, BUF_SIZE, BUF_SIZE,
				     GL_COLOR_BUFFER_BIT, GL_NEAREST);
	}

	if (f.iformat)
		glDeleteRenderbuffersEXT(1, &cb);

	return res;
}

typedef void (*transfer_func)(GLfloat*, GLushort*);

static void read_separately(GLfloat *depth, GLushort *stencil)
{
	glReadPixels(0, 0, BUF_SIZE, BUF_SIZE, GL_DEPTH_COMPONENT,
		     GL_FLOAT, depth);
	glReadPixels(0, 0, BUF_SIZE, BUF_SIZE, GL_STENCIL_INDEX,
		     GL_UNSIGNED_SHORT, stencil);
}

static void read_24_8(GLfloat *depth, GLushort *stencil)
{
	GLuint buf[BUF_SIZE*BUF_SIZE];
	int x, y;

	glReadPixels(0, 0, BUF_SIZE, BUF_SIZE, GL_DEPTH_STENCIL,
		     GL_UNSIGNED_INT_24_8, buf);

	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {
			depth[y*BUF_SIZE+x] = (buf[y*BUF_SIZE+x] >> 8) / (float)0xffffff;
			stencil[y*BUF_SIZE+x] = buf[y*BUF_SIZE+x] & 0xff;
		}
	}
}

static void read_32f_8(GLfloat *depth, GLushort *stencil)
{
	GLuint buf[BUF_SIZE*BUF_SIZE*2];
	float *buff = (float*)buf;
	int x, y;

	glReadPixels(0, 0, BUF_SIZE, BUF_SIZE, GL_DEPTH_STENCIL,
		     GL_FLOAT_32_UNSIGNED_INT_24_8_REV, buf);

	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {
			depth[y*BUF_SIZE+x] = buff[(y*BUF_SIZE+x)*2];
			stencil[y*BUF_SIZE+x] = buf[(y*BUF_SIZE+x)*2+1] & 0xff;
		}
	}
}

static enum piglit_result compare(transfer_func read)
{
	int x, y, failures = 0;
	GLfloat depth[BUF_SIZE*BUF_SIZE];
	GLfloat expected_depth;
	GLushort stencil[BUF_SIZE*BUF_SIZE];
	GLushort expected_stencil;

	/* Read buffers. */
	read(depth, stencil);

	/* Compare results. */
	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {

			/* Skip the middle row and column of pixels because
			 * drawing polygons for the left/right and bottom/top
			 * quadrants may hit the middle pixels differently
			 * depending on minor transformation and rasterization
			 * differences.
			 */
			if (x == BUF_SIZE / 2 || y == BUF_SIZE / 2)
				continue;

			if (y < BUF_SIZE/2) {
				expected_depth = x < BUF_SIZE/2 ? 0.25 : 0.375;
				expected_stencil = (x < BUF_SIZE/2 ? 0x3333 : 0x6666) & stencilmask;
			} else {
				expected_depth = x < BUF_SIZE/2 ? 0.625 : 0.75;
				expected_stencil = (x < BUF_SIZE/2 ? 0x9999 : 0xbbbb) & stencilmask;
			}

			if (fabs(depth[y*BUF_SIZE+x] - expected_depth) > 0.001) {
				failures++;
				if (failures < 20) {
					printf("Depth at %i,%i   Expected: %f   Observed: %f\n",
						x, y, expected_depth, depth[y*BUF_SIZE+x]);
				} else if (failures == 20) {
					printf("...\n");
				}
			}
			if (stencil[y*BUF_SIZE+x] != expected_stencil) {
				failures++;
				if (failures < 20) {
					printf("Stencil at %i,%i   Expected: 0x%02x   Observed: 0x%02x\n",
						x, y, expected_stencil, stencil[y*BUF_SIZE+x]);
				} else if (failures == 20) {
					printf("...\n");
				}
			}
		}
	}
	if (failures)
		printf("Total failures: %i\n", failures);

	return failures == 0 ? PIGLIT_PASS : PIGLIT_FAIL;
}

static enum piglit_result test_readpixels(transfer_func read)
{
	/* Clear. */
	glClearDepth(0);
	glClearStencil(0xfefe);
	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	/* Initialize. */
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_ALWAYS);

	glEnable(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

	glStencilFunc(GL_ALWAYS, 0x3333 & stencilmask, ~0);
	piglit_draw_rect_z(-0.5, -1, -1, 1, 1);

	glStencilFunc(GL_ALWAYS, 0x6666 & stencilmask, ~0);
	piglit_draw_rect_z(-0.25, 0, -1, 1, 1);

	glStencilFunc(GL_ALWAYS, 0x9999 & stencilmask, ~0);
	piglit_draw_rect_z(0.25, -1, 0, 1, 1);

	glStencilFunc(GL_ALWAYS, 0xbbbb & stencilmask, ~0);
	piglit_draw_rect_z(0.5, 0, 0, 1, 1);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_STENCIL_TEST);

	return compare(read);
}

static void draw_separately(GLfloat *depth, GLushort *stencil)
{
	glDrawPixels(BUF_SIZE, BUF_SIZE, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
	glDrawPixels(BUF_SIZE, BUF_SIZE, GL_STENCIL_INDEX, GL_UNSIGNED_SHORT, stencil);
}

static void draw_24_8(GLfloat *depth, GLushort *stencil)
{
	GLuint buf[BUF_SIZE*BUF_SIZE];
	int x, y;

	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {
			buf[y*BUF_SIZE+x] =
				(stencil[y*BUF_SIZE+x] & 0xff) |
				((GLuint)(depth[y*BUF_SIZE+x] * (double)0xffffff) << 8);
		}
	}

	glDrawPixels(BUF_SIZE, BUF_SIZE, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, buf);
}

static void draw_32f_8(GLfloat *depth, GLushort *stencil)
{
	GLuint buf[BUF_SIZE*BUF_SIZE*2];
	GLfloat *buff = (GLfloat*)buf;
	int x, y;

	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {
			buff[(y*BUF_SIZE+x)*2] = depth[y*BUF_SIZE+x];
			buf[(y*BUF_SIZE+x)*2+1] = stencil[y*BUF_SIZE+x] & 0xff;
		}
	}

	glDrawPixels(BUF_SIZE, BUF_SIZE, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, buf);
}

static enum piglit_result test_drawpixels(transfer_func draw, transfer_func read)
{
	int x, y;
	GLfloat depth[BUF_SIZE*BUF_SIZE];
	GLushort stencil[BUF_SIZE*BUF_SIZE];

	for (y = 0; y < BUF_SIZE; y++) {
		for (x = 0; x < BUF_SIZE; x++) {
			if (y < BUF_SIZE/2) {
				depth[y*BUF_SIZE+x] = x < BUF_SIZE/2 ? 0.25 : 0.375;
				stencil[y*BUF_SIZE+x] = (x < BUF_SIZE/2 ? ~0x3333 : ~0x6666) & stencilmask;
			} else {
				depth[y*BUF_SIZE+x] = x < BUF_SIZE/2 ? 0.625 : 0.75;
				stencil[y*BUF_SIZE+x] = (x < BUF_SIZE/2 ? ~0x9999 : ~0xbbbb) & stencilmask;
			}
		}
	}

	/* Clear. */
	glClearDepth(0);
	glClearStencil(0xfefe);
	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	/* Draw pixels. */
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_ALWAYS);
	draw(depth, stencil);
	glDisable(GL_DEPTH_TEST);

	/* Invert bits. */
	glEnable(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
	piglit_draw_rect(-1, -1, 2, 2);
	glDisable(GL_STENCIL_TEST);

	return compare(read);
}

static enum piglit_result test_copy(void)
{
	/* Clear. */
	glClearDepth(0);
	glClearStencil(0xfefe);
	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	/* Initialize buffers. */
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_ALWAYS);
	glEnable(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

	/* Set the upper-right corner to 0x3333 and copy the content to the lower-left one. */
	glStencilFunc(GL_ALWAYS, 0x3333 & stencilmask, ~0);
	piglit_draw_rect_z(-0.5, 0, 0, 1, 1);
	if (test == BLIT)
		glBlitFramebufferEXT(BUF_SIZE/2+1, BUF_SIZE/2+1, BUF_SIZE, BUF_SIZE,
				     0, 0, BUF_SIZE/2, BUF_SIZE/2,
				     GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
	else
		glCopyPixels(BUF_SIZE/2+1, BUF_SIZE/2+1, BUF_SIZE/2, BUF_SIZE/2, GL_DEPTH_STENCIL);

	/* Initialize the other corners. */
	glStencilFunc(GL_ALWAYS, 0x6666 & stencilmask, ~0);
	piglit_draw_rect_z(-0.25, 0, -1, 1, 1);

	glStencilFunc(GL_ALWAYS, 0x9999 & stencilmask, ~0);
	piglit_draw_rect_z(0.25, -1, 0, 1, 1);

	glStencilFunc(GL_ALWAYS, 0xbbbb & stencilmask, ~0);
	piglit_draw_rect_z(0.5, 0, 0, 1, 1);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_STENCIL_TEST);

	return compare(read_separately);
}

enum piglit_result piglit_display(void)
{
	enum piglit_result res;
	GLuint fb, rb;
	GLint stencil_size;
	GLenum status;
	transfer_func read = NULL, draw = NULL;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT);

	/* Create the FBO. */
	if (f.iformat) {
		glGenRenderbuffersEXT(1, &rb);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
		glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, f.iformat, BUF_SIZE, BUF_SIZE);
		glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_STENCIL_SIZE_EXT, &stencil_size);
		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

		glGenFramebuffersEXT(1, &fb);
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT,
					GL_RENDERBUFFER_EXT, rb);
		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT,
					GL_RENDERBUFFER_EXT, rb);
		glViewport(0, 0, BUF_SIZE, BUF_SIZE);
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);
		status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
		if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
			printf("FBO incomplete status 0x%X\n", status);
			piglit_report_result(PIGLIT_SKIP);
		}
	} else {
		stencil_size = 8;
	}

	stencilmask = (1 << stencil_size) - 1;

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);

	switch (pf.type) {
	case 0:
		read = read_separately;
		draw = draw_separately;
		break;
	case GL_UNSIGNED_INT_24_8:
		read = read_24_8;
		draw = draw_24_8;
		break;
	case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
		read = read_32f_8;
		draw = draw_32f_8;
		break;
	default:
		assert(0);
	}

	switch (test) {
	case CLEAR:
		puts("Testing glClear(depthstencil).");
		res = test_clear();
		break;
	case READPIXELS:
		puts("Testing glReadPixels(depthstencil).");
		res = test_readpixels(read);
		break;
	case DRAWPIXELS:
		puts("Testing glDrawPixels(depthstencil).");
		res = test_drawpixels(draw, read);
		break;
	case COPYPIXELS:
	case BLIT:
		puts(test == BLIT ?
		     "Testing glBlitFramebuffer(depthstencil)." :
		     "Testing glCopyPixels(depthstencil).");
		res = test_copy();
		break;
	default:
		assert(0);
	}

	/* Cleanup. */
	if (f.iformat) {
		glDeleteFramebuffersEXT(1, &fb);
		glDeleteRenderbuffersEXT(1, &rb);
	}

	glutSwapBuffers();

	assert(glGetError() == 0);
	return res;
}

void piglit_init(int argc, char **argv)
{
	unsigned i, p;

	piglit_require_extension("GL_EXT_framebuffer_object");
	piglit_require_extension("GL_EXT_framebuffer_blit");

	for (p = 1; p < argc; p++) {
		if (!strcmp(argv[p], "clear")) {
			test = CLEAR;
			continue;
		}
		if (!strcmp(argv[p], "readpixels")) {
			test = READPIXELS;
			continue;
		}
		if (!strcmp(argv[p], "drawpixels")) {
			test = DRAWPIXELS;
			continue;
		}
		if (!strcmp(argv[p], "copypixels")) {
			test = COPYPIXELS;
			continue;
		}
		if (!strcmp(argv[p], "blit")) {
			test = BLIT;
			continue;
		}
		for (i = 0; i < sizeof(formats)/sizeof(*formats); i++) {
			if (!strcmp(argv[p], formats[i].name)) {
				if (formats[i].extension)
					piglit_require_extension(formats[i].extension);
				f = formats[i];
				printf("Testing %s.\n", f.name);
				break;
			}
		}
		for (i = 0; i < sizeof(packformats)/sizeof(*packformats); i++) {
			if (!strcmp(argv[p], packformats[i].name)) {
				if (packformats[i].extension)
					piglit_require_extension(packformats[i].extension);
				pf = packformats[i];
				printf("Testing %s.\n", pf.name);
				break;
			}
		}
	}

	if (!f.name) {
		printf("Not enough parameters.\n");
		piglit_report_result(PIGLIT_SKIP);
	}
}