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