llvmpipe: add out of memory checking to triangle setup path
[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
38 #define NUM_CHANNELS 4
39
40
41 /**
42 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
43 */
44 static 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 */
60 static 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 */
108 static 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 */
142 static void
143 setup_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 */
170 static 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 */
186 static 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 if (setup->flatshade_first) {
209 for (i = 0; i < NUM_CHANNELS; i++)
210 constant_coef(setup, tri, slot+1, v1[vert_attr][i], i);
211 }
212 else {
213 for (i = 0; i < NUM_CHANNELS; i++)
214 constant_coef(setup, tri, slot+1, v3[vert_attr][i], i);
215 }
216 break;
217
218 case LP_INTERP_LINEAR:
219 for (i = 0; i < NUM_CHANNELS; i++)
220 linear_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
221 break;
222
223 case LP_INTERP_PERSPECTIVE:
224 for (i = 0; i < NUM_CHANNELS; i++)
225 perspective_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
226 break;
227
228 case LP_INTERP_POSITION:
229 /* XXX: fix me - duplicates the values in slot zero.
230 */
231 setup_fragcoord_coef(setup, tri, oneoverarea, slot+1, v1, v2, v3);
232 break;
233
234 case LP_INTERP_FACING:
235 setup_facing_coef(setup, tri, slot+1, frontface);
236 break;
237
238 default:
239 assert(0);
240 }
241 }
242 }
243
244
245
246 static INLINE int subpixel_snap( float a )
247 {
248 return util_iround(FIXED_ONE * a - (FIXED_ONE / 2));
249 }
250
251
252
253 /**
254 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
255 * immediately after it.
256 * The memory is allocated from the per-scene pool, not per-tile.
257 * \param tri_size returns number of bytes allocated
258 * \param nr_inputs number of fragment shader inputs
259 * \return pointer to triangle space
260 */
261 static INLINE struct lp_rast_triangle *
262 alloc_triangle(struct lp_scene *scene, unsigned nr_inputs, unsigned *tri_size)
263 {
264 unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
265 struct lp_rast_triangle *tri;
266 unsigned bytes;
267 char *inputs;
268
269 assert(sizeof(*tri) % 16 == 0);
270
271 bytes = sizeof(*tri) + (3 * input_array_sz);
272
273 tri = lp_scene_alloc_aligned( scene, bytes, 16 );
274
275 if (tri) {
276 inputs = (char *) (tri + 1);
277 tri->inputs.a0 = (float (*)[4]) inputs;
278 tri->inputs.dadx = (float (*)[4]) (inputs + input_array_sz);
279 tri->inputs.dady = (float (*)[4]) (inputs + 2 * input_array_sz);
280
281 *tri_size = bytes;
282 }
283
284 return tri;
285 }
286
287
288 /**
289 * Print triangle vertex attribs (for debug).
290 */
291 static void
292 print_triangle(struct lp_setup_context *setup,
293 const float (*v1)[4],
294 const float (*v2)[4],
295 const float (*v3)[4])
296 {
297 uint i;
298
299 debug_printf("llvmpipe triangle\n");
300 for (i = 0; i < setup->fs.nr_inputs; i++) {
301 debug_printf(" v1[%d]: %f %f %f %f\n", i,
302 v1[i][0], v1[i][1], v1[i][2], v1[i][3]);
303 }
304 for (i = 0; i < setup->fs.nr_inputs; i++) {
305 debug_printf(" v2[%d]: %f %f %f %f\n", i,
306 v2[i][0], v2[i][1], v2[i][2], v2[i][3]);
307 }
308 for (i = 0; i < setup->fs.nr_inputs; i++) {
309 debug_printf(" v3[%d]: %f %f %f %f\n", i,
310 v3[i][0], v3[i][1], v3[i][2], v3[i][3]);
311 }
312 }
313
314
315 /**
316 * Do basic setup for triangle rasterization and determine which
317 * framebuffer tiles are touched. Put the triangle in the scene's
318 * bins for the tiles which we overlap.
319 */
320 static void
321 do_triangle_ccw(struct lp_setup_context *setup,
322 const float (*v1)[4],
323 const float (*v2)[4],
324 const float (*v3)[4],
325 boolean frontfacing )
326 {
327 /* x/y positions in fixed point */
328 const int x1 = subpixel_snap(v1[0][0] + 0.5 - setup->pixel_offset);
329 const int x2 = subpixel_snap(v2[0][0] + 0.5 - setup->pixel_offset);
330 const int x3 = subpixel_snap(v3[0][0] + 0.5 - setup->pixel_offset);
331 const int y1 = subpixel_snap(v1[0][1] + 0.5 - setup->pixel_offset);
332 const int y2 = subpixel_snap(v2[0][1] + 0.5 - setup->pixel_offset);
333 const int y3 = subpixel_snap(v3[0][1] + 0.5 - setup->pixel_offset);
334
335 struct lp_scene *scene = lp_setup_get_current_scene(setup);
336 struct lp_rast_triangle *tri;
337 int area;
338 float oneoverarea;
339 int minx, maxx, miny, maxy;
340 unsigned tri_bytes;
341
342 if (0)
343 print_triangle(setup, v1, v2, v3);
344
345 tri = alloc_triangle(scene, setup->fs.nr_inputs, &tri_bytes);
346 if (!tri)
347 return;
348
349 #ifdef DEBUG
350 tri->v[0][0] = v1[0][0];
351 tri->v[1][0] = v2[0][0];
352 tri->v[2][0] = v3[0][0];
353 tri->v[0][1] = v1[0][1];
354 tri->v[1][1] = v2[0][1];
355 tri->v[2][1] = v3[0][1];
356 #endif
357
358 tri->dx12 = x1 - x2;
359 tri->dx23 = x2 - x3;
360 tri->dx31 = x3 - x1;
361
362 tri->dy12 = y1 - y2;
363 tri->dy23 = y2 - y3;
364 tri->dy31 = y3 - y1;
365
366 area = (tri->dx12 * tri->dy31 - tri->dx31 * tri->dy12);
367
368 LP_COUNT(nr_tris);
369
370 /* Cull non-ccw and zero-sized triangles.
371 *
372 * XXX: subject to overflow??
373 */
374 if (area <= 0) {
375 lp_scene_putback_data( scene, tri_bytes );
376 LP_COUNT(nr_culled_tris);
377 return;
378 }
379
380 /* Bounding rectangle (in pixels) */
381 minx = (MIN3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
382 maxx = (MAX3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
383 miny = (MIN3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
384 maxy = (MAX3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
385
386 if (setup->scissor_test) {
387 minx = MAX2(minx, setup->scissor.current.minx);
388 maxx = MIN2(maxx, setup->scissor.current.maxx);
389 miny = MAX2(miny, setup->scissor.current.miny);
390 maxy = MIN2(maxy, setup->scissor.current.maxy);
391 }
392
393 if (miny == maxy ||
394 minx == maxx) {
395 lp_scene_putback_data( scene, tri_bytes );
396 LP_COUNT(nr_culled_tris);
397 return;
398 }
399
400 /*
401 */
402 oneoverarea = ((float)FIXED_ONE) / (float)area;
403
404 /* Setup parameter interpolants:
405 */
406 setup_tri_coefficients( setup, tri, oneoverarea, v1, v2, v3, frontfacing );
407
408 tri->inputs.facing = frontfacing ? 1.0F : -1.0F;
409
410 /* half-edge constants, will be interated over the whole render target.
411 */
412 tri->c1 = tri->dy12 * x1 - tri->dx12 * y1;
413 tri->c2 = tri->dy23 * x2 - tri->dx23 * y2;
414 tri->c3 = tri->dy31 * x3 - tri->dx31 * y3;
415
416 /* correct for top-left fill convention:
417 */
418 if (tri->dy12 < 0 || (tri->dy12 == 0 && tri->dx12 > 0)) tri->c1++;
419 if (tri->dy23 < 0 || (tri->dy23 == 0 && tri->dx23 > 0)) tri->c2++;
420 if (tri->dy31 < 0 || (tri->dy31 == 0 && tri->dx31 > 0)) tri->c3++;
421
422 tri->dy12 *= FIXED_ONE;
423 tri->dy23 *= FIXED_ONE;
424 tri->dy31 *= FIXED_ONE;
425
426 tri->dx12 *= FIXED_ONE;
427 tri->dx23 *= FIXED_ONE;
428 tri->dx31 *= FIXED_ONE;
429
430 /* find trivial reject offsets for each edge for a single-pixel
431 * sized block. These will be scaled up at each recursive level to
432 * match the active blocksize. Scaling in this way works best if
433 * the blocks are square.
434 */
435 tri->eo1 = 0;
436 if (tri->dy12 < 0) tri->eo1 -= tri->dy12;
437 if (tri->dx12 > 0) tri->eo1 += tri->dx12;
438
439 tri->eo2 = 0;
440 if (tri->dy23 < 0) tri->eo2 -= tri->dy23;
441 if (tri->dx23 > 0) tri->eo2 += tri->dx23;
442
443 tri->eo3 = 0;
444 if (tri->dy31 < 0) tri->eo3 -= tri->dy31;
445 if (tri->dx31 > 0) tri->eo3 += tri->dx31;
446
447 /* Calculate trivial accept offsets from the above.
448 */
449 tri->ei1 = tri->dx12 - tri->dy12 - tri->eo1;
450 tri->ei2 = tri->dx23 - tri->dy23 - tri->eo2;
451 tri->ei3 = tri->dx31 - tri->dy31 - tri->eo3;
452
453 /* Fill in the inputs.step[][] arrays.
454 * We've manually unrolled some loops here.
455 */
456 {
457 const int xstep1 = -tri->dy12;
458 const int xstep2 = -tri->dy23;
459 const int xstep3 = -tri->dy31;
460 const int ystep1 = tri->dx12;
461 const int ystep2 = tri->dx23;
462 const int ystep3 = tri->dx31;
463
464 #define SETUP_STEP(i, x, y) \
465 do { \
466 tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
467 tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
468 tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
469 } while (0)
470
471 SETUP_STEP(0, 0, 0);
472 SETUP_STEP(1, 1, 0);
473 SETUP_STEP(2, 0, 1);
474 SETUP_STEP(3, 1, 1);
475
476 SETUP_STEP(4, 2, 0);
477 SETUP_STEP(5, 3, 0);
478 SETUP_STEP(6, 2, 1);
479 SETUP_STEP(7, 3, 1);
480
481 SETUP_STEP(8, 0, 2);
482 SETUP_STEP(9, 1, 2);
483 SETUP_STEP(10, 0, 3);
484 SETUP_STEP(11, 1, 3);
485
486 SETUP_STEP(12, 2, 2);
487 SETUP_STEP(13, 3, 2);
488 SETUP_STEP(14, 2, 3);
489 SETUP_STEP(15, 3, 3);
490 #undef STEP
491 }
492
493 /*
494 * All fields of 'tri' are now set. The remaining code here is
495 * concerned with binning.
496 */
497
498 /* Convert to tile coordinates:
499 */
500 minx = minx / TILE_SIZE;
501 miny = miny / TILE_SIZE;
502 maxx = maxx / TILE_SIZE;
503 maxy = maxy / TILE_SIZE;
504
505 /*
506 * Clamp to framebuffer size
507 */
508 minx = MAX2(minx, 0);
509 miny = MAX2(miny, 0);
510 maxx = MIN2(maxx, scene->tiles_x - 1);
511 maxy = MIN2(maxy, scene->tiles_y - 1);
512
513 /* Determine which tile(s) intersect the triangle's bounding box
514 */
515 if (miny == maxy && minx == maxx)
516 {
517 /* Triangle is contained in a single tile:
518 */
519 lp_scene_bin_command( scene, minx, miny, lp_rast_triangle,
520 lp_rast_arg_triangle(tri) );
521 }
522 else
523 {
524 int c1 = (tri->c1 +
525 tri->dx12 * miny * TILE_SIZE -
526 tri->dy12 * minx * TILE_SIZE);
527 int c2 = (tri->c2 +
528 tri->dx23 * miny * TILE_SIZE -
529 tri->dy23 * minx * TILE_SIZE);
530 int c3 = (tri->c3 +
531 tri->dx31 * miny * TILE_SIZE -
532 tri->dy31 * minx * TILE_SIZE);
533
534 int ei1 = tri->ei1 << TILE_ORDER;
535 int ei2 = tri->ei2 << TILE_ORDER;
536 int ei3 = tri->ei3 << TILE_ORDER;
537
538 int eo1 = tri->eo1 << TILE_ORDER;
539 int eo2 = tri->eo2 << TILE_ORDER;
540 int eo3 = tri->eo3 << TILE_ORDER;
541
542 int xstep1 = -(tri->dy12 << TILE_ORDER);
543 int xstep2 = -(tri->dy23 << TILE_ORDER);
544 int xstep3 = -(tri->dy31 << TILE_ORDER);
545
546 int ystep1 = tri->dx12 << TILE_ORDER;
547 int ystep2 = tri->dx23 << TILE_ORDER;
548 int ystep3 = tri->dx31 << TILE_ORDER;
549 int x, y;
550
551
552 /* Test tile-sized blocks against the triangle.
553 * Discard blocks fully outside the tri. If the block is fully
554 * contained inside the tri, bin an lp_rast_shade_tile command.
555 * Else, bin a lp_rast_triangle command.
556 */
557 for (y = miny; y <= maxy; y++)
558 {
559 int cx1 = c1;
560 int cx2 = c2;
561 int cx3 = c3;
562 boolean in = FALSE; /* are we inside the triangle? */
563
564 for (x = minx; x <= maxx; x++)
565 {
566 if (cx1 + eo1 < 0 ||
567 cx2 + eo2 < 0 ||
568 cx3 + eo3 < 0)
569 {
570 /* do nothing */
571 LP_COUNT(nr_empty_64);
572 if (in)
573 break; /* exiting triangle, all done with this row */
574 }
575 else if (cx1 + ei1 > 0 &&
576 cx2 + ei2 > 0 &&
577 cx3 + ei3 > 0)
578 {
579 /* triangle covers the whole tile- shade whole tile */
580 LP_COUNT(nr_fully_covered_64);
581 in = TRUE;
582 if(setup->fs.current.opaque) {
583 lp_scene_bin_reset( scene, x, y );
584 lp_scene_bin_command( scene, x, y,
585 lp_rast_set_state,
586 lp_rast_arg_state(setup->fs.stored) );
587 }
588 lp_scene_bin_command( scene, x, y,
589 lp_rast_shade_tile,
590 lp_rast_arg_inputs(&tri->inputs) );
591 }
592 else
593 {
594 /* rasterizer/shade partial tile */
595 LP_COUNT(nr_partially_covered_64);
596 in = TRUE;
597 lp_scene_bin_command( scene, x, y,
598 lp_rast_triangle,
599 lp_rast_arg_triangle(tri) );
600 }
601
602 /* Iterate cx values across the region:
603 */
604 cx1 += xstep1;
605 cx2 += xstep2;
606 cx3 += xstep3;
607 }
608
609 /* Iterate c values down the region:
610 */
611 c1 += ystep1;
612 c2 += ystep2;
613 c3 += ystep3;
614 }
615 }
616 }
617
618
619 /**
620 * Draw triangle if it's CW, cull otherwise.
621 */
622 static void triangle_cw( struct lp_setup_context *setup,
623 const float (*v0)[4],
624 const float (*v1)[4],
625 const float (*v2)[4] )
626 {
627 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
628 }
629
630
631 /**
632 * Draw triangle if it's CCW, cull otherwise.
633 */
634 static void triangle_ccw( struct lp_setup_context *setup,
635 const float (*v0)[4],
636 const float (*v1)[4],
637 const float (*v2)[4] )
638 {
639 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
640 }
641
642
643
644 /**
645 * Draw triangle whether it's CW or CCW.
646 */
647 static void triangle_both( struct lp_setup_context *setup,
648 const float (*v0)[4],
649 const float (*v1)[4],
650 const float (*v2)[4] )
651 {
652 /* edge vectors e = v0 - v2, f = v1 - v2 */
653 const float ex = v0[0][0] - v2[0][0];
654 const float ey = v0[0][1] - v2[0][1];
655 const float fx = v1[0][0] - v2[0][0];
656 const float fy = v1[0][1] - v2[0][1];
657
658 /* det = cross(e,f).z */
659 if (ex * fy - ey * fx < 0.0f)
660 triangle_ccw( setup, v0, v1, v2 );
661 else
662 triangle_cw( setup, v0, v1, v2 );
663 }
664
665
666 static void triangle_nop( struct lp_setup_context *setup,
667 const float (*v0)[4],
668 const float (*v1)[4],
669 const float (*v2)[4] )
670 {
671 }
672
673
674 void
675 lp_setup_choose_triangle( struct lp_setup_context *setup )
676 {
677 switch (setup->cullmode) {
678 case PIPE_FACE_NONE:
679 setup->triangle = triangle_both;
680 break;
681 case PIPE_FACE_BACK:
682 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
683 break;
684 case PIPE_FACE_FRONT:
685 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
686 break;
687 default:
688 setup->triangle = triangle_nop;
689 break;
690 }
691 }