glsl-array-bounds: set out-of-bounds array index inside shader
[piglit.git] / tests / shaders / glsl-fs-discard-02.c
blob3277a3ed6e41e9f659200f7429ad720ee06dcb9c
1 /*
2 * Copyright © 2009 Marek Olšák (maraeo@gmail.com)
3 * Copyright © 2010 Intel Corporation
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.
24 * Authors:
25 * Marek Olšák <mareao@gmail.com>
26 * Eric Anholt <eric@anholt.net>
30 /** @file glsl-fs-discard-02.c
32 * Tests that discarding fragments doesn't let early depth writes through.
35 #include "piglit-util-gl.h"
37 PIGLIT_GL_TEST_CONFIG_BEGIN
39 config.supports_gl_compat_version = 10;
41 config.window_visual = PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_DEPTH;
43 PIGLIT_GL_TEST_CONFIG_END
45 static char vs_code[] =
46 "varying vec4 color;"
47 "\n"
48 "void main()\n"
49 "{\n"
50 " gl_Position = gl_Vertex;\n"
51 " if (gl_Vertex.z > 0.75)\n"
52 " color = vec4(1.0, 0.0, 0.0, gl_Vertex.z);\n"
53 " else if (gl_Vertex.z > 0.25)\n"
54 " color = vec4(0.0, 1.0, 0.0, gl_Vertex.z);\n"
55 " else\n"
56 " color = vec4(0.0, 0.0, 1.0, gl_Vertex.z);\n"
57 "}\n";
59 static char fs_code[] =
60 "varying vec4 color;\n"
61 "\n"
62 "void main()\n"
63 "{\n"
64 " if (color.w < 0.25)\n"
65 " discard;"
66 " gl_FragColor = vec4(color.xyz, 0.0);\n"
67 "}\n";
69 static GLuint setup_shaders()
71 GLuint vs, fs, prog;
73 vs = piglit_compile_shader_text(GL_VERTEX_SHADER, vs_code);
74 fs = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fs_code);
75 prog = piglit_link_simple_program(vs, fs);
77 glUseProgram(prog);
78 return prog;
81 static GLboolean test()
83 GLint prog;
84 GLboolean pass = GL_TRUE;
85 float green[4] = {0, 1, 0, 0};
87 prog = setup_shaders();
89 glClear(GL_DEPTH_BUFFER_BIT);
91 glDepthFunc(GL_LEQUAL);
92 glEnable(GL_DEPTH_TEST);
94 piglit_draw_rect_z(1.0, -1, -1, 2, 2); // red
95 piglit_draw_rect_z(0.0, -1, -1, 2, 2); // discard
96 piglit_draw_rect_z(0.5, -1, -1, 2, 2); // green
98 if (!piglit_check_gl_error(GL_NO_ERROR))
99 piglit_report_result(PIGLIT_FAIL);
101 pass = pass && piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, green);
103 glDeleteProgram(prog);
105 return pass;
108 enum piglit_result piglit_display(void)
110 GLboolean pass;
112 pass = test();
114 piglit_present_results();
116 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
119 void piglit_init(int argc, char **argv)
121 piglit_require_vertex_shader();
122 piglit_require_fragment_shader();