1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * \brief Primitive rasterization/rendering (points, lines, triangles)
31 * \author Keith Whitwell <keith@tungstengraphics.com>
35 #include "sp_context.h"
37 #include "sp_quad_pipe.h"
40 #include "draw/draw_context.h"
41 #include "draw/draw_vertex.h"
42 #include "pipe/p_shader_tokens.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
55 float dx
; /**< X(v1) - X(v0), used only during setup */
56 float dy
; /**< Y(v1) - Y(v0), used only during setup */
57 float dxdy
; /**< dx/dy */
58 float sx
, sy
; /**< first sample point coord */
59 int lines
; /**< number of lines on this edge */
64 * Max number of quads (2x2 pixel blocks) to process per batch.
65 * This can't be arbitrarily increased since we depend on some 32-bit
66 * bitmasks (two bits per quad).
72 * Triangle setup info.
73 * Also used for line drawing (taking some liberties).
75 struct setup_context
{
76 struct softpipe_context
*softpipe
;
78 /* Vertices are just an array of floats making up each attribute in
79 * turn. Currently fixed at 4 floats, but should change in time.
80 * Codegen will help cope with this.
82 const float (*vmax
)[4];
83 const float (*vmid
)[4];
84 const float (*vmin
)[4];
85 const float (*vprovoke
)[4];
96 struct quad_header quad
[MAX_QUADS
];
97 struct quad_header
*quad_ptrs
[MAX_QUADS
];
100 struct tgsi_interp_coef coef
[PIPE_MAX_SHADER_INPUTS
];
101 struct tgsi_interp_coef posCoef
; /* For Z, W */
104 int left
[2]; /**< [0] = row0, [1] = row1 */
110 uint numFragsEmitted
; /**< per primitive */
111 uint numFragsWritten
; /**< per primitive */
114 unsigned cull_face
; /* which faces cull */
115 unsigned nr_vertex_attrs
;
125 * Clip setup->quad against the scissor/surface bounds.
128 quad_clip(struct setup_context
*setup
, struct quad_header
*quad
)
130 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
131 const int minx
= (int) cliprect
->minx
;
132 const int maxx
= (int) cliprect
->maxx
;
133 const int miny
= (int) cliprect
->miny
;
134 const int maxy
= (int) cliprect
->maxy
;
136 if (quad
->input
.x0
>= maxx
||
137 quad
->input
.y0
>= maxy
||
138 quad
->input
.x0
+ 1 < minx
||
139 quad
->input
.y0
+ 1 < miny
) {
140 /* totally clipped */
141 quad
->inout
.mask
= 0x0;
144 if (quad
->input
.x0
< minx
)
145 quad
->inout
.mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
146 if (quad
->input
.y0
< miny
)
147 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
148 if (quad
->input
.x0
== maxx
- 1)
149 quad
->inout
.mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
150 if (quad
->input
.y0
== maxy
- 1)
151 quad
->inout
.mask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
156 * Emit a quad (pass to next stage) with clipping.
159 clip_emit_quad(struct setup_context
*setup
, struct quad_header
*quad
)
161 quad_clip( setup
, quad
);
163 if (quad
->inout
.mask
) {
164 struct softpipe_context
*sp
= setup
->softpipe
;
167 setup
->numFragsEmitted
+= util_bitcount(quad
->inout
.mask
);
170 sp
->quad
.first
->run( sp
->quad
.first
, &quad
, 1 );
177 * Given an X or Y coordinate, return the block/quad coordinate that it
195 * Render a horizontal span of quads
198 flush_spans(struct setup_context
*setup
)
200 const int step
= MAX_QUADS
;
201 const int xleft0
= setup
->span
.left
[0];
202 const int xleft1
= setup
->span
.left
[1];
203 const int xright0
= setup
->span
.right
[0];
204 const int xright1
= setup
->span
.right
[1];
205 struct quad_stage
*pipe
= setup
->softpipe
->quad
.first
;
207 const int minleft
= block_x(MIN2(xleft0
, xleft1
));
208 const int maxright
= MAX2(xright0
, xright1
);
211 /* process quads in horizontal chunks of 16 */
212 for (x
= minleft
; x
< maxright
; x
+= step
) {
213 unsigned skip_left0
= CLAMP(xleft0
- x
, 0, step
);
214 unsigned skip_left1
= CLAMP(xleft1
- x
, 0, step
);
215 unsigned skip_right0
= CLAMP(x
+ step
- xright0
, 0, step
);
216 unsigned skip_right1
= CLAMP(x
+ step
- xright1
, 0, step
);
220 unsigned skipmask_left0
= (1U << skip_left0
) - 1U;
221 unsigned skipmask_left1
= (1U << skip_left1
) - 1U;
223 /* These calculations fail when step == 32 and skip_right == 0.
225 unsigned skipmask_right0
= ~0U << (unsigned)(step
- skip_right0
);
226 unsigned skipmask_right1
= ~0U << (unsigned)(step
- skip_right1
);
228 unsigned mask0
= ~skipmask_left0
& ~skipmask_right0
;
229 unsigned mask1
= ~skipmask_left1
& ~skipmask_right1
;
233 unsigned quadmask
= (mask0
& 3) | ((mask1
& 3) << 2);
235 setup
->quad
[q
].input
.x0
= lx
;
236 setup
->quad
[q
].input
.y0
= setup
->span
.y
;
237 setup
->quad
[q
].input
.facing
= setup
->facing
;
238 setup
->quad
[q
].inout
.mask
= quadmask
;
239 setup
->quad_ptrs
[q
] = &setup
->quad
[q
];
242 setup
->numFragsEmitted
+= util_bitcount(quadmask
);
248 } while (mask0
| mask1
);
250 pipe
->run( pipe
, setup
->quad_ptrs
, q
);
256 setup
->span
.right
[0] = 0;
257 setup
->span
.right
[1] = 0;
258 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
259 setup
->span
.left
[1] = 1000000; /* greater than right[1] */
265 print_vertex(const struct setup_context
*setup
,
269 debug_printf(" Vertex: (%p)\n", (void *) v
);
270 for (i
= 0; i
< setup
->nr_vertex_attrs
; i
++) {
271 debug_printf(" %d: %f %f %f %f\n", i
,
272 v
[i
][0], v
[i
][1], v
[i
][2], v
[i
][3]);
273 if (util_is_inf_or_nan(v
[i
][0])) {
274 debug_printf(" NaN!\n");
282 * Sort the vertices from top to bottom order, setting up the triangle
283 * edge fields (ebot, emaj, etop).
284 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
287 setup_sort_vertices(struct setup_context
*setup
,
289 const float (*v0
)[4],
290 const float (*v1
)[4],
291 const float (*v2
)[4])
293 if (setup
->softpipe
->rasterizer
->flatshade_first
)
294 setup
->vprovoke
= v0
;
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
379 * 0 = front-facing, 1 = back-facing
383 (setup
->softpipe
->rasterizer
->front_ccw
));
386 unsigned face
= setup
->facing
== 0 ? PIPE_FACE_FRONT
: PIPE_FACE_BACK
;
388 if (face
& setup
->cull_face
)
393 /* Prepare pixel offset for rasterisation:
394 * - pixel center (0.5, 0.5) for GL, or
395 * - assume (0.0, 0.0) for other APIs.
397 if (setup
->softpipe
->rasterizer
->gl_rasterization_rules
) {
398 setup
->pixel_offset
= 0.5f
;
400 setup
->pixel_offset
= 0.0f
;
407 /* Apply cylindrical wrapping to v0, v1, v2 coordinates, if enabled.
408 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
409 * Some combinations of coordinates produce invalid results,
410 * but this behaviour is acceptable.
413 tri_apply_cylindrical_wrap(float v0
,
416 uint cylindrical_wrap
,
419 if (cylindrical_wrap
) {
426 else if (delta
< -0.5f
) {
434 else if (delta
< -0.5f
) {
442 else if (delta
< -0.5f
) {
454 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
455 * The value value comes from vertex[slot][i].
456 * The result will be put into setup->coef[slot].a0[i].
457 * \param slot which attribute slot
458 * \param i which component of the slot (0..3)
461 const_coeff(struct setup_context
*setup
,
462 struct tgsi_interp_coef
*coef
,
463 uint vertSlot
, uint i
)
470 /* need provoking vertex info!
472 coef
->a0
[i
] = setup
->vprovoke
[vertSlot
][i
];
477 * Compute a0, dadx and dady for a linearly interpolated coefficient,
479 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
482 tri_linear_coeff(struct setup_context
*setup
,
483 struct tgsi_interp_coef
*coef
,
487 float botda
= v
[1] - v
[0];
488 float majda
= v
[2] - v
[0];
489 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
490 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
491 float dadx
= a
* setup
->oneoverarea
;
492 float dady
= b
* setup
->oneoverarea
;
496 coef
->dadx
[i
] = dadx
;
497 coef
->dady
[i
] = dady
;
499 /* calculate a0 as the value which would be sampled for the
500 * fragment at (0,0), taking into account that we want to sample at
501 * pixel centers, in other words (pixel_offset, pixel_offset).
503 * this is neat but unfortunately not a good way to do things for
504 * triangles with very large values of dadx or dady as it will
505 * result in the subtraction and re-addition from a0 of a very
506 * large number, which means we'll end up loosing a lot of the
507 * fractional bits and precision from a0. the way to fix this is
508 * to define a0 as the sample at a pixel center somewhere near vmin
509 * instead - i'll switch to this later.
511 coef
->a0
[i
] = (v
[0] -
512 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
513 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
516 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
518 setup->coef[slot].a0[i],
519 setup->coef[slot].dadx[i],
520 setup->coef[slot].dady[i]);
526 * Compute a0, dadx and dady for a perspective-corrected interpolant,
528 * We basically multiply the vertex value by 1/w before computing
529 * the plane coefficients (a0, dadx, dady).
530 * Later, when we compute the value at a particular fragment position we'll
531 * divide the interpolated value by the interpolated W at that fragment.
532 * v[0], v[1] and v[2] are vmin, vmid and vmax, respectively.
535 tri_persp_coeff(struct setup_context
*setup
,
536 struct tgsi_interp_coef
*coef
,
540 /* premultiply by 1/w (v[0][3] is always W):
542 float mina
= v
[0] * setup
->vmin
[0][3];
543 float mida
= v
[1] * setup
->vmid
[0][3];
544 float maxa
= v
[2] * setup
->vmax
[0][3];
545 float botda
= mida
- mina
;
546 float majda
= maxa
- mina
;
547 float a
= setup
->ebot
.dy
* majda
- botda
* setup
->emaj
.dy
;
548 float b
= setup
->emaj
.dx
* botda
- majda
* setup
->ebot
.dx
;
549 float dadx
= a
* setup
->oneoverarea
;
550 float dady
= b
* setup
->oneoverarea
;
553 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
554 setup->vmin[vertSlot][i],
555 setup->vmid[vertSlot][i],
556 setup->vmax[vertSlot][i]
561 coef
->dadx
[i
] = dadx
;
562 coef
->dady
[i
] = dady
;
563 coef
->a0
[i
] = (mina
-
564 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
565 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
570 * Special coefficient setup for gl_FragCoord.
571 * X and Y are trivial, though Y may have to be inverted for OpenGL.
572 * Z and W are copied from posCoef which should have already been computed.
573 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
576 setup_fragcoord_coeff(struct setup_context
*setup
, uint slot
)
578 const struct tgsi_shader_info
*fsInfo
= &setup
->softpipe
->fs_variant
->info
;
581 setup
->coef
[slot
].a0
[0] = fsInfo
->pixel_center_integer
? 0.0f
: 0.5f
;
582 setup
->coef
[slot
].dadx
[0] = 1.0f
;
583 setup
->coef
[slot
].dady
[0] = 0.0f
;
585 setup
->coef
[slot
].a0
[1] =
586 (fsInfo
->origin_lower_left
? setup
->softpipe
->framebuffer
.height
-1 : 0)
587 + (fsInfo
->pixel_center_integer
? 0.0f
: 0.5f
);
588 setup
->coef
[slot
].dadx
[1] = 0.0f
;
589 setup
->coef
[slot
].dady
[1] = fsInfo
->origin_lower_left
? -1.0f
: 1.0f
;
591 setup
->coef
[slot
].a0
[2] = setup
->posCoef
.a0
[2];
592 setup
->coef
[slot
].dadx
[2] = setup
->posCoef
.dadx
[2];
593 setup
->coef
[slot
].dady
[2] = setup
->posCoef
.dady
[2];
595 setup
->coef
[slot
].a0
[3] = setup
->posCoef
.a0
[3];
596 setup
->coef
[slot
].dadx
[3] = setup
->posCoef
.dadx
[3];
597 setup
->coef
[slot
].dady
[3] = setup
->posCoef
.dady
[3];
603 * Compute the setup->coef[] array dadx, dady, a0 values.
604 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
607 setup_tri_coefficients(struct setup_context
*setup
)
609 struct softpipe_context
*softpipe
= setup
->softpipe
;
610 const struct tgsi_shader_info
*fsInfo
= &setup
->softpipe
->fs_variant
->info
;
611 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
615 /* z and w are done by linear interpolation:
617 v
[0] = setup
->vmin
[0][2];
618 v
[1] = setup
->vmid
[0][2];
619 v
[2] = setup
->vmax
[0][2];
620 tri_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
622 v
[0] = setup
->vmin
[0][3];
623 v
[1] = setup
->vmid
[0][3];
624 v
[2] = setup
->vmax
[0][3];
625 tri_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
627 /* setup interpolation for all the remaining attributes:
629 for (fragSlot
= 0; fragSlot
< fsInfo
->num_inputs
; fragSlot
++) {
630 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
633 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
634 case INTERP_CONSTANT
:
635 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++)
636 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
639 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
640 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
641 setup
->vmid
[vertSlot
][j
],
642 setup
->vmax
[vertSlot
][j
],
643 fsInfo
->input_cylindrical_wrap
[fragSlot
] & (1 << j
),
645 tri_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
648 case INTERP_PERSPECTIVE
:
649 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
650 tri_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
651 setup
->vmid
[vertSlot
][j
],
652 setup
->vmax
[vertSlot
][j
],
653 fsInfo
->input_cylindrical_wrap
[fragSlot
] & (1 << j
),
655 tri_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
659 setup_fragcoord_coeff(setup
, fragSlot
);
665 if (fsInfo
->input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
666 /* convert 0 to 1.0 and 1 to -1.0 */
667 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
668 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
669 setup
->coef
[fragSlot
].dady
[0] = 0.0;
676 setup_tri_edges(struct setup_context
*setup
)
678 float vmin_x
= setup
->vmin
[0][0] + setup
->pixel_offset
;
679 float vmid_x
= setup
->vmid
[0][0] + setup
->pixel_offset
;
681 float vmin_y
= setup
->vmin
[0][1] - setup
->pixel_offset
;
682 float vmid_y
= setup
->vmid
[0][1] - setup
->pixel_offset
;
683 float vmax_y
= setup
->vmax
[0][1] - setup
->pixel_offset
;
685 setup
->emaj
.sy
= ceilf(vmin_y
);
686 setup
->emaj
.lines
= (int) ceilf(vmax_y
- setup
->emaj
.sy
);
687 setup
->emaj
.dxdy
= setup
->emaj
.dy
? setup
->emaj
.dx
/ setup
->emaj
.dy
: .0f
;
688 setup
->emaj
.sx
= vmin_x
+ (setup
->emaj
.sy
- vmin_y
) * setup
->emaj
.dxdy
;
690 setup
->etop
.sy
= ceilf(vmid_y
);
691 setup
->etop
.lines
= (int) ceilf(vmax_y
- setup
->etop
.sy
);
692 setup
->etop
.dxdy
= setup
->etop
.dy
? setup
->etop
.dx
/ setup
->etop
.dy
: .0f
;
693 setup
->etop
.sx
= vmid_x
+ (setup
->etop
.sy
- vmid_y
) * setup
->etop
.dxdy
;
695 setup
->ebot
.sy
= ceilf(vmin_y
);
696 setup
->ebot
.lines
= (int) ceilf(vmid_y
- setup
->ebot
.sy
);
697 setup
->ebot
.dxdy
= setup
->ebot
.dy
? setup
->ebot
.dx
/ setup
->ebot
.dy
: .0f
;
698 setup
->ebot
.sx
= vmin_x
+ (setup
->ebot
.sy
- vmin_y
) * setup
->ebot
.dxdy
;
703 * Render the upper or lower half of a triangle.
704 * Scissoring/cliprect is applied here too.
707 subtriangle(struct setup_context
*setup
,
712 const struct pipe_scissor_state
*cliprect
= &setup
->softpipe
->cliprect
;
713 const int minx
= (int) cliprect
->minx
;
714 const int maxx
= (int) cliprect
->maxx
;
715 const int miny
= (int) cliprect
->miny
;
716 const int maxy
= (int) cliprect
->maxy
;
717 int y
, start_y
, finish_y
;
718 int sy
= (int)eleft
->sy
;
720 assert((int)eleft
->sy
== (int) eright
->sy
);
723 /* clip top/bottom */
728 finish_y
= sy
+ lines
;
736 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
739 for (y
= start_y
; y
< finish_y
; y
++) {
741 /* avoid accumulating adds as floats don't have the precision to
742 * accurately iterate large triangle edges that way. luckily we
743 * can just multiply these days.
745 * this is all drowned out by the attribute interpolation anyway.
747 int left
= (int)(eleft
->sx
+ y
* eleft
->dxdy
);
748 int right
= (int)(eright
->sx
+ y
* eright
->dxdy
);
750 /* clip left/right */
758 if (block(_y
) != setup
->span
.y
) {
760 setup
->span
.y
= block(_y
);
763 setup
->span
.left
[_y
&1] = left
;
764 setup
->span
.right
[_y
&1] = right
;
769 /* save the values so that emaj can be restarted:
771 eleft
->sx
+= lines
* eleft
->dxdy
;
772 eright
->sx
+= lines
* eright
->dxdy
;
779 * Recalculate prim's determinant. This is needed as we don't have
780 * get this information through the vbuf_render interface & we must
784 calc_det(const float (*v0
)[4],
785 const float (*v1
)[4],
786 const float (*v2
)[4])
788 /* edge vectors e = v0 - v2, f = v1 - v2 */
789 const float ex
= v0
[0][0] - v2
[0][0];
790 const float ey
= v0
[0][1] - v2
[0][1];
791 const float fx
= v1
[0][0] - v2
[0][0];
792 const float fy
= v1
[0][1] - v2
[0][1];
794 /* det = cross(e,f).z */
795 return ex
* fy
- ey
* fx
;
800 * Do setup for triangle rasterization, then render the triangle.
803 sp_setup_tri(struct setup_context
*setup
,
804 const float (*v0
)[4],
805 const float (*v1
)[4],
806 const float (*v2
)[4])
811 debug_printf("Setup triangle:\n");
812 print_vertex(setup
, v0
);
813 print_vertex(setup
, v1
);
814 print_vertex(setup
, v2
);
817 if (setup
->softpipe
->no_rast
|| setup
->softpipe
->rasterizer
->rasterizer_discard
)
820 det
= calc_det(v0
, v1
, v2
);
822 debug_printf("%s\n", __FUNCTION__ );
826 setup
->numFragsEmitted
= 0;
827 setup
->numFragsWritten
= 0;
830 if (!setup_sort_vertices( setup
, det
, v0
, v1
, v2
))
833 setup_tri_coefficients( setup
);
834 setup_tri_edges( setup
);
836 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_TRIANGLES
);
839 setup
->span
.right
[0] = 0;
840 setup
->span
.right
[1] = 0;
841 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
843 /* init_constant_attribs( setup ); */
845 if (setup
->oneoverarea
< 0.0) {
848 subtriangle( setup
, &setup
->emaj
, &setup
->ebot
, setup
->ebot
.lines
);
849 subtriangle( setup
, &setup
->emaj
, &setup
->etop
, setup
->etop
.lines
);
854 subtriangle( setup
, &setup
->ebot
, &setup
->emaj
, setup
->ebot
.lines
);
855 subtriangle( setup
, &setup
->etop
, &setup
->emaj
, setup
->etop
.lines
);
858 flush_spans( setup
);
861 printf("Tri: %u frags emitted, %u written\n",
862 setup
->numFragsEmitted
,
863 setup
->numFragsWritten
);
868 /* Apply cylindrical wrapping to v0, v1 coordinates, if enabled.
869 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
872 line_apply_cylindrical_wrap(float v0
,
874 uint cylindrical_wrap
,
877 if (cylindrical_wrap
) {
884 else if (delta
< -0.5f
) {
895 * Compute a0, dadx and dady for a linearly interpolated coefficient,
897 * v[0] and v[1] are vmin and vmax, respectively.
900 line_linear_coeff(const struct setup_context
*setup
,
901 struct tgsi_interp_coef
*coef
,
905 const float da
= v
[1] - v
[0];
906 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
907 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
908 coef
->dadx
[i
] = dadx
;
909 coef
->dady
[i
] = dady
;
910 coef
->a0
[i
] = (v
[0] -
911 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
912 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
917 * Compute a0, dadx and dady for a perspective-corrected interpolant,
919 * v[0] and v[1] are vmin and vmax, respectively.
922 line_persp_coeff(const struct setup_context
*setup
,
923 struct tgsi_interp_coef
*coef
,
927 const float a0
= v
[0] * setup
->vmin
[0][3];
928 const float a1
= v
[1] * setup
->vmax
[0][3];
929 const float da
= a1
- a0
;
930 const float dadx
= da
* setup
->emaj
.dx
* setup
->oneoverarea
;
931 const float dady
= da
* setup
->emaj
.dy
* setup
->oneoverarea
;
932 coef
->dadx
[i
] = dadx
;
933 coef
->dady
[i
] = dady
;
935 (dadx
* (setup
->vmin
[0][0] - setup
->pixel_offset
) +
936 dady
* (setup
->vmin
[0][1] - setup
->pixel_offset
)));
941 * Compute the setup->coef[] array dadx, dady, a0 values.
942 * Must be called after setup->vmin,vmax are initialized.
945 setup_line_coefficients(struct setup_context
*setup
,
946 const float (*v0
)[4],
947 const float (*v1
)[4])
949 struct softpipe_context
*softpipe
= setup
->softpipe
;
950 const struct tgsi_shader_info
*fsInfo
= &setup
->softpipe
->fs_variant
->info
;
951 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
956 /* use setup->vmin, vmax to point to vertices */
957 if (softpipe
->rasterizer
->flatshade_first
)
958 setup
->vprovoke
= v0
;
960 setup
->vprovoke
= v1
;
964 setup
->emaj
.dx
= setup
->vmax
[0][0] - setup
->vmin
[0][0];
965 setup
->emaj
.dy
= setup
->vmax
[0][1] - setup
->vmin
[0][1];
967 /* NOTE: this is not really area but something proportional to it */
968 area
= setup
->emaj
.dx
* setup
->emaj
.dx
+ setup
->emaj
.dy
* setup
->emaj
.dy
;
969 if (area
== 0.0f
|| util_is_inf_or_nan(area
))
971 setup
->oneoverarea
= 1.0f
/ area
;
973 /* z and w are done by linear interpolation:
975 v
[0] = setup
->vmin
[0][2];
976 v
[1] = setup
->vmax
[0][2];
977 line_linear_coeff(setup
, &setup
->posCoef
, 2, v
);
979 v
[0] = setup
->vmin
[0][3];
980 v
[1] = setup
->vmax
[0][3];
981 line_linear_coeff(setup
, &setup
->posCoef
, 3, v
);
983 /* setup interpolation for all the remaining attributes:
985 for (fragSlot
= 0; fragSlot
< fsInfo
->num_inputs
; fragSlot
++) {
986 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
989 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
990 case INTERP_CONSTANT
:
991 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++)
992 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
995 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
996 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
997 setup
->vmax
[vertSlot
][j
],
998 fsInfo
->input_cylindrical_wrap
[fragSlot
] & (1 << j
),
1000 line_linear_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
1003 case INTERP_PERSPECTIVE
:
1004 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++) {
1005 line_apply_cylindrical_wrap(setup
->vmin
[vertSlot
][j
],
1006 setup
->vmax
[vertSlot
][j
],
1007 fsInfo
->input_cylindrical_wrap
[fragSlot
] & (1 << j
),
1009 line_persp_coeff(setup
, &setup
->coef
[fragSlot
], j
, v
);
1013 setup_fragcoord_coeff(setup
, fragSlot
);
1019 if (fsInfo
->input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1020 /* convert 0 to 1.0 and 1 to -1.0 */
1021 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
1022 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1023 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1031 * Plot a pixel in a line segment.
1034 plot(struct setup_context
*setup
, int x
, int y
)
1036 const int iy
= y
& 1;
1037 const int ix
= x
& 1;
1038 const int quadX
= x
- ix
;
1039 const int quadY
= y
- iy
;
1040 const int mask
= (1 << ix
) << (2 * iy
);
1042 if (quadX
!= setup
->quad
[0].input
.x0
||
1043 quadY
!= setup
->quad
[0].input
.y0
)
1045 /* flush prev quad, start new quad */
1047 if (setup
->quad
[0].input
.x0
!= -1)
1048 clip_emit_quad( setup
, &setup
->quad
[0] );
1050 setup
->quad
[0].input
.x0
= quadX
;
1051 setup
->quad
[0].input
.y0
= quadY
;
1052 setup
->quad
[0].inout
.mask
= 0x0;
1055 setup
->quad
[0].inout
.mask
|= mask
;
1060 * Do setup for line rasterization, then render the line.
1061 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
1062 * to handle stippling and wide lines.
1065 sp_setup_line(struct setup_context
*setup
,
1066 const float (*v0
)[4],
1067 const float (*v1
)[4])
1069 int x0
= (int) v0
[0][0];
1070 int x1
= (int) v1
[0][0];
1071 int y0
= (int) v0
[0][1];
1072 int y1
= (int) v1
[0][1];
1078 debug_printf("Setup line:\n");
1079 print_vertex(setup
, v0
);
1080 print_vertex(setup
, v1
);
1083 if (setup
->softpipe
->no_rast
|| setup
->softpipe
->rasterizer
->rasterizer_discard
)
1086 if (dx
== 0 && dy
== 0)
1089 if (!setup_line_coefficients(setup
, v0
, v1
))
1092 assert(v0
[0][0] < 1.0e9
);
1093 assert(v0
[0][1] < 1.0e9
);
1094 assert(v1
[0][0] < 1.0e9
);
1095 assert(v1
[0][1] < 1.0e9
);
1098 dx
= -dx
; /* make positive */
1106 dy
= -dy
; /* make positive */
1115 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_LINES
);
1117 setup
->quad
[0].input
.x0
= setup
->quad
[0].input
.y0
= -1;
1118 setup
->quad
[0].inout
.mask
= 0x0;
1120 /* XXX temporary: set coverage to 1.0 so the line appears
1121 * if AA mode happens to be enabled.
1123 setup
->quad
[0].input
.coverage
[0] =
1124 setup
->quad
[0].input
.coverage
[1] =
1125 setup
->quad
[0].input
.coverage
[2] =
1126 setup
->quad
[0].input
.coverage
[3] = 1.0;
1129 /*** X-major line ***/
1131 const int errorInc
= dy
+ dy
;
1132 int error
= errorInc
- dx
;
1133 const int errorDec
= error
- dx
;
1135 for (i
= 0; i
< dx
; i
++) {
1136 plot(setup
, x0
, y0
);
1149 /*** Y-major line ***/
1151 const int errorInc
= dx
+ dx
;
1152 int error
= errorInc
- dy
;
1153 const int errorDec
= error
- dy
;
1155 for (i
= 0; i
< dy
; i
++) {
1156 plot(setup
, x0
, y0
);
1169 /* draw final quad */
1170 if (setup
->quad
[0].inout
.mask
) {
1171 clip_emit_quad( setup
, &setup
->quad
[0] );
1177 point_persp_coeff(const struct setup_context
*setup
,
1178 const float (*vert
)[4],
1179 struct tgsi_interp_coef
*coef
,
1180 uint vertSlot
, uint i
)
1183 coef
->dadx
[i
] = 0.0F
;
1184 coef
->dady
[i
] = 0.0F
;
1185 coef
->a0
[i
] = vert
[vertSlot
][i
] * vert
[0][3];
1190 * Do setup for point rasterization, then render the point.
1191 * Round or square points...
1192 * XXX could optimize a lot for 1-pixel points.
1195 sp_setup_point(struct setup_context
*setup
,
1196 const float (*v0
)[4])
1198 struct softpipe_context
*softpipe
= setup
->softpipe
;
1199 const struct tgsi_shader_info
*fsInfo
= &setup
->softpipe
->fs_variant
->info
;
1200 const int sizeAttr
= setup
->softpipe
->psize_slot
;
1202 = sizeAttr
> 0 ? v0
[sizeAttr
][0]
1203 : setup
->softpipe
->rasterizer
->point_size
;
1204 const float halfSize
= 0.5F
* size
;
1205 const boolean round
= (boolean
) setup
->softpipe
->rasterizer
->point_smooth
;
1206 const float x
= v0
[0][0]; /* Note: data[0] is always position */
1207 const float y
= v0
[0][1];
1208 const struct vertex_info
*vinfo
= softpipe_get_vertex_info(softpipe
);
1212 debug_printf("Setup point:\n");
1213 print_vertex(setup
, v0
);
1216 if (setup
->softpipe
->no_rast
|| setup
->softpipe
->rasterizer
->rasterizer_discard
)
1219 assert(setup
->softpipe
->reduced_prim
== PIPE_PRIM_POINTS
);
1221 /* For points, all interpolants are constant-valued.
1222 * However, for point sprites, we'll need to setup texcoords appropriately.
1223 * XXX: which coefficients are the texcoords???
1224 * We may do point sprites as textured quads...
1226 * KW: We don't know which coefficients are texcoords - ultimately
1227 * the choice of what interpolation mode to use for each attribute
1228 * should be determined by the fragment program, using
1229 * per-attribute declaration statements that include interpolation
1230 * mode as a parameter. So either the fragment program will have
1231 * to be adjusted for pointsprite vs normal point behaviour, or
1232 * otherwise a special interpolation mode will have to be defined
1233 * which matches the required behaviour for point sprites. But -
1234 * the latter is not a feature of normal hardware, and as such
1235 * probably should be ruled out on that basis.
1237 setup
->vprovoke
= v0
;
1240 const_coeff(setup
, &setup
->posCoef
, 0, 2);
1241 const_coeff(setup
, &setup
->posCoef
, 0, 3);
1243 for (fragSlot
= 0; fragSlot
< fsInfo
->num_inputs
; fragSlot
++) {
1244 const uint vertSlot
= vinfo
->attrib
[fragSlot
].src_index
;
1247 switch (vinfo
->attrib
[fragSlot
].interp_mode
) {
1248 case INTERP_CONSTANT
:
1251 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++)
1252 const_coeff(setup
, &setup
->coef
[fragSlot
], vertSlot
, j
);
1254 case INTERP_PERSPECTIVE
:
1255 for (j
= 0; j
< TGSI_NUM_CHANNELS
; j
++)
1256 point_persp_coeff(setup
, setup
->vprovoke
,
1257 &setup
->coef
[fragSlot
], vertSlot
, j
);
1260 setup_fragcoord_coeff(setup
, fragSlot
);
1266 if (fsInfo
->input_semantic_name
[fragSlot
] == TGSI_SEMANTIC_FACE
) {
1267 /* convert 0 to 1.0 and 1 to -1.0 */
1268 setup
->coef
[fragSlot
].a0
[0] = setup
->facing
* -2.0f
+ 1.0f
;
1269 setup
->coef
[fragSlot
].dadx
[0] = 0.0;
1270 setup
->coef
[fragSlot
].dady
[0] = 0.0;
1275 if (halfSize
<= 0.5 && !round
) {
1276 /* special case for 1-pixel points */
1277 const int ix
= ((int) x
) & 1;
1278 const int iy
= ((int) y
) & 1;
1279 setup
->quad
[0].input
.x0
= (int) x
- ix
;
1280 setup
->quad
[0].input
.y0
= (int) y
- iy
;
1281 setup
->quad
[0].inout
.mask
= (1 << ix
) << (2 * iy
);
1282 clip_emit_quad( setup
, &setup
->quad
[0] );
1286 /* rounded points */
1287 const int ixmin
= block((int) (x
- halfSize
));
1288 const int ixmax
= block((int) (x
+ halfSize
));
1289 const int iymin
= block((int) (y
- halfSize
));
1290 const int iymax
= block((int) (y
+ halfSize
));
1291 const float rmin
= halfSize
- 0.7071F
; /* 0.7071 = sqrt(2)/2 */
1292 const float rmax
= halfSize
+ 0.7071F
;
1293 const float rmin2
= MAX2(0.0F
, rmin
* rmin
);
1294 const float rmax2
= rmax
* rmax
;
1295 const float cscale
= 1.0F
/ (rmax2
- rmin2
);
1298 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1299 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1300 float dx
, dy
, dist2
, cover
;
1302 setup
->quad
[0].inout
.mask
= 0x0;
1304 dx
= (ix
+ 0.5f
) - x
;
1305 dy
= (iy
+ 0.5f
) - y
;
1306 dist2
= dx
* dx
+ dy
* dy
;
1307 if (dist2
<= rmax2
) {
1308 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1309 setup
->quad
[0].input
.coverage
[QUAD_TOP_LEFT
] = MIN2(cover
, 1.0f
);
1310 setup
->quad
[0].inout
.mask
|= MASK_TOP_LEFT
;
1313 dx
= (ix
+ 1.5f
) - x
;
1314 dy
= (iy
+ 0.5f
) - y
;
1315 dist2
= dx
* dx
+ dy
* dy
;
1316 if (dist2
<= rmax2
) {
1317 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1318 setup
->quad
[0].input
.coverage
[QUAD_TOP_RIGHT
] = MIN2(cover
, 1.0f
);
1319 setup
->quad
[0].inout
.mask
|= MASK_TOP_RIGHT
;
1322 dx
= (ix
+ 0.5f
) - x
;
1323 dy
= (iy
+ 1.5f
) - y
;
1324 dist2
= dx
* dx
+ dy
* dy
;
1325 if (dist2
<= rmax2
) {
1326 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1327 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_LEFT
] = MIN2(cover
, 1.0f
);
1328 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_LEFT
;
1331 dx
= (ix
+ 1.5f
) - x
;
1332 dy
= (iy
+ 1.5f
) - y
;
1333 dist2
= dx
* dx
+ dy
* dy
;
1334 if (dist2
<= rmax2
) {
1335 cover
= 1.0F
- (dist2
- rmin2
) * cscale
;
1336 setup
->quad
[0].input
.coverage
[QUAD_BOTTOM_RIGHT
] = MIN2(cover
, 1.0f
);
1337 setup
->quad
[0].inout
.mask
|= MASK_BOTTOM_RIGHT
;
1340 if (setup
->quad
[0].inout
.mask
) {
1341 setup
->quad
[0].input
.x0
= ix
;
1342 setup
->quad
[0].input
.y0
= iy
;
1343 clip_emit_quad( setup
, &setup
->quad
[0] );
1350 const int xmin
= (int) (x
+ 0.75 - halfSize
);
1351 const int ymin
= (int) (y
+ 0.25 - halfSize
);
1352 const int xmax
= xmin
+ (int) size
;
1353 const int ymax
= ymin
+ (int) size
;
1354 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1355 const int ixmin
= block(xmin
);
1356 const int ixmax
= block(xmax
- 1);
1357 const int iymin
= block(ymin
);
1358 const int iymax
= block(ymax
- 1);
1362 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1364 for (iy
= iymin
; iy
<= iymax
; iy
+= 2) {
1367 /* above the top edge */
1368 rowMask
&= (MASK_BOTTOM_LEFT
| MASK_BOTTOM_RIGHT
);
1370 if (iy
+ 1 >= ymax
) {
1371 /* below the bottom edge */
1372 rowMask
&= (MASK_TOP_LEFT
| MASK_TOP_RIGHT
);
1375 for (ix
= ixmin
; ix
<= ixmax
; ix
+= 2) {
1376 uint mask
= rowMask
;
1379 /* fragment is past left edge of point, turn off left bits */
1380 mask
&= (MASK_BOTTOM_RIGHT
| MASK_TOP_RIGHT
);
1382 if (ix
+ 1 >= xmax
) {
1383 /* past the right edge */
1384 mask
&= (MASK_BOTTOM_LEFT
| MASK_TOP_LEFT
);
1387 setup
->quad
[0].inout
.mask
= mask
;
1388 setup
->quad
[0].input
.x0
= ix
;
1389 setup
->quad
[0].input
.y0
= iy
;
1390 clip_emit_quad( setup
, &setup
->quad
[0] );
1399 * Called by vbuf code just before we start buffering primitives.
1402 sp_setup_prepare(struct setup_context
*setup
)
1404 struct softpipe_context
*sp
= setup
->softpipe
;
1407 softpipe_update_derived(sp
, sp
->reduced_api_prim
);
1410 /* Note: nr_attrs is only used for debugging (vertex printing) */
1411 setup
->nr_vertex_attrs
= draw_num_shader_outputs(sp
->draw
);
1413 sp
->quad
.first
->begin( sp
->quad
.first
);
1415 if (sp
->reduced_api_prim
== PIPE_PRIM_TRIANGLES
&&
1416 sp
->rasterizer
->fill_front
== PIPE_POLYGON_MODE_FILL
&&
1417 sp
->rasterizer
->fill_back
== PIPE_POLYGON_MODE_FILL
) {
1418 /* we'll do culling */
1419 setup
->cull_face
= sp
->rasterizer
->cull_face
;
1422 /* 'draw' will do culling */
1423 setup
->cull_face
= PIPE_FACE_NONE
;
1429 sp_setup_destroy_context(struct setup_context
*setup
)
1436 * Create a new primitive setup/render stage.
1438 struct setup_context
*
1439 sp_setup_create_context(struct softpipe_context
*softpipe
)
1441 struct setup_context
*setup
= CALLOC_STRUCT(setup_context
);
1444 setup
->softpipe
= softpipe
;
1446 for (i
= 0; i
< MAX_QUADS
; i
++) {
1447 setup
->quad
[i
].coef
= setup
->coef
;
1448 setup
->quad
[i
].posCoef
= &setup
->posCoef
;
1451 setup
->span
.left
[0] = 1000000; /* greater than right[0] */
1452 setup
->span
.left
[1] = 1000000; /* greater than right[1] */