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