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>
35 #include "sp_context.h"
37 #include "sp_quad_pipe.h"
40 #include "draw/draw_context.h"
41 #include "draw/draw_vertex.h"
42 #include "pipe/p_shader_tokens.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
54 float dx
; /**< X(v1) - X(v0), used only during setup */
55 float dy
; /**< Y(v1) - Y(v0), used only during setup */
56 float dxdy
; /**< dx/dy */
57 float sx
, sy
; /**< first sample point coord */
58 int lines
; /**< number of lines on this edge */
66 * Triangle setup info (derived from draw_stage).
67 * Also used for line drawing (taking some liberties).
69 struct setup_context
{
70 struct softpipe_context
*softpipe
;
72 /* Vertices are just an array of floats making up each attribute in
73 * turn. Currently fixed at 4 floats, but should change in time.
74 * Codegen will help cope with this.
76 const float (*vmax
)[4];
77 const float (*vmid
)[4];
78 const float (*vmin
)[4];
79 const float (*vprovoke
)[4];
90 struct quad_header quad
[MAX_QUADS
];
91 struct quad_header
*quad_ptrs
[MAX_QUADS
];
94 struct tgsi_interp_coef coef
[PIPE_MAX_SHADER_INPUTS
];
95 struct tgsi_interp_coef posCoef
; /* For Z, W */
98 int left
[2]; /**< [0] = row0, [1] = row1 */
104 uint numFragsEmitted
; /**< per primitive */
105 uint numFragsWritten
; /**< per primitive */
108 unsigned winding
; /* which winding to cull */
109 unsigned nr_vertex_attrs
;
116 * Do triangle cull test using tri determinant (sign indicates orientation)
117 * \return true if triangle is to be culled.
119 static INLINE boolean
120 cull_tri(const struct setup_context
*setup
, float det
)
123 /* if (det < 0 then Z points toward camera and triangle is
124 * counter-clockwise winding.
126 unsigned winding
= (det
< 0) ? PIPE_WINDING_CCW
: PIPE_WINDING_CW
;
128 if ((winding
& setup
->winding
) == 0)
140 * Clip setup->quad against the scissor/surface bounds.
143 quad_clip( struct setup_context
*setup
, struct quad_header
*quad
)
145 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
146 const int minx
= (int) cliprect
->minx
;
147 const int maxx
= (int) cliprect
->maxx
;
148 const int miny
= (int) cliprect
->miny
;
149 const int maxy
= (int) cliprect
->maxy
;
151 if (quad
->input
.x0
>= maxx
||
152 quad
->input
.y0
>= maxy
||
153 quad
->input
.x0
+ 1 < minx
||
154 quad
->input
.y0
+ 1 < miny
) {
155 /* totally clipped */
156 quad
->inout
.mask
= 0x0;
159 if (quad
->input
.x0
< minx
)
160 quad
->inout
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
161 if (quad
->input
.y0
< miny
)
162 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
163 if (quad
->input
.x0
== maxx
- 1)
164 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
165 if (quad
->input
.y0
== maxy
- 1)
166 quad
->inout
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
171 * Emit a quad (pass to next stage) with clipping.
174 clip_emit_quad( struct setup_context
*setup
, struct quad_header
*quad
)
176 quad_clip( setup
, quad
);
178 if (quad
->inout
.mask
) {
179 struct softpipe_context
*sp
= setup
->softpipe
;
181 sp
->quad
.first
->run( sp
->quad
.first
, &quad
, 1 );
188 * Given an X or Y coordinate, return the block/quad coordinate that it
191 static INLINE
int block( int x
)
196 static INLINE
int block_x( int x
)
203 * Render a horizontal span of quads
205 static void flush_spans( struct setup_context
*setup
)
208 const int xleft0
= setup
->span
.left
[0];
209 const int xleft1
= setup
->span
.left
[1];
210 const int xright0
= setup
->span
.right
[0];
211 const int xright1
= setup
->span
.right
[1];
212 struct quad_stage
*pipe
= setup
->softpipe
->quad
.first
;
215 int minleft
= block_x(MIN2(xleft0
, xleft1
));
216 int maxright
= MAX2(xright0
, xright1
);
219 for (x
= minleft
; x
< maxright
; x
+= step
) {
220 unsigned skip_left0
= CLAMP(xleft0
- x
, 0, step
);
221 unsigned skip_left1
= CLAMP(xleft1
- x
, 0, step
);
222 unsigned skip_right0
= CLAMP(x
+ step
- xright0
, 0, step
);
223 unsigned skip_right1
= CLAMP(x
+ step
- xright1
, 0, step
);
227 unsigned skipmask_left0
= (1U << skip_left0
) - 1U;
228 unsigned skipmask_left1
= (1U << skip_left1
) - 1U;
230 /* These calculations fail when step == 32 and skip_right == 0.
232 unsigned skipmask_right0
= ~0U << (unsigned)(step
- skip_right0
);
233 unsigned skipmask_right1
= ~0U << (unsigned)(step
- skip_right1
);
235 unsigned mask0
= ~skipmask_left0
& ~skipmask_right0
;
236 unsigned mask1
= ~skipmask_left1
& ~skipmask_right1
;
240 unsigned quadmask
= (mask0
& 3) | ((mask1
& 3) << 2);
242 setup
->quad
[q
].input
.x0
= lx
;
243 setup
->quad
[q
].input
.y0
= setup
->span
.y
;
244 setup
->quad
[q
].input
.facing
= setup
->facing
;
245 setup
->quad
[q
].inout
.mask
= quadmask
;
246 setup
->quad_ptrs
[q
] = &setup
->quad
[q
];
252 } while (mask0
| mask1
);
254 pipe
->run( pipe
, setup
->quad_ptrs
, q
);
260 setup
->span
.right
[0] = 0;
261 setup
->span
.right
[1] = 0;
262 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
263 setup
->span
.left
[1] = 1000000; /* greater than right[1] */
268 static void print_vertex(const struct setup_context
*setup
,
272 debug_printf(" Vertex: (%p)\n", (void *) v
);
273 for (i
= 0; i
< setup
->nr_vertex_attrs
; i
++) {
274 debug_printf(" %d: %f %f %f %f\n", i
,
275 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
276 if (util_is_inf_or_nan(v
[i
][0])) {
277 debug_printf(" NaN!\n");
284 * Sort the vertices from top to bottom order, setting up the triangle
285 * edge fields (ebot, emaj, etop).
286 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
288 static boolean
setup_sort_vertices( struct setup_context
*setup
,
290 const float (*v0
)[4],
291 const float (*v1
)[4],
292 const float (*v2
)[4] )
294 setup
->vprovoke
= v2
;
296 /* determine bottom to top order of vertices */
343 setup
->ebot
.dx
= setup
->vmid
[0][0] - setup
->vmin
[0][0];
344 setup
->ebot
.dy
= setup
->vmid
[0][1] - setup
->vmin
[0][1];
345 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
346 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
347 setup
->etop
.dx
= setup
->vmax
[0][0] - setup
->vmid
[0][0];
348 setup
->etop
.dy
= setup
->vmax
[0][1] - setup
->vmid
[0][1];
351 * Compute triangle's area. Use 1/area to compute partial
352 * derivatives of attributes later.
354 * The area will be the same as prim->det, but the sign may be
355 * different depending on how the vertices get sorted above.
357 * To determine whether the primitive is front or back facing we
358 * use the prim->det value because its sign is correct.
361 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
362 setup
->ebot
.dx
* setup
->emaj
.dy
);
364 setup
->oneoverarea
= 1.0f
/ area
;
367 debug_printf("%s one-over-area %f area %f det %f\n",
368 __FUNCTION__, setup->oneoverarea, area, det );
370 if (util_is_inf_or_nan(setup
->oneoverarea
))
374 /* We need to know if this is a front or back-facing triangle for:
375 * - the GLSL gl_FrontFacing fragment attribute (bool)
376 * - two-sided stencil test
380 (setup
->softpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
));
382 /* Prepare pixel offset for rasterisation:
383 * - pixel center (0.5, 0.5) for GL, or
384 * - assume (0.0, 0.0) for other APIs.
386 if (setup
->softpipe
->rasterizer
->gl_rasterization_rules
) {
387 setup
->pixel_offset
= 0.5f
;
389 setup
->pixel_offset
= 0.0f
;
396 /* Apply cylindrical wrapping to v0, v1, v2 coordinates, if enabled.
397 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
398 * Some combinations of coordinates produce invalid results,
399 * but this behaviour is acceptable.
402 tri_apply_cylindrical_wrap(float v0
,
405 uint cylindrical_wrap
,
408 if (cylindrical_wrap
) {
415 else if (delta
< -0.5f
) {
423 else if (delta
< -0.5f
) {
431 else if (delta
< -0.5f
) {
443 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
444 * The value value comes from vertex[slot][i].
445 * The result will be put into setup->coef[slot].a0[i].
446 * \param slot which attribute slot
447 * \param i which component of the slot (0..3)
449 static void const_coeff( struct setup_context
*setup
,
450 struct tgsi_interp_coef
*coef
,
451 uint vertSlot
, uint i
)
458 /* need provoking vertex info!
460 coef
->a0
[i
] = setup
->vprovoke
[vertSlot
][i
];
465 * Compute a0, dadx and dady for a linearly interpolated coefficient,
467 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
470 tri_linear_coeff(struct setup_context
*setup
,
471 struct tgsi_interp_coef
*coef
,
475 float botda
= v
[1] - v
[0];
476 float majda
= v
[2] - v
[0];
477 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
478 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
479 float dadx
= a
* setup
->oneoverarea
;
480 float dady
= b
* setup
->oneoverarea
;
484 coef
->dadx
[i
] = dadx
;
485 coef
->dady
[i
] = dady
;
487 /* calculate a0 as the value which would be sampled for the
488 * fragment at (0,0), taking into account that we want to sample at
489 * pixel centers, in other words (pixel_offset, pixel_offset).
491 * this is neat but unfortunately not a good way to do things for
492 * triangles with very large values of dadx or dady as it will
493 * result in the subtraction and re-addition from a0 of a very
494 * large number, which means we'll end up loosing a lot of the
495 * fractional bits and precision from a0. the way to fix this is
496 * to define a0 as the sample at a pixel center somewhere near vmin
497 * instead - i'll switch to this later.
499 coef
->a0
[i
] = (v
[0] -
500 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
501 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
504 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
506 setup->coef[slot].a0[i],
507 setup->coef[slot].dadx[i],
508 setup->coef[slot].dady[i]);
514 * Compute a0, dadx and dady for a perspective-corrected interpolant,
516 * We basically multiply the vertex value by 1/w before computing
517 * the plane coefficients (a0, dadx, dady).
518 * Later, when we compute the value at a particular fragment position we'll
519 * divide the interpolated value by the interpolated W at that fragment.
520 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
523 tri_persp_coeff(struct setup_context
*setup
,
524 struct tgsi_interp_coef
*coef
,
528 /* premultiply by 1/w (v[0][3] is always W):
530 float mina
= v
[0] * setup
->vmin
[0][3];
531 float mida
= v
[1] * setup
->vmid
[0][3];
532 float maxa
= v
[2] * setup
->vmax
[0][3];
533 float botda
= mida
- mina
;
534 float majda
= maxa
- mina
;
535 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
536 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
537 float dadx
= a
* setup
->oneoverarea
;
538 float dady
= b
* setup
->oneoverarea
;
541 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
542 setup->vmin[vertSlot][i],
543 setup->vmid[vertSlot][i],
544 setup->vmax[vertSlot][i]
549 coef
->dadx
[i
] = dadx
;
550 coef
->dady
[i
] = dady
;
551 coef
->a0
[i
] = (mina
-
552 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
553 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
558 * Special coefficient setup for gl_FragCoord.
559 * X and Y are trivial, though Y may have to be inverted for OpenGL.
560 * Z and W are copied from posCoef which should have already been computed.
561 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
564 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
566 struct sp_fragment_shader
* spfs
= setup
->softpipe
->fs
;
568 setup
->coef
[slot
].a0
[0] = spfs
->pixel_center_integer
? 0.0 : 0.5;
569 setup
->coef
[slot
].dadx
[0] = 1.0;
570 setup
->coef
[slot
].dady
[0] = 0.0;
572 setup
->coef
[slot
].a0
[1] =
573 (spfs
->origin_lower_left
? setup
->softpipe
->framebuffer
.height
: 0)
574 + (spfs
->pixel_center_integer
? 0.0 : 0.5);
575 setup
->coef
[slot
].dadx
[1] = 0.0;
576 setup
->coef
[slot
].dady
[1] = spfs
->origin_lower_left
? -1.0 : 1.0;
578 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
579 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
580 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
582 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
583 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
584 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
590 * Compute the setup->coef[] array dadx, dady, a0 values.
591 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
593 static void setup_tri_coefficients( struct setup_context
*setup
)
595 struct softpipe_context
*softpipe
= setup
->softpipe
;
596 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
597 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
601 /* z and w are done by linear interpolation:
603 v
[0] = setup
->vmin
[0][2];
604 v
[1] = setup
->vmid
[0][2];
605 v
[2] = setup
->vmax
[0][2];
606 tri_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
608 v
[0] = setup
->vmin
[0][3];
609 v
[1] = setup
->vmid
[0][3];
610 v
[2] = setup
->vmax
[0][3];
611 tri_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
613 /* setup interpolation for all the remaining attributes:
615 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
616 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
619 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
620 case INTERP_CONSTANT
:
621 for (j
= 0; j
< NUM_CHANNELS
; j
++)
622 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
625 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
626 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
627 setup
->vmid
[vertSlot
][j
],
628 setup
->vmax
[vertSlot
][j
],
629 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
631 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
634 case INTERP_PERSPECTIVE
:
635 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
636 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
637 setup
->vmid
[vertSlot
][j
],
638 setup
->vmax
[vertSlot
][j
],
639 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
641 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
645 setup_fragcoord_coeff(setup
, fragSlot
);
651 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
652 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
653 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
654 setup
->coef
[fragSlot
].dady
[0] = 0.0;
661 static void setup_tri_edges( struct setup_context
*setup
)
663 float vmin_x
= setup
->vmin
[0][0] + setup
->pixel_offset
;
664 float vmid_x
= setup
->vmid
[0][0] + setup
->pixel_offset
;
666 float vmin_y
= setup
->vmin
[0][1] - setup
->pixel_offset
;
667 float vmid_y
= setup
->vmid
[0][1] - setup
->pixel_offset
;
668 float vmax_y
= setup
->vmax
[0][1] - setup
->pixel_offset
;
670 setup
->emaj
.sy
= ceilf(vmin_y
);
671 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
672 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
673 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
675 setup
->etop
.sy
= ceilf(vmid_y
);
676 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
677 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
678 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
680 setup
->ebot
.sy
= ceilf(vmin_y
);
681 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
682 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
683 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
688 * Render the upper or lower half of a triangle.
689 * Scissoring/cliprect is applied here too.
691 static void subtriangle( struct setup_context
*setup
,
696 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
697 const int minx
= (int) cliprect
->minx
;
698 const int maxx
= (int) cliprect
->maxx
;
699 const int miny
= (int) cliprect
->miny
;
700 const int maxy
= (int) cliprect
->maxy
;
701 int y
, start_y
, finish_y
;
702 int sy
= (int)eleft
->sy
;
704 assert((int)eleft
->sy
== (int) eright
->sy
);
706 /* clip top/bottom */
711 finish_y
= sy
+ lines
;
719 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
722 for (y
= start_y
; y
< finish_y
; y
++) {
724 /* avoid accumulating adds as floats don't have the precision to
725 * accurately iterate large triangle edges that way. luckily we
726 * can just multiply these days.
728 * this is all drowned out by the attribute interpolation anyway.
730 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
731 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
733 /* clip left/right */
741 if (block(_y
) != setup
->span
.y
) {
743 setup
->span
.y
= block(_y
);
746 setup
->span
.left
[_y
&1] = left
;
747 setup
->span
.right
[_y
&1] = right
;
752 /* save the values so that emaj can be restarted:
754 eleft
->sx
+= lines
* eleft
->dxdy
;
755 eright
->sx
+= lines
* eright
->dxdy
;
762 * Recalculate prim's determinant. This is needed as we don't have
763 * get this information through the vbuf_render interface & we must
767 calc_det( const float (*v0
)[4],
768 const float (*v1
)[4],
769 const float (*v2
)[4] )
771 /* edge vectors e = v0 - v2, f = v1 - v2 */
772 const float ex
= v0
[0][0] - v2
[0][0];
773 const float ey
= v0
[0][1] - v2
[0][1];
774 const float fx
= v1
[0][0] - v2
[0][0];
775 const float fy
= v1
[0][1] - v2
[0][1];
777 /* det = cross(e,f).z */
778 return ex
* fy
- ey
* fx
;
783 * Do setup for triangle rasterization, then render the triangle.
785 void sp_setup_tri( struct setup_context
*setup
,
786 const float (*v0
)[4],
787 const float (*v1
)[4],
788 const float (*v2
)[4] )
793 debug_printf("Setup triangle:\n");
794 print_vertex(setup
, v0
);
795 print_vertex(setup
, v1
);
796 print_vertex(setup
, v2
);
799 if (setup
->softpipe
->no_rast
)
802 det
= calc_det(v0
, v1
, v2
);
804 debug_printf("%s\n", __FUNCTION__ );
808 setup
->numFragsEmitted
= 0;
809 setup
->numFragsWritten
= 0;
812 if (cull_tri( setup
, det
))
815 if (!setup_sort_vertices( setup
, det
, v0
, v1
, v2
))
817 setup_tri_coefficients( setup
);
818 setup_tri_edges( setup
);
820 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_TRIANGLES
);
823 setup
->span
.right
[0] = 0;
824 setup
->span
.right
[1] = 0;
825 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
827 /* init_constant_attribs( setup ); */
829 if (setup
->oneoverarea
< 0.0) {
832 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
833 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
838 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
839 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
842 flush_spans( setup
);
845 printf("Tri: %u frags emitted, %u written\n",
846 setup
->numFragsEmitted
,
847 setup
->numFragsWritten
);
852 /* Apply cylindrical wrapping to v0, v1 coordinates, if enabled.
853 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
856 line_apply_cylindrical_wrap(float v0
,
858 uint cylindrical_wrap
,
861 if (cylindrical_wrap
) {
868 else if (delta
< -0.5f
) {
879 * Compute a0, dadx and dady for a linearly interpolated coefficient,
881 * v[0] and v[1] are vmin and vmax, respectively.
884 line_linear_coeff(const struct setup_context
*setup
,
885 struct tgsi_interp_coef
*coef
,
889 const float da
= v
[1] - v
[0];
890 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
891 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
892 coef
->dadx
[i
] = dadx
;
893 coef
->dady
[i
] = dady
;
894 coef
->a0
[i
] = (v
[0] -
895 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
896 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
901 * Compute a0, dadx and dady for a perspective-corrected interpolant,
903 * v[0] and v[1] are vmin and vmax, respectively.
906 line_persp_coeff(const struct setup_context
*setup
,
907 struct tgsi_interp_coef
*coef
,
911 /* XXX double-check/verify this arithmetic */
912 const float a0
= v
[0] * setup
->vmin
[0][3];
913 const float a1
= v
[1] * setup
->vmax
[0][3];
914 const float da
= a1
- a0
;
915 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
916 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
917 coef
->dadx
[i
] = dadx
;
918 coef
->dady
[i
] = dady
;
919 coef
->a0
[i
] = (v
[0] - /* XXX: <-- shouldn't that be a0? */
920 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
921 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
926 * Compute the setup->coef[] array dadx, dady, a0 values.
927 * Must be called after setup->vmin,vmax are initialized.
929 static INLINE boolean
930 setup_line_coefficients(struct setup_context
*setup
,
931 const float (*v0
)[4],
932 const float (*v1
)[4])
934 struct softpipe_context
*softpipe
= setup
->softpipe
;
935 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
936 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
941 /* use setup->vmin, vmax to point to vertices */
942 if (softpipe
->rasterizer
->flatshade_first
)
943 setup
->vprovoke
= v0
;
945 setup
->vprovoke
= v1
;
949 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
950 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
952 /* NOTE: this is not really area but something proportional to it */
953 area
= setup
->emaj
.dx
* setup
->emaj
.dx
+ setup
->emaj
.dy
* setup
->emaj
.dy
;
954 if (area
== 0.0f
|| util_is_inf_or_nan(area
))
956 setup
->oneoverarea
= 1.0f
/ area
;
958 /* z and w are done by linear interpolation:
960 v
[0] = setup
->vmin
[0][2];
961 v
[1] = setup
->vmax
[0][2];
962 line_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
964 v
[0] = setup
->vmin
[0][3];
965 v
[1] = setup
->vmax
[0][3];
966 line_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
968 /* setup interpolation for all the remaining attributes:
970 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
971 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
974 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
975 case INTERP_CONSTANT
:
976 for (j
= 0; j
< NUM_CHANNELS
; j
++)
977 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
980 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
981 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
982 setup
->vmax
[vertSlot
][j
],
983 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
985 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
988 case INTERP_PERSPECTIVE
:
989 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
990 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
991 setup
->vmax
[vertSlot
][j
],
992 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
994 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
998 setup_fragcoord_coeff(setup
, fragSlot
);
1004 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1005 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
1006 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1007 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1015 * Plot a pixel in a line segment.
1018 plot(struct setup_context
*setup
, int x
, int y
)
1020 const int iy
= y
& 1;
1021 const int ix
= x
& 1;
1022 const int quadX
= x
- ix
;
1023 const int quadY
= y
- iy
;
1024 const int mask
= (1 << ix
) << (2 * iy
);
1026 if (quadX
!= setup
->quad
[0].input
.x0
||
1027 quadY
!= setup
->quad
[0].input
.y0
)
1029 /* flush prev quad, start new quad */
1031 if (setup
->quad
[0].input
.x0
!= -1)
1032 clip_emit_quad( setup
, &setup
->quad
[0] );
1034 setup
->quad
[0].input
.x0
= quadX
;
1035 setup
->quad
[0].input
.y0
= quadY
;
1036 setup
->quad
[0].inout
.mask
= 0x0;
1039 setup
->quad
[0].inout
.mask
|= mask
;
1044 * Do setup for line rasterization, then render the line.
1045 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
1046 * to handle stippling and wide lines.
1049 sp_setup_line(struct setup_context
*setup
,
1050 const float (*v0
)[4],
1051 const float (*v1
)[4])
1053 int x0
= (int) v0
[0][0];
1054 int x1
= (int) v1
[0][0];
1055 int y0
= (int) v0
[0][1];
1056 int y1
= (int) v1
[0][1];
1062 debug_printf("Setup line:\n");
1063 print_vertex(setup
, v0
);
1064 print_vertex(setup
, v1
);
1067 if (setup
->softpipe
->no_rast
)
1070 if (dx
== 0 && dy
== 0)
1073 if (!setup_line_coefficients(setup
, v0
, v1
))
1076 assert(v0
[0][0] < 1.0e9
);
1077 assert(v0
[0][1] < 1.0e9
);
1078 assert(v1
[0][0] < 1.0e9
);
1079 assert(v1
[0][1] < 1.0e9
);
1082 dx
= -dx
; /* make positive */
1090 dy
= -dy
; /* make positive */
1099 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_LINES
);
1101 setup
->quad
[0].input
.x0
= setup
->quad
[0].input
.y0
= -1;
1102 setup
->quad
[0].inout
.mask
= 0x0;
1104 /* XXX temporary: set coverage to 1.0 so the line appears
1105 * if AA mode happens to be enabled.
1107 setup
->quad
[0].input
.coverage
[0] =
1108 setup
->quad
[0].input
.coverage
[1] =
1109 setup
->quad
[0].input
.coverage
[2] =
1110 setup
->quad
[0].input
.coverage
[3] = 1.0;
1113 /*** X-major line ***/
1115 const int errorInc
= dy
+ dy
;
1116 int error
= errorInc
- dx
;
1117 const int errorDec
= error
- dx
;
1119 for (i
= 0; i
< dx
; i
++) {
1120 plot(setup
, x0
, y0
);
1133 /*** Y-major line ***/
1135 const int errorInc
= dx
+ dx
;
1136 int error
= errorInc
- dy
;
1137 const int errorDec
= error
- dy
;
1139 for (i
= 0; i
< dy
; i
++) {
1140 plot(setup
, x0
, y0
);
1153 /* draw final quad */
1154 if (setup
->quad
[0].inout
.mask
) {
1155 clip_emit_quad( setup
, &setup
->quad
[0] );
1161 point_persp_coeff(const struct setup_context
*setup
,
1162 const float (*vert
)[4],
1163 struct tgsi_interp_coef
*coef
,
1164 uint vertSlot
, uint i
)
1167 coef
->dadx
[i
] = 0.0F
;
1168 coef
->dady
[i
] = 0.0F
;
1169 coef
->a0
[i
] = vert
[vertSlot
][i
] * vert
[0][3];
1174 * Do setup for point rasterization, then render the point.
1175 * Round or square points...
1176 * XXX could optimize a lot for 1-pixel points.
1179 sp_setup_point( struct setup_context
*setup
,
1180 const float (*v0
)[4] )
1182 struct softpipe_context
*softpipe
= setup
->softpipe
;
1183 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
1184 const int sizeAttr
= setup
->softpipe
->psize_slot
;
1186 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1187 : setup
->softpipe
->rasterizer
->point_size
;
1188 const float halfSize
= 0.5F
* size
;
1189 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
1190 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1191 const float y
= v0
[0][1];
1192 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
1196 debug_printf("Setup point:\n");
1197 print_vertex(setup
, v0
);
1200 if (softpipe
->no_rast
)
1203 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_POINTS
);
1205 /* For points, all interpolants are constant-valued.
1206 * However, for point sprites, we'll need to setup texcoords appropriately.
1207 * XXX: which coefficients are the texcoords???
1208 * We may do point sprites as textured quads...
1210 * KW: We don't know which coefficients are texcoords - ultimately
1211 * the choice of what interpolation mode to use for each attribute
1212 * should be determined by the fragment program, using
1213 * per-attribute declaration statements that include interpolation
1214 * mode as a parameter. So either the fragment program will have
1215 * to be adjusted for pointsprite vs normal point behaviour, or
1216 * otherwise a special interpolation mode will have to be defined
1217 * which matches the required behaviour for point sprites. But -
1218 * the latter is not a feature of normal hardware, and as such
1219 * probably should be ruled out on that basis.
1221 setup
->vprovoke
= v0
;
1224 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1225 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1227 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
1228 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1231 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1232 case INTERP_CONSTANT
:
1235 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1236 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1238 case INTERP_PERSPECTIVE
:
1239 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1240 point_persp_coeff(setup
, setup
->vprovoke
,
1241 &setup
->coef
[fragSlot
], vertSlot
, j
);
1244 setup_fragcoord_coeff(setup
, fragSlot
);
1250 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1251 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
1252 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1253 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1258 if (halfSize
<= 0.5 && !round
) {
1259 /* special case for 1-pixel points */
1260 const int ix
= ((int) x
) & 1;
1261 const int iy
= ((int) y
) & 1;
1262 setup
->quad
[0].input
.x0
= (int) x
- ix
;
1263 setup
->quad
[0].input
.y0
= (int) y
- iy
;
1264 setup
->quad
[0].inout
.mask
= (1 << ix
) << (2 * iy
);
1265 clip_emit_quad( setup
, &setup
->quad
[0] );
1269 /* rounded points */
1270 const int ixmin
= block((int) (x
- halfSize
));
1271 const int ixmax
= block((int) (x
+ halfSize
));
1272 const int iymin
= block((int) (y
- halfSize
));
1273 const int iymax
= block((int) (y
+ halfSize
));
1274 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1275 const float rmax
= halfSize
+ 0.7071F
;
1276 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1277 const float rmax2
= rmax
* rmax
;
1278 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1281 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1282 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1283 float dx
, dy
, dist2
, cover
;
1285 setup
->quad
[0].inout
.mask
= 0x0;
1287 dx
= (ix
+ 0.5f
) - x
;
1288 dy
= (iy
+ 0.5f
) - y
;
1289 dist2
= dx
* dx
+ dy
* dy
;
1290 if (dist2
<= rmax2
) {
1291 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1292 setup
->quad
[0].input
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1293 setup
->quad
[0].inout
.mask
|= MASK_TOP_LEFT
;
1296 dx
= (ix
+ 1.5f
) - x
;
1297 dy
= (iy
+ 0.5f
) - y
;
1298 dist2
= dx
* dx
+ dy
* dy
;
1299 if (dist2
<= rmax2
) {
1300 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1301 setup
->quad
[0].input
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1302 setup
->quad
[0].inout
.mask
|= MASK_TOP_RIGHT
;
1305 dx
= (ix
+ 0.5f
) - x
;
1306 dy
= (iy
+ 1.5f
) - y
;
1307 dist2
= dx
* dx
+ dy
* dy
;
1308 if (dist2
<= rmax2
) {
1309 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1310 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1311 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_LEFT
;
1314 dx
= (ix
+ 1.5f
) - x
;
1315 dy
= (iy
+ 1.5f
) - y
;
1316 dist2
= dx
* dx
+ dy
* dy
;
1317 if (dist2
<= rmax2
) {
1318 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1319 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1320 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_RIGHT
;
1323 if (setup
->quad
[0].inout
.mask
) {
1324 setup
->quad
[0].input
.x0
= ix
;
1325 setup
->quad
[0].input
.y0
= iy
;
1326 clip_emit_quad( setup
, &setup
->quad
[0] );
1333 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1334 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1335 const int xmax
= xmin
+ (int) size
;
1336 const int ymax
= ymin
+ (int) size
;
1337 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1338 const int ixmin
= block(xmin
);
1339 const int ixmax
= block(xmax
- 1);
1340 const int iymin
= block(ymin
);
1341 const int iymax
= block(ymax
- 1);
1345 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1347 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1350 /* above the top edge */
1351 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1353 if (iy
+ 1 >= ymax
) {
1354 /* below the bottom edge */
1355 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1358 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1359 uint mask
= rowMask
;
1362 /* fragment is past left edge of point, turn off left bits */
1363 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1365 if (ix
+ 1 >= xmax
) {
1366 /* past the right edge */
1367 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1370 setup
->quad
[0].inout
.mask
= mask
;
1371 setup
->quad
[0].input
.x0
= ix
;
1372 setup
->quad
[0].input
.y0
= iy
;
1373 clip_emit_quad( setup
, &setup
->quad
[0] );
1380 void sp_setup_prepare( struct setup_context
*setup
)
1382 struct softpipe_context
*sp
= setup
->softpipe
;
1385 softpipe_update_derived(sp
);
1388 /* Note: nr_attrs is only used for debugging (vertex printing) */
1389 setup
->nr_vertex_attrs
= draw_num_shader_outputs(sp
->draw
);
1391 sp
->quad
.first
->begin( sp
->quad
.first
);
1393 if (sp
->reduced_api_prim
== PIPE_PRIM_TRIANGLES
&&
1394 sp
->rasterizer
->fill_cw
== PIPE_POLYGON_MODE_FILL
&&
1395 sp
->rasterizer
->fill_ccw
== PIPE_POLYGON_MODE_FILL
) {
1396 /* we'll do culling */
1397 setup
->winding
= sp
->rasterizer
->cull_mode
;
1400 /* 'draw' will do culling */
1401 setup
->winding
= PIPE_WINDING_NONE
;
1407 void sp_setup_destroy_context( struct setup_context
*setup
)
1414 * Create a new primitive setup/render stage.
1416 struct setup_context
*sp_setup_create_context( struct softpipe_context
*softpipe
)
1418 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
1421 setup
->softpipe
= softpipe
;
1423 for (i
= 0; i
< MAX_QUADS
; i
++) {
1424 setup
->quad
[i
].coef
= setup
->coef
;
1425 setup
->quad
[i
].posCoef
= &setup
->posCoef
;
1428 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
1429 setup
->span
.left
[1] = 1000000; /* greater than right[1] */