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