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