da20028904aa0e78d8ec8809e3f0a61906443cc4
[mesa.git] / src / mesa / pipe / draw / draw_clip.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * \brief Clipping stage
30 *
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 */
33
34
35 #include "pipe/p_util.h"
36 #include "pipe/p_shader_tokens.h"
37
38 #include "draw_context.h"
39 #include "draw_private.h"
40
41
42 #ifndef IS_NEGATIVE
43 #define IS_NEGATIVE(X) ((X) < 0.0)
44 #endif
45
46 #ifndef DIFFERENT_SIGNS
47 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
48 #endif
49
50 #ifndef MAX_CLIPPED_VERTICES
51 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
52 #endif
53
54
55
56 struct clipper {
57 struct draw_stage stage; /**< base class */
58
59 /* Basically duplicate some of the flatshading logic here:
60 */
61 boolean flat;
62 uint num_color_attribs;
63 uint color_attribs[4]; /* front/back primary/secondary colors */
64
65 float (*plane)[4];
66 };
67
68
69 /* This is a bit confusing:
70 */
71 static INLINE struct clipper *clipper_stage( struct draw_stage *stage )
72 {
73 return (struct clipper *)stage;
74 }
75
76
77 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
78
79
80 /* All attributes are float[4], so this is easy:
81 */
82 static void interp_attr( float *fdst,
83 float t,
84 const float *fin,
85 const float *fout )
86 {
87 fdst[0] = LINTERP( t, fout[0], fin[0] );
88 fdst[1] = LINTERP( t, fout[1], fin[1] );
89 fdst[2] = LINTERP( t, fout[2], fin[2] );
90 fdst[3] = LINTERP( t, fout[3], fin[3] );
91 }
92
93 static void copy_colors( struct draw_stage *stage,
94 struct vertex_header *dst,
95 const struct vertex_header *src )
96 {
97 const struct clipper *clipper = clipper_stage(stage);
98 uint i;
99 for (i = 0; i < clipper->num_color_attribs; i++) {
100 const uint attr = clipper->color_attribs[i];
101 COPY_4FV(dst->data[attr], src->data[attr]);
102 }
103 }
104
105
106
107 /* Interpolate between two vertices to produce a third.
108 */
109 static void interp( const struct clipper *clip,
110 struct vertex_header *dst,
111 float t,
112 const struct vertex_header *out,
113 const struct vertex_header *in )
114 {
115 const unsigned nr_attrs = clip->stage.draw->num_vs_outputs;
116 unsigned j;
117
118 /* Vertex header.
119 */
120 {
121 dst->clipmask = 0;
122 dst->edgeflag = 0;
123 dst->pad = 0;
124 dst->vertex_id = UNDEFINED_VERTEX_ID;
125 }
126
127 /* Clip coordinates: interpolate normally
128 */
129 {
130 interp_attr(dst->clip, t, in->clip, out->clip);
131 }
132
133 /* Do the projective divide and insert window coordinates:
134 */
135 {
136 const float *pos = dst->clip;
137 const float *scale = clip->stage.draw->viewport.scale;
138 const float *trans = clip->stage.draw->viewport.translate;
139 const float oow = 1.0f / pos[3];
140
141 dst->data[0][0] = pos[0] * oow * scale[0] + trans[0];
142 dst->data[0][1] = pos[1] * oow * scale[1] + trans[1];
143 dst->data[0][2] = pos[2] * oow * scale[2] + trans[2];
144 dst->data[0][3] = oow;
145 }
146
147 /* Other attributes
148 * Note: start at 1 to skip winpos (data[0]) since we just computed
149 * it above.
150 */
151 for (j = 1; j < nr_attrs; j++) {
152 interp_attr(dst->data[j], t, in->data[j], out->data[j]);
153 }
154 }
155
156
157 static void emit_poly( struct draw_stage *stage,
158 struct vertex_header **inlist,
159 unsigned n,
160 const struct prim_header *origPrim)
161 {
162 struct prim_header header;
163 unsigned i;
164
165 /* later stages may need the determinant, but only the sign matters */
166 header.det = origPrim->det;
167
168 for (i = 2; i < n; i++) {
169 header.v[0] = inlist[i-1];
170 header.v[1] = inlist[i];
171 header.v[2] = inlist[0]; /* keep in v[2] for flatshading */
172
173 {
174 unsigned tmp1 = header.v[1]->edgeflag;
175 unsigned tmp2 = header.v[2]->edgeflag;
176
177 if (i != n-1) header.v[1]->edgeflag = 0;
178 if (i != 2) header.v[2]->edgeflag = 0;
179
180 header.edgeflags = ((header.v[0]->edgeflag << 0) |
181 (header.v[1]->edgeflag << 1) |
182 (header.v[2]->edgeflag << 2));
183
184 stage->next->tri( stage->next, &header );
185
186 header.v[1]->edgeflag = tmp1;
187 header.v[2]->edgeflag = tmp2;
188 }
189 }
190 }
191
192
193
194
195 /* Clip a triangle against the viewport and user clip planes.
196 */
197 static void
198 do_clip_tri( struct draw_stage *stage,
199 struct prim_header *header,
200 unsigned clipmask )
201 {
202 struct clipper *clipper = clipper_stage( stage );
203 struct vertex_header *a[MAX_CLIPPED_VERTICES];
204 struct vertex_header *b[MAX_CLIPPED_VERTICES];
205 struct vertex_header **inlist = a;
206 struct vertex_header **outlist = b;
207 unsigned tmpnr = 0;
208 unsigned n = 3;
209 unsigned i;
210
211 inlist[0] = header->v[0];
212 inlist[1] = header->v[1];
213 inlist[2] = header->v[2];
214
215 while (clipmask && n >= 3) {
216 const unsigned plane_idx = ffs(clipmask)-1;
217 const float *plane = clipper->plane[plane_idx];
218 struct vertex_header *vert_prev = inlist[0];
219 float dp_prev = dot4( vert_prev->clip, plane );
220 unsigned outcount = 0;
221
222 clipmask &= ~(1<<plane_idx);
223
224 inlist[n] = inlist[0]; /* prevent rotation of vertices */
225
226 for (i = 1; i <= n; i++) {
227 struct vertex_header *vert = inlist[i];
228
229 float dp = dot4( vert->clip, plane );
230
231 if (!IS_NEGATIVE(dp_prev)) {
232 outlist[outcount++] = vert_prev;
233 }
234
235 if (DIFFERENT_SIGNS(dp, dp_prev)) {
236 struct vertex_header *new_vert = clipper->stage.tmp[tmpnr++];
237 outlist[outcount++] = new_vert;
238
239 if (IS_NEGATIVE(dp)) {
240 /* Going out of bounds. Avoid division by zero as we
241 * know dp != dp_prev from DIFFERENT_SIGNS, above.
242 */
243 float t = dp / (dp - dp_prev);
244 interp( clipper, new_vert, t, vert, vert_prev );
245
246 /* Force edgeflag true in this case:
247 */
248 new_vert->edgeflag = 1;
249 } else {
250 /* Coming back in.
251 */
252 float t = dp_prev / (dp_prev - dp);
253 interp( clipper, new_vert, t, vert_prev, vert );
254
255 /* Copy starting vert's edgeflag:
256 */
257 new_vert->edgeflag = vert_prev->edgeflag;
258 }
259 }
260
261 vert_prev = vert;
262 dp_prev = dp;
263 }
264
265 {
266 struct vertex_header **tmp = inlist;
267 inlist = outlist;
268 outlist = tmp;
269 n = outcount;
270 }
271 }
272
273 /* If flat-shading, copy color to new provoking vertex.
274 */
275 if (clipper->flat && inlist[0] != header->v[2]) {
276 if (1) {
277 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
278 }
279
280 copy_colors(stage, inlist[0], header->v[2]);
281 }
282
283
284
285 /* Emit the polygon as triangles to the setup stage:
286 */
287 if (n >= 3)
288 emit_poly( stage, inlist, n, header );
289 }
290
291
292 /* Clip a line against the viewport and user clip planes.
293 */
294 static void
295 do_clip_line( struct draw_stage *stage,
296 struct prim_header *header,
297 unsigned clipmask )
298 {
299 const struct clipper *clipper = clipper_stage( stage );
300 struct vertex_header *v0 = header->v[0];
301 struct vertex_header *v1 = header->v[1];
302 const float *pos0 = v0->clip;
303 const float *pos1 = v1->clip;
304 float t0 = 0.0F;
305 float t1 = 0.0F;
306 struct prim_header newprim;
307
308 while (clipmask) {
309 const unsigned plane_idx = ffs(clipmask)-1;
310 const float *plane = clipper->plane[plane_idx];
311 const float dp0 = dot4( pos0, plane );
312 const float dp1 = dot4( pos1, plane );
313
314 if (dp1 < 0.0F) {
315 float t = dp1 / (dp1 - dp0);
316 t1 = MAX2(t1, t);
317 }
318
319 if (dp0 < 0.0F) {
320 float t = dp0 / (dp0 - dp1);
321 t0 = MAX2(t0, t);
322 }
323
324 if (t0 + t1 >= 1.0F)
325 return; /* discard */
326
327 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
328 }
329
330 if (v0->clipmask) {
331 interp( clipper, stage->tmp[0], t0, v0, v1 );
332
333 if (clipper->flat)
334 copy_colors(stage, stage->tmp[0], v0);
335
336 newprim.v[0] = stage->tmp[0];
337 }
338 else {
339 newprim.v[0] = v0;
340 }
341
342 if (v1->clipmask) {
343 interp( clipper, stage->tmp[1], t1, v1, v0 );
344 newprim.v[1] = stage->tmp[1];
345 }
346 else {
347 newprim.v[1] = v1;
348 }
349
350 stage->next->line( stage->next, &newprim );
351 }
352
353
354 static void
355 clip_point( struct draw_stage *stage,
356 struct prim_header *header )
357 {
358 if (header->v[0]->clipmask == 0)
359 stage->next->point( stage->next, header );
360 }
361
362
363 static void
364 clip_line( struct draw_stage *stage,
365 struct prim_header *header )
366 {
367 unsigned clipmask = (header->v[0]->clipmask |
368 header->v[1]->clipmask);
369
370 if (clipmask == 0) {
371 /* no clipping needed */
372 stage->next->line( stage->next, header );
373 }
374 else if ((header->v[0]->clipmask &
375 header->v[1]->clipmask) == 0) {
376 do_clip_line(stage, header, clipmask);
377 }
378 /* else, totally clipped */
379 }
380
381
382 static void
383 clip_tri( struct draw_stage *stage,
384 struct prim_header *header )
385 {
386 unsigned clipmask = (header->v[0]->clipmask |
387 header->v[1]->clipmask |
388 header->v[2]->clipmask);
389
390 if (clipmask == 0) {
391 /* no clipping needed */
392 stage->next->tri( stage->next, header );
393 }
394 else if ((header->v[0]->clipmask &
395 header->v[1]->clipmask &
396 header->v[2]->clipmask) == 0) {
397 do_clip_tri(stage, header, clipmask);
398 }
399 }
400
401 /* Update state. Could further delay this until we hit the first
402 * primitive that really requires clipping.
403 */
404 static void
405 clip_init_state( struct draw_stage *stage )
406 {
407 struct clipper *clipper = clipper_stage( stage );
408
409 clipper->flat = stage->draw->rasterizer->flatshade;
410
411 if (clipper->flat) {
412 const struct pipe_shader_state *vs = stage->draw->vertex_shader->state;
413 uint i;
414
415 clipper->num_color_attribs = 0;
416 for (i = 0; i < vs->num_outputs; i++) {
417 if (vs->output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
418 vs->output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
419 clipper->color_attribs[clipper->num_color_attribs++] = i;
420 }
421 }
422 }
423
424 stage->tri = clip_tri;
425 stage->line = clip_line;
426 }
427
428
429
430 static void clip_first_tri( struct draw_stage *stage,
431 struct prim_header *header )
432 {
433 clip_init_state( stage );
434 stage->tri( stage, header );
435 }
436
437 static void clip_first_line( struct draw_stage *stage,
438 struct prim_header *header )
439 {
440 clip_init_state( stage );
441 stage->line( stage, header );
442 }
443
444
445 static void clip_flush( struct draw_stage *stage,
446 unsigned flags )
447 {
448 stage->tri = clip_first_tri;
449 stage->line = clip_first_line;
450 stage->next->flush( stage->next, flags );
451 }
452
453
454 static void clip_reset_stipple_counter( struct draw_stage *stage )
455 {
456 stage->next->reset_stipple_counter( stage->next );
457 }
458
459
460 static void clip_destroy( struct draw_stage *stage )
461 {
462 draw_free_tmps( stage );
463 FREE( stage );
464 }
465
466
467 /**
468 * Allocate a new clipper stage.
469 * \return pointer to new stage object
470 */
471 struct draw_stage *draw_clip_stage( struct draw_context *draw )
472 {
473 struct clipper *clipper = CALLOC_STRUCT(clipper);
474
475 draw_alloc_tmps( &clipper->stage, MAX_CLIPPED_VERTICES+1 );
476
477 clipper->stage.draw = draw;
478 clipper->stage.point = clip_point;
479 clipper->stage.line = clip_first_line;
480 clipper->stage.tri = clip_first_tri;
481 clipper->stage.flush = clip_flush;
482 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
483 clipper->stage.destroy = clip_destroy;
484
485 clipper->plane = draw->plane;
486
487 return &clipper->stage;
488 }