1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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"
39 #include "lp_context.h"
40 #include "draw/draw_context.h"
42 #define NUM_CHANNELS 4
61 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
63 static void constant_coef( struct lp_setup_context
*setup
,
64 struct lp_line_info
*info
,
69 info
->a0
[slot
][i
] = value
;
70 info
->dadx
[slot
][i
] = 0.0f
;
71 info
->dady
[slot
][i
] = 0.0f
;
76 * Compute a0, dadx and dady for a linearly interpolated coefficient,
79 static void linear_coef( struct lp_setup_context
*setup
,
80 struct lp_line_info
*info
,
85 float a1
= info
->v1
[vert_attr
][i
];
86 float a2
= info
->v2
[vert_attr
][i
];
89 float dadx
= da21
* info
->dx
* info
->oneoverarea
;
90 float dady
= da21
* info
->dy
* info
->oneoverarea
;
92 info
->dadx
[slot
][i
] = dadx
;
93 info
->dady
[slot
][i
] = dady
;
95 info
->a0
[slot
][i
] = (a1
-
96 (dadx
* (info
->v1
[0][0] - setup
->pixel_offset
) +
97 dady
* (info
->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_line_info
*info
,
115 /* premultiply by 1/w (v[0][3] is always 1/w):
117 float a1
= info
->v1
[vert_attr
][i
] * info
->v1
[0][3];
118 float a2
= info
->v2
[vert_attr
][i
] * info
->v2
[0][3];
120 float da21
= a1
- a2
;
121 float dadx
= da21
* info
->dx
* info
->oneoverarea
;
122 float dady
= da21
* info
->dy
* info
->oneoverarea
;
124 info
->dadx
[slot
][i
] = dadx
;
125 info
->dady
[slot
][i
] = dady
;
127 info
->a0
[slot
][i
] = (a1
-
128 (dadx
* (info
->v1
[0][0] - setup
->pixel_offset
) +
129 dady
* (info
->v1
[0][1] - setup
->pixel_offset
)));
133 setup_fragcoord_coef( struct lp_setup_context
*setup
,
134 struct lp_line_info
*info
,
139 if (usage_mask
& TGSI_WRITEMASK_X
) {
140 info
->a0
[slot
][0] = 0.0;
141 info
->dadx
[slot
][0] = 1.0;
142 info
->dady
[slot
][0] = 0.0;
146 if (usage_mask
& TGSI_WRITEMASK_Y
) {
147 info
->a0
[slot
][1] = 0.0;
148 info
->dadx
[slot
][1] = 0.0;
149 info
->dady
[slot
][1] = 1.0;
153 if (usage_mask
& TGSI_WRITEMASK_Z
) {
154 linear_coef(setup
, info
, slot
, 0, 2);
158 if (usage_mask
& TGSI_WRITEMASK_W
) {
159 linear_coef(setup
, info
, slot
, 0, 3);
164 * Compute the tri->coef[] array dadx, dady, a0 values.
166 static void setup_line_coefficients( struct lp_setup_context
*setup
,
167 struct lp_line_info
*info
)
169 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
170 unsigned fragcoord_usage_mask
= TGSI_WRITEMASK_XYZ
;
173 /* setup interpolation for all the remaining attributes:
175 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
176 unsigned vert_attr
= key
->inputs
[slot
].src_index
;
177 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
180 switch (key
->inputs
[slot
].interp
) {
181 case LP_INTERP_CONSTANT
:
182 if (key
->flatshade_first
) {
183 for (i
= 0; i
< NUM_CHANNELS
; i
++)
184 if (usage_mask
& (1 << i
))
185 constant_coef(setup
, info
, slot
+1, info
->v1
[vert_attr
][i
], i
);
188 for (i
= 0; i
< NUM_CHANNELS
; i
++)
189 if (usage_mask
& (1 << i
))
190 constant_coef(setup
, info
, slot
+1, info
->v2
[vert_attr
][i
], i
);
194 case LP_INTERP_LINEAR
:
195 for (i
= 0; i
< NUM_CHANNELS
; i
++)
196 if (usage_mask
& (1 << i
))
197 linear_coef(setup
, info
, slot
+1, vert_attr
, i
);
200 case LP_INTERP_PERSPECTIVE
:
201 for (i
= 0; i
< NUM_CHANNELS
; i
++)
202 if (usage_mask
& (1 << i
))
203 perspective_coef(setup
, info
, slot
+1, vert_attr
, i
);
204 fragcoord_usage_mask
|= TGSI_WRITEMASK_W
;
207 case LP_INTERP_POSITION
:
209 * The generated pixel interpolators will pick up the coeffs from
210 * slot 0, so all need to ensure that the usage mask is covers all
213 fragcoord_usage_mask
|= usage_mask
;
216 case LP_INTERP_FACING
:
217 for (i
= 0; i
< NUM_CHANNELS
; i
++)
218 if (usage_mask
& (1 << i
))
219 constant_coef(setup
, info
, slot
+1,
220 info
->frontfacing
? 1.0f
: -1.0f
, i
);
228 /* The internal position input is in slot zero:
230 setup_fragcoord_coef(setup
, info
, 0,
231 fragcoord_usage_mask
);
236 static inline int subpixel_snap( float a
)
238 return util_iround(FIXED_ONE
* a
);
243 * Print line vertex attribs (for debug).
246 print_line(struct lp_setup_context
*setup
,
247 const float (*v1
)[4],
248 const float (*v2
)[4])
250 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
253 debug_printf("llvmpipe line\n");
254 for (i
= 0; i
< 1 + key
->num_inputs
; i
++) {
255 debug_printf(" v1[%d]: %f %f %f %f\n", i
,
256 v1
[i
][0], v1
[i
][1], v1
[i
][2], v1
[i
][3]);
258 for (i
= 0; i
< 1 + key
->num_inputs
; i
++) {
259 debug_printf(" v2[%d]: %f %f %f %f\n", i
,
260 v2
[i
][0], v2
[i
][1], v2
[i
][2], v2
[i
][3]);
265 static inline boolean
sign(float x
){
270 /* Used on positive floats only:
272 static inline float fracf(float f
)
274 return f
- floorf(f
);
280 try_setup_line( struct lp_setup_context
*setup
,
281 const float (*v1
)[4],
282 const float (*v2
)[4])
284 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
285 struct lp_scene
*scene
= setup
->scene
;
286 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
287 struct lp_rast_triangle
*line
;
288 struct lp_rast_plane
*plane
;
289 struct lp_line_info info
;
290 float width
= MAX2(1.0, setup
->line_width
);
297 unsigned viewport_index
= 0;
300 /* linewidth should be interpreted as integer */
301 int fixed_width
= util_iround(width
) * FIXED_ONE
;
305 float x_offset_end
=0;
306 float y_offset_end
=0;
314 const float (*pv
)[4];
318 boolean will_draw_start
;
319 boolean will_draw_end
;
322 print_line(setup
, v1
, v2
);
324 if (setup
->flatshade_first
) {
330 if (setup
->viewport_index_slot
> 0) {
331 unsigned *udata
= (unsigned*)pv
[setup
->viewport_index_slot
];
332 viewport_index
= lp_clamp_viewport_idx(*udata
);
334 if (setup
->layer_slot
> 0) {
335 layer
= *(unsigned*)pv
[setup
->layer_slot
];
336 layer
= MIN2(layer
, scene
->fb_max_layer
);
339 if (setup
->scissor_test
) {
346 dx
= v1
[0][0] - v2
[0][0];
347 dy
= v1
[0][1] - v2
[0][1];
348 area
= (dx
* dx
+ dy
* dy
);
350 LP_COUNT(nr_culled_tris
);
354 info
.oneoverarea
= 1.0f
/ area
;
362 if (fabsf(dx
) >= fabsf(dy
)) {
363 float dydx
= dy
/ dx
;
365 x1diff
= v1
[0][0] - (float) floor(v1
[0][0]) - 0.5;
366 y1diff
= v1
[0][1] - (float) floor(v1
[0][1]) - 0.5;
367 x2diff
= v2
[0][0] - (float) floor(v2
[0][0]) - 0.5;
368 y2diff
= v2
[0][1] - (float) floor(v2
[0][1]) - 0.5;
370 if (y2diff
==-0.5 && dy
<0){
375 * Diamond exit rule test for starting point
377 if (fabsf(x1diff
) + fabsf(y1diff
) < 0.5) {
380 else if (sign(x1diff
) == sign(-dx
)) {
383 else if (sign(-y1diff
) != sign(dy
)) {
387 /* do intersection test */
388 float yintersect
= fracf(v1
[0][1]) + x1diff
* dydx
;
389 draw_start
= (yintersect
< 1.0 && yintersect
> 0.0);
394 * Diamond exit rule test for ending point
396 if (fabsf(x2diff
) + fabsf(y2diff
) < 0.5) {
399 else if (sign(x2diff
) != sign(-dx
)) {
402 else if (sign(-y2diff
) == sign(dy
)) {
406 /* do intersection test */
407 float yintersect
= fracf(v2
[0][1]) + x2diff
* dydx
;
408 draw_end
= (yintersect
< 1.0 && yintersect
> 0.0);
411 /* Are we already drawing start/end?
413 will_draw_start
= sign(-x1diff
) != sign(dx
);
414 will_draw_end
= (sign(x2diff
) == sign(-dx
)) || x2diff
==0;
417 /* if v2 is to the right of v1, swap pointers */
418 const float (*temp
)[4] = v1
;
423 /* Otherwise shift planes appropriately */
424 if (will_draw_start
!= draw_start
) {
425 x_offset_end
= - x1diff
- 0.5;
426 y_offset_end
= x_offset_end
* dydx
;
429 if (will_draw_end
!= draw_end
) {
430 x_offset
= - x2diff
- 0.5;
431 y_offset
= x_offset
* dydx
;
436 /* Otherwise shift planes appropriately */
437 if (will_draw_start
!= draw_start
) {
438 x_offset
= - x1diff
+ 0.5;
439 y_offset
= x_offset
* dydx
;
441 if (will_draw_end
!= draw_end
) {
442 x_offset_end
= - x2diff
+ 0.5;
443 y_offset_end
= x_offset_end
* dydx
;
447 /* x/y positions in fixed point */
448 x
[0] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
);
449 x
[1] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
);
450 x
[2] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
);
451 x
[3] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
);
453 y
[0] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
) - fixed_width
/2;
454 y
[1] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
) - fixed_width
/2;
455 y
[2] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
) + fixed_width
/2;
456 y
[3] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
) + fixed_width
/2;
460 const float dxdy
= dx
/ dy
;
463 x1diff
= v1
[0][0] - (float) floor(v1
[0][0]) - 0.5;
464 y1diff
= v1
[0][1] - (float) floor(v1
[0][1]) - 0.5;
465 x2diff
= v2
[0][0] - (float) floor(v2
[0][0]) - 0.5;
466 y2diff
= v2
[0][1] - (float) floor(v2
[0][1]) - 0.5;
468 if (x2diff
==-0.5 && dx
<0) {
473 * Diamond exit rule test for starting point
475 if (fabsf(x1diff
) + fabsf(y1diff
) < 0.5) {
478 else if (sign(-y1diff
) == sign(dy
)) {
481 else if (sign(x1diff
) != sign(-dx
)) {
485 /* do intersection test */
486 float xintersect
= fracf(v1
[0][0]) + y1diff
* dxdy
;
487 draw_start
= (xintersect
< 1.0 && xintersect
> 0.0);
491 * Diamond exit rule test for ending point
493 if (fabsf(x2diff
) + fabsf(y2diff
) < 0.5) {
496 else if (sign(-y2diff
) != sign(dy
) ) {
499 else if (sign(x2diff
) == sign(-dx
) ) {
503 /* do intersection test */
504 float xintersect
= fracf(v2
[0][0]) + y2diff
* dxdy
;
505 draw_end
= (xintersect
< 1.0 && xintersect
>= 0.0);
508 /* Are we already drawing start/end?
510 will_draw_start
= sign(y1diff
) == sign(dy
);
511 will_draw_end
= (sign(-y2diff
) == sign(dy
)) || y2diff
==0;
514 /* if v2 is on top of v1, swap pointers */
515 const float (*temp
)[4] = v1
;
521 /* Otherwise shift planes appropriately */
522 if (will_draw_start
!= draw_start
) {
523 y_offset_end
= - y1diff
+ 0.5;
524 x_offset_end
= y_offset_end
* dxdy
;
526 if (will_draw_end
!= draw_end
) {
527 y_offset
= - y2diff
+ 0.5;
528 x_offset
= y_offset
* dxdy
;
532 /* Otherwise shift planes appropriately */
533 if (will_draw_start
!= draw_start
) {
534 y_offset
= - y1diff
- 0.5;
535 x_offset
= y_offset
* dxdy
;
538 if (will_draw_end
!= draw_end
) {
539 y_offset_end
= - y2diff
- 0.5;
540 x_offset_end
= y_offset_end
* dxdy
;
544 /* x/y positions in fixed point */
545 x
[0] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
) - fixed_width
/2;
546 x
[1] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
) - fixed_width
/2;
547 x
[2] = subpixel_snap(v2
[0][0] + x_offset_end
- setup
->pixel_offset
) + fixed_width
/2;
548 x
[3] = subpixel_snap(v1
[0][0] + x_offset
- setup
->pixel_offset
) + fixed_width
/2;
550 y
[0] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
);
551 y
[1] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
);
552 y
[2] = subpixel_snap(v2
[0][1] + y_offset_end
- setup
->pixel_offset
);
553 y
[3] = subpixel_snap(v1
[0][1] + y_offset
- setup
->pixel_offset
);
556 /* Bounding rectangle (in pixels) */
558 /* Yes this is necessary to accurately calculate bounding boxes
559 * with the two fill-conventions we support. GL (normally) ends
560 * up needing a bottom-left fill convention, which requires
561 * slightly different rounding.
563 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
565 bbox
.x0
= (MIN4(x
[0], x
[1], x
[2], x
[3]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
566 bbox
.x1
= (MAX4(x
[0], x
[1], x
[2], x
[3]) + (FIXED_ONE
-1)) >> FIXED_ORDER
;
567 bbox
.y0
= (MIN4(y
[0], y
[1], y
[2], y
[3]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
568 bbox
.y1
= (MAX4(y
[0], y
[1], y
[2], y
[3]) + (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
570 /* Inclusive coordinates:
576 if (bbox
.x1
< bbox
.x0
||
578 if (0) debug_printf("empty bounding box\n");
579 LP_COUNT(nr_culled_tris
);
583 if (!u_rect_test_intersection(&setup
->draw_regions
[viewport_index
], &bbox
)) {
584 if (0) debug_printf("offscreen\n");
585 LP_COUNT(nr_culled_tris
);
589 /* Can safely discard negative regions:
591 bbox
.x0
= MAX2(bbox
.x0
, 0);
592 bbox
.y0
= MAX2(bbox
.y0
, 0);
594 line
= lp_setup_alloc_triangle(scene
,
602 line
->v
[0][0] = v1
[0][0];
603 line
->v
[1][0] = v2
[0][0];
604 line
->v
[0][1] = v1
[0][1];
605 line
->v
[1][1] = v2
[0][1];
610 if (lp_context
->active_statistics_queries
&&
611 !llvmpipe_rasterization_disabled(lp_context
)) {
612 lp_context
->pipeline_statistics
.c_primitives
++;
615 /* calculate the deltas */
616 plane
= GET_PLANES(line
);
617 plane
[0].dcdy
= x
[0] - x
[1];
618 plane
[1].dcdy
= x
[1] - x
[2];
619 plane
[2].dcdy
= x
[2] - x
[3];
620 plane
[3].dcdy
= x
[3] - x
[0];
622 plane
[0].dcdx
= y
[0] - y
[1];
623 plane
[1].dcdx
= y
[1] - y
[2];
624 plane
[2].dcdx
= y
[2] - y
[3];
625 plane
[3].dcdx
= y
[3] - y
[0];
627 if (draw_will_inject_frontface(lp_context
->draw
) &&
628 setup
->face_slot
> 0) {
629 line
->inputs
.frontfacing
= v1
[setup
->face_slot
][0];
631 line
->inputs
.frontfacing
= TRUE
;
634 /* Setup parameter interpolants:
636 info
.a0
= GET_A0(&line
->inputs
);
637 info
.dadx
= GET_DADX(&line
->inputs
);
638 info
.dady
= GET_DADY(&line
->inputs
);
639 info
.frontfacing
= line
->inputs
.frontfacing
;
640 setup_line_coefficients(setup
, &info
);
642 line
->inputs
.disable
= FALSE
;
643 line
->inputs
.opaque
= FALSE
;
644 line
->inputs
.layer
= layer
;
645 line
->inputs
.viewport_index
= viewport_index
;
648 * XXX: this code is mostly identical to the one in lp_setup_tri, except it
649 * uses 4 planes instead of 3. Could share the code (including the sse
650 * assembly, in fact we'd get the 4th plane for free).
651 * The only difference apart from storing the 4th plane would be some
652 * different shuffle for calculating dcdx/dcdy.
654 for (i
= 0; i
< 4; i
++) {
656 /* half-edge constants, will be iterated over the whole render
659 plane
[i
].c
= IMUL64(plane
[i
].dcdx
, x
[i
]) - IMUL64(plane
[i
].dcdy
, y
[i
]);
661 /* correct for top-left vs. bottom-left fill convention.
663 if (plane
[i
].dcdx
< 0) {
664 /* both fill conventions want this - adjust for left edges */
667 else if (plane
[i
].dcdx
== 0) {
668 if (setup
->pixel_offset
== 0) {
669 /* correct for top-left fill convention:
671 if (plane
[i
].dcdy
> 0) plane
[i
].c
++;
674 /* correct for bottom-left fill convention:
676 if (plane
[i
].dcdy
< 0) plane
[i
].c
++;
680 plane
[i
].dcdx
*= FIXED_ONE
;
681 plane
[i
].dcdy
*= FIXED_ONE
;
683 /* find trivial reject offsets for each edge for a single-pixel
684 * sized block. These will be scaled up at each recursive level to
685 * match the active blocksize. Scaling in this way works best if
686 * the blocks are square.
689 if (plane
[i
].dcdx
< 0) plane
[i
].eo
-= plane
[i
].dcdx
;
690 if (plane
[i
].dcdy
> 0) plane
[i
].eo
+= plane
[i
].dcdy
;
695 * When rasterizing scissored tris, use the intersection of the
696 * triangle bounding box and the scissor rect to generate the
699 * This permits us to cut off the triangle "tails" that are present
700 * in the intermediate recursive levels caused when two of the
701 * triangles edges don't diverge quickly enough to trivially reject
702 * exterior blocks from the triangle.
704 * It's not really clear if it's worth worrying about these tails,
705 * but since we generate the planes for each scissored tri, it's
706 * free to trim them in this case.
708 * Note that otherwise, the scissor planes only vary in 'C' value,
709 * and even then only on state-changes. Could alternatively store
710 * these planes elsewhere.
712 if (nr_planes
== 8) {
713 const struct u_rect
*scissor
=
714 &setup
->scissors
[viewport_index
];
718 plane
[4].c
= 1-scissor
->x0
;
723 plane
[5].c
= scissor
->x1
+1;
728 plane
[6].c
= 1-scissor
->y0
;
733 plane
[7].c
= scissor
->y1
+1;
737 return lp_setup_bin_triangle(setup
, line
, &bbox
, nr_planes
, viewport_index
);
741 static void lp_setup_line( struct lp_setup_context
*setup
,
742 const float (*v0
)[4],
743 const float (*v1
)[4] )
745 if (!try_setup_line( setup
, v0
, v1
))
747 if (!lp_setup_flush_and_restart(setup
))
750 if (!try_setup_line( setup
, v0
, v1
))
756 void lp_setup_choose_line( struct lp_setup_context
*setup
)
758 setup
->line
= lp_setup_line
;