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>
36 #include "sp_context.h"
37 #include "sp_headers.h"
40 #include "sp_prim_setup.h"
41 #include "draw/draw_private.h"
42 #include "draw/draw_vertex.h"
43 #include "pipe/p_util.h"
44 #include "pipe/p_shader_tokens.h"
52 float dx
; /**< X(v1) - X(v0), used only during setup */
53 float dy
; /**< Y(v1) - Y(v0), used only during setup */
54 float dxdy
; /**< dx/dy */
55 float sx
, sy
; /**< first sample point coord */
56 int lines
; /**< number of lines on this edge */
61 * Triangle setup info (derived from draw_stage).
62 * Also used for line drawing (taking some liberties).
65 struct draw_stage stage
; /**< This must be first (base class) */
67 struct softpipe_context
*softpipe
;
69 /* Vertices are just an array of floats making up each attribute in
70 * turn. Currently fixed at 4 floats, but should change in time.
71 * Codegen will help cope with this.
73 const struct vertex_header
*vmax
;
74 const struct vertex_header
*vmid
;
75 const struct vertex_header
*vmin
;
76 const struct vertex_header
*vprovoke
;
84 struct 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 */
100 * Basically a cast wrapper.
102 static INLINE
struct setup_stage
*setup_stage( struct draw_stage
*stage
)
104 return (struct setup_stage
*)stage
;
109 * Clip setup->quad against the scissor/surface bounds.
112 quad_clip(struct setup_stage
*setup
)
114 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
115 const int minx
= (int) cliprect
->minx
;
116 const int maxx
= (int) cliprect
->maxx
;
117 const int miny
= (int) cliprect
->miny
;
118 const int maxy
= (int) cliprect
->maxy
;
120 if (setup
->quad
.x0
>= maxx
||
121 setup
->quad
.y0
>= maxy
||
122 setup
->quad
.x0
+ 1 < minx
||
123 setup
->quad
.y0
+ 1 < miny
) {
124 /* totally clipped */
125 setup
->quad
.mask
= 0x0;
128 if (setup
->quad
.x0
< minx
)
129 setup
->quad
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
130 if (setup
->quad
.y0
< miny
)
131 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
132 if (setup
->quad
.x0
== maxx
- 1)
133 setup
->quad
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
134 if (setup
->quad
.y0
== maxy
- 1)
135 setup
->quad
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
140 * Emit a quad (pass to next stage) with clipping.
143 clip_emit_quad(struct setup_stage
*setup
)
146 if (setup
->quad
.mask
) {
147 struct softpipe_context
*sp
= setup
->softpipe
;
148 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
154 * Emit a quad (pass to next stage). No clipping is done.
157 emit_quad( struct setup_stage
*setup
, int x
, int y
, unsigned mask
)
159 struct softpipe_context
*sp
= setup
->softpipe
;
162 setup
->quad
.mask
= mask
;
163 sp
->quad
.first
->run(sp
->quad
.first
, &setup
->quad
);
168 * Given an X or Y coordinate, return the block/quad coordinate that it
171 static INLINE
int block( int x
)
178 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
179 * the triangle's bounds.
181 * this is pretty nasty... may need to rework flush_spans again to
182 * fix it, if possible.
184 static unsigned calculate_mask( struct setup_stage
*setup
, int x
)
188 if (x
>= setup
->span
.left
[0] && x
< setup
->span
.right
[0])
189 mask
|= MASK_TOP_LEFT
;
191 if (x
>= setup
->span
.left
[1] && x
< setup
->span
.right
[1])
192 mask
|= MASK_BOTTOM_LEFT
;
194 if (x
+1 >= setup
->span
.left
[0] && x
+1 < setup
->span
.right
[0])
195 mask
|= MASK_TOP_RIGHT
;
197 if (x
+1 >= setup
->span
.left
[1] && x
+1 < setup
->span
.right
[1])
198 mask
|= MASK_BOTTOM_RIGHT
;
205 * Render a horizontal span of quads
207 static void flush_spans( struct setup_stage
*setup
)
209 int minleft
, maxright
;
212 switch (setup
->span
.y_flags
) {
214 /* both odd and even lines written (both quad rows) */
215 minleft
= MIN2(setup
->span
.left
[0], setup
->span
.left
[1]);
216 maxright
= MAX2(setup
->span
.right
[0], setup
->span
.right
[1]);
220 /* only even line written (quad top row) */
221 minleft
= setup
->span
.left
[0];
222 maxright
= setup
->span
.right
[0];
226 /* only odd line written (quad bottom row) */
227 minleft
= setup
->span
.left
[1];
228 maxright
= setup
->span
.right
[1];
235 /* XXX this loop could be moved into the above switch cases and
236 * calculate_mask() could be simplified a bit...
238 for (x
= block(minleft
); x
<= block(maxright
); x
+= 2) {
239 emit_quad( setup
, x
, setup
->span
.y
,
240 calculate_mask( setup
, x
) );
244 setup
->span
.y_flags
= 0;
245 setup
->span
.right
[0] = 0;
246 setup
->span
.right
[1] = 0;
250 static void print_vertex(const struct setup_stage
*setup
,
251 const struct vertex_header
*v
)
254 debug_printf("Vertex: (%p)\n", v
);
255 for (i
= 0; i
< setup
->quad
.nr_attrs
; i
++) {
256 debug_printf(" %d: %f %f %f %f\n", i
,
257 v
->data
[i
][0], v
->data
[i
][1], v
->data
[i
][2], v
->data
[i
][3]);
262 static boolean
setup_sort_vertices( struct setup_stage
*setup
,
263 const struct prim_header
*prim
)
265 const struct vertex_header
*v0
= prim
->v
[0];
266 const struct vertex_header
*v1
= prim
->v
[1];
267 const struct vertex_header
*v2
= prim
->v
[2];
270 debug_printf("Triangle:\n");
271 print_vertex(setup
, v0
);
272 print_vertex(setup
, v1
);
273 print_vertex(setup
, v2
);
276 setup
->vprovoke
= v2
;
278 /* determine bottom to top order of vertices */
280 float y0
= v0
->data
[0][1];
281 float y1
= v1
->data
[0][1];
282 float y2
= v2
->data
[0][1];
325 setup
->ebot
.dx
= setup
->vmid
->data
[0][0] - setup
->vmin
->data
[0][0];
326 setup
->ebot
.dy
= setup
->vmid
->data
[0][1] - setup
->vmin
->data
[0][1];
327 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
328 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
329 setup
->etop
.dx
= setup
->vmax
->data
[0][0] - setup
->vmid
->data
[0][0];
330 setup
->etop
.dy
= setup
->vmax
->data
[0][1] - setup
->vmid
->data
[0][1];
333 * Compute triangle's area. Use 1/area to compute partial
334 * derivatives of attributes later.
336 * The area will be the same as prim->det, but the sign may be
337 * different depending on how the vertices get sorted above.
339 * To determine whether the primitive is front or back facing we
340 * use the prim->det value because its sign is correct.
343 const float area
= (setup
->emaj
.dx
* setup
->ebot
.dy
-
344 setup
->ebot
.dx
* setup
->emaj
.dy
);
346 setup
->oneoverarea
= 1.0f
/ area
;
348 debug_printf("%s one-over-area %f area %f det %f\n",
349 __FUNCTION__, setup->oneoverarea, area, prim->det );
353 /* We need to know if this is a front or back-facing triangle for:
354 * - the GLSL gl_FrontFacing fragment attribute (bool)
355 * - two-sided stencil test
357 setup
->quad
.facing
= (prim
->det
> 0.0) ^ (setup
->softpipe
->rasterizer
->front_winding
== PIPE_WINDING_CW
);
364 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
365 * The value value comes from vertex->data[slot][i].
366 * The result will be put into setup->coef[slot].a0[i].
367 * \param slot which attribute slot
368 * \param i which component of the slot (0..3)
370 static void const_coeff( struct setup_stage
*setup
,
371 struct tgsi_interp_coef
*coef
,
372 uint vertSlot
, uint i
)
379 /* need provoking vertex info!
381 coef
->a0
[i
] = setup
->vprovoke
->data
[vertSlot
][i
];
386 * Compute a0, dadx and dady for a linearly interpolated coefficient,
389 static void tri_linear_coeff( struct setup_stage
*setup
,
390 struct tgsi_interp_coef
*coef
,
391 uint vertSlot
, uint i
)
393 float botda
= setup
->vmid
->data
[vertSlot
][i
] - setup
->vmin
->data
[vertSlot
][i
];
394 float majda
= setup
->vmax
->data
[vertSlot
][i
] - setup
->vmin
->data
[vertSlot
][i
];
395 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
396 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
397 float dadx
= a
* setup
->oneoverarea
;
398 float dady
= b
* setup
->oneoverarea
;
402 coef
->dadx
[i
] = dadx
;
403 coef
->dady
[i
] = dady
;
405 /* calculate a0 as the value which would be sampled for the
406 * fragment at (0,0), taking into account that we want to sample at
407 * pixel centers, in other words (0.5, 0.5).
409 * this is neat but unfortunately not a good way to do things for
410 * triangles with very large values of dadx or dady as it will
411 * result in the subtraction and re-addition from a0 of a very
412 * large number, which means we'll end up loosing a lot of the
413 * fractional bits and precision from a0. the way to fix this is
414 * to define a0 as the sample at a pixel center somewhere near vmin
415 * instead - i'll switch to this later.
417 coef
->a0
[i
] = (setup
->vmin
->data
[vertSlot
][i
] -
418 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
419 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
422 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
424 setup->coef[slot].a0[i],
425 setup->coef[slot].dadx[i],
426 setup->coef[slot].dady[i]);
432 * Compute a0, dadx and dady for a perspective-corrected interpolant,
434 * We basically multiply the vertex value by 1/w before computing
435 * the plane coefficients (a0, dadx, dady).
436 * Later, when we compute the value at a particular fragment position we'll
437 * divide the interpolated value by the interpolated W at that fragment.
439 static void tri_persp_coeff( struct setup_stage
*setup
,
440 struct tgsi_interp_coef
*coef
,
441 uint vertSlot
, uint i
)
443 /* premultiply by 1/w (v->data[0][3] is always W):
445 float mina
= setup
->vmin
->data
[vertSlot
][i
] * setup
->vmin
->data
[0][3];
446 float mida
= setup
->vmid
->data
[vertSlot
][i
] * setup
->vmid
->data
[0][3];
447 float maxa
= setup
->vmax
->data
[vertSlot
][i
] * setup
->vmax
->data
[0][3];
448 float botda
= mida
- mina
;
449 float majda
= maxa
- mina
;
450 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
451 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
452 float dadx
= a
* setup
->oneoverarea
;
453 float dady
= b
* setup
->oneoverarea
;
456 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
457 setup->vmin->data[vertSlot][i],
458 setup->vmid->data[vertSlot][i],
459 setup->vmax->data[vertSlot][i]
464 coef
->dadx
[i
] = dadx
;
465 coef
->dady
[i
] = dady
;
466 coef
->a0
[i
] = (mina
-
467 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
468 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
473 * Special coefficient setup for gl_FragCoord.
474 * X and Y are trivial, though Y has to be inverted for OpenGL.
475 * Z and W are copied from posCoef which should have already been computed.
476 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
479 setup_fragcoord_coeff(struct setup_stage
*setup
, uint slot
)
482 setup
->coef
[slot
].a0
[0] = 0;
483 setup
->coef
[slot
].dadx
[0] = 1.0;
484 setup
->coef
[slot
].dady
[0] = 0.0;
486 if (setup
->softpipe
->rasterizer
->origin_lower_left
) {
488 const int winHeight
= setup
->softpipe
->framebuffer
.cbufs
[0]->height
;
489 setup
->coef
[slot
].a0
[1] = (float) (winHeight
- 1);
490 setup
->coef
[slot
].dady
[1] = -1.0;
494 setup
->coef
[slot
].a0
[1] = 0.0;
495 setup
->coef
[slot
].dady
[1] = 1.0;
497 setup
->coef
[slot
].dadx
[1] = 0.0;
499 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
500 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
501 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
503 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
504 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
505 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
511 * Compute the setup->coef[] array dadx, dady, a0 values.
512 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
514 static void setup_tri_coefficients( struct setup_stage
*setup
)
516 struct softpipe_context
*softpipe
= setup
->softpipe
;
517 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
518 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
521 /* z and w are done by linear interpolation:
523 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
524 tri_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
526 /* setup interpolation for all the remaining attributes:
528 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
529 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
532 switch (vinfo
->interp_mode
[fragSlot
]) {
533 case INTERP_CONSTANT
:
534 for (j
= 0; j
< NUM_CHANNELS
; j
++)
535 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
538 for (j
= 0; j
< NUM_CHANNELS
; j
++)
539 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
541 case INTERP_PERSPECTIVE
:
542 for (j
= 0; j
< NUM_CHANNELS
; j
++)
543 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
546 setup_fragcoord_coeff(setup
, fragSlot
);
552 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
553 /* FOG.y = front/back facing XXX fix this */
554 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
555 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
556 setup
->coef
[fragSlot
].dady
[1] = 0.0;
563 static void setup_tri_edges( struct setup_stage
*setup
)
565 float vmin_x
= setup
->vmin
->data
[0][0] + 0.5f
;
566 float vmid_x
= setup
->vmid
->data
[0][0] + 0.5f
;
568 float vmin_y
= setup
->vmin
->data
[0][1] - 0.5f
;
569 float vmid_y
= setup
->vmid
->data
[0][1] - 0.5f
;
570 float vmax_y
= setup
->vmax
->data
[0][1] - 0.5f
;
572 setup
->emaj
.sy
= CEILF(vmin_y
);
573 setup
->emaj
.lines
= (int) CEILF(vmax_y
- setup
->emaj
.sy
);
574 setup
->emaj
.dxdy
= setup
->emaj
.dx
/ setup
->emaj
.dy
;
575 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
577 setup
->etop
.sy
= CEILF(vmid_y
);
578 setup
->etop
.lines
= (int) CEILF(vmax_y
- setup
->etop
.sy
);
579 setup
->etop
.dxdy
= setup
->etop
.dx
/ setup
->etop
.dy
;
580 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
582 setup
->ebot
.sy
= CEILF(vmin_y
);
583 setup
->ebot
.lines
= (int) CEILF(vmid_y
- setup
->ebot
.sy
);
584 setup
->ebot
.dxdy
= setup
->ebot
.dx
/ setup
->ebot
.dy
;
585 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
590 * Render the upper or lower half of a triangle.
591 * Scissoring/cliprect is applied here too.
593 static void subtriangle( struct setup_stage
*setup
,
598 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
599 const int minx
= (int) cliprect
->minx
;
600 const int maxx
= (int) cliprect
->maxx
;
601 const int miny
= (int) cliprect
->miny
;
602 const int maxy
= (int) cliprect
->maxy
;
603 int y
, start_y
, finish_y
;
604 int sy
= (int)eleft
->sy
;
606 assert((int)eleft
->sy
== (int) eright
->sy
);
608 /* clip top/bottom */
610 finish_y
= sy
+ lines
;
622 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
625 for (y
= start_y
; y
< finish_y
; y
++) {
627 /* avoid accumulating adds as floats don't have the precision to
628 * accurately iterate large triangle edges that way. luckily we
629 * can just multiply these days.
631 * this is all drowned out by the attribute interpolation anyway.
633 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
634 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
636 /* clip left/right */
644 if (block(_y
) != setup
->span
.y
) {
646 setup
->span
.y
= block(_y
);
649 setup
->span
.left
[_y
&1] = left
;
650 setup
->span
.right
[_y
&1] = right
;
651 setup
->span
.y_flags
|= 1<<(_y
&1);
656 /* save the values so that emaj can be restarted:
658 eleft
->sx
+= lines
* eleft
->dxdy
;
659 eright
->sx
+= lines
* eright
->dxdy
;
666 * Do setup for triangle rasterization, then render the triangle.
668 static void setup_tri( struct draw_stage
*stage
,
669 struct prim_header
*prim
)
671 struct setup_stage
*setup
= setup_stage( stage
);
674 debug_printf("%s\n", __FUNCTION__ );
677 setup_sort_vertices( setup
, prim
);
678 setup_tri_coefficients( setup
);
679 setup_tri_edges( setup
);
681 setup
->quad
.prim
= PRIM_TRI
;
684 setup
->span
.y_flags
= 0;
685 setup
->span
.right
[0] = 0;
686 setup
->span
.right
[1] = 0;
687 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
689 /* init_constant_attribs( setup ); */
691 if (setup
->oneoverarea
< 0.0) {
694 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
695 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
700 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
701 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
704 flush_spans( setup
);
710 * Compute a0, dadx and dady for a linearly interpolated coefficient,
714 line_linear_coeff(struct setup_stage
*setup
,
715 struct tgsi_interp_coef
*coef
,
716 uint vertSlot
, uint i
)
718 const float da
= setup
->vmax
->data
[vertSlot
][i
] - setup
->vmin
->data
[vertSlot
][i
];
719 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
720 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
721 coef
->dadx
[i
] = dadx
;
722 coef
->dady
[i
] = dady
;
723 coef
->a0
[i
] = (setup
->vmin
->data
[vertSlot
][i
] -
724 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
725 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
730 * Compute a0, dadx and dady for a perspective-corrected interpolant,
734 line_persp_coeff(struct setup_stage
*setup
,
735 struct tgsi_interp_coef
*coef
,
736 uint vertSlot
, uint i
)
738 /* XXX double-check/verify this arithmetic */
739 const float a0
= setup
->vmin
->data
[vertSlot
][i
] * setup
->vmin
->data
[0][3];
740 const float a1
= setup
->vmax
->data
[vertSlot
][i
] * setup
->vmax
->data
[0][3];
741 const float da
= a1
- a0
;
742 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
743 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
744 coef
->dadx
[i
] = dadx
;
745 coef
->dady
[i
] = dady
;
746 coef
->a0
[i
] = (setup
->vmin
->data
[vertSlot
][i
] -
747 (dadx
* (setup
->vmin
->data
[0][0] - 0.5f
) +
748 dady
* (setup
->vmin
->data
[0][1] - 0.5f
)));
753 * Compute the setup->coef[] array dadx, dady, a0 values.
754 * Must be called after setup->vmin,vmax are initialized.
757 setup_line_coefficients(struct setup_stage
*setup
, struct prim_header
*prim
)
759 struct softpipe_context
*softpipe
= setup
->softpipe
;
760 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
761 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
764 /* use setup->vmin, vmax to point to vertices */
765 setup
->vprovoke
= prim
->v
[1];
766 setup
->vmin
= prim
->v
[0];
767 setup
->vmax
= prim
->v
[1];
769 setup
->emaj
.dx
= setup
->vmax
->data
[0][0] - setup
->vmin
->data
[0][0];
770 setup
->emaj
.dy
= setup
->vmax
->data
[0][1] - setup
->vmin
->data
[0][1];
771 /* NOTE: this is not really 1/area */
772 setup
->oneoverarea
= 1.0f
/ (setup
->emaj
.dx
* setup
->emaj
.dx
+
773 setup
->emaj
.dy
* setup
->emaj
.dy
);
775 /* z and w are done by linear interpolation:
777 line_linear_coeff(setup
, &setup
->posCoef
, 0, 2);
778 line_linear_coeff(setup
, &setup
->posCoef
, 0, 3);
780 /* setup interpolation for all the remaining attributes:
782 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
783 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
786 switch (vinfo
->interp_mode
[fragSlot
]) {
787 case INTERP_CONSTANT
:
788 for (j
= 0; j
< NUM_CHANNELS
; j
++)
789 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
792 for (j
= 0; j
< NUM_CHANNELS
; j
++)
793 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
795 case INTERP_PERSPECTIVE
:
796 for (j
= 0; j
< NUM_CHANNELS
; j
++)
797 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
800 setup_fragcoord_coeff(setup
, fragSlot
);
806 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
807 /* FOG.y = front/back facing XXX fix this */
808 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
809 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
810 setup
->coef
[fragSlot
].dady
[1] = 0.0;
817 * Plot a pixel in a line segment.
820 plot(struct setup_stage
*setup
, int x
, int y
)
822 const int iy
= y
& 1;
823 const int ix
= x
& 1;
824 const int quadX
= x
- ix
;
825 const int quadY
= y
- iy
;
826 const int mask
= (1 << ix
) << (2 * iy
);
828 if (quadX
!= setup
->quad
.x0
||
829 quadY
!= setup
->quad
.y0
)
831 /* flush prev quad, start new quad */
833 if (setup
->quad
.x0
!= -1)
834 clip_emit_quad(setup
);
836 setup
->quad
.x0
= quadX
;
837 setup
->quad
.y0
= quadY
;
838 setup
->quad
.mask
= 0x0;
841 setup
->quad
.mask
|= mask
;
846 * Do setup for line rasterization, then render the line.
847 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
848 * to handle stippling and wide lines.
851 setup_line(struct draw_stage
*stage
, struct prim_header
*prim
)
853 const struct vertex_header
*v0
= prim
->v
[0];
854 const struct vertex_header
*v1
= prim
->v
[1];
855 struct setup_stage
*setup
= setup_stage( stage
);
856 int x0
= (int) v0
->data
[0][0];
857 int x1
= (int) v1
->data
[0][0];
858 int y0
= (int) v0
->data
[0][1];
859 int y1
= (int) v1
->data
[0][1];
864 if (dx
== 0 && dy
== 0)
867 setup_line_coefficients(setup
, prim
);
870 dx
= -dx
; /* make positive */
878 dy
= -dy
; /* make positive */
888 setup
->quad
.x0
= setup
->quad
.y0
= -1;
889 setup
->quad
.mask
= 0x0;
890 setup
->quad
.prim
= PRIM_LINE
;
891 /* XXX temporary: set coverage to 1.0 so the line appears
892 * if AA mode happens to be enabled.
894 setup
->quad
.coverage
[0] =
895 setup
->quad
.coverage
[1] =
896 setup
->quad
.coverage
[2] =
897 setup
->quad
.coverage
[3] = 1.0;
900 /*** X-major line ***/
902 const int errorInc
= dy
+ dy
;
903 int error
= errorInc
- dx
;
904 const int errorDec
= error
- dx
;
906 for (i
= 0; i
< dx
; i
++) {
920 /*** Y-major line ***/
922 const int errorInc
= dx
+ dx
;
923 int error
= errorInc
- dy
;
924 const int errorDec
= error
- dy
;
926 for (i
= 0; i
< dy
; i
++) {
940 /* draw final quad */
941 if (setup
->quad
.mask
) {
942 clip_emit_quad(setup
);
948 point_persp_coeff(struct setup_stage
*setup
,
949 const struct vertex_header
*vert
,
950 struct tgsi_interp_coef
*coef
,
951 uint vertSlot
, uint i
)
954 coef
->dadx
[i
] = 0.0F
;
955 coef
->dady
[i
] = 0.0F
;
956 coef
->a0
[i
] = vert
->data
[vertSlot
][i
] * vert
->data
[0][3];
961 * Do setup for point rasterization, then render the point.
962 * Round or square points...
963 * XXX could optimize a lot for 1-pixel points.
966 setup_point(struct draw_stage
*stage
, struct prim_header
*prim
)
968 struct setup_stage
*setup
= setup_stage( stage
);
969 struct softpipe_context
*softpipe
= setup
->softpipe
;
970 const struct sp_fragment_shader
*spfs
= softpipe
->fs
;
971 const struct vertex_header
*v0
= prim
->v
[0];
972 const int sizeAttr
= setup
->softpipe
->psize_slot
;
974 = sizeAttr
> 0 ? v0
->data
[sizeAttr
][0]
975 : setup
->softpipe
->rasterizer
->point_size
;
976 const float halfSize
= 0.5F
* size
;
977 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
978 const float x
= v0
->data
[0][0]; /* Note: data[0] is always position */
979 const float y
= v0
->data
[0][1];
980 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
983 /* For points, all interpolants are constant-valued.
984 * However, for point sprites, we'll need to setup texcoords appropriately.
985 * XXX: which coefficients are the texcoords???
986 * We may do point sprites as textured quads...
988 * KW: We don't know which coefficients are texcoords - ultimately
989 * the choice of what interpolation mode to use for each attribute
990 * should be determined by the fragment program, using
991 * per-attribute declaration statements that include interpolation
992 * mode as a parameter. So either the fragment program will have
993 * to be adjusted for pointsprite vs normal point behaviour, or
994 * otherwise a special interpolation mode will have to be defined
995 * which matches the required behaviour for point sprites. But -
996 * the latter is not a feature of normal hardware, and as such
997 * probably should be ruled out on that basis.
999 setup
->vprovoke
= prim
->v
[0];
1002 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1003 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1005 for (fragSlot
= 0; fragSlot
< spfs
->info
.num_inputs
; fragSlot
++) {
1006 const uint vertSlot
= vinfo
->src_index
[fragSlot
];
1009 switch (vinfo
->interp_mode
[fragSlot
]) {
1010 case INTERP_CONSTANT
:
1013 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1014 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1016 case INTERP_PERSPECTIVE
:
1017 for (j
= 0; j
< NUM_CHANNELS
; j
++)
1018 point_persp_coeff(setup
, setup
->vprovoke
,
1019 &setup
->coef
[fragSlot
], vertSlot
, j
);
1022 setup_fragcoord_coeff(setup
, fragSlot
);
1028 if (spfs
->info
.input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FOG
) {
1029 /* FOG.y = front/back facing XXX fix this */
1030 setup
->coef
[fragSlot
].a0
[1] = 1.0f
- setup
->quad
.facing
;
1031 setup
->coef
[fragSlot
].dadx
[1] = 0.0;
1032 setup
->coef
[fragSlot
].dady
[1] = 0.0;
1036 setup
->quad
.prim
= PRIM_POINT
;
1038 if (halfSize
<= 0.5 && !round
) {
1039 /* special case for 1-pixel points */
1040 const int ix
= ((int) x
) & 1;
1041 const int iy
= ((int) y
) & 1;
1042 setup
->quad
.x0
= (int) x
- ix
;
1043 setup
->quad
.y0
= (int) y
- iy
;
1044 setup
->quad
.mask
= (1 << ix
) << (2 * iy
);
1045 clip_emit_quad(setup
);
1049 /* rounded points */
1050 const int ixmin
= block((int) (x
- halfSize
));
1051 const int ixmax
= block((int) (x
+ halfSize
));
1052 const int iymin
= block((int) (y
- halfSize
));
1053 const int iymax
= block((int) (y
+ halfSize
));
1054 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1055 const float rmax
= halfSize
+ 0.7071F
;
1056 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1057 const float rmax2
= rmax
* rmax
;
1058 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1061 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1062 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1063 float dx
, dy
, dist2
, cover
;
1065 setup
->quad
.mask
= 0x0;
1067 dx
= (ix
+ 0.5f
) - x
;
1068 dy
= (iy
+ 0.5f
) - y
;
1069 dist2
= dx
* dx
+ dy
* dy
;
1070 if (dist2
<= rmax2
) {
1071 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1072 setup
->quad
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1073 setup
->quad
.mask
|= MASK_TOP_LEFT
;
1076 dx
= (ix
+ 1.5f
) - x
;
1077 dy
= (iy
+ 0.5f
) - y
;
1078 dist2
= dx
* dx
+ dy
* dy
;
1079 if (dist2
<= rmax2
) {
1080 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1081 setup
->quad
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1082 setup
->quad
.mask
|= MASK_TOP_RIGHT
;
1085 dx
= (ix
+ 0.5f
) - x
;
1086 dy
= (iy
+ 1.5f
) - y
;
1087 dist2
= dx
* dx
+ dy
* dy
;
1088 if (dist2
<= rmax2
) {
1089 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1090 setup
->quad
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1091 setup
->quad
.mask
|= MASK_BOTTOM_LEFT
;
1094 dx
= (ix
+ 1.5f
) - x
;
1095 dy
= (iy
+ 1.5f
) - y
;
1096 dist2
= dx
* dx
+ dy
* dy
;
1097 if (dist2
<= rmax2
) {
1098 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1099 setup
->quad
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1100 setup
->quad
.mask
|= MASK_BOTTOM_RIGHT
;
1103 if (setup
->quad
.mask
) {
1104 setup
->quad
.x0
= ix
;
1105 setup
->quad
.y0
= iy
;
1106 clip_emit_quad(setup
);
1113 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1114 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1115 const int xmax
= xmin
+ (int) size
;
1116 const int ymax
= ymin
+ (int) size
;
1117 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1118 const int ixmin
= block(xmin
);
1119 const int ixmax
= block(xmax
- 1);
1120 const int iymin
= block(ymin
);
1121 const int iymax
= block(ymax
- 1);
1125 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1127 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1130 /* above the top edge */
1131 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1133 if (iy
+ 1 >= ymax
) {
1134 /* below the bottom edge */
1135 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1138 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1139 uint mask
= rowMask
;
1142 /* fragment is past left edge of point, turn off left bits */
1143 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1145 if (ix
+ 1 >= xmax
) {
1146 /* past the right edge */
1147 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1150 setup
->quad
.mask
= mask
;
1151 setup
->quad
.x0
= ix
;
1152 setup
->quad
.y0
= iy
;
1153 clip_emit_quad(setup
);
1162 static void setup_begin( struct draw_stage
*stage
)
1164 struct setup_stage
*setup
= setup_stage(stage
);
1165 struct softpipe_context
*sp
= setup
->softpipe
;
1166 const struct pipe_shader_state
*fs
= &setup
->softpipe
->fs
->shader
;
1169 softpipe_update_derived(sp
);
1172 setup
->quad
.nr_attrs
= fs
->num_inputs
;
1174 sp
->quad
.first
->begin(sp
->quad
.first
);
1176 stage
->point
= setup_point
;
1177 stage
->line
= setup_line
;
1178 stage
->tri
= setup_tri
;
1182 static void setup_first_point( struct draw_stage
*stage
,
1183 struct prim_header
*header
)
1186 stage
->point( stage
, header
);
1189 static void setup_first_line( struct draw_stage
*stage
,
1190 struct prim_header
*header
)
1193 stage
->line( stage
, header
);
1197 static void setup_first_tri( struct draw_stage
*stage
,
1198 struct prim_header
*header
)
1201 stage
->tri( stage
, header
);
1206 static void setup_flush( struct draw_stage
*stage
,
1209 stage
->point
= setup_first_point
;
1210 stage
->line
= setup_first_line
;
1211 stage
->tri
= setup_first_tri
;
1215 static void reset_stipple_counter( struct draw_stage
*stage
)
1220 static void render_destroy( struct draw_stage
*stage
)
1227 * Create a new primitive setup/render stage.
1229 struct draw_stage
*sp_draw_render_stage( struct softpipe_context
*softpipe
)
1231 struct setup_stage
*setup
= CALLOC_STRUCT(setup_stage
);
1233 setup
->softpipe
= softpipe
;
1234 setup
->stage
.draw
= softpipe
->draw
;
1235 setup
->stage
.point
= setup_first_point
;
1236 setup
->stage
.line
= setup_first_line
;
1237 setup
->stage
.tri
= setup_first_tri
;
1238 setup
->stage
.flush
= setup_flush
;
1239 setup
->stage
.reset_stipple_counter
= reset_stipple_counter
;
1240 setup
->stage
.destroy
= render_destroy
;
1242 setup
->quad
.coef
= setup
->coef
;
1243 setup
->quad
.posCoef
= &setup
->posCoef
;
1245 return &setup
->stage
;