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