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"
38 #define NUM_CHANNELS 4
42 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
44 static void constant_coef( struct lp_setup_context
*setup
,
45 struct lp_rast_triangle
*tri
,
50 tri
->inputs
.a0
[slot
][i
] = value
;
51 tri
->inputs
.dadx
[slot
][i
] = 0.0f
;
52 tri
->inputs
.dady
[slot
][i
] = 0.0f
;
57 * Compute a0, dadx and dady for a linearly interpolated coefficient,
60 static void linear_coef( struct lp_setup_context
*setup
,
61 struct lp_rast_triangle
*tri
,
70 float a1
= v1
[vert_attr
][i
];
71 float a2
= v2
[vert_attr
][i
];
72 float a3
= v3
[vert_attr
][i
];
76 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
77 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
79 tri
->inputs
.dadx
[slot
][i
] = dadx
;
80 tri
->inputs
.dady
[slot
][i
] = dady
;
82 /* calculate a0 as the value which would be sampled for the
83 * fragment at (0,0), taking into account that we want to sample at
84 * pixel centers, in other words (0.5, 0.5).
86 * this is neat but unfortunately not a good way to do things for
87 * triangles with very large values of dadx or dady as it will
88 * result in the subtraction and re-addition from a0 of a very
89 * large number, which means we'll end up loosing a lot of the
90 * fractional bits and precision from a0. the way to fix this is
91 * to define a0 as the sample at a pixel center somewhere near vmin
92 * instead - i'll switch to this later.
94 tri
->inputs
.a0
[slot
][i
] = (a1
-
95 (dadx
* (v1
[0][0] - setup
->pixel_offset
) +
96 dady
* (v1
[0][1] - setup
->pixel_offset
)));
101 * Compute a0, dadx and dady for a perspective-corrected interpolant,
103 * We basically multiply the vertex value by 1/w before computing
104 * the plane coefficients (a0, dadx, dady).
105 * Later, when we compute the value at a particular fragment position we'll
106 * divide the interpolated value by the interpolated W at that fragment.
108 static void perspective_coef( struct lp_setup_context
*setup
,
109 struct lp_rast_triangle
*tri
,
112 const float (*v1
)[4],
113 const float (*v2
)[4],
114 const float (*v3
)[4],
118 /* premultiply by 1/w (v[0][3] is always 1/w):
120 float a1
= v1
[vert_attr
][i
] * v1
[0][3];
121 float a2
= v2
[vert_attr
][i
] * v2
[0][3];
122 float a3
= v3
[vert_attr
][i
] * v3
[0][3];
123 float da12
= a1
- a2
;
124 float da31
= a3
- a1
;
125 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
126 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
128 tri
->inputs
.dadx
[slot
][i
] = dadx
;
129 tri
->inputs
.dady
[slot
][i
] = dady
;
130 tri
->inputs
.a0
[slot
][i
] = (a1
-
131 (dadx
* (v1
[0][0] - setup
->pixel_offset
) +
132 dady
* (v1
[0][1] - setup
->pixel_offset
)));
137 * Special coefficient setup for gl_FragCoord.
138 * X and Y are trivial
139 * Z and W are copied from position_coef which should have already been computed.
140 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
143 setup_fragcoord_coef(struct lp_setup_context
*setup
,
144 struct lp_rast_triangle
*tri
,
147 const float (*v1
)[4],
148 const float (*v2
)[4],
149 const float (*v3
)[4])
152 tri
->inputs
.a0
[slot
][0] = 0.0;
153 tri
->inputs
.dadx
[slot
][0] = 1.0;
154 tri
->inputs
.dady
[slot
][0] = 0.0;
156 tri
->inputs
.a0
[slot
][1] = 0.0;
157 tri
->inputs
.dadx
[slot
][1] = 0.0;
158 tri
->inputs
.dady
[slot
][1] = 1.0;
160 linear_coef(setup
, tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 2);
162 linear_coef(setup
, tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 3);
166 static void setup_facing_coef( struct lp_setup_context
*setup
,
167 struct lp_rast_triangle
*tri
,
171 constant_coef( setup
, tri
, slot
, 1.0f
- frontface
, 0 );
172 constant_coef( setup
, tri
, slot
, 0.0f
, 1 ); /* wasted */
173 constant_coef( setup
, tri
, slot
, 0.0f
, 2 ); /* wasted */
174 constant_coef( setup
, tri
, slot
, 0.0f
, 3 ); /* wasted */
179 * Compute the tri->coef[] array dadx, dady, a0 values.
181 static void setup_tri_coefficients( struct lp_setup_context
*setup
,
182 struct lp_rast_triangle
*tri
,
184 const float (*v1
)[4],
185 const float (*v2
)[4],
186 const float (*v3
)[4],
191 /* The internal position input is in slot zero:
193 setup_fragcoord_coef(setup
, tri
, oneoverarea
, 0, v1
, v2
, v3
);
195 /* setup interpolation for all the remaining attributes:
197 for (slot
= 0; slot
< setup
->fs
.nr_inputs
; slot
++) {
198 unsigned vert_attr
= setup
->fs
.input
[slot
].src_index
;
201 switch (setup
->fs
.input
[slot
].interp
) {
202 case LP_INTERP_CONSTANT
:
203 for (i
= 0; i
< NUM_CHANNELS
; i
++)
204 constant_coef(setup
, tri
, slot
+1, v3
[vert_attr
][i
], i
);
207 case LP_INTERP_LINEAR
:
208 for (i
= 0; i
< NUM_CHANNELS
; i
++)
209 linear_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
212 case LP_INTERP_PERSPECTIVE
:
213 for (i
= 0; i
< NUM_CHANNELS
; i
++)
214 perspective_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
217 case LP_INTERP_POSITION
:
218 /* XXX: fix me - duplicates the values in slot zero.
220 setup_fragcoord_coef(setup
, tri
, oneoverarea
, slot
+1, v1
, v2
, v3
);
223 case LP_INTERP_FACING
:
224 setup_facing_coef(setup
, tri
, slot
+1, frontface
);
235 static INLINE
int subpixel_snap( float a
)
237 return util_iround(FIXED_ONE
* a
- (FIXED_ONE
/ 2));
243 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
244 * immediately after it.
245 * The memory is allocated from the per-scene pool, not per-tile.
246 * \param tri_size returns number of bytes allocated
247 * \param nr_inputs number of fragment shader inputs
248 * \return pointer to triangle space
250 static INLINE
struct lp_rast_triangle
*
251 alloc_triangle(struct lp_scene
*scene
, unsigned nr_inputs
, unsigned *tri_size
)
253 unsigned input_array_sz
= NUM_CHANNELS
* (nr_inputs
+ 1) * sizeof(float);
254 struct lp_rast_triangle
*tri
;
258 assert(sizeof(*tri
) % 16 == 0);
260 bytes
= sizeof(*tri
) + (3 * input_array_sz
);
262 tri
= lp_scene_alloc_aligned( scene
, bytes
, 16 );
264 inputs
= (char *) (tri
+ 1);
265 tri
->inputs
.a0
= (float (*)[4]) inputs
;
266 tri
->inputs
.dadx
= (float (*)[4]) (inputs
+ input_array_sz
);
267 tri
->inputs
.dady
= (float (*)[4]) (inputs
+ 2 * input_array_sz
);
277 * Do basic setup for triangle rasterization and determine which
278 * framebuffer tiles are touched. Put the triangle in the scene's
279 * bins for the tiles which we overlap.
282 do_triangle_ccw(struct lp_setup_context
*setup
,
283 const float (*v1
)[4],
284 const float (*v2
)[4],
285 const float (*v3
)[4],
286 boolean frontfacing
)
288 /* x/y positions in fixed point */
289 const int x1
= subpixel_snap(v1
[0][0] + 0.5 - setup
->pixel_offset
);
290 const int x2
= subpixel_snap(v2
[0][0] + 0.5 - setup
->pixel_offset
);
291 const int x3
= subpixel_snap(v3
[0][0] + 0.5 - setup
->pixel_offset
);
292 const int y1
= subpixel_snap(v1
[0][1] + 0.5 - setup
->pixel_offset
);
293 const int y2
= subpixel_snap(v2
[0][1] + 0.5 - setup
->pixel_offset
);
294 const int y3
= subpixel_snap(v3
[0][1] + 0.5 - setup
->pixel_offset
);
296 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
297 struct lp_rast_triangle
*tri
;
300 int minx
, maxx
, miny
, maxy
;
303 tri
= alloc_triangle(scene
, setup
->fs
.nr_inputs
, &tri_bytes
);
306 tri
->v
[0][0] = v1
[0][0];
307 tri
->v
[1][0] = v2
[0][0];
308 tri
->v
[2][0] = v3
[0][0];
309 tri
->v
[0][1] = v1
[0][1];
310 tri
->v
[1][1] = v2
[0][1];
311 tri
->v
[2][1] = v3
[0][1];
322 area
= (tri
->dx12
* tri
->dy31
- tri
->dx31
* tri
->dy12
);
326 /* Cull non-ccw and zero-sized triangles.
328 * XXX: subject to overflow??
331 lp_scene_putback_data( scene
, tri_bytes
);
332 LP_COUNT(nr_culled_tris
);
336 /* Bounding rectangle (in pixels) */
337 minx
= (MIN3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
338 maxx
= (MAX3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
339 miny
= (MIN3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
340 maxy
= (MAX3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
342 if (setup
->scissor_test
) {
343 minx
= MAX2(minx
, setup
->scissor
.current
.minx
);
344 maxx
= MIN2(maxx
, setup
->scissor
.current
.maxx
);
345 miny
= MAX2(miny
, setup
->scissor
.current
.miny
);
346 maxy
= MIN2(maxy
, setup
->scissor
.current
.maxy
);
351 lp_scene_putback_data( scene
, tri_bytes
);
352 LP_COUNT(nr_culled_tris
);
358 oneoverarea
= ((float)FIXED_ONE
) / (float)area
;
360 /* Setup parameter interpolants:
362 setup_tri_coefficients( setup
, tri
, oneoverarea
, v1
, v2
, v3
, frontfacing
);
364 /* half-edge constants, will be interated over the whole render target.
366 tri
->c1
= tri
->dy12
* x1
- tri
->dx12
* y1
;
367 tri
->c2
= tri
->dy23
* x2
- tri
->dx23
* y2
;
368 tri
->c3
= tri
->dy31
* x3
- tri
->dx31
* y3
;
370 /* correct for top-left fill convention:
372 if (tri
->dy12
< 0 || (tri
->dy12
== 0 && tri
->dx12
> 0)) tri
->c1
++;
373 if (tri
->dy23
< 0 || (tri
->dy23
== 0 && tri
->dx23
> 0)) tri
->c2
++;
374 if (tri
->dy31
< 0 || (tri
->dy31
== 0 && tri
->dx31
> 0)) tri
->c3
++;
376 tri
->dy12
*= FIXED_ONE
;
377 tri
->dy23
*= FIXED_ONE
;
378 tri
->dy31
*= FIXED_ONE
;
380 tri
->dx12
*= FIXED_ONE
;
381 tri
->dx23
*= FIXED_ONE
;
382 tri
->dx31
*= FIXED_ONE
;
384 /* find trivial reject offsets for each edge for a single-pixel
385 * sized block. These will be scaled up at each recursive level to
386 * match the active blocksize. Scaling in this way works best if
387 * the blocks are square.
390 if (tri
->dy12
< 0) tri
->eo1
-= tri
->dy12
;
391 if (tri
->dx12
> 0) tri
->eo1
+= tri
->dx12
;
394 if (tri
->dy23
< 0) tri
->eo2
-= tri
->dy23
;
395 if (tri
->dx23
> 0) tri
->eo2
+= tri
->dx23
;
398 if (tri
->dy31
< 0) tri
->eo3
-= tri
->dy31
;
399 if (tri
->dx31
> 0) tri
->eo3
+= tri
->dx31
;
401 /* Calculate trivial accept offsets from the above.
403 tri
->ei1
= tri
->dx12
- tri
->dy12
- tri
->eo1
;
404 tri
->ei2
= tri
->dx23
- tri
->dy23
- tri
->eo2
;
405 tri
->ei3
= tri
->dx31
- tri
->dy31
- tri
->eo3
;
407 /* Fill in the inputs.step[][] arrays.
408 * We've manually unrolled some loops here.
411 const int xstep1
= -tri
->dy12
;
412 const int xstep2
= -tri
->dy23
;
413 const int xstep3
= -tri
->dy31
;
414 const int ystep1
= tri
->dx12
;
415 const int ystep2
= tri
->dx23
;
416 const int ystep3
= tri
->dx31
;
418 #define SETUP_STEP(i, x, y) \
420 tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
421 tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
422 tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
437 SETUP_STEP(10, 0, 3);
438 SETUP_STEP(11, 1, 3);
440 SETUP_STEP(12, 2, 2);
441 SETUP_STEP(13, 3, 2);
442 SETUP_STEP(14, 2, 3);
443 SETUP_STEP(15, 3, 3);
448 * All fields of 'tri' are now set. The remaining code here is
449 * concerned with binning.
452 /* Convert to tile coordinates:
454 minx
= minx
/ TILE_SIZE
;
455 miny
= miny
/ TILE_SIZE
;
456 maxx
= maxx
/ TILE_SIZE
;
457 maxy
= maxy
/ TILE_SIZE
;
460 * Clamp to framebuffer size
462 minx
= MAX2(minx
, 0);
463 miny
= MAX2(miny
, 0);
464 maxx
= MIN2(maxx
, scene
->tiles_x
- 1);
465 maxy
= MIN2(maxy
, scene
->tiles_y
- 1);
467 /* Determine which tile(s) intersect the triangle's bounding box
469 if (miny
== maxy
&& minx
== maxx
)
471 /* Triangle is contained in a single tile:
473 lp_scene_bin_command( scene
, minx
, miny
, lp_rast_triangle
,
474 lp_rast_arg_triangle(tri
) );
479 tri
->dx12
* miny
* TILE_SIZE
-
480 tri
->dy12
* minx
* TILE_SIZE
);
482 tri
->dx23
* miny
* TILE_SIZE
-
483 tri
->dy23
* minx
* TILE_SIZE
);
485 tri
->dx31
* miny
* TILE_SIZE
-
486 tri
->dy31
* minx
* TILE_SIZE
);
488 int ei1
= tri
->ei1
<< TILE_ORDER
;
489 int ei2
= tri
->ei2
<< TILE_ORDER
;
490 int ei3
= tri
->ei3
<< TILE_ORDER
;
492 int eo1
= tri
->eo1
<< TILE_ORDER
;
493 int eo2
= tri
->eo2
<< TILE_ORDER
;
494 int eo3
= tri
->eo3
<< TILE_ORDER
;
496 int xstep1
= -(tri
->dy12
<< TILE_ORDER
);
497 int xstep2
= -(tri
->dy23
<< TILE_ORDER
);
498 int xstep3
= -(tri
->dy31
<< TILE_ORDER
);
500 int ystep1
= tri
->dx12
<< TILE_ORDER
;
501 int ystep2
= tri
->dx23
<< TILE_ORDER
;
502 int ystep3
= tri
->dx31
<< TILE_ORDER
;
506 /* Test tile-sized blocks against the triangle.
507 * Discard blocks fully outside the tri. If the block is fully
508 * contained inside the tri, bin an lp_rast_shade_tile command.
509 * Else, bin a lp_rast_triangle command.
511 for (y
= miny
; y
<= maxy
; y
++)
516 boolean in
= FALSE
; /* are we inside the triangle? */
518 for (x
= minx
; x
<= maxx
; x
++)
525 LP_COUNT(nr_empty_64
);
527 break; /* exiting triangle, all done with this row */
529 else if (cx1
+ ei1
> 0 &&
533 /* triangle covers the whole tile- shade whole tile */
534 LP_COUNT(nr_fully_covered_64
);
536 if(setup
->fs
.current
.opaque
) {
537 lp_scene_bin_reset( scene
, x
, y
);
538 lp_scene_bin_command( scene
, x
, y
,
540 lp_rast_arg_state(setup
->fs
.stored
) );
542 lp_scene_bin_command( scene
, x
, y
,
544 lp_rast_arg_inputs(&tri
->inputs
) );
548 /* rasterizer/shade partial tile */
549 LP_COUNT(nr_partially_covered_64
);
551 lp_scene_bin_command( scene
, x
, y
,
553 lp_rast_arg_triangle(tri
) );
556 /* Iterate cx values across the region:
563 /* Iterate c values down the region:
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
);
582 static void triangle_ccw( struct lp_setup_context
*setup
,
583 const float (*v0
)[4],
584 const float (*v1
)[4],
585 const float (*v2
)[4] )
587 do_triangle_ccw( setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
591 static void triangle_both( struct lp_setup_context
*setup
,
592 const float (*v0
)[4],
593 const float (*v1
)[4],
594 const float (*v2
)[4] )
596 /* edge vectors e = v0 - v2, f = v1 - v2 */
597 const float ex
= v0
[0][0] - v2
[0][0];
598 const float ey
= v0
[0][1] - v2
[0][1];
599 const float fx
= v1
[0][0] - v2
[0][0];
600 const float fy
= v1
[0][1] - v2
[0][1];
602 /* det = cross(e,f).z */
603 if (ex
* fy
- ey
* fx
< 0.0f
)
604 triangle_ccw( setup
, v0
, v1
, v2
);
606 triangle_cw( setup
, v0
, v1
, v2
);
610 static void triangle_nop( struct lp_setup_context
*setup
,
611 const float (*v0
)[4],
612 const float (*v1
)[4],
613 const float (*v2
)[4] )
619 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
621 switch (setup
->cullmode
) {
622 case PIPE_WINDING_NONE
:
623 setup
->triangle
= triangle_both
;
625 case PIPE_WINDING_CCW
:
626 setup
->triangle
= triangle_cw
;
628 case PIPE_WINDING_CW
:
629 setup
->triangle
= triangle_ccw
;
632 setup
->triangle
= triangle_nop
;