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
;
64 /* List of the attributes to be constant interpolated. */
65 uint num_const_attribs
;
66 uint8_t const_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
67 /* List of the attributes to be linear interpolated. */
68 uint num_linear_attribs
;
69 uint8_t linear_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
70 /* List of the attributes to be perspective interpolated. */
71 uint num_perspect_attribs
;
72 uint8_t perspect_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
79 static inline struct clip_stage
*clip_stage(struct draw_stage
*stage
)
81 return (struct clip_stage
*)stage
;
84 static inline unsigned
85 draw_viewport_index(struct draw_context
*draw
,
86 const struct vertex_header
*leading_vertex
)
88 if (draw_current_shader_uses_viewport_index(draw
)) {
89 unsigned viewport_index_output
=
90 draw_current_shader_viewport_index_output(draw
);
91 unsigned viewport_index
=
92 *((unsigned*)leading_vertex
->data
[viewport_index_output
]);
93 return draw_clamp_viewport_idx(viewport_index
);
100 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
103 /* All attributes are float[4], so this is easy:
105 static void interp_attr(float dst
[4],
110 dst
[0] = LINTERP( t
, out
[0], in
[0] );
111 dst
[1] = LINTERP( t
, out
[1], in
[1] );
112 dst
[2] = LINTERP( t
, out
[2], in
[2] );
113 dst
[3] = LINTERP( t
, out
[3], in
[3] );
118 * Copy flat shaded attributes src vertex to dst vertex.
120 static void copy_flat(struct draw_stage
*stage
,
121 struct vertex_header
*dst
,
122 const struct vertex_header
*src
)
124 const struct clip_stage
*clipper
= clip_stage(stage
);
126 for (i
= 0; i
< clipper
->num_const_attribs
; i
++) {
127 const uint attr
= clipper
->const_attribs
[i
];
128 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
132 /* Interpolate between two vertices to produce a third.
134 static void interp(const struct clip_stage
*clip
,
135 struct vertex_header
*dst
,
137 const struct vertex_header
*out
,
138 const struct vertex_header
*in
,
139 unsigned viewport_index
)
141 const unsigned pos_attr
= clip
->pos_attr
;
148 dst
->edgeflag
= 0; /* will get overwritten later */
150 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
152 /* Interpolate the clip-space coords.
154 interp_attr(dst
->clip_vertex
, t
, in
->clip_vertex
, out
->clip_vertex
);
155 /* interpolate the clip-space position */
156 interp_attr(dst
->clip_pos
, t
, in
->clip_pos
, out
->clip_pos
);
158 /* Do the projective divide and viewport transformation to get
159 * new window coordinates:
162 const float *pos
= dst
->clip_pos
;
164 clip
->stage
.draw
->viewports
[viewport_index
].scale
;
166 clip
->stage
.draw
->viewports
[viewport_index
].translate
;
167 const float oow
= 1.0f
/ pos
[3];
169 dst
->data
[pos_attr
][0] = pos
[0] * oow
* scale
[0] + trans
[0];
170 dst
->data
[pos_attr
][1] = pos
[1] * oow
* scale
[1] + trans
[1];
171 dst
->data
[pos_attr
][2] = pos
[2] * oow
* scale
[2] + trans
[2];
172 dst
->data
[pos_attr
][3] = oow
;
176 /* interp perspective attribs */
177 for (j
= 0; j
< clip
->num_perspect_attribs
; j
++) {
178 const unsigned attr
= clip
->perspect_attribs
[j
];
179 interp_attr(dst
->data
[attr
], t
, in
->data
[attr
], out
->data
[attr
]);
183 * Compute the t in screen-space instead of 3d space to use
184 * for noperspective interpolation.
186 * The points can be aligned with the X axis, so in that case try
187 * the Y. When both points are at the same screen position, we can
188 * pick whatever value (the interpolated point won't be in front
189 * anyway), so just use the 3d t.
191 if (clip
->num_linear_attribs
) {
194 /* find either in.x != out.x or in.y != out.y */
195 for (k
= 0; k
< 2; k
++) {
196 if (in
->clip_pos
[k
] != out
->clip_pos
[k
]) {
197 /* do divide by W, then compute linear interpolation factor */
198 float in_coord
= in
->clip_pos
[k
] / in
->clip_pos
[3];
199 float out_coord
= out
->clip_pos
[k
] / out
->clip_pos
[3];
200 float dst_coord
= dst
->clip_pos
[k
] / dst
->clip_pos
[3];
201 t_nopersp
= (dst_coord
- out_coord
) / (in_coord
- out_coord
);
205 for (j
= 0; j
< clip
->num_linear_attribs
; j
++) {
206 const unsigned attr
= clip
->linear_attribs
[j
];
207 interp_attr(dst
->data
[attr
], t_nopersp
, in
->data
[attr
], out
->data
[attr
]);
213 * Checks whether the specified triangle is empty and if it is returns
214 * true, otherwise returns false.
215 * Triangle is considered null/empty if its area is equal to zero.
217 static inline boolean
218 is_tri_null(struct draw_context
*draw
, const struct prim_header
*header
)
220 const unsigned pos_attr
= draw_current_shader_position_output(draw
);
221 float x1
= header
->v
[1]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
222 float y1
= header
->v
[1]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
223 float z1
= header
->v
[1]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
225 float x2
= header
->v
[2]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
226 float y2
= header
->v
[2]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
227 float z2
= header
->v
[2]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
229 float vx
= y1
* z2
- z1
* y2
;
230 float vy
= x1
* z2
- z1
* x2
;
231 float vz
= x1
* y2
- y1
* x2
;
233 return (vx
*vx
+ vy
*vy
+ vz
*vz
) == 0.f
;
237 * Emit a post-clip polygon to the next pipeline stage. The polygon
238 * will be convex and the provoking vertex will always be vertex[0].
240 static void emit_poly(struct draw_stage
*stage
,
241 struct vertex_header
**inlist
,
242 const boolean
*edgeflags
,
244 const struct prim_header
*origPrim
)
246 struct prim_header header
;
248 ushort edge_first
, edge_middle
, edge_last
;
249 boolean last_tri_was_null
= FALSE
;
250 boolean tri_was_not_null
= FALSE
;
252 if (stage
->draw
->rasterizer
->flatshade_first
) {
253 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
254 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
255 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
258 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
259 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
260 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
266 /* later stages may need the determinant, but only the sign matters */
267 header
.det
= origPrim
->det
;
268 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
271 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
273 /* order the triangle verts to respect the provoking vertex mode */
274 if (stage
->draw
->rasterizer
->flatshade_first
) {
275 header
.v
[0] = inlist
[0]; /* the provoking vertex */
276 header
.v
[1] = inlist
[i
-1];
277 header
.v
[2] = inlist
[i
];
280 header
.v
[0] = inlist
[i
-1];
281 header
.v
[1] = inlist
[i
];
282 header
.v
[2] = inlist
[0]; /* the provoking vertex */
285 tri_null
= is_tri_null(stage
->draw
, &header
);
286 /* If we generated a triangle with an area, aka. non-null triangle,
287 * or if the previous triangle was also null then skip all subsequent
289 if ((tri_was_not_null
&& tri_null
) || (last_tri_was_null
&& tri_null
)) {
290 last_tri_was_null
= tri_null
;
293 last_tri_was_null
= tri_null
;
295 tri_was_not_null
= TRUE
;
298 if (!edgeflags
[i
-1]) {
299 header
.flags
&= ~edge_middle
;
302 if (i
== n
- 1 && edgeflags
[i
])
303 header
.flags
|= edge_last
;
307 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
308 stage
->draw
->rasterizer
->flatshade_first
);
309 for (j
= 0; j
< 3; j
++) {
310 debug_printf(" Vert %d: clip: %f %f %f %f\n", j
,
311 header
.v
[j
]->clip_vertex
[0],
312 header
.v
[j
]->clip_vertex
[1],
313 header
.v
[j
]->clip_vertex
[2],
314 header
.v
[j
]->clip_vertex
[3]);
315 for (k
= 0; k
< draw_num_shader_outputs(stage
->draw
); k
++) {
316 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
317 header
.v
[j
]->data
[k
][0],
318 header
.v
[j
]->data
[k
][1],
319 header
.v
[j
]->data
[k
][2],
320 header
.v
[j
]->data
[k
][3]);
324 stage
->next
->tri( stage
->next
, &header
);
330 dot4(const float *a
, const float *b
)
332 return (a
[0] * b
[0] +
339 * this function extracts the clip distance for the current plane,
340 * it first checks if the shader provided a clip distance, otherwise
341 * it works out the value using the clipvertex
343 static inline float getclipdist(const struct clip_stage
*clipper
,
344 struct vertex_header
*vert
,
350 /* ordinary xyz view volume clipping uses pos output */
351 plane
= clipper
->plane
[plane_idx
];
352 dp
= dot4(vert
->clip_pos
, plane
);
354 else if (clipper
->have_clipdist
) {
355 /* pick the correct clipdistance element from the output vectors */
356 int _idx
= plane_idx
- 6;
358 int vidx
= cdi
? _idx
- 4 : _idx
;
359 dp
= vert
->data
[draw_current_shader_clipdistance_output(clipper
->stage
.draw
, cdi
)][vidx
];
362 * legacy user clip planes or gl_ClipVertex
363 * (clip will contain clipVertex output if available, pos otherwise).
365 plane
= clipper
->plane
[plane_idx
];
366 dp
= dot4(vert
->clip_vertex
, plane
);
371 /* Clip a triangle against the viewport and user clip planes.
374 do_clip_tri(struct draw_stage
*stage
,
375 struct prim_header
*header
,
378 struct clip_stage
*clipper
= clip_stage( stage
);
379 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
380 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
381 struct vertex_header
**inlist
= a
;
382 struct vertex_header
**outlist
= b
;
383 struct vertex_header
*prov_vertex
;
387 boolean aEdges
[MAX_CLIPPED_VERTICES
];
388 boolean bEdges
[MAX_CLIPPED_VERTICES
];
389 boolean
*inEdges
= aEdges
;
390 boolean
*outEdges
= bEdges
;
391 int viewport_index
= 0;
393 inlist
[0] = header
->v
[0];
394 inlist
[1] = header
->v
[1];
395 inlist
[2] = header
->v
[2];
398 * For d3d10, we need to take this from the leading (first) vertex.
399 * For GL, we could do anything (as long as we advertize
400 * GL_UNDEFINED_VERTEX for the VIEWPORT_INDEX_PROVOKING_VERTEX query),
401 * but it needs to be consistent with what other parts (i.e. driver)
402 * will do, and that seems easier with GL_PROVOKING_VERTEX logic.
404 if (stage
->draw
->rasterizer
->flatshade_first
) {
405 prov_vertex
= inlist
[0];
408 prov_vertex
= inlist
[2];
410 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
413 const float *v0
= header
->v
[0]->clip_vertex
;
414 const float *v1
= header
->v
[1]->clip_vertex
;
415 const float *v2
= header
->v
[2]->clip_vertex
;
416 debug_printf("Clip triangle:\n");
417 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
418 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
419 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
423 * Note: at this point we can't just use the per-vertex edge flags.
424 * We have to observe the edge flag bits set in header->flags which
425 * were set during primitive decomposition. Put those flags into
426 * an edge flags array which parallels the vertex array.
427 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
428 * the header.flags bit is set AND the per-vertex edgeflag field is set.
430 inEdges
[0] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_0
);
431 inEdges
[1] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_1
);
432 inEdges
[2] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_2
);
434 while (clipmask
&& n
>= 3) {
435 const unsigned plane_idx
= ffs(clipmask
)-1;
436 const boolean is_user_clip_plane
= plane_idx
>= 6;
437 struct vertex_header
*vert_prev
= inlist
[0];
438 boolean
*edge_prev
= &inEdges
[0];
440 unsigned outcount
= 0;
442 dp_prev
= getclipdist(clipper
, vert_prev
, plane_idx
);
443 clipmask
&= ~(1<<plane_idx
);
445 if (util_is_inf_or_nan(dp_prev
))
446 return; //discard nan
448 assert(n
< MAX_CLIPPED_VERTICES
);
449 if (n
>= MAX_CLIPPED_VERTICES
)
451 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
452 inEdges
[n
] = inEdges
[0];
454 for (i
= 1; i
<= n
; i
++) {
455 struct vertex_header
*vert
= inlist
[i
];
456 boolean
*edge
= &inEdges
[i
];
458 float dp
= getclipdist(clipper
, vert
, plane_idx
);
460 if (util_is_inf_or_nan(dp
))
461 return; //discard nan
463 if (dp_prev
>= 0.0f
) {
464 assert(outcount
< MAX_CLIPPED_VERTICES
);
465 if (outcount
>= MAX_CLIPPED_VERTICES
)
467 outEdges
[outcount
] = *edge_prev
;
468 outlist
[outcount
++] = vert_prev
;
471 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
472 struct vertex_header
*new_vert
;
475 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
476 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
478 new_vert
= clipper
->stage
.tmp
[tmpnr
++];
480 assert(outcount
< MAX_CLIPPED_VERTICES
);
481 if (outcount
>= MAX_CLIPPED_VERTICES
)
484 new_edge
= &outEdges
[outcount
];
485 outlist
[outcount
++] = new_vert
;
488 /* Going out of bounds. Avoid division by zero as we
489 * know dp != dp_prev from DIFFERENT_SIGNS, above.
491 float t
= dp
/ (dp
- dp_prev
);
492 interp( clipper
, new_vert
, t
, vert
, vert_prev
, viewport_index
);
494 /* Whether or not to set edge flag for the new vert depends
495 * on whether it's a user-defined clipping plane. We're
496 * copying NVIDIA's behaviour here.
498 if (is_user_clip_plane
) {
499 /* we want to see an edge along the clip plane */
501 new_vert
->edgeflag
= TRUE
;
504 /* we don't want to see an edge along the frustum clip plane */
505 *new_edge
= *edge_prev
;
506 new_vert
->edgeflag
= FALSE
;
512 float t
= dp_prev
/ (dp_prev
- dp
);
513 interp( clipper
, new_vert
, t
, vert_prev
, vert
, viewport_index
);
515 /* Copy starting vert's edgeflag:
517 new_vert
->edgeflag
= vert_prev
->edgeflag
;
518 *new_edge
= *edge_prev
;
527 /* swap in/out lists */
529 struct vertex_header
**tmp
= inlist
;
535 boolean
*tmp
= inEdges
;
542 /* If constant interpolated, copy provoking vertex attrib to polygon vertex[0]
545 if (clipper
->num_const_attribs
) {
546 if (stage
->draw
->rasterizer
->flatshade_first
) {
547 if (inlist
[0] != header
->v
[0]) {
548 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
549 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
551 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
552 copy_flat(stage
, inlist
[0], header
->v
[0]);
556 if (inlist
[0] != header
->v
[2]) {
557 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
558 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
560 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
561 copy_flat(stage
, inlist
[0], header
->v
[2]);
566 /* Emit the polygon as triangles to the setup stage:
568 emit_poly( stage
, inlist
, inEdges
, n
, header
);
573 /* Clip a line against the viewport and user clip planes.
576 do_clip_line(struct draw_stage
*stage
,
577 struct prim_header
*header
,
580 const struct clip_stage
*clipper
= clip_stage( stage
);
581 struct vertex_header
*v0
= header
->v
[0];
582 struct vertex_header
*v1
= header
->v
[1];
583 struct vertex_header
*prov_vertex
;
586 struct prim_header newprim
;
589 if (stage
->draw
->rasterizer
->flatshade_first
) {
595 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
598 const unsigned plane_idx
= ffs(clipmask
)-1;
599 const float dp0
= getclipdist(clipper
, v0
, plane_idx
);
600 const float dp1
= getclipdist(clipper
, v1
, plane_idx
);
602 if (util_is_inf_or_nan(dp0
) || util_is_inf_or_nan(dp1
))
603 return; //discard nan
606 float t
= dp1
/ (dp1
- dp0
);
611 float t
= dp0
/ (dp0
- dp1
);
616 return; /* discard */
618 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
622 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
, viewport_index
);
623 if (stage
->draw
->rasterizer
->flatshade_first
) {
624 copy_flat(stage
, stage
->tmp
[0], v0
); /* copy v0 color to tmp[0] */
627 copy_flat(stage
, stage
->tmp
[0], v1
); /* copy v1 color to tmp[0] */
629 newprim
.v
[0] = stage
->tmp
[0];
636 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
, viewport_index
);
637 if (stage
->draw
->rasterizer
->flatshade_first
) {
638 copy_flat(stage
, stage
->tmp
[1], v0
); /* copy v0 color to tmp[1] */
641 copy_flat(stage
, stage
->tmp
[1], v1
); /* copy v1 color to tmp[1] */
643 newprim
.v
[1] = stage
->tmp
[1];
649 stage
->next
->line( stage
->next
, &newprim
);
654 clip_point(struct draw_stage
*stage
, struct prim_header
*header
)
656 if (header
->v
[0]->clipmask
== 0)
657 stage
->next
->point( stage
->next
, header
);
662 * Clip points but ignore the first 4 (xy) clip planes.
663 * (Because the generated clip mask is completely unaffacted by guard band,
664 * we still need to manually evaluate the x/y planes if they are outside
665 * the guard band and not just outside the vp.)
668 clip_point_guard_xy(struct draw_stage
*stage
, struct prim_header
*header
)
670 unsigned clipmask
= header
->v
[0]->clipmask
;
671 if ((clipmask
& 0xffffffff) == 0)
672 stage
->next
->point(stage
->next
, header
);
673 else if ((clipmask
& 0xfffffff0) == 0) {
675 const unsigned plane_idx
= ffs(clipmask
)-1;
676 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
677 /* TODO: this should really do proper guardband clipping,
678 * currently just throw out infs/nans.
679 * Also note that vertices with negative w values MUST be tossed
680 * out (not sure if proper guardband clipping would do this
681 * automatically). These would usually be captured by depth clip
682 * too but this can be disabled.
684 if (header
->v
[0]->clip_pos
[3] <= 0.0f
||
685 util_is_inf_or_nan(header
->v
[0]->clip_pos
[0]) ||
686 util_is_inf_or_nan(header
->v
[0]->clip_pos
[1]))
689 stage
->next
->point(stage
->next
, header
);
695 clip_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
697 stage
->point
= stage
->draw
->guard_band_points_xy
? clip_point_guard_xy
: clip_point
;
698 stage
->point(stage
, header
);
703 clip_line(struct draw_stage
*stage
, struct prim_header
*header
)
705 unsigned clipmask
= (header
->v
[0]->clipmask
|
706 header
->v
[1]->clipmask
);
709 /* no clipping needed */
710 stage
->next
->line( stage
->next
, header
);
712 else if ((header
->v
[0]->clipmask
&
713 header
->v
[1]->clipmask
) == 0) {
714 do_clip_line(stage
, header
, clipmask
);
716 /* else, totally clipped */
721 clip_tri(struct draw_stage
*stage
, struct prim_header
*header
)
723 unsigned clipmask
= (header
->v
[0]->clipmask
|
724 header
->v
[1]->clipmask
|
725 header
->v
[2]->clipmask
);
728 /* no clipping needed */
729 stage
->next
->tri( stage
->next
, header
);
731 else if ((header
->v
[0]->clipmask
&
732 header
->v
[1]->clipmask
&
733 header
->v
[2]->clipmask
) == 0) {
734 do_clip_tri(stage
, header
, clipmask
);
740 find_interp(const struct draw_fragment_shader
*fs
, int *indexed_interp
,
741 uint semantic_name
, uint semantic_index
)
744 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
745 * from the array we've filled before. */
746 if (semantic_name
== TGSI_SEMANTIC_COLOR
||
747 semantic_name
== TGSI_SEMANTIC_BCOLOR
) {
748 interp
= indexed_interp
[semantic_index
];
749 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
||
750 semantic_name
== TGSI_SEMANTIC_CLIPVERTEX
) {
751 /* these inputs are handled specially always */
754 /* Otherwise, search in the FS inputs, with a decent default
755 * if we don't find it.
756 * This probably only matters for layer, vpindex, culldist, maybe
760 if (semantic_name
== TGSI_SEMANTIC_LAYER
||
761 semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
762 interp
= TGSI_INTERPOLATE_CONSTANT
;
765 interp
= TGSI_INTERPOLATE_PERSPECTIVE
;
768 for (j
= 0; j
< fs
->info
.num_inputs
; j
++) {
769 if (semantic_name
== fs
->info
.input_semantic_name
[j
] &&
770 semantic_index
== fs
->info
.input_semantic_index
[j
]) {
771 interp
= fs
->info
.input_interpolate
[j
];
780 /* Update state. Could further delay this until we hit the first
781 * primitive that really requires clipping.
784 clip_init_state(struct draw_stage
*stage
)
786 struct clip_stage
*clipper
= clip_stage(stage
);
787 const struct draw_context
*draw
= stage
->draw
;
788 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
789 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
791 int indexed_interp
[2];
793 clipper
->pos_attr
= draw_current_shader_position_output(draw
);
794 clipper
->have_clipdist
= draw_current_shader_num_written_clipdistances(draw
) > 0;
796 /* We need to know for each attribute what kind of interpolation is
797 * done on it (flat, smooth or noperspective). But the information
798 * is not directly accessible for outputs, only for inputs. So we
799 * have to match semantic name and index between the VS (or GS/ES)
800 * outputs and the FS inputs to get to the interpolation mode.
802 * The only hitch is with gl_FrontColor/gl_BackColor which map to
803 * gl_Color, and their Secondary versions. First there are (up to)
804 * two outputs for one input, so we tuck the information in a
805 * specific array. Second if they don't have qualifiers, the
806 * default value has to be picked from the global shade mode.
808 * Of course, if we don't have a fragment shader in the first
809 * place, defaults should be used.
812 /* First pick up the interpolation mode for
813 * gl_Color/gl_SecondaryColor, with the correct default.
815 indexed_interp
[0] = indexed_interp
[1] = draw
->rasterizer
->flatshade
?
816 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
819 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
820 if (fs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
) {
821 if (fs
->info
.input_interpolate
[i
] != TGSI_INTERPOLATE_COLOR
)
822 indexed_interp
[fs
->info
.input_semantic_index
[i
]] = fs
->info
.input_interpolate
[i
];
827 /* Then resolve the interpolation mode for every output attribute. */
829 clipper
->num_const_attribs
= 0;
830 clipper
->num_linear_attribs
= 0;
831 clipper
->num_perspect_attribs
= 0;
832 for (i
= 0; i
< info
->num_outputs
; i
++) {
833 /* Find the interpolation mode for a specific attribute */
834 int interp
= find_interp(fs
, indexed_interp
,
835 info
->output_semantic_name
[i
],
836 info
->output_semantic_index
[i
]);
838 case TGSI_INTERPOLATE_CONSTANT
:
839 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
;
840 clipper
->num_const_attribs
++;
842 case TGSI_INTERPOLATE_LINEAR
:
843 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
;
844 clipper
->num_linear_attribs
++;
846 case TGSI_INTERPOLATE_PERSPECTIVE
:
847 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
;
848 clipper
->num_perspect_attribs
++;
851 assert(interp
== -1);
855 /* Search the extra vertex attributes */
856 for (j
= 0; j
< draw
->extra_shader_outputs
.num
; j
++) {
857 /* Find the interpolation mode for a specific attribute */
858 int interp
= find_interp(fs
, indexed_interp
,
859 draw
->extra_shader_outputs
.semantic_name
[j
],
860 draw
->extra_shader_outputs
.semantic_index
[j
]);
862 case TGSI_INTERPOLATE_CONSTANT
:
863 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
+ j
;
864 clipper
->num_const_attribs
++;
866 case TGSI_INTERPOLATE_LINEAR
:
867 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
+ j
;
868 clipper
->num_linear_attribs
++;
870 case TGSI_INTERPOLATE_PERSPECTIVE
:
871 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
+ j
;
872 clipper
->num_perspect_attribs
++;
875 assert(interp
== -1);
880 stage
->tri
= clip_tri
;
881 stage
->line
= clip_line
;
886 static void clip_first_tri(struct draw_stage
*stage
,
887 struct prim_header
*header
)
889 clip_init_state( stage
);
890 stage
->tri( stage
, header
);
893 static void clip_first_line(struct draw_stage
*stage
,
894 struct prim_header
*header
)
896 clip_init_state( stage
);
897 stage
->line( stage
, header
);
901 static void clip_flush(struct draw_stage
*stage
, unsigned flags
)
903 stage
->tri
= clip_first_tri
;
904 stage
->line
= clip_first_line
;
905 stage
->next
->flush( stage
->next
, flags
);
909 static void clip_reset_stipple_counter(struct draw_stage
*stage
)
911 stage
->next
->reset_stipple_counter( stage
->next
);
915 static void clip_destroy(struct draw_stage
*stage
)
917 draw_free_temp_verts( stage
);
923 * Allocate a new clipper stage.
924 * \return pointer to new stage object
926 struct draw_stage
*draw_clip_stage(struct draw_context
*draw
)
928 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
932 clipper
->stage
.draw
= draw
;
933 clipper
->stage
.name
= "clipper";
934 clipper
->stage
.point
= clip_first_point
;
935 clipper
->stage
.line
= clip_first_line
;
936 clipper
->stage
.tri
= clip_first_tri
;
937 clipper
->stage
.flush
= clip_flush
;
938 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
939 clipper
->stage
.destroy
= clip_destroy
;
941 clipper
->plane
= draw
->plane
;
943 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
946 return &clipper
->stage
;
950 clipper
->stage
.destroy( &clipper
->stage
);