2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * A basic drawing test for GL_ARB_stencil_texturing which ensures that
28 * sampling occurs from the right position in the texture.
30 * It creates two packed depth/stencil textures. The first has a horizontal
31 * gradient (0.0 -> 1.0 for depth, 0 -> 255 for stencil), and the second a
34 * The expected output is four squares, separated by a blue border.
35 * The left half of the window is generated by stencil texturing, and drawn
36 * in red. The right half is generated by depth texturing, and drawn in green.
42 * --------------------
49 #define __STDC_FORMAT_MACROS
51 #include "piglit-util-gl.h"
53 PIGLIT_GL_TEST_CONFIG_BEGIN
55 config
.supports_gl_compat_version
= 30;
56 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
57 config
.window_width
= 256 * 2 + 3;
58 config
.window_height
= 256 * 2 + 3;
60 PIGLIT_GL_TEST_CONFIG_END
62 float stencil_horiz_expected
[256 * 256 * 3];
63 float stencil_vert_expected
[256 * 256 * 3];
64 float depth_horiz_expected
[256 * 256 * 3];
65 float depth_vert_expected
[256 * 256 * 3];
70 /** A program that samples from stencil using usampler2D. */
73 /** A program that samples from depth using sampler2D. */
76 /******************************************************************************/
79 * Bind a texture and change DEPTH_STENCIL_TEXTURE_MODE (convenience function).
82 select_tex(GLint handle
, GLenum depth_stencil_texture_mode
)
84 glBindTexture(GL_TEXTURE_2D
, handle
);
85 glTexParameteri(GL_TEXTURE_2D
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
86 depth_stencil_texture_mode
);
94 glClearColor(0.0, 0.0, 1.0, 1.0);
95 glClear(GL_COLOR_BUFFER_BIT
);
97 glUseProgram(stencil_prog
);
99 /* Upper left corner: Stencil (black to red, left to right). */
100 glViewport(0, 259, 256, 256);
101 select_tex(horiz_tex
, GL_STENCIL_INDEX
);
102 piglit_draw_rect(-1, -1, 2, 2);
103 if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected
)) {
104 printf(" FAIL: stencil (horizontal).\n");
109 /* Lower left corner: Stencil (black to red, upwards). */
110 glViewport(0, 0, 256, 256);
111 select_tex(vert_tex
, GL_STENCIL_INDEX
);
112 piglit_draw_rect(-1, -1, 2, 2);
113 if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected
)) {
114 printf(" FAIL: stencil (vertical).\n");
118 glUseProgram(depth_prog
);
120 /* Upper right corner: Depth (black to green, left to right). */
121 glViewport(259, 259, 256, 256);
122 select_tex(horiz_tex
, GL_DEPTH_COMPONENT
);
123 piglit_draw_rect(-1, -1, 2, 2);
124 if (!piglit_probe_image_rgb(259, 259, 256, 256, depth_horiz_expected
)) {
125 printf(" FAIL: depth (horizontal).\n");
129 /* Lower right corner: Depth (black to green, upwards). */
130 glViewport(259, 0, 256, 256);
131 select_tex(vert_tex
, GL_DEPTH_COMPONENT
);
132 piglit_draw_rect(-1, -1, 2, 2);
133 if (!piglit_probe_image_rgb(259, 0, 256, 256, depth_vert_expected
)) {
134 printf(" FAIL: depth (vertical).\n");
138 piglit_present_results();
140 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
143 /******************************************************************************/
146 * Pack a floating point depth value and 8-bit stencil value into
147 * GL_UNSIGNED_INT24_S8 data.
150 pack_z24_s8(float z
, uint8_t s
)
152 uint32_t z24
= z
* (double) 0xffffff;
153 return (z24
<< 8) | s
;
157 * Generate two textures containing gradients:
158 * - Depth ranges from 0.0 to 1.0.
159 * - Stencil ranges from 0 to 255.
161 * horiz_tex is left to right (increasing in the +x direction);
162 * vert_tex is bottom to top (increasing in the +y direction).
164 * Also set up expected values.
170 uint32_t horiz_data
[256 * 256];
171 uint32_t vert_data
[256 * 256];
173 for (i
= 0; i
< ARRAY_SIZE(horiz_data
); i
++) {
174 unsigned x
= i
% 256;
175 unsigned y
= i
/ 256;
176 horiz_data
[i
] = pack_z24_s8(x
/ 255.0f
, x
);
177 vert_data
[i
] = pack_z24_s8(y
/ 255.0f
, y
);
180 glGenTextures(1, &horiz_tex
);
181 glBindTexture(GL_TEXTURE_2D
, horiz_tex
);
182 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
183 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
184 glTexImage2D(GL_TEXTURE_2D
, 0, GL_DEPTH24_STENCIL8
,
185 256, 256, 0, GL_DEPTH_STENCIL
, GL_UNSIGNED_INT_24_8
,
187 if (!piglit_check_gl_error(GL_NO_ERROR
))
188 piglit_report_result(PIGLIT_FAIL
);
190 glGenTextures(1, &vert_tex
);
191 glBindTexture(GL_TEXTURE_2D
, vert_tex
);
192 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
193 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
194 glTexImage2D(GL_TEXTURE_2D
, 0, GL_DEPTH24_STENCIL8
,
195 256, 256, 0, GL_DEPTH_STENCIL
, GL_UNSIGNED_INT_24_8
,
197 if (!piglit_check_gl_error(GL_NO_ERROR
))
198 piglit_report_result(PIGLIT_FAIL
);
200 /* Set up expected values. */
201 memset(stencil_horiz_expected
, 0, sizeof(stencil_horiz_expected
));
202 memset(stencil_vert_expected
, 0, sizeof(stencil_vert_expected
));
203 memset(depth_horiz_expected
, 0, sizeof(depth_horiz_expected
));
204 memset(depth_vert_expected
, 0, sizeof(depth_vert_expected
));
206 for (i
= 0; i
< 256 * 256; i
++) {
207 unsigned x
= i
% 256;
208 unsigned y
= i
/ 256;
209 stencil_horiz_expected
[3*i
] = x
/ 255.0f
;
210 stencil_vert_expected
[3*i
] = y
/ 255.0f
;
211 depth_horiz_expected
[3*i
+1] = x
/ 255.0f
;
212 depth_vert_expected
[3*i
+1] = y
/ 255.0f
;
219 * Compile the shaders used by this program.
221 * Depth data should be read using a floating point sampler2D; stencil data
222 * should be read using an unsigned integer usampler2D. So, we need two
232 "out vec2 texcoords;\n"
235 " gl_Position = gl_Vertex;\n"
236 " texcoords = (gl_Vertex.xy + 1.0) / 2.0;\n"
239 const char *fs_stencil
=
241 "in vec2 texcoords;\n"
242 "uniform usampler2D tex;\n"
245 " uint stencil = texture(tex, texcoords).x;\n"
246 " gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
249 const char *fs_depth
=
251 "in vec2 texcoords;\n"
252 "uniform sampler2D tex;\n"
255 " float depth = texture(tex, texcoords).x;\n"
256 " gl_FragColor = vec4(0, depth, 0, 1);\n"
259 stencil_prog
= piglit_build_simple_program(vs
, fs_stencil
);
260 loc
= glGetUniformLocation(stencil_prog
, "tex");
261 glUseProgram(stencil_prog
);
264 depth_prog
= piglit_build_simple_program(vs
, fs_depth
);
265 loc
= glGetUniformLocation(depth_prog
, "tex");
266 glUseProgram(depth_prog
);
271 piglit_init(int argc
, char **argv
)
273 piglit_require_extension("GL_ARB_stencil_texturing");