1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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"
43 #define NUM_CHANNELS 4
45 #if defined(PIPE_ARCH_SSE)
46 #include <emmintrin.h>
50 subpixel_snap(float a
)
52 return util_iround(FIXED_ONE
* a
);
58 return a
* (1.0f
/ FIXED_ONE
);
62 /* Position and area in fixed point coordinates */
63 struct fixed_position
{
75 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
76 * immediately after it.
77 * The memory is allocated from the per-scene pool, not per-tile.
78 * \param tri_size returns number of bytes allocated
79 * \param num_inputs number of fragment shader inputs
80 * \return pointer to triangle space
82 struct lp_rast_triangle
*
83 lp_setup_alloc_triangle(struct lp_scene
*scene
,
88 unsigned input_array_sz
= NUM_CHANNELS
* (nr_inputs
+ 1) * sizeof(float);
89 unsigned plane_sz
= nr_planes
* sizeof(struct lp_rast_plane
);
90 struct lp_rast_triangle
*tri
;
92 *tri_size
= (sizeof(struct lp_rast_triangle
) +
96 tri
= lp_scene_alloc_aligned( scene
, *tri_size
, 16 );
100 tri
->inputs
.stride
= input_array_sz
;
103 char *a
= (char *)tri
;
104 char *b
= (char *)&GET_PLANES(tri
)[nr_planes
];
105 assert(b
- a
== *tri_size
);
112 lp_setup_print_vertex(struct lp_setup_context
*setup
,
116 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
119 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
121 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
123 for (i
= 0; i
< key
->num_inputs
; i
++) {
124 const float *in
= v
[key
->inputs
[i
].src_index
];
126 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
128 name
, key
->inputs
[i
].src_index
,
129 (key
->inputs
[i
].usage_mask
& 0x1) ? "x" : " ",
130 (key
->inputs
[i
].usage_mask
& 0x2) ? "y" : " ",
131 (key
->inputs
[i
].usage_mask
& 0x4) ? "z" : " ",
132 (key
->inputs
[i
].usage_mask
& 0x8) ? "w" : " ");
134 for (j
= 0; j
< 4; j
++)
135 if (key
->inputs
[i
].usage_mask
& (1<<j
))
136 debug_printf("%.5f ", in
[j
]);
144 * Print triangle vertex attribs (for debug).
147 lp_setup_print_triangle(struct lp_setup_context
*setup
,
148 const float (*v0
)[4],
149 const float (*v1
)[4],
150 const float (*v2
)[4])
152 debug_printf("triangle\n");
155 const float ex
= v0
[0][0] - v2
[0][0];
156 const float ey
= v0
[0][1] - v2
[0][1];
157 const float fx
= v1
[0][0] - v2
[0][0];
158 const float fy
= v1
[0][1] - v2
[0][1];
160 /* det = cross(e,f).z */
161 const float det
= ex
* fy
- ey
* fx
;
163 debug_printf(" - ccw\n");
165 debug_printf(" - cw\n");
167 debug_printf(" - zero area\n");
170 lp_setup_print_vertex(setup
, "v0", v0
);
171 lp_setup_print_vertex(setup
, "v1", v1
);
172 lp_setup_print_vertex(setup
, "v2", v2
);
178 lp_rast_tri_tab
[MAX_PLANES
+1] = {
179 0, /* should be impossible */
180 LP_RAST_OP_TRIANGLE_1
,
181 LP_RAST_OP_TRIANGLE_2
,
182 LP_RAST_OP_TRIANGLE_3
,
183 LP_RAST_OP_TRIANGLE_4
,
184 LP_RAST_OP_TRIANGLE_5
,
185 LP_RAST_OP_TRIANGLE_6
,
186 LP_RAST_OP_TRIANGLE_7
,
187 LP_RAST_OP_TRIANGLE_8
193 * The primitive covers the whole tile- shade whole tile.
195 * \param tx, ty the tile position in tiles, not pixels
198 lp_setup_whole_tile(struct lp_setup_context
*setup
,
199 const struct lp_rast_shader_inputs
*inputs
,
202 struct lp_scene
*scene
= setup
->scene
;
204 LP_COUNT(nr_fully_covered_64
);
206 /* if variant is opaque and scissor doesn't effect the tile */
207 if (inputs
->opaque
) {
208 /* Several things prevent this optimization from working:
209 * - For layered rendering we can't determine if this covers the same layer
210 * as previous rendering (or in case of clears those actually always cover
211 * all layers so optimization is impossible). Need to use fb_max_layer and
212 * not setup->layer_slot to determine this since even if there's currently
213 * no slot assigned previous rendering could have used one.
214 * - If there were any Begin/End query commands in the scene then those
215 * would get removed which would be very wrong. Furthermore, if queries
216 * were just active we also can't do the optimization since to get
217 * accurate query results we unfortunately need to execute the rendering
220 if (!scene
->fb
.zsbuf
&& scene
->fb_max_layer
== 0 && !scene
->had_queries
) {
222 * All previous rendering will be overwritten so reset the bin.
224 lp_scene_bin_reset( scene
, tx
, ty
);
227 LP_COUNT(nr_shade_opaque_64
);
228 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
230 LP_RAST_OP_SHADE_TILE_OPAQUE
,
231 lp_rast_arg_inputs(inputs
) );
233 LP_COUNT(nr_shade_64
);
234 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
236 LP_RAST_OP_SHADE_TILE
,
237 lp_rast_arg_inputs(inputs
) );
243 * Do basic setup for triangle rasterization and determine which
244 * framebuffer tiles are touched. Put the triangle in the scene's
245 * bins for the tiles which we overlap.
248 do_triangle_ccw(struct lp_setup_context
*setup
,
249 struct fixed_position
* position
,
250 const float (*v0
)[4],
251 const float (*v1
)[4],
252 const float (*v2
)[4],
253 boolean frontfacing
)
255 struct lp_scene
*scene
= setup
->scene
;
256 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
257 struct lp_rast_triangle
*tri
;
258 struct lp_rast_plane
*plane
;
262 unsigned scissor_index
= 0;
265 /* Area should always be positive here */
266 assert(position
->area
> 0);
269 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
271 if (setup
->scissor_test
) {
273 if (setup
->viewport_index_slot
> 0) {
274 unsigned *udata
= (unsigned*)v0
[setup
->viewport_index_slot
];
275 scissor_index
= lp_clamp_scissor_idx(*udata
);
281 if (setup
->layer_slot
> 0) {
282 layer
= *(unsigned*)v1
[setup
->layer_slot
];
283 layer
= MIN2(layer
, scene
->fb_max_layer
);
286 /* Bounding rectangle (in pixels) */
288 /* Yes this is necessary to accurately calculate bounding boxes
289 * with the two fill-conventions we support. GL (normally) ends
290 * up needing a bottom-left fill convention, which requires
291 * slightly different rounding.
293 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
295 /* Inclusive x0, exclusive x1 */
296 bbox
.x0
= MIN3(position
->x
[0], position
->x
[1], position
->x
[2]) >> FIXED_ORDER
;
297 bbox
.x1
= (MAX3(position
->x
[0], position
->x
[1], position
->x
[2]) - 1) >> FIXED_ORDER
;
299 /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
300 bbox
.y0
= (MIN3(position
->y
[0], position
->y
[1], position
->y
[2]) + adj
) >> FIXED_ORDER
;
301 bbox
.y1
= (MAX3(position
->y
[0], position
->y
[1], position
->y
[2]) - 1 + adj
) >> FIXED_ORDER
;
304 if (bbox
.x1
< bbox
.x0
||
306 if (0) debug_printf("empty bounding box\n");
307 LP_COUNT(nr_culled_tris
);
311 if (!u_rect_test_intersection(&setup
->draw_regions
[scissor_index
], &bbox
)) {
312 if (0) debug_printf("offscreen\n");
313 LP_COUNT(nr_culled_tris
);
317 /* Can safely discard negative regions, but need to keep hold of
318 * information about when the triangle extends past screen
319 * boundaries. See trimmed_box in lp_setup_bin_triangle().
321 bbox
.x0
= MAX2(bbox
.x0
, 0);
322 bbox
.y0
= MAX2(bbox
.y0
, 0);
324 tri
= lp_setup_alloc_triangle(scene
,
332 tri
->v
[0][0] = v0
[0][0];
333 tri
->v
[1][0] = v1
[0][0];
334 tri
->v
[2][0] = v2
[0][0];
335 tri
->v
[0][1] = v0
[0][1];
336 tri
->v
[1][1] = v1
[0][1];
337 tri
->v
[2][1] = v2
[0][1];
342 /* Setup parameter interpolants:
344 setup
->setup
.variant
->jit_function( v0
,
348 GET_A0(&tri
->inputs
),
349 GET_DADX(&tri
->inputs
),
350 GET_DADY(&tri
->inputs
) );
352 tri
->inputs
.frontfacing
= frontfacing
;
353 tri
->inputs
.disable
= FALSE
;
354 tri
->inputs
.opaque
= setup
->fs
.current
.variant
->opaque
;
355 tri
->inputs
.layer
= layer
;
358 lp_dump_setup_coef(&setup
->setup
.variant
->key
,
359 (const float (*)[4])GET_A0(&tri
->inputs
),
360 (const float (*)[4])GET_DADX(&tri
->inputs
),
361 (const float (*)[4])GET_DADY(&tri
->inputs
));
363 plane
= GET_PLANES(tri
);
365 #if defined(PIPE_ARCH_SSE)
367 __m128i vertx
, verty
;
368 __m128i shufx
, shufy
;
369 __m128i dcdx
, dcdy
, c
;
371 __m128i dcdx_neg_mask
;
372 __m128i dcdy_neg_mask
;
373 __m128i dcdx_zero_mask
;
374 __m128i top_left_flag
;
375 __m128i c_inc_mask
, c_inc
;
376 __m128i eo
, p0
, p1
, p2
;
377 __m128i zero
= _mm_setzero_si128();
379 vertx
= _mm_loadu_si128((__m128i
*)position
->x
); /* vertex x coords */
380 verty
= _mm_loadu_si128((__m128i
*)position
->y
); /* vertex y coords */
382 shufx
= _mm_shuffle_epi32(vertx
, _MM_SHUFFLE(3,0,2,1));
383 shufy
= _mm_shuffle_epi32(verty
, _MM_SHUFFLE(3,0,2,1));
385 dcdx
= _mm_sub_epi32(verty
, shufy
);
386 dcdy
= _mm_sub_epi32(vertx
, shufx
);
388 dcdx_neg_mask
= _mm_srai_epi32(dcdx
, 31);
389 dcdx_zero_mask
= _mm_cmpeq_epi32(dcdx
, zero
);
390 dcdy_neg_mask
= _mm_srai_epi32(dcdy
, 31);
392 top_left_flag
= _mm_set1_epi32((setup
->bottom_edge_rule
== 0) ? ~0 : 0);
394 c_inc_mask
= _mm_or_si128(dcdx_neg_mask
,
395 _mm_and_si128(dcdx_zero_mask
,
396 _mm_xor_si128(dcdy_neg_mask
,
399 c_inc
= _mm_srli_epi32(c_inc_mask
, 31);
401 c
= _mm_sub_epi32(mm_mullo_epi32(dcdx
, vertx
),
402 mm_mullo_epi32(dcdy
, verty
));
404 c
= _mm_add_epi32(c
, c_inc
);
406 /* Scale up to match c:
408 dcdx
= _mm_slli_epi32(dcdx
, FIXED_ORDER
);
409 dcdy
= _mm_slli_epi32(dcdy
, FIXED_ORDER
);
411 /* Calculate trivial reject values:
413 eo
= _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask
, dcdy
),
414 _mm_and_si128(dcdx_neg_mask
, dcdx
));
416 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
418 /* Pointless transpose which gets undone immediately in
421 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
422 &p0
, &p1
, &p2
, &unused
);
424 _mm_store_si128((__m128i
*)&plane
[0], p0
);
425 _mm_store_si128((__m128i
*)&plane
[1], p1
);
426 _mm_store_si128((__m128i
*)&plane
[2], p2
);
431 plane
[0].dcdy
= position
->dx01
;
432 plane
[1].dcdy
= position
->x
[1] - position
->x
[2];
433 plane
[2].dcdy
= position
->dx20
;
434 plane
[0].dcdx
= position
->dy01
;
435 plane
[1].dcdx
= position
->y
[1] - position
->y
[2];
436 plane
[2].dcdx
= position
->dy20
;
438 for (i
= 0; i
< 3; i
++) {
439 /* half-edge constants, will be interated over the whole render
442 plane
[i
].c
= plane
[i
].dcdx
* position
->x
[i
] - plane
[i
].dcdy
* position
->y
[i
];
444 /* correct for top-left vs. bottom-left fill convention.
446 if (plane
[i
].dcdx
< 0) {
447 /* both fill conventions want this - adjust for left edges */
450 else if (plane
[i
].dcdx
== 0) {
451 if (setup
->bottom_edge_rule
== 0){
452 /* correct for top-left fill convention:
454 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
457 /* correct for bottom-left fill convention:
459 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
463 plane
[i
].dcdx
*= FIXED_ONE
;
464 plane
[i
].dcdy
*= FIXED_ONE
;
466 /* find trivial reject offsets for each edge for a single-pixel
467 * sized block. These will be scaled up at each recursive level to
468 * match the active blocksize. Scaling in this way works best if
469 * the blocks are square.
472 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
473 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
479 debug_printf("p0: %08x/%08x/%08x/%08x\n",
485 debug_printf("p1: %08x/%08x/%08x/%08x\n",
491 debug_printf("p0: %08x/%08x/%08x/%08x\n",
500 * When rasterizing scissored tris, use the intersection of the
501 * triangle bounding box and the scissor rect to generate the
504 * This permits us to cut off the triangle "tails" that are present
505 * in the intermediate recursive levels caused when two of the
506 * triangles edges don't diverge quickly enough to trivially reject
507 * exterior blocks from the triangle.
509 * It's not really clear if it's worth worrying about these tails,
510 * but since we generate the planes for each scissored tri, it's
511 * free to trim them in this case.
513 * Note that otherwise, the scissor planes only vary in 'C' value,
514 * and even then only on state-changes. Could alternatively store
515 * these planes elsewhere.
517 if (nr_planes
== 7) {
518 const struct u_rect
*scissor
= &setup
->scissors
[scissor_index
];
522 plane
[3].c
= 1-scissor
->x0
;
527 plane
[4].c
= scissor
->x1
+1;
532 plane
[5].c
= 1-scissor
->y0
;
537 plane
[6].c
= scissor
->y1
+1;
541 return lp_setup_bin_triangle(setup
, tri
, &bbox
, nr_planes
, scissor_index
);
545 * Round to nearest less or equal power of two of the input.
547 * Undefined if no bit set exists, so code should check against 0 first.
549 static INLINE
uint32_t
550 floor_pot(uint32_t n
)
552 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
572 lp_setup_bin_triangle( struct lp_setup_context
*setup
,
573 struct lp_rast_triangle
*tri
,
574 const struct u_rect
*bbox
,
576 unsigned scissor_index
)
578 struct lp_scene
*scene
= setup
->scene
;
579 struct u_rect trimmed_box
= *bbox
;
582 /* What is the largest power-of-two boundary this triangle crosses:
584 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
585 (bbox
->y0
^ bbox
->y1
));
587 /* The largest dimension of the rasterized area of the triangle
588 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
590 int sz
= floor_pot((bbox
->x1
- (bbox
->x0
& ~3)) |
591 (bbox
->y1
- (bbox
->y0
& ~3)));
593 /* Now apply scissor, etc to the bounding box. Could do this
594 * earlier, but it confuses the logic for tri-16 and would force
595 * the rasterizer to also respect scissor, etc, just for the rare
596 * cases where a small triangle extends beyond the scissor.
598 u_rect_find_intersection(&setup
->draw_regions
[scissor_index
],
601 /* Determine which tile(s) intersect the triangle's bounding box
605 int ix0
= bbox
->x0
/ TILE_SIZE
;
606 int iy0
= bbox
->y0
/ TILE_SIZE
;
607 unsigned px
= bbox
->x0
& 63 & ~3;
608 unsigned py
= bbox
->y0
& 63 & ~3;
610 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
611 ix0
== bbox
->x1
/ TILE_SIZE
);
613 if (nr_planes
== 3) {
616 /* Triangle is contained in a single 4x4 stamp:
618 assert(px
+ 4 <= TILE_SIZE
);
619 assert(py
+ 4 <= TILE_SIZE
);
620 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
622 LP_RAST_OP_TRIANGLE_3_4
,
623 lp_rast_arg_triangle_contained(tri
, px
, py
) );
628 /* Triangle is contained in a single 16x16 block:
632 * The 16x16 block is only 4x4 aligned, and can exceed the tile
633 * dimensions if the triangle is 16 pixels in one dimension but 4
634 * in the other. So budge the 16x16 back inside the tile.
636 px
= MIN2(px
, TILE_SIZE
- 16);
637 py
= MIN2(py
, TILE_SIZE
- 16);
639 assert(px
+ 16 <= TILE_SIZE
);
640 assert(py
+ 16 <= TILE_SIZE
);
642 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
644 LP_RAST_OP_TRIANGLE_3_16
,
645 lp_rast_arg_triangle_contained(tri
, px
, py
) );
648 else if (nr_planes
== 4 && sz
< 16)
650 px
= MIN2(px
, TILE_SIZE
- 16);
651 py
= MIN2(py
, TILE_SIZE
- 16);
653 assert(px
+ 16 <= TILE_SIZE
);
654 assert(py
+ 16 <= TILE_SIZE
);
656 return lp_scene_bin_cmd_with_state(scene
, ix0
, iy0
,
658 LP_RAST_OP_TRIANGLE_4_16
,
659 lp_rast_arg_triangle_contained(tri
, px
, py
));
663 /* Triangle is contained in a single tile:
665 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
, setup
->fs
.stored
,
666 lp_rast_tri_tab
[nr_planes
],
667 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1) );
671 struct lp_rast_plane
*plane
= GET_PLANES(tri
);
676 int xstep
[MAX_PLANES
];
677 int ystep
[MAX_PLANES
];
680 int ix0
= trimmed_box
.x0
/ TILE_SIZE
;
681 int iy0
= trimmed_box
.y0
/ TILE_SIZE
;
682 int ix1
= trimmed_box
.x1
/ TILE_SIZE
;
683 int iy1
= trimmed_box
.y1
/ TILE_SIZE
;
685 for (i
= 0; i
< nr_planes
; i
++) {
687 plane
[i
].dcdy
* iy0
* TILE_SIZE
-
688 plane
[i
].dcdx
* ix0
* TILE_SIZE
);
690 ei
[i
] = (plane
[i
].dcdy
-
692 plane
[i
].eo
) << TILE_ORDER
;
694 eo
[i
] = plane
[i
].eo
<< TILE_ORDER
;
695 xstep
[i
] = -(plane
[i
].dcdx
<< TILE_ORDER
);
696 ystep
[i
] = plane
[i
].dcdy
<< TILE_ORDER
;
701 /* Test tile-sized blocks against the triangle.
702 * Discard blocks fully outside the tri. If the block is fully
703 * contained inside the tri, bin an lp_rast_shade_tile command.
704 * Else, bin a lp_rast_triangle command.
706 for (y
= iy0
; y
<= iy1
; y
++)
708 boolean in
= FALSE
; /* are we inside the triangle? */
711 for (i
= 0; i
< nr_planes
; i
++)
714 for (x
= ix0
; x
<= ix1
; x
++)
719 for (i
= 0; i
< nr_planes
; i
++) {
720 int planeout
= cx
[i
] + eo
[i
];
721 int planepartial
= cx
[i
] + ei
[i
] - 1;
722 out
|= (planeout
>> 31);
723 partial
|= (planepartial
>> 31) & (1<<i
);
729 break; /* exiting triangle, all done with this row */
730 LP_COUNT(nr_empty_64
);
733 /* Not trivially accepted by at least one plane -
734 * rasterize/shade partial tile
736 int count
= util_bitcount(partial
);
739 if (!lp_scene_bin_cmd_with_state( scene
, x
, y
,
741 lp_rast_tri_tab
[count
],
742 lp_rast_arg_triangle(tri
, partial
) ))
745 LP_COUNT(nr_partially_covered_64
);
748 /* triangle covers the whole tile- shade whole tile */
749 LP_COUNT(nr_fully_covered_64
);
751 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
755 /* Iterate cx values across the region:
757 for (i
= 0; i
< nr_planes
; i
++)
761 /* Iterate c values down the region:
763 for (i
= 0; i
< nr_planes
; i
++)
771 /* Need to disable any partially binned triangle. This is easier
772 * than trying to locate all the triangle, shade-tile, etc,
773 * commands which may have been binned.
775 tri
->inputs
.disable
= TRUE
;
781 * Try to draw the triangle, restart the scene on failure.
783 static void retry_triangle_ccw( struct lp_setup_context
*setup
,
784 struct fixed_position
* position
,
785 const float (*v0
)[4],
786 const float (*v1
)[4],
787 const float (*v2
)[4],
790 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
792 if (!lp_setup_flush_and_restart(setup
))
795 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
801 * Calculate fixed position data for a triangle
804 calc_fixed_position( struct lp_setup_context
*setup
,
805 struct fixed_position
* position
,
806 const float (*v0
)[4],
807 const float (*v1
)[4],
808 const float (*v2
)[4])
810 position
->x
[0] = subpixel_snap(v0
[0][0] - setup
->pixel_offset
);
811 position
->x
[1] = subpixel_snap(v1
[0][0] - setup
->pixel_offset
);
812 position
->x
[2] = subpixel_snap(v2
[0][0] - setup
->pixel_offset
);
815 position
->y
[0] = subpixel_snap(v0
[0][1] - setup
->pixel_offset
);
816 position
->y
[1] = subpixel_snap(v1
[0][1] - setup
->pixel_offset
);
817 position
->y
[2] = subpixel_snap(v2
[0][1] - setup
->pixel_offset
);
820 position
->dx01
= position
->x
[0] - position
->x
[1];
821 position
->dy01
= position
->y
[0] - position
->y
[1];
823 position
->dx20
= position
->x
[2] - position
->x
[0];
824 position
->dy20
= position
->y
[2] - position
->y
[0];
826 position
->area
= position
->dx01
* position
->dy20
- position
->dx20
* position
->dy01
;
831 * Rotate a triangle, flipping its clockwise direction,
832 * Swaps values for xy[0] and xy[1]
835 rotate_fixed_position_01( struct fixed_position
* position
)
841 position
->x
[1] = position
->x
[0];
842 position
->y
[1] = position
->y
[0];
846 position
->dx01
= -position
->dx01
;
847 position
->dy01
= -position
->dy01
;
848 position
->dx20
= position
->x
[2] - position
->x
[0];
849 position
->dy20
= position
->y
[2] - position
->y
[0];
851 position
->area
= -position
->area
;
856 * Rotate a triangle, flipping its clockwise direction,
857 * Swaps values for xy[1] and xy[2]
860 rotate_fixed_position_12( struct fixed_position
* position
)
866 position
->x
[2] = position
->x
[1];
867 position
->y
[2] = position
->y
[1];
873 position
->dx01
= -position
->dx20
;
874 position
->dy01
= -position
->dy20
;
878 position
->area
= -position
->area
;
882 typedef void (*triangle_func_t
)(struct lp_setup_context
*setup
,
883 const float (*v0
)[4],
884 const float (*v1
)[4],
885 const float (*v2
)[4]);
889 * Subdivide this triangle by bisecting edge (v0, v1).
890 * \param pv the provoking vertex (must = v0 or v1 or v2)
891 * TODO: should probably think about non-overflowing arithmetic elsewhere.
892 * This will definitely screw with pipeline counters for instance.
895 subdiv_tri(struct lp_setup_context
*setup
,
896 const float (*v0
)[4],
897 const float (*v1
)[4],
898 const float (*v2
)[4],
899 const float (*pv
)[4],
902 unsigned n
= setup
->fs
.current
.variant
->shader
->info
.base
.num_inputs
+ 1;
903 const struct lp_shader_input
*inputs
=
904 setup
->fs
.current
.variant
->shader
->inputs
;
905 PIPE_ALIGN_VAR(LP_MIN_VECTOR_ALIGN
) float vmid
[PIPE_MAX_ATTRIBS
][4];
906 const float (*vm
)[4] = (const float (*)[4]) vmid
;
909 boolean flatshade
= setup
->fs
.current
.variant
->key
.flatshade
;
911 /* find position midpoint (attrib[0] = position) */
912 vmid
[0][0] = 0.5f
* (v1
[0][0] + v0
[0][0]);
913 vmid
[0][1] = 0.5f
* (v1
[0][1] + v0
[0][1]);
914 vmid
[0][2] = 0.5f
* (v1
[0][2] + v0
[0][2]);
915 vmid
[0][3] = 0.5f
* (v1
[0][3] + v0
[0][3]);
921 /* interpolate other attributes */
922 for (i
= 1; i
< n
; i
++) {
923 if ((inputs
[i
- 1].interp
== LP_INTERP_COLOR
&& flatshade
) ||
924 inputs
[i
- 1].interp
== LP_INTERP_CONSTANT
) {
925 /* copy the provoking vertex's attribute */
926 vmid
[i
][0] = pv
[i
][0];
927 vmid
[i
][1] = pv
[i
][1];
928 vmid
[i
][2] = pv
[i
][2];
929 vmid
[i
][3] = pv
[i
][3];
932 /* interpolate with perspective correction (for linear too) */
933 vmid
[i
][0] = 0.5f
* (v1
[i
][0] * w1
+ v0
[i
][0] * w0
) / wm
;
934 vmid
[i
][1] = 0.5f
* (v1
[i
][1] * w1
+ v0
[i
][1] * w0
) / wm
;
935 vmid
[i
][2] = 0.5f
* (v1
[i
][2] * w1
+ v0
[i
][2] * w0
) / wm
;
936 vmid
[i
][3] = 0.5f
* (v1
[i
][3] * w1
+ v0
[i
][3] * w0
) / wm
;
940 /* handling flat shading and first vs. last provoking vertex is a
944 if (setup
->flatshade_first
) {
945 /* first vertex must be v0 or vm */
946 tri(setup
, v0
, vm
, v2
);
947 tri(setup
, vm
, v1
, v2
);
950 /* last vertex must be v0 or vm */
951 tri(setup
, vm
, v2
, v0
);
952 tri(setup
, v1
, v2
, vm
);
956 if (setup
->flatshade_first
) {
957 tri(setup
, vm
, v2
, v0
);
958 tri(setup
, v1
, v2
, vm
);
961 tri(setup
, v2
, v0
, vm
);
962 tri(setup
, v2
, vm
, v1
);
966 if (setup
->flatshade_first
) {
967 tri(setup
, v2
, v0
, vm
);
968 tri(setup
, v2
, vm
, v1
);
971 tri(setup
, v0
, vm
, v2
);
972 tri(setup
, vm
, v1
, v2
);
979 * Check the lengths of the edges of the triangle. If any edge is too
980 * long, subdivide the longest edge and draw two sub-triangles.
981 * Note: this may be called recursively.
982 * \return TRUE if triangle was subdivided, FALSE otherwise
985 check_subdivide_triangle(struct lp_setup_context
*setup
,
986 const float (*v0
)[4],
987 const float (*v1
)[4],
988 const float (*v2
)[4],
991 const float maxLen
= (float) MAX_FIXED_LENGTH
; /* longest permissible edge, in pixels */
992 float dx10
, dy10
, len10
;
993 float dx21
, dy21
, len21
;
994 float dx02
, dy02
, len02
;
995 const float (*pv
)[4] = setup
->flatshade_first
? v0
: v2
;
997 /* compute lengths of triangle edges, squared */
998 dx10
= v1
[0][0] - v0
[0][0];
999 dy10
= v1
[0][1] - v0
[0][1];
1000 len10
= dx10
* dx10
+ dy10
* dy10
;
1002 dx21
= v2
[0][0] - v1
[0][0];
1003 dy21
= v2
[0][1] - v1
[0][1];
1004 len21
= dx21
* dx21
+ dy21
* dy21
;
1006 dx02
= v0
[0][0] - v2
[0][0];
1007 dy02
= v0
[0][1] - v2
[0][1];
1008 len02
= dx02
* dx02
+ dy02
* dy02
;
1010 /* Look for longest the edge that's longer than maxLen. If we find
1011 * such an edge, split the triangle using the midpoint of that edge.
1012 * Note: it's important to split the longest edge, not just any edge
1013 * that's longer than maxLen. Otherwise, we can get into a degenerate
1014 * situation and recurse indefinitely.
1016 if (len10
> maxLen
* maxLen
&&
1019 /* subdivide v0, v1 edge */
1020 subdiv_tri(setup
, v0
, v1
, v2
, pv
, tri
);
1024 if (len21
> maxLen
* maxLen
&&
1027 /* subdivide v1, v2 edge */
1028 subdiv_tri(setup
, v1
, v2
, v0
, pv
, tri
);
1032 if (len02
> maxLen
* maxLen
&&
1035 /* subdivide v2, v0 edge */
1036 subdiv_tri(setup
, v2
, v0
, v1
, pv
, tri
);
1045 * Draw triangle if it's CW, cull otherwise.
1047 static void triangle_cw( struct lp_setup_context
*setup
,
1048 const float (*v0
)[4],
1049 const float (*v1
)[4],
1050 const float (*v2
)[4] )
1052 struct fixed_position position
;
1054 if (setup
->subdivide_large_triangles
&&
1055 check_subdivide_triangle(setup
, v0
, v1
, v2
, triangle_cw
))
1058 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1060 if (position
.area
< 0) {
1061 if (setup
->flatshade_first
) {
1062 rotate_fixed_position_12(&position
);
1063 retry_triangle_ccw(setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1065 rotate_fixed_position_01(&position
);
1066 retry_triangle_ccw(setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1072 static void triangle_ccw( 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
;
1079 if (setup
->subdivide_large_triangles
&&
1080 check_subdivide_triangle(setup
, v0
, v1
, v2
, triangle_ccw
))
1083 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1085 if (position
.area
> 0)
1086 retry_triangle_ccw(setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1090 * Draw triangle whether it's CW or CCW.
1092 static void triangle_both( struct lp_setup_context
*setup
,
1093 const float (*v0
)[4],
1094 const float (*v1
)[4],
1095 const float (*v2
)[4] )
1097 struct fixed_position position
;
1098 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
1100 if (setup
->subdivide_large_triangles
&&
1101 check_subdivide_triangle(setup
, v0
, v1
, v2
, triangle_both
))
1104 if (lp_context
->active_statistics_queries
&&
1105 !llvmpipe_rasterization_disabled(lp_context
)) {
1106 lp_context
->pipeline_statistics
.c_primitives
++;
1109 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1112 assert(!util_is_inf_or_nan(v0
[0][0]));
1113 assert(!util_is_inf_or_nan(v0
[0][1]));
1114 assert(!util_is_inf_or_nan(v1
[0][0]));
1115 assert(!util_is_inf_or_nan(v1
[0][1]));
1116 assert(!util_is_inf_or_nan(v2
[0][0]));
1117 assert(!util_is_inf_or_nan(v2
[0][1]));
1120 if (position
.area
> 0)
1121 retry_triangle_ccw( setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1122 else if (position
.area
< 0) {
1123 if (setup
->flatshade_first
) {
1124 rotate_fixed_position_12( &position
);
1125 retry_triangle_ccw( setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1127 rotate_fixed_position_01( &position
);
1128 retry_triangle_ccw( setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1134 static void triangle_nop( struct lp_setup_context
*setup
,
1135 const float (*v0
)[4],
1136 const float (*v1
)[4],
1137 const float (*v2
)[4] )
1143 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
1145 switch (setup
->cullmode
) {
1146 case PIPE_FACE_NONE
:
1147 setup
->triangle
= triangle_both
;
1149 case PIPE_FACE_BACK
:
1150 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
1152 case PIPE_FACE_FRONT
:
1153 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
1156 setup
->triangle
= triangle_nop
;