aboutsummaryrefslogtreecommitdiff
path: root/tests/texturing/depth-tex-compare.c
blob: d44a0024dae9159c6696fc2f0491079cad28de1a (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
/*
 * Copyright © 2009 Intel Corporation
 *
 * 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.
 */

// author: Ben Holmes

/*
 * This test draws depth textures as LUMINANCE, INTENSITY, and ALPHA. These
 * textures are compared to the r component of the texture coordinate and
 * compared using all eight texture compare functions. The result of the
 * comparison is modulated with the vertex color (pink) and blended with the
 * clear color (green) using the alpha value.
 */

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

#define BOX_SIZE 25

PIGLIT_GL_TEST_MAIN(
    400 /*window_width*/,
    300 /*window_height*/,
    GLUT_DOUBLE | GLUT_RGB)

static GLuint tex;

static const char *const compare_names[8] = {
	"GL_NEVER", "GL_LESS", "GL_EQUAL", "GL_LEQUAL",
	"GL_GREATER", "GL_NOTEQUAL", "GL_GEQUAL", "GL_ALWAYS"
};

static const char *const mode_names[3] = {
	"GL_ALPHA", "GL_LUMINANCE", "GL_INTENSITY"
};

void
piglit_init(int argc, char **argv)
{
	#define height 2
	#define width 2
	int i, j;

	GLfloat texDepthData[width][height];

	(void) argc;
	(void) argv;

	piglit_require_extension("GL_ARB_depth_texture");
	piglit_require_extension("GL_ARB_shadow");
	piglit_require_extension("GL_EXT_shadow_funcs");
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glClearColor(0.0, 1.0, 0.0, 1.0);

	for (i=0; i < width; ++i) {
		for (j=0; j < height; ++j) {
			if ((i+j) & 1) {
				texDepthData[i][j] = 1.0;
			}
			else {
				texDepthData[i][j] = 0.5;
			}
		}
	}

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
	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);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
			GL_COMPARE_R_TO_TEXTURE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0,
			GL_DEPTH_COMPONENT, GL_FLOAT, texDepthData);
	#undef height
	#undef width
}


enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;

	static const GLfloat pink[3] = {1.0, 0.0, 1.0};
	static const GLfloat white[3] = {1.0, 1.0, 1.0};
	static const GLfloat black[3] = {0.0, 0.0, 0.0};
	static const GLfloat green[3] = {0.0, 1.0, 0.0};

	static const struct {
		GLenum compare;
		float r0;
		float r1;
		const GLfloat *probes[9];
	} tests[] = {
		{
			GL_LESS,     2.0, 0.0,
			{
				pink, white, white,
				pink, black, black,
				pink, green, green
			}
		},
		{
			GL_LEQUAL,   2.0, 0.0,
			{
				pink, white, pink,
				pink, black, pink,
				pink, green, pink,
			}
		},
		{
			GL_GREATER,  2.0, 0.0,
			{
				white, pink, white,
				black, pink, black,
				green, pink, green,
			}
		},
		{
			GL_GEQUAL,   2.0, 0.0,
			{
				white, pink, pink,
				black, pink, pink,
				green, pink, pink,
			}
		},
		{
			GL_ALWAYS,   2.0, 0.0,
			{
				pink, pink, pink,
				pink, pink, pink,
				pink, pink, pink,
			}
		},
		{
			GL_NEVER,    2.0, 0.0,
			{
				white, white, white,
				black, black, black,
				green, green, green,
			}
		},
		{
			GL_NOTEQUAL, 0.5, 0.5,
			{
				white, white, pink,
				black, black, pink,
				green, green, pink,
			}
		},
		{
			GL_EQUAL,    0.5, 0.5,
			{
				pink, pink, white,
				pink, pink, black,
				pink, pink, green,
			}
		},
	};

	static const GLenum modes[3] = {
		GL_ALPHA, GL_LUMINANCE, GL_INTENSITY
	};

	unsigned row;
	unsigned col;

	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0, 0.0, 1.0);

	glBindTexture(GL_TEXTURE_2D, tex);
	for (row = 0; row < ARRAY_SIZE(tests); row++) {
		const int y = 275 - (35 * row);
		const GLenum compare = tests[row].compare;

		for (col = 0; col < 3; col++) {
			const int x = 150 + (col * 50);
			unsigned i;

			glTexParameteri(GL_TEXTURE_2D,
					GL_TEXTURE_COMPARE_FUNC,
					compare);
			glTexParameteri(GL_TEXTURE_2D,
					GL_DEPTH_TEXTURE_MODE,
					modes[col]);

			glBegin(GL_TRIANGLE_STRIP);
			glTexCoord3f(1.0, 0.0, tests[row].r0);
			glVertex2f(x + BOX_SIZE, y);
			glTexCoord3f(1.0, 1.0, tests[row].r0);
			glVertex2f(x + BOX_SIZE, y + BOX_SIZE);
			glTexCoord3f(0.0, 0.0, tests[row].r1);
			glVertex2f(x,            y);
			glTexCoord3f(0.0, 1.0, tests[row].r1);
			glVertex2f(x,            y + BOX_SIZE);
			glEnd();

			for (i = 0; i < 3; i++) {
				const GLfloat *const color =
					tests[row].probes[(3 * col) + i];

				if (!piglit_probe_pixel_rgb(x + 5 + (i * 5),
							    y + 10,
							    color)) {
					if (!piglit_automatic) {
						printf("compare = %s, mode = %s\n",
						       compare_names[compare - GL_NEVER],
						       mode_names[col]);
					}

					pass = GL_FALSE;
				}
			}
		}
	}

	piglit_present_results();

	printf("Left to Right: ALPHA, LUMINANCE, INTENSITY\n");
	printf("Top to Bottom: LESS, LEQUAL, GREATER, GEQUAL, "
	       "ALWAYS, NEVER, NOTEQUAL, EQUAL\n");

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}