1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 Clipping stage
31 * \author Keith Whitwell <keithw@vmware.com>
35 #include "util/u_bitcast.h"
36 #include "util/u_memory.h"
37 #include "util/u_math.h"
39 #include "pipe/p_shader_tokens.h"
42 #include "draw_pipe.h"
47 /** Set to 1 to enable printing of coords before/after clipping */
51 #ifndef DIFFERENT_SIGNS
52 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
55 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
60 struct draw_stage stage
; /**< base class */
63 boolean have_clipdist
;
66 /* List of the attributes to be constant interpolated. */
67 uint num_const_attribs
;
68 uint8_t const_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
69 /* List of the attributes to be linear interpolated. */
70 uint num_linear_attribs
;
71 uint8_t linear_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
72 /* List of the attributes to be perspective interpolated. */
73 uint num_perspect_attribs
;
74 uint8_t perspect_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
81 static inline struct clip_stage
*clip_stage(struct draw_stage
*stage
)
83 return (struct clip_stage
*)stage
;
86 static inline unsigned
87 draw_viewport_index(struct draw_context
*draw
,
88 const struct vertex_header
*leading_vertex
)
90 if (draw_current_shader_uses_viewport_index(draw
)) {
91 unsigned viewport_index_output
=
92 draw_current_shader_viewport_index_output(draw
);
93 unsigned viewport_index
=
94 u_bitcast_f2u(leading_vertex
->data
[viewport_index_output
][0]);
95 return draw_clamp_viewport_idx(viewport_index
);
102 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
105 /* All attributes are float[4], so this is easy:
107 static void interp_attr(float dst
[4],
112 dst
[0] = LINTERP( t
, out
[0], in
[0] );
113 dst
[1] = LINTERP( t
, out
[1], in
[1] );
114 dst
[2] = LINTERP( t
, out
[2], in
[2] );
115 dst
[3] = LINTERP( t
, out
[3], in
[3] );
120 * Copy flat shaded attributes src vertex to dst vertex.
122 static void copy_flat(struct draw_stage
*stage
,
123 struct vertex_header
*dst
,
124 const struct vertex_header
*src
)
126 const struct clip_stage
*clipper
= clip_stage(stage
);
128 for (i
= 0; i
< clipper
->num_const_attribs
; i
++) {
129 const uint attr
= clipper
->const_attribs
[i
];
130 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
134 /* Interpolate between two vertices to produce a third.
136 static void interp(const struct clip_stage
*clip
,
137 struct vertex_header
*dst
,
139 const struct vertex_header
*out
,
140 const struct vertex_header
*in
,
141 unsigned viewport_index
)
143 const unsigned pos_attr
= clip
->pos_attr
;
150 dst
->edgeflag
= 0; /* will get overwritten later */
152 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
154 /* Interpolate the clip-space coords.
156 if (clip
->cv_attr
>= 0) {
157 interp_attr(dst
->data
[clip
->cv_attr
], t
,
158 in
->data
[clip
->cv_attr
], out
->data
[clip
->cv_attr
]);
160 /* interpolate the clip-space position */
161 interp_attr(dst
->clip_pos
, t
, in
->clip_pos
, out
->clip_pos
);
163 /* Do the projective divide and viewport transformation to get
164 * new window coordinates:
167 const float *pos
= dst
->clip_pos
;
169 clip
->stage
.draw
->viewports
[viewport_index
].scale
;
171 clip
->stage
.draw
->viewports
[viewport_index
].translate
;
172 const float oow
= 1.0f
/ pos
[3];
174 dst
->data
[pos_attr
][0] = pos
[0] * oow
* scale
[0] + trans
[0];
175 dst
->data
[pos_attr
][1] = pos
[1] * oow
* scale
[1] + trans
[1];
176 dst
->data
[pos_attr
][2] = pos
[2] * oow
* scale
[2] + trans
[2];
177 dst
->data
[pos_attr
][3] = oow
;
181 /* interp perspective attribs */
182 for (j
= 0; j
< clip
->num_perspect_attribs
; j
++) {
183 const unsigned attr
= clip
->perspect_attribs
[j
];
184 interp_attr(dst
->data
[attr
], t
, in
->data
[attr
], out
->data
[attr
]);
188 * Compute the t in screen-space instead of 3d space to use
189 * for noperspective interpolation.
191 * The points can be aligned with the X axis, so in that case try
192 * the Y. When both points are at the same screen position, we can
193 * pick whatever value (the interpolated point won't be in front
194 * anyway), so just use the 3d t.
196 if (clip
->num_linear_attribs
) {
199 /* find either in.x != out.x or in.y != out.y */
200 for (k
= 0; k
< 2; k
++) {
201 if (in
->clip_pos
[k
] != out
->clip_pos
[k
]) {
202 /* do divide by W, then compute linear interpolation factor */
203 float in_coord
= in
->clip_pos
[k
] / in
->clip_pos
[3];
204 float out_coord
= out
->clip_pos
[k
] / out
->clip_pos
[3];
205 float dst_coord
= dst
->clip_pos
[k
] / dst
->clip_pos
[3];
206 t_nopersp
= (dst_coord
- out_coord
) / (in_coord
- out_coord
);
210 for (j
= 0; j
< clip
->num_linear_attribs
; j
++) {
211 const unsigned attr
= clip
->linear_attribs
[j
];
212 interp_attr(dst
->data
[attr
], t_nopersp
, in
->data
[attr
], out
->data
[attr
]);
218 * Checks whether the specified triangle is empty and if it is returns
219 * true, otherwise returns false.
220 * Triangle is considered null/empty if its area is equal to zero.
222 static inline boolean
223 is_tri_null(const struct clip_stage
*clip
, const struct prim_header
*header
)
225 const unsigned pos_attr
= clip
->pos_attr
;
226 float x1
= header
->v
[1]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
227 float y1
= header
->v
[1]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
228 float z1
= header
->v
[1]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
230 float x2
= header
->v
[2]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
231 float y2
= header
->v
[2]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
232 float z2
= header
->v
[2]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
234 float vx
= y1
* z2
- z1
* y2
;
235 float vy
= x1
* z2
- z1
* x2
;
236 float vz
= x1
* y2
- y1
* x2
;
238 return (vx
*vx
+ vy
*vy
+ vz
*vz
) == 0.f
;
242 * Emit a post-clip polygon to the next pipeline stage. The polygon
243 * will be convex and the provoking vertex will always be vertex[0].
245 static void emit_poly(struct draw_stage
*stage
,
246 struct vertex_header
**inlist
,
247 const boolean
*edgeflags
,
249 const struct prim_header
*origPrim
)
251 const struct clip_stage
*clipper
= clip_stage(stage
);
252 struct prim_header header
;
254 ushort edge_first
, edge_middle
, edge_last
;
255 boolean last_tri_was_null
= FALSE
;
256 boolean tri_was_not_null
= FALSE
;
258 if (stage
->draw
->rasterizer
->flatshade_first
) {
259 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
260 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
261 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
264 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
265 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
266 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
272 /* later stages may need the determinant, but only the sign matters */
273 header
.det
= origPrim
->det
;
274 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
277 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
279 /* order the triangle verts to respect the provoking vertex mode */
280 if (stage
->draw
->rasterizer
->flatshade_first
) {
281 header
.v
[0] = inlist
[0]; /* the provoking vertex */
282 header
.v
[1] = inlist
[i
-1];
283 header
.v
[2] = inlist
[i
];
286 header
.v
[0] = inlist
[i
-1];
287 header
.v
[1] = inlist
[i
];
288 header
.v
[2] = inlist
[0]; /* the provoking vertex */
291 tri_null
= is_tri_null(clipper
, &header
);
292 /* If we generated a triangle with an area, aka. non-null triangle,
293 * or if the previous triangle was also null then skip all subsequent
295 if ((tri_was_not_null
&& tri_null
) || (last_tri_was_null
&& tri_null
)) {
296 last_tri_was_null
= tri_null
;
299 last_tri_was_null
= tri_null
;
301 tri_was_not_null
= TRUE
;
304 if (!edgeflags
[i
-1]) {
305 header
.flags
&= ~edge_middle
;
308 if (i
== n
- 1 && edgeflags
[i
])
309 header
.flags
|= edge_last
;
313 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
314 stage
->draw
->rasterizer
->flatshade_first
);
315 for (j
= 0; j
< 3; j
++) {
316 debug_printf(" Vert %d: clip pos: %f %f %f %f\n", j
,
317 header
.v
[j
]->clip_pos
[0],
318 header
.v
[j
]->clip_pos
[1],
319 header
.v
[j
]->clip_pos
[2],
320 header
.v
[j
]->clip_pos
[3]);
321 if (clipper
->cv_attr
>= 0) {
322 debug_printf(" Vert %d: cv: %f %f %f %f\n", j
,
323 header
.v
[j
]->data
[clipper
->cv_attr
][0],
324 header
.v
[j
]->data
[clipper
->cv_attr
][1],
325 header
.v
[j
]->data
[clipper
->cv_attr
][2],
326 header
.v
[j
]->data
[clipper
->cv_attr
][3]);
328 for (k
= 0; k
< draw_num_shader_outputs(stage
->draw
); k
++) {
329 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
330 header
.v
[j
]->data
[k
][0],
331 header
.v
[j
]->data
[k
][1],
332 header
.v
[j
]->data
[k
][2],
333 header
.v
[j
]->data
[k
][3]);
337 stage
->next
->tri(stage
->next
, &header
);
343 dot4(const float *a
, const float *b
)
345 return (a
[0] * b
[0] +
352 * this function extracts the clip distance for the current plane,
353 * it first checks if the shader provided a clip distance, otherwise
354 * it works out the value using the clipvertex
356 static inline float getclipdist(const struct clip_stage
*clipper
,
357 struct vertex_header
*vert
,
363 /* ordinary xyz view volume clipping uses pos output */
364 plane
= clipper
->plane
[plane_idx
];
365 dp
= dot4(vert
->clip_pos
, plane
);
367 else if (clipper
->have_clipdist
) {
368 /* pick the correct clipdistance element from the output vectors */
369 int _idx
= plane_idx
- 6;
371 int vidx
= cdi
? _idx
- 4 : _idx
;
372 dp
= vert
->data
[draw_current_shader_ccdistance_output(clipper
->stage
.draw
, cdi
)][vidx
];
375 * legacy user clip planes or gl_ClipVertex
377 plane
= clipper
->plane
[plane_idx
];
378 if (clipper
->cv_attr
>= 0) {
379 dp
= dot4(vert
->data
[clipper
->cv_attr
], plane
);
382 dp
= dot4(vert
->clip_pos
, plane
);
388 /* Clip a triangle against the viewport and user clip planes.
391 do_clip_tri(struct draw_stage
*stage
,
392 struct prim_header
*header
,
395 struct clip_stage
*clipper
= clip_stage( stage
);
396 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
397 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
398 struct vertex_header
**inlist
= a
;
399 struct vertex_header
**outlist
= b
;
400 struct vertex_header
*prov_vertex
;
404 boolean aEdges
[MAX_CLIPPED_VERTICES
];
405 boolean bEdges
[MAX_CLIPPED_VERTICES
];
406 boolean
*inEdges
= aEdges
;
407 boolean
*outEdges
= bEdges
;
408 int viewport_index
= 0;
410 inlist
[0] = header
->v
[0];
411 inlist
[1] = header
->v
[1];
412 inlist
[2] = header
->v
[2];
415 * For d3d10, we need to take this from the leading (first) vertex.
416 * For GL, we could do anything (as long as we advertize
417 * GL_UNDEFINED_VERTEX for the VIEWPORT_INDEX_PROVOKING_VERTEX query),
418 * but it needs to be consistent with what other parts (i.e. driver)
419 * will do, and that seems easier with GL_PROVOKING_VERTEX logic.
421 if (stage
->draw
->rasterizer
->flatshade_first
) {
422 prov_vertex
= inlist
[0];
425 prov_vertex
= inlist
[2];
427 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
430 const float *v0
= header
->v
[0]->clip_pos
;
431 const float *v1
= header
->v
[1]->clip_pos
;
432 const float *v2
= header
->v
[2]->clip_pos
;
433 debug_printf("Clip triangle pos:\n");
434 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
435 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
436 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
437 if (clipper
->cv_attr
>= 0) {
438 const float *v0
= header
->v
[0]->data
[clipper
->cv_attr
];
439 const float *v1
= header
->v
[1]->data
[clipper
->cv_attr
];
440 const float *v2
= header
->v
[2]->data
[clipper
->cv_attr
];
441 debug_printf("Clip triangle cv:\n");
442 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
443 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
444 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
449 * Note: at this point we can't just use the per-vertex edge flags.
450 * We have to observe the edge flag bits set in header->flags which
451 * were set during primitive decomposition. Put those flags into
452 * an edge flags array which parallels the vertex array.
453 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
454 * the header.flags bit is set AND the per-vertex edgeflag field is set.
456 inEdges
[0] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_0
);
457 inEdges
[1] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_1
);
458 inEdges
[2] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_2
);
460 while (clipmask
&& n
>= 3) {
461 const unsigned plane_idx
= ffs(clipmask
)-1;
462 const boolean is_user_clip_plane
= plane_idx
>= 6;
463 struct vertex_header
*vert_prev
= inlist
[0];
464 boolean
*edge_prev
= &inEdges
[0];
466 unsigned outcount
= 0;
468 dp_prev
= getclipdist(clipper
, vert_prev
, plane_idx
);
469 clipmask
&= ~(1<<plane_idx
);
471 if (util_is_inf_or_nan(dp_prev
))
472 return; //discard nan
474 assert(n
< MAX_CLIPPED_VERTICES
);
475 if (n
>= MAX_CLIPPED_VERTICES
)
477 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
478 inEdges
[n
] = inEdges
[0];
480 for (i
= 1; i
<= n
; i
++) {
481 struct vertex_header
*vert
= inlist
[i
];
482 boolean
*edge
= &inEdges
[i
];
484 float dp
= getclipdist(clipper
, vert
, plane_idx
);
486 if (util_is_inf_or_nan(dp
))
487 return; //discard nan
489 if (dp_prev
>= 0.0f
) {
490 assert(outcount
< MAX_CLIPPED_VERTICES
);
491 if (outcount
>= MAX_CLIPPED_VERTICES
)
493 outEdges
[outcount
] = *edge_prev
;
494 outlist
[outcount
++] = vert_prev
;
497 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
498 struct vertex_header
*new_vert
;
501 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
502 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
504 new_vert
= clipper
->stage
.tmp
[tmpnr
++];
506 assert(outcount
< MAX_CLIPPED_VERTICES
);
507 if (outcount
>= MAX_CLIPPED_VERTICES
)
510 new_edge
= &outEdges
[outcount
];
511 outlist
[outcount
++] = new_vert
;
514 /* Going out of bounds. Avoid division by zero as we
515 * know dp != dp_prev from DIFFERENT_SIGNS, above.
517 float t
= dp
/ (dp
- dp_prev
);
518 interp( clipper
, new_vert
, t
, vert
, vert_prev
, viewport_index
);
520 /* Whether or not to set edge flag for the new vert depends
521 * on whether it's a user-defined clipping plane. We're
522 * copying NVIDIA's behaviour here.
524 if (is_user_clip_plane
) {
525 /* we want to see an edge along the clip plane */
527 new_vert
->edgeflag
= TRUE
;
530 /* we don't want to see an edge along the frustum clip plane */
531 *new_edge
= *edge_prev
;
532 new_vert
->edgeflag
= FALSE
;
538 float t
= dp_prev
/ (dp_prev
- dp
);
539 interp( clipper
, new_vert
, t
, vert_prev
, vert
, viewport_index
);
541 /* Copy starting vert's edgeflag:
543 new_vert
->edgeflag
= vert_prev
->edgeflag
;
544 *new_edge
= *edge_prev
;
553 /* swap in/out lists */
555 struct vertex_header
**tmp
= inlist
;
561 boolean
*tmp
= inEdges
;
568 /* If constant interpolated, copy provoking vertex attrib to polygon vertex[0]
571 if (clipper
->num_const_attribs
) {
572 if (stage
->draw
->rasterizer
->flatshade_first
) {
573 if (inlist
[0] != header
->v
[0]) {
574 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
575 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
577 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
578 copy_flat(stage
, inlist
[0], header
->v
[0]);
582 if (inlist
[0] != header
->v
[2]) {
583 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
584 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
586 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
587 copy_flat(stage
, inlist
[0], header
->v
[2]);
592 /* Emit the polygon as triangles to the setup stage:
594 emit_poly(stage
, inlist
, inEdges
, n
, header
);
599 /* Clip a line against the viewport and user clip planes.
602 do_clip_line(struct draw_stage
*stage
,
603 struct prim_header
*header
,
606 const struct clip_stage
*clipper
= clip_stage(stage
);
607 struct vertex_header
*v0
= header
->v
[0];
608 struct vertex_header
*v1
= header
->v
[1];
609 struct vertex_header
*prov_vertex
;
612 struct prim_header newprim
;
615 newprim
.flags
= header
->flags
;
617 if (stage
->draw
->rasterizer
->flatshade_first
) {
623 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
626 const unsigned plane_idx
= ffs(clipmask
)-1;
627 const float dp0
= getclipdist(clipper
, v0
, plane_idx
);
628 const float dp1
= getclipdist(clipper
, v1
, plane_idx
);
630 if (util_is_inf_or_nan(dp0
) || util_is_inf_or_nan(dp1
))
631 return; //discard nan
634 float t
= dp1
/ (dp1
- dp0
);
639 float t
= dp0
/ (dp0
- dp1
);
644 return; /* discard */
646 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
650 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
, viewport_index
);
651 if (stage
->draw
->rasterizer
->flatshade_first
) {
652 copy_flat(stage
, stage
->tmp
[0], v0
); /* copy v0 color to tmp[0] */
655 copy_flat(stage
, stage
->tmp
[0], v1
); /* copy v1 color to tmp[0] */
657 newprim
.v
[0] = stage
->tmp
[0];
664 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
, viewport_index
);
665 if (stage
->draw
->rasterizer
->flatshade_first
) {
666 copy_flat(stage
, stage
->tmp
[1], v0
); /* copy v0 color to tmp[1] */
669 copy_flat(stage
, stage
->tmp
[1], v1
); /* copy v1 color to tmp[1] */
671 newprim
.v
[1] = stage
->tmp
[1];
677 stage
->next
->line( stage
->next
, &newprim
);
682 clip_point(struct draw_stage
*stage
, struct prim_header
*header
)
684 if (header
->v
[0]->clipmask
== 0)
685 stage
->next
->point( stage
->next
, header
);
690 * Clip points but ignore the first 4 (xy) clip planes.
691 * (Because the generated clip mask is completely unaffacted by guard band,
692 * we still need to manually evaluate the x/y planes if they are outside
693 * the guard band and not just outside the vp.)
696 clip_point_guard_xy(struct draw_stage
*stage
, struct prim_header
*header
)
698 unsigned clipmask
= header
->v
[0]->clipmask
;
699 if ((clipmask
& 0xffffffff) == 0)
700 stage
->next
->point(stage
->next
, header
);
701 else if ((clipmask
& 0xfffffff0) == 0) {
703 const unsigned plane_idx
= ffs(clipmask
)-1;
704 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
705 /* TODO: this should really do proper guardband clipping,
706 * currently just throw out infs/nans.
707 * Also note that vertices with negative w values MUST be tossed
708 * out (not sure if proper guardband clipping would do this
709 * automatically). These would usually be captured by depth clip
710 * too but this can be disabled.
712 if (header
->v
[0]->clip_pos
[3] <= 0.0f
||
713 util_is_inf_or_nan(header
->v
[0]->clip_pos
[0]) ||
714 util_is_inf_or_nan(header
->v
[0]->clip_pos
[1]))
717 stage
->next
->point(stage
->next
, header
);
723 clip_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
725 stage
->point
= stage
->draw
->guard_band_points_xy
? clip_point_guard_xy
: clip_point
;
726 stage
->point(stage
, header
);
731 clip_line(struct draw_stage
*stage
, struct prim_header
*header
)
733 unsigned clipmask
= (header
->v
[0]->clipmask
|
734 header
->v
[1]->clipmask
);
737 /* no clipping needed */
738 stage
->next
->line( stage
->next
, header
);
740 else if ((header
->v
[0]->clipmask
&
741 header
->v
[1]->clipmask
) == 0) {
742 do_clip_line(stage
, header
, clipmask
);
744 /* else, totally clipped */
749 clip_tri(struct draw_stage
*stage
, struct prim_header
*header
)
751 unsigned clipmask
= (header
->v
[0]->clipmask
|
752 header
->v
[1]->clipmask
|
753 header
->v
[2]->clipmask
);
756 /* no clipping needed */
757 stage
->next
->tri( stage
->next
, header
);
759 else if ((header
->v
[0]->clipmask
&
760 header
->v
[1]->clipmask
&
761 header
->v
[2]->clipmask
) == 0) {
762 do_clip_tri(stage
, header
, clipmask
);
768 find_interp(const struct draw_fragment_shader
*fs
, int *indexed_interp
,
769 uint semantic_name
, uint semantic_index
)
772 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
773 * from the array we've filled before. */
774 if ((semantic_name
== TGSI_SEMANTIC_COLOR
||
775 semantic_name
== TGSI_SEMANTIC_BCOLOR
) &&
776 semantic_index
< 2) {
777 interp
= indexed_interp
[semantic_index
];
778 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
||
779 semantic_name
== TGSI_SEMANTIC_CLIPVERTEX
) {
780 /* these inputs are handled specially always */
783 /* Otherwise, search in the FS inputs, with a decent default
784 * if we don't find it.
785 * This probably only matters for layer, vpindex, culldist, maybe
789 if (semantic_name
== TGSI_SEMANTIC_LAYER
||
790 semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
791 interp
= TGSI_INTERPOLATE_CONSTANT
;
794 interp
= TGSI_INTERPOLATE_PERSPECTIVE
;
797 for (j
= 0; j
< fs
->info
.num_inputs
; j
++) {
798 if (semantic_name
== fs
->info
.input_semantic_name
[j
] &&
799 semantic_index
== fs
->info
.input_semantic_index
[j
]) {
800 interp
= fs
->info
.input_interpolate
[j
];
809 /* Update state. Could further delay this until we hit the first
810 * primitive that really requires clipping.
813 clip_init_state(struct draw_stage
*stage
)
815 struct clip_stage
*clipper
= clip_stage(stage
);
816 const struct draw_context
*draw
= stage
->draw
;
817 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
818 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
820 int indexed_interp
[2];
822 clipper
->pos_attr
= draw_current_shader_position_output(draw
);
823 clipper
->have_clipdist
= draw_current_shader_num_written_clipdistances(draw
) > 0;
824 if (draw_current_shader_clipvertex_output(draw
) != clipper
->pos_attr
) {
825 clipper
->cv_attr
= (int)draw_current_shader_clipvertex_output(draw
);
828 clipper
->cv_attr
= -1;
831 /* We need to know for each attribute what kind of interpolation is
832 * done on it (flat, smooth or noperspective). But the information
833 * is not directly accessible for outputs, only for inputs. So we
834 * have to match semantic name and index between the VS (or GS/ES)
835 * outputs and the FS inputs to get to the interpolation mode.
837 * The only hitch is with gl_FrontColor/gl_BackColor which map to
838 * gl_Color, and their Secondary versions. First there are (up to)
839 * two outputs for one input, so we tuck the information in a
840 * specific array. Second if they don't have qualifiers, the
841 * default value has to be picked from the global shade mode.
843 * Of course, if we don't have a fragment shader in the first
844 * place, defaults should be used.
847 /* First pick up the interpolation mode for
848 * gl_Color/gl_SecondaryColor, with the correct default.
850 indexed_interp
[0] = indexed_interp
[1] = draw
->rasterizer
->flatshade
?
851 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
854 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
855 if (fs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
&&
856 fs
->info
.input_semantic_index
[i
] < 2) {
857 if (fs
->info
.input_interpolate
[i
] != TGSI_INTERPOLATE_COLOR
)
858 indexed_interp
[fs
->info
.input_semantic_index
[i
]] = fs
->info
.input_interpolate
[i
];
863 /* Then resolve the interpolation mode for every output attribute. */
865 clipper
->num_const_attribs
= 0;
866 clipper
->num_linear_attribs
= 0;
867 clipper
->num_perspect_attribs
= 0;
868 for (i
= 0; i
< info
->num_outputs
; i
++) {
869 /* Find the interpolation mode for a specific attribute */
870 int interp
= find_interp(fs
, indexed_interp
,
871 info
->output_semantic_name
[i
],
872 info
->output_semantic_index
[i
]);
874 case TGSI_INTERPOLATE_CONSTANT
:
875 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
;
876 clipper
->num_const_attribs
++;
878 case TGSI_INTERPOLATE_LINEAR
:
879 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
;
880 clipper
->num_linear_attribs
++;
882 case TGSI_INTERPOLATE_PERSPECTIVE
:
883 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
;
884 clipper
->num_perspect_attribs
++;
886 case TGSI_INTERPOLATE_COLOR
:
887 if (draw
->rasterizer
->flatshade
) {
888 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
;
889 clipper
->num_const_attribs
++;
891 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
;
892 clipper
->num_perspect_attribs
++;
896 assert(interp
== -1);
900 /* Search the extra vertex attributes */
901 for (j
= 0; j
< draw
->extra_shader_outputs
.num
; j
++) {
902 /* Find the interpolation mode for a specific attribute */
903 int interp
= find_interp(fs
, indexed_interp
,
904 draw
->extra_shader_outputs
.semantic_name
[j
],
905 draw
->extra_shader_outputs
.semantic_index
[j
]);
907 case TGSI_INTERPOLATE_CONSTANT
:
908 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
+ j
;
909 clipper
->num_const_attribs
++;
911 case TGSI_INTERPOLATE_LINEAR
:
912 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
+ j
;
913 clipper
->num_linear_attribs
++;
915 case TGSI_INTERPOLATE_PERSPECTIVE
:
916 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
+ j
;
917 clipper
->num_perspect_attribs
++;
920 assert(interp
== -1);
925 stage
->tri
= clip_tri
;
926 stage
->line
= clip_line
;
931 static void clip_first_tri(struct draw_stage
*stage
,
932 struct prim_header
*header
)
934 clip_init_state( stage
);
935 stage
->tri( stage
, header
);
938 static void clip_first_line(struct draw_stage
*stage
,
939 struct prim_header
*header
)
941 clip_init_state( stage
);
942 stage
->line( stage
, header
);
946 static void clip_flush(struct draw_stage
*stage
, unsigned flags
)
948 stage
->tri
= clip_first_tri
;
949 stage
->line
= clip_first_line
;
950 stage
->next
->flush( stage
->next
, flags
);
954 static void clip_reset_stipple_counter(struct draw_stage
*stage
)
956 stage
->next
->reset_stipple_counter( stage
->next
);
960 static void clip_destroy(struct draw_stage
*stage
)
962 draw_free_temp_verts( stage
);
968 * Allocate a new clipper stage.
969 * \return pointer to new stage object
971 struct draw_stage
*draw_clip_stage(struct draw_context
*draw
)
973 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
977 clipper
->stage
.draw
= draw
;
978 clipper
->stage
.name
= "clipper";
979 clipper
->stage
.point
= clip_first_point
;
980 clipper
->stage
.line
= clip_first_line
;
981 clipper
->stage
.tri
= clip_first_tri
;
982 clipper
->stage
.flush
= clip_flush
;
983 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
984 clipper
->stage
.destroy
= clip_destroy
;
986 clipper
->plane
= draw
->plane
;
988 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
991 return &clipper
->stage
;
995 clipper
->stage
.destroy( &clipper
->stage
);