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
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
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
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
54 static enum piglit_result
55 test_negative_clamp(void * unused
)
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");
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");
81 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
84 static enum piglit_result
85 test_positive_clamp(void * unused
)
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
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");
107 /* And now set the clamp so that all parts of the polygon pass
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");
118 return pass
? PIGLIT_PASS
: PIGLIT_FAIL
;
121 static const struct piglit_subtest tests
[] = {
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;
143 config
.supports_gl_es_version
= 20;
145 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DEPTH
| PIGLIT_GL_VISUAL_DOUBLE
;
147 PIGLIT_GL_TEST_CONFIG_END
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
165 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
167 enum piglit_result result
= PIGLIT_PASS
;
168 result
= piglit_run_selected_subtests(
170 piglit_config
->selected_subtests
,
171 piglit_config
->num_selected_subtests
,
173 piglit_present_results();
179 piglit_init(int argc
, char **argv
)
181 static const float verts
[4][4] = {
191 piglit_require_extension("GL_EXT_polygon_offset_clamp");
193 prog
= piglit_build_simple_program(
194 #ifdef PIGLIT_USE_OPENGL
198 "attribute vec4 vertex;\n"
199 "#define gl_Vertex vertex\n"
201 "uniform float zflip;\n"
202 "void main() { gl_Position = gl_Vertex * vec4(1, 1, zflip, 1); }\n",
204 #ifdef PIGLIT_USE_OPENGL
208 "precision highp float;\n"
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);