1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Binning code for triangles
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_rect.h"
35 #include "util/u_sse.h"
37 #include "lp_setup_context.h"
39 #include "lp_state_fs.h"
40 #include "lp_state_setup.h"
41 #include "lp_context.h"
45 #define NUM_CHANNELS 4
47 #if defined(PIPE_ARCH_SSE)
48 #include <emmintrin.h>
49 #elif defined(_ARCH_PWR8) && defined(PIPE_ARCH_LITTLE_ENDIAN)
51 #include "util/u_pwr8.h"
55 subpixel_snap(float a
)
57 return util_iround(FIXED_ONE
* a
);
63 return a
* (1.0f
/ FIXED_ONE
);
67 /* Position and area in fixed point coordinates */
68 struct fixed_position
{
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
87 struct lp_rast_triangle
*
88 lp_setup_alloc_triangle(struct lp_scene
*scene
,
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
;
97 *tri_size
= (sizeof(struct lp_rast_triangle
) +
101 tri
= lp_scene_alloc_aligned( scene
, *tri_size
, 16 );
105 tri
->inputs
.stride
= input_array_sz
;
108 char *a
= (char *)tri
;
109 char *b
= (char *)&GET_PLANES(tri
)[nr_planes
];
110 assert(b
- a
== *tri_size
);
117 lp_setup_print_vertex(struct lp_setup_context
*setup
,
121 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
124 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
126 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
128 for (i
= 0; i
< key
->num_inputs
; i
++) {
129 const float *in
= v
[key
->inputs
[i
].src_index
];
131 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
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" : " ");
139 for (j
= 0; j
< 4; j
++)
140 if (key
->inputs
[i
].usage_mask
& (1<<j
))
141 debug_printf("%.5f ", in
[j
]);
149 * Print triangle vertex attribs (for debug).
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])
157 debug_printf("triangle\n");
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];
165 /* det = cross(e,f).z */
166 const float det
= ex
* fy
- ey
* fx
;
168 debug_printf(" - ccw\n");
170 debug_printf(" - cw\n");
172 debug_printf(" - zero area\n");
175 lp_setup_print_vertex(setup
, "v0", v0
);
176 lp_setup_print_vertex(setup
, "v1", v1
);
177 lp_setup_print_vertex(setup
, "v2", v2
);
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
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
211 * The primitive covers the whole tile- shade whole tile.
213 * \param tx, ty the tile position in tiles, not pixels
216 lp_setup_whole_tile(struct lp_setup_context
*setup
,
217 const struct lp_rast_shader_inputs
*inputs
,
220 struct lp_scene
*scene
= setup
->scene
;
222 LP_COUNT(nr_fully_covered_64
);
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
238 if (!scene
->fb
.zsbuf
&& scene
->fb_max_layer
== 0 && !scene
->had_queries
) {
240 * All previous rendering will be overwritten so reset the bin.
242 lp_scene_bin_reset( scene
, tx
, ty
);
245 LP_COUNT(nr_shade_opaque_64
);
246 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
248 LP_RAST_OP_SHADE_TILE_OPAQUE
,
249 lp_rast_arg_inputs(inputs
) );
251 LP_COUNT(nr_shade_64
);
252 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
254 LP_RAST_OP_SHADE_TILE
,
255 lp_rast_arg_inputs(inputs
) );
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.
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
)
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
;
280 unsigned viewport_index
= 0;
282 const float (*pv
)[4];
284 /* Area should always be positive here */
285 assert(position
->area
> 0);
288 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
290 if (setup
->flatshade_first
) {
296 if (setup
->viewport_index_slot
> 0) {
297 unsigned *udata
= (unsigned*)pv
[setup
->viewport_index_slot
];
298 viewport_index
= lp_clamp_viewport_idx(*udata
);
300 if (setup
->layer_slot
> 0) {
301 layer
= *(unsigned*)pv
[setup
->layer_slot
];
302 layer
= MIN2(layer
, scene
->fb_max_layer
);
305 if (setup
->scissor_test
) {
312 /* Bounding rectangle (in pixels) */
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.
319 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
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
;
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
;
330 if (bbox
.x1
< bbox
.x0
||
332 if (0) debug_printf("empty bounding box\n");
333 LP_COUNT(nr_culled_tris
);
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
);
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().
347 bbox
.x0
= MAX2(bbox
.x0
, 0);
348 bbox
.y0
= MAX2(bbox
.y0
, 0);
350 tri
= lp_setup_alloc_triangle(scene
,
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];
368 /* Setup parameter interpolants:
370 setup
->setup
.variant
->jit_function( v0
,
374 GET_A0(&tri
->inputs
),
375 GET_DADX(&tri
->inputs
),
376 GET_DADY(&tri
->inputs
) );
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
;
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
));
390 plane
= GET_PLANES(tri
);
392 #if defined(PIPE_ARCH_SSE)
393 if (setup
->fb
.width
<= MAX_FIXED_LENGTH32
&&
394 setup
->fb
.height
<= MAX_FIXED_LENGTH32
&&
395 (bbox
.x1
- bbox
.x0
) <= MAX_FIXED_LENGTH32
&&
396 (bbox
.y1
- bbox
.y0
) <= MAX_FIXED_LENGTH32
) {
397 __m128i vertx
, verty
;
398 __m128i shufx
, shufy
;
399 __m128i dcdx
, dcdy
, c
;
401 __m128i dcdx_neg_mask
;
402 __m128i dcdy_neg_mask
;
403 __m128i dcdx_zero_mask
;
404 __m128i top_left_flag
;
405 __m128i c_inc_mask
, c_inc
;
406 __m128i eo
, p0
, p1
, p2
;
407 __m128i zero
= _mm_setzero_si128();
408 PIPE_ALIGN_VAR(16) int32_t temp_vec
[4];
410 vertx
= _mm_loadu_si128((__m128i
*)position
->x
); /* vertex x coords */
411 verty
= _mm_loadu_si128((__m128i
*)position
->y
); /* vertex y coords */
413 shufx
= _mm_shuffle_epi32(vertx
, _MM_SHUFFLE(3,0,2,1));
414 shufy
= _mm_shuffle_epi32(verty
, _MM_SHUFFLE(3,0,2,1));
416 dcdx
= _mm_sub_epi32(verty
, shufy
);
417 dcdy
= _mm_sub_epi32(vertx
, shufx
);
419 dcdx_neg_mask
= _mm_srai_epi32(dcdx
, 31);
420 dcdx_zero_mask
= _mm_cmpeq_epi32(dcdx
, zero
);
421 dcdy_neg_mask
= _mm_srai_epi32(dcdy
, 31);
423 top_left_flag
= _mm_set1_epi32((setup
->bottom_edge_rule
== 0) ? ~0 : 0);
425 c_inc_mask
= _mm_or_si128(dcdx_neg_mask
,
426 _mm_and_si128(dcdx_zero_mask
,
427 _mm_xor_si128(dcdy_neg_mask
,
430 c_inc
= _mm_srli_epi32(c_inc_mask
, 31);
432 c
= _mm_sub_epi32(mm_mullo_epi32(dcdx
, vertx
),
433 mm_mullo_epi32(dcdy
, verty
));
435 c
= _mm_add_epi32(c
, c_inc
);
437 /* Scale up to match c:
439 dcdx
= _mm_slli_epi32(dcdx
, FIXED_ORDER
);
440 dcdy
= _mm_slli_epi32(dcdy
, FIXED_ORDER
);
442 /* Calculate trivial reject values:
444 eo
= _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask
, dcdy
),
445 _mm_and_si128(dcdx_neg_mask
, dcdx
));
447 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
449 /* Pointless transpose which gets undone immediately in
452 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
453 &p0
, &p1
, &p2
, &unused
);
455 #define STORE_PLANE(plane, vec) do { \
456 _mm_store_si128((__m128i *)&temp_vec, vec); \
457 plane.c = (int64_t)temp_vec[0]; \
458 plane.dcdx = temp_vec[1]; \
459 plane.dcdy = temp_vec[2]; \
460 plane.eo = temp_vec[3]; \
463 STORE_PLANE(plane
[0], p0
);
464 STORE_PLANE(plane
[1], p1
);
465 STORE_PLANE(plane
[2], p2
);
468 #elif defined(_ARCH_PWR8) && defined(PIPE_ARCH_LITTLE_ENDIAN)
470 * XXX this code is effectively disabled for all practical purposes,
471 * as the allowed fb size is tiny if FIXED_ORDER is 8.
473 if (setup
->fb
.width
<= MAX_FIXED_LENGTH32
&&
474 setup
->fb
.height
<= MAX_FIXED_LENGTH32
&&
475 (bbox
.x1
- bbox
.x0
) <= MAX_FIXED_LENGTH32
&&
476 (bbox
.y1
- bbox
.y0
) <= MAX_FIXED_LENGTH32
) {
477 unsigned int bottom_edge
;
478 __m128i vertx
, verty
;
479 __m128i shufx
, shufy
;
480 __m128i dcdx
, dcdy
, c
;
482 __m128i dcdx_neg_mask
;
483 __m128i dcdy_neg_mask
;
484 __m128i dcdx_zero_mask
;
485 __m128i top_left_flag
;
486 __m128i c_inc_mask
, c_inc
;
487 __m128i eo
, p0
, p1
, p2
;
488 __m128i_union vshuf_mask
;
489 __m128i zero
= vec_splats((unsigned char) 0);
490 PIPE_ALIGN_VAR(16) int32_t temp_vec
[4];
492 #ifdef PIPE_ARCH_LITTLE_ENDIAN
493 vshuf_mask
.i
[0] = 0x07060504;
494 vshuf_mask
.i
[1] = 0x0B0A0908;
495 vshuf_mask
.i
[2] = 0x03020100;
496 vshuf_mask
.i
[3] = 0x0F0E0D0C;
498 vshuf_mask
.i
[0] = 0x00010203;
499 vshuf_mask
.i
[1] = 0x0C0D0E0F;
500 vshuf_mask
.i
[2] = 0x04050607;
501 vshuf_mask
.i
[3] = 0x08090A0B;
504 /* vertex x coords */
505 vertx
= vec_load_si128((const uint32_t *) position
->x
);
506 /* vertex y coords */
507 verty
= vec_load_si128((const uint32_t *) position
->y
);
509 shufx
= vec_perm (vertx
, vertx
, vshuf_mask
.m128i
);
510 shufy
= vec_perm (verty
, verty
, vshuf_mask
.m128i
);
512 dcdx
= vec_sub_epi32(verty
, shufy
);
513 dcdy
= vec_sub_epi32(vertx
, shufx
);
515 dcdx_neg_mask
= vec_srai_epi32(dcdx
, 31);
516 dcdx_zero_mask
= vec_cmpeq_epi32(dcdx
, zero
);
517 dcdy_neg_mask
= vec_srai_epi32(dcdy
, 31);
519 bottom_edge
= (setup
->bottom_edge_rule
== 0) ? ~0 : 0;
520 top_left_flag
= (__m128i
) vec_splats(bottom_edge
);
522 c_inc_mask
= vec_or(dcdx_neg_mask
,
523 vec_and(dcdx_zero_mask
,
524 vec_xor(dcdy_neg_mask
,
527 c_inc
= vec_srli_epi32(c_inc_mask
, 31);
529 c
= vec_sub_epi32(vec_mullo_epi32(dcdx
, vertx
),
530 vec_mullo_epi32(dcdy
, verty
));
532 c
= vec_add_epi32(c
, c_inc
);
534 /* Scale up to match c:
536 dcdx
= vec_slli_epi32(dcdx
, FIXED_ORDER
);
537 dcdy
= vec_slli_epi32(dcdy
, FIXED_ORDER
);
539 /* Calculate trivial reject values:
541 eo
= vec_sub_epi32(vec_andc(dcdy_neg_mask
, dcdy
),
542 vec_and(dcdx_neg_mask
, dcdx
));
544 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
546 /* Pointless transpose which gets undone immediately in
549 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
550 &p0
, &p1
, &p2
, &unused
);
552 #define STORE_PLANE(plane, vec) do { \
553 vec_store_si128((uint32_t *)&temp_vec, vec); \
554 plane.c = (int64_t)temp_vec[0]; \
555 plane.dcdx = temp_vec[1]; \
556 plane.dcdy = temp_vec[2]; \
557 plane.eo = temp_vec[3]; \
560 STORE_PLANE(plane
[0], p0
);
561 STORE_PLANE(plane
[1], p1
);
562 STORE_PLANE(plane
[2], p2
);
568 plane
[0].dcdy
= position
->dx01
;
569 plane
[1].dcdy
= position
->x
[1] - position
->x
[2];
570 plane
[2].dcdy
= position
->dx20
;
571 plane
[0].dcdx
= position
->dy01
;
572 plane
[1].dcdx
= position
->y
[1] - position
->y
[2];
573 plane
[2].dcdx
= position
->dy20
;
575 for (i
= 0; i
< 3; i
++) {
576 /* half-edge constants, will be interated over the whole render
579 plane
[i
].c
= IMUL64(plane
[i
].dcdx
, position
->x
[i
]) -
580 IMUL64(plane
[i
].dcdy
, position
->y
[i
]);
582 /* correct for top-left vs. bottom-left fill convention.
584 if (plane
[i
].dcdx
< 0) {
585 /* both fill conventions want this - adjust for left edges */
588 else if (plane
[i
].dcdx
== 0) {
589 if (setup
->bottom_edge_rule
== 0){
590 /* correct for top-left fill convention:
592 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
595 /* correct for bottom-left fill convention:
597 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
601 /* Scale up to match c:
603 assert((plane
[i
].dcdx
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdx
);
604 assert((plane
[i
].dcdy
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdy
);
605 plane
[i
].dcdx
<<= FIXED_ORDER
;
606 plane
[i
].dcdy
<<= FIXED_ORDER
;
608 /* find trivial reject offsets for each edge for a single-pixel
609 * sized block. These will be scaled up at each recursive level to
610 * match the active blocksize. Scaling in this way works best if
611 * the blocks are square.
614 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
615 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
620 debug_printf("p0: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
626 debug_printf("p1: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
632 debug_printf("p2: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
641 * When rasterizing scissored tris, use the intersection of the
642 * triangle bounding box and the scissor rect to generate the
645 * This permits us to cut off the triangle "tails" that are present
646 * in the intermediate recursive levels caused when two of the
647 * triangles edges don't diverge quickly enough to trivially reject
648 * exterior blocks from the triangle.
650 * It's not really clear if it's worth worrying about these tails,
651 * but since we generate the planes for each scissored tri, it's
652 * free to trim them in this case.
654 * Note that otherwise, the scissor planes only vary in 'C' value,
655 * and even then only on state-changes. Could alternatively store
656 * these planes elsewhere.
658 if (nr_planes
== 7) {
659 const struct u_rect
*scissor
= &setup
->scissors
[viewport_index
];
663 plane
[3].c
= 1-scissor
->x0
;
668 plane
[4].c
= scissor
->x1
+1;
673 plane
[5].c
= 1-scissor
->y0
;
678 plane
[6].c
= scissor
->y1
+1;
682 return lp_setup_bin_triangle(setup
, tri
, &bbox
, nr_planes
, viewport_index
);
686 * Round to nearest less or equal power of two of the input.
688 * Undefined if no bit set exists, so code should check against 0 first.
690 static inline uint32_t
691 floor_pot(uint32_t n
)
693 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
713 lp_setup_bin_triangle( struct lp_setup_context
*setup
,
714 struct lp_rast_triangle
*tri
,
715 const struct u_rect
*bbox
,
717 unsigned viewport_index
)
719 struct lp_scene
*scene
= setup
->scene
;
720 struct u_rect trimmed_box
= *bbox
;
722 /* What is the largest power-of-two boundary this triangle crosses:
724 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
725 (bbox
->y0
^ bbox
->y1
));
727 /* The largest dimension of the rasterized area of the triangle
728 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
730 int max_sz
= ((bbox
->x1
- (bbox
->x0
& ~3)) |
731 (bbox
->y1
- (bbox
->y0
& ~3)));
732 int sz
= floor_pot(max_sz
);
733 boolean use_32bits
= max_sz
<= MAX_FIXED_LENGTH32
;
735 /* Now apply scissor, etc to the bounding box. Could do this
736 * earlier, but it confuses the logic for tri-16 and would force
737 * the rasterizer to also respect scissor, etc, just for the rare
738 * cases where a small triangle extends beyond the scissor.
740 u_rect_find_intersection(&setup
->draw_regions
[viewport_index
],
743 /* Determine which tile(s) intersect the triangle's bounding box
747 int ix0
= bbox
->x0
/ TILE_SIZE
;
748 int iy0
= bbox
->y0
/ TILE_SIZE
;
749 unsigned px
= bbox
->x0
& 63 & ~3;
750 unsigned py
= bbox
->y0
& 63 & ~3;
752 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
753 ix0
== bbox
->x1
/ TILE_SIZE
);
755 if (nr_planes
== 3) {
758 /* Triangle is contained in a single 4x4 stamp:
760 assert(px
+ 4 <= TILE_SIZE
);
761 assert(py
+ 4 <= TILE_SIZE
);
762 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
765 LP_RAST_OP_TRIANGLE_32_3_4
:
766 LP_RAST_OP_TRIANGLE_3_4
,
767 lp_rast_arg_triangle_contained(tri
, px
, py
) );
772 /* Triangle is contained in a single 16x16 block:
776 * The 16x16 block is only 4x4 aligned, and can exceed the tile
777 * dimensions if the triangle is 16 pixels in one dimension but 4
778 * in the other. So budge the 16x16 back inside the tile.
780 px
= MIN2(px
, TILE_SIZE
- 16);
781 py
= MIN2(py
, TILE_SIZE
- 16);
783 assert(px
+ 16 <= TILE_SIZE
);
784 assert(py
+ 16 <= TILE_SIZE
);
786 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
789 LP_RAST_OP_TRIANGLE_32_3_16
:
790 LP_RAST_OP_TRIANGLE_3_16
,
791 lp_rast_arg_triangle_contained(tri
, px
, py
) );
794 else if (nr_planes
== 4 && sz
< 16)
796 px
= MIN2(px
, TILE_SIZE
- 16);
797 py
= MIN2(py
, TILE_SIZE
- 16);
799 assert(px
+ 16 <= TILE_SIZE
);
800 assert(py
+ 16 <= TILE_SIZE
);
802 return lp_scene_bin_cmd_with_state(scene
, ix0
, iy0
,
805 LP_RAST_OP_TRIANGLE_32_4_16
:
806 LP_RAST_OP_TRIANGLE_4_16
,
807 lp_rast_arg_triangle_contained(tri
, px
, py
));
811 /* Triangle is contained in a single tile:
813 return lp_scene_bin_cmd_with_state(
814 scene
, ix0
, iy0
, setup
->fs
.stored
,
815 use_32bits
? lp_rast_32_tri_tab
[nr_planes
] : lp_rast_tri_tab
[nr_planes
],
816 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1));
820 struct lp_rast_plane
*plane
= GET_PLANES(tri
);
821 int64_t c
[MAX_PLANES
];
822 int64_t ei
[MAX_PLANES
];
824 int64_t eo
[MAX_PLANES
];
825 int64_t xstep
[MAX_PLANES
];
826 int64_t ystep
[MAX_PLANES
];
829 int ix0
= trimmed_box
.x0
/ TILE_SIZE
;
830 int iy0
= trimmed_box
.y0
/ TILE_SIZE
;
831 int ix1
= trimmed_box
.x1
/ TILE_SIZE
;
832 int iy1
= trimmed_box
.y1
/ TILE_SIZE
;
834 for (i
= 0; i
< nr_planes
; i
++) {
836 IMUL64(plane
[i
].dcdy
, iy0
) * TILE_SIZE
-
837 IMUL64(plane
[i
].dcdx
, ix0
) * TILE_SIZE
);
839 ei
[i
] = (plane
[i
].dcdy
-
841 plane
[i
].eo
) << TILE_ORDER
;
843 eo
[i
] = plane
[i
].eo
<< TILE_ORDER
;
844 xstep
[i
] = -(((int64_t)plane
[i
].dcdx
) << TILE_ORDER
);
845 ystep
[i
] = ((int64_t)plane
[i
].dcdy
) << TILE_ORDER
;
850 /* Test tile-sized blocks against the triangle.
851 * Discard blocks fully outside the tri. If the block is fully
852 * contained inside the tri, bin an lp_rast_shade_tile command.
853 * Else, bin a lp_rast_triangle command.
855 for (y
= iy0
; y
<= iy1
; y
++)
857 boolean in
= FALSE
; /* are we inside the triangle? */
858 int64_t cx
[MAX_PLANES
];
860 for (i
= 0; i
< nr_planes
; i
++)
863 for (x
= ix0
; x
<= ix1
; x
++)
868 for (i
= 0; i
< nr_planes
; i
++) {
869 int64_t planeout
= cx
[i
] + eo
[i
];
870 int64_t planepartial
= cx
[i
] + ei
[i
] - 1;
871 out
|= (int) (planeout
>> 63);
872 partial
|= ((int) (planepartial
>> 63)) & (1<<i
);
878 break; /* exiting triangle, all done with this row */
879 LP_COUNT(nr_empty_64
);
882 /* Not trivially accepted by at least one plane -
883 * rasterize/shade partial tile
885 int count
= util_bitcount(partial
);
888 if (!lp_scene_bin_cmd_with_state( scene
, x
, y
,
891 lp_rast_32_tri_tab
[count
] :
892 lp_rast_tri_tab
[count
],
893 lp_rast_arg_triangle(tri
, partial
) ))
896 LP_COUNT(nr_partially_covered_64
);
899 /* triangle covers the whole tile- shade whole tile */
900 LP_COUNT(nr_fully_covered_64
);
902 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
906 /* Iterate cx values across the region: */
907 for (i
= 0; i
< nr_planes
; i
++)
911 /* Iterate c values down the region: */
912 for (i
= 0; i
< nr_planes
; i
++)
920 /* Need to disable any partially binned triangle. This is easier
921 * than trying to locate all the triangle, shade-tile, etc,
922 * commands which may have been binned.
924 tri
->inputs
.disable
= TRUE
;
930 * Try to draw the triangle, restart the scene on failure.
932 static void retry_triangle_ccw( struct lp_setup_context
*setup
,
933 struct fixed_position
* position
,
934 const float (*v0
)[4],
935 const float (*v1
)[4],
936 const float (*v2
)[4],
939 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
941 if (!lp_setup_flush_and_restart(setup
))
944 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
950 * Calculate fixed position data for a triangle
953 calc_fixed_position( struct lp_setup_context
*setup
,
954 struct fixed_position
* position
,
955 const float (*v0
)[4],
956 const float (*v1
)[4],
957 const float (*v2
)[4])
959 position
->x
[0] = subpixel_snap(v0
[0][0] - setup
->pixel_offset
);
960 position
->x
[1] = subpixel_snap(v1
[0][0] - setup
->pixel_offset
);
961 position
->x
[2] = subpixel_snap(v2
[0][0] - setup
->pixel_offset
);
964 position
->y
[0] = subpixel_snap(v0
[0][1] - setup
->pixel_offset
);
965 position
->y
[1] = subpixel_snap(v1
[0][1] - setup
->pixel_offset
);
966 position
->y
[2] = subpixel_snap(v2
[0][1] - setup
->pixel_offset
);
969 position
->dx01
= position
->x
[0] - position
->x
[1];
970 position
->dy01
= position
->y
[0] - position
->y
[1];
972 position
->dx20
= position
->x
[2] - position
->x
[0];
973 position
->dy20
= position
->y
[2] - position
->y
[0];
975 position
->area
= IMUL64(position
->dx01
, position
->dy20
) -
976 IMUL64(position
->dx20
, position
->dy01
);
981 * Rotate a triangle, flipping its clockwise direction,
982 * Swaps values for xy[0] and xy[1]
985 rotate_fixed_position_01( struct fixed_position
* position
)
991 position
->x
[1] = position
->x
[0];
992 position
->y
[1] = position
->y
[0];
996 position
->dx01
= -position
->dx01
;
997 position
->dy01
= -position
->dy01
;
998 position
->dx20
= position
->x
[2] - position
->x
[0];
999 position
->dy20
= position
->y
[2] - position
->y
[0];
1001 position
->area
= -position
->area
;
1006 * Rotate a triangle, flipping its clockwise direction,
1007 * Swaps values for xy[1] and xy[2]
1010 rotate_fixed_position_12( struct fixed_position
* position
)
1016 position
->x
[2] = position
->x
[1];
1017 position
->y
[2] = position
->y
[1];
1023 position
->dx01
= -position
->dx20
;
1024 position
->dy01
= -position
->dy20
;
1025 position
->dx20
= -x
;
1026 position
->dy20
= -y
;
1028 position
->area
= -position
->area
;
1033 * Draw triangle if it's CW, cull otherwise.
1035 static void triangle_cw( struct lp_setup_context
*setup
,
1036 const float (*v0
)[4],
1037 const float (*v1
)[4],
1038 const float (*v2
)[4] )
1040 struct fixed_position position
;
1042 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1044 if (position
.area
< 0) {
1045 if (setup
->flatshade_first
) {
1046 rotate_fixed_position_12(&position
);
1047 retry_triangle_ccw(setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1049 rotate_fixed_position_01(&position
);
1050 retry_triangle_ccw(setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1056 static void triangle_ccw( struct lp_setup_context
*setup
,
1057 const float (*v0
)[4],
1058 const float (*v1
)[4],
1059 const float (*v2
)[4])
1061 struct fixed_position position
;
1063 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1065 if (position
.area
> 0)
1066 retry_triangle_ccw(setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1070 * Draw triangle whether it's CW or CCW.
1072 static void triangle_both( struct lp_setup_context
*setup
,
1073 const float (*v0
)[4],
1074 const float (*v1
)[4],
1075 const float (*v2
)[4] )
1077 struct fixed_position position
;
1078 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
1080 if (lp_context
->active_statistics_queries
&&
1081 !llvmpipe_rasterization_disabled(lp_context
)) {
1082 lp_context
->pipeline_statistics
.c_primitives
++;
1085 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1088 assert(!util_is_inf_or_nan(v0
[0][0]));
1089 assert(!util_is_inf_or_nan(v0
[0][1]));
1090 assert(!util_is_inf_or_nan(v1
[0][0]));
1091 assert(!util_is_inf_or_nan(v1
[0][1]));
1092 assert(!util_is_inf_or_nan(v2
[0][0]));
1093 assert(!util_is_inf_or_nan(v2
[0][1]));
1096 if (position
.area
> 0)
1097 retry_triangle_ccw( setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1098 else if (position
.area
< 0) {
1099 if (setup
->flatshade_first
) {
1100 rotate_fixed_position_12( &position
);
1101 retry_triangle_ccw( setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1103 rotate_fixed_position_01( &position
);
1104 retry_triangle_ccw( setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1110 static void triangle_nop( struct lp_setup_context
*setup
,
1111 const float (*v0
)[4],
1112 const float (*v1
)[4],
1113 const float (*v2
)[4] )
1119 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
1121 switch (setup
->cullmode
) {
1122 case PIPE_FACE_NONE
:
1123 setup
->triangle
= triangle_both
;
1125 case PIPE_FACE_BACK
:
1126 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
1128 case PIPE_FACE_FRONT
:
1129 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
1132 setup
->triangle
= triangle_nop
;