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 */
71 /* This is a bit confusing:
73 static INLINE
struct clip_stage
*clip_stage( struct draw_stage
*stage
)
75 return (struct clip_stage
*)stage
;
79 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
82 /* All attributes are float[4], so this is easy:
84 static void interp_attr( float *fdst
,
89 fdst
[0] = LINTERP( t
, fout
[0], fin
[0] );
90 fdst
[1] = LINTERP( t
, fout
[1], fin
[1] );
91 fdst
[2] = LINTERP( t
, fout
[2], fin
[2] );
92 fdst
[3] = LINTERP( t
, fout
[3], fin
[3] );
96 static void copy_colors( struct draw_stage
*stage
,
97 struct vertex_header
*dst
,
98 const struct vertex_header
*src
)
100 const struct clip_stage
*clipper
= clip_stage(stage
);
102 for (i
= 0; i
< clipper
->num_color_attribs
; i
++) {
103 const uint attr
= clipper
->color_attribs
[i
];
104 COPY_4FV(dst
->data
[attr
], src
->data
[attr
]);
110 /* Interpolate between two vertices to produce a third.
112 static void interp( const struct clip_stage
*clip
,
113 struct vertex_header
*dst
,
115 const struct vertex_header
*out
,
116 const struct vertex_header
*in
)
118 const unsigned nr_attrs
= draw_current_shader_outputs(clip
->stage
.draw
);
119 const unsigned pos_attr
= draw_current_shader_position_output(clip
->stage
.draw
);
126 dst
->edgeflag
= 0; /* will get overwritten later */
128 dst
->vertex_id
= UNDEFINED_VERTEX_ID
;
131 /* Clip coordinates: interpolate normally
134 interp_attr(dst
->clip
, t
, in
->clip
, out
->clip
);
137 /* Do the projective divide and insert 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
]);
160 static void emit_poly( struct draw_stage
*stage
,
161 struct vertex_header
**inlist
,
163 const struct prim_header
*origPrim
)
165 struct prim_header header
;
167 ushort edge_first
, edge_middle
, edge_last
;
169 if (stage
->draw
->rasterizer
->flatshade_first
) {
170 edge_first
= DRAW_PIPE_EDGE_FLAG_0
;
171 edge_middle
= DRAW_PIPE_EDGE_FLAG_1
;
172 edge_last
= DRAW_PIPE_EDGE_FLAG_2
;
175 edge_first
= DRAW_PIPE_EDGE_FLAG_2
;
176 edge_middle
= DRAW_PIPE_EDGE_FLAG_0
;
177 edge_last
= DRAW_PIPE_EDGE_FLAG_1
;
180 /* later stages may need the determinant, but only the sign matters */
181 header
.det
= origPrim
->det
;
182 header
.flags
= DRAW_PIPE_RESET_STIPPLE
| edge_first
| edge_middle
;
185 for (i
= 2; i
< n
; i
++, header
.flags
= edge_middle
) {
186 /* keep in provoking vertex for flatshading */
187 if (stage
->draw
->rasterizer
->flatshade_first
) {
188 header
.v
[0] = inlist
[0];
189 header
.v
[1] = inlist
[i
-1];
190 header
.v
[2] = inlist
[i
];
193 header
.v
[0] = inlist
[i
-1];
194 header
.v
[1] = inlist
[i
];
195 header
.v
[2] = inlist
[0];
199 header
.flags
|= edge_last
;
202 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
204 debug_printf("Clipped tri:\n");
205 for (j
= 0; j
< 3; j
++) {
206 for (k
= 0; k
< vs
->info
.num_outputs
; k
++) {
207 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j
, k
,
208 header
.v
[j
]->data
[k
][0],
209 header
.v
[j
]->data
[k
][1],
210 header
.v
[j
]->data
[k
][2],
211 header
.v
[j
]->data
[k
][3]);
216 stage
->next
->tri( stage
->next
, &header
);
222 dot4(const float *a
, const float *b
)
224 return (a
[0] * b
[0] +
231 /* Clip a triangle against the viewport and user clip planes.
234 do_clip_tri( struct draw_stage
*stage
,
235 struct prim_header
*header
,
238 struct clip_stage
*clipper
= clip_stage( stage
);
239 struct vertex_header
*a
[MAX_CLIPPED_VERTICES
];
240 struct vertex_header
*b
[MAX_CLIPPED_VERTICES
];
241 struct vertex_header
**inlist
= a
;
242 struct vertex_header
**outlist
= b
;
247 inlist
[0] = header
->v
[0];
248 inlist
[1] = header
->v
[1];
249 inlist
[2] = header
->v
[2];
251 while (clipmask
&& n
>= 3) {
252 const unsigned plane_idx
= ffs(clipmask
)-1;
253 const float *plane
= clipper
->plane
[plane_idx
];
254 struct vertex_header
*vert_prev
= inlist
[0];
255 float dp_prev
= dot4( vert_prev
->clip
, plane
);
256 unsigned outcount
= 0;
258 clipmask
&= ~(1<<plane_idx
);
260 inlist
[n
] = inlist
[0]; /* prevent rotation of vertices */
262 for (i
= 1; i
<= n
; i
++) {
263 struct vertex_header
*vert
= inlist
[i
];
265 float dp
= dot4( vert
->clip
, plane
);
267 if (!IS_NEGATIVE(dp_prev
)) {
268 outlist
[outcount
++] = vert_prev
;
271 if (DIFFERENT_SIGNS(dp
, dp_prev
)) {
272 struct vertex_header
*new_vert
= clipper
->stage
.tmp
[tmpnr
++];
273 outlist
[outcount
++] = new_vert
;
275 if (IS_NEGATIVE(dp
)) {
276 /* Going out of bounds. Avoid division by zero as we
277 * know dp != dp_prev from DIFFERENT_SIGNS, above.
279 float t
= dp
/ (dp
- dp_prev
);
280 interp( clipper
, new_vert
, t
, vert
, vert_prev
);
282 /* Force edgeflag true in this case:
284 new_vert
->edgeflag
= 1;
288 float t
= dp_prev
/ (dp_prev
- dp
);
289 interp( clipper
, new_vert
, t
, vert_prev
, vert
);
291 /* Copy starting vert's edgeflag:
293 new_vert
->edgeflag
= vert_prev
->edgeflag
;
301 /* swap in/out lists */
303 struct vertex_header
**tmp
= inlist
;
310 /* If flat-shading, copy color to new provoking vertex.
313 if (stage
->draw
->rasterizer
->flatshade_first
) {
314 if (inlist
[0] != header
->v
[0]) {
315 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
316 copy_colors(stage
, inlist
[0], header
->v
[0]);
320 if (inlist
[0] != header
->v
[2]) {
321 inlist
[0] = dup_vert(stage
, inlist
[0], tmpnr
++);
322 copy_colors(stage
, inlist
[0], header
->v
[2]);
327 /* Emit the polygon as triangles to the setup stage:
330 emit_poly( stage
, inlist
, n
, header
);
334 /* Clip a line against the viewport and user clip planes.
337 do_clip_line( struct draw_stage
*stage
,
338 struct prim_header
*header
,
341 const struct clip_stage
*clipper
= clip_stage( stage
);
342 struct vertex_header
*v0
= header
->v
[0];
343 struct vertex_header
*v1
= header
->v
[1];
344 const float *pos0
= v0
->clip
;
345 const float *pos1
= v1
->clip
;
348 struct prim_header newprim
;
351 const unsigned plane_idx
= ffs(clipmask
)-1;
352 const float *plane
= clipper
->plane
[plane_idx
];
353 const float dp0
= dot4( pos0
, plane
);
354 const float dp1
= dot4( pos1
, plane
);
357 float t
= dp1
/ (dp1
- dp0
);
362 float t
= dp0
/ (dp0
- dp1
);
367 return; /* discard */
369 clipmask
&= ~(1 << plane_idx
); /* turn off this plane's bit */
373 interp( clipper
, stage
->tmp
[0], t0
, v0
, v1
);
376 copy_colors(stage
, stage
->tmp
[0], v0
);
378 newprim
.v
[0] = stage
->tmp
[0];
385 interp( clipper
, stage
->tmp
[1], t1
, v1
, v0
);
386 newprim
.v
[1] = stage
->tmp
[1];
392 stage
->next
->line( stage
->next
, &newprim
);
397 clip_point( struct draw_stage
*stage
,
398 struct prim_header
*header
)
400 if (header
->v
[0]->clipmask
== 0)
401 stage
->next
->point( stage
->next
, header
);
406 clip_line( struct draw_stage
*stage
,
407 struct prim_header
*header
)
409 unsigned clipmask
= (header
->v
[0]->clipmask
|
410 header
->v
[1]->clipmask
);
413 /* no clipping needed */
414 stage
->next
->line( stage
->next
, header
);
416 else if ((header
->v
[0]->clipmask
&
417 header
->v
[1]->clipmask
) == 0) {
418 do_clip_line(stage
, header
, clipmask
);
420 /* else, totally clipped */
425 clip_tri( struct draw_stage
*stage
,
426 struct prim_header
*header
)
428 unsigned clipmask
= (header
->v
[0]->clipmask
|
429 header
->v
[1]->clipmask
|
430 header
->v
[2]->clipmask
);
433 /* no clipping needed */
434 stage
->next
->tri( stage
->next
, header
);
436 else if ((header
->v
[0]->clipmask
&
437 header
->v
[1]->clipmask
&
438 header
->v
[2]->clipmask
) == 0) {
439 do_clip_tri(stage
, header
, clipmask
);
444 /* Update state. Could further delay this until we hit the first
445 * primitive that really requires clipping.
448 clip_init_state( struct draw_stage
*stage
)
450 struct clip_stage
*clipper
= clip_stage( stage
);
452 clipper
->flat
= stage
->draw
->rasterizer
->flatshade
? TRUE
: FALSE
;
455 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
458 clipper
->num_color_attribs
= 0;
459 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
460 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
||
461 vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_BCOLOR
) {
462 clipper
->color_attribs
[clipper
->num_color_attribs
++] = i
;
467 stage
->tri
= clip_tri
;
468 stage
->line
= clip_line
;
473 static void clip_first_tri( struct draw_stage
*stage
,
474 struct prim_header
*header
)
476 clip_init_state( stage
);
477 stage
->tri( stage
, header
);
480 static void clip_first_line( struct draw_stage
*stage
,
481 struct prim_header
*header
)
483 clip_init_state( stage
);
484 stage
->line( stage
, header
);
488 static void clip_flush( struct draw_stage
*stage
,
491 stage
->tri
= clip_first_tri
;
492 stage
->line
= clip_first_line
;
493 stage
->next
->flush( stage
->next
, flags
);
497 static void clip_reset_stipple_counter( struct draw_stage
*stage
)
499 stage
->next
->reset_stipple_counter( stage
->next
);
503 static void clip_destroy( struct draw_stage
*stage
)
505 draw_free_temp_verts( stage
);
511 * Allocate a new clipper stage.
512 * \return pointer to new stage object
514 struct draw_stage
*draw_clip_stage( struct draw_context
*draw
)
516 struct clip_stage
*clipper
= CALLOC_STRUCT(clip_stage
);
520 if (!draw_alloc_temp_verts( &clipper
->stage
, MAX_CLIPPED_VERTICES
+1 ))
523 clipper
->stage
.draw
= draw
;
524 clipper
->stage
.name
= "clipper";
525 clipper
->stage
.point
= clip_point
;
526 clipper
->stage
.line
= clip_first_line
;
527 clipper
->stage
.tri
= clip_first_tri
;
528 clipper
->stage
.flush
= clip_flush
;
529 clipper
->stage
.reset_stipple_counter
= clip_reset_stipple_counter
;
530 clipper
->stage
.destroy
= clip_destroy
;
532 clipper
->plane
= draw
->plane
;
534 return &clipper
->stage
;
538 clipper
->stage
.destroy( &clipper
->stage
);