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 */
63 /* List of the attributes to be constant interpolated. */
64 uint num_const_attribs
;
65 uint8_t const_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
66 /* List of the attributes to be linear interpolated. */
67 uint num_linear_attribs
;
68 uint8_t linear_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
69 /* List of the attributes to be perspective interpolated. */
70 uint num_perspect_attribs
;
71 uint8_t perspect_attribs
[PIPE_MAX_SHADER_OUTPUTS
];
78 static inline struct clip_stage
*clip_stage(struct draw_stage
*stage
)
80 return (struct clip_stage
*)stage
;
83 static inline unsigned
84 draw_viewport_index(struct draw_context
*draw
,
85 const struct vertex_header
*leading_vertex
)
87 if (draw_current_shader_uses_viewport_index(draw
)) {
88 unsigned viewport_index_output
=
89 draw_current_shader_viewport_index_output(draw
);
90 unsigned viewport_index
=
91 *((unsigned*)leading_vertex
->data
[viewport_index_output
]);
92 return draw_clamp_viewport_idx(viewport_index
);
99 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
102 /* All attributes are float[4], so this is easy:
104 static void interp_attr(float dst
[4],
109 dst
[0] = LINTERP( t
, out
[0], in
[0] );
110 dst
[1] = LINTERP( t
, out
[1], in
[1] );
111 dst
[2] = LINTERP( t
, out
[2], in
[2] );
112 dst
[3] = LINTERP( t
, out
[3], in
[3] );
117 * Copy flat shaded attributes src vertex to dst vertex.
119 static void copy_flat(struct draw_stage
*stage
,
120 struct vertex_header
*dst
,
121 const struct vertex_header
*src
)
123 const struct clip_stage
*clipper
= clip_stage(stage
);
125 for (i
= 0; i
< clipper
->num_const_attribs
; i
++) {
126 const uint attr
= clipper
->const_attribs
[i
];
127 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
131 /* Interpolate between two vertices to produce a third.
133 static void interp(const struct clip_stage
*clip
,
134 struct vertex_header
*dst
,
136 const struct vertex_header
*out
,
137 const struct vertex_header
*in
,
138 unsigned viewport_index
)
140 const unsigned pos_attr
= clip
->pos_attr
;
147 dst
->edgeflag
= 0; /* will get overwritten later */
148 dst
->have_clipdist
= in
->have_clipdist
;
149 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
151 /* Interpolate the clip-space coords.
153 interp_attr(dst
->clip
, t
, in
->clip
, out
->clip
);
154 /* interpolate the clip-space position */
155 interp_attr(dst
->pre_clip_pos
, t
, in
->pre_clip_pos
, out
->pre_clip_pos
);
157 /* Do the projective divide and viewport transformation to get
158 * new window coordinates:
161 const float *pos
= dst
->pre_clip_pos
;
163 clip
->stage
.draw
->viewports
[viewport_index
].scale
;
165 clip
->stage
.draw
->viewports
[viewport_index
].translate
;
166 const float oow
= 1.0f
/ pos
[3];
168 dst
->data
[pos_attr
][0] = pos
[0] * oow
* scale
[0] + trans
[0];
169 dst
->data
[pos_attr
][1] = pos
[1] * oow
* scale
[1] + trans
[1];
170 dst
->data
[pos_attr
][2] = pos
[2] * oow
* scale
[2] + trans
[2];
171 dst
->data
[pos_attr
][3] = oow
;
175 /* interp perspective attribs */
176 for (j
= 0; j
< clip
->num_perspect_attribs
; j
++) {
177 const unsigned attr
= clip
->perspect_attribs
[j
];
178 interp_attr(dst
->data
[attr
], t
, in
->data
[attr
], out
->data
[attr
]);
182 * Compute the t in screen-space instead of 3d space to use
183 * for noperspective interpolation.
185 * The points can be aligned with the X axis, so in that case try
186 * the Y. When both points are at the same screen position, we can
187 * pick whatever value (the interpolated point won't be in front
188 * anyway), so just use the 3d t.
190 if (clip
->num_linear_attribs
) {
193 /* find either in.x != out.x or in.y != out.y */
194 for (k
= 0; k
< 2; k
++) {
195 if (in
->pre_clip_pos
[k
] != out
->pre_clip_pos
[k
]) {
196 /* do divide by W, then compute linear interpolation factor */
197 float in_coord
= in
->pre_clip_pos
[k
] / in
->pre_clip_pos
[3];
198 float out_coord
= out
->pre_clip_pos
[k
] / out
->pre_clip_pos
[3];
199 float dst_coord
= dst
->pre_clip_pos
[k
] / dst
->pre_clip_pos
[3];
200 t_nopersp
= (dst_coord
- out_coord
) / (in_coord
- out_coord
);
204 for (j
= 0; j
< clip
->num_linear_attribs
; j
++) {
205 const unsigned attr
= clip
->linear_attribs
[j
];
206 interp_attr(dst
->data
[attr
], t_nopersp
, in
->data
[attr
], out
->data
[attr
]);
212 * Checks whether the specified triangle is empty and if it is returns
213 * true, otherwise returns false.
214 * Triangle is considered null/empty if its area is equal to zero.
216 static inline boolean
217 is_tri_null(struct draw_context
*draw
, const struct prim_header
*header
)
219 const unsigned pos_attr
= draw_current_shader_position_output(draw
);
220 float x1
= header
->v
[1]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
221 float y1
= header
->v
[1]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
222 float z1
= header
->v
[1]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
224 float x2
= header
->v
[2]->data
[pos_attr
][0] - header
->v
[0]->data
[pos_attr
][0];
225 float y2
= header
->v
[2]->data
[pos_attr
][1] - header
->v
[0]->data
[pos_attr
][1];
226 float z2
= header
->v
[2]->data
[pos_attr
][2] - header
->v
[0]->data
[pos_attr
][2];
228 float vx
= y1
* z2
- z1
* y2
;
229 float vy
= x1
* z2
- z1
* x2
;
230 float vz
= x1
* y2
- y1
* x2
;
232 return (vx
*vx
+ vy
*vy
+ vz
*vz
) == 0.f
;
236 * Emit a post-clip polygon to the next pipeline stage. The polygon
237 * will be convex and the provoking vertex will always be vertex[0].
239 static void emit_poly(struct draw_stage
*stage
,
240 struct vertex_header
**inlist
,
241 const boolean
*edgeflags
,
243 const struct prim_header
*origPrim
)
245 struct prim_header header
;
247 ushort edge_first
, edge_middle
, edge_last
;
248 boolean last_tri_was_null
= FALSE
;
249 boolean tri_was_not_null
= FALSE
;
251 if (stage
->draw
->rasterizer
->flatshade_first
) {
252 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
253 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
254 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
257 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
258 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
259 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
265 /* later stages may need the determinant, but only the sign matters */
266 header
.det
= origPrim
->det
;
267 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
270 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
272 /* order the triangle verts to respect the provoking vertex mode */
273 if (stage
->draw
->rasterizer
->flatshade_first
) {
274 header
.v
[0] = inlist
[0]; /* the provoking vertex */
275 header
.v
[1] = inlist
[i
-1];
276 header
.v
[2] = inlist
[i
];
279 header
.v
[0] = inlist
[i
-1];
280 header
.v
[1] = inlist
[i
];
281 header
.v
[2] = inlist
[0]; /* the provoking vertex */
284 tri_null
= is_tri_null(stage
->draw
, &header
);
285 /* If we generated a triangle with an area, aka. non-null triangle,
286 * or if the previous triangle was also null then skip all subsequent
288 if ((tri_was_not_null
&& tri_null
) || (last_tri_was_null
&& tri_null
)) {
289 last_tri_was_null
= tri_null
;
292 last_tri_was_null
= tri_null
;
294 tri_was_not_null
= TRUE
;
297 if (!edgeflags
[i
-1]) {
298 header
.flags
&= ~edge_middle
;
301 if (i
== n
- 1 && edgeflags
[i
])
302 header
.flags
|= edge_last
;
306 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
307 stage
->draw
->rasterizer
->flatshade_first
);
308 for (j
= 0; j
< 3; j
++) {
309 debug_printf(" Vert %d: clip: %f %f %f %f\n", j
,
310 header
.v
[j
]->clip
[0],
311 header
.v
[j
]->clip
[1],
312 header
.v
[j
]->clip
[2],
313 header
.v
[j
]->clip
[3]);
314 for (k
= 0; k
< draw_num_shader_outputs(stage
->draw
); k
++) {
315 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
316 header
.v
[j
]->data
[k
][0],
317 header
.v
[j
]->data
[k
][1],
318 header
.v
[j
]->data
[k
][2],
319 header
.v
[j
]->data
[k
][3]);
323 stage
->next
->tri( stage
->next
, &header
);
329 dot4(const float *a
, const float *b
)
331 return (a
[0] * b
[0] +
338 * this function extracts the clip distance for the current plane,
339 * it first checks if the shader provided a clip distance, otherwise
340 * it works out the value using the clipvertex
342 static inline float getclipdist(const struct clip_stage
*clipper
,
343 struct vertex_header
*vert
,
348 if (vert
->have_clipdist
&& plane_idx
>= 6) {
349 /* pick the correct clipdistance element from the output vectors */
350 int _idx
= plane_idx
- 6;
352 int vidx
= cdi
? _idx
- 4 : _idx
;
353 dp
= vert
->data
[draw_current_shader_clipdistance_output(clipper
->stage
.draw
, cdi
)][vidx
];
355 plane
= clipper
->plane
[plane_idx
];
356 dp
= dot4(vert
->clip
, plane
);
361 /* Clip a triangle against the viewport and user clip planes.
364 do_clip_tri(struct draw_stage
*stage
,
365 struct prim_header
*header
,
368 struct clip_stage
*clipper
= clip_stage( stage
);
369 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
370 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
371 struct vertex_header
**inlist
= a
;
372 struct vertex_header
**outlist
= b
;
373 struct vertex_header
*prov_vertex
;
377 boolean aEdges
[MAX_CLIPPED_VERTICES
];
378 boolean bEdges
[MAX_CLIPPED_VERTICES
];
379 boolean
*inEdges
= aEdges
;
380 boolean
*outEdges
= bEdges
;
381 int viewport_index
= 0;
383 inlist
[0] = header
->v
[0];
384 inlist
[1] = header
->v
[1];
385 inlist
[2] = header
->v
[2];
388 * For d3d10, we need to take this from the leading (first) vertex.
389 * For GL, we could do anything (as long as we advertize
390 * GL_UNDEFINED_VERTEX for the VIEWPORT_INDEX_PROVOKING_VERTEX query),
391 * but it needs to be consistent with what other parts (i.e. driver)
392 * will do, and that seems easier with GL_PROVOKING_VERTEX logic.
394 if (stage
->draw
->rasterizer
->flatshade_first
) {
395 prov_vertex
= inlist
[0];
398 prov_vertex
= inlist
[2];
400 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
403 const float *v0
= header
->v
[0]->clip
;
404 const float *v1
= header
->v
[1]->clip
;
405 const float *v2
= header
->v
[2]->clip
;
406 debug_printf("Clip triangle:\n");
407 debug_printf(" %f, %f, %f, %f\n", v0
[0], v0
[1], v0
[2], v0
[3]);
408 debug_printf(" %f, %f, %f, %f\n", v1
[0], v1
[1], v1
[2], v1
[3]);
409 debug_printf(" %f, %f, %f, %f\n", v2
[0], v2
[1], v2
[2], v2
[3]);
413 * Note: at this point we can't just use the per-vertex edge flags.
414 * We have to observe the edge flag bits set in header->flags which
415 * were set during primitive decomposition. Put those flags into
416 * an edge flags array which parallels the vertex array.
417 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
418 * the header.flags bit is set AND the per-vertex edgeflag field is set.
420 inEdges
[0] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_0
);
421 inEdges
[1] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_1
);
422 inEdges
[2] = !!(header
->flags
& DRAW_PIPE_EDGE_FLAG_2
);
424 while (clipmask
&& n
>= 3) {
425 const unsigned plane_idx
= ffs(clipmask
)-1;
426 const boolean is_user_clip_plane
= plane_idx
>= 6;
427 struct vertex_header
*vert_prev
= inlist
[0];
428 boolean
*edge_prev
= &inEdges
[0];
430 unsigned outcount
= 0;
432 dp_prev
= getclipdist(clipper
, vert_prev
, plane_idx
);
433 clipmask
&= ~(1<<plane_idx
);
435 if (util_is_inf_or_nan(dp_prev
))
436 return; //discard nan
438 assert(n
< MAX_CLIPPED_VERTICES
);
439 if (n
>= MAX_CLIPPED_VERTICES
)
441 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
442 inEdges
[n
] = inEdges
[0];
444 for (i
= 1; i
<= n
; i
++) {
445 struct vertex_header
*vert
= inlist
[i
];
446 boolean
*edge
= &inEdges
[i
];
448 float dp
= getclipdist(clipper
, vert
, plane_idx
);
450 if (util_is_inf_or_nan(dp
))
451 return; //discard nan
453 if (dp_prev
>= 0.0f
) {
454 assert(outcount
< MAX_CLIPPED_VERTICES
);
455 if (outcount
>= MAX_CLIPPED_VERTICES
)
457 outEdges
[outcount
] = *edge_prev
;
458 outlist
[outcount
++] = vert_prev
;
461 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
462 struct vertex_header
*new_vert
;
465 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
466 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
468 new_vert
= clipper
->stage
.tmp
[tmpnr
++];
470 assert(outcount
< MAX_CLIPPED_VERTICES
);
471 if (outcount
>= MAX_CLIPPED_VERTICES
)
474 new_edge
= &outEdges
[outcount
];
475 outlist
[outcount
++] = new_vert
;
478 /* Going out of bounds. Avoid division by zero as we
479 * know dp != dp_prev from DIFFERENT_SIGNS, above.
481 float t
= dp
/ (dp
- dp_prev
);
482 interp( clipper
, new_vert
, t
, vert
, vert_prev
, viewport_index
);
484 /* Whether or not to set edge flag for the new vert depends
485 * on whether it's a user-defined clipping plane. We're
486 * copying NVIDIA's behaviour here.
488 if (is_user_clip_plane
) {
489 /* we want to see an edge along the clip plane */
491 new_vert
->edgeflag
= TRUE
;
494 /* we don't want to see an edge along the frustum clip plane */
495 *new_edge
= *edge_prev
;
496 new_vert
->edgeflag
= FALSE
;
502 float t
= dp_prev
/ (dp_prev
- dp
);
503 interp( clipper
, new_vert
, t
, vert_prev
, vert
, viewport_index
);
505 /* Copy starting vert's edgeflag:
507 new_vert
->edgeflag
= vert_prev
->edgeflag
;
508 *new_edge
= *edge_prev
;
517 /* swap in/out lists */
519 struct vertex_header
**tmp
= inlist
;
525 boolean
*tmp
= inEdges
;
532 /* If constant interpolated, copy provoking vertex attrib to polygon vertex[0]
535 if (clipper
->num_const_attribs
) {
536 if (stage
->draw
->rasterizer
->flatshade_first
) {
537 if (inlist
[0] != header
->v
[0]) {
538 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
539 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
541 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
542 copy_flat(stage
, inlist
[0], header
->v
[0]);
546 if (inlist
[0] != header
->v
[2]) {
547 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
548 if (tmpnr
>= MAX_CLIPPED_VERTICES
+ 1)
550 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
551 copy_flat(stage
, inlist
[0], header
->v
[2]);
556 /* Emit the polygon as triangles to the setup stage:
558 emit_poly( stage
, inlist
, inEdges
, n
, header
);
563 /* Clip a line against the viewport and user clip planes.
566 do_clip_line(struct draw_stage
*stage
,
567 struct prim_header
*header
,
570 const struct clip_stage
*clipper
= clip_stage( stage
);
571 struct vertex_header
*v0
= header
->v
[0];
572 struct vertex_header
*v1
= header
->v
[1];
573 struct vertex_header
*prov_vertex
;
576 struct prim_header newprim
;
579 if (stage
->draw
->rasterizer
->flatshade_first
) {
585 viewport_index
= draw_viewport_index(clipper
->stage
.draw
, prov_vertex
);
588 const unsigned plane_idx
= ffs(clipmask
)-1;
589 const float dp0
= getclipdist(clipper
, v0
, plane_idx
);
590 const float dp1
= getclipdist(clipper
, v1
, plane_idx
);
592 if (util_is_inf_or_nan(dp0
) || util_is_inf_or_nan(dp1
))
593 return; //discard nan
596 float t
= dp1
/ (dp1
- dp0
);
601 float t
= dp0
/ (dp0
- dp1
);
606 return; /* discard */
608 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
612 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
, viewport_index
);
613 if (stage
->draw
->rasterizer
->flatshade_first
) {
614 copy_flat(stage
, stage
->tmp
[0], v0
); /* copy v0 color to tmp[0] */
617 copy_flat(stage
, stage
->tmp
[0], v1
); /* copy v1 color to tmp[0] */
619 newprim
.v
[0] = stage
->tmp
[0];
626 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
, viewport_index
);
627 if (stage
->draw
->rasterizer
->flatshade_first
) {
628 copy_flat(stage
, stage
->tmp
[1], v0
); /* copy v0 color to tmp[1] */
631 copy_flat(stage
, stage
->tmp
[1], v1
); /* copy v1 color to tmp[1] */
633 newprim
.v
[1] = stage
->tmp
[1];
639 stage
->next
->line( stage
->next
, &newprim
);
644 clip_point(struct draw_stage
*stage
, struct prim_header
*header
)
646 if (header
->v
[0]->clipmask
== 0)
647 stage
->next
->point( stage
->next
, header
);
652 * Clip points but ignore the first 4 (xy) clip planes.
653 * (Because the generated clip mask is completely unaffacted by guard band,
654 * we still need to manually evaluate the x/y planes if they are outside
655 * the guard band and not just outside the vp.)
658 clip_point_guard_xy(struct draw_stage
*stage
, struct prim_header
*header
)
660 unsigned clipmask
= header
->v
[0]->clipmask
;
661 if ((clipmask
& 0xffffffff) == 0)
662 stage
->next
->point(stage
->next
, header
);
663 else if ((clipmask
& 0xfffffff0) == 0) {
665 const unsigned plane_idx
= ffs(clipmask
)-1;
666 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
667 /* TODO: this should really do proper guardband clipping,
668 * currently just throw out infs/nans.
669 * Also note that vertices with negative w values MUST be tossed
670 * out (not sure if proper guardband clipping would do this
671 * automatically). These would usually be captured by depth clip
672 * too but this can be disabled.
674 if (header
->v
[0]->clip
[3] <= 0.0f
||
675 util_is_inf_or_nan(header
->v
[0]->clip
[0]) ||
676 util_is_inf_or_nan(header
->v
[0]->clip
[1]))
679 stage
->next
->point(stage
->next
, header
);
685 clip_first_point(struct draw_stage
*stage
, struct prim_header
*header
)
687 stage
->point
= stage
->draw
->guard_band_points_xy
? clip_point_guard_xy
: clip_point
;
688 stage
->point(stage
, header
);
693 clip_line(struct draw_stage
*stage
, struct prim_header
*header
)
695 unsigned clipmask
= (header
->v
[0]->clipmask
|
696 header
->v
[1]->clipmask
);
699 /* no clipping needed */
700 stage
->next
->line( stage
->next
, header
);
702 else if ((header
->v
[0]->clipmask
&
703 header
->v
[1]->clipmask
) == 0) {
704 do_clip_line(stage
, header
, clipmask
);
706 /* else, totally clipped */
711 clip_tri(struct draw_stage
*stage
, struct prim_header
*header
)
713 unsigned clipmask
= (header
->v
[0]->clipmask
|
714 header
->v
[1]->clipmask
|
715 header
->v
[2]->clipmask
);
718 /* no clipping needed */
719 stage
->next
->tri( stage
->next
, header
);
721 else if ((header
->v
[0]->clipmask
&
722 header
->v
[1]->clipmask
&
723 header
->v
[2]->clipmask
) == 0) {
724 do_clip_tri(stage
, header
, clipmask
);
730 find_interp(const struct draw_fragment_shader
*fs
, int *indexed_interp
,
731 uint semantic_name
, uint semantic_index
)
734 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
735 * from the array we've filled before. */
736 if (semantic_name
== TGSI_SEMANTIC_COLOR
||
737 semantic_name
== TGSI_SEMANTIC_BCOLOR
) {
738 interp
= indexed_interp
[semantic_index
];
739 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
||
740 semantic_name
== TGSI_SEMANTIC_CLIPVERTEX
) {
741 /* these inputs are handled specially always */
744 /* Otherwise, search in the FS inputs, with a decent default
745 * if we don't find it.
746 * This probably only matters for layer, vpindex, culldist, maybe
750 if (semantic_name
== TGSI_SEMANTIC_LAYER
||
751 semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) {
752 interp
= TGSI_INTERPOLATE_CONSTANT
;
755 interp
= TGSI_INTERPOLATE_PERSPECTIVE
;
758 for (j
= 0; j
< fs
->info
.num_inputs
; j
++) {
759 if (semantic_name
== fs
->info
.input_semantic_name
[j
] &&
760 semantic_index
== fs
->info
.input_semantic_index
[j
]) {
761 interp
= fs
->info
.input_interpolate
[j
];
770 /* Update state. Could further delay this until we hit the first
771 * primitive that really requires clipping.
774 clip_init_state(struct draw_stage
*stage
)
776 struct clip_stage
*clipper
= clip_stage( stage
);
777 const struct draw_context
*draw
= stage
->draw
;
778 const struct draw_fragment_shader
*fs
= draw
->fs
.fragment_shader
;
779 const struct tgsi_shader_info
*info
= draw_get_shader_info(draw
);
781 int indexed_interp
[2];
783 clipper
->pos_attr
= draw_current_shader_position_output(draw
);
785 /* We need to know for each attribute what kind of interpolation is
786 * done on it (flat, smooth or noperspective). But the information
787 * is not directly accessible for outputs, only for inputs. So we
788 * have to match semantic name and index between the VS (or GS/ES)
789 * outputs and the FS inputs to get to the interpolation mode.
791 * The only hitch is with gl_FrontColor/gl_BackColor which map to
792 * gl_Color, and their Secondary versions. First there are (up to)
793 * two outputs for one input, so we tuck the information in a
794 * specific array. Second if they don't have qualifiers, the
795 * default value has to be picked from the global shade mode.
797 * Of course, if we don't have a fragment shader in the first
798 * place, defaults should be used.
801 /* First pick up the interpolation mode for
802 * gl_Color/gl_SecondaryColor, with the correct default.
804 indexed_interp
[0] = indexed_interp
[1] = draw
->rasterizer
->flatshade
?
805 TGSI_INTERPOLATE_CONSTANT
: TGSI_INTERPOLATE_PERSPECTIVE
;
808 for (i
= 0; i
< fs
->info
.num_inputs
; i
++) {
809 if (fs
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
) {
810 if (fs
->info
.input_interpolate
[i
] != TGSI_INTERPOLATE_COLOR
)
811 indexed_interp
[fs
->info
.input_semantic_index
[i
]] = fs
->info
.input_interpolate
[i
];
816 /* Then resolve the interpolation mode for every output attribute. */
818 clipper
->num_const_attribs
= 0;
819 clipper
->num_linear_attribs
= 0;
820 clipper
->num_perspect_attribs
= 0;
821 for (i
= 0; i
< info
->num_outputs
; i
++) {
822 /* Find the interpolation mode for a specific attribute */
823 int interp
= find_interp(fs
, indexed_interp
,
824 info
->output_semantic_name
[i
],
825 info
->output_semantic_index
[i
]);
827 case TGSI_INTERPOLATE_CONSTANT
:
828 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
;
829 clipper
->num_const_attribs
++;
831 case TGSI_INTERPOLATE_LINEAR
:
832 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
;
833 clipper
->num_linear_attribs
++;
835 case TGSI_INTERPOLATE_PERSPECTIVE
:
836 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
;
837 clipper
->num_perspect_attribs
++;
840 assert(interp
== -1);
844 /* Search the extra vertex attributes */
845 for (j
= 0; j
< draw
->extra_shader_outputs
.num
; j
++) {
846 /* Find the interpolation mode for a specific attribute */
847 int interp
= find_interp(fs
, indexed_interp
,
848 draw
->extra_shader_outputs
.semantic_name
[j
],
849 draw
->extra_shader_outputs
.semantic_index
[j
]);
851 case TGSI_INTERPOLATE_CONSTANT
:
852 clipper
->const_attribs
[clipper
->num_const_attribs
] = i
+ j
;
853 clipper
->num_const_attribs
++;
855 case TGSI_INTERPOLATE_LINEAR
:
856 clipper
->linear_attribs
[clipper
->num_linear_attribs
] = i
+ j
;
857 clipper
->num_linear_attribs
++;
859 case TGSI_INTERPOLATE_PERSPECTIVE
:
860 clipper
->perspect_attribs
[clipper
->num_perspect_attribs
] = i
+ j
;
861 clipper
->num_perspect_attribs
++;
864 assert(interp
== -1);
869 stage
->tri
= clip_tri
;
870 stage
->line
= clip_line
;
875 static void clip_first_tri(struct draw_stage
*stage
,
876 struct prim_header
*header
)
878 clip_init_state( stage
);
879 stage
->tri( stage
, header
);
882 static void clip_first_line(struct draw_stage
*stage
,
883 struct prim_header
*header
)
885 clip_init_state( stage
);
886 stage
->line( stage
, header
);
890 static void clip_flush(struct draw_stage
*stage
, unsigned flags
)
892 stage
->tri
= clip_first_tri
;
893 stage
->line
= clip_first_line
;
894 stage
->next
->flush( stage
->next
, flags
);
898 static void clip_reset_stipple_counter(struct draw_stage
*stage
)
900 stage
->next
->reset_stipple_counter( stage
->next
);
904 static void clip_destroy(struct draw_stage
*stage
)
906 draw_free_temp_verts( stage
);
912 * Allocate a new clipper stage.
913 * \return pointer to new stage object
915 struct draw_stage
*draw_clip_stage(struct draw_context
*draw
)
917 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
921 clipper
->stage
.draw
= draw
;
922 clipper
->stage
.name
= "clipper";
923 clipper
->stage
.point
= clip_first_point
;
924 clipper
->stage
.line
= clip_first_line
;
925 clipper
->stage
.tri
= clip_first_tri
;
926 clipper
->stage
.flush
= clip_flush
;
927 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
928 clipper
->stage
.destroy
= clip_destroy
;
930 clipper
->plane
= draw
->plane
;
932 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
935 return &clipper
->stage
;
939 clipper
->stage
.destroy( &clipper
->stage
);