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_private.h"
42 #include "draw/draw_vertex.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "pipe/p_thread.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
56 float dx
; /**< X(v1) - X(v0), used only during setup */
57 float dy
; /**< Y(v1) - Y(v0), used only during setup */
58 float dxdy
; /**< dx/dy */
59 float sx
, sy
; /**< first sample point coord */
60 int lines
; /**< number of lines on this edge */
68 * Triangle setup info (derived from draw_stage).
69 * Also used for line drawing (taking some liberties).
71 struct setup_context
{
72 struct softpipe_context
*softpipe
;
74 /* Vertices are just an array of floats making up each attribute in
75 * turn. Currently fixed at 4 floats, but should change in time.
76 * Codegen will help cope with this.
78 const float (*vmax
)[4];
79 const float (*vmid
)[4];
80 const float (*vmin
)[4];
81 const float (*vprovoke
)[4];
92 struct quad_header quad
[MAX_QUADS
];
93 struct quad_header
*quad_ptrs
[MAX_QUADS
];
96 struct tgsi_interp_coef coef
[PIPE_MAX_SHADER_INPUTS
];
97 struct tgsi_interp_coef posCoef
; /* For Z, W */
100 int left
[2]; /**< [0] = row0, [1] = row1 */
106 uint numFragsEmitted
; /**< per primitive */
107 uint numFragsWritten
; /**< per primitive */
110 unsigned winding
; /* which winding to cull */
111 unsigned nr_vertex_attrs
;
118 * Do triangle cull test using tri determinant (sign indicates orientation)
119 * \return true if triangle is to be culled.
121 static INLINE boolean
122 cull_tri(const struct setup_context
*setup
, float det
)
125 /* if (det < 0 then Z points toward camera and triangle is
126 * counter-clockwise winding.
128 unsigned winding
= (det
< 0) ? PIPE_WINDING_CCW
: PIPE_WINDING_CW
;
130 if ((winding
& setup
->winding
) == 0)
142 * Clip setup->quad against the scissor/surface bounds.
145 quad_clip( struct setup_context
*setup
, struct quad_header
*quad
)
147 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
148 const int minx
= (int) cliprect
->minx
;
149 const int maxx
= (int) cliprect
->maxx
;
150 const int miny
= (int) cliprect
->miny
;
151 const int maxy
= (int) cliprect
->maxy
;
153 if (quad
->input
.x0
>= maxx
||
154 quad
->input
.y0
>= maxy
||
155 quad
->input
.x0
+ 1 < minx
||
156 quad
->input
.y0
+ 1 < miny
) {
157 /* totally clipped */
158 quad
->inout
.mask
= 0x0;
161 if (quad
->input
.x0
< minx
)
162 quad
->inout
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
163 if (quad
->input
.y0
< miny
)
164 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
165 if (quad
->input
.x0
== maxx
- 1)
166 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
167 if (quad
->input
.y0
== maxy
- 1)
168 quad
->inout
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
173 * Emit a quad (pass to next stage) with clipping.
176 clip_emit_quad( struct setup_context
*setup
, struct quad_header
*quad
)
178 quad_clip( setup
, quad
);
180 if (quad
->inout
.mask
) {
181 struct softpipe_context
*sp
= setup
->softpipe
;
183 sp
->quad
.first
->run( sp
->quad
.first
, &quad
, 1 );
190 * Given an X or Y coordinate, return the block/quad coordinate that it
193 static INLINE
int block( int x
)
198 static INLINE
int block_x( int x
)
205 * Render a horizontal span of quads
207 static void flush_spans( struct setup_context
*setup
)
210 const int xleft0
= setup
->span
.left
[0];
211 const int xleft1
= setup
->span
.left
[1];
212 const int xright0
= setup
->span
.right
[0];
213 const int xright1
= setup
->span
.right
[1];
214 struct quad_stage
*pipe
= setup
->softpipe
->quad
.first
;
217 int minleft
= block_x(MIN2(xleft0
, xleft1
));
218 int maxright
= MAX2(xright0
, xright1
);
221 for (x
= minleft
; x
< maxright
; x
+= step
) {
222 unsigned skip_left0
= CLAMP(xleft0
- x
, 0, step
);
223 unsigned skip_left1
= CLAMP(xleft1
- x
, 0, step
);
224 unsigned skip_right0
= CLAMP(x
+ step
- xright0
, 0, step
);
225 unsigned skip_right1
= CLAMP(x
+ step
- xright1
, 0, step
);
229 unsigned skipmask_left0
= (1U << skip_left0
) - 1U;
230 unsigned skipmask_left1
= (1U << skip_left1
) - 1U;
232 /* These calculations fail when step == 32 and skip_right == 0.
234 unsigned skipmask_right0
= ~0U << (unsigned)(step
- skip_right0
);
235 unsigned skipmask_right1
= ~0U << (unsigned)(step
- skip_right1
);
237 unsigned mask0
= ~skipmask_left0
& ~skipmask_right0
;
238 unsigned mask1
= ~skipmask_left1
& ~skipmask_right1
;
242 unsigned quadmask
= (mask0
& 3) | ((mask1
& 3) << 2);
244 setup
->quad
[q
].input
.x0
= lx
;
245 setup
->quad
[q
].input
.y0
= setup
->span
.y
;
246 setup
->quad
[q
].input
.facing
= setup
->facing
;
247 setup
->quad
[q
].inout
.mask
= quadmask
;
248 setup
->quad_ptrs
[q
] = &setup
->quad
[q
];
254 } while (mask0
| mask1
);
256 pipe
->run( pipe
, setup
->quad_ptrs
, q
);
262 setup
->span
.right
[0] = 0;
263 setup
->span
.right
[1] = 0;
264 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
265 setup
->span
.left
[1] = 1000000; /* greater than right[1] */
270 static void print_vertex(const struct setup_context
*setup
,
274 debug_printf(" Vertex: (%p)\n", (void *) v
);
275 for (i
= 0; i
< setup
->nr_vertex_attrs
; i
++) {
276 debug_printf(" %d: %f %f %f %f\n", i
,
277 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
278 if (util_is_inf_or_nan(v
[i
][0])) {
279 debug_printf(" NaN!\n");
286 * Sort the vertices from top to bottom order, setting up the triangle
287 * edge fields (ebot, emaj, etop).
288 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
290 static boolean
setup_sort_vertices( struct setup_context
*setup
,
292 const float (*v0
)[4],
293 const float (*v1
)[4],
294 const float (*v2
)[4] )
296 setup
->vprovoke
= v2
;
298 /* determine bottom to top order of vertices */
345 setup
->ebot
.dx
= setup
->vmid
[0][0] - setup
->vmin
[0][0];
346 setup
->ebot
.dy
= setup
->vmid
[0][1] - setup
->vmin
[0][1];
347 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
348 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
349 setup
->etop
.dx
= setup
->vmax
[0][0] - setup
->vmid
[0][0];
350 setup
->etop
.dy
= setup
->vmax
[0][1] - setup
->vmid
[0][1];
353 * Compute triangle's area. Use 1/area to compute partial
354 * derivatives of attributes later.
356 * The area will be the same as prim->det, but the sign may be
357 * different depending on how the vertices get sorted above.
359 * To determine whether the primitive is front or back facing we
360 * use the prim->det value because its sign is correct.
363 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
364 setup
->ebot
.dx
* setup
->emaj
.dy
);
366 setup
->oneoverarea
= 1.0f
/ area
;
369 debug_printf("%s one-over-area %f area %f det %f\n",
370 __FUNCTION__, setup->oneoverarea, area, det );
372 if (util_is_inf_or_nan(setup
->oneoverarea
))
376 /* We need to know if this is a front or back-facing triangle for:
377 * - the GLSL gl_FrontFacing fragment attribute (bool)
378 * - two-sided stencil test
382 (setup
->softpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
));
384 /* Prepare pixel offset for rasterisation:
385 * - pixel center (0.5, 0.5) for GL, or
386 * - assume (0.0, 0.0) for other APIs.
388 if (setup
->softpipe
->rasterizer
->gl_rasterization_rules
) {
389 setup
->pixel_offset
= 0.5f
;
391 setup
->pixel_offset
= 0.0f
;
399 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
400 * The value value comes from vertex[slot][i].
401 * The result will be put into setup->coef[slot].a0[i].
402 * \param slot which attribute slot
403 * \param i which component of the slot (0..3)
405 static void const_coeff( struct setup_context
*setup
,
406 struct tgsi_interp_coef
*coef
,
407 uint vertSlot
, uint i
)
414 /* need provoking vertex info!
416 coef
->a0
[i
] = setup
->vprovoke
[vertSlot
][i
];
421 * Compute a0, dadx and dady for a linearly interpolated coefficient,
424 static void tri_linear_coeff( struct setup_context
*setup
,
425 struct tgsi_interp_coef
*coef
,
426 uint vertSlot
, uint i
)
428 float botda
= setup
->vmid
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
429 float majda
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
430 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
431 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
432 float dadx
= a
* setup
->oneoverarea
;
433 float dady
= b
* setup
->oneoverarea
;
437 coef
->dadx
[i
] = dadx
;
438 coef
->dady
[i
] = dady
;
440 /* calculate a0 as the value which would be sampled for the
441 * fragment at (0,0), taking into account that we want to sample at
442 * pixel centers, in other words (pixel_offset, pixel_offset).
444 * this is neat but unfortunately not a good way to do things for
445 * triangles with very large values of dadx or dady as it will
446 * result in the subtraction and re-addition from a0 of a very
447 * large number, which means we'll end up loosing a lot of the
448 * fractional bits and precision from a0. the way to fix this is
449 * to define a0 as the sample at a pixel center somewhere near vmin
450 * instead - i'll switch to this later.
452 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
453 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
454 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
457 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
459 setup->coef[slot].a0[i],
460 setup->coef[slot].dadx[i],
461 setup->coef[slot].dady[i]);
467 * Compute a0, dadx and dady for a perspective-corrected interpolant,
469 * We basically multiply the vertex value by 1/w before computing
470 * the plane coefficients (a0, dadx, dady).
471 * Later, when we compute the value at a particular fragment position we'll
472 * divide the interpolated value by the interpolated W at that fragment.
474 static void tri_persp_coeff( struct setup_context
*setup
,
475 struct tgsi_interp_coef
*coef
,
476 uint vertSlot
, uint i
)
478 /* premultiply by 1/w (v[0][3] is always W):
480 float mina
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
481 float mida
= setup
->vmid
[vertSlot
][i
] * setup
->vmid
[0][3];
482 float maxa
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
483 float botda
= mida
- mina
;
484 float majda
= maxa
- mina
;
485 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
486 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
487 float dadx
= a
* setup
->oneoverarea
;
488 float dady
= b
* setup
->oneoverarea
;
491 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
492 setup->vmin[vertSlot][i],
493 setup->vmid[vertSlot][i],
494 setup->vmax[vertSlot][i]
499 coef
->dadx
[i
] = dadx
;
500 coef
->dady
[i
] = dady
;
501 coef
->a0
[i
] = (mina
-
502 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
503 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
508 * Special coefficient setup for gl_FragCoord.
509 * X and Y are trivial, though Y has to be inverted for OpenGL.
510 * Z and W are copied from posCoef which should have already been computed.
511 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
514 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
517 setup
->coef
[slot
].a0
[0] = 0;
518 setup
->coef
[slot
].dadx
[0] = 1.0;
519 setup
->coef
[slot
].dady
[0] = 0.0;
521 setup
->coef
[slot
].a0
[1] = 0.0;
522 setup
->coef
[slot
].dadx
[1] = 0.0;
523 setup
->coef
[slot
].dady
[1] = 1.0;
525 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
526 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
527 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
529 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
530 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
531 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
537 * Compute the setup->coef[] array dadx, dady, a0 values.
538 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
540 static void setup_tri_coefficients( struct setup_context
*setup
)
542 struct softpipe_context
*softpipe
= setup
->softpipe
;
543 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
544 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
547 /* z and w are done by linear interpolation:
549 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
550 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
552 /* setup interpolation for all the remaining attributes:
554 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
555 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
558 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
559 case INTERP_CONSTANT
:
560 for (j
= 0; j
< NUM_CHANNELS
; j
++)
561 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
564 for (j
= 0; j
< NUM_CHANNELS
; j
++)
565 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
567 case INTERP_PERSPECTIVE
:
568 for (j
= 0; j
< NUM_CHANNELS
; j
++)
569 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
572 setup_fragcoord_coeff(setup
, fragSlot
);
578 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
579 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
580 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
581 setup
->coef
[fragSlot
].dady
[0] = 0.0;
588 static void setup_tri_edges( struct setup_context
*setup
)
590 float vmin_x
= setup
->vmin
[0][0] + setup
->pixel_offset
;
591 float vmid_x
= setup
->vmid
[0][0] + setup
->pixel_offset
;
593 float vmin_y
= setup
->vmin
[0][1] - setup
->pixel_offset
;
594 float vmid_y
= setup
->vmid
[0][1] - setup
->pixel_offset
;
595 float vmax_y
= setup
->vmax
[0][1] - setup
->pixel_offset
;
597 setup
->emaj
.sy
= ceilf(vmin_y
);
598 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
599 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
600 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
602 setup
->etop
.sy
= ceilf(vmid_y
);
603 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
604 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
605 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
607 setup
->ebot
.sy
= ceilf(vmin_y
);
608 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
609 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
610 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
615 * Render the upper or lower half of a triangle.
616 * Scissoring/cliprect is applied here too.
618 static void subtriangle( struct setup_context
*setup
,
623 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
624 const int minx
= (int) cliprect
->minx
;
625 const int maxx
= (int) cliprect
->maxx
;
626 const int miny
= (int) cliprect
->miny
;
627 const int maxy
= (int) cliprect
->maxy
;
628 int y
, start_y
, finish_y
;
629 int sy
= (int)eleft
->sy
;
631 assert((int)eleft
->sy
== (int) eright
->sy
);
633 /* clip top/bottom */
638 finish_y
= sy
+ lines
;
646 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
649 for (y
= start_y
; y
< finish_y
; y
++) {
651 /* avoid accumulating adds as floats don't have the precision to
652 * accurately iterate large triangle edges that way. luckily we
653 * can just multiply these days.
655 * this is all drowned out by the attribute interpolation anyway.
657 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
658 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
660 /* clip left/right */
668 if (block(_y
) != setup
->span
.y
) {
670 setup
->span
.y
= block(_y
);
673 setup
->span
.left
[_y
&1] = left
;
674 setup
->span
.right
[_y
&1] = right
;
679 /* save the values so that emaj can be restarted:
681 eleft
->sx
+= lines
* eleft
->dxdy
;
682 eright
->sx
+= lines
* eright
->dxdy
;
689 * Recalculate prim's determinant. This is needed as we don't have
690 * get this information through the vbuf_render interface & we must
694 calc_det( const float (*v0
)[4],
695 const float (*v1
)[4],
696 const float (*v2
)[4] )
698 /* edge vectors e = v0 - v2, f = v1 - v2 */
699 const float ex
= v0
[0][0] - v2
[0][0];
700 const float ey
= v0
[0][1] - v2
[0][1];
701 const float fx
= v1
[0][0] - v2
[0][0];
702 const float fy
= v1
[0][1] - v2
[0][1];
704 /* det = cross(e,f).z */
705 return ex
* fy
- ey
* fx
;
710 * Do setup for triangle rasterization, then render the triangle.
712 void sp_setup_tri( struct setup_context
*setup
,
713 const float (*v0
)[4],
714 const float (*v1
)[4],
715 const float (*v2
)[4] )
720 debug_printf("Setup triangle:\n");
721 print_vertex(setup
, v0
);
722 print_vertex(setup
, v1
);
723 print_vertex(setup
, v2
);
726 if (setup
->softpipe
->no_rast
)
729 det
= calc_det(v0
, v1
, v2
);
731 debug_printf("%s\n", __FUNCTION__ );
735 setup
->numFragsEmitted
= 0;
736 setup
->numFragsWritten
= 0;
739 if (cull_tri( setup
, det
))
742 if (!setup_sort_vertices( setup
, det
, v0
, v1
, v2
))
744 setup_tri_coefficients( setup
);
745 setup_tri_edges( setup
);
747 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_TRIANGLES
);
750 setup
->span
.right
[0] = 0;
751 setup
->span
.right
[1] = 0;
752 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
754 /* init_constant_attribs( setup ); */
756 if (setup
->oneoverarea
< 0.0) {
759 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
760 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
765 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
766 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
769 flush_spans( setup
);
772 printf("Tri: %u frags emitted, %u written\n",
773 setup
->numFragsEmitted
,
774 setup
->numFragsWritten
);
781 * Compute a0, dadx and dady for a linearly interpolated coefficient,
785 line_linear_coeff(const struct setup_context
*setup
,
786 struct tgsi_interp_coef
*coef
,
787 uint vertSlot
, uint i
)
789 const float da
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
790 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
791 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
792 coef
->dadx
[i
] = dadx
;
793 coef
->dady
[i
] = dady
;
794 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
795 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
796 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
801 * Compute a0, dadx and dady for a perspective-corrected interpolant,
805 line_persp_coeff(const struct setup_context
*setup
,
806 struct tgsi_interp_coef
*coef
,
807 uint vertSlot
, uint i
)
809 /* XXX double-check/verify this arithmetic */
810 const float a0
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
811 const float a1
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
812 const float da
= a1
- a0
;
813 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
814 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
815 coef
->dadx
[i
] = dadx
;
816 coef
->dady
[i
] = dady
;
817 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
818 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
819 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
824 * Compute the setup->coef[] array dadx, dady, a0 values.
825 * Must be called after setup->vmin,vmax are initialized.
827 static INLINE boolean
828 setup_line_coefficients(struct setup_context
*setup
,
829 const float (*v0
)[4],
830 const float (*v1
)[4])
832 struct softpipe_context
*softpipe
= setup
->softpipe
;
833 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
834 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
838 /* use setup->vmin, vmax to point to vertices */
839 if (softpipe
->rasterizer
->flatshade_first
)
840 setup
->vprovoke
= v0
;
842 setup
->vprovoke
= v1
;
846 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
847 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
849 /* NOTE: this is not really area but something proportional to it */
850 area
= setup
->emaj
.dx
* setup
->emaj
.dx
+ setup
->emaj
.dy
* setup
->emaj
.dy
;
851 if (area
== 0.0f
|| util_is_inf_or_nan(area
))
853 setup
->oneoverarea
= 1.0f
/ area
;
855 /* z and w are done by linear interpolation:
857 line_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
858 line_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
860 /* setup interpolation for all the remaining attributes:
862 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
863 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
866 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
867 case INTERP_CONSTANT
:
868 for (j
= 0; j
< NUM_CHANNELS
; j
++)
869 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
872 for (j
= 0; j
< NUM_CHANNELS
; j
++)
873 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
875 case INTERP_PERSPECTIVE
:
876 for (j
= 0; j
< NUM_CHANNELS
; j
++)
877 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
880 setup_fragcoord_coeff(setup
, fragSlot
);
886 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
887 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
888 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
889 setup
->coef
[fragSlot
].dady
[0] = 0.0;
897 * Plot a pixel in a line segment.
900 plot(struct setup_context
*setup
, int x
, int y
)
902 const int iy
= y
& 1;
903 const int ix
= x
& 1;
904 const int quadX
= x
- ix
;
905 const int quadY
= y
- iy
;
906 const int mask
= (1 << ix
) << (2 * iy
);
908 if (quadX
!= setup
->quad
[0].input
.x0
||
909 quadY
!= setup
->quad
[0].input
.y0
)
911 /* flush prev quad, start new quad */
913 if (setup
->quad
[0].input
.x0
!= -1)
914 clip_emit_quad( setup
, &setup
->quad
[0] );
916 setup
->quad
[0].input
.x0
= quadX
;
917 setup
->quad
[0].input
.y0
= quadY
;
918 setup
->quad
[0].inout
.mask
= 0x0;
921 setup
->quad
[0].inout
.mask
|= mask
;
926 * Do setup for line rasterization, then render the line.
927 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
928 * to handle stippling and wide lines.
931 sp_setup_line(struct setup_context
*setup
,
932 const float (*v0
)[4],
933 const float (*v1
)[4])
935 int x0
= (int) v0
[0][0];
936 int x1
= (int) v1
[0][0];
937 int y0
= (int) v0
[0][1];
938 int y1
= (int) v1
[0][1];
944 debug_printf("Setup line:\n");
945 print_vertex(setup
, v0
);
946 print_vertex(setup
, v1
);
949 if (setup
->softpipe
->no_rast
)
952 if (dx
== 0 && dy
== 0)
955 if (!setup_line_coefficients(setup
, v0
, v1
))
958 assert(v0
[0][0] < 1.0e9
);
959 assert(v0
[0][1] < 1.0e9
);
960 assert(v1
[0][0] < 1.0e9
);
961 assert(v1
[0][1] < 1.0e9
);
964 dx
= -dx
; /* make positive */
972 dy
= -dy
; /* make positive */
981 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_LINES
);
983 setup
->quad
[0].input
.x0
= setup
->quad
[0].input
.y0
= -1;
984 setup
->quad
[0].inout
.mask
= 0x0;
986 /* XXX temporary: set coverage to 1.0 so the line appears
987 * if AA mode happens to be enabled.
989 setup
->quad
[0].input
.coverage
[0] =
990 setup
->quad
[0].input
.coverage
[1] =
991 setup
->quad
[0].input
.coverage
[2] =
992 setup
->quad
[0].input
.coverage
[3] = 1.0;
995 /*** X-major line ***/
997 const int errorInc
= dy
+ dy
;
998 int error
= errorInc
- dx
;
999 const int errorDec
= error
- dx
;
1001 for (i
= 0; i
< dx
; i
++) {
1002 plot(setup
, x0
, y0
);
1015 /*** Y-major line ***/
1017 const int errorInc
= dx
+ dx
;
1018 int error
= errorInc
- dy
;
1019 const int errorDec
= error
- dy
;
1021 for (i
= 0; i
< dy
; i
++) {
1022 plot(setup
, x0
, y0
);
1035 /* draw final quad */
1036 if (setup
->quad
[0].inout
.mask
) {
1037 clip_emit_quad( setup
, &setup
->quad
[0] );
1043 point_persp_coeff(const struct setup_context
*setup
,
1044 const float (*vert
)[4],
1045 struct tgsi_interp_coef
*coef
,
1046 uint vertSlot
, uint i
)
1049 coef
->dadx
[i
] = 0.0F
;
1050 coef
->dady
[i
] = 0.0F
;
1051 coef
->a0
[i
] = vert
[vertSlot
][i
] * vert
[0][3];
1056 * Do setup for point rasterization, then render the point.
1057 * Round or square points...
1058 * XXX could optimize a lot for 1-pixel points.
1061 sp_setup_point( struct setup_context
*setup
,
1062 const float (*v0
)[4] )
1064 struct softpipe_context
*softpipe
= setup
->softpipe
;
1065 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
1066 const int sizeAttr
= setup
->softpipe
->psize_slot
;
1068 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1069 : setup
->softpipe
->rasterizer
->point_size
;
1070 const float halfSize
= 0.5F
* size
;
1071 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
1072 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1073 const float y
= v0
[0][1];
1074 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
1078 debug_printf("Setup point:\n");
1079 print_vertex(setup
, v0
);
1082 if (softpipe
->no_rast
)
1085 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_POINTS
);
1087 /* For points, all interpolants are constant-valued.
1088 * However, for point sprites, we'll need to setup texcoords appropriately.
1089 * XXX: which coefficients are the texcoords???
1090 * We may do point sprites as textured quads...
1092 * KW: We don't know which coefficients are texcoords - ultimately
1093 * the choice of what interpolation mode to use for each attribute
1094 * should be determined by the fragment program, using
1095 * per-attribute declaration statements that include interpolation
1096 * mode as a parameter. So either the fragment program will have
1097 * to be adjusted for pointsprite vs normal point behaviour, or
1098 * otherwise a special interpolation mode will have to be defined
1099 * which matches the required behaviour for point sprites. But -
1100 * the latter is not a feature of normal hardware, and as such
1101 * probably should be ruled out on that basis.
1103 setup
->vprovoke
= v0
;
1106 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1107 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1109 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
1110 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1113 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1114 case INTERP_CONSTANT
:
1117 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1118 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1120 case INTERP_PERSPECTIVE
:
1121 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1122 point_persp_coeff(setup
, setup
->vprovoke
,
1123 &setup
->coef
[fragSlot
], vertSlot
, j
);
1126 setup_fragcoord_coeff(setup
, fragSlot
);
1132 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1133 setup
->coef
[fragSlot
].a0
[0] = 1.0f
- setup
->facing
;
1134 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1135 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1140 if (halfSize
<= 0.5 && !round
) {
1141 /* special case for 1-pixel points */
1142 const int ix
= ((int) x
) & 1;
1143 const int iy
= ((int) y
) & 1;
1144 setup
->quad
[0].input
.x0
= (int) x
- ix
;
1145 setup
->quad
[0].input
.y0
= (int) y
- iy
;
1146 setup
->quad
[0].inout
.mask
= (1 << ix
) << (2 * iy
);
1147 clip_emit_quad( setup
, &setup
->quad
[0] );
1151 /* rounded points */
1152 const int ixmin
= block((int) (x
- halfSize
));
1153 const int ixmax
= block((int) (x
+ halfSize
));
1154 const int iymin
= block((int) (y
- halfSize
));
1155 const int iymax
= block((int) (y
+ halfSize
));
1156 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1157 const float rmax
= halfSize
+ 0.7071F
;
1158 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1159 const float rmax2
= rmax
* rmax
;
1160 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1163 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1164 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1165 float dx
, dy
, dist2
, cover
;
1167 setup
->quad
[0].inout
.mask
= 0x0;
1169 dx
= (ix
+ 0.5f
) - x
;
1170 dy
= (iy
+ 0.5f
) - y
;
1171 dist2
= dx
* dx
+ dy
* dy
;
1172 if (dist2
<= rmax2
) {
1173 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1174 setup
->quad
[0].input
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1175 setup
->quad
[0].inout
.mask
|= MASK_TOP_LEFT
;
1178 dx
= (ix
+ 1.5f
) - x
;
1179 dy
= (iy
+ 0.5f
) - y
;
1180 dist2
= dx
* dx
+ dy
* dy
;
1181 if (dist2
<= rmax2
) {
1182 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1183 setup
->quad
[0].input
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1184 setup
->quad
[0].inout
.mask
|= MASK_TOP_RIGHT
;
1187 dx
= (ix
+ 0.5f
) - x
;
1188 dy
= (iy
+ 1.5f
) - y
;
1189 dist2
= dx
* dx
+ dy
* dy
;
1190 if (dist2
<= rmax2
) {
1191 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1192 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1193 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_LEFT
;
1196 dx
= (ix
+ 1.5f
) - x
;
1197 dy
= (iy
+ 1.5f
) - y
;
1198 dist2
= dx
* dx
+ dy
* dy
;
1199 if (dist2
<= rmax2
) {
1200 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1201 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1202 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_RIGHT
;
1205 if (setup
->quad
[0].inout
.mask
) {
1206 setup
->quad
[0].input
.x0
= ix
;
1207 setup
->quad
[0].input
.y0
= iy
;
1208 clip_emit_quad( setup
, &setup
->quad
[0] );
1215 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1216 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1217 const int xmax
= xmin
+ (int) size
;
1218 const int ymax
= ymin
+ (int) size
;
1219 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1220 const int ixmin
= block(xmin
);
1221 const int ixmax
= block(xmax
- 1);
1222 const int iymin
= block(ymin
);
1223 const int iymax
= block(ymax
- 1);
1227 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1229 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1232 /* above the top edge */
1233 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1235 if (iy
+ 1 >= ymax
) {
1236 /* below the bottom edge */
1237 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1240 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1241 uint mask
= rowMask
;
1244 /* fragment is past left edge of point, turn off left bits */
1245 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1247 if (ix
+ 1 >= xmax
) {
1248 /* past the right edge */
1249 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1252 setup
->quad
[0].inout
.mask
= mask
;
1253 setup
->quad
[0].input
.x0
= ix
;
1254 setup
->quad
[0].input
.y0
= iy
;
1255 clip_emit_quad( setup
, &setup
->quad
[0] );
1262 void sp_setup_prepare( struct setup_context
*setup
)
1264 struct softpipe_context
*sp
= setup
->softpipe
;
1267 softpipe_update_derived(sp
);
1270 /* Note: nr_attrs is only used for debugging (vertex printing) */
1271 setup
->nr_vertex_attrs
= draw_num_shader_outputs(sp
->draw
);
1273 sp
->quad
.first
->begin( sp
->quad
.first
);
1275 if (sp
->reduced_api_prim
== PIPE_PRIM_TRIANGLES
&&
1276 sp
->rasterizer
->fill_cw
== PIPE_POLYGON_MODE_FILL
&&
1277 sp
->rasterizer
->fill_ccw
== PIPE_POLYGON_MODE_FILL
) {
1278 /* we'll do culling */
1279 setup
->winding
= sp
->rasterizer
->cull_mode
;
1282 /* 'draw' will do culling */
1283 setup
->winding
= PIPE_WINDING_NONE
;
1289 void sp_setup_destroy_context( struct setup_context
*setup
)
1296 * Create a new primitive setup/render stage.
1298 struct setup_context
*sp_setup_create_context( struct softpipe_context
*softpipe
)
1300 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
1303 setup
->softpipe
= softpipe
;
1305 for (i
= 0; i
< MAX_QUADS
; i
++) {
1306 setup
->quad
[i
].coef
= setup
->coef
;
1307 setup
->quad
[i
].posCoef
= &setup
->posCoef
;
1310 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
1311 setup
->span
.left
[1] = 1000000; /* greater than right[1] */