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_memory.h"
36 #include "util/u_math.h"
38 #include "pipe/p_shader_tokens.h"
41 #include "draw_pipe.h"
46 /** Set to 1 to enable printing of coords before/after clipping */
50 #ifndef DIFFERENT_SIGNS
51 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
54 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
59 struct draw_stage stage
; /**< base class */
62 boolean have_clipdist
;
65 /* List of the attributes to be constant interpolated. */
66 uint num_const_attribs
;
67 uint8_t const_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
68 /* List of the attributes to be linear interpolated. */
69 uint num_linear_attribs
;
70 uint8_t linear_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
71 /* List of the attributes to be perspective interpolated. */
72 uint num_perspect_attribs
;
73 uint8_t perspect_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
80 static inline struct clip_stage
*clip_stage(struct draw_stage
*stage
)
82 return (struct clip_stage
*)stage
;
85 static inline unsigned
86 draw_viewport_index(struct draw_context
*draw
,
87 const struct vertex_header
*leading_vertex
)
89 if (draw_current_shader_uses_viewport_index(draw
)) {
90 unsigned viewport_index_output
=
91 draw_current_shader_viewport_index_output(draw
);
92 unsigned viewport_index
=
93 *((unsigned*)leading_vertex
->data
[viewport_index_output
]);
94 return draw_clamp_viewport_idx(viewport_index
);
101 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
104 /* All attributes are float[4], so this is easy:
106 static void interp_attr(float dst
[4],
111 dst
[0] = LINTERP( t
, out
[0], in
[0] );
112 dst
[1] = LINTERP( t
, out
[1], in
[1] );
113 dst
[2] = LINTERP( t
, out
[2], in
[2] );
114 dst
[3] = LINTERP( t
, out
[3], in
[3] );
119 * Copy flat shaded attributes src vertex to dst vertex.
121 static void copy_flat(struct draw_stage
*stage
,
122 struct vertex_header
*dst
,
123 const struct vertex_header
*src
)
125 const struct clip_stage
*clipper
= clip_stage(stage
);
127 for (i
= 0; i
< clipper
->num_const_attribs
; i
++) {
128 const uint attr
= clipper
->const_attribs
[i
];
129 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
133 /* Interpolate between two vertices to produce a third.
135 static void interp(const struct clip_stage
*clip
,
136 struct vertex_header
*dst
,
138 const struct vertex_header
*out
,
139 const struct vertex_header
*in
,
140 unsigned viewport_index
)
142 const unsigned pos_attr
= clip
->pos_attr
;
149 dst
->edgeflag
= 0; /* will get overwritten later */
151 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
153 /* Interpolate the clip-space coords.
155 if (clip
->cv_attr
>= 0) {
156 interp_attr(dst
->data
[clip
->cv_attr
], t
,
157 in
->data
[clip
->cv_attr
], out
->data
[clip
->cv_attr
]);
159 /* interpolate the clip-space position */
160 interp_attr(dst
->clip_pos
, t
, in
->clip_pos
, out
->clip_pos
);
162 /* Do the projective divide and viewport transformation to get
163 * new window coordinates:
166 const float *pos
= dst
->clip_pos
;
168 clip
->stage
.draw
->viewports
[viewport_index
].scale
;
170 clip
->stage
.draw
->viewports
[viewport_index
].translate
;
171 const float oow
= 1.0f
/ pos
[3];
173 dst
->data
[pos_attr
][0] = pos
[0] * oow
* scale
[0] + trans
[0];
174 dst
->data
[pos_attr
][1] = pos
[1] * oow
* scale
[1] + trans
[1];
175 dst
->data
[pos_attr
][2] = pos
[2] * oow
* scale
[2] + trans
[2];
176 dst
->data
[pos_attr
][3] = oow
;
180 /* interp perspective attribs */
181 for (j
= 0; j
< clip
->num_perspect_attribs
; j
++) {
182 const unsigned attr
= clip
->perspect_attribs
[j
];
183 interp_attr(dst
->data
[attr
], t
, in
->data
[attr
], out
->data
[attr
]);
187 * Compute the t in screen-space instead of 3d space to use
188 * for noperspective interpolation.
190 * The points can be aligned with the X axis, so in that case try
191 * the Y. When both points are at the same screen position, we can
192 * pick whatever value (the interpolated point won't be in front
193 * anyway), so just use the 3d t.
195 if (clip
->num_linear_attribs
) {
198 /* find either in.x != out.x or in.y != out.y */
199 for (k
= 0; k
< 2; k
++) {
200 if (in
->clip_pos
[k
] != out
->clip_pos
[k
]) {
201 /* do divide by W, then compute linear interpolation factor */
202 float in_coord
= in
->clip_pos
[k
] / in
->clip_pos
[3];
203 float out_coord
= out
->clip_pos
[k
] / out
->clip_pos
[3];
204 float dst_coord
= dst
->clip_pos
[k
] / dst
->clip_pos
[3];
205 t_nopersp
= (dst_coord
- out_coord
) / (in_coord
- out_coord
);
209 for (j
= 0; j
< clip
->num_linear_attribs
; j
++) {
210 const unsigned attr
= clip
->linear_attribs
[j
];
211 interp_attr(dst
->data
[attr
], t_nopersp
, in
->data
[attr
], out
->data
[attr
]);
217 * Checks whether the specified triangle is empty and if it is returns
218 * true, otherwise returns false.
219 * Triangle is considered null/empty if its area is equal to zero.
221 static inline boolean
222 is_tri_null(const struct clip_stage
*clip
, const struct prim_header
*header
)
224 const unsigned pos_attr
= clip
->pos_attr
;
225 float x1
= header
->v
[1]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
226 float y1
= header
->v
[1]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
227 float z1
= header
->v
[1]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
229 float x2
= header
->v
[2]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
230 float y2
= header
->v
[2]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
231 float z2
= header
->v
[2]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
233 float vx
= y1
* z2
- z1
* y2
;
234 float vy
= x1
* z2
- z1
* x2
;
235 float vz
= x1
* y2
- y1
* x2
;
237 return (vx
*vx
+ vy
*vy
+ vz
*vz
) == 0.f
;
241 * Emit a post-clip polygon to the next pipeline stage. The polygon
242 * will be convex and the provoking vertex will always be vertex[0].
244 static void emit_poly(struct draw_stage
*stage
,
245 struct vertex_header
**inlist
,
246 const boolean
*edgeflags
,
248 const struct prim_header
*origPrim
)
250 const struct clip_stage
*clipper
= clip_stage(stage
);
251 struct prim_header header
;
253 ushort edge_first
, edge_middle
, edge_last
;
254 boolean last_tri_was_null
= FALSE
;
255 boolean tri_was_not_null
= FALSE
;
257 if (stage
->draw
->rasterizer
->flatshade_first
) {
258 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
259 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
260 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
263 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
264 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
265 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
271 /* later stages may need the determinant, but only the sign matters */
272 header
.det
= origPrim
->det
;
273 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
276 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
278 /* order the triangle verts to respect the provoking vertex mode */
279 if (stage
->draw
->rasterizer
->flatshade_first
) {
280 header
.v
[0] = inlist
[0]; /* the provoking vertex */
281 header
.v
[1] = inlist
[i
-1];
282 header
.v
[2] = inlist
[i
];
285 header
.v
[0] = inlist
[i
-1];
286 header
.v
[1] = inlist
[i
];
287 header
.v
[2] = inlist
[0]; /* the provoking vertex */
290 tri_null
= is_tri_null(clipper
, &header
);
291 /* If we generated a triangle with an area, aka. non-null triangle,
292 * or if the previous triangle was also null then skip all subsequent
294 if ((tri_was_not_null
&& tri_null
) || (last_tri_was_null
&& tri_null
)) {
295 last_tri_was_null
= tri_null
;
298 last_tri_was_null
= tri_null
;
300 tri_was_not_null
= TRUE
;
303 if (!edgeflags
[i
-1]) {
304 header
.flags
&= ~edge_middle
;
307 if (i
== n
- 1 && edgeflags
[i
])
308 header
.flags
|= edge_last
;
312 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
313 stage
->draw
->rasterizer
->flatshade_first
);
314 for (j
= 0; j
< 3; j
++) {
315 debug_printf(" Vert %d: clip pos: %f %f %f %f\n", j
,
316 header
.v
[j
]->clip_pos
[0],
317 header
.v
[j
]->clip_pos
[1],
318 header
.v
[j
]->clip_pos
[2],
319 header
.v
[j
]->clip_pos
[3]);
320 if (clipper
->cv_attr
>= 0) {
321 debug_printf(" Vert %d: cv: %f %f %f %f\n", j
,
322 header
.v
[j
]->data
[clipper
->cv_attr
][0],
323 header
.v
[j
]->data
[clipper
->cv_attr
][1],
324 header
.v
[j
]->data
[clipper
->cv_attr
][2],
325 header
.v
[j
]->data
[clipper
->cv_attr
][3]);
327 for (k
= 0; k
< draw_num_shader_outputs(stage
->draw
); k
++) {
328 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
329 header
.v
[j
]->data
[k
][0],
330 header
.v
[j
]->data
[k
][1],
331 header
.v
[j
]->data
[k
][2],
332 header
.v
[j
]->data
[k
][3]);
336 stage
->next
->tri(stage
->next
, &header
);
342 dot4(const float *a
, const float *b
)
344 return (a
[0] * b
[0] +
351 * this function extracts the clip distance for the current plane,
352 * it first checks if the shader provided a clip distance, otherwise
353 * it works out the value using the clipvertex
355 static inline float getclipdist(const struct clip_stage
*clipper
,
356 struct vertex_header
*vert
,
362 /* ordinary xyz view volume clipping uses pos output */
363 plane
= clipper
->plane
[plane_idx
];
364 dp
= dot4(vert
->clip_pos
, plane
);
366 else if (clipper
->have_clipdist
) {
367 /* pick the correct clipdistance element from the output vectors */
368 int _idx
= plane_idx
- 6;
370 int vidx
= cdi
? _idx
- 4 : _idx
;
371 dp
= vert
->data
[draw_current_shader_clipdistance_output(clipper
->stage
.draw
, cdi
)][vidx
];
374 * legacy user clip planes or gl_ClipVertex
376 plane
= clipper
->plane
[plane_idx
];
377 if (clipper
->cv_attr
>= 0) {
378 dp
= dot4(vert
->data
[clipper
->cv_attr
], plane
);
381 dp
= dot4(vert
->clip_pos
, plane
);
387 /* Clip a triangle against the viewport and user clip planes.
390 do_clip_tri(struct draw_stage
*stage
,
391 struct prim_header
*header
,
394 struct clip_stage
*clipper
= clip_stage( stage
);
395 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
396 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
397 struct vertex_header
**inlist
= a
;
398 struct vertex_header
**outlist
= b
;
399 struct vertex_header
*prov_vertex
;
403 boolean aEdges
[MAX_CLIPPED_VERTICES
];
404 boolean bEdges
[MAX_CLIPPED_VERTICES
];
405 boolean
*inEdges
= aEdges
;
406 boolean
*outEdges
= bEdges
;
407 int viewport_index
= 0;
409 inlist
[0] = header
->v
[0];
410 inlist
[1] = header
->v
[1];
411 inlist
[2] = header
->v
[2];
414 * For d3d10, we need to take this from the leading (first) vertex.
415 * For GL, we could do anything (as long as we advertize
416 * GL_UNDEFINED_VERTEX for the VIEWPORT_INDEX_PROVOKING_VERTEX query),
417 * but it needs to be consistent with what other parts (i.e. driver)
418 * will do, and that seems easier with GL_PROVOKING_VERTEX logic.
420 if (stage
->draw
->rasterizer
->flatshade_first
) {
421 prov_vertex
= inlist
[0];
424 prov_vertex
= inlist
[2];
426 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
429 const float *v0
= header
->v
[0]->clip_pos
;
430 const float *v1
= header
->v
[1]->clip_pos
;
431 const float *v2
= header
->v
[2]->clip_pos
;
432 debug_printf("Clip triangle pos:\n");
433 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
434 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
435 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
436 if (clipper
->cv_attr
>= 0) {
437 const float *v0
= header
->v
[0]->data
[clipper
->cv_attr
];
438 const float *v1
= header
->v
[1]->data
[clipper
->cv_attr
];
439 const float *v2
= header
->v
[2]->data
[clipper
->cv_attr
];
440 debug_printf("Clip triangle cv:\n");
441 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
442 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
443 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
448 * Note: at this point we can't just use the per-vertex edge flags.
449 * We have to observe the edge flag bits set in header->flags which
450 * were set during primitive decomposition. Put those flags into
451 * an edge flags array which parallels the vertex array.
452 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
453 * the header.flags bit is set AND the per-vertex edgeflag field is set.
455 inEdges
[0] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_0
);
456 inEdges
[1] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_1
);
457 inEdges
[2] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_2
);
459 while (clipmask
&& n
>= 3) {
460 const unsigned plane_idx
= ffs(clipmask
)-1;
461 const boolean is_user_clip_plane
= plane_idx
>= 6;
462 struct vertex_header
*vert_prev
= inlist
[0];
463 boolean
*edge_prev
= &inEdges
[0];
465 unsigned outcount
= 0;
467 dp_prev
= getclipdist(clipper
, vert_prev
, plane_idx
);
468 clipmask
&= ~(1<<plane_idx
);
470 if (util_is_inf_or_nan(dp_prev
))
471 return; //discard nan
473 assert(n
< MAX_CLIPPED_VERTICES
);
474 if (n
>= MAX_CLIPPED_VERTICES
)
476 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
477 inEdges
[n
] = inEdges
[0];
479 for (i
= 1; i
<= n
; i
++) {
480 struct vertex_header
*vert
= inlist
[i
];
481 boolean
*edge
= &inEdges
[i
];
483 float dp
= getclipdist(clipper
, vert
, plane_idx
);
485 if (util_is_inf_or_nan(dp
))
486 return; //discard nan
488 if (dp_prev
>= 0.0f
) {
489 assert(outcount
< MAX_CLIPPED_VERTICES
);
490 if (outcount
>= MAX_CLIPPED_VERTICES
)
492 outEdges
[outcount
] = *edge_prev
;
493 outlist
[outcount
++] = vert_prev
;
496 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
497 struct vertex_header
*new_vert
;
500 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
501 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
503 new_vert
= clipper
->stage
.tmp
[tmpnr
++];
505 assert(outcount
< MAX_CLIPPED_VERTICES
);
506 if (outcount
>= MAX_CLIPPED_VERTICES
)
509 new_edge
= &outEdges
[outcount
];
510 outlist
[outcount
++] = new_vert
;
513 /* Going out of bounds. Avoid division by zero as we
514 * know dp != dp_prev from DIFFERENT_SIGNS, above.
516 float t
= dp
/ (dp
- dp_prev
);
517 interp( clipper
, new_vert
, t
, vert
, vert_prev
, viewport_index
);
519 /* Whether or not to set edge flag for the new vert depends
520 * on whether it's a user-defined clipping plane. We're
521 * copying NVIDIA's behaviour here.
523 if (is_user_clip_plane
) {
524 /* we want to see an edge along the clip plane */
526 new_vert
->edgeflag
= TRUE
;
529 /* we don't want to see an edge along the frustum clip plane */
530 *new_edge
= *edge_prev
;
531 new_vert
->edgeflag
= FALSE
;
537 float t
= dp_prev
/ (dp_prev
- dp
);
538 interp( clipper
, new_vert
, t
, vert_prev
, vert
, viewport_index
);
540 /* Copy starting vert's edgeflag:
542 new_vert
->edgeflag
= vert_prev
->edgeflag
;
543 *new_edge
= *edge_prev
;
552 /* swap in/out lists */
554 struct vertex_header
**tmp
= inlist
;
560 boolean
*tmp
= inEdges
;
567 /* If constant interpolated, copy provoking vertex attrib to polygon vertex[0]
570 if (clipper
->num_const_attribs
) {
571 if (stage
->draw
->rasterizer
->flatshade_first
) {
572 if (inlist
[0] != header
->v
[0]) {
573 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
574 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
576 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
577 copy_flat(stage
, inlist
[0], header
->v
[0]);
581 if (inlist
[0] != header
->v
[2]) {
582 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
583 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
585 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
586 copy_flat(stage
, inlist
[0], header
->v
[2]);
591 /* Emit the polygon as triangles to the setup stage:
593 emit_poly(stage
, inlist
, inEdges
, n
, header
);
598 /* Clip a line against the viewport and user clip planes.
601 do_clip_line(struct draw_stage
*stage
,
602 struct prim_header
*header
,
605 const struct clip_stage
*clipper
= clip_stage(stage
);
606 struct vertex_header
*v0
= header
->v
[0];
607 struct vertex_header
*v1
= header
->v
[1];
608 struct vertex_header
*prov_vertex
;
611 struct prim_header newprim
;
614 if (stage
->draw
->rasterizer
->flatshade_first
) {
620 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
623 const unsigned plane_idx
= ffs(clipmask
)-1;
624 const float dp0
= getclipdist(clipper
, v0
, plane_idx
);
625 const float dp1
= getclipdist(clipper
, v1
, plane_idx
);
627 if (util_is_inf_or_nan(dp0
) || util_is_inf_or_nan(dp1
))
628 return; //discard nan
631 float t
= dp1
/ (dp1
- dp0
);
636 float t
= dp0
/ (dp0
- dp1
);
641 return; /* discard */
643 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
647 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
, viewport_index
);
648 if (stage
->draw
->rasterizer
->flatshade_first
) {
649 copy_flat(stage
, stage
->tmp
[0], v0
); /* copy v0 color to tmp[0] */
652 copy_flat(stage
, stage
->tmp
[0], v1
); /* copy v1 color to tmp[0] */
654 newprim
.v
[0] = stage
->tmp
[0];
661 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
, viewport_index
);
662 if (stage
->draw
->rasterizer
->flatshade_first
) {
663 copy_flat(stage
, stage
->tmp
[1], v0
); /* copy v0 color to tmp[1] */
666 copy_flat(stage
, stage
->tmp
[1], v1
); /* copy v1 color to tmp[1] */
668 newprim
.v
[1] = stage
->tmp
[1];
674 stage
->next
->line( stage
->next
, &newprim
);
679 clip_point(struct draw_stage
*stage
, struct prim_header
*header
)
681 if (header
->v
[0]->clipmask
== 0)
682 stage
->next
->point( stage
->next
, header
);
687 * Clip points but ignore the first 4 (xy) clip planes.
688 * (Because the generated clip mask is completely unaffacted by guard band,
689 * we still need to manually evaluate the x/y planes if they are outside
690 * the guard band and not just outside the vp.)
693 clip_point_guard_xy(struct draw_stage
*stage
, struct prim_header
*header
)
695 unsigned clipmask
= header
->v
[0]->clipmask
;
696 if ((clipmask
& 0xffffffff) == 0)
697 stage
->next
->point(stage
->next
, header
);
698 else if ((clipmask
& 0xfffffff0) == 0) {
700 const unsigned plane_idx
= ffs(clipmask
)-1;
701 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
702 /* TODO: this should really do proper guardband clipping,
703 * currently just throw out infs/nans.
704 * Also note that vertices with negative w values MUST be tossed
705 * out (not sure if proper guardband clipping would do this
706 * automatically). These would usually be captured by depth clip
707 * too but this can be disabled.
709 if (header
->v
[0]->clip_pos
[3] <= 0.0f
||
710 util_is_inf_or_nan(header
->v
[0]->clip_pos
[0]) ||
711 util_is_inf_or_nan(header
->v
[0]->clip_pos
[1]))
714 stage
->next
->point(stage
->next
, header
);
720 clip_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
722 stage
->point
= stage
->draw
->guard_band_points_xy
? clip_point_guard_xy
: clip_point
;
723 stage
->point(stage
, header
);
728 clip_line(struct draw_stage
*stage
, struct prim_header
*header
)
730 unsigned clipmask
= (header
->v
[0]->clipmask
|
731 header
->v
[1]->clipmask
);
734 /* no clipping needed */
735 stage
->next
->line( stage
->next
, header
);
737 else if ((header
->v
[0]->clipmask
&
738 header
->v
[1]->clipmask
) == 0) {
739 do_clip_line(stage
, header
, clipmask
);
741 /* else, totally clipped */
746 clip_tri(struct draw_stage
*stage
, struct prim_header
*header
)
748 unsigned clipmask
= (header
->v
[0]->clipmask
|
749 header
->v
[1]->clipmask
|
750 header
->v
[2]->clipmask
);
753 /* no clipping needed */
754 stage
->next
->tri( stage
->next
, header
);
756 else if ((header
->v
[0]->clipmask
&
757 header
->v
[1]->clipmask
&
758 header
->v
[2]->clipmask
) == 0) {
759 do_clip_tri(stage
, header
, clipmask
);
765 find_interp(const struct draw_fragment_shader
*fs
, int *indexed_interp
,
766 uint semantic_name
, uint semantic_index
)
769 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
770 * from the array we've filled before. */
771 if (semantic_name
== TGSI_SEMANTIC_COLOR
||
772 semantic_name
== TGSI_SEMANTIC_BCOLOR
) {
773 interp
= indexed_interp
[semantic_index
];
774 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
||
775 semantic_name
== TGSI_SEMANTIC_CLIPVERTEX
) {
776 /* these inputs are handled specially always */
779 /* Otherwise, search in the FS inputs, with a decent default
780 * if we don't find it.
781 * This probably only matters for layer, vpindex, culldist, maybe
785 if (semantic_name
== TGSI_SEMANTIC_LAYER
||
786 semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
787 interp
= TGSI_INTERPOLATE_CONSTANT
;
790 interp
= TGSI_INTERPOLATE_PERSPECTIVE
;
793 for (j
= 0; j
< fs
->info
.num_inputs
; j
++) {
794 if (semantic_name
== fs
->info
.input_semantic_name
[j
] &&
795 semantic_index
== fs
->info
.input_semantic_index
[j
]) {
796 interp
= fs
->info
.input_interpolate
[j
];
805 /* Update state. Could further delay this until we hit the first
806 * primitive that really requires clipping.
809 clip_init_state(struct draw_stage
*stage
)
811 struct clip_stage
*clipper
= clip_stage(stage
);
812 const struct draw_context
*draw
= stage
->draw
;
813 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
814 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
816 int indexed_interp
[2];
818 clipper
->pos_attr
= draw_current_shader_position_output(draw
);
819 clipper
->have_clipdist
= draw_current_shader_num_written_clipdistances(draw
) > 0;
820 if (draw_current_shader_clipvertex_output(draw
) != clipper
->pos_attr
) {
821 clipper
->cv_attr
= (int)draw_current_shader_clipvertex_output(draw
);
824 clipper
->cv_attr
= -1;
827 /* We need to know for each attribute what kind of interpolation is
828 * done on it (flat, smooth or noperspective). But the information
829 * is not directly accessible for outputs, only for inputs. So we
830 * have to match semantic name and index between the VS (or GS/ES)
831 * outputs and the FS inputs to get to the interpolation mode.
833 * The only hitch is with gl_FrontColor/gl_BackColor which map to
834 * gl_Color, and their Secondary versions. First there are (up to)
835 * two outputs for one input, so we tuck the information in a
836 * specific array. Second if they don't have qualifiers, the
837 * default value has to be picked from the global shade mode.
839 * Of course, if we don't have a fragment shader in the first
840 * place, defaults should be used.
843 /* First pick up the interpolation mode for
844 * gl_Color/gl_SecondaryColor, with the correct default.
846 indexed_interp
[0] = indexed_interp
[1] = draw
->rasterizer
->flatshade
?
847 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
850 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
851 if (fs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
) {
852 if (fs
->info
.input_interpolate
[i
] != TGSI_INTERPOLATE_COLOR
)
853 indexed_interp
[fs
->info
.input_semantic_index
[i
]] = fs
->info
.input_interpolate
[i
];
858 /* Then resolve the interpolation mode for every output attribute. */
860 clipper
->num_const_attribs
= 0;
861 clipper
->num_linear_attribs
= 0;
862 clipper
->num_perspect_attribs
= 0;
863 for (i
= 0; i
< info
->num_outputs
; i
++) {
864 /* Find the interpolation mode for a specific attribute */
865 int interp
= find_interp(fs
, indexed_interp
,
866 info
->output_semantic_name
[i
],
867 info
->output_semantic_index
[i
]);
869 case TGSI_INTERPOLATE_CONSTANT
:
870 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
;
871 clipper
->num_const_attribs
++;
873 case TGSI_INTERPOLATE_LINEAR
:
874 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
;
875 clipper
->num_linear_attribs
++;
877 case TGSI_INTERPOLATE_PERSPECTIVE
:
878 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
;
879 clipper
->num_perspect_attribs
++;
882 assert(interp
== -1);
886 /* Search the extra vertex attributes */
887 for (j
= 0; j
< draw
->extra_shader_outputs
.num
; j
++) {
888 /* Find the interpolation mode for a specific attribute */
889 int interp
= find_interp(fs
, indexed_interp
,
890 draw
->extra_shader_outputs
.semantic_name
[j
],
891 draw
->extra_shader_outputs
.semantic_index
[j
]);
893 case TGSI_INTERPOLATE_CONSTANT
:
894 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
+ j
;
895 clipper
->num_const_attribs
++;
897 case TGSI_INTERPOLATE_LINEAR
:
898 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
+ j
;
899 clipper
->num_linear_attribs
++;
901 case TGSI_INTERPOLATE_PERSPECTIVE
:
902 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
+ j
;
903 clipper
->num_perspect_attribs
++;
906 assert(interp
== -1);
911 stage
->tri
= clip_tri
;
912 stage
->line
= clip_line
;
917 static void clip_first_tri(struct draw_stage
*stage
,
918 struct prim_header
*header
)
920 clip_init_state( stage
);
921 stage
->tri( stage
, header
);
924 static void clip_first_line(struct draw_stage
*stage
,
925 struct prim_header
*header
)
927 clip_init_state( stage
);
928 stage
->line( stage
, header
);
932 static void clip_flush(struct draw_stage
*stage
, unsigned flags
)
934 stage
->tri
= clip_first_tri
;
935 stage
->line
= clip_first_line
;
936 stage
->next
->flush( stage
->next
, flags
);
940 static void clip_reset_stipple_counter(struct draw_stage
*stage
)
942 stage
->next
->reset_stipple_counter( stage
->next
);
946 static void clip_destroy(struct draw_stage
*stage
)
948 draw_free_temp_verts( stage
);
954 * Allocate a new clipper stage.
955 * \return pointer to new stage object
957 struct draw_stage
*draw_clip_stage(struct draw_context
*draw
)
959 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
963 clipper
->stage
.draw
= draw
;
964 clipper
->stage
.name
= "clipper";
965 clipper
->stage
.point
= clip_first_point
;
966 clipper
->stage
.line
= clip_first_line
;
967 clipper
->stage
.tri
= clip_first_tri
;
968 clipper
->stage
.flush
= clip_flush
;
969 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
970 clipper
->stage
.destroy
= clip_destroy
;
972 clipper
->plane
= draw
->plane
;
974 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
977 return &clipper
->stage
;
981 clipper
->stage
.destroy( &clipper
->stage
);