eeaaf41f719af0ce267a8dddaa465af7fbcb8083
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_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 "util/u_memory.h"
36 #include "util/u_math.h"
37
38 #include "pipe/p_shader_tokens.h"
39
40 #include "draw_vs.h"
41 #include "draw_pipe.h"
42 #include "draw_fs.h"
43 #include "draw_gs.h"
44
45
46 /** Set to 1 to enable printing of coords before/after clipping */
47 #define DEBUG_CLIP 0
48
49
50 #ifndef IS_NEGATIVE
51 #define IS_NEGATIVE(X) ((X) < 0.0)
52 #endif
53
54 #ifndef DIFFERENT_SIGNS
55 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
56 #endif
57
58 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
59
60
61
62 struct clip_stage {
63 struct draw_stage stage; /**< base class */
64
65 /* List of the attributes to be flatshaded. */
66 uint num_flat_attribs;
67 uint flat_attribs[PIPE_MAX_SHADER_OUTPUTS];
68
69 /* Mask of attributes in noperspective mode */
70 boolean noperspective_attribs[PIPE_MAX_SHADER_OUTPUTS];
71
72 float (*plane)[4];
73 };
74
75
76 /** Cast wrapper */
77 static INLINE struct clip_stage *clip_stage( struct draw_stage *stage )
78 {
79 return (struct clip_stage *)stage;
80 }
81
82
83 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
84
85
86 /* All attributes are float[4], so this is easy:
87 */
88 static void interp_attr( float dst[4],
89 float t,
90 const float in[4],
91 const float out[4] )
92 {
93 dst[0] = LINTERP( t, out[0], in[0] );
94 dst[1] = LINTERP( t, out[1], in[1] );
95 dst[2] = LINTERP( t, out[2], in[2] );
96 dst[3] = LINTERP( t, out[3], in[3] );
97 }
98
99
100 /**
101 * Copy flat shaded attributes src vertex to dst vertex.
102 */
103 static void copy_flat( struct draw_stage *stage,
104 struct vertex_header *dst,
105 const struct vertex_header *src )
106 {
107 const struct clip_stage *clipper = clip_stage(stage);
108 uint i;
109 for (i = 0; i < clipper->num_flat_attribs; i++) {
110 const uint attr = clipper->flat_attribs[i];
111 COPY_4FV(dst->data[attr], src->data[attr]);
112 }
113 }
114
115
116
117 /* Interpolate between two vertices to produce a third.
118 */
119 static void interp( const struct clip_stage *clip,
120 struct vertex_header *dst,
121 float t,
122 const struct vertex_header *out,
123 const struct vertex_header *in )
124 {
125 const unsigned nr_attrs = draw_current_shader_outputs(clip->stage.draw);
126 const unsigned pos_attr = draw_current_shader_position_output(clip->stage.draw);
127 const unsigned clip_attr = draw_current_shader_clipvertex_output(clip->stage.draw);
128 unsigned j;
129 float t_nopersp;
130
131 /* Vertex header.
132 */
133 dst->clipmask = 0;
134 dst->edgeflag = 0; /* will get overwritten later */
135 dst->have_clipdist = in->have_clipdist;
136 dst->vertex_id = UNDEFINED_VERTEX_ID;
137
138 /* Interpolate the clip-space coords.
139 */
140 interp_attr(dst->clip, t, in->clip, out->clip);
141 /* interpolate the clip-space position */
142 interp_attr(dst->pre_clip_pos, t, in->pre_clip_pos, out->pre_clip_pos);
143
144 /* Do the projective divide and viewport transformation to get
145 * new window coordinates:
146 */
147 {
148 const float *pos = dst->pre_clip_pos;
149 const float *scale = clip->stage.draw->viewport.scale;
150 const float *trans = clip->stage.draw->viewport.translate;
151 const float oow = 1.0f / pos[3];
152
153 dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
154 dst->data[pos_attr][1] = pos[1] * oow * scale[1] + trans[1];
155 dst->data[pos_attr][2] = pos[2] * oow * scale[2] + trans[2];
156 dst->data[pos_attr][3] = oow;
157 }
158
159 /**
160 * Compute the t in screen-space instead of 3d space to use
161 * for noperspective interpolation.
162 *
163 * The points can be aligned with the X axis, so in that case try
164 * the Y. When both points are at the same screen position, we can
165 * pick whatever value (the interpolated point won't be in front
166 * anyway), so just use the 3d t.
167 */
168 {
169 int k;
170 t_nopersp = t;
171 /* find either in.x != out.x or in.y != out.y */
172 for (k = 0; k < 2; k++) {
173 if (in->clip[k] != out->clip[k]) {
174 /* do divide by W, then compute linear interpolation factor */
175 float in_coord = in->clip[k] / in->clip[3];
176 float out_coord = out->clip[k] / out->clip[3];
177 float dst_coord = dst->clip[k] / dst->clip[3];
178 t_nopersp = (dst_coord - out_coord) / (in_coord - out_coord);
179 break;
180 }
181 }
182 }
183
184 /* Other attributes
185 */
186 for (j = 0; j < nr_attrs; j++) {
187 if (j != pos_attr && j != clip_attr) {
188 if (clip->noperspective_attribs[j])
189 interp_attr(dst->data[j], t_nopersp, in->data[j], out->data[j]);
190 else
191 interp_attr(dst->data[j], t, in->data[j], out->data[j]);
192 }
193 }
194 }
195
196
197 /**
198 * Emit a post-clip polygon to the next pipeline stage. The polygon
199 * will be convex and the provoking vertex will always be vertex[0].
200 */
201 static void emit_poly( struct draw_stage *stage,
202 struct vertex_header **inlist,
203 const boolean *edgeflags,
204 unsigned n,
205 const struct prim_header *origPrim)
206 {
207 struct prim_header header;
208 unsigned i;
209 ushort edge_first, edge_middle, edge_last;
210
211 if (stage->draw->rasterizer->flatshade_first) {
212 edge_first = DRAW_PIPE_EDGE_FLAG_0;
213 edge_middle = DRAW_PIPE_EDGE_FLAG_1;
214 edge_last = DRAW_PIPE_EDGE_FLAG_2;
215 }
216 else {
217 edge_first = DRAW_PIPE_EDGE_FLAG_2;
218 edge_middle = DRAW_PIPE_EDGE_FLAG_0;
219 edge_last = DRAW_PIPE_EDGE_FLAG_1;
220 }
221
222 if (!edgeflags[0])
223 edge_first = 0;
224
225 /* later stages may need the determinant, but only the sign matters */
226 header.det = origPrim->det;
227 header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
228 header.pad = 0;
229
230 for (i = 2; i < n; i++, header.flags = edge_middle) {
231 /* order the triangle verts to respect the provoking vertex mode */
232 if (stage->draw->rasterizer->flatshade_first) {
233 header.v[0] = inlist[0]; /* the provoking vertex */
234 header.v[1] = inlist[i-1];
235 header.v[2] = inlist[i];
236 }
237 else {
238 header.v[0] = inlist[i-1];
239 header.v[1] = inlist[i];
240 header.v[2] = inlist[0]; /* the provoking vertex */
241 }
242
243 if (!edgeflags[i-1]) {
244 header.flags &= ~edge_middle;
245 }
246
247 if (i == n - 1 && edgeflags[i])
248 header.flags |= edge_last;
249
250 if (DEBUG_CLIP) {
251 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
252 uint j, k;
253 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
254 stage->draw->rasterizer->flatshade_first);
255 for (j = 0; j < 3; j++) {
256 debug_printf(" Vert %d: clip: %f %f %f %f\n", j,
257 header.v[j]->clip[0],
258 header.v[j]->clip[1],
259 header.v[j]->clip[2],
260 header.v[j]->clip[3]);
261 for (k = 0; k < vs->info.num_outputs; k++) {
262 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
263 header.v[j]->data[k][0],
264 header.v[j]->data[k][1],
265 header.v[j]->data[k][2],
266 header.v[j]->data[k][3]);
267 }
268 }
269 }
270
271 stage->next->tri( stage->next, &header );
272 }
273 }
274
275
276 static INLINE float
277 dot4(const float *a, const float *b)
278 {
279 return (a[0] * b[0] +
280 a[1] * b[1] +
281 a[2] * b[2] +
282 a[3] * b[3]);
283 }
284
285 /*
286 * this function extracts the clip distance for the current plane,
287 * it first checks if the shader provided a clip distance, otherwise
288 * it works out the value using the clipvertex
289 */
290 static INLINE float getclipdist(const struct clip_stage *clipper,
291 struct vertex_header *vert,
292 int plane_idx)
293 {
294 const float *plane;
295 float dp;
296 if (vert->have_clipdist && plane_idx >= 6) {
297 /* pick the correct clipdistance element from the output vectors */
298 int _idx = plane_idx - 6;
299 int cdi = _idx >= 4;
300 int vidx = cdi ? _idx - 4 : _idx;
301 dp = vert->data[draw_current_shader_clipdistance_output(clipper->stage.draw, cdi)][vidx];
302 } else {
303 plane = clipper->plane[plane_idx];
304 dp = dot4(vert->clip, plane);
305 }
306 return dp;
307 }
308
309 /* Clip a triangle against the viewport and user clip planes.
310 */
311 static void
312 do_clip_tri( struct draw_stage *stage,
313 struct prim_header *header,
314 unsigned clipmask )
315 {
316 struct clip_stage *clipper = clip_stage( stage );
317 struct vertex_header *a[MAX_CLIPPED_VERTICES];
318 struct vertex_header *b[MAX_CLIPPED_VERTICES];
319 struct vertex_header **inlist = a;
320 struct vertex_header **outlist = b;
321 unsigned tmpnr = 0;
322 unsigned n = 3;
323 unsigned i;
324 boolean aEdges[MAX_CLIPPED_VERTICES];
325 boolean bEdges[MAX_CLIPPED_VERTICES];
326 boolean *inEdges = aEdges;
327 boolean *outEdges = bEdges;
328
329 inlist[0] = header->v[0];
330 inlist[1] = header->v[1];
331 inlist[2] = header->v[2];
332
333 if (DEBUG_CLIP) {
334 const float *v0 = header->v[0]->clip;
335 const float *v1 = header->v[1]->clip;
336 const float *v2 = header->v[2]->clip;
337 debug_printf("Clip triangle:\n");
338 debug_printf(" %f, %f, %f, %f\n", v0[0], v0[1], v0[2], v0[3]);
339 debug_printf(" %f, %f, %f, %f\n", v1[0], v1[1], v1[2], v1[3]);
340 debug_printf(" %f, %f, %f, %f\n", v2[0], v2[1], v2[2], v2[3]);
341 }
342
343 /*
344 * Note: at this point we can't just use the per-vertex edge flags.
345 * We have to observe the edge flag bits set in header->flags which
346 * were set during primitive decomposition. Put those flags into
347 * an edge flags array which parallels the vertex array.
348 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
349 * the header.flags bit is set AND the per-vertex edgeflag field is set.
350 */
351 inEdges[0] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_0);
352 inEdges[1] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_1);
353 inEdges[2] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_2);
354
355 while (clipmask && n >= 3) {
356 const unsigned plane_idx = ffs(clipmask)-1;
357 const boolean is_user_clip_plane = plane_idx >= 6;
358 struct vertex_header *vert_prev = inlist[0];
359 boolean *edge_prev = &inEdges[0];
360 float dp_prev;
361 unsigned outcount = 0;
362
363 dp_prev = getclipdist(clipper, vert_prev, plane_idx);
364 clipmask &= ~(1<<plane_idx);
365
366 assert(n < MAX_CLIPPED_VERTICES);
367 if (n >= MAX_CLIPPED_VERTICES)
368 return;
369 inlist[n] = inlist[0]; /* prevent rotation of vertices */
370 inEdges[n] = inEdges[0];
371
372 for (i = 1; i <= n; i++) {
373 struct vertex_header *vert = inlist[i];
374 boolean *edge = &inEdges[i];
375
376 float dp = getclipdist(clipper, vert, plane_idx);
377
378 if (!IS_NEGATIVE(dp_prev)) {
379 assert(outcount < MAX_CLIPPED_VERTICES);
380 if (outcount >= MAX_CLIPPED_VERTICES)
381 return;
382 outEdges[outcount] = *edge_prev;
383 outlist[outcount++] = vert_prev;
384 }
385
386 if (DIFFERENT_SIGNS(dp, dp_prev)) {
387 struct vertex_header *new_vert;
388 boolean *new_edge;
389
390 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
391 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
392 return;
393 new_vert = clipper->stage.tmp[tmpnr++];
394
395 assert(outcount < MAX_CLIPPED_VERTICES);
396 if (outcount >= MAX_CLIPPED_VERTICES)
397 return;
398
399 new_edge = &outEdges[outcount];
400 outlist[outcount++] = new_vert;
401
402 if (IS_NEGATIVE(dp)) {
403 /* Going out of bounds. Avoid division by zero as we
404 * know dp != dp_prev from DIFFERENT_SIGNS, above.
405 */
406 float t = dp / (dp - dp_prev);
407 interp( clipper, new_vert, t, vert, vert_prev );
408
409 /* Whether or not to set edge flag for the new vert depends
410 * on whether it's a user-defined clipping plane. We're
411 * copying NVIDIA's behaviour here.
412 */
413 if (is_user_clip_plane) {
414 /* we want to see an edge along the clip plane */
415 *new_edge = TRUE;
416 new_vert->edgeflag = TRUE;
417 }
418 else {
419 /* we don't want to see an edge along the frustum clip plane */
420 *new_edge = *edge_prev;
421 new_vert->edgeflag = FALSE;
422 }
423 }
424 else {
425 /* Coming back in.
426 */
427 float t = dp_prev / (dp_prev - dp);
428 interp( clipper, new_vert, t, vert_prev, vert );
429
430 /* Copy starting vert's edgeflag:
431 */
432 new_vert->edgeflag = vert_prev->edgeflag;
433 *new_edge = *edge_prev;
434 }
435 }
436
437 vert_prev = vert;
438 edge_prev = edge;
439 dp_prev = dp;
440 }
441
442 /* swap in/out lists */
443 {
444 struct vertex_header **tmp = inlist;
445 inlist = outlist;
446 outlist = tmp;
447 n = outcount;
448 }
449 {
450 boolean *tmp = inEdges;
451 inEdges = outEdges;
452 outEdges = tmp;
453 }
454
455 }
456
457 /* If flat-shading, copy provoking vertex color to polygon vertex[0]
458 */
459 if (n >= 3) {
460 if (clipper->num_flat_attribs) {
461 if (stage->draw->rasterizer->flatshade_first) {
462 if (inlist[0] != header->v[0]) {
463 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
464 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
465 return;
466 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
467 copy_flat(stage, inlist[0], header->v[0]);
468 }
469 }
470 else {
471 if (inlist[0] != header->v[2]) {
472 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
473 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
474 return;
475 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
476 copy_flat(stage, inlist[0], header->v[2]);
477 }
478 }
479 }
480
481 /* Emit the polygon as triangles to the setup stage:
482 */
483 emit_poly( stage, inlist, inEdges, n, header );
484 }
485 }
486
487
488 /* Clip a line against the viewport and user clip planes.
489 */
490 static void
491 do_clip_line( struct draw_stage *stage,
492 struct prim_header *header,
493 unsigned clipmask )
494 {
495 const struct clip_stage *clipper = clip_stage( stage );
496 struct vertex_header *v0 = header->v[0];
497 struct vertex_header *v1 = header->v[1];
498 float t0 = 0.0F;
499 float t1 = 0.0F;
500 struct prim_header newprim;
501
502 while (clipmask) {
503 const unsigned plane_idx = ffs(clipmask)-1;
504 const float dp0 = getclipdist(clipper, v0, plane_idx);
505 const float dp1 = getclipdist(clipper, v1, plane_idx);
506
507 if (dp1 < 0.0F) {
508 float t = dp1 / (dp1 - dp0);
509 t1 = MAX2(t1, t);
510 }
511
512 if (dp0 < 0.0F) {
513 float t = dp0 / (dp0 - dp1);
514 t0 = MAX2(t0, t);
515 }
516
517 if (t0 + t1 >= 1.0F)
518 return; /* discard */
519
520 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
521 }
522
523 if (v0->clipmask) {
524 interp( clipper, stage->tmp[0], t0, v0, v1 );
525 copy_flat(stage, stage->tmp[0], v0);
526 newprim.v[0] = stage->tmp[0];
527 }
528 else {
529 newprim.v[0] = v0;
530 }
531
532 if (v1->clipmask) {
533 interp( clipper, stage->tmp[1], t1, v1, v0 );
534 newprim.v[1] = stage->tmp[1];
535 }
536 else {
537 newprim.v[1] = v1;
538 }
539
540 stage->next->line( stage->next, &newprim );
541 }
542
543
544 static void
545 clip_point( struct draw_stage *stage,
546 struct prim_header *header )
547 {
548 if (header->v[0]->clipmask == 0)
549 stage->next->point( stage->next, header );
550 }
551
552
553 static void
554 clip_line( struct draw_stage *stage,
555 struct prim_header *header )
556 {
557 unsigned clipmask = (header->v[0]->clipmask |
558 header->v[1]->clipmask);
559
560 if (clipmask == 0) {
561 /* no clipping needed */
562 stage->next->line( stage->next, header );
563 }
564 else if ((header->v[0]->clipmask &
565 header->v[1]->clipmask) == 0) {
566 do_clip_line(stage, header, clipmask);
567 }
568 /* else, totally clipped */
569 }
570
571
572 static void
573 clip_tri( struct draw_stage *stage,
574 struct prim_header *header )
575 {
576 unsigned clipmask = (header->v[0]->clipmask |
577 header->v[1]->clipmask |
578 header->v[2]->clipmask);
579
580 if (clipmask == 0) {
581 /* no clipping needed */
582 stage->next->tri( stage->next, header );
583 }
584 else if ((header->v[0]->clipmask &
585 header->v[1]->clipmask &
586 header->v[2]->clipmask) == 0) {
587 do_clip_tri(stage, header, clipmask);
588 }
589 }
590
591
592 /* Update state. Could further delay this until we hit the first
593 * primitive that really requires clipping.
594 */
595 static void
596 clip_init_state( struct draw_stage *stage )
597 {
598 struct clip_stage *clipper = clip_stage( stage );
599 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
600 const struct draw_geometry_shader *gs = stage->draw->gs.geometry_shader;
601 const struct draw_fragment_shader *fs = stage->draw->fs.fragment_shader;
602 uint i;
603 struct tgsi_shader_info *vs_info = gs ? &gs->info : &vs->info;
604
605 /* We need to know for each attribute what kind of interpolation is
606 * done on it (flat, smooth or noperspective). But the information
607 * is not directly accessible for outputs, only for inputs. So we
608 * have to match semantic name and index between the VS (or GS/ES)
609 * outputs and the FS inputs to get to the interpolation mode.
610 *
611 * The only hitch is with gl_FrontColor/gl_BackColor which map to
612 * gl_Color, and their Secondary versions. First there are (up to)
613 * two outputs for one input, so we tuck the information in a
614 * specific array. Second if they don't have qualifiers, the
615 * default value has to be picked from the global shade mode.
616 *
617 * Of course, if we don't have a fragment shader in the first
618 * place, defaults should be used.
619 */
620
621 /* First pick up the interpolation mode for
622 * gl_Color/gl_SecondaryColor, with the correct default.
623 */
624 int indexed_interp[2];
625 indexed_interp[0] = indexed_interp[1] = stage->draw->rasterizer->flatshade ?
626 TGSI_INTERPOLATE_CONSTANT : TGSI_INTERPOLATE_PERSPECTIVE;
627
628 if (fs) {
629 for (i = 0; i < fs->info.num_inputs; i++) {
630 if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_COLOR) {
631 if (fs->info.input_interpolate[i] != TGSI_INTERPOLATE_COLOR)
632 indexed_interp[fs->info.input_semantic_index[i]] = fs->info.input_interpolate[i];
633 }
634 }
635 }
636
637 /* Then resolve the interpolation mode for every output attribute.
638 *
639 * Given how the rest of the code, the most efficient way is to
640 * have a vector of flat-mode attributes, and a mask for
641 * noperspective attributes.
642 */
643
644 clipper->num_flat_attribs = 0;
645 memset(clipper->noperspective_attribs, 0, sizeof(clipper->noperspective_attribs));
646 for (i = 0; i < vs_info->num_outputs; i++) {
647 /* Find the interpolation mode for a specific attribute
648 */
649 int interp;
650
651 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
652 * from the array we've filled before. */
653 if (vs_info->output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
654 vs_info->output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
655 interp = indexed_interp[vs_info->output_semantic_index[i]];
656 } else {
657 /* Otherwise, search in the FS inputs, with a decent default
658 * if we don't find it.
659 */
660 uint j;
661 interp = TGSI_INTERPOLATE_PERSPECTIVE;
662 if (fs) {
663 for (j = 0; j < fs->info.num_inputs; j++) {
664 if (vs_info->output_semantic_name[i] == fs->info.input_semantic_name[j] &&
665 vs_info->output_semantic_index[i] == fs->info.input_semantic_index[j]) {
666 interp = fs->info.input_interpolate[j];
667 break;
668 }
669 }
670 }
671 }
672
673 /* If it's flat, add it to the flat vector. Otherwise update
674 * the noperspective mask.
675 */
676 if (interp == TGSI_INTERPOLATE_CONSTANT) {
677 clipper->flat_attribs[clipper->num_flat_attribs] = i;
678 clipper->num_flat_attribs++;
679 } else
680 clipper->noperspective_attribs[i] = interp == TGSI_INTERPOLATE_LINEAR;
681 }
682
683 stage->tri = clip_tri;
684 stage->line = clip_line;
685 }
686
687
688
689 static void clip_first_tri( struct draw_stage *stage,
690 struct prim_header *header )
691 {
692 clip_init_state( stage );
693 stage->tri( stage, header );
694 }
695
696 static void clip_first_line( struct draw_stage *stage,
697 struct prim_header *header )
698 {
699 clip_init_state( stage );
700 stage->line( stage, header );
701 }
702
703
704 static void clip_flush( struct draw_stage *stage,
705 unsigned flags )
706 {
707 stage->tri = clip_first_tri;
708 stage->line = clip_first_line;
709 stage->next->flush( stage->next, flags );
710 }
711
712
713 static void clip_reset_stipple_counter( struct draw_stage *stage )
714 {
715 stage->next->reset_stipple_counter( stage->next );
716 }
717
718
719 static void clip_destroy( struct draw_stage *stage )
720 {
721 draw_free_temp_verts( stage );
722 FREE( stage );
723 }
724
725
726 /**
727 * Allocate a new clipper stage.
728 * \return pointer to new stage object
729 */
730 struct draw_stage *draw_clip_stage( struct draw_context *draw )
731 {
732 struct clip_stage *clipper = CALLOC_STRUCT(clip_stage);
733 if (clipper == NULL)
734 goto fail;
735
736 clipper->stage.draw = draw;
737 clipper->stage.name = "clipper";
738 clipper->stage.point = clip_point;
739 clipper->stage.line = clip_first_line;
740 clipper->stage.tri = clip_first_tri;
741 clipper->stage.flush = clip_flush;
742 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
743 clipper->stage.destroy = clip_destroy;
744
745 clipper->plane = draw->plane;
746
747 if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
748 goto fail;
749
750 return &clipper->stage;
751
752 fail:
753 if (clipper)
754 clipper->stage.destroy( &clipper->stage );
755
756 return NULL;
757 }