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>
37 #include "sp_context.h"
38 #include "sp_headers.h"
41 #include "sp_prim_setup.h"
42 #include "draw/draw_private.h"
43 #include "draw/draw_vertex.h"
44 #include "pipe/p_util.h"
45 #include "pipe/p_shader_tokens.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 */
63 * Triangle setup info (derived from draw_stage).
64 * Also used for line drawing (taking some liberties).
66 struct setup_context
{
67 struct softpipe_context
*softpipe
;
69 /* Vertices are just an array of floats making up each attribute in
70 * turn. Currently fixed at 4 floats, but should change in time.
71 * Codegen will help cope with this.
73 const float (*vmax
)[4];
74 const float (*vmid
)[4];
75 const float (*vmin
)[4];
76 const float (*vprovoke
)[4];
84 struct tgsi_interp_coef coef
[PIPE_MAX_SHADER_INPUTS
];
85 struct tgsi_interp_coef posCoef
; /* For Z, W */
86 struct quad_header quad
;
89 int left
[2]; /**< [0] = row0, [1] = row1 */
93 unsigned mask
; /**< mask of MASK_BOTTOM/TOP_LEFT/RIGHT bits */
97 uint numFragsEmitted
; /**< per primitive */
98 uint numFragsWritten
; /**< per primitive */
101 unsigned winding
; /* which winding to cull */
108 static boolean
cull_tri( struct setup_context
*setup
,
113 /* if (det < 0 then Z points toward camera and triangle is
114 * counter-clockwise winding.
116 unsigned winding
= (det
< 0) ? PIPE_WINDING_CCW
: PIPE_WINDING_CW
;
118 if ((winding
& setup
->winding
) == 0)
130 * Clip setup->quad against the scissor/surface bounds.
133 quad_clip(struct setup_context
*setup
)
135 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
136 const int minx
= (int) cliprect
->minx
;
137 const int maxx
= (int) cliprect
->maxx
;
138 const int miny
= (int) cliprect
->miny
;
139 const int maxy
= (int) cliprect
->maxy
;
141 if (setup
->quad
.x0
>= maxx
||
142 setup
->quad
.y0
>= maxy
||
143 setup
->quad
.x0
+ 1 < minx
||
144 setup
->quad
.y0
+ 1 < miny
) {
145 /* totally clipped */
146 setup
->quad
.mask
= 0x0;
149 if (setup
->quad
.x0
< minx
)
150 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
151 if (setup
->quad
.y0
< miny
)
152 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
153 if (setup
->quad
.x0
== maxx
- 1)
154 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
155 if (setup
->quad
.y0
== maxy
- 1)
156 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
161 * Emit a quad (pass to next stage) with clipping.
164 clip_emit_quad(struct setup_context
*setup
)
167 if (setup
->quad
.mask
) {
168 struct softpipe_context
*sp
= setup
->softpipe
;
169 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
175 * Emit a quad (pass to next stage). No clipping is done.
178 emit_quad( struct setup_context
*setup
, int x
, int y
, unsigned mask
)
180 struct softpipe_context
*sp
= setup
->softpipe
;
183 setup
->quad
.mask
= mask
;
185 if (mask
& 1) setup
->numFragsEmitted
++;
186 if (mask
& 2) setup
->numFragsEmitted
++;
187 if (mask
& 4) setup
->numFragsEmitted
++;
188 if (mask
& 8) setup
->numFragsEmitted
++;
190 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
192 mask
= setup
->quad
.mask
;
193 if (mask
& 1) setup
->numFragsWritten
++;
194 if (mask
& 2) setup
->numFragsWritten
++;
195 if (mask
& 4) setup
->numFragsWritten
++;
196 if (mask
& 8) setup
->numFragsWritten
++;
202 * Given an X or Y coordinate, return the block/quad coordinate that it
205 static INLINE
int block( int x
)
212 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
213 * the triangle's bounds.
215 * this is pretty nasty... may need to rework flush_spans again to
216 * fix it, if possible.
218 static unsigned calculate_mask( struct setup_context
*setup
, int x
)
222 if (x
>= setup
->span
.left
[0] && x
< setup
->span
.right
[0])
223 mask
|= MASK_TOP_LEFT
;
225 if (x
>= setup
->span
.left
[1] && x
< setup
->span
.right
[1])
226 mask
|= MASK_BOTTOM_LEFT
;
228 if (x
+1 >= setup
->span
.left
[0] && x
+1 < setup
->span
.right
[0])
229 mask
|= MASK_TOP_RIGHT
;
231 if (x
+1 >= setup
->span
.left
[1] && x
+1 < setup
->span
.right
[1])
232 mask
|= MASK_BOTTOM_RIGHT
;
239 * Render a horizontal span of quads
241 static void flush_spans( struct setup_context
*setup
)
243 int minleft
, maxright
;
246 switch (setup
->span
.y_flags
) {
248 /* both odd and even lines written (both quad rows) */
249 minleft
= MIN2(setup
->span
.left
[0], setup
->span
.left
[1]);
250 maxright
= MAX2(setup
->span
.right
[0], setup
->span
.right
[1]);
254 /* only even line written (quad top row) */
255 minleft
= setup
->span
.left
[0];
256 maxright
= setup
->span
.right
[0];
260 /* only odd line written (quad bottom row) */
261 minleft
= setup
->span
.left
[1];
262 maxright
= setup
->span
.right
[1];
269 /* XXX this loop could be moved into the above switch cases and
270 * calculate_mask() could be simplified a bit...
272 for (x
= block(minleft
); x
<= block(maxright
); x
+= 2) {
273 emit_quad( setup
, x
, setup
->span
.y
,
274 calculate_mask( setup
, x
) );
278 setup
->span
.y_flags
= 0;
279 setup
->span
.right
[0] = 0;
280 setup
->span
.right
[1] = 0;
284 static void print_vertex(const struct setup_context
*setup
,
288 debug_printf("Vertex: (%p)\n", v
);
289 for (i
= 0; i
< setup
->quad
.nr_attrs
; i
++) {
290 debug_printf(" %d: %f %f %f %f\n", i
,
291 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
296 static boolean
setup_sort_vertices( struct setup_context
*setup
,
298 const float (*v0
)[4],
299 const float (*v1
)[4],
300 const float (*v2
)[4] )
303 debug_printf("Triangle:\n");
304 print_vertex(setup
, v0
);
305 print_vertex(setup
, v1
);
306 print_vertex(setup
, v2
);
309 setup
->vprovoke
= v2
;
311 /* determine bottom to top order of vertices */
358 setup
->ebot
.dx
= setup
->vmid
[0][0] - setup
->vmin
[0][0];
359 setup
->ebot
.dy
= setup
->vmid
[0][1] - setup
->vmin
[0][1];
360 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
361 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
362 setup
->etop
.dx
= setup
->vmax
[0][0] - setup
->vmid
[0][0];
363 setup
->etop
.dy
= setup
->vmax
[0][1] - setup
->vmid
[0][1];
366 * Compute triangle's area. Use 1/area to compute partial
367 * derivatives of attributes later.
369 * The area will be the same as prim->det, but the sign may be
370 * different depending on how the vertices get sorted above.
372 * To determine whether the primitive is front or back facing we
373 * use the prim->det value because its sign is correct.
376 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
377 setup
->ebot
.dx
* setup
->emaj
.dy
);
379 setup
->oneoverarea
= 1.0f
/ area
;
381 debug_printf("%s one-over-area %f area %f det %f\n",
382 __FUNCTION__, setup->oneoverarea, area, det );
386 /* We need to know if this is a front or back-facing triangle for:
387 * - the GLSL gl_FrontFacing fragment attribute (bool)
388 * - two-sided stencil test
390 setup
->quad
.facing
= (det
> 0.0) ^ (setup
->softpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
);
397 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
398 * The value value comes from vertex[slot][i].
399 * The result will be put into setup->coef[slot].a0[i].
400 * \param slot which attribute slot
401 * \param i which component of the slot (0..3)
403 static void const_coeff( struct setup_context
*setup
,
404 struct tgsi_interp_coef
*coef
,
405 uint vertSlot
, uint i
)
412 /* need provoking vertex info!
414 coef
->a0
[i
] = setup
->vprovoke
[vertSlot
][i
];
419 * Compute a0, dadx and dady for a linearly interpolated coefficient,
422 static void tri_linear_coeff( struct setup_context
*setup
,
423 struct tgsi_interp_coef
*coef
,
424 uint vertSlot
, uint i
)
426 float botda
= setup
->vmid
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
427 float majda
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
428 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
429 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
430 float dadx
= a
* setup
->oneoverarea
;
431 float dady
= b
* setup
->oneoverarea
;
435 coef
->dadx
[i
] = dadx
;
436 coef
->dady
[i
] = dady
;
438 /* calculate a0 as the value which would be sampled for the
439 * fragment at (0,0), taking into account that we want to sample at
440 * pixel centers, in other words (0.5, 0.5).
442 * this is neat but unfortunately not a good way to do things for
443 * triangles with very large values of dadx or dady as it will
444 * result in the subtraction and re-addition from a0 of a very
445 * large number, which means we'll end up loosing a lot of the
446 * fractional bits and precision from a0. the way to fix this is
447 * to define a0 as the sample at a pixel center somewhere near vmin
448 * instead - i'll switch to this later.
450 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
451 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
452 dady
* (setup
->vmin
[0][1] - 0.5f
)));
455 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
457 setup->coef[slot].a0[i],
458 setup->coef[slot].dadx[i],
459 setup->coef[slot].dady[i]);
465 * Compute a0, dadx and dady for a perspective-corrected interpolant,
467 * We basically multiply the vertex value by 1/w before computing
468 * the plane coefficients (a0, dadx, dady).
469 * Later, when we compute the value at a particular fragment position we'll
470 * divide the interpolated value by the interpolated W at that fragment.
472 static void tri_persp_coeff( struct setup_context
*setup
,
473 struct tgsi_interp_coef
*coef
,
474 uint vertSlot
, uint i
)
476 /* premultiply by 1/w (v[0][3] is always W):
478 float mina
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
479 float mida
= setup
->vmid
[vertSlot
][i
] * setup
->vmid
[0][3];
480 float maxa
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
481 float botda
= mida
- mina
;
482 float majda
= maxa
- mina
;
483 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
484 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
485 float dadx
= a
* setup
->oneoverarea
;
486 float dady
= b
* setup
->oneoverarea
;
489 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
490 setup->vmin[vertSlot][i],
491 setup->vmid[vertSlot][i],
492 setup->vmax[vertSlot][i]
497 coef
->dadx
[i
] = dadx
;
498 coef
->dady
[i
] = dady
;
499 coef
->a0
[i
] = (mina
-
500 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
501 dady
* (setup
->vmin
[0][1] - 0.5f
)));
506 * Special coefficient setup for gl_FragCoord.
507 * X and Y are trivial, though Y has to be inverted for OpenGL.
508 * Z and W are copied from posCoef which should have already been computed.
509 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
512 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
515 setup
->coef
[slot
].a0
[0] = 0;
516 setup
->coef
[slot
].dadx
[0] = 1.0;
517 setup
->coef
[slot
].dady
[0] = 0.0;
519 if (setup
->softpipe
->rasterizer
->origin_lower_left
) {
521 const int winHeight
= setup
->softpipe
->framebuffer
.height
;
522 setup
->coef
[slot
].a0
[1] = (float) (winHeight
- 1);
523 setup
->coef
[slot
].dady
[1] = -1.0;
527 setup
->coef
[slot
].a0
[1] = 0.0;
528 setup
->coef
[slot
].dady
[1] = 1.0;
530 setup
->coef
[slot
].dadx
[1] = 0.0;
532 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
533 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
534 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
536 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
537 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
538 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
544 * Compute the setup->coef[] array dadx, dady, a0 values.
545 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
547 static void setup_tri_coefficients( struct setup_context
*setup
)
549 struct softpipe_context
*softpipe
= setup
->softpipe
;
550 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
551 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
554 /* z and w are done by linear interpolation:
556 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
557 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
559 /* setup interpolation for all the remaining attributes:
561 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
562 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
565 switch (vinfo
->interp_mode
[fragSlot
]) {
566 case INTERP_CONSTANT
:
567 for (j
= 0; j
< NUM_CHANNELS
; j
++)
568 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
571 for (j
= 0; j
< NUM_CHANNELS
; j
++)
572 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
574 case INTERP_PERSPECTIVE
:
575 for (j
= 0; j
< NUM_CHANNELS
; j
++)
576 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
579 setup_fragcoord_coeff(setup
, fragSlot
);
585 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
586 /* FOG.y = front/back facing XXX fix this */
587 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
588 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
589 setup
->coef
[fragSlot
].dady
[1] = 0.0;
596 static void setup_tri_edges( struct setup_context
*setup
)
598 float vmin_x
= setup
->vmin
[0][0] + 0.5f
;
599 float vmid_x
= setup
->vmid
[0][0] + 0.5f
;
601 float vmin_y
= setup
->vmin
[0][1] - 0.5f
;
602 float vmid_y
= setup
->vmid
[0][1] - 0.5f
;
603 float vmax_y
= setup
->vmax
[0][1] - 0.5f
;
605 setup
->emaj
.sy
= CEILF(vmin_y
);
606 setup
->emaj
.lines
= (int) CEILF(vmax_y
- setup
->emaj
.sy
);
607 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
608 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
610 setup
->etop
.sy
= CEILF(vmid_y
);
611 setup
->etop
.lines
= (int) CEILF(vmax_y
- setup
->etop
.sy
);
612 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
613 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
615 setup
->ebot
.sy
= CEILF(vmin_y
);
616 setup
->ebot
.lines
= (int) CEILF(vmid_y
- setup
->ebot
.sy
);
617 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
618 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
623 * Render the upper or lower half of a triangle.
624 * Scissoring/cliprect is applied here too.
626 static void subtriangle( struct setup_context
*setup
,
631 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
632 const int minx
= (int) cliprect
->minx
;
633 const int maxx
= (int) cliprect
->maxx
;
634 const int miny
= (int) cliprect
->miny
;
635 const int maxy
= (int) cliprect
->maxy
;
636 int y
, start_y
, finish_y
;
637 int sy
= (int)eleft
->sy
;
639 assert((int)eleft
->sy
== (int) eright
->sy
);
641 /* clip top/bottom */
643 finish_y
= sy
+ lines
;
655 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
658 for (y
= start_y
; y
< finish_y
; y
++) {
660 /* avoid accumulating adds as floats don't have the precision to
661 * accurately iterate large triangle edges that way. luckily we
662 * can just multiply these days.
664 * this is all drowned out by the attribute interpolation anyway.
666 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
667 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
669 /* clip left/right */
677 if (block(_y
) != setup
->span
.y
) {
679 setup
->span
.y
= block(_y
);
682 setup
->span
.left
[_y
&1] = left
;
683 setup
->span
.right
[_y
&1] = right
;
684 setup
->span
.y_flags
|= 1<<(_y
&1);
689 /* save the values so that emaj can be restarted:
691 eleft
->sx
+= lines
* eleft
->dxdy
;
692 eright
->sx
+= lines
* eright
->dxdy
;
699 * Recalculate prim's determinant. This is needed as we don't have
700 * get this information through the vbuf_render interface & we must
704 calc_det( const float (*v0
)[4],
705 const float (*v1
)[4],
706 const float (*v2
)[4] )
708 /* edge vectors e = v0 - v2, f = v1 - v2 */
709 const float ex
= v0
[0][0] - v2
[0][0];
710 const float ey
= v0
[0][1] - v2
[0][1];
711 const float fx
= v1
[0][0] - v2
[0][0];
712 const float fy
= v1
[0][1] - v2
[0][1];
714 /* det = cross(e,f).z */
715 return ex
* fy
- ey
* fx
;
720 * Do setup for triangle rasterization, then render the triangle.
722 void setup_tri( struct setup_context
*setup
,
723 const float (*v0
)[4],
724 const float (*v1
)[4],
725 const float (*v2
)[4] )
727 float det
= calc_det(v0
, v1
, v2
);
730 debug_printf("%s\n", __FUNCTION__ );
734 setup
->numFragsEmitted
= 0;
735 setup
->numFragsWritten
= 0;
740 if (cull_tri( setup
, det
))
743 setup_sort_vertices( setup
, det
, v0
, v1
, v2
);
744 setup_tri_coefficients( setup
);
745 setup_tri_edges( setup
);
747 setup
->quad
.prim
= PRIM_TRI
;
750 setup
->span
.y_flags
= 0;
751 setup
->span
.right
[0] = 0;
752 setup
->span
.right
[1] = 0;
753 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
755 /* init_constant_attribs( setup ); */
757 if (setup
->oneoverarea
< 0.0) {
760 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
761 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
766 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
767 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
770 flush_spans( setup
);
773 printf("Tri: %u frags emitted, %u written\n",
774 setup
->numFragsEmitted
,
775 setup
->numFragsWritten
);
782 * Compute a0, dadx and dady for a linearly interpolated coefficient,
786 line_linear_coeff(struct setup_context
*setup
,
787 struct tgsi_interp_coef
*coef
,
788 uint vertSlot
, uint i
)
790 const float da
= setup
->vmax
[vertSlot
][i
] - setup
->vmin
[vertSlot
][i
];
791 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
792 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
793 coef
->dadx
[i
] = dadx
;
794 coef
->dady
[i
] = dady
;
795 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
796 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
797 dady
* (setup
->vmin
[0][1] - 0.5f
)));
802 * Compute a0, dadx and dady for a perspective-corrected interpolant,
806 line_persp_coeff(struct setup_context
*setup
,
807 struct tgsi_interp_coef
*coef
,
808 uint vertSlot
, uint i
)
810 /* XXX double-check/verify this arithmetic */
811 const float a0
= setup
->vmin
[vertSlot
][i
] * setup
->vmin
[0][3];
812 const float a1
= setup
->vmax
[vertSlot
][i
] * setup
->vmax
[0][3];
813 const float da
= a1
- a0
;
814 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
815 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
816 coef
->dadx
[i
] = dadx
;
817 coef
->dady
[i
] = dady
;
818 coef
->a0
[i
] = (setup
->vmin
[vertSlot
][i
] -
819 (dadx
* (setup
->vmin
[0][0] - 0.5f
) +
820 dady
* (setup
->vmin
[0][1] - 0.5f
)));
825 * Compute the setup->coef[] array dadx, dady, a0 values.
826 * Must be called after setup->vmin,vmax are initialized.
829 setup_line_coefficients(struct setup_context
*setup
,
830 const float (*v0
)[4],
831 const float (*v1
)[4])
833 struct softpipe_context
*softpipe
= setup
->softpipe
;
834 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
835 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
838 /* use setup->vmin, vmax to point to vertices */
839 setup
->vprovoke
= v1
;
843 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
844 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
845 /* NOTE: this is not really 1/area */
846 setup
->oneoverarea
= 1.0f
/ (setup
->emaj
.dx
* setup
->emaj
.dx
+
847 setup
->emaj
.dy
* setup
->emaj
.dy
);
849 /* z and w are done by linear interpolation:
851 line_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
852 line_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
854 /* setup interpolation for all the remaining attributes:
856 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
857 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
860 switch (vinfo
->interp_mode
[fragSlot
]) {
861 case INTERP_CONSTANT
:
862 for (j
= 0; j
< NUM_CHANNELS
; j
++)
863 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
866 for (j
= 0; j
< NUM_CHANNELS
; j
++)
867 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
869 case INTERP_PERSPECTIVE
:
870 for (j
= 0; j
< NUM_CHANNELS
; j
++)
871 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
874 setup_fragcoord_coeff(setup
, fragSlot
);
880 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
881 /* FOG.y = front/back facing XXX fix this */
882 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
883 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
884 setup
->coef
[fragSlot
].dady
[1] = 0.0;
891 * Plot a pixel in a line segment.
894 plot(struct setup_context
*setup
, int x
, int y
)
896 const int iy
= y
& 1;
897 const int ix
= x
& 1;
898 const int quadX
= x
- ix
;
899 const int quadY
= y
- iy
;
900 const int mask
= (1 << ix
) << (2 * iy
);
902 if (quadX
!= setup
->quad
.x0
||
903 quadY
!= setup
->quad
.y0
)
905 /* flush prev quad, start new quad */
907 if (setup
->quad
.x0
!= -1)
908 clip_emit_quad(setup
);
910 setup
->quad
.x0
= quadX
;
911 setup
->quad
.y0
= quadY
;
912 setup
->quad
.mask
= 0x0;
915 setup
->quad
.mask
|= mask
;
920 * Do setup for line rasterization, then render the line.
921 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
922 * to handle stippling and wide lines.
925 setup_line(struct setup_context
*setup
,
926 const float (*v0
)[4],
927 const float (*v1
)[4])
929 int x0
= (int) v0
[0][0];
930 int x1
= (int) v1
[0][0];
931 int y0
= (int) v0
[0][1];
932 int y1
= (int) v1
[0][1];
937 if (dx
== 0 && dy
== 0)
940 setup_line_coefficients(setup
, v0
, v1
);
943 dx
= -dx
; /* make positive */
951 dy
= -dy
; /* make positive */
961 setup
->quad
.x0
= setup
->quad
.y0
= -1;
962 setup
->quad
.mask
= 0x0;
963 setup
->quad
.prim
= PRIM_LINE
;
964 /* XXX temporary: set coverage to 1.0 so the line appears
965 * if AA mode happens to be enabled.
967 setup
->quad
.coverage
[0] =
968 setup
->quad
.coverage
[1] =
969 setup
->quad
.coverage
[2] =
970 setup
->quad
.coverage
[3] = 1.0;
973 /*** X-major line ***/
975 const int errorInc
= dy
+ dy
;
976 int error
= errorInc
- dx
;
977 const int errorDec
= error
- dx
;
979 for (i
= 0; i
< dx
; i
++) {
993 /*** Y-major line ***/
995 const int errorInc
= dx
+ dx
;
996 int error
= errorInc
- dy
;
997 const int errorDec
= error
- dy
;
999 for (i
= 0; i
< dy
; i
++) {
1000 plot(setup
, x0
, y0
);
1013 /* draw final quad */
1014 if (setup
->quad
.mask
) {
1015 clip_emit_quad(setup
);
1021 point_persp_coeff(struct setup_context
*setup
,
1022 const float (*vert
)[4],
1023 struct tgsi_interp_coef
*coef
,
1024 uint vertSlot
, uint i
)
1027 coef
->dadx
[i
] = 0.0F
;
1028 coef
->dady
[i
] = 0.0F
;
1029 coef
->a0
[i
] = vert
[vertSlot
][i
] * vert
[0][3];
1034 * Do setup for point rasterization, then render the point.
1035 * Round or square points...
1036 * XXX could optimize a lot for 1-pixel points.
1039 setup_point( struct setup_context
*setup
,
1040 const float (*v0
)[4] )
1042 struct softpipe_context
*softpipe
= setup
->softpipe
;
1043 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
1044 const int sizeAttr
= setup
->softpipe
->psize_slot
;
1046 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1047 : setup
->softpipe
->rasterizer
->point_size
;
1048 const float halfSize
= 0.5F
* size
;
1049 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
1050 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1051 const float y
= v0
[0][1];
1052 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
1055 /* For points, all interpolants are constant-valued.
1056 * However, for point sprites, we'll need to setup texcoords appropriately.
1057 * XXX: which coefficients are the texcoords???
1058 * We may do point sprites as textured quads...
1060 * KW: We don't know which coefficients are texcoords - ultimately
1061 * the choice of what interpolation mode to use for each attribute
1062 * should be determined by the fragment program, using
1063 * per-attribute declaration statements that include interpolation
1064 * mode as a parameter. So either the fragment program will have
1065 * to be adjusted for pointsprite vs normal point behaviour, or
1066 * otherwise a special interpolation mode will have to be defined
1067 * which matches the required behaviour for point sprites. But -
1068 * the latter is not a feature of normal hardware, and as such
1069 * probably should be ruled out on that basis.
1071 setup
->vprovoke
= v0
;
1074 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1075 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1077 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
1078 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
1081 switch (vinfo
->interp_mode
[fragSlot
]) {
1082 case INTERP_CONSTANT
:
1085 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1086 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1088 case INTERP_PERSPECTIVE
:
1089 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1090 point_persp_coeff(setup
, setup
->vprovoke
,
1091 &setup
->coef
[fragSlot
], vertSlot
, j
);
1094 setup_fragcoord_coeff(setup
, fragSlot
);
1100 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
1101 /* FOG.y = front/back facing XXX fix this */
1102 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
1103 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
1104 setup
->coef
[fragSlot
].dady
[1] = 0.0;
1108 setup
->quad
.prim
= PRIM_POINT
;
1110 if (halfSize
<= 0.5 && !round
) {
1111 /* special case for 1-pixel points */
1112 const int ix
= ((int) x
) & 1;
1113 const int iy
= ((int) y
) & 1;
1114 setup
->quad
.x0
= (int) x
- ix
;
1115 setup
->quad
.y0
= (int) y
- iy
;
1116 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
1117 clip_emit_quad(setup
);
1121 /* rounded points */
1122 const int ixmin
= block((int) (x
- halfSize
));
1123 const int ixmax
= block((int) (x
+ halfSize
));
1124 const int iymin
= block((int) (y
- halfSize
));
1125 const int iymax
= block((int) (y
+ halfSize
));
1126 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1127 const float rmax
= halfSize
+ 0.7071F
;
1128 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1129 const float rmax2
= rmax
* rmax
;
1130 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1133 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1134 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1135 float dx
, dy
, dist2
, cover
;
1137 setup
->quad
.mask
= 0x0;
1139 dx
= (ix
+ 0.5f
) - x
;
1140 dy
= (iy
+ 0.5f
) - y
;
1141 dist2
= dx
* dx
+ dy
* dy
;
1142 if (dist2
<= rmax2
) {
1143 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1144 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1145 setup
->quad
.mask
|= MASK_TOP_LEFT
;
1148 dx
= (ix
+ 1.5f
) - x
;
1149 dy
= (iy
+ 0.5f
) - y
;
1150 dist2
= dx
* dx
+ dy
* dy
;
1151 if (dist2
<= rmax2
) {
1152 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1153 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1154 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
1157 dx
= (ix
+ 0.5f
) - x
;
1158 dy
= (iy
+ 1.5f
) - y
;
1159 dist2
= dx
* dx
+ dy
* dy
;
1160 if (dist2
<= rmax2
) {
1161 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1162 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1163 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
1166 dx
= (ix
+ 1.5f
) - x
;
1167 dy
= (iy
+ 1.5f
) - y
;
1168 dist2
= dx
* dx
+ dy
* dy
;
1169 if (dist2
<= rmax2
) {
1170 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1171 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1172 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
1175 if (setup
->quad
.mask
) {
1176 setup
->quad
.x0
= ix
;
1177 setup
->quad
.y0
= iy
;
1178 clip_emit_quad(setup
);
1185 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1186 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1187 const int xmax
= xmin
+ (int) size
;
1188 const int ymax
= ymin
+ (int) size
;
1189 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1190 const int ixmin
= block(xmin
);
1191 const int ixmax
= block(xmax
- 1);
1192 const int iymin
= block(ymin
);
1193 const int iymax
= block(ymax
- 1);
1197 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1199 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1202 /* above the top edge */
1203 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1205 if (iy
+ 1 >= ymax
) {
1206 /* below the bottom edge */
1207 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1210 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1211 uint mask
= rowMask
;
1214 /* fragment is past left edge of point, turn off left bits */
1215 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1217 if (ix
+ 1 >= xmax
) {
1218 /* past the right edge */
1219 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1222 setup
->quad
.mask
= mask
;
1223 setup
->quad
.x0
= ix
;
1224 setup
->quad
.y0
= iy
;
1225 clip_emit_quad(setup
);
1232 void setup_prepare( struct setup_context
*setup
)
1234 struct softpipe_context
*sp
= setup
->softpipe
;
1238 softpipe_update_derived(sp
);
1241 /* Mark surfaces as defined now */
1242 for (i
= 0; i
< sp
->framebuffer
.num_cbufs
; i
++){
1243 if (sp
->framebuffer
.cbufs
[i
]) {
1244 sp
->framebuffer
.cbufs
[i
]->status
= PIPE_SURFACE_STATUS_DEFINED
;
1247 if (sp
->framebuffer
.zsbuf
) {
1248 sp
->framebuffer
.zsbuf
->status
= PIPE_SURFACE_STATUS_DEFINED
;
1252 const struct sp_fragment_shader
*fs
= setup
->softpipe
->fs
;
1253 setup
->quad
.nr_attrs
= fs
->info
.num_inputs
;
1254 sp
->quad
.first
->begin(sp
->quad
.first
);
1257 setup
->winding
= sp
->rasterizer
->cull_mode
;
1262 void setup_destroy_context( struct setup_context
*setup
)
1269 * Create a new primitive setup/render stage.
1271 struct setup_context
*setup_create_context( struct softpipe_context
*softpipe
)
1273 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
1275 setup
->softpipe
= softpipe
;
1277 setup
->quad
.coef
= setup
->coef
;
1278 setup
->quad
.posCoef
= &setup
->posCoef
;