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