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