gallium/draw: whitespace and comments
[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 clipper {
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 clipper *clipper_stage( struct draw_stage *stage )
74 {
75 return (struct clipper *)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 clipper *clipper = clipper_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 clipper *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 static void emit_poly( struct draw_stage *stage,
161 struct vertex_header **inlist,
162 unsigned n,
163 const struct prim_header *origPrim)
164 {
165 struct prim_header header;
166 unsigned i;
167
168 const ushort edge_first = DRAW_PIPE_EDGE_FLAG_2;
169 const ushort edge_middle = DRAW_PIPE_EDGE_FLAG_0;
170 const ushort edge_last = DRAW_PIPE_EDGE_FLAG_1;
171
172 /* later stages may need the determinant, but only the sign matters */
173 header.det = origPrim->det;
174 header.flags = DRAW_PIPE_RESET_STIPPLE | edge_first | edge_middle;
175 header.pad = 0;
176
177 for (i = 2; i < n; i++, header.flags = edge_middle) {
178 header.v[0] = inlist[i-1];
179 header.v[1] = inlist[i];
180 header.v[2] = inlist[0]; /* keep in v[2] for flatshading */
181
182 if (i == n-1)
183 header.flags |= edge_last;
184
185 if (0) {
186 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
187 uint j, k;
188 debug_printf("Clipped tri:\n");
189 for (j = 0; j < 3; j++) {
190 for (k = 0; k < vs->info.num_outputs; k++) {
191 debug_printf(" Vert %d: Attr %d: %f %f %f %f\n", j, k,
192 header.v[j]->data[k][0],
193 header.v[j]->data[k][1],
194 header.v[j]->data[k][2],
195 header.v[j]->data[k][3]);
196 }
197 }
198 }
199
200 stage->next->tri( stage->next, &header );
201 }
202 }
203
204
205 static INLINE float
206 dot4(const float *a, const float *b)
207 {
208 return (a[0] * b[0] +
209 a[1] * b[1] +
210 a[2] * b[2] +
211 a[3] * b[3]);
212 }
213
214
215 /* Clip a triangle against the viewport and user clip planes.
216 */
217 static void
218 do_clip_tri( struct draw_stage *stage,
219 struct prim_header *header,
220 unsigned clipmask )
221 {
222 struct clipper *clipper = clipper_stage( stage );
223 struct vertex_header *a[MAX_CLIPPED_VERTICES];
224 struct vertex_header *b[MAX_CLIPPED_VERTICES];
225 struct vertex_header **inlist = a;
226 struct vertex_header **outlist = b;
227 unsigned tmpnr = 0;
228 unsigned n = 3;
229 unsigned i;
230
231 inlist[0] = header->v[0];
232 inlist[1] = header->v[1];
233 inlist[2] = header->v[2];
234
235 while (clipmask && n >= 3) {
236 const unsigned plane_idx = ffs(clipmask)-1;
237 const float *plane = clipper->plane[plane_idx];
238 struct vertex_header *vert_prev = inlist[0];
239 float dp_prev = dot4( vert_prev->clip, plane );
240 unsigned outcount = 0;
241
242 clipmask &= ~(1<<plane_idx);
243
244 inlist[n] = inlist[0]; /* prevent rotation of vertices */
245
246 for (i = 1; i <= n; i++) {
247 struct vertex_header *vert = inlist[i];
248
249 float dp = dot4( vert->clip, plane );
250
251 if (!IS_NEGATIVE(dp_prev)) {
252 outlist[outcount++] = vert_prev;
253 }
254
255 if (DIFFERENT_SIGNS(dp, dp_prev)) {
256 struct vertex_header *new_vert = clipper->stage.tmp[tmpnr++];
257 outlist[outcount++] = new_vert;
258
259 if (IS_NEGATIVE(dp)) {
260 /* Going out of bounds. Avoid division by zero as we
261 * know dp != dp_prev from DIFFERENT_SIGNS, above.
262 */
263 float t = dp / (dp - dp_prev);
264 interp( clipper, new_vert, t, vert, vert_prev );
265
266 /* Force edgeflag true in this case:
267 */
268 new_vert->edgeflag = 1;
269 } else {
270 /* Coming back in.
271 */
272 float t = dp_prev / (dp_prev - dp);
273 interp( clipper, new_vert, t, vert_prev, vert );
274
275 /* Copy starting vert's edgeflag:
276 */
277 new_vert->edgeflag = vert_prev->edgeflag;
278 }
279 }
280
281 vert_prev = vert;
282 dp_prev = dp;
283 }
284
285 /* swap in/out lists */
286 {
287 struct vertex_header **tmp = inlist;
288 inlist = outlist;
289 outlist = tmp;
290 n = outcount;
291 }
292 }
293
294 /* If flat-shading, copy color to new provoking vertex.
295 */
296 if (clipper->flat && inlist[0] != header->v[2]) {
297 inlist[0] = dup_vert(stage, inlist[0], tmpnr++);
298
299 copy_colors(stage, inlist[0], header->v[2]);
300 }
301
302 /* Emit the polygon as triangles to the setup stage:
303 */
304 if (n >= 3)
305 emit_poly( stage, inlist, n, header );
306 }
307
308
309 /* Clip a line against the viewport and user clip planes.
310 */
311 static void
312 do_clip_line( struct draw_stage *stage,
313 struct prim_header *header,
314 unsigned clipmask )
315 {
316 const struct clipper *clipper = clipper_stage( stage );
317 struct vertex_header *v0 = header->v[0];
318 struct vertex_header *v1 = header->v[1];
319 const float *pos0 = v0->clip;
320 const float *pos1 = v1->clip;
321 float t0 = 0.0F;
322 float t1 = 0.0F;
323 struct prim_header newprim;
324
325 while (clipmask) {
326 const unsigned plane_idx = ffs(clipmask)-1;
327 const float *plane = clipper->plane[plane_idx];
328 const float dp0 = dot4( pos0, plane );
329 const float dp1 = dot4( pos1, plane );
330
331 if (dp1 < 0.0F) {
332 float t = dp1 / (dp1 - dp0);
333 t1 = MAX2(t1, t);
334 }
335
336 if (dp0 < 0.0F) {
337 float t = dp0 / (dp0 - dp1);
338 t0 = MAX2(t0, t);
339 }
340
341 if (t0 + t1 >= 1.0F)
342 return; /* discard */
343
344 clipmask &= ~(1 << plane_idx); /* turn off this plane's bit */
345 }
346
347 if (v0->clipmask) {
348 interp( clipper, stage->tmp[0], t0, v0, v1 );
349
350 if (clipper->flat)
351 copy_colors(stage, stage->tmp[0], v0);
352
353 newprim.v[0] = stage->tmp[0];
354 }
355 else {
356 newprim.v[0] = v0;
357 }
358
359 if (v1->clipmask) {
360 interp( clipper, stage->tmp[1], t1, v1, v0 );
361 newprim.v[1] = stage->tmp[1];
362 }
363 else {
364 newprim.v[1] = v1;
365 }
366
367 stage->next->line( stage->next, &newprim );
368 }
369
370
371 static void
372 clip_point( struct draw_stage *stage,
373 struct prim_header *header )
374 {
375 if (header->v[0]->clipmask == 0)
376 stage->next->point( stage->next, header );
377 }
378
379
380 static void
381 clip_line( struct draw_stage *stage,
382 struct prim_header *header )
383 {
384 unsigned clipmask = (header->v[0]->clipmask |
385 header->v[1]->clipmask);
386
387 if (clipmask == 0) {
388 /* no clipping needed */
389 stage->next->line( stage->next, header );
390 }
391 else if ((header->v[0]->clipmask &
392 header->v[1]->clipmask) == 0) {
393 do_clip_line(stage, header, clipmask);
394 }
395 /* else, totally clipped */
396 }
397
398
399 static void
400 clip_tri( struct draw_stage *stage,
401 struct prim_header *header )
402 {
403 unsigned clipmask = (header->v[0]->clipmask |
404 header->v[1]->clipmask |
405 header->v[2]->clipmask);
406
407 if (clipmask == 0) {
408 /* no clipping needed */
409 stage->next->tri( stage->next, header );
410 }
411 else if ((header->v[0]->clipmask &
412 header->v[1]->clipmask &
413 header->v[2]->clipmask) == 0) {
414 do_clip_tri(stage, header, clipmask);
415 }
416 }
417
418
419 /* Update state. Could further delay this until we hit the first
420 * primitive that really requires clipping.
421 */
422 static void
423 clip_init_state( struct draw_stage *stage )
424 {
425 struct clipper *clipper = clipper_stage( stage );
426
427 clipper->flat = stage->draw->rasterizer->flatshade ? TRUE : FALSE;
428
429 if (clipper->flat) {
430 const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
431 uint i;
432
433 clipper->num_color_attribs = 0;
434 for (i = 0; i < vs->info.num_outputs; i++) {
435 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR ||
436 vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
437 clipper->color_attribs[clipper->num_color_attribs++] = i;
438 }
439 }
440 }
441
442 stage->tri = clip_tri;
443 stage->line = clip_line;
444 }
445
446
447
448 static void clip_first_tri( struct draw_stage *stage,
449 struct prim_header *header )
450 {
451 clip_init_state( stage );
452 stage->tri( stage, header );
453 }
454
455 static void clip_first_line( struct draw_stage *stage,
456 struct prim_header *header )
457 {
458 clip_init_state( stage );
459 stage->line( stage, header );
460 }
461
462
463 static void clip_flush( struct draw_stage *stage,
464 unsigned flags )
465 {
466 stage->tri = clip_first_tri;
467 stage->line = clip_first_line;
468 stage->next->flush( stage->next, flags );
469 }
470
471
472 static void clip_reset_stipple_counter( struct draw_stage *stage )
473 {
474 stage->next->reset_stipple_counter( stage->next );
475 }
476
477
478 static void clip_destroy( struct draw_stage *stage )
479 {
480 draw_free_temp_verts( stage );
481 FREE( stage );
482 }
483
484
485 /**
486 * Allocate a new clipper stage.
487 * \return pointer to new stage object
488 */
489 struct draw_stage *draw_clip_stage( struct draw_context *draw )
490 {
491 struct clipper *clipper = CALLOC_STRUCT(clipper);
492 if (clipper == NULL)
493 goto fail;
494
495 if (!draw_alloc_temp_verts( &clipper->stage, MAX_CLIPPED_VERTICES+1 ))
496 goto fail;
497
498 clipper->stage.draw = draw;
499 clipper->stage.name = "clipper";
500 clipper->stage.point = clip_point;
501 clipper->stage.line = clip_first_line;
502 clipper->stage.tri = clip_first_tri;
503 clipper->stage.flush = clip_flush;
504 clipper->stage.reset_stipple_counter = clip_reset_stipple_counter;
505 clipper->stage.destroy = clip_destroy;
506
507 clipper->plane = draw->plane;
508
509 return &clipper->stage;
510
511 fail:
512 if (clipper)
513 clipper->stage.destroy( &clipper->stage );
514
515 return NULL;
516 }