lp_setup_tri.c revision db4ccc004a96255f3ad0dc26467f2243a133c24b
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/*
29 * Binning code for triangles
30 */
31
32#include "util/u_math.h"
33#include "util/u_memory.h"
34#include "lp_perf.h"
35#include "lp_setup_context.h"
36#include "lp_rast.h"
37
38#define NUM_CHANNELS 4
39
40
41/**
42 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
43 */
44static void constant_coef( struct lp_setup_context *setup,
45                           struct lp_rast_triangle *tri,
46                           unsigned slot,
47			   const float value,
48                           unsigned i )
49{
50   tri->inputs.a0[slot][i] = value;
51   tri->inputs.dadx[slot][i] = 0.0f;
52   tri->inputs.dady[slot][i] = 0.0f;
53}
54
55
56/**
57 * Compute a0, dadx and dady for a linearly interpolated coefficient,
58 * for a triangle.
59 */
60static void linear_coef( struct lp_setup_context *setup,
61                         struct lp_rast_triangle *tri,
62                         float oneoverarea,
63                         unsigned slot,
64                         const float (*v1)[4],
65                         const float (*v2)[4],
66                         const float (*v3)[4],
67                         unsigned vert_attr,
68                         unsigned i)
69{
70   float a1 = v1[vert_attr][i];
71   float a2 = v2[vert_attr][i];
72   float a3 = v3[vert_attr][i];
73
74   float da12 = a1 - a2;
75   float da31 = a3 - a1;
76   float dadx = (da12 * tri->dy31 - tri->dy12 * da31) * oneoverarea;
77   float dady = (da31 * tri->dx12 - tri->dx31 * da12) * oneoverarea;
78
79   tri->inputs.dadx[slot][i] = dadx;
80   tri->inputs.dady[slot][i] = dady;
81
82   /* calculate a0 as the value which would be sampled for the
83    * fragment at (0,0), taking into account that we want to sample at
84    * pixel centers, in other words (0.5, 0.5).
85    *
86    * this is neat but unfortunately not a good way to do things for
87    * triangles with very large values of dadx or dady as it will
88    * result in the subtraction and re-addition from a0 of a very
89    * large number, which means we'll end up loosing a lot of the
90    * fractional bits and precision from a0.  the way to fix this is
91    * to define a0 as the sample at a pixel center somewhere near vmin
92    * instead - i'll switch to this later.
93    */
94   tri->inputs.a0[slot][i] = (a1 -
95                              (dadx * (v1[0][0] - setup->pixel_offset) +
96                               dady * (v1[0][1] - setup->pixel_offset)));
97}
98
99
100/**
101 * Compute a0, dadx and dady for a perspective-corrected interpolant,
102 * for a triangle.
103 * We basically multiply the vertex value by 1/w before computing
104 * the plane coefficients (a0, dadx, dady).
105 * Later, when we compute the value at a particular fragment position we'll
106 * divide the interpolated value by the interpolated W at that fragment.
107 */
108static void perspective_coef( struct lp_setup_context *setup,
109                              struct lp_rast_triangle *tri,
110                              float oneoverarea,
111                              unsigned slot,
112			      const float (*v1)[4],
113			      const float (*v2)[4],
114			      const float (*v3)[4],
115			      unsigned vert_attr,
116                              unsigned i)
117{
118   /* premultiply by 1/w  (v[0][3] is always 1/w):
119    */
120   float a1 = v1[vert_attr][i] * v1[0][3];
121   float a2 = v2[vert_attr][i] * v2[0][3];
122   float a3 = v3[vert_attr][i] * v3[0][3];
123   float da12 = a1 - a2;
124   float da31 = a3 - a1;
125   float dadx = (da12 * tri->dy31 - tri->dy12 * da31) * oneoverarea;
126   float dady = (da31 * tri->dx12 - tri->dx31 * da12) * oneoverarea;
127
128   tri->inputs.dadx[slot][i] = dadx;
129   tri->inputs.dady[slot][i] = dady;
130   tri->inputs.a0[slot][i] = (a1 -
131                              (dadx * (v1[0][0] - setup->pixel_offset) +
132                               dady * (v1[0][1] - setup->pixel_offset)));
133}
134
135
136/**
137 * Special coefficient setup for gl_FragCoord.
138 * X and Y are trivial
139 * Z and W are copied from position_coef which should have already been computed.
140 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
141 */
142static void
143setup_fragcoord_coef(struct lp_setup_context *setup,
144                     struct lp_rast_triangle *tri,
145                     float oneoverarea,
146                     unsigned slot,
147                     const float (*v1)[4],
148                     const float (*v2)[4],
149                     const float (*v3)[4])
150{
151   /*X*/
152   tri->inputs.a0[slot][0] = 0.0;
153   tri->inputs.dadx[slot][0] = 1.0;
154   tri->inputs.dady[slot][0] = 0.0;
155   /*Y*/
156   tri->inputs.a0[slot][1] = 0.0;
157   tri->inputs.dadx[slot][1] = 0.0;
158   tri->inputs.dady[slot][1] = 1.0;
159   /*Z*/
160   linear_coef(setup, tri, oneoverarea, slot, v1, v2, v3, 0, 2);
161   /*W*/
162   linear_coef(setup, tri, oneoverarea, slot, v1, v2, v3, 0, 3);
163}
164
165
166/**
167 * Setup the fragment input attribute with the front-facing value.
168 * \param frontface  is the triangle front facing?
169 */
170static void setup_facing_coef( struct lp_setup_context *setup,
171                               struct lp_rast_triangle *tri,
172                               unsigned slot,
173                               boolean frontface )
174{
175   /* convert TRUE to 1.0 and FALSE to -1.0 */
176   constant_coef( setup, tri, slot, 2.0f * frontface - 1.0f, 0 );
177   constant_coef( setup, tri, slot, 0.0f, 1 ); /* wasted */
178   constant_coef( setup, tri, slot, 0.0f, 2 ); /* wasted */
179   constant_coef( setup, tri, slot, 0.0f, 3 ); /* wasted */
180}
181
182
183/**
184 * Compute the tri->coef[] array dadx, dady, a0 values.
185 */
186static void setup_tri_coefficients( struct lp_setup_context *setup,
187				    struct lp_rast_triangle *tri,
188                                    float oneoverarea,
189				    const float (*v1)[4],
190				    const float (*v2)[4],
191				    const float (*v3)[4],
192				    boolean frontface)
193{
194   unsigned slot;
195
196   /* The internal position input is in slot zero:
197    */
198   setup_fragcoord_coef(setup, tri, oneoverarea, 0, v1, v2, v3);
199
200   /* setup interpolation for all the remaining attributes:
201    */
202   for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
203      unsigned vert_attr = setup->fs.input[slot].src_index;
204      unsigned i;
205
206      switch (setup->fs.input[slot].interp) {
207      case LP_INTERP_CONSTANT:
208         for (i = 0; i < NUM_CHANNELS; i++)
209            constant_coef(setup, tri, slot+1, v3[vert_attr][i], i);
210         break;
211
212      case LP_INTERP_LINEAR:
213         for (i = 0; i < NUM_CHANNELS; i++)
214            linear_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
215         break;
216
217      case LP_INTERP_PERSPECTIVE:
218         for (i = 0; i < NUM_CHANNELS; i++)
219            perspective_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
220         break;
221
222      case LP_INTERP_POSITION:
223         /* XXX: fix me - duplicates the values in slot zero.
224          */
225         setup_fragcoord_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3);
226         break;
227
228      case LP_INTERP_FACING:
229         setup_facing_coef(setup, tri, slot+1, frontface);
230         break;
231
232      default:
233         assert(0);
234      }
235   }
236}
237
238
239
240static INLINE int subpixel_snap( float a )
241{
242   return util_iround(FIXED_ONE * a - (FIXED_ONE / 2));
243}
244
245
246
247/**
248 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
249 * immediately after it.
250 * The memory is allocated from the per-scene pool, not per-tile.
251 * \param tri_size  returns number of bytes allocated
252 * \param nr_inputs  number of fragment shader inputs
253 * \return pointer to triangle space
254 */
255static INLINE struct lp_rast_triangle *
256alloc_triangle(struct lp_scene *scene, unsigned nr_inputs, unsigned *tri_size)
257{
258   unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
259   struct lp_rast_triangle *tri;
260   unsigned bytes;
261   char *inputs;
262
263   assert(sizeof(*tri) % 16 == 0);
264
265   bytes = sizeof(*tri) + (3 * input_array_sz);
266
267   tri = lp_scene_alloc_aligned( scene, bytes, 16 );
268
269   inputs = (char *) (tri + 1);
270   tri->inputs.a0   = (float (*)[4]) inputs;
271   tri->inputs.dadx = (float (*)[4]) (inputs + input_array_sz);
272   tri->inputs.dady = (float (*)[4]) (inputs + 2 * input_array_sz);
273
274   *tri_size = bytes;
275
276   return tri;
277}
278
279
280/**
281 * Print triangle vertex attribs (for debug).
282 */
283static void
284print_triangle(struct lp_setup_context *setup,
285               const float (*v1)[4],
286               const float (*v2)[4],
287               const float (*v3)[4])
288{
289   uint i;
290
291   debug_printf("llvmpipe triangle\n");
292   for (i = 0; i < setup->fs.nr_inputs; i++) {
293      debug_printf("  v1[%d]:  %f %f %f %f\n", i,
294                   v1[i][0], v1[i][1], v1[i][2], v1[i][3]);
295   }
296   for (i = 0; i < setup->fs.nr_inputs; i++) {
297      debug_printf("  v2[%d]:  %f %f %f %f\n", i,
298                   v2[i][0], v2[i][1], v2[i][2], v2[i][3]);
299   }
300   for (i = 0; i < setup->fs.nr_inputs; i++) {
301      debug_printf("  v3[%d]:  %f %f %f %f\n", i,
302                   v3[i][0], v3[i][1], v3[i][2], v3[i][3]);
303   }
304}
305
306
307/**
308 * Do basic setup for triangle rasterization and determine which
309 * framebuffer tiles are touched.  Put the triangle in the scene's
310 * bins for the tiles which we overlap.
311 */
312static void
313do_triangle_ccw(struct lp_setup_context *setup,
314		const float (*v1)[4],
315		const float (*v2)[4],
316		const float (*v3)[4],
317		boolean frontfacing )
318{
319   /* x/y positions in fixed point */
320   const int x1 = subpixel_snap(v1[0][0] + 0.5 - setup->pixel_offset);
321   const int x2 = subpixel_snap(v2[0][0] + 0.5 - setup->pixel_offset);
322   const int x3 = subpixel_snap(v3[0][0] + 0.5 - setup->pixel_offset);
323   const int y1 = subpixel_snap(v1[0][1] + 0.5 - setup->pixel_offset);
324   const int y2 = subpixel_snap(v2[0][1] + 0.5 - setup->pixel_offset);
325   const int y3 = subpixel_snap(v3[0][1] + 0.5 - setup->pixel_offset);
326
327   struct lp_scene *scene = lp_setup_get_current_scene(setup);
328   struct lp_rast_triangle *tri;
329   int area;
330   float oneoverarea;
331   int minx, maxx, miny, maxy;
332   unsigned tri_bytes;
333
334   if (0)
335      print_triangle(setup, v1, v2, v3);
336
337   tri = alloc_triangle(scene, setup->fs.nr_inputs, &tri_bytes);
338
339#ifdef DEBUG
340   tri->v[0][0] = v1[0][0];
341   tri->v[1][0] = v2[0][0];
342   tri->v[2][0] = v3[0][0];
343   tri->v[0][1] = v1[0][1];
344   tri->v[1][1] = v2[0][1];
345   tri->v[2][1] = v3[0][1];
346#endif
347
348   tri->dx12 = x1 - x2;
349   tri->dx23 = x2 - x3;
350   tri->dx31 = x3 - x1;
351
352   tri->dy12 = y1 - y2;
353   tri->dy23 = y2 - y3;
354   tri->dy31 = y3 - y1;
355
356   area = (tri->dx12 * tri->dy31 - tri->dx31 * tri->dy12);
357
358   LP_COUNT(nr_tris);
359
360   /* Cull non-ccw and zero-sized triangles.
361    *
362    * XXX: subject to overflow??
363    */
364   if (area <= 0) {
365      lp_scene_putback_data( scene, tri_bytes );
366      LP_COUNT(nr_culled_tris);
367      return;
368   }
369
370   /* Bounding rectangle (in pixels) */
371   minx = (MIN3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
372   maxx = (MAX3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
373   miny = (MIN3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
374   maxy = (MAX3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
375
376   if (setup->scissor_test) {
377      minx = MAX2(minx, setup->scissor.current.minx);
378      maxx = MIN2(maxx, setup->scissor.current.maxx);
379      miny = MAX2(miny, setup->scissor.current.miny);
380      maxy = MIN2(maxy, setup->scissor.current.maxy);
381   }
382
383   if (miny == maxy ||
384       minx == maxx) {
385      lp_scene_putback_data( scene, tri_bytes );
386      LP_COUNT(nr_culled_tris);
387      return;
388   }
389
390   /*
391    */
392   oneoverarea = ((float)FIXED_ONE) / (float)area;
393
394   /* Setup parameter interpolants:
395    */
396   setup_tri_coefficients( setup, tri, oneoverarea, v1, v2, v3, frontfacing );
397
398   tri->inputs.facing = frontfacing ? 1.0F : -1.0F;
399
400   /* half-edge constants, will be interated over the whole render target.
401    */
402   tri->c1 = tri->dy12 * x1 - tri->dx12 * y1;
403   tri->c2 = tri->dy23 * x2 - tri->dx23 * y2;
404   tri->c3 = tri->dy31 * x3 - tri->dx31 * y3;
405
406   /* correct for top-left fill convention:
407    */
408   if (tri->dy12 < 0 || (tri->dy12 == 0 && tri->dx12 > 0)) tri->c1++;
409   if (tri->dy23 < 0 || (tri->dy23 == 0 && tri->dx23 > 0)) tri->c2++;
410   if (tri->dy31 < 0 || (tri->dy31 == 0 && tri->dx31 > 0)) tri->c3++;
411
412   tri->dy12 *= FIXED_ONE;
413   tri->dy23 *= FIXED_ONE;
414   tri->dy31 *= FIXED_ONE;
415
416   tri->dx12 *= FIXED_ONE;
417   tri->dx23 *= FIXED_ONE;
418   tri->dx31 *= FIXED_ONE;
419
420   /* find trivial reject offsets for each edge for a single-pixel
421    * sized block.  These will be scaled up at each recursive level to
422    * match the active blocksize.  Scaling in this way works best if
423    * the blocks are square.
424    */
425   tri->eo1 = 0;
426   if (tri->dy12 < 0) tri->eo1 -= tri->dy12;
427   if (tri->dx12 > 0) tri->eo1 += tri->dx12;
428
429   tri->eo2 = 0;
430   if (tri->dy23 < 0) tri->eo2 -= tri->dy23;
431   if (tri->dx23 > 0) tri->eo2 += tri->dx23;
432
433   tri->eo3 = 0;
434   if (tri->dy31 < 0) tri->eo3 -= tri->dy31;
435   if (tri->dx31 > 0) tri->eo3 += tri->dx31;
436
437   /* Calculate trivial accept offsets from the above.
438    */
439   tri->ei1 = tri->dx12 - tri->dy12 - tri->eo1;
440   tri->ei2 = tri->dx23 - tri->dy23 - tri->eo2;
441   tri->ei3 = tri->dx31 - tri->dy31 - tri->eo3;
442
443   /* Fill in the inputs.step[][] arrays.
444    * We've manually unrolled some loops here.
445    */
446   {
447      const int xstep1 = -tri->dy12;
448      const int xstep2 = -tri->dy23;
449      const int xstep3 = -tri->dy31;
450      const int ystep1 = tri->dx12;
451      const int ystep2 = tri->dx23;
452      const int ystep3 = tri->dx31;
453
454#define SETUP_STEP(i, x, y)                                \
455      do {                                                 \
456         tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
457         tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
458         tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
459      } while (0)
460
461      SETUP_STEP(0, 0, 0);
462      SETUP_STEP(1, 1, 0);
463      SETUP_STEP(2, 0, 1);
464      SETUP_STEP(3, 1, 1);
465
466      SETUP_STEP(4, 2, 0);
467      SETUP_STEP(5, 3, 0);
468      SETUP_STEP(6, 2, 1);
469      SETUP_STEP(7, 3, 1);
470
471      SETUP_STEP(8, 0, 2);
472      SETUP_STEP(9, 1, 2);
473      SETUP_STEP(10, 0, 3);
474      SETUP_STEP(11, 1, 3);
475
476      SETUP_STEP(12, 2, 2);
477      SETUP_STEP(13, 3, 2);
478      SETUP_STEP(14, 2, 3);
479      SETUP_STEP(15, 3, 3);
480#undef STEP
481   }
482
483   /*
484    * All fields of 'tri' are now set.  The remaining code here is
485    * concerned with binning.
486    */
487
488   /* Convert to tile coordinates:
489    */
490   minx = minx / TILE_SIZE;
491   miny = miny / TILE_SIZE;
492   maxx = maxx / TILE_SIZE;
493   maxy = maxy / TILE_SIZE;
494
495   /*
496    * Clamp to framebuffer size
497    */
498   minx = MAX2(minx, 0);
499   miny = MAX2(miny, 0);
500   maxx = MIN2(maxx, scene->tiles_x - 1);
501   maxy = MIN2(maxy, scene->tiles_y - 1);
502
503   /* Determine which tile(s) intersect the triangle's bounding box
504    */
505   if (miny == maxy && minx == maxx)
506   {
507      /* Triangle is contained in a single tile:
508       */
509      lp_scene_bin_command( scene, minx, miny, lp_rast_triangle,
510			    lp_rast_arg_triangle(tri) );
511   }
512   else
513   {
514      int c1 = (tri->c1 +
515                tri->dx12 * miny * TILE_SIZE -
516                tri->dy12 * minx * TILE_SIZE);
517      int c2 = (tri->c2 +
518                tri->dx23 * miny * TILE_SIZE -
519                tri->dy23 * minx * TILE_SIZE);
520      int c3 = (tri->c3 +
521                tri->dx31 * miny * TILE_SIZE -
522                tri->dy31 * minx * TILE_SIZE);
523
524      int ei1 = tri->ei1 << TILE_ORDER;
525      int ei2 = tri->ei2 << TILE_ORDER;
526      int ei3 = tri->ei3 << TILE_ORDER;
527
528      int eo1 = tri->eo1 << TILE_ORDER;
529      int eo2 = tri->eo2 << TILE_ORDER;
530      int eo3 = tri->eo3 << TILE_ORDER;
531
532      int xstep1 = -(tri->dy12 << TILE_ORDER);
533      int xstep2 = -(tri->dy23 << TILE_ORDER);
534      int xstep3 = -(tri->dy31 << TILE_ORDER);
535
536      int ystep1 = tri->dx12 << TILE_ORDER;
537      int ystep2 = tri->dx23 << TILE_ORDER;
538      int ystep3 = tri->dx31 << TILE_ORDER;
539      int x, y;
540
541
542      /* Test tile-sized blocks against the triangle.
543       * Discard blocks fully outside the tri.  If the block is fully
544       * contained inside the tri, bin an lp_rast_shade_tile command.
545       * Else, bin a lp_rast_triangle command.
546       */
547      for (y = miny; y <= maxy; y++)
548      {
549	 int cx1 = c1;
550	 int cx2 = c2;
551	 int cx3 = c3;
552	 boolean in = FALSE;  /* are we inside the triangle? */
553
554	 for (x = minx; x <= maxx; x++)
555	 {
556	    if (cx1 + eo1 < 0 ||
557		cx2 + eo2 < 0 ||
558		cx3 + eo3 < 0)
559	    {
560	       /* do nothing */
561               LP_COUNT(nr_empty_64);
562	       if (in)
563		  break;  /* exiting triangle, all done with this row */
564	    }
565	    else if (cx1 + ei1 > 0 &&
566		     cx2 + ei2 > 0 &&
567		     cx3 + ei3 > 0)
568	    {
569               /* triangle covers the whole tile- shade whole tile */
570               LP_COUNT(nr_fully_covered_64);
571	       in = TRUE;
572	       if(setup->fs.current.opaque) {
573	          lp_scene_bin_reset( scene, x, y );
574	          lp_scene_bin_command( scene, x, y,
575	                                lp_rast_set_state,
576	                                lp_rast_arg_state(setup->fs.stored) );
577	       }
578               lp_scene_bin_command( scene, x, y,
579				     lp_rast_shade_tile,
580				     lp_rast_arg_inputs(&tri->inputs) );
581	    }
582	    else
583	    {
584               /* rasterizer/shade partial tile */
585               LP_COUNT(nr_partially_covered_64);
586	       in = TRUE;
587               lp_scene_bin_command( scene, x, y,
588				     lp_rast_triangle,
589				     lp_rast_arg_triangle(tri) );
590	    }
591
592	    /* Iterate cx values across the region:
593	     */
594	    cx1 += xstep1;
595	    cx2 += xstep2;
596	    cx3 += xstep3;
597	 }
598
599	 /* Iterate c values down the region:
600	  */
601	 c1 += ystep1;
602	 c2 += ystep2;
603	 c3 += ystep3;
604      }
605   }
606}
607
608
609/**
610 * Draw triangle if it's CW, cull otherwise.
611 */
612static void triangle_cw( struct lp_setup_context *setup,
613			 const float (*v0)[4],
614			 const float (*v1)[4],
615			 const float (*v2)[4] )
616{
617   do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
618}
619
620
621/**
622 * Draw triangle if it's CCW, cull otherwise.
623 */
624static void triangle_ccw( struct lp_setup_context *setup,
625			 const float (*v0)[4],
626			 const float (*v1)[4],
627			 const float (*v2)[4] )
628{
629   do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
630}
631
632
633
634/**
635 * Draw triangle whether it's CW or CCW.
636 */
637static void triangle_both( struct lp_setup_context *setup,
638			   const float (*v0)[4],
639			   const float (*v1)[4],
640			   const float (*v2)[4] )
641{
642   /* edge vectors e = v0 - v2, f = v1 - v2 */
643   const float ex = v0[0][0] - v2[0][0];
644   const float ey = v0[0][1] - v2[0][1];
645   const float fx = v1[0][0] - v2[0][0];
646   const float fy = v1[0][1] - v2[0][1];
647
648   /* det = cross(e,f).z */
649   if (ex * fy - ey * fx < 0.0f)
650      triangle_ccw( setup, v0, v1, v2 );
651   else
652      triangle_cw( setup, v0, v1, v2 );
653}
654
655
656static void triangle_nop( struct lp_setup_context *setup,
657			  const float (*v0)[4],
658			  const float (*v1)[4],
659			  const float (*v2)[4] )
660{
661}
662
663
664void
665lp_setup_choose_triangle( struct lp_setup_context *setup )
666{
667   switch (setup->cullmode) {
668   case PIPE_WINDING_NONE:
669      setup->triangle = triangle_both;
670      break;
671   case PIPE_WINDING_CCW:
672      setup->triangle = triangle_cw;
673      break;
674   case PIPE_WINDING_CW:
675      setup->triangle = triangle_ccw;
676      break;
677   default:
678      setup->triangle = triangle_nop;
679      break;
680   }
681}
682