summaryrefslogtreecommitdiff
path: root/tests/spec/arb_stencil_texturing/draw.c
blob: 184c10571a7dec03ac151cb77ea88f73085a0f99 (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
/*
 * Copyright © 2014 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 draw.c
 *
 * A basic drawing test for GL_ARB_stencil_texturing which ensures that
 * sampling occurs from the right position in the texture.
 *
 * It creates two packed depth/stencil textures.  The first has a horizontal
 * gradient (0.0 -> 1.0 for depth, 0 -> 255 for stencil), and the second a
 * vertical gradient.
 *
 * The expected output is four squares, separated by a blue border.
 * The left half of the window is generated by stencil texturing, and drawn
 * in red.  The right half is generated by depth texturing, and drawn in green.
 *
 *   Stencil |   Depth
 *    (red)  |  (green)
 *           |
 *   0 --> 1 |  0 --> 1
 *  --------------------
 *      1    |     1
 *      ^    |     ^
 *      |    |     |
 *      0    |     0
 */

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 20;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
	config.window_width = 256 * 2 + 3;
	config.window_height = 256 * 2 + 3;

PIGLIT_GL_TEST_CONFIG_END

float stencil_horiz_expected[256 * 256 * 3];
float stencil_vert_expected[256 * 256 * 3];
float depth_horiz_expected[256 * 256 * 3];
float depth_vert_expected[256 * 256 * 3];

GLuint horiz_tex;
GLuint vert_tex;

/** A program that samples from stencil using usampler2D. */
GLint stencil_prog;

/** A program that samples from depth using sampler2D. */
GLint depth_prog;

/******************************************************************************/

/**
 * Bind a texture and change DEPTH_STENCIL_TEXTURE_MODE (convenience function).
 */
static void
select_tex(GLint handle, GLenum depth_stencil_texture_mode)
{
	glBindTexture(GL_TEXTURE_2D, handle);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE,
			depth_stencil_texture_mode);
}

enum piglit_result
piglit_display(void)
{
	bool pass = true;

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

	glUseProgram(stencil_prog);

	/* Upper left corner: Stencil (black to red, left to right). */
	glViewport(0, 259, 256, 256);
	select_tex(horiz_tex, GL_STENCIL_INDEX);
	piglit_draw_rect(-1, -1, 2, 2);
	if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected)) {
		printf("  FAIL: stencil (horizontal).\n");
		pass = false;
	}


	/* Lower left corner: Stencil (black to red, upwards). */
	glViewport(0, 0, 256, 256);
	select_tex(vert_tex, GL_STENCIL_INDEX);
	piglit_draw_rect(-1, -1, 2, 2);
	if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected)) {
		printf("  FAIL: stencil (vertical).\n");
		pass = false;
	}

	glUseProgram(depth_prog);

	/* Upper right corner: Depth (black to green, left to right). */
	glViewport(259, 259, 256, 256);
	select_tex(horiz_tex, GL_DEPTH_COMPONENT);
	piglit_draw_rect(-1, -1, 2, 2);
	if (!piglit_probe_image_rgb(259, 259, 256, 256, depth_horiz_expected)) {
		printf("  FAIL: depth (horizontal).\n");
		pass = false;
	}

	/* Lower right corner: Depth (black to green, upwards). */
	glViewport(259, 0, 256, 256);
	select_tex(vert_tex, GL_DEPTH_COMPONENT);
	piglit_draw_rect(-1, -1, 2, 2);
	if (!piglit_probe_image_rgb(259, 0, 256, 256, depth_vert_expected)) {
		printf("  FAIL: depth (vertical).\n");
		pass = false;
	}

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

/******************************************************************************/

/**
 * Pack a floating point depth value and 8-bit stencil value into
 * GL_UNSIGNED_INT24_S8 data.
 */
uint32_t
pack_z24_s8(float z, uint8_t s)
{
	uint32_t z24 = z * (double) 0xffffff;
	return (z24 << 8) | s;
}

/**
 * Generate two textures containing gradients:
 * - Depth ranges from 0.0 to 1.0.
 * - Stencil ranges from 0 to 255.
 *
 * horiz_tex is left to right (increasing in the +x direction);
 * vert_tex is bottom to top (increasing in the +y direction).
 *
 * Also set up expected values.
 */
static void
setup_textures(void)
{
	int i;
	uint32_t horiz_data[256 * 256];
	uint32_t vert_data[256 * 256];

	for (i = 0; i < ARRAY_SIZE(horiz_data); i++) {
		unsigned x = i % 256;
		unsigned y = i / 256;
		horiz_data[i] = pack_z24_s8(x / 255.0f, x);
		vert_data[i] = pack_z24_s8(y / 255.0f, y);
	}

	glGenTextures(1, &horiz_tex);
	glBindTexture(GL_TEXTURE_2D, horiz_tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
		     256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
		     horiz_data);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	glGenTextures(1, &vert_tex);
	glBindTexture(GL_TEXTURE_2D, vert_tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
		     256, 256, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
		     vert_data);
	if (!piglit_check_gl_error(GL_NO_ERROR))
		piglit_report_result(PIGLIT_FAIL);

	/* Set up expected values. */
	memset(stencil_horiz_expected, 0, sizeof(stencil_horiz_expected));
	memset(stencil_vert_expected, 0, sizeof(stencil_vert_expected));
	memset(depth_horiz_expected, 0, sizeof(depth_horiz_expected));
	memset(depth_vert_expected, 0, sizeof(depth_vert_expected));

	for (i = 0; i < 256 * 256; i++) {
		unsigned x = i % 256;
		unsigned y = i / 256;
		stencil_horiz_expected[3*i] = x / 255.0f;
		stencil_vert_expected[3*i]  = y / 255.0f;
		depth_horiz_expected[3*i+1] = x / 255.0f;
		depth_vert_expected[3*i+1]  = y / 255.0f;
	}


}

/**
 * Compile the shaders used by this program.
 *
 * Depth data should be read using a floating point sampler2D; stencil data
 * should be read using an unsigned integer usampler2D.  So, we need two
 * separate shaders.
 */
static void
setup_shaders(void)
{
	int loc;

	const char *vs =
	"#version 130\n"
	"out vec2 texcoords;\n"
	"void main()\n"
	"{\n"
	"    gl_Position = gl_Vertex;\n"
	"    texcoords = (gl_Vertex.xy + 1.0) / 2.0;\n"
	"}\n";

	const char *fs_stencil =
	"#version 130\n"
	"in vec2 texcoords;\n"
	"uniform usampler2D tex;\n"
	"void main()\n"
	"{\n"
	"    uint stencil = texture(tex, texcoords).x;\n"
	"    gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
	"}\n";

	const char *fs_depth =
	"#version 130\n"
	"in vec2 texcoords;\n"
	"uniform sampler2D tex;\n"
	"void main()\n"
	"{\n"
	"    float depth = texture(tex, texcoords).x;\n"
	"    gl_FragColor = vec4(0, depth, 0, 1);\n"
	"}\n";

	stencil_prog = piglit_build_simple_program(vs, fs_stencil);
	loc = glGetUniformLocation(stencil_prog, "tex");
	glUseProgram(stencil_prog);
	glUniform1i(loc, 0);

	depth_prog = piglit_build_simple_program(vs, fs_depth);
	loc = glGetUniformLocation(depth_prog, "tex");
	glUseProgram(depth_prog);
	glUniform1i(loc, 0);
}

void
piglit_init(int argc, char **argv)
{
	piglit_require_extension("GL_ARB_stencil_texturing");
	piglit_require_GLSL_version(130); /* for usampler2D */

	setup_textures();
	setup_shaders();
}