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"
35 #include "lp_setup_context.h"
37 #include "lp_state_fs.h"
39 #define NUM_CHANNELS 4
43 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
45 static void constant_coef( struct lp_setup_context
*setup
,
46 struct lp_rast_triangle
*tri
,
51 tri
->inputs
.a0
[slot
][i
] = value
;
52 tri
->inputs
.dadx
[slot
][i
] = 0.0f
;
53 tri
->inputs
.dady
[slot
][i
] = 0.0f
;
58 * Compute a0, dadx and dady for a linearly interpolated coefficient,
61 static void linear_coef( struct lp_setup_context
*setup
,
62 struct lp_rast_triangle
*tri
,
71 float a1
= v1
[vert_attr
][i
];
72 float a2
= v2
[vert_attr
][i
];
73 float a3
= v3
[vert_attr
][i
];
77 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
78 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
80 tri
->inputs
.dadx
[slot
][i
] = dadx
;
81 tri
->inputs
.dady
[slot
][i
] = dady
;
83 /* calculate a0 as the value which would be sampled for the
84 * fragment at (0,0), taking into account that we want to sample at
85 * pixel centers, in other words (0.5, 0.5).
87 * this is neat but unfortunately not a good way to do things for
88 * triangles with very large values of dadx or dady as it will
89 * result in the subtraction and re-addition from a0 of a very
90 * large number, which means we'll end up loosing a lot of the
91 * fractional bits and precision from a0. the way to fix this is
92 * to define a0 as the sample at a pixel center somewhere near vmin
93 * instead - i'll switch to this later.
95 tri
->inputs
.a0
[slot
][i
] = (a1
-
96 (dadx
* (v1
[0][0] - setup
->pixel_offset
) +
97 dady
* (v1
[0][1] - setup
->pixel_offset
)));
102 * Compute a0, dadx and dady for a perspective-corrected interpolant,
104 * We basically multiply the vertex value by 1/w before computing
105 * the plane coefficients (a0, dadx, dady).
106 * Later, when we compute the value at a particular fragment position we'll
107 * divide the interpolated value by the interpolated W at that fragment.
109 static void perspective_coef( struct lp_setup_context
*setup
,
110 struct lp_rast_triangle
*tri
,
113 const float (*v1
)[4],
114 const float (*v2
)[4],
115 const float (*v3
)[4],
119 /* premultiply by 1/w (v[0][3] is always 1/w):
121 float a1
= v1
[vert_attr
][i
] * v1
[0][3];
122 float a2
= v2
[vert_attr
][i
] * v2
[0][3];
123 float a3
= v3
[vert_attr
][i
] * v3
[0][3];
124 float da12
= a1
- a2
;
125 float da31
= a3
- a1
;
126 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
127 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
129 tri
->inputs
.dadx
[slot
][i
] = dadx
;
130 tri
->inputs
.dady
[slot
][i
] = dady
;
131 tri
->inputs
.a0
[slot
][i
] = (a1
-
132 (dadx
* (v1
[0][0] - setup
->pixel_offset
) +
133 dady
* (v1
[0][1] - setup
->pixel_offset
)));
138 * Special coefficient setup for gl_FragCoord.
139 * X and Y are trivial
140 * Z and W are copied from position_coef which should have already been computed.
141 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
144 setup_fragcoord_coef(struct lp_setup_context
*setup
,
145 struct lp_rast_triangle
*tri
,
148 const float (*v1
)[4],
149 const float (*v2
)[4],
150 const float (*v3
)[4])
153 tri
->inputs
.a0
[slot
][0] = 0.0;
154 tri
->inputs
.dadx
[slot
][0] = 1.0;
155 tri
->inputs
.dady
[slot
][0] = 0.0;
157 tri
->inputs
.a0
[slot
][1] = 0.0;
158 tri
->inputs
.dadx
[slot
][1] = 0.0;
159 tri
->inputs
.dady
[slot
][1] = 1.0;
161 linear_coef(setup
, tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 2);
163 linear_coef(setup
, tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 3);
168 * Setup the fragment input attribute with the front-facing value.
169 * \param frontface is the triangle front facing?
171 static void setup_facing_coef( struct lp_setup_context
*setup
,
172 struct lp_rast_triangle
*tri
,
176 /* convert TRUE to 1.0 and FALSE to -1.0 */
177 constant_coef( setup
, tri
, slot
, 2.0f
* frontface
- 1.0f
, 0 );
178 constant_coef( setup
, tri
, slot
, 0.0f
, 1 ); /* wasted */
179 constant_coef( setup
, tri
, slot
, 0.0f
, 2 ); /* wasted */
180 constant_coef( setup
, tri
, slot
, 0.0f
, 3 ); /* wasted */
185 * Compute the tri->coef[] array dadx, dady, a0 values.
187 static void setup_tri_coefficients( struct lp_setup_context
*setup
,
188 struct lp_rast_triangle
*tri
,
190 const float (*v1
)[4],
191 const float (*v2
)[4],
192 const float (*v3
)[4],
197 /* The internal position input is in slot zero:
199 setup_fragcoord_coef(setup
, tri
, oneoverarea
, 0, v1
, v2
, v3
);
201 /* setup interpolation for all the remaining attributes:
203 for (slot
= 0; slot
< setup
->fs
.nr_inputs
; slot
++) {
204 unsigned vert_attr
= setup
->fs
.input
[slot
].src_index
;
207 switch (setup
->fs
.input
[slot
].interp
) {
208 case LP_INTERP_CONSTANT
:
209 if (setup
->flatshade_first
) {
210 for (i
= 0; i
< NUM_CHANNELS
; i
++)
211 if (setup
->fs
.input
[slot
].usage_mask
& (1 << i
))
212 constant_coef(setup
, tri
, slot
+1, v1
[vert_attr
][i
], i
);
215 for (i
= 0; i
< NUM_CHANNELS
; i
++)
216 if (setup
->fs
.input
[slot
].usage_mask
& (1 << i
))
217 constant_coef(setup
, tri
, slot
+1, v3
[vert_attr
][i
], i
);
221 case LP_INTERP_LINEAR
:
222 for (i
= 0; i
< NUM_CHANNELS
; i
++)
223 if (setup
->fs
.input
[slot
].usage_mask
& (1 << i
))
224 linear_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
227 case LP_INTERP_PERSPECTIVE
:
228 for (i
= 0; i
< NUM_CHANNELS
; i
++)
229 if (setup
->fs
.input
[slot
].usage_mask
& (1 << i
))
230 perspective_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
233 case LP_INTERP_POSITION
:
234 /* XXX: fix me - duplicates the values in slot zero.
236 setup_fragcoord_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
);
239 case LP_INTERP_FACING
:
240 setup_facing_coef(setup
, tri
, slot
+1, frontface
);
251 static INLINE
int subpixel_snap( float a
)
253 return util_iround(FIXED_ONE
* a
- (FIXED_ONE
/ 2));
259 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
260 * immediately after it.
261 * The memory is allocated from the per-scene pool, not per-tile.
262 * \param tri_size returns number of bytes allocated
263 * \param nr_inputs number of fragment shader inputs
264 * \return pointer to triangle space
266 static INLINE
struct lp_rast_triangle
*
267 alloc_triangle(struct lp_scene
*scene
, unsigned nr_inputs
, unsigned *tri_size
)
269 unsigned input_array_sz
= NUM_CHANNELS
* (nr_inputs
+ 1) * sizeof(float);
270 struct lp_rast_triangle
*tri
;
274 assert(sizeof(*tri
) % 16 == 0);
276 bytes
= sizeof(*tri
) + (3 * input_array_sz
);
278 tri
= lp_scene_alloc_aligned( scene
, bytes
, 16 );
281 inputs
= (char *) (tri
+ 1);
282 tri
->inputs
.a0
= (float (*)[4]) inputs
;
283 tri
->inputs
.dadx
= (float (*)[4]) (inputs
+ input_array_sz
);
284 tri
->inputs
.dady
= (float (*)[4]) (inputs
+ 2 * input_array_sz
);
294 * Print triangle vertex attribs (for debug).
297 print_triangle(struct lp_setup_context
*setup
,
298 const float (*v1
)[4],
299 const float (*v2
)[4],
300 const float (*v3
)[4])
304 debug_printf("llvmpipe triangle\n");
305 for (i
= 0; i
< setup
->fs
.nr_inputs
; i
++) {
306 debug_printf(" v1[%d]: %f %f %f %f\n", i
,
307 v1
[i
][0], v1
[i
][1], v1
[i
][2], v1
[i
][3]);
309 for (i
= 0; i
< setup
->fs
.nr_inputs
; i
++) {
310 debug_printf(" v2[%d]: %f %f %f %f\n", i
,
311 v2
[i
][0], v2
[i
][1], v2
[i
][2], v2
[i
][3]);
313 for (i
= 0; i
< setup
->fs
.nr_inputs
; i
++) {
314 debug_printf(" v3[%d]: %f %f %f %f\n", i
,
315 v3
[i
][0], v3
[i
][1], v3
[i
][2], v3
[i
][3]);
321 * Do basic setup for triangle rasterization and determine which
322 * framebuffer tiles are touched. Put the triangle in the scene's
323 * bins for the tiles which we overlap.
326 do_triangle_ccw(struct lp_setup_context
*setup
,
327 const float (*v1
)[4],
328 const float (*v2
)[4],
329 const float (*v3
)[4],
330 boolean frontfacing
)
332 /* x/y positions in fixed point */
333 const int x1
= subpixel_snap(v1
[0][0] + 0.5 - setup
->pixel_offset
);
334 const int x2
= subpixel_snap(v2
[0][0] + 0.5 - setup
->pixel_offset
);
335 const int x3
= subpixel_snap(v3
[0][0] + 0.5 - setup
->pixel_offset
);
336 const int y1
= subpixel_snap(v1
[0][1] + 0.5 - setup
->pixel_offset
);
337 const int y2
= subpixel_snap(v2
[0][1] + 0.5 - setup
->pixel_offset
);
338 const int y3
= subpixel_snap(v3
[0][1] + 0.5 - setup
->pixel_offset
);
340 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
341 struct lp_rast_triangle
*tri
;
344 int minx
, maxx
, miny
, maxy
;
348 print_triangle(setup
, v1
, v2
, v3
);
350 tri
= alloc_triangle(scene
, setup
->fs
.nr_inputs
, &tri_bytes
);
355 tri
->v
[0][0] = v1
[0][0];
356 tri
->v
[1][0] = v2
[0][0];
357 tri
->v
[2][0] = v3
[0][0];
358 tri
->v
[0][1] = v1
[0][1];
359 tri
->v
[1][1] = v2
[0][1];
360 tri
->v
[2][1] = v3
[0][1];
371 area
= (tri
->dx12
* tri
->dy31
- tri
->dx31
* tri
->dy12
);
375 /* Cull non-ccw and zero-sized triangles.
377 * XXX: subject to overflow??
380 lp_scene_putback_data( scene
, tri_bytes
);
381 LP_COUNT(nr_culled_tris
);
385 /* Bounding rectangle (in pixels) */
386 minx
= (MIN3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
387 maxx
= (MAX3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
388 miny
= (MIN3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
389 maxy
= (MAX3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
391 if (setup
->scissor_test
) {
392 minx
= MAX2(minx
, setup
->scissor
.current
.minx
);
393 maxx
= MIN2(maxx
, setup
->scissor
.current
.maxx
);
394 miny
= MAX2(miny
, setup
->scissor
.current
.miny
);
395 maxy
= MIN2(maxy
, setup
->scissor
.current
.maxy
);
400 lp_scene_putback_data( scene
, tri_bytes
);
401 LP_COUNT(nr_culled_tris
);
407 oneoverarea
= ((float)FIXED_ONE
) / (float)area
;
409 /* Setup parameter interpolants:
411 setup_tri_coefficients( setup
, tri
, oneoverarea
, v1
, v2
, v3
, frontfacing
);
413 tri
->inputs
.facing
= frontfacing
? 1.0F
: -1.0F
;
415 /* half-edge constants, will be interated over the whole render target.
417 tri
->c1
= tri
->dy12
* x1
- tri
->dx12
* y1
;
418 tri
->c2
= tri
->dy23
* x2
- tri
->dx23
* y2
;
419 tri
->c3
= tri
->dy31
* x3
- tri
->dx31
* y3
;
421 /* correct for top-left fill convention:
423 if (tri
->dy12
< 0 || (tri
->dy12
== 0 && tri
->dx12
> 0)) tri
->c1
++;
424 if (tri
->dy23
< 0 || (tri
->dy23
== 0 && tri
->dx23
> 0)) tri
->c2
++;
425 if (tri
->dy31
< 0 || (tri
->dy31
== 0 && tri
->dx31
> 0)) tri
->c3
++;
427 tri
->dy12
*= FIXED_ONE
;
428 tri
->dy23
*= FIXED_ONE
;
429 tri
->dy31
*= FIXED_ONE
;
431 tri
->dx12
*= FIXED_ONE
;
432 tri
->dx23
*= FIXED_ONE
;
433 tri
->dx31
*= FIXED_ONE
;
435 /* find trivial reject offsets for each edge for a single-pixel
436 * sized block. These will be scaled up at each recursive level to
437 * match the active blocksize. Scaling in this way works best if
438 * the blocks are square.
441 if (tri
->dy12
< 0) tri
->eo1
-= tri
->dy12
;
442 if (tri
->dx12
> 0) tri
->eo1
+= tri
->dx12
;
445 if (tri
->dy23
< 0) tri
->eo2
-= tri
->dy23
;
446 if (tri
->dx23
> 0) tri
->eo2
+= tri
->dx23
;
449 if (tri
->dy31
< 0) tri
->eo3
-= tri
->dy31
;
450 if (tri
->dx31
> 0) tri
->eo3
+= tri
->dx31
;
452 /* Calculate trivial accept offsets from the above.
454 tri
->ei1
= tri
->dx12
- tri
->dy12
- tri
->eo1
;
455 tri
->ei2
= tri
->dx23
- tri
->dy23
- tri
->eo2
;
456 tri
->ei3
= tri
->dx31
- tri
->dy31
- tri
->eo3
;
458 /* Fill in the inputs.step[][] arrays.
459 * We've manually unrolled some loops here.
462 const int xstep1
= -tri
->dy12
;
463 const int xstep2
= -tri
->dy23
;
464 const int xstep3
= -tri
->dy31
;
465 const int ystep1
= tri
->dx12
;
466 const int ystep2
= tri
->dx23
;
467 const int ystep3
= tri
->dx31
;
469 #define SETUP_STEP(i, x, y) \
471 tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
472 tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
473 tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
488 SETUP_STEP(10, 0, 3);
489 SETUP_STEP(11, 1, 3);
491 SETUP_STEP(12, 2, 2);
492 SETUP_STEP(13, 3, 2);
493 SETUP_STEP(14, 2, 3);
494 SETUP_STEP(15, 3, 3);
499 * All fields of 'tri' are now set. The remaining code here is
500 * concerned with binning.
503 /* Convert to tile coordinates:
505 minx
= minx
/ TILE_SIZE
;
506 miny
= miny
/ TILE_SIZE
;
507 maxx
= maxx
/ TILE_SIZE
;
508 maxy
= maxy
/ TILE_SIZE
;
511 * Clamp to framebuffer size
513 minx
= MAX2(minx
, 0);
514 miny
= MAX2(miny
, 0);
515 maxx
= MIN2(maxx
, scene
->tiles_x
- 1);
516 maxy
= MIN2(maxy
, scene
->tiles_y
- 1);
518 /* Determine which tile(s) intersect the triangle's bounding box
520 if (miny
== maxy
&& minx
== maxx
)
522 /* Triangle is contained in a single tile:
524 lp_scene_bin_command( scene
, minx
, miny
, lp_rast_triangle
,
525 lp_rast_arg_triangle(tri
) );
530 tri
->dx12
* miny
* TILE_SIZE
-
531 tri
->dy12
* minx
* TILE_SIZE
);
533 tri
->dx23
* miny
* TILE_SIZE
-
534 tri
->dy23
* minx
* TILE_SIZE
);
536 tri
->dx31
* miny
* TILE_SIZE
-
537 tri
->dy31
* minx
* TILE_SIZE
);
539 int ei1
= tri
->ei1
<< TILE_ORDER
;
540 int ei2
= tri
->ei2
<< TILE_ORDER
;
541 int ei3
= tri
->ei3
<< TILE_ORDER
;
543 int eo1
= tri
->eo1
<< TILE_ORDER
;
544 int eo2
= tri
->eo2
<< TILE_ORDER
;
545 int eo3
= tri
->eo3
<< TILE_ORDER
;
547 int xstep1
= -(tri
->dy12
<< TILE_ORDER
);
548 int xstep2
= -(tri
->dy23
<< TILE_ORDER
);
549 int xstep3
= -(tri
->dy31
<< TILE_ORDER
);
551 int ystep1
= tri
->dx12
<< TILE_ORDER
;
552 int ystep2
= tri
->dx23
<< TILE_ORDER
;
553 int ystep3
= tri
->dx31
<< TILE_ORDER
;
557 /* Test tile-sized blocks against the triangle.
558 * Discard blocks fully outside the tri. If the block is fully
559 * contained inside the tri, bin an lp_rast_shade_tile command.
560 * Else, bin a lp_rast_triangle command.
562 for (y
= miny
; y
<= maxy
; y
++)
567 boolean in
= FALSE
; /* are we inside the triangle? */
569 for (x
= minx
; x
<= maxx
; x
++)
576 LP_COUNT(nr_empty_64
);
578 break; /* exiting triangle, all done with this row */
580 else if (cx1
+ ei1
> 0 &&
584 /* triangle covers the whole tile- shade whole tile */
585 LP_COUNT(nr_fully_covered_64
);
587 if (setup
->fs
.current
.variant
->opaque
) {
588 lp_scene_bin_reset( scene
, x
, y
);
589 lp_scene_bin_command( scene
, x
, y
,
591 lp_rast_arg_state(setup
->fs
.stored
) );
593 lp_scene_bin_command( scene
, x
, y
,
595 lp_rast_arg_inputs(&tri
->inputs
) );
599 /* rasterizer/shade partial tile */
600 LP_COUNT(nr_partially_covered_64
);
602 lp_scene_bin_command( scene
, x
, y
,
604 lp_rast_arg_triangle(tri
) );
607 /* Iterate cx values across the region:
614 /* Iterate c values down the region:
625 * Draw triangle if it's CW, cull otherwise.
627 static void triangle_cw( struct lp_setup_context
*setup
,
628 const float (*v0
)[4],
629 const float (*v1
)[4],
630 const float (*v2
)[4] )
632 do_triangle_ccw( setup
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
637 * Draw triangle if it's CCW, cull otherwise.
639 static void triangle_ccw( struct lp_setup_context
*setup
,
640 const float (*v0
)[4],
641 const float (*v1
)[4],
642 const float (*v2
)[4] )
644 do_triangle_ccw( setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
650 * Draw triangle whether it's CW or CCW.
652 static void triangle_both( struct lp_setup_context
*setup
,
653 const float (*v0
)[4],
654 const float (*v1
)[4],
655 const float (*v2
)[4] )
657 /* edge vectors e = v0 - v2, f = v1 - v2 */
658 const float ex
= v0
[0][0] - v2
[0][0];
659 const float ey
= v0
[0][1] - v2
[0][1];
660 const float fx
= v1
[0][0] - v2
[0][0];
661 const float fy
= v1
[0][1] - v2
[0][1];
663 /* det = cross(e,f).z */
664 if (ex
* fy
- ey
* fx
< 0.0f
)
665 triangle_ccw( setup
, v0
, v1
, v2
);
667 triangle_cw( setup
, v0
, v1
, v2
);
671 static void triangle_nop( struct lp_setup_context
*setup
,
672 const float (*v0
)[4],
673 const float (*v1
)[4],
674 const float (*v2
)[4] )
680 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
682 switch (setup
->cullmode
) {
684 setup
->triangle
= triangle_both
;
687 setup
->triangle
= setup
->ccw_is_frontface
? triangle_ccw
: triangle_cw
;
689 case PIPE_FACE_FRONT
:
690 setup
->triangle
= setup
->ccw_is_frontface
? triangle_cw
: triangle_ccw
;
693 setup
->triangle
= triangle_nop
;