Merge branch 'glsl2'
[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
361 /**
362 * Print triangle vertex attribs (for debug).
363 */
364 static void
365 print_triangle(struct lp_setup_context *setup,
366 const float (*v1)[4],
367 const float (*v2)[4],
368 const float (*v3)[4])
369 {
370 uint i;
371
372 debug_printf("llvmpipe triangle\n");
373 for (i = 0; i < 1 + setup->fs.nr_inputs; i++) {
374 debug_printf(" v1[%d]: %f %f %f %f\n", i,
375 v1[i][0], v1[i][1], v1[i][2], v1[i][3]);
376 }
377 for (i = 0; i < 1 + setup->fs.nr_inputs; i++) {
378 debug_printf(" v2[%d]: %f %f %f %f\n", i,
379 v2[i][0], v2[i][1], v2[i][2], v2[i][3]);
380 }
381 for (i = 0; i < 1 + setup->fs.nr_inputs; i++) {
382 debug_printf(" v3[%d]: %f %f %f %f\n", i,
383 v3[i][0], v3[i][1], v3[i][2], v3[i][3]);
384 }
385 }
386
387
388 lp_rast_cmd lp_rast_tri_tab[8] = {
389 NULL, /* should be impossible */
390 lp_rast_triangle_1,
391 lp_rast_triangle_2,
392 lp_rast_triangle_3,
393 lp_rast_triangle_4,
394 lp_rast_triangle_5,
395 lp_rast_triangle_6,
396 lp_rast_triangle_7
397 };
398
399 /**
400 * Do basic setup for triangle rasterization and determine which
401 * framebuffer tiles are touched. Put the triangle in the scene's
402 * bins for the tiles which we overlap.
403 */
404 static void
405 do_triangle_ccw(struct lp_setup_context *setup,
406 const float (*v1)[4],
407 const float (*v2)[4],
408 const float (*v3)[4],
409 boolean frontfacing )
410 {
411
412 struct lp_scene *scene = lp_setup_get_current_scene(setup);
413 struct lp_fragment_shader_variant *variant = setup->fs.current.variant;
414 struct lp_rast_triangle *tri;
415 struct tri_info info;
416 int area;
417 int minx, maxx, miny, maxy;
418 int ix0, ix1, iy0, iy1;
419 unsigned tri_bytes;
420 int i;
421 int nr_planes = 3;
422
423 if (0)
424 print_triangle(setup, v1, v2, v3);
425
426 if (setup->scissor_test) {
427 nr_planes = 7;
428 }
429 else {
430 nr_planes = 3;
431 }
432
433
434 tri = alloc_triangle(scene,
435 setup->fs.nr_inputs,
436 nr_planes,
437 &tri_bytes);
438 if (!tri)
439 return;
440
441 #ifdef DEBUG
442 tri->v[0][0] = v1[0][0];
443 tri->v[1][0] = v2[0][0];
444 tri->v[2][0] = v3[0][0];
445 tri->v[0][1] = v1[0][1];
446 tri->v[1][1] = v2[0][1];
447 tri->v[2][1] = v3[0][1];
448 #endif
449
450 /* x/y positions in fixed point */
451 info.x[0] = subpixel_snap(v1[0][0] - setup->pixel_offset);
452 info.x[1] = subpixel_snap(v2[0][0] - setup->pixel_offset);
453 info.x[2] = subpixel_snap(v3[0][0] - setup->pixel_offset);
454 info.y[0] = subpixel_snap(v1[0][1] - setup->pixel_offset);
455 info.y[1] = subpixel_snap(v2[0][1] - setup->pixel_offset);
456 info.y[2] = subpixel_snap(v3[0][1] - setup->pixel_offset);
457
458 tri->plane[0].dcdy = info.x[0] - info.x[1];
459 tri->plane[1].dcdy = info.x[1] - info.x[2];
460 tri->plane[2].dcdy = info.x[2] - info.x[0];
461
462 tri->plane[0].dcdx = info.y[0] - info.y[1];
463 tri->plane[1].dcdx = info.y[1] - info.y[2];
464 tri->plane[2].dcdx = info.y[2] - info.y[0];
465
466 area = (tri->plane[0].dcdy * tri->plane[2].dcdx -
467 tri->plane[2].dcdy * tri->plane[0].dcdx);
468
469 LP_COUNT(nr_tris);
470
471 /* Cull non-ccw and zero-sized triangles.
472 *
473 * XXX: subject to overflow??
474 */
475 if (area <= 0) {
476 lp_scene_putback_data( scene, tri_bytes );
477 LP_COUNT(nr_culled_tris);
478 return;
479 }
480
481 /* Bounding rectangle (in pixels) */
482 {
483 /* Yes this is necessary to accurately calculate bounding boxes
484 * with the two fill-conventions we support. GL (normally) ends
485 * up needing a bottom-left fill convention, which requires
486 * slightly different rounding.
487 */
488 int adj = (setup->pixel_offset != 0) ? 1 : 0;
489
490 minx = (MIN3(info.x[0], info.x[1], info.x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
491 maxx = (MAX3(info.x[0], info.x[1], info.x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
492 miny = (MIN3(info.y[0], info.y[1], info.y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
493 maxy = (MAX3(info.y[0], info.y[1], info.y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
494 }
495
496 if (setup->scissor_test) {
497 minx = MAX2(minx, setup->scissor.current.minx);
498 maxx = MIN2(maxx, setup->scissor.current.maxx);
499 miny = MAX2(miny, setup->scissor.current.miny);
500 maxy = MIN2(maxy, setup->scissor.current.maxy);
501 }
502 else {
503 minx = MAX2(minx, 0);
504 miny = MAX2(miny, 0);
505 maxx = MIN2(maxx, scene->fb.width);
506 maxy = MIN2(maxy, scene->fb.height);
507 }
508
509
510 if (miny >= maxy || minx >= maxx) {
511 lp_scene_putback_data( scene, tri_bytes );
512 LP_COUNT(nr_culled_tris);
513 return;
514 }
515
516 /*
517 */
518 info.pixel_offset = setup->pixel_offset;
519 info.v0 = v1;
520 info.v1 = v2;
521 info.v2 = v3;
522 info.dx01 = info.v0[0][0] - info.v1[0][0];
523 info.dx20 = info.v2[0][0] - info.v0[0][0];
524 info.dy01 = info.v0[0][1] - info.v1[0][1];
525 info.dy20 = info.v2[0][1] - info.v0[0][1];
526 info.oneoverarea = 1.0f / (info.dx01 * info.dy20 - info.dx20 * info.dy01);
527 info.frontfacing = frontfacing;
528
529 /* Setup parameter interpolants:
530 */
531 setup_tri_coefficients( setup, tri, &info );
532
533 tri->inputs.facing = frontfacing ? 1.0F : -1.0F;
534 tri->inputs.state = setup->fs.stored;
535
536
537
538 for (i = 0; i < 3; i++) {
539 struct lp_rast_plane *plane = &tri->plane[i];
540
541 /* half-edge constants, will be interated over the whole render
542 * target.
543 */
544 plane->c = plane->dcdx * info.x[i] - plane->dcdy * info.y[i];
545
546 /* correct for top-left vs. bottom-left fill convention.
547 *
548 * note that we're overloading gl_rasterization_rules to mean
549 * both (0.5,0.5) pixel centers *and* bottom-left filling
550 * convention.
551 *
552 * GL actually has a top-left filling convention, but GL's
553 * notion of "top" differs from gallium's...
554 *
555 * Also, sometimes (in FBO cases) GL will render upside down
556 * to its usual method, in which case it will probably want
557 * to use the opposite, top-left convention.
558 */
559 if (plane->dcdx < 0) {
560 /* both fill conventions want this - adjust for left edges */
561 plane->c++;
562 }
563 else if (plane->dcdx == 0) {
564 if (setup->pixel_offset == 0) {
565 /* correct for top-left fill convention:
566 */
567 if (plane->dcdy > 0) plane->c++;
568 }
569 else {
570 /* correct for bottom-left fill convention:
571 */
572 if (plane->dcdy < 0) plane->c++;
573 }
574 }
575
576 plane->dcdx *= FIXED_ONE;
577 plane->dcdy *= FIXED_ONE;
578
579 /* find trivial reject offsets for each edge for a single-pixel
580 * sized block. These will be scaled up at each recursive level to
581 * match the active blocksize. Scaling in this way works best if
582 * the blocks are square.
583 */
584 plane->eo = 0;
585 if (plane->dcdx < 0) plane->eo -= plane->dcdx;
586 if (plane->dcdy > 0) plane->eo += plane->dcdy;
587
588 /* Calculate trivial accept offsets from the above.
589 */
590 plane->ei = plane->dcdy - plane->dcdx - plane->eo;
591 }
592
593
594 /*
595 * When rasterizing scissored tris, use the intersection of the
596 * triangle bounding box and the scissor rect to generate the
597 * scissor planes.
598 *
599 * This permits us to cut off the triangle "tails" that are present
600 * in the intermediate recursive levels caused when two of the
601 * triangles edges don't diverge quickly enough to trivially reject
602 * exterior blocks from the triangle.
603 *
604 * It's not really clear if it's worth worrying about these tails,
605 * but since we generate the planes for each scissored tri, it's
606 * free to trim them in this case.
607 *
608 * Note that otherwise, the scissor planes only vary in 'C' value,
609 * and even then only on state-changes. Could alternatively store
610 * these planes elsewhere.
611 */
612 if (nr_planes == 7) {
613 tri->plane[3].dcdx = -1;
614 tri->plane[3].dcdy = 0;
615 tri->plane[3].c = 1-minx;
616 tri->plane[3].ei = 0;
617 tri->plane[3].eo = 1;
618
619 tri->plane[4].dcdx = 1;
620 tri->plane[4].dcdy = 0;
621 tri->plane[4].c = maxx;
622 tri->plane[4].ei = -1;
623 tri->plane[4].eo = 0;
624
625 tri->plane[5].dcdx = 0;
626 tri->plane[5].dcdy = 1;
627 tri->plane[5].c = 1-miny;
628 tri->plane[5].ei = 0;
629 tri->plane[5].eo = 1;
630
631 tri->plane[6].dcdx = 0;
632 tri->plane[6].dcdy = -1;
633 tri->plane[6].c = maxy;
634 tri->plane[6].ei = -1;
635 tri->plane[6].eo = 0;
636 }
637
638
639 /*
640 * All fields of 'tri' are now set. The remaining code here is
641 * concerned with binning.
642 */
643
644 /* Convert to tile coordinates, and inclusive ranges:
645 */
646 if (nr_planes == 3) {
647 int ix0 = minx / 16;
648 int iy0 = miny / 16;
649 int ix1 = (maxx-1) / 16;
650 int iy1 = (maxy-1) / 16;
651
652 if (iy0 == iy1 && ix0 == ix1)
653 {
654
655 /* Triangle is contained in a single 16x16 block:
656 */
657 int mask = (ix0 & 3) | ((iy0 & 3) << 4);
658
659 lp_scene_bin_command( scene, ix0/4, iy0/4,
660 lp_rast_triangle_3_16,
661 lp_rast_arg_triangle(tri, mask) );
662 return;
663 }
664 }
665
666 ix0 = minx / TILE_SIZE;
667 iy0 = miny / TILE_SIZE;
668 ix1 = (maxx-1) / TILE_SIZE;
669 iy1 = (maxy-1) / TILE_SIZE;
670
671 /*
672 * Clamp to framebuffer size
673 */
674 assert(ix0 == MAX2(ix0, 0));
675 assert(iy0 == MAX2(iy0, 0));
676 assert(ix1 == MIN2(ix1, scene->tiles_x - 1));
677 assert(iy1 == MIN2(iy1, scene->tiles_y - 1));
678
679 /* Determine which tile(s) intersect the triangle's bounding box
680 */
681 if (iy0 == iy1 && ix0 == ix1)
682 {
683 /* Triangle is contained in a single tile:
684 */
685 lp_scene_bin_command( scene, ix0, iy0,
686 lp_rast_tri_tab[nr_planes],
687 lp_rast_arg_triangle(tri, (1<<nr_planes)-1) );
688 }
689 else
690 {
691 int c[7];
692 int ei[7];
693 int eo[7];
694 int xstep[7];
695 int ystep[7];
696 int x, y;
697
698 for (i = 0; i < nr_planes; i++) {
699 c[i] = (tri->plane[i].c +
700 tri->plane[i].dcdy * iy0 * TILE_SIZE -
701 tri->plane[i].dcdx * ix0 * TILE_SIZE);
702
703 ei[i] = tri->plane[i].ei << TILE_ORDER;
704 eo[i] = tri->plane[i].eo << TILE_ORDER;
705 xstep[i] = -(tri->plane[i].dcdx << TILE_ORDER);
706 ystep[i] = tri->plane[i].dcdy << TILE_ORDER;
707 }
708
709
710
711 /* Test tile-sized blocks against the triangle.
712 * Discard blocks fully outside the tri. If the block is fully
713 * contained inside the tri, bin an lp_rast_shade_tile command.
714 * Else, bin a lp_rast_triangle command.
715 */
716 for (y = iy0; y <= iy1; y++)
717 {
718 boolean in = FALSE; /* are we inside the triangle? */
719 int cx[7];
720
721 for (i = 0; i < nr_planes; i++)
722 cx[i] = c[i];
723
724 for (x = ix0; x <= ix1; x++)
725 {
726 int out = 0;
727 int partial = 0;
728
729 for (i = 0; i < nr_planes; i++) {
730 int planeout = cx[i] + eo[i];
731 int planepartial = cx[i] + ei[i] - 1;
732 out |= (planeout >> 31);
733 partial |= (planepartial >> 31) & (1<<i);
734 }
735
736 if (out) {
737 /* do nothing */
738 if (in)
739 break; /* exiting triangle, all done with this row */
740 LP_COUNT(nr_empty_64);
741 }
742 else if (partial) {
743 /* Not trivially accepted by at least one plane -
744 * rasterize/shade partial tile
745 */
746 int count = util_bitcount(partial);
747 in = TRUE;
748 lp_scene_bin_command( scene, x, y,
749 lp_rast_tri_tab[count],
750 lp_rast_arg_triangle(tri, partial) );
751
752 LP_COUNT(nr_partially_covered_64);
753 }
754 else {
755 /* triangle covers the whole tile- shade whole tile */
756 LP_COUNT(nr_fully_covered_64);
757 in = TRUE;
758 if (variant->opaque &&
759 !setup->fb.zsbuf) {
760 lp_scene_bin_reset( scene, x, y );
761 }
762 lp_scene_bin_command( scene, x, y,
763 lp_rast_shade_tile,
764 lp_rast_arg_inputs(&tri->inputs) );
765 }
766
767 /* Iterate cx values across the region:
768 */
769 for (i = 0; i < nr_planes; i++)
770 cx[i] += xstep[i];
771 }
772
773 /* Iterate c values down the region:
774 */
775 for (i = 0; i < nr_planes; i++)
776 c[i] += ystep[i];
777 }
778 }
779 }
780
781
782 /**
783 * Draw triangle if it's CW, cull otherwise.
784 */
785 static void triangle_cw( struct lp_setup_context *setup,
786 const float (*v0)[4],
787 const float (*v1)[4],
788 const float (*v2)[4] )
789 {
790 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
791 }
792
793
794 /**
795 * Draw triangle if it's CCW, cull otherwise.
796 */
797 static void triangle_ccw( struct lp_setup_context *setup,
798 const float (*v0)[4],
799 const float (*v1)[4],
800 const float (*v2)[4] )
801 {
802 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
803 }
804
805
806
807 /**
808 * Draw triangle whether it's CW or CCW.
809 */
810 static void triangle_both( struct lp_setup_context *setup,
811 const float (*v0)[4],
812 const float (*v1)[4],
813 const float (*v2)[4] )
814 {
815 /* edge vectors e = v0 - v2, f = v1 - v2 */
816 const float ex = v0[0][0] - v2[0][0];
817 const float ey = v0[0][1] - v2[0][1];
818 const float fx = v1[0][0] - v2[0][0];
819 const float fy = v1[0][1] - v2[0][1];
820
821 /* det = cross(e,f).z */
822 if (ex * fy - ey * fx < 0.0f)
823 triangle_ccw( setup, v0, v1, v2 );
824 else
825 triangle_cw( setup, v0, v1, v2 );
826 }
827
828
829 static void triangle_nop( struct lp_setup_context *setup,
830 const float (*v0)[4],
831 const float (*v1)[4],
832 const float (*v2)[4] )
833 {
834 }
835
836
837 void
838 lp_setup_choose_triangle( struct lp_setup_context *setup )
839 {
840 switch (setup->cullmode) {
841 case PIPE_FACE_NONE:
842 setup->triangle = triangle_both;
843 break;
844 case PIPE_FACE_BACK:
845 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
846 break;
847 case PIPE_FACE_FRONT:
848 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
849 break;
850 default:
851 setup->triangle = triangle_nop;
852 break;
853 }
854 }