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
);
163 lp_rast_cmd lp_rast_tri_tab
[9] = {
164 NULL
, /* should be impossible */
176 * Do basic setup for triangle rasterization and determine which
177 * framebuffer tiles are touched. Put the triangle in the scene's
178 * bins for the tiles which we overlap.
181 do_triangle_ccw(struct lp_setup_context
*setup
,
182 const float (*v0
)[4],
183 const float (*v1
)[4],
184 const float (*v2
)[4],
185 boolean frontfacing
)
188 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
189 struct lp_fragment_shader_variant
*variant
= setup
->fs
.current
.variant
;
190 struct lp_rast_triangle
*tri
;
196 struct lp_tri_info info
;
199 int ix0
, ix1
, iy0
, iy1
;
205 lp_setup_print_triangle(setup
, v0
, v1
, v2
);
207 if (setup
->scissor_test
) {
214 /* x/y positions in fixed point */
215 x
[0] = subpixel_snap(v0
[0][0] - setup
->pixel_offset
);
216 x
[1] = subpixel_snap(v1
[0][0] - setup
->pixel_offset
);
217 x
[2] = subpixel_snap(v2
[0][0] - setup
->pixel_offset
);
218 y
[0] = subpixel_snap(v0
[0][1] - setup
->pixel_offset
);
219 y
[1] = subpixel_snap(v1
[0][1] - setup
->pixel_offset
);
220 y
[2] = subpixel_snap(v2
[0][1] - setup
->pixel_offset
);
223 /* Bounding rectangle (in pixels) */
225 /* Yes this is necessary to accurately calculate bounding boxes
226 * with the two fill-conventions we support. GL (normally) ends
227 * up needing a bottom-left fill convention, which requires
228 * slightly different rounding.
230 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
232 bbox
.x0
= (MIN3(x
[0], x
[1], x
[2]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
233 bbox
.x1
= (MAX3(x
[0], x
[1], x
[2]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
234 bbox
.y0
= (MIN3(y
[0], y
[1], y
[2]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
235 bbox
.y1
= (MAX3(y
[0], y
[1], y
[2]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
237 /* Inclusive coordinates:
243 if (bbox
.x1
< bbox
.x0
||
245 if (0) debug_printf("empty bounding box\n");
246 LP_COUNT(nr_culled_tris
);
250 if (!u_rect_test_intersection(&setup
->draw_region
, &bbox
)) {
251 if (0) debug_printf("offscreen\n");
252 LP_COUNT(nr_culled_tris
);
256 u_rect_find_intersection(&setup
->draw_region
, &bbox
);
258 tri
= lp_setup_alloc_triangle(scene
,
266 tri
->v
[0][0] = v0
[0][0];
267 tri
->v
[1][0] = v1
[0][0];
268 tri
->v
[2][0] = v2
[0][0];
269 tri
->v
[0][1] = v0
[0][1];
270 tri
->v
[1][1] = v1
[0][1];
271 tri
->v
[2][1] = v2
[0][1];
274 tri
->plane
[0].dcdy
= x
[0] - x
[1];
275 tri
->plane
[1].dcdy
= x
[1] - x
[2];
276 tri
->plane
[2].dcdy
= x
[2] - x
[0];
278 tri
->plane
[0].dcdx
= y
[0] - y
[1];
279 tri
->plane
[1].dcdx
= y
[1] - y
[2];
280 tri
->plane
[2].dcdx
= y
[2] - y
[0];
282 area
= (tri
->plane
[0].dcdy
* tri
->plane
[2].dcdx
-
283 tri
->plane
[2].dcdy
* tri
->plane
[0].dcdx
);
287 /* Cull non-ccw and zero-sized triangles.
289 * XXX: subject to overflow??
292 lp_scene_putback_data( scene
, tri_bytes
);
293 LP_COUNT(nr_culled_tris
);
300 dx01
= v0
[0][0] - v1
[0][0];
301 dy01
= v0
[0][1] - v1
[0][1];
302 dx20
= v2
[0][0] - v0
[0][0];
303 dy20
= v2
[0][1] - v0
[0][1];
304 oneoverarea
= 1.0f
/ (dx01
* dy20
- dx20
* dy01
);
309 info
.frontfacing
= frontfacing
;
310 info
.x0_center
= v0
[0][0] - setup
->pixel_offset
;
311 info
.y0_center
= v0
[0][1] - setup
->pixel_offset
;
312 info
.dx01_ooa
= dx01
* oneoverarea
;
313 info
.dx20_ooa
= dx20
* oneoverarea
;
314 info
.dy01_ooa
= dy01
* oneoverarea
;
315 info
.dy20_ooa
= dy20
* oneoverarea
;
317 /* Setup parameter interpolants:
319 lp_setup_tri_coef( setup
, &tri
->inputs
, &info
);
321 tri
->inputs
.facing
= frontfacing
? 1.0F
: -1.0F
;
322 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 if (plane
->dcdx
< 0) {
348 /* both fill conventions want this - adjust for left edges */
351 else if (plane
->dcdx
== 0) {
352 if (setup
->pixel_offset
== 0) {
353 /* correct for top-left fill convention:
355 if (plane
->dcdy
> 0) plane
->c
++;
358 /* correct for bottom-left fill convention:
360 if (plane
->dcdy
< 0) plane
->c
++;
364 plane
->dcdx
*= FIXED_ONE
;
365 plane
->dcdy
*= FIXED_ONE
;
367 /* find trivial reject offsets for each edge for a single-pixel
368 * sized block. These will be scaled up at each recursive level to
369 * match the active blocksize. Scaling in this way works best if
370 * the blocks are square.
373 if (plane
->dcdx
< 0) plane
->eo
-= plane
->dcdx
;
374 if (plane
->dcdy
> 0) plane
->eo
+= plane
->dcdy
;
376 /* Calculate trivial accept offsets from the above.
378 plane
->ei
= plane
->dcdy
- plane
->dcdx
- plane
->eo
;
383 * When rasterizing scissored tris, use the intersection of the
384 * triangle bounding box and the scissor rect to generate the
387 * This permits us to cut off the triangle "tails" that are present
388 * in the intermediate recursive levels caused when two of the
389 * triangles edges don't diverge quickly enough to trivially reject
390 * exterior blocks from the triangle.
392 * It's not really clear if it's worth worrying about these tails,
393 * but since we generate the planes for each scissored tri, it's
394 * free to trim them in this case.
396 * Note that otherwise, the scissor planes only vary in 'C' value,
397 * and even then only on state-changes. Could alternatively store
398 * these planes elsewhere.
400 if (nr_planes
== 7) {
401 tri
->plane
[3].dcdx
= -1;
402 tri
->plane
[3].dcdy
= 0;
403 tri
->plane
[3].c
= 1-bbox
.x0
;
404 tri
->plane
[3].ei
= 0;
405 tri
->plane
[3].eo
= 1;
407 tri
->plane
[4].dcdx
= 1;
408 tri
->plane
[4].dcdy
= 0;
409 tri
->plane
[4].c
= bbox
.x1
+1;
410 tri
->plane
[4].ei
= -1;
411 tri
->plane
[4].eo
= 0;
413 tri
->plane
[5].dcdx
= 0;
414 tri
->plane
[5].dcdy
= 1;
415 tri
->plane
[5].c
= 1-bbox
.y0
;
416 tri
->plane
[5].ei
= 0;
417 tri
->plane
[5].eo
= 1;
419 tri
->plane
[6].dcdx
= 0;
420 tri
->plane
[6].dcdy
= -1;
421 tri
->plane
[6].c
= bbox
.y1
+1;
422 tri
->plane
[6].ei
= -1;
423 tri
->plane
[6].eo
= 0;
428 * All fields of 'tri' are now set. The remaining code here is
429 * concerned with binning.
432 /* Convert to tile coordinates, and inclusive ranges:
434 if (nr_planes
== 3) {
435 int ix0
= bbox
.x0
/ 16;
436 int iy0
= bbox
.y0
/ 16;
437 int ix1
= bbox
.x1
/ 16;
438 int iy1
= bbox
.y1
/ 16;
440 if (iy0
== iy1
&& ix0
== ix1
)
443 /* Triangle is contained in a single 16x16 block:
445 int mask
= (ix0
& 3) | ((iy0
& 3) << 4);
447 lp_scene_bin_command( scene
, ix0
/4, iy0
/4,
448 lp_rast_triangle_3_16
,
449 lp_rast_arg_triangle(tri
, mask
) );
454 ix0
= bbox
.x0
/ TILE_SIZE
;
455 iy0
= bbox
.y0
/ TILE_SIZE
;
456 ix1
= bbox
.x1
/ TILE_SIZE
;
457 iy1
= bbox
.y1
/ TILE_SIZE
;
460 * Clamp to framebuffer size
462 assert(ix0
== MAX2(ix0
, 0));
463 assert(iy0
== MAX2(iy0
, 0));
464 assert(ix1
== MIN2(ix1
, scene
->tiles_x
- 1));
465 assert(iy1
== MIN2(iy1
, scene
->tiles_y
- 1));
467 /* Determine which tile(s) intersect the triangle's bounding box
469 if (iy0
== iy1
&& ix0
== ix1
)
471 /* Triangle is contained in a single tile:
473 lp_scene_bin_command( scene
, ix0
, iy0
,
474 lp_rast_tri_tab
[nr_planes
],
475 lp_rast_arg_triangle(tri
, (1<<nr_planes
)-1) );
486 for (i
= 0; i
< nr_planes
; i
++) {
487 c
[i
] = (tri
->plane
[i
].c
+
488 tri
->plane
[i
].dcdy
* iy0
* TILE_SIZE
-
489 tri
->plane
[i
].dcdx
* ix0
* TILE_SIZE
);
491 ei
[i
] = tri
->plane
[i
].ei
<< TILE_ORDER
;
492 eo
[i
] = tri
->plane
[i
].eo
<< TILE_ORDER
;
493 xstep
[i
] = -(tri
->plane
[i
].dcdx
<< TILE_ORDER
);
494 ystep
[i
] = tri
->plane
[i
].dcdy
<< TILE_ORDER
;
499 /* Test tile-sized blocks against the triangle.
500 * Discard blocks fully outside the tri. If the block is fully
501 * contained inside the tri, bin an lp_rast_shade_tile command.
502 * Else, bin a lp_rast_triangle command.
504 for (y
= iy0
; y
<= iy1
; y
++)
506 boolean in
= FALSE
; /* are we inside the triangle? */
509 for (i
= 0; i
< nr_planes
; i
++)
512 for (x
= ix0
; x
<= ix1
; x
++)
517 for (i
= 0; i
< nr_planes
; i
++) {
518 int planeout
= cx
[i
] + eo
[i
];
519 int planepartial
= cx
[i
] + ei
[i
] - 1;
520 out
|= (planeout
>> 31);
521 partial
|= (planepartial
>> 31) & (1<<i
);
527 break; /* exiting triangle, all done with this row */
528 LP_COUNT(nr_empty_64
);
531 /* Not trivially accepted by at least one plane -
532 * rasterize/shade partial tile
534 int count
= util_bitcount(partial
);
536 lp_scene_bin_command( scene
, x
, y
,
537 lp_rast_tri_tab
[count
],
538 lp_rast_arg_triangle(tri
, partial
) );
540 LP_COUNT(nr_partially_covered_64
);
543 /* triangle covers the whole tile- shade whole tile */
544 LP_COUNT(nr_fully_covered_64
);
546 if (variant
->opaque
&&
548 lp_scene_bin_reset( scene
, x
, y
);
550 lp_scene_bin_command( scene
, x
, y
,
552 lp_rast_arg_inputs(&tri
->inputs
) );
555 /* Iterate cx values across the region:
557 for (i
= 0; i
< nr_planes
; i
++)
561 /* Iterate c values down the region:
563 for (i
= 0; i
< nr_planes
; i
++)
571 * Draw triangle if it's CW, cull otherwise.
573 static void triangle_cw( struct lp_setup_context
*setup
,
574 const float (*v0
)[4],
575 const float (*v1
)[4],
576 const float (*v2
)[4] )
578 do_triangle_ccw( setup
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
583 * Draw triangle if it's CCW, cull otherwise.
585 static void triangle_ccw( struct lp_setup_context
*setup
,
586 const float (*v0
)[4],
587 const float (*v1
)[4],
588 const float (*v2
)[4] )
590 do_triangle_ccw( setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
596 * Draw triangle whether it's CW or CCW.
598 static void triangle_both( struct lp_setup_context
*setup
,
599 const float (*v0
)[4],
600 const float (*v1
)[4],
601 const float (*v2
)[4] )
603 /* edge vectors e = v0 - v2, f = v1 - v2 */
604 const float ex
= v0
[0][0] - v2
[0][0];
605 const float ey
= v0
[0][1] - v2
[0][1];
606 const float fx
= v1
[0][0] - v2
[0][0];
607 const float fy
= v1
[0][1] - v2
[0][1];
609 /* det = cross(e,f).z */
610 const float det
= ex
* fy
- ey
* fx
;
612 triangle_ccw( setup
, v0
, v1
, v2
);
614 triangle_cw( setup
, v0
, v1
, v2
);
618 static void triangle_nop( struct lp_setup_context
*setup
,
619 const float (*v0
)[4],
620 const float (*v1
)[4],
621 const float (*v2
)[4] )
627 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
629 switch (setup
->cullmode
) {
631 setup
->triangle
= triangle_both
;
634 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
636 case PIPE_FACE_FRONT
:
637 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
640 setup
->triangle
= triangle_nop
;