summaryrefslogtreecommitdiff
path: root/tests/general/polygon-mode-facing.c
blob: dcef353ad9d9ffba654311b8a85f6e99c6e8e92b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2011 VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL VMWARE AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * Tests glPolygonMode wrt facing.
 * Roland Scheidegger
 * December 2015
 */

#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 20;

	config.window_width = 400;
	config.window_height = 100;
	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
	config.khr_no_error_support = PIGLIT_NO_ERRORS;

PIGLIT_GL_TEST_CONFIG_END

static const char *TestName = "polygon-mode-facing";

static const char *vstext =
        "#version 130\n"
        "\n"
        "void main()\n"
        "{\n"
        "  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
        "}\n";

static const char *fstext =
        "#version 130\n"
        "\n"
        "void main()\n"
        "{\n"
        "  vec4 color = gl_FrontFacing ? vec4(1.0, 0.0, 0.0, 1.0)\n"
        "                              : vec4(0.0, 1.0, 0.0, 1.0);\n"
        "  gl_FragColor = color;\n"
        "}\n";


static const GLfloat Colors[2][4] = {
   /* back color */
   {0, 1, 0, 1},
   /* front color */
   {1, 0, 0, 1},
};

static const GLfloat Positions[4][4][2] = {
   {{10, 10},
    {90, 10},
    {90, 90},
    {10, 90}},

   {{190, 10},
    {110, 10},
    {110, 90},
    {190, 90}},

   {{290, 10},
    {210, 10},
    {210, 90},
    {290, 90}},
  
   {{310, 10},
    {390, 10},
    {390, 90},
    {310, 90}},
};

static GLenum
get_prim_mode(GLenum mode)
{
   switch (mode) {
   case GL_POINT:
      return GL_POINTS;
   case GL_LINE:
      return GL_LINE_LOOP;
   case GL_FILL:
      return GL_QUADS;
   default:
      return 0;
   }
}


/**
 * Probe a 3x3 pixel region to see if any of the pixels matches the
 * expected color.
 */
static GLboolean
probe_region(float px, float py, const GLfloat expectedColor[4])
{
   GLfloat img[3][3][4];
   int i, j;

   glReadPixels(px-1, py-1, 3, 3, GL_RGBA, GL_FLOAT, img);

   /* see if any of the pixels matches the expected color */
   for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
         if (img[i][j][0] == expectedColor[0] &&
             img[i][j][1] == expectedColor[1] &&
             img[i][j][2] == expectedColor[2]) {
            return GL_TRUE;
         }
      }
   }

   return GL_FALSE;
}


/**
 * Examine the pixels drawn by a rect using the four vertex positions
 * and determine if it was drawn filled, outlined, or as four points.
 * \return GL_FILL, GL_LINE, GL_POINT or GL_NONE
 */
static GLenum
identify_primitive(const GLfloat positions[4][2],
                   const GLfloat expectedColor[4])
{
   /* center */
   float cx = (positions[0][0] + positions[2][0]) / 2.0;
   float cy = (positions[0][1] + positions[2][1]) / 2.0;
   /* left edge */
   float lx = MIN2(positions[0][0], positions[2][0]);
   float ly = cy;
   /* right edge */
   float rx = MAX2(positions[0][0], positions[2][0]);
   float ry = cy;
   /* bottom edge */
   float bx = cx;
   float by = positions[0][1];
   /* top edge */
   float tx = cx;
   float ty = positions[1][1];

   /* probe center */
   if (probe_region(cx, cy, expectedColor))
      return GL_FILL;

   /* probe left edge */
   if (probe_region(lx, ly, expectedColor)) {
      /* and bottom edge */
      if (probe_region(bx, by, expectedColor)) {
         /* and right edge */
         if (probe_region(rx, ry, expectedColor)) {
            /* and top edge */
            if (probe_region(tx, ty, expectedColor)) {
               return GL_LINE;
            }
         }
      }
   }

   /* probe lower-left corner */
   if (probe_region(lx, by, expectedColor)) {
      /* probe lower-right corner */
      if (probe_region(rx, by, expectedColor)) {
         /* probe top-left corner */
         if (probe_region(lx, ty, expectedColor)) {
            /* probe top-right corner */
            if (probe_region(rx, ty, expectedColor)) {
               return GL_POINT;
            }
         }
      }
   }

   return GL_NONE;
}



static GLboolean
test_combo(GLenum frontMode, GLenum backMode)
{
   GLenum frontPrim = get_prim_mode(frontMode);
   GLenum backPrim = get_prim_mode(backMode);
   GLboolean pass = GL_TRUE;
   GLenum expectedPrims[4];
   int i;
   int expectFrontColorRef[4] = {1,1,1,1};
   
   glClear(GL_COLOR_BUFFER_BIT);

   /*
    * Drawing 4 quads. The first and 3rd should always be red (front facing).
    * The 2nd and 4th are green with FILL backMode (lines/points are always
    * front facing).
    */
   if (backMode == GL_FILL) {
      expectFrontColorRef[1] = expectFrontColorRef[3] = 0;
   }

   /* Draw reference image */
   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
   glFrontFace(GL_CCW);
   glDrawArrays(frontPrim, 0, 4);
   glDrawArrays(backPrim, 4, 4);
   glFrontFace(GL_CW);
   glDrawArrays(frontPrim, 8, 4);
   glDrawArrays(backPrim, 12, 4);

   /* determine what kind of primitives were drawn */
   for (i = 0; i < 4; i++) {
      bool err = false;
      expectedPrims[i] = identify_primitive(Positions[i], Colors[expectFrontColorRef[i]]);
      if (i & 1) {
         if (expectedPrims[i] != backMode) {
            err = true;
         }
      }
      else {
         if (expectedPrims[i] != frontMode) {
            err = true;
         }
      }
      if (err) {
         /* we didn't get the expected reference primitive */
         fprintf(stderr,
                 "%s: reference drawing failed for frontPrim=%s, backPrim=%s\n",
                 TestName, piglit_get_gl_enum_name(frontMode),
                           piglit_get_gl_enum_name(backMode));
         return GL_FALSE;
      }
   }

   /* Draw test image */
   glClear(GL_COLOR_BUFFER_BIT);
   glPolygonMode(GL_FRONT, frontMode);
   glPolygonMode(GL_BACK, backMode);
   glFrontFace(GL_CCW);
   glDrawArrays(GL_QUADS, 0, 8);
   glFrontFace(GL_CW);
   glDrawArrays(GL_QUADS, 8, 8);

   /* check that these prims match expectations */
   /*
    * The first and 3rd should always be red (front-facing), the 2nd and 4th
    * green (back-facing).
    */
   for (i = 0; i < 4; i++) {
      GLenum prim = identify_primitive(Positions[i], Colors[!(i % 2)]);
      if (prim != expectedPrims[i]) {
         fprintf(stderr, "%s: glPolygonMode(front=%s, back=%s) failed\n",
                 TestName, piglit_get_gl_enum_name(frontMode),
                           piglit_get_gl_enum_name(backMode));
         pass = GL_FALSE;
      }
   }

   piglit_present_results();

   return pass;
}


static GLboolean
test_polygonmode(void)
{
   GLenum pass = GL_TRUE;

   glVertexPointer(2, GL_FLOAT, 0, Positions);
   glEnableClientState(GL_VERTEX_ARRAY);

   /*
    * First test with same front/back mode.
    * Those are probably more important to get right...
    */
   if (!test_combo(GL_FILL, GL_FILL))
      pass = GL_FALSE;

   if (!test_combo(GL_POINT, GL_POINT))
      pass = GL_FALSE;

   /*
    * Be extra mean to mesa draw stage interactions turning lines back
    * to tris...
    */
   glEnable(GL_LINE_SMOOTH);

   if (!test_combo(GL_LINE, GL_LINE))
      pass = GL_FALSE;

   glDisable(GL_LINE_SMOOTH);

   if (!test_combo(GL_FILL, GL_POINT))
      pass = GL_FALSE;

   if (!test_combo(GL_POINT, GL_LINE))
      pass = GL_FALSE;

   if (!test_combo(GL_POINT, GL_FILL))
      pass = GL_FALSE;

   if (!test_combo(GL_LINE, GL_FILL))
      pass = GL_FALSE;

   if (!test_combo(GL_LINE, GL_POINT))
     pass = GL_FALSE;


   /*
    * Be really mean to mesa draw stage interactions turning lines back
    * to tris...
    */
   glEnable(GL_LINE_SMOOTH);

   if (!test_combo(GL_FILL, GL_LINE))
      pass = GL_FALSE;

   if (!test_combo(GL_POINT, GL_LINE))
      pass = GL_FALSE;

   if (!test_combo(GL_LINE, GL_FILL))
      pass = GL_FALSE;

   if (!test_combo(GL_LINE, GL_POINT))
      pass = GL_FALSE;

   return pass;
}


enum piglit_result
piglit_display(void)
{
   if (!test_polygonmode())
      return PIGLIT_FAIL;

   return PIGLIT_PASS;
}


void
piglit_init(int argc, char **argv)
{
   GLuint prog;
   piglit_require_GLSL_version(130);
   piglit_ortho_projection(piglit_width, piglit_height, false);
   prog = piglit_build_simple_program(vstext, fstext);
   glUseProgram(prog);
}