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) && UTIL_ARCH_LITTLE_ENDIAN
51 #include "util/u_pwr8.h"
54 #if !defined(PIPE_ARCH_SSE)
57 subpixel_snap(float a
)
59 return util_iround(FIXED_ONE
* a
);
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 STATIC_ASSERT(sizeof(struct lp_rast_plane
) % 8 == 0);
96 *tri_size
= (sizeof(struct lp_rast_triangle
) +
100 tri
= lp_scene_alloc_aligned( scene
, *tri_size
, 16 );
104 tri
->inputs
.stride
= input_array_sz
;
107 char *a
= (char *)tri
;
108 char *b
= (char *)&GET_PLANES(tri
)[nr_planes
];
109 assert(b
- a
== *tri_size
);
116 lp_setup_print_vertex(struct lp_setup_context
*setup
,
120 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
123 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
125 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
127 for (i
= 0; i
< key
->num_inputs
; i
++) {
128 const float *in
= v
[key
->inputs
[i
].src_index
];
130 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
132 name
, key
->inputs
[i
].src_index
,
133 (key
->inputs
[i
].usage_mask
& 0x1) ? "x" : " ",
134 (key
->inputs
[i
].usage_mask
& 0x2) ? "y" : " ",
135 (key
->inputs
[i
].usage_mask
& 0x4) ? "z" : " ",
136 (key
->inputs
[i
].usage_mask
& 0x8) ? "w" : " ");
138 for (j
= 0; j
< 4; j
++)
139 if (key
->inputs
[i
].usage_mask
& (1<<j
))
140 debug_printf("%.5f ", in
[j
]);
148 * Print triangle vertex attribs (for debug).
151 lp_setup_print_triangle(struct lp_setup_context
*setup
,
152 const float (*v0
)[4],
153 const float (*v1
)[4],
154 const float (*v2
)[4])
156 debug_printf("triangle\n");
159 const float ex
= v0
[0][0] - v2
[0][0];
160 const float ey
= v0
[0][1] - v2
[0][1];
161 const float fx
= v1
[0][0] - v2
[0][0];
162 const float fy
= v1
[0][1] - v2
[0][1];
164 /* det = cross(e,f).z */
165 const float det
= ex
* fy
- ey
* fx
;
167 debug_printf(" - ccw\n");
169 debug_printf(" - cw\n");
171 debug_printf(" - zero area\n");
174 lp_setup_print_vertex(setup
, "v0", v0
);
175 lp_setup_print_vertex(setup
, "v1", v1
);
176 lp_setup_print_vertex(setup
, "v2", v2
);
182 lp_rast_tri_tab
[MAX_PLANES
+1] = {
183 0, /* should be impossible */
184 LP_RAST_OP_TRIANGLE_1
,
185 LP_RAST_OP_TRIANGLE_2
,
186 LP_RAST_OP_TRIANGLE_3
,
187 LP_RAST_OP_TRIANGLE_4
,
188 LP_RAST_OP_TRIANGLE_5
,
189 LP_RAST_OP_TRIANGLE_6
,
190 LP_RAST_OP_TRIANGLE_7
,
191 LP_RAST_OP_TRIANGLE_8
195 lp_rast_32_tri_tab
[MAX_PLANES
+1] = {
196 0, /* should be impossible */
197 LP_RAST_OP_TRIANGLE_32_1
,
198 LP_RAST_OP_TRIANGLE_32_2
,
199 LP_RAST_OP_TRIANGLE_32_3
,
200 LP_RAST_OP_TRIANGLE_32_4
,
201 LP_RAST_OP_TRIANGLE_32_5
,
202 LP_RAST_OP_TRIANGLE_32_6
,
203 LP_RAST_OP_TRIANGLE_32_7
,
204 LP_RAST_OP_TRIANGLE_32_8
208 lp_rast_ms_tri_tab
[MAX_PLANES
+1] = {
209 0, /* should be impossible */
210 LP_RAST_OP_MS_TRIANGLE_1
,
211 LP_RAST_OP_MS_TRIANGLE_2
,
212 LP_RAST_OP_MS_TRIANGLE_3
,
213 LP_RAST_OP_MS_TRIANGLE_4
,
214 LP_RAST_OP_MS_TRIANGLE_5
,
215 LP_RAST_OP_MS_TRIANGLE_6
,
216 LP_RAST_OP_MS_TRIANGLE_7
,
217 LP_RAST_OP_MS_TRIANGLE_8
221 * The primitive covers the whole tile- shade whole tile.
223 * \param tx, ty the tile position in tiles, not pixels
226 lp_setup_whole_tile(struct lp_setup_context
*setup
,
227 const struct lp_rast_shader_inputs
*inputs
,
230 struct lp_scene
*scene
= setup
->scene
;
232 LP_COUNT(nr_fully_covered_64
);
234 /* if variant is opaque and scissor doesn't effect the tile */
235 if (inputs
->opaque
) {
236 /* Several things prevent this optimization from working:
237 * - For layered rendering we can't determine if this covers the same layer
238 * as previous rendering (or in case of clears those actually always cover
239 * all layers so optimization is impossible). Need to use fb_max_layer and
240 * not setup->layer_slot to determine this since even if there's currently
241 * no slot assigned previous rendering could have used one.
242 * - If there were any Begin/End query commands in the scene then those
243 * would get removed which would be very wrong. Furthermore, if queries
244 * were just active we also can't do the optimization since to get
245 * accurate query results we unfortunately need to execute the rendering
248 if (!scene
->fb
.zsbuf
&& scene
->fb_max_layer
== 0 && !scene
->had_queries
) {
250 * All previous rendering will be overwritten so reset the bin.
252 lp_scene_bin_reset( scene
, tx
, ty
);
255 LP_COUNT(nr_shade_opaque_64
);
256 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
258 LP_RAST_OP_SHADE_TILE_OPAQUE
,
259 lp_rast_arg_inputs(inputs
) );
261 LP_COUNT(nr_shade_64
);
262 return lp_scene_bin_cmd_with_state( scene
, tx
, ty
,
264 LP_RAST_OP_SHADE_TILE
,
265 lp_rast_arg_inputs(inputs
) );
271 * Do basic setup for triangle rasterization and determine which
272 * framebuffer tiles are touched. Put the triangle in the scene's
273 * bins for the tiles which we overlap.
276 do_triangle_ccw(struct lp_setup_context
*setup
,
277 struct fixed_position
* position
,
278 const float (*v0
)[4],
279 const float (*v1
)[4],
280 const float (*v2
)[4],
281 boolean frontfacing
)
283 struct lp_scene
*scene
= setup
->scene
;
284 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
285 struct lp_rast_triangle
*tri
;
286 struct lp_rast_plane
*plane
;
287 const struct u_rect
*scissor
= NULL
;
288 struct u_rect bbox
, bboxpos
;
292 unsigned viewport_index
= 0;
294 const float (*pv
)[4];
296 /* Area should always be positive here */
297 assert(position
->area
> 0);
300 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
302 if (setup
->flatshade_first
) {
308 if (setup
->viewport_index_slot
> 0) {
309 unsigned *udata
= (unsigned*)pv
[setup
->viewport_index_slot
];
310 viewport_index
= lp_clamp_viewport_idx(*udata
);
312 if (setup
->layer_slot
> 0) {
313 layer
= *(unsigned*)pv
[setup
->layer_slot
];
314 layer
= MIN2(layer
, scene
->fb_max_layer
);
317 /* Bounding rectangle (in pixels) */
319 /* Yes this is necessary to accurately calculate bounding boxes
320 * with the two fill-conventions we support. GL (normally) ends
321 * up needing a bottom-left fill convention, which requires
322 * slightly different rounding.
324 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
326 /* Inclusive x0, exclusive x1 */
327 bbox
.x0
= MIN3(position
->x
[0], position
->x
[1], position
->x
[2]) >> FIXED_ORDER
;
328 bbox
.x1
= (MAX3(position
->x
[0], position
->x
[1], position
->x
[2]) - 1) >> FIXED_ORDER
;
330 /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
331 bbox
.y0
= (MIN3(position
->y
[0], position
->y
[1], position
->y
[2]) + adj
) >> FIXED_ORDER
;
332 bbox
.y1
= (MAX3(position
->y
[0], position
->y
[1], position
->y
[2]) - 1 + adj
) >> FIXED_ORDER
;
335 if (bbox
.x1
< bbox
.x0
||
337 if (0) debug_printf("empty bounding box\n");
338 LP_COUNT(nr_culled_tris
);
342 if (!u_rect_test_intersection(&setup
->draw_regions
[viewport_index
], &bbox
)) {
343 if (0) debug_printf("offscreen\n");
344 LP_COUNT(nr_culled_tris
);
350 /* Can safely discard negative regions, but need to keep hold of
351 * information about when the triangle extends past screen
352 * boundaries. See trimmed_box in lp_setup_bin_triangle().
354 bboxpos
.x0
= MAX2(bboxpos
.x0
, 0);
355 bboxpos
.y0
= MAX2(bboxpos
.y0
, 0);
359 * Determine how many scissor planes we need, that is drop scissor
360 * edges if the bounding box of the tri is fully inside that edge.
362 if (setup
->scissor_test
) {
363 /* why not just use draw_regions */
364 scissor
= &setup
->scissors
[viewport_index
];
365 scissor_planes_needed(s_planes
, &bboxpos
, scissor
);
366 nr_planes
+= s_planes
[0] + s_planes
[1] + s_planes
[2] + s_planes
[3];
368 scissor
= &setup
->draw_regions
[viewport_index
];
369 scissor_planes_needed(s_planes
, &bboxpos
, scissor
);
370 nr_planes
+= s_planes
[0] + s_planes
[1] + s_planes
[2] + s_planes
[3];
373 tri
= lp_setup_alloc_triangle(scene
,
381 tri
->v
[0][0] = v0
[0][0];
382 tri
->v
[1][0] = v1
[0][0];
383 tri
->v
[2][0] = v2
[0][0];
384 tri
->v
[0][1] = v0
[0][1];
385 tri
->v
[1][1] = v1
[0][1];
386 tri
->v
[2][1] = v2
[0][1];
391 /* Setup parameter interpolants:
393 setup
->setup
.variant
->jit_function(v0
, v1
, v2
,
395 GET_A0(&tri
->inputs
),
396 GET_DADX(&tri
->inputs
),
397 GET_DADY(&tri
->inputs
));
399 tri
->inputs
.frontfacing
= frontfacing
;
400 tri
->inputs
.disable
= FALSE
;
401 tri
->inputs
.opaque
= setup
->fs
.current
.variant
->opaque
;
402 tri
->inputs
.layer
= layer
;
403 tri
->inputs
.viewport_index
= viewport_index
;
406 lp_dump_setup_coef(&setup
->setup
.variant
->key
,
407 (const float (*)[4])GET_A0(&tri
->inputs
),
408 (const float (*)[4])GET_DADX(&tri
->inputs
),
409 (const float (*)[4])GET_DADY(&tri
->inputs
));
411 plane
= GET_PLANES(tri
);
413 #if defined(PIPE_ARCH_SSE)
415 __m128i vertx
, verty
;
416 __m128i shufx
, shufy
;
418 __m128i cdx02
, cdx13
, cdy02
, cdy13
, c02
, c13
;
419 __m128i c01
, c23
, unused
;
420 __m128i dcdx_neg_mask
;
421 __m128i dcdy_neg_mask
;
422 __m128i dcdx_zero_mask
;
423 __m128i top_left_flag
, c_dec
;
424 __m128i eo
, p0
, p1
, p2
;
425 __m128i zero
= _mm_setzero_si128();
427 vertx
= _mm_load_si128((__m128i
*)position
->x
); /* vertex x coords */
428 verty
= _mm_load_si128((__m128i
*)position
->y
); /* vertex y coords */
430 shufx
= _mm_shuffle_epi32(vertx
, _MM_SHUFFLE(3,0,2,1));
431 shufy
= _mm_shuffle_epi32(verty
, _MM_SHUFFLE(3,0,2,1));
433 dcdx
= _mm_sub_epi32(verty
, shufy
);
434 dcdy
= _mm_sub_epi32(vertx
, shufx
);
436 dcdx_neg_mask
= _mm_srai_epi32(dcdx
, 31);
437 dcdx_zero_mask
= _mm_cmpeq_epi32(dcdx
, zero
);
438 dcdy_neg_mask
= _mm_srai_epi32(dcdy
, 31);
440 top_left_flag
= _mm_set1_epi32((setup
->bottom_edge_rule
== 0) ? ~0 : 0);
442 c_dec
= _mm_or_si128(dcdx_neg_mask
,
443 _mm_and_si128(dcdx_zero_mask
,
444 _mm_xor_si128(dcdy_neg_mask
,
449 * Note we need _signed_ mul (_mm_mul_epi32) which we emulate.
451 cdx02
= mm_mullohi_epi32(dcdx
, vertx
, &cdx13
);
452 cdy02
= mm_mullohi_epi32(dcdy
, verty
, &cdy13
);
453 c02
= _mm_sub_epi64(cdx02
, cdy02
);
454 c13
= _mm_sub_epi64(cdx13
, cdy13
);
455 c02
= _mm_sub_epi64(c02
, _mm_shuffle_epi32(c_dec
,
456 _MM_SHUFFLE(2,2,0,0)));
457 c13
= _mm_sub_epi64(c13
, _mm_shuffle_epi32(c_dec
,
458 _MM_SHUFFLE(3,3,1,1)));
461 * Useful for very small fbs/tris (or fewer subpixel bits) only:
462 * c = _mm_sub_epi32(mm_mullo_epi32(dcdx, vertx),
463 * mm_mullo_epi32(dcdy, verty));
465 * c = _mm_sub_epi32(c, c_dec);
468 /* Scale up to match c:
470 dcdx
= _mm_slli_epi32(dcdx
, FIXED_ORDER
);
471 dcdy
= _mm_slli_epi32(dcdy
, FIXED_ORDER
);
474 * Calculate trivial reject values:
475 * Note eo cannot overflow even if dcdx/dcdy would already have
476 * 31 bits (which they shouldn't have). This is because eo
477 * is never negative (albeit if we rely on that need to be careful...)
479 eo
= _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask
, dcdy
),
480 _mm_and_si128(dcdx_neg_mask
, dcdx
));
482 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
485 * Pointless transpose which gets undone immediately in
487 * It is actually difficult to do away with it - would essentially
488 * need GET_PLANES_DX, GET_PLANES_DY etc., but the calculations
489 * for this then would need to depend on the number of planes.
490 * The transpose is quite special here due to c being 64bit...
491 * The store has to be unaligned (unless we'd make the plane size
492 * a multiple of 128), and of course storing eo separately...
494 c01
= _mm_unpacklo_epi64(c02
, c13
);
495 c23
= _mm_unpackhi_epi64(c02
, c13
);
496 transpose2_64_2_32(&c01
, &c23
, &dcdx
, &dcdy
,
497 &p0
, &p1
, &p2
, &unused
);
498 _mm_storeu_si128((__m128i
*)&plane
[0], p0
);
499 plane
[0].eo
= (uint32_t)_mm_cvtsi128_si32(eo
);
500 _mm_storeu_si128((__m128i
*)&plane
[1], p1
);
501 eo
= _mm_shuffle_epi32(eo
, _MM_SHUFFLE(3,2,0,1));
502 plane
[1].eo
= (uint32_t)_mm_cvtsi128_si32(eo
);
503 _mm_storeu_si128((__m128i
*)&plane
[2], p2
);
504 eo
= _mm_shuffle_epi32(eo
, _MM_SHUFFLE(0,0,0,2));
505 plane
[2].eo
= (uint32_t)_mm_cvtsi128_si32(eo
);
507 #elif defined(_ARCH_PWR8) && UTIL_ARCH_LITTLE_ENDIAN
509 * XXX this code is effectively disabled for all practical purposes,
510 * as the allowed fb size is tiny if FIXED_ORDER is 8.
512 if (setup
->fb
.width
<= MAX_FIXED_LENGTH32
&&
513 setup
->fb
.height
<= MAX_FIXED_LENGTH32
&&
514 (bbox
.x1
- bbox
.x0
) <= MAX_FIXED_LENGTH32
&&
515 (bbox
.y1
- bbox
.y0
) <= MAX_FIXED_LENGTH32
) {
516 unsigned int bottom_edge
;
517 __m128i vertx
, verty
;
518 __m128i shufx
, shufy
;
519 __m128i dcdx
, dcdy
, c
;
521 __m128i dcdx_neg_mask
;
522 __m128i dcdy_neg_mask
;
523 __m128i dcdx_zero_mask
;
524 __m128i top_left_flag
;
525 __m128i c_inc_mask
, c_inc
;
526 __m128i eo
, p0
, p1
, p2
;
527 __m128i_union vshuf_mask
;
528 __m128i zero
= vec_splats((unsigned char) 0);
529 PIPE_ALIGN_VAR(16) int32_t temp_vec
[4];
531 #if UTIL_ARCH_LITTLE_ENDIAN
532 vshuf_mask
.i
[0] = 0x07060504;
533 vshuf_mask
.i
[1] = 0x0B0A0908;
534 vshuf_mask
.i
[2] = 0x03020100;
535 vshuf_mask
.i
[3] = 0x0F0E0D0C;
537 vshuf_mask
.i
[0] = 0x00010203;
538 vshuf_mask
.i
[1] = 0x0C0D0E0F;
539 vshuf_mask
.i
[2] = 0x04050607;
540 vshuf_mask
.i
[3] = 0x08090A0B;
543 /* vertex x coords */
544 vertx
= vec_load_si128((const uint32_t *) position
->x
);
545 /* vertex y coords */
546 verty
= vec_load_si128((const uint32_t *) position
->y
);
548 shufx
= vec_perm (vertx
, vertx
, vshuf_mask
.m128i
);
549 shufy
= vec_perm (verty
, verty
, vshuf_mask
.m128i
);
551 dcdx
= vec_sub_epi32(verty
, shufy
);
552 dcdy
= vec_sub_epi32(vertx
, shufx
);
554 dcdx_neg_mask
= vec_srai_epi32(dcdx
, 31);
555 dcdx_zero_mask
= vec_cmpeq_epi32(dcdx
, zero
);
556 dcdy_neg_mask
= vec_srai_epi32(dcdy
, 31);
558 bottom_edge
= (setup
->bottom_edge_rule
== 0) ? ~0 : 0;
559 top_left_flag
= (__m128i
) vec_splats(bottom_edge
);
561 c_inc_mask
= vec_or(dcdx_neg_mask
,
562 vec_and(dcdx_zero_mask
,
563 vec_xor(dcdy_neg_mask
,
566 c_inc
= vec_srli_epi32(c_inc_mask
, 31);
568 c
= vec_sub_epi32(vec_mullo_epi32(dcdx
, vertx
),
569 vec_mullo_epi32(dcdy
, verty
));
571 c
= vec_add_epi32(c
, c_inc
);
573 /* Scale up to match c:
575 dcdx
= vec_slli_epi32(dcdx
, FIXED_ORDER
);
576 dcdy
= vec_slli_epi32(dcdy
, FIXED_ORDER
);
578 /* Calculate trivial reject values:
580 eo
= vec_sub_epi32(vec_andnot_si128(dcdy_neg_mask
, dcdy
),
581 vec_and(dcdx_neg_mask
, dcdx
));
583 /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
585 /* Pointless transpose which gets undone immediately in
588 transpose4_epi32(&c
, &dcdx
, &dcdy
, &eo
,
589 &p0
, &p1
, &p2
, &unused
);
591 #define STORE_PLANE(plane, vec) do { \
592 vec_store_si128((uint32_t *)&temp_vec, vec); \
593 plane.c = (int64_t)temp_vec[0]; \
594 plane.dcdx = temp_vec[1]; \
595 plane.dcdy = temp_vec[2]; \
596 plane.eo = temp_vec[3]; \
599 STORE_PLANE(plane
[0], p0
);
600 STORE_PLANE(plane
[1], p1
);
601 STORE_PLANE(plane
[2], p2
);
607 plane
[0].dcdy
= position
->dx01
;
608 plane
[1].dcdy
= position
->x
[1] - position
->x
[2];
609 plane
[2].dcdy
= position
->dx20
;
610 plane
[0].dcdx
= position
->dy01
;
611 plane
[1].dcdx
= position
->y
[1] - position
->y
[2];
612 plane
[2].dcdx
= position
->dy20
;
614 for (i
= 0; i
< 3; i
++) {
615 /* half-edge constants, will be iterated over the whole render
618 plane
[i
].c
= IMUL64(plane
[i
].dcdx
, position
->x
[i
]) -
619 IMUL64(plane
[i
].dcdy
, position
->y
[i
]);
621 /* correct for top-left vs. bottom-left fill convention.
623 if (plane
[i
].dcdx
< 0) {
624 /* both fill conventions want this - adjust for left edges */
627 else if (plane
[i
].dcdx
== 0) {
628 if (setup
->bottom_edge_rule
== 0){
629 /* correct for top-left fill convention:
631 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
634 /* correct for bottom-left fill convention:
636 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
640 /* Scale up to match c:
642 assert((plane
[i
].dcdx
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdx
);
643 assert((plane
[i
].dcdy
<< FIXED_ORDER
) >> FIXED_ORDER
== plane
[i
].dcdy
);
644 plane
[i
].dcdx
<<= FIXED_ORDER
;
645 plane
[i
].dcdy
<<= FIXED_ORDER
;
647 /* find trivial reject offsets for each edge for a single-pixel
648 * sized block. These will be scaled up at each recursive level to
649 * match the active blocksize. Scaling in this way works best if
650 * the blocks are square.
653 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
654 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
659 debug_printf("p0: %"PRIx64
"/%08x/%08x/%08x\n",
665 debug_printf("p1: %"PRIx64
"/%08x/%08x/%08x\n",
671 debug_printf("p2: %"PRIx64
"/%08x/%08x/%08x\n",
680 * When rasterizing scissored tris, use the intersection of the
681 * triangle bounding box and the scissor rect to generate the
684 * This permits us to cut off the triangle "tails" that are present
685 * in the intermediate recursive levels caused when two of the
686 * triangles edges don't diverge quickly enough to trivially reject
687 * exterior blocks from the triangle.
689 * It's not really clear if it's worth worrying about these tails,
690 * but since we generate the planes for each scissored tri, it's
691 * free to trim them in this case.
693 * Note that otherwise, the scissor planes only vary in 'C' value,
694 * and even then only on state-changes. Could alternatively store
695 * these planes elsewhere.
696 * (Or only store the c value together with a bit indicating which
697 * scissor edge this is, so rasterization would treat them differently
698 * (easier to evaluate) to ordinary planes.)
701 /* why not just use draw_regions */
702 struct lp_rast_plane
*plane_s
= &plane
[3];
705 plane_s
->dcdx
= ~0U << 8;
707 plane_s
->c
= (1-scissor
->x0
) << 8;
708 plane_s
->eo
= 1 << 8;
712 plane_s
->dcdx
= 1 << 8;
714 plane_s
->c
= (scissor
->x1
+1) << 8;
715 plane_s
->eo
= 0 << 8;
720 plane_s
->dcdy
= 1 << 8;
721 plane_s
->c
= (1-scissor
->y0
) << 8;
722 plane_s
->eo
= 1 << 8;
727 plane_s
->dcdy
= ~0U << 8;
728 plane_s
->c
= (scissor
->y1
+1) << 8;
732 assert(plane_s
== &plane
[nr_planes
]);
735 return lp_setup_bin_triangle(setup
, tri
, &bbox
, &bboxpos
, nr_planes
, viewport_index
);
739 * Round to nearest less or equal power of two of the input.
741 * Undefined if no bit set exists, so code should check against 0 first.
743 static inline uint32_t
744 floor_pot(uint32_t n
)
746 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
767 lp_setup_bin_triangle(struct lp_setup_context
*setup
,
768 struct lp_rast_triangle
*tri
,
769 const struct u_rect
*bboxorig
,
770 const struct u_rect
*bbox
,
772 unsigned viewport_index
)
774 struct lp_scene
*scene
= setup
->scene
;
775 struct u_rect trimmed_box
= *bbox
;
779 /* What is the largest power-of-two boundary this triangle crosses:
781 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
782 (bbox
->y0
^ bbox
->y1
));
784 /* The largest dimension of the rasterized area of the triangle
785 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
787 int max_sz
= ((bbox
->x1
- (bbox
->x0
& ~3)) |
788 (bbox
->y1
- (bbox
->y0
& ~3)));
789 int sz
= floor_pot(max_sz
);
792 * NOTE: It is important to use the original bounding box
793 * which might contain negative values here, because if the
794 * plane math may overflow or not with the 32bit rasterization
795 * functions depends on the original extent of the triangle.
797 int max_szorig
= ((bboxorig
->x1
- (bboxorig
->x0
& ~3)) |
798 (bboxorig
->y1
- (bboxorig
->y0
& ~3)));
799 boolean use_32bits
= max_szorig
<= MAX_FIXED_LENGTH32
;
801 /* Now apply scissor, etc to the bounding box. Could do this
802 * earlier, but it confuses the logic for tri-16 and would force
803 * the rasterizer to also respect scissor, etc, just for the rare
804 * cases where a small triangle extends beyond the scissor.
806 u_rect_find_intersection(&setup
->draw_regions
[viewport_index
],
809 /* Determine which tile(s) intersect the triangle's bounding box
813 int ix0
= bbox
->x0
/ TILE_SIZE
;
814 int iy0
= bbox
->y0
/ TILE_SIZE
;
815 unsigned px
= bbox
->x0
& 63 & ~3;
816 unsigned py
= bbox
->y0
& 63 & ~3;
818 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
819 ix0
== bbox
->x1
/ TILE_SIZE
);
821 if (nr_planes
== 3) {
824 /* Triangle is contained in a single 4x4 stamp:
826 assert(px
+ 4 <= TILE_SIZE
);
827 assert(py
+ 4 <= TILE_SIZE
);
828 if (setup
->multisample
)
829 cmd
= LP_RAST_OP_MS_TRIANGLE_3_4
;
831 cmd
= use_32bits
? LP_RAST_OP_TRIANGLE_32_3_4
: LP_RAST_OP_TRIANGLE_3_4
;
832 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
833 setup
->fs
.stored
, cmd
,
834 lp_rast_arg_triangle_contained(tri
, px
, py
) );
839 /* Triangle is contained in a single 16x16 block:
843 * The 16x16 block is only 4x4 aligned, and can exceed the tile
844 * dimensions if the triangle is 16 pixels in one dimension but 4
845 * in the other. So budge the 16x16 back inside the tile.
847 px
= MIN2(px
, TILE_SIZE
- 16);
848 py
= MIN2(py
, TILE_SIZE
- 16);
850 assert(px
+ 16 <= TILE_SIZE
);
851 assert(py
+ 16 <= TILE_SIZE
);
853 if (setup
->multisample
)
854 cmd
= LP_RAST_OP_MS_TRIANGLE_3_16
;
856 cmd
= use_32bits
? LP_RAST_OP_TRIANGLE_32_3_16
: LP_RAST_OP_TRIANGLE_3_16
;
857 return lp_scene_bin_cmd_with_state( scene
, ix0
, iy0
,
858 setup
->fs
.stored
, cmd
,
859 lp_rast_arg_triangle_contained(tri
, px
, py
) );
862 else if (nr_planes
== 4 && sz
< 16)
864 px
= MIN2(px
, TILE_SIZE
- 16);
865 py
= MIN2(py
, TILE_SIZE
- 16);
867 assert(px
+ 16 <= TILE_SIZE
);
868 assert(py
+ 16 <= TILE_SIZE
);
870 if (setup
->multisample
)
871 cmd
= LP_RAST_OP_MS_TRIANGLE_4_16
;
873 cmd
= use_32bits
? LP_RAST_OP_TRIANGLE_32_4_16
: LP_RAST_OP_TRIANGLE_4_16
;
874 return lp_scene_bin_cmd_with_state(scene
, ix0
, iy0
,
875 setup
->fs
.stored
, cmd
,
876 lp_rast_arg_triangle_contained(tri
, px
, py
));
880 /* Triangle is contained in a single tile:
882 if (setup
->multisample
)
883 cmd
= lp_rast_ms_tri_tab
[nr_planes
];
885 cmd
= use_32bits
? lp_rast_32_tri_tab
[nr_planes
] : lp_rast_tri_tab
[nr_planes
];
886 return lp_scene_bin_cmd_with_state(
887 scene
, ix0
, iy0
, setup
->fs
.stored
, cmd
,
888 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1));
892 struct lp_rast_plane
*plane
= GET_PLANES(tri
);
893 int64_t c
[MAX_PLANES
];
894 int64_t ei
[MAX_PLANES
];
896 int64_t eo
[MAX_PLANES
];
897 int64_t xstep
[MAX_PLANES
];
898 int64_t ystep
[MAX_PLANES
];
901 int ix0
= trimmed_box
.x0
/ TILE_SIZE
;
902 int iy0
= trimmed_box
.y0
/ TILE_SIZE
;
903 int ix1
= trimmed_box
.x1
/ TILE_SIZE
;
904 int iy1
= trimmed_box
.y1
/ TILE_SIZE
;
906 for (i
= 0; i
< nr_planes
; i
++) {
908 IMUL64(plane
[i
].dcdy
, iy0
) * TILE_SIZE
-
909 IMUL64(plane
[i
].dcdx
, ix0
) * TILE_SIZE
);
911 ei
[i
] = (plane
[i
].dcdy
-
913 (int64_t)plane
[i
].eo
) << TILE_ORDER
;
915 eo
[i
] = (int64_t)plane
[i
].eo
<< TILE_ORDER
;
916 xstep
[i
] = -(((int64_t)plane
[i
].dcdx
) << TILE_ORDER
);
917 ystep
[i
] = ((int64_t)plane
[i
].dcdy
) << TILE_ORDER
;
922 /* Test tile-sized blocks against the triangle.
923 * Discard blocks fully outside the tri. If the block is fully
924 * contained inside the tri, bin an lp_rast_shade_tile command.
925 * Else, bin a lp_rast_triangle command.
927 for (y
= iy0
; y
<= iy1
; y
++)
929 boolean in
= FALSE
; /* are we inside the triangle? */
930 int64_t cx
[MAX_PLANES
];
932 for (i
= 0; i
< nr_planes
; i
++)
935 for (x
= ix0
; x
<= ix1
; x
++)
940 for (i
= 0; i
< nr_planes
; i
++) {
941 int64_t planeout
= cx
[i
] + eo
[i
];
942 int64_t planepartial
= cx
[i
] + ei
[i
] - 1;
943 out
|= (int) (planeout
>> 63);
944 partial
|= ((int) (planepartial
>> 63)) & (1<<i
);
950 break; /* exiting triangle, all done with this row */
951 LP_COUNT(nr_empty_64
);
954 /* Not trivially accepted by at least one plane -
955 * rasterize/shade partial tile
957 int count
= util_bitcount(partial
);
960 if (setup
->multisample
)
961 cmd
= lp_rast_ms_tri_tab
[count
];
963 cmd
= use_32bits
? lp_rast_32_tri_tab
[count
] : lp_rast_tri_tab
[count
];
964 if (!lp_scene_bin_cmd_with_state( scene
, x
, y
,
965 setup
->fs
.stored
, cmd
,
966 lp_rast_arg_triangle(tri
, partial
) ))
969 LP_COUNT(nr_partially_covered_64
);
972 /* triangle covers the whole tile- shade whole tile */
973 LP_COUNT(nr_fully_covered_64
);
975 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
979 /* Iterate cx values across the region: */
980 for (i
= 0; i
< nr_planes
; i
++)
984 /* Iterate c values down the region: */
985 for (i
= 0; i
< nr_planes
; i
++)
993 /* Need to disable any partially binned triangle. This is easier
994 * than trying to locate all the triangle, shade-tile, etc,
995 * commands which may have been binned.
997 tri
->inputs
.disable
= TRUE
;
1003 * Try to draw the triangle, restart the scene on failure.
1005 static void retry_triangle_ccw( struct lp_setup_context
*setup
,
1006 struct fixed_position
* position
,
1007 const float (*v0
)[4],
1008 const float (*v1
)[4],
1009 const float (*v2
)[4],
1012 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
1014 if (!lp_setup_flush_and_restart(setup
))
1017 if (!do_triangle_ccw( setup
, position
, v0
, v1
, v2
, front
))
1023 * Calculate fixed position data for a triangle
1024 * It is unfortunate we need to do that here (as we need area
1025 * calculated in fixed point), as there's quite some code duplication
1026 * to what is done in the jit setup prog.
1029 calc_fixed_position(struct lp_setup_context
*setup
,
1030 struct fixed_position
* position
,
1031 const float (*v0
)[4],
1032 const float (*v1
)[4],
1033 const float (*v2
)[4])
1035 float pixel_offset
= setup
->multisample
? 0.0 : setup
->pixel_offset
;
1037 * The rounding may not be quite the same with PIPE_ARCH_SSE
1038 * (util_iround right now only does nearest/even on x87,
1039 * otherwise nearest/away-from-zero).
1040 * Both should be acceptable, I think.
1042 #if defined(PIPE_ARCH_SSE)
1044 __m128 vxy0xy2
, vxy1xy0
;
1045 __m128i vxy0xy2i
, vxy1xy0i
;
1046 __m128i dxdy0120
, x0x2y0y2
, x1x0y1y0
, x0120
, y0120
;
1047 __m128 pix_offset
= _mm_set1_ps(pixel_offset
);
1048 __m128 fixed_one
= _mm_set1_ps((float)FIXED_ONE
);
1049 v0r
= _mm_castpd_ps(_mm_load_sd((double *)v0
[0]));
1050 vxy0xy2
= _mm_loadh_pi(v0r
, (__m64
*)v2
[0]);
1051 v1r
= _mm_castpd_ps(_mm_load_sd((double *)v1
[0]));
1052 vxy1xy0
= _mm_movelh_ps(v1r
, vxy0xy2
);
1053 vxy0xy2
= _mm_sub_ps(vxy0xy2
, pix_offset
);
1054 vxy1xy0
= _mm_sub_ps(vxy1xy0
, pix_offset
);
1055 vxy0xy2
= _mm_mul_ps(vxy0xy2
, fixed_one
);
1056 vxy1xy0
= _mm_mul_ps(vxy1xy0
, fixed_one
);
1057 vxy0xy2i
= _mm_cvtps_epi32(vxy0xy2
);
1058 vxy1xy0i
= _mm_cvtps_epi32(vxy1xy0
);
1059 dxdy0120
= _mm_sub_epi32(vxy0xy2i
, vxy1xy0i
);
1060 _mm_store_si128((__m128i
*)&position
->dx01
, dxdy0120
);
1062 * For the mul, would need some more shuffles, plus emulation
1063 * for the signed mul (without sse41), so don't bother.
1065 x0x2y0y2
= _mm_shuffle_epi32(vxy0xy2i
, _MM_SHUFFLE(3,1,2,0));
1066 x1x0y1y0
= _mm_shuffle_epi32(vxy1xy0i
, _MM_SHUFFLE(3,1,2,0));
1067 x0120
= _mm_unpacklo_epi32(x0x2y0y2
, x1x0y1y0
);
1068 y0120
= _mm_unpackhi_epi32(x0x2y0y2
, x1x0y1y0
);
1069 _mm_store_si128((__m128i
*)&position
->x
[0], x0120
);
1070 _mm_store_si128((__m128i
*)&position
->y
[0], y0120
);
1073 position
->x
[0] = subpixel_snap(v0
[0][0] - pixel_offset
);
1074 position
->x
[1] = subpixel_snap(v1
[0][0] - pixel_offset
);
1075 position
->x
[2] = subpixel_snap(v2
[0][0] - pixel_offset
);
1076 position
->x
[3] = 0; // should be unused
1078 position
->y
[0] = subpixel_snap(v0
[0][1] - pixel_offset
);
1079 position
->y
[1] = subpixel_snap(v1
[0][1] - pixel_offset
);
1080 position
->y
[2] = subpixel_snap(v2
[0][1] - pixel_offset
);
1081 position
->y
[3] = 0; // should be unused
1083 position
->dx01
= position
->x
[0] - position
->x
[1];
1084 position
->dy01
= position
->y
[0] - position
->y
[1];
1086 position
->dx20
= position
->x
[2] - position
->x
[0];
1087 position
->dy20
= position
->y
[2] - position
->y
[0];
1090 position
->area
= IMUL64(position
->dx01
, position
->dy20
) -
1091 IMUL64(position
->dx20
, position
->dy01
);
1096 * Rotate a triangle, flipping its clockwise direction,
1097 * Swaps values for xy[0] and xy[1]
1100 rotate_fixed_position_01( struct fixed_position
* position
)
1106 position
->x
[1] = position
->x
[0];
1107 position
->y
[1] = position
->y
[0];
1111 position
->dx01
= -position
->dx01
;
1112 position
->dy01
= -position
->dy01
;
1113 position
->dx20
= position
->x
[2] - position
->x
[0];
1114 position
->dy20
= position
->y
[2] - position
->y
[0];
1116 position
->area
= -position
->area
;
1121 * Rotate a triangle, flipping its clockwise direction,
1122 * Swaps values for xy[1] and xy[2]
1125 rotate_fixed_position_12( struct fixed_position
* position
)
1131 position
->x
[2] = position
->x
[1];
1132 position
->y
[2] = position
->y
[1];
1138 position
->dx01
= -position
->dx20
;
1139 position
->dy01
= -position
->dy20
;
1140 position
->dx20
= -x
;
1141 position
->dy20
= -y
;
1143 position
->area
= -position
->area
;
1148 * Draw triangle if it's CW, cull otherwise.
1150 static void triangle_cw(struct lp_setup_context
*setup
,
1151 const float (*v0
)[4],
1152 const float (*v1
)[4],
1153 const float (*v2
)[4])
1155 PIPE_ALIGN_VAR(16) struct fixed_position position
;
1156 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
1158 if (lp_context
->active_statistics_queries
) {
1159 lp_context
->pipeline_statistics
.c_primitives
++;
1162 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1164 if (position
.area
< 0) {
1165 if (setup
->flatshade_first
) {
1166 rotate_fixed_position_12(&position
);
1167 retry_triangle_ccw(setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1169 rotate_fixed_position_01(&position
);
1170 retry_triangle_ccw(setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1176 static void triangle_ccw(struct lp_setup_context
*setup
,
1177 const float (*v0
)[4],
1178 const float (*v1
)[4],
1179 const float (*v2
)[4])
1181 PIPE_ALIGN_VAR(16) struct fixed_position position
;
1182 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
1184 if (lp_context
->active_statistics_queries
) {
1185 lp_context
->pipeline_statistics
.c_primitives
++;
1188 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1190 if (position
.area
> 0)
1191 retry_triangle_ccw(setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1195 * Draw triangle whether it's CW or CCW.
1197 static void triangle_both(struct lp_setup_context
*setup
,
1198 const float (*v0
)[4],
1199 const float (*v1
)[4],
1200 const float (*v2
)[4])
1202 PIPE_ALIGN_VAR(16) struct fixed_position position
;
1203 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
1205 if (lp_context
->active_statistics_queries
) {
1206 lp_context
->pipeline_statistics
.c_primitives
++;
1209 calc_fixed_position(setup
, &position
, v0
, v1
, v2
);
1212 assert(!util_is_inf_or_nan(v0
[0][0]));
1213 assert(!util_is_inf_or_nan(v0
[0][1]));
1214 assert(!util_is_inf_or_nan(v1
[0][0]));
1215 assert(!util_is_inf_or_nan(v1
[0][1]));
1216 assert(!util_is_inf_or_nan(v2
[0][0]));
1217 assert(!util_is_inf_or_nan(v2
[0][1]));
1220 if (position
.area
> 0)
1221 retry_triangle_ccw( setup
, &position
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
1222 else if (position
.area
< 0) {
1223 if (setup
->flatshade_first
) {
1224 rotate_fixed_position_12( &position
);
1225 retry_triangle_ccw( setup
, &position
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
1227 rotate_fixed_position_01( &position
);
1228 retry_triangle_ccw( setup
, &position
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
1234 static void triangle_noop(struct lp_setup_context
*setup
,
1235 const float (*v0
)[4],
1236 const float (*v1
)[4],
1237 const float (*v2
)[4])
1243 lp_setup_choose_triangle(struct lp_setup_context
*setup
)
1245 if (setup
->rasterizer_discard
) {
1246 setup
->triangle
= triangle_noop
;
1249 switch (setup
->cullmode
) {
1250 case PIPE_FACE_NONE
:
1251 setup
->triangle
= triangle_both
;
1253 case PIPE_FACE_BACK
:
1254 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
1256 case PIPE_FACE_FRONT
:
1257 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
1260 setup
->triangle
= triangle_noop
;