1e3a7501ed5c9277a47af55ea536362fb4651cba
[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 int32_t dx01;
72 int32_t dy01;
73 int32_t dx20;
74 int32_t dy20;
75 int64_t area;
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 if (1) {
394 __m128i vertx, verty;
395 __m128i shufx, shufy;
396 __m128i dcdx, dcdy;
397 __m128i cdx02, cdx13, cdy02, cdy13, c02, c13;
398 __m128i c01, c23, unused;
399 __m128i dcdx_neg_mask;
400 __m128i dcdy_neg_mask;
401 __m128i dcdx_zero_mask;
402 __m128i top_left_flag, c_dec;
403 __m128i eo, p0, p1, p2;
404 __m128i zero = _mm_setzero_si128();
405
406 vertx = _mm_load_si128((__m128i *)position->x); /* vertex x coords */
407 verty = _mm_load_si128((__m128i *)position->y); /* vertex y coords */
408
409 shufx = _mm_shuffle_epi32(vertx, _MM_SHUFFLE(3,0,2,1));
410 shufy = _mm_shuffle_epi32(verty, _MM_SHUFFLE(3,0,2,1));
411
412 dcdx = _mm_sub_epi32(verty, shufy);
413 dcdy = _mm_sub_epi32(vertx, shufx);
414
415 dcdx_neg_mask = _mm_srai_epi32(dcdx, 31);
416 dcdx_zero_mask = _mm_cmpeq_epi32(dcdx, zero);
417 dcdy_neg_mask = _mm_srai_epi32(dcdy, 31);
418
419 top_left_flag = _mm_set1_epi32((setup->bottom_edge_rule == 0) ? ~0 : 0);
420
421 c_dec = _mm_or_si128(dcdx_neg_mask,
422 _mm_and_si128(dcdx_zero_mask,
423 _mm_xor_si128(dcdy_neg_mask,
424 top_left_flag)));
425
426 /*
427 * 64 bit arithmetic.
428 * Note we need _signed_ mul (_mm_mul_epi32) which we emulate.
429 */
430 cdx02 = mm_mullohi_epi32(dcdx, vertx, &cdx13);
431 cdy02 = mm_mullohi_epi32(dcdy, verty, &cdy13);
432 c02 = _mm_sub_epi64(cdx02, cdy02);
433 c13 = _mm_sub_epi64(cdx13, cdy13);
434 c02 = _mm_sub_epi64(c02, _mm_shuffle_epi32(c_dec,
435 _MM_SHUFFLE(2,2,0,0)));
436 c13 = _mm_sub_epi64(c13, _mm_shuffle_epi32(c_dec,
437 _MM_SHUFFLE(3,3,1,1)));
438
439 /*
440 * Useful for very small fbs/tris (or fewer subpixel bits) only:
441 * c = _mm_sub_epi32(mm_mullo_epi32(dcdx, vertx),
442 * mm_mullo_epi32(dcdy, verty));
443 *
444 * c = _mm_sub_epi32(c, c_dec);
445 */
446
447 /* Scale up to match c:
448 */
449 dcdx = _mm_slli_epi32(dcdx, FIXED_ORDER);
450 dcdy = _mm_slli_epi32(dcdy, FIXED_ORDER);
451
452 /*
453 * Calculate trivial reject values:
454 * Note eo cannot overflow even if dcdx/dcdy would already have
455 * 31 bits (which they shouldn't have). This is because eo
456 * is never negative (albeit if we rely on that need to be careful...)
457 */
458 eo = _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask, dcdy),
459 _mm_and_si128(dcdx_neg_mask, dcdx));
460
461 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
462
463 /*
464 * Pointless transpose which gets undone immediately in
465 * rasterization.
466 * It is actually difficult to do away with it - would essentially
467 * need GET_PLANES_DX, GET_PLANES_DY etc., but the calculations
468 * for this then would need to depend on the number of planes.
469 * The transpose is quite special here due to c being 64bit...
470 * The store has to be unaligned (unless we'd make the plane size
471 * a multiple of 128), and of course storing eo separately...
472 */
473 c01 = _mm_unpacklo_epi64(c02, c13);
474 c23 = _mm_unpackhi_epi64(c02, c13);
475 transpose2_64_2_32(&c01, &c23, &dcdx, &dcdy,
476 &p0, &p1, &p2, &unused);
477 _mm_storeu_si128((__m128i *)&plane[0], p0);
478 plane[0].eo = (uint32_t)_mm_cvtsi128_si32(eo);
479 _mm_storeu_si128((__m128i *)&plane[1], p1);
480 eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(3,2,0,1));
481 plane[1].eo = (uint32_t)_mm_cvtsi128_si32(eo);
482 _mm_storeu_si128((__m128i *)&plane[2], p2);
483 eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(0,0,0,2));
484 plane[2].eo = (uint32_t)_mm_cvtsi128_si32(eo);
485 } else
486 #elif defined(_ARCH_PWR8) && defined(PIPE_ARCH_LITTLE_ENDIAN)
487 /*
488 * XXX this code is effectively disabled for all practical purposes,
489 * as the allowed fb size is tiny if FIXED_ORDER is 8.
490 */
491 if (setup->fb.width <= MAX_FIXED_LENGTH32 &&
492 setup->fb.height <= MAX_FIXED_LENGTH32 &&
493 (bbox.x1 - bbox.x0) <= MAX_FIXED_LENGTH32 &&
494 (bbox.y1 - bbox.y0) <= MAX_FIXED_LENGTH32) {
495 unsigned int bottom_edge;
496 __m128i vertx, verty;
497 __m128i shufx, shufy;
498 __m128i dcdx, dcdy, c;
499 __m128i unused;
500 __m128i dcdx_neg_mask;
501 __m128i dcdy_neg_mask;
502 __m128i dcdx_zero_mask;
503 __m128i top_left_flag;
504 __m128i c_inc_mask, c_inc;
505 __m128i eo, p0, p1, p2;
506 __m128i_union vshuf_mask;
507 __m128i zero = vec_splats((unsigned char) 0);
508 PIPE_ALIGN_VAR(16) int32_t temp_vec[4];
509
510 #ifdef PIPE_ARCH_LITTLE_ENDIAN
511 vshuf_mask.i[0] = 0x07060504;
512 vshuf_mask.i[1] = 0x0B0A0908;
513 vshuf_mask.i[2] = 0x03020100;
514 vshuf_mask.i[3] = 0x0F0E0D0C;
515 #else
516 vshuf_mask.i[0] = 0x00010203;
517 vshuf_mask.i[1] = 0x0C0D0E0F;
518 vshuf_mask.i[2] = 0x04050607;
519 vshuf_mask.i[3] = 0x08090A0B;
520 #endif
521
522 /* vertex x coords */
523 vertx = vec_load_si128((const uint32_t *) position->x);
524 /* vertex y coords */
525 verty = vec_load_si128((const uint32_t *) position->y);
526
527 shufx = vec_perm (vertx, vertx, vshuf_mask.m128i);
528 shufy = vec_perm (verty, verty, vshuf_mask.m128i);
529
530 dcdx = vec_sub_epi32(verty, shufy);
531 dcdy = vec_sub_epi32(vertx, shufx);
532
533 dcdx_neg_mask = vec_srai_epi32(dcdx, 31);
534 dcdx_zero_mask = vec_cmpeq_epi32(dcdx, zero);
535 dcdy_neg_mask = vec_srai_epi32(dcdy, 31);
536
537 bottom_edge = (setup->bottom_edge_rule == 0) ? ~0 : 0;
538 top_left_flag = (__m128i) vec_splats(bottom_edge);
539
540 c_inc_mask = vec_or(dcdx_neg_mask,
541 vec_and(dcdx_zero_mask,
542 vec_xor(dcdy_neg_mask,
543 top_left_flag)));
544
545 c_inc = vec_srli_epi32(c_inc_mask, 31);
546
547 c = vec_sub_epi32(vec_mullo_epi32(dcdx, vertx),
548 vec_mullo_epi32(dcdy, verty));
549
550 c = vec_add_epi32(c, c_inc);
551
552 /* Scale up to match c:
553 */
554 dcdx = vec_slli_epi32(dcdx, FIXED_ORDER);
555 dcdy = vec_slli_epi32(dcdy, FIXED_ORDER);
556
557 /* Calculate trivial reject values:
558 */
559 eo = vec_sub_epi32(vec_andnot_si128(dcdy_neg_mask, dcdy),
560 vec_and(dcdx_neg_mask, dcdx));
561
562 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
563
564 /* Pointless transpose which gets undone immediately in
565 * rasterization:
566 */
567 transpose4_epi32(&c, &dcdx, &dcdy, &eo,
568 &p0, &p1, &p2, &unused);
569
570 #define STORE_PLANE(plane, vec) do { \
571 vec_store_si128((uint32_t *)&temp_vec, vec); \
572 plane.c = (int64_t)temp_vec[0]; \
573 plane.dcdx = temp_vec[1]; \
574 plane.dcdy = temp_vec[2]; \
575 plane.eo = temp_vec[3]; \
576 } while(0)
577
578 STORE_PLANE(plane[0], p0);
579 STORE_PLANE(plane[1], p1);
580 STORE_PLANE(plane[2], p2);
581 #undef STORE_PLANE
582 } else
583 #endif
584 {
585 int i;
586 plane[0].dcdy = position->dx01;
587 plane[1].dcdy = position->x[1] - position->x[2];
588 plane[2].dcdy = position->dx20;
589 plane[0].dcdx = position->dy01;
590 plane[1].dcdx = position->y[1] - position->y[2];
591 plane[2].dcdx = position->dy20;
592
593 for (i = 0; i < 3; i++) {
594 /* half-edge constants, will be iterated over the whole render
595 * target.
596 */
597 plane[i].c = IMUL64(plane[i].dcdx, position->x[i]) -
598 IMUL64(plane[i].dcdy, position->y[i]);
599
600 /* correct for top-left vs. bottom-left fill convention.
601 */
602 if (plane[i].dcdx < 0) {
603 /* both fill conventions want this - adjust for left edges */
604 plane[i].c++;
605 }
606 else if (plane[i].dcdx == 0) {
607 if (setup->bottom_edge_rule == 0){
608 /* correct for top-left fill convention:
609 */
610 if (plane[i].dcdy > 0) plane[i].c++;
611 }
612 else {
613 /* correct for bottom-left fill convention:
614 */
615 if (plane[i].dcdy < 0) plane[i].c++;
616 }
617 }
618
619 /* Scale up to match c:
620 */
621 assert((plane[i].dcdx << FIXED_ORDER) >> FIXED_ORDER == plane[i].dcdx);
622 assert((plane[i].dcdy << FIXED_ORDER) >> FIXED_ORDER == plane[i].dcdy);
623 plane[i].dcdx <<= FIXED_ORDER;
624 plane[i].dcdy <<= FIXED_ORDER;
625
626 /* find trivial reject offsets for each edge for a single-pixel
627 * sized block. These will be scaled up at each recursive level to
628 * match the active blocksize. Scaling in this way works best if
629 * the blocks are square.
630 */
631 plane[i].eo = 0;
632 if (plane[i].dcdx < 0) plane[i].eo -= plane[i].dcdx;
633 if (plane[i].dcdy > 0) plane[i].eo += plane[i].dcdy;
634 }
635 }
636
637 if (0) {
638 debug_printf("p0: %"PRIx64"/%08x/%08x/%08x\n",
639 plane[0].c,
640 plane[0].dcdx,
641 plane[0].dcdy,
642 plane[0].eo);
643
644 debug_printf("p1: %"PRIx64"/%08x/%08x/%08x\n",
645 plane[1].c,
646 plane[1].dcdx,
647 plane[1].dcdy,
648 plane[1].eo);
649
650 debug_printf("p2: %"PRIx64"/%08x/%08x/%08x\n",
651 plane[2].c,
652 plane[2].dcdx,
653 plane[2].dcdy,
654 plane[2].eo);
655 }
656
657
658 /*
659 * When rasterizing scissored tris, use the intersection of the
660 * triangle bounding box and the scissor rect to generate the
661 * scissor planes.
662 *
663 * This permits us to cut off the triangle "tails" that are present
664 * in the intermediate recursive levels caused when two of the
665 * triangles edges don't diverge quickly enough to trivially reject
666 * exterior blocks from the triangle.
667 *
668 * It's not really clear if it's worth worrying about these tails,
669 * but since we generate the planes for each scissored tri, it's
670 * free to trim them in this case.
671 *
672 * Note that otherwise, the scissor planes only vary in 'C' value,
673 * and even then only on state-changes. Could alternatively store
674 * these planes elsewhere.
675 */
676 if (nr_planes == 7) {
677 const struct u_rect *scissor = &setup->scissors[viewport_index];
678
679 plane[3].dcdx = -1 << 8;
680 plane[3].dcdy = 0;
681 plane[3].c = (1-scissor->x0) << 8;
682 plane[3].eo = 1 << 8;
683
684 plane[4].dcdx = 1 << 8;
685 plane[4].dcdy = 0;
686 plane[4].c = (scissor->x1+1) << 8;
687 plane[4].eo = 0;
688
689 plane[5].dcdx = 0;
690 plane[5].dcdy = 1 << 8;
691 plane[5].c = (1-scissor->y0) << 8;
692 plane[5].eo = 1 << 8;
693
694 plane[6].dcdx = 0;
695 plane[6].dcdy = -1 << 8;
696 plane[6].c = (scissor->y1+1) << 8;
697 plane[6].eo = 0;
698 }
699
700 return lp_setup_bin_triangle(setup, tri, &bbox, nr_planes, viewport_index);
701 }
702
703 /*
704 * Round to nearest less or equal power of two of the input.
705 *
706 * Undefined if no bit set exists, so code should check against 0 first.
707 */
708 static inline uint32_t
709 floor_pot(uint32_t n)
710 {
711 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
712 if (n == 0)
713 return 0;
714
715 __asm__("bsr %1,%0"
716 : "=r" (n)
717 : "rm" (n));
718 return 1 << n;
719 #else
720 n |= (n >> 1);
721 n |= (n >> 2);
722 n |= (n >> 4);
723 n |= (n >> 8);
724 n |= (n >> 16);
725 return n - (n >> 1);
726 #endif
727 }
728
729
730 boolean
731 lp_setup_bin_triangle( struct lp_setup_context *setup,
732 struct lp_rast_triangle *tri,
733 const struct u_rect *bbox,
734 int nr_planes,
735 unsigned viewport_index )
736 {
737 struct lp_scene *scene = setup->scene;
738 struct u_rect trimmed_box = *bbox;
739 int i;
740 /* What is the largest power-of-two boundary this triangle crosses:
741 */
742 int dx = floor_pot((bbox->x0 ^ bbox->x1) |
743 (bbox->y0 ^ bbox->y1));
744
745 /* The largest dimension of the rasterized area of the triangle
746 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
747 */
748 int max_sz = ((bbox->x1 - (bbox->x0 & ~3)) |
749 (bbox->y1 - (bbox->y0 & ~3)));
750 int sz = floor_pot(max_sz);
751 boolean use_32bits = max_sz <= MAX_FIXED_LENGTH32;
752
753 /* Now apply scissor, etc to the bounding box. Could do this
754 * earlier, but it confuses the logic for tri-16 and would force
755 * the rasterizer to also respect scissor, etc, just for the rare
756 * cases where a small triangle extends beyond the scissor.
757 */
758 u_rect_find_intersection(&setup->draw_regions[viewport_index],
759 &trimmed_box);
760
761 /* Determine which tile(s) intersect the triangle's bounding box
762 */
763 if (dx < TILE_SIZE)
764 {
765 int ix0 = bbox->x0 / TILE_SIZE;
766 int iy0 = bbox->y0 / TILE_SIZE;
767 unsigned px = bbox->x0 & 63 & ~3;
768 unsigned py = bbox->y0 & 63 & ~3;
769
770 assert(iy0 == bbox->y1 / TILE_SIZE &&
771 ix0 == bbox->x1 / TILE_SIZE);
772
773 if (nr_planes == 3) {
774 if (sz < 4)
775 {
776 /* Triangle is contained in a single 4x4 stamp:
777 */
778 assert(px + 4 <= TILE_SIZE);
779 assert(py + 4 <= TILE_SIZE);
780 return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
781 setup->fs.stored,
782 use_32bits ?
783 LP_RAST_OP_TRIANGLE_32_3_4 :
784 LP_RAST_OP_TRIANGLE_3_4,
785 lp_rast_arg_triangle_contained(tri, px, py) );
786 }
787
788 if (sz < 16)
789 {
790 /* Triangle is contained in a single 16x16 block:
791 */
792
793 /*
794 * The 16x16 block is only 4x4 aligned, and can exceed the tile
795 * dimensions if the triangle is 16 pixels in one dimension but 4
796 * in the other. So budge the 16x16 back inside the tile.
797 */
798 px = MIN2(px, TILE_SIZE - 16);
799 py = MIN2(py, TILE_SIZE - 16);
800
801 assert(px + 16 <= TILE_SIZE);
802 assert(py + 16 <= TILE_SIZE);
803
804 return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
805 setup->fs.stored,
806 use_32bits ?
807 LP_RAST_OP_TRIANGLE_32_3_16 :
808 LP_RAST_OP_TRIANGLE_3_16,
809 lp_rast_arg_triangle_contained(tri, px, py) );
810 }
811 }
812 else if (nr_planes == 4 && sz < 16)
813 {
814 px = MIN2(px, TILE_SIZE - 16);
815 py = MIN2(py, TILE_SIZE - 16);
816
817 assert(px + 16 <= TILE_SIZE);
818 assert(py + 16 <= TILE_SIZE);
819
820 return lp_scene_bin_cmd_with_state(scene, ix0, iy0,
821 setup->fs.stored,
822 use_32bits ?
823 LP_RAST_OP_TRIANGLE_32_4_16 :
824 LP_RAST_OP_TRIANGLE_4_16,
825 lp_rast_arg_triangle_contained(tri, px, py));
826 }
827
828
829 /* Triangle is contained in a single tile:
830 */
831 return lp_scene_bin_cmd_with_state(
832 scene, ix0, iy0, setup->fs.stored,
833 use_32bits ? lp_rast_32_tri_tab[nr_planes] : lp_rast_tri_tab[nr_planes],
834 lp_rast_arg_triangle(tri, (1<<nr_planes)-1));
835 }
836 else
837 {
838 struct lp_rast_plane *plane = GET_PLANES(tri);
839 int64_t c[MAX_PLANES];
840 int64_t ei[MAX_PLANES];
841
842 int64_t eo[MAX_PLANES];
843 int64_t xstep[MAX_PLANES];
844 int64_t ystep[MAX_PLANES];
845 int x, y;
846
847 int ix0 = trimmed_box.x0 / TILE_SIZE;
848 int iy0 = trimmed_box.y0 / TILE_SIZE;
849 int ix1 = trimmed_box.x1 / TILE_SIZE;
850 int iy1 = trimmed_box.y1 / TILE_SIZE;
851
852 for (i = 0; i < nr_planes; i++) {
853 c[i] = (plane[i].c +
854 IMUL64(plane[i].dcdy, iy0) * TILE_SIZE -
855 IMUL64(plane[i].dcdx, ix0) * TILE_SIZE);
856
857 ei[i] = (plane[i].dcdy -
858 plane[i].dcdx -
859 (int64_t)plane[i].eo) << TILE_ORDER;
860
861 eo[i] = (int64_t)plane[i].eo << TILE_ORDER;
862 xstep[i] = -(((int64_t)plane[i].dcdx) << TILE_ORDER);
863 ystep[i] = ((int64_t)plane[i].dcdy) << TILE_ORDER;
864 }
865
866
867
868 /* Test tile-sized blocks against the triangle.
869 * Discard blocks fully outside the tri. If the block is fully
870 * contained inside the tri, bin an lp_rast_shade_tile command.
871 * Else, bin a lp_rast_triangle command.
872 */
873 for (y = iy0; y <= iy1; y++)
874 {
875 boolean in = FALSE; /* are we inside the triangle? */
876 int64_t cx[MAX_PLANES];
877
878 for (i = 0; i < nr_planes; i++)
879 cx[i] = c[i];
880
881 for (x = ix0; x <= ix1; x++)
882 {
883 int out = 0;
884 int partial = 0;
885
886 for (i = 0; i < nr_planes; i++) {
887 int64_t planeout = cx[i] + eo[i];
888 int64_t planepartial = cx[i] + ei[i] - 1;
889 out |= (int) (planeout >> 63);
890 partial |= ((int) (planepartial >> 63)) & (1<<i);
891 }
892
893 if (out) {
894 /* do nothing */
895 if (in)
896 break; /* exiting triangle, all done with this row */
897 LP_COUNT(nr_empty_64);
898 }
899 else if (partial) {
900 /* Not trivially accepted by at least one plane -
901 * rasterize/shade partial tile
902 */
903 int count = util_bitcount(partial);
904 in = TRUE;
905
906 if (!lp_scene_bin_cmd_with_state( scene, x, y,
907 setup->fs.stored,
908 use_32bits ?
909 lp_rast_32_tri_tab[count] :
910 lp_rast_tri_tab[count],
911 lp_rast_arg_triangle(tri, partial) ))
912 goto fail;
913
914 LP_COUNT(nr_partially_covered_64);
915 }
916 else {
917 /* triangle covers the whole tile- shade whole tile */
918 LP_COUNT(nr_fully_covered_64);
919 in = TRUE;
920 if (!lp_setup_whole_tile(setup, &tri->inputs, x, y))
921 goto fail;
922 }
923
924 /* Iterate cx values across the region: */
925 for (i = 0; i < nr_planes; i++)
926 cx[i] += xstep[i];
927 }
928
929 /* Iterate c values down the region: */
930 for (i = 0; i < nr_planes; i++)
931 c[i] += ystep[i];
932 }
933 }
934
935 return TRUE;
936
937 fail:
938 /* Need to disable any partially binned triangle. This is easier
939 * than trying to locate all the triangle, shade-tile, etc,
940 * commands which may have been binned.
941 */
942 tri->inputs.disable = TRUE;
943 return FALSE;
944 }
945
946
947 /**
948 * Try to draw the triangle, restart the scene on failure.
949 */
950 static void retry_triangle_ccw( struct lp_setup_context *setup,
951 struct fixed_position* position,
952 const float (*v0)[4],
953 const float (*v1)[4],
954 const float (*v2)[4],
955 boolean front)
956 {
957 if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
958 {
959 if (!lp_setup_flush_and_restart(setup))
960 return;
961
962 if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
963 return;
964 }
965 }
966
967 /**
968 * Calculate fixed position data for a triangle
969 * It is unfortunate we need to do that here (as we need area
970 * calculated in fixed point), as there's quite some code duplication
971 * to what is done in the jit setup prog.
972 */
973 static inline void
974 calc_fixed_position(struct lp_setup_context *setup,
975 struct fixed_position* position,
976 const float (*v0)[4],
977 const float (*v1)[4],
978 const float (*v2)[4])
979 {
980 /*
981 * The rounding may not be quite the same with PIPE_ARCH_SSE
982 * (util_iround right now only does nearest/even on x87,
983 * otherwise nearest/away-from-zero).
984 * Both should be acceptable, I think.
985 */
986 #if defined(PIPE_ARCH_SSE)
987 __m128 v0r, v1r;
988 __m128 vxy0xy2, vxy1xy0;
989 __m128i vxy0xy2i, vxy1xy0i;
990 __m128i dxdy0120, x0x2y0y2, x1x0y1y0, x0120, y0120;
991 __m128 pix_offset = _mm_set1_ps(setup->pixel_offset);
992 __m128 fixed_one = _mm_set1_ps((float)FIXED_ONE);
993 v0r = _mm_castpd_ps(_mm_load_sd((double *)v0[0]));
994 vxy0xy2 = _mm_loadh_pi(v0r, (__m64 *)v2[0]);
995 v1r = _mm_castpd_ps(_mm_load_sd((double *)v1[0]));
996 vxy1xy0 = _mm_movelh_ps(v1r, vxy0xy2);
997 vxy0xy2 = _mm_sub_ps(vxy0xy2, pix_offset);
998 vxy1xy0 = _mm_sub_ps(vxy1xy0, pix_offset);
999 vxy0xy2 = _mm_mul_ps(vxy0xy2, fixed_one);
1000 vxy1xy0 = _mm_mul_ps(vxy1xy0, fixed_one);
1001 vxy0xy2i = _mm_cvtps_epi32(vxy0xy2);
1002 vxy1xy0i = _mm_cvtps_epi32(vxy1xy0);
1003 dxdy0120 = _mm_sub_epi32(vxy0xy2i, vxy1xy0i);
1004 _mm_store_si128((__m128i *)&position->dx01, dxdy0120);
1005 /*
1006 * For the mul, would need some more shuffles, plus emulation
1007 * for the signed mul (without sse41), so don't bother.
1008 */
1009 x0x2y0y2 = _mm_shuffle_epi32(vxy0xy2i, _MM_SHUFFLE(3,1,2,0));
1010 x1x0y1y0 = _mm_shuffle_epi32(vxy1xy0i, _MM_SHUFFLE(3,1,2,0));
1011 x0120 = _mm_unpacklo_epi32(x0x2y0y2, x1x0y1y0);
1012 y0120 = _mm_unpackhi_epi32(x0x2y0y2, x1x0y1y0);
1013 _mm_store_si128((__m128i *)&position->x[0], x0120);
1014 _mm_store_si128((__m128i *)&position->y[0], y0120);
1015
1016 #else
1017 position->x[0] = subpixel_snap(v0[0][0] - setup->pixel_offset);
1018 position->x[1] = subpixel_snap(v1[0][0] - setup->pixel_offset);
1019 position->x[2] = subpixel_snap(v2[0][0] - setup->pixel_offset);
1020 position->x[3] = 0; // should be unused
1021
1022 position->y[0] = subpixel_snap(v0[0][1] - setup->pixel_offset);
1023 position->y[1] = subpixel_snap(v1[0][1] - setup->pixel_offset);
1024 position->y[2] = subpixel_snap(v2[0][1] - setup->pixel_offset);
1025 position->y[3] = 0; // should be unused
1026
1027 position->dx01 = position->x[0] - position->x[1];
1028 position->dy01 = position->y[0] - position->y[1];
1029
1030 position->dx20 = position->x[2] - position->x[0];
1031 position->dy20 = position->y[2] - position->y[0];
1032 #endif
1033
1034 position->area = IMUL64(position->dx01, position->dy20) -
1035 IMUL64(position->dx20, position->dy01);
1036 }
1037
1038
1039 /**
1040 * Rotate a triangle, flipping its clockwise direction,
1041 * Swaps values for xy[0] and xy[1]
1042 */
1043 static inline void
1044 rotate_fixed_position_01( struct fixed_position* position )
1045 {
1046 int x, y;
1047
1048 x = position->x[1];
1049 y = position->y[1];
1050 position->x[1] = position->x[0];
1051 position->y[1] = position->y[0];
1052 position->x[0] = x;
1053 position->y[0] = y;
1054
1055 position->dx01 = -position->dx01;
1056 position->dy01 = -position->dy01;
1057 position->dx20 = position->x[2] - position->x[0];
1058 position->dy20 = position->y[2] - position->y[0];
1059
1060 position->area = -position->area;
1061 }
1062
1063
1064 /**
1065 * Rotate a triangle, flipping its clockwise direction,
1066 * Swaps values for xy[1] and xy[2]
1067 */
1068 static inline void
1069 rotate_fixed_position_12( struct fixed_position* position )
1070 {
1071 int x, y;
1072
1073 x = position->x[2];
1074 y = position->y[2];
1075 position->x[2] = position->x[1];
1076 position->y[2] = position->y[1];
1077 position->x[1] = x;
1078 position->y[1] = y;
1079
1080 x = position->dx01;
1081 y = position->dy01;
1082 position->dx01 = -position->dx20;
1083 position->dy01 = -position->dy20;
1084 position->dx20 = -x;
1085 position->dy20 = -y;
1086
1087 position->area = -position->area;
1088 }
1089
1090
1091 /**
1092 * Draw triangle if it's CW, cull otherwise.
1093 */
1094 static void triangle_cw(struct lp_setup_context *setup,
1095 const float (*v0)[4],
1096 const float (*v1)[4],
1097 const float (*v2)[4])
1098 {
1099 PIPE_ALIGN_VAR(16) struct fixed_position position;
1100
1101 calc_fixed_position(setup, &position, v0, v1, v2);
1102
1103 if (position.area < 0) {
1104 if (setup->flatshade_first) {
1105 rotate_fixed_position_12(&position);
1106 retry_triangle_ccw(setup, &position, v0, v2, v1, !setup->ccw_is_frontface);
1107 } else {
1108 rotate_fixed_position_01(&position);
1109 retry_triangle_ccw(setup, &position, v1, v0, v2, !setup->ccw_is_frontface);
1110 }
1111 }
1112 }
1113
1114
1115 static void triangle_ccw(struct lp_setup_context *setup,
1116 const float (*v0)[4],
1117 const float (*v1)[4],
1118 const float (*v2)[4])
1119 {
1120 PIPE_ALIGN_VAR(16) struct fixed_position position;
1121
1122 calc_fixed_position(setup, &position, v0, v1, v2);
1123
1124 if (position.area > 0)
1125 retry_triangle_ccw(setup, &position, v0, v1, v2, setup->ccw_is_frontface);
1126 }
1127
1128 /**
1129 * Draw triangle whether it's CW or CCW.
1130 */
1131 static void triangle_both(struct lp_setup_context *setup,
1132 const float (*v0)[4],
1133 const float (*v1)[4],
1134 const float (*v2)[4])
1135 {
1136 PIPE_ALIGN_VAR(16) struct fixed_position position;
1137 struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1138
1139 if (lp_context->active_statistics_queries &&
1140 !llvmpipe_rasterization_disabled(lp_context)) {
1141 lp_context->pipeline_statistics.c_primitives++;
1142 }
1143
1144 calc_fixed_position(setup, &position, v0, v1, v2);
1145
1146 if (0) {
1147 assert(!util_is_inf_or_nan(v0[0][0]));
1148 assert(!util_is_inf_or_nan(v0[0][1]));
1149 assert(!util_is_inf_or_nan(v1[0][0]));
1150 assert(!util_is_inf_or_nan(v1[0][1]));
1151 assert(!util_is_inf_or_nan(v2[0][0]));
1152 assert(!util_is_inf_or_nan(v2[0][1]));
1153 }
1154
1155 if (position.area > 0)
1156 retry_triangle_ccw( setup, &position, v0, v1, v2, setup->ccw_is_frontface );
1157 else if (position.area < 0) {
1158 if (setup->flatshade_first) {
1159 rotate_fixed_position_12( &position );
1160 retry_triangle_ccw( setup, &position, v0, v2, v1, !setup->ccw_is_frontface );
1161 } else {
1162 rotate_fixed_position_01( &position );
1163 retry_triangle_ccw( setup, &position, v1, v0, v2, !setup->ccw_is_frontface );
1164 }
1165 }
1166 }
1167
1168
1169 static void triangle_nop( struct lp_setup_context *setup,
1170 const float (*v0)[4],
1171 const float (*v1)[4],
1172 const float (*v2)[4] )
1173 {
1174 }
1175
1176
1177 void
1178 lp_setup_choose_triangle( struct lp_setup_context *setup )
1179 {
1180 switch (setup->cullmode) {
1181 case PIPE_FACE_NONE:
1182 setup->triangle = triangle_both;
1183 break;
1184 case PIPE_FACE_BACK:
1185 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
1186 break;
1187 case PIPE_FACE_FRONT:
1188 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
1189 break;
1190 default:
1191 setup->triangle = triangle_nop;
1192 break;
1193 }
1194 }