8d781e358f8e72991ab226a58a64aec98446ac9e
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_rast_triangle
*tri
,
49 tri
->inputs
.a0
[slot
][i
] = value
;
50 tri
->inputs
.dadx
[slot
][i
] = 0.0f
;
51 tri
->inputs
.dady
[slot
][i
] = 0.0f
;
56 * Compute a0, dadx and dady for a linearly interpolated coefficient,
59 static void linear_coef( struct lp_rast_triangle
*tri
,
68 float a1
= v1
[vert_attr
][i
];
69 float a2
= v2
[vert_attr
][i
];
70 float a3
= v3
[vert_attr
][i
];
74 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
75 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
77 tri
->inputs
.dadx
[slot
][i
] = dadx
;
78 tri
->inputs
.dady
[slot
][i
] = dady
;
80 /* calculate a0 as the value which would be sampled for the
81 * fragment at (0,0), taking into account that we want to sample at
82 * pixel centers, in other words (0.5, 0.5).
84 * this is neat but unfortunately not a good way to do things for
85 * triangles with very large values of dadx or dady as it will
86 * result in the subtraction and re-addition from a0 of a very
87 * large number, which means we'll end up loosing a lot of the
88 * fractional bits and precision from a0. the way to fix this is
89 * to define a0 as the sample at a pixel center somewhere near vmin
90 * instead - i'll switch to this later.
92 tri
->inputs
.a0
[slot
][i
] = (a1
-
93 (dadx
* (v1
[0][0] - 0.5f
) +
94 dady
* (v1
[0][1] - 0.5f
)));
99 * Compute a0, dadx and dady for a perspective-corrected interpolant,
101 * We basically multiply the vertex value by 1/w before computing
102 * the plane coefficients (a0, dadx, dady).
103 * Later, when we compute the value at a particular fragment position we'll
104 * divide the interpolated value by the interpolated W at that fragment.
106 static void perspective_coef( struct lp_rast_triangle
*tri
,
109 const float (*v1
)[4],
110 const float (*v2
)[4],
111 const float (*v3
)[4],
115 /* premultiply by 1/w (v[0][3] is always 1/w):
117 float a1
= v1
[vert_attr
][i
] * v1
[0][3];
118 float a2
= v2
[vert_attr
][i
] * v2
[0][3];
119 float a3
= v3
[vert_attr
][i
] * v3
[0][3];
120 float da12
= a1
- a2
;
121 float da31
= a3
- a1
;
122 float dadx
= (da12
* tri
->dy31
- tri
->dy12
* da31
) * oneoverarea
;
123 float dady
= (da31
* tri
->dx12
- tri
->dx31
* da12
) * oneoverarea
;
125 tri
->inputs
.dadx
[slot
][i
] = dadx
;
126 tri
->inputs
.dady
[slot
][i
] = dady
;
127 tri
->inputs
.a0
[slot
][i
] = (a1
-
128 (dadx
* (v1
[0][0] - 0.5f
) +
129 dady
* (v1
[0][1] - 0.5f
)));
134 * Special coefficient setup for gl_FragCoord.
135 * X and Y are trivial
136 * Z and W are copied from position_coef which should have already been computed.
137 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
140 setup_fragcoord_coef(struct lp_rast_triangle
*tri
,
143 const float (*v1
)[4],
144 const float (*v2
)[4],
145 const float (*v3
)[4])
148 tri
->inputs
.a0
[slot
][0] = 0.0;
149 tri
->inputs
.dadx
[slot
][0] = 1.0;
150 tri
->inputs
.dady
[slot
][0] = 0.0;
152 tri
->inputs
.a0
[slot
][1] = 0.0;
153 tri
->inputs
.dadx
[slot
][1] = 0.0;
154 tri
->inputs
.dady
[slot
][1] = 1.0;
156 linear_coef(tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 2);
158 linear_coef(tri
, oneoverarea
, slot
, v1
, v2
, v3
, 0, 3);
162 static void setup_facing_coef( struct lp_rast_triangle
*tri
,
166 constant_coef( tri
, slot
, 1.0f
- frontface
, 0 );
167 constant_coef( tri
, slot
, 0.0f
, 1 ); /* wasted */
168 constant_coef( tri
, slot
, 0.0f
, 2 ); /* wasted */
169 constant_coef( tri
, slot
, 0.0f
, 3 ); /* wasted */
174 * Compute the tri->coef[] array dadx, dady, a0 values.
176 static void setup_tri_coefficients( struct lp_setup_context
*setup
,
177 struct lp_rast_triangle
*tri
,
179 const float (*v1
)[4],
180 const float (*v2
)[4],
181 const float (*v3
)[4],
186 /* The internal position input is in slot zero:
188 setup_fragcoord_coef(tri
, oneoverarea
, 0, v1
, v2
, v3
);
190 /* setup interpolation for all the remaining attributes:
192 for (slot
= 0; slot
< setup
->fs
.nr_inputs
; slot
++) {
193 unsigned vert_attr
= setup
->fs
.input
[slot
].src_index
;
196 switch (setup
->fs
.input
[slot
].interp
) {
197 case LP_INTERP_CONSTANT
:
198 for (i
= 0; i
< NUM_CHANNELS
; i
++)
199 constant_coef(tri
, slot
+1, v3
[vert_attr
][i
], i
);
202 case LP_INTERP_LINEAR
:
203 for (i
= 0; i
< NUM_CHANNELS
; i
++)
204 linear_coef(tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
207 case LP_INTERP_PERSPECTIVE
:
208 for (i
= 0; i
< NUM_CHANNELS
; i
++)
209 perspective_coef(tri
, oneoverarea
, slot
+1, v1
, v2
, v3
, vert_attr
, i
);
212 case LP_INTERP_POSITION
:
213 /* XXX: fix me - duplicates the values in slot zero.
215 setup_fragcoord_coef(tri
, oneoverarea
, slot
+1, v1
, v2
, v3
);
218 case LP_INTERP_FACING
:
219 setup_facing_coef(tri
, slot
+1, frontface
);
230 static INLINE
int subpixel_snap( float a
)
232 return util_iround(FIXED_ONE
* a
- (FIXED_ONE
/ 2));
238 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
239 * immediately after it.
240 * The memory is allocated from the per-scene pool, not per-tile.
241 * \param tri_size returns number of bytes allocated
242 * \param nr_inputs number of fragment shader inputs
243 * \return pointer to triangle space
245 static INLINE
struct lp_rast_triangle
*
246 alloc_triangle(struct lp_scene
*scene
, unsigned nr_inputs
, unsigned *tri_size
)
248 unsigned input_array_sz
= NUM_CHANNELS
* (nr_inputs
+ 1) * sizeof(float);
249 struct lp_rast_triangle
*tri
;
253 assert(sizeof(*tri
) % 16 == 0);
255 bytes
= sizeof(*tri
) + (3 * input_array_sz
);
257 tri
= lp_scene_alloc_aligned( scene
, bytes
, 16 );
259 inputs
= (char *) (tri
+ 1);
260 tri
->inputs
.a0
= (float (*)[4]) inputs
;
261 tri
->inputs
.dadx
= (float (*)[4]) (inputs
+ input_array_sz
);
262 tri
->inputs
.dady
= (float (*)[4]) (inputs
+ 2 * input_array_sz
);
272 * Do basic setup for triangle rasterization and determine which
273 * framebuffer tiles are touched. Put the triangle in the scene's
274 * bins for the tiles which we overlap.
277 do_triangle_ccw(struct lp_setup_context
*setup
,
278 const float (*v1
)[4],
279 const float (*v2
)[4],
280 const float (*v3
)[4],
281 boolean frontfacing
)
283 /* x/y positions in fixed point */
284 const int x1
= subpixel_snap(v1
[0][0]);
285 const int x2
= subpixel_snap(v2
[0][0]);
286 const int x3
= subpixel_snap(v3
[0][0]);
287 const int y1
= subpixel_snap(v1
[0][1]);
288 const int y2
= subpixel_snap(v2
[0][1]);
289 const int y3
= subpixel_snap(v3
[0][1]);
291 struct lp_scene
*scene
= lp_setup_get_current_scene(setup
);
292 struct lp_rast_triangle
*tri
;
295 int minx
, maxx
, miny
, maxy
;
298 tri
= alloc_triangle(scene
, setup
->fs
.nr_inputs
, &tri_bytes
);
301 tri
->v
[0][0] = v1
[0][0];
302 tri
->v
[1][0] = v2
[0][0];
303 tri
->v
[2][0] = v3
[0][0];
304 tri
->v
[0][1] = v1
[0][1];
305 tri
->v
[1][1] = v2
[0][1];
306 tri
->v
[2][1] = v3
[0][1];
317 area
= (tri
->dx12
* tri
->dy31
- tri
->dx31
* tri
->dy12
);
321 /* Cull non-ccw and zero-sized triangles.
323 * XXX: subject to overflow??
326 lp_scene_putback_data( scene
, tri_bytes
);
327 LP_COUNT(nr_culled_tris
);
331 /* Bounding rectangle (in pixels) */
332 minx
= (MIN3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
333 maxx
= (MAX3(x1
, x2
, x3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
334 miny
= (MIN3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
335 maxy
= (MAX3(y1
, y2
, y3
) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
337 if (setup
->scissor_test
) {
338 minx
= MAX2(minx
, setup
->scissor
.current
.minx
);
339 maxx
= MIN2(maxx
, setup
->scissor
.current
.maxx
);
340 miny
= MAX2(miny
, setup
->scissor
.current
.miny
);
341 maxy
= MIN2(maxy
, setup
->scissor
.current
.maxy
);
346 lp_scene_putback_data( scene
, tri_bytes
);
347 LP_COUNT(nr_culled_tris
);
353 oneoverarea
= ((float)FIXED_ONE
) / (float)area
;
355 /* Setup parameter interpolants:
357 setup_tri_coefficients( setup
, tri
, oneoverarea
, v1
, v2
, v3
, frontfacing
);
359 /* half-edge constants, will be interated over the whole render target.
361 tri
->c1
= tri
->dy12
* x1
- tri
->dx12
* y1
;
362 tri
->c2
= tri
->dy23
* x2
- tri
->dx23
* y2
;
363 tri
->c3
= tri
->dy31
* x3
- tri
->dx31
* y3
;
365 /* correct for top-left fill convention:
367 if (tri
->dy12
< 0 || (tri
->dy12
== 0 && tri
->dx12
> 0)) tri
->c1
++;
368 if (tri
->dy23
< 0 || (tri
->dy23
== 0 && tri
->dx23
> 0)) tri
->c2
++;
369 if (tri
->dy31
< 0 || (tri
->dy31
== 0 && tri
->dx31
> 0)) tri
->c3
++;
371 tri
->dy12
*= FIXED_ONE
;
372 tri
->dy23
*= FIXED_ONE
;
373 tri
->dy31
*= FIXED_ONE
;
375 tri
->dx12
*= FIXED_ONE
;
376 tri
->dx23
*= FIXED_ONE
;
377 tri
->dx31
*= FIXED_ONE
;
379 /* find trivial reject offsets for each edge for a single-pixel
380 * sized block. These will be scaled up at each recursive level to
381 * match the active blocksize. Scaling in this way works best if
382 * the blocks are square.
385 if (tri
->dy12
< 0) tri
->eo1
-= tri
->dy12
;
386 if (tri
->dx12
> 0) tri
->eo1
+= tri
->dx12
;
389 if (tri
->dy23
< 0) tri
->eo2
-= tri
->dy23
;
390 if (tri
->dx23
> 0) tri
->eo2
+= tri
->dx23
;
393 if (tri
->dy31
< 0) tri
->eo3
-= tri
->dy31
;
394 if (tri
->dx31
> 0) tri
->eo3
+= tri
->dx31
;
396 /* Calculate trivial accept offsets from the above.
398 tri
->ei1
= tri
->dx12
- tri
->dy12
- tri
->eo1
;
399 tri
->ei2
= tri
->dx23
- tri
->dy23
- tri
->eo2
;
400 tri
->ei3
= tri
->dx31
- tri
->dy31
- tri
->eo3
;
402 /* Fill in the inputs.step[][] arrays.
403 * We've manually unrolled some loops here.
406 const int xstep1
= -tri
->dy12
;
407 const int xstep2
= -tri
->dy23
;
408 const int xstep3
= -tri
->dy31
;
409 const int ystep1
= tri
->dx12
;
410 const int ystep2
= tri
->dx23
;
411 const int ystep3
= tri
->dx31
;
413 #define SETUP_STEP(i, x, y) \
415 tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
416 tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
417 tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
432 SETUP_STEP(10, 0, 3);
433 SETUP_STEP(11, 1, 3);
435 SETUP_STEP(12, 2, 2);
436 SETUP_STEP(13, 3, 2);
437 SETUP_STEP(14, 2, 3);
438 SETUP_STEP(15, 3, 3);
443 * All fields of 'tri' are now set. The remaining code here is
444 * concerned with binning.
447 /* Convert to tile coordinates:
449 minx
= minx
/ TILE_SIZE
;
450 miny
= miny
/ TILE_SIZE
;
451 maxx
= maxx
/ TILE_SIZE
;
452 maxy
= maxy
/ TILE_SIZE
;
455 * Clamp to framebuffer size
457 minx
= MAX2(minx
, 0);
458 miny
= MAX2(miny
, 0);
459 maxx
= MIN2(maxx
, scene
->tiles_x
- 1);
460 maxy
= MIN2(maxy
, scene
->tiles_y
- 1);
462 /* Determine which tile(s) intersect the triangle's bounding box
464 if (miny
== maxy
&& minx
== maxx
)
466 /* Triangle is contained in a single tile:
468 lp_scene_bin_command( scene
, minx
, miny
, lp_rast_triangle
,
469 lp_rast_arg_triangle(tri
) );
474 tri
->dx12
* miny
* TILE_SIZE
-
475 tri
->dy12
* minx
* TILE_SIZE
);
477 tri
->dx23
* miny
* TILE_SIZE
-
478 tri
->dy23
* minx
* TILE_SIZE
);
480 tri
->dx31
* miny
* TILE_SIZE
-
481 tri
->dy31
* minx
* TILE_SIZE
);
483 int ei1
= tri
->ei1
<< TILE_ORDER
;
484 int ei2
= tri
->ei2
<< TILE_ORDER
;
485 int ei3
= tri
->ei3
<< TILE_ORDER
;
487 int eo1
= tri
->eo1
<< TILE_ORDER
;
488 int eo2
= tri
->eo2
<< TILE_ORDER
;
489 int eo3
= tri
->eo3
<< TILE_ORDER
;
491 int xstep1
= -(tri
->dy12
<< TILE_ORDER
);
492 int xstep2
= -(tri
->dy23
<< TILE_ORDER
);
493 int xstep3
= -(tri
->dy31
<< TILE_ORDER
);
495 int ystep1
= tri
->dx12
<< TILE_ORDER
;
496 int ystep2
= tri
->dx23
<< TILE_ORDER
;
497 int ystep3
= tri
->dx31
<< TILE_ORDER
;
501 /* Test tile-sized blocks against the triangle.
502 * Discard blocks fully outside the tri. If the block is fully
503 * contained inside the tri, bin an lp_rast_shade_tile command.
504 * Else, bin a lp_rast_triangle command.
506 for (y
= miny
; y
<= maxy
; y
++)
511 boolean in
= FALSE
; /* are we inside the triangle? */
513 for (x
= minx
; x
<= maxx
; x
++)
520 LP_COUNT(nr_empty_64
);
522 break; /* exiting triangle, all done with this row */
524 else if (cx1
+ ei1
> 0 &&
528 /* triangle covers the whole tile- shade whole tile */
529 LP_COUNT(nr_fully_covered_64
);
531 if(setup
->fs
.current
.opaque
) {
532 lp_scene_bin_reset( scene
, x
, y
);
533 lp_scene_bin_command( scene
, x
, y
,
535 lp_rast_arg_state(setup
->fs
.stored
) );
537 lp_scene_bin_command( scene
, x
, y
,
539 lp_rast_arg_inputs(&tri
->inputs
) );
543 /* rasterizer/shade partial tile */
544 LP_COUNT(nr_partially_covered_64
);
546 lp_scene_bin_command( scene
, x
, y
,
548 lp_rast_arg_triangle(tri
) );
551 /* Iterate cx values across the region:
558 /* Iterate c values down the region:
568 static void triangle_cw( struct lp_setup_context
*setup
,
569 const float (*v0
)[4],
570 const float (*v1
)[4],
571 const float (*v2
)[4] )
573 do_triangle_ccw( setup
, v1
, v0
, v2
, !setup
->ccw_is_frontface
);
577 static void triangle_ccw( struct lp_setup_context
*setup
,
578 const float (*v0
)[4],
579 const float (*v1
)[4],
580 const float (*v2
)[4] )
582 do_triangle_ccw( setup
, v0
, v1
, v2
, setup
->ccw_is_frontface
);
586 static void triangle_both( struct lp_setup_context
*setup
,
587 const float (*v0
)[4],
588 const float (*v1
)[4],
589 const float (*v2
)[4] )
591 /* edge vectors e = v0 - v2, f = v1 - v2 */
592 const float ex
= v0
[0][0] - v2
[0][0];
593 const float ey
= v0
[0][1] - v2
[0][1];
594 const float fx
= v1
[0][0] - v2
[0][0];
595 const float fy
= v1
[0][1] - v2
[0][1];
597 /* det = cross(e,f).z */
598 if (ex
* fy
- ey
* fx
< 0.0f
)
599 triangle_ccw( setup
, v0
, v1
, v2
);
601 triangle_cw( setup
, v0
, v1
, v2
);
605 static void triangle_nop( struct lp_setup_context
*setup
,
606 const float (*v0
)[4],
607 const float (*v1
)[4],
608 const float (*v2
)[4] )
614 lp_setup_choose_triangle( struct lp_setup_context
*setup
)
616 switch (setup
->cullmode
) {
617 case PIPE_WINDING_NONE
:
618 setup
->triangle
= triangle_both
;
620 case PIPE_WINDING_CCW
:
621 setup
->triangle
= triangle_cw
;
623 case PIPE_WINDING_CW
:
624 setup
->triangle
= triangle_ccw
;
627 setup
->triangle
= triangle_nop
;