ext_gpu_shader4: add compiler tests for everything
[piglit.git] / tests / spec / arb_texture_stencil8 / fbo-stencil8.c
blob054297b7dad48675ae993a55c4a7442dc440d168
1 /*
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
14 * Software.
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
22 * IN THE SOFTWARE.
26 /** @file fbo-stencil8.c
28 * Tests glClear, glReadPixels, glBlitFramebuffer
29 * with stencil buffers generated from stencil textures.
32 #include "piglit-util-gl.h"
34 #define BUF_SIZE 123
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
45 enum {
46 CLEAR,
47 READPIXELS,
48 BLIT
50 int test = CLEAR;
52 struct format {
53 GLuint iformat;
54 const char *extension;
55 } formats[] = {
56 {GL_STENCIL_INDEX1, NULL},
57 {GL_STENCIL_INDEX4, NULL},
58 {GL_STENCIL_INDEX8, NULL},
59 {GL_STENCIL_INDEX16, NULL},
62 struct format f;
63 GLuint mask;
64 GLuint vao;
66 static const char vs_text[] =
67 "#version 130 \n"
68 "in vec4 piglit_vertex;\n"
69 "in vec4 piglit_texcoord;\n"
70 "out vec4 colfs;\n"
71 "void main()\n"
72 "{\n"
73 " gl_Position = piglit_vertex;\n"
74 " colfs = piglit_texcoord;\n"
75 "}\n";
77 static const char fs_text[] =
78 "#version 130 \n"
79 "in vec4 colfs;\n"
80 "void main()\n"
81 "{\n"
82 " gl_FragColor = colfs;\n"
83 "}\n";
85 static GLuint prog;
87 static enum piglit_result test_clear(void)
89 GLuint cb_tex;
90 GLenum status;
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);
133 return res;
136 static enum piglit_result compare_stencil(void)
138 int x, y, failures = 0;
139 GLushort stencil[BUF_SIZE*BUF_SIZE];
140 GLushort expected;
142 /* Read stencil. */
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
155 * differences.
157 if (x == BUF_SIZE / 2 || y == BUF_SIZE / 2)
158 continue;
160 if (y < BUF_SIZE/2)
161 expected = (x < BUF_SIZE/2 ? 0x3333 : 0x6666) & mask;
162 else
163 expected = (x < BUF_SIZE/2 ? 0x9999 : 0xbbbb) & mask;
165 if (stencil[y*BUF_SIZE+x] != expected) {
166 failures++;
167 if (failures < 20) {
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) {
171 printf("...\n");
176 if (failures)
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;
244 GLuint fb, tex;
245 GLint stencil_size =8;
246 GLenum status;
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;
272 switch (test) {
273 case CLEAR:
274 puts("Testing glClear(stencil8).");
275 res = test_clear();
276 break;
277 case READPIXELS:
278 puts("Testing glReadPixels(stencil8).");
279 res = test_readpixels();
280 break;
281 case BLIT:
282 puts("Testing glBlitFramebuffer(stencil8).");
283 res = test_copy();
284 break;
285 default:
286 assert(0);
287 res = PIGLIT_SKIP;
290 /* Cleanup. */
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);
299 return res;
302 void piglit_init(int argc, char **argv)
304 unsigned i, p;
305 GLuint fs, vs;
306 piglit_require_extension("GL_ARB_texture_stencil8");
308 for (p = 1; p < argc; p++) {
309 if (!strcmp(argv[p], "clear")) {
310 test = CLEAR;
311 continue;
313 if (!strcmp(argv[p], "readpixels")) {
314 test = READPIXELS;
315 continue;
317 if (!strcmp(argv[p], "blit")) {
318 test = BLIT;
319 continue;
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);
326 f = formats[i];
327 printf("Testing %s.\n",
328 piglit_get_gl_enum_name(f.iformat));
329 break;
334 if (!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);
343 glUseProgram(prog);
344 glGenVertexArrays(1, &vao);
345 glBindVertexArray(vao);