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