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>
52 subpixel_snap(float a
)
54 return util_iround(FIXED_ONE
* a
);
60 return a
* (1.0f
/ FIXED_ONE
);
64 /* Position and area in fixed point coordinates */
65 struct fixed_position
{
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
84 struct lp_rast_triangle
*
85 lp_setup_alloc_triangle(struct lp_scene
*scene
,
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
;
94 *tri_size
= (sizeof(struct lp_rast_triangle
) +
98 tri
= lp_scene_alloc_aligned( scene
, *tri_size
, 16 );
102 tri
->inputs
.stride
= input_array_sz
;
105 char *a
= (char *)tri
;
106 char *b
= (char *)&GET_PLANES(tri
)[nr_planes
];
107 assert(b
- a
== *tri_size
);
114 lp_setup_print_vertex(struct lp_setup_context
*setup
,
118 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
121 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
123 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
125 for (i
= 0; i
< key
->num_inputs
; i
++) {
126 const float *in
= v
[key
->inputs
[i
].src_index
];
128 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
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" : " ");
136 for (j
= 0; j
< 4; j
++)
137 if (key
->inputs
[i
].usage_mask
& (1<<j
))
138 debug_printf("%.5f ", in
[j
]);
146 * Print triangle vertex attribs (for debug).
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])
154 debug_printf("triangle\n");
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];
162 /* det = cross(e,f).z */
163 const float det
= ex
* fy
- ey
* fx
;
165 debug_printf(" - ccw\n");
167 debug_printf(" - cw\n");
169 debug_printf(" - zero area\n");
172 lp_setup_print_vertex(setup
, "v0", v0
);
173 lp_setup_print_vertex(setup
, "v1", v1
);
174 lp_setup_print_vertex(setup
, "v2", v2
);
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
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
208 * The primitive covers the whole tile- shade whole tile.
210 * \param tx, ty the tile position in tiles, not pixels
213 lp_setup_whole_tile(struct lp_setup_context
*setup
,
214 const struct lp_rast_shader_inputs
*inputs
,
217 struct lp_scene
*scene
= setup
->scene
;
219 LP_COUNT(nr_fully_covered_64
);
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
235 if (!scene
->fb
.zsbuf
&& scene
->fb_max_layer
== 0 && !scene
->had_queries
) {
237 * All previous rendering will be overwritten so reset the bin.
239 lp_scene_bin_reset( scene
, tx
, ty
);
242 LP_COUNT(nr_shade_opaque_64
);
243 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
245 LP_RAST_OP_SHADE_TILE_OPAQUE
,
246 lp_rast_arg_inputs(inputs
) );
248 LP_COUNT(nr_shade_64
);
249 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
251 LP_RAST_OP_SHADE_TILE
,
252 lp_rast_arg_inputs(inputs
) );
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.
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
)
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
;
277 unsigned viewport_index
= 0;
280 /* Area should always be positive here */
281 assert(position
->area
> 0);
284 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
286 if (setup
->scissor_test
) {
288 if (setup
->viewport_index_slot
> 0) {
289 unsigned *udata
= (unsigned*)v0
[setup
->viewport_index_slot
];
290 viewport_index
= lp_clamp_viewport_idx(*udata
);
296 if (setup
->layer_slot
> 0) {
297 layer
= *(unsigned*)v1
[setup
->layer_slot
];
298 layer
= MIN2(layer
, scene
->fb_max_layer
);
301 /* Bounding rectangle (in pixels) */
303 /* Yes this is necessary to accurately calculate bounding boxes
304 * with the two fill-conventions we support. GL (normally) ends
305 * up needing a bottom-left fill convention, which requires
306 * slightly different rounding.
308 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
310 /* Inclusive x0, exclusive x1 */
311 bbox
.x0
= MIN3(position
->x
[0], position
->x
[1], position
->x
[2]) >> FIXED_ORDER
;
312 bbox
.x1
= (MAX3(position
->x
[0], position
->x
[1], position
->x
[2]) - 1) >> FIXED_ORDER
;
314 /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
315 bbox
.y0
= (MIN3(position
->y
[0], position
->y
[1], position
->y
[2]) + adj
) >> FIXED_ORDER
;
316 bbox
.y1
= (MAX3(position
->y
[0], position
->y
[1], position
->y
[2]) - 1 + adj
) >> FIXED_ORDER
;
319 if (bbox
.x1
< bbox
.x0
||
321 if (0) debug_printf("empty bounding box\n");
322 LP_COUNT(nr_culled_tris
);
326 if (!u_rect_test_intersection(&setup
->draw_regions
[viewport_index
], &bbox
)) {
327 if (0) debug_printf("offscreen\n");
328 LP_COUNT(nr_culled_tris
);
332 /* Can safely discard negative regions, but need to keep hold of
333 * information about when the triangle extends past screen
334 * boundaries. See trimmed_box in lp_setup_bin_triangle().
336 bbox
.x0
= MAX2(bbox
.x0
, 0);
337 bbox
.y0
= MAX2(bbox
.y0
, 0);
339 tri
= lp_setup_alloc_triangle(scene
,
347 tri
->v
[0][0] = v0
[0][0];
348 tri
->v
[1][0] = v1
[0][0];
349 tri
->v
[2][0] = v2
[0][0];
350 tri
->v
[0][1] = v0
[0][1];
351 tri
->v
[1][1] = v1
[0][1];
352 tri
->v
[2][1] = v2
[0][1];
357 /* Setup parameter interpolants:
359 setup
->setup
.variant
->jit_function( v0
,
363 GET_A0(&tri
->inputs
),
364 GET_DADX(&tri
->inputs
),
365 GET_DADY(&tri
->inputs
) );
367 tri
->inputs
.frontfacing
= frontfacing
;
368 tri
->inputs
.disable
= FALSE
;
369 tri
->inputs
.opaque
= setup
->fs
.current
.variant
->opaque
;
370 tri
->inputs
.layer
= layer
;
371 tri
->inputs
.viewport_index
= viewport_index
;
374 lp_dump_setup_coef(&setup
->setup
.variant
->key
,
375 (const float (*)[4])GET_A0(&tri
->inputs
),
376 (const float (*)[4])GET_DADX(&tri
->inputs
),
377 (const float (*)[4])GET_DADY(&tri
->inputs
));
379 plane
= GET_PLANES(tri
);
381 #if defined(PIPE_ARCH_SSE)
382 if (setup
->fb
.width
<= MAX_FIXED_LENGTH32
&&
383 setup
->fb
.height
<= MAX_FIXED_LENGTH32
&&
384 (bbox
.x1
- bbox
.x0
) <= MAX_FIXED_LENGTH32
&&
385 (bbox
.y1
- bbox
.y0
) <= MAX_FIXED_LENGTH32
) {
386 __m128i vertx
, verty
;
387 __m128i shufx
, shufy
;
388 __m128i dcdx
, dcdy
, c
;
390 __m128i dcdx_neg_mask
;
391 __m128i dcdy_neg_mask
;
392 __m128i dcdx_zero_mask
;
393 __m128i top_left_flag
;
394 __m128i c_inc_mask
, c_inc
;
395 __m128i eo
, p0
, p1
, p2
;
396 __m128i zero
= _mm_setzero_si128();
397 PIPE_ALIGN_VAR(16) int32_t temp_vec
[4];
399 vertx
= _mm_loadu_si128((__m128i
*)position
->x
); /* vertex x coords */
400 verty
= _mm_loadu_si128((__m128i
*)position
->y
); /* vertex y coords */
402 shufx
= _mm_shuffle_epi32(vertx
, _MM_SHUFFLE(3,0,2,1));
403 shufy
= _mm_shuffle_epi32(verty
, _MM_SHUFFLE(3,0,2,1));
405 dcdx
= _mm_sub_epi32(verty
, shufy
);
406 dcdy
= _mm_sub_epi32(vertx
, shufx
);
408 dcdx_neg_mask
= _mm_srai_epi32(dcdx
, 31);
409 dcdx_zero_mask
= _mm_cmpeq_epi32(dcdx
, zero
);
410 dcdy_neg_mask
= _mm_srai_epi32(dcdy
, 31);
412 top_left_flag
= _mm_set1_epi32((setup
->bottom_edge_rule
== 0) ? ~0 : 0);
414 c_inc_mask
= _mm_or_si128(dcdx_neg_mask
,
415 _mm_and_si128(dcdx_zero_mask
,
416 _mm_xor_si128(dcdy_neg_mask
,
419 c_inc
= _mm_srli_epi32(c_inc_mask
, 31);
421 c
= _mm_sub_epi32(mm_mullo_epi32(dcdx
, vertx
),
422 mm_mullo_epi32(dcdy
, verty
));
424 c
= _mm_add_epi32(c
, c_inc
);
426 /* Scale up to match c:
428 dcdx
= _mm_slli_epi32(dcdx
, FIXED_ORDER
);
429 dcdy
= _mm_slli_epi32(dcdy
, FIXED_ORDER
);
431 /* Calculate trivial reject values:
433 eo
= _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask
, dcdy
),
434 _mm_and_si128(dcdx_neg_mask
, dcdx
));
436 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
438 /* Pointless transpose which gets undone immediately in
441 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
442 &p0
, &p1
, &p2
, &unused
);
444 #define STORE_PLANE(plane, vec) do { \
445 _mm_store_si128((__m128i *)&temp_vec, vec); \
446 plane.c = (int64_t)temp_vec[0]; \
447 plane.dcdx = temp_vec[1]; \
448 plane.dcdy = temp_vec[2]; \
449 plane.eo = temp_vec[3]; \
452 STORE_PLANE(plane
[0], p0
);
453 STORE_PLANE(plane
[1], p1
);
454 STORE_PLANE(plane
[2], p2
);
460 plane
[0].dcdy
= position
->dx01
;
461 plane
[1].dcdy
= position
->x
[1] - position
->x
[2];
462 plane
[2].dcdy
= position
->dx20
;
463 plane
[0].dcdx
= position
->dy01
;
464 plane
[1].dcdx
= position
->y
[1] - position
->y
[2];
465 plane
[2].dcdx
= position
->dy20
;
467 for (i
= 0; i
< 3; i
++) {
468 /* half-edge constants, will be interated over the whole render
471 plane
[i
].c
= IMUL64(plane
[i
].dcdx
, position
->x
[i
]) -
472 IMUL64(plane
[i
].dcdy
, position
->y
[i
]);
474 /* correct for top-left vs. bottom-left fill convention.
476 if (plane
[i
].dcdx
< 0) {
477 /* both fill conventions want this - adjust for left edges */
480 else if (plane
[i
].dcdx
== 0) {
481 if (setup
->bottom_edge_rule
== 0){
482 /* correct for top-left fill convention:
484 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
487 /* correct for bottom-left fill convention:
489 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
493 /* Scale up to match c:
495 assert((plane
[i
].dcdx
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdx
);
496 assert((plane
[i
].dcdy
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdy
);
497 plane
[i
].dcdx
<<= FIXED_ORDER
;
498 plane
[i
].dcdy
<<= FIXED_ORDER
;
500 /* find trivial reject offsets for each edge for a single-pixel
501 * sized block. These will be scaled up at each recursive level to
502 * match the active blocksize. Scaling in this way works best if
503 * the blocks are square.
506 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
507 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
512 debug_printf("p0: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
518 debug_printf("p1: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
524 debug_printf("p2: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
533 * When rasterizing scissored tris, use the intersection of the
534 * triangle bounding box and the scissor rect to generate the
537 * This permits us to cut off the triangle "tails" that are present
538 * in the intermediate recursive levels caused when two of the
539 * triangles edges don't diverge quickly enough to trivially reject
540 * exterior blocks from the triangle.
542 * It's not really clear if it's worth worrying about these tails,
543 * but since we generate the planes for each scissored tri, it's
544 * free to trim them in this case.
546 * Note that otherwise, the scissor planes only vary in 'C' value,
547 * and even then only on state-changes. Could alternatively store
548 * these planes elsewhere.
550 if (nr_planes
== 7) {
551 const struct u_rect
*scissor
= &setup
->scissors
[viewport_index
];
555 plane
[3].c
= 1-scissor
->x0
;
560 plane
[4].c
= scissor
->x1
+1;
565 plane
[5].c
= 1-scissor
->y0
;
570 plane
[6].c
= scissor
->y1
+1;
574 return lp_setup_bin_triangle(setup
, tri
, &bbox
, nr_planes
, viewport_index
);
578 * Round to nearest less or equal power of two of the input.
580 * Undefined if no bit set exists, so code should check against 0 first.
582 static inline uint32_t
583 floor_pot(uint32_t n
)
585 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
605 lp_setup_bin_triangle( struct lp_setup_context
*setup
,
606 struct lp_rast_triangle
*tri
,
607 const struct u_rect
*bbox
,
609 unsigned viewport_index
)
611 struct lp_scene
*scene
= setup
->scene
;
612 struct u_rect trimmed_box
= *bbox
;
614 /* What is the largest power-of-two boundary this triangle crosses:
616 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
617 (bbox
->y0
^ bbox
->y1
));
619 /* The largest dimension of the rasterized area of the triangle
620 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
622 int max_sz
= ((bbox
->x1
- (bbox
->x0
& ~3)) |
623 (bbox
->y1
- (bbox
->y0
& ~3)));
624 int sz
= floor_pot(max_sz
);
625 boolean use_32bits
= max_sz
<= MAX_FIXED_LENGTH32
;
627 /* Now apply scissor, etc to the bounding box. Could do this
628 * earlier, but it confuses the logic for tri-16 and would force
629 * the rasterizer to also respect scissor, etc, just for the rare
630 * cases where a small triangle extends beyond the scissor.
632 u_rect_find_intersection(&setup
->draw_regions
[viewport_index
],
635 /* Determine which tile(s) intersect the triangle's bounding box
639 int ix0
= bbox
->x0
/ TILE_SIZE
;
640 int iy0
= bbox
->y0
/ TILE_SIZE
;
641 unsigned px
= bbox
->x0
& 63 & ~3;
642 unsigned py
= bbox
->y0
& 63 & ~3;
644 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
645 ix0
== bbox
->x1
/ TILE_SIZE
);
647 if (nr_planes
== 3) {
650 /* Triangle is contained in a single 4x4 stamp:
652 assert(px
+ 4 <= TILE_SIZE
);
653 assert(py
+ 4 <= TILE_SIZE
);
654 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
657 LP_RAST_OP_TRIANGLE_32_3_4
:
658 LP_RAST_OP_TRIANGLE_3_4
,
659 lp_rast_arg_triangle_contained(tri
, px
, py
) );
664 /* Triangle is contained in a single 16x16 block:
668 * The 16x16 block is only 4x4 aligned, and can exceed the tile
669 * dimensions if the triangle is 16 pixels in one dimension but 4
670 * in the other. So budge the 16x16 back inside the tile.
672 px
= MIN2(px
, TILE_SIZE
- 16);
673 py
= MIN2(py
, TILE_SIZE
- 16);
675 assert(px
+ 16 <= TILE_SIZE
);
676 assert(py
+ 16 <= TILE_SIZE
);
678 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
681 LP_RAST_OP_TRIANGLE_32_3_16
:
682 LP_RAST_OP_TRIANGLE_3_16
,
683 lp_rast_arg_triangle_contained(tri
, px
, py
) );
686 else if (nr_planes
== 4 && sz
< 16)
688 px
= MIN2(px
, TILE_SIZE
- 16);
689 py
= MIN2(py
, TILE_SIZE
- 16);
691 assert(px
+ 16 <= TILE_SIZE
);
692 assert(py
+ 16 <= TILE_SIZE
);
694 return lp_scene_bin_cmd_with_state(scene
, ix0
, iy0
,
697 LP_RAST_OP_TRIANGLE_32_4_16
:
698 LP_RAST_OP_TRIANGLE_4_16
,
699 lp_rast_arg_triangle_contained(tri
, px
, py
));
703 /* Triangle is contained in a single tile:
705 return lp_scene_bin_cmd_with_state(
706 scene
, ix0
, iy0
, setup
->fs
.stored
,
707 use_32bits
? lp_rast_32_tri_tab
[nr_planes
] : lp_rast_tri_tab
[nr_planes
],
708 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1));
712 struct lp_rast_plane
*plane
= GET_PLANES(tri
);
713 int64_t c
[MAX_PLANES
];
714 int64_t ei
[MAX_PLANES
];
716 int64_t eo
[MAX_PLANES
];
717 int64_t xstep
[MAX_PLANES
];
718 int64_t ystep
[MAX_PLANES
];
721 int ix0
= trimmed_box
.x0
/ TILE_SIZE
;
722 int iy0
= trimmed_box
.y0
/ TILE_SIZE
;
723 int ix1
= trimmed_box
.x1
/ TILE_SIZE
;
724 int iy1
= trimmed_box
.y1
/ TILE_SIZE
;
726 for (i
= 0; i
< nr_planes
; i
++) {
728 IMUL64(plane
[i
].dcdy
, iy0
) * TILE_SIZE
-
729 IMUL64(plane
[i
].dcdx
, ix0
) * TILE_SIZE
);
731 ei
[i
] = (plane
[i
].dcdy
-
733 plane
[i
].eo
) << TILE_ORDER
;
735 eo
[i
] = plane
[i
].eo
<< TILE_ORDER
;
736 xstep
[i
] = -(((int64_t)plane
[i
].dcdx
) << TILE_ORDER
);
737 ystep
[i
] = ((int64_t)plane
[i
].dcdy
) << TILE_ORDER
;
742 /* Test tile-sized blocks against the triangle.
743 * Discard blocks fully outside the tri. If the block is fully
744 * contained inside the tri, bin an lp_rast_shade_tile command.
745 * Else, bin a lp_rast_triangle command.
747 for (y
= iy0
; y
<= iy1
; y
++)
749 boolean in
= FALSE
; /* are we inside the triangle? */
750 int64_t cx
[MAX_PLANES
];
752 for (i
= 0; i
< nr_planes
; i
++)
755 for (x
= ix0
; x
<= ix1
; x
++)
760 for (i
= 0; i
< nr_planes
; i
++) {
761 int64_t planeout
= cx
[i
] + eo
[i
];
762 int64_t planepartial
= cx
[i
] + ei
[i
] - 1;
763 out
|= (int) (planeout
>> 63);
764 partial
|= ((int) (planepartial
>> 63)) & (1<<i
);
770 break; /* exiting triangle, all done with this row */
771 LP_COUNT(nr_empty_64
);
774 /* Not trivially accepted by at least one plane -
775 * rasterize/shade partial tile
777 int count
= util_bitcount(partial
);
780 if (!lp_scene_bin_cmd_with_state( scene
, x
, y
,
783 lp_rast_32_tri_tab
[count
] :
784 lp_rast_tri_tab
[count
],
785 lp_rast_arg_triangle(tri
, partial
) ))
788 LP_COUNT(nr_partially_covered_64
);
791 /* triangle covers the whole tile- shade whole tile */
792 LP_COUNT(nr_fully_covered_64
);
794 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
798 /* Iterate cx values across the region: */
799 for (i
= 0; i
< nr_planes
; i
++)
803 /* Iterate c values down the region: */
804 for (i
= 0; i
< nr_planes
; i
++)
812 /* Need to disable any partially binned triangle. This is easier
813 * than trying to locate all the triangle, shade-tile, etc,
814 * commands which may have been binned.
816 tri
->inputs
.disable
= TRUE
;
822 * Try to draw the triangle, restart the scene on failure.
824 static void retry_triangle_ccw( struct lp_setup_context
*setup
,
825 struct fixed_position
* position
,
826 const float (*v0
)[4],
827 const float (*v1
)[4],
828 const float (*v2
)[4],
831 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
833 if (!lp_setup_flush_and_restart(setup
))
836 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
842 * Calculate fixed position data for a triangle
845 calc_fixed_position( struct lp_setup_context
*setup
,
846 struct fixed_position
* position
,
847 const float (*v0
)[4],
848 const float (*v1
)[4],
849 const float (*v2
)[4])
851 position
->x
[0] = subpixel_snap(v0
[0][0] - setup
->pixel_offset
);
852 position
->x
[1] = subpixel_snap(v1
[0][0] - setup
->pixel_offset
);
853 position
->x
[2] = subpixel_snap(v2
[0][0] - setup
->pixel_offset
);
856 position
->y
[0] = subpixel_snap(v0
[0][1] - setup
->pixel_offset
);
857 position
->y
[1] = subpixel_snap(v1
[0][1] - setup
->pixel_offset
);
858 position
->y
[2] = subpixel_snap(v2
[0][1] - setup
->pixel_offset
);
861 position
->dx01
= position
->x
[0] - position
->x
[1];
862 position
->dy01
= position
->y
[0] - position
->y
[1];
864 position
->dx20
= position
->x
[2] - position
->x
[0];
865 position
->dy20
= position
->y
[2] - position
->y
[0];
867 position
->area
= IMUL64(position
->dx01
, position
->dy20
) -
868 IMUL64(position
->dx20
, position
->dy01
);
873 * Rotate a triangle, flipping its clockwise direction,
874 * Swaps values for xy[0] and xy[1]
877 rotate_fixed_position_01( struct fixed_position
* position
)
883 position
->x
[1] = position
->x
[0];
884 position
->y
[1] = position
->y
[0];
888 position
->dx01
= -position
->dx01
;
889 position
->dy01
= -position
->dy01
;
890 position
->dx20
= position
->x
[2] - position
->x
[0];
891 position
->dy20
= position
->y
[2] - position
->y
[0];
893 position
->area
= -position
->area
;
898 * Rotate a triangle, flipping its clockwise direction,
899 * Swaps values for xy[1] and xy[2]
902 rotate_fixed_position_12( struct fixed_position
* position
)
908 position
->x
[2] = position
->x
[1];
909 position
->y
[2] = position
->y
[1];
915 position
->dx01
= -position
->dx20
;
916 position
->dy01
= -position
->dy20
;
920 position
->area
= -position
->area
;
925 * Draw triangle if it's CW, cull otherwise.
927 static void triangle_cw( struct lp_setup_context
*setup
,
928 const float (*v0
)[4],
929 const float (*v1
)[4],
930 const float (*v2
)[4] )
932 struct fixed_position position
;
934 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
936 if (position
.area
< 0) {
937 if (setup
->flatshade_first
) {
938 rotate_fixed_position_12(&position
);
939 retry_triangle_ccw(setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
941 rotate_fixed_position_01(&position
);
942 retry_triangle_ccw(setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
948 static void triangle_ccw( struct lp_setup_context
*setup
,
949 const float (*v0
)[4],
950 const float (*v1
)[4],
951 const float (*v2
)[4])
953 struct fixed_position position
;
955 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
957 if (position
.area
> 0)
958 retry_triangle_ccw(setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
962 * Draw triangle whether it's CW or CCW.
964 static void triangle_both( struct lp_setup_context
*setup
,
965 const float (*v0
)[4],
966 const float (*v1
)[4],
967 const float (*v2
)[4] )
969 struct fixed_position position
;
970 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
972 if (lp_context
->active_statistics_queries
&&
973 !llvmpipe_rasterization_disabled(lp_context
)) {
974 lp_context
->pipeline_statistics
.c_primitives
++;
977 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
980 assert(!util_is_inf_or_nan(v0
[0][0]));
981 assert(!util_is_inf_or_nan(v0
[0][1]));
982 assert(!util_is_inf_or_nan(v1
[0][0]));
983 assert(!util_is_inf_or_nan(v1
[0][1]));
984 assert(!util_is_inf_or_nan(v2
[0][0]));
985 assert(!util_is_inf_or_nan(v2
[0][1]));
988 if (position
.area
> 0)
989 retry_triangle_ccw( setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
990 else if (position
.area
< 0) {
991 if (setup
->flatshade_first
) {
992 rotate_fixed_position_12( &position
);
993 retry_triangle_ccw( setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
995 rotate_fixed_position_01( &position
);
996 retry_triangle_ccw( setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1002 static void triangle_nop( struct lp_setup_context
*setup
,
1003 const float (*v0
)[4],
1004 const float (*v1
)[4],
1005 const float (*v2
)[4] )
1011 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
1013 switch (setup
->cullmode
) {
1014 case PIPE_FACE_NONE
:
1015 setup
->triangle
= triangle_both
;
1017 case PIPE_FACE_BACK
:
1018 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
1020 case PIPE_FACE_FRONT
:
1021 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
1024 setup
->triangle
= triangle_nop
;