1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * \brief Clipping stage
31 * \author Keith Whitwell <keith@tungstengraphics.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"
45 #define IS_NEGATIVE(X) ((X) < 0.0)
48 #ifndef DIFFERENT_SIGNS
49 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
52 #ifndef MAX_CLIPPED_VERTICES
53 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
59 struct draw_stage stage
; /**< base class */
61 /* Basically duplicate some of the flatshading logic here:
64 uint num_color_attribs
;
65 uint color_attribs
[4]; /* front/back primary/secondary colors */
72 static INLINE
struct clip_stage
*clip_stage( struct draw_stage
*stage
)
74 return (struct clip_stage
*)stage
;
78 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
81 /* All attributes are float[4], so this is easy:
83 static void interp_attr( float dst
[4],
88 dst
[0] = LINTERP( t
, out
[0], in
[0] );
89 dst
[1] = LINTERP( t
, out
[1], in
[1] );
90 dst
[2] = LINTERP( t
, out
[2], in
[2] );
91 dst
[3] = LINTERP( t
, out
[3], in
[3] );
96 * Copy front/back, primary/secondary colors from src vertex to dst vertex.
97 * Used when flat shading.
99 static void copy_colors( struct draw_stage
*stage
,
100 struct vertex_header
*dst
,
101 const struct vertex_header
*src
)
103 const struct clip_stage
*clipper
= clip_stage(stage
);
105 for (i
= 0; i
< clipper
->num_color_attribs
; i
++) {
106 const uint attr
= clipper
->color_attribs
[i
];
107 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
113 /* Interpolate between two vertices to produce a third.
115 static void interp( const struct clip_stage
*clip
,
116 struct vertex_header
*dst
,
118 const struct vertex_header
*out
,
119 const struct vertex_header
*in
)
121 const unsigned nr_attrs
= draw_current_shader_outputs(clip
->stage
.draw
);
122 const unsigned pos_attr
= draw_current_shader_position_output(clip
->stage
.draw
);
128 dst
->edgeflag
= 0; /* will get overwritten later */
130 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
132 /* Interpolate the clip-space coords.
134 interp_attr(dst
->clip
, t
, in
->clip
, out
->clip
);
136 /* Do the projective divide and viewport transformation to get
137 * new window coordinates:
140 const float *pos
= dst
->clip
;
141 const float *scale
= clip
->stage
.draw
->viewport
.scale
;
142 const float *trans
= clip
->stage
.draw
->viewport
.translate
;
143 const float oow
= 1.0f
/ pos
[3];
145 dst
->data
[pos_attr
][0] = pos
[0] * oow
* scale
[0] + trans
[0];
146 dst
->data
[pos_attr
][1] = pos
[1] * oow
* scale
[1] + trans
[1];
147 dst
->data
[pos_attr
][2] = pos
[2] * oow
* scale
[2] + trans
[2];
148 dst
->data
[pos_attr
][3] = oow
;
153 for (j
= 0; j
< nr_attrs
; j
++) {
155 interp_attr(dst
->data
[j
], t
, in
->data
[j
], out
->data
[j
]);
161 * Emit a post-clip polygon to the next pipeline stage. The polygon
162 * will be convex and the provoking vertex will always be vertex[0].
164 static void emit_poly( struct draw_stage
*stage
,
165 struct vertex_header
**inlist
,
167 const struct prim_header
*origPrim
)
169 struct prim_header header
;
171 ushort edge_first
, edge_middle
, edge_last
;
173 if (stage
->draw
->rasterizer
->flatshade_first
) {
174 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
175 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
176 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
179 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
180 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
181 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
184 /* later stages may need the determinant, but only the sign matters */
185 header
.det
= origPrim
->det
;
186 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
189 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
190 /* order the triangle verts to respect the provoking vertex mode */
191 if (stage
->draw
->rasterizer
->flatshade_first
) {
192 header
.v
[0] = inlist
[0]; /* the provoking vertex */
193 header
.v
[1] = inlist
[i
-1];
194 header
.v
[2] = inlist
[i
];
197 header
.v
[0] = inlist
[i
-1];
198 header
.v
[1] = inlist
[i
];
199 header
.v
[2] = inlist
[0]; /* the provoking vertex */
203 header
.flags
|= edge_last
;
206 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
208 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
209 stage
->draw
->rasterizer
->flatshade_first
);
210 for (j
= 0; j
< 3; j
++) {
211 for (k
= 0; k
< vs
->info
.num_outputs
; k
++) {
212 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
213 header
.v
[j
]->data
[k
][0],
214 header
.v
[j
]->data
[k
][1],
215 header
.v
[j
]->data
[k
][2],
216 header
.v
[j
]->data
[k
][3]);
221 stage
->next
->tri( stage
->next
, &header
);
227 dot4(const float *a
, const float *b
)
229 return (a
[0] * b
[0] +
236 /* Clip a triangle against the viewport and user clip planes.
239 do_clip_tri( struct draw_stage
*stage
,
240 struct prim_header
*header
,
243 struct clip_stage
*clipper
= clip_stage( stage
);
244 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
245 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
246 struct vertex_header
**inlist
= a
;
247 struct vertex_header
**outlist
= b
;
252 inlist
[0] = header
->v
[0];
253 inlist
[1] = header
->v
[1];
254 inlist
[2] = header
->v
[2];
256 while (clipmask
&& n
>= 3) {
257 const unsigned plane_idx
= ffs(clipmask
)-1;
258 const float *plane
= clipper
->plane
[plane_idx
];
259 struct vertex_header
*vert_prev
= inlist
[0];
260 float dp_prev
= dot4( vert_prev
->clip
, plane
);
261 unsigned outcount
= 0;
263 clipmask
&= ~(1<<plane_idx
);
265 assert(n
< MAX_CLIPPED_VERTICES
);
266 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
268 for (i
= 1; i
<= n
; i
++) {
269 struct vertex_header
*vert
= inlist
[i
];
271 float dp
= dot4( vert
->clip
, plane
);
273 if (!IS_NEGATIVE(dp_prev
)) {
274 assert(outcount
< MAX_CLIPPED_VERTICES
);
275 outlist
[outcount
++] = vert_prev
;
278 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
279 struct vertex_header
*new_vert
;
281 assert(tmpnr
< MAX_CLIPPED_VERTICES
+1);
282 new_vert
= clipper
->stage
.tmp
[tmpnr
++];
284 assert(outcount
< MAX_CLIPPED_VERTICES
);
285 outlist
[outcount
++] = new_vert
;
287 if (IS_NEGATIVE(dp
)) {
288 /* Going out of bounds. Avoid division by zero as we
289 * know dp != dp_prev from DIFFERENT_SIGNS, above.
291 float t
= dp
/ (dp
- dp_prev
);
292 interp( clipper
, new_vert
, t
, vert
, vert_prev
);
294 /* Force edgeflag true in this case:
296 new_vert
->edgeflag
= 1;
300 float t
= dp_prev
/ (dp_prev
- dp
);
301 interp( clipper
, new_vert
, t
, vert_prev
, vert
);
303 /* Copy starting vert's edgeflag:
305 new_vert
->edgeflag
= vert_prev
->edgeflag
;
313 /* swap in/out lists */
315 struct vertex_header
**tmp
= inlist
;
322 /* If flat-shading, copy provoking vertex color to polygon vertex[0]
325 if (stage
->draw
->rasterizer
->flatshade_first
) {
326 if (inlist
[0] != header
->v
[0]) {
327 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
328 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
329 copy_colors(stage
, inlist
[0], header
->v
[0]);
333 if (inlist
[0] != header
->v
[2]) {
334 assert(tmpnr
< MAX_CLIPPED_VERTICES
+ 1);
335 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
336 copy_colors(stage
, inlist
[0], header
->v
[2]);
341 /* Emit the polygon as triangles to the setup stage:
344 emit_poly( stage
, inlist
, n
, header
);
348 /* Clip a line against the viewport and user clip planes.
351 do_clip_line( struct draw_stage
*stage
,
352 struct prim_header
*header
,
355 const struct clip_stage
*clipper
= clip_stage( stage
);
356 struct vertex_header
*v0
= header
->v
[0];
357 struct vertex_header
*v1
= header
->v
[1];
358 const float *pos0
= v0
->clip
;
359 const float *pos1
= v1
->clip
;
362 struct prim_header newprim
;
365 const unsigned plane_idx
= ffs(clipmask
)-1;
366 const float *plane
= clipper
->plane
[plane_idx
];
367 const float dp0
= dot4( pos0
, plane
);
368 const float dp1
= dot4( pos1
, plane
);
371 float t
= dp1
/ (dp1
- dp0
);
376 float t
= dp0
/ (dp0
- dp1
);
381 return; /* discard */
383 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
387 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
);
390 copy_colors(stage
, stage
->tmp
[0], v0
);
392 newprim
.v
[0] = stage
->tmp
[0];
399 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
);
400 newprim
.v
[1] = stage
->tmp
[1];
406 stage
->next
->line( stage
->next
, &newprim
);
411 clip_point( struct draw_stage
*stage
,
412 struct prim_header
*header
)
414 if (header
->v
[0]->clipmask
== 0)
415 stage
->next
->point( stage
->next
, header
);
420 clip_line( struct draw_stage
*stage
,
421 struct prim_header
*header
)
423 unsigned clipmask
= (header
->v
[0]->clipmask
|
424 header
->v
[1]->clipmask
);
427 /* no clipping needed */
428 stage
->next
->line( stage
->next
, header
);
430 else if ((header
->v
[0]->clipmask
&
431 header
->v
[1]->clipmask
) == 0) {
432 do_clip_line(stage
, header
, clipmask
);
434 /* else, totally clipped */
439 clip_tri( struct draw_stage
*stage
,
440 struct prim_header
*header
)
442 unsigned clipmask
= (header
->v
[0]->clipmask
|
443 header
->v
[1]->clipmask
|
444 header
->v
[2]->clipmask
);
447 /* no clipping needed */
448 stage
->next
->tri( stage
->next
, header
);
450 else if ((header
->v
[0]->clipmask
&
451 header
->v
[1]->clipmask
&
452 header
->v
[2]->clipmask
) == 0) {
453 do_clip_tri(stage
, header
, clipmask
);
458 /* Update state. Could further delay this until we hit the first
459 * primitive that really requires clipping.
462 clip_init_state( struct draw_stage
*stage
)
464 struct clip_stage
*clipper
= clip_stage( stage
);
466 clipper
->flat
= stage
->draw
->rasterizer
->flatshade
? TRUE
: FALSE
;
469 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
472 clipper
->num_color_attribs
= 0;
473 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
474 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
||
475 vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_BCOLOR
) {
476 clipper
->color_attribs
[clipper
->num_color_attribs
++] = i
;
481 stage
->tri
= clip_tri
;
482 stage
->line
= clip_line
;
487 static void clip_first_tri( struct draw_stage
*stage
,
488 struct prim_header
*header
)
490 clip_init_state( stage
);
491 stage
->tri( stage
, header
);
494 static void clip_first_line( struct draw_stage
*stage
,
495 struct prim_header
*header
)
497 clip_init_state( stage
);
498 stage
->line( stage
, header
);
502 static void clip_flush( struct draw_stage
*stage
,
505 stage
->tri
= clip_first_tri
;
506 stage
->line
= clip_first_line
;
507 stage
->next
->flush( stage
->next
, flags
);
511 static void clip_reset_stipple_counter( struct draw_stage
*stage
)
513 stage
->next
->reset_stipple_counter( stage
->next
);
517 static void clip_destroy( struct draw_stage
*stage
)
519 draw_free_temp_verts( stage
);
525 * Allocate a new clipper stage.
526 * \return pointer to new stage object
528 struct draw_stage
*draw_clip_stage( struct draw_context
*draw
)
530 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
534 clipper
->stage
.draw
= draw
;
535 clipper
->stage
.name
= "clipper";
536 clipper
->stage
.point
= clip_point
;
537 clipper
->stage
.line
= clip_first_line
;
538 clipper
->stage
.tri
= clip_first_tri
;
539 clipper
->stage
.flush
= clip_flush
;
540 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
541 clipper
->stage
.destroy
= clip_destroy
;
543 clipper
->plane
= draw
->plane
;
545 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
548 return &clipper
->stage
;
552 clipper
->stage
.destroy( &clipper
->stage
);