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