ilo: support and prefer compact array spacing
[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 "util/u_rect.h"
35 #include "util/u_sse.h"
36 #include "lp_perf.h"
37 #include "lp_setup_context.h"
38 #include "lp_rast.h"
39 #include "lp_state_fs.h"
40 #include "lp_state_setup.h"
41
42 #define NUM_CHANNELS 4
43
44 #if defined(PIPE_ARCH_SSE)
45 #include <emmintrin.h>
46 #endif
47
48 static INLINE int
49 subpixel_snap(float a)
50 {
51 return util_iround(FIXED_ONE * a);
52 }
53
54 static INLINE float
55 fixed_to_float(int a)
56 {
57 return a * (1.0f / FIXED_ONE);
58 }
59
60
61 /* Position and area in fixed point coordinates */
62 struct fixed_position {
63 int x[4];
64 int y[4];
65 int area;
66 int dx01;
67 int dy01;
68 int dx20;
69 int dy20;
70 };
71
72
73 /**
74 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
75 * immediately after it.
76 * The memory is allocated from the per-scene pool, not per-tile.
77 * \param tri_size returns number of bytes allocated
78 * \param num_inputs number of fragment shader inputs
79 * \return pointer to triangle space
80 */
81 struct lp_rast_triangle *
82 lp_setup_alloc_triangle(struct lp_scene *scene,
83 unsigned nr_inputs,
84 unsigned nr_planes,
85 unsigned *tri_size)
86 {
87 unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
88 unsigned plane_sz = nr_planes * sizeof(struct lp_rast_plane);
89 struct lp_rast_triangle *tri;
90
91 *tri_size = (sizeof(struct lp_rast_triangle) +
92 3 * input_array_sz +
93 plane_sz);
94
95 tri = lp_scene_alloc_aligned( scene, *tri_size, 16 );
96 if (tri == NULL)
97 return NULL;
98
99 tri->inputs.stride = input_array_sz;
100
101 {
102 char *a = (char *)tri;
103 char *b = (char *)&GET_PLANES(tri)[nr_planes];
104 assert(b - a == *tri_size);
105 }
106
107 return tri;
108 }
109
110 void
111 lp_setup_print_vertex(struct lp_setup_context *setup,
112 const char *name,
113 const float (*v)[4])
114 {
115 const struct lp_setup_variant_key *key = &setup->setup.variant->key;
116 int i, j;
117
118 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
119 name,
120 v[0][0], v[0][1], v[0][2], v[0][3]);
121
122 for (i = 0; i < key->num_inputs; i++) {
123 const float *in = v[key->inputs[i].src_index];
124
125 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
126 i,
127 name, key->inputs[i].src_index,
128 (key->inputs[i].usage_mask & 0x1) ? "x" : " ",
129 (key->inputs[i].usage_mask & 0x2) ? "y" : " ",
130 (key->inputs[i].usage_mask & 0x4) ? "z" : " ",
131 (key->inputs[i].usage_mask & 0x8) ? "w" : " ");
132
133 for (j = 0; j < 4; j++)
134 if (key->inputs[i].usage_mask & (1<<j))
135 debug_printf("%.5f ", in[j]);
136
137 debug_printf("\n");
138 }
139 }
140
141
142 /**
143 * Print triangle vertex attribs (for debug).
144 */
145 void
146 lp_setup_print_triangle(struct lp_setup_context *setup,
147 const float (*v0)[4],
148 const float (*v1)[4],
149 const float (*v2)[4])
150 {
151 debug_printf("triangle\n");
152
153 {
154 const float ex = v0[0][0] - v2[0][0];
155 const float ey = v0[0][1] - v2[0][1];
156 const float fx = v1[0][0] - v2[0][0];
157 const float fy = v1[0][1] - v2[0][1];
158
159 /* det = cross(e,f).z */
160 const float det = ex * fy - ey * fx;
161 if (det < 0.0f)
162 debug_printf(" - ccw\n");
163 else if (det > 0.0f)
164 debug_printf(" - cw\n");
165 else
166 debug_printf(" - zero area\n");
167 }
168
169 lp_setup_print_vertex(setup, "v0", v0);
170 lp_setup_print_vertex(setup, "v1", v1);
171 lp_setup_print_vertex(setup, "v2", v2);
172 }
173
174
175 #define MAX_PLANES 8
176 static unsigned
177 lp_rast_tri_tab[MAX_PLANES+1] = {
178 0, /* should be impossible */
179 LP_RAST_OP_TRIANGLE_1,
180 LP_RAST_OP_TRIANGLE_2,
181 LP_RAST_OP_TRIANGLE_3,
182 LP_RAST_OP_TRIANGLE_4,
183 LP_RAST_OP_TRIANGLE_5,
184 LP_RAST_OP_TRIANGLE_6,
185 LP_RAST_OP_TRIANGLE_7,
186 LP_RAST_OP_TRIANGLE_8
187 };
188
189
190
191 /**
192 * The primitive covers the whole tile- shade whole tile.
193 *
194 * \param tx, ty the tile position in tiles, not pixels
195 */
196 static boolean
197 lp_setup_whole_tile(struct lp_setup_context *setup,
198 const struct lp_rast_shader_inputs *inputs,
199 int tx, int ty)
200 {
201 struct lp_scene *scene = setup->scene;
202
203 LP_COUNT(nr_fully_covered_64);
204
205 /* if variant is opaque and scissor doesn't effect the tile */
206 if (inputs->opaque) {
207 if (!scene->fb.zsbuf) {
208 /*
209 * All previous rendering will be overwritten so reset the bin.
210 */
211 lp_scene_bin_reset( scene, tx, ty );
212 }
213
214 LP_COUNT(nr_shade_opaque_64);
215 return lp_scene_bin_cmd_with_state( scene, tx, ty,
216 setup->fs.stored,
217 LP_RAST_OP_SHADE_TILE_OPAQUE,
218 lp_rast_arg_inputs(inputs) );
219 } else {
220 LP_COUNT(nr_shade_64);
221 return lp_scene_bin_cmd_with_state( scene, tx, ty,
222 setup->fs.stored,
223 LP_RAST_OP_SHADE_TILE,
224 lp_rast_arg_inputs(inputs) );
225 }
226 }
227
228
229 /**
230 * Do basic setup for triangle rasterization and determine which
231 * framebuffer tiles are touched. Put the triangle in the scene's
232 * bins for the tiles which we overlap.
233 */
234 static boolean
235 do_triangle_ccw(struct lp_setup_context *setup,
236 struct fixed_position* position,
237 const float (*v0)[4],
238 const float (*v1)[4],
239 const float (*v2)[4],
240 boolean frontfacing )
241 {
242 struct lp_scene *scene = setup->scene;
243 const struct lp_setup_variant_key *key = &setup->setup.variant->key;
244 struct lp_rast_triangle *tri;
245 struct lp_rast_plane *plane;
246 struct u_rect bbox;
247 unsigned tri_bytes;
248 int nr_planes = 3;
249
250 /* Area should always be positive here */
251 assert(position->area > 0);
252
253 if (0)
254 lp_setup_print_triangle(setup, v0, v1, v2);
255
256 if (setup->scissor_test) {
257 nr_planes = 7;
258 }
259 else {
260 nr_planes = 3;
261 }
262
263 /* Bounding rectangle (in pixels) */
264 {
265 /* Yes this is necessary to accurately calculate bounding boxes
266 * with the two fill-conventions we support. GL (normally) ends
267 * up needing a bottom-left fill convention, which requires
268 * slightly different rounding.
269 */
270 int adj = (setup->pixel_offset != 0) ? 1 : 0;
271
272 /* Inclusive x0, exclusive x1 */
273 bbox.x0 = MIN3(position->x[0], position->x[1], position->x[2]) >> FIXED_ORDER;
274 bbox.x1 = (MAX3(position->x[0], position->x[1], position->x[2]) - 1) >> FIXED_ORDER;
275
276 /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
277 bbox.y0 = (MIN3(position->y[0], position->y[1], position->y[2]) + adj) >> FIXED_ORDER;
278 bbox.y1 = (MAX3(position->y[0], position->y[1], position->y[2]) - 1 + adj) >> FIXED_ORDER;
279 }
280
281 if (bbox.x1 < bbox.x0 ||
282 bbox.y1 < bbox.y0) {
283 if (0) debug_printf("empty bounding box\n");
284 LP_COUNT(nr_culled_tris);
285 return TRUE;
286 }
287
288 if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
289 if (0) debug_printf("offscreen\n");
290 LP_COUNT(nr_culled_tris);
291 return TRUE;
292 }
293
294 /* Can safely discard negative regions, but need to keep hold of
295 * information about when the triangle extends past screen
296 * boundaries. See trimmed_box in lp_setup_bin_triangle().
297 */
298 bbox.x0 = MAX2(bbox.x0, 0);
299 bbox.y0 = MAX2(bbox.y0, 0);
300
301 tri = lp_setup_alloc_triangle(scene,
302 key->num_inputs,
303 nr_planes,
304 &tri_bytes);
305 if (!tri)
306 return FALSE;
307
308 #if 0
309 tri->v[0][0] = v0[0][0];
310 tri->v[1][0] = v1[0][0];
311 tri->v[2][0] = v2[0][0];
312 tri->v[0][1] = v0[0][1];
313 tri->v[1][1] = v1[0][1];
314 tri->v[2][1] = v2[0][1];
315 #endif
316
317 LP_COUNT(nr_tris);
318
319 /* Setup parameter interpolants:
320 */
321 setup->setup.variant->jit_function( v0,
322 v1,
323 v2,
324 frontfacing,
325 GET_A0(&tri->inputs),
326 GET_DADX(&tri->inputs),
327 GET_DADY(&tri->inputs) );
328
329 tri->inputs.frontfacing = frontfacing;
330 tri->inputs.disable = FALSE;
331 tri->inputs.opaque = setup->fs.current.variant->opaque;
332
333 if (0)
334 lp_dump_setup_coef(&setup->setup.variant->key,
335 (const float (*)[4])GET_A0(&tri->inputs),
336 (const float (*)[4])GET_DADX(&tri->inputs),
337 (const float (*)[4])GET_DADY(&tri->inputs));
338
339 plane = GET_PLANES(tri);
340
341 #if defined(PIPE_ARCH_SSE)
342 {
343 __m128i vertx, verty;
344 __m128i shufx, shufy;
345 __m128i dcdx, dcdy, c;
346 __m128i unused;
347 __m128i dcdx_neg_mask;
348 __m128i dcdy_neg_mask;
349 __m128i dcdx_zero_mask;
350 __m128i top_left_flag;
351 __m128i c_inc_mask, c_inc;
352 __m128i eo, p0, p1, p2;
353 __m128i zero = _mm_setzero_si128();
354
355 vertx = _mm_loadu_si128((__m128i *)position->x); /* vertex x coords */
356 verty = _mm_loadu_si128((__m128i *)position->y); /* vertex y coords */
357
358 shufx = _mm_shuffle_epi32(vertx, _MM_SHUFFLE(3,0,2,1));
359 shufy = _mm_shuffle_epi32(verty, _MM_SHUFFLE(3,0,2,1));
360
361 dcdx = _mm_sub_epi32(verty, shufy);
362 dcdy = _mm_sub_epi32(vertx, shufx);
363
364 dcdx_neg_mask = _mm_srai_epi32(dcdx, 31);
365 dcdx_zero_mask = _mm_cmpeq_epi32(dcdx, zero);
366 dcdy_neg_mask = _mm_srai_epi32(dcdy, 31);
367
368 top_left_flag = _mm_set1_epi32((setup->bottom_edge_rule == 0) ? ~0 : 0);
369
370 c_inc_mask = _mm_or_si128(dcdx_neg_mask,
371 _mm_and_si128(dcdx_zero_mask,
372 _mm_xor_si128(dcdy_neg_mask,
373 top_left_flag)));
374
375 c_inc = _mm_srli_epi32(c_inc_mask, 31);
376
377 c = _mm_sub_epi32(mm_mullo_epi32(dcdx, vertx),
378 mm_mullo_epi32(dcdy, verty));
379
380 c = _mm_add_epi32(c, c_inc);
381
382 /* Scale up to match c:
383 */
384 dcdx = _mm_slli_epi32(dcdx, FIXED_ORDER);
385 dcdy = _mm_slli_epi32(dcdy, FIXED_ORDER);
386
387 /* Calculate trivial reject values:
388 */
389 eo = _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask, dcdy),
390 _mm_and_si128(dcdx_neg_mask, dcdx));
391
392 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
393
394 /* Pointless transpose which gets undone immediately in
395 * rasterization:
396 */
397 transpose4_epi32(&c, &dcdx, &dcdy, &eo,
398 &p0, &p1, &p2, &unused);
399
400 _mm_store_si128((__m128i *)&plane[0], p0);
401 _mm_store_si128((__m128i *)&plane[1], p1);
402 _mm_store_si128((__m128i *)&plane[2], p2);
403 }
404 #else
405 {
406 int i;
407 plane[0].dcdy = position->dx01;
408 plane[1].dcdy = position->x[1] - position->x[2];
409 plane[2].dcdy = position->dx20;
410 plane[0].dcdx = position->dy01;
411 plane[1].dcdx = position->y[1] - position->y[2];
412 plane[2].dcdx = position->dy20;
413
414 for (i = 0; i < 3; i++) {
415 /* half-edge constants, will be interated over the whole render
416 * target.
417 */
418 plane[i].c = plane[i].dcdx * position->x[i] - plane[i].dcdy * position->y[i];
419
420 /* correct for top-left vs. bottom-left fill convention.
421 */
422 if (plane[i].dcdx < 0) {
423 /* both fill conventions want this - adjust for left edges */
424 plane[i].c++;
425 }
426 else if (plane[i].dcdx == 0) {
427 if (setup->bottom_edge_rule == 0){
428 /* correct for top-left fill convention:
429 */
430 if (plane[i].dcdy > 0) plane[i].c++;
431 }
432 else {
433 /* correct for bottom-left fill convention:
434 */
435 if (plane[i].dcdy < 0) plane[i].c++;
436 }
437 }
438
439 plane[i].dcdx *= FIXED_ONE;
440 plane[i].dcdy *= FIXED_ONE;
441
442 /* find trivial reject offsets for each edge for a single-pixel
443 * sized block. These will be scaled up at each recursive level to
444 * match the active blocksize. Scaling in this way works best if
445 * the blocks are square.
446 */
447 plane[i].eo = 0;
448 if (plane[i].dcdx < 0) plane[i].eo -= plane[i].dcdx;
449 if (plane[i].dcdy > 0) plane[i].eo += plane[i].dcdy;
450 }
451 }
452 #endif
453
454 if (0) {
455 debug_printf("p0: %08x/%08x/%08x/%08x\n",
456 plane[0].c,
457 plane[0].dcdx,
458 plane[0].dcdy,
459 plane[0].eo);
460
461 debug_printf("p1: %08x/%08x/%08x/%08x\n",
462 plane[1].c,
463 plane[1].dcdx,
464 plane[1].dcdy,
465 plane[1].eo);
466
467 debug_printf("p0: %08x/%08x/%08x/%08x\n",
468 plane[2].c,
469 plane[2].dcdx,
470 plane[2].dcdy,
471 plane[2].eo);
472 }
473
474
475 /*
476 * When rasterizing scissored tris, use the intersection of the
477 * triangle bounding box and the scissor rect to generate the
478 * scissor planes.
479 *
480 * This permits us to cut off the triangle "tails" that are present
481 * in the intermediate recursive levels caused when two of the
482 * triangles edges don't diverge quickly enough to trivially reject
483 * exterior blocks from the triangle.
484 *
485 * It's not really clear if it's worth worrying about these tails,
486 * but since we generate the planes for each scissored tri, it's
487 * free to trim them in this case.
488 *
489 * Note that otherwise, the scissor planes only vary in 'C' value,
490 * and even then only on state-changes. Could alternatively store
491 * these planes elsewhere.
492 */
493 if (nr_planes == 7) {
494 const struct u_rect *scissor = &setup->scissor;
495
496 plane[3].dcdx = -1;
497 plane[3].dcdy = 0;
498 plane[3].c = 1-scissor->x0;
499 plane[3].eo = 1;
500
501 plane[4].dcdx = 1;
502 plane[4].dcdy = 0;
503 plane[4].c = scissor->x1+1;
504 plane[4].eo = 0;
505
506 plane[5].dcdx = 0;
507 plane[5].dcdy = 1;
508 plane[5].c = 1-scissor->y0;
509 plane[5].eo = 1;
510
511 plane[6].dcdx = 0;
512 plane[6].dcdy = -1;
513 plane[6].c = scissor->y1+1;
514 plane[6].eo = 0;
515 }
516
517 return lp_setup_bin_triangle( setup, tri, &bbox, nr_planes );
518 }
519
520 /*
521 * Round to nearest less or equal power of two of the input.
522 *
523 * Undefined if no bit set exists, so code should check against 0 first.
524 */
525 static INLINE uint32_t
526 floor_pot(uint32_t n)
527 {
528 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
529 if (n == 0)
530 return 0;
531
532 __asm__("bsr %1,%0"
533 : "=r" (n)
534 : "rm" (n));
535 return 1 << n;
536 #else
537 n |= (n >> 1);
538 n |= (n >> 2);
539 n |= (n >> 4);
540 n |= (n >> 8);
541 n |= (n >> 16);
542 return n - (n >> 1);
543 #endif
544 }
545
546
547 boolean
548 lp_setup_bin_triangle( struct lp_setup_context *setup,
549 struct lp_rast_triangle *tri,
550 const struct u_rect *bbox,
551 int nr_planes )
552 {
553 struct lp_scene *scene = setup->scene;
554 struct u_rect trimmed_box = *bbox;
555 int i;
556
557 /* What is the largest power-of-two boundary this triangle crosses:
558 */
559 int dx = floor_pot((bbox->x0 ^ bbox->x1) |
560 (bbox->y0 ^ bbox->y1));
561
562 /* The largest dimension of the rasterized area of the triangle
563 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
564 */
565 int sz = floor_pot((bbox->x1 - (bbox->x0 & ~3)) |
566 (bbox->y1 - (bbox->y0 & ~3)));
567
568 /* Now apply scissor, etc to the bounding box. Could do this
569 * earlier, but it confuses the logic for tri-16 and would force
570 * the rasterizer to also respect scissor, etc, just for the rare
571 * cases where a small triangle extends beyond the scissor.
572 */
573 u_rect_find_intersection(&setup->draw_region, &trimmed_box);
574
575 /* Determine which tile(s) intersect the triangle's bounding box
576 */
577 if (dx < TILE_SIZE)
578 {
579 int ix0 = bbox->x0 / TILE_SIZE;
580 int iy0 = bbox->y0 / TILE_SIZE;
581 unsigned px = bbox->x0 & 63 & ~3;
582 unsigned py = bbox->y0 & 63 & ~3;
583
584 assert(iy0 == bbox->y1 / TILE_SIZE &&
585 ix0 == bbox->x1 / TILE_SIZE);
586
587 if (nr_planes == 3) {
588 if (sz < 4)
589 {
590 /* Triangle is contained in a single 4x4 stamp:
591 */
592 assert(px + 4 <= TILE_SIZE);
593 assert(py + 4 <= TILE_SIZE);
594 return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
595 setup->fs.stored,
596 LP_RAST_OP_TRIANGLE_3_4,
597 lp_rast_arg_triangle_contained(tri, px, py) );
598 }
599
600 if (sz < 16)
601 {
602 /* Triangle is contained in a single 16x16 block:
603 */
604
605 /*
606 * The 16x16 block is only 4x4 aligned, and can exceed the tile
607 * dimensions if the triangle is 16 pixels in one dimension but 4
608 * in the other. So budge the 16x16 back inside the tile.
609 */
610 px = MIN2(px, TILE_SIZE - 16);
611 py = MIN2(py, TILE_SIZE - 16);
612
613 assert(px + 16 <= TILE_SIZE);
614 assert(py + 16 <= TILE_SIZE);
615
616 return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
617 setup->fs.stored,
618 LP_RAST_OP_TRIANGLE_3_16,
619 lp_rast_arg_triangle_contained(tri, px, py) );
620 }
621 }
622 else if (nr_planes == 4 && sz < 16)
623 {
624 px = MIN2(px, TILE_SIZE - 16);
625 py = MIN2(py, TILE_SIZE - 16);
626
627 assert(px + 16 <= TILE_SIZE);
628 assert(py + 16 <= TILE_SIZE);
629
630 return lp_scene_bin_cmd_with_state(scene, ix0, iy0,
631 setup->fs.stored,
632 LP_RAST_OP_TRIANGLE_4_16,
633 lp_rast_arg_triangle_contained(tri, px, py));
634 }
635
636
637 /* Triangle is contained in a single tile:
638 */
639 return lp_scene_bin_cmd_with_state( scene, ix0, iy0, setup->fs.stored,
640 lp_rast_tri_tab[nr_planes],
641 lp_rast_arg_triangle(tri, (1<<nr_planes)-1) );
642 }
643 else
644 {
645 struct lp_rast_plane *plane = GET_PLANES(tri);
646 int c[MAX_PLANES];
647 int ei[MAX_PLANES];
648
649 int eo[MAX_PLANES];
650 int xstep[MAX_PLANES];
651 int ystep[MAX_PLANES];
652 int x, y;
653
654 int ix0 = trimmed_box.x0 / TILE_SIZE;
655 int iy0 = trimmed_box.y0 / TILE_SIZE;
656 int ix1 = trimmed_box.x1 / TILE_SIZE;
657 int iy1 = trimmed_box.y1 / TILE_SIZE;
658
659 for (i = 0; i < nr_planes; i++) {
660 c[i] = (plane[i].c +
661 plane[i].dcdy * iy0 * TILE_SIZE -
662 plane[i].dcdx * ix0 * TILE_SIZE);
663
664 ei[i] = (plane[i].dcdy -
665 plane[i].dcdx -
666 plane[i].eo) << TILE_ORDER;
667
668 eo[i] = plane[i].eo << TILE_ORDER;
669 xstep[i] = -(plane[i].dcdx << TILE_ORDER);
670 ystep[i] = plane[i].dcdy << TILE_ORDER;
671 }
672
673
674
675 /* Test tile-sized blocks against the triangle.
676 * Discard blocks fully outside the tri. If the block is fully
677 * contained inside the tri, bin an lp_rast_shade_tile command.
678 * Else, bin a lp_rast_triangle command.
679 */
680 for (y = iy0; y <= iy1; y++)
681 {
682 boolean in = FALSE; /* are we inside the triangle? */
683 int cx[MAX_PLANES];
684
685 for (i = 0; i < nr_planes; i++)
686 cx[i] = c[i];
687
688 for (x = ix0; x <= ix1; x++)
689 {
690 int out = 0;
691 int partial = 0;
692
693 for (i = 0; i < nr_planes; i++) {
694 int planeout = cx[i] + eo[i];
695 int planepartial = cx[i] + ei[i] - 1;
696 out |= (planeout >> 31);
697 partial |= (planepartial >> 31) & (1<<i);
698 }
699
700 if (out) {
701 /* do nothing */
702 if (in)
703 break; /* exiting triangle, all done with this row */
704 LP_COUNT(nr_empty_64);
705 }
706 else if (partial) {
707 /* Not trivially accepted by at least one plane -
708 * rasterize/shade partial tile
709 */
710 int count = util_bitcount(partial);
711 in = TRUE;
712
713 if (!lp_scene_bin_cmd_with_state( scene, x, y,
714 setup->fs.stored,
715 lp_rast_tri_tab[count],
716 lp_rast_arg_triangle(tri, partial) ))
717 goto fail;
718
719 LP_COUNT(nr_partially_covered_64);
720 }
721 else {
722 /* triangle covers the whole tile- shade whole tile */
723 LP_COUNT(nr_fully_covered_64);
724 in = TRUE;
725 if (!lp_setup_whole_tile(setup, &tri->inputs, x, y))
726 goto fail;
727 }
728
729 /* Iterate cx values across the region:
730 */
731 for (i = 0; i < nr_planes; i++)
732 cx[i] += xstep[i];
733 }
734
735 /* Iterate c values down the region:
736 */
737 for (i = 0; i < nr_planes; i++)
738 c[i] += ystep[i];
739 }
740 }
741
742 return TRUE;
743
744 fail:
745 /* Need to disable any partially binned triangle. This is easier
746 * than trying to locate all the triangle, shade-tile, etc,
747 * commands which may have been binned.
748 */
749 tri->inputs.disable = TRUE;
750 return FALSE;
751 }
752
753
754 /**
755 * Try to draw the triangle, restart the scene on failure.
756 */
757 static void retry_triangle_ccw( struct lp_setup_context *setup,
758 struct fixed_position* position,
759 const float (*v0)[4],
760 const float (*v1)[4],
761 const float (*v2)[4],
762 boolean front)
763 {
764 if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
765 {
766 if (!lp_setup_flush_and_restart(setup))
767 return;
768
769 if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
770 return;
771 }
772 }
773
774
775 /**
776 * Calculate fixed position data for a triangle
777 */
778 static INLINE void
779 calc_fixed_position( struct lp_setup_context *setup,
780 struct fixed_position* position,
781 const float (*v0)[4],
782 const float (*v1)[4],
783 const float (*v2)[4])
784 {
785 position->x[0] = subpixel_snap(v0[0][0] - setup->pixel_offset);
786 position->x[1] = subpixel_snap(v1[0][0] - setup->pixel_offset);
787 position->x[2] = subpixel_snap(v2[0][0] - setup->pixel_offset);
788 position->x[3] = 0;
789
790 position->y[0] = subpixel_snap(v0[0][1] - setup->pixel_offset);
791 position->y[1] = subpixel_snap(v1[0][1] - setup->pixel_offset);
792 position->y[2] = subpixel_snap(v2[0][1] - setup->pixel_offset);
793 position->y[3] = 0;
794
795 position->dx01 = position->x[0] - position->x[1];
796 position->dy01 = position->y[0] - position->y[1];
797
798 position->dx20 = position->x[2] - position->x[0];
799 position->dy20 = position->y[2] - position->y[0];
800
801 position->area = position->dx01 * position->dy20 - position->dx20 * position->dy01;
802 }
803
804
805 /**
806 * Rotate a triangle, flipping its clockwise direction,
807 * Swaps values for xy[0] and xy[1]
808 */
809 static INLINE void
810 rotate_fixed_position_01( struct fixed_position* position )
811 {
812 int x, y;
813
814 x = position->x[1];
815 y = position->y[1];
816 position->x[1] = position->x[0];
817 position->y[1] = position->y[0];
818 position->x[0] = x;
819 position->y[0] = y;
820
821 position->dx01 = -position->dx01;
822 position->dy01 = -position->dy01;
823 position->dx20 = position->x[2] - position->x[0];
824 position->dy20 = position->y[2] - position->y[0];
825
826 position->area = -position->area;
827 }
828
829
830 /**
831 * Rotate a triangle, flipping its clockwise direction,
832 * Swaps values for xy[1] and xy[2]
833 */
834 static INLINE void
835 rotate_fixed_position_12( struct fixed_position* position )
836 {
837 int x, y;
838
839 x = position->x[2];
840 y = position->y[2];
841 position->x[2] = position->x[1];
842 position->y[2] = position->y[1];
843 position->x[1] = x;
844 position->y[1] = y;
845
846 x = position->dx01;
847 y = position->dy01;
848 position->dx01 = -position->dx20;
849 position->dy01 = -position->dy20;
850 position->dx20 = -x;
851 position->dy20 = -y;
852
853 position->area = -position->area;
854 }
855
856
857 typedef void (*triangle_func_t)(struct lp_setup_context *setup,
858 const float (*v0)[4],
859 const float (*v1)[4],
860 const float (*v2)[4]);
861
862
863 /**
864 * Subdivide this triangle by bisecting edge (v0, v1).
865 * \param pv the provoking vertex (must = v0 or v1 or v2)
866 */
867 static void
868 subdiv_tri(struct lp_setup_context *setup,
869 const float (*v0)[4],
870 const float (*v1)[4],
871 const float (*v2)[4],
872 const float (*pv)[4],
873 triangle_func_t tri)
874 {
875 unsigned n = setup->fs.current.variant->shader->info.base.num_inputs + 1;
876 const struct lp_shader_input *inputs =
877 setup->fs.current.variant->shader->inputs;
878 float vmid[PIPE_MAX_ATTRIBS][4];
879 const float (*vm)[4] = (const float (*)[4]) vmid;
880 unsigned i;
881 float w0, w1, wm;
882 boolean flatshade = setup->fs.current.variant->key.flatshade;
883
884 /* find position midpoint (attrib[0] = position) */
885 vmid[0][0] = 0.5f * (v1[0][0] + v0[0][0]);
886 vmid[0][1] = 0.5f * (v1[0][1] + v0[0][1]);
887 vmid[0][2] = 0.5f * (v1[0][2] + v0[0][2]);
888 vmid[0][3] = 0.5f * (v1[0][3] + v0[0][3]);
889
890 w0 = v0[0][3];
891 w1 = v1[0][3];
892 wm = vmid[0][3];
893
894 /* interpolate other attributes */
895 for (i = 1; i < n; i++) {
896 if ((inputs[i - 1].interp == LP_INTERP_COLOR && flatshade) ||
897 inputs[i - 1].interp == LP_INTERP_CONSTANT) {
898 /* copy the provoking vertex's attribute */
899 vmid[i][0] = pv[i][0];
900 vmid[i][1] = pv[i][1];
901 vmid[i][2] = pv[i][2];
902 vmid[i][3] = pv[i][3];
903 }
904 else {
905 /* interpolate with perspective correction (for linear too) */
906 vmid[i][0] = 0.5f * (v1[i][0] * w1 + v0[i][0] * w0) / wm;
907 vmid[i][1] = 0.5f * (v1[i][1] * w1 + v0[i][1] * w0) / wm;
908 vmid[i][2] = 0.5f * (v1[i][2] * w1 + v0[i][2] * w0) / wm;
909 vmid[i][3] = 0.5f * (v1[i][3] * w1 + v0[i][3] * w0) / wm;
910 }
911 }
912
913 /* handling flat shading and first vs. last provoking vertex is a
914 * little tricky...
915 */
916 if (pv == v0) {
917 if (setup->flatshade_first) {
918 /* first vertex must be v0 or vm */
919 tri(setup, v0, vm, v2);
920 tri(setup, vm, v1, v2);
921 }
922 else {
923 /* last vertex must be v0 or vm */
924 tri(setup, vm, v2, v0);
925 tri(setup, v1, v2, vm);
926 }
927 }
928 else if (pv == v1) {
929 if (setup->flatshade_first) {
930 tri(setup, vm, v2, v0);
931 tri(setup, v1, v2, vm);
932 }
933 else {
934 tri(setup, v2, v0, vm);
935 tri(setup, v2, vm, v1);
936 }
937 }
938 else {
939 if (setup->flatshade_first) {
940 tri(setup, v2, v0, vm);
941 tri(setup, v2, vm, v1);
942 }
943 else {
944 tri(setup, v0, vm, v2);
945 tri(setup, vm, v1, v2);
946 }
947 }
948 }
949
950
951 /**
952 * Check the lengths of the edges of the triangle. If any edge is too
953 * long, subdivide the longest edge and draw two sub-triangles.
954 * Note: this may be called recursively.
955 * \return TRUE if triangle was subdivided, FALSE otherwise
956 */
957 static boolean
958 check_subdivide_triangle(struct lp_setup_context *setup,
959 const float (*v0)[4],
960 const float (*v1)[4],
961 const float (*v2)[4],
962 triangle_func_t tri)
963 {
964 const float maxLen = 2048.0f; /* longest permissible edge, in pixels */
965 float dx10, dy10, len10;
966 float dx21, dy21, len21;
967 float dx02, dy02, len02;
968 const float (*pv)[4] = setup->flatshade_first ? v0 : v2;
969
970 /* compute lengths of triangle edges, squared */
971 dx10 = v1[0][0] - v0[0][0];
972 dy10 = v1[0][1] - v0[0][1];
973 len10 = dx10 * dx10 + dy10 * dy10;
974
975 dx21 = v2[0][0] - v1[0][0];
976 dy21 = v2[0][1] - v1[0][1];
977 len21 = dx21 * dx21 + dy21 * dy21;
978
979 dx02 = v0[0][0] - v2[0][0];
980 dy02 = v0[0][1] - v2[0][1];
981 len02 = dx02 * dx02 + dy02 * dy02;
982
983 /* Look for longest the edge that's longer than maxLen. If we find
984 * such an edge, split the triangle using the midpoint of that edge.
985 * Note: it's important to split the longest edge, not just any edge
986 * that's longer than maxLen. Otherwise, we can get into a degenerate
987 * situation and recurse indefinitely.
988 */
989 if (len10 > maxLen * maxLen &&
990 len10 >= len21 &&
991 len10 >= len02) {
992 /* subdivide v0, v1 edge */
993 subdiv_tri(setup, v0, v1, v2, pv, tri);
994 return TRUE;
995 }
996
997 if (len21 > maxLen * maxLen &&
998 len21 >= len10 &&
999 len21 >= len02) {
1000 /* subdivide v1, v2 edge */
1001 subdiv_tri(setup, v1, v2, v0, pv, tri);
1002 return TRUE;
1003 }
1004
1005 if (len02 > maxLen * maxLen &&
1006 len02 >= len21 &&
1007 len02 >= len10) {
1008 /* subdivide v2, v0 edge */
1009 subdiv_tri(setup, v2, v0, v1, pv, tri);
1010 return TRUE;
1011 }
1012
1013 return FALSE;
1014 }
1015
1016
1017 /**
1018 * Draw triangle if it's CW, cull otherwise.
1019 */
1020 static void triangle_cw( struct lp_setup_context *setup,
1021 const float (*v0)[4],
1022 const float (*v1)[4],
1023 const float (*v2)[4] )
1024 {
1025 struct fixed_position position;
1026
1027 if (setup->subdivide_large_triangles &&
1028 check_subdivide_triangle(setup, v0, v1, v2, triangle_cw))
1029 return;
1030
1031 calc_fixed_position(setup, &position, v0, v1, v2);
1032
1033 if (position.area < 0) {
1034 if (setup->flatshade_first) {
1035 rotate_fixed_position_12(&position);
1036 retry_triangle_ccw(setup, &position, v0, v2, v1, !setup->ccw_is_frontface);
1037 } else {
1038 rotate_fixed_position_01(&position);
1039 retry_triangle_ccw(setup, &position, v1, v0, v2, !setup->ccw_is_frontface);
1040 }
1041 }
1042 }
1043
1044
1045 static void triangle_ccw( struct lp_setup_context *setup,
1046 const float (*v0)[4],
1047 const float (*v1)[4],
1048 const float (*v2)[4])
1049 {
1050 struct fixed_position position;
1051
1052 if (setup->subdivide_large_triangles &&
1053 check_subdivide_triangle(setup, v0, v1, v2, triangle_ccw))
1054 return;
1055
1056 calc_fixed_position(setup, &position, v0, v1, v2);
1057
1058 if (position.area > 0)
1059 retry_triangle_ccw(setup, &position, v0, v1, v2, setup->ccw_is_frontface);
1060 }
1061
1062 /**
1063 * Draw triangle whether it's CW or CCW.
1064 */
1065 static void triangle_both( struct lp_setup_context *setup,
1066 const float (*v0)[4],
1067 const float (*v1)[4],
1068 const float (*v2)[4] )
1069 {
1070 struct fixed_position position;
1071
1072 if (setup->subdivide_large_triangles &&
1073 check_subdivide_triangle(setup, v0, v1, v2, triangle_both))
1074 return;
1075
1076 calc_fixed_position(setup, &position, v0, v1, v2);
1077
1078 if (0) {
1079 assert(!util_is_inf_or_nan(v0[0][0]));
1080 assert(!util_is_inf_or_nan(v0[0][1]));
1081 assert(!util_is_inf_or_nan(v1[0][0]));
1082 assert(!util_is_inf_or_nan(v1[0][1]));
1083 assert(!util_is_inf_or_nan(v2[0][0]));
1084 assert(!util_is_inf_or_nan(v2[0][1]));
1085 }
1086
1087 if (position.area > 0)
1088 retry_triangle_ccw( setup, &position, v0, v1, v2, setup->ccw_is_frontface );
1089 else if (position.area < 0) {
1090 if (setup->flatshade_first) {
1091 rotate_fixed_position_12( &position );
1092 retry_triangle_ccw( setup, &position, v0, v2, v1, !setup->ccw_is_frontface );
1093 } else {
1094 rotate_fixed_position_01( &position );
1095 retry_triangle_ccw( setup, &position, v1, v0, v2, !setup->ccw_is_frontface );
1096 }
1097 }
1098 }
1099
1100
1101 static void triangle_nop( struct lp_setup_context *setup,
1102 const float (*v0)[4],
1103 const float (*v1)[4],
1104 const float (*v2)[4] )
1105 {
1106 }
1107
1108
1109 void
1110 lp_setup_choose_triangle( struct lp_setup_context *setup )
1111 {
1112 switch (setup->cullmode) {
1113 case PIPE_FACE_NONE:
1114 setup->triangle = triangle_both;
1115 break;
1116 case PIPE_FACE_BACK:
1117 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
1118 break;
1119 case PIPE_FACE_FRONT:
1120 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
1121 break;
1122 default:
1123 setup->triangle = triangle_nop;
1124 break;
1125 }
1126 }