glsl-1.10: test mesa bug conflict between globals
[piglit.git] / tests / spec / ext_polygon_offset_clamp / draw.c
blob632a7ebad031b6f569c4349bd893f485f7086c5b
1 /*
2 * Copyright (C) 2015 Ilia Mirkin
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
13 * Software.
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
21 * IN THE SOFTWARE.
24 /**
25 * \file draw.c
27 * Test that GL_EXT_polygon_offset_clamp actually applies the
28 * clamp. The polygon is set up between z = 1 and z = 0.1 (so under
29 * depth = 0.5).
31 * 1. Clear the depth buffer to 0.5 (leaving the depth func as LESS)
32 * 2. Draw the polygon with red, clamping the offset to -0.05. This
33 * ensures that even the z=0.1 end (i.e. depth = 0.55) does not go
34 * below the value in the depth buffer.
35 * 3. Draw the polygon again with green, clamping the offset at -0.51,
36 * ensuring that every point of the polygon can end up being offset to
37 * a depth value below 0.5.
40 #include "piglit-util-gl.h"
42 GLint prog, color, zflip;
44 static const struct piglit_gl_test_config * piglit_config;
45 static const float blue[4] = {0, 0, 1, 1};
46 static const float red[4] = {1, 0, 0, 1};
47 static const float green[4] = {0, 1, 0, 1};
49 /* NOTE: It appears that at least nvidia hw will end up wrapping around if the
50 * final z value goes below 0 (or something). This can come up when testing
51 * without the clamp.
54 static enum piglit_result
55 test_negative_clamp(void * unused)
57 bool pass = true;
59 /* Draw red rectangle that slopes between 1 and 0.1. Use a
60 * polygon offset with a high factor but small clamp
62 glPolygonOffsetClampEXT(-1000, 0, -0.05);
63 glUniform4fv(color, 1, red);
64 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
65 if (!piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, blue)) {
66 printf(" FAIL: red rect peeks over blue rect\n");
67 pass = false;
70 /* And now set the clamp such that all parts of the polygon
71 * can pass the depth test.
73 glPolygonOffsetClampEXT(-1000, 0, -0.51);
74 glUniform4fv(color, 1, green);
75 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
76 if (!piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, green)) {
77 printf(" FAIL: green rect does not cover blue rect\n");
78 pass = false;
81 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
84 static enum piglit_result
85 test_positive_clamp(void * unused)
87 bool pass = true;
89 /* Now try this again with the inverse approach and a positive
90 * clamp value. The polygon will now slope between -1 and
91 * -0.1. Everything is reversed, so just negate all the
92 * previous values.
95 glUniform1f(zflip, -1.0);
96 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
97 glDepthFunc(GL_GREATER);
99 glPolygonOffsetClampEXT(1000, 0, 0.05);
100 glUniform4fv(color, 1, red);
101 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
102 if (!piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, blue)) {
103 printf(" FAIL: red rect peeks over blue rect\n");
104 pass = false;
107 /* And now set the clamp so that all parts of the polygon pass
108 * the depth test.
110 glPolygonOffsetClampEXT(1000, 0, 0.51);
111 glUniform4fv(color, 1, green);
112 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
113 if (!piglit_probe_rect_rgba(0, 0, piglit_width, piglit_height, green)) {
114 printf(" FAIL: green rect does not cover blue rect\n");
115 pass = false;
118 return pass ? PIGLIT_PASS : PIGLIT_FAIL;
121 static const struct piglit_subtest tests[] = {
123 "negative clamp",
124 "neg_clamp",
125 test_negative_clamp,
126 NULL
129 "positive clamp",
130 "pos_clamp",
131 test_positive_clamp,
132 NULL
134 { NULL }
137 PIGLIT_GL_TEST_CONFIG_BEGIN
138 piglit_config = &config;
139 config.subtests = tests;
140 #if PIGLIT_USE_OPENGL
141 config.supports_gl_compat_version = 21;
142 #else
143 config.supports_gl_es_version = 20;
144 #endif
145 config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DEPTH | PIGLIT_GL_VISUAL_DOUBLE;
147 PIGLIT_GL_TEST_CONFIG_END
149 enum piglit_result
150 piglit_display(void)
152 glUseProgram(prog);
154 glViewport(0, 0, piglit_width, piglit_height);
155 glEnable(GL_DEPTH_TEST);
156 glEnable(GL_POLYGON_OFFSET_FILL);
158 glUniform1f(zflip, 1.0);
159 glClearColor(0, 0, 1, 1);
160 #ifdef PIGLIT_USE_OPENGL
161 glClearDepth(0.5);
162 #else
163 glClearDepthf(0.5);
164 #endif
165 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167 enum piglit_result result = PIGLIT_PASS;
168 result = piglit_run_selected_subtests(
169 tests,
170 piglit_config->selected_subtests,
171 piglit_config->num_selected_subtests,
172 result);
173 piglit_present_results();
175 return result;
178 void
179 piglit_init(int argc, char **argv)
181 static const float verts[4][4] = {
182 /* x y z w */
183 { -1, -1, 1.0, 1 },
184 { 1, -1, 1.0, 1 },
185 { -1, 1, 0.1, 1 },
186 { 1, 1, 0.1, 1 }
189 GLuint bo;
191 piglit_require_extension("GL_EXT_polygon_offset_clamp");
193 prog = piglit_build_simple_program(
194 #ifdef PIGLIT_USE_OPENGL
195 "#version 120\n"
196 #else
197 "#version 100\n"
198 "attribute vec4 vertex;\n"
199 "#define gl_Vertex vertex\n"
200 #endif
201 "uniform float zflip;\n"
202 "void main() { gl_Position = gl_Vertex * vec4(1, 1, zflip, 1); }\n",
204 #ifdef PIGLIT_USE_OPENGL
205 "#version 120\n"
206 #else
207 "#version 100\n"
208 "precision highp float;\n"
209 #endif
210 "uniform vec4 color;\n"
211 "void main() { gl_FragColor = color; }\n");
212 color = glGetUniformLocation(prog, "color");
213 zflip = glGetUniformLocation(prog, "zflip");
215 glEnableVertexAttribArray(0);
216 glGenBuffers(1, &bo);
217 glBindBuffer(GL_ARRAY_BUFFER, bo);
218 glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
219 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid const *)0);