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"
55 float dx
; /**< X(v1) - X(v0), used only during setup */
56 float dy
; /**< Y(v1) - Y(v0), used only during setup */
57 float dxdy
; /**< dx/dy */
58 float sx
, sy
; /**< first sample point coord */
59 int lines
; /**< number of lines on this edge */
64 * Max number of quads (2x2 pixel blocks) to process per batch.
65 * This can't be arbitrarily increased since we depend on some 32-bit
66 * bitmasks (two bits per quad).
72 * Triangle setup info.
73 * Also used for line drawing (taking some liberties).
75 struct setup_context
{
76 struct softpipe_context
*softpipe
;
78 /* Vertices are just an array of floats making up each attribute in
79 * turn. Currently fixed at 4 floats, but should change in time.
80 * Codegen will help cope with this.
82 const float (*vmax
)[4];
83 const float (*vmid
)[4];
84 const float (*vmin
)[4];
85 const float (*vprovoke
)[4];
96 struct quad_header quad
[MAX_QUADS
];
97 struct quad_header
*quad_ptrs
[MAX_QUADS
];
100 struct tgsi_interp_coef coef
[PIPE_MAX_SHADER_INPUTS
];
101 struct tgsi_interp_coef posCoef
; /* For Z, W */
104 int left
[2]; /**< [0] = row0, [1] = row1 */
110 uint numFragsEmitted
; /**< per primitive */
111 uint numFragsWritten
; /**< per primitive */
114 unsigned cull_face
; /* which faces cull */
115 unsigned nr_vertex_attrs
;
125 * Clip setup->quad against the scissor/surface bounds.
128 quad_clip(struct setup_context
*setup
, struct quad_header
*quad
)
130 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
131 const int minx
= (int) cliprect
->minx
;
132 const int maxx
= (int) cliprect
->maxx
;
133 const int miny
= (int) cliprect
->miny
;
134 const int maxy
= (int) cliprect
->maxy
;
136 if (quad
->input
.x0
>= maxx
||
137 quad
->input
.y0
>= maxy
||
138 quad
->input
.x0
+ 1 < minx
||
139 quad
->input
.y0
+ 1 < miny
) {
140 /* totally clipped */
141 quad
->inout
.mask
= 0x0;
144 if (quad
->input
.x0
< minx
)
145 quad
->inout
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
146 if (quad
->input
.y0
< miny
)
147 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
148 if (quad
->input
.x0
== maxx
- 1)
149 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
150 if (quad
->input
.y0
== maxy
- 1)
151 quad
->inout
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
156 * Emit a quad (pass to next stage) with clipping.
159 clip_emit_quad(struct setup_context
*setup
, struct quad_header
*quad
)
161 quad_clip( setup
, quad
);
163 if (quad
->inout
.mask
) {
164 struct softpipe_context
*sp
= setup
->softpipe
;
166 sp
->quad
.first
->run( sp
->quad
.first
, &quad
, 1 );
173 * Given an X or Y coordinate, return the block/quad coordinate that it
191 * Render a horizontal span of quads
194 flush_spans(struct setup_context
*setup
)
196 const int step
= MAX_QUADS
;
197 const int xleft0
= setup
->span
.left
[0];
198 const int xleft1
= setup
->span
.left
[1];
199 const int xright0
= setup
->span
.right
[0];
200 const int xright1
= setup
->span
.right
[1];
201 struct quad_stage
*pipe
= setup
->softpipe
->quad
.first
;
203 const int minleft
= block_x(MIN2(xleft0
, xleft1
));
204 const int maxright
= MAX2(xright0
, xright1
);
207 /* process quads in horizontal chunks of 16 */
208 for (x
= minleft
; x
< maxright
; x
+= step
) {
209 unsigned skip_left0
= CLAMP(xleft0
- x
, 0, step
);
210 unsigned skip_left1
= CLAMP(xleft1
- x
, 0, step
);
211 unsigned skip_right0
= CLAMP(x
+ step
- xright0
, 0, step
);
212 unsigned skip_right1
= CLAMP(x
+ step
- xright1
, 0, step
);
216 unsigned skipmask_left0
= (1U << skip_left0
) - 1U;
217 unsigned skipmask_left1
= (1U << skip_left1
) - 1U;
219 /* These calculations fail when step == 32 and skip_right == 0.
221 unsigned skipmask_right0
= ~0U << (unsigned)(step
- skip_right0
);
222 unsigned skipmask_right1
= ~0U << (unsigned)(step
- skip_right1
);
224 unsigned mask0
= ~skipmask_left0
& ~skipmask_right0
;
225 unsigned mask1
= ~skipmask_left1
& ~skipmask_right1
;
229 unsigned quadmask
= (mask0
& 3) | ((mask1
& 3) << 2);
231 setup
->quad
[q
].input
.x0
= lx
;
232 setup
->quad
[q
].input
.y0
= setup
->span
.y
;
233 setup
->quad
[q
].input
.facing
= setup
->facing
;
234 setup
->quad
[q
].inout
.mask
= quadmask
;
235 setup
->quad_ptrs
[q
] = &setup
->quad
[q
];
241 } while (mask0
| mask1
);
243 pipe
->run( pipe
, setup
->quad_ptrs
, q
);
249 setup
->span
.right
[0] = 0;
250 setup
->span
.right
[1] = 0;
251 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
252 setup
->span
.left
[1] = 1000000; /* greater than right[1] */
258 print_vertex(const struct setup_context
*setup
,
262 debug_printf(" Vertex: (%p)\n", (void *) v
);
263 for (i
= 0; i
< setup
->nr_vertex_attrs
; i
++) {
264 debug_printf(" %d: %f %f %f %f\n", i
,
265 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
266 if (util_is_inf_or_nan(v
[i
][0])) {
267 debug_printf(" NaN!\n");
275 * Sort the vertices from top to bottom order, setting up the triangle
276 * edge fields (ebot, emaj, etop).
277 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
280 setup_sort_vertices(struct setup_context
*setup
,
282 const float (*v0
)[4],
283 const float (*v1
)[4],
284 const float (*v2
)[4])
286 if (setup
->softpipe
->rasterizer
->flatshade_first
)
287 setup
->vprovoke
= v0
;
289 setup
->vprovoke
= v2
;
291 /* determine bottom to top order of vertices */
338 setup
->ebot
.dx
= setup
->vmid
[0][0] - setup
->vmin
[0][0];
339 setup
->ebot
.dy
= setup
->vmid
[0][1] - setup
->vmin
[0][1];
340 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
341 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
342 setup
->etop
.dx
= setup
->vmax
[0][0] - setup
->vmid
[0][0];
343 setup
->etop
.dy
= setup
->vmax
[0][1] - setup
->vmid
[0][1];
346 * Compute triangle's area. Use 1/area to compute partial
347 * derivatives of attributes later.
349 * The area will be the same as prim->det, but the sign may be
350 * different depending on how the vertices get sorted above.
352 * To determine whether the primitive is front or back facing we
353 * use the prim->det value because its sign is correct.
356 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
357 setup
->ebot
.dx
* setup
->emaj
.dy
);
359 setup
->oneoverarea
= 1.0f
/ area
;
362 debug_printf("%s one-over-area %f area %f det %f\n",
363 __FUNCTION__, setup->oneoverarea, area, det );
365 if (util_is_inf_or_nan(setup
->oneoverarea
))
369 /* We need to know if this is a front or back-facing triangle for:
370 * - the GLSL gl_FrontFacing fragment attribute (bool)
371 * - two-sided stencil test
372 * 0 = front-facing, 1 = back-facing
376 (setup
->softpipe
->rasterizer
->front_ccw
));
379 unsigned face
= setup
->facing
== 0 ? PIPE_FACE_FRONT
: PIPE_FACE_BACK
;
381 if (face
& setup
->cull_face
)
386 /* Prepare pixel offset for rasterisation:
387 * - pixel center (0.5, 0.5) for GL, or
388 * - assume (0.0, 0.0) for other APIs.
390 if (setup
->softpipe
->rasterizer
->gl_rasterization_rules
) {
391 setup
->pixel_offset
= 0.5f
;
393 setup
->pixel_offset
= 0.0f
;
400 /* Apply cylindrical wrapping to v0, v1, v2 coordinates, if enabled.
401 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
402 * Some combinations of coordinates produce invalid results,
403 * but this behaviour is acceptable.
406 tri_apply_cylindrical_wrap(float v0
,
409 uint cylindrical_wrap
,
412 if (cylindrical_wrap
) {
419 else if (delta
< -0.5f
) {
427 else if (delta
< -0.5f
) {
435 else if (delta
< -0.5f
) {
447 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
448 * The value value comes from vertex[slot][i].
449 * The result will be put into setup->coef[slot].a0[i].
450 * \param slot which attribute slot
451 * \param i which component of the slot (0..3)
454 const_coeff(struct setup_context
*setup
,
455 struct tgsi_interp_coef
*coef
,
456 uint vertSlot
, uint i
)
463 /* need provoking vertex info!
465 coef
->a0
[i
] = setup
->vprovoke
[vertSlot
][i
];
470 * Compute a0, dadx and dady for a linearly interpolated coefficient,
472 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
475 tri_linear_coeff(struct setup_context
*setup
,
476 struct tgsi_interp_coef
*coef
,
480 float botda
= v
[1] - v
[0];
481 float majda
= v
[2] - v
[0];
482 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
483 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
484 float dadx
= a
* setup
->oneoverarea
;
485 float dady
= b
* setup
->oneoverarea
;
489 coef
->dadx
[i
] = dadx
;
490 coef
->dady
[i
] = dady
;
492 /* calculate a0 as the value which would be sampled for the
493 * fragment at (0,0), taking into account that we want to sample at
494 * pixel centers, in other words (pixel_offset, pixel_offset).
496 * this is neat but unfortunately not a good way to do things for
497 * triangles with very large values of dadx or dady as it will
498 * result in the subtraction and re-addition from a0 of a very
499 * large number, which means we'll end up loosing a lot of the
500 * fractional bits and precision from a0. the way to fix this is
501 * to define a0 as the sample at a pixel center somewhere near vmin
502 * instead - i'll switch to this later.
504 coef
->a0
[i
] = (v
[0] -
505 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
506 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
509 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
511 setup->coef[slot].a0[i],
512 setup->coef[slot].dadx[i],
513 setup->coef[slot].dady[i]);
519 * Compute a0, dadx and dady for a perspective-corrected interpolant,
521 * We basically multiply the vertex value by 1/w before computing
522 * the plane coefficients (a0, dadx, dady).
523 * Later, when we compute the value at a particular fragment position we'll
524 * divide the interpolated value by the interpolated W at that fragment.
525 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
528 tri_persp_coeff(struct setup_context
*setup
,
529 struct tgsi_interp_coef
*coef
,
533 /* premultiply by 1/w (v[0][3] is always W):
535 float mina
= v
[0] * setup
->vmin
[0][3];
536 float mida
= v
[1] * setup
->vmid
[0][3];
537 float maxa
= v
[2] * setup
->vmax
[0][3];
538 float botda
= mida
- mina
;
539 float majda
= maxa
- mina
;
540 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
541 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
542 float dadx
= a
* setup
->oneoverarea
;
543 float dady
= b
* setup
->oneoverarea
;
546 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
547 setup->vmin[vertSlot][i],
548 setup->vmid[vertSlot][i],
549 setup->vmax[vertSlot][i]
554 coef
->dadx
[i
] = dadx
;
555 coef
->dady
[i
] = dady
;
556 coef
->a0
[i
] = (mina
-
557 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
558 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
563 * Special coefficient setup for gl_FragCoord.
564 * X and Y are trivial, though Y may have to be inverted for OpenGL.
565 * Z and W are copied from posCoef which should have already been computed.
566 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
569 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
571 struct sp_fragment_shader
* spfs
= setup
->softpipe
->fs
;
573 setup
->coef
[slot
].a0
[0] = spfs
->pixel_center_integer
? 0.0 : 0.5;
574 setup
->coef
[slot
].dadx
[0] = 1.0;
575 setup
->coef
[slot
].dady
[0] = 0.0;
577 setup
->coef
[slot
].a0
[1] =
578 (spfs
->origin_lower_left
? setup
->softpipe
->framebuffer
.height
-1 : 0)
579 + (spfs
->pixel_center_integer
? 0.0 : 0.5);
580 setup
->coef
[slot
].dadx
[1] = 0.0;
581 setup
->coef
[slot
].dady
[1] = spfs
->origin_lower_left
? -1.0 : 1.0;
583 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
584 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
585 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
587 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
588 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
589 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
595 * Compute the setup->coef[] array dadx, dady, a0 values.
596 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
599 setup_tri_coefficients(struct setup_context
*setup
)
601 struct softpipe_context
*softpipe
= setup
->softpipe
;
602 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
603 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
607 /* z and w are done by linear interpolation:
609 v
[0] = setup
->vmin
[0][2];
610 v
[1] = setup
->vmid
[0][2];
611 v
[2] = setup
->vmax
[0][2];
612 tri_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
614 v
[0] = setup
->vmin
[0][3];
615 v
[1] = setup
->vmid
[0][3];
616 v
[2] = setup
->vmax
[0][3];
617 tri_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
619 /* setup interpolation for all the remaining attributes:
621 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
622 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
625 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
626 case INTERP_CONSTANT
:
627 for (j
= 0; j
< NUM_CHANNELS
; j
++)
628 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
631 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
632 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
633 setup
->vmid
[vertSlot
][j
],
634 setup
->vmax
[vertSlot
][j
],
635 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
637 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
640 case INTERP_PERSPECTIVE
:
641 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
642 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
643 setup
->vmid
[vertSlot
][j
],
644 setup
->vmax
[vertSlot
][j
],
645 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
647 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
651 setup_fragcoord_coeff(setup
, fragSlot
);
657 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
658 /* convert 0 to 1.0 and 1 to -1.0 */
659 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
660 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
661 setup
->coef
[fragSlot
].dady
[0] = 0.0;
668 setup_tri_edges(struct setup_context
*setup
)
670 float vmin_x
= setup
->vmin
[0][0] + setup
->pixel_offset
;
671 float vmid_x
= setup
->vmid
[0][0] + setup
->pixel_offset
;
673 float vmin_y
= setup
->vmin
[0][1] - setup
->pixel_offset
;
674 float vmid_y
= setup
->vmid
[0][1] - setup
->pixel_offset
;
675 float vmax_y
= setup
->vmax
[0][1] - setup
->pixel_offset
;
677 setup
->emaj
.sy
= ceilf(vmin_y
);
678 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
679 setup
->emaj
.dxdy
= setup
->emaj
.dy
? setup
->emaj
.dx
/ setup
->emaj
.dy
: .0f
;
680 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
682 setup
->etop
.sy
= ceilf(vmid_y
);
683 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
684 setup
->etop
.dxdy
= setup
->etop
.dy
? setup
->etop
.dx
/ setup
->etop
.dy
: .0f
;
685 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
687 setup
->ebot
.sy
= ceilf(vmin_y
);
688 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
689 setup
->ebot
.dxdy
= setup
->ebot
.dy
? setup
->ebot
.dx
/ setup
->ebot
.dy
: .0f
;
690 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
695 * Render the upper or lower half of a triangle.
696 * Scissoring/cliprect is applied here too.
699 subtriangle(struct setup_context
*setup
,
704 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
705 const int minx
= (int) cliprect
->minx
;
706 const int maxx
= (int) cliprect
->maxx
;
707 const int miny
= (int) cliprect
->miny
;
708 const int maxy
= (int) cliprect
->maxy
;
709 int y
, start_y
, finish_y
;
710 int sy
= (int)eleft
->sy
;
712 assert((int)eleft
->sy
== (int) eright
->sy
);
715 /* clip top/bottom */
720 finish_y
= sy
+ lines
;
728 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
731 for (y
= start_y
; y
< finish_y
; y
++) {
733 /* avoid accumulating adds as floats don't have the precision to
734 * accurately iterate large triangle edges that way. luckily we
735 * can just multiply these days.
737 * this is all drowned out by the attribute interpolation anyway.
739 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
740 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
742 /* clip left/right */
750 if (block(_y
) != setup
->span
.y
) {
752 setup
->span
.y
= block(_y
);
755 setup
->span
.left
[_y
&1] = left
;
756 setup
->span
.right
[_y
&1] = right
;
761 /* save the values so that emaj can be restarted:
763 eleft
->sx
+= lines
* eleft
->dxdy
;
764 eright
->sx
+= lines
* eright
->dxdy
;
771 * Recalculate prim's determinant. This is needed as we don't have
772 * get this information through the vbuf_render interface & we must
776 calc_det(const float (*v0
)[4],
777 const float (*v1
)[4],
778 const float (*v2
)[4])
780 /* edge vectors e = v0 - v2, f = v1 - v2 */
781 const float ex
= v0
[0][0] - v2
[0][0];
782 const float ey
= v0
[0][1] - v2
[0][1];
783 const float fx
= v1
[0][0] - v2
[0][0];
784 const float fy
= v1
[0][1] - v2
[0][1];
786 /* det = cross(e,f).z */
787 return ex
* fy
- ey
* fx
;
792 * Do setup for triangle rasterization, then render the triangle.
795 sp_setup_tri(struct setup_context
*setup
,
796 const float (*v0
)[4],
797 const float (*v1
)[4],
798 const float (*v2
)[4])
803 debug_printf("Setup triangle:\n");
804 print_vertex(setup
, v0
);
805 print_vertex(setup
, v1
);
806 print_vertex(setup
, v2
);
809 if (setup
->softpipe
->no_rast
)
812 det
= calc_det(v0
, v1
, v2
);
814 debug_printf("%s\n", __FUNCTION__ );
818 setup
->numFragsEmitted
= 0;
819 setup
->numFragsWritten
= 0;
822 if (!setup_sort_vertices( setup
, det
, v0
, v1
, v2
))
825 setup_tri_coefficients( setup
);
826 setup_tri_edges( setup
);
828 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_TRIANGLES
);
831 setup
->span
.right
[0] = 0;
832 setup
->span
.right
[1] = 0;
833 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
835 /* init_constant_attribs( setup ); */
837 if (setup
->oneoverarea
< 0.0) {
840 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
841 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
846 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
847 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
850 flush_spans( setup
);
853 printf("Tri: %u frags emitted, %u written\n",
854 setup
->numFragsEmitted
,
855 setup
->numFragsWritten
);
860 /* Apply cylindrical wrapping to v0, v1 coordinates, if enabled.
861 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
864 line_apply_cylindrical_wrap(float v0
,
866 uint cylindrical_wrap
,
869 if (cylindrical_wrap
) {
876 else if (delta
< -0.5f
) {
887 * Compute a0, dadx and dady for a linearly interpolated coefficient,
889 * v[0] and v[1] are vmin and vmax, respectively.
892 line_linear_coeff(const struct setup_context
*setup
,
893 struct tgsi_interp_coef
*coef
,
897 const float da
= v
[1] - v
[0];
898 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
899 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
900 coef
->dadx
[i
] = dadx
;
901 coef
->dady
[i
] = dady
;
902 coef
->a0
[i
] = (v
[0] -
903 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
904 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
909 * Compute a0, dadx and dady for a perspective-corrected interpolant,
911 * v[0] and v[1] are vmin and vmax, respectively.
914 line_persp_coeff(const struct setup_context
*setup
,
915 struct tgsi_interp_coef
*coef
,
919 const float a0
= v
[0] * setup
->vmin
[0][3];
920 const float a1
= v
[1] * setup
->vmax
[0][3];
921 const float da
= a1
- a0
;
922 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
923 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
924 coef
->dadx
[i
] = dadx
;
925 coef
->dady
[i
] = dady
;
927 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
928 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
933 * Compute the setup->coef[] array dadx, dady, a0 values.
934 * Must be called after setup->vmin,vmax are initialized.
937 setup_line_coefficients(struct setup_context
*setup
,
938 const float (*v0
)[4],
939 const float (*v1
)[4])
941 struct softpipe_context
*softpipe
= setup
->softpipe
;
942 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
943 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
948 /* use setup->vmin, vmax to point to vertices */
949 if (softpipe
->rasterizer
->flatshade_first
)
950 setup
->vprovoke
= v0
;
952 setup
->vprovoke
= v1
;
956 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
957 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
959 /* NOTE: this is not really area but something proportional to it */
960 area
= setup
->emaj
.dx
* setup
->emaj
.dx
+ setup
->emaj
.dy
* setup
->emaj
.dy
;
961 if (area
== 0.0f
|| util_is_inf_or_nan(area
))
963 setup
->oneoverarea
= 1.0f
/ area
;
965 /* z and w are done by linear interpolation:
967 v
[0] = setup
->vmin
[0][2];
968 v
[1] = setup
->vmax
[0][2];
969 line_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
971 v
[0] = setup
->vmin
[0][3];
972 v
[1] = setup
->vmax
[0][3];
973 line_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
975 /* setup interpolation for all the remaining attributes:
977 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
978 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
981 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
982 case INTERP_CONSTANT
:
983 for (j
= 0; j
< NUM_CHANNELS
; j
++)
984 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
987 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
988 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
989 setup
->vmax
[vertSlot
][j
],
990 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
992 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
995 case INTERP_PERSPECTIVE
:
996 for (j
= 0; j
< NUM_CHANNELS
; j
++) {
997 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
998 setup
->vmax
[vertSlot
][j
],
999 spfs
->info
.input_cylindrical_wrap
[fragSlot
] & (1 << j
),
1001 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
1005 setup_fragcoord_coeff(setup
, fragSlot
);
1011 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1012 /* convert 0 to 1.0 and 1 to -1.0 */
1013 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
1014 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1015 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1023 * Plot a pixel in a line segment.
1026 plot(struct setup_context
*setup
, int x
, int y
)
1028 const int iy
= y
& 1;
1029 const int ix
= x
& 1;
1030 const int quadX
= x
- ix
;
1031 const int quadY
= y
- iy
;
1032 const int mask
= (1 << ix
) << (2 * iy
);
1034 if (quadX
!= setup
->quad
[0].input
.x0
||
1035 quadY
!= setup
->quad
[0].input
.y0
)
1037 /* flush prev quad, start new quad */
1039 if (setup
->quad
[0].input
.x0
!= -1)
1040 clip_emit_quad( setup
, &setup
->quad
[0] );
1042 setup
->quad
[0].input
.x0
= quadX
;
1043 setup
->quad
[0].input
.y0
= quadY
;
1044 setup
->quad
[0].inout
.mask
= 0x0;
1047 setup
->quad
[0].inout
.mask
|= mask
;
1052 * Do setup for line rasterization, then render the line.
1053 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
1054 * to handle stippling and wide lines.
1057 sp_setup_line(struct setup_context
*setup
,
1058 const float (*v0
)[4],
1059 const float (*v1
)[4])
1061 int x0
= (int) v0
[0][0];
1062 int x1
= (int) v1
[0][0];
1063 int y0
= (int) v0
[0][1];
1064 int y1
= (int) v1
[0][1];
1070 debug_printf("Setup line:\n");
1071 print_vertex(setup
, v0
);
1072 print_vertex(setup
, v1
);
1075 if (setup
->softpipe
->no_rast
)
1078 if (dx
== 0 && dy
== 0)
1081 if (!setup_line_coefficients(setup
, v0
, v1
))
1084 assert(v0
[0][0] < 1.0e9
);
1085 assert(v0
[0][1] < 1.0e9
);
1086 assert(v1
[0][0] < 1.0e9
);
1087 assert(v1
[0][1] < 1.0e9
);
1090 dx
= -dx
; /* make positive */
1098 dy
= -dy
; /* make positive */
1107 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_LINES
);
1109 setup
->quad
[0].input
.x0
= setup
->quad
[0].input
.y0
= -1;
1110 setup
->quad
[0].inout
.mask
= 0x0;
1112 /* XXX temporary: set coverage to 1.0 so the line appears
1113 * if AA mode happens to be enabled.
1115 setup
->quad
[0].input
.coverage
[0] =
1116 setup
->quad
[0].input
.coverage
[1] =
1117 setup
->quad
[0].input
.coverage
[2] =
1118 setup
->quad
[0].input
.coverage
[3] = 1.0;
1121 /*** X-major line ***/
1123 const int errorInc
= dy
+ dy
;
1124 int error
= errorInc
- dx
;
1125 const int errorDec
= error
- dx
;
1127 for (i
= 0; i
< dx
; i
++) {
1128 plot(setup
, x0
, y0
);
1141 /*** Y-major line ***/
1143 const int errorInc
= dx
+ dx
;
1144 int error
= errorInc
- dy
;
1145 const int errorDec
= error
- dy
;
1147 for (i
= 0; i
< dy
; i
++) {
1148 plot(setup
, x0
, y0
);
1161 /* draw final quad */
1162 if (setup
->quad
[0].inout
.mask
) {
1163 clip_emit_quad( setup
, &setup
->quad
[0] );
1169 point_persp_coeff(const struct setup_context
*setup
,
1170 const float (*vert
)[4],
1171 struct tgsi_interp_coef
*coef
,
1172 uint vertSlot
, uint i
)
1175 coef
->dadx
[i
] = 0.0F
;
1176 coef
->dady
[i
] = 0.0F
;
1177 coef
->a0
[i
] = vert
[vertSlot
][i
] * vert
[0][3];
1182 * Do setup for point rasterization, then render the point.
1183 * Round or square points...
1184 * XXX could optimize a lot for 1-pixel points.
1187 sp_setup_point(struct setup_context
*setup
,
1188 const float (*v0
)[4])
1190 struct softpipe_context
*softpipe
= setup
->softpipe
;
1191 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
1192 const int sizeAttr
= setup
->softpipe
->psize_slot
;
1194 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1195 : setup
->softpipe
->rasterizer
->point_size
;
1196 const float halfSize
= 0.5F
* size
;
1197 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
1198 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1199 const float y
= v0
[0][1];
1200 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
1204 debug_printf("Setup point:\n");
1205 print_vertex(setup
, v0
);
1208 if (softpipe
->no_rast
)
1211 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_POINTS
);
1213 /* For points, all interpolants are constant-valued.
1214 * However, for point sprites, we'll need to setup texcoords appropriately.
1215 * XXX: which coefficients are the texcoords???
1216 * We may do point sprites as textured quads...
1218 * KW: We don't know which coefficients are texcoords - ultimately
1219 * the choice of what interpolation mode to use for each attribute
1220 * should be determined by the fragment program, using
1221 * per-attribute declaration statements that include interpolation
1222 * mode as a parameter. So either the fragment program will have
1223 * to be adjusted for pointsprite vs normal point behaviour, or
1224 * otherwise a special interpolation mode will have to be defined
1225 * which matches the required behaviour for point sprites. But -
1226 * the latter is not a feature of normal hardware, and as such
1227 * probably should be ruled out on that basis.
1229 setup
->vprovoke
= v0
;
1232 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1233 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1235 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
1236 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1239 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1240 case INTERP_CONSTANT
:
1243 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1244 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1246 case INTERP_PERSPECTIVE
:
1247 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1248 point_persp_coeff(setup
, setup
->vprovoke
,
1249 &setup
->coef
[fragSlot
], vertSlot
, j
);
1252 setup_fragcoord_coeff(setup
, fragSlot
);
1258 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1259 /* convert 0 to 1.0 and 1 to -1.0 */
1260 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
1261 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1262 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1267 if (halfSize
<= 0.5 && !round
) {
1268 /* special case for 1-pixel points */
1269 const int ix
= ((int) x
) & 1;
1270 const int iy
= ((int) y
) & 1;
1271 setup
->quad
[0].input
.x0
= (int) x
- ix
;
1272 setup
->quad
[0].input
.y0
= (int) y
- iy
;
1273 setup
->quad
[0].inout
.mask
= (1 << ix
) << (2 * iy
);
1274 clip_emit_quad( setup
, &setup
->quad
[0] );
1278 /* rounded points */
1279 const int ixmin
= block((int) (x
- halfSize
));
1280 const int ixmax
= block((int) (x
+ halfSize
));
1281 const int iymin
= block((int) (y
- halfSize
));
1282 const int iymax
= block((int) (y
+ halfSize
));
1283 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1284 const float rmax
= halfSize
+ 0.7071F
;
1285 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1286 const float rmax2
= rmax
* rmax
;
1287 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1290 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1291 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1292 float dx
, dy
, dist2
, cover
;
1294 setup
->quad
[0].inout
.mask
= 0x0;
1296 dx
= (ix
+ 0.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_LEFT
] = MIN2(cover
, 1.0f
);
1302 setup
->quad
[0].inout
.mask
|= MASK_TOP_LEFT
;
1305 dx
= (ix
+ 1.5f
) - x
;
1306 dy
= (iy
+ 0.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_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1311 setup
->quad
[0].inout
.mask
|= MASK_TOP_RIGHT
;
1314 dx
= (ix
+ 0.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_LEFT
] = MIN2(cover
, 1.0f
);
1320 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_LEFT
;
1323 dx
= (ix
+ 1.5f
) - x
;
1324 dy
= (iy
+ 1.5f
) - y
;
1325 dist2
= dx
* dx
+ dy
* dy
;
1326 if (dist2
<= rmax2
) {
1327 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1328 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1329 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_RIGHT
;
1332 if (setup
->quad
[0].inout
.mask
) {
1333 setup
->quad
[0].input
.x0
= ix
;
1334 setup
->quad
[0].input
.y0
= iy
;
1335 clip_emit_quad( setup
, &setup
->quad
[0] );
1342 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1343 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1344 const int xmax
= xmin
+ (int) size
;
1345 const int ymax
= ymin
+ (int) size
;
1346 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1347 const int ixmin
= block(xmin
);
1348 const int ixmax
= block(xmax
- 1);
1349 const int iymin
= block(ymin
);
1350 const int iymax
= block(ymax
- 1);
1354 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1356 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1359 /* above the top edge */
1360 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1362 if (iy
+ 1 >= ymax
) {
1363 /* below the bottom edge */
1364 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1367 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1368 uint mask
= rowMask
;
1371 /* fragment is past left edge of point, turn off left bits */
1372 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1374 if (ix
+ 1 >= xmax
) {
1375 /* past the right edge */
1376 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1379 setup
->quad
[0].inout
.mask
= mask
;
1380 setup
->quad
[0].input
.x0
= ix
;
1381 setup
->quad
[0].input
.y0
= iy
;
1382 clip_emit_quad( setup
, &setup
->quad
[0] );
1391 * Called by vbuf code just before we start buffering primitives.
1394 sp_setup_prepare(struct setup_context
*setup
)
1396 struct softpipe_context
*sp
= setup
->softpipe
;
1399 softpipe_update_derived(sp
);
1402 /* Note: nr_attrs is only used for debugging (vertex printing) */
1403 setup
->nr_vertex_attrs
= draw_num_shader_outputs(sp
->draw
);
1405 sp
->quad
.first
->begin( sp
->quad
.first
);
1407 if (sp
->reduced_api_prim
== PIPE_PRIM_TRIANGLES
&&
1408 sp
->rasterizer
->fill_front
== PIPE_POLYGON_MODE_FILL
&&
1409 sp
->rasterizer
->fill_back
== PIPE_POLYGON_MODE_FILL
) {
1410 /* we'll do culling */
1411 setup
->cull_face
= sp
->rasterizer
->cull_face
;
1414 /* 'draw' will do culling */
1415 setup
->cull_face
= PIPE_FACE_NONE
;
1421 sp_setup_destroy_context(struct setup_context
*setup
)
1428 * Create a new primitive setup/render stage.
1430 struct setup_context
*
1431 sp_setup_create_context(struct softpipe_context
*softpipe
)
1433 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
1436 setup
->softpipe
= softpipe
;
1438 for (i
= 0; i
< MAX_QUADS
; i
++) {
1439 setup
->quad
[i
].coef
= setup
->coef
;
1440 setup
->quad
[i
].posCoef
= &setup
->posCoef
;
1443 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
1444 setup
->span
.left
[1] = 1000000; /* greater than right[1] */