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 */
95 struct pipe_scissor_state scissor
;
101 * Basically a cast wrapper.
103 static inline struct setup_stage
*setup_stage( struct draw_stage
*stage
)
105 return (struct setup_stage
*)stage
;
110 * Clip setup->quad against the scissor/surface bounds.
113 quad_clip(struct setup_stage
*setup
)
115 if (setup
->quad
.x0
>= setup
->scissor
.maxx
||
116 setup
->quad
.y0
>= setup
->scissor
.maxy
||
117 setup
->quad
.x0
+ 1 < setup
->scissor
.minx
||
118 setup
->quad
.y0
+ 1 < setup
->scissor
.miny
) {
119 /* totally clipped */
120 setup
->quad
.mask
= 0x0;
123 if (setup
->quad
.x0
< setup
->scissor
.minx
)
124 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
125 if (setup
->quad
.y0
< setup
->scissor
.miny
)
126 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
127 if (setup
->quad
.x0
== setup
->scissor
.maxx
- 1)
128 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
129 if (setup
->quad
.y0
== setup
->scissor
.maxy
- 1)
130 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
135 * Emit/render a quad.
136 * This passes the quad to the first stage of per-fragment operations.
139 quad_emit(struct setup_stage
*setup
)
142 if (setup
->quad
.mask
) {
143 struct softpipe_context
*sp
= setup
->softpipe
;
144 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
150 * Given an X or Y coordinate, return the block/quad coordinate that it
153 static inline GLint
block( GLint x
)
161 * Run shader on a quad/block.
163 static void run_shader_block( struct setup_stage
*setup
,
164 GLint x
, GLint y
, GLuint mask
)
168 setup
->quad
.mask
= mask
;
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 GLuint
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 GLint 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 run_shader_block( setup
, x
,
235 calculate_mask( setup
, x
) );
240 setup
->span
.y_flags
= 0;
241 setup
->span
.right
[0] = 0;
242 setup
->span
.right
[1] = 0;
246 static GLboolean
setup_sort_vertices( struct setup_stage
*setup
,
247 const struct prim_header
*prim
)
249 const struct vertex_header
*v0
= prim
->v
[0];
250 const struct vertex_header
*v1
= prim
->v
[1];
251 const struct vertex_header
*v2
= prim
->v
[2];
253 setup
->vprovoke
= v2
;
255 /* determine bottom to top order of vertices */
257 GLfloat y0
= v0
->data
[0][1];
258 GLfloat y1
= v1
->data
[0][1];
259 GLfloat y2
= v2
->data
[0][1];
302 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
303 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
304 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
305 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
306 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
307 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
310 * Compute triangle's area. Use 1/area to compute partial
311 * derivatives of attributes later.
313 * The area will be the same as prim->det, but the sign may be
314 * different depending on how the vertices get sorted above.
316 * To determine whether the primitive is front or back facing we
317 * use the prim->det value because its sign is correct.
320 const GLfloat area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
321 setup
->ebot
.dx
* setup
->emaj
.dy
);
323 setup
->oneoverarea
= 1.0 / area
;
325 _mesa_printf("%s one-over-area %f area %f det %f\n",
326 __FUNCTION__, setup->oneoverarea, area, prim->det );
330 /* We need to know if this is a front or back-facing triangle for:
331 * - the GLSL gl_FrontFacing fragment attribute (bool)
332 * - two-sided stencil test
334 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->setup
.front_winding
== PIPE_WINDING_CW
);
341 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
342 * The value value comes from vertex->data[slot][i].
343 * The result will be put into setup->coef[slot].a0[i].
344 * \param slot which attribute slot
345 * \param i which component of the slot (0..3)
347 static void const_coeff( struct setup_stage
*setup
,
351 assert(slot
< FRAG_ATTRIB_MAX
);
354 setup
->coef
[slot
].dadx
[i
] = 0;
355 setup
->coef
[slot
].dady
[i
] = 0;
357 /* need provoking vertex info!
359 setup
->coef
[slot
].a0
[i
] = setup
->vprovoke
->data
[slot
][i
];
364 * Compute a0, dadx and dady for a linearly interpolated coefficient,
367 static void tri_linear_coeff( struct setup_stage
*setup
,
371 GLfloat botda
= setup
->vmid
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
372 GLfloat majda
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
373 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
374 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
376 assert(slot
< FRAG_ATTRIB_MAX
);
379 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
380 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
382 /* calculate a0 as the value which would be sampled for the
383 * fragment at (0,0), taking into account that we want to sample at
384 * pixel centers, in other words (0.5, 0.5).
386 * this is neat but unfortunately not a good way to do things for
387 * triangles with very large values of dadx or dady as it will
388 * result in the subtraction and re-addition from a0 of a very
389 * large number, which means we'll end up loosing a lot of the
390 * fractional bits and precision from a0. the way to fix this is
391 * to define a0 as the sample at a pixel center somewhere near vmin
392 * instead - i'll switch to this later.
394 setup
->coef
[slot
].a0
[i
] = (setup
->vmin
->data
[slot
][i
] -
395 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
396 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
399 _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
401 setup->coef[slot].a0[i],
402 setup->coef[slot].dadx[i],
403 setup->coef[slot].dady[i]);
409 * Compute a0, dadx and dady for a perspective-corrected interpolant,
412 static void tri_persp_coeff( struct setup_stage
*setup
,
416 /* premultiply by 1/w:
418 GLfloat mina
= setup
->vmin
->data
[slot
][i
] * setup
->vmin
->data
[0][3];
419 GLfloat mida
= setup
->vmid
->data
[slot
][i
] * setup
->vmid
->data
[0][3];
420 GLfloat maxa
= setup
->vmax
->data
[slot
][i
] * setup
->vmax
->data
[0][3];
422 GLfloat botda
= mida
- mina
;
423 GLfloat majda
= maxa
- mina
;
424 GLfloat a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
425 GLfloat b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
427 assert(slot
< FRAG_ATTRIB_MAX
);
430 setup
->coef
[slot
].dadx
[i
] = a
* setup
->oneoverarea
;
431 setup
->coef
[slot
].dady
[i
] = b
* setup
->oneoverarea
;
432 setup
->coef
[slot
].a0
[i
] = (mina
-
433 (setup
->coef
[slot
].dadx
[i
] * (setup
->vmin
->data
[0][0] - 0.5) +
434 setup
->coef
[slot
].dady
[i
] * (setup
->vmin
->data
[0][1] - 0.5)));
440 * Compute the setup->coef[] array dadx, dady, a0 values.
441 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
443 static void setup_tri_coefficients( struct setup_stage
*setup
)
445 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
448 /* z and w are done by linear interpolation:
450 tri_linear_coeff(setup
, 0, 2);
451 tri_linear_coeff(setup
, 0, 3);
453 /* setup interpolation for all the remaining attributes:
455 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
456 switch (interp
[slot
]) {
457 case INTERP_CONSTANT
:
458 for (j
= 0; j
< NUM_CHANNELS
; j
++)
459 const_coeff(setup
, slot
, j
);
463 for (j
= 0; j
< NUM_CHANNELS
; j
++)
464 tri_linear_coeff(setup
, slot
, j
);
467 case INTERP_PERSPECTIVE
:
468 for (j
= 0; j
< NUM_CHANNELS
; j
++)
469 tri_persp_coeff(setup
, slot
, j
);
477 static void setup_tri_edges( struct setup_stage
*setup
)
479 GLfloat vmin_x
= setup
->vmin
->data
[0][0] + 0.5;
480 GLfloat vmid_x
= setup
->vmid
->data
[0][0] + 0.5;
482 GLfloat vmin_y
= setup
->vmin
->data
[0][1] - 0.5;
483 GLfloat vmid_y
= setup
->vmid
->data
[0][1] - 0.5;
484 GLfloat vmax_y
= setup
->vmax
->data
[0][1] - 0.5;
486 setup
->emaj
.sy
= ceilf(vmin_y
);
487 setup
->emaj
.lines
= (GLint
) ceilf(vmax_y
- setup
->emaj
.sy
);
488 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
489 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
491 setup
->etop
.sy
= ceilf(vmid_y
);
492 setup
->etop
.lines
= (GLint
) ceilf(vmax_y
- setup
->etop
.sy
);
493 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
494 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
496 setup
->ebot
.sy
= ceilf(vmin_y
);
497 setup
->ebot
.lines
= (GLint
) ceilf(vmid_y
- setup
->ebot
.sy
);
498 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
499 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
504 * Render the upper or lower half of a triangle.
505 * Scissoring is applied here too.
507 static void subtriangle( struct setup_stage
*setup
,
512 GLint y
, start_y
, finish_y
;
513 GLint sy
= (GLint
)eleft
->sy
;
515 assert((GLint
)eleft
->sy
== (GLint
) eright
->sy
);
516 assert((GLint
)eleft
->sy
>= 0); /* catch bug in x64? */
520 if (setup
->softpipe
->setup
.scissor
) {
522 finish_y
= start_y
+ lines
;
524 if (start_y
< setup
->softpipe
->scissor
.miny
)
525 start_y
= setup
->softpipe
->scissor
.miny
;
527 if (finish_y
> setup
->softpipe
->scissor
.maxy
)
528 finish_y
= setup
->softpipe
->scissor
.maxy
;
539 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
542 for (y
= start_y
; y
< finish_y
; y
++) {
544 /* avoid accumulating adds as floats don't have the precision to
545 * accurately iterate large triangle edges that way. luckily we
546 * can just multiply these days.
548 * this is all drowned out by the attribute interpolation anyway.
550 GLint left
= (GLint
)(eleft
->sx
+ y
* eleft
->dxdy
);
551 GLint right
= (GLint
)(eright
->sx
+ y
* eright
->dxdy
);
555 if (setup
->softpipe
->setup
.scissor
) {
556 if (left
< setup
->softpipe
->scissor
.minx
)
557 left
= setup
->softpipe
->scissor
.minx
;
559 if (right
> setup
->softpipe
->scissor
.maxx
)
560 right
= setup
->softpipe
->scissor
.maxx
;
565 if (block(_y
) != setup
->span
.y
) {
567 setup
->span
.y
= block(_y
);
570 setup
->span
.left
[_y
&1] = left
;
571 setup
->span
.right
[_y
&1] = right
;
572 setup
->span
.y_flags
|= 1<<(_y
&1);
577 /* save the values so that emaj can be restarted:
579 eleft
->sx
+= lines
* eleft
->dxdy
;
580 eright
->sx
+= lines
* eright
->dxdy
;
587 * Do setup for triangle rasterization, then render the triangle.
589 static void setup_tri( struct draw_stage
*stage
,
590 struct prim_header
*prim
)
592 struct setup_stage
*setup
= setup_stage( stage
);
595 _mesa_printf("%s\n", __FUNCTION__ );
598 setup_sort_vertices( setup
, prim
);
599 setup_tri_coefficients( setup
);
600 setup_tri_edges( setup
);
602 setup
->quad
.prim
= PRIM_TRI
;
605 setup
->span
.y_flags
= 0;
606 setup
->span
.right
[0] = 0;
607 setup
->span
.right
[1] = 0;
608 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
610 /* init_constant_attribs( setup ); */
612 if (setup
->oneoverarea
< 0.0) {
615 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
616 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
621 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
622 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
625 flush_spans( setup
);
631 * Compute a0, dadx and dady for a linearly interpolated coefficient,
635 line_linear_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
637 const GLfloat dz
= setup
->vmax
->data
[slot
][i
] - setup
->vmin
->data
[slot
][i
];
638 const GLfloat dadx
= dz
* setup
->emaj
.dx
* setup
->oneoverarea
;
639 const GLfloat dady
= dz
* setup
->emaj
.dy
* setup
->oneoverarea
;
640 setup
->coef
[slot
].dadx
[i
] = dadx
;
641 setup
->coef
[slot
].dady
[i
] = dady
;
642 setup
->coef
[slot
].a0
[i
]
643 = (setup
->vmin
->data
[slot
][i
] -
644 (dadx
* (setup
->vmin
->data
[0][0] - 0.5) +
645 dady
* (setup
->vmin
->data
[0][1] - 0.5)));
650 * Compute a0, dadx and dady for a perspective-corrected interpolant,
654 line_persp_coeff(struct setup_stage
*setup
, GLuint slot
, GLuint i
)
657 line_linear_coeff(setup
, slot
, i
); /* XXX temporary */
662 * Compute the setup->coef[] array dadx, dady, a0 values.
663 * Must be called after setup->vmin,vmax are initialized.
666 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
668 const enum interp_mode
*interp
= setup
->softpipe
->interp
;
671 /* use setup->vmin, vmax to point to vertices */
672 setup
->vprovoke
= prim
->v
[1];
673 setup
->vmin
= prim
->v
[0];
674 setup
->vmax
= prim
->v
[1];
676 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
677 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
678 /* NOTE: this is not really 1/area */
679 setup
->oneoverarea
= 1.0 / (setup
->emaj
.dx
* setup
->emaj
.dx
+
680 setup
->emaj
.dy
* setup
->emaj
.dy
);
682 /* z and w are done by linear interpolation:
684 line_linear_coeff(setup
, 0, 2);
685 line_linear_coeff(setup
, 0, 3);
687 /* setup interpolation for all the remaining attributes:
689 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
690 switch (interp
[slot
]) {
691 case INTERP_CONSTANT
:
692 for (j
= 0; j
< NUM_CHANNELS
; j
++)
693 const_coeff(setup
, slot
, j
);
697 for (j
= 0; j
< NUM_CHANNELS
; j
++)
698 line_linear_coeff(setup
, slot
, j
);
701 case INTERP_PERSPECTIVE
:
702 for (j
= 0; j
< NUM_CHANNELS
; j
++)
703 line_persp_coeff(setup
, slot
, j
);
711 * Plot a pixel in a line segment.
714 plot(struct setup_stage
*setup
, GLint x
, GLint y
)
716 const GLint iy
= y
& 1;
717 const GLint ix
= x
& 1;
718 const GLint quadX
= x
- ix
;
719 const GLint quadY
= y
- iy
;
720 const GLint mask
= (1 << ix
) << (2 * iy
);
722 if (quadX
!= setup
->quad
.x0
||
723 quadY
!= setup
->quad
.y0
)
725 /* flush prev quad, start new quad */
727 if (setup
->quad
.x0
!= -1)
730 setup
->quad
.x0
= quadX
;
731 setup
->quad
.y0
= quadY
;
732 setup
->quad
.mask
= 0x0;
735 setup
->quad
.mask
|= mask
;
740 * Determine whether or not to emit a line fragment by checking
741 * line stipple pattern.
744 stipple_test(GLint counter
, GLushort pattern
, GLint factor
)
746 GLint b
= (counter
/ factor
) & 0xf;
747 return (1 << b
) & pattern
;
752 * Do setup for line rasterization, then render the line.
753 * XXX single-pixel width, no stipple, etc
754 * XXX no scissoring yet.
757 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
759 const struct vertex_header
*v0
= prim
->v
[0];
760 const struct vertex_header
*v1
= prim
->v
[1];
761 struct setup_stage
*setup
= setup_stage( stage
);
762 struct softpipe_context
*sp
= setup
->softpipe
;
764 GLint x0
= (GLint
) v0
->data
[0][0];
765 GLint x1
= (GLint
) v1
->data
[0][0];
766 GLint y0
= (GLint
) v0
->data
[0][1];
767 GLint y1
= (GLint
) v1
->data
[0][1];
772 if (dx
== 0 && dy
== 0)
775 setup_line_coefficients(setup
, prim
);
778 dx
= -dx
; /* make positive */
786 dy
= -dy
; /* make positive */
796 setup
->quad
.x0
= setup
->quad
.y0
= -1;
797 setup
->quad
.mask
= 0x0;
798 setup
->quad
.prim
= PRIM_LINE
;
799 /* XXX temporary: set coverage to 1.0 so the line appears
800 * if AA mode happens to be enabled.
802 setup
->quad
.coverage
[0] =
803 setup
->quad
.coverage
[1] =
804 setup
->quad
.coverage
[2] =
805 setup
->quad
.coverage
[3] = 1.0;
808 /*** X-major line ***/
810 const GLint errorInc
= dy
+ dy
;
811 GLint error
= errorInc
- dx
;
812 const GLint errorDec
= error
- dx
;
814 for (i
= 0; i
< dx
; i
++) {
815 if (!sp
->setup
.line_stipple_enable
||
816 stipple_test(sp
->line_stipple_counter
,
817 sp
->setup
.line_stipple_pattern
,
818 sp
->setup
.line_stipple_factor
+ 1)) {
831 sp
->line_stipple_counter
++;
835 /*** Y-major line ***/
837 const GLint errorInc
= dx
+ dx
;
838 GLint error
= errorInc
- dy
;
839 const GLint errorDec
= error
- dy
;
841 for (i
= 0; i
< dy
; i
++) {
842 if (!sp
->setup
.line_stipple_enable
||
843 stipple_test(sp
->line_stipple_counter
,
844 sp
->setup
.line_stipple_pattern
,
845 sp
->setup
.line_stipple_factor
+ 1)) {
859 sp
->line_stipple_counter
++;
863 /* draw final quad */
864 if (setup
->quad
.mask
) {
871 * Do setup for point rasterization, then render the point.
872 * Round or square points...
873 * XXX could optimize a lot for 1-pixel points.
876 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
878 struct setup_stage
*setup
= setup_stage( stage
);
879 /*XXX this should be a vertex attrib! */
880 GLfloat halfSize
= 0.5 * setup
->softpipe
->setup
.point_size
;
881 GLboolean round
= setup
->softpipe
->setup
.point_smooth
;
882 const struct vertex_header
*v0
= prim
->v
[0];
883 const GLfloat x
= v0
->data
[FRAG_ATTRIB_WPOS
][0];
884 const GLfloat y
= v0
->data
[FRAG_ATTRIB_WPOS
][1];
887 /* For points, all interpolants are constant-valued.
888 * However, for point sprites, we'll need to setup texcoords appropriately.
889 * XXX: which coefficients are the texcoords???
890 * We may do point sprites as textured quads...
892 * KW: We don't know which coefficients are texcoords - ultimately
893 * the choice of what interpolation mode to use for each attribute
894 * should be determined by the fragment program, using
895 * per-attribute declaration statements that include interpolation
896 * mode as a parameter. So either the fragment program will have
897 * to be adjusted for pointsprite vs normal point behaviour, or
898 * otherwise a special interpolation mode will have to be defined
899 * which matches the required behaviour for point sprites. But -
900 * the latter is not a feature of normal hardware, and as such
901 * probably should be ruled out on that basis.
903 setup
->vprovoke
= prim
->v
[0];
904 const_coeff(setup
, 0, 2);
905 const_coeff(setup
, 0, 3);
906 for (slot
= 1; slot
< setup
->quad
.nr_attrs
; slot
++) {
907 for (j
= 0; j
< NUM_CHANNELS
; j
++)
908 const_coeff(setup
, slot
, j
);
911 setup
->quad
.prim
= PRIM_POINT
;
913 /* XXX need to clip against scissor bounds too */
915 if (halfSize
<= 0.5 && !round
) {
916 /* special case for 1-pixel points */
917 const GLint ix
= ((GLint
) x
) & 1;
918 const GLint iy
= ((GLint
) y
) & 1;
919 setup
->quad
.x0
= x
- ix
;
920 setup
->quad
.y0
= y
- iy
;
921 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
925 const GLint ixmin
= block((GLint
) (x
- halfSize
));
926 const GLint ixmax
= block((GLint
) (x
+ halfSize
));
927 const GLint iymin
= block((GLint
) (y
- halfSize
));
928 const GLint iymax
= block((GLint
) (y
+ halfSize
));
933 const GLfloat rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
934 const GLfloat rmax
= halfSize
+ 0.7071F
;
935 const GLfloat rmin2
= MAX2(0.0F
, rmin
* rmin
);
936 const GLfloat rmax2
= rmax
* rmax
;
937 const GLfloat cscale
= 1.0F
/ (rmax2
- rmin2
);
939 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
940 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
941 GLfloat dx
, dy
, dist2
, cover
;
943 setup
->quad
.mask
= 0x0;
947 dist2
= dx
* dx
+ dy
* dy
;
948 if (dist2
<= rmax2
) {
949 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
950 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0);
951 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
956 dist2
= dx
* dx
+ dy
* dy
;
957 if (dist2
<= rmax2
) {
958 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
959 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0);
960 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
965 dist2
= dx
* dx
+ dy
* dy
;
966 if (dist2
<= rmax2
) {
967 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
968 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0);
969 setup
->quad
.mask
|= MASK_TOP_LEFT
;
974 dist2
= dx
* dx
+ dy
* dy
;
975 if (dist2
<= rmax2
) {
976 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
977 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0);
978 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
981 if (setup
->quad
.mask
) {
991 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
992 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
993 setup
->quad
.mask
= 0xf;
995 if (ix
+ 0.5 < x
- halfSize
) {
996 /* fragment is past left edge of point, turn off left bits */
997 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1000 if (ix
+ 1.5 > x
+ halfSize
) {
1001 /* past the right edge */
1002 setup
->quad
.mask
&= ~(MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1005 if (iy
+ 0.5 < y
- halfSize
) {
1006 /* below the bottom edge */
1007 setup
->quad
.mask
&= ~(MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1010 if (iy
+ 1.5 > y
+ halfSize
) {
1011 /* above the top edge */
1012 setup
->quad
.mask
&= ~(MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1015 if (setup
->quad
.mask
) {
1016 setup
->quad
.x0
= ix
;
1017 setup
->quad
.y0
= iy
;
1028 static void setup_begin( struct draw_stage
*stage
)
1030 struct setup_stage
*setup
= setup_stage(stage
);
1032 setup
->quad
.nr_attrs
= setup
->softpipe
->nr_frag_attrs
;
1034 /* compute scissor/drawing bounds.
1035 * XXX we should probably move this into the sp_state_derived.c file
1036 * and only compute when scissor or setup state changes.
1039 const struct softpipe_context
*sp
= setup
->softpipe
;
1040 const struct pipe_surface
*surf
= sp
->cbuf
;
1041 if (sp
->setup
.scissor
) {
1042 setup
->scissor
.minx
= MAX2(sp
->scissor
.minx
, 0);
1043 setup
->scissor
.miny
= MAX2(sp
->scissor
.miny
, 0);
1044 setup
->scissor
.maxx
= MIN2(sp
->scissor
.maxx
, surf
->width
- 1);
1045 setup
->scissor
.maxy
= MIN2(sp
->scissor
.maxy
, surf
->height
- 1);
1048 setup
->scissor
.minx
= 0;
1049 setup
->scissor
.miny
= 0;
1050 setup
->scissor
.maxx
= surf
->width
- 1;
1051 setup
->scissor
.maxy
= surf
->height
- 1;
1056 * XXX this is where we might map() the renderbuffers to begin
1062 static void setup_end( struct draw_stage
*stage
)
1065 * XXX this is where we might unmap() the renderbuffers after
1071 static void reset_stipple_counter( struct draw_stage
*stage
)
1073 struct setup_stage
*setup
= setup_stage(stage
);
1074 setup
->softpipe
->line_stipple_counter
= 0;
1079 * Create a new primitive setup/render stage.
1081 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1083 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1085 setup
->softpipe
= softpipe
;
1086 setup
->stage
.draw
= softpipe
->draw
;
1087 setup
->stage
.begin
= setup_begin
;
1088 setup
->stage
.point
= setup_point
;
1089 setup
->stage
.line
= setup_line
;
1090 setup
->stage
.tri
= setup_tri
;
1091 setup
->stage
.end
= setup_end
;
1092 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1094 setup
->quad
.coef
= setup
->coef
;
1096 return &setup
->stage
;