gallium/draw: additional comments in the clipping code
[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 "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
43
44 #ifndef IS_NEGATIVE
45 #define IS_NEGATIVE(X) ((X) < 0.0)
46 #endif
47
48 #ifndef DIFFERENT_SIGNS
49 #define DIFFERENT_SIGNS(x, y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
50 #endif
51
52 #ifndef MAX_CLIPPED_VERTICES
53 #define MAX_CLIPPED_VERTICES ((2 * (6 + PIPE_MAX_CLIP_PLANES))+1)
54 #endif
55
56
57
58 struct clip_stage {
59 struct draw_stage stage; /**< base class */
60
61 /* Basically duplicate some of the flatshading logic here:
62 */
63 boolean flat;
64 uint num_color_attribs;
65 uint color_attribs[4]; /* front/back primary/secondary colors */
66
67 float (*plane)[4];
68 };
69
70
71 /* This is a bit confusing:
72 */
73 static INLINE struct clip_stage *clip_stage( struct draw_stage *stage )
74 {
75 return (struct clip_stage *)stage;
76 }
77
78
79 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
80
81
82 /* All attributes are float[4], so this is easy:
83 */
84 static void interp_attr( float *fdst,
85 float t,
86 const float *fin,
87 const float *fout )
88 {
89 fdst[0] = LINTERP( t, fout[0], fin[0] );
90 fdst[1] = LINTERP( t, fout[1], fin[1] );
91 fdst[2] = LINTERP( t, fout[2], fin[2] );
92 fdst[3] = LINTERP( t, fout[3], fin[3] );
93 }
94
95
96 static void copy_colors( struct draw_stage *stage,
97 struct vertex_header *dst,
98 const struct vertex_header *src )
99 {
100 const struct clip_stage *clipper = clip_stage(stage);
101 uint i;
102 for (i = 0; i < clipper->num_color_attribs; i++) {
103 const uint attr = clipper->color_attribs[i];
104 COPY_4FV(dst->data[attr], src->data[attr]);
105 }
106 }
107
108
109
110 /* Interpolate between two vertices to produce a third.
111 */
112 static void interp( const struct clip_stage *clip,
113 struct vertex_header *dst,
114 float t,
115 const struct vertex_header *out,
116 const struct vertex_header *in )
117 {
118 const unsigned nr_attrs = draw_current_shader_outputs(clip->stage.draw);
119 const unsigned pos_attr = draw_current_shader_position_output(clip->stage.draw);
120 unsigned j;
121
122 /* Vertex header.
123 */
124 {
125 dst->clipmask = 0;
126 dst->edgeflag = 0; /* will get overwritten later */
127 dst->pad = 0;
128 dst->vertex_id = UNDEFINED_VERTEX_ID;
129 }
130
131 /* Clip coordinates: interpolate normally
132 */
133 {
134 interp_attr(dst->clip, t, in->clip, out->clip);
135 }
136
137 /* Do the projective divide and insert window coordinates:
138 */
139 {
140 const float *pos = dst->clip;
141 const float *scale = clip->stage.draw->viewport.scale;
142 const float *trans = clip->stage.draw->viewport.translate;
143 const float oow = 1.0f / pos[3];
144
145 dst->data[pos_attr][0] = pos[0] * oow * scale[0] + trans[0];
146 dst->data[pos_attr][1] = pos[1] * oow * scale[1] + trans[1];
147 dst->data[pos_attr][2] = pos[2] * oow * scale[2] + trans[2];
148 dst->data[pos_attr][3] = oow;
149 }
150
151 /* Other attributes
152 */
153 for (j = 0; j < nr_attrs; j++) {
154 if (j != pos_attr)
155 interp_attr(dst->data[j], t, in->data[j], out->data[j]);
156 }
157 }
158
159
160 /**
161 * Emit a post-clip polygon to the next pipeline stage. The polygon
162 * will be convex and the provoking vertex will always be vertex[0].
163 */
164 static void emit_poly( struct draw_stage *stage,
165 struct vertex_header **inlist,
166 unsigned n,
167 const struct prim_header *origPrim)
168 {
169 struct prim_header header;
170 unsigned i;
171 ushort edge_first, edge_middle, edge_last;
172
173 if (stage->draw->rasterizer->flatshade_first) {
174 edge_first = DRAW_PIPE_EDGE_FLAG_0;
175 edge_middle = DRAW_PIPE_EDGE_FLAG_1;
176 edge_last = DRAW_PIPE_EDGE_FLAG_2;
177 }
178 else {
179 edge_first = DRAW_PIPE_EDGE_FLAG_2;
180 edge_middle = DRAW_PIPE_EDGE_FLAG_0;
181 edge_last = DRAW_PIPE_EDGE_FLAG_1;
182 }
183
184 /* later stages may need the determinant, but only the sign matters */
185 header.det = origPrim->det;
186 header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
187 header.pad = 0;
188
189 for (i = 2; i < n; i++, header.flags = edge_middle) {
190 /* order the triangle verts to respect the provoking vertex mode */
191 if (stage->draw->rasterizer->flatshade_first) {
192 header.v[0] = inlist[0]; /* the provoking vertex */
193 header.v[1] = inlist[i-1];
194 header.v[2] = inlist[i];
195 }
196 else {
197 header.v[0] = inlist[i-1];
198 header.v[1] = inlist[i];
199 header.v[2] = inlist[0]; /* the provoking vertex */
200 }
201
202 if (i == n-1)
203 header.flags |= edge_last;
204
205 if (0) {
206 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
207 uint j, k;
208 debug_printf("Clipped tri: (flat-shade-first = %d)\n",
209 stage->draw->rasterizer->flatshade_first);
210 for (j = 0; j < 3; j++) {
211 for (k = 0; k < vs->info.num_outputs; k++) {
212 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
213 header.v[j]->data[k][0],
214 header.v[j]->data[k][1],
215 header.v[j]->data[k][2],
216 header.v[j]->data[k][3]);
217 }
218 }
219 }
220
221 stage->next->tri( stage->next, &header );
222 }
223 }
224
225
226 static INLINE float
227 dot4(const float *a, const float *b)
228 {
229 return (a[0] * b[0] +
230 a[1] * b[1] +
231 a[2] * b[2] +
232 a[3] * b[3]);
233 }
234
235
236 /* Clip a triangle against the viewport and user clip planes.
237 */
238 static void
239 do_clip_tri( struct draw_stage *stage,
240 struct prim_header *header,
241 unsigned clipmask )
242 {
243 struct clip_stage *clipper = clip_stage( stage );
244 struct vertex_header *a[MAX_CLIPPED_VERTICES];
245 struct vertex_header *b[MAX_CLIPPED_VERTICES];
246 struct vertex_header **inlist = a;
247 struct vertex_header **outlist = b;
248 unsigned tmpnr = 0;
249 unsigned n = 3;
250 unsigned i;
251
252 inlist[0] = header->v[0];
253 inlist[1] = header->v[1];
254 inlist[2] = header->v[2];
255
256 while (clipmask && n >= 3) {
257 const unsigned plane_idx = ffs(clipmask)-1;
258 const float *plane = clipper->plane[plane_idx];
259 struct vertex_header *vert_prev = inlist[0];
260 float dp_prev = dot4( vert_prev->clip, plane );
261 unsigned outcount = 0;
262
263 clipmask &= ~(1<<plane_idx);
264
265 inlist[n] = inlist[0]; /* prevent rotation of vertices */
266
267 for (i = 1; i <= n; i++) {
268 struct vertex_header *vert = inlist[i];
269
270 float dp = dot4( vert->clip, plane );
271
272 if (!IS_NEGATIVE(dp_prev)) {
273 outlist[outcount++] = vert_prev;
274 }
275
276 if (DIFFERENT_SIGNS(dp, dp_prev)) {
277 struct vertex_header *new_vert = clipper->stage.tmp[tmpnr++];
278 outlist[outcount++] = new_vert;
279
280 if (IS_NEGATIVE(dp)) {
281 /* Going out of bounds. Avoid division by zero as we
282 * know dp != dp_prev from DIFFERENT_SIGNS, above.
283 */
284 float t = dp / (dp - dp_prev);
285 interp( clipper, new_vert, t, vert, vert_prev );
286
287 /* Force edgeflag true in this case:
288 */
289 new_vert->edgeflag = 1;
290 } else {
291 /* Coming back in.
292 */
293 float t = dp_prev / (dp_prev - dp);
294 interp( clipper, new_vert, t, vert_prev, vert );
295
296 /* Copy starting vert's edgeflag:
297 */
298 new_vert->edgeflag = vert_prev->edgeflag;
299 }
300 }
301
302 vert_prev = vert;
303 dp_prev = dp;
304 }
305
306 /* swap in/out lists */
307 {
308 struct vertex_header **tmp = inlist;
309 inlist = outlist;
310 outlist = tmp;
311 n = outcount;
312 }
313 }
314
315 /* If flat-shading, copy provoking vertex color to polygon vertex[0]
316 */
317 if (clipper->flat) {
318 if (stage->draw->rasterizer->flatshade_first) {
319 if (inlist[0] != header->v[0]) {
320 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
321 copy_colors(stage, inlist[0], header->v[0]);
322 }
323 }
324 else {
325 if (inlist[0] != header->v[2]) {
326 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
327 copy_colors(stage, inlist[0], header->v[2]);
328 }
329 }
330 }
331
332 /* Emit the polygon as triangles to the setup stage:
333 */
334 if (n >= 3)
335 emit_poly( stage, inlist, n, header );
336 }
337
338
339 /* Clip a line against the viewport and user clip planes.
340 */
341 static void
342 do_clip_line( struct draw_stage *stage,
343 struct prim_header *header,
344 unsigned clipmask )
345 {
346 const struct clip_stage *clipper = clip_stage( stage );
347 struct vertex_header *v0 = header->v[0];
348 struct vertex_header *v1 = header->v[1];
349 const float *pos0 = v0->clip;
350 const float *pos1 = v1->clip;
351 float t0 = 0.0F;
352 float t1 = 0.0F;
353 struct prim_header newprim;
354
355 while (clipmask) {
356 const unsigned plane_idx = ffs(clipmask)-1;
357 const float *plane = clipper->plane[plane_idx];
358 const float dp0 = dot4( pos0, plane );
359 const float dp1 = dot4( pos1, plane );
360
361 if (dp1 < 0.0F) {
362 float t = dp1 / (dp1 - dp0);
363 t1 = MAX2(t1, t);
364 }
365
366 if (dp0 < 0.0F) {
367 float t = dp0 / (dp0 - dp1);
368 t0 = MAX2(t0, t);
369 }
370
371 if (t0 + t1 >= 1.0F)
372 return; /* discard */
373
374 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
375 }
376
377 if (v0->clipmask) {
378 interp( clipper, stage->tmp[0], t0, v0, v1 );
379
380 if (clipper->flat)
381 copy_colors(stage, stage->tmp[0], v0);
382
383 newprim.v[0] = stage->tmp[0];
384 }
385 else {
386 newprim.v[0] = v0;
387 }
388
389 if (v1->clipmask) {
390 interp( clipper, stage->tmp[1], t1, v1, v0 );
391 newprim.v[1] = stage->tmp[1];
392 }
393 else {
394 newprim.v[1] = v1;
395 }
396
397 stage->next->line( stage->next, &newprim );
398 }
399
400
401 static void
402 clip_point( struct draw_stage *stage,
403 struct prim_header *header )
404 {
405 if (header->v[0]->clipmask == 0)
406 stage->next->point( stage->next, header );
407 }
408
409
410 static void
411 clip_line( struct draw_stage *stage,
412 struct prim_header *header )
413 {
414 unsigned clipmask = (header->v[0]->clipmask |
415 header->v[1]->clipmask);
416
417 if (clipmask == 0) {
418 /* no clipping needed */
419 stage->next->line( stage->next, header );
420 }
421 else if ((header->v[0]->clipmask &
422 header->v[1]->clipmask) == 0) {
423 do_clip_line(stage, header, clipmask);
424 }
425 /* else, totally clipped */
426 }
427
428
429 static void
430 clip_tri( struct draw_stage *stage,
431 struct prim_header *header )
432 {
433 unsigned clipmask = (header->v[0]->clipmask |
434 header->v[1]->clipmask |
435 header->v[2]->clipmask);
436
437 if (clipmask == 0) {
438 /* no clipping needed */
439 stage->next->tri( stage->next, header );
440 }
441 else if ((header->v[0]->clipmask &
442 header->v[1]->clipmask &
443 header->v[2]->clipmask) == 0) {
444 do_clip_tri(stage, header, clipmask);
445 }
446 }
447
448
449 /* Update state. Could further delay this until we hit the first
450 * primitive that really requires clipping.
451 */
452 static void
453 clip_init_state( struct draw_stage *stage )
454 {
455 struct clip_stage *clipper = clip_stage( stage );
456
457 clipper->flat = stage->draw->rasterizer->flatshade ? TRUE : FALSE;
458
459 if (clipper->flat) {
460 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
461 uint i;
462
463 clipper->num_color_attribs = 0;
464 for (i = 0; i < vs->info.num_outputs; i++) {
465 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
466 vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
467 clipper->color_attribs[clipper->num_color_attribs++] = i;
468 }
469 }
470 }
471
472 stage->tri = clip_tri;
473 stage->line = clip_line;
474 }
475
476
477
478 static void clip_first_tri( struct draw_stage *stage,
479 struct prim_header *header )
480 {
481 clip_init_state( stage );
482 stage->tri( stage, header );
483 }
484
485 static void clip_first_line( struct draw_stage *stage,
486 struct prim_header *header )
487 {
488 clip_init_state( stage );
489 stage->line( stage, header );
490 }
491
492
493 static void clip_flush( struct draw_stage *stage,
494 unsigned flags )
495 {
496 stage->tri = clip_first_tri;
497 stage->line = clip_first_line;
498 stage->next->flush( stage->next, flags );
499 }
500
501
502 static void clip_reset_stipple_counter( struct draw_stage *stage )
503 {
504 stage->next->reset_stipple_counter( stage->next );
505 }
506
507
508 static void clip_destroy( struct draw_stage *stage )
509 {
510 draw_free_temp_verts( stage );
511 FREE( stage );
512 }
513
514
515 /**
516 * Allocate a new clipper stage.
517 * \return pointer to new stage object
518 */
519 struct draw_stage *draw_clip_stage( struct draw_context *draw )
520 {
521 struct clip_stage *clipper = CALLOC_STRUCT(clip_stage);
522 if (clipper == NULL)
523 goto fail;
524
525 if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
526 goto fail;
527
528 clipper->stage.draw = draw;
529 clipper->stage.name = "clipper";
530 clipper->stage.point = clip_point;
531 clipper->stage.line = clip_first_line;
532 clipper->stage.tri = clip_first_tri;
533 clipper->stage.flush = clip_flush;
534 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
535 clipper->stage.destroy = clip_destroy;
536
537 clipper->plane = draw->plane;
538
539 return &clipper->stage;
540
541 fail:
542 if (clipper)
543 clipper->stage.destroy( &clipper->stage );
544
545 return NULL;
546 }