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