llvmpipe: better triangle debugging
[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 struct tri_info {
42
43 float pixel_offset;
44
45 /* fixed point vertex coordinates */
46 int x[3];
47 int y[3];
48
49 /* float x,y deltas - all from the original coordinates
50 */
51 float dy01, dy20;
52 float dx01, dx20;
53 float oneoverarea;
54
55 const float (*v0)[4];
56 const float (*v1)[4];
57 const float (*v2)[4];
58
59 boolean frontfacing;
60 };
61
62
63
64
65 static INLINE int
66 subpixel_snap(float a)
67 {
68 return util_iround(FIXED_ONE * a);
69 }
70
71 static INLINE float
72 fixed_to_float(int a)
73 {
74 return a * (1.0 / FIXED_ONE);
75 }
76
77
78
79 /**
80 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
81 */
82 static void constant_coef( struct lp_rast_triangle *tri,
83 unsigned slot,
84 const float value,
85 unsigned i )
86 {
87 tri->inputs.a0[slot][i] = value;
88 tri->inputs.dadx[slot][i] = 0.0f;
89 tri->inputs.dady[slot][i] = 0.0f;
90 }
91
92
93
94 static void linear_coef( struct lp_rast_triangle *tri,
95 const struct tri_info *info,
96 unsigned slot,
97 unsigned vert_attr,
98 unsigned i)
99 {
100 float a0 = info->v0[vert_attr][i];
101 float a1 = info->v1[vert_attr][i];
102 float a2 = info->v2[vert_attr][i];
103
104 float da01 = a0 - a1;
105 float da20 = a2 - a0;
106 float dadx = (da01 * info->dy20 - info->dy01 * da20) * info->oneoverarea;
107 float dady = (da20 * info->dx01 - info->dx20 * da01) * info->oneoverarea;
108
109 tri->inputs.dadx[slot][i] = dadx;
110 tri->inputs.dady[slot][i] = dady;
111
112 /* calculate a0 as the value which would be sampled for the
113 * fragment at (0,0), taking into account that we want to sample at
114 * pixel centers, in other words (0.5, 0.5).
115 *
116 * this is neat but unfortunately not a good way to do things for
117 * triangles with very large values of dadx or dady as it will
118 * result in the subtraction and re-addition from a0 of a very
119 * large number, which means we'll end up loosing a lot of the
120 * fractional bits and precision from a0. the way to fix this is
121 * to define a0 as the sample at a pixel center somewhere near vmin
122 * instead - i'll switch to this later.
123 */
124 tri->inputs.a0[slot][i] = (a0 -
125 (dadx * (info->v0[0][0] - info->pixel_offset) +
126 dady * (info->v0[0][1] - info->pixel_offset)));
127 }
128
129
130 /**
131 * Compute a0, dadx and dady for a perspective-corrected interpolant,
132 * for a triangle.
133 * We basically multiply the vertex value by 1/w before computing
134 * the plane coefficients (a0, dadx, dady).
135 * Later, when we compute the value at a particular fragment position we'll
136 * divide the interpolated value by the interpolated W at that fragment.
137 */
138 static void perspective_coef( struct lp_rast_triangle *tri,
139 const struct tri_info *info,
140 unsigned slot,
141 unsigned vert_attr,
142 unsigned i)
143 {
144 /* premultiply by 1/w (v[0][3] is always 1/w):
145 */
146 float a0 = info->v0[vert_attr][i] * info->v0[0][3];
147 float a1 = info->v1[vert_attr][i] * info->v1[0][3];
148 float a2 = info->v2[vert_attr][i] * info->v2[0][3];
149 float da01 = a0 - a1;
150 float da20 = a2 - a0;
151 float dadx = (da01 * info->dy20 - info->dy01 * da20) * info->oneoverarea;
152 float dady = (da20 * info->dx01 - info->dx20 * da01) * info->oneoverarea;
153
154 tri->inputs.dadx[slot][i] = dadx;
155 tri->inputs.dady[slot][i] = dady;
156 tri->inputs.a0[slot][i] = (a0 -
157 (dadx * (info->v0[0][0] - info->pixel_offset) +
158 dady * (info->v0[0][1] - info->pixel_offset)));
159 }
160
161
162 /**
163 * Special coefficient setup for gl_FragCoord.
164 * X and Y are trivial
165 * Z and W are copied from position_coef which should have already been computed.
166 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
167 */
168 static void
169 setup_fragcoord_coef(struct lp_rast_triangle *tri,
170 const struct tri_info *info,
171 unsigned slot,
172 unsigned usage_mask)
173 {
174 /*X*/
175 if (usage_mask & TGSI_WRITEMASK_X) {
176 tri->inputs.a0[slot][0] = 0.0;
177 tri->inputs.dadx[slot][0] = 1.0;
178 tri->inputs.dady[slot][0] = 0.0;
179 }
180
181 /*Y*/
182 if (usage_mask & TGSI_WRITEMASK_Y) {
183 tri->inputs.a0[slot][1] = 0.0;
184 tri->inputs.dadx[slot][1] = 0.0;
185 tri->inputs.dady[slot][1] = 1.0;
186 }
187
188 /*Z*/
189 if (usage_mask & TGSI_WRITEMASK_Z) {
190 linear_coef(tri, info, slot, 0, 2);
191 }
192
193 /*W*/
194 if (usage_mask & TGSI_WRITEMASK_W) {
195 linear_coef(tri, info, slot, 0, 3);
196 }
197 }
198
199
200 /**
201 * Setup the fragment input attribute with the front-facing value.
202 * \param frontface is the triangle front facing?
203 */
204 static void setup_facing_coef( struct lp_rast_triangle *tri,
205 unsigned slot,
206 boolean frontface,
207 unsigned usage_mask)
208 {
209 /* convert TRUE to 1.0 and FALSE to -1.0 */
210 if (usage_mask & TGSI_WRITEMASK_X)
211 constant_coef( tri, slot, 2.0f * frontface - 1.0f, 0 );
212
213 if (usage_mask & TGSI_WRITEMASK_Y)
214 constant_coef( tri, slot, 0.0f, 1 ); /* wasted */
215
216 if (usage_mask & TGSI_WRITEMASK_Z)
217 constant_coef( tri, slot, 0.0f, 2 ); /* wasted */
218
219 if (usage_mask & TGSI_WRITEMASK_W)
220 constant_coef( tri, slot, 0.0f, 3 ); /* wasted */
221 }
222
223
224 /**
225 * Compute the tri->coef[] array dadx, dady, a0 values.
226 */
227 static void setup_tri_coefficients( struct lp_setup_context *setup,
228 struct lp_rast_triangle *tri,
229 const struct tri_info *info)
230 {
231 unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
232 unsigned slot;
233 unsigned i;
234
235 /* setup interpolation for all the remaining attributes:
236 */
237 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
238 unsigned vert_attr = setup->fs.input[slot].src_index;
239 unsigned usage_mask = setup->fs.input[slot].usage_mask;
240
241 switch (setup->fs.input[slot].interp) {
242 case LP_INTERP_CONSTANT:
243 if (setup->flatshade_first) {
244 for (i = 0; i < NUM_CHANNELS; i++)
245 if (usage_mask & (1 << i))
246 constant_coef(tri, slot+1, info->v0[vert_attr][i], i);
247 }
248 else {
249 for (i = 0; i < NUM_CHANNELS; i++)
250 if (usage_mask & (1 << i))
251 constant_coef(tri, slot+1, info->v2[vert_attr][i], i);
252 }
253 break;
254
255 case LP_INTERP_LINEAR:
256 for (i = 0; i < NUM_CHANNELS; i++)
257 if (usage_mask & (1 << i))
258 linear_coef(tri, info, slot+1, vert_attr, i);
259 break;
260
261 case LP_INTERP_PERSPECTIVE:
262 for (i = 0; i < NUM_CHANNELS; i++)
263 if (usage_mask & (1 << i))
264 perspective_coef(tri, info, slot+1, vert_attr, i);
265 fragcoord_usage_mask |= TGSI_WRITEMASK_W;
266 break;
267
268 case LP_INTERP_POSITION:
269 /*
270 * The generated pixel interpolators will pick up the coeffs from
271 * slot 0, so all need to ensure that the usage mask is covers all
272 * usages.
273 */
274 fragcoord_usage_mask |= usage_mask;
275 break;
276
277 case LP_INTERP_FACING:
278 setup_facing_coef(tri, slot+1, info->frontfacing, usage_mask);
279 break;
280
281 default:
282 assert(0);
283 }
284 }
285
286 /* The internal position input is in slot zero:
287 */
288 setup_fragcoord_coef(tri, info, 0, fragcoord_usage_mask);
289
290 if (0) {
291 for (i = 0; i < NUM_CHANNELS; i++) {
292 float a0 = tri->inputs.a0 [0][i];
293 float dadx = tri->inputs.dadx[0][i];
294 float dady = tri->inputs.dady[0][i];
295
296 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
297 "xyzw"[i],
298 a0, dadx, dady);
299 }
300
301 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
302 unsigned usage_mask = setup->fs.input[slot].usage_mask;
303 for (i = 0; i < NUM_CHANNELS; i++) {
304 if (usage_mask & (1 << i)) {
305 float a0 = tri->inputs.a0 [1 + slot][i];
306 float dadx = tri->inputs.dadx[1 + slot][i];
307 float dady = tri->inputs.dady[1 + slot][i];
308
309 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
310 slot,
311 "xyzw"[i],
312 a0, dadx, dady);
313 }
314 }
315 }
316 }
317 }
318
319
320
321
322
323
324 /**
325 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
326 * immediately after it.
327 * The memory is allocated from the per-scene pool, not per-tile.
328 * \param tri_size returns number of bytes allocated
329 * \param nr_inputs number of fragment shader inputs
330 * \return pointer to triangle space
331 */
332 static INLINE struct lp_rast_triangle *
333 alloc_triangle(struct lp_scene *scene,
334 unsigned nr_inputs,
335 unsigned nr_planes,
336 unsigned *tri_size)
337 {
338 unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
339 struct lp_rast_triangle *tri;
340 unsigned tri_bytes, bytes;
341 char *inputs;
342
343 tri_bytes = align(Offset(struct lp_rast_triangle, plane[nr_planes]), 16);
344 bytes = tri_bytes + (3 * input_array_sz);
345
346 tri = lp_scene_alloc_aligned( scene, bytes, 16 );
347
348 if (tri) {
349 inputs = ((char *)tri) + tri_bytes;
350 tri->inputs.a0 = (float (*)[4]) inputs;
351 tri->inputs.dadx = (float (*)[4]) (inputs + input_array_sz);
352 tri->inputs.dady = (float (*)[4]) (inputs + 2 * input_array_sz);
353
354 *tri_size = bytes;
355 }
356
357 return tri;
358 }
359
360 void
361 lp_setup_print_vertex(struct lp_setup_context *setup,
362 const char *name,
363 const float (*v)[4])
364 {
365 int i, j;
366
367 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
368 name,
369 v[0][0], v[0][1], v[0][2], v[0][3]);
370
371 for (i = 0; i < setup->fs.nr_inputs; i++) {
372 const float *in = v[setup->fs.input[i].src_index];
373
374 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
375 i,
376 name, setup->fs.input[i].src_index,
377 (setup->fs.input[i].usage_mask & 0x1) ? "x" : " ",
378 (setup->fs.input[i].usage_mask & 0x2) ? "y" : " ",
379 (setup->fs.input[i].usage_mask & 0x4) ? "z" : " ",
380 (setup->fs.input[i].usage_mask & 0x8) ? "w" : " ");
381
382 for (j = 0; j < 4; j++)
383 if (setup->fs.input[i].usage_mask & (1<<j))
384 debug_printf("%.5f ", in[j]);
385
386 debug_printf("\n");
387 }
388 }
389
390
391 /**
392 * Print triangle vertex attribs (for debug).
393 */
394 void
395 lp_setup_print_triangle(struct lp_setup_context *setup,
396 const float (*v0)[4],
397 const float (*v1)[4],
398 const float (*v2)[4])
399 {
400 debug_printf("triangle\n");
401
402 {
403 const float ex = v0[0][0] - v2[0][0];
404 const float ey = v0[0][1] - v2[0][1];
405 const float fx = v1[0][0] - v2[0][0];
406 const float fy = v1[0][1] - v2[0][1];
407
408 /* det = cross(e,f).z */
409 const float det = ex * fy - ey * fx;
410 if (det < 0.0f)
411 debug_printf(" - ccw\n");
412 else if (det > 0.0f)
413 debug_printf(" - cw\n");
414 else
415 debug_printf(" - zero area\n");
416 }
417
418 lp_setup_print_vertex(setup, "v0", v0);
419 lp_setup_print_vertex(setup, "v1", v1);
420 lp_setup_print_vertex(setup, "v2", v2);
421 }
422
423
424 lp_rast_cmd lp_rast_tri_tab[8] = {
425 NULL, /* should be impossible */
426 lp_rast_triangle_1,
427 lp_rast_triangle_2,
428 lp_rast_triangle_3,
429 lp_rast_triangle_4,
430 lp_rast_triangle_5,
431 lp_rast_triangle_6,
432 lp_rast_triangle_7
433 };
434
435 /**
436 * Do basic setup for triangle rasterization and determine which
437 * framebuffer tiles are touched. Put the triangle in the scene's
438 * bins for the tiles which we overlap.
439 */
440 static void
441 do_triangle_ccw(struct lp_setup_context *setup,
442 const float (*v1)[4],
443 const float (*v2)[4],
444 const float (*v3)[4],
445 boolean frontfacing )
446 {
447
448 struct lp_scene *scene = lp_setup_get_current_scene(setup);
449 struct lp_fragment_shader_variant *variant = setup->fs.current.variant;
450 struct lp_rast_triangle *tri;
451 struct tri_info info;
452 int area;
453 int minx, maxx, miny, maxy;
454 int ix0, ix1, iy0, iy1;
455 unsigned tri_bytes;
456 int i;
457 int nr_planes = 3;
458
459 if (0)
460 lp_setup_print_triangle(setup, v1, v2, v3);
461
462 if (setup->scissor_test) {
463 nr_planes = 7;
464 }
465 else {
466 nr_planes = 3;
467 }
468
469
470 tri = alloc_triangle(scene,
471 setup->fs.nr_inputs,
472 nr_planes,
473 &tri_bytes);
474 if (!tri)
475 return;
476
477 #ifdef DEBUG
478 tri->v[0][0] = v1[0][0];
479 tri->v[1][0] = v2[0][0];
480 tri->v[2][0] = v3[0][0];
481 tri->v[0][1] = v1[0][1];
482 tri->v[1][1] = v2[0][1];
483 tri->v[2][1] = v3[0][1];
484 #endif
485
486 /* x/y positions in fixed point */
487 info.x[0] = subpixel_snap(v1[0][0] - setup->pixel_offset);
488 info.x[1] = subpixel_snap(v2[0][0] - setup->pixel_offset);
489 info.x[2] = subpixel_snap(v3[0][0] - setup->pixel_offset);
490 info.y[0] = subpixel_snap(v1[0][1] - setup->pixel_offset);
491 info.y[1] = subpixel_snap(v2[0][1] - setup->pixel_offset);
492 info.y[2] = subpixel_snap(v3[0][1] - setup->pixel_offset);
493
494 tri->plane[0].dcdy = info.x[0] - info.x[1];
495 tri->plane[1].dcdy = info.x[1] - info.x[2];
496 tri->plane[2].dcdy = info.x[2] - info.x[0];
497
498 tri->plane[0].dcdx = info.y[0] - info.y[1];
499 tri->plane[1].dcdx = info.y[1] - info.y[2];
500 tri->plane[2].dcdx = info.y[2] - info.y[0];
501
502 area = (tri->plane[0].dcdy * tri->plane[2].dcdx -
503 tri->plane[2].dcdy * tri->plane[0].dcdx);
504
505 LP_COUNT(nr_tris);
506
507 /* Cull non-ccw and zero-sized triangles.
508 *
509 * XXX: subject to overflow??
510 */
511 if (area <= 0) {
512 lp_scene_putback_data( scene, tri_bytes );
513 LP_COUNT(nr_culled_tris);
514 return;
515 }
516
517 /* Bounding rectangle (in pixels) */
518 {
519 /* Yes this is necessary to accurately calculate bounding boxes
520 * with the two fill-conventions we support. GL (normally) ends
521 * up needing a bottom-left fill convention, which requires
522 * slightly different rounding.
523 */
524 int adj = (setup->pixel_offset != 0) ? 1 : 0;
525
526 minx = (MIN3(info.x[0], info.x[1], info.x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
527 maxx = (MAX3(info.x[0], info.x[1], info.x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
528 miny = (MIN3(info.y[0], info.y[1], info.y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
529 maxy = (MAX3(info.y[0], info.y[1], info.y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
530 }
531
532 if (setup->scissor_test) {
533 minx = MAX2(minx, setup->scissor.current.minx);
534 maxx = MIN2(maxx, setup->scissor.current.maxx);
535 miny = MAX2(miny, setup->scissor.current.miny);
536 maxy = MIN2(maxy, setup->scissor.current.maxy);
537 }
538 else {
539 minx = MAX2(minx, 0);
540 miny = MAX2(miny, 0);
541 maxx = MIN2(maxx, scene->fb.width);
542 maxy = MIN2(maxy, scene->fb.height);
543 }
544
545
546 if (miny >= maxy || minx >= maxx) {
547 lp_scene_putback_data( scene, tri_bytes );
548 LP_COUNT(nr_culled_tris);
549 return;
550 }
551
552 /*
553 */
554 info.pixel_offset = setup->pixel_offset;
555 info.v0 = v1;
556 info.v1 = v2;
557 info.v2 = v3;
558 info.dx01 = info.v0[0][0] - info.v1[0][0];
559 info.dx20 = info.v2[0][0] - info.v0[0][0];
560 info.dy01 = info.v0[0][1] - info.v1[0][1];
561 info.dy20 = info.v2[0][1] - info.v0[0][1];
562 info.oneoverarea = 1.0f / (info.dx01 * info.dy20 - info.dx20 * info.dy01);
563 info.frontfacing = frontfacing;
564
565 /* Setup parameter interpolants:
566 */
567 setup_tri_coefficients( setup, tri, &info );
568
569 tri->inputs.facing = frontfacing ? 1.0F : -1.0F;
570 tri->inputs.state = setup->fs.stored;
571
572
573
574 for (i = 0; i < 3; i++) {
575 struct lp_rast_plane *plane = &tri->plane[i];
576
577 /* half-edge constants, will be interated over the whole render
578 * target.
579 */
580 plane->c = plane->dcdx * info.x[i] - plane->dcdy * info.y[i];
581
582 /* correct for top-left vs. bottom-left fill convention.
583 *
584 * note that we're overloading gl_rasterization_rules to mean
585 * both (0.5,0.5) pixel centers *and* bottom-left filling
586 * convention.
587 *
588 * GL actually has a top-left filling convention, but GL's
589 * notion of "top" differs from gallium's...
590 *
591 * Also, sometimes (in FBO cases) GL will render upside down
592 * to its usual method, in which case it will probably want
593 * to use the opposite, top-left convention.
594 */
595 if (plane->dcdx < 0) {
596 /* both fill conventions want this - adjust for left edges */
597 plane->c++;
598 }
599 else if (plane->dcdx == 0) {
600 if (setup->pixel_offset == 0) {
601 /* correct for top-left fill convention:
602 */
603 if (plane->dcdy > 0) plane->c++;
604 }
605 else {
606 /* correct for bottom-left fill convention:
607 */
608 if (plane->dcdy < 0) plane->c++;
609 }
610 }
611
612 plane->dcdx *= FIXED_ONE;
613 plane->dcdy *= FIXED_ONE;
614
615 /* find trivial reject offsets for each edge for a single-pixel
616 * sized block. These will be scaled up at each recursive level to
617 * match the active blocksize. Scaling in this way works best if
618 * the blocks are square.
619 */
620 plane->eo = 0;
621 if (plane->dcdx < 0) plane->eo -= plane->dcdx;
622 if (plane->dcdy > 0) plane->eo += plane->dcdy;
623
624 /* Calculate trivial accept offsets from the above.
625 */
626 plane->ei = plane->dcdy - plane->dcdx - plane->eo;
627 }
628
629
630 /*
631 * When rasterizing scissored tris, use the intersection of the
632 * triangle bounding box and the scissor rect to generate the
633 * scissor planes.
634 *
635 * This permits us to cut off the triangle "tails" that are present
636 * in the intermediate recursive levels caused when two of the
637 * triangles edges don't diverge quickly enough to trivially reject
638 * exterior blocks from the triangle.
639 *
640 * It's not really clear if it's worth worrying about these tails,
641 * but since we generate the planes for each scissored tri, it's
642 * free to trim them in this case.
643 *
644 * Note that otherwise, the scissor planes only vary in 'C' value,
645 * and even then only on state-changes. Could alternatively store
646 * these planes elsewhere.
647 */
648 if (nr_planes == 7) {
649 tri->plane[3].dcdx = -1;
650 tri->plane[3].dcdy = 0;
651 tri->plane[3].c = 1-minx;
652 tri->plane[3].ei = 0;
653 tri->plane[3].eo = 1;
654
655 tri->plane[4].dcdx = 1;
656 tri->plane[4].dcdy = 0;
657 tri->plane[4].c = maxx;
658 tri->plane[4].ei = -1;
659 tri->plane[4].eo = 0;
660
661 tri->plane[5].dcdx = 0;
662 tri->plane[5].dcdy = 1;
663 tri->plane[5].c = 1-miny;
664 tri->plane[5].ei = 0;
665 tri->plane[5].eo = 1;
666
667 tri->plane[6].dcdx = 0;
668 tri->plane[6].dcdy = -1;
669 tri->plane[6].c = maxy;
670 tri->plane[6].ei = -1;
671 tri->plane[6].eo = 0;
672 }
673
674
675 /*
676 * All fields of 'tri' are now set. The remaining code here is
677 * concerned with binning.
678 */
679
680 /* Convert to tile coordinates, and inclusive ranges:
681 */
682 if (nr_planes == 3) {
683 int ix0 = minx / 16;
684 int iy0 = miny / 16;
685 int ix1 = (maxx-1) / 16;
686 int iy1 = (maxy-1) / 16;
687
688 if (iy0 == iy1 && ix0 == ix1)
689 {
690
691 /* Triangle is contained in a single 16x16 block:
692 */
693 int mask = (ix0 & 3) | ((iy0 & 3) << 4);
694
695 lp_scene_bin_command( scene, ix0/4, iy0/4,
696 lp_rast_triangle_3_16,
697 lp_rast_arg_triangle(tri, mask) );
698 return;
699 }
700 }
701
702 ix0 = minx / TILE_SIZE;
703 iy0 = miny / TILE_SIZE;
704 ix1 = (maxx-1) / TILE_SIZE;
705 iy1 = (maxy-1) / TILE_SIZE;
706
707 /*
708 * Clamp to framebuffer size
709 */
710 assert(ix0 == MAX2(ix0, 0));
711 assert(iy0 == MAX2(iy0, 0));
712 assert(ix1 == MIN2(ix1, scene->tiles_x - 1));
713 assert(iy1 == MIN2(iy1, scene->tiles_y - 1));
714
715 /* Determine which tile(s) intersect the triangle's bounding box
716 */
717 if (iy0 == iy1 && ix0 == ix1)
718 {
719 /* Triangle is contained in a single tile:
720 */
721 lp_scene_bin_command( scene, ix0, iy0,
722 lp_rast_tri_tab[nr_planes],
723 lp_rast_arg_triangle(tri, (1<<nr_planes)-1) );
724 }
725 else
726 {
727 int c[7];
728 int ei[7];
729 int eo[7];
730 int xstep[7];
731 int ystep[7];
732 int x, y;
733
734 for (i = 0; i < nr_planes; i++) {
735 c[i] = (tri->plane[i].c +
736 tri->plane[i].dcdy * iy0 * TILE_SIZE -
737 tri->plane[i].dcdx * ix0 * TILE_SIZE);
738
739 ei[i] = tri->plane[i].ei << TILE_ORDER;
740 eo[i] = tri->plane[i].eo << TILE_ORDER;
741 xstep[i] = -(tri->plane[i].dcdx << TILE_ORDER);
742 ystep[i] = tri->plane[i].dcdy << TILE_ORDER;
743 }
744
745
746
747 /* Test tile-sized blocks against the triangle.
748 * Discard blocks fully outside the tri. If the block is fully
749 * contained inside the tri, bin an lp_rast_shade_tile command.
750 * Else, bin a lp_rast_triangle command.
751 */
752 for (y = iy0; y <= iy1; y++)
753 {
754 boolean in = FALSE; /* are we inside the triangle? */
755 int cx[7];
756
757 for (i = 0; i < nr_planes; i++)
758 cx[i] = c[i];
759
760 for (x = ix0; x <= ix1; x++)
761 {
762 int out = 0;
763 int partial = 0;
764
765 for (i = 0; i < nr_planes; i++) {
766 int planeout = cx[i] + eo[i];
767 int planepartial = cx[i] + ei[i] - 1;
768 out |= (planeout >> 31);
769 partial |= (planepartial >> 31) & (1<<i);
770 }
771
772 if (out) {
773 /* do nothing */
774 if (in)
775 break; /* exiting triangle, all done with this row */
776 LP_COUNT(nr_empty_64);
777 }
778 else if (partial) {
779 /* Not trivially accepted by at least one plane -
780 * rasterize/shade partial tile
781 */
782 int count = util_bitcount(partial);
783 in = TRUE;
784 lp_scene_bin_command( scene, x, y,
785 lp_rast_tri_tab[count],
786 lp_rast_arg_triangle(tri, partial) );
787
788 LP_COUNT(nr_partially_covered_64);
789 }
790 else {
791 /* triangle covers the whole tile- shade whole tile */
792 LP_COUNT(nr_fully_covered_64);
793 in = TRUE;
794 if (variant->opaque &&
795 !setup->fb.zsbuf) {
796 lp_scene_bin_reset( scene, x, y );
797 }
798 lp_scene_bin_command( scene, x, y,
799 lp_rast_shade_tile,
800 lp_rast_arg_inputs(&tri->inputs) );
801 }
802
803 /* Iterate cx values across the region:
804 */
805 for (i = 0; i < nr_planes; i++)
806 cx[i] += xstep[i];
807 }
808
809 /* Iterate c values down the region:
810 */
811 for (i = 0; i < nr_planes; i++)
812 c[i] += ystep[i];
813 }
814 }
815 }
816
817
818 /**
819 * Draw triangle if it's CW, cull otherwise.
820 */
821 static void triangle_cw( struct lp_setup_context *setup,
822 const float (*v0)[4],
823 const float (*v1)[4],
824 const float (*v2)[4] )
825 {
826 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
827 }
828
829
830 /**
831 * Draw triangle if it's CCW, cull otherwise.
832 */
833 static void triangle_ccw( struct lp_setup_context *setup,
834 const float (*v0)[4],
835 const float (*v1)[4],
836 const float (*v2)[4] )
837 {
838 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
839 }
840
841
842
843 /**
844 * Draw triangle whether it's CW or CCW.
845 */
846 static void triangle_both( struct lp_setup_context *setup,
847 const float (*v0)[4],
848 const float (*v1)[4],
849 const float (*v2)[4] )
850 {
851 /* edge vectors e = v0 - v2, f = v1 - v2 */
852 const float ex = v0[0][0] - v2[0][0];
853 const float ey = v0[0][1] - v2[0][1];
854 const float fx = v1[0][0] - v2[0][0];
855 const float fy = v1[0][1] - v2[0][1];
856
857 /* det = cross(e,f).z */
858 const float det = ex * fy - ey * fx;
859 if (det < 0.0f)
860 triangle_ccw( setup, v0, v1, v2 );
861 else if (det > 0.0f)
862 triangle_cw( setup, v0, v1, v2 );
863 }
864
865
866 static void triangle_nop( struct lp_setup_context *setup,
867 const float (*v0)[4],
868 const float (*v1)[4],
869 const float (*v2)[4] )
870 {
871 }
872
873
874 void
875 lp_setup_choose_triangle( struct lp_setup_context *setup )
876 {
877 switch (setup->cullmode) {
878 case PIPE_FACE_NONE:
879 setup->triangle = triangle_both;
880 break;
881 case PIPE_FACE_BACK:
882 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
883 break;
884 case PIPE_FACE_FRONT:
885 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
886 break;
887 default:
888 setup->triangle = triangle_nop;
889 break;
890 }
891 }