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