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