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;
279 const float (*pv
)[4];
281 /* Area should always be positive here */
282 assert(position
->area
> 0);
285 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
287 if (setup
->flatshade_first
) {
293 if (setup
->viewport_index_slot
> 0) {
294 unsigned *udata
= (unsigned*)pv
[setup
->viewport_index_slot
];
295 viewport_index
= lp_clamp_viewport_idx(*udata
);
297 if (setup
->layer_slot
> 0) {
298 layer
= *(unsigned*)pv
[setup
->layer_slot
];
299 layer
= MIN2(layer
, scene
->fb_max_layer
);
302 if (setup
->scissor_test
) {
309 /* Bounding rectangle (in pixels) */
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.
316 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
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
;
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
;
327 if (bbox
.x1
< bbox
.x0
||
329 if (0) debug_printf("empty bounding box\n");
330 LP_COUNT(nr_culled_tris
);
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
);
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().
344 bbox
.x0
= MAX2(bbox
.x0
, 0);
345 bbox
.y0
= MAX2(bbox
.y0
, 0);
347 tri
= lp_setup_alloc_triangle(scene
,
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];
365 /* Setup parameter interpolants:
367 setup
->setup
.variant
->jit_function( v0
,
371 GET_A0(&tri
->inputs
),
372 GET_DADX(&tri
->inputs
),
373 GET_DADY(&tri
->inputs
) );
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
;
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
));
387 plane
= GET_PLANES(tri
);
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
;
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];
407 vertx
= _mm_loadu_si128((__m128i
*)position
->x
); /* vertex x coords */
408 verty
= _mm_loadu_si128((__m128i
*)position
->y
); /* vertex y coords */
410 shufx
= _mm_shuffle_epi32(vertx
, _MM_SHUFFLE(3,0,2,1));
411 shufy
= _mm_shuffle_epi32(verty
, _MM_SHUFFLE(3,0,2,1));
413 dcdx
= _mm_sub_epi32(verty
, shufy
);
414 dcdy
= _mm_sub_epi32(vertx
, shufx
);
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);
420 top_left_flag
= _mm_set1_epi32((setup
->bottom_edge_rule
== 0) ? ~0 : 0);
422 c_inc_mask
= _mm_or_si128(dcdx_neg_mask
,
423 _mm_and_si128(dcdx_zero_mask
,
424 _mm_xor_si128(dcdy_neg_mask
,
427 c_inc
= _mm_srli_epi32(c_inc_mask
, 31);
429 c
= _mm_sub_epi32(mm_mullo_epi32(dcdx
, vertx
),
430 mm_mullo_epi32(dcdy
, verty
));
432 c
= _mm_add_epi32(c
, c_inc
);
434 /* Scale up to match c:
436 dcdx
= _mm_slli_epi32(dcdx
, FIXED_ORDER
);
437 dcdy
= _mm_slli_epi32(dcdy
, FIXED_ORDER
);
439 /* Calculate trivial reject values:
441 eo
= _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask
, dcdy
),
442 _mm_and_si128(dcdx_neg_mask
, dcdx
));
444 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
446 /* Pointless transpose which gets undone immediately in
449 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
450 &p0
, &p1
, &p2
, &unused
);
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]; \
460 STORE_PLANE(plane
[0], p0
);
461 STORE_PLANE(plane
[1], p1
);
462 STORE_PLANE(plane
[2], p2
);
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
;
475 for (i
= 0; i
< 3; i
++) {
476 /* half-edge constants, will be interated over the whole render
479 plane
[i
].c
= IMUL64(plane
[i
].dcdx
, position
->x
[i
]) -
480 IMUL64(plane
[i
].dcdy
, position
->y
[i
]);
482 /* correct for top-left vs. bottom-left fill convention.
484 if (plane
[i
].dcdx
< 0) {
485 /* both fill conventions want this - adjust for left edges */
488 else if (plane
[i
].dcdx
== 0) {
489 if (setup
->bottom_edge_rule
== 0){
490 /* correct for top-left fill convention:
492 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
495 /* correct for bottom-left fill convention:
497 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
501 /* Scale up to match c:
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
;
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.
514 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
515 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
520 debug_printf("p0: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
526 debug_printf("p1: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
532 debug_printf("p2: %"PRIx64
"/%08x/%08x/%"PRIx64
"\n",
541 * When rasterizing scissored tris, use the intersection of the
542 * triangle bounding box and the scissor rect to generate the
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.
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.
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.
558 if (nr_planes
== 7) {
559 const struct u_rect
*scissor
= &setup
->scissors
[viewport_index
];
563 plane
[3].c
= 1-scissor
->x0
;
568 plane
[4].c
= scissor
->x1
+1;
573 plane
[5].c
= 1-scissor
->y0
;
578 plane
[6].c
= scissor
->y1
+1;
582 return lp_setup_bin_triangle(setup
, tri
, &bbox
, nr_planes
, viewport_index
);
586 * Round to nearest less or equal power of two of the input.
588 * Undefined if no bit set exists, so code should check against 0 first.
590 static inline uint32_t
591 floor_pot(uint32_t n
)
593 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
613 lp_setup_bin_triangle( struct lp_setup_context
*setup
,
614 struct lp_rast_triangle
*tri
,
615 const struct u_rect
*bbox
,
617 unsigned viewport_index
)
619 struct lp_scene
*scene
= setup
->scene
;
620 struct u_rect trimmed_box
= *bbox
;
622 /* What is the largest power-of-two boundary this triangle crosses:
624 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
625 (bbox
->y0
^ bbox
->y1
));
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:
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
;
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.
640 u_rect_find_intersection(&setup
->draw_regions
[viewport_index
],
643 /* Determine which tile(s) intersect the triangle's bounding box
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;
652 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
653 ix0
== bbox
->x1
/ TILE_SIZE
);
655 if (nr_planes
== 3) {
658 /* Triangle is contained in a single 4x4 stamp:
660 assert(px
+ 4 <= TILE_SIZE
);
661 assert(py
+ 4 <= TILE_SIZE
);
662 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
665 LP_RAST_OP_TRIANGLE_32_3_4
:
666 LP_RAST_OP_TRIANGLE_3_4
,
667 lp_rast_arg_triangle_contained(tri
, px
, py
) );
672 /* Triangle is contained in a single 16x16 block:
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.
680 px
= MIN2(px
, TILE_SIZE
- 16);
681 py
= MIN2(py
, TILE_SIZE
- 16);
683 assert(px
+ 16 <= TILE_SIZE
);
684 assert(py
+ 16 <= TILE_SIZE
);
686 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
689 LP_RAST_OP_TRIANGLE_32_3_16
:
690 LP_RAST_OP_TRIANGLE_3_16
,
691 lp_rast_arg_triangle_contained(tri
, px
, py
) );
694 else if (nr_planes
== 4 && sz
< 16)
696 px
= MIN2(px
, TILE_SIZE
- 16);
697 py
= MIN2(py
, TILE_SIZE
- 16);
699 assert(px
+ 16 <= TILE_SIZE
);
700 assert(py
+ 16 <= TILE_SIZE
);
702 return lp_scene_bin_cmd_with_state(scene
, ix0
, iy0
,
705 LP_RAST_OP_TRIANGLE_32_4_16
:
706 LP_RAST_OP_TRIANGLE_4_16
,
707 lp_rast_arg_triangle_contained(tri
, px
, py
));
711 /* Triangle is contained in a single tile:
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));
720 struct lp_rast_plane
*plane
= GET_PLANES(tri
);
721 int64_t c
[MAX_PLANES
];
722 int64_t ei
[MAX_PLANES
];
724 int64_t eo
[MAX_PLANES
];
725 int64_t xstep
[MAX_PLANES
];
726 int64_t ystep
[MAX_PLANES
];
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
;
734 for (i
= 0; i
< nr_planes
; i
++) {
736 IMUL64(plane
[i
].dcdy
, iy0
) * TILE_SIZE
-
737 IMUL64(plane
[i
].dcdx
, ix0
) * TILE_SIZE
);
739 ei
[i
] = (plane
[i
].dcdy
-
741 plane
[i
].eo
) << TILE_ORDER
;
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
;
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.
755 for (y
= iy0
; y
<= iy1
; y
++)
757 boolean in
= FALSE
; /* are we inside the triangle? */
758 int64_t cx
[MAX_PLANES
];
760 for (i
= 0; i
< nr_planes
; i
++)
763 for (x
= ix0
; x
<= ix1
; x
++)
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
);
778 break; /* exiting triangle, all done with this row */
779 LP_COUNT(nr_empty_64
);
782 /* Not trivially accepted by at least one plane -
783 * rasterize/shade partial tile
785 int count
= util_bitcount(partial
);
788 if (!lp_scene_bin_cmd_with_state( scene
, x
, y
,
791 lp_rast_32_tri_tab
[count
] :
792 lp_rast_tri_tab
[count
],
793 lp_rast_arg_triangle(tri
, partial
) ))
796 LP_COUNT(nr_partially_covered_64
);
799 /* triangle covers the whole tile- shade whole tile */
800 LP_COUNT(nr_fully_covered_64
);
802 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
806 /* Iterate cx values across the region: */
807 for (i
= 0; i
< nr_planes
; i
++)
811 /* Iterate c values down the region: */
812 for (i
= 0; i
< nr_planes
; i
++)
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.
824 tri
->inputs
.disable
= TRUE
;
830 * Try to draw the triangle, restart the scene on failure.
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],
839 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
841 if (!lp_setup_flush_and_restart(setup
))
844 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
850 * Calculate fixed position data for a triangle
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])
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
);
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
);
869 position
->dx01
= position
->x
[0] - position
->x
[1];
870 position
->dy01
= position
->y
[0] - position
->y
[1];
872 position
->dx20
= position
->x
[2] - position
->x
[0];
873 position
->dy20
= position
->y
[2] - position
->y
[0];
875 position
->area
= IMUL64(position
->dx01
, position
->dy20
) -
876 IMUL64(position
->dx20
, position
->dy01
);
881 * Rotate a triangle, flipping its clockwise direction,
882 * Swaps values for xy[0] and xy[1]
885 rotate_fixed_position_01( struct fixed_position
* position
)
891 position
->x
[1] = position
->x
[0];
892 position
->y
[1] = position
->y
[0];
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];
901 position
->area
= -position
->area
;
906 * Rotate a triangle, flipping its clockwise direction,
907 * Swaps values for xy[1] and xy[2]
910 rotate_fixed_position_12( struct fixed_position
* position
)
916 position
->x
[2] = position
->x
[1];
917 position
->y
[2] = position
->y
[1];
923 position
->dx01
= -position
->dx20
;
924 position
->dy01
= -position
->dy20
;
928 position
->area
= -position
->area
;
933 * Draw triangle if it's CW, cull otherwise.
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] )
940 struct fixed_position position
;
942 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
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
);
949 rotate_fixed_position_01(&position
);
950 retry_triangle_ccw(setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
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])
961 struct fixed_position position
;
963 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
965 if (position
.area
> 0)
966 retry_triangle_ccw(setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
970 * Draw triangle whether it's CW or CCW.
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] )
977 struct fixed_position position
;
978 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
980 if (lp_context
->active_statistics_queries
&&
981 !llvmpipe_rasterization_disabled(lp_context
)) {
982 lp_context
->pipeline_statistics
.c_primitives
++;
985 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
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]));
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
);
1003 rotate_fixed_position_01( &position
);
1004 retry_triangle_ccw( setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
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] )
1019 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
1021 switch (setup
->cullmode
) {
1022 case PIPE_FACE_NONE
:
1023 setup
->triangle
= triangle_both
;
1025 case PIPE_FACE_BACK
:
1026 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
1028 case PIPE_FACE_FRONT
:
1029 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
1032 setup
->triangle
= triangle_nop
;