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>
39 #include "sp_context.h"
40 #include "sp_headers.h"
41 #include "pipe/draw/draw_private.h"
43 #include "sp_prim_setup.h"
50 GLfloat dx
; /**< X(v1) - X(v0), used only during setup */
51 GLfloat dy
; /**< Y(v1) - Y(v0), used only during setup */
52 GLfloat dxdy
; /**< dx/dy */
53 GLfloat sx
, sy
; /**< first sample point coord */
54 GLint 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 setup_coefficient coef
[FRAG_ATTRIB_MAX
];
83 struct quad_header quad
;
86 GLint left
[2]; /**< [0] = row0, [1] = row1 */
90 GLuint 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 if (setup
->quad
.x0
>= cliprect
->maxx
||
113 setup
->quad
.y0
>= cliprect
->maxy
||
114 setup
->quad
.x0
+ 1 < cliprect
->minx
||
115 setup
->quad
.y0
+ 1 < cliprect
->miny
) {
116 /* totally clipped */
117 setup
->quad
.mask
= 0x0;
120 if (setup
->quad
.x0
< cliprect
->minx
)
121 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
122 if (setup
->quad
.y0
< cliprect
->miny
)
123 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
124 if (setup
->quad
.x0
== cliprect
->maxx
- 1)
125 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
126 if (setup
->quad
.y0
== cliprect
->maxy
- 1)
127 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
132 * Emit a quad (pass to next stage) with clipping.
135 clip_emit_quad(struct setup_stage
*setup
)
138 if (setup
->quad
.mask
) {
139 struct softpipe_context
*sp
= setup
->softpipe
;
140 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
146 * Emit a quad (pass to next stage). No clipping is done.
149 emit_quad( struct setup_stage
*setup
, GLint x
, GLint y
, GLuint mask
)
151 struct softpipe_context
*sp
= setup
->softpipe
;
154 setup
->quad
.mask
= mask
;
155 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
160 * Given an X or Y coordinate, return the block/quad coordinate that it
163 static INLINE GLint
block( GLint x
)
170 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
171 * the triangle's bounds.
173 * this is pretty nasty... may need to rework flush_spans again to
174 * fix it, if possible.
176 static GLuint
calculate_mask( struct setup_stage
*setup
,
181 if (x
>= setup
->span
.left
[0] && x
< setup
->span
.right
[0])
182 mask
|= MASK_BOTTOM_LEFT
;
184 if (x
>= setup
->span
.left
[1] && x
< setup
->span
.right
[1])
185 mask
|= MASK_TOP_LEFT
;
187 if (x
+1 >= setup
->span
.left
[0] && x
+1 < setup
->span
.right
[0])
188 mask
|= MASK_BOTTOM_RIGHT
;
190 if (x
+1 >= setup
->span
.left
[1] && x
+1 < setup
->span
.right
[1])
191 mask
|= MASK_TOP_RIGHT
;
198 * Render a horizontal span of quads
200 static void flush_spans( struct setup_stage
*setup
)
202 GLint minleft
, maxright
;
205 switch (setup
->span
.y_flags
) {
207 minleft
= MIN2(setup
->span
.left
[0], setup
->span
.left
[1]);
208 maxright
= MAX2(setup
->span
.right
[0], setup
->span
.right
[1]);
212 minleft
= setup
->span
.left
[0];
213 maxright
= setup
->span
.right
[0];
217 minleft
= setup
->span
.left
[1];
218 maxright
= setup
->span
.right
[1];
226 for (x
= block(minleft
); x
<= block(maxright
); )
228 emit_quad( setup
, x
, setup
->span
.y
,
229 calculate_mask( setup
, x
) );
234 setup
->span
.y_flags
= 0;
235 setup
->span
.right
[0] = 0;
236 setup
->span
.right
[1] = 0;
240 static GLboolean
setup_sort_vertices( struct setup_stage
*setup
,
241 const struct prim_header
*prim
)
243 const struct vertex_header
*v0
= prim
->v
[0];
244 const struct vertex_header
*v1
= prim
->v
[1];
245 const struct vertex_header
*v2
= prim
->v
[2];
247 setup
->vprovoke
= v2
;
249 /* determine bottom to top order of vertices */
251 GLfloat y0
= v0
->data
[0][1];
252 GLfloat y1
= v1
->data
[0][1];
253 GLfloat y2
= v2
->data
[0][1];
296 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
297 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
298 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
299 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
300 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
301 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
304 * Compute triangle's area. Use 1/area to compute partial
305 * derivatives of attributes later.
307 * The area will be the same as prim->det, but the sign may be
308 * different depending on how the vertices get sorted above.
310 * To determine whether the primitive is front or back facing we
311 * use the prim->det value because its sign is correct.
314 const GLfloat area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
315 setup
->ebot
.dx
* setup
->emaj
.dy
);
317 setup
->oneoverarea
= 1.0 / area
;
319 _mesa_printf("%s one-over-area %f area %f det %f\n",
320 __FUNCTION__, setup->oneoverarea, area, prim->det );
324 /* We need to know if this is a front or back-facing triangle for:
325 * - the GLSL gl_FrontFacing fragment attribute (bool)
326 * - two-sided stencil test
328 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->setup
.front_winding
== PIPE_WINDING_CW
);
335 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
336 * The value value comes from vertex->data[slot][i].
337 * The result will be put into setup->coef[slot].a0[i].
338 * \param slot which attribute slot
339 * \param i which component of the slot (0..3)
341 static void const_coeff( struct setup_stage
*setup
,
345 assert(slot
< FRAG_ATTRIB_MAX
);
348 setup
->coef
[slot
].dadx
[i
] = 0;
349 setup
->coef
[slot
].dady
[i
] = 0;
351 /* need provoking vertex info!
353 setup
->coef
[slot
].a0
[i
] = setup
->vprovoke
->data
[slot
][i
];
358 * Compute a0, dadx and dady for a linearly interpolated coefficient,
361 static void tri_linear_coeff( struct setup_stage
*setup
,
365 GLfloat botda
= setup
->vmid
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
366 GLfloat majda
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
367 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
368 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
370 assert(slot
< FRAG_ATTRIB_MAX
);
373 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
374 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
376 /* calculate a0 as the value which would be sampled for the
377 * fragment at (0,0), taking into account that we want to sample at
378 * pixel centers, in other words (0.5, 0.5).
380 * this is neat but unfortunately not a good way to do things for
381 * triangles with very large values of dadx or dady as it will
382 * result in the subtraction and re-addition from a0 of a very
383 * large number, which means we'll end up loosing a lot of the
384 * fractional bits and precision from a0. the way to fix this is
385 * to define a0 as the sample at a pixel center somewhere near vmin
386 * instead - i'll switch to this later.
388 setup
->coef
[slot
].a0
[i
] = (setup
->vmin
->data
[slot
][i
] -
389 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
390 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
393 _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
395 setup->coef[slot].a0[i],
396 setup->coef[slot].dadx[i],
397 setup->coef[slot].dady[i]);
403 * Compute a0, dadx and dady for a perspective-corrected interpolant,
406 static void tri_persp_coeff( struct setup_stage
*setup
,
410 /* premultiply by 1/w:
412 GLfloat mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
413 GLfloat mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
414 GLfloat maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
416 GLfloat botda
= mida
- mina
;
417 GLfloat majda
= maxa
- mina
;
418 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
419 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
421 assert(slot
< FRAG_ATTRIB_MAX
);
424 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
425 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
426 setup
->coef
[slot
].a0
[i
] = (mina
-
427 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
428 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
434 * Compute the setup->coef[] array dadx, dady, a0 values.
435 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
437 static void setup_tri_coefficients( struct setup_stage
*setup
)
439 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
442 /* z and w are done by linear interpolation:
444 tri_linear_coeff(setup
, 0, 2);
445 tri_linear_coeff(setup
, 0, 3);
447 /* setup interpolation for all the remaining attributes:
449 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
450 switch (interp
[slot
]) {
451 case INTERP_CONSTANT
:
452 for (j
= 0; j
< NUM_CHANNELS
; j
++)
453 const_coeff(setup
, slot
, j
);
457 for (j
= 0; j
< NUM_CHANNELS
; j
++)
458 tri_linear_coeff(setup
, slot
, j
);
461 case INTERP_PERSPECTIVE
:
462 for (j
= 0; j
< NUM_CHANNELS
; j
++)
463 tri_persp_coeff(setup
, slot
, j
);
471 static void setup_tri_edges( struct setup_stage
*setup
)
473 GLfloat vmin_x
= setup
->vmin
->data
[0][0] + 0.5;
474 GLfloat vmid_x
= setup
->vmid
->data
[0][0] + 0.5;
476 GLfloat vmin_y
= setup
->vmin
->data
[0][1] - 0.5;
477 GLfloat vmid_y
= setup
->vmid
->data
[0][1] - 0.5;
478 GLfloat vmax_y
= setup
->vmax
->data
[0][1] - 0.5;
480 setup
->emaj
.sy
= ceilf(vmin_y
);
481 setup
->emaj
.lines
= (GLint
) ceilf(vmax_y
- setup
->emaj
.sy
);
482 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
483 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
485 setup
->etop
.sy
= ceilf(vmid_y
);
486 setup
->etop
.lines
= (GLint
) ceilf(vmax_y
- setup
->etop
.sy
);
487 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
488 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
490 setup
->ebot
.sy
= ceilf(vmin_y
);
491 setup
->ebot
.lines
= (GLint
) ceilf(vmid_y
- setup
->ebot
.sy
);
492 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
493 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
498 * Render the upper or lower half of a triangle.
499 * Scissoring/cliprect is applied here too.
501 static void subtriangle( struct setup_stage
*setup
,
506 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
507 GLint y
, start_y
, finish_y
;
508 GLint sy
= (GLint
)eleft
->sy
;
510 assert((GLint
)eleft
->sy
== (GLint
) eright
->sy
);
512 /* clip top/bottom */
514 finish_y
= sy
+ lines
;
516 if (start_y
< cliprect
->miny
)
517 start_y
= cliprect
->miny
;
519 if (finish_y
> cliprect
->maxy
)
520 finish_y
= cliprect
->maxy
;
526 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
529 for (y
= start_y
; y
< finish_y
; y
++) {
531 /* avoid accumulating adds as floats don't have the precision to
532 * accurately iterate large triangle edges that way. luckily we
533 * can just multiply these days.
535 * this is all drowned out by the attribute interpolation anyway.
537 GLint left
= (GLint
)(eleft
->sx
+ y
* eleft
->dxdy
);
538 GLint right
= (GLint
)(eright
->sx
+ y
* eright
->dxdy
);
540 /* clip left/right */
541 if (left
< cliprect
->minx
)
542 left
= cliprect
->minx
;
543 if (right
> cliprect
->maxx
)
544 right
= cliprect
->maxx
;
548 if (block(_y
) != setup
->span
.y
) {
550 setup
->span
.y
= block(_y
);
553 setup
->span
.left
[_y
&1] = left
;
554 setup
->span
.right
[_y
&1] = right
;
555 setup
->span
.y_flags
|= 1<<(_y
&1);
560 /* save the values so that emaj can be restarted:
562 eleft
->sx
+= lines
* eleft
->dxdy
;
563 eright
->sx
+= lines
* eright
->dxdy
;
570 * Do setup for triangle rasterization, then render the triangle.
572 static void setup_tri( struct draw_stage
*stage
,
573 struct prim_header
*prim
)
575 struct setup_stage
*setup
= setup_stage( stage
);
578 _mesa_printf("%s\n", __FUNCTION__ );
581 setup_sort_vertices( setup
, prim
);
582 setup_tri_coefficients( setup
);
583 setup_tri_edges( setup
);
585 setup
->quad
.prim
= PRIM_TRI
;
588 setup
->span
.y_flags
= 0;
589 setup
->span
.right
[0] = 0;
590 setup
->span
.right
[1] = 0;
591 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
593 /* init_constant_attribs( setup ); */
595 if (setup
->oneoverarea
< 0.0) {
598 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
599 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
604 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
605 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
608 flush_spans( setup
);
614 * Compute a0, dadx and dady for a linearly interpolated coefficient,
618 line_linear_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
620 const GLfloat dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
621 const GLfloat dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
622 const GLfloat dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
623 setup
->coef
[slot
].dadx
[i
] = dadx
;
624 setup
->coef
[slot
].dady
[i
] = dady
;
625 setup
->coef
[slot
].a0
[i
]
626 = (setup
->vmin
->data
[slot
][i
] -
627 (dadx
* (setup
->vmin
->data
[0][0] - 0.5) +
628 dady
* (setup
->vmin
->data
[0][1] - 0.5)));
633 * Compute a0, dadx and dady for a perspective-corrected interpolant,
637 line_persp_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
640 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
645 * Compute the setup->coef[] array dadx, dady, a0 values.
646 * Must be called after setup->vmin,vmax are initialized.
649 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
651 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
654 /* use setup->vmin, vmax to point to vertices */
655 setup
->vprovoke
= prim
->v
[1];
656 setup
->vmin
= prim
->v
[0];
657 setup
->vmax
= prim
->v
[1];
659 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
660 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
661 /* NOTE: this is not really 1/area */
662 setup
->oneoverarea
= 1.0 / (setup
->emaj
.dx
* setup
->emaj
.dx
+
663 setup
->emaj
.dy
* setup
->emaj
.dy
);
665 /* z and w are done by linear interpolation:
667 line_linear_coeff(setup
, 0, 2);
668 line_linear_coeff(setup
, 0, 3);
670 /* setup interpolation for all the remaining attributes:
672 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
673 switch (interp
[slot
]) {
674 case INTERP_CONSTANT
:
675 for (j
= 0; j
< NUM_CHANNELS
; j
++)
676 const_coeff(setup
, slot
, j
);
680 for (j
= 0; j
< NUM_CHANNELS
; j
++)
681 line_linear_coeff(setup
, slot
, j
);
684 case INTERP_PERSPECTIVE
:
685 for (j
= 0; j
< NUM_CHANNELS
; j
++)
686 line_persp_coeff(setup
, slot
, j
);
694 * Plot a pixel in a line segment.
697 plot(struct setup_stage
*setup
, GLint x
, GLint y
)
699 const GLint iy
= y
& 1;
700 const GLint ix
= x
& 1;
701 const GLint quadX
= x
- ix
;
702 const GLint quadY
= y
- iy
;
703 const GLint mask
= (1 << ix
) << (2 * iy
);
705 if (quadX
!= setup
->quad
.x0
||
706 quadY
!= setup
->quad
.y0
)
708 /* flush prev quad, start new quad */
710 if (setup
->quad
.x0
!= -1)
711 clip_emit_quad(setup
);
713 setup
->quad
.x0
= quadX
;
714 setup
->quad
.y0
= quadY
;
715 setup
->quad
.mask
= 0x0;
718 setup
->quad
.mask
|= mask
;
723 * Determine whether or not to emit a line fragment by checking
724 * line stipple pattern.
727 stipple_test(GLint counter
, GLushort pattern
, GLint factor
)
729 GLint b
= (counter
/ factor
) & 0xf;
730 return (1 << b
) & pattern
;
735 * Do setup for line rasterization, then render the line.
736 * XXX single-pixel width, no stipple, etc
739 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
741 const struct vertex_header
*v0
= prim
->v
[0];
742 const struct vertex_header
*v1
= prim
->v
[1];
743 struct setup_stage
*setup
= setup_stage( stage
);
744 struct softpipe_context
*sp
= setup
->softpipe
;
746 GLint x0
= (GLint
) v0
->data
[0][0];
747 GLint x1
= (GLint
) v1
->data
[0][0];
748 GLint y0
= (GLint
) v0
->data
[0][1];
749 GLint y1
= (GLint
) v1
->data
[0][1];
754 if (dx
== 0 && dy
== 0)
757 setup_line_coefficients(setup
, prim
);
760 dx
= -dx
; /* make positive */
768 dy
= -dy
; /* make positive */
778 setup
->quad
.x0
= setup
->quad
.y0
= -1;
779 setup
->quad
.mask
= 0x0;
780 setup
->quad
.prim
= PRIM_LINE
;
781 /* XXX temporary: set coverage to 1.0 so the line appears
782 * if AA mode happens to be enabled.
784 setup
->quad
.coverage
[0] =
785 setup
->quad
.coverage
[1] =
786 setup
->quad
.coverage
[2] =
787 setup
->quad
.coverage
[3] = 1.0;
790 /*** X-major line ***/
792 const GLint errorInc
= dy
+ dy
;
793 GLint error
= errorInc
- dx
;
794 const GLint errorDec
= error
- dx
;
796 for (i
= 0; i
< dx
; i
++) {
797 if (!sp
->setup
.line_stipple_enable
||
798 stipple_test(sp
->line_stipple_counter
,
799 sp
->setup
.line_stipple_pattern
,
800 sp
->setup
.line_stipple_factor
+ 1)) {
813 sp
->line_stipple_counter
++;
817 /*** Y-major line ***/
819 const GLint errorInc
= dx
+ dx
;
820 GLint error
= errorInc
- dy
;
821 const GLint errorDec
= error
- dy
;
823 for (i
= 0; i
< dy
; i
++) {
824 if (!sp
->setup
.line_stipple_enable
||
825 stipple_test(sp
->line_stipple_counter
,
826 sp
->setup
.line_stipple_pattern
,
827 sp
->setup
.line_stipple_factor
+ 1)) {
841 sp
->line_stipple_counter
++;
845 /* draw final quad */
846 if (setup
->quad
.mask
) {
847 clip_emit_quad(setup
);
853 * Do setup for point rasterization, then render the point.
854 * Round or square points...
855 * XXX could optimize a lot for 1-pixel points.
858 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
860 struct setup_stage
*setup
= setup_stage( stage
);
861 /*XXX this should be a vertex attrib! */
862 const GLfloat halfSize
= 0.5 * setup
->softpipe
->setup
.point_size
;
863 const GLboolean round
= setup
->softpipe
->setup
.point_smooth
;
864 const struct vertex_header
*v0
= prim
->v
[0];
865 const GLfloat x
= v0
->data
[FRAG_ATTRIB_WPOS
][0];
866 const GLfloat y
= v0
->data
[FRAG_ATTRIB_WPOS
][1];
869 /* For points, all interpolants are constant-valued.
870 * However, for point sprites, we'll need to setup texcoords appropriately.
871 * XXX: which coefficients are the texcoords???
872 * We may do point sprites as textured quads...
874 * KW: We don't know which coefficients are texcoords - ultimately
875 * the choice of what interpolation mode to use for each attribute
876 * should be determined by the fragment program, using
877 * per-attribute declaration statements that include interpolation
878 * mode as a parameter. So either the fragment program will have
879 * to be adjusted for pointsprite vs normal point behaviour, or
880 * otherwise a special interpolation mode will have to be defined
881 * which matches the required behaviour for point sprites. But -
882 * the latter is not a feature of normal hardware, and as such
883 * probably should be ruled out on that basis.
885 setup
->vprovoke
= prim
->v
[0];
886 const_coeff(setup
, 0, 2);
887 const_coeff(setup
, 0, 3);
888 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
889 for (j
= 0; j
< NUM_CHANNELS
; j
++)
890 const_coeff(setup
, slot
, j
);
893 setup
->quad
.prim
= PRIM_POINT
;
895 if (halfSize
<= 0.5 && !round
) {
896 /* special case for 1-pixel points */
897 const GLint ix
= ((GLint
) x
) & 1;
898 const GLint iy
= ((GLint
) y
) & 1;
899 setup
->quad
.x0
= x
- ix
;
900 setup
->quad
.y0
= y
- iy
;
901 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
902 clip_emit_quad(setup
);
905 const GLint ixmin
= block((GLint
) (x
- halfSize
));
906 const GLint ixmax
= block((GLint
) (x
+ halfSize
));
907 const GLint iymin
= block((GLint
) (y
- halfSize
));
908 const GLint iymax
= block((GLint
) (y
+ halfSize
));
913 const GLfloat rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
914 const GLfloat rmax
= halfSize
+ 0.7071F
;
915 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
916 const GLfloat rmax2
= rmax
* rmax
;
917 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
919 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
920 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
921 GLfloat dx
, dy
, dist2
, cover
;
923 setup
->quad
.mask
= 0x0;
927 dist2
= dx
* dx
+ dy
* dy
;
928 if (dist2
<= rmax2
) {
929 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
930 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0);
931 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
936 dist2
= dx
* dx
+ dy
* dy
;
937 if (dist2
<= rmax2
) {
938 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
939 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0);
940 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
945 dist2
= dx
* dx
+ dy
* dy
;
946 if (dist2
<= rmax2
) {
947 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
948 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0);
949 setup
->quad
.mask
|= MASK_TOP_LEFT
;
954 dist2
= dx
* dx
+ dy
* dy
;
955 if (dist2
<= rmax2
) {
956 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
957 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0);
958 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
961 if (setup
->quad
.mask
) {
964 clip_emit_quad(setup
);
971 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
972 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
973 setup
->quad
.mask
= 0xf;
975 if (ix
+ 0.5 < x
- halfSize
) {
976 /* fragment is past left edge of point, turn off left bits */
977 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
980 if (ix
+ 1.5 > x
+ halfSize
) {
981 /* past the right edge */
982 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
985 if (iy
+ 0.5 < y
- halfSize
) {
986 /* below the bottom edge */
987 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
990 if (iy
+ 1.5 > y
+ halfSize
) {
991 /* above the top edge */
992 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
995 if (setup
->quad
.mask
) {
998 clip_emit_quad(setup
);
1008 static void setup_begin( struct draw_stage
*stage
)
1010 struct setup_stage
*setup
= setup_stage(stage
);
1011 struct softpipe_context
*sp
= setup
->softpipe
;
1013 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1015 sp
->quad
.first
->begin(sp
->quad
.first
);
1019 static void setup_end( struct draw_stage
*stage
)
1024 static void reset_stipple_counter( struct draw_stage
*stage
)
1026 struct setup_stage
*setup
= setup_stage(stage
);
1027 setup
->softpipe
->line_stipple_counter
= 0;
1032 * Create a new primitive setup/render stage.
1034 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1036 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1038 setup
->softpipe
= softpipe
;
1039 setup
->stage
.draw
= softpipe
->draw
;
1040 setup
->stage
.begin
= setup_begin
;
1041 setup
->stage
.point
= setup_point
;
1042 setup
->stage
.line
= setup_line
;
1043 setup
->stage
.tri
= setup_tri
;
1044 setup
->stage
.end
= setup_end
;
1045 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1047 setup
->quad
.coef
= setup
->coef
;
1049 return &setup
->stage
;