2 * Copyright (c) 2011 VMware, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NON-INFRINGEMENT. IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Tests glPolygonMode wrt facing.
31 #include "piglit-util-gl.h"
33 PIGLIT_GL_TEST_CONFIG_BEGIN
35 config
.supports_gl_compat_version
= 20;
37 config
.window_width
= 400;
38 config
.window_height
= 100;
39 config
.window_visual
= PIGLIT_GL_VISUAL_RGB
| PIGLIT_GL_VISUAL_DOUBLE
;
40 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
42 PIGLIT_GL_TEST_CONFIG_END
44 static const char *TestName
= "polygon-mode-facing";
46 static const char *vstext
=
51 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
54 static const char *fstext
=
59 " vec4 color = gl_FrontFacing ? vec4(1.0, 0.0, 0.0, 1.0)\n"
60 " : vec4(0.0, 1.0, 0.0, 1.0);\n"
61 " gl_FragColor = color;\n"
65 static const GLfloat Colors
[2][4] = {
72 static const GLfloat Positions
[4][4][2] = {
95 get_prim_mode(GLenum mode
)
111 * Probe a 3x3 pixel region to see if any of the pixels matches the
115 probe_region(float px
, float py
, const GLfloat expectedColor
[4])
117 GLfloat img
[3][3][4];
120 glReadPixels(px
-1, py
-1, 3, 3, GL_RGBA
, GL_FLOAT
, img
);
122 /* see if any of the pixels matches the expected color */
123 for (i
= 0; i
< 3; i
++) {
124 for (j
= 0; j
< 3; j
++) {
125 if (img
[i
][j
][0] == expectedColor
[0] &&
126 img
[i
][j
][1] == expectedColor
[1] &&
127 img
[i
][j
][2] == expectedColor
[2]) {
138 * Examine the pixels drawn by a rect using the four vertex positions
139 * and determine if it was drawn filled, outlined, or as four points.
140 * \return GL_FILL, GL_LINE, GL_POINT or GL_NONE
143 identify_primitive(const GLfloat positions
[4][2],
144 const GLfloat expectedColor
[4])
147 float cx
= (positions
[0][0] + positions
[2][0]) / 2.0;
148 float cy
= (positions
[0][1] + positions
[2][1]) / 2.0;
150 float lx
= MIN2(positions
[0][0], positions
[2][0]);
153 float rx
= MAX2(positions
[0][0], positions
[2][0]);
157 float by
= positions
[0][1];
160 float ty
= positions
[1][1];
163 if (probe_region(cx
, cy
, expectedColor
))
166 /* probe left edge */
167 if (probe_region(lx
, ly
, expectedColor
)) {
168 /* and bottom edge */
169 if (probe_region(bx
, by
, expectedColor
)) {
171 if (probe_region(rx
, ry
, expectedColor
)) {
173 if (probe_region(tx
, ty
, expectedColor
)) {
180 /* probe lower-left corner */
181 if (probe_region(lx
, by
, expectedColor
)) {
182 /* probe lower-right corner */
183 if (probe_region(rx
, by
, expectedColor
)) {
184 /* probe top-left corner */
185 if (probe_region(lx
, ty
, expectedColor
)) {
186 /* probe top-right corner */
187 if (probe_region(rx
, ty
, expectedColor
)) {
200 test_combo(GLenum frontMode
, GLenum backMode
)
202 GLenum frontPrim
= get_prim_mode(frontMode
);
203 GLenum backPrim
= get_prim_mode(backMode
);
204 GLboolean pass
= GL_TRUE
;
205 GLenum expectedPrims
[4];
207 int expectFrontColorRef
[4] = {1,1,1,1};
209 glClear(GL_COLOR_BUFFER_BIT
);
212 * Drawing 4 quads. The first and 3rd should always be red (front facing).
213 * The 2nd and 4th are green with FILL backMode (lines/points are always
216 if (backMode
== GL_FILL
) {
217 expectFrontColorRef
[1] = expectFrontColorRef
[3] = 0;
220 /* Draw reference image */
221 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
223 glDrawArrays(frontPrim
, 0, 4);
224 glDrawArrays(backPrim
, 4, 4);
226 glDrawArrays(frontPrim
, 8, 4);
227 glDrawArrays(backPrim
, 12, 4);
229 /* determine what kind of primitives were drawn */
230 for (i
= 0; i
< 4; i
++) {
232 expectedPrims
[i
] = identify_primitive(Positions
[i
], Colors
[expectFrontColorRef
[i
]]);
234 if (expectedPrims
[i
] != backMode
) {
239 if (expectedPrims
[i
] != frontMode
) {
244 /* we didn't get the expected reference primitive */
246 "%s: reference drawing failed for frontPrim=%s, backPrim=%s\n",
247 TestName
, piglit_get_gl_enum_name(frontMode
),
248 piglit_get_gl_enum_name(backMode
));
253 /* Draw test image */
254 glClear(GL_COLOR_BUFFER_BIT
);
255 glPolygonMode(GL_FRONT
, frontMode
);
256 glPolygonMode(GL_BACK
, backMode
);
258 glDrawArrays(GL_QUADS
, 0, 8);
260 glDrawArrays(GL_QUADS
, 8, 8);
262 /* check that these prims match expectations */
264 * The first and 3rd should always be red (front-facing), the 2nd and 4th
265 * green (back-facing).
267 for (i
= 0; i
< 4; i
++) {
268 GLenum prim
= identify_primitive(Positions
[i
], Colors
[!(i
% 2)]);
269 if (prim
!= expectedPrims
[i
]) {
270 fprintf(stderr
, "%s: glPolygonMode(front=%s, back=%s) failed\n",
271 TestName
, piglit_get_gl_enum_name(frontMode
),
272 piglit_get_gl_enum_name(backMode
));
277 piglit_present_results();
284 test_polygonmode(void)
286 GLenum pass
= GL_TRUE
;
288 glVertexPointer(2, GL_FLOAT
, 0, Positions
);
289 glEnableClientState(GL_VERTEX_ARRAY
);
292 * First test with same front/back mode.
293 * Those are probably more important to get right...
295 if (!test_combo(GL_FILL
, GL_FILL
))
298 if (!test_combo(GL_POINT
, GL_POINT
))
302 * Be extra mean to mesa draw stage interactions turning lines back
305 glEnable(GL_LINE_SMOOTH
);
307 if (!test_combo(GL_LINE
, GL_LINE
))
310 glDisable(GL_LINE_SMOOTH
);
312 if (!test_combo(GL_FILL
, GL_POINT
))
315 if (!test_combo(GL_POINT
, GL_LINE
))
318 if (!test_combo(GL_POINT
, GL_FILL
))
321 if (!test_combo(GL_LINE
, GL_FILL
))
324 if (!test_combo(GL_LINE
, GL_POINT
))
329 * Be really mean to mesa draw stage interactions turning lines back
332 glEnable(GL_LINE_SMOOTH
);
334 if (!test_combo(GL_FILL
, GL_LINE
))
337 if (!test_combo(GL_POINT
, GL_LINE
))
340 if (!test_combo(GL_LINE
, GL_FILL
))
343 if (!test_combo(GL_LINE
, GL_POINT
))
353 if (!test_polygonmode())
361 piglit_init(int argc
, char **argv
)
364 piglit_require_GLSL_version(130);
365 piglit_ortho_projection(piglit_width
, piglit_height
, false);
366 prog
= piglit_build_simple_program(vstext
, fstext
);