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 lines
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
35 #include "lp_setup_context.h"
37 #include "lp_state_fs.h"
38 #include "lp_state_setup.h"
40 #define NUM_CHANNELS 4
58 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
60 static void constant_coef( struct lp_setup_context
*setup
,
61 struct lp_line_info
*info
,
66 info
->a0
[slot
][i
] = value
;
67 info
->dadx
[slot
][i
] = 0.0f
;
68 info
->dady
[slot
][i
] = 0.0f
;
73 * Compute a0, dadx and dady for a linearly interpolated coefficient,
76 static void linear_coef( struct lp_setup_context
*setup
,
77 struct lp_line_info
*info
,
82 float a1
= info
->v1
[vert_attr
][i
];
83 float a2
= info
->v2
[vert_attr
][i
];
86 float dadx
= da21
* info
->dx
* info
->oneoverarea
;
87 float dady
= da21
* info
->dy
* info
->oneoverarea
;
89 info
->dadx
[slot
][i
] = dadx
;
90 info
->dady
[slot
][i
] = dady
;
92 info
->a0
[slot
][i
] = (a1
-
93 (dadx
* (info
->v1
[0][0] - setup
->pixel_offset
) +
94 dady
* (info
->v1
[0][1] - setup
->pixel_offset
)));
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_setup_context
*setup
,
107 struct lp_line_info
*info
,
112 /* premultiply by 1/w (v[0][3] is always 1/w):
114 float a1
= info
->v1
[vert_attr
][i
] * info
->v1
[0][3];
115 float a2
= info
->v2
[vert_attr
][i
] * info
->v2
[0][3];
117 float da21
= a1
- a2
;
118 float dadx
= da21
* info
->dx
* info
->oneoverarea
;
119 float dady
= da21
* info
->dy
* info
->oneoverarea
;
121 info
->dadx
[slot
][i
] = dadx
;
122 info
->dady
[slot
][i
] = dady
;
124 info
->a0
[slot
][i
] = (a1
-
125 (dadx
* (info
->v1
[0][0] - setup
->pixel_offset
) +
126 dady
* (info
->v1
[0][1] - setup
->pixel_offset
)));
130 setup_fragcoord_coef( struct lp_setup_context
*setup
,
131 struct lp_line_info
*info
,
136 if (usage_mask
& TGSI_WRITEMASK_X
) {
137 info
->a0
[slot
][0] = 0.0;
138 info
->dadx
[slot
][0] = 1.0;
139 info
->dady
[slot
][0] = 0.0;
143 if (usage_mask
& TGSI_WRITEMASK_Y
) {
144 info
->a0
[slot
][1] = 0.0;
145 info
->dadx
[slot
][1] = 0.0;
146 info
->dady
[slot
][1] = 1.0;
150 if (usage_mask
& TGSI_WRITEMASK_Z
) {
151 linear_coef(setup
, info
, slot
, 0, 2);
155 if (usage_mask
& TGSI_WRITEMASK_W
) {
156 linear_coef(setup
, info
, slot
, 0, 3);
161 * Compute the tri->coef[] array dadx, dady, a0 values.
163 static void setup_line_coefficients( struct lp_setup_context
*setup
,
164 struct lp_line_info
*info
)
166 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
167 unsigned fragcoord_usage_mask
= TGSI_WRITEMASK_XYZ
;
170 /* setup interpolation for all the remaining attributes:
172 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
173 unsigned vert_attr
= key
->inputs
[slot
].src_index
;
174 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
177 switch (key
->inputs
[slot
].interp
) {
178 case LP_INTERP_CONSTANT
:
179 if (key
->flatshade_first
) {
180 for (i
= 0; i
< NUM_CHANNELS
; i
++)
181 if (usage_mask
& (1 << i
))
182 constant_coef(setup
, info
, slot
+1, info
->v1
[vert_attr
][i
], i
);
185 for (i
= 0; i
< NUM_CHANNELS
; i
++)
186 if (usage_mask
& (1 << i
))
187 constant_coef(setup
, info
, slot
+1, info
->v2
[vert_attr
][i
], i
);
191 case LP_INTERP_LINEAR
:
192 for (i
= 0; i
< NUM_CHANNELS
; i
++)
193 if (usage_mask
& (1 << i
))
194 linear_coef(setup
, info
, slot
+1, vert_attr
, i
);
197 case LP_INTERP_PERSPECTIVE
:
198 for (i
= 0; i
< NUM_CHANNELS
; i
++)
199 if (usage_mask
& (1 << i
))
200 perspective_coef(setup
, info
, slot
+1, vert_attr
, i
);
201 fragcoord_usage_mask
|= TGSI_WRITEMASK_W
;
204 case LP_INTERP_POSITION
:
206 * The generated pixel interpolators will pick up the coeffs from
207 * slot 0, so all need to ensure that the usage mask is covers all
210 fragcoord_usage_mask
|= usage_mask
;
213 case LP_INTERP_FACING
:
214 for (i
= 0; i
< NUM_CHANNELS
; i
++)
215 if (usage_mask
& (1 << i
))
216 constant_coef(setup
, info
, slot
+1, 1.0, i
);
224 /* The internal position input is in slot zero:
226 setup_fragcoord_coef(setup
, info
, 0,
227 fragcoord_usage_mask
);
232 static INLINE
int subpixel_snap( float a
)
234 return util_iround(FIXED_ONE
* a
);
239 * Print line vertex attribs (for debug).
242 print_line(struct lp_setup_context
*setup
,
243 const float (*v1
)[4],
244 const float (*v2
)[4])
246 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
249 debug_printf("llvmpipe line\n");
250 for (i
= 0; i
< 1 + key
->num_inputs
; i
++) {
251 debug_printf(" v1[%d]: %f %f %f %f\n", i
,
252 v1
[i
][0], v1
[i
][1], v1
[i
][2], v1
[i
][3]);
254 for (i
= 0; i
< 1 + key
->num_inputs
; i
++) {
255 debug_printf(" v2[%d]: %f %f %f %f\n", i
,
256 v2
[i
][0], v2
[i
][1], v2
[i
][2], v2
[i
][3]);
261 static INLINE boolean
sign(float x
){
266 /* Used on positive floats only:
268 static INLINE
float fracf(float f
)
270 return f
- floorf(f
);
276 try_setup_line( struct lp_setup_context
*setup
,
277 const float (*v1
)[4],
278 const float (*v2
)[4])
280 struct lp_scene
*scene
= setup
->scene
;
281 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
282 struct lp_rast_triangle
*line
;
283 struct lp_rast_plane
*plane
;
284 struct lp_line_info info
;
285 float width
= MAX2(1.0, setup
->line_width
);
293 /* linewidth should be interpreted as integer */
294 int fixed_width
= util_iround(width
) * FIXED_ONE
;
298 float x_offset_end
=0;
299 float y_offset_end
=0;
310 boolean will_draw_start
;
311 boolean will_draw_end
;
314 print_line(setup
, v1
, v2
);
316 if (setup
->scissor_test
) {
324 dx
= v1
[0][0] - v2
[0][0];
325 dy
= v1
[0][1] - v2
[0][1];
326 area
= (dx
* dx
+ dy
* dy
);
328 LP_COUNT(nr_culled_tris
);
332 info
.oneoverarea
= 1.0f
/ area
;
340 if (fabsf(dx
) >= fabsf(dy
)) {
341 float dydx
= dy
/ dx
;
343 x1diff
= v1
[0][0] - (float) floor(v1
[0][0]) - 0.5;
344 y1diff
= v1
[0][1] - (float) floor(v1
[0][1]) - 0.5;
345 x2diff
= v2
[0][0] - (float) floor(v2
[0][0]) - 0.5;
346 y2diff
= v2
[0][1] - (float) floor(v2
[0][1]) - 0.5;
348 if (y2diff
==-0.5 && dy
<0){
353 * Diamond exit rule test for starting point
355 if (fabsf(x1diff
) + fabsf(y1diff
) < 0.5) {
358 else if (sign(x1diff
) == sign(-dx
)) {
361 else if (sign(-y1diff
) != sign(dy
)) {
365 /* do intersection test */
366 float yintersect
= fracf(v1
[0][1]) + x1diff
* dydx
;
367 draw_start
= (yintersect
< 1.0 && yintersect
> 0.0);
372 * Diamond exit rule test for ending point
374 if (fabsf(x2diff
) + fabsf(y2diff
) < 0.5) {
377 else if (sign(x2diff
) != sign(-dx
)) {
380 else if (sign(-y2diff
) == sign(dy
)) {
384 /* do intersection test */
385 float yintersect
= fracf(v2
[0][1]) + x2diff
* dydx
;
386 draw_end
= (yintersect
< 1.0 && yintersect
> 0.0);
389 /* Are we already drawing start/end?
391 will_draw_start
= sign(-x1diff
) != sign(dx
);
392 will_draw_end
= (sign(x2diff
) == sign(-dx
)) || x2diff
==0;
395 /* if v2 is to the right of v1, swap pointers */
396 const float (*temp
)[4] = v1
;
401 /* Otherwise shift planes appropriately */
402 if (will_draw_start
!= draw_start
) {
403 x_offset_end
= - x1diff
- 0.5;
404 y_offset_end
= x_offset_end
* dydx
;
407 if (will_draw_end
!= draw_end
) {
408 x_offset
= - x2diff
- 0.5;
409 y_offset
= x_offset
* dydx
;
414 /* Otherwise shift planes appropriately */
415 if (will_draw_start
!= draw_start
) {
416 x_offset
= - x1diff
+ 0.5;
417 y_offset
= x_offset
* dydx
;
419 if (will_draw_end
!= draw_end
) {
420 x_offset_end
= - x2diff
+ 0.5;
421 y_offset_end
= x_offset_end
* dydx
;
425 /* x/y positions in fixed point */
426 x
[0] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
);
427 x
[1] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
);
428 x
[2] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
);
429 x
[3] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
);
431 y
[0] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
) - fixed_width
/2;
432 y
[1] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
) - fixed_width
/2;
433 y
[2] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
) + fixed_width
/2;
434 y
[3] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
) + fixed_width
/2;
438 const float dxdy
= dx
/ dy
;
441 x1diff
= v1
[0][0] - (float) floor(v1
[0][0]) - 0.5;
442 y1diff
= v1
[0][1] - (float) floor(v1
[0][1]) - 0.5;
443 x2diff
= v2
[0][0] - (float) floor(v2
[0][0]) - 0.5;
444 y2diff
= v2
[0][1] - (float) floor(v2
[0][1]) - 0.5;
446 if (x2diff
==-0.5 && dx
<0) {
451 * Diamond exit rule test for starting point
453 if (fabsf(x1diff
) + fabsf(y1diff
) < 0.5) {
456 else if (sign(-y1diff
) == sign(dy
)) {
459 else if (sign(x1diff
) != sign(-dx
)) {
463 /* do intersection test */
464 float xintersect
= fracf(v1
[0][0]) + y1diff
* dxdy
;
465 draw_start
= (xintersect
< 1.0 && xintersect
> 0.0);
469 * Diamond exit rule test for ending point
471 if (fabsf(x2diff
) + fabsf(y2diff
) < 0.5) {
474 else if (sign(-y2diff
) != sign(dy
) ) {
477 else if (sign(x2diff
) == sign(-dx
) ) {
481 /* do intersection test */
482 float xintersect
= fracf(v2
[0][0]) + y2diff
* dxdy
;
483 draw_end
= (xintersect
< 1.0 && xintersect
>= 0.0);
486 /* Are we already drawing start/end?
488 will_draw_start
= sign(y1diff
) == sign(dy
);
489 will_draw_end
= (sign(-y2diff
) == sign(dy
)) || y2diff
==0;
492 /* if v2 is on top of v1, swap pointers */
493 const float (*temp
)[4] = v1
;
499 /* Otherwise shift planes appropriately */
500 if (will_draw_start
!= draw_start
) {
501 y_offset_end
= - y1diff
+ 0.5;
502 x_offset_end
= y_offset_end
* dxdy
;
504 if (will_draw_end
!= draw_end
) {
505 y_offset
= - y2diff
+ 0.5;
506 x_offset
= y_offset
* dxdy
;
510 /* Otherwise shift planes appropriately */
511 if (will_draw_start
!= draw_start
) {
512 y_offset
= - y1diff
- 0.5;
513 x_offset
= y_offset
* dxdy
;
516 if (will_draw_end
!= draw_end
) {
517 y_offset_end
= - y2diff
- 0.5;
518 x_offset_end
= y_offset_end
* dxdy
;
522 /* x/y positions in fixed point */
523 x
[0] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
) - fixed_width
/2;
524 x
[1] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
) - fixed_width
/2;
525 x
[2] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
) + fixed_width
/2;
526 x
[3] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
) + fixed_width
/2;
528 y
[0] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
);
529 y
[1] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
);
530 y
[2] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
);
531 y
[3] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
);
539 /* Bounding rectangle (in pixels) */
541 /* Yes this is necessary to accurately calculate bounding boxes
542 * with the two fill-conventions we support. GL (normally) ends
543 * up needing a bottom-left fill convention, which requires
544 * slightly different rounding.
546 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
548 bbox
.x0
= (MIN4(x
[0], x
[1], x
[2], x
[3]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
549 bbox
.x1
= (MAX4(x
[0], x
[1], x
[2], x
[3]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
550 bbox
.y0
= (MIN4(y
[0], y
[1], y
[2], y
[3]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
551 bbox
.y1
= (MAX4(y
[0], y
[1], y
[2], y
[3]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
553 /* Inclusive coordinates:
559 if (bbox
.x1
< bbox
.x0
||
561 if (0) debug_printf("empty bounding box\n");
562 LP_COUNT(nr_culled_tris
);
566 if (!u_rect_test_intersection(&setup
->draw_region
, &bbox
)) {
567 if (0) debug_printf("offscreen\n");
568 LP_COUNT(nr_culled_tris
);
572 /* Can safely discard negative regions:
574 bbox
.x0
= MAX2(bbox
.x0
, 0);
575 bbox
.y0
= MAX2(bbox
.y0
, 0);
577 line
= lp_setup_alloc_triangle(scene
,
585 line
->v
[0][0] = v1
[0][0];
586 line
->v
[1][0] = v2
[0][0];
587 line
->v
[0][1] = v1
[0][1];
588 line
->v
[1][1] = v2
[0][1];
591 /* calculate the deltas */
592 plane
= GET_PLANES(line
);
593 plane
[0].dcdy
= x
[0] - x
[1];
594 plane
[1].dcdy
= x
[1] - x
[2];
595 plane
[2].dcdy
= x
[2] - x
[3];
596 plane
[3].dcdy
= x
[3] - x
[0];
598 plane
[0].dcdx
= y
[0] - y
[1];
599 plane
[1].dcdx
= y
[1] - y
[2];
600 plane
[2].dcdx
= y
[2] - y
[3];
601 plane
[3].dcdx
= y
[3] - y
[0];
604 /* Setup parameter interpolants:
606 info
.a0
= GET_A0(&line
->inputs
);
607 info
.dadx
= GET_DADX(&line
->inputs
);
608 info
.dady
= GET_DADY(&line
->inputs
);
609 setup_line_coefficients(setup
, &info
);
611 line
->inputs
.frontfacing
= TRUE
;
612 line
->inputs
.disable
= FALSE
;
613 line
->inputs
.opaque
= FALSE
;
615 for (i
= 0; i
< 4; i
++) {
617 /* half-edge constants, will be interated over the whole render
620 plane
[i
].c
= plane
[i
].dcdx
* x
[i
] - plane
[i
].dcdy
* y
[i
];
623 /* correct for top-left vs. bottom-left fill convention.
625 * note that we're overloading gl_rasterization_rules to mean
626 * both (0.5,0.5) pixel centers *and* bottom-left filling
629 * GL actually has a top-left filling convention, but GL's
630 * notion of "top" differs from gallium's...
632 * Also, sometimes (in FBO cases) GL will render upside down
633 * to its usual method, in which case it will probably want
634 * to use the opposite, top-left convention.
636 if (plane
[i
].dcdx
< 0) {
637 /* both fill conventions want this - adjust for left edges */
640 else if (plane
[i
].dcdx
== 0) {
641 if (setup
->pixel_offset
== 0) {
642 /* correct for top-left fill convention:
644 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
647 /* correct for bottom-left fill convention:
649 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
653 plane
[i
].dcdx
*= FIXED_ONE
;
654 plane
[i
].dcdy
*= FIXED_ONE
;
656 /* find trivial reject offsets for each edge for a single-pixel
657 * sized block. These will be scaled up at each recursive level to
658 * match the active blocksize. Scaling in this way works best if
659 * the blocks are square.
662 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
663 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
668 * When rasterizing scissored tris, use the intersection of the
669 * triangle bounding box and the scissor rect to generate the
672 * This permits us to cut off the triangle "tails" that are present
673 * in the intermediate recursive levels caused when two of the
674 * triangles edges don't diverge quickly enough to trivially reject
675 * exterior blocks from the triangle.
677 * It's not really clear if it's worth worrying about these tails,
678 * but since we generate the planes for each scissored tri, it's
679 * free to trim them in this case.
681 * Note that otherwise, the scissor planes only vary in 'C' value,
682 * and even then only on state-changes. Could alternatively store
683 * these planes elsewhere.
685 if (nr_planes
== 8) {
686 const struct u_rect
*scissor
= &setup
->scissor
;
690 plane
[4].c
= 1-scissor
->x0
;
695 plane
[5].c
= scissor
->x1
+1;
700 plane
[6].c
= 1-scissor
->y0
;
705 plane
[7].c
= scissor
->y1
+1;
709 return lp_setup_bin_triangle(setup
, line
, &bbox
, nr_planes
);
713 static void lp_setup_line( struct lp_setup_context
*setup
,
714 const float (*v0
)[4],
715 const float (*v1
)[4] )
717 if (!try_setup_line( setup
, v0
, v1
))
719 if (!lp_setup_flush_and_restart(setup
))
722 if (!try_setup_line( setup
, v0
, v1
))
728 void lp_setup_choose_line( struct lp_setup_context
*setup
)
730 setup
->line
= lp_setup_line
;