2 * Copyright © 2014 Intel Corporation
3 * Copyright © 2015 Red Hat
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * A basic drawing test for GL_ARB_texture_stencil8 which ensures that
29 * sampling occurs from the right position in the texture.
31 * It creates two stencil textures. The first has a horizontal
32 * gradient (0 -> 255 for stencil), and the second a
35 * The expected output is two squares, separated by a blue border.
36 * The left half of the window is generated by stencil texturing, and drawn
50 #define __STDC_FORMAT_MACROS
52 #include "piglit-util-gl.h"
54 PIGLIT_GL_TEST_CONFIG_BEGIN
56 config
.supports_gl_core_version
= 32;
57 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
58 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
59 config
.window_width
= 256 + 3;
60 config
.window_height
= 256 * 2 + 3;
62 PIGLIT_GL_TEST_CONFIG_END
64 float stencil_horiz_expected
[256 * 256 * 3];
65 float stencil_vert_expected
[256 * 256 * 3];
70 /** A program that samples from stencil using usampler2D. */
75 /******************************************************************************/
82 glClearColor(0.0, 0.0, 1.0, 1.0);
83 glClear(GL_COLOR_BUFFER_BIT
);
85 glUseProgram(stencil_prog
);
87 /* Upper left corner: Stencil (black to red, left to right). */
88 glViewport(0, 259, 256, 256);
89 glBindTexture(GL_TEXTURE_2D
, horiz_tex
);
90 piglit_draw_rect(-1, -1, 2, 2);
91 if (!piglit_probe_image_rgb(0, 259, 256, 256, stencil_horiz_expected
)) {
92 printf(" FAIL: stencil (horizontal).\n");
97 /* Lower left corner: Stencil (black to red, upwards). */
98 glViewport(0, 0, 256, 256);
99 glBindTexture(GL_TEXTURE_2D
, vert_tex
);
100 piglit_draw_rect(-1, -1, 2, 2);
101 if (!piglit_probe_image_rgb(0, 0, 256, 256, stencil_vert_expected
)) {
102 printf(" FAIL: stencil (vertical).\n");
106 piglit_present_results();
108 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
111 /******************************************************************************/
114 * - Stencil ranges from 0 to 255.
116 * horiz_tex is left to right (increasing in the +x direction);
117 * vert_tex is bottom to top (increasing in the +y direction).
119 * Also set up expected values.
125 uint8_t horiz_data
[256 * 256];
126 uint8_t vert_data
[256 * 256];
128 for (i
= 0; i
< ARRAY_SIZE(horiz_data
); i
++) {
129 unsigned x
= i
% 256;
130 unsigned y
= i
/ 256;
135 glGenTextures(1, &horiz_tex
);
136 glBindTexture(GL_TEXTURE_2D
, horiz_tex
);
137 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
138 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
139 glTexImage2D(GL_TEXTURE_2D
, 0, GL_STENCIL_INDEX8
,
140 256, 256, 0, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
142 if (!piglit_check_gl_error(GL_NO_ERROR
))
143 piglit_report_result(PIGLIT_FAIL
);
145 glGenTextures(1, &vert_tex
);
146 glBindTexture(GL_TEXTURE_2D
, vert_tex
);
147 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
148 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
149 glTexImage2D(GL_TEXTURE_2D
, 0, GL_STENCIL_INDEX8
,
150 256, 256, 0, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
,
152 if (!piglit_check_gl_error(GL_NO_ERROR
))
153 piglit_report_result(PIGLIT_FAIL
);
155 /* Set up expected values. */
156 memset(stencil_horiz_expected
, 0, sizeof(stencil_horiz_expected
));
157 memset(stencil_vert_expected
, 0, sizeof(stencil_vert_expected
));
159 for (i
= 0; i
< 256 * 256; i
++) {
160 unsigned x
= i
% 256;
161 unsigned y
= i
/ 256;
162 stencil_horiz_expected
[3*i
] = x
/ 255.0f
;
163 stencil_vert_expected
[3*i
] = y
/ 255.0f
;
170 * Compile the shaders used by this program.
180 "in vec4 piglit_vertex;\n"
181 "out vec2 texcoords;\n"
184 " gl_Position = piglit_vertex;\n"
185 " texcoords = (piglit_vertex.xy + 1.0) / 2.0;\n"
188 const char *fs_stencil
=
190 "in vec2 texcoords;\n"
191 "uniform usampler2D tex;\n"
194 " uint stencil = texture(tex, texcoords).x;\n"
195 " gl_FragColor = vec4(float(stencil) / 255.0, 0, 0, 1);\n"
198 stencil_prog
= piglit_build_simple_program(vs
, fs_stencil
);
199 loc
= glGetUniformLocation(stencil_prog
, "tex");
200 glUseProgram(stencil_prog
);
206 piglit_init(int argc
, char **argv
)
208 piglit_require_extension("GL_ARB_texture_stencil8");
210 glGenVertexArrays(1, &vao
);
211 glBindVertexArray(vao
);