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 * \brief Primitive rasterization/rendering (points, lines, triangles)
31 * \author Keith Whitwell <keith@tungstengraphics.com>
36 #include "sp_context.h"
37 #include "sp_headers.h"
39 #include "sp_prim_setup.h"
40 #include "pipe/draw/draw_private.h"
41 #include "pipe/draw/draw_vertex.h"
42 #include "pipe/p_util.h"
50 float dx
; /**< X(v1) - X(v0), used only during setup */
51 float dy
; /**< Y(v1) - Y(v0), used only during setup */
52 float dxdy
; /**< dx/dy */
53 float sx
, sy
; /**< first sample point coord */
54 int lines
; /**< number of lines on this edge */
59 * Triangle setup info (derived from draw_stage).
60 * Also used for line drawing (taking some liberties).
63 struct draw_stage stage
; /**< This must be first (base class) */
65 struct softpipe_context
*softpipe
;
67 /* Vertices are just an array of floats making up each attribute in
68 * turn. Currently fixed at 4 floats, but should change in time.
69 * Codegen will help cope with this.
71 const struct vertex_header
*vmax
;
72 const struct vertex_header
*vmid
;
73 const struct vertex_header
*vmin
;
74 const struct vertex_header
*vprovoke
;
82 const unsigned *lookup
; /**< vertex attribute positions */
84 struct tgsi_interp_coef coef
[TGSI_ATTRIB_MAX
];
85 struct quad_header quad
;
88 int left
[2]; /**< [0] = row0, [1] = row1 */
92 unsigned mask
; /**< mask of MASK_BOTTOM/TOP_LEFT/RIGHT bits */
99 * Basically a cast wrapper.
101 static INLINE
struct setup_stage
*setup_stage( struct draw_stage
*stage
)
103 return (struct setup_stage
*)stage
;
108 * Clip setup->quad against the scissor/surface bounds.
111 quad_clip(struct setup_stage
*setup
)
113 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
114 const int minx
= (int) cliprect
->minx
;
115 const int maxx
= (int) cliprect
->maxx
;
116 const int miny
= (int) cliprect
->miny
;
117 const int maxy
= (int) cliprect
->maxy
;
119 if (setup
->quad
.x0
>= maxx
||
120 setup
->quad
.y0
>= maxy
||
121 setup
->quad
.x0
+ 1 < minx
||
122 setup
->quad
.y0
+ 1 < miny
) {
123 /* totally clipped */
124 setup
->quad
.mask
= 0x0;
127 if (setup
->quad
.x0
< minx
)
128 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
129 if (setup
->quad
.y0
< miny
)
130 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
131 if (setup
->quad
.x0
== maxx
- 1)
132 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
133 if (setup
->quad
.y0
== maxy
- 1)
134 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
139 * Emit a quad (pass to next stage) with clipping.
142 clip_emit_quad(struct setup_stage
*setup
)
145 if (setup
->quad
.mask
) {
146 struct softpipe_context
*sp
= setup
->softpipe
;
147 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
153 * Emit a quad (pass to next stage). No clipping is done.
156 emit_quad( struct setup_stage
*setup
, int x
, int y
, unsigned mask
)
158 struct softpipe_context
*sp
= setup
->softpipe
;
161 setup
->quad
.mask
= mask
;
162 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
167 * Given an X or Y coordinate, return the block/quad coordinate that it
170 static INLINE
int block( int x
)
177 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
178 * the triangle's bounds.
180 * this is pretty nasty... may need to rework flush_spans again to
181 * fix it, if possible.
183 static unsigned calculate_mask( struct setup_stage
*setup
,
188 if (x
>= setup
->span
.left
[0] && x
< setup
->span
.right
[0])
189 mask
|= MASK_BOTTOM_LEFT
;
191 if (x
>= setup
->span
.left
[1] && x
< setup
->span
.right
[1])
192 mask
|= MASK_TOP_LEFT
;
194 if (x
+1 >= setup
->span
.left
[0] && x
+1 < setup
->span
.right
[0])
195 mask
|= MASK_BOTTOM_RIGHT
;
197 if (x
+1 >= setup
->span
.left
[1] && x
+1 < setup
->span
.right
[1])
198 mask
|= MASK_TOP_RIGHT
;
205 * Render a horizontal span of quads
207 static void flush_spans( struct setup_stage
*setup
)
209 int minleft
, maxright
;
212 switch (setup
->span
.y_flags
) {
214 minleft
= MIN2(setup
->span
.left
[0], setup
->span
.left
[1]);
215 maxright
= MAX2(setup
->span
.right
[0], setup
->span
.right
[1]);
219 minleft
= setup
->span
.left
[0];
220 maxright
= setup
->span
.right
[0];
224 minleft
= setup
->span
.left
[1];
225 maxright
= setup
->span
.right
[1];
233 for (x
= block(minleft
); x
<= block(maxright
); )
235 emit_quad( setup
, x
, setup
->span
.y
,
236 calculate_mask( setup
, x
) );
241 setup
->span
.y_flags
= 0;
242 setup
->span
.right
[0] = 0;
243 setup
->span
.right
[1] = 0;
247 static void print_vertex(const struct setup_stage
*setup
,
248 const struct vertex_header
*v
)
252 for (i
= 0; i
< setup
->softpipe
->nr_attrs
; i
++) {
253 printf(" %d: %f %f %f\n", i
,
254 v
->data
[i
][0], v
->data
[i
][1], v
->data
[i
][2]);
259 static boolean
setup_sort_vertices( struct setup_stage
*setup
,
260 const struct prim_header
*prim
)
262 const struct vertex_header
*v0
= prim
->v
[0];
263 const struct vertex_header
*v1
= prim
->v
[1];
264 const struct vertex_header
*v2
= prim
->v
[2];
267 printf("Triangle:\n");
268 print_vertex(setup
, v0
);
269 print_vertex(setup
, v1
);
270 print_vertex(setup
, v2
);
273 setup
->vprovoke
= v2
;
275 /* determine bottom to top order of vertices */
277 float y0
= v0
->data
[0][1];
278 float y1
= v1
->data
[0][1];
279 float y2
= v2
->data
[0][1];
322 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
323 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
324 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
325 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
326 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
327 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
330 * Compute triangle's area. Use 1/area to compute partial
331 * derivatives of attributes later.
333 * The area will be the same as prim->det, but the sign may be
334 * different depending on how the vertices get sorted above.
336 * To determine whether the primitive is front or back facing we
337 * use the prim->det value because its sign is correct.
340 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
341 setup
->ebot
.dx
* setup
->emaj
.dy
);
343 setup
->oneoverarea
= 1.0f
/ area
;
345 _mesa_printf("%s one-over-area %f area %f det %f\n",
346 __FUNCTION__, setup->oneoverarea, area, prim->det );
350 /* We need to know if this is a front or back-facing triangle for:
351 * - the GLSL gl_FrontFacing fragment attribute (bool)
352 * - two-sided stencil test
354 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
);
361 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
362 * The value value comes from vertex->data[slot][i].
363 * The result will be put into setup->coef[slot].a0[i].
364 * \param slot which attribute slot
365 * \param i which component of the slot (0..3)
367 static void const_coeff( struct setup_stage
*setup
,
371 assert(slot
< TGSI_ATTRIB_MAX
);
374 setup
->coef
[slot
].dadx
[i
] = 0;
375 setup
->coef
[slot
].dady
[i
] = 0;
377 /* need provoking vertex info!
379 setup
->coef
[slot
].a0
[i
] = setup
->vprovoke
->data
[slot
][i
];
384 * Compute a0, dadx and dady for a linearly interpolated coefficient,
387 static void tri_linear_coeff( struct setup_stage
*setup
,
391 float botda
= setup
->vmid
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
392 float majda
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
393 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
394 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
396 assert(slot
< TGSI_ATTRIB_MAX
);
399 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
400 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
402 /* calculate a0 as the value which would be sampled for the
403 * fragment at (0,0), taking into account that we want to sample at
404 * pixel centers, in other words (0.5, 0.5).
406 * this is neat but unfortunately not a good way to do things for
407 * triangles with very large values of dadx or dady as it will
408 * result in the subtraction and re-addition from a0 of a very
409 * large number, which means we'll end up loosing a lot of the
410 * fractional bits and precision from a0. the way to fix this is
411 * to define a0 as the sample at a pixel center somewhere near vmin
412 * instead - i'll switch to this later.
414 setup
->coef
[slot
].a0
[i
] = (setup
->vmin
->data
[slot
][i
] -
415 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5f
) +
416 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5f
)));
419 _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
421 setup->coef[slot].a0[i],
422 setup->coef[slot].dadx[i],
423 setup->coef[slot].dady[i]);
429 * Compute a0, dadx and dady for a perspective-corrected interpolant,
432 static void tri_persp_coeff( struct setup_stage
*setup
,
436 /* premultiply by 1/w:
438 float mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
439 float mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
440 float maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
442 float botda
= mida
- mina
;
443 float majda
= maxa
- mina
;
444 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
445 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
447 assert(slot
< TGSI_ATTRIB_MAX
);
450 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
451 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
452 setup
->coef
[slot
].a0
[i
] = (mina
-
453 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5f
) +
454 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5f
)));
459 * Compute the setup->coef[] array dadx, dady, a0 values.
460 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
462 static void setup_tri_coefficients( struct setup_stage
*setup
)
464 const interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
467 /* z and w are done by linear interpolation:
469 tri_linear_coeff(setup
, 0, 2);
470 tri_linear_coeff(setup
, 0, 3);
472 /* setup interpolation for all the remaining attributes:
474 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
475 switch (interp
[slot
]) {
476 case INTERP_CONSTANT
:
477 for (j
= 0; j
< NUM_CHANNELS
; j
++)
478 const_coeff(setup
, slot
, j
);
482 for (j
= 0; j
< NUM_CHANNELS
; j
++)
483 tri_linear_coeff(setup
, slot
, j
);
486 case INTERP_PERSPECTIVE
:
487 for (j
= 0; j
< NUM_CHANNELS
; j
++)
488 tri_persp_coeff(setup
, slot
, j
);
492 /* invalid interp mode */
500 static void setup_tri_edges( struct setup_stage
*setup
)
502 float vmin_x
= setup
->vmin
->data
[0][0] + 0.5f
;
503 float vmid_x
= setup
->vmid
->data
[0][0] + 0.5f
;
505 float vmin_y
= setup
->vmin
->data
[0][1] - 0.5f
;
506 float vmid_y
= setup
->vmid
->data
[0][1] - 0.5f
;
507 float vmax_y
= setup
->vmax
->data
[0][1] - 0.5f
;
509 setup
->emaj
.sy
= ceilf(vmin_y
);
510 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
511 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
512 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
514 setup
->etop
.sy
= ceilf(vmid_y
);
515 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
516 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
517 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
519 setup
->ebot
.sy
= ceilf(vmin_y
);
520 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
521 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
522 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
527 * Render the upper or lower half of a triangle.
528 * Scissoring/cliprect is applied here too.
530 static void subtriangle( struct setup_stage
*setup
,
535 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
536 const int minx
= (int) cliprect
->minx
;
537 const int maxx
= (int) cliprect
->maxx
;
538 const int miny
= (int) cliprect
->miny
;
539 const int maxy
= (int) cliprect
->maxy
;
540 int y
, start_y
, finish_y
;
541 int sy
= (int)eleft
->sy
;
543 assert((int)eleft
->sy
== (int) eright
->sy
);
545 /* clip top/bottom */
547 finish_y
= sy
+ lines
;
559 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
562 for (y
= start_y
; y
< finish_y
; y
++) {
564 /* avoid accumulating adds as floats don't have the precision to
565 * accurately iterate large triangle edges that way. luckily we
566 * can just multiply these days.
568 * this is all drowned out by the attribute interpolation anyway.
570 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
571 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
573 /* clip left/right */
581 if (block(_y
) != setup
->span
.y
) {
583 setup
->span
.y
= block(_y
);
586 setup
->span
.left
[_y
&1] = left
;setup
->span
.right
[_y
&1] = right
;
587 setup
->span
.y_flags
|= 1<<(_y
&1);
592 /* save the values so that emaj can be restarted:
594 eleft
->sx
+= lines
* eleft
->dxdy
;
595 eright
->sx
+= lines
* eright
->dxdy
;
602 * Do setup for triangle rasterization, then render the triangle.
604 static void setup_tri( struct draw_stage
*stage
,
605 struct prim_header
*prim
)
607 struct setup_stage
*setup
= setup_stage( stage
);
610 _mesa_printf("%s\n", __FUNCTION__ );
613 setup_sort_vertices( setup
, prim
);
614 setup_tri_coefficients( setup
);
615 setup_tri_edges( setup
);
617 setup
->quad
.prim
= PRIM_TRI
;
620 setup
->span
.y_flags
= 0;
621 setup
->span
.right
[0] = 0;
622 setup
->span
.right
[1] = 0;
623 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
625 /* init_constant_attribs( setup ); */
627 if (setup
->oneoverarea
< 0.0) {
630 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
631 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
636 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
637 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
640 flush_spans( setup
);
646 * Compute a0, dadx and dady for a linearly interpolated coefficient,
650 line_linear_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
652 const float dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
653 const float dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
654 const float dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
655 setup
->coef
[slot
].dadx
[i
] = dadx
;
656 setup
->coef
[slot
].dady
[i
] = dady
;
657 setup
->coef
[slot
].a0
[i
]
658 = (setup
->vmin
->data
[slot
][i
] -
659 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
660 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
665 * Compute a0, dadx and dady for a perspective-corrected interpolant,
669 line_persp_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
672 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
677 * Compute the setup->coef[] array dadx, dady, a0 values.
678 * Must be called after setup->vmin,vmax are initialized.
681 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
683 const interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
686 /* use setup->vmin, vmax to point to vertices */
687 setup
->vprovoke
= prim
->v
[1];
688 setup
->vmin
= prim
->v
[0];
689 setup
->vmax
= prim
->v
[1];
691 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
692 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
693 /* NOTE: this is not really 1/area */
694 setup
->oneoverarea
= 1.0f
/ (setup
->emaj
.dx
* setup
->emaj
.dx
+
695 setup
->emaj
.dy
* setup
->emaj
.dy
);
697 /* z and w are done by linear interpolation:
699 line_linear_coeff(setup
, 0, 2);
700 line_linear_coeff(setup
, 0, 3);
702 /* setup interpolation for all the remaining attributes:
704 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
705 switch (interp
[slot
]) {
706 case INTERP_CONSTANT
:
707 for (j
= 0; j
< NUM_CHANNELS
; j
++)
708 const_coeff(setup
, slot
, j
);
712 for (j
= 0; j
< NUM_CHANNELS
; j
++)
713 line_linear_coeff(setup
, slot
, j
);
716 case INTERP_PERSPECTIVE
:
717 for (j
= 0; j
< NUM_CHANNELS
; j
++)
718 line_persp_coeff(setup
, slot
, j
);
722 /* invalid interp mode */
730 * Plot a pixel in a line segment.
733 plot(struct setup_stage
*setup
, int x
, int y
)
735 const int iy
= y
& 1;
736 const int ix
= x
& 1;
737 const int quadX
= x
- ix
;
738 const int quadY
= y
- iy
;
739 const int mask
= (1 << ix
) << (2 * iy
);
741 if (quadX
!= setup
->quad
.x0
||
742 quadY
!= setup
->quad
.y0
)
744 /* flush prev quad, start new quad */
746 if (setup
->quad
.x0
!= -1)
747 clip_emit_quad(setup
);
749 setup
->quad
.x0
= quadX
;
750 setup
->quad
.y0
= quadY
;
751 setup
->quad
.mask
= 0x0;
754 setup
->quad
.mask
|= mask
;
759 * Determine whether or not to emit a line fragment by checking
760 * line stipple pattern.
762 static INLINE
unsigned
763 stipple_test(int counter
, ushort pattern
, int factor
)
765 int b
= (counter
/ factor
) & 0xf;
766 return (1 << b
) & pattern
;
771 * Do setup for line rasterization, then render the line.
772 * XXX single-pixel width, no stipple, etc
775 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
777 const struct vertex_header
*v0
= prim
->v
[0];
778 const struct vertex_header
*v1
= prim
->v
[1];
779 struct setup_stage
*setup
= setup_stage( stage
);
780 struct softpipe_context
*sp
= setup
->softpipe
;
782 int x0
= (int) v0
->data
[0][0];
783 int x1
= (int) v1
->data
[0][0];
784 int y0
= (int) v0
->data
[0][1];
785 int y1
= (int) v1
->data
[0][1];
790 if (dx
== 0 && dy
== 0)
793 setup_line_coefficients(setup
, prim
);
796 dx
= -dx
; /* make positive */
804 dy
= -dy
; /* make positive */
814 setup
->quad
.x0
= setup
->quad
.y0
= -1;
815 setup
->quad
.mask
= 0x0;
816 setup
->quad
.prim
= PRIM_LINE
;
817 /* XXX temporary: set coverage to 1.0 so the line appears
818 * if AA mode happens to be enabled.
820 setup
->quad
.coverage
[0] =
821 setup
->quad
.coverage
[1] =
822 setup
->quad
.coverage
[2] =
823 setup
->quad
.coverage
[3] = 1.0;
826 /*** X-major line ***/
828 const int errorInc
= dy
+ dy
;
829 int error
= errorInc
- dx
;
830 const int errorDec
= error
- dx
;
832 for (i
= 0; i
< dx
; i
++) {
833 if (!sp
->rasterizer
->line_stipple_enable
||
834 stipple_test(sp
->line_stipple_counter
,
835 sp
->rasterizer
->line_stipple_pattern
,
836 sp
->rasterizer
->line_stipple_factor
+ 1)) {
849 sp
->line_stipple_counter
++;
853 /*** Y-major line ***/
855 const int errorInc
= dx
+ dx
;
856 int error
= errorInc
- dy
;
857 const int errorDec
= error
- dy
;
859 for (i
= 0; i
< dy
; i
++) {
860 if (!sp
->rasterizer
->line_stipple_enable
||
861 stipple_test(sp
->line_stipple_counter
,
862 sp
->rasterizer
->line_stipple_pattern
,
863 sp
->rasterizer
->line_stipple_factor
+ 1)) {
877 sp
->line_stipple_counter
++;
881 /* draw final quad */
882 if (setup
->quad
.mask
) {
883 clip_emit_quad(setup
);
889 * Do setup for point rasterization, then render the point.
890 * Round or square points...
891 * XXX could optimize a lot for 1-pixel points.
894 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
896 struct setup_stage
*setup
= setup_stage( stage
);
897 const struct vertex_header
*v0
= prim
->v
[0];
899 const int sizeAttr
= setup
->lookup
[TGSI_ATTRIB_POINTSIZE
];
901 = sizeAttr
? (0.5f
* v0
->data
[sizeAttr
][0])
902 : (0.5f
* setup
->softpipe
->rasterizer
->point_size
);
903 const boolean round
= setup
->softpipe
->rasterizer
->point_smooth
;
904 const float x
= v0
->data
[TGSI_ATTRIB_POS
][0];
905 const float y
= v0
->data
[TGSI_ATTRIB_POS
][1];
908 /* For points, all interpolants are constant-valued.
909 * However, for point sprites, we'll need to setup texcoords appropriately.
910 * XXX: which coefficients are the texcoords???
911 * We may do point sprites as textured quads...
913 * KW: We don't know which coefficients are texcoords - ultimately
914 * the choice of what interpolation mode to use for each attribute
915 * should be determined by the fragment program, using
916 * per-attribute declaration statements that include interpolation
917 * mode as a parameter. So either the fragment program will have
918 * to be adjusted for pointsprite vs normal point behaviour, or
919 * otherwise a special interpolation mode will have to be defined
920 * which matches the required behaviour for point sprites. But -
921 * the latter is not a feature of normal hardware, and as such
922 * probably should be ruled out on that basis.
924 setup
->vprovoke
= prim
->v
[0];
925 const_coeff(setup
, 0, 2);
926 const_coeff(setup
, 0, 3);
927 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
928 for (j
= 0; j
< NUM_CHANNELS
; j
++)
929 const_coeff(setup
, slot
, j
);
932 setup
->quad
.prim
= PRIM_POINT
;
934 if (halfSize
<= 0.5 && !round
) {
935 /* special case for 1-pixel points */
936 const int ix
= ((int) x
) & 1;
937 const int iy
= ((int) y
) & 1;
938 setup
->quad
.x0
= (int) x
- ix
;
939 setup
->quad
.y0
= (int) y
- iy
;
940 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
941 clip_emit_quad(setup
);
944 const int ixmin
= block((int) (x
- halfSize
));
945 const int ixmax
= block((int) (x
+ halfSize
));
946 const int iymin
= block((int) (y
- halfSize
));
947 const int iymax
= block((int) (y
+ halfSize
));
952 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
953 const float rmax
= halfSize
+ 0.7071F
;
954 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
955 const float rmax2
= rmax
* rmax
;
956 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
958 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
959 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
960 float dx
, dy
, dist2
, cover
;
962 setup
->quad
.mask
= 0x0;
964 dx
= (ix
+ 0.5f
) - x
;
965 dy
= (iy
+ 0.5f
) - y
;
966 dist2
= dx
* dx
+ dy
* dy
;
967 if (dist2
<= rmax2
) {
968 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
969 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
970 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
973 dx
= (ix
+ 1.5f
) - x
;
974 dy
= (iy
+ 0.5f
) - y
;
975 dist2
= dx
* dx
+ dy
* dy
;
976 if (dist2
<= rmax2
) {
977 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
978 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
979 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
982 dx
= (ix
+ 0.5f
) - x
;
983 dy
= (iy
+ 1.5f
) - y
;
984 dist2
= dx
* dx
+ dy
* dy
;
985 if (dist2
<= rmax2
) {
986 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
987 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
988 setup
->quad
.mask
|= MASK_TOP_LEFT
;
991 dx
= (ix
+ 1.5f
) - x
;
992 dy
= (iy
+ 1.5f
) - y
;
993 dist2
= dx
* dx
+ dy
* dy
;
994 if (dist2
<= rmax2
) {
995 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
996 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
997 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
1000 if (setup
->quad
.mask
) {
1001 setup
->quad
.x0
= ix
;
1002 setup
->quad
.y0
= iy
;
1003 clip_emit_quad(setup
);
1010 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1011 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1012 setup
->quad
.mask
= 0xf;
1014 if (ix
+ 0.5 < x
- halfSize
) {
1015 /* fragment is past left edge of point, turn off left bits */
1016 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1019 if (ix
+ 1.5 > x
+ halfSize
) {
1020 /* past the right edge */
1021 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1024 if (iy
+ 0.5 < y
- halfSize
) {
1025 /* below the bottom edge */
1026 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1029 if (iy
+ 1.5 > y
+ halfSize
) {
1030 /* above the top edge */
1031 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1034 if (setup
->quad
.mask
) {
1035 setup
->quad
.x0
= ix
;
1036 setup
->quad
.y0
= iy
;
1037 clip_emit_quad(setup
);
1047 static void setup_begin( struct draw_stage
*stage
)
1049 struct setup_stage
*setup
= setup_stage(stage
);
1050 struct softpipe_context
*sp
= setup
->softpipe
;
1052 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1054 sp
->quad
.first
->begin(sp
->quad
.first
);
1058 static void setup_end( struct draw_stage
*stage
)
1063 static void reset_stipple_counter( struct draw_stage
*stage
)
1065 struct setup_stage
*setup
= setup_stage(stage
);
1066 setup
->softpipe
->line_stipple_counter
= 0;
1071 * Create a new primitive setup/render stage.
1073 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1075 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1077 setup
->softpipe
= softpipe
;
1078 setup
->stage
.draw
= softpipe
->draw
;
1079 setup
->stage
.begin
= setup_begin
;
1080 setup
->stage
.point
= setup_point
;
1081 setup
->stage
.line
= setup_line
;
1082 setup
->stage
.tri
= setup_tri
;
1083 setup
->stage
.end
= setup_end
;
1084 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1086 setup
->quad
.coef
= setup
->coef
;
1088 setup
->lookup
= softpipe
->draw
->vertex_info
.attrib_to_slot
;
1090 return &setup
->stage
;