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