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/p_util.h"
43 #include "pipe/draw/draw_vertex.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 struct tgsi_interp_coef coef
[TGSI_ATTRIB_MAX
];
83 struct quad_header quad
;
86 int left
[2]; /**< [0] = row0, [1] = row1 */
90 unsigned mask
; /**< mask of MASK_BOTTOM/TOP_LEFT/RIGHT bits */
97 * Basically a cast wrapper.
99 static INLINE
struct setup_stage
*setup_stage( struct draw_stage
*stage
)
101 return (struct setup_stage
*)stage
;
106 * Clip setup->quad against the scissor/surface bounds.
109 quad_clip(struct setup_stage
*setup
)
111 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
112 const int minx
= (int) cliprect
->minx
;
113 const int maxx
= (int) cliprect
->maxx
;
114 const int miny
= (int) cliprect
->miny
;
115 const int maxy
= (int) cliprect
->maxy
;
117 if (setup
->quad
.x0
>= maxx
||
118 setup
->quad
.y0
>= maxy
||
119 setup
->quad
.x0
+ 1 < minx
||
120 setup
->quad
.y0
+ 1 < miny
) {
121 /* totally clipped */
122 setup
->quad
.mask
= 0x0;
125 if (setup
->quad
.x0
< minx
)
126 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
127 if (setup
->quad
.y0
< miny
)
128 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
129 if (setup
->quad
.x0
== maxx
- 1)
130 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
131 if (setup
->quad
.y0
== maxy
- 1)
132 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
137 * Emit a quad (pass to next stage) with clipping.
140 clip_emit_quad(struct setup_stage
*setup
)
143 if (setup
->quad
.mask
) {
144 struct softpipe_context
*sp
= setup
->softpipe
;
145 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
151 * Emit a quad (pass to next stage). No clipping is done.
154 emit_quad( struct setup_stage
*setup
, int x
, int y
, unsigned mask
)
156 struct softpipe_context
*sp
= setup
->softpipe
;
159 setup
->quad
.mask
= mask
;
160 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
165 * Given an X or Y coordinate, return the block/quad coordinate that it
168 static INLINE
int block( int x
)
175 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
176 * the triangle's bounds.
178 * this is pretty nasty... may need to rework flush_spans again to
179 * fix it, if possible.
181 static unsigned calculate_mask( struct setup_stage
*setup
,
186 if (x
>= setup
->span
.left
[0] && x
< setup
->span
.right
[0])
187 mask
|= MASK_BOTTOM_LEFT
;
189 if (x
>= setup
->span
.left
[1] && x
< setup
->span
.right
[1])
190 mask
|= MASK_TOP_LEFT
;
192 if (x
+1 >= setup
->span
.left
[0] && x
+1 < setup
->span
.right
[0])
193 mask
|= MASK_BOTTOM_RIGHT
;
195 if (x
+1 >= setup
->span
.left
[1] && x
+1 < setup
->span
.right
[1])
196 mask
|= MASK_TOP_RIGHT
;
203 * Render a horizontal span of quads
205 static void flush_spans( struct setup_stage
*setup
)
207 int minleft
, maxright
;
210 switch (setup
->span
.y_flags
) {
212 minleft
= MIN2(setup
->span
.left
[0], setup
->span
.left
[1]);
213 maxright
= MAX2(setup
->span
.right
[0], setup
->span
.right
[1]);
217 minleft
= setup
->span
.left
[0];
218 maxright
= setup
->span
.right
[0];
222 minleft
= setup
->span
.left
[1];
223 maxright
= setup
->span
.right
[1];
231 for (x
= block(minleft
); x
<= block(maxright
); )
233 emit_quad( setup
, x
, setup
->span
.y
,
234 calculate_mask( setup
, x
) );
239 setup
->span
.y_flags
= 0;
240 setup
->span
.right
[0] = 0;
241 setup
->span
.right
[1] = 0;
245 static void print_vertex(const struct setup_stage
*setup
,
246 const struct vertex_header
*v
)
250 for (i
= 0; i
< setup
->softpipe
->nr_attrs
; i
++) {
251 printf(" %d: %f %f %f\n", i
,
252 v
->data
[i
][0], v
->data
[i
][1], v
->data
[i
][2]);
257 static boolean
setup_sort_vertices( struct setup_stage
*setup
,
258 const struct prim_header
*prim
)
260 const struct vertex_header
*v0
= prim
->v
[0];
261 const struct vertex_header
*v1
= prim
->v
[1];
262 const struct vertex_header
*v2
= prim
->v
[2];
265 printf("Triangle:\n");
266 print_vertex(setup
, v0
);
267 print_vertex(setup
, v1
);
268 print_vertex(setup
, v2
);
271 setup
->vprovoke
= v2
;
273 /* determine bottom to top order of vertices */
275 float y0
= v0
->data
[0][1];
276 float y1
= v1
->data
[0][1];
277 float y2
= v2
->data
[0][1];
320 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
321 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
322 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
323 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
324 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
325 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
328 * Compute triangle's area. Use 1/area to compute partial
329 * derivatives of attributes later.
331 * The area will be the same as prim->det, but the sign may be
332 * different depending on how the vertices get sorted above.
334 * To determine whether the primitive is front or back facing we
335 * use the prim->det value because its sign is correct.
338 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
339 setup
->ebot
.dx
* setup
->emaj
.dy
);
341 setup
->oneoverarea
= 1.0f
/ area
;
343 _mesa_printf("%s one-over-area %f area %f det %f\n",
344 __FUNCTION__, setup->oneoverarea, area, prim->det );
348 /* We need to know if this is a front or back-facing triangle for:
349 * - the GLSL gl_FrontFacing fragment attribute (bool)
350 * - two-sided stencil test
352 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->setup
.front_winding
== PIPE_WINDING_CW
);
359 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
360 * The value value comes from vertex->data[slot][i].
361 * The result will be put into setup->coef[slot].a0[i].
362 * \param slot which attribute slot
363 * \param i which component of the slot (0..3)
365 static void const_coeff( struct setup_stage
*setup
,
369 assert(slot
< TGSI_ATTRIB_MAX
);
372 setup
->coef
[slot
].dadx
[i
] = 0;
373 setup
->coef
[slot
].dady
[i
] = 0;
375 /* need provoking vertex info!
377 setup
->coef
[slot
].a0
[i
] = setup
->vprovoke
->data
[slot
][i
];
382 * Compute a0, dadx and dady for a linearly interpolated coefficient,
385 static void tri_linear_coeff( struct setup_stage
*setup
,
389 float botda
= setup
->vmid
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
390 float majda
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
391 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
392 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
394 assert(slot
< TGSI_ATTRIB_MAX
);
397 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
398 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
400 /* calculate a0 as the value which would be sampled for the
401 * fragment at (0,0), taking into account that we want to sample at
402 * pixel centers, in other words (0.5, 0.5).
404 * this is neat but unfortunately not a good way to do things for
405 * triangles with very large values of dadx or dady as it will
406 * result in the subtraction and re-addition from a0 of a very
407 * large number, which means we'll end up loosing a lot of the
408 * fractional bits and precision from a0. the way to fix this is
409 * to define a0 as the sample at a pixel center somewhere near vmin
410 * instead - i'll switch to this later.
412 setup
->coef
[slot
].a0
[i
] = (setup
->vmin
->data
[slot
][i
] -
413 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5f
) +
414 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5f
)));
417 _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
419 setup->coef[slot].a0[i],
420 setup->coef[slot].dadx[i],
421 setup->coef[slot].dady[i]);
427 * Compute a0, dadx and dady for a perspective-corrected interpolant,
430 static void tri_persp_coeff( struct setup_stage
*setup
,
434 /* premultiply by 1/w:
436 float mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
437 float mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
438 float maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
440 float botda
= mida
- mina
;
441 float majda
= maxa
- mina
;
442 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
443 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
445 assert(slot
< TGSI_ATTRIB_MAX
);
448 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
449 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
450 setup
->coef
[slot
].a0
[i
] = (mina
-
451 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5f
) +
452 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5f
)));
457 * Compute the setup->coef[] array dadx, dady, a0 values.
458 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
460 static void setup_tri_coefficients( struct setup_stage
*setup
)
462 const enum interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
465 /* z and w are done by linear interpolation:
467 tri_linear_coeff(setup
, 0, 2);
468 tri_linear_coeff(setup
, 0, 3);
470 /* setup interpolation for all the remaining attributes:
472 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
473 switch (interp
[slot
]) {
474 case INTERP_CONSTANT
:
475 for (j
= 0; j
< NUM_CHANNELS
; j
++)
476 const_coeff(setup
, slot
, j
);
480 for (j
= 0; j
< NUM_CHANNELS
; j
++)
481 tri_linear_coeff(setup
, slot
, j
);
484 case INTERP_PERSPECTIVE
:
485 for (j
= 0; j
< NUM_CHANNELS
; j
++)
486 tri_persp_coeff(setup
, slot
, j
);
494 static void setup_tri_edges( struct setup_stage
*setup
)
496 float vmin_x
= setup
->vmin
->data
[0][0] + 0.5f
;
497 float vmid_x
= setup
->vmid
->data
[0][0] + 0.5f
;
499 float vmin_y
= setup
->vmin
->data
[0][1] - 0.5f
;
500 float vmid_y
= setup
->vmid
->data
[0][1] - 0.5f
;
501 float vmax_y
= setup
->vmax
->data
[0][1] - 0.5f
;
503 setup
->emaj
.sy
= ceilf(vmin_y
);
504 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
505 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
506 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
508 setup
->etop
.sy
= ceilf(vmid_y
);
509 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
510 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
511 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
513 setup
->ebot
.sy
= ceilf(vmin_y
);
514 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
515 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
516 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
521 * Render the upper or lower half of a triangle.
522 * Scissoring/cliprect is applied here too.
524 static void subtriangle( struct setup_stage
*setup
,
529 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
530 const int minx
= (int) cliprect
->minx
;
531 const int maxx
= (int) cliprect
->maxx
;
532 const int miny
= (int) cliprect
->miny
;
533 const int maxy
= (int) cliprect
->maxy
;
534 int y
, start_y
, finish_y
;
535 int sy
= (int)eleft
->sy
;
537 assert((int)eleft
->sy
== (int) eright
->sy
);
539 /* clip top/bottom */
541 finish_y
= sy
+ lines
;
553 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
556 for (y
= start_y
; y
< finish_y
; y
++) {
558 /* avoid accumulating adds as floats don't have the precision to
559 * accurately iterate large triangle edges that way. luckily we
560 * can just multiply these days.
562 * this is all drowned out by the attribute interpolation anyway.
564 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
565 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
567 /* clip left/right */
575 if (block(_y
) != setup
->span
.y
) {
577 setup
->span
.y
= block(_y
);
580 setup
->span
.left
[_y
&1] = left
;setup
->span
.right
[_y
&1] = right
;
581 setup
->span
.y_flags
|= 1<<(_y
&1);
586 /* save the values so that emaj can be restarted:
588 eleft
->sx
+= lines
* eleft
->dxdy
;
589 eright
->sx
+= lines
* eright
->dxdy
;
596 * Do setup for triangle rasterization, then render the triangle.
598 static void setup_tri( struct draw_stage
*stage
,
599 struct prim_header
*prim
)
601 struct setup_stage
*setup
= setup_stage( stage
);
604 _mesa_printf("%s\n", __FUNCTION__ );
607 setup_sort_vertices( setup
, prim
);
608 setup_tri_coefficients( setup
);
609 setup_tri_edges( setup
);
611 setup
->quad
.prim
= PRIM_TRI
;
614 setup
->span
.y_flags
= 0;
615 setup
->span
.right
[0] = 0;
616 setup
->span
.right
[1] = 0;
617 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
619 /* init_constant_attribs( setup ); */
621 if (setup
->oneoverarea
< 0.0) {
624 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
625 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
630 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
631 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
634 flush_spans( setup
);
640 * Compute a0, dadx and dady for a linearly interpolated coefficient,
644 line_linear_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
646 const float dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
647 const float dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
648 const float dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
649 setup
->coef
[slot
].dadx
[i
] = dadx
;
650 setup
->coef
[slot
].dady
[i
] = dady
;
651 setup
->coef
[slot
].a0
[i
]
652 = (setup
->vmin
->data
[slot
][i
] -
653 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
654 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
659 * Compute a0, dadx and dady for a perspective-corrected interpolant,
663 line_persp_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
666 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
671 * Compute the setup->coef[] array dadx, dady, a0 values.
672 * Must be called after setup->vmin,vmax are initialized.
675 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
677 const enum interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
680 /* use setup->vmin, vmax to point to vertices */
681 setup
->vprovoke
= prim
->v
[1];
682 setup
->vmin
= prim
->v
[0];
683 setup
->vmax
= prim
->v
[1];
685 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
686 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
687 /* NOTE: this is not really 1/area */
688 setup
->oneoverarea
= 1.0f
/ (setup
->emaj
.dx
* setup
->emaj
.dx
+
689 setup
->emaj
.dy
* setup
->emaj
.dy
);
691 /* z and w are done by linear interpolation:
693 line_linear_coeff(setup
, 0, 2);
694 line_linear_coeff(setup
, 0, 3);
696 /* setup interpolation for all the remaining attributes:
698 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
699 switch (interp
[slot
]) {
700 case INTERP_CONSTANT
:
701 for (j
= 0; j
< NUM_CHANNELS
; j
++)
702 const_coeff(setup
, slot
, j
);
706 for (j
= 0; j
< NUM_CHANNELS
; j
++)
707 line_linear_coeff(setup
, slot
, j
);
710 case INTERP_PERSPECTIVE
:
711 for (j
= 0; j
< NUM_CHANNELS
; j
++)
712 line_persp_coeff(setup
, slot
, j
);
720 * Plot a pixel in a line segment.
723 plot(struct setup_stage
*setup
, int x
, int y
)
725 const int iy
= y
& 1;
726 const int ix
= x
& 1;
727 const int quadX
= x
- ix
;
728 const int quadY
= y
- iy
;
729 const int mask
= (1 << ix
) << (2 * iy
);
731 if (quadX
!= setup
->quad
.x0
||
732 quadY
!= setup
->quad
.y0
)
734 /* flush prev quad, start new quad */
736 if (setup
->quad
.x0
!= -1)
737 clip_emit_quad(setup
);
739 setup
->quad
.x0
= quadX
;
740 setup
->quad
.y0
= quadY
;
741 setup
->quad
.mask
= 0x0;
744 setup
->quad
.mask
|= mask
;
749 * Determine whether or not to emit a line fragment by checking
750 * line stipple pattern.
752 static INLINE
unsigned
753 stipple_test(int counter
, ushort pattern
, int factor
)
755 int b
= (counter
/ factor
) & 0xf;
756 return (1 << b
) & pattern
;
761 * Do setup for line rasterization, then render the line.
762 * XXX single-pixel width, no stipple, etc
765 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
767 const struct vertex_header
*v0
= prim
->v
[0];
768 const struct vertex_header
*v1
= prim
->v
[1];
769 struct setup_stage
*setup
= setup_stage( stage
);
770 struct softpipe_context
*sp
= setup
->softpipe
;
772 int x0
= (int) v0
->data
[0][0];
773 int x1
= (int) v1
->data
[0][0];
774 int y0
= (int) v0
->data
[0][1];
775 int y1
= (int) v1
->data
[0][1];
780 if (dx
== 0 && dy
== 0)
783 setup_line_coefficients(setup
, prim
);
786 dx
= -dx
; /* make positive */
794 dy
= -dy
; /* make positive */
804 setup
->quad
.x0
= setup
->quad
.y0
= -1;
805 setup
->quad
.mask
= 0x0;
806 setup
->quad
.prim
= PRIM_LINE
;
807 /* XXX temporary: set coverage to 1.0 so the line appears
808 * if AA mode happens to be enabled.
810 setup
->quad
.coverage
[0] =
811 setup
->quad
.coverage
[1] =
812 setup
->quad
.coverage
[2] =
813 setup
->quad
.coverage
[3] = 1.0;
816 /*** X-major line ***/
818 const int errorInc
= dy
+ dy
;
819 int error
= errorInc
- dx
;
820 const int errorDec
= error
- dx
;
822 for (i
= 0; i
< dx
; i
++) {
823 if (!sp
->setup
.line_stipple_enable
||
824 stipple_test(sp
->line_stipple_counter
,
825 sp
->setup
.line_stipple_pattern
,
826 sp
->setup
.line_stipple_factor
+ 1)) {
839 sp
->line_stipple_counter
++;
843 /*** Y-major line ***/
845 const int errorInc
= dx
+ dx
;
846 int error
= errorInc
- dy
;
847 const int errorDec
= error
- dy
;
849 for (i
= 0; i
< dy
; i
++) {
850 if (!sp
->setup
.line_stipple_enable
||
851 stipple_test(sp
->line_stipple_counter
,
852 sp
->setup
.line_stipple_pattern
,
853 sp
->setup
.line_stipple_factor
+ 1)) {
867 sp
->line_stipple_counter
++;
871 /* draw final quad */
872 if (setup
->quad
.mask
) {
873 clip_emit_quad(setup
);
879 * Do setup for point rasterization, then render the point.
880 * Round or square points...
881 * XXX could optimize a lot for 1-pixel points.
884 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
886 struct setup_stage
*setup
= setup_stage( stage
);
887 /*XXX this should be a vertex attrib! */
888 const float halfSize
= 0.5f
* setup
->softpipe
->setup
.point_size
;
889 const boolean round
= setup
->softpipe
->setup
.point_smooth
;
890 const struct vertex_header
*v0
= prim
->v
[0];
891 const float x
= v0
->data
[TGSI_ATTRIB_POS
][0];
892 const float y
= v0
->data
[TGSI_ATTRIB_POS
][1];
895 /* For points, all interpolants are constant-valued.
896 * However, for point sprites, we'll need to setup texcoords appropriately.
897 * XXX: which coefficients are the texcoords???
898 * We may do point sprites as textured quads...
900 * KW: We don't know which coefficients are texcoords - ultimately
901 * the choice of what interpolation mode to use for each attribute
902 * should be determined by the fragment program, using
903 * per-attribute declaration statements that include interpolation
904 * mode as a parameter. So either the fragment program will have
905 * to be adjusted for pointsprite vs normal point behaviour, or
906 * otherwise a special interpolation mode will have to be defined
907 * which matches the required behaviour for point sprites. But -
908 * the latter is not a feature of normal hardware, and as such
909 * probably should be ruled out on that basis.
911 setup
->vprovoke
= prim
->v
[0];
912 const_coeff(setup
, 0, 2);
913 const_coeff(setup
, 0, 3);
914 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
915 for (j
= 0; j
< NUM_CHANNELS
; j
++)
916 const_coeff(setup
, slot
, j
);
919 setup
->quad
.prim
= PRIM_POINT
;
921 if (halfSize
<= 0.5 && !round
) {
922 /* special case for 1-pixel points */
923 const int ix
= ((int) x
) & 1;
924 const int iy
= ((int) y
) & 1;
925 setup
->quad
.x0
= (int) x
- ix
;
926 setup
->quad
.y0
= (int) y
- iy
;
927 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
928 clip_emit_quad(setup
);
931 const int ixmin
= block((int) (x
- halfSize
));
932 const int ixmax
= block((int) (x
+ halfSize
));
933 const int iymin
= block((int) (y
- halfSize
));
934 const int iymax
= block((int) (y
+ halfSize
));
939 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
940 const float rmax
= halfSize
+ 0.7071F
;
941 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
942 const float rmax2
= rmax
* rmax
;
943 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
945 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
946 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
947 float dx
, dy
, dist2
, cover
;
949 setup
->quad
.mask
= 0x0;
951 dx
= (ix
+ 0.5f
) - x
;
952 dy
= (iy
+ 0.5f
) - y
;
953 dist2
= dx
* dx
+ dy
* dy
;
954 if (dist2
<= rmax2
) {
955 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
956 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
957 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
960 dx
= (ix
+ 1.5f
) - x
;
961 dy
= (iy
+ 0.5f
) - y
;
962 dist2
= dx
* dx
+ dy
* dy
;
963 if (dist2
<= rmax2
) {
964 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
965 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
966 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
969 dx
= (ix
+ 0.5f
) - x
;
970 dy
= (iy
+ 1.5f
) - y
;
971 dist2
= dx
* dx
+ dy
* dy
;
972 if (dist2
<= rmax2
) {
973 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
974 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
975 setup
->quad
.mask
|= MASK_TOP_LEFT
;
978 dx
= (ix
+ 1.5f
) - x
;
979 dy
= (iy
+ 1.5f
) - y
;
980 dist2
= dx
* dx
+ dy
* dy
;
981 if (dist2
<= rmax2
) {
982 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
983 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
984 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
987 if (setup
->quad
.mask
) {
990 clip_emit_quad(setup
);
997 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
998 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
999 setup
->quad
.mask
= 0xf;
1001 if (ix
+ 0.5 < x
- halfSize
) {
1002 /* fragment is past left edge of point, turn off left bits */
1003 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1006 if (ix
+ 1.5 > x
+ halfSize
) {
1007 /* past the right edge */
1008 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1011 if (iy
+ 0.5 < y
- halfSize
) {
1012 /* below the bottom edge */
1013 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1016 if (iy
+ 1.5 > y
+ halfSize
) {
1017 /* above the top edge */
1018 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1021 if (setup
->quad
.mask
) {
1022 setup
->quad
.x0
= ix
;
1023 setup
->quad
.y0
= iy
;
1024 clip_emit_quad(setup
);
1034 static void setup_begin( struct draw_stage
*stage
)
1036 struct setup_stage
*setup
= setup_stage(stage
);
1037 struct softpipe_context
*sp
= setup
->softpipe
;
1039 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1041 sp
->quad
.first
->begin(sp
->quad
.first
);
1045 static void setup_end( struct draw_stage
*stage
)
1050 static void reset_stipple_counter( struct draw_stage
*stage
)
1052 struct setup_stage
*setup
= setup_stage(stage
);
1053 setup
->softpipe
->line_stipple_counter
= 0;
1058 * Create a new primitive setup/render stage.
1060 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1062 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1064 setup
->softpipe
= softpipe
;
1065 setup
->stage
.draw
= softpipe
->draw
;
1066 setup
->stage
.begin
= setup_begin
;
1067 setup
->stage
.point
= setup_point
;
1068 setup
->stage
.line
= setup_line
;
1069 setup
->stage
.tri
= setup_tri
;
1070 setup
->stage
.end
= setup_end
;
1071 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1073 setup
->quad
.coef
= setup
->coef
;
1075 return &setup
->stage
;