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