summaryrefslogtreecommitdiff
path: root/tests/general/select.c
blob: fa60e39d9355ae7b5d845b9e800cd433c21a3d1b (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
/*
 * Copyright © 2011
 *
 * 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.
 *
 * Authors:
 *	 Pierre-Eric Pelloux-Prayer <pelloux@gmail.com>
 *
 * Description:
 *  This test represents a simple usage of GL_SELECT rendering mode.
 *  It draws several squares to screen, with various GL_..._TEST active,
 *  and then verifies the number of hits and the content of the select buffer.
 *  Based on this documentation: http://glprogramming.com/red/chapter13.html
 *
 */

#include "piglit-util.h"
#include "piglit-framework.h"

int piglit_width = 100;
int piglit_height = 100;
int piglit_window_mode = GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH | GLUT_STENCIL;

GLuint ReferenceHitEntries[3][64];
#define NAME_STACK_DEPTH	0
#define MIN_Z			1
#define MAX_Z			2
#define NAME_STACK_0		3

/**
 * Draw 4 objects and handle name stack
 */
static void
draw_objects()
{
	float zscale = (float)(~0u);
	glInitNames();

	/* no draw call issued for name '0' */
	glPushName(0);

	/* OBJECT 1 */
	glPushName(1);
	/* draw object */
	glColor3f(1.0, 0.0, 0.0);
	piglit_draw_rect_z(0.8, 10, 30, 50, 50);
	/* fill reference buffer */
	ReferenceHitEntries[0][NAME_STACK_DEPTH] = 2;
	ReferenceHitEntries[0][MIN_Z] = (GLuint)roundf(zscale * ((1 - 0.8) * 0.5));
	ReferenceHitEntries[0][MAX_Z] = ReferenceHitEntries[0][MIN_Z];
	ReferenceHitEntries[0][NAME_STACK_0] = 0;
	ReferenceHitEntries[0][NAME_STACK_0 + 1] = 1;

	/* OBJECT 2 */
	/* 2 draw calls for name '2' */
	glPushName(2);
	glColor3f(0.0, 1.0, 0.0);
	piglit_draw_rect_z(0.5, 40, 5, 25, 30);
	piglit_draw_rect_z(0.4, 10, 75, 25, 10);
	/* fill reference buffer */
	ReferenceHitEntries[1][NAME_STACK_DEPTH] = 3;
	ReferenceHitEntries[1][MIN_Z] = (GLuint)roundf(zscale * ((1 - 0.5)*0.5));
	ReferenceHitEntries[1][MAX_Z] = (GLuint)roundf(zscale * ((1 - 0.4)*0.5));
	ReferenceHitEntries[1][NAME_STACK_0] = 0;
	ReferenceHitEntries[1][NAME_STACK_0 + 1] = 1;
	ReferenceHitEntries[1][NAME_STACK_0 + 2] = 2;

	/* OBJECT 3 */
	glPopName();
	glPushName(3);
	/* drawn offscreen */
	piglit_draw_rect_z(0.3, 250, 45, 280, 20);

	/* OBJECT 4 */
	/* glLoadName instead of glPushName */
	glLoadName(4);
	glColor3f(0.0, 0.0, 1.0);
	piglit_draw_rect_z(0.2, 50, 45, 80, 20);
	/* fill reference buffer */
	ReferenceHitEntries[2][NAME_STACK_DEPTH] = 3;
	ReferenceHitEntries[2][MIN_Z] = (GLuint)roundf(zscale * ((1 - 0.2)*0.5));
	ReferenceHitEntries[2][MAX_Z] = ReferenceHitEntries[2][MIN_Z];
	ReferenceHitEntries[2][NAME_STACK_0] = 0;
	ReferenceHitEntries[2][NAME_STACK_0 + 1] = 1;
	ReferenceHitEntries[2][NAME_STACK_0 + 2] = 4;
}

/**
 * Helper function to compare 2 hit records
 */
static bool
compare_hit_record(GLuint* hit1, GLuint* hit2)
{
	int i;
	float zscale = (float)(~0u);
	float diffz;

	if (hit1[NAME_STACK_DEPTH] != hit2[NAME_STACK_DEPTH]) {
		printf("\t%s : Incorrect name stack depth : %u %u\n",
			__FUNCTION__,
			hit1[NAME_STACK_DEPTH],
			hit2[NAME_STACK_DEPTH]);
		return false;
	}

	diffz = abs(hit1[MIN_Z] - hit2[MIN_Z])/zscale;
	if (diffz > 0.1) {
		printf("\t%s : Incorrect Minz : %u %u (%f %f) %f\n",
			__FUNCTION__,
			hit1[MIN_Z],
			hit2[MIN_Z],
			hit1[MIN_Z] / zscale,
			hit2[MIN_Z] / zscale,
			diffz);
		return false;
	}

	diffz = abs(hit1[MAX_Z] - hit2[MAX_Z])/zscale;
	if (diffz > 0.1) {
		printf("\t%s : Incorrect Maxz : %u %u (%f %f) %f\n",
			__FUNCTION__,
			hit1[MAX_Z],
			hit2[MAX_Z],
			hit1[MAX_Z] / zscale,
			hit2[MAX_Z] / zscale,
			diffz);
		return false;
	}

	for (i=0; i<hit1[NAME_STACK_DEPTH]; i++) {
		if (hit1[NAME_STACK_0 + i] != hit2[NAME_STACK_0 + i])
			return false;
	}
	return true;
}

/**
 * Helper function to check select buffer
 */
static bool
validate_select_buffer(GLuint* buffer)
{
	int i,j;
	GLint hits = glRenderMode(GL_RENDER);
	bool object_hit_found[3] = {false, false, false};
	GLuint* ptr;

	if (hits != 3) {
		printf("\t%s : unexpected hit count:%d\n", __FUNCTION__, hits);
		return false;
	}

	ptr = buffer;
	/* ordering in select buffer isn't necessarly the same as drawing order,
	 *  so we need to look for each hit entry
	 */
	for (i=0; i<hits; i++) {
		for (j=0; j<3; j++) {
			if (!object_hit_found[j]) {
				if (compare_hit_record(ptr, ReferenceHitEntries[j])) {
					object_hit_found[j] = true;
					break;
				}
			}
		}
		if (j == 3)
			return false;

		/* advance pointer */
		ptr += 3 + ptr[NAME_STACK_DEPTH];
	}

	return true;
}

void
test_case_setup(bool depth, bool stencil, bool alpha, bool scissor) {
	if (depth)
		glEnable(GL_DEPTH_TEST);
	else
		glDisable(GL_DEPTH_TEST);

	if (stencil)
		glEnable(GL_STENCIL_TEST);
	else
		glDisable(GL_STENCIL_TEST);

	if (alpha)
		glEnable(GL_ALPHA_TEST);
	else
		glDisable(GL_ALPHA_TEST);

	if (scissor)
		glEnable(GL_SCISSOR_TEST);
	else
		glDisable(GL_SCISSOR_TEST);

	/* setup all test functions to never pass */
	glDepthFunc(GL_NEVER);
	glStencilFunc(GL_NEVER, 0, 0);
	glAlphaFunc(GL_NEVER, 0);
	glScissor(0, 0, 0, 0);
}

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

enum piglit_result
do_blit_test(void)
{
	GLuint buff[64] = {0};
	glSelectBuffer(64, buff);

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
	glClearColor(0.5, 0.5, 0.5, 0.0);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glRenderMode(GL_SELECT);
	draw_objects();

	return validate_select_buffer(buff) ? PIGLIT_PASS : PIGLIT_FAIL;
}

void piglit_init(int argc, char**argv)
{
	enum piglit_result pass;

	if (argc < 2) {
		test_case_setup(false, false, false, false);
		pass = do_blit_test();
	} else {
		test_case_setup(
			strcmp(argv[1], "depth") == 0,
			strcmp(argv[1], "stencil") == 0,
			strcmp(argv[1], "alpha") == 0,
			strcmp(argv[1], "scissor") == 0);
		pass = do_blit_test();
	}
	piglit_report_result(pass);
}