913ae44601290d86df8abb5e873645c9d5701be3
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 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
->quad
.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
->rasterizer
->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,
429 * We basically multiply the vertex value by 1/w before computing
430 * the plane coefficients (a0, dadx, dady).
431 * Later, when we compute the value at a particular fragment position we'll
432 * divide the interpolated value by the interpolated W at that fragment.
434 static void tri_persp_coeff( struct setup_stage
*setup
,
438 /* premultiply by 1/w:
440 float mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
441 float mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
442 float maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
444 float botda
= mida
- mina
;
445 float majda
= maxa
- mina
;
446 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
447 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
450 printf("tri persp %d,%d: %f %f %f\n", slot, i,
451 setup->vmin->data[slot][i],
452 setup->vmid->data[slot][i],
453 setup->vmax->data[slot][i]
457 assert(slot
< TGSI_ATTRIB_MAX
);
460 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
461 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
462 setup
->coef
[slot
].a0
[i
] = (mina
-
463 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5f
) +
464 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5f
)));
469 * Compute the setup->coef[] array dadx, dady, a0 values.
470 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
472 static void setup_tri_coefficients( struct setup_stage
*setup
)
474 const interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
477 /* z and w are done by linear interpolation:
479 tri_linear_coeff(setup
, 0, 2);
480 tri_linear_coeff(setup
, 0, 3);
482 /* setup interpolation for all the remaining attributes:
484 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
485 switch (interp
[slot
]) {
486 case INTERP_CONSTANT
:
487 for (j
= 0; j
< NUM_CHANNELS
; j
++)
488 const_coeff(setup
, slot
, j
);
492 for (j
= 0; j
< NUM_CHANNELS
; j
++)
493 tri_linear_coeff(setup
, slot
, j
);
496 case INTERP_PERSPECTIVE
:
497 for (j
= 0; j
< NUM_CHANNELS
; j
++)
498 tri_persp_coeff(setup
, slot
, j
);
502 /* invalid interp mode */
510 static void setup_tri_edges( struct setup_stage
*setup
)
512 float vmin_x
= setup
->vmin
->data
[0][0] + 0.5f
;
513 float vmid_x
= setup
->vmid
->data
[0][0] + 0.5f
;
515 float vmin_y
= setup
->vmin
->data
[0][1] - 0.5f
;
516 float vmid_y
= setup
->vmid
->data
[0][1] - 0.5f
;
517 float vmax_y
= setup
->vmax
->data
[0][1] - 0.5f
;
519 setup
->emaj
.sy
= ceilf(vmin_y
);
520 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
521 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
522 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
524 setup
->etop
.sy
= ceilf(vmid_y
);
525 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
526 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
527 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
529 setup
->ebot
.sy
= ceilf(vmin_y
);
530 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
531 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
532 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
537 * Render the upper or lower half of a triangle.
538 * Scissoring/cliprect is applied here too.
540 static void subtriangle( struct setup_stage
*setup
,
545 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
546 const int minx
= (int) cliprect
->minx
;
547 const int maxx
= (int) cliprect
->maxx
;
548 const int miny
= (int) cliprect
->miny
;
549 const int maxy
= (int) cliprect
->maxy
;
550 int y
, start_y
, finish_y
;
551 int sy
= (int)eleft
->sy
;
553 assert((int)eleft
->sy
== (int) eright
->sy
);
555 /* clip top/bottom */
557 finish_y
= sy
+ lines
;
569 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
572 for (y
= start_y
; y
< finish_y
; y
++) {
574 /* avoid accumulating adds as floats don't have the precision to
575 * accurately iterate large triangle edges that way. luckily we
576 * can just multiply these days.
578 * this is all drowned out by the attribute interpolation anyway.
580 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
581 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
583 /* clip left/right */
591 if (block(_y
) != setup
->span
.y
) {
593 setup
->span
.y
= block(_y
);
596 setup
->span
.left
[_y
&1] = left
;setup
->span
.right
[_y
&1] = right
;
597 setup
->span
.y_flags
|= 1<<(_y
&1);
602 /* save the values so that emaj can be restarted:
604 eleft
->sx
+= lines
* eleft
->dxdy
;
605 eright
->sx
+= lines
* eright
->dxdy
;
612 * Do setup for triangle rasterization, then render the triangle.
614 static void setup_tri( struct draw_stage
*stage
,
615 struct prim_header
*prim
)
617 struct setup_stage
*setup
= setup_stage( stage
);
620 _mesa_printf("%s\n", __FUNCTION__ );
623 setup_sort_vertices( setup
, prim
);
624 setup_tri_coefficients( setup
);
625 setup_tri_edges( setup
);
627 setup
->quad
.prim
= PRIM_TRI
;
630 setup
->span
.y_flags
= 0;
631 setup
->span
.right
[0] = 0;
632 setup
->span
.right
[1] = 0;
633 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
635 /* init_constant_attribs( setup ); */
637 if (setup
->oneoverarea
< 0.0) {
640 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
641 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
646 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
647 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
650 flush_spans( setup
);
656 * Compute a0, dadx and dady for a linearly interpolated coefficient,
660 line_linear_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
662 const float dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
663 const float dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
664 const float dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
665 setup
->coef
[slot
].dadx
[i
] = dadx
;
666 setup
->coef
[slot
].dady
[i
] = dady
;
667 setup
->coef
[slot
].a0
[i
]
668 = (setup
->vmin
->data
[slot
][i
] -
669 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
670 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
675 * Compute a0, dadx and dady for a perspective-corrected interpolant,
679 line_persp_coeff(struct setup_stage
*setup
, unsigned slot
, unsigned i
)
682 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
687 * Compute the setup->coef[] array dadx, dady, a0 values.
688 * Must be called after setup->vmin,vmax are initialized.
691 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
693 const interp_mode
*interp
= setup
->softpipe
->vertex_info
.interp_mode
;
696 /* use setup->vmin, vmax to point to vertices */
697 setup
->vprovoke
= prim
->v
[1];
698 setup
->vmin
= prim
->v
[0];
699 setup
->vmax
= prim
->v
[1];
701 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
702 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
703 /* NOTE: this is not really 1/area */
704 setup
->oneoverarea
= 1.0f
/ (setup
->emaj
.dx
* setup
->emaj
.dx
+
705 setup
->emaj
.dy
* setup
->emaj
.dy
);
707 /* z and w are done by linear interpolation:
709 line_linear_coeff(setup
, 0, 2);
710 line_linear_coeff(setup
, 0, 3);
712 /* setup interpolation for all the remaining attributes:
714 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
715 switch (interp
[slot
]) {
716 case INTERP_CONSTANT
:
717 for (j
= 0; j
< NUM_CHANNELS
; j
++)
718 const_coeff(setup
, slot
, j
);
722 for (j
= 0; j
< NUM_CHANNELS
; j
++)
723 line_linear_coeff(setup
, slot
, j
);
726 case INTERP_PERSPECTIVE
:
727 for (j
= 0; j
< NUM_CHANNELS
; j
++)
728 line_persp_coeff(setup
, slot
, j
);
732 /* invalid interp mode */
740 * Plot a pixel in a line segment.
743 plot(struct setup_stage
*setup
, int x
, int y
)
745 const int iy
= y
& 1;
746 const int ix
= x
& 1;
747 const int quadX
= x
- ix
;
748 const int quadY
= y
- iy
;
749 const int mask
= (1 << ix
) << (2 * iy
);
751 if (quadX
!= setup
->quad
.x0
||
752 quadY
!= setup
->quad
.y0
)
754 /* flush prev quad, start new quad */
756 if (setup
->quad
.x0
!= -1)
757 clip_emit_quad(setup
);
759 setup
->quad
.x0
= quadX
;
760 setup
->quad
.y0
= quadY
;
761 setup
->quad
.mask
= 0x0;
764 setup
->quad
.mask
|= mask
;
769 * Determine whether or not to emit a line fragment by checking
770 * line stipple pattern.
772 static INLINE
unsigned
773 stipple_test(int counter
, ushort pattern
, int factor
)
775 int b
= (counter
/ factor
) & 0xf;
776 return (1 << b
) & pattern
;
781 * Do setup for line rasterization, then render the line.
782 * XXX single-pixel width, no stipple, etc
785 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
787 const struct vertex_header
*v0
= prim
->v
[0];
788 const struct vertex_header
*v1
= prim
->v
[1];
789 struct setup_stage
*setup
= setup_stage( stage
);
790 struct softpipe_context
*sp
= setup
->softpipe
;
792 int x0
= (int) v0
->data
[0][0];
793 int x1
= (int) v1
->data
[0][0];
794 int y0
= (int) v0
->data
[0][1];
795 int y1
= (int) v1
->data
[0][1];
800 if (dx
== 0 && dy
== 0)
803 setup_line_coefficients(setup
, prim
);
806 dx
= -dx
; /* make positive */
814 dy
= -dy
; /* make positive */
824 setup
->quad
.x0
= setup
->quad
.y0
= -1;
825 setup
->quad
.mask
= 0x0;
826 setup
->quad
.prim
= PRIM_LINE
;
827 /* XXX temporary: set coverage to 1.0 so the line appears
828 * if AA mode happens to be enabled.
830 setup
->quad
.coverage
[0] =
831 setup
->quad
.coverage
[1] =
832 setup
->quad
.coverage
[2] =
833 setup
->quad
.coverage
[3] = 1.0;
836 /*** X-major line ***/
838 const int errorInc
= dy
+ dy
;
839 int error
= errorInc
- dx
;
840 const int errorDec
= error
- dx
;
842 for (i
= 0; i
< dx
; i
++) {
843 if (!sp
->rasterizer
->line_stipple_enable
||
844 stipple_test(sp
->line_stipple_counter
,
845 sp
->rasterizer
->line_stipple_pattern
,
846 sp
->rasterizer
->line_stipple_factor
+ 1)) {
859 sp
->line_stipple_counter
++;
863 /*** Y-major line ***/
865 const int errorInc
= dx
+ dx
;
866 int error
= errorInc
- dy
;
867 const int errorDec
= error
- dy
;
869 for (i
= 0; i
< dy
; i
++) {
870 if (!sp
->rasterizer
->line_stipple_enable
||
871 stipple_test(sp
->line_stipple_counter
,
872 sp
->rasterizer
->line_stipple_pattern
,
873 sp
->rasterizer
->line_stipple_factor
+ 1)) {
887 sp
->line_stipple_counter
++;
891 /* draw final quad */
892 if (setup
->quad
.mask
) {
893 clip_emit_quad(setup
);
899 * Do setup for point rasterization, then render the point.
900 * Round or square points...
901 * XXX could optimize a lot for 1-pixel points.
904 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
906 struct setup_stage
*setup
= setup_stage( stage
);
907 const struct vertex_header
*v0
= prim
->v
[0];
908 const int sizeAttr
= setup
->softpipe
->psize_slot
;
910 = sizeAttr
? (0.5f
* v0
->data
[sizeAttr
][0])
911 : (0.5f
* setup
->softpipe
->rasterizer
->point_size
);
912 const boolean round
= setup
->softpipe
->rasterizer
->point_smooth
;
913 const float x
= v0
->data
[TGSI_ATTRIB_POS
][0];
914 const float y
= v0
->data
[TGSI_ATTRIB_POS
][1];
917 assert(sizeAttr
>= 0);
919 /* For points, all interpolants are constant-valued.
920 * However, for point sprites, we'll need to setup texcoords appropriately.
921 * XXX: which coefficients are the texcoords???
922 * We may do point sprites as textured quads...
924 * KW: We don't know which coefficients are texcoords - ultimately
925 * the choice of what interpolation mode to use for each attribute
926 * should be determined by the fragment program, using
927 * per-attribute declaration statements that include interpolation
928 * mode as a parameter. So either the fragment program will have
929 * to be adjusted for pointsprite vs normal point behaviour, or
930 * otherwise a special interpolation mode will have to be defined
931 * which matches the required behaviour for point sprites. But -
932 * the latter is not a feature of normal hardware, and as such
933 * probably should be ruled out on that basis.
935 setup
->vprovoke
= prim
->v
[0];
936 const_coeff(setup
, 0, 2);
937 const_coeff(setup
, 0, 3);
938 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
939 for (j
= 0; j
< NUM_CHANNELS
; j
++)
940 const_coeff(setup
, slot
, j
);
943 setup
->quad
.prim
= PRIM_POINT
;
945 if (halfSize
<= 0.5 && !round
) {
946 /* special case for 1-pixel points */
947 const int ix
= ((int) x
) & 1;
948 const int iy
= ((int) y
) & 1;
949 setup
->quad
.x0
= (int) x
- ix
;
950 setup
->quad
.y0
= (int) y
- iy
;
951 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
952 clip_emit_quad(setup
);
955 const int ixmin
= block((int) (x
- halfSize
));
956 const int ixmax
= block((int) (x
+ halfSize
));
957 const int iymin
= block((int) (y
- halfSize
));
958 const int iymax
= block((int) (y
+ halfSize
));
963 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
964 const float rmax
= halfSize
+ 0.7071F
;
965 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
966 const float rmax2
= rmax
* rmax
;
967 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
969 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
970 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
971 float dx
, dy
, dist2
, cover
;
973 setup
->quad
.mask
= 0x0;
975 dx
= (ix
+ 0.5f
) - x
;
976 dy
= (iy
+ 0.5f
) - y
;
977 dist2
= dx
* dx
+ dy
* dy
;
978 if (dist2
<= rmax2
) {
979 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
980 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
981 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
984 dx
= (ix
+ 1.5f
) - x
;
985 dy
= (iy
+ 0.5f
) - y
;
986 dist2
= dx
* dx
+ dy
* dy
;
987 if (dist2
<= rmax2
) {
988 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
989 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
990 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
993 dx
= (ix
+ 0.5f
) - x
;
994 dy
= (iy
+ 1.5f
) - y
;
995 dist2
= dx
* dx
+ dy
* dy
;
996 if (dist2
<= rmax2
) {
997 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
998 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
999 setup
->quad
.mask
|= MASK_TOP_LEFT
;
1002 dx
= (ix
+ 1.5f
) - x
;
1003 dy
= (iy
+ 1.5f
) - y
;
1004 dist2
= dx
* dx
+ dy
* dy
;
1005 if (dist2
<= rmax2
) {
1006 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1007 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1008 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
1011 if (setup
->quad
.mask
) {
1012 setup
->quad
.x0
= ix
;
1013 setup
->quad
.y0
= iy
;
1014 clip_emit_quad(setup
);
1021 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1022 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1023 setup
->quad
.mask
= 0xf;
1025 if (ix
+ 0.5 < x
- halfSize
) {
1026 /* fragment is past left edge of point, turn off left bits */
1027 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1030 if (ix
+ 1.5 > x
+ halfSize
) {
1031 /* past the right edge */
1032 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1035 if (iy
+ 0.5 < y
- halfSize
) {
1036 /* below the bottom edge */
1037 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1040 if (iy
+ 1.5 > y
+ halfSize
) {
1041 /* above the top edge */
1042 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1045 if (setup
->quad
.mask
) {
1046 setup
->quad
.x0
= ix
;
1047 setup
->quad
.y0
= iy
;
1048 clip_emit_quad(setup
);
1058 static void setup_begin( struct draw_stage
*stage
)
1060 struct setup_stage
*setup
= setup_stage(stage
);
1061 struct softpipe_context
*sp
= setup
->softpipe
;
1063 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1065 sp
->quad
.first
->begin(sp
->quad
.first
);
1069 static void setup_end( struct draw_stage
*stage
)
1074 static void reset_stipple_counter( struct draw_stage
*stage
)
1076 struct setup_stage
*setup
= setup_stage(stage
);
1077 setup
->softpipe
->line_stipple_counter
= 0;
1082 * Create a new primitive setup/render stage.
1084 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1086 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1088 setup
->softpipe
= softpipe
;
1089 setup
->stage
.draw
= softpipe
->draw
;
1090 setup
->stage
.begin
= setup_begin
;
1091 setup
->stage
.point
= setup_point
;
1092 setup
->stage
.line
= setup_line
;
1093 setup
->stage
.tri
= setup_tri
;
1094 setup
->stage
.end
= setup_end
;
1095 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1097 setup
->quad
.coef
= setup
->coef
;
1099 return &setup
->stage
;