2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
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
26 /** @file fbo-stencil8.c
28 * Tests glClear, glReadPixels, glBlitFramebuffer
29 * with stencil buffers generated from stencil textures.
32 #include "piglit-util-gl.h"
36 PIGLIT_GL_TEST_CONFIG_BEGIN
38 config
.supports_gl_core_version
= 32;
40 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
;
41 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
43 PIGLIT_GL_TEST_CONFIG_END
54 const char *extension
;
56 {GL_STENCIL_INDEX1
, NULL
},
57 {GL_STENCIL_INDEX4
, NULL
},
58 {GL_STENCIL_INDEX8
, NULL
},
59 {GL_STENCIL_INDEX16
, NULL
},
66 static const char vs_text
[] =
68 "in vec4 piglit_vertex;\n"
69 "in vec4 piglit_texcoord;\n"
73 " gl_Position = piglit_vertex;\n"
74 " colfs = piglit_texcoord;\n"
77 static const char fs_text
[] =
82 " gl_FragColor = colfs;\n"
87 static enum piglit_result
test_clear(void)
91 float green
[3] = {0, 1, 0};
92 enum piglit_result res
;
94 glGenTextures(1, &cb_tex
);
95 glBindTexture(GL_TEXTURE_2D
, cb_tex
);
97 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA8
, BUF_SIZE
, BUF_SIZE
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
99 /* Add a colorbuffer. */
100 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, cb_tex
, 0);
102 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
103 glReadBuffer(GL_COLOR_ATTACHMENT0
);
104 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
105 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
106 printf("FBO incomplete status 0x%X\n", status
);
107 piglit_report_result(PIGLIT_FAIL
); /* RGBA8 must succeed. */
110 glClearStencil(0x3456);
111 glClear(GL_STENCIL_BUFFER_BIT
);
113 glEnable(GL_STENCIL_TEST
);
114 glStencilFunc(GL_EQUAL
, 0x3456 & mask
, ~0);
116 glVertexAttrib3fv(PIGLIT_ATTRIB_TEX
, green
);
117 piglit_draw_rect(-1, -1, 2, 2);
118 glVertexAttrib3f(PIGLIT_ATTRIB_TEX
, 1, 1, 1);
120 glDisable(GL_STENCIL_TEST
);
122 res
= piglit_probe_rect_rgb(0, 0, BUF_SIZE
, BUF_SIZE
, green
) ? PIGLIT_PASS
: PIGLIT_FAIL
;
124 /* Display the colorbuffer. */
125 if (!piglit_automatic
) {
126 glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT
, piglit_winsys_fbo
);
127 glBlitFramebufferEXT(0, 0, BUF_SIZE
, BUF_SIZE
, 0, 0, BUF_SIZE
, BUF_SIZE
,
128 GL_COLOR_BUFFER_BIT
, GL_NEAREST
);
131 glDeleteTextures(1, &cb_tex
);
136 static enum piglit_result
compare_stencil(void)
138 int x
, y
, failures
= 0;
139 GLushort stencil
[BUF_SIZE
*BUF_SIZE
];
143 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
144 glReadPixels(0, 0, BUF_SIZE
, BUF_SIZE
, GL_STENCIL_INDEX
,
145 GL_UNSIGNED_SHORT
, stencil
);
147 /* Compare results. */
148 for (y
= 0; y
< BUF_SIZE
; y
++) {
149 for (x
= 0; x
< BUF_SIZE
; x
++) {
151 /* Skip the middle row and column of pixels because
152 * drawing polygons for the left/right and bottom/top
153 * quadrants may hit the middle pixels differently
154 * depending on minor transformation and rasterization
157 if (x
== BUF_SIZE
/ 2 || y
== BUF_SIZE
/ 2)
161 expected
= (x
< BUF_SIZE
/2 ? 0x3333 : 0x6666) & mask
;
163 expected
= (x
< BUF_SIZE
/2 ? 0x9999 : 0xbbbb) & mask
;
165 if (stencil
[y
*BUF_SIZE
+x
] != expected
) {
168 printf("Stencil at %i,%i Expected: 0x%02x Observed: 0x%02x\n",
169 x
, y
, expected
, stencil
[y
*BUF_SIZE
+x
]);
170 } else if (failures
== 20) {
177 printf("Total failures: %i\n", failures
);
179 return failures
== 0 ? PIGLIT_PASS
: PIGLIT_FAIL
;
182 static enum piglit_result
test_readpixels(void)
184 /* Clear stencil to 0xfe. */
185 glClearStencil(0xfefe);
186 glClear(GL_STENCIL_BUFFER_BIT
);
188 /* Initialize stencil. */
189 glEnable(GL_STENCIL_TEST
);
190 glStencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
192 glStencilFunc(GL_ALWAYS
, 0x3333 & mask
, ~0);
193 piglit_draw_rect(-1, -1, 1, 1);
195 glStencilFunc(GL_ALWAYS
, 0x6666 & mask
, ~0);
196 piglit_draw_rect(0, -1, 1, 1);
198 glStencilFunc(GL_ALWAYS
, 0x9999 & mask
, ~0);
199 piglit_draw_rect(-1, 0, 1, 1);
201 glStencilFunc(GL_ALWAYS
, 0xbbbb & mask
, ~0);
202 piglit_draw_rect(0, 0, 1, 1);
204 glDisable(GL_STENCIL_TEST
);
206 return compare_stencil();
209 static enum piglit_result
test_copy(void)
211 /* Clear stencil to 0xfe. */
212 glClearStencil(0xfefe);
213 glClear(GL_STENCIL_BUFFER_BIT
);
215 /* Initialize stencil. */
216 glEnable(GL_STENCIL_TEST
);
217 glStencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
219 /* Set the upper-right corner to 0x3333 and copy the content to the lower-left one. */
220 glStencilFunc(GL_ALWAYS
, 0x3333 & mask
, ~0);
221 piglit_draw_rect(0, 0, 1, 1);
222 glBlitFramebufferEXT(BUF_SIZE
/2+1, BUF_SIZE
/2+1, BUF_SIZE
, BUF_SIZE
,
223 0, 0, BUF_SIZE
/2, BUF_SIZE
/2,
224 GL_STENCIL_BUFFER_BIT
, GL_NEAREST
);
226 /* Initialize the other corners. */
227 glStencilFunc(GL_ALWAYS
, 0x6666 & mask
, ~0);
228 piglit_draw_rect(0, -1, 1, 1);
230 glStencilFunc(GL_ALWAYS
, 0x9999 & mask
, ~0);
231 piglit_draw_rect(-1, 0, 1, 1);
233 glStencilFunc(GL_ALWAYS
, 0xbbbb & mask
, ~0);
234 piglit_draw_rect(0, 0, 1, 1);
236 glDisable(GL_STENCIL_TEST
);
238 return compare_stencil();
241 enum piglit_result
piglit_display(void)
243 enum piglit_result res
;
245 GLint stencil_size
=8;
248 glClearColor(0, 0, 0, 0);
249 glClear(GL_COLOR_BUFFER_BIT
);
251 /* Create the FBO. */
252 glGenTextures(1, &tex
);
254 glBindTexture(GL_TEXTURE_2D
, tex
);
255 glTexImage2D(GL_TEXTURE_2D
, 0, GL_STENCIL_INDEX8
, BUF_SIZE
, BUF_SIZE
,
256 0, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, NULL
);
257 glGenFramebuffersEXT(1, &fb
);
258 glBindFramebuffer(GL_FRAMEBUFFER
, fb
);
259 glFramebufferTexture(GL_FRAMEBUFFER_EXT
, GL_STENCIL_ATTACHMENT
, tex
, 0);
260 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, tex
, 0);
261 glViewport(0, 0, BUF_SIZE
, BUF_SIZE
);
262 glDrawBuffer(GL_NONE
);
263 glReadBuffer(GL_NONE
);
264 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
265 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
266 printf("FBO incomplete status 0x%X\n", status
);
267 piglit_report_result(PIGLIT_SKIP
);
270 mask
= (1 << stencil_size
) - 1;
274 puts("Testing glClear(stencil8).");
278 puts("Testing glReadPixels(stencil8).");
279 res
= test_readpixels();
282 puts("Testing glBlitFramebuffer(stencil8).");
291 glBindFramebuffer(GL_FRAMEBUFFER
, piglit_winsys_fbo
);
292 glDeleteFramebuffersEXT(1, &fb
);
293 glDeleteTextures(1, &tex
);
295 piglit_present_results();
297 if (!piglit_check_gl_error(GL_NO_ERROR
))
298 piglit_report_result(PIGLIT_FAIL
);
302 void piglit_init(int argc
, char **argv
)
306 piglit_require_extension("GL_ARB_texture_stencil8");
308 for (p
= 1; p
< argc
; p
++) {
309 if (!strcmp(argv
[p
], "clear")) {
313 if (!strcmp(argv
[p
], "readpixels")) {
317 if (!strcmp(argv
[p
], "blit")) {
321 const GLenum arg
= piglit_get_gl_enum_from_name(argv
[p
]);
322 for (i
= 0; i
< sizeof(formats
)/sizeof(*formats
); i
++) {
323 if (arg
== formats
[i
].iformat
) {
324 if (formats
[i
].extension
)
325 piglit_require_extension(formats
[i
].extension
);
327 printf("Testing %s.\n",
328 piglit_get_gl_enum_name(f
.iformat
));
335 printf("Not enough parameters.\n");
336 piglit_report_result(PIGLIT_SKIP
);
339 fs
= piglit_compile_shader_text(GL_FRAGMENT_SHADER
, fs_text
);
340 vs
= piglit_compile_shader_text(GL_VERTEX_SHADER
, vs_text
);
341 prog
= piglit_link_simple_program(vs
, fs
);
344 glGenVertexArrays(1, &vao
);
345 glBindVertexArray(vao
);