Merge branch 'gallium-i915-current' into gallium-0.1
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_clip.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * \brief Clipping stage
30 *
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 */
33
34
35 #include "pipe/p_util.h"
36 #include "pipe/p_shader_tokens.h"
37
38 #include "draw_vs.h"
39 #include "draw_pipe.h"
40
41
42 #ifndef IS_NEGATIVE
43 #define IS_NEGATIVE(X) ((X) < 0.0)
44 #endif
45
46 #ifndef DIFFERENT_SIGNS
47 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
48 #endif
49
50 #ifndef MAX_CLIPPED_VERTICES
51 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
52 #endif
53
54
55
56 struct clipper {
57 struct draw_stage stage; /**< base class */
58
59 /* Basically duplicate some of the flatshading logic here:
60 */
61 boolean flat;
62 uint num_color_attribs;
63 uint color_attribs[4]; /* front/back primary/secondary colors */
64
65 float (*plane)[4];
66 };
67
68
69 /* This is a bit confusing:
70 */
71 static INLINE struct clipper *clipper_stage( struct draw_stage *stage )
72 {
73 return (struct clipper *)stage;
74 }
75
76
77 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
78
79
80 /* All attributes are float[4], so this is easy:
81 */
82 static void interp_attr( float *fdst,
83 float t,
84 const float *fin,
85 const float *fout )
86 {
87 fdst[0] = LINTERP( t, fout[0], fin[0] );
88 fdst[1] = LINTERP( t, fout[1], fin[1] );
89 fdst[2] = LINTERP( t, fout[2], fin[2] );
90 fdst[3] = LINTERP( t, fout[3], fin[3] );
91 }
92
93 static void copy_colors( struct draw_stage *stage,
94 struct vertex_header *dst,
95 const struct vertex_header *src )
96 {
97 const struct clipper *clipper = clipper_stage(stage);
98 uint i;
99 for (i = 0; i < clipper->num_color_attribs; i++) {
100 const uint attr = clipper->color_attribs[i];
101 COPY_4FV(dst->data[attr], src->data[attr]);
102 }
103 }
104
105
106
107 /* Interpolate between two vertices to produce a third.
108 */
109 static void interp( const struct clipper *clip,
110 struct vertex_header *dst,
111 float t,
112 const struct vertex_header *out,
113 const struct vertex_header *in )
114 {
115 const unsigned nr_attrs = clip->stage.draw->num_vs_outputs;
116 unsigned j;
117
118 /* Vertex header.
119 */
120 {
121 dst->clipmask = 0;
122 dst->edgeflag = 0; /* will get overwritten later */
123 dst->pad = 0;
124 dst->vertex_id = UNDEFINED_VERTEX_ID;
125 }
126
127 /* Clip coordinates: interpolate normally
128 */
129 {
130 interp_attr(dst->clip, t, in->clip, out->clip);
131 }
132
133 /* Do the projective divide and insert window coordinates:
134 */
135 {
136 const float *pos = dst->clip;
137 const float *scale = clip->stage.draw->viewport.scale;
138 const float *trans = clip->stage.draw->viewport.translate;
139 const float oow = 1.0f / pos[3];
140
141 dst->data[0][0] = pos[0] * oow * scale[0] + trans[0];
142 dst->data[0][1] = pos[1] * oow * scale[1] + trans[1];
143 dst->data[0][2] = pos[2] * oow * scale[2] + trans[2];
144 dst->data[0][3] = oow;
145 }
146
147 /* Other attributes
148 * Note: start at 1 to skip winpos (data[0]) since we just computed
149 * it above.
150 */
151 for (j = 1; j < nr_attrs; j++) {
152 interp_attr(dst->data[j], t, in->data[j], out->data[j]);
153 }
154 }
155
156
157 static void emit_poly( struct draw_stage *stage,
158 struct vertex_header **inlist,
159 unsigned n,
160 const struct prim_header *origPrim)
161 {
162 struct prim_header header;
163 unsigned i;
164
165 const ushort edge_first = DRAW_PIPE_EDGE_FLAG_2;
166 const ushort edge_middle = DRAW_PIPE_EDGE_FLAG_0;
167 const ushort edge_last = DRAW_PIPE_EDGE_FLAG_1;
168
169 /* later stages may need the determinant, but only the sign matters */
170 header.det = origPrim->det;
171 header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
172 header.pad = 0;
173
174 for (i = 2; i < n; i++, header.flags = 0) {
175 header.v[0] = inlist[i-1];
176 header.v[1] = inlist[i];
177 header.v[2] = inlist[0]; /* keep in v[2] for flatshading */
178
179 if (i == n-1)
180 header.flags |= edge_last;
181
182 if (0) {
183 const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
184 uint j, k;
185 debug_printf("Clipped tri:\n");
186 for (j = 0; j < 3; j++) {
187 for (k = 0; k < vs->info.num_outputs; k++) {
188 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
189 header.v[j]->data[k][0],
190 header.v[j]->data[k][1],
191 header.v[j]->data[k][2],
192 header.v[j]->data[k][3]);
193 }
194 }
195 }
196
197 stage->next->tri( stage->next, &header );
198 }
199 }
200
201 static INLINE float
202 dot4(const float *a, const float *b)
203 {
204 return (a[0]*b[0] +
205 a[1]*b[1] +
206 a[2]*b[2] +
207 a[3]*b[3]);
208 }
209
210
211 /* Clip a triangle against the viewport and user clip planes.
212 */
213 static void
214 do_clip_tri( struct draw_stage *stage,
215 struct prim_header *header,
216 unsigned clipmask )
217 {
218 struct clipper *clipper = clipper_stage( stage );
219 struct vertex_header *a[MAX_CLIPPED_VERTICES];
220 struct vertex_header *b[MAX_CLIPPED_VERTICES];
221 struct vertex_header **inlist = a;
222 struct vertex_header **outlist = b;
223 unsigned tmpnr = 0;
224 unsigned n = 3;
225 unsigned i;
226
227 inlist[0] = header->v[0];
228 inlist[1] = header->v[1];
229 inlist[2] = header->v[2];
230
231 while (clipmask && n >= 3) {
232 const unsigned plane_idx = ffs(clipmask)-1;
233 const float *plane = clipper->plane[plane_idx];
234 struct vertex_header *vert_prev = inlist[0];
235 float dp_prev = dot4( vert_prev->clip, plane );
236 unsigned outcount = 0;
237
238 clipmask &= ~(1<<plane_idx);
239
240 inlist[n] = inlist[0]; /* prevent rotation of vertices */
241
242 for (i = 1; i <= n; i++) {
243 struct vertex_header *vert = inlist[i];
244
245 float dp = dot4( vert->clip, plane );
246
247 if (!IS_NEGATIVE(dp_prev)) {
248 outlist[outcount++] = vert_prev;
249 }
250
251 if (DIFFERENT_SIGNS(dp, dp_prev)) {
252 struct vertex_header *new_vert = clipper->stage.tmp[tmpnr++];
253 outlist[outcount++] = new_vert;
254
255 if (IS_NEGATIVE(dp)) {
256 /* Going out of bounds. Avoid division by zero as we
257 * know dp != dp_prev from DIFFERENT_SIGNS, above.
258 */
259 float t = dp / (dp - dp_prev);
260 interp( clipper, new_vert, t, vert, vert_prev );
261
262 /* Force edgeflag true in this case:
263 */
264 new_vert->edgeflag = 1;
265 } else {
266 /* Coming back in.
267 */
268 float t = dp_prev / (dp_prev - dp);
269 interp( clipper, new_vert, t, vert_prev, vert );
270
271 /* Copy starting vert's edgeflag:
272 */
273 new_vert->edgeflag = vert_prev->edgeflag;
274 }
275 }
276
277 vert_prev = vert;
278 dp_prev = dp;
279 }
280
281 {
282 struct vertex_header **tmp = inlist;
283 inlist = outlist;
284 outlist = tmp;
285 n = outcount;
286 }
287 }
288
289 /* If flat-shading, copy color to new provoking vertex.
290 */
291 if (clipper->flat && inlist[0] != header->v[2]) {
292 if (1) {
293 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
294 }
295
296 copy_colors(stage, inlist[0], header->v[2]);
297 }
298
299
300
301 /* Emit the polygon as triangles to the setup stage:
302 */
303 if (n >= 3)
304 emit_poly( stage, inlist, n, header );
305 }
306
307
308 /* Clip a line against the viewport and user clip planes.
309 */
310 static void
311 do_clip_line( struct draw_stage *stage,
312 struct prim_header *header,
313 unsigned clipmask )
314 {
315 const struct clipper *clipper = clipper_stage( stage );
316 struct vertex_header *v0 = header->v[0];
317 struct vertex_header *v1 = header->v[1];
318 const float *pos0 = v0->clip;
319 const float *pos1 = v1->clip;
320 float t0 = 0.0F;
321 float t1 = 0.0F;
322 struct prim_header newprim;
323
324 while (clipmask) {
325 const unsigned plane_idx = ffs(clipmask)-1;
326 const float *plane = clipper->plane[plane_idx];
327 const float dp0 = dot4( pos0, plane );
328 const float dp1 = dot4( pos1, plane );
329
330 if (dp1 < 0.0F) {
331 float t = dp1 / (dp1 - dp0);
332 t1 = MAX2(t1, t);
333 }
334
335 if (dp0 < 0.0F) {
336 float t = dp0 / (dp0 - dp1);
337 t0 = MAX2(t0, t);
338 }
339
340 if (t0 + t1 >= 1.0F)
341 return; /* discard */
342
343 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
344 }
345
346 if (v0->clipmask) {
347 interp( clipper, stage->tmp[0], t0, v0, v1 );
348
349 if (clipper->flat)
350 copy_colors(stage, stage->tmp[0], v0);
351
352 newprim.v[0] = stage->tmp[0];
353 }
354 else {
355 newprim.v[0] = v0;
356 }
357
358 if (v1->clipmask) {
359 interp( clipper, stage->tmp[1], t1, v1, v0 );
360 newprim.v[1] = stage->tmp[1];
361 }
362 else {
363 newprim.v[1] = v1;
364 }
365
366 stage->next->line( stage->next, &newprim );
367 }
368
369
370 static void
371 clip_point( struct draw_stage *stage,
372 struct prim_header *header )
373 {
374 if (header->v[0]->clipmask == 0)
375 stage->next->point( stage->next, header );
376 }
377
378
379 static void
380 clip_line( struct draw_stage *stage,
381 struct prim_header *header )
382 {
383 unsigned clipmask = (header->v[0]->clipmask |
384 header->v[1]->clipmask);
385
386 if (clipmask == 0) {
387 /* no clipping needed */
388 stage->next->line( stage->next, header );
389 }
390 else if ((header->v[0]->clipmask &
391 header->v[1]->clipmask) == 0) {
392 do_clip_line(stage, header, clipmask);
393 }
394 /* else, totally clipped */
395 }
396
397
398 static void
399 clip_tri( struct draw_stage *stage,
400 struct prim_header *header )
401 {
402 unsigned clipmask = (header->v[0]->clipmask |
403 header->v[1]->clipmask |
404 header->v[2]->clipmask);
405
406 if (clipmask == 0) {
407 /* no clipping needed */
408 stage->next->tri( stage->next, header );
409 }
410 else if ((header->v[0]->clipmask &
411 header->v[1]->clipmask &
412 header->v[2]->clipmask) == 0) {
413 do_clip_tri(stage, header, clipmask);
414 }
415 }
416
417 /* Update state. Could further delay this until we hit the first
418 * primitive that really requires clipping.
419 */
420 static void
421 clip_init_state( struct draw_stage *stage )
422 {
423 struct clipper *clipper = clipper_stage( stage );
424
425 clipper->flat = stage->draw->rasterizer->flatshade ? TRUE : FALSE;
426
427 if (clipper->flat) {
428 const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
429 uint i;
430
431 clipper->num_color_attribs = 0;
432 for (i = 0; i < vs->info.num_outputs; i++) {
433 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
434 vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
435 clipper->color_attribs[clipper->num_color_attribs++] = i;
436 }
437 }
438 }
439
440 stage->tri = clip_tri;
441 stage->line = clip_line;
442 }
443
444
445
446 static void clip_first_tri( struct draw_stage *stage,
447 struct prim_header *header )
448 {
449 clip_init_state( stage );
450 stage->tri( stage, header );
451 }
452
453 static void clip_first_line( struct draw_stage *stage,
454 struct prim_header *header )
455 {
456 clip_init_state( stage );
457 stage->line( stage, header );
458 }
459
460
461 static void clip_flush( struct draw_stage *stage,
462 unsigned flags )
463 {
464 stage->tri = clip_first_tri;
465 stage->line = clip_first_line;
466 stage->next->flush( stage->next, flags );
467 }
468
469
470 static void clip_reset_stipple_counter( struct draw_stage *stage )
471 {
472 stage->next->reset_stipple_counter( stage->next );
473 }
474
475
476 static void clip_destroy( struct draw_stage *stage )
477 {
478 draw_free_temp_verts( stage );
479 FREE( stage );
480 }
481
482
483 /**
484 * Allocate a new clipper stage.
485 * \return pointer to new stage object
486 */
487 struct draw_stage *draw_clip_stage( struct draw_context *draw )
488 {
489 struct clipper *clipper = CALLOC_STRUCT(clipper);
490 if (clipper == NULL)
491 goto fail;
492
493 if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
494 goto fail;
495
496 clipper->stage.draw = draw;
497 clipper->stage.point = clip_point;
498 clipper->stage.line = clip_first_line;
499 clipper->stage.tri = clip_first_tri;
500 clipper->stage.flush = clip_flush;
501 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
502 clipper->stage.destroy = clip_destroy;
503
504 clipper->plane = draw->plane;
505
506 return &clipper->stage;
507
508 fail:
509 if (clipper)
510 clipper->stage.destroy( &clipper->stage );
511
512 return NULL;
513 }