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