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
; /* first sample point x coord */
55 GLint lines
; /* number of lines on this edge */
60 * Triangle setup info (derived from draw_stage).
61 * Also used for line drawing (taking some liberties).
64 struct draw_stage stage
; /**< This must be first (base class) */
67 struct softpipe_context
*softpipe
;
69 /* Vertices are just an array of floats making up each attribute in
70 * turn. Currently fixed at 4 floats, but should change in time.
71 * Codegen will help cope with this.
73 const struct vertex_header
*vmax
;
74 const struct vertex_header
*vmid
;
75 const struct vertex_header
*vmin
;
76 const struct vertex_header
*vprovoke
;
84 struct setup_coefficient coef
[FRAG_ATTRIB_MAX
];
85 struct quad_header quad
;
88 GLint left
[2]; /**< [0] = row0, [1] = row1 */
92 GLuint 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 if (setup
->quad
.x0
>= cliprect
->maxx
||
115 setup
->quad
.y0
>= cliprect
->maxy
||
116 setup
->quad
.x0
+ 1 < cliprect
->minx
||
117 setup
->quad
.y0
+ 1 < cliprect
->miny
) {
118 /* totally clipped */
119 setup
->quad
.mask
= 0x0;
122 if (setup
->quad
.x0
< cliprect
->minx
)
123 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
124 if (setup
->quad
.y0
< cliprect
->miny
)
125 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
126 if (setup
->quad
.x0
== cliprect
->maxx
- 1)
127 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
128 if (setup
->quad
.y0
== cliprect
->maxy
- 1)
129 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
134 * Emit/render a quad.
135 * Called during point/line rendering. For triangles, we call
136 * run_shader_block() which doesn't do clipping (since clipping is
137 * done at a higher level for tris).
138 * This passes the quad to the first stage of per-fragment operations.
141 quad_emit(struct setup_stage
*setup
)
144 if (setup
->quad
.mask
) {
145 struct softpipe_context
*sp
= setup
->softpipe
;
146 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
152 * Given an X or Y coordinate, return the block/quad coordinate that it
155 static inline GLint
block( GLint x
)
163 * Run shader on a quad/block.
165 static void run_shader_block( struct setup_stage
*setup
,
166 GLint x
, GLint y
, GLuint mask
)
168 struct softpipe_context
*sp
= setup
->softpipe
;
171 setup
->quad
.mask
= mask
;
172 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
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 GLuint
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 GLint 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 run_shader_block( setup
, x
,
237 calculate_mask( setup
, x
) );
242 setup
->span
.y_flags
= 0;
243 setup
->span
.right
[0] = 0;
244 setup
->span
.right
[1] = 0;
248 static GLboolean
setup_sort_vertices( struct setup_stage
*setup
,
249 const struct prim_header
*prim
)
251 const struct vertex_header
*v0
= prim
->v
[0];
252 const struct vertex_header
*v1
= prim
->v
[1];
253 const struct vertex_header
*v2
= prim
->v
[2];
255 setup
->vprovoke
= v2
;
257 /* determine bottom to top order of vertices */
259 GLfloat y0
= v0
->data
[0][1];
260 GLfloat y1
= v1
->data
[0][1];
261 GLfloat y2
= v2
->data
[0][1];
304 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
305 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
306 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
307 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
308 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
309 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
312 * Compute triangle's area. Use 1/area to compute partial
313 * derivatives of attributes later.
315 * The area will be the same as prim->det, but the sign may be
316 * different depending on how the vertices get sorted above.
318 * To determine whether the primitive is front or back facing we
319 * use the prim->det value because its sign is correct.
322 const GLfloat area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
323 setup
->ebot
.dx
* setup
->emaj
.dy
);
325 setup
->oneoverarea
= 1.0 / area
;
327 _mesa_printf("%s one-over-area %f area %f det %f\n",
328 __FUNCTION__, setup->oneoverarea, area, prim->det );
332 /* We need to know if this is a front or back-facing triangle for:
333 * - the GLSL gl_FrontFacing fragment attribute (bool)
334 * - two-sided stencil test
336 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->setup
.front_winding
== PIPE_WINDING_CW
);
343 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
344 * The value value comes from vertex->data[slot][i].
345 * The result will be put into setup->coef[slot].a0[i].
346 * \param slot which attribute slot
347 * \param i which component of the slot (0..3)
349 static void const_coeff( struct setup_stage
*setup
,
353 assert(slot
< FRAG_ATTRIB_MAX
);
356 setup
->coef
[slot
].dadx
[i
] = 0;
357 setup
->coef
[slot
].dady
[i
] = 0;
359 /* need provoking vertex info!
361 setup
->coef
[slot
].a0
[i
] = setup
->vprovoke
->data
[slot
][i
];
366 * Compute a0, dadx and dady for a linearly interpolated coefficient,
369 static void tri_linear_coeff( struct setup_stage
*setup
,
373 GLfloat botda
= setup
->vmid
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
374 GLfloat majda
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
375 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
376 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
378 assert(slot
< FRAG_ATTRIB_MAX
);
381 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
382 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
384 /* calculate a0 as the value which would be sampled for the
385 * fragment at (0,0), taking into account that we want to sample at
386 * pixel centers, in other words (0.5, 0.5).
388 * this is neat but unfortunately not a good way to do things for
389 * triangles with very large values of dadx or dady as it will
390 * result in the subtraction and re-addition from a0 of a very
391 * large number, which means we'll end up loosing a lot of the
392 * fractional bits and precision from a0. the way to fix this is
393 * to define a0 as the sample at a pixel center somewhere near vmin
394 * instead - i'll switch to this later.
396 setup
->coef
[slot
].a0
[i
] = (setup
->vmin
->data
[slot
][i
] -
397 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
398 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
401 _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
403 setup->coef[slot].a0[i],
404 setup->coef[slot].dadx[i],
405 setup->coef[slot].dady[i]);
411 * Compute a0, dadx and dady for a perspective-corrected interpolant,
414 static void tri_persp_coeff( struct setup_stage
*setup
,
418 /* premultiply by 1/w:
420 GLfloat mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
421 GLfloat mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
422 GLfloat maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
424 GLfloat botda
= mida
- mina
;
425 GLfloat majda
= maxa
- mina
;
426 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
427 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
429 assert(slot
< FRAG_ATTRIB_MAX
);
432 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
433 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
434 setup
->coef
[slot
].a0
[i
] = (mina
-
435 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
436 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
442 * Compute the setup->coef[] array dadx, dady, a0 values.
443 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
445 static void setup_tri_coefficients( struct setup_stage
*setup
)
447 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
450 /* z and w are done by linear interpolation:
452 tri_linear_coeff(setup
, 0, 2);
453 tri_linear_coeff(setup
, 0, 3);
455 /* setup interpolation for all the remaining attributes:
457 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
458 switch (interp
[slot
]) {
459 case INTERP_CONSTANT
:
460 for (j
= 0; j
< NUM_CHANNELS
; j
++)
461 const_coeff(setup
, slot
, j
);
465 for (j
= 0; j
< NUM_CHANNELS
; j
++)
466 tri_linear_coeff(setup
, slot
, j
);
469 case INTERP_PERSPECTIVE
:
470 for (j
= 0; j
< NUM_CHANNELS
; j
++)
471 tri_persp_coeff(setup
, slot
, j
);
479 static void setup_tri_edges( struct setup_stage
*setup
)
481 GLfloat vmin_x
= setup
->vmin
->data
[0][0] + 0.5;
482 GLfloat vmid_x
= setup
->vmid
->data
[0][0] + 0.5;
484 GLfloat vmin_y
= setup
->vmin
->data
[0][1] - 0.5;
485 GLfloat vmid_y
= setup
->vmid
->data
[0][1] - 0.5;
486 GLfloat vmax_y
= setup
->vmax
->data
[0][1] - 0.5;
488 setup
->emaj
.sy
= ceilf(vmin_y
);
489 setup
->emaj
.lines
= (GLint
) ceilf(vmax_y
- setup
->emaj
.sy
);
490 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
491 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
493 setup
->etop
.sy
= ceilf(vmid_y
);
494 setup
->etop
.lines
= (GLint
) ceilf(vmax_y
- setup
->etop
.sy
);
495 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
496 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
498 setup
->ebot
.sy
= ceilf(vmin_y
);
499 setup
->ebot
.lines
= (GLint
) ceilf(vmid_y
- setup
->ebot
.sy
);
500 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
501 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
506 * Render the upper or lower half of a triangle.
507 * Scissoring/cliprect is applied here too.
509 static void subtriangle( struct setup_stage
*setup
,
514 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
515 GLint y
, start_y
, finish_y
;
516 GLint sy
= (GLint
)eleft
->sy
;
518 assert((GLint
)eleft
->sy
== (GLint
) eright
->sy
);
520 /* clip top/bottom */
522 finish_y
= sy
+ lines
;
524 if (start_y
< cliprect
->miny
)
525 start_y
= cliprect
->miny
;
527 if (finish_y
> cliprect
->maxy
)
528 finish_y
= cliprect
->maxy
;
534 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
537 for (y
= start_y
; y
< finish_y
; y
++) {
539 /* avoid accumulating adds as floats don't have the precision to
540 * accurately iterate large triangle edges that way. luckily we
541 * can just multiply these days.
543 * this is all drowned out by the attribute interpolation anyway.
545 GLint left
= (GLint
)(eleft
->sx
+ y
* eleft
->dxdy
);
546 GLint right
= (GLint
)(eright
->sx
+ y
* eright
->dxdy
);
548 /* clip left/right */
549 if (left
< cliprect
->minx
)
550 left
= cliprect
->minx
;
551 if (right
> cliprect
->maxx
)
552 right
= cliprect
->maxx
;
556 if (block(_y
) != setup
->span
.y
) {
558 setup
->span
.y
= block(_y
);
561 setup
->span
.left
[_y
&1] = left
;
562 setup
->span
.right
[_y
&1] = right
;
563 setup
->span
.y_flags
|= 1<<(_y
&1);
568 /* save the values so that emaj can be restarted:
570 eleft
->sx
+= lines
* eleft
->dxdy
;
571 eright
->sx
+= lines
* eright
->dxdy
;
578 * Do setup for triangle rasterization, then render the triangle.
580 static void setup_tri( struct draw_stage
*stage
,
581 struct prim_header
*prim
)
583 struct setup_stage
*setup
= setup_stage( stage
);
586 _mesa_printf("%s\n", __FUNCTION__ );
589 setup_sort_vertices( setup
, prim
);
590 setup_tri_coefficients( setup
);
591 setup_tri_edges( setup
);
593 setup
->quad
.prim
= PRIM_TRI
;
596 setup
->span
.y_flags
= 0;
597 setup
->span
.right
[0] = 0;
598 setup
->span
.right
[1] = 0;
599 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
601 /* init_constant_attribs( setup ); */
603 if (setup
->oneoverarea
< 0.0) {
606 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
607 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
612 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
613 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
616 flush_spans( setup
);
622 * Compute a0, dadx and dady for a linearly interpolated coefficient,
626 line_linear_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
628 const GLfloat dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
629 const GLfloat dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
630 const GLfloat dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
631 setup
->coef
[slot
].dadx
[i
] = dadx
;
632 setup
->coef
[slot
].dady
[i
] = dady
;
633 setup
->coef
[slot
].a0
[i
]
634 = (setup
->vmin
->data
[slot
][i
] -
635 (dadx
* (setup
->vmin
->data
[0][0] - 0.5) +
636 dady
* (setup
->vmin
->data
[0][1] - 0.5)));
641 * Compute a0, dadx and dady for a perspective-corrected interpolant,
645 line_persp_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
648 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
653 * Compute the setup->coef[] array dadx, dady, a0 values.
654 * Must be called after setup->vmin,vmax are initialized.
657 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
659 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
662 /* use setup->vmin, vmax to point to vertices */
663 setup
->vprovoke
= prim
->v
[1];
664 setup
->vmin
= prim
->v
[0];
665 setup
->vmax
= prim
->v
[1];
667 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
668 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
669 /* NOTE: this is not really 1/area */
670 setup
->oneoverarea
= 1.0 / (setup
->emaj
.dx
* setup
->emaj
.dx
+
671 setup
->emaj
.dy
* setup
->emaj
.dy
);
673 /* z and w are done by linear interpolation:
675 line_linear_coeff(setup
, 0, 2);
676 line_linear_coeff(setup
, 0, 3);
678 /* setup interpolation for all the remaining attributes:
680 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
681 switch (interp
[slot
]) {
682 case INTERP_CONSTANT
:
683 for (j
= 0; j
< NUM_CHANNELS
; j
++)
684 const_coeff(setup
, slot
, j
);
688 for (j
= 0; j
< NUM_CHANNELS
; j
++)
689 line_linear_coeff(setup
, slot
, j
);
692 case INTERP_PERSPECTIVE
:
693 for (j
= 0; j
< NUM_CHANNELS
; j
++)
694 line_persp_coeff(setup
, slot
, j
);
702 * Plot a pixel in a line segment.
705 plot(struct setup_stage
*setup
, GLint x
, GLint y
)
707 const GLint iy
= y
& 1;
708 const GLint ix
= x
& 1;
709 const GLint quadX
= x
- ix
;
710 const GLint quadY
= y
- iy
;
711 const GLint mask
= (1 << ix
) << (2 * iy
);
713 if (quadX
!= setup
->quad
.x0
||
714 quadY
!= setup
->quad
.y0
)
716 /* flush prev quad, start new quad */
718 if (setup
->quad
.x0
!= -1)
721 setup
->quad
.x0
= quadX
;
722 setup
->quad
.y0
= quadY
;
723 setup
->quad
.mask
= 0x0;
726 setup
->quad
.mask
|= mask
;
731 * Determine whether or not to emit a line fragment by checking
732 * line stipple pattern.
735 stipple_test(GLint counter
, GLushort pattern
, GLint factor
)
737 GLint b
= (counter
/ factor
) & 0xf;
738 return (1 << b
) & pattern
;
743 * Do setup for line rasterization, then render the line.
744 * XXX single-pixel width, no stipple, etc
747 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
749 const struct vertex_header
*v0
= prim
->v
[0];
750 const struct vertex_header
*v1
= prim
->v
[1];
751 struct setup_stage
*setup
= setup_stage( stage
);
752 struct softpipe_context
*sp
= setup
->softpipe
;
754 GLint x0
= (GLint
) v0
->data
[0][0];
755 GLint x1
= (GLint
) v1
->data
[0][0];
756 GLint y0
= (GLint
) v0
->data
[0][1];
757 GLint y1
= (GLint
) v1
->data
[0][1];
762 if (dx
== 0 && dy
== 0)
765 setup_line_coefficients(setup
, prim
);
768 dx
= -dx
; /* make positive */
776 dy
= -dy
; /* make positive */
786 setup
->quad
.x0
= setup
->quad
.y0
= -1;
787 setup
->quad
.mask
= 0x0;
788 setup
->quad
.prim
= PRIM_LINE
;
789 /* XXX temporary: set coverage to 1.0 so the line appears
790 * if AA mode happens to be enabled.
792 setup
->quad
.coverage
[0] =
793 setup
->quad
.coverage
[1] =
794 setup
->quad
.coverage
[2] =
795 setup
->quad
.coverage
[3] = 1.0;
798 /*** X-major line ***/
800 const GLint errorInc
= dy
+ dy
;
801 GLint error
= errorInc
- dx
;
802 const GLint errorDec
= error
- dx
;
804 for (i
= 0; i
< dx
; i
++) {
805 if (!sp
->setup
.line_stipple_enable
||
806 stipple_test(sp
->line_stipple_counter
,
807 sp
->setup
.line_stipple_pattern
,
808 sp
->setup
.line_stipple_factor
+ 1)) {
821 sp
->line_stipple_counter
++;
825 /*** Y-major line ***/
827 const GLint errorInc
= dx
+ dx
;
828 GLint error
= errorInc
- dy
;
829 const GLint errorDec
= error
- dy
;
831 for (i
= 0; i
< dy
; i
++) {
832 if (!sp
->setup
.line_stipple_enable
||
833 stipple_test(sp
->line_stipple_counter
,
834 sp
->setup
.line_stipple_pattern
,
835 sp
->setup
.line_stipple_factor
+ 1)) {
849 sp
->line_stipple_counter
++;
853 /* draw final quad */
854 if (setup
->quad
.mask
) {
861 * Do setup for point rasterization, then render the point.
862 * Round or square points...
863 * XXX could optimize a lot for 1-pixel points.
866 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
868 struct setup_stage
*setup
= setup_stage( stage
);
869 /*XXX this should be a vertex attrib! */
870 const GLfloat halfSize
= 0.5 * setup
->softpipe
->setup
.point_size
;
871 const GLboolean round
= setup
->softpipe
->setup
.point_smooth
;
872 const struct vertex_header
*v0
= prim
->v
[0];
873 const GLfloat x
= v0
->data
[FRAG_ATTRIB_WPOS
][0];
874 const GLfloat y
= v0
->data
[FRAG_ATTRIB_WPOS
][1];
877 /* For points, all interpolants are constant-valued.
878 * However, for point sprites, we'll need to setup texcoords appropriately.
879 * XXX: which coefficients are the texcoords???
880 * We may do point sprites as textured quads...
882 * KW: We don't know which coefficients are texcoords - ultimately
883 * the choice of what interpolation mode to use for each attribute
884 * should be determined by the fragment program, using
885 * per-attribute declaration statements that include interpolation
886 * mode as a parameter. So either the fragment program will have
887 * to be adjusted for pointsprite vs normal point behaviour, or
888 * otherwise a special interpolation mode will have to be defined
889 * which matches the required behaviour for point sprites. But -
890 * the latter is not a feature of normal hardware, and as such
891 * probably should be ruled out on that basis.
893 setup
->vprovoke
= prim
->v
[0];
894 const_coeff(setup
, 0, 2);
895 const_coeff(setup
, 0, 3);
896 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
897 for (j
= 0; j
< NUM_CHANNELS
; j
++)
898 const_coeff(setup
, slot
, j
);
901 setup
->quad
.prim
= PRIM_POINT
;
903 if (halfSize
<= 0.5 && !round
) {
904 /* special case for 1-pixel points */
905 const GLint ix
= ((GLint
) x
) & 1;
906 const GLint iy
= ((GLint
) y
) & 1;
907 setup
->quad
.x0
= x
- ix
;
908 setup
->quad
.y0
= y
- iy
;
909 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
913 const GLint ixmin
= block((GLint
) (x
- halfSize
));
914 const GLint ixmax
= block((GLint
) (x
+ halfSize
));
915 const GLint iymin
= block((GLint
) (y
- halfSize
));
916 const GLint iymax
= block((GLint
) (y
+ halfSize
));
921 const GLfloat rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
922 const GLfloat rmax
= halfSize
+ 0.7071F
;
923 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
924 const GLfloat rmax2
= rmax
* rmax
;
925 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
927 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
928 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
929 GLfloat dx
, dy
, dist2
, cover
;
931 setup
->quad
.mask
= 0x0;
935 dist2
= dx
* dx
+ dy
* dy
;
936 if (dist2
<= rmax2
) {
937 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
938 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0);
939 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
944 dist2
= dx
* dx
+ dy
* dy
;
945 if (dist2
<= rmax2
) {
946 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
947 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0);
948 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
953 dist2
= dx
* dx
+ dy
* dy
;
954 if (dist2
<= rmax2
) {
955 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
956 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0);
957 setup
->quad
.mask
|= MASK_TOP_LEFT
;
962 dist2
= dx
* dx
+ dy
* dy
;
963 if (dist2
<= rmax2
) {
964 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
965 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0);
966 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
969 if (setup
->quad
.mask
) {
979 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
980 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
981 setup
->quad
.mask
= 0xf;
983 if (ix
+ 0.5 < x
- halfSize
) {
984 /* fragment is past left edge of point, turn off left bits */
985 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
988 if (ix
+ 1.5 > x
+ halfSize
) {
989 /* past the right edge */
990 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
993 if (iy
+ 0.5 < y
- halfSize
) {
994 /* below the bottom edge */
995 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
998 if (iy
+ 1.5 > y
+ halfSize
) {
999 /* above the top edge */
1000 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1003 if (setup
->quad
.mask
) {
1004 setup
->quad
.x0
= ix
;
1005 setup
->quad
.y0
= iy
;
1016 static void setup_begin( struct draw_stage
*stage
)
1018 struct setup_stage
*setup
= setup_stage(stage
);
1020 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1023 * XXX this is where we might map() the renderbuffers to begin
1029 static void setup_end( struct draw_stage
*stage
)
1032 * XXX this is where we might unmap() the renderbuffers after
1038 static void reset_stipple_counter( struct draw_stage
*stage
)
1040 struct setup_stage
*setup
= setup_stage(stage
);
1041 setup
->softpipe
->line_stipple_counter
= 0;
1046 * Create a new primitive setup/render stage.
1048 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1050 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1052 setup
->softpipe
= softpipe
;
1053 setup
->stage
.draw
= softpipe
->draw
;
1054 setup
->stage
.begin
= setup_begin
;
1055 setup
->stage
.point
= setup_point
;
1056 setup
->stage
.line
= setup_line
;
1057 setup
->stage
.tri
= setup_tri
;
1058 setup
->stage
.end
= setup_end
;
1059 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1061 setup
->quad
.coef
= setup
->coef
;
1063 return &setup
->stage
;