aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_texture_swizzle/depth_texture_mode_and_swizzle.c
blob: 074ca64027d021ce9d51151f428ad527aad00291 (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
/*
 * Copyright © 2012 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.
 */

/**
 * \file depth_texture_mode_and_swizzle.c
 *
 * Tests the interactions between EXT_texture_swizzle and DEPTH_TEXTURE_MODE.
 *
 * From the EXT_texture_swizzle specfication:
 * "4) How does this interact with depth component textures?
 *
 *  RESOLVED: The swizzle is applied after the DEPTH_TEXTURE_MODE. This
 *  naturally falls out of specifying the swizzle in terms of Table 3.20."
 *
 * It would be very easy to write an implementation that respects one or the
 * other (but not both), or applies them in the wrong order.  This test guards
 * against those pitfalls.
 */

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

PIGLIT_GL_TEST_MAIN(170, 30, PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE)

enum piglit_result
piglit_display()
{
	bool pass = true;
	int i = 0;
	static const struct {
		GLenum depth_mode;
		int swizzles[4];
		float expected[4];
	} tests[] = {
		{
			GL_INTENSITY,
			{ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA },
			{ .5, .5, .5, .5 }
		},
		{
			GL_INTENSITY,
			{ GL_ONE, GL_GREEN, GL_BLUE, GL_ALPHA },
			{ 1, .5, .5, .5 }
		},
		{
			GL_LUMINANCE,
			{ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA },
			{ .5, .5, .5, 1 }
		},
		{
			GL_LUMINANCE,
			{ GL_RED, GL_ALPHA, GL_ALPHA, GL_ONE },
			{ .5, 1, 1, 1 }
		},
		{
			GL_RED,
			{ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA },
			{ .5, 0, 0, 1 }
		},
		{
			GL_RED,
			{ GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA },
			{ 0, 0, .5, 1 }
		},
		{
			GL_ALPHA,
			{ GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA },
			{ 0, 0, 0, 0.5 }
		},
		{
			GL_ALPHA,
			{ GL_ONE, GL_GREEN, GL_ALPHA, GL_ZERO },
			{ 1, 0, .5, 0 }
		},
	};

	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	glClearColor(0.15, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE,
				tests[i].depth_mode);
		glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA,
				 tests[i].swizzles);

		piglit_draw_rect(10 + 20 * i, 10, 10, 10);

		pass = piglit_probe_rect_rgba(10 + 20 * i, 10, 10, 10,
					      tests[i].expected) && pass;
	}

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

/**
 * Create texel data: a 1x1 depth texture containing 0.5.
 */
void
setup_texture()
{
	GLuint tex;

	const float contents = 0.5;

	glGenTextures(1, &tex);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex);
	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_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1, 1, 0,
		     GL_DEPTH_COMPONENT, GL_FLOAT, &contents);

	/* Omit the complexity of depth comparisons; just use the raw data. */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
}

/**
 * Generate, compile, link, and use the GLSL shaders.
 */
void
setup_shaders()
{
	GLuint vs, fs, prog, tex_location;

	static const char *vs_code =
		 "#version 120\n"
		 "void main()\n"
		 "{\n"
		 "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
		 "}\n";
	static const char *fs_code =
		 "#version 120\n"
		 "uniform sampler2D tex;\n"
		 "void main()\n"
		 "{\n"
		 "   gl_FragColor = texture2D(tex, vec2(0.5, 0.5));\n"
		 "}\n";

	vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_code);
	if (!vs) {
		printf("VS code:\n%s", vs_code);
		piglit_report_result(PIGLIT_FAIL);
	}
	fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_code);
	if (!fs) {
		printf("FS code:\n%s", fs_code);
		piglit_report_result(PIGLIT_FAIL);
	}
	prog = piglit_link_simple_program(vs, fs);
	if (!piglit_link_check_status(prog))
		piglit_report_result(PIGLIT_FAIL);

	piglit_UseProgram(prog);

	tex_location = piglit_GetUniformLocation(prog, "tex");
	piglit_Uniform1i(tex_location, 0);
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_EXT_texture_swizzle");
	piglit_require_extension("GL_ARB_texture_rg");

	setup_shaders();
	setup_texture();
}