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"
36 #include "lp_setup_context.h"
37 #include "lp_setup_coef.h"
39 #include "lp_state_fs.h"
41 #define NUM_CHANNELS 4
46 subpixel_snap(float a
)
48 return util_iround(FIXED_ONE
* a
);
54 return a
* (1.0 / FIXED_ONE
);
64 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
65 * immediately after it.
66 * The memory is allocated from the per-scene pool, not per-tile.
67 * \param tri_size returns number of bytes allocated
68 * \param nr_inputs number of fragment shader inputs
69 * \return pointer to triangle space
71 struct lp_rast_triangle
*
72 lp_setup_alloc_triangle(struct lp_scene
*scene
,
77 unsigned input_array_sz
= NUM_CHANNELS
* (nr_inputs
+ 1) * sizeof(float);
78 struct lp_rast_triangle
*tri
;
79 unsigned tri_bytes
, bytes
;
82 tri_bytes
= align(Offset(struct lp_rast_triangle
, plane
[nr_planes
]), 16);
83 bytes
= tri_bytes
+ (3 * input_array_sz
);
85 tri
= lp_scene_alloc_aligned( scene
, bytes
, 16 );
88 inputs
= ((char *)tri
) + tri_bytes
;
89 tri
->inputs
.a0
= (float (*)[4]) inputs
;
90 tri
->inputs
.dadx
= (float (*)[4]) (inputs
+ input_array_sz
);
91 tri
->inputs
.dady
= (float (*)[4]) (inputs
+ 2 * input_array_sz
);
100 lp_setup_print_vertex(struct lp_setup_context
*setup
,
106 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
108 v
[0][0], v
[0][1], v
[0][2], v
[0][3]);
110 for (i
= 0; i
< setup
->fs
.nr_inputs
; i
++) {
111 const float *in
= v
[setup
->fs
.input
[i
].src_index
];
113 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
115 name
, setup
->fs
.input
[i
].src_index
,
116 (setup
->fs
.input
[i
].usage_mask
& 0x1) ? "x" : " ",
117 (setup
->fs
.input
[i
].usage_mask
& 0x2) ? "y" : " ",
118 (setup
->fs
.input
[i
].usage_mask
& 0x4) ? "z" : " ",
119 (setup
->fs
.input
[i
].usage_mask
& 0x8) ? "w" : " ");
121 for (j
= 0; j
< 4; j
++)
122 if (setup
->fs
.input
[i
].usage_mask
& (1<<j
))
123 debug_printf("%.5f ", in
[j
]);
131 * Print triangle vertex attribs (for debug).
134 lp_setup_print_triangle(struct lp_setup_context
*setup
,
135 const float (*v0
)[4],
136 const float (*v1
)[4],
137 const float (*v2
)[4])
139 debug_printf("triangle\n");
142 const float ex
= v0
[0][0] - v2
[0][0];
143 const float ey
= v0
[0][1] - v2
[0][1];
144 const float fx
= v1
[0][0] - v2
[0][0];
145 const float fy
= v1
[0][1] - v2
[0][1];
147 /* det = cross(e,f).z */
148 const float det
= ex
* fy
- ey
* fx
;
150 debug_printf(" - ccw\n");
152 debug_printf(" - cw\n");
154 debug_printf(" - zero area\n");
157 lp_setup_print_vertex(setup
, "v0", v0
);
158 lp_setup_print_vertex(setup
, "v1", v1
);
159 lp_setup_print_vertex(setup
, "v2", v2
);
165 lp_rast_tri_tab
[MAX_PLANES
+1] = {
166 0, /* should be impossible */
167 LP_RAST_OP_TRIANGLE_1
,
168 LP_RAST_OP_TRIANGLE_2
,
169 LP_RAST_OP_TRIANGLE_3
,
170 LP_RAST_OP_TRIANGLE_4
,
171 LP_RAST_OP_TRIANGLE_5
,
172 LP_RAST_OP_TRIANGLE_6
,
173 LP_RAST_OP_TRIANGLE_7
,
174 LP_RAST_OP_TRIANGLE_8
180 * The primitive covers the whole tile- shade whole tile.
182 * \param tx, ty the tile position in tiles, not pixels
185 lp_setup_whole_tile(struct lp_setup_context
*setup
,
186 const struct lp_rast_shader_inputs
*inputs
,
189 struct lp_scene
*scene
= setup
->scene
;
191 LP_COUNT(nr_fully_covered_64
);
193 /* if variant is opaque and scissor doesn't effect the tile */
194 if (inputs
->opaque
) {
195 if (!scene
->fb
.zsbuf
) {
197 * All previous rendering will be overwritten so reset the bin.
199 lp_scene_bin_reset( scene
, tx
, ty
);
202 LP_COUNT(nr_shade_opaque_64
);
203 return lp_scene_bin_command( scene
, tx
, ty
,
204 LP_RAST_OP_SHADE_TILE_OPAQUE
,
205 lp_rast_arg_inputs(inputs
) );
207 LP_COUNT(nr_shade_64
);
208 return lp_scene_bin_command( scene
, tx
, ty
,
209 LP_RAST_OP_SHADE_TILE
,
210 lp_rast_arg_inputs(inputs
) );
216 * Do basic setup for triangle rasterization and determine which
217 * framebuffer tiles are touched. Put the triangle in the scene's
218 * bins for the tiles which we overlap.
221 do_triangle_ccw(struct lp_setup_context
*setup
,
222 const float (*v0
)[4],
223 const float (*v1
)[4],
224 const float (*v2
)[4],
225 boolean frontfacing
)
227 struct lp_scene
*scene
= setup
->scene
;
228 struct lp_rast_triangle
*tri
;
237 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
239 if (setup
->scissor_test
) {
246 /* x/y positions in fixed point */
247 x
[0] = subpixel_snap(v0
[0][0] - setup
->pixel_offset
);
248 x
[1] = subpixel_snap(v1
[0][0] - setup
->pixel_offset
);
249 x
[2] = subpixel_snap(v2
[0][0] - setup
->pixel_offset
);
250 y
[0] = subpixel_snap(v0
[0][1] - setup
->pixel_offset
);
251 y
[1] = subpixel_snap(v1
[0][1] - setup
->pixel_offset
);
252 y
[2] = subpixel_snap(v2
[0][1] - setup
->pixel_offset
);
255 /* Bounding rectangle (in pixels) */
257 /* Yes this is necessary to accurately calculate bounding boxes
258 * with the two fill-conventions we support. GL (normally) ends
259 * up needing a bottom-left fill convention, which requires
260 * slightly different rounding.
262 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
264 bbox
.x0
= (MIN3(x
[0], x
[1], x
[2]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
265 bbox
.x1
= (MAX3(x
[0], x
[1], x
[2]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
266 bbox
.y0
= (MIN3(y
[0], y
[1], y
[2]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
267 bbox
.y1
= (MAX3(y
[0], y
[1], y
[2]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
269 /* Inclusive coordinates:
275 if (bbox
.x1
< bbox
.x0
||
277 if (0) debug_printf("empty bounding box\n");
278 LP_COUNT(nr_culled_tris
);
282 if (!u_rect_test_intersection(&setup
->draw_region
, &bbox
)) {
283 if (0) debug_printf("offscreen\n");
284 LP_COUNT(nr_culled_tris
);
288 u_rect_find_intersection(&setup
->draw_region
, &bbox
);
290 tri
= lp_setup_alloc_triangle(scene
,
298 tri
->v
[0][0] = v0
[0][0];
299 tri
->v
[1][0] = v1
[0][0];
300 tri
->v
[2][0] = v2
[0][0];
301 tri
->v
[0][1] = v0
[0][1];
302 tri
->v
[1][1] = v1
[0][1];
303 tri
->v
[2][1] = v2
[0][1];
306 tri
->plane
[0].dcdy
= x
[0] - x
[1];
307 tri
->plane
[1].dcdy
= x
[1] - x
[2];
308 tri
->plane
[2].dcdy
= x
[2] - x
[0];
310 tri
->plane
[0].dcdx
= y
[0] - y
[1];
311 tri
->plane
[1].dcdx
= y
[1] - y
[2];
312 tri
->plane
[2].dcdx
= y
[2] - y
[0];
316 /* Setup parameter interpolants:
318 lp_setup_tri_coef( setup
, &tri
->inputs
, v0
, v1
, v2
, frontfacing
);
320 tri
->inputs
.facing
= frontfacing
? 1.0F
: -1.0F
;
321 tri
->inputs
.disable
= FALSE
;
322 tri
->inputs
.opaque
= setup
->fs
.current
.variant
->opaque
;
323 tri
->inputs
.state
= setup
->fs
.stored
;
326 for (i
= 0; i
< 3; i
++) {
327 struct lp_rast_plane
*plane
= &tri
->plane
[i
];
329 /* half-edge constants, will be interated over the whole render
332 plane
->c
= plane
->dcdx
* x
[i
] - plane
->dcdy
* y
[i
];
334 /* correct for top-left vs. bottom-left fill convention.
336 * note that we're overloading gl_rasterization_rules to mean
337 * both (0.5,0.5) pixel centers *and* bottom-left filling
340 * GL actually has a top-left filling convention, but GL's
341 * notion of "top" differs from gallium's...
343 * Also, sometimes (in FBO cases) GL will render upside down
344 * to its usual method, in which case it will probably want
345 * to use the opposite, top-left convention.
347 * XXX: Chances are this will get stripped away. In fact this
348 * is only meaningful if:
350 * (plane->c & (FIXED_ONE-1)) == 0
353 if ((plane
->c
& (FIXED_ONE
-1)) == 0) {
354 if (plane
->dcdx
< 0) {
355 /* both fill conventions want this - adjust for left edges */
358 else if (plane
->dcdx
== 0) {
359 if (setup
->pixel_offset
== 0) {
360 /* correct for top-left fill convention:
362 if (plane
->dcdy
> 0) plane
->c
++;
365 /* correct for bottom-left fill convention:
367 if (plane
->dcdy
< 0) plane
->c
++;
372 plane
->c
= (plane
->c
+ (FIXED_ONE
-1)) / FIXED_ONE
;
375 /* find trivial reject offsets for each edge for a single-pixel
376 * sized block. These will be scaled up at each recursive level to
377 * match the active blocksize. Scaling in this way works best if
378 * the blocks are square.
381 if (plane
->dcdx
< 0) plane
->eo
-= plane
->dcdx
;
382 if (plane
->dcdy
> 0) plane
->eo
+= plane
->dcdy
;
384 /* Calculate trivial accept offsets from the above.
386 plane
->ei
= plane
->dcdy
- plane
->dcdx
- plane
->eo
;
391 * When rasterizing scissored tris, use the intersection of the
392 * triangle bounding box and the scissor rect to generate the
395 * This permits us to cut off the triangle "tails" that are present
396 * in the intermediate recursive levels caused when two of the
397 * triangles edges don't diverge quickly enough to trivially reject
398 * exterior blocks from the triangle.
400 * It's not really clear if it's worth worrying about these tails,
401 * but since we generate the planes for each scissored tri, it's
402 * free to trim them in this case.
404 * Note that otherwise, the scissor planes only vary in 'C' value,
405 * and even then only on state-changes. Could alternatively store
406 * these planes elsewhere.
408 if (nr_planes
== 7) {
409 tri
->plane
[3].dcdx
= -1;
410 tri
->plane
[3].dcdy
= 0;
411 tri
->plane
[3].c
= 1-bbox
.x0
;
412 tri
->plane
[3].ei
= 0;
413 tri
->plane
[3].eo
= 1;
415 tri
->plane
[4].dcdx
= 1;
416 tri
->plane
[4].dcdy
= 0;
417 tri
->plane
[4].c
= bbox
.x1
+1;
418 tri
->plane
[4].ei
= -1;
419 tri
->plane
[4].eo
= 0;
421 tri
->plane
[5].dcdx
= 0;
422 tri
->plane
[5].dcdy
= 1;
423 tri
->plane
[5].c
= 1-bbox
.y0
;
424 tri
->plane
[5].ei
= 0;
425 tri
->plane
[5].eo
= 1;
427 tri
->plane
[6].dcdx
= 0;
428 tri
->plane
[6].dcdy
= -1;
429 tri
->plane
[6].c
= bbox
.y1
+1;
430 tri
->plane
[6].ei
= -1;
431 tri
->plane
[6].eo
= 0;
434 return lp_setup_bin_triangle( setup
, tri
, &bbox
, nr_planes
);
438 * Round to nearest less or equal power of two of the input.
440 * Undefined if no bit set exists, so code should check against 0 first.
442 static INLINE
uint32_t
443 floor_pot(uint32_t n
)
445 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
465 lp_setup_bin_triangle( struct lp_setup_context
*setup
,
466 struct lp_rast_triangle
*tri
,
467 const struct u_rect
*bbox
,
470 struct lp_scene
*scene
= setup
->scene
;
473 /* What is the largest power-of-two boundary this triangle crosses:
475 int dx
= floor_pot((bbox
->x0
^ bbox
->x1
) |
476 (bbox
->y0
^ bbox
->y1
));
478 /* The largest dimension of the rasterized area of the triangle
479 * (aligned to a 4x4 grid), rounded down to the nearest power of two:
481 int sz
= floor_pot((bbox
->x1
- (bbox
->x0
& ~3)) |
482 (bbox
->y1
- (bbox
->y0
& ~3)));
484 /* Determine which tile(s) intersect the triangle's bounding box
488 int ix0
= bbox
->x0
/ TILE_SIZE
;
489 int iy0
= bbox
->y0
/ TILE_SIZE
;
490 int px
= bbox
->x0
& 63 & ~3;
491 int py
= bbox
->y0
& 63 & ~3;
492 int mask
= px
| (py
<< 8);
494 assert(iy0
== bbox
->y1
/ TILE_SIZE
&&
495 ix0
== bbox
->x1
/ TILE_SIZE
);
497 if (nr_planes
== 3) {
500 /* Triangle is contained in a single 4x4 stamp:
503 return lp_scene_bin_command( scene
, ix0
, iy0
,
504 LP_RAST_OP_TRIANGLE_3_4
,
505 lp_rast_arg_triangle(tri
, mask
) );
510 /* Triangle is contained in a single 16x16 block:
512 return lp_scene_bin_command( scene
, ix0
, iy0
,
513 LP_RAST_OP_TRIANGLE_3_16
,
514 lp_rast_arg_triangle(tri
, mask
) );
517 else if (nr_planes
== 4 && sz
< 16)
519 return lp_scene_bin_command( scene
, ix0
, iy0
,
520 LP_RAST_OP_TRIANGLE_4_16
,
521 lp_rast_arg_triangle(tri
, mask
) );
525 /* Triangle is contained in a single tile:
527 return lp_scene_bin_command( scene
, ix0
, iy0
,
528 lp_rast_tri_tab
[nr_planes
],
529 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1) );
536 int xstep
[MAX_PLANES
];
537 int ystep
[MAX_PLANES
];
540 int ix0
= bbox
->x0
/ TILE_SIZE
;
541 int iy0
= bbox
->y0
/ TILE_SIZE
;
542 int ix1
= bbox
->x1
/ TILE_SIZE
;
543 int iy1
= bbox
->y1
/ TILE_SIZE
;
545 for (i
= 0; i
< nr_planes
; i
++) {
546 c
[i
] = (tri
->plane
[i
].c
+
547 tri
->plane
[i
].dcdy
* iy0
* TILE_SIZE
-
548 tri
->plane
[i
].dcdx
* ix0
* TILE_SIZE
);
550 ei
[i
] = tri
->plane
[i
].ei
<< TILE_ORDER
;
551 eo
[i
] = tri
->plane
[i
].eo
<< TILE_ORDER
;
552 xstep
[i
] = -(tri
->plane
[i
].dcdx
<< TILE_ORDER
);
553 ystep
[i
] = tri
->plane
[i
].dcdy
<< TILE_ORDER
;
558 /* Test tile-sized blocks against the triangle.
559 * Discard blocks fully outside the tri. If the block is fully
560 * contained inside the tri, bin an lp_rast_shade_tile command.
561 * Else, bin a lp_rast_triangle command.
563 for (y
= iy0
; y
<= iy1
; y
++)
565 boolean in
= FALSE
; /* are we inside the triangle? */
568 for (i
= 0; i
< nr_planes
; i
++)
571 for (x
= ix0
; x
<= ix1
; x
++)
576 for (i
= 0; i
< nr_planes
; i
++) {
577 int planeout
= cx
[i
] + eo
[i
];
578 int planepartial
= cx
[i
] + ei
[i
] - 1;
579 out
|= (planeout
>> 31);
580 partial
|= (planepartial
>> 31) & (1<<i
);
586 break; /* exiting triangle, all done with this row */
587 LP_COUNT(nr_empty_64
);
590 /* Not trivially accepted by at least one plane -
591 * rasterize/shade partial tile
593 int count
= util_bitcount(partial
);
595 if (!lp_scene_bin_command( scene
, x
, y
,
596 lp_rast_tri_tab
[count
],
597 lp_rast_arg_triangle(tri
, partial
) ))
600 LP_COUNT(nr_partially_covered_64
);
603 /* triangle covers the whole tile- shade whole tile */
604 LP_COUNT(nr_fully_covered_64
);
606 if (!lp_setup_whole_tile(setup
, &tri
->inputs
, x
, y
))
610 /* Iterate cx values across the region:
612 for (i
= 0; i
< nr_planes
; i
++)
616 /* Iterate c values down the region:
618 for (i
= 0; i
< nr_planes
; i
++)
626 /* Need to disable any partially binned triangle. This is easier
627 * than trying to locate all the triangle, shade-tile, etc,
628 * commands which may have been binned.
630 tri
->inputs
.disable
= TRUE
;
636 * Try to draw the triangle, restart the scene on failure.
638 static void retry_triangle_ccw( struct lp_setup_context
*setup
,
639 const float (*v0
)[4],
640 const float (*v1
)[4],
641 const float (*v2
)[4],
644 if (!do_triangle_ccw( setup
, v0
, v1
, v2
, front
))
646 if (!lp_setup_flush_and_restart(setup
))
649 if (!do_triangle_ccw( setup
, v0
, v1
, v2
, front
))
655 calc_area(const float (*v0
)[4],
656 const float (*v1
)[4],
657 const float (*v2
)[4])
659 float dx01
= v0
[0][0] - v1
[0][0];
660 float dy01
= v0
[0][1] - v1
[0][1];
661 float dx20
= v2
[0][0] - v0
[0][0];
662 float dy20
= v2
[0][1] - v0
[0][1];
663 return dx01
* dy20
- dx20
* dy01
;
668 * Draw triangle if it's CW, cull otherwise.
670 static void triangle_cw( struct lp_setup_context
*setup
,
671 const float (*v0
)[4],
672 const float (*v1
)[4],
673 const float (*v2
)[4] )
675 float area
= calc_area(v0
, v1
, v2
);
678 retry_triangle_ccw(setup
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
682 static void triangle_ccw( struct lp_setup_context
*setup
,
683 const float (*v0
)[4],
684 const float (*v1
)[4],
685 const float (*v2
)[4])
687 float area
= calc_area(v0
, v1
, v2
);
690 retry_triangle_ccw(setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
694 * Draw triangle whether it's CW or CCW.
696 static void triangle_both( struct lp_setup_context
*setup
,
697 const float (*v0
)[4],
698 const float (*v1
)[4],
699 const float (*v2
)[4] )
701 float area
= calc_area(v0
, v1
, v2
);
704 retry_triangle_ccw( setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
705 else if (area
< 0.0f
)
706 retry_triangle_ccw( setup
, v0
, v2
, v1
, !setup
->ccw_is_frontface
);
710 static void triangle_nop( struct lp_setup_context
*setup
,
711 const float (*v0
)[4],
712 const float (*v1
)[4],
713 const float (*v2
)[4] )
719 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
721 switch (setup
->cullmode
) {
723 setup
->triangle
= triangle_both
;
726 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
728 case PIPE_FACE_FRONT
:
729 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
732 setup
->triangle
= triangle_nop
;