s/Tungsten Graphics/VMware/
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_clip.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 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.
25 *
26 **************************************************************************/
27
28 /**
29 * \brief Clipping stage
30 *
31 * \author Keith Whitwell <keithw@vmware.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 static INLINE unsigned
83 draw_viewport_index(struct draw_context *draw,
84 const struct vertex_header *leading_vertex)
85 {
86 if (draw_current_shader_uses_viewport_index(draw)) {
87 unsigned viewport_index_output =
88 draw_current_shader_viewport_index_output(draw);
89 unsigned viewport_index =
90 *((unsigned*)leading_vertex->data[viewport_index_output]);
91 return draw_clamp_viewport_idx(viewport_index);
92 } else {
93 return 0;
94 }
95 }
96
97
98 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
99
100
101 /* All attributes are float[4], so this is easy:
102 */
103 static void interp_attr( float dst[4],
104 float t,
105 const float in[4],
106 const float out[4] )
107 {
108 dst[0] = LINTERP( t, out[0], in[0] );
109 dst[1] = LINTERP( t, out[1], in[1] );
110 dst[2] = LINTERP( t, out[2], in[2] );
111 dst[3] = LINTERP( t, out[3], in[3] );
112 }
113
114
115 /**
116 * Copy flat shaded attributes src vertex to dst vertex.
117 */
118 static void copy_flat( struct draw_stage *stage,
119 struct vertex_header *dst,
120 const struct vertex_header *src )
121 {
122 const struct clip_stage *clipper = clip_stage(stage);
123 uint i;
124 for (i = 0; i < clipper->num_flat_attribs; i++) {
125 const uint attr = clipper->flat_attribs[i];
126 COPY_4FV(dst->data[attr], src->data[attr]);
127 }
128 }
129
130 /* Interpolate between two vertices to produce a third.
131 */
132 static void interp( const struct clip_stage *clip,
133 struct vertex_header *dst,
134 float t,
135 const struct vertex_header *out,
136 const struct vertex_header *in,
137 unsigned viewport_index )
138 {
139 const unsigned nr_attrs = draw_num_shader_outputs(clip->stage.draw);
140 const unsigned pos_attr = draw_current_shader_position_output(clip->stage.draw);
141 const unsigned clip_attr = draw_current_shader_clipvertex_output(clip->stage.draw);
142 unsigned j;
143 float t_nopersp;
144
145 /* Vertex header.
146 */
147 dst->clipmask = 0;
148 dst->edgeflag = 0; /* will get overwritten later */
149 dst->have_clipdist = in->have_clipdist;
150 dst->vertex_id = UNDEFINED_VERTEX_ID;
151
152 /* Interpolate the clip-space coords.
153 */
154 interp_attr(dst->clip, t, in->clip, out->clip);
155 /* interpolate the clip-space position */
156 interp_attr(dst->pre_clip_pos, t, in->pre_clip_pos, out->pre_clip_pos);
157
158 /* Do the projective divide and viewport transformation to get
159 * new window coordinates:
160 */
161 {
162 const float *pos = dst->pre_clip_pos;
163 const float *scale =
164 clip->stage.draw->viewports[viewport_index].scale;
165 const float *trans =
166 clip->stage.draw->viewports[viewport_index].translate;
167 const float oow = 1.0f / pos[3];
168
169 dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
170 dst->data[pos_attr][1] = pos[1] * oow * scale[1] + trans[1];
171 dst->data[pos_attr][2] = pos[2] * oow * scale[2] + trans[2];
172 dst->data[pos_attr][3] = oow;
173 }
174
175 /**
176 * Compute the t in screen-space instead of 3d space to use
177 * for noperspective interpolation.
178 *
179 * The points can be aligned with the X axis, so in that case try
180 * the Y. When both points are at the same screen position, we can
181 * pick whatever value (the interpolated point won't be in front
182 * anyway), so just use the 3d t.
183 */
184 {
185 int k;
186 t_nopersp = t;
187 /* find either in.x != out.x or in.y != out.y */
188 for (k = 0; k < 2; k++) {
189 if (in->clip[k] != out->clip[k]) {
190 /* do divide by W, then compute linear interpolation factor */
191 float in_coord = in->clip[k] / in->clip[3];
192 float out_coord = out->clip[k] / out->clip[3];
193 float dst_coord = dst->clip[k] / dst->clip[3];
194 t_nopersp = (dst_coord - out_coord) / (in_coord - out_coord);
195 break;
196 }
197 }
198 }
199
200 /* Other attributes
201 */
202 for (j = 0; j < nr_attrs; j++) {
203 if (j != pos_attr && j != clip_attr) {
204 if (clip->noperspective_attribs[j])
205 interp_attr(dst->data[j], t_nopersp, in->data[j], out->data[j]);
206 else
207 interp_attr(dst->data[j], t, in->data[j], out->data[j]);
208 }
209 }
210 }
211
212 /**
213 * Checks whether the specifed triangle is empty and if it is returns
214 * true, otherwise returns false.
215 * Triangle is considered null/empty if it's area is qual to zero.
216 */
217 static INLINE boolean
218 is_tri_null(struct draw_context *draw, const struct prim_header *header)
219 {
220 const unsigned pos_attr = draw_current_shader_position_output(draw);
221 float x1 = header->v[1]->data[pos_attr][0] - header->v[0]->data[pos_attr][0];
222 float y1 = header->v[1]->data[pos_attr][1] - header->v[0]->data[pos_attr][1];
223 float z1 = header->v[1]->data[pos_attr][2] - header->v[0]->data[pos_attr][2];
224
225 float x2 = header->v[2]->data[pos_attr][0] - header->v[0]->data[pos_attr][0];
226 float y2 = header->v[2]->data[pos_attr][1] - header->v[0]->data[pos_attr][1];
227 float z2 = header->v[2]->data[pos_attr][2] - header->v[0]->data[pos_attr][2];
228
229 float vx = y1 * z2 - z1 * y2;
230 float vy = x1 * z2 - z1 * x2;
231 float vz = x1 * y2 - y1 * x2;
232
233 return (vx*vx + vy*vy + vz*vz) == 0.f;
234 }
235
236 /**
237 * Emit a post-clip polygon to the next pipeline stage. The polygon
238 * will be convex and the provoking vertex will always be vertex[0].
239 */
240 static void emit_poly( struct draw_stage *stage,
241 struct vertex_header **inlist,
242 const boolean *edgeflags,
243 unsigned n,
244 const struct prim_header *origPrim)
245 {
246 struct prim_header header;
247 unsigned i;
248 ushort edge_first, edge_middle, edge_last;
249 boolean last_tri_was_null = FALSE;
250 boolean tri_was_not_null = FALSE;
251
252 if (stage->draw->rasterizer->flatshade_first) {
253 edge_first = DRAW_PIPE_EDGE_FLAG_0;
254 edge_middle = DRAW_PIPE_EDGE_FLAG_1;
255 edge_last = DRAW_PIPE_EDGE_FLAG_2;
256 }
257 else {
258 edge_first = DRAW_PIPE_EDGE_FLAG_2;
259 edge_middle = DRAW_PIPE_EDGE_FLAG_0;
260 edge_last = DRAW_PIPE_EDGE_FLAG_1;
261 }
262
263 if (!edgeflags[0])
264 edge_first = 0;
265
266 /* later stages may need the determinant, but only the sign matters */
267 header.det = origPrim->det;
268 header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
269 header.pad = 0;
270
271 for (i = 2; i < n; i++, header.flags = edge_middle) {
272 boolean tri_null;
273 /* order the triangle verts to respect the provoking vertex mode */
274 if (stage->draw->rasterizer->flatshade_first) {
275 header.v[0] = inlist[0]; /* the provoking vertex */
276 header.v[1] = inlist[i-1];
277 header.v[2] = inlist[i];
278 }
279 else {
280 header.v[0] = inlist[i-1];
281 header.v[1] = inlist[i];
282 header.v[2] = inlist[0]; /* the provoking vertex */
283 }
284
285 tri_null = is_tri_null(stage->draw, &header);
286 /* If we generated a triangle with an area, aka. non-null triangle,
287 * or if the previous triangle was also null then skip all subsequent
288 * null triangles */
289 if ((tri_was_not_null && tri_null) || (last_tri_was_null && tri_null)) {
290 last_tri_was_null = tri_null;
291 continue;
292 }
293 last_tri_was_null = tri_null;
294 if (!tri_null) {
295 tri_was_not_null = TRUE;
296 }
297
298 if (!edgeflags[i-1]) {
299 header.flags &= ~edge_middle;
300 }
301
302 if (i == n - 1 && edgeflags[i])
303 header.flags |= edge_last;
304
305 if (DEBUG_CLIP) {
306 uint j, k;
307 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
308 stage->draw->rasterizer->flatshade_first);
309 for (j = 0; j < 3; j++) {
310 debug_printf(" Vert %d: clip: %f %f %f %f\n", j,
311 header.v[j]->clip[0],
312 header.v[j]->clip[1],
313 header.v[j]->clip[2],
314 header.v[j]->clip[3]);
315 for (k = 0; k < draw_num_shader_outputs(stage->draw); k++) {
316 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
317 header.v[j]->data[k][0],
318 header.v[j]->data[k][1],
319 header.v[j]->data[k][2],
320 header.v[j]->data[k][3]);
321 }
322 }
323 }
324 stage->next->tri( stage->next, &header );
325 }
326 }
327
328
329 static INLINE float
330 dot4(const float *a, const float *b)
331 {
332 return (a[0] * b[0] +
333 a[1] * b[1] +
334 a[2] * b[2] +
335 a[3] * b[3]);
336 }
337
338 /*
339 * this function extracts the clip distance for the current plane,
340 * it first checks if the shader provided a clip distance, otherwise
341 * it works out the value using the clipvertex
342 */
343 static INLINE float getclipdist(const struct clip_stage *clipper,
344 struct vertex_header *vert,
345 int plane_idx)
346 {
347 const float *plane;
348 float dp;
349 if (vert->have_clipdist && plane_idx >= 6) {
350 /* pick the correct clipdistance element from the output vectors */
351 int _idx = plane_idx - 6;
352 int cdi = _idx >= 4;
353 int vidx = cdi ? _idx - 4 : _idx;
354 dp = vert->data[draw_current_shader_clipdistance_output(clipper->stage.draw, cdi)][vidx];
355 } else {
356 plane = clipper->plane[plane_idx];
357 dp = dot4(vert->clip, plane);
358 }
359 return dp;
360 }
361
362 /* Clip a triangle against the viewport and user clip planes.
363 */
364 static void
365 do_clip_tri( struct draw_stage *stage,
366 struct prim_header *header,
367 unsigned clipmask )
368 {
369 struct clip_stage *clipper = clip_stage( stage );
370 struct vertex_header *a[MAX_CLIPPED_VERTICES];
371 struct vertex_header *b[MAX_CLIPPED_VERTICES];
372 struct vertex_header **inlist = a;
373 struct vertex_header **outlist = b;
374 unsigned tmpnr = 0;
375 unsigned n = 3;
376 unsigned i;
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;
382
383 inlist[0] = header->v[0];
384 inlist[1] = header->v[1];
385 inlist[2] = header->v[2];
386
387 viewport_index = draw_viewport_index(clipper->stage.draw, inlist[0]);
388
389 if (DEBUG_CLIP) {
390 const float *v0 = header->v[0]->clip;
391 const float *v1 = header->v[1]->clip;
392 const float *v2 = header->v[2]->clip;
393 debug_printf("Clip triangle:\n");
394 debug_printf(" %f, %f, %f, %f\n", v0[0], v0[1], v0[2], v0[3]);
395 debug_printf(" %f, %f, %f, %f\n", v1[0], v1[1], v1[2], v1[3]);
396 debug_printf(" %f, %f, %f, %f\n", v2[0], v2[1], v2[2], v2[3]);
397 }
398
399 /*
400 * Note: at this point we can't just use the per-vertex edge flags.
401 * We have to observe the edge flag bits set in header->flags which
402 * were set during primitive decomposition. Put those flags into
403 * an edge flags array which parallels the vertex array.
404 * Later, in the 'unfilled' pipeline stage we'll draw the edge if both
405 * the header.flags bit is set AND the per-vertex edgeflag field is set.
406 */
407 inEdges[0] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_0);
408 inEdges[1] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_1);
409 inEdges[2] = !!(header->flags & DRAW_PIPE_EDGE_FLAG_2);
410
411 while (clipmask && n >= 3) {
412 const unsigned plane_idx = ffs(clipmask)-1;
413 const boolean is_user_clip_plane = plane_idx >= 6;
414 struct vertex_header *vert_prev = inlist[0];
415 boolean *edge_prev = &inEdges[0];
416 float dp_prev;
417 unsigned outcount = 0;
418
419 dp_prev = getclipdist(clipper, vert_prev, plane_idx);
420 clipmask &= ~(1<<plane_idx);
421
422 if (util_is_inf_or_nan(dp_prev))
423 return; //discard nan
424
425 assert(n < MAX_CLIPPED_VERTICES);
426 if (n >= MAX_CLIPPED_VERTICES)
427 return;
428 inlist[n] = inlist[0]; /* prevent rotation of vertices */
429 inEdges[n] = inEdges[0];
430
431 for (i = 1; i <= n; i++) {
432 struct vertex_header *vert = inlist[i];
433 boolean *edge = &inEdges[i];
434
435 float dp = getclipdist(clipper, vert, plane_idx);
436
437 if (util_is_inf_or_nan(dp))
438 return; //discard nan
439
440 if (!IS_NEGATIVE(dp_prev)) {
441 assert(outcount < MAX_CLIPPED_VERTICES);
442 if (outcount >= MAX_CLIPPED_VERTICES)
443 return;
444 outEdges[outcount] = *edge_prev;
445 outlist[outcount++] = vert_prev;
446 }
447
448 if (DIFFERENT_SIGNS(dp, dp_prev)) {
449 struct vertex_header *new_vert;
450 boolean *new_edge;
451
452 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
453 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
454 return;
455 new_vert = clipper->stage.tmp[tmpnr++];
456
457 assert(outcount < MAX_CLIPPED_VERTICES);
458 if (outcount >= MAX_CLIPPED_VERTICES)
459 return;
460
461 new_edge = &outEdges[outcount];
462 outlist[outcount++] = new_vert;
463
464 if (IS_NEGATIVE(dp)) {
465 /* Going out of bounds. Avoid division by zero as we
466 * know dp != dp_prev from DIFFERENT_SIGNS, above.
467 */
468 float t = dp / (dp - dp_prev);
469 interp( clipper, new_vert, t, vert, vert_prev, viewport_index );
470
471 /* Whether or not to set edge flag for the new vert depends
472 * on whether it's a user-defined clipping plane. We're
473 * copying NVIDIA's behaviour here.
474 */
475 if (is_user_clip_plane) {
476 /* we want to see an edge along the clip plane */
477 *new_edge = TRUE;
478 new_vert->edgeflag = TRUE;
479 }
480 else {
481 /* we don't want to see an edge along the frustum clip plane */
482 *new_edge = *edge_prev;
483 new_vert->edgeflag = FALSE;
484 }
485 }
486 else {
487 /* Coming back in.
488 */
489 float t = dp_prev / (dp_prev - dp);
490 interp( clipper, new_vert, t, vert_prev, vert, viewport_index );
491
492 /* Copy starting vert's edgeflag:
493 */
494 new_vert->edgeflag = vert_prev->edgeflag;
495 *new_edge = *edge_prev;
496 }
497 }
498
499 vert_prev = vert;
500 edge_prev = edge;
501 dp_prev = dp;
502 }
503
504 /* swap in/out lists */
505 {
506 struct vertex_header **tmp = inlist;
507 inlist = outlist;
508 outlist = tmp;
509 n = outcount;
510 }
511 {
512 boolean *tmp = inEdges;
513 inEdges = outEdges;
514 outEdges = tmp;
515 }
516
517 }
518
519 /* If flat-shading, copy provoking vertex color to polygon vertex[0]
520 */
521 if (n >= 3) {
522 if (clipper->num_flat_attribs) {
523 if (stage->draw->rasterizer->flatshade_first) {
524 if (inlist[0] != header->v[0]) {
525 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
526 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
527 return;
528 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
529 copy_flat(stage, inlist[0], header->v[0]);
530 }
531 }
532 else {
533 if (inlist[0] != header->v[2]) {
534 assert(tmpnr < MAX_CLIPPED_VERTICES + 1);
535 if (tmpnr >= MAX_CLIPPED_VERTICES + 1)
536 return;
537 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
538 copy_flat(stage, inlist[0], header->v[2]);
539 }
540 }
541 }
542
543 /* Emit the polygon as triangles to the setup stage:
544 */
545 emit_poly( stage, inlist, inEdges, n, header );
546 }
547 }
548
549
550 /* Clip a line against the viewport and user clip planes.
551 */
552 static void
553 do_clip_line( struct draw_stage *stage,
554 struct prim_header *header,
555 unsigned clipmask )
556 {
557 const struct clip_stage *clipper = clip_stage( stage );
558 struct vertex_header *v0 = header->v[0];
559 struct vertex_header *v1 = header->v[1];
560 float t0 = 0.0F;
561 float t1 = 0.0F;
562 struct prim_header newprim;
563 int viewport_index = draw_viewport_index(clipper->stage.draw, v0);
564
565 while (clipmask) {
566 const unsigned plane_idx = ffs(clipmask)-1;
567 const float dp0 = getclipdist(clipper, v0, plane_idx);
568 const float dp1 = getclipdist(clipper, v1, plane_idx);
569
570 if (util_is_inf_or_nan(dp0) || util_is_inf_or_nan(dp1))
571 return; //discard nan
572
573 if (dp1 < 0.0F) {
574 float t = dp1 / (dp1 - dp0);
575 t1 = MAX2(t1, t);
576 }
577
578 if (dp0 < 0.0F) {
579 float t = dp0 / (dp0 - dp1);
580 t0 = MAX2(t0, t);
581 }
582
583 if (t0 + t1 >= 1.0F)
584 return; /* discard */
585
586 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
587 }
588
589 if (v0->clipmask) {
590 interp( clipper, stage->tmp[0], t0, v0, v1, viewport_index );
591 copy_flat(stage, stage->tmp[0], v0);
592 newprim.v[0] = stage->tmp[0];
593 }
594 else {
595 newprim.v[0] = v0;
596 }
597
598 if (v1->clipmask) {
599 interp( clipper, stage->tmp[1], t1, v1, v0, viewport_index );
600 newprim.v[1] = stage->tmp[1];
601 }
602 else {
603 newprim.v[1] = v1;
604 }
605
606 stage->next->line( stage->next, &newprim );
607 }
608
609
610 static void
611 clip_point( struct draw_stage *stage,
612 struct prim_header *header )
613 {
614 if (header->v[0]->clipmask == 0)
615 stage->next->point( stage->next, header );
616 }
617
618 /*
619 * Clip points but ignore the first 4 (xy) clip planes.
620 * (This is necessary because we don't generate a different shader variant
621 * just for points hence xy clip bits are still generated. This is not really
622 * optimal because of the extra calculations both in generating clip masks
623 * and executing the clip stage but it gets the job done.)
624 */
625 static void
626 clip_point_no_xy( struct draw_stage *stage,
627 struct prim_header *header )
628 {
629 if ((header->v[0]->clipmask & 0xfffffff0) == 0)
630 stage->next->point( stage->next, header );
631 }
632
633
634
635 static void
636 clip_first_point( struct draw_stage *stage,
637 struct prim_header *header )
638 {
639 stage->point = stage->draw->clip_points_xy ? clip_point : clip_point_no_xy;
640 stage->point(stage, header);
641 }
642
643
644 static void
645 clip_line( struct draw_stage *stage,
646 struct prim_header *header )
647 {
648 unsigned clipmask = (header->v[0]->clipmask |
649 header->v[1]->clipmask);
650
651 if (clipmask == 0) {
652 /* no clipping needed */
653 stage->next->line( stage->next, header );
654 }
655 else if ((header->v[0]->clipmask &
656 header->v[1]->clipmask) == 0) {
657 do_clip_line(stage, header, clipmask);
658 }
659 /* else, totally clipped */
660 }
661
662
663 static void
664 clip_tri( struct draw_stage *stage,
665 struct prim_header *header )
666 {
667 unsigned clipmask = (header->v[0]->clipmask |
668 header->v[1]->clipmask |
669 header->v[2]->clipmask);
670
671 if (clipmask == 0) {
672 /* no clipping needed */
673 stage->next->tri( stage->next, header );
674 }
675 else if ((header->v[0]->clipmask &
676 header->v[1]->clipmask &
677 header->v[2]->clipmask) == 0) {
678 do_clip_tri(stage, header, clipmask);
679 }
680 }
681
682
683 static int
684 find_interp(const struct draw_fragment_shader *fs, int *indexed_interp,
685 uint semantic_name, uint semantic_index)
686 {
687 int interp;
688 /* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode
689 * from the array we've filled before. */
690 if (semantic_name == TGSI_SEMANTIC_COLOR ||
691 semantic_name == TGSI_SEMANTIC_BCOLOR) {
692 interp = indexed_interp[semantic_index];
693 } else {
694 /* Otherwise, search in the FS inputs, with a decent default
695 * if we don't find it.
696 */
697 uint j;
698 interp = TGSI_INTERPOLATE_PERSPECTIVE;
699 if (fs) {
700 for (j = 0; j < fs->info.num_inputs; j++) {
701 if (semantic_name == fs->info.input_semantic_name[j] &&
702 semantic_index == fs->info.input_semantic_index[j]) {
703 interp = fs->info.input_interpolate[j];
704 break;
705 }
706 }
707 }
708 }
709 return interp;
710 }
711
712 /* Update state. Could further delay this until we hit the first
713 * primitive that really requires clipping.
714 */
715 static void
716 clip_init_state( struct draw_stage *stage )
717 {
718 struct clip_stage *clipper = clip_stage( stage );
719 const struct draw_fragment_shader *fs = stage->draw->fs.fragment_shader;
720 uint i, j;
721 const struct tgsi_shader_info *info = draw_get_shader_info(stage->draw);
722
723 /* We need to know for each attribute what kind of interpolation is
724 * done on it (flat, smooth or noperspective). But the information
725 * is not directly accessible for outputs, only for inputs. So we
726 * have to match semantic name and index between the VS (or GS/ES)
727 * outputs and the FS inputs to get to the interpolation mode.
728 *
729 * The only hitch is with gl_FrontColor/gl_BackColor which map to
730 * gl_Color, and their Secondary versions. First there are (up to)
731 * two outputs for one input, so we tuck the information in a
732 * specific array. Second if they don't have qualifiers, the
733 * default value has to be picked from the global shade mode.
734 *
735 * Of course, if we don't have a fragment shader in the first
736 * place, defaults should be used.
737 */
738
739 /* First pick up the interpolation mode for
740 * gl_Color/gl_SecondaryColor, with the correct default.
741 */
742 int indexed_interp[2];
743 indexed_interp[0] = indexed_interp[1] = stage->draw->rasterizer->flatshade ?
744 TGSI_INTERPOLATE_CONSTANT : TGSI_INTERPOLATE_PERSPECTIVE;
745
746 if (fs) {
747 for (i = 0; i < fs->info.num_inputs; i++) {
748 if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_COLOR) {
749 if (fs->info.input_interpolate[i] != TGSI_INTERPOLATE_COLOR)
750 indexed_interp[fs->info.input_semantic_index[i]] = fs->info.input_interpolate[i];
751 }
752 }
753 }
754
755 /* Then resolve the interpolation mode for every output attribute.
756 *
757 * Given how the rest of the code, the most efficient way is to
758 * have a vector of flat-mode attributes, and a mask for
759 * noperspective attributes.
760 */
761
762 clipper->num_flat_attribs = 0;
763 memset(clipper->noperspective_attribs, 0, sizeof(clipper->noperspective_attribs));
764 for (i = 0; i < info->num_outputs; i++) {
765 /* Find the interpolation mode for a specific attribute */
766 int interp = find_interp(fs, indexed_interp,
767 info->output_semantic_name[i],
768 info->output_semantic_index[i]);
769 /* If it's flat, add it to the flat vector. Otherwise update
770 * the noperspective mask.
771 */
772
773 if (interp == TGSI_INTERPOLATE_CONSTANT) {
774 clipper->flat_attribs[clipper->num_flat_attribs] = i;
775 clipper->num_flat_attribs++;
776 } else
777 clipper->noperspective_attribs[i] = interp == TGSI_INTERPOLATE_LINEAR;
778 }
779 /* Search the extra vertex attributes */
780 for (j = 0; j < stage->draw->extra_shader_outputs.num; j++) {
781 /* Find the interpolation mode for a specific attribute */
782 int interp = find_interp(fs, indexed_interp,
783 stage->draw->extra_shader_outputs.semantic_name[j],
784 stage->draw->extra_shader_outputs.semantic_index[j]);
785 /* If it's flat, add it to the flat vector. Otherwise update
786 * the noperspective mask.
787 */
788 if (interp == TGSI_INTERPOLATE_CONSTANT) {
789 clipper->flat_attribs[clipper->num_flat_attribs] = i + j;
790 clipper->num_flat_attribs++;
791 } else
792 clipper->noperspective_attribs[i + j] = interp == TGSI_INTERPOLATE_LINEAR;
793 }
794
795 stage->tri = clip_tri;
796 stage->line = clip_line;
797 }
798
799
800
801 static void clip_first_tri( struct draw_stage *stage,
802 struct prim_header *header )
803 {
804 clip_init_state( stage );
805 stage->tri( stage, header );
806 }
807
808 static void clip_first_line( struct draw_stage *stage,
809 struct prim_header *header )
810 {
811 clip_init_state( stage );
812 stage->line( stage, header );
813 }
814
815
816 static void clip_flush( struct draw_stage *stage,
817 unsigned flags )
818 {
819 stage->tri = clip_first_tri;
820 stage->line = clip_first_line;
821 stage->next->flush( stage->next, flags );
822 }
823
824
825 static void clip_reset_stipple_counter( struct draw_stage *stage )
826 {
827 stage->next->reset_stipple_counter( stage->next );
828 }
829
830
831 static void clip_destroy( struct draw_stage *stage )
832 {
833 draw_free_temp_verts( stage );
834 FREE( stage );
835 }
836
837
838 /**
839 * Allocate a new clipper stage.
840 * \return pointer to new stage object
841 */
842 struct draw_stage *draw_clip_stage( struct draw_context *draw )
843 {
844 struct clip_stage *clipper = CALLOC_STRUCT(clip_stage);
845 if (clipper == NULL)
846 goto fail;
847
848 clipper->stage.draw = draw;
849 clipper->stage.name = "clipper";
850 clipper->stage.point = clip_first_point;
851 clipper->stage.line = clip_first_line;
852 clipper->stage.tri = clip_first_tri;
853 clipper->stage.flush = clip_flush;
854 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
855 clipper->stage.destroy = clip_destroy;
856
857 clipper->plane = draw->plane;
858
859 if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
860 goto fail;
861
862 return &clipper->stage;
863
864 fail:
865 if (clipper)
866 clipper->stage.destroy( &clipper->stage );
867
868 return NULL;
869 }